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 key information
105 gpg_host --list-secret-keys --fingerprint
108 # output just key fingerprint
109 fingerprint_server_key() {
110 gpg_host --list-secret-keys --fingerprint --with-colons --fixed-list-mode | \
111 grep '^fpr:' | head -1 | cut -d: -f10
114 # update authorized_keys for users
117 # get users from command line
120 # or just look at all users if none specified
121 unames=$(getent passwd | cut -d: -f1)
125 MODE="authorized_keys"
128 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
130 # check to see if the gpg trust database has been initialized
131 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
132 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
135 # make sure the authorized_keys directory exists
136 mkdir -p "${VARLIB}/authorized_keys"
139 for uname in $unames ; do
140 # check all specified users exist
141 if ! getent passwd "$uname" >/dev/null ; then
142 log "----- unknown user '$uname' -----"
146 # set authorized_user_ids and raw authorized_keys variables,
147 # translating ssh-style path variables
148 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
149 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
151 # if neither is found, skip user
152 if [ ! -s "$authorizedUserIDs" ] ; then
153 if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
158 log "----- user: $uname -----"
160 # exit if the authorized_user_ids file is empty
161 if ! check_key_file_permissions "$uname" "$AUTHORIZED_USER_IDS" ; then
162 log "Improper permissions on authorized_user_ids file path."
166 # check permissions on the authorized_keys file path
167 if ! check_key_file_permissions "$uname" "$RAW_AUTHORIZED_KEYS" ; then
168 log "Improper permissions on authorized_keys file path path."
172 # make temporary directory
175 # trap to delete temporary directory on exit
176 trap "rm -rf $TMPDIR" EXIT
178 # create temporary authorized_user_ids file
179 TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
180 touch "$TMP_AUTHORIZED_USER_IDS"
182 # create temporary authorized_keys file
183 AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
184 touch "$AUTHORIZED_KEYS"
186 # set restrictive permissions on the temporary files
187 # FIXME: is there a better way to do this?
189 chmod 0600 "$AUTHORIZED_KEYS"
190 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
191 chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
193 # if the authorized_user_ids file exists...
194 if [ -s "$authorizedUserIDs" ] ; then
195 # copy user authorized_user_ids file to temporary
197 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
199 # export needed variables
200 export AUTHORIZED_KEYS
201 export TMP_AUTHORIZED_USER_IDS
203 # process authorized_user_ids file, as monkeysphere
205 su_monkeysphere_user \
206 ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
210 # add user-controlled authorized_keys file path if specified
211 if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
212 log -n "adding raw authorized_keys file... "
213 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
217 # openssh appears to check the contents of the
218 # authorized_keys file as the user in question, so the
219 # file must be readable by that user at least.
220 # FIXME: is there a better way to do this?
221 chown root "$AUTHORIZED_KEYS"
222 chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
223 chmod g+r "$AUTHORIZED_KEYS"
225 # move the resulting authorized_keys file into place
226 mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
228 # destroy temporary directory
233 # generate server gpg key
245 # set default key parameter values
253 TEMP=$(getopt -o e:l:r -l expire:,length:,revoker: -n "$PGRM" -- "$@")
255 if [ $? != 0 ] ; then
259 # Note the quotes around `$TEMP': they are essential!
285 hostName=${1:-$(hostname --fqdn)}
286 userID="ssh://${hostName}"
288 # check for presense of key with user ID
289 if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
290 failure "Key for '$userID' already exists"
293 # prompt about key expiration if not specified
294 if [ -z "$keyExpire" ] ; then
296 Please specify how long the key should be valid.
297 0 = key does not expire
298 <n> = key expires in n days
299 <n>w = key expires in n weeks
300 <n>m = key expires in n months
301 <n>y = key expires in n years
303 while [ -z "$keyExpire" ] ; do
304 read -p "Key is valid for? (0) " keyExpire
305 if ! test_gpg_expire ${keyExpire:=0} ; then
310 elif ! test_gpg_expire "$keyExpire" ; then
311 failure "invalid key expiration value '$keyExpire'."
315 keyParameters=$(cat <<EOF
317 Key-Length: $keyLength
320 Expire-Date: $keyExpire
324 # add the revoker field if specified
325 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
326 # FIXME: key is marked "sensitive"? is this appropriate?
327 if [ "$revoker" ] ; then
328 keyParameters="${keyParameters}"$(cat <<EOF
329 Revoker: 1:$revoker sensitive
334 echo "The following key parameters will be used for the host private key:"
335 echo "$keyParameters"
337 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
338 if [ ${OK/y/Y} != 'Y' ] ; then
343 keyParameters="${keyParameters}"$(cat <<EOF
350 log "generating server key..."
351 echo "$keyParameters" | gpg_host --batch --gen-key
353 # output the server fingerprint
354 fingerprint_server_key "=${userID}"
356 # find the key fingerprint of the newly generated key
357 fingerprint=$(fingerprint_server_key)
359 # export host ownertrust to authentication keyring
360 log "setting ultimate owner trust for server key..."
361 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
363 # translate the private key to ssh format, and export to a file
365 # NOTE: assumes that the primary key is the proper key to use
367 gpg_host --export-secret-key "$fingerprint" | \
368 openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
369 log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
372 # add hostname user ID to server key
374 if [ -z "$1" ] ; then
375 failure "You must specify a hostname to add."
380 if [ "$(gpg_host --list-key "=${userID}")" ] ; then
381 failure "Host userID '$userID' already exists."
384 fingerprint=$(fingerprint_server_key)
386 adduidCommand=$(cat <<EOF
397 echo "$adduidCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
399 echo "NOTE: new host userID has not been published."
400 echo "Use '$PGRM publish-key' to publish these changes."
403 # revoke hostname user ID to server key
411 if [ -z "$1" ] ; then
412 failure "You must specify a hostname to revoke."
415 fpr=$(fingerprint_server_key)
416 tmpuidMatch="u:$(escape "ssh://$1")"
418 if linenum=$(gpg_host --list-keys --with-colons --fixed-list-mode "0x$fpr"\! | egrep '^(uid|uat):' | cut -f2,10 -d: | grep -n -x -F "$tmpuidMatch") ; then
419 uidNum=${linenum%%:*}
421 failure "no non-revoked hostname '$1' is listed."
424 msg="hostname removed by monkeysphere-server on $(date +%F)"
427 revuidCommand=$(cat <<EOF
439 echo "$revuidCommand" | gpg_host --quiet --command-fd 0 --edit-key "0x$fpr"\!
441 echo "NOTE: host userID revokation has not been published."
442 echo "Use '$PGRM publish-key' to publish these changes."
445 # publish server key to keyserver
446 publish_server_key() {
447 read -p "Really publish host key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
448 if [ ${OK/y/Y} != 'Y' ] ; then
452 # find the key fingerprint
453 fingerprint=$(fingerprint_server_key)
456 gpg_authentication "--keyserver $KEYSERVER --send-keys $fingerprint"
460 # * check on the status and validity of the key and public certificates
473 # FIXME: what's the correct, cross-platform answer?
474 sshd_config=/etc/ssh/sshd_config
475 seckey=$(fingerprint_server_key)
476 keysfound=$(echo "$seckey" | grep -c ^sec:)
478 # warn when anything is 2 months away from expiration
479 warnwindow='2 months'
480 warndate=$(date +%s -d "$warnwindow")
482 echo "Checking host GPG key..."
483 if (( "$keysfound" < 1 )); then
484 echo "! No host key found."
485 echo " - Recommendation: run 'monkeysphere-server gen-key'"
486 elif (( "$keysfound" > 1 )); then
487 echo "! More than one host key found?"
488 # FIXME: recommend a way to resolve this
490 create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
491 expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
492 fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
493 # check for key expiration:
494 if [ "$expire" ]; then
495 if (( "$expire" < "$curdate" )); then
496 echo "! Host key is expired."
497 # FIXME: recommend a way to resolve this other than re-keying?
498 elif (( "$expire" < "$warndate" )); then
499 echo "! Host key expires in less than $warnwindow:" $(date -d "$(( $expire - $curdate )) seconds" +%F)
500 # FIXME: recommend a way to resolve this?
505 if [ "$create" ] && (( "$create" > "$curdate" )); then
506 echo "! Host key was created in the future(?!). Is your clock correct?"
507 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
510 # check for UserID expiration:
511 echo "$seckey" | grep ^uid: | cut -d: -f6,7,10 | \
512 while IFS=: read create expire uid ; do
513 # FIXME: should we be doing any checking on the form
514 # of the User ID? Should we be unmangling it somehow?
516 if [ "$create" ] && (( "$create" > "$curdate" )); then
517 echo "! User ID '$uid' was created in the future(?!). Is your clock correct?"
518 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
520 if [ "$expire" ] ; then
521 if (( "$expire" < "$curdate" )); then
522 echo "! User ID '$uid' is expired."
523 # FIXME: recommend a way to resolve this
524 elif (( "$expire" < "$warndate" )); then
525 echo "! User ID '$uid' expires in less than $warnwindow:" $(date -d "$(( $expire - $curdate )) seconds" +%F)
526 # FIXME: recommend a way to resolve this
531 # FIXME: verify that the host key is properly published to the
532 # keyservers (do this with the non-privileged user)
534 # FIXME: check that there are valid, non-expired certifying signatures
535 # attached to the host key after fetching from the public keyserver
536 # (do this with the non-privileged user as well)
538 # FIXME: propose adding a revoker to the host key if none exist (do we
539 # have a way to do that after key generation?)
541 # Ensure that the ssh_host_rsa_key file is present and non-empty:
542 echo "Checking host SSH key..."
543 if [ ! -s "${VARLIB}/ssh_host_rsa_key" ] ; then
544 echo "! The host key as prepared for SSH (${VARLIB}/ssh_host_rsa_key) is missing or empty."
546 if [ $(stat -c '%a' "${VARLIB}/ssh_host_rsa_key") != 600 ] ; then
547 echo "! Permissions seem wrong for ${VARLIB}/ssh_host_rsa_key -- should be 0600."
550 # propose changes needed for sshd_config (if any)
551 if ! grep -q "^HostKey[[:space:]]\+${VARLIB}/ssh_host_rsa_key$" "$sshd_config"; then
552 echo "! $sshd_config does not point to the monkeysphere host key (${VARLIB}/ssh_host_rsa_key)."
553 echo " - Recommendation: add a line to $sshd_config: 'HostKey ${VARLIB}/ssh_host_rsa_key'"
555 if badhostkeys=$(grep -i '^HostKey' "$sshd_config" | grep -q -v "^HostKey[[:space:]]\+${VARLIB}/ssh_host_rsa_key$") ; then
556 echo "! /etc/sshd_config refers to some non-monkeysphere host keys:"
558 echo " - Recommendation: remove the above HostKey lines from $sshd_config"
563 # FIXME: look at the ownership/privileges of the various keyrings,
564 # directories housing them, etc (what should those values be? can
565 # we make them as minimal as possible?)
567 # FIXME: look to see that the ownertrust rules are set properly on the
568 # authentication keyring
570 # FIXME: make sure that at least one identity certifier exists
572 echo "Checking for MonkeySphere-enabled public-key authentication for users ..."
573 # Ensure that User ID authentication is enabled:
574 if ! grep -q "^AuthorizedKeysFile[[:space:]]\+${VARLIB}/authorized_keys/%u$" "$sshd_config"; then
575 echo "! $sshd_config does not point to monkeysphere authorized keys."
576 echo " - Recommendation: add a line to $sshd_config: 'AuthorizedKeysFile ${VARLIB}/authorized_keys/%u'"
578 if badauthorizedkeys=$(grep -i '^AuthorizedKeysFile' "$sshd_config" | grep -q -v "^AuthorizedKeysFile[[:space:]]\+${VARLIB}/authorized_keys/%u$") ; then
579 echo "! /etc/sshd_config refers to non-monkeysphere authorized_keys files:"
580 echo "$badauthorizedkeys"
581 echo " - Recommendation: remove the above AuthorizedKeysFile lines from $sshd_config"
585 # retrieve key from web of trust, import it into the host keyring, and
586 # ltsign the key in the host keyring so that it may certify other keys
596 # set default values for trust depth and domain
602 TEMP=$(getopt -o n:t:d: -l domain:,trust:,depth: -n "$PGRM" -- "$@")
604 if [ $? != 0 ] ; then
608 # Note the quotes around `$TEMP': they are essential!
635 if [ -z "$keyID" ] ; then
636 failure "You must specify the key ID of a key to add."
640 # get the key from the key server
641 gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'"
643 # get the full fingerprint of a key ID
644 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
645 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
648 gpg_authentication "--fingerprint $fingerprint"
650 echo "Are you sure you want to add this key as a certifier of"
651 read -p "users on this system? (y/N) " OK; OK=${OK:-N}
652 if [ "${OK/y/Y}" != 'Y' ] ; then
656 # export the key to the host keyring
657 gpg_authentication "--export $keyID" | gpg_host --import
659 if [ "$trust" == marginal ]; then
661 elif [ "$trust" == full ]; then
664 failure "trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)"
668 # NOTE: *all* user IDs will be ltsigned
669 ltsignCommand=$(cat <<EOF
681 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
683 # update the trustdb for the authentication keyring
684 gpg_authentication "--check-trustdb"
687 # delete a certifiers key from the host keyring
693 if [ -z "$keyID" ] ; then
694 failure "You must specify the key ID of a key to remove."
697 # delete the requested key (with prompting)
698 gpg_host --delete-key "$keyID"
700 # update the trustdb for the authentication keyring
701 gpg_authentication "--check-trustdb"
704 # list the host certifiers
709 # issue command to gpg-authentication keyring
710 gpg_authentication_cmd() {
711 gpg_authentication "$@"
714 ########################################################################
716 ########################################################################
718 # unset variables that should be defined only in config file
720 unset AUTHORIZED_USER_IDS
721 unset RAW_AUTHORIZED_KEYS
722 unset MONKEYSPHERE_USER
724 # load configuration file
725 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
727 # set empty config variable with ones from the environment, or with
729 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
730 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
731 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
732 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
735 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
736 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
737 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
738 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
740 # export variables needed in su invocation
743 export MONKEYSPHERE_USER
745 export CHECK_KEYSERVER
746 export REQUIRED_USER_KEY_CAPABILITY
747 export GNUPGHOME_HOST
748 export GNUPGHOME_AUTHENTICATION
753 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
757 'update-users'|'update-user'|'u')
765 'add-hostname'|'add-name'|'n+')
769 'revoke-hostname'|'revoke-name'|'n-')
773 'show-key'|'show'|'s')
777 'show-fingerprint'|'fingerprint'|'f')
778 fingerprint_server_key
781 'publish-key'|'publish'|'p')
789 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
793 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
794 remove_certifier "$@"
797 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
801 'gpg-authentication-cmd')
802 gpg_authentication_cmd "$@"
805 '--help'|'help'|'-h'|'h'|'?')
810 failure "Unknown command: '$COMMAND'
811 Type '$PGRM help' for usage."