/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-12 19:12:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080912191240-edjlcll43eoijkx0
* Makefile (install): Use "install-client-nokey".
  (install-server): Create "/etc/default" and "/usr/sbin", too.
  (install-client): Do not depend on "$(INITRAMFSTOOLS)/hooks/.".
                    Renamed to "install-client-nokey".  Split out
                    post-installation-stuff to new "install-client"
                    target.

* mandos-clients.conf.xml: White space adjustments.
* mandos-keygen.xml: - '' -
* mandos.conf.xml: - '' -
* mandos.xml: - '' -
* plugin-runner.xml: - '' -
* plugins.d/mandos-client.xml: - '' -

* overview.xml: Improved grammar.

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
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
43
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
40
44
    --longoptions version,help,password,dir:,type:,length:,subtype:,sublength:,name:,email:,comment:,expire:,force \
108
112
PUBKEYFILE="$KEYDIR/pubkey.txt"
109
113
 
110
114
# Check for some invalid values
111
 
if [ -d "$KEYDIR" ]; then :; else
 
115
if [ ! -d "$KEYDIR" ]; then
112
116
    echo "$KEYDIR not a directory" >&2
113
117
    exit 1
114
118
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
 
119
if [ ! -r "$KEYDIR" ]; then
 
120
    echo "Directory $KEYDIR not readable" >&2
122
121
    exit 1
123
122
fi
124
123
 
125
124
if [ "$mode" = keygen ]; then
 
125
    if [ ! -w "$KEYDIR" ]; then
 
126
        echo "Directory $KEYDIR not writeable" >&2
 
127
        exit 1
 
128
    fi
126
129
    if [ -z "$KEYTYPE" ]; then
127
130
        echo "Empty key type" >&2
128
131
        exit 1
149
152
        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
150
153
    esac
151
154
    
152
 
    if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
153
 
        && [ "$FORCE" -eq 0 ]; then
 
155
    if [ \( -e "$SECKEYFILE" -o -e "$PUBKEYFILE" \) \
 
156
        -a "$FORCE" -eq 0 ]; then
154
157
        echo "Refusing to overwrite old key files; use --force" >&2
155
158
        exit 1
156
159
    fi
164
167
    fi
165
168
 
166
169
    # Create temporary gpg batch file
167
 
    BATCHFILE="`mktemp -t mandos-gpg-batch.XXXXXXXXXX`"
 
170
    BATCHFILE="`mktemp -t mandos-keygen-batch.XXXXXXXXXX`"
168
171
fi
169
172
 
170
173
if [ "$mode" = password ]; then
171
174
    # 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
 
175
    SECFILE="`mktemp -t mandos-keygen-secfile.XXXXXXXXXX`"
 
176
fi
 
177
 
 
178
# Create temporary key ring directory
 
179
RINGDIR="`mktemp -d -t mandos-keygen-keyrings.XXXXXXXXXX`"
186
180
 
187
181
# Remove temporary files on exit
188
182
trap "
189
183
set +e; \
190
 
rm --force $PUBRING ${PUBRING}~ $BATCHFILE $TRUSTDB; \
191
 
shred --remove $SECRING $SECFILE; \
 
184
test -n \"$SECFILE\" && shred --remove \"$SECFILE\"; \
 
185
shred --remove \"$RINGDIR\"/sec*;
 
186
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
 
187
rm --recursive --force \"$RINGDIR\";
192
188
stty echo; \
193
189
" EXIT
194
190
 
195
 
umask 027
 
191
umask 077
196
192
 
197
193
if [ "$mode" = keygen ]; then
198
194
    # Create batch file for GnuPG
209
205
        Expire-Date: $KEYEXPIRE
210
206
        #Preferences: <string>
211
207
        #Handle: <no-spaces>
212
 
        %pubring $PUBRING
213
 
        %secring $SECRING
 
208
        #%pubring pubring.gpg
 
209
        #%secring secring.gpg
214
210
        %commit
215
211
        EOF
216
212
    
217
213
    # 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" \
 
214
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
215
        --homedir "$RINGDIR" --trust-model always \
221
216
        --gen-key "$BATCHFILE"
222
217
    rm --force "$BATCHFILE"
223
 
 
 
218
    
224
219
    # Backup any old key files
225
220
    if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
226
221
        2>/dev/null; then
241
236
    fi
242
237
    
243
238
    # 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
 
239
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
240
        --homedir "$RINGDIR" --armor --export-options export-minimal \
 
241
        --comment "$FILECOMMENT" --output "$SECKEYFILE" \
 
242
        --export-secret-keys
 
243
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
244
        --homedir "$RINGDIR" --armor --export-options export-minimal \
 
245
        --comment "$FILECOMMENT" --output "$PUBKEYFILE" --export
254
246
fi
255
247
 
256
248
if [ "$mode" = password ]; then
257
249
    # 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
 
 
 
250
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
251
        --homedir "$RINGDIR" --trust-model always --armor \
 
252
        --import "$SECKEYFILE"
 
253
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
254
        --homedir "$RINGDIR" --trust-model always --armor \
 
255
        --import "$PUBKEYFILE"
 
256
    
269
257
    # 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}'`"
 
258
    FINGERPRINT="`gpg --quiet --batch --no-tty --no-options \
 
259
        --enable-dsa2 --homedir \"$RINGDIR\" --trust-model always \
 
260
        --fingerprint --with-colons \
 
261
        | sed --quiet \
 
262
        --expression='/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
276
263
    
277
264
    test -n "$FINGERPRINT"
278
265
    
280
267
    
281
268
    stty -echo
282
269
    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" \
 
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" \
290
274
        > "$SECFILE"
291
275
    echo >&2
292
276
    stty echo
297
281
        fingerprint = $FINGERPRINT
298
282
        secret =
299
283
EOF
300
 
    sed -n -e '
 
284
    sed --quiet --expression='
301
285
        /^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{
302
286
            /^$/,${
303
287
                # Remove 24-bit Radix-64 checksum
316
300
    shred --remove "$SECFILE"
317
301
fi
318
302
# 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
 
303
shred --remove "$RINGDIR"/sec*
 
304
rm --recursive --force "$RINGDIR"