/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-keygen

  • Committer: Teddy Hogeborn
  • Date: 2013-10-28 20:08:32 UTC
  • Revision ID: teddy@recompile.se-20131028200832-80d1k8c5e8mugpkx
Syntax fix; use "raise" better in Mandos server.

* mandos: Syntax fix; use "raise" better in Mandos server; Always
          raise instances, not classes, skip argument when re-raising,
          etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
3
3
# Mandos key generator - create a new OpenPGP key for a Mandos client
4
4
5
 
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
5
# Copyright © 2008-2013 Teddy Hogeborn
 
6
# Copyright © 2008-2013 Björn Påhlsson
6
7
7
8
# This program is free software: you can redistribute it and/or modify
8
9
# it under the terms of the GNU General Public License as published by
17
18
# You should have received a copy of the GNU General Public License
18
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
20
 
# Contact the authors at <mandos@fukt.bsnet.se>.
 
21
# Contact the authors at <mandos@recompile.se>.
21
22
22
23
 
23
 
VERSION="1.0"
 
24
VERSION="1.6.2"
24
25
 
25
26
KEYDIR="/etc/keys/mandos"
26
 
KEYTYPE=DSA
27
 
KEYLENGTH=2048
28
 
SUBKEYTYPE=ELG-E
29
 
SUBKEYLENGTH=2048
30
 
KEYNAME="`hostname --fqdn`"
 
27
KEYTYPE=RSA
 
28
KEYLENGTH=4096
 
29
SUBKEYTYPE=RSA
 
30
SUBKEYLENGTH=4096
 
31
KEYNAME="`hostname --fqdn 2>/dev/null || hostname`"
31
32
KEYEMAIL=""
32
 
KEYCOMMENT="Mandos client key"
 
33
KEYCOMMENT=""
33
34
KEYEXPIRE=0
34
35
FORCE=no
35
36
KEYCOMMENT_ORIG="$KEYCOMMENT"
36
37
mode=keygen
37
38
 
 
39
if [ ! -d "$KEYDIR" ]; then
 
40
    KEYDIR="/etc/mandos/keys"
 
41
fi
 
42
 
38
43
# Parse options
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 \
 
44
TEMP=`getopt --options vhpF:d:t:l:s:L:n:e:c:x:f \
 
45
    --longoptions version,help,password,passfile:,dir:,type:,length:,subtype:,sublength:,name:,email:,comment:,expire:,force \
41
46
    --name "$0" -- "$@"`
42
47
 
