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:-"__SYSSHAREDIR_PREFIX__/share/monkeysphere"}
24 . "${SYSSHAREDIR}/defaultenv"
25 . "${SYSSHAREDIR}/common"
27 # sharedir for host functions
28 MHSHAREDIR="${SYSSHAREDIR}/mh"
30 # datadir for host functions
31 MHDATADIR="${SYSDATADIR}/host"
34 HOST_KEY_FILE="${SYSDATADIR}/host_keys.pub.pgp"
36 # UTC date in ISO 8601 format if needed
37 DATE=$(date -u '+%FT%T')
39 # unset some environment variables that could screw things up
42 ########################################################################
44 ########################################################################
48 usage: $PGRM <subcommand> [options] [args]
49 Monkeysphere host admin tool.
52 import-key (i) FILE SERVICENAME import PEM-encoded key from file
53 show-keys (s) [KEYID ...] output host key information
54 publish-keys (p) [KEYID ...] publish key(s) to keyserver
55 set-expire (e) EXPIRE [KEYID] set key expiration
56 add-servicename (n+) SERVICENAME [KEYID]
57 add a service name to key
58 revoke-servicename (n-) SERVICENAME [KEYID]
59 revoke a service name from key
60 add-revoker (r+) REVOKER_KEYID|FILE [KEYID]
62 revoke-key [KEYID] generate and/or publish revocation
65 version (v) show version number
68 See ${PGRM}(8) for more info.
72 # function to interact with the gpg keyring
74 GNUPGHOME="$GNUPGHOME_HOST" LC_ALL=C gpg --no-auto-check-trustdb --trust-model=always --no-greeting --quiet --no-tty "$@"
77 # list the info about the a key, in colon format, to stdout
78 gpg_host_list_keys() {
80 gpg_host --list-keys --with-colons --fixed-list-mode \
81 --with-fingerprint --with-fingerprint \
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 OpenPGP pub key file
95 update_pgp_pub_file() {
96 log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
97 gpg_host --export --armor --export-options export-minimal \
98 $(gpg_host --list-secret-keys --with-colons --fingerprint | grep ^fpr | cut -f10 -d:) \
102 # check that the service name is well formed. we assume that the
103 # service name refers to a host; DNS labels for host names are limited
104 # to a very small range of characters (see RFC 1912, section 2.1).
106 # FIXME: i'm failing to check here for label components that are
107 # all-number (e.g. ssh://666.666), which are technically not allowed
108 # (though some exist on the 'net, apparently)
110 # FIXME: this will probably misbehave if raw IP addresses are provided,
111 # either IPv4 or IPv6 using the bracket notation.
113 # FIXME: this doesn't address the use of hashed User IDs.
115 check_service_name() {
123 failure "You must supply a service name to check"
125 printf '%s' "$name" | perl -n -e '($str = $_) =~ s/\s//g ; exit !(lc($str) eq $_);' || \
126 failure "Not a valid service name: '$name'
128 Service names should be canonicalized to all lower-case,
131 [[ "$name" =~ ^[a-z0-9./:-]+$ ]] || \
132 failure "Not a valid service name: '$name'
134 Service names should contain only lower-case ASCII letters
135 numbers, dots (.), hyphens (-), slashes (/), and a colon (:).
136 If you are using non-ASCII characters (e.g. IDN), you should
137 use the canonicalized ASCII (NAMEPREP -> Punycode) representation
140 [[ "$name" =~ \. ]] || \
141 failure "Not a valid service name: '$name'
143 Service names should use fully-qualified domain names (FQDN), but the
144 domain name you chose appears to only have the local part. For
145 example: don't use 'ssh://foo' ; use 'ssh://foo.example.com' instead."
147 [[ "$name" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?://[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.|((\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+))(:[1-9][0-9]{0,4})?$ ]] || \
148 failure "Not a valid service name: '$name'
150 Service names look like <scheme>://full.example.com[:<portnumber>],
151 where <scheme> is something like ssh or https, and <portnumber> is
152 a decimal number (supplied only if the service is on a non-standard
155 scheme=$(cut -f1 -d: <<<"$name")
156 port=$(cut -f3 -d: <<<"$name")
158 # check that the scheme name is found in the system services
160 available_=$(get_port_for_service "$scheme") || \
161 log error "Error looking up service scheme named '%s'" "$scheme"
163 # FIXME: if the service isn't found, or does not have a port, what
164 # should we do? at the moment, we're just warning.
166 if [ -n "$port" ]; then
167 # check that the port number is a legitimate port number (> 0, < 65536)
168 [ "$port" -gt 0 ] && [ "$port" -lt 65536 ] || \
169 failure "The given port number should be greater than 0 and
170 less than 65536. '$port' is not OK"
172 # if the port number is given, and the scheme is in the services
173 # database, check that the port number does *not* match the
175 if (printf '%s' "$assigned_ports" | grep -q -F -x "$port" ) ; then
176 failure $(printf "The scheme %s uses port number %d by default.
177 You should leave off the port number if it is the default" "$scheme" "$port")
183 # fail if host key not present
185 [ -s "$HOST_KEY_FILE" ] \
186 || failure "You don't appear to have a Monkeysphere host key on this server.
187 Please run 'monkeysphere-host import-key' import a key."
190 # key input to functions, outputs full fingerprint of specified key if
194 # array of fingerprints
195 local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
199 failure "You don't appear to have any Monkeysphere host keys.
200 Please run 'monkeysphere-host import-key' to import a key."
206 if [ -z "$keyID" ] ; then
207 failure "Your host keyring contains multiple keys.
208 Please specify one to act on (see 'monkeysphere-host show-keys')."
212 printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
213 || failure "Host key '$keyID' not found."
216 # return 0 if user ID was found.
217 # return 1 if user ID not found.
223 # match to only "unknown" user IDs (host has no need for ultimate trust)
224 tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
226 # See whether the requsted user ID is present
227 gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
228 grep -q -x -F "$tmpuidMatch" 2>/dev/null
231 prompt_userid_exists() {
236 if gpgOut=$(gpg_host_list_keys "=${userID}" 2>/dev/null) ; then
237 fingerprint=$(echo "$gpgOut" | grep '^fpr:' | cut -d: -f10)
238 if [ "$PROMPT" != "false" ] ; then
239 printf "Service name '%s' is already being used by key '%s'.\nAre you sure you want to use it again? (y/N) " "$userID" "$fingerprint" >&2
241 if [ "${OK/y/Y}" != 'Y' ] ; then
242 failure "Service name not added."
245 log info "Key '%s' is already using the service name '%s'." "$fingerprint" "$userID" >&2
250 # run command looped over keys
260 log debug "listing primary fingerprints from $HOST_KEY_FILE"
261 local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
262 log debug "obtained the following fingerprints: $fprs"
264 if [[ -z "$1" || "$1" == '--all' ]] ; then
265 log debug "publishing all keys"
269 log debug "using keys: $keys"
271 for key in $keys ; do
272 if (( i++ > 0 )) ; then
275 log debug "invoking $cmd $key"
280 # show info about the a key
288 export GNUPGHOME=$(msmktempdir)
290 # trap to remove tmp dir if break
291 trap "rm -rf $GNUPGHOME" EXIT
293 # import the host key into the tmp dir
294 gpg --quiet --import <"$HOST_KEY_FILE"
296 # get the gpg fingerprint
297 if gpg --quiet --list-keys \
298 --with-colons --with-fingerprint "$id" \
299 | grep '^fpr:' | cut -d: -f10 > "$GNUPGHOME"/fingerprint ; then
300 fingerprint=$(cat "$GNUPGHOME"/fingerprint)
302 failure "ID '$id' not found."
305 # list the host key info
306 # FIXME: make no-show-keyring work so we don't have to do the grep'ing
307 # FIXME: can we show uid validity somehow?
308 gpg --list-keys --list-options show-unusable-uids "$fingerprint" 2>/dev/null \
309 | grep -v "^${GNUPGHOME}/pubring.gpg$" \
313 # list revokers, if there are any
314 revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$fingerprint" \
315 | awk -F: '/^rvk:/{ print $10 }' )
316 if [ "$revokers" ] ; then
317 echo "The following keys are allowed to revoke this host key:"
318 for key in $revokers ; do
323 # list the pgp fingerprint
324 echo "OpenPGP fingerprint: $fingerprint"
326 # list the ssh fingerprint
327 printf "ssh fingerprint: %s\n" \
328 "$(gpg --export --no-armor "$fingerprint" 2>/dev/null | "$SYSSHAREDIR/keytrans" openpgp2sshfpr "$fingerprint")"
330 # remove the tmp file
335 ########################################################################
337 ########################################################################
339 # load configuration file
340 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
341 && . "$MONKEYSPHERE_HOST_CONFIG"
343 # set empty config variable with ones from the environment, or with
345 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
346 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
347 log debug "using keyserver: $KEYSERVER"
348 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
349 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
350 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
351 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
354 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
355 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
357 # export variables needed in su invocation
361 export CHECK_KEYSERVER
362 export MONKEYSPHERE_USER
363 export MONKEYSPHERE_GROUP
365 export GNUPGHOME_HOST
367 export HOST_FINGERPRINT
370 if [ "$#" -eq 0 ] ; then
372 failure "Please supply a subcommand."
380 'import-key'|'import'|'i')
381 source "${MHSHAREDIR}/import_key"
385 'show-keys'|'show-key'|'show'|'s')
386 multi_key show_key "$@"
389 'set-expire'|'extend-key'|'extend'|'e')
390 source "${MHSHAREDIR}/set_expire"
394 'add-servicename'|'add-hostname'|'add-name'|'n+')
395 source "${MHSHAREDIR}/add_name"
399 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
400 source "${MHSHAREDIR}/revoke_name"
405 source "${MHSHAREDIR}/add_revoker"
410 source "${MHSHAREDIR}/revoke_key"
414 'publish-keys'|'publish-key'|'publish'|'p')
415 source "${MHSHAREDIR}/publish_key"
416 multi_key publish_key "$@"
420 source "${MHSHAREDIR}/diagnostics"
424 'update-pgp-pub-file')
428 'version'|'--version'|'v')
432 '--help'|'help'|'-h'|'h'|'?')
437 failure "Unknown command: '$COMMAND'
438 Try '$PGRM help' for usage."