/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: 2008-10-04 03:11:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081004031139-llfxo3ac8kk4fs6b
* Makefile (DOCS): Added "plugins.d/usplash.8mandos" and
                   "plugins.d/splashy.8mandos".
  (install-client-nokey): Also install "plugins.d/usplash.8mandos" and
                          "plugins.d/splashy.8mandos".
  (uninstall-client): Also uninstall "plugins.d/usplash.8mandos" and
                      "plugins.d/splashy.8mandos".

* plugins.d/splashy.xml: New.

* plugins.d/usplash.xml: - '' -

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 Teddy Hogeborn & Björn Påhlsson
6
6
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
27
27
KEYLENGTH=2048
28
28
SUBKEYTYPE=ELG-E
29
29
SUBKEYLENGTH=2048
30
 
KEYNAME="`hostname --fqdn`"
 
30
KEYNAME="`hostname --fqdn 2>/dev/null || hostname`"
31
31
KEYEMAIL=""
32
32
KEYCOMMENT="Mandos client key"
33
33
KEYEXPIRE=0
35
35
KEYCOMMENT_ORIG="$KEYCOMMENT"
36
36
mode=keygen
37
37
 
 
38
if [ ! -d "$KEYDIR" ]; then
 
39
    KEYDIR="/etc/mandos/keys"
 
40
fi
 
41
 
38
42
# 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 \
 
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" -- "$@"`
42
46
 
43
47
help(){
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]
52
57
 
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.
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;;
148
158
        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
149
159
    esac
150
160
    
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
154
164
        exit 1
155
165
    fi
184
194
stty echo; \
185
195
" EXIT
186
196
 
187
 
umask 027
 
197
umask 077
188
198
 
189
199
if [ "$mode" = keygen ]; then
190
200
    # Create batch file for GnuPG
231
241
        FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
232
242
    fi
233
243
    
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" \
242
252
fi
243
253
 
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}'`"
 
267
        | sed --quiet \
 
268
        --expression='/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
258
269
    
259
270
    test -n "$FINGERPRINT"
260
271
    
261
272
    FILECOMMENT="Encrypted password for a Mandos client"
262
273
    
263
 
    stty -echo
264
 
    echo -n "Enter passphrase: " >&2
265
 
    sed -e '1q' \
266
 
        | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
274
    if [ -n "$PASSFILE" ]; then
 
275
        cat "$PASSFILE"
 
276
    else
 
277
        stty -echo
 
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')"
 
282
        echo >&2
 
283
        stty echo
 
284
        if [ "$first" != "$second" ]; then
 
285
            echo -e "Passphrase mismatch" >&2
 
286
            false
 
287
        else
 
288
            echo -n "$first"
 
289
        fi
 
290
    fi | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
267
291
        --homedir "$RINGDIR" --trust-model always --armor --encrypt \
268
292
        --recipient "$FINGERPRINT" --comment "$FILECOMMENT" \
269
293
        > "$SECFILE"
270
 
    echo >&2
271
 
    stty echo
 
294
    status="${PIPESTATUS[0]}"
 
295
    if [ "$status" -ne 0 ]; then
 
296
        exit "$status"
 
297
    fi
272
298
    
273
299
    cat <<-EOF
274
300
        [$KEYNAME]
275
301
        host = $KEYNAME
276
302
        fingerprint = $FINGERPRINT
277
303
        secret =
278
 
EOF
279
 
    sed -n -e '
 
304
        EOF
 
305
    sed --quiet --expression='
280
306
        /^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{
281
307
            /^$/,${
282
308
                # Remove 24-bit Radix-64 checksum