/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-26 19:05:21 UTC
  • Revision ID: teddy@recompile.se-20131026190521-giagilisbyciox2h
Fall back to /var/run for pidfile if /run is not a directory.

This is for old (possibly non-Debian) systems which have not migrated
from /var/run to /run yet.

* init.d-mandos (PIDFILE): Fall back to /var/run/mandos.pid if /run is
                           not a directory.
* mandos (pidfilename): - '' -
* mandos.xml (FILES): Document fallback to /var/run/mandos.pid if /run
                      is not a directory.

Reported-by: Nathanael D. Noblet <nathanael@gnat.ca>
Suggested-by: Nathanael D. Noblet <nathanael@gnat.ca>

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"
40
41
fi
41
42
 
42
43
# Parse options
43
 
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
44
 
    --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 \
45
46
    --name "$0" -- "$@"`
46
47
 
47
48
help(){
53
54
       $basename [ OPTIONS ]
54
55
   Encrypted password creation:
55
56
       $basename { -p | --password } [ --name NAME ] [ --dir DIR]
 
57
       $basename { -F | --passfile } FILE [ --name NAME ] [ --dir DIR]
56
58
 
57
59
Key creation options:
58
60
  -v, --version         Show program's version number and exit
59
61
  -h, --help            Show this help message and exit
60
62
  -d DIR, --dir DIR     Target directory for key files
61
 
  -t TYPE, --type TYPE  Key type.  Default is DSA.
 
63
  -t TYPE, --type TYPE  Key type.  Default is RSA.
62
64
  -l BITS, --length BITS
63
 
                        Key length in bits.  Default is 2048.
 
65
                        Key length in bits.  Default is 4096.
64
66
  -s TYPE, --subtype TYPE
65
 
                        Subkey type.  Default is ELG-E.
 
67
                        Subkey type.  Default is RSA.
66
68
  -L BITS, --sublength BITS
67
 
                        Subkey length in bits.  Default is 2048.
 
69
                        Subkey length in bits.  Default is 4096.
68
70
  -n NAME, --name NAME  Name of key.  Default is the FQDN.
69
71
  -e ADDRESS, --email ADDRESS
70
72
                        Email address of key.  Default is empty.
71
73
  -c TEXT, --comment TEXT
72
 
                        Comment field for key.  The default value is
73
 
                        "Mandos client key".
 
74
                        Comment field for key.  The default is empty.
74
75
  -x TIME, --expire TIME
75
76
                        Key expire time.  Default is no expiration.
76
77
                        See gpg(1) for syntax.
77
 
  -f, --force           Force overwriting old keys.
 
78
  -f, --force           Force overwriting old key files.
78
79
 
79
80
Password creation options:
80
 
  -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
81
86
                        the key directory.  All options other than
82
87
                        --dir and --name are ignored.
83
88
EOF
87
92
while :; do
88
93
    case "$1" in
89
94
        -p|--password) mode=password; shift;;
 
95
        -F|--passfile) mode=password; PASSFILE="$2"; shift 2;;
90
96
        -d|--dir) KEYDIR="$2"; shift 2;;
91
97
        -t|--type) KEYTYPE="$2"; shift 2;;
92
98
        -s|--subtype) SUBKEYTYPE="$2"; shift 2;;
140
146
        echo "Invalid key length" >&2
141
147
        exit 1
142
148
    fi
143
 
 
 
149
    
144
150
    if [ -z "$KEYEXPIRE" ]; then
145
151
        echo "Empty key expiration" >&2
146
152
        exit 1
165
171
    if [ -n "$KEYEMAIL" ]; then
166
172
        KEYEMAILLINE="Name-Email: $KEYEMAIL"
167
173
    fi
168
 
 
 
174
    
169
175
    # Create temporary gpg batch file
170
176
    BATCHFILE="`mktemp -t mandos-keygen-batch.XXXXXXXXXX`"
171
177
fi
185
191
shred --remove \"$RINGDIR\"/sec*;
186
192
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
187
193
rm --recursive --force \"$RINGDIR\";
188
 
stty echo; \
 
194
tty --quiet && stty echo; \
189
195
" EXIT
190
196
 
 
197
set -e
 
198
 
191
199
umask 077
192
200
 
193
201
if [ "$mode" = keygen ]; then
195
203
    cat >"$BATCHFILE" <<-EOF
196
204
        Key-Type: $KEYTYPE
197
205
        Key-Length: $KEYLENGTH
198
 
        #Key-Usage: encrypt,sign,auth
 
206
        Key-Usage: sign,auth
199
207
        Subkey-Type: $SUBKEYTYPE
200
208
        Subkey-Length: $SUBKEYLENGTH
201
 
        #Subkey-Usage: encrypt,sign,auth
 
209
        Subkey-Usage: encrypt
202
210
        Name-Real: $KEYNAME
203
211
        $KEYCOMMENTLINE
204
212
        $KEYEMAILLINE
210
218
        %commit
211
219
        EOF
212
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
    
213
231
    # Generate a new key in the key rings
214
232
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
215
233
        --homedir "$RINGDIR" --trust-model always \
216
234
        --gen-key "$BATCHFILE"
217
235
    rm --force "$BATCHFILE"
218
236
    
 
237
    if tty --quiet; then
 
238
        echo -n "Finished: "
 
239
        date
 
240
    fi
 
241
    
219
242
    # Backup any old key files
220
243
    if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
221
244
        2>/dev/null; then
235
258
        FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
236
259
    fi
237
260
    
238
 
    # Export keys from key rings to key files
 
261
    # Export key from key rings to key files
239
262
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
240
263
        --homedir "$RINGDIR" --armor --export-options export-minimal \
241
264
        --comment "$FILECOMMENT" --output "$SECKEYFILE" \
246
269
fi
247
270
 
248
271
if [ "$mode" = password ]; then
249
 
    # Import keys into temporary key rings
 
272
    # Import key into temporary key rings
250
273
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
251
274
        --homedir "$RINGDIR" --trust-model always --armor \
252
275
        --import "$SECKEYFILE"
265
288
    
266
289
    FILECOMMENT="Encrypted password for a Mandos client"
267
290
    
268
 
    stty -echo
269
 
    echo -n "Enter passphrase: " >&2
270
 
    head --lines=1 | tr --delete '\n' \
271
 
        | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
272
 
        --homedir "$RINGDIR" --trust-model always --armor --encrypt \
273
 
        --recipient "$FINGERPRINT" --comment "$FILECOMMENT" \
274
 
        > "$SECFILE"
275
 
    echo >&2
276
 
    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
277
324
    
278
325
    cat <<-EOF
279
326
        [$KEYNAME]
280
327
        host = $KEYNAME
281
328
        fingerprint = $FINGERPRINT
282
329
        secret =
283
 
EOF
 
330
        EOF
284
331
    sed --quiet --expression='
285
332
        /^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{
286
333
            /^$/,${