fix show_key function to handle user ID input (needed for import_key)
[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-2010, 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}/host_keys.pub.gpg"
38 # host pub key fingerprints file
39 HOST_KEY_FPR_FILE="${SYSDATADIR}/host_keys.fprs"
40
41 # UTC date in ISO 8601 format if needed
42 DATE=$(date -u '+%FT%T')
43
44 # unset some environment variables that could screw things up
45 unset GREP_OPTIONS
46
47 ########################################################################
48 # FUNCTIONS
49 ########################################################################
50
51 usage() {
52     cat <<EOF >&2
53 usage: $PGRM <subcommand> [options] [args]
54 Monkeysphere host admin tool.
55
56 subcommands:
57  import-key (i) FILE SERVICENAME       import PEM-encoded key from file
58  show-keys (s) [KEYID ...]             output host key information
59  publish-keys (p) [KEYID ...]          publish key(s) to keyserver
60  set-expire (e) EXPIRE [KEYID]         set key expiration
61  add-servicename (n+) SERVICENAME [KEYID]
62                                        add a service name to key
63  revoke-servicename (n-) SERVICENAME [KEYID]
64                                        revoke a service name from key
65  add-revoker (r+) REVOKER_KEYID|FILE [KEYID]
66                                        add a revoker to key
67  revoke-key [KEYID]                    generate and/or publish revocation
68                                        certificate for key
69
70  version (v)                           show version number
71  help (h,?)                            this help
72
73 See ${PGRM}(8) for more info.
74 EOF
75 }
76
77 # function to interact with the gpg keyring
78 gpg_host() {
79     GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
80 }
81
82 # list the info about the a key, in colon format, to stdout
83 gpg_host_list_keys() {
84     gpg_host --list-keys --with-colons --fixed-list-mode \
85         --with-fingerprint --with-fingerprint \
86         "$1"
87 }
88
89 # edit key scripts, takes scripts on stdin, and keyID as first input
90 gpg_host_edit() {
91     gpg_host --command-fd 0 --edit-key "$@"
92 }
93
94 # export the monkeysphere gpg pub key file
95 update_gpg_pub_file() {
96     log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
97     gpg_host --export --armor --export-options export-minimal > "$HOST_KEY_FILE"
98     log debug "updating fingerprint file '$HOST_KEY_FPR_FILE'..."
99     gpg_host --list-secret-key --with-colons --with-fingerprint \
100         | awk -F: '/^fpr:/{ print $10 }' > "$HOST_KEY_FPR_FILE"
101 }
102
103 host_fingerprints() {
104     local fprs=($(cat "$HOST_KEY_FPR_FILE"))
105
106     log debug "host key fingerprints:"
107     printf '%s\n' "${fprs[@]}" | log debug
108     printf '%s\n' "${fprs[@]}"
109 }
110
111 # check that the service name is well formed
112 check_service_name() {
113     local name="$1"
114     log error "FIX ME: check service name"
115 }
116
117 # fail if host key not present
118 check_no_keys() {
119     [ -s "$HOST_KEY_FILE" ] || [ -s "$HOST_KEY_FPR_FILE" ] \
120         || failure "You don't appear to have a Monkeysphere host key on this server.
121 Please run 'monkeysphere-host import-key' import a key."
122 }
123
124 # key input to functions, outputs full fingerprint of specified key if
125 # found
126 check_key_input() {
127     local keyID="$1"
128     # array of fingerprints
129     local fprs=($(host_fingerprints))
130
131     case ${#fprs[@]} in
132         0)
133             failure "You don't appear to have any Monkeysphere host keys.
134 Please run 'monkeysphere-host import-key' to import a key."
135             ;;
136         1)
137             :
138             ;;
139         *)
140             if [ -z "$keyID" ] ; then
141                 failure "Your host keyring contains multiple keys.
142 Please specify one to act on (see 'monkeysphere-host show-key')."
143             fi
144             ;;
145     esac
146     printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
147         || failure "Host key '$keyID' not found."
148 }
149
150 # return 0 if user ID was found.
151 # return 1 if user ID not found.
152 check_key_userid() {
153     local keyID="$1"
154     local userID="$2"
155     local tmpuidMatch
156
157     # match to only "unknown" user IDs (host has no need for ultimate trust)
158     tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
159
160     # See whether the requsted user ID is present
161     gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
162         grep -q -x -F "$tmpuidMatch" 2>/dev/null
163 }
164
165 # run command looped over keys
166 multi_key() {
167     local cmd="$1"
168     shift
169     local keys=$@
170     local i=0
171     local fprs=($(host_fingerprints))
172     local key
173
174     check_no_keys
175
176     if [[ -z "$1" || "$1" == '--all' ]] ; then
177         keys="${fprs[@]}"
178     fi
179
180     for key in $keys ; do
181         if (( i++ > 0 )) ; then
182             echo "##############################"
183         fi
184         eval "$cmd" "$key"
185     done
186 }
187
188 # show info about the a key
189 show_key() {
190     local id="$1"
191     local GNUPGHOME
192     local fingerprint
193     local tmpssh
194     local revokers
195
196     # tmp gpghome dir
197     export GNUPGHOME=$(msmktempdir)
198
199     # trap to remove tmp dir if break
200     trap "rm -rf $GNUPGHOME" EXIT
201
202     # import the host key into the tmp dir
203     gpg --quiet --import <"$HOST_KEY_FILE"
204
205     # get the gpg fingerprint
206     if gpg --quiet --list-keys \
207         --with-colons --with-fingerprint "$id" \
208         | grep '^fpr:' | cut -d: -f10 > "$GNUPGHOME"/fingerprint ; then
209         fingerprint=$(cat "$GNUPGHOME"/fingerprint)
210     else
211         failure "ID '$id' not found."
212     fi
213
214     # create the ssh key
215     tmpssh="$GNUPGHOME"/ssh_host_key_rsa_pub
216     gpg --export "$fingerprint" 2>/dev/null \
217         | openpgp2ssh 2>/dev/null >"$tmpssh"
218
219     # list the host key info
220     # FIXME: make no-show-keyring work so we don't have to do the grep'ing
221     # FIXME: can we show uid validity somehow?
222     gpg --list-keys --list-options show-unusable-uids "$fingerprint" 2>/dev/null \
223         | grep -v "^${GNUPGHOME}/pubring.gpg$" \
224         | egrep -v '^-+$'
225
226     # list revokers, if there are any
227     revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$fingerprint" \
228         | awk -F: '/^rvk:/{ print $10 }' )
229     if [ "$revokers" ] ; then
230         echo "The following keys are allowed to revoke this host key:"
231         for key in $revokers ; do
232             echo "revoker: $key"
233         done
234         echo
235     fi
236
237     # list the pgp fingerprint
238     echo "OpenPGP fingerprint: $fingerprint"
239
240     # list the ssh fingerprint
241     echo -n "ssh fingerprint: "
242     ssh-keygen -l -f "$tmpssh" | awk '{ print $1, $2, $4 }'
243
244     # remove the tmp file
245     trap - EXIT
246     rm -rf "$GNUPGHOME"
247 }
248
249 ########################################################################
250 # MAIN
251 ########################################################################
252
253 # load configuration file
254 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
255     && . "$MONKEYSPHERE_HOST_CONFIG"
256
257 # set empty config variable with ones from the environment, or with
258 # defaults
259 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
260 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
261 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
262 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
263 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
264 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
265
266 # other variables
267 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
268 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
269
270 # export variables needed in su invocation
271 export DATE
272 export LOG_LEVEL
273 export KEYSERVER
274 export CHECK_KEYSERVER
275 export MONKEYSPHERE_USER
276 export MONKEYSPHERE_GROUP
277 export PROMPT
278 export GNUPGHOME_HOST
279 export GNUPGHOME
280 export HOST_FINGERPRINT
281 export LOG_PREFIX
282
283 if [ "$#" -eq 0 ] ; then 
284     usage
285     failure "Please supply a subcommand."
286 fi
287
288 # get subcommand
289 COMMAND="$1"
290 shift
291
292 case $COMMAND in
293     'import-key'|'i')
294         source "${MHSHAREDIR}/import_key"
295         import_key "$@"
296         ;;
297
298     'show-keys'|'show-key'|'show'|'s')
299         multi_key show_key "$@"
300         ;;
301
302     'set-expire'|'extend-key'|'e')
303         source "${MHSHAREDIR}/set_expire"
304         set_expire "$@"
305         ;;
306
307     'add-servicename'|'add-hostname'|'add-name'|'n+')
308         source "${MHSHAREDIR}/add_name"
309         add_name "$@"
310         ;;
311
312     'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
313         source "${MHSHAREDIR}/revoke_name"
314         revoke_name "$@"
315         ;;
316
317     'add-revoker'|'r+')
318         source "${MHSHAREDIR}/add_revoker"
319         add_revoker "$@"
320         ;;
321
322     'revoke-key')
323         source "${MHSHAREDIR}/revoke_key"
324         revoke_key "$@"
325         ;;
326
327     'publish-keys'|'publish-key'|'publish'|'p')
328         source "${MHSHAREDIR}/publish_key"
329         multi_key publish_key "$@"
330         ;;
331
332     'diagnostics'|'d')
333         source "${MHSHAREDIR}/diagnostics"
334         diagnostics
335         ;;
336
337     'update-gpg-pub-file')
338         update_gpg_pub_file
339         ;;
340
341     'version'|'v')
342         version
343         ;;
344
345     '--help'|'help'|'-h'|'h'|'?')
346         usage
347         ;;
348
349     *)
350         failure "Unknown command: '$COMMAND'
351 Try '$PGRM help' for usage."
352         ;;
353 esac