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.gpg"
38 # host pub key fingerprints file
39 HOST_KEY_FPR_FILE="${SYSDATADIR}/host_keys.fprs"
41 # UTC date in ISO 8601 format if needed
42 DATE=$(date -u '+%FT%T')
44 # unset some environment variables that could screw things up
47 ########################################################################
49 ########################################################################
53 usage: $PGRM <subcommand> [options] [args]
54 Monkeysphere host admin tool.
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]
67 revoke-key [KEYID] generate and/or publish revocation
70 version (v) show version number
73 See ${PGRM}(8) for more info.
77 # function to interact with the gpg keyring
79 GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
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 \
89 # edit key scripts, takes scripts on stdin, and keyID as first input
91 gpg_host --command-fd 0 --edit-key "$@"
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"
103 host_fingerprints() {
104 local fprs=($(cat "$HOST_KEY_FPR_FILE"))
106 log debug "host key fingerprints:"
107 printf '%s\n' "${fprs[@]}" | log debug
108 printf '%s\n' "${fprs[@]}"
111 # check that the service name is well formed
112 check_service_name() {
114 log error "FIX ME: check service name"
117 # fail if host key not present
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."
124 # key input to functions, outputs full fingerprint of specified key if
128 # array of fingerprints
129 local fprs=($(host_fingerprints))
133 failure "You don't appear to have any Monkeysphere host keys.
134 Please run 'monkeysphere-host import-key' to import a key."
140 if [ -z "$keyID" ] ; then
141 failure "Keyring contains multiple keys. Please specify one to act on (see 'monkeysphere-host show-key')."
145 printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
146 || failure "Key '$keyID' not found."
149 # return 0 if user ID was found.
150 # return 1 if user ID not found.
156 # match to only "unknown" user IDs (host has no need for ultimate trust)
157 tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
159 # See whether the requsted user ID is present
160 gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
161 grep -q -x -F "$tmpuidMatch" 2>/dev/null
164 # run command looped over keys
170 local fprs=($(host_fingerprints))
175 if [[ -z "$1" || "$1" == '--all' ]] ; then
178 for key in $keys ; do
179 printf '%s\n' "${fprs[@]}" | grep "${key}$" \
180 || failure "Key '$key' not found."
184 for key in $keys ; do
185 if (( i++ > 0 )) ; then
186 echo "##############################"
192 # show info about the a key
201 export GNUPGHOME=$(msmktempdir)
203 # trap to remove tmp dir if break
204 trap "rm -rf $GNUPGHOME" EXIT
206 # import the host key into the tmp dir
207 gpg --quiet --import <"$HOST_KEY_FILE"
210 TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
211 gpg --export "$id" | openpgp2ssh 2>/dev/null >"$TMPSSH"
213 # get the gpg fingerprint
214 fingerprint=$(gpg --quiet --list-keys \
215 --with-colons --with-fingerprint "$id" \
216 | grep '^fpr:' | cut -d: -f10 )
218 # list the host key info
219 # FIXME: make no-show-keyring work so we don't have to do the grep'ing
220 # FIXME: can we show uid validity somehow?
221 gpg --list-keys --list-options show-unusable-uids "$id" 2>/dev/null \
222 | grep -v "^${GNUPGHOME}/pubring.gpg$" \
225 # list revokers, if there are any
226 revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$id" \
227 | awk -F: '/^rvk:/{ print $10 }' )
228 if [ "$revokers" ] ; then
229 echo "The following keys are allowed to revoke this host key:"
230 for key in $revokers ; do
236 # list the pgp fingerprint
237 echo "OpenPGP fingerprint: $fingerprint"
239 # list the ssh fingerprint
240 echo -n "ssh fingerprint: "
241 ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
243 # remove the tmp file
248 ########################################################################
250 ########################################################################
252 # load configuration file
253 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
254 && . "$MONKEYSPHERE_HOST_CONFIG"
256 # set empty config variable with ones from the environment, or with
258 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
259 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
260 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
261 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
262 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
263 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
266 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
267 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
269 # export variables needed in su invocation
273 export CHECK_KEYSERVER
274 export MONKEYSPHERE_USER
275 export MONKEYSPHERE_GROUP
277 export GNUPGHOME_HOST
279 export HOST_FINGERPRINT
282 if [ "$#" -eq 0 ] ; then
284 failure "Please supply a subcommand."
293 source "${MHSHAREDIR}/import_key"
297 'show-keys'|'show-key'|'show'|'s')
298 multi_key show_key "$@"
301 'set-expire'|'extend-key'|'e')
302 source "${MHSHAREDIR}/set_expire"
306 'add-servicename'|'add-hostname'|'add-name'|'n+')
307 source "${MHSHAREDIR}/add_name"
311 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
312 source "${MHSHAREDIR}/revoke_name"
317 source "${MHSHAREDIR}/add_revoker"
322 source "${MHSHAREDIR}/revoke_key"
326 'publish-keys'|'publish-key'|'publish'|'p')
327 source "${MHSHAREDIR}/publish_key"
328 multi_key publish_key "$@"
332 source "${MHSHAREDIR}/diagnostics"
336 'update-gpg-pub-file')
344 '--help'|'help'|'-h'|'h'|'?')
349 failure "Unknown command: '$COMMAND'
350 Try '$PGRM help' for usage."