3
3
# Mandos key generator - create a new OpenPGP key for a Mandos client
5
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
5
# Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
7
7
# This program is free software: you can redistribute it and/or modify
8
8
# it under the terms of the GNU General Public License as published by
20
20
# Contact the authors at <mandos@fukt.bsnet.se>.
25
25
KEYDIR="/etc/keys/mandos"
30
KEYNAME="`hostname --fqdn`"
30
KEYNAME="`hostname --fqdn 2>/dev/null || hostname`"
32
32
KEYCOMMENT="Mandos client key"
35
35
KEYCOMMENT_ORIG="$KEYCOMMENT"
38
if [ ! -d "$KEYDIR" ]; then
39
KEYDIR="/etc/mandos/keys"
39
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
40
--longoptions version,help,password,dir:,type:,length:,subtype:,sublength:,name:,email:,comment:,expire:,force \
43
TEMP=`getopt --options vhpF:d:t:l:s:L:n:e:c:x:f \
44
--longoptions version,help,password,passfile:,dir:,type:,length:,subtype:,sublength:,name:,email:,comment:,expire:,force \
41
45
--name "$0" -- "$@"`
49
53
$basename [ OPTIONS ]
50
54
Encrypted password creation:
51
55
$basename { -p | --password } [ --name NAME ] [ --dir DIR]
56
$basename { -F | --passfile } FILE [ --name NAME ] [ --dir DIR]
53
58
Key creation options:
54
59
-v, --version Show program's version number and exit
70
75
-x TIME, --expire TIME
71
76
Key expire time. Default is no expiration.
72
77
See gpg(1) for syntax.
73
-f, --force Force overwriting old keys.
78
-f, --force Force overwriting old key files.
75
80
Password creation options:
76
-p, --password Create an encrypted password using the keys in
81
-p, --password Create an encrypted password using the key in
82
the key directory. All options other than
83
--dir and --name are ignored.
84
-F FILE, --passfile FILE
85
Encrypt a password from FILE using the key in
77
86
the key directory. All options other than
78
87
--dir and --name are ignored.
85
94
-p|--password) mode=password; shift;;
95
-F|--passfile) mode=password; PASSFILE="$2"; shift 2;;
86
96
-d|--dir) KEYDIR="$2"; shift 2;;
87
97
-t|--type) KEYTYPE="$2"; shift 2;;
88
98
-s|--subtype) SUBKEYTYPE="$2"; shift 2;;
148
158
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
151
if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
152
&& [ "$FORCE" -eq 0 ]; then
161
if [ \( -e "$SECKEYFILE" -o -e "$PUBKEYFILE" \) \
162
-a "$FORCE" -eq 0 ]; then
153
163
echo "Refusing to overwrite old key files; use --force" >&2
231
241
FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
234
# Export keys from key rings to key files
244
# Export key from key rings to key files
235
245
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
236
246
--homedir "$RINGDIR" --armor --export-options export-minimal \
237
247
--comment "$FILECOMMENT" --output "$SECKEYFILE" \
244
254
if [ "$mode" = password ]; then
245
# Import keys into temporary key rings
255
# Import key into temporary key rings
246
256
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
247
257
--homedir "$RINGDIR" --trust-model always --armor \
248
258
--import "$SECKEYFILE"
254
264
FINGERPRINT="`gpg --quiet --batch --no-tty --no-options \
255
265
--enable-dsa2 --homedir \"$RINGDIR\" --trust-model always \
256
266
--fingerprint --with-colons \
257
| sed -n -e '/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
268
--expression='/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
259
270
test -n "$FINGERPRINT"
261
272
FILECOMMENT="Encrypted password for a Mandos client"
264
echo -n "Enter passphrase: " >&2
266
| gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
274
if [ -n "$PASSFILE" ]; then
278
echo -n "Enter passphrase: " >&2
279
first="$(head --lines=1 | tr --delete '\n')"
280
echo -n -e "\nRepeat passphrase: " >&2
281
second="$(head --lines=1 | tr --delete '\n')"
284
if [ "$first" != "$second" ]; then
285
echo -e "Passphrase mismatch" >&2
290
fi | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
267
291
--homedir "$RINGDIR" --trust-model always --armor --encrypt \
268
--recipient "$FINGERPRINT" --comment "$FILECOMMENT" \
292
--sign --recipient "$FINGERPRINT" --comment "$FILECOMMENT" \
294
status="${PIPESTATUS[0]}"
295
if [ "$status" -ne 0 ]; then
276
302
fingerprint = $FINGERPRINT
305
sed --quiet --expression='
280
306
/^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{
282
308
# Remove 24-bit Radix-64 checksum