/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: 2017-08-20 14:14:14 UTC
  • Revision ID: teddy@recompile.se-20170820141414-m034xuebg7ccaeui
Add some more restrictions to the systemd service file.

* mandos.service ([Service]/ProtectKernelTunables): New; set to "yes".
  ([Service]/ProtectControlGroups): - '' -

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-2013 Teddy Hogeborn
6
 
# Copyright © 2008-2013 Björn Påhlsson
 
5
# Copyright © 2008-2017 Teddy Hogeborn
 
6
# Copyright © 2008-2017 Björn Påhlsson
7
7
8
8
# This program is free software: you can redistribute it and/or modify
9
9
# it under the terms of the GNU General Public License as published by
21
21
# Contact the authors at <mandos@recompile.se>.
22
22
23
23
 
24
 
VERSION="1.6.4"
 
24
VERSION="1.7.15"
25
25
 
26
26
KEYDIR="/etc/keys/mandos"
27
27
KEYTYPE=RSA
33
33
KEYCOMMENT=""
34
34
KEYEXPIRE=0
35
35
FORCE=no
 
36
SSH=yes
36
37
KEYCOMMENT_ORIG="$KEYCOMMENT"
37
38
mode=keygen
38
39
 
41
42
fi
42
43
 
43
44
# Parse options
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
TEMP=`getopt --options vhpF:d:t:l:s:L:n:e:c:x:fS \
 
46
    --longoptions version,help,password,passfile:,dir:,type:,length:,subtype:,sublength:,name:,email:,comment:,expire:,force,no-ssh \
46
47
    --name "$0" -- "$@"`
47
48
 
48
49
help(){
49
 
basename="`basename $0`"
 
50
basename="`basename "$0"`"
50
51
cat <<EOF
51
52
Usage: $basename [ -v | --version ]
52
53
       $basename [ -h | --help ]
85
86
                        Encrypt a password from FILE using the key in
86
87
                        the key directory.  All options other than
87
88
                        --dir and --name are ignored.
 
89
  -S, --no-ssh          Don't get SSH key or set "checker" option.
88
90
EOF
89
91
}
90
92
 
103
105
        -c|--comment) KEYCOMMENT="$2"; shift 2;;
104
106
        -x|--expire) KEYEXPIRE="$2"; shift 2;;
105
107
        -f|--force) FORCE=yes; shift;;
 
108
        -S|--no-ssh) SSH=no; shift;;
106
109
        -v|--version) echo "$0 $VERSION"; exit;;
107
110
        -h|--help) help; exit;;
108
111
        --) shift; break;;
110
113
    esac
111
114
done
112
115
if [ "$#" -gt 0 ]; then
113
 
    echo "Unknown arguments: '$@'" >&2
 
116
    echo "Unknown arguments: '$*'" >&2
114
117
    exit 1
115
118
fi
116
119
 
158
161
        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
159
162
    esac
160
163
    
161
 
    if [ \( -e "$SECKEYFILE" -o -e "$PUBKEYFILE" \) \
162
 
        -a "$FORCE" -eq 0 ]; then
 
164
    if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
 
165
        && [ "$FORCE" -eq 0 ]; then
163
166
        echo "Refusing to overwrite old key files; use --force" >&2
164
167
        exit 1
165
168
    fi
188
191
trap "
189
192
set +e; \
190
193
test -n \"$SECFILE\" && shred --remove \"$SECFILE\"; \
191
 
shred --remove \"$RINGDIR\"/sec*;
 
194
shred --remove \"$RINGDIR\"/sec* 2>/dev/null;
192
195
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
193
196
rm --recursive --force \"$RINGDIR\";
194
197
tty --quiet && stty echo; \
215
218
        #Handle: <no-spaces>
216
219
        #%pubring pubring.gpg
217
220
        #%secring secring.gpg
 
221
        %no-protection
218
222
        %commit
219
223
        EOF
220
224
    
228
232
        date
229
233
    fi
230
234
    
 
235
    # Make sure trustdb.gpg exists;
 
236
    # this is a workaround for Debian bug #737128
 
237
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
238
        --homedir "$RINGDIR" \
 
239
        --import-ownertrust < /dev/null
231
240
    # Generate a new key in the key rings
232
241
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
233
242
        --homedir "$RINGDIR" --trust-model always \
269
278
fi
270
279
 
271
280
if [ "$mode" = password ]; then
 
281
    
 
282
    # Make SSH be 0 or 1
 
283
    case "$SSH" in
 
284
        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) SSH=1;;
 
285
        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) SSH=0;;
 
286
    esac
 
287
    
 
288
    if [ $SSH -eq 1 ]; then
 
289
        for ssh_keytype in ecdsa-sha2-nistp256 ed25519 rsa; do
 
290
            set +e
 
291
            ssh_fingerprint="`ssh-keyscan -t $ssh_keytype localhost 2>/dev/null`"
 
292
            err=$?
 
293
            set -e
 
294
            if [ $err -ne 0 ]; then
 
295
                ssh_fingerprint=""
 
296
                continue
 
297
            fi
 
298
            if [ -n "$ssh_fingerprint" ]; then
 
299
                ssh_fingerprint="${ssh_fingerprint#localhost }"
 
300
                break
 
301
            fi
 
302
        done
 
303
    fi
 
304
    
272
305
    # Import key into temporary key rings
273
306
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
274
307
        --homedir "$RINGDIR" --trust-model always --armor \
279
312
    
280
313
    # Get fingerprint of key
281
314
    FINGERPRINT="`gpg --quiet --batch --no-tty --no-options \
282
 
        --enable-dsa2 --homedir \"$RINGDIR\" --trust-model always \
 
315
        --enable-dsa2 --homedir "$RINGDIR" --trust-model always \
283
316
        --fingerprint --with-colons \
284
317
        | sed --quiet \
285
318
        --expression='/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
293
326
            cat "$PASSFILE"
294
327
        else
295
328
            tty --quiet && stty -echo
296
 
            echo -n "Enter passphrase: " >&2
297
 
            read first
 
329
            echo -n "Enter passphrase: " >/dev/tty
 
330
            read -r first
298
331
            tty --quiet && echo >&2
299
 
            echo -n "Repeat passphrase: " >&2
300
 
            read second
 
332
            echo -n "Repeat passphrase: " >/dev/tty
 
333
            read -r second
301
334
            if tty --quiet; then
302
335
                echo >&2
303
336
                stty echo
337
370
                /^[^-]/s/^/    /p
338
371
            }
339
372
        }' < "$SECFILE"
 
373
    if [ -n "$ssh_fingerprint" ]; then
 
374
        echo 'checker = ssh-keyscan -t '"$ssh_keytype"' %%(host)s 2>/dev/null | grep --fixed-strings --line-regexp --quiet --regexp=%%(host)s" %(ssh_fingerprint)s"'
 
375
        echo "ssh_fingerprint = ${ssh_fingerprint}"
 
376
    fi
340
377
fi
341
378
 
342
379
trap - EXIT
347
384
    shred --remove "$SECFILE"
348
385
fi
349
386
# Remove the key rings
350
 
shred --remove "$RINGDIR"/sec*
 
387
shred --remove "$RINGDIR"/sec* 2>/dev/null
351
388
rm --recursive --force "$RINGDIR"