3 # monkeysphere-authentication: Monkeysphere authentication 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/authentication"}
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 authentication admin tool.
47 update-users (u) [USER]... update user authorized_keys files
48 add-id-certifier (c+) KEYID import and tsign a certification key
49 --domain (-n) DOMAIN limit ID certifications to DOMAIN
50 --trust (-t) TRUST trust level of certifier (full)
51 --depth (-d) DEPTH trust depth for certifier (1)
52 remove-id-certifier (c-) KEYID remove a certification key
53 list-id-certifiers (c) list certification keys
56 diagnostics (d) monkeysphere authentication status
57 gpg-cmd CMD execute gpg command
59 version (v) show version number
65 # function to run command as monkeysphere user
66 su_monkeysphere_user() {
67 # if the current user is the monkeysphere user, then just eval
69 if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
72 # otherwise su command as monkeysphere user
74 su "$MONKEYSPHERE_USER" -c "$@"
78 # function to interact with the host gnupg keyring
82 GNUPGHOME="$GNUPGHOME_HOST"
85 # NOTE: we supress this warning because we need the monkeysphere
86 # user to be able to read the host pubring. we realize this might
87 # be problematic, but it's the simplest solution, without too much
89 gpg --no-permission-warning "$@"
92 # always reset the permissions on the host pubring so that the
93 # monkeysphere user can read the trust signatures
94 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
95 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
100 # function to interact with the authentication gnupg keyring
101 # FIXME: this function requires basically accepts only a single
102 # argument because of problems with quote expansion. this needs to be
104 gpg_authentication() {
105 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
108 su_monkeysphere_user "gpg $@"
111 # check if user is root
113 [ $(id -u 2>/dev/null) = '0' ]
116 # check that user is root, for functions that require root access
118 is_root || failure "You must be root to run this command."
121 # output just key fingerprint
122 fingerprint_server_key() {
123 # set the pipefail option so functions fails if can't read sec key
126 gpg_host --list-secret-keys --fingerprint \
127 --with-colons --fixed-list-mode 2> /dev/null | \
128 grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
131 # function to check for host secret key
132 check_host_keyring() {
133 fingerprint_server_key >/dev/null \
134 || failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-server gen-key' first."
137 # update authorized_keys for users
140 # get users from command line
143 # or just look at all users if none specified
144 unames=$(getent passwd | cut -d: -f1)
150 MODE="authorized_keys"
153 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
155 # check to see if the gpg trust database has been initialized
156 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
157 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
160 # make sure the authorized_keys directory exists
161 mkdir -p "${SYSDATADIR}/authorized_keys"
164 for uname in $unames ; do
165 # check all specified users exist
166 if ! id "$uname" >/dev/null ; then
167 log error "----- unknown user '$uname' -----"
171 log verbose "----- user: $uname -----"
173 # make temporary directory
174 TMPLOC=$(mktemp -d ${MSTMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
176 # trap to delete temporary directory on exit
177 trap "rm -rf $TMPLOC" EXIT
179 # create temporary authorized_user_ids file
180 TMP_AUTHORIZED_USER_IDS="${TMPLOC}/authorized_user_ids"
181 touch "$TMP_AUTHORIZED_USER_IDS"
183 # create temporary authorized_keys file
184 AUTHORIZED_KEYS="${TMPLOC}/authorized_keys"
185 touch "$AUTHORIZED_KEYS"
187 # set restrictive permissions on the temporary files
188 # FIXME: is there a better way to do this?
190 chmod 0600 "$AUTHORIZED_KEYS"
191 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
192 chown -R "$MONKEYSPHERE_USER" "$TMPLOC"
194 # process authorized_user_ids file
195 log debug "checking for authorized_user_ids..."
196 # translating ssh-style path variables
197 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
198 if [ -s "$authorizedUserIDs" ] ; then
199 # check permissions on the authorized_user_ids file path
200 if check_key_file_permissions "$uname" "$authorizedUserIDs" ; then
201 # copy user authorized_user_ids file to temporary
203 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
205 # export needed variables
206 export AUTHORIZED_KEYS
207 export TMP_AUTHORIZED_USER_IDS
209 # process authorized_user_ids file, as monkeysphere
211 su_monkeysphere_user \
212 ". ${SYSSHAREDIR}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
215 log debug "not processing authorized_user_ids."
218 log debug "empty or absent authorized_user_ids file."
221 # add user-controlled authorized_keys file if specified
222 # translate ssh-style path variables
223 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
224 if [ "$rawAuthorizedKeys" != 'none' ] ; then
225 log debug "checking for raw authorized_keys..."
226 if [ -s "$rawAuthorizedKeys" ] ; then
227 # check permissions on the authorized_keys file path
228 if check_key_file_permissions "$uname" "$rawAuthorizedKeys" ; then
229 log verbose "adding raw authorized_keys file... "
230 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
232 log debug "not adding raw authorized_keys file."
235 log debug "empty or absent authorized_keys file."
239 # move the new authorized_keys file into place
240 if [ -s "$AUTHORIZED_KEYS" ] ; then
241 # openssh appears to check the contents of the
242 # authorized_keys file as the user in question, so the
243 # file must be readable by that user at least.
245 # but in general, we don't want the user tampering with
246 # this file directly, so we'll adopt this approach: Own
247 # the file by the monkeysphere-server invoker (usually
248 # root, but should be the same uid that sshd is launched
249 # as); change the group of the file so that members of the
250 # user's group can read it.
252 # FIXME: is there a better way to do this?
253 chown $(whoami) "$AUTHORIZED_KEYS" && \
254 chgrp $(id -g "$uname") "$AUTHORIZED_KEYS" && \
255 chmod g+r "$AUTHORIZED_KEYS" && \
256 mv -f "$AUTHORIZED_KEYS" "${SYSDATADIR}/authorized_keys/${uname}" || \
258 log error "Failed to install authorized_keys for '$uname'!"
259 rm -f "${SYSDATADIR}/authorized_keys/${uname}"
260 # indicate that there has been a failure:
264 rm -f "${SYSDATADIR}/authorized_keys/${uname}"
270 # destroy temporary directory
276 # * check on the status and validity of the key and public certificates
288 local problemsfound=0
290 # FIXME: what's the correct, cross-platform answer?
291 sshd_config=/etc/ssh/sshd_config
292 seckey=$(gpg_host --list-secret-keys --fingerprint --with-colons --fixed-list-mode)
293 keysfound=$(echo "$seckey" | grep -c ^sec:)
295 # warn when anything is 2 months away from expiration
296 warnwindow='2 months'
297 warndate=$(advance_date $warnwindow +%s)
299 if ! id monkeysphere >/dev/null ; then
300 echo "! No monkeysphere user found! Please create a monkeysphere system user with bash as its shell."
301 problemsfound=$(($problemsfound+1))
304 if ! [ -d "$SYSDATADIR" ] ; then
305 echo "! no $SYSDATADIR directory found. Please create it."
306 problemsfound=$(($problemsfound+1))
309 echo "Checking host GPG key..."
310 if (( "$keysfound" < 1 )); then
311 echo "! No host key found."
312 echo " - Recommendation: run 'monkeysphere-server gen-key'"
313 problemsfound=$(($problemsfound+1))
314 elif (( "$keysfound" > 1 )); then
315 echo "! More than one host key found?"
316 # FIXME: recommend a way to resolve this
317 problemsfound=$(($problemsfound+1))
319 create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
320 expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
321 fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
322 # check for key expiration:
323 if [ "$expire" ]; then
324 if (( "$expire" < "$curdate" )); then
325 echo "! Host key is expired."
326 echo " - Recommendation: extend lifetime of key with 'monkeysphere-server extend-key'"
327 problemsfound=$(($problemsfound+1))
328 elif (( "$expire" < "$warndate" )); then
329 echo "! Host key expires in less than $warnwindow:" $(advance_date $(( $expire - $curdate )) seconds +%F)
330 echo " - Recommendation: extend lifetime of key with 'monkeysphere-server extend-key'"
331 problemsfound=$(($problemsfound+1))
336 if [ "$create" ] && (( "$create" > "$curdate" )); then
337 echo "! Host key was created in the future(?!). Is your clock correct?"
338 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
339 problemsfound=$(($problemsfound+1))
342 # check for UserID expiration:
343 echo "$seckey" | grep ^uid: | cut -d: -f6,7,10 | \
344 while IFS=: read create expire uid ; do
345 # FIXME: should we be doing any checking on the form
346 # of the User ID? Should we be unmangling it somehow?
348 if [ "$create" ] && (( "$create" > "$curdate" )); then
349 echo "! User ID '$uid' was created in the future(?!). Is your clock correct?"
350 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
351 problemsfound=$(($problemsfound+1))
353 if [ "$expire" ] ; then
354 if (( "$expire" < "$curdate" )); then
355 echo "! User ID '$uid' is expired."
356 # FIXME: recommend a way to resolve this
357 problemsfound=$(($problemsfound+1))
358 elif (( "$expire" < "$warndate" )); then
359 echo "! User ID '$uid' expires in less than $warnwindow:" $(advance_date $(( $expire - $curdate )) seconds +%F)
360 # FIXME: recommend a way to resolve this
361 problemsfound=$(($problemsfound+1))
366 # FIXME: verify that the host key is properly published to the
367 # keyservers (do this with the non-privileged user)
369 # FIXME: check that there are valid, non-expired certifying signatures
370 # attached to the host key after fetching from the public keyserver
371 # (do this with the non-privileged user as well)
373 # FIXME: propose adding a revoker to the host key if none exist (do we
374 # have a way to do that after key generation?)
376 # Ensure that the ssh_host_rsa_key file is present and non-empty:
378 echo "Checking host SSH key..."
379 if [ ! -s "${SYSDATADIR}/ssh_host_rsa_key" ] ; then
380 echo "! The host key as prepared for SSH (${SYSDATADIR}/ssh_host_rsa_key) is missing or empty."
381 problemsfound=$(($problemsfound+1))
383 if [ $(ls -l "${SYSDATADIR}/ssh_host_rsa_key" | cut -f1 -d\ ) != '-rw-------' ] ; then
384 echo "! Permissions seem wrong for ${SYSDATADIR}/ssh_host_rsa_key -- should be 0600."
385 problemsfound=$(($problemsfound+1))
388 # propose changes needed for sshd_config (if any)
389 if ! grep -q "^HostKey[[:space:]]\+${SYSDATADIR}/ssh_host_rsa_key$" "$sshd_config"; then
390 echo "! $sshd_config does not point to the monkeysphere host key (${SYSDATADIR}/ssh_host_rsa_key)."
391 echo " - Recommendation: add a line to $sshd_config: 'HostKey ${SYSDATADIR}/ssh_host_rsa_key'"
392 problemsfound=$(($problemsfound+1))
394 if badhostkeys=$(grep -i '^HostKey' "$sshd_config" | grep -v "^HostKey[[:space:]]\+${SYSDATADIR}/ssh_host_rsa_key$") ; then
395 echo "! $sshd_config refers to some non-monkeysphere host keys:"
397 echo " - Recommendation: remove the above HostKey lines from $sshd_config"
398 problemsfound=$(($problemsfound+1))
401 # FIXME: test (with ssh-keyscan?) that the running ssh
402 # daemon is actually offering the monkeysphere host key.
407 # FIXME: look at the ownership/privileges of the various keyrings,
408 # directories housing them, etc (what should those values be? can
409 # we make them as minimal as possible?)
411 # FIXME: look to see that the ownertrust rules are set properly on the
412 # authentication keyring
414 # FIXME: make sure that at least one identity certifier exists
416 # FIXME: look at the timestamps on the monkeysphere-generated
417 # authorized_keys files -- warn if they seem out-of-date.
419 # FIXME: check for a cronjob that updates monkeysphere-generated
423 echo "Checking for MonkeySphere-enabled public-key authentication for users ..."
424 # Ensure that User ID authentication is enabled:
425 if ! grep -q "^AuthorizedKeysFile[[:space:]]\+${SYSDATADIR}/authorized_keys/%u$" "$sshd_config"; then
426 echo "! $sshd_config does not point to monkeysphere authorized keys."
427 echo " - Recommendation: add a line to $sshd_config: 'AuthorizedKeysFile ${SYSDATADIR}/authorized_keys/%u'"
428 problemsfound=$(($problemsfound+1))
430 if badauthorizedkeys=$(grep -i '^AuthorizedKeysFile' "$sshd_config" | grep -v "^AuthorizedKeysFile[[:space:]]\+${SYSDATADIR}/authorized_keys/%u$") ; then
431 echo "! $sshd_config refers to non-monkeysphere authorized_keys files:"
432 echo "$badauthorizedkeys"
433 echo " - Recommendation: remove the above AuthorizedKeysFile lines from $sshd_config"
434 problemsfound=$(($problemsfound+1))
437 if [ "$problemsfound" -gt 0 ]; then
438 echo "When the above $problemsfound issue"$(if [ "$problemsfound" -eq 1 ] ; then echo " is" ; else echo "s are" ; fi)" resolved, please re-run:"
439 echo " monkeysphere-server diagnostics"
441 echo "Everything seems to be in order!"
445 # retrieve key from web of trust, import it into the host keyring, and
446 # ltsign the key in the host keyring so that it may certify other keys
456 # set default values for trust depth and domain
477 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
478 failure "Unknown option '$1'.
479 Type '$PGRM help' for usage."
487 if [ -z "$keyID" ] ; then
488 failure "You must specify the key ID of a key to add, or specify a file to read the key from."
490 if [ -f "$keyID" ] ; then
491 echo "Reading key from file '$keyID':"
492 importinfo=$(gpg_authentication "--import" < "$keyID" 2>&1) || failure "could not read key from '$keyID'"
493 # FIXME: if this is tried when the key database is not
494 # up-to-date, i got these errors (using set -x):
496 # ++ su -m monkeysphere -c '\''gpg --import'\''
497 # Warning: using insecure memory!
498 # gpg: key D21739E9: public key "Daniel Kahn Gillmor <dkg@fifthhorseman.net>" imported
499 # gpg: Total number processed: 1
500 # gpg: imported: 1 (RSA: 1)
501 # gpg: can'\''t create `/var/monkeysphere/gnupg-host/pubring.gpg.tmp'\'': Permission denied
502 # gpg: failed to rebuild keyring cache: Permission denied
503 # gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
504 # gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
505 # gpg: next trustdb check due at 2009-01-10'
506 # + failure 'could not read key from '\''/root/dkg.gpg'\'''
507 # + echo 'could not read key from '\''/root/dkg.gpg'\'''
509 keyID=$(echo "$importinfo" | grep '^gpg: key ' | cut -f2 -d: | cut -f3 -d\ )
510 if [ -z "$keyID" ] || [ $(echo "$keyID" | wc -l) -ne 1 ] ; then
511 failure "Expected there to be a single gpg key in the file."
514 # get the key from the key server
515 gpg_authentication "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
521 # get the full fingerprint of a key ID
522 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint 0x${keyID}!" | \
523 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
525 if [ -z "$fingerprint" ] ; then
526 failure "Key '$keyID' not found."
531 gpg_authentication "--fingerprint 0x${fingerprint}!"
533 echo "Are you sure you want to add the above key as a"
534 read -p "certifier of users on this system? (y/N) " OK; OK=${OK:-N}
535 if [ "${OK/y/Y}" != 'Y' ] ; then
536 failure "Identity certifier not added."
539 # export the key to the host keyring
540 gpg_authentication "--export 0x${fingerprint}!" | gpg_host --import
542 if [ "$trust" = marginal ]; then
544 elif [ "$trust" = full ]; then
547 failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
551 # NOTE: *all* user IDs will be ltsigned
552 ltsignCommand=$(cat <<EOF
564 if echo "$ltsignCommand" | \
565 gpg_host --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
567 # update the trustdb for the authentication keyring
568 gpg_authentication "--check-trustdb"
571 echo "Identity certifier added."
573 failure "Problem adding identify certifier."
577 # delete a certifiers key from the host keyring
583 if [ -z "$keyID" ] ; then
584 failure "You must specify the key ID of a key to remove."
587 if gpg_authentication "--no-options --list-options show-uid-validity --keyring ${GNUPGHOME_AUTHENTICATION}/pubring.gpg --list-key 0x${keyID}!" ; then
588 read -p "Really remove above listed identity certifier? (y/N) " OK; OK=${OK:-N}
589 if [ "${OK/y/Y}" != 'Y' ] ; then
590 failure "Identity certifier not removed."
596 # delete the requested key
597 if gpg_authentication "--delete-key --batch --yes 0x${keyID}!" ; then
598 # delete key from host keyring as well
599 gpg_host --delete-key --batch --yes "0x${keyID}!"
601 # update the trustdb for the authentication keyring
602 gpg_authentication "--check-trustdb"
605 echo "Identity certifier removed."
607 failure "Problem removing identity certifier."
611 # list the host certifiers
616 # find trusted keys in authentication keychain
617 keys=$(gpg_authentication "--no-options --list-options show-uid-validity --keyring ${GNUPGHOME_AUTHENTICATION}/pubring.gpg --list-keys --with-colons --fingerprint" | \
618 grep ^pub: | cut -d: -f2,5 | egrep '^(u|f):' | cut -d: -f2)
621 for key in $keys ; do
622 gpg_authentication "--no-options --list-options show-uid-validity --keyring ${GNUPGHOME_AUTHENTICATION}/pubring.gpg --list-key --fingerprint $key"
626 ########################################################################
628 ########################################################################
630 # unset variables that should be defined only in config file
632 unset AUTHORIZED_USER_IDS
633 unset RAW_AUTHORIZED_KEYS
634 unset MONKEYSPHERE_USER
636 # load configuration file
637 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${SYSCONFIGDIR}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
639 # set empty config variable with ones from the environment, or with
641 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
642 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
643 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
644 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
645 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
648 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
649 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
650 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${SYSDATADIR}/gnupg-host"}
651 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${SYSDATADIR}/gnupg-authentication"}
653 # export variables needed in su invocation
656 export MONKEYSPHERE_USER
659 export CHECK_KEYSERVER
660 export REQUIRED_USER_KEY_CAPABILITY
661 export GNUPGHOME_HOST
662 export GNUPGHOME_AUTHENTICATION
667 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
671 'update-users'|'update-user'|'u')
677 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
683 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
686 remove_certifier "$@"
689 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
699 case "$SUBCOMMAND" in
705 gpg_authentication "$@"
709 failure "Unknown expert subcommand: '$COMMAND'
710 Type '$PGRM help' for usage."
719 '--help'|'help'|'-h'|'h'|'?')
724 failure "Unknown command: '$COMMAND'
725 Type '$PGRM help' for usage."