/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-08-24 06:17:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080824061702-zxrru4r1vxmx4tuq
* Makefile (PREFIX, CONFDIR, MANDIR): Use $(DESTDIR).
  (install-server, install-client): Use "install --directory" instead
                                    of mkdir.

* mandos-keygen: New options --subtype and --sublength.
  (trap): Added semicolons and backslashes.
  (gpg): Added "--enable-dsa2" to all invocations.

* mandos-keygen.xml: Changed single quotes to double quotes for
                     consistency.
  (/refentry/refentryinfo/copyright) Split copyright holders.
  (SYNOPSIS): Added "--subtype", "--sublength", "-s", and "-L".
  (OPTIONS): Document the subtype and sublength options.
  (SECURITY): Also note the "--subtype" and "--sublength" options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh -e
 
2
 
3
# Mandos key generator - create a new OpenPGP key for a Mandos client
 
4
 
5
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
6
 
7
# This program is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation, either version 3 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
#     This program is distributed in the hope that it will be useful,
 
13
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#     GNU General Public License for more details.
 
16
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
20
# Contact the authors at <mandos@fukt.bsnet.se>.
 
21
 
22
 
 
23
VERSION="1.0"
 
24
 
 
25
KEYDIR="/etc/mandos"
 
26
KEYTYPE=DSA
 
27
KEYLENGTH=1024
 
28
SUBKEYTYPE=ELG-E
 
29
SUBKEYLENGTH=2048
 
30
KEYNAME="`hostname --fqdn`"
 
31
KEYEMAIL=""
 
32
KEYCOMMENT="Mandos client key"
 
33
KEYEXPIRE=0
 
34
FORCE=no
 
35
KEYCOMMENT_ORIG="$KEYCOMMENT"
 
36
 
 
37
# Parse options
 
38
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
 
39
    --longoptions version,help,dir:,type:,length:,name:,email:,comment:,expire:,force \
 
