3 # monkeysphere-host: Monkeysphere host admin tool
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
10 # They are Copyright 2008, and are all released under the GPL, version 3
13 ########################################################################
16 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
18 . "${SYSSHAREDIR}/common" || exit 1
20 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere/host"}
23 # monkeysphere temp directory, in sysdatadir to enable atomic moves of
24 # authorized_keys files
25 MSTMPDIR="${SYSDATADIR}/tmp"
28 # UTC date in ISO 8601 format if needed
29 DATE=$(date -u '+%FT%T')
31 # unset some environment variables that could screw things up
37 ########################################################################
39 ########################################################################
43 usage: $PGRM <subcommand> [options] [args]
44 Monkeysphere host admin tool.
47 show-key (s) output all host key information
48 extend-key (e) EXPIRE extend host key expiration
49 add-hostname (n+) NAME[:PORT] add hostname user ID to host key
50 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
51 add-revoker (o) FINGERPRINT add a revoker to the host key
52 revoke-key (r) revoke host key
53 publish-key (p) publish server host key to keyserver
56 import-key (i) import existing ssh key to gpg
57 --hostname (-h) NAME[:PORT] hostname for key user ID
58 --keyfile (-f) FILE key file to import
59 --expire (-e) EXPIRE date to expire
60 gen-key (g) generate gpg key for the host
61 --hostname (-h) NAME[:PORT] hostname for key user ID
62 --length (-l) BITS key length in bits (2048)
63 --expire (-e) EXPIRE date to expire
64 --revoker (-r) FINGERPRINT add a revoker
65 diagnostics (d) monkeysphere host status
67 version (v) show version number
73 # function to run command as monkeysphere user
74 su_monkeysphere_user() {
75 # if the current user is the monkeysphere user, then just eval
77 if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
80 # otherwise su command as monkeysphere user
82 su "$MONKEYSPHERE_USER" -c "$@"
86 # function to interact with the host gnupg keyring
90 GNUPGHOME="$GNUPGHOME_HOST"
93 # NOTE: we supress this warning because we need the monkeysphere
94 # user to be able to read the host pubring. we realize this might
95 # be problematic, but it's the simplest solution, without too much
97 gpg --no-permission-warning "$@"
100 # always reset the permissions on the host pubring so that the
101 # monkeysphere user can read the trust signatures
102 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
103 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
108 # check if user is root
110 [ $(id -u 2>/dev/null) = '0' ]
113 # check that user is root, for functions that require root access
115 is_root || failure "You must be root to run this command."
118 # output just key fingerprint
119 fingerprint_server_key() {
120 # set the pipefail option so functions fails if can't read sec key
123 gpg_host --list-secret-keys --fingerprint \
124 --with-colons --fixed-list-mode 2> /dev/null | \
125 grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
128 # function to check for host secret key
129 check_host_keyring() {
130 fingerprint_server_key >/dev/null \
131 || failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-server gen-key' first."
134 # output key information
140 # FIXME: you shouldn't have to be root to see the host key fingerprint
143 fingerprintPGP=$(fingerprint_server_key)
144 gpg_authentication "--fingerprint --list-key --list-options show-unusable-uids $fingerprintPGP" 2>/dev/null
145 echo "OpenPGP fingerprint: $fingerprintPGP"
147 log info "You must be root to see host OpenPGP fingerprint."
151 if [ -f "${SYSDATADIR}/ssh_host_rsa_key.pub" ] ; then
152 fingerprintSSH=$(ssh-keygen -l -f "${SYSDATADIR}/ssh_host_rsa_key.pub" | \
153 awk '{ print $1, $2, $4 }')
154 echo "ssh fingerprint: $fingerprintSSH"
156 log info "SSH host key not found."
163 # import an existing ssh key to a gpg key
165 local hostName=$(hostname -f)
166 local keyFile="/etc/ssh/ssh_host_rsa_key"
170 # check for presense of secret key
171 # FIXME: is this the proper test to be doing here?
172 fingerprint_server_key >/dev/null \
173 && failure "An OpenPGP host key already exists."
191 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
192 failure "Unknown option '$1'.
193 Type '$PGRM help' for usage."
200 if [ ! -f "$keyFile" ] ; then
201 failure "SSH secret key file '$keyFile' not found."
204 userID="ssh://${hostName}"
206 # prompt about key expiration if not specified
207 keyExpire=$(get_gpg_expiration "$keyExpire")
209 echo "The following key parameters will be used for the host private key:"
210 echo "Import: $keyFile"
211 echo "Name-Real: $userID"
212 echo "Expire-Date: $keyExpire"
214 read -p "Import key? (Y/n) " OK; OK=${OK:=Y}
215 if [ ${OK/y/Y} != 'Y' ] ; then
219 log verbose "importing ssh key..."
220 # translate ssh key to a private key
222 pem2openpgp "$userID" "$keyExpire" < "$sshKey" | gpg_host --import)
224 # find the key fingerprint of the newly converted key
225 fingerprint=$(fingerprint_server_key)
227 # export host ownertrust to authentication keyring
228 log verbose "setting ultimate owner trust for host key..."
229 echo "${fingerprint}:6:" | gpg_host "--import-ownertrust"
230 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
232 # export public key to file
233 gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
234 log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
236 # show info about new key
240 # generate server gpg key
243 local keyLength="2048"
244 local keyUsage="auth"
247 local hostName=$(hostname -f)
252 # check for presense of secret key
253 # FIXME: is this the proper test to be doing here?
254 fingerprint_server_key >/dev/null \
255 && failure "An OpenPGP host key already exists."
277 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
278 failure "Unknown option '$1'.
279 Type '$PGRM help' for usage."
286 userID="ssh://${hostName}"
288 # prompt about key expiration if not specified
289 keyExpire=$(get_gpg_expiration "$keyExpire")
294 Key-Length: $keyLength
297 Expire-Date: $keyExpire"
299 # add the revoker field if specified
300 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
301 # FIXME: key is marked "sensitive"? is this appropriate?
302 if [ "$revoker" ] ; then
305 Revoker: 1:${revoker} sensitive"
308 echo "The following key parameters will be used for the host private key:"
309 echo "$keyParameters"
311 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
312 if [ ${OK/y/Y} != 'Y' ] ; then
317 # must include blank line!
324 log verbose "generating host key..."
325 echo "$keyParameters" | gpg_host --batch --gen-key
327 # find the key fingerprint of the newly generated key
328 fingerprint=$(fingerprint_server_key)
330 # export host ownertrust to authentication keyring
331 log verbose "setting ultimate owner trust for host key..."
332 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
334 # translate the private key to ssh format, and export to a file
336 # NOTE: assumes that the primary key is the proper key to use
338 gpg_host --export-secret-key "$fingerprint" | \
339 openpgp2ssh "$fingerprint" > "${SYSDATADIR}/ssh_host_rsa_key")
340 log info "SSH host private key output to file: ${SYSDATADIR}/ssh_host_rsa_key"
341 ssh-keygen -y -f "${SYSDATADIR}/ssh_host_rsa_key" > "${SYSDATADIR}/ssh_host_rsa_key.pub"
342 log info "SSH host public key output to file: ${SYSDATADIR}/ssh_host_rsa_key.pub"
343 gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
344 log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
346 # show info about new key
350 # extend the lifetime of a host key:
352 local fpr=$(fingerprint_server_key)
355 # get the new expiration date
356 extendTo=$(get_gpg_expiration "$extendTo")
358 gpg_host --quiet --command-fd 0 --edit-key "$fpr" <<EOF
365 echo "NOTE: Host key expiration date adjusted, but not yet published."
366 echo "Run '$PGRM publish-key' to publish the new expiration date."
369 # add hostname user ID to server key
377 if [ -z "$1" ] ; then
378 failure "You must specify a hostname to add."
383 fingerprint=$(fingerprint_server_key)
385 # match to only ultimately trusted user IDs
386 tmpuidMatch="u:$(echo $userID | gpg_escape)"
388 # find the index of the requsted user ID
389 # NOTE: this is based on circumstantial evidence that the order of
390 # this output is the appropriate index
391 if line=$(gpg_host --list-keys --with-colons --fixed-list-mode "0x${fingerprint}!" \
392 | egrep '^(uid|uat):' | cut -f2,10 -d: | grep -n -x -F "$tmpuidMatch") ; then
393 failure "Host userID '$userID' already exists."
396 echo "The following user ID will be added to the host key:"
398 read -p "Are you sure you would like to add this user ID? (y/N) " OK; OK=${OK:=N}
399 if [ ${OK/y/Y} != 'Y' ] ; then
400 failure "User ID not added."
403 # edit-key script command to add user ID
404 adduidCommand=$(cat <<EOF
413 # execute edit-key script
414 if echo "$adduidCommand" | \
415 gpg_host --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
417 # update the trustdb for the authentication keyring
418 gpg_authentication "--check-trustdb"
423 echo "NOTE: User ID added to key, but key not published."
424 echo "Run '$PGRM publish-key' to publish the new user ID."
426 failure "Problem adding user ID."
430 # revoke hostname user ID to server key
440 if [ -z "$1" ] ; then
441 failure "You must specify a hostname to revoke."
444 echo "WARNING: There is a known bug in this function."
445 echo "This function has been known to occasionally revoke the wrong user ID."
446 echo "Please see the following bug report for more information:"
447 echo "http://web.monkeysphere.info/bugs/revoke-hostname-revoking-wrong-userid/"
448 read -p "Are you sure you would like to proceed? (y/N) " OK; OK=${OK:=N}
449 if [ ${OK/y/Y} != 'Y' ] ; then
455 fingerprint=$(fingerprint_server_key)
457 # match to only ultimately trusted user IDs
458 tmpuidMatch="u:$(echo $userID | gpg_escape)"
460 # find the index of the requsted user ID
461 # NOTE: this is based on circumstantial evidence that the order of
462 # this output is the appropriate index
463 if line=$(gpg_host --list-keys --with-colons --fixed-list-mode "0x${fingerprint}!" \
464 | egrep '^(uid|uat):' | cut -f2,10 -d: | grep -n -x -F "$tmpuidMatch") ; then
467 failure "No non-revoked user ID '$userID' is found."
470 echo "The following host key user ID will be revoked:"
472 read -p "Are you sure you would like to revoke this user ID? (y/N) " OK; OK=${OK:=N}
473 if [ ${OK/y/Y} != 'Y' ] ; then
474 failure "User ID not revoked."
477 message="Hostname removed by monkeysphere-server $DATE"
479 # edit-key script command to revoke user ID
480 revuidCommand=$(cat <<EOF
492 # execute edit-key script
493 if echo "$revuidCommand" | \
494 gpg_host --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
496 # update the trustdb for the authentication keyring
497 gpg_authentication "--check-trustdb"
502 echo "NOTE: User ID revoked, but revocation not published."
503 echo "Run '$PGRM publish-key' to publish the revocation."
505 failure "Problem revoking user ID."
509 # add a revoker to the host key
512 failure "not implemented yet!"
515 # revoke the host key
518 failure "not implemented yet!"
521 # publish server key to keyserver
522 publish_server_key() {
523 read -p "Really publish host key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
524 if [ ${OK/y/Y} != 'Y' ] ; then
525 failure "key not published."
528 # find the key fingerprint
529 fingerprint=$(fingerprint_server_key)
532 gpg_authentication "--keyserver $KEYSERVER --send-keys '0x${fingerprint}!'"
536 # * check on the status and validity of the key and public certificates
548 local problemsfound=0
550 # FIXME: what's the correct, cross-platform answer?
551 sshd_config=/etc/ssh/sshd_config
552 seckey=$(gpg_host --list-secret-keys --fingerprint --with-colons --fixed-list-mode)
553 keysfound=$(echo "$seckey" | grep -c ^sec:)
555 # warn when anything is 2 months away from expiration
556 warnwindow='2 months'
557 warndate=$(advance_date $warnwindow +%s)
559 if ! id monkeysphere >/dev/null ; then
560 echo "! No monkeysphere user found! Please create a monkeysphere system user with bash as its shell."
561 problemsfound=$(($problemsfound+1))
564 if ! [ -d "$SYSDATADIR" ] ; then
565 echo "! no $SYSDATADIR directory found. Please create it."
566 problemsfound=$(($problemsfound+1))
569 echo "Checking host GPG key..."
570 if (( "$keysfound" < 1 )); then
571 echo "! No host key found."
572 echo " - Recommendation: run 'monkeysphere-server gen-key'"
573 problemsfound=$(($problemsfound+1))
574 elif (( "$keysfound" > 1 )); then
575 echo "! More than one host key found?"
576 # FIXME: recommend a way to resolve this
577 problemsfound=$(($problemsfound+1))
579 create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
580 expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
581 fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
582 # check for key expiration:
583 if [ "$expire" ]; then
584 if (( "$expire" < "$curdate" )); then
585 echo "! Host key is expired."
586 echo " - Recommendation: extend lifetime of key with 'monkeysphere-server extend-key'"
587 problemsfound=$(($problemsfound+1))
588 elif (( "$expire" < "$warndate" )); then
589 echo "! Host key expires in less than $warnwindow:" $(advance_date $(( $expire - $curdate )) seconds +%F)
590 echo " - Recommendation: extend lifetime of key with 'monkeysphere-server extend-key'"
591 problemsfound=$(($problemsfound+1))
596 if [ "$create" ] && (( "$create" > "$curdate" )); then
597 echo "! Host key was created in the future(?!). Is your clock correct?"
598 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
599 problemsfound=$(($problemsfound+1))
602 # check for UserID expiration:
603 echo "$seckey" | grep ^uid: | cut -d: -f6,7,10 | \
604 while IFS=: read create expire uid ; do
605 # FIXME: should we be doing any checking on the form
606 # of the User ID? Should we be unmangling it somehow?
608 if [ "$create" ] && (( "$create" > "$curdate" )); then
609 echo "! User ID '$uid' was created in the future(?!). Is your clock correct?"
610 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
611 problemsfound=$(($problemsfound+1))
613 if [ "$expire" ] ; then
614 if (( "$expire" < "$curdate" )); then
615 echo "! User ID '$uid' is expired."
616 # FIXME: recommend a way to resolve this
617 problemsfound=$(($problemsfound+1))
618 elif (( "$expire" < "$warndate" )); then
619 echo "! User ID '$uid' expires in less than $warnwindow:" $(advance_date $(( $expire - $curdate )) seconds +%F)
620 # FIXME: recommend a way to resolve this
621 problemsfound=$(($problemsfound+1))
626 # FIXME: verify that the host key is properly published to the
627 # keyservers (do this with the non-privileged user)
629 # FIXME: check that there are valid, non-expired certifying signatures
630 # attached to the host key after fetching from the public keyserver
631 # (do this with the non-privileged user as well)
633 # FIXME: propose adding a revoker to the host key if none exist (do we
634 # have a way to do that after key generation?)
636 # Ensure that the ssh_host_rsa_key file is present and non-empty:
638 echo "Checking host SSH key..."
639 if [ ! -s "${SYSDATADIR}/ssh_host_rsa_key" ] ; then
640 echo "! The host key as prepared for SSH (${SYSDATADIR}/ssh_host_rsa_key) is missing or empty."
641 problemsfound=$(($problemsfound+1))
643 if [ $(ls -l "${SYSDATADIR}/ssh_host_rsa_key" | cut -f1 -d\ ) != '-rw-------' ] ; then
644 echo "! Permissions seem wrong for ${SYSDATADIR}/ssh_host_rsa_key -- should be 0600."
645 problemsfound=$(($problemsfound+1))
648 # propose changes needed for sshd_config (if any)
649 if ! grep -q "^HostKey[[:space:]]\+${SYSDATADIR}/ssh_host_rsa_key$" "$sshd_config"; then
650 echo "! $sshd_config does not point to the monkeysphere host key (${SYSDATADIR}/ssh_host_rsa_key)."
651 echo " - Recommendation: add a line to $sshd_config: 'HostKey ${SYSDATADIR}/ssh_host_rsa_key'"
652 problemsfound=$(($problemsfound+1))
654 if badhostkeys=$(grep -i '^HostKey' "$sshd_config" | grep -v "^HostKey[[:space:]]\+${SYSDATADIR}/ssh_host_rsa_key$") ; then
655 echo "! $sshd_config refers to some non-monkeysphere host keys:"
657 echo " - Recommendation: remove the above HostKey lines from $sshd_config"
658 problemsfound=$(($problemsfound+1))
661 # FIXME: test (with ssh-keyscan?) that the running ssh
662 # daemon is actually offering the monkeysphere host key.
667 # FIXME: look at the ownership/privileges of the various keyrings,
668 # directories housing them, etc (what should those values be? can
669 # we make them as minimal as possible?)
671 # FIXME: look to see that the ownertrust rules are set properly on the
672 # authentication keyring
674 # FIXME: make sure that at least one identity certifier exists
676 # FIXME: look at the timestamps on the monkeysphere-generated
677 # authorized_keys files -- warn if they seem out-of-date.
679 # FIXME: check for a cronjob that updates monkeysphere-generated
683 echo "Checking for MonkeySphere-enabled public-key authentication for users ..."
684 # Ensure that User ID authentication is enabled:
685 if ! grep -q "^AuthorizedKeysFile[[:space:]]\+${SYSDATADIR}/authorized_keys/%u$" "$sshd_config"; then
686 echo "! $sshd_config does not point to monkeysphere authorized keys."
687 echo " - Recommendation: add a line to $sshd_config: 'AuthorizedKeysFile ${SYSDATADIR}/authorized_keys/%u'"
688 problemsfound=$(($problemsfound+1))
690 if badauthorizedkeys=$(grep -i '^AuthorizedKeysFile' "$sshd_config" | grep -v "^AuthorizedKeysFile[[:space:]]\+${SYSDATADIR}/authorized_keys/%u$") ; then
691 echo "! $sshd_config refers to non-monkeysphere authorized_keys files:"
692 echo "$badauthorizedkeys"
693 echo " - Recommendation: remove the above AuthorizedKeysFile lines from $sshd_config"
694 problemsfound=$(($problemsfound+1))
697 if [ "$problemsfound" -gt 0 ]; then
698 echo "When the above $problemsfound issue"$(if [ "$problemsfound" -eq 1 ] ; then echo " is" ; else echo "s are" ; fi)" resolved, please re-run:"
699 echo " monkeysphere-server diagnostics"
701 echo "Everything seems to be in order!"
705 ########################################################################
707 ########################################################################
709 # unset variables that should be defined only in config file
711 unset AUTHORIZED_USER_IDS
712 unset RAW_AUTHORIZED_KEYS
713 unset MONKEYSPHERE_USER
715 # load configuration file
716 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${SYSCONFIGDIR}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
718 # set empty config variable with ones from the environment, or with
720 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
721 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
722 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
723 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
724 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
727 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
728 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
729 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${SYSDATADIR}/gnupg-host"}
730 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${SYSDATADIR}/gnupg-authentication"}
732 # export variables needed in su invocation
735 export MONKEYSPHERE_USER
738 export CHECK_KEYSERVER
739 export REQUIRED_USER_KEY_CAPABILITY
740 export GNUPGHOME_HOST
741 export GNUPGHOME_AUTHENTICATION
746 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
750 'show-key'|'show'|'s')
760 'add-hostname'|'add-name'|'n+')
766 'revoke-hostname'|'revoke-name'|'n-')
784 'publish-key'|'publish'|'p')
794 case "$SUBCOMMAND" in
808 failure "Unknown expert subcommand: '$COMMAND'
809 Type '$PGRM help' for usage."
818 '--help'|'help'|'-h'|'h'|'?')
823 failure "Unknown command: '$COMMAND'
824 Type '$PGRM help' for usage."