3 # monkeysphere-host: Monkeysphere host admin tool
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>
11 # They are Copyright 2008-2010, and are all released under the GPL,
14 ########################################################################
17 # set the pipefail option so pipelines fail on first command failure
22 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
24 . "${SYSSHAREDIR}/defaultenv"
25 . "${SYSSHAREDIR}/common"
27 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
30 # sharedir for host functions
31 MHSHAREDIR="${SYSSHAREDIR}/mh"
33 # datadir for host functions
34 MHDATADIR="${SYSDATADIR}/host"
37 HOST_KEY_FILE="${SYSDATADIR}/host_keys.pub.pgp"
39 # UTC date in ISO 8601 format if needed
40 DATE=$(date -u '+%FT%T')
42 # unset some environment variables that could screw things up
45 ########################################################################
47 ########################################################################
51 usage: $PGRM <subcommand> [options] [args]
52 Monkeysphere host admin tool.
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]
65 revoke-key [KEYID] generate and/or publish revocation
68 version (v) show version number
71 See ${PGRM}(8) for more info.
75 # function to interact with the gpg keyring
77 GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
80 # list the info about the a key, in colon format, to stdout
81 gpg_host_list_keys() {
83 gpg_host --list-keys --with-colons --fixed-list-mode \
84 --with-fingerprint --with-fingerprint \
87 gpg_host --list-keys --with-colons --fixed-list-mode \
88 --with-fingerprint --with-fingerprint
92 # edit key scripts, takes scripts on stdin, and keyID as first input
94 gpg_host --command-fd 0 --edit-key "$@"
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:) \
105 # check that the service name is well formed
106 check_service_name() {
108 log error "FIX ME: check service name"
111 # fail if host key not present
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."
118 # key input to functions, outputs full fingerprint of specified key if
122 # array of fingerprints
123 local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
127 failure "You don't appear to have any Monkeysphere host keys.
128 Please run 'monkeysphere-host import-key' to import a key."
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')."
140 printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
141 || failure "Host key '$keyID' not found."
144 # return 0 if user ID was found.
145 # return 1 if user ID not found.
151 # match to only "unknown" user IDs (host has no need for ultimate trust)
152 tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
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
159 prompt_userid_exists() {
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
169 if [ "${OK/y/Y}" != 'Y' ] ; then
170 failure "Service name not added."
173 log info "Key '%s' is already using the service name '%s'." "$fingerprint" "$userID" >&2
178 # run command looped over keys
184 local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
189 if [[ -z "$1" || "$1" == '--all' ]] ; then
193 for key in $keys ; do
194 if (( i++ > 0 )) ; then
195 echo "##############################"
201 # show info about the a key
210 export GNUPGHOME=$(msmktempdir)
212 # trap to remove tmp dir if break
213 trap "rm -rf $GNUPGHOME" EXIT
215 # import the host key into the tmp dir
216 gpg --quiet --import <"$HOST_KEY_FILE"
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)
224 failure "ID '$id' not found."
228 tmpssh="$GNUPGHOME"/ssh_host_key_rsa_pub
229 gpg --export "$fingerprint" 2>/dev/null \
230 | openpgp2ssh 2>/dev/null >"$tmpssh"
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$" \
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
250 # list the pgp fingerprint
251 echo "OpenPGP fingerprint: $fingerprint"
253 # list the ssh fingerprint
254 echo -n "ssh fingerprint: "
255 ssh-keygen -l -f "$tmpssh" | awk '{ print $1, $2, $4 }'
257 # remove the tmp file
262 ########################################################################
264 ########################################################################
266 # load configuration file
267 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
268 && . "$MONKEYSPHERE_HOST_CONFIG"
270 # set empty config variable with ones from the environment, or with
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}
280 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
281 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
283 # export variables needed in su invocation
287 export CHECK_KEYSERVER
288 export MONKEYSPHERE_USER
289 export MONKEYSPHERE_GROUP
291 export GNUPGHOME_HOST
293 export HOST_FINGERPRINT
296 if [ "$#" -eq 0 ] ; then
298 failure "Please supply a subcommand."
307 source "${MHSHAREDIR}/import_key"
311 'show-keys'|'show-key'|'show'|'s')
312 multi_key show_key "$@"
315 'set-expire'|'extend-key'|'e')
316 source "${MHSHAREDIR}/set_expire"
320 'add-servicename'|'add-hostname'|'add-name'|'n+')
321 source "${MHSHAREDIR}/add_name"
325 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
326 source "${MHSHAREDIR}/revoke_name"
331 source "${MHSHAREDIR}/add_revoker"
336 source "${MHSHAREDIR}/revoke_key"
340 'publish-keys'|'publish-key'|'publish'|'p')
341 source "${MHSHAREDIR}/publish_key"
342 multi_key publish_key "$@"
346 source "${MHSHAREDIR}/diagnostics"
350 'update-pgp-pub-file')
358 '--help'|'help'|'-h'|'h'|'?')
363 failure "Unknown command: '$COMMAND'
364 Try '$PGRM help' for usage."