Merge commit 'greg/master'
[monkeysphere.git] / src / monkeysphere-host
1 #!/usr/bin/env bash
2
3 # monkeysphere-host: Monkeysphere host admin tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@finestructure.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 # Micah Anderson <micah@riseup.net>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 ########################################################################
15 set -e
16
17 # set the pipefail option so pipelines fail on first command failure
18 set -o pipefail
19
20 PGRM=$(basename $0)
21
22 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
23 export SYSSHAREDIR
24 . "${SYSSHAREDIR}/defaultenv"
25 . "${SYSSHAREDIR}/common"
26
27 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
28 export SYSDATADIR
29
30 # sharedir for host functions
31 MHSHAREDIR="${SYSSHAREDIR}/mh"
32
33 # datadir for host functions
34 MHDATADIR="${SYSDATADIR}/host"
35
36 # host pub key files
37 HOST_KEY_FILE="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
38
39 # UTC date in ISO 8601 format if needed
40 DATE=$(date -u '+%FT%T')
41
42 # unset some environment variables that could screw things up
43 unset GREP_OPTIONS
44
45 ########################################################################
46 # FUNCTIONS
47 ########################################################################
48
49 usage() {
50     cat <<EOF >&2
51 usage: $PGRM <subcommand> [options] [args]
52 Monkeysphere host admin tool.
53
54 subcommands:
55  import-key (i) FILE NAME[:PORT]     import existing ssh key to gpg
56  show-key (s)                        output all host key information
57  publish-key (p)                     publish host key to keyserver
58  set-expire (e) [EXPIRE]             set host key expiration
59  add-hostname (n+) NAME[:PORT]       add hostname user ID to host key
60  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
61  add-revoker (r+) KEYID|FILE         add a revoker to the host key
62  revoke-key                          generate and/or publish revocation
63                                      certificate for host key
64
65  version (v)                         show version number
66  help (h,?)                          this help
67
68 See ${PGRM}(8) for more info.
69 EOF
70 }
71
72 # function to interact with the gpg keyring
73 gpg_host() {
74     GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
75 }
76
77 # command to list the info about the host key, in colon format, to
78 # stdout
79 gpg_host_list() {
80     gpg_host --list-keys --with-colons --fixed-list-mode \
81         --with-fingerprint --with-fingerprint \
82         "0x${HOST_FINGERPRINT}!"
83
84 }
85
86 # command for edit key scripts, takes scripts on stdin
87 gpg_host_edit() {
88     gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
89 }
90
91 # export the host public key to the monkeysphere gpg pub key file
92 update_gpg_pub_file() {
93     log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
94     gpg_host --export --armor --export-options export-minimal \
95         "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
96 }
97
98 # load the host fingerprint into the fingerprint variable, using the
99 # export gpg pub key file
100 # FIXME: this seems much less than ideal, with all this temp keyring
101 # stuff.  is there a way we can do this without having to create temp
102 # files?  what if we stored the fingerprint in MHDATADIR/fingerprint?
103 load_fingerprint() {
104     if [ -f "$HOST_KEY_FILE" ] ; then
105         HOST_FINGERPRINT=$( \
106             (FUBAR=$(msmktempdir) && export GNUPGHOME="$FUBAR" \
107             && gpg --quiet --import \
108             && gpg --quiet --list-keys --with-colons --with-fingerprint \
109             && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
110             | grep '^fpr:' | cut -d: -f10 )
111     else
112         failure "host key gpg pub file not found."
113     fi
114 }
115
116 # load the host fingerprint into the fingerprint variable, using the
117 # gpg host secret key
118 load_fingerprint_secret() {
119     HOST_FINGERPRINT=$( \
120         gpg_host --list-secret-key --with-colons --with-fingerprint \
121         | grep '^fpr:' | cut -d: -f10 )
122 }
123
124 # fail if host key present
125 check_host_key() {
126     [ ! -s "$HOST_KEY_FILE" ] \
127         || failure "An OpenPGP host key already exists."
128 }
129
130 # fail if host key not present
131 check_host_no_key() {
132     [ -s "$HOST_KEY_FILE" ] \
133         || failure "You don't appear to have a Monkeysphere host key on this server.
134 Please run 'monkeysphere-host import-key...' first."
135 }
136
137 # output the index of a user ID on the host key
138 # return 1 if user ID not found
139 find_host_userid() {
140     local userID="$1"
141     local tmpuidMatch
142     local line
143
144     # match to only ultimately trusted user IDs
145     tmpuidMatch="u:$(echo $userID | gpg_escape)"
146
147     # find the index of the requsted user ID
148     # NOTE: this is based on circumstantial evidence that the order of
149     # this output is the appropriate index
150     line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
151         grep -n -x -F "$tmpuidMatch" 2>/dev/null)
152
153     if [ "$line" ] ; then
154         echo ${line%%:*}
155         return 0
156     else
157         return 1
158     fi
159 }
160
161 # show info about the host key
162 show_key() {
163     local GNUPGHOME
164     local TMPSSH
165     local revokers
166
167     # tmp gpghome dir
168     export GNUPGHOME=$(msmktempdir)
169
170     # trap to remove tmp dir if break
171     trap "rm -rf $GNUPGHOME" EXIT
172
173     # import the host key into the tmp dir
174     gpg --quiet --import <"$HOST_KEY_FILE"
175
176     # create the ssh key
177     TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
178     gpg --export | openpgp2ssh 2>/dev/null >"$TMPSSH"
179
180     # get the gpg fingerprint
181     HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
182         | grep '^fpr:' | cut -d: -f10 )
183
184     # list the host key info
185     # FIXME: make no-show-keyring work so we don't have to do the grep'ing
186     # FIXME: can we show uid validity somehow?
187     gpg --list-keys --fingerprint \
188         --list-options show-unusable-uids 2>/dev/null \
189         | grep -v "^${GNUPGHOME}/pubring.gpg$" \
190         | egrep -v '^-+$'
191
192     # list revokers, if there are any
193     revokers=$(gpg --list-keys --with-colons --fixed-list-mode \
194         | awk -F: '/^rvk:/{ print $10 }' )
195     if [ "$revokers" ] ; then
196         echo "The following keys are allowed to revoke this host key:"
197         for key in $revokers ; do
198             echo "revoker: $key"
199         done
200         echo
201     fi
202
203     # list the pgp fingerprint
204     echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
205
206     # list the ssh fingerprint
207     echo -n "ssh fingerprint: "
208     ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
209
210     # remove the tmp file
211     trap - EXIT
212     rm -rf "$GNUPGHOME"
213 }
214
215 ########################################################################
216 # MAIN
217 ########################################################################
218
219 # load configuration file
220 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
221     && . "$MONKEYSPHERE_HOST_CONFIG"
222
223 # set empty config variable with ones from the environment, or with
224 # defaults
225 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
226 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
227 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
228 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
229 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
230 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
231
232 # other variables
233 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
234 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
235
236 # export variables needed in su invocation
237 export DATE
238 export LOG_LEVEL
239 export KEYSERVER
240 export CHECK_KEYSERVER
241 export MONKEYSPHERE_USER
242 export MONKEYSPHERE_GROUP
243 export PROMPT
244 export GNUPGHOME_HOST
245 export GNUPGHOME
246 export HOST_FINGERPRINT
247 export LOG_PREFIX
248
249 # get subcommand
250 COMMAND="$1"
251 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
252 shift
253
254 case $COMMAND in
255     'import-key'|'i')
256         check_host_key
257         source "${MHSHAREDIR}/import_key"
258         import_key "$@"
259         ;;
260
261     'show-key'|'show'|'s')
262         check_host_no_key
263         show_key
264         ;;
265
266     'set-expire'|'extend-key'|'e')
267         check_host_no_key
268         load_fingerprint
269         source "${MHSHAREDIR}/set_expire"
270         set_expire "$@"
271         ;;
272
273     'add-hostname'|'add-name'|'n+')
274         check_host_no_key
275         load_fingerprint
276         source "${MHSHAREDIR}/add_hostname"
277         add_hostname "$@"
278         ;;
279
280     'revoke-hostname'|'revoke-name'|'n-')
281         check_host_no_key
282         load_fingerprint
283         source "${MHSHAREDIR}/revoke_hostname"
284         revoke_hostname "$@"
285         ;;
286
287     'add-revoker'|'r+')
288         check_host_no_key
289         load_fingerprint
290         source "${MHSHAREDIR}/add_revoker"
291         add_revoker "$@"
292         ;;
293
294     'revoke-key')
295         check_host_no_key
296         load_fingerprint
297         source "${MHSHAREDIR}/revoke_key"
298         revoke_key "$@"
299         ;;
300
301     'publish-key'|'publish'|'p')
302         check_host_no_key
303         load_fingerprint
304         source "${MHSHAREDIR}/publish_key"
305         publish_key
306         ;;
307
308     'diagnostics'|'d')
309         check_host_no_key
310         load_fingerprint
311         source "${MHSHAREDIR}/diagnostics"
312         diagnostics
313         ;;
314
315     'update-gpg-pub-file')
316         load_fingerprint_secret
317         update_gpg_pub_file
318         ;;
319
320     'version'|'v')
321         version
322         ;;
323
324     '--help'|'help'|'-h'|'h'|'?')
325         usage
326         ;;
327
328     *)
329         failure "Unknown command: '$COMMAND'
330 Type '$PGRM help' for usage."
331         ;;
332 esac