/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: 2014-06-14 22:55:11 UTC
  • Revision ID: teddy@recompile.se-20140614225511-0ge6tce2esulf65t
In plugin-runner, do scandirat() or scandir() instead of readdir().

* plugin-runner.c (set_cloexec_flag): Only define if O_CLOEXEC is not
                                      defined, it is not called
                                      otherwise.
  (main): Remove variables "dir" and "dirst"; new variable "dir_fd".
          Use scandirat() if available, scandir() otherwise, both
          using new filter function "good_name".  Removed checking of
          plugin file name patterns; this is now done by "good_name".
          Do not set FD_CLOEXEC flag on plugin_fd, since fexecve()
          will not work with it.  Use pipe2() to set O_CLOEXEC on pipe
          FD's (if possible).
  (main/good_name): New filter function for scandirat() or scandir().

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 © 2008 Teddy Hogeborn & Björn Påhlsson
 
5
# Copyright © 2008-2014 Teddy Hogeborn
 
6
# Copyright © 2008-2014 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.5"
24
25
 
25
26
KEYDIR="/etc/keys/mandos"
26
 
KEYTYPE=DSA
27
 
KEYLENGTH=2048
28
 
SUBKEYTYPE=ELG-E
29
 
SUBKEYLENGTH=2048
 
27
KEYTYPE=RSA
 
28
KEYLENGTH=4096
 
29
SUBKEYTYPE=RSA
 
30
SUBKEYLENGTH=4096
30
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"
59
60
  -v, --version         Show program's version number and exit
60
61
  -h, --help            Show this help message and exit
61
62
  -d DIR, --dir DIR     Target directory for key files
62
 
  -t TYPE, --type TYPE  Key type.  Default is DSA.
 
63
  -t TYPE, --type TYPE  Key type.  Default is RSA.
63
64
  -l BITS, --length BITS
64
 
                        Key length in bits.  Default is 2048.
 
65
                        Key length in bits.  Default is 4096.
65
66
  -s TYPE, --subtype TYPE
66
 
                        Subkey type.  Default is ELG-E.
 
67
                        Subkey type.  Default is RSA.
67
68
  -L BITS, --sublength BITS
68
 
                        Subkey length in bits.  Default is 2048.
 
69
                        Subkey length in bits.  Default is 4096.
69
70
  -n NAME, --name NAME  Name of key.  Default is the FQDN.
70
71
  -e ADDRESS, --email ADDRESS
71
72
                        Email address of key.  Default is empty.
72
73
  -c TEXT, --comment TEXT
73
 
                        Comment field for key.  The default value is
74
 
                        "Mandos client key".
 
74
                        Comment field for key.  The default is empty.
75
75
  -x TIME, --expire TIME
76
76
                        Key expire time.  Default is no expiration.
77
77
                        See gpg(1) for syntax.
146
146
        echo "Invalid key length" >&2
147
147
        exit 1
148
148
    fi
149
 
 
 
149
    
150
150
    if [ -z "$KEYEXPIRE" ]; then
151
151
        echo "Empty key expiration" >&2
152
152
        exit 1
171
171
    if [ -n "$KEYEMAIL" ]; then
172
172
        KEYEMAILLINE="Name-Email: $KEYEMAIL"
173
173
    fi
174
 
 
 
174
    
175
175
    # Create temporary gpg batch file
176
176
    BATCHFILE="`mktemp -t mandos-keygen-batch.XXXXXXXXXX`"
177
177
fi
191
191
shred --remove \"$RINGDIR\"/sec*;
192
192
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
193
193
rm --recursive --force \"$RINGDIR\";
194
 
stty echo; \
 
194
tty --quiet && stty echo; \
195
195
" EXIT
196
196
 
 
197
set -e
 
198
 
197
199
umask 077
198
200
 
199
201
if [ "$mode" = keygen ]; then
201
203
    cat >"$BATCHFILE" <<-EOF
202
204
        Key-Type: $KEYTYPE
203
205
        Key-Length: $KEYLENGTH
204
 
        #Key-Usage: encrypt,sign,auth
 
206
        Key-Usage: sign,auth
205
207
        Subkey-Type: $SUBKEYTYPE
206
208
        Subkey-Length: $SUBKEYLENGTH
207
 
        #Subkey-Usage: encrypt,sign,auth
 
209
        Subkey-Usage: encrypt
208
210
        Name-Real: $KEYNAME
209
211
        $KEYCOMMENTLINE
210
212
        $KEYEMAILLINE
216
218
        %commit
217
219
        EOF
218
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
    
 
231
    # Make sure trustdb.gpg exists;
 
232
    # this is a workaround for Debian bug #737128
 
233
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
234
        --homedir "$RINGDIR" \
 
235
        --import-ownertrust < /dev/null
219
236
    # Generate a new key in the key rings
220
237
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
221
238
        --homedir "$RINGDIR" --trust-model always \
222
239
        --gen-key "$BATCHFILE"
223
240
    rm --force "$BATCHFILE"
224
241
    
 
242
    if tty --quiet; then
 
243
        echo -n "Finished: "
 
244
        date
 
245
    fi
 
246
    
225
247
    # Backup any old key files
226
248
    if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
227
249
        2>/dev/null; then
271
293
    
272
294
    FILECOMMENT="Encrypted password for a Mandos client"
273
295
    
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
 
296
    while [ ! -s "$SECFILE" ]; do
 
297
        if [ -n "$PASSFILE" ]; then
 
298
            cat "$PASSFILE"
287
299
        else
288
 
            echo -n "$first"
 
300
            tty --quiet && stty -echo
 
301
            echo -n "Enter passphrase: " >&2
 
302
            read first
 
303
            tty --quiet && echo >&2
 
304
            echo -n "Repeat passphrase: " >&2
 
305
            read second
 
306
            if tty --quiet; then
 
307
                echo >&2
 
308
                stty echo
 
309
            fi
 
310
            if [ "$first" != "$second" ]; then
 
311
                echo "Passphrase mismatch" >&2
 
312
                touch "$RINGDIR"/mismatch
 
313
            else
 
314
                echo -n "$first"
 
315
            fi
 
316
        fi | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
317
            --homedir "$RINGDIR" --trust-model always --armor \
 
318
            --encrypt --sign --recipient "$FINGERPRINT" --comment \
 
319
            "$FILECOMMENT" > "$SECFILE"
 
320
        if [ -e "$RINGDIR"/mismatch ]; then
 
321
            rm --force "$RINGDIR"/mismatch
 
322
            if tty --quiet; then
 
323
                > "$SECFILE"
 
324
            else
 
325
                exit 1
 
326
            fi
289
327
        fi
290
 
    fi | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
291
 
        --homedir "$RINGDIR" --trust-model always --armor --encrypt \
292
 
        --recipient "$FINGERPRINT" --comment "$FILECOMMENT" \
293
 
        > "$SECFILE"
294
 
    status="${PIPESTATUS[0]}"
295
 
    if [ "$status" -ne 0 ]; then
296
 
        exit "$status"
297
 
    fi
 
328
    done
298
329
    
299
330
    cat <<-EOF
300
331
        [$KEYNAME]