3 # monkeysphere-server: MonkeySphere server admin tool
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # They are Copyright 2008, and are all released under the GPL, version 3
11 ########################################################################
14 SHARE=${MONKEYSPHERE_SHARE:="/usr/share/monkeysphere"}
16 . "${SHARE}/common" || exit 1
18 VARLIB="/var/lib/monkeysphere"
21 # date in UTF format if needed
22 DATE=$(date -u '+%FT%T')
24 # unset some environment variables that could screw things up
30 ########################################################################
32 ########################################################################
36 usage: $PGRM <subcommand> [options] [args]
37 MonkeySphere server admin tool.
40 update-users (u) [USER]... update user authorized_keys files
42 gen-key (g) [NAME[:PORT]] generate gpg key for the server
43 -l|--length BITS key length in bits (2048)
44 -e|--expire EXPIRE date to expire
45 -r|--revoker FINGERPRINT add a revoker
46 add-hostname (n+) NAME[:PORT] add hostname user ID to server key
47 revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
48 show-key (s) output all server host key information
49 fingerprint (f) output just the key fingerprint
50 publish-key (p) publish server host key to keyserver
51 diagnostics (d) report on server monkeysphere status
53 add-id-certifier (c+) KEYID import and tsign a certification key
54 -n|--domain DOMAIN limit ID certifications to DOMAIN
55 -t|--trust TRUST trust level of certifier (full)
56 -d|--depth DEPTH trust depth for certifier (1)
57 remove-id-certifier (c-) KEYID remove a certification key
58 list-id-certifiers (c) list certification keys
60 gpg-authentication-cmd CMD gnupg-authentication command
62 -h|--help|help (h,?) this help
66 su_monkeysphere_user() {
67 su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
70 # function to interact with the host gnupg keyring
74 GNUPGHOME="$GNUPGHOME_HOST"
77 # NOTE: we supress this warning because we need the monkeysphere
78 # user to be able to read the host pubring. we realize this might
79 # be problematic, but it's the simplest solution, without too much
81 gpg --no-permission-warning "$@"
84 # always reset the permissions on the host pubring so that the
85 # monkeysphere user can read the trust signatures
86 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
87 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
92 # function to interact with the authentication gnupg keyring
93 # FIXME: this function requires basically accepts only a single
94 # argument because of problems with quote expansion. this needs to be
96 gpg_authentication() {
97 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
100 su_monkeysphere_user "gpg $@"
103 # output just key fingerprint
104 fingerprint_server_key() {
105 gpg_host --list-secret-keys --fingerprint --with-colons --fixed-list-mode | \
106 grep '^fpr:' | head -1 | cut -d: -f10
109 # output key information
112 fingerprint=$(fingerprint_server_key)
113 gpg_host --fingerprint --list-secret-key "$fingerprint"
116 # update authorized_keys for users
119 # get users from command line
122 # or just look at all users if none specified
123 unames=$(getent passwd | cut -d: -f1)
127 MODE="authorized_keys"
130 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
132 # check to see if the gpg trust database has been initialized
133 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
134 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
137 # make sure the authorized_keys directory exists
138 mkdir -p "${VARLIB}/authorized_keys"
141 for uname in $unames ; do
142 # check all specified users exist
143 if ! getent passwd "$uname" >/dev/null ; then
144 log "----- unknown user '$uname' -----"
148 # set authorized_user_ids and raw authorized_keys variables,
149 # translating ssh-style path variables
150 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
151 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
153 # if neither is found, skip user
154 if [ ! -s "$authorizedUserIDs" ] ; then
155 if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
160 log "----- user: $uname -----"
162 # exit if the authorized_user_ids file is empty
163 if ! check_key_file_permissions "$uname" "$AUTHORIZED_USER_IDS" ; then
164 log "Improper permissions on authorized_user_ids file path."
168 # check permissions on the authorized_keys file path
169 if ! check_key_file_permissions "$uname" "$RAW_AUTHORIZED_KEYS" ; then
170 log "Improper permissions on authorized_keys file path path."
174 # make temporary directory
177 # trap to delete temporary directory on exit
178 trap "rm -rf $TMPDIR" EXIT
180 # create temporary authorized_user_ids file
181 TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
182 touch "$TMP_AUTHORIZED_USER_IDS"
184 # create temporary authorized_keys file
185 AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
186 touch "$AUTHORIZED_KEYS"
188 # set restrictive permissions on the temporary files
189 # FIXME: is there a better way to do this?
191 chmod 0600 "$AUTHORIZED_KEYS"
192 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
193 chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
195 # if the authorized_user_ids file exists...
196 if [ -s "$authorizedUserIDs" ] ; then
197 # copy user authorized_user_ids file to temporary
199 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
201 # export needed variables
202 export AUTHORIZED_KEYS
203 export TMP_AUTHORIZED_USER_IDS
205 # process authorized_user_ids file, as monkeysphere
207 su_monkeysphere_user \
208 ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
212 # add user-controlled authorized_keys file path if specified
213 if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
214 log -n "adding raw authorized_keys file... "
215 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
219 # openssh appears to check the contents of the
220 # authorized_keys file as the user in question, so the
221 # file must be readable by that user at least.
222 # FIXME: is there a better way to do this?
223 chown root "$AUTHORIZED_KEYS"
224 chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
225 chmod g+r "$AUTHORIZED_KEYS"
227 # move the resulting authorized_keys file into place
228 mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
230 # destroy temporary directory
235 # generate server gpg key
247 # set default key parameter values
255 TEMP=$(getopt -o e:l:r -l expire:,length:,revoker: -n "$PGRM" -- "$@")
257 if [ $? != 0 ] ; then
261 # Note the quotes around `$TEMP': they are essential!
287 hostName=${1:-$(hostname --fqdn)}
288 userID="ssh://${hostName}"
290 # check for presense of key with user ID
291 if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
292 failure "Key for '$userID' already exists"
295 # prompt about key expiration if not specified
296 if [ -z "$keyExpire" ] ; then
298 Please specify how long the key should be valid.
299 0 = key does not expire
300 <n> = key expires in n days
301 <n>w = key expires in n weeks
302 <n>m = key expires in n months
303 <n>y = key expires in n years
305 while [ -z "$keyExpire" ] ; do
306 read -p "Key is valid for? (0) " keyExpire
307 if ! test_gpg_expire ${keyExpire:=0} ; then
312 elif ! test_gpg_expire "$keyExpire" ; then
313 failure "invalid key expiration value '$keyExpire'."
317 keyParameters=$(cat <<EOF
319 Key-Length: $keyLength
322 Expire-Date: $keyExpire
326 # add the revoker field if specified
327 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
328 # FIXME: key is marked "sensitive"? is this appropriate?
329 if [ "$revoker" ] ; then
330 keyParameters="${keyParameters}"$(cat <<EOF
331 Revoker: 1:$revoker sensitive
336 echo "The following key parameters will be used for the host private key:"
337 echo "$keyParameters"
339 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
340 if [ ${OK/y/Y} != 'Y' ] ; then
345 keyParameters="${keyParameters}"$(cat <<EOF
352 log "generating server key..."
353 echo "$keyParameters" | gpg_host --batch --gen-key
355 # output the server fingerprint
356 fingerprint_server_key "=${userID}"
358 # find the key fingerprint of the newly generated key
359 fingerprint=$(fingerprint_server_key)
361 # export host ownertrust to authentication keyring
362 log "setting ultimate owner trust for server key..."
363 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
365 # translate the private key to ssh format, and export to a file
367 # NOTE: assumes that the primary key is the proper key to use
369 gpg_host --export-secret-key "$fingerprint" | \
370 openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
371 log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
374 # add hostname user ID to server key
380 if [ -z "$1" ] ; then
381 failure "You must specify a hostname to add."
386 if [ "$(gpg_host --list-key "=${userID}" 2> /dev/null)" ] ; then
387 failure "Host userID '$userID' already exists."
390 echo "The following user ID will be added to the host key:"
392 read -p "Are you sure you would like to add this user ID? (y/N) " OK; OK=${OK:=N}
393 if [ ${OK/y/Y} != 'Y' ] ; then
394 failure "user ID not added."
397 fingerprint=$(fingerprint_server_key)
399 # edit-key script command to add user ID
400 adduidCommand=$(cat <<EOF
409 # execute edit-key script
410 echo "$adduidCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
413 gpg_host --check-trustdb
421 # revoke hostname user ID to server key
426 if [ -z "$1" ] ; then
427 failure "You must specify a hostname to revoke."
432 fingerprint=$(fingerprint_server_key)
434 # find the index of the requsted user ID
435 # NOTE: this is based on circumstantial evidence that the order of
436 # this output is the appropriate index
437 uidIndex=$(gpg_host --with-colons --fixed-list-mode --list-key "$fingerprint" 2> /dev/null | \
438 egrep "^(uid|uat):" | cut -d: -f10 | gpg_unescape | cat -n | \
439 grep "$userID" | awk '{ print $1 }')
441 if [ -z "$uidIndex" ] ; then
442 failure "User ID '$userID' not found in host key."
445 echo "The following user ID will be revoked from the host key:"
447 read -p "Are you sure you would like to revoke this user ID? (y/N) " OK; OK=${OK:=N}
448 if [ ${OK/y/Y} != 'Y' ] ; then
449 failure "user ID not revoked."
452 # edit-key script command to revoke user ID
453 revuidCommand=$(cat <<EOF
464 # execute edit-key script
465 echo "$revuidCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
468 gpg_host --check-trustdb
476 # publish server key to keyserver
477 publish_server_key() {
478 read -p "Really publish host key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
479 if [ ${OK/y/Y} != 'Y' ] ; then
480 failure "key not published."
483 # find the key fingerprint
484 fingerprint=$(fingerprint_server_key)
487 gpg_authentication "--keyserver $KEYSERVER --send-keys $fingerprint"
491 # * check on the status and validity of the key and public certificates
503 seckey=$(fingerprint_server_key)
504 keysfound=$(echo "$seckey" | grep -c ^sec:)
506 # warn when anything is 2 months away from expiration
507 warnwindow='2 months'
508 warndate=$(date +%s -d "$warnwindow")
510 echo "Checking host GPG key..."
511 if (( "$keysfound" < 1 )); then
512 echo "! No host key found."
513 echo " - Recommendation: run 'monkeysphere-server gen-key'"
514 elif (( "$keysfound" > 1 )); then
515 echo "! More than one host key found?"
516 # FIXME: recommend a way to resolve this
518 create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
519 expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
520 fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
521 # check for key expiration:
522 if [ "$expire" ]; then
523 if (( "$expire" < "$curdate" )); then
524 echo "! Host key is expired."
525 # FIXME: recommend a way to resolve this other than re-keying?
526 elif (( "$expire" < "$warndate" )); then
527 echo "! Host key expires in less than $warnwindow:" $(date -d "$(( $expire - $curdate )) seconds" +%F)
528 # FIXME: recommend a way to resolve this?
533 if [ "$create" ] && (( "$create" > "$curdate" )); then
534 echo "! Host key was created in the future(?!). Is your clock correct?"
535 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
538 # check for UserID expiration:
539 echo "$seckey" | grep ^uid: | cut -d: -f6,7,10 | \
540 while IFS=: read create expire uid ; do
541 # FIXME: should we be doing any checking on the form
542 # of the User ID? Should we be unmangling it somehow?
544 if [ "$create" ] && (( "$create" > "$curdate" )); then
545 echo "! User ID '$uid' was created in the future(?!). Is your clock correct?"
546 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
548 if [ "$expire" ] ; then
549 if (( "$expire" < "$curdate" )); then
550 echo "! User ID '$uid' is expired."
551 # FIXME: recommend a way to resolve this
552 elif (( "$expire" < "$warndate" )); then
553 echo "! User ID '$uid' expires in less than $warnwindow:" $(date -d "$(( $expire - $curdate )) seconds" +%F)
554 # FIXME: recommend a way to resolve this
559 # FIXME: verify that the host key is properly published to the
560 # keyservers (do this with the non-privileged user)
562 # FIXME: check that there are valid, non-expired certifying signatures
563 # attached to the host key after fetching from the public keyserver
564 # (do this with the non-privileged user as well)
566 # FIXME: propose adding a revoker to the host key if none exist (do we
567 # have a way to do that after key generation?)
569 # Ensure that the ssh_host_rsa_key file is present and non-empty:
570 echo "Checking host SSH key..."
571 if [ ! -s "${VARLIB}/ssh_host_rsa_key" ] ; then
572 echo "! The host key as prepared for SSH (${VARLIB}/ssh_host_rsa_key) is missing or empty."
574 if [ $(stat -c '%a' "${VARLIB}/ssh_host_rsa_key") != 600 ] ; then
575 echo "! Permissions seem wrong for ${VARLIB}/ssh_host_rsa_key -- should be 0600."
578 # propose changes needed for sshd_config (if any)
579 if ! grep -q "^HostKey[[:space:]]\+${VARLIB}/ssh_host_rsa_key$" /etc/ssh/sshd_config; then
580 echo "! /etc/ssh/sshd_config does not point to the monkeysphere host key (${VARLIB}/ssh_host_rsa_key)."
581 echo " - Recommendation: add a line to /etc/ssh/sshd_config: 'HostKey ${VARLIB}/ssh_host_rsa_key'"
583 if badhostkeys=$(grep -i '^HostKey' | grep -q -v "^HostKey[[:space:]]\+${VARLIB}/ssh_host_rsa_key$") ; then
584 echo "! /etc/sshd_config refers to some non-monkeysphere host keys:"
586 echo " - Recommendation: remove the above HostKey lines from /etc/ssh/sshd_config"
591 # FIXME: look at the ownership/privileges of the various keyrings,
592 # directories housing them, etc (what should those values be? can
593 # we make them as minimal as possible?)
595 # FIXME: look to see that the ownertrust rules are set properly on the
596 # authentication keyring
598 # FIXME: make sure that at least one identity certifier exists
600 echo "Checking for MonkeySphere-enabled public-key authentication for users ..."
601 # Ensure that User ID authentication is enabled:
602 if ! grep -q "^AuthorizedKeysFile[[:space:]]\+${VARLIB}/authorized_keys/%u$" /etc/ssh/sshd_config; then
603 echo "! /etc/ssh/sshd_config does not point to monkeysphere authorized keys."
604 echo " - Recommendation: add a line to /etc/ssh/sshd_config: 'AuthorizedKeysFile ${VARLIB}/authorized_keys/%u'"
606 if badauthorizedkeys=$(grep -i '^AuthorizedKeysFile' | grep -q -v "^AuthorizedKeysFile[[:space:]]\+${VARLIB}/authorized_keys/%u$") ; then
607 echo "! /etc/sshd_config refers to non-monkeysphere authorized_keys files:"
608 echo "$badauthorizedkeys"
609 echo " - Recommendation: remove the above AuthorizedKeysFile lines from /etc/ssh/sshd_config"
613 # retrieve key from web of trust, import it into the host keyring, and
614 # ltsign the key in the host keyring so that it may certify other keys
624 # set default values for trust depth and domain
630 TEMP=$(getopt -o n:t:d: -l domain:,trust:,depth: -n "$PGRM" -- "$@")
632 if [ $? != 0 ] ; then
636 # Note the quotes around `$TEMP': they are essential!
663 if [ -z "$keyID" ] ; then
664 failure "You must specify the key ID of a key to add."
668 # get the key from the key server
669 gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'"
671 # get the full fingerprint of a key ID
672 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
673 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
676 gpg_authentication "--fingerprint $fingerprint"
678 echo "Are you sure you want to add this key as a certifier of"
679 read -p "users on this system? (y/N) " OK; OK=${OK:-N}
680 if [ "${OK/y/Y}" != 'Y' ] ; then
684 # export the key to the host keyring
685 gpg_authentication "--export $keyID" | gpg_host --import
687 if [ "$trust" == marginal ]; then
689 elif [ "$trust" == full ]; then
692 failure "trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)"
696 # NOTE: *all* user IDs will be ltsigned
697 ltsignCommand=$(cat <<EOF
709 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
711 # update the trustdb for the authentication keyring
712 gpg_authentication "--check-trustdb"
715 # delete a certifiers key from the host keyring
721 if [ -z "$keyID" ] ; then
722 failure "You must specify the key ID of a key to remove."
725 # delete the requested key (with prompting)
726 gpg_host --delete-key "$keyID"
728 # update the trustdb for the authentication keyring
729 gpg_authentication "--check-trustdb"
732 # list the host certifiers
737 # issue command to gpg-authentication keyring
738 gpg_authentication_cmd() {
739 gpg_authentication "$@"
742 ########################################################################
744 ########################################################################
746 # unset variables that should be defined only in config file
748 unset AUTHORIZED_USER_IDS
749 unset RAW_AUTHORIZED_KEYS
750 unset MONKEYSPHERE_USER
752 # load configuration file
753 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
755 # set empty config variable with ones from the environment, or with
757 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
758 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
759 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
760 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
763 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
764 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
765 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
766 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
768 # export variables needed in su invocation
771 export MONKEYSPHERE_USER
773 export CHECK_KEYSERVER
774 export REQUIRED_USER_KEY_CAPABILITY
775 export GNUPGHOME_HOST
776 export GNUPGHOME_AUTHENTICATION
781 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
785 'update-users'|'update-user'|'u')
793 'add-hostname'|'add-name'|'n+')
797 'revoke-hostname'|'revoke-name'|'n-')
801 'show-key'|'show'|'s')
805 'show-fingerprint'|'fingerprint'|'f')
806 fingerprint_server_key
809 'publish-key'|'publish'|'p')
817 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
821 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
822 remove_certifier "$@"
825 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
829 'gpg-authentication-cmd')
830 gpg_authentication_cmd "$@"
833 '--help'|'help'|'-h'|'h'|'?')
838 failure "Unknown command: '$COMMAND'
839 Type '$PGRM help' for usage."