40
    --name "$0" -- "$@"`
 
41
 
 
42
help(){
 
43
cat <<EOF
 
44
Usage: `basename $0` [options]
 
45
 
 
46
Options:
 
47
  -v, --version         Show program's version number and exit
 
48
  -h, --help            Show this help message and exit
 
49
  -d DIR, --dir DIR     Target directory for key files
 
50
  -t TYPE, --type TYPE  Key type.  Default is DSA.
 
51
  -l BITS, --length BITS
 
52
                        Key length in bits.  Default is 1024.
 
53
  -s TYPE, --subtype TYPE
 
54
                        Subkey type.  Default is ELG-E.
 
55
  -L BITS, --sublength BITS
 
56
                        Subkey length in bits.  Default is 2048.
 
57
  -n NAME, --name NAME  Name of key.  Default is the FQDN.
 
58
  -e EMAIL, --email EMAIL
 
59
                        Email address of key.  Default is empty.
 
60
  -c COMMENT, --comment COMMENT
 
61
                        Comment field for key.  The default value is
 
62
                        "Mandos client key".
 
63
  -x TIME, --expire TIME
 
64
                        Key expire time.  Default is no expiration.
 
65
                        See gpg(1) for syntax.
 
66
  -f, --force           Force overwriting old keys.
 
67
EOF
 
68
}
 
69
 
 
70
eval set -- "$TEMP"
 
71
while :; do
 
72
    case "$1" in
 
73
        -d|--dir) KEYDIR="$2"; shift 2;;
 
74
        -t|--type) KEYTYPE="$2"; shift 2;;
 
75
        -s|--subtype) SUBKEYTYPE="$2"; shift 2;;
 
76
        -l|--length) KEYLENGTH="$2"; shift 2;;
 
77
        -L|--sublength) SUBKEYLENGTH="$2"; shift 2;;
 
78
        -n|--name) KEYNAME="$2"; shift 2;;
 
79
        -e|--email) KEYEMAIL="$2"; shift 2;;
 
80
        -c|--comment) KEYCOMMENT="$2"; shift 2;;
 
81
        -x|--expire) KEYEXPIRE="$2"; shift 2;;
 
82
        -f|--force) FORCE=yes; shift;;
 
83
        -v|--version) echo "$0 $VERSION"; exit;;
 
84
        -h|--help) help; exit;;
 
85
        --) shift; break;;
 
86
        *) echo "Internal error" >&2; exit 1;;
 
87
    esac
 
88
done
 
89
if [ "$#" -gt 0 ]; then
 
90
    echo "Unknown arguments: '$@'" >&2
 
91
    exit 1
 
92
fi
 
93
 
 
94
SECKEYFILE="$KEYDIR/seckey.txt"
 
95
PUBKEYFILE="$KEYDIR/pubkey.txt"
 
96
 
 
97
# Check for some invalid values
 
98
if [ -d "$KEYDIR" ]; then :; else
 
99
    echo "$KEYDIR not a directory" >&2
 
100
    exit 1
 
101
fi
 
102
if [ -w "$KEYDIR" ]; then :; else
 
103
    echo "Directory $KEYDIR not writeable" >&2
 
104
    exit 1
 
105
fi
 
106
 
 
107
if [ -z "$KEYTYPE" ]; then
 
108
    echo "Empty key type" >&2
 
109
    exit 1
 
110
fi
 
111
 
 
112
if [ -z "$KEYNAME" ]; then
 
113
    echo "Empty key name" >&2
 
114
    exit 1
 
115
fi
 
116
 
 
117
if [ -z "$KEYLENGTH" ] || [ "$KEYLENGTH" -lt 512 ]; then
 
118
    echo "Invalid key length" >&2
 
119
    exit 1
 
120
fi
 
121
 
 
122
if [ -z "$KEYEXPIRE" ]; then
 
123
    echo "Empty key expiration" >&2
 
124
    exit 1
 
125
fi
 
126
 
 
127
# Make FORCE be 0 or 1
 
128
case "$FORCE" in
 
129
    [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) FORCE=1;;
 
130
    [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
 
131
esac
 
132
 
 
133
if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
 
134
    && [ "$FORCE" -eq 0 ]; then
 
135
    echo "Refusing to overwrite old key files; use --force" >&2
 
136
    exit 1
 
137
fi
 
138
 
 
139
# Set lines for GnuPG batch file
 
140
if [ -n "$KEYCOMMENT" ]; then
 
141
    KEYCOMMENTLINE="Name-Comment: $KEYCOMMENT"
 
142
fi
 
143
if [ -n "$KEYEMAIL" ]; then
 
144
    KEYEMAILLINE="Name-Email: $KEYEMAIL"
 
145
fi
 
146
 
 
147
# Create temp files
 
148
BATCHFILE="`mktemp -t mandos-gpg-batch.XXXXXXXXXX`"
 
149
SECRING="`mktemp -t mandos-gpg-secring.XXXXXXXXXX`"
 
150
PUBRING="`mktemp -t mandos-gpg-pubring.XXXXXXXXXX`"
 
151
 
 
152
# Remove temporary files on exit
 
153
trap "
 
154
set +e; \
 
155
rm --force $PUBRING $BATCHFILE; \
 
156
shred --remove $SECRING; \
 
157
stty echo; \
 
158
" EXIT
 
159
 
 
160
# Create batch file for GnuPG
 
161
cat >"$BATCHFILE" <<EOF
 
162
Key-Type: $KEYTYPE
 
163
Key-Length: $KEYLENGTH
 
164
#Key-Usage: encrypt,sign,auth
 
165
Subkey-Type: $SUBKEYTYPE
 
166
Subkey-Length: $SUBKEYLENGTH
 
167
#Subkey-Usage: encrypt,sign,auth
 
168
Name-Real: $KEYNAME
 
169
$KEYCOMMENTLINE
 
170
$KEYEMAILLINE
 
171
Expire-Date: $KEYEXPIRE
 
172
#Preferences: <string>
 
173
#Handle: <no-spaces>
 
174
%pubring $PUBRING
 
175
%secring $SECRING
 
176
%commit
 
177
EOF
 
178
 
 
179
umask 027
 
180
 
 
181
# Generate a new key in the key rings
 
182
gpg --no-random-seed-file --quiet --batch --no-tty \
 
183
    --no-default-keyring --no-options --enable-dsa2 \
 
184
    --secret-keyring "$SECRING" --keyring "$PUBRING" \
 
185
    --gen-key "$BATCHFILE"
 
186
rm --force "$BATCHFILE"
 
187
 
 
188
# Backup any old key files
 
189
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
 
190
    2>/dev/null; then
 
191
    shred --remove "$SECKEYFILE"
 
192
fi
 
193
if cp --backup=numbered --force "$PUBKEYFILE" "$PUBKEYFILE" \
 
194
    2>/dev/null; then
 
195
    rm --force "$PUBKEYFILE"
 
196
fi
 
197
 
 
198
FILECOMMENT="Mandos client key for $KEYNAME"
 
199
if [ "$KEYCOMMENT" != "$KEYCOMMENT_ORIG" ]; then
 
200
    FILECOMMENT="$FILECOMMENT ($KEYCOMMENT)"
 
201
fi
 
202
 
 
203
if [ -n "$KEYEMAIL" ]; then
 
204
    FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
 
205
fi
 
206
 
 
207
# Export keys from key rings to key files
 
208
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
 
209
    --no-default-keyring --no-options --enable-dsa2 \
 
210
    --secret-keyring "$SECRING" --keyring "$PUBRING" \
 
211
    --export-options export-minimal --comment "$FILECOMMENT" \
 
212
    --output "$SECKEYFILE" --export-secret-keys
 
213
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
 
214
    --no-default-keyring --no-options --enable-dsa2 \
 
215
    --secret-keyring "$SECRING" --keyring "$PUBRING" \
 
216
    --export-options export-minimal --comment "$FILECOMMENT" \
 
217
    --output "$PUBKEYFILE" --export
 
218
 
 
219
trap - EXIT
 
220
 
 
221
# Remove the key rings
 
222
shred --remove "$SECRING"
 
223
rm --force "$PUBRING"