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