/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
94 by Teddy Hogeborn
* clients.conf ([DEFAULT]/checker): Update to new default value.
145
trap "
146
set +e
147
rm --force $PUBRING $BATCHFILE;
148
shred --remove $SECRING;
149
stty echo
150
" EXIT
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
151
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
152
# Create batch file for GnuPG
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
153
cat >"$BATCHFILE" <<EOF
154
Key-Type: $KEYTYPE
155
Key-Length: $KEYLENGTH
156
#Key-Usage: encrypt,sign,auth
157
Name-Real: $KEYNAME
158
$KEYCOMMENTLINE
159
$KEYEMAILLINE
160
Expire-Date: $KEYEXPIRE
161
%pubring $PUBRING
162
%secring $SECRING
163
%commit
164
EOF
165
166
umask 027
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
167
168
# Generate a new key in the key rings
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
169
gpg --no-random-seed-file --quiet --batch --no-tty \
70 by Teddy Hogeborn
* initramfs-tools-hook (gpg): Added "--no-random-seed-file" and
170
    --no-default-keyring --no-options --batch \
171
    --secret-keyring "$SECRING" --keyring "$PUBRING" \
172
    --gen-key "$BATCHFILE"
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
173
rm --force "$BATCHFILE"
174
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
175
# Backup any old key files
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
176
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
177
    2>/dev/null; then
178
    shred --remove "$SECKEYFILE"
179
fi
180
if cp --backup=numbered --force "$PUBKEYFILE" "$PUBKEYFILE" \
181
    2>/dev/null; then
182
    rm --force "$PUBKEYFILE"
183
fi
184
185
FILECOMMENT="Mandos client key for $KEYNAME"
186
if [ "$KEYCOMMENT" != "$KEYCOMMENT_ORIG" ]; then
187
    FILECOMMENT="$FILECOMMENT ($KEYCOMMENT)"
188
fi
189
190
if [ -n "$KEYEMAIL" ]; then
191
    FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
192
fi
193
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
194
# Export keys from key rings to key files
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
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 "$SECKEYFILE" \
199
    --export-secret-keys
200
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".
201
    --no-default-keyring --no-options --secret-keyring "$SECRING" \
67 by Teddy Hogeborn
* mandos-keygen: New program to generate new client keys on
202
    --keyring "$PUBRING" --export-options export-minimal \
203
    --comment "$FILECOMMENT" --output "$PUBKEYFILE" \
204
    --export
73 by Teddy Hogeborn
* Makefile (COVERAGE): Change back to "--coverage".
205
206
trap - EXIT
207
208
# Remove the key rings
209
shred --remove "$SECRING"
210
rm --force "$PUBRING"