/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
1
#!/bin/sh -e
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
2
# 
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
3
# Mandos key generator - create a new OpenPGP key for a Mandos client
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
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
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
23
VERSION="1.0"
24
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
25
KEYDIR="/etc/mandos"
26
KEYTYPE=DSA
27
KEYLENGTH=1024
28
KEYNAME="`hostname --fqdn`"
29
KEYEMAIL=""
30
KEYCOMMENT="Mandos client key"
31
KEYEXPIRE=0
32
FORCE=no
33
KEYCOMMENT_ORIG="$KEYCOMMENT"
34
35
# Parse options
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
36
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
37
    --longoptions version,help,dir:,type:,length:,name:,email:,comment:,expire:,force \
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
38
    --name "$0" -- "$@"`
39
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
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
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
64
eval set -- "$TEMP"
65
while :; do
66
    case "$1" in
67
	-d|--dir) KEYDIR="$2"; shift 2;;
68
	-t|--type) KEYTYPE="$2"; shift 2;;
69
	-l|--length) KEYLENGTH="$2"; shift 2;;
70
	-n|--name) KEYNAME="$2"; shift 2;;
71
	-e|--email) KEYEMAIL="$2"; shift 2;;
72
	-c|--comment) KEYCOMMENT="$2"; shift 2;;
87 by Teddy Hogeborn
* Makefile: Bug fix: fixed creation of man pages in "plugins.d".
73
	-x|--expire) KEYEXPIRE="$2"; shift 2;;
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
74
	-f|--force) FORCE=yes; shift;;
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
75
	-v|--version) echo "$0 $VERSION"; exit;;
76
	-h|--help) help; exit;;
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
77
	--) shift; break;;
78
	*) echo "Internal error" >&2; exit 1;;
79
    esac
80
done
81
if [ "$#" -gt 0 ]; then
82
    echo "Unknown arguments: '$@'" >&2
83
    exit 1
84
fi
85
86
SECKEYFILE="$KEYDIR/seckey.txt"
87
PUBKEYFILE="$KEYDIR/pubkey.txt"
88
89
# Check for some invalid values
90
if [ -d "$KEYDIR" ]; then :; else
91
    echo "$KEYDIR not a directory" >&2
92
    exit 1
93
fi
94
if [ -w "$KEYDIR" ]; then :; else
95
    echo "Directory $KEYDIR not writeable" >&2
96
    exit 1
97
fi
98
99
if [ -z "$KEYTYPE" ]; then
100
    echo "Empty key type" >&2
101
    exit 1
102
fi
103
104
if [ -z "$KEYNAME" ]; then
105
    echo "Empty key name" >&2
106
    exit 1
107
fi
108
109
if [ -z "$KEYLENGTH" ] || [ "$KEYLENGTH" -lt 512 ]; then
110
    echo "Invalid key length" >&2
111
    exit 1
112
fi
113
114
if [ -z "$KEYEXPIRE" ]; then
115
    echo "Empty key expiration" >&2
116
    exit 1
117
fi
118
119
# Make FORCE be 0 or 1
120
case "$FORCE" in
121
    [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) FORCE=1;;
122
    [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
123
esac
124
125
if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
126
    && [ "$FORCE" -eq 0 ]; then
127
    echo "Refusing to overwrite old key files; use --force" >&2
128
    exit 1
129
fi
130
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
131
# Set lines for GnuPG batch file
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
132
if [ -n "$KEYCOMMENT" ]; then
133
    KEYCOMMENTLINE="Name-Comment: $KEYCOMMENT"
134
fi
135
if [ -n "$KEYEMAIL" ]; then
136
    KEYEMAILLINE="Name-Email: $KEYEMAIL"
137
fi
138
139
# Create temp files
140
BATCHFILE="`mktemp -t mandos-gpg-batch.XXXXXXXXXX`"
141
SECRING="`mktemp -t mandos-gpg-secring.XXXXXXXXXX`"
142
PUBRING="`mktemp -t mandos-gpg-pubring.XXXXXXXXXX`"
143
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
144
# Remove temporary files on exit
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
145
trap "rm --force $PUBRING $BATCHFILE; shred --remove $SECRING" EXIT
146
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
147
# Create batch file for GnuPG
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
148
cat >"$BATCHFILE" <<EOF
149
Key-Type: $KEYTYPE
150
Key-Length: $KEYLENGTH
151
#Key-Usage: encrypt,sign,auth
152
Name-Real: $KEYNAME
153
$KEYCOMMENTLINE
154
$KEYEMAILLINE
155
Expire-Date: $KEYEXPIRE
156
%pubring $PUBRING
157
%secring $SECRING
158
%commit
159
EOF
160
161
umask 027
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
162
163
# Generate a new key in the key rings
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
164
gpg --no-random-seed-file --quiet --batch --no-tty \
70 by Teddy Hogeborn
* initramfs-tools-hook (gpg): Added "--no-random-seed-file" and
165
    --no-default-keyring --no-options --batch \
166
    --secret-keyring "$SECRING" --keyring "$PUBRING" \
167
    --gen-key "$BATCHFILE"
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
168
rm --force "$BATCHFILE"
169
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
170
# Backup any old key files
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
171
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
172
    2>/dev/null; then
173
    shred --remove "$SECKEYFILE"
174
fi
175
if cp --backup=numbered --force "$PUBKEYFILE" "$PUBKEYFILE" \
176
    2>/dev/null; then
177
    rm --force "$PUBKEYFILE"
178
fi
179
180
FILECOMMENT="Mandos client key for $KEYNAME"
181
if [ "$KEYCOMMENT" != "$KEYCOMMENT_ORIG" ]; then
182
    FILECOMMENT="$FILECOMMENT ($KEYCOMMENT)"
183
fi
184
185
if [ -n "$KEYEMAIL" ]; then
186
    FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
187
fi
188
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
189
# Export keys from key rings to key files
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
190
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
87 by Teddy Hogeborn
* Makefile: Bug fix: fixed creation of man pages in "plugins.d".
191
    --no-default-keyring --no-options --secret-keyring "$SECRING" \
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
192
    --keyring "$PUBRING" --export-options export-minimal \
193
    --comment "$FILECOMMENT" --output "$SECKEYFILE" \
194
    --export-secret-keys
195
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
87 by Teddy Hogeborn
* Makefile: Bug fix: fixed creation of man pages in "plugins.d".
196
    --no-default-keyring --no-options --secret-keyring "$SECRING" \
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
197
    --keyring "$PUBRING" --export-options export-minimal \
198
    --comment "$FILECOMMENT" --output "$PUBKEYFILE" \
199
    --export
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
200
201
trap - EXIT
202
203
# Remove the key rings
204
shred --remove "$SECRING"
205
rm --force "$PUBRING"