43
48
help(){
49
54
       $basename [ OPTIONS ]
50
55
   Encrypted password creation:
51
56
       $basename { -p | --password } [ --name NAME ] [ --dir DIR]
 
57
       $basename { -F | --passfile } FILE [ --name NAME ] [ --dir DIR]
52
58
 
53
59
Key creation options:
54
60
  -v, --version         Show program's version number and exit
55
61
  -h, --help            Show this help message and exit
56
62
  -d DIR, --dir DIR     Target directory for key files
57
 
  -t TYPE, --type TYPE  Key type.  Default is DSA.
 
63
  -t TYPE, --type TYPE  Key type.  Default is RSA.
58
64
  -l BITS, --length BITS
59
 
                        Key length in bits.  Default is 2048.
 
65
                        Key length in bits.  Default is 4096.
60
66
  -s TYPE, --subtype TYPE
61
 
                        Subkey type.  Default is ELG-E.
 
67
                        Subkey type.  Default is RSA.
62
68
  -L BITS, --sublength BITS
63
 
                        Subkey length in bits.  Default is 2048.
 
69
                        Subkey length in bits.  Default is 4096.
64
70
  -n NAME, --name NAME  Name of key.  Default is the FQDN.
65
71
  -e ADDRESS, --email ADDRESS
66
72
                        Email address of key.  Default is empty.
67
73
  -c TEXT, --comment TEXT
68
 
                        Comment field for key.  The default value is
69
 
                        "Mandos client key".
 
74
                        Comment field for key.  The default is empty.
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.
74
79
 
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.
79
88
EOF
83
92
while :; do
84
93
    case "$1" in
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;;
136
146
        echo "Invalid key length" >&2
137
147
        exit 1
138
148
    fi
139
 
 
 
149
    
140
150
    if [ -z "$KEYEXPIRE" ]; then
141
151
        echo "Empty key expiration" >&2
142
152
        exit 1
161
171
    if [ -n "$KEYEMAIL" ]; then
162
172
        KEYEMAILLINE="Name-Email: $KEYEMAIL"
163
173
    fi
164
 
 
 
174
    
165
175
    # Create temporary gpg batch file
166
176
    BATCHFILE="`mktemp -t mandos-keygen-batch.XXXXXXXXXX`"
167
177
fi
181
191
shred --remove \"$RINGDIR\"/sec*;
182
192
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
183
193
rm --recursive --force \"$RINGDIR\";
184
 
stty echo; \
 
194
tty --quiet && stty echo; \
185
195
" EXIT
186
196
 
 
197
set -e
 
198
 
187
199
umask 077
188
200
 
189
201
if [ "$mode" = keygen ]; then
191
203
    cat >"$BATCHFILE" <<-EOF
192
204
        Key-Type: $KEYTYPE
193
205
        Key-Length: $KEYLENGTH
194
 
        #Key-Usage: encrypt,sign,auth
 
206
        Key-Usage: sign,auth
195
207
        Subkey-Type: $SUBKEYTYPE
196
208
        Subkey-Length: $SUBKEYLENGTH
197
 
        #Subkey-Usage: encrypt,sign,auth
 
209
        Subkey-Usage: encrypt
198
210
        Name-Real: $KEYNAME
199
211
        $KEYCOMMENTLINE
200
212
        $KEYEMAILLINE
206
218
        %commit
207
219
        EOF
208
220
    
 
221
    if tty --quiet; then
 
222
        cat <<-EOF
 
223
        Note: Due to entropy requirements, key generation could take
 
224
        anything from a few minutes to SEVERAL HOURS.  Please be
 
225
        patient and/or supply the system with more entropy if needed.
 
226
        EOF
 
227
        echo -n "Started: "
 
228
        date
 
229
    fi
 
230
    
209
231
    # Generate a new key in the key rings
210
232
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
211
233
        --homedir "$RINGDIR" --trust-model always \
212
234
        --gen-key "$BATCHFILE"
213
235
    rm --force "$BATCHFILE"
214
236
    
 
237
    if tty --quiet; then
 
238
        echo -n "Finished: "
 
239
        date
 
240
    fi
 
241
    
215
242
    # Backup any old key files
216
243
    if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
217
244
        2>/dev/null; then
231
258
        FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
232
259
    fi
233
260
    
234
 
    # Export keys from key rings to key files
 
261
    # Export key from key rings to key files
235
262
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
236
263
        --homedir "$RINGDIR" --armor --export-options export-minimal \
237
264
        --comment "$FILECOMMENT" --output "$SECKEYFILE" \
242
269
fi
243
270
 
244
271
if [ "$mode" = password ]; then
245
 
    # Import keys into temporary key rings
 
272
    # Import key into temporary key rings
246
273
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
247
274
        --homedir "$RINGDIR" --trust-model always --armor \
248
275
        --import "$SECKEYFILE"
261
288
    
262
289
    FILECOMMENT="Encrypted password for a Mandos client"
263
290
    
264
 
    stty -echo
265
 
    echo -n "Enter passphrase: " >&2
266
 
    head --lines=1 | tr --delete '\n' \
267
 
        | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
268
 
        --homedir "$RINGDIR" --trust-model always --armor --encrypt \
269
 
        --recipient "$FINGERPRINT" --comment "$FILECOMMENT" \
270
 
        > "$SECFILE"
271
 
    echo >&2
272
 
    stty echo
 
291
    while [ ! -s "$SECFILE" ]; do
 
292
        if [ -n "$PASSFILE" ]; then
 
293
            cat "$PASSFILE"
 
294
        else
 
295
            tty --quiet && stty -echo
 
296
            echo -n "Enter passphrase: " >&2
 
297
            read first
 
298
            tty --quiet && echo >&2
 
299
            echo -n "Repeat passphrase: " >&2
 
300
            read second
 
301
            if tty --quiet; then
 
302
                echo >&2
 
303
                stty echo
 
304
            fi
 
305
            if [ "$first" != "$second" ]; then
 
306
                echo "Passphrase mismatch" >&2
 
307
                touch "$RINGDIR"/mismatch
 
308
            else
 
309
                echo -n "$first"
 
310
            fi
 
311
        fi | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
312
            --homedir "$RINGDIR" --trust-model always --armor \
 
313
            --encrypt --sign --recipient "$FINGERPRINT" --comment \
 
314
            "$FILECOMMENT" > "$SECFILE"
 
315
        if [ -e "$RINGDIR"/mismatch ]; then
 
316
            rm --force "$RINGDIR"/mismatch
 
317
            if tty --quiet; then
 
318
                > "$SECFILE"
 
319
            else
 
320
                exit 1
 
321
            fi
 
322
        fi
 
323
    done
273
324
    
274
325
    cat <<-EOF
275
326
        [$KEYNAME]
276
327
        host = $KEYNAME
277
328
        fingerprint = $FINGERPRINT
278
329
        secret =
279
 
EOF
 
330
        EOF
280
331
    sed --quiet --expression='
281
332
        /^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{
282
333
            /^$/,${