/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
 
1
#!/bin/sh -e
2
2
3
 
# Mandos key generator - create new OpenPGP keys for Mandos clients
 
3
# Mandos key generator - create a new OpenPGP key for a Mandos client
4
4
5
5
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
6
6
20
20
# Contact the authors at <mandos@fukt.bsnet.se>.
21
21
22
22
 
 
23
VERSION="1.0"
 
24
 
23
25
KEYDIR="/etc/mandos"
24
26
KEYTYPE=DSA
25
27
KEYLENGTH=1024
31
33
KEYCOMMENT_ORIG="$KEYCOMMENT"
32
34
 
33
35
# Parse options
34
 
TEMP=`getopt --options d:t:l:n:e:c:x:f \
35
 
    --longoptions dir:,type:,length:,name:,email:,comment:,expire:,force \
 
36
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
 
37
    --longoptions version,help,dir:,type:,length:,name:,email:,comment:,expire:,force \
36
38
    --name "$0" -- "$@"`
37
39
 
 
40
help(){
 
41
cat <<EOF
 
42
Usage: `basename $0` [options]
 
43
 
 
44
Options:
 
45
  -v, --version         Show program's version number and exit
 
46
  -h, --help            Show this help message and exit
 
47
  -d DIR, --dir DIR     Target directory for key files
 
48
  -t TYPE, --type TYPE  Key type.  Default is DSA.
 
49
  -l BITS, --length BITS
 
50
                        Key length in bits.  Default is 1024.
 
51
  -n NAME, --name NAME  Name of key.  Default is the FQDN.
 
52
  -e EMAIL, --email EMAIL
 
53
                        Email address of key.  Default is empty.
 
54
  -c COMMENT, --comment COMMENT
 
55
                        Comment field for key.  The default value is
 
56
                        "Mandos client key".
 
57
  -x TIME, --expire TIME
 
58
                        Key expire time.  Default is no expiration.
 
59
                        See gpg(1) for syntax.
 
60
  -f, --force           Force overwriting old keys.
 
61
EOF
 
62
}
 
63
 
38
64
eval set -- "$TEMP"
39
65
while :; do
40
66
    case "$1" in
46
72
        -c|--comment) KEYCOMMENT="$2"; shift 2;;
47
73
        -x|--expire) KEYCOMMENT="$2"; shift 2;;
48
74
        -f|--force) FORCE=yes; shift;;
 
75
        -v|--version) echo "$0 $VERSION"; exit;;
 
76
        -h|--help) help; exit;;
49
77
        --) shift; break;;
50
78
        *) echo "Internal error" >&2; exit 1;;
51
79
    esac
100
128
    exit 1
101
129
fi
102
130
 
103
 
# Set lines for GPG batch file
 
131
# Set lines for GnuPG batch file
104
132
if [ -n "$KEYCOMMENT" ]; then
105
133
    KEYCOMMENTLINE="Name-Comment: $KEYCOMMENT"
106
134
fi
113
141
SECRING="`mktemp -t mandos-gpg-secring.XXXXXXXXXX`"
114
142
PUBRING="`mktemp -t mandos-gpg-pubring.XXXXXXXXXX`"
115
143
 
 
144
# Remove temporary files on exit
116
145
trap "rm --force $PUBRING $BATCHFILE; shred --remove $SECRING" EXIT
117
146
 
118
 
# Create batch file for GPG
 
147
# Create batch file for GnuPG
119
148
cat >"$BATCHFILE" <<EOF
120
149
Key-Type: $KEYTYPE
121
150
Key-Length: $KEYLENGTH
130
159
EOF
131
160
 
132
161
umask 027
 
162
 
 
163
# Generate a new key in the key rings
133
164
gpg --no-random-seed-file --quiet --batch --no-tty \
134
165
    --no-default-keyring --no-options --batch \
135
166
    --secret-keyring "$SECRING" --keyring "$PUBRING" \
136
167
    --gen-key "$BATCHFILE"
137
168
rm --force "$BATCHFILE"
138
169
 
 
170
# Backup any old key files
139
171
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
140
172
    2>/dev/null; then
141
173
    shred --remove "$SECKEYFILE"
154
186
    FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
155
187
fi
156
188
 
 
189
# Export keys from key rings to key files
157
190
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
158
191
    --no-default-keyring --secret-keyring "$SECRING" \
159
192
    --keyring "$PUBRING" --export-options export-minimal \
164
197
    --keyring "$PUBRING" --export-options export-minimal \
165
198
    --comment "$FILECOMMENT" --output "$PUBKEYFILE" \
166
199
    --export
 
200
 
 
201
trap - EXIT
 
202
 
 
203
# Remove the key rings
 
204
shred --remove "$SECRING"
 
205
rm --force "$PUBRING"