/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-09-06 16:11:50 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080906161150-1umb8yz6a29zxg28
* Makefile (install-client): Add "/etc/plugins.d/README".
  (uninstall-client): Remove "/etc/plugins.d/README".

* etc-plugins.d-README: New file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
VERSION="1.0"
24
24
 
25
 
KEYDIR="/etc/mandos"
 
25
KEYDIR="/etc/keys/mandos"
26
26
KEYTYPE=DSA
27
27
KEYLENGTH=2048
28
28
SUBKEYTYPE=ELG-E
64
64
  -n NAME, --name NAME  Name of key.  Default is the FQDN.
65
65
  -e ADDRESS, --email ADDRESS
66
66
                        Email address of key.  Default is empty.
67
 
  -c COMMENT, --comment COMMENT
 
67
  -c TEXT, --comment TEXT
68
68
                        Comment field for key.  The default value is
69
69
                        "Mandos client key".
70
70
  -x TIME, --expire TIME
75
75
Password creation options:
76
76
  -p, --password        Create an encrypted password using the keys in
77
77
                        the key directory.  All options other than
78
 
                        --keydir and --name are ignored.
 
78
                        --dir and --name are ignored.
79
79
EOF
80
80
}
81
81
 
108
108
PUBKEYFILE="$KEYDIR/pubkey.txt"
109
109
 
110
110
# Check for some invalid values
111
 
if [ -d "$KEYDIR" ]; then :; else
 
111
if [ ! -d "$KEYDIR" ]; then
112
112
    echo "$KEYDIR not a directory" >&2
113
113
    exit 1
114
114
fi
115
 
if [ -w "$KEYDIR" ]; then :; else
116
 
    echo "Directory $KEYDIR not writeable" >&2
117
 
    exit 1
118
 
fi
119
 
 
120
 
if [ "$mode" = password -a -e "$KEYDIR/trustdb.gpg.lock" ]; then
121
 
    echo "Key directory has locked trustdb; aborting." >&2
 
115
if [ ! -r "$KEYDIR" ]; then
 
116
    echo "Directory $KEYDIR not readable" >&2
122
117
    exit 1
123
118
fi
124
119
 
125
120
if [ "$mode" = keygen ]; then
 
121
    if [ ! -w "$KEYDIR" ]; then
 
122
        echo "Directory $KEYDIR not writeable" >&2
 
123
        exit 1
 
124
    fi
126
125
    if [ -z "$KEYTYPE" ]; then
127
126
        echo "Empty key type" >&2
128
127
        exit 1
149
148
        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
150
149
    esac
151
150
    
152
 
    if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
153
 
        && [ "$FORCE" -eq 0 ]; then
 
151
    if [ \( -e "$SECKEYFILE" -o -e "$PUBKEYFILE" \) \
 
152
        -a "$FORCE" -eq 0 ]; then
154
153
        echo "Refusing to overwrite old key files; use --force" >&2
155
154
        exit 1
156
155
    fi
164
163
    fi
165
164
 
166
165
    # Create temporary gpg batch file
167
 
    BATCHFILE="`mktemp -t mandos-gpg-batch.XXXXXXXXXX`"
 
166
    BATCHFILE="`mktemp -t mandos-keygen-batch.XXXXXXXXXX`"
168
167
fi
169
168
 
170
169
if [ "$mode" = password ]; then
171
170
    # Create temporary encrypted password file
172
 
    SECFILE="`mktemp -t mandos-gpg-secfile.XXXXXXXXXX`"
173
 
fi
174
 
 
175
 
# Create temporary key rings
176
 
SECRING="`mktemp -t mandos-gpg-secring.XXXXXXXXXX`"
177
 
PUBRING="`mktemp -t mandos-gpg-pubring.XXXXXXXXXX`"
178
 
 
179
 
if [ "$mode" = password ]; then
180
 
    # If a trustdb.gpg file does not already exist, schedule it for
181
 
    # deletion when we are done.
182
 
    if ! [ -e "$KEYDIR/trustdb.gpg" ]; then
183
 
        TRUSTDB="$KEYDIR/trustdb.gpg"
184
 
    fi
185
 
fi
 
171
    SECFILE="`mktemp -t mandos-keygen-secfile.XXXXXXXXXX`"
 
172
fi
 
173
 
 
174
# Create temporary key ring directory
 
175
RINGDIR="`mktemp -d -t mandos-keygen-keyrings.XXXXXXXXXX`"
186
176
 
187
177
# Remove temporary files on exit
188
178
trap "
189
179
set +e; \
190
 
rm --force $PUBRING ${PUBRING}~ $BATCHFILE $TRUSTDB; \
191
 
shred --remove $SECRING $SECFILE; \
 
180
test -n \"$SECFILE\" && shred --remove \"$SECFILE\"; \
 
181
shred --remove \"$RINGDIR\"/sec*;
 
182
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
 
183
rm --recursive --force \"$RINGDIR\";
192
184
stty echo; \
193
185
" EXIT
194
186
 
195
 
umask 027
 
187
umask 077
196
188
 
197
189
if [ "$mode" = keygen ]; then
198
190
    # Create batch file for GnuPG
209
201
        Expire-Date: $KEYEXPIRE
210
202
        #Preferences: <string>
211
203
        #Handle: <no-spaces>
212
 
        %pubring $PUBRING
213
 
        %secring $SECRING
 
204
        #%pubring pubring.gpg
 
205
        #%secring secring.gpg
214
206
        %commit
215
207
        EOF
216
208
    
217
209
    # Generate a new key in the key rings
218
 
    gpg --no-random-seed-file --quiet --batch --no-tty \
219
 
        --no-default-keyring --no-options --enable-dsa2 \
220
 
        --secret-keyring "$SECRING" --keyring "$PUBRING" \
 
210
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
211
        --homedir "$RINGDIR" --trust-model always \
221
212
        --gen-key "$BATCHFILE"
222
213
    rm --force "$BATCHFILE"
223
 
 
 
214
    
224
215
    # Backup any old key files
225
216
    if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
226
217
        2>/dev/null; then
241
232
    fi
242
233
    
243
234
    # Export keys from key rings to key files
244
 
    gpg --no-random-seed-file --quiet --batch --no-tty --armor \
245
 
        --no-default-keyring --no-options --enable-dsa2 \
246
 
        --secret-keyring "$SECRING" --keyring "$PUBRING" \
247
 
        --export-options export-minimal --comment "$FILECOMMENT" \
248
 
        --output "$SECKEYFILE" --export-secret-keys
249
 
    gpg --no-random-seed-file --quiet --batch --no-tty --armor \
250
 
        --no-default-keyring --no-options --enable-dsa2 \
251
 
        --secret-keyring "$SECRING" --keyring "$PUBRING" \
252
 
        --export-options export-minimal --comment "$FILECOMMENT" \
253
 
        --output "$PUBKEYFILE" --export
 
235
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
236
        --homedir "$RINGDIR" --armor --export-options export-minimal \
 
237
        --comment "$FILECOMMENT" --output "$SECKEYFILE" \
 
238
        --export-secret-keys
 
239
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
240
        --homedir "$RINGDIR" --armor --export-options export-minimal \
 
241
        --comment "$FILECOMMENT" --output "$PUBKEYFILE" --export
254
242
fi
255
243
 
256
244
if [ "$mode" = password ]; then
257
245
    # Import keys into temporary key rings
258
 
    gpg --no-random-seed-file --quiet --batch --no-tty --armor \
259
 
        --no-default-keyring --no-options --enable-dsa2 \
260
 
        --homedir "$KEYDIR" --no-permission-warning \
261
 
        --secret-keyring "$SECRING" --keyring "$PUBRING" \
262
 
        --trust-model always --import "$SECKEYFILE"
263
 
    gpg --no-random-seed-file --quiet --batch --no-tty --armor \
264
 
        --no-default-keyring --no-options --enable-dsa2 \
265
 
        --homedir "$KEYDIR" --no-permission-warning \
266
 
        --secret-keyring "$SECRING" --keyring "$PUBRING" \
267
 
        --trust-model always --import "$PUBKEYFILE"
268
 
 
 
246
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
247
        --homedir "$RINGDIR" --trust-model always --armor \
 
248
        --import "$SECKEYFILE"
 
249
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
250
        --homedir "$RINGDIR" --trust-model always --armor \
 
251
        --import "$PUBKEYFILE"
 
252
    
269
253
    # Get fingerprint of key
270
 
    FINGERPRINT="`gpg --no-random-seed-file --quiet --batch --no-tty \
271
 
        --armor --no-default-keyring --no-options --enable-dsa2 \
272
 
        --homedir \"$KEYDIR\" --no-permission-warning \
273
 
        --secret-keyring \"$SECRING\" --keyring \"$PUBRING\" \
274
 
        --trust-model always --fingerprint --with-colons \
275
 
        | sed -n -e '/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
 
254
    FINGERPRINT="`gpg --quiet --batch --no-tty --no-options \
 
255
        --enable-dsa2 --homedir \"$RINGDIR\" --trust-model always \
 
256
        --fingerprint --with-colons \
 
257
        | sed --quiet \
 
258
        --expression='/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
276
259
    
277
260
    test -n "$FINGERPRINT"
278
261
    
280
263
    
281
264
    stty -echo
282
265
    echo -n "Enter passphrase: " >&2
283
 
    sed -e '1q' \
284
 
        | gpg --no-random-seed-file --batch --no-tty --armor \
285
 
        --no-default-keyring --no-options --enable-dsa2 \
286
 
        --homedir "$KEYDIR" --no-permission-warning \
287
 
        --secret-keyring "$SECRING" --keyring "$PUBRING" \
288
 
        --trust-model always --encrypt --recipient "$FINGERPRINT" \
289
 
        --comment "$FILECOMMENT" \
 
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" \
290
270
        > "$SECFILE"
291
271
    echo >&2
292
272
    stty echo
297
277
        fingerprint = $FINGERPRINT
298
278
        secret =
299
279
EOF
300
 
    sed -n -e '
 
280
    sed --quiet --expression='
301
281
        /^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{
302
282
            /^$/,${
303
283
                # Remove 24-bit Radix-64 checksum
316
296
    shred --remove "$SECFILE"
317
297
fi
318
298
# Remove the key rings
319
 
shred --remove "$SECRING"
320
 
rm --force "$PUBRING" "${PUBRING}~"
321
 
# Remove the trustdb, if one did not exist when we started
322
 
if [ -n "$TRUSTDB" ]; then
323
 
    rm --force "$TRUSTDB"
324
 
fi
 
299
shred --remove "$RINGDIR"/sec*
 
300
rm --recursive --force "$RINGDIR"