2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
4 # Shared sh functions for the monkeysphere
7 # Jameson Rollins <jrollins@finestructure.net>
8 # Jamie McClelland <jm@mayfirst.org>
9 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
11 # Copyright 2008-2009, released under the GPL, version 3 or later
13 # all-caps variables are meant to be user supplied (ie. from config
14 # file) and are considered global
16 ########################################################################
21 cat "${SYSSHAREDIR}/VERSION"
24 # failure function. exits with code 255, unless specified otherwise.
26 [ "$1" ] && echo "$1" >&2
30 # write output to stderr based on specified LOG_LEVEL the first
31 # parameter is the priority of the output, and everything else is what
32 # is echoed to stderr. If there is nothing else, then output comes
33 # from stdin, and is not prefaced by log prefix.
41 # don't include SILENT in alllevels: it's handled separately
42 # list in decreasing verbosity (all caps).
43 # separate with $IFS explicitly, since we do some fancy footwork
45 alllevels="DEBUG${IFS}VERBOSE${IFS}INFO${IFS}ERROR"
47 # translate lowers to uppers in global log level
48 LOG_LEVEL=$(echo "$LOG_LEVEL" | tr "[:lower:]" "[:upper:]")
50 # just go ahead and return if the log level is silent
51 if [ "$LOG_LEVEL" = 'SILENT' ] ; then
55 for level in $alllevels ; do
56 if [ "$LOG_LEVEL" = "$level" ] ; then
60 if [ -z "$found" ] ; then
65 # get priority from first parameter, translating all lower to
67 priority=$(echo "$1" | tr "[:lower:]" "[:upper:]")
70 # scan over available levels
71 for level in $alllevels ; do
72 # output if the log level matches, set output to true
73 # this will output for all subsequent loops as well.
74 if [ "$LOG_LEVEL" = "$level" ] ; then
77 if [ "$priority" = "$level" -a "$output" = 'true' ] ; then
82 fi | sed 's/^/'"${LOG_PREFIX}"'/' >&2
87 # run command as monkeysphere user
88 su_monkeysphere_user() {
89 # our main goal here is to run the given command as the the
90 # monkeysphere user, but without prompting for any sort of
91 # authentication. If this is not possible, we should just fail.
93 # FIXME: our current implementation is overly restrictive, because
94 # there may be some su PAM configurations that would allow su
95 # "$MONKEYSPHERE_USER" -c "$@" to Just Work without prompting,
96 # allowing specific users to invoke commands which make use of
99 # chpst (from runit) would be nice to use, but we don't want to
100 # introduce an extra dependency just for this. This may be a
101 # candidate for re-factoring if we switch implementation languages.
104 # if monkeysphere user, run the command under bash
105 "$MONKEYSPHERE_USER")
109 # if root, su command as monkeysphere user
111 su "$MONKEYSPHERE_USER" -c "$@"
116 log error "non-privileged user."
121 # cut out all comments(#) and blank lines from standard input
123 grep -v -e "^[[:space:]]*#" -e '^$' "$1"
126 # cut a specified line from standard input
128 head --line="$1" "$2" | tail -1
131 # make a temporary directory
133 mktemp -d ${TMPDIR:-/tmp}/monkeysphere.XXXXXXXXXX
136 # make a temporary file
138 mktemp ${TMPDIR:-/tmp}/monkeysphere.XXXXXXXXXX
141 # this is a wrapper for doing lock functions.
143 # it lets us depend on either lockfile-progs (preferred) or procmail's
144 # lockfile, and should
146 local use_lockfileprogs=true
150 if ! ( type lockfile-create &>/dev/null ) ; then
151 if ! ( type lockfile &>/dev/null ); then
152 failure "Neither lockfile-create nor lockfile are in the path!"
159 if [ -n "$use_lockfileprogs" ] ; then
160 lockfile-create "$file" || failure "unable to lock '$file'"
162 lockfile -r 20 "${file}.lock" || failure "unable to lock '$file'"
164 log debug "lock created on '$file'."
167 if [ -n "$use_lockfileprogs" ] ; then
168 lockfile-touch --oneshot "$file"
172 log debug "lock touched on '$file'."
175 if [ -n "$use_lockfileprogs" ] ; then
176 lockfile-remove "$file"
180 log debug "lock removed on '$file'."
183 failure "bad argument for lock subfunction '$action'"
188 # for portability, between gnu date and BSD date.
189 # arguments should be: number longunits format
191 # e.g. advance_date 20 seconds +%F
199 # try things the GNU way first
200 if date -d "$number $longunits" "$format" &>/dev/null; then
201 date -d "$number $longunits" "$format"
203 # otherwise, convert to (a limited version of) BSD date syntax:
227 # this is a longshot, and will likely fail; oh well.
228 shortunits="$longunits"
230 date "-v+${number}${shortunits}" "$format"
235 # check that characters are in a string (in an AND fashion).
236 # used for checking key capability
237 # check_capability capability a [b...]
246 if echo "$usage" | grep -q -v "$capcheck" ; then
255 if type md5sum &>/dev/null ; then
257 elif type md5 &>/dev/null ; then
260 failure "Neither md5sum nor md5 are in the path!"
264 # convert escaped characters in pipeline from gpg output back into
266 # FIXME: undo all escape character translation in with-colons gpg
272 # convert nasty chars into gpg-friendly form in pipeline
273 # FIXME: escape everything, not just colons!
278 # prompt for GPG-formatted expiration, and emit result on stdout
279 get_gpg_expiration() {
284 if [ -z "$keyExpire" -a "$PROMPT" != 'false' ]; then
286 Please specify how long the key should be valid.
287 0 = key does not expire
288 <n> = key expires in n days
289 <n>w = key expires in n weeks
290 <n>m = key expires in n months
291 <n>y = key expires in n years
293 while [ -z "$keyExpire" ] ; do
294 printf "Key is valid for? (0) " >&2
296 if ! test_gpg_expire ${keyExpire:=0} ; then
297 echo "invalid value" >&2
301 elif ! test_gpg_expire "$keyExpire" ; then
302 failure "invalid key expiration value '$keyExpire'."
308 passphrase_prompt() {
313 if [ "$DISPLAY" ] && type "${SSH_ASKPASS:-ssh-askpass}" >/dev/null 2>/dev/null; then
314 printf 'Launching "%s"\n' "${SSH_ASKPASS:-ssh-askpass}" | log info
315 printf '(with prompt "%s")\n' "$prompt" | log debug
316 "${SSH_ASKPASS:-ssh-askpass}" "$prompt" > "$fifo"
318 read -s -p "$prompt" PASS
319 # Uses the builtin echo, so should not put the passphrase into
320 # the process table. I think. --dkg
321 echo "$PASS" > "$fifo"
325 # remove all lines with specified string from specified file
334 if [ -z "$file" -o -z "$string" ] ; then
338 if [ ! -e "$file" ] ; then
342 # if the string is in the file...
343 if grep -q -F "$string" "$file" 2>/dev/null ; then
344 tempfile=$(mktemp "${file}.XXXXXXX") || \
345 failure "Unable to make temp file '${file}.XXXXXXX'"
347 # remove the line with the string, and return 0
348 grep -v -F "$string" "$file" >"$tempfile"
349 cat "$tempfile" > "$file"
358 # remove all lines with MonkeySphere strings in file
359 remove_monkeysphere_lines() {
365 # return error if file does not exist
366 if [ ! -e "$file" ] ; then
370 # just return ok if the file is empty, since there aren't any
372 if [ ! -s "$file" ] ; then
376 tempfile=$(mktemp "${file}.XXXXXXX") || \
377 failure "Could not make temporary file '${file}.XXXXXXX'."
379 egrep -v '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$' \
381 cat "$tempfile" > "$file"
385 # translate ssh-style path variables %h and %u
386 translate_ssh_variables() {
393 # get the user's home directory
394 userHome=$(get_homedir "$uname")
396 # translate '%u' to user name
397 path=${path/\%u/"$uname"}
398 # translate '%h' to user home directory
399 path=${path/\%h/"$userHome"}
404 # test that a string to conforms to GPG's expiration format
406 echo "$1" | egrep -q "^[0-9]+[mwy]?$"
409 # check that a file is properly owned, and that all it's parent
410 # directories are not group/other writable
411 check_key_file_permissions() {
418 if [ "$STRICT_MODES" = 'false' ] ; then
419 log debug "skipping path permission check for '$path' because STRICT_MODES is false..."
422 log debug "checking path permission '$path'..."
423 "${SYSSHAREDIR}/checkperms" "$uname" "$path"
426 # return a list of all users on the system
428 if type getent &>/dev/null ; then
429 # for linux and FreeBSD systems
430 getent passwd | cut -d: -f1
431 elif type dscl &>/dev/null ; then
433 dscl localhost -list /Search/Users
435 failure "Neither getent or dscl is in the path! Could not determine list of users."
439 # take one argument, a service name. in response, print a series of
440 # lines, each with a unique numeric port number that might be
441 # associated with that service name. (e.g. in: "https", out: "443")
442 # if nothing is found, print nothing, and return 0.
444 # return 1 if there was an error in the search somehow
445 get_port_for_service() {
447 [[ "$1" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]] || \
448 failure $(printf "This is not a valid service name: '%s'" "$1")
449 if type getent &>/dev/null ; then
450 # for linux and FreeBSD systems (getent returns 2 if not found, 0 on success, 1 or 3 on various failures)
451 (getent services "$service" || if [ "$?" -eq 2 ] ; then true ; else false; fi) | awk '{ print $2 }' | cut -f1 -d/ | sort -u
452 elif [ -r /etc/services ] ; then
453 # fall back to /etc/services for systems that don't have getent (MacOS?)
454 # FIXME: doesn't handle aliases like "null" (or "http"?), which don't show up at the beginning of the line.
455 awk $(printf '/^%s[[:space:]]/{ print $2 }' "$1") /etc/services | cut -f1 -d/ | sort -u
461 # return the path to the home directory of a user
463 local uname=${1:-`whoami`}
464 eval "echo ~${uname}"
467 # return the primary group of a user
468 get_primary_group() {
469 local uname=${1:-`whoami`}
470 groups "$uname" | sed 's/^..* : //' | awk '{ print $1 }'
473 ### CONVERSION UTILITIES
475 # output the ssh key for a given key ID
481 gpg --export --no-armor "$keyID" | openpgp2ssh "$keyID" 2>/dev/null
484 # output known_hosts line from ssh key
490 # FIXME this does not properly deal with IPv6 hosts using the
491 # standard port (because it's unclear whether their final
492 # colon-delimited address section is a port number or an address
498 # specify the host and port properly for new ssh known_hosts
500 if [ "$port" != "$host" ] ; then
501 host="[${host}]:${port}"
503 printf "%s %s MonkeySphere%s\n" "$host" "$key" "$DATE"
506 # output authorized_keys line from ssh key
507 ssh2authorized_keys() {
514 printf "%s MonkeySphere%s %s\n" "$key" "$DATE" "$userID"
517 # convert key from gpg to ssh known_hosts format
526 key=$(gpg2ssh "$keyID")
528 # NOTE: it seems that ssh-keygen -R removes all comment fields from
529 # all lines in the known_hosts file. why?
530 # NOTE: just in case, the COMMENT can be matched with the
532 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
533 printf "%s %s MonkeySphere%s\n" "$host" "$key" "$DATE"
536 # convert key from gpg to ssh authorized_keys format
537 gpg2authorized_keys() {
545 key=$(gpg2ssh "$keyID")
547 # NOTE: just in case, the COMMENT can be matched with the
549 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
550 printf "%s MonkeySphere%s %s\n" "$key" "$DATE" "$userID"
555 # script to determine if gpg version is equal to or greater than specified version
556 is_gpg_version_greater_equal() {
557 local gpgVersion=$(gpg --version | head -1 | awk '{ print $3 }')
558 local latest=$(printf '%s\n%s\n' "$1" "$gpgVersion" \
559 | tr '.' ' ' | sort -g -k1 -k2 -k3 \
560 | tail -1 | tr ' ' '.')
561 [[ "$gpgVersion" == "$latest" ]]
564 # retrieve all keys with given user id from keyserver
565 # FIXME: need to figure out how to retrieve all matching keys
566 # (not just first N (5 in this case))
571 if [ "$CHECK_KEYSERVER" != 'true' ] ; then
577 log verbose " checking keyserver $KEYSERVER... "
579 gpg --quiet --batch --with-colons \
580 --command-fd 0 --keyserver "$KEYSERVER" \
581 --search ="$userID" &>/dev/null
587 ########################################################################
588 ### PROCESSING FUNCTIONS
590 # userid and key policy checking
591 # the following checks policy on the returned keys
592 # - checks that full key has appropriate valididy (u|f)
593 # - checks key has specified capability (REQUIRED_KEY_CAPABILITY)
594 # - checks that requested user ID has appropriate validity
595 # (see /usr/share/doc/gnupg/DETAILS.gz)
596 # output is one line for every found key, in the following format:
600 # "flag" is an acceptability flag, 0 = ok, 1 = bad
601 # "sshKey" is the translated gpg key
603 # all log output must go to stderr, as stdout is used to pass the
604 # flag:sshKey to the calling function.
608 local requiredCapability
609 local requiredPubCapability
624 # set the required key capability based on the mode
625 requiredCapability=${REQUIRED_KEY_CAPABILITY:="a"}
626 requiredPubCapability=$(echo "$requiredCapability" | tr "[:lower:]" "[:upper:]")
628 # fetch the user ID if necessary/requested
629 gpg_fetch_userid "$userID"
631 # output gpg info for (exact) userid and store
632 gpgOut=$(gpg --list-key --fixed-list-mode --with-colon \
633 --with-fingerprint --with-fingerprint \
634 ="$userID" 2>/dev/null) || returnCode="$?"
636 # if the gpg query return code is not 0, return 1
637 if [ "$returnCode" -ne 0 ] ; then
638 log verbose " no primary keys found."
642 # loop over all lines in the gpg output and process.
643 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
644 while IFS=: read -r type validity keyid uidfpr usage ; do
645 # process based on record type
647 'pub') # primary keys
648 # new key, wipe the slate
655 log verbose " primary key found: $keyid"
657 # if overall key is not valid, skip
658 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
659 log debug " - unacceptable primary key validity ($validity)."
662 # if overall key is disabled, skip
663 if check_capability "$usage" 'D' ; then
664 log debug " - key disabled."
667 # if overall key capability is not ok, skip
668 if ! check_capability "$usage" $requiredPubCapability ; then
669 log debug " - unacceptable primary key capability ($usage)."
673 # mark overall key as ok
676 # mark primary key as ok if capability is ok
677 if check_capability "$usage" $requiredCapability ; then
682 if [ "$lastKey" != pub ] ; then
683 log verbose " ! got a user ID after a sub key?! user IDs should only follow primary keys!"
686 # if an acceptable user ID was already found, skip
687 if [ "$uidOK" = 'true' ] ; then
690 # if the user ID does matches...
691 if [ "$(echo "$uidfpr" | gpg_unescape)" = "$userID" ] ; then
692 # and the user ID validity is ok
693 if [ "$validity" = 'u' -o "$validity" = 'f' ] ; then
694 # mark user ID acceptable
697 log debug " - unacceptable user ID validity ($validity)."
703 # output a line for the primary key
705 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
706 log verbose " * acceptable primary key."
707 if [ -z "$sshKey" ] ; then
708 log error " ! primary key could not be translated (not RSA?)."
713 log debug " - unacceptable primary key."
714 if [ -z "$sshKey" ] ; then
715 log debug " ! primary key could not be translated (not RSA?)."
722 # unset acceptability of last key
727 # don't bother with sub keys if the primary key is not valid
728 if [ "$keyOK" != true ] ; then
732 # don't bother with sub keys if no user ID is acceptable:
733 if [ "$uidOK" != true ] ; then
737 # if sub key validity is not ok, skip
738 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
739 log debug " - unacceptable sub key validity ($validity)."
742 # if sub key capability is not ok, skip
743 if ! check_capability "$usage" $requiredCapability ; then
744 log debug " - unacceptable sub key capability ($usage)."
751 'fpr') # key fingerprint
752 fingerprint="$uidfpr"
754 sshKey=$(gpg2ssh "$fingerprint")
756 # if the last key was the pub key, skip
757 if [ "$lastKey" = pub ] ; then
761 # output a line for the sub key
763 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
764 log verbose " * acceptable sub key."
765 if [ -z "$sshKey" ] ; then
766 log error " ! sub key could not be translated (not RSA?)."
771 log debug " - unacceptable sub key."
772 if [ -z "$sshKey" ] ; then
773 log debug " ! sub key could not be translated (not RSA?)."
780 done | sort -t: -k1 -n -r
781 # NOTE: this last sort is important so that the "good" keys (key
782 # flag '0') come last. This is so that they take precedence when
783 # being processed in the key files over "bad" keys (key flag '1')
786 # output all valid keys for specified user ID literal
798 log verbose "processing: $userID"
804 for line in $(process_user_id "${userID}") ; do
805 # note that key was found
808 ok=$(echo "$line" | cut -d: -f1)
809 sshKey=$(echo "$line" | cut -d: -f2)
811 if [ -z "$sshKey" ] ; then
815 # if key OK, output key to stdout
816 if [ "$ok" -eq '0' ] ; then
817 # note that key was found ok
818 nKeysOK=$((nKeysOK+1))
820 printf '%s\n' "$sshKey"
824 # if at least one key was found...
825 if [ "$nKeys" -gt 0 ] ; then
826 # if ok keys were found, return 0
827 if [ "$nKeysOK" -gt 0 ] ; then
833 # if no keys were found, return 1
839 # process a single host in the known_host file
840 process_host_known_hosts() {
850 # set the key processing mode
851 export REQUIRED_KEY_CAPABILITY="$REQUIRED_HOST_KEY_CAPABILITY"
854 userID="ssh://${host}"
856 log verbose "processing: $host"
862 for line in $(process_user_id "${userID}") ; do
863 # note that key was found
866 ok=$(echo "$line" | cut -d: -f1)
867 sshKey=$(echo "$line" | cut -d: -f2)
869 if [ -z "$sshKey" ] ; then
873 # remove any old host key line, and note if removed nothing is
875 remove_line "$KNOWN_HOSTS" "$sshKey" || noKey=true
877 # if key OK, add new host line
878 if [ "$ok" -eq '0' ] ; then
879 # note that key was found ok
880 nKeysOK=$((nKeysOK+1))
883 if [ "$HASH_KNOWN_HOSTS" = 'true' ] ; then
884 # FIXME: this is really hackish cause ssh-keygen won't
885 # hash from stdin to stdout
886 tmpfile=$(mktemp ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)
887 ssh2known_hosts "$host" "$sshKey" > "$tmpfile"
888 ssh-keygen -H -f "$tmpfile" 2>/dev/null
889 cat "$tmpfile" >> "$KNOWN_HOSTS"
890 rm -f "$tmpfile" "${tmpfile}.old"
892 ssh2known_hosts "$host" "$sshKey" >> "$KNOWN_HOSTS"
895 # log if this is a new key to the known_hosts file
896 if [ "$noKey" ] ; then
897 log info "* new key for $host added to known_hosts file."
902 # if at least one key was found...
903 if [ "$nKeys" -gt 0 ] ; then
904 # if ok keys were found, return 0
905 if [ "$nKeysOK" -gt 0 ] ; then
911 # if no keys were found, return 1
917 # update the known_hosts file for a set of hosts listed on command
919 update_known_hosts() {
928 # the number of hosts specified on command line
934 # touch the known_hosts file so that the file permission check
935 # below won't fail upon not finding the file
936 if [ ! -f "$KNOWN_HOSTS" ]; then
937 # make sure to create any files or directories with the appropriate write bits turned off:
938 newUmask=$(printf "%04o" $(( 0$(umask) | 0022 )) )
939 [ -d $(dirname "$KNOWN_HOSTS") ] \
940 || (umask "$newUmask" && mkdir -p -m 0700 $(dirname "$KNOWN_HOSTS") ) \
941 || failure "Could not create path to known_hosts file '$KNOWN_HOSTS'"
942 # make sure to create this file with the appropriate bits turned off:
943 (umask "$newUmask" && touch "$KNOWN_HOSTS") \
944 || failure "Unable to create known_hosts file '$KNOWN_HOSTS'"
947 # check permissions on the known_hosts file path
948 check_key_file_permissions $(whoami) "$KNOWN_HOSTS" \
949 || failure "Bad permissions governing known_hosts file '$KNOWN_HOSTS'"
951 # create a lockfile on known_hosts:
952 lock create "$KNOWN_HOSTS"
953 # FIXME: we're discarding any pre-existing EXIT trap; is this bad?
954 trap "lock remove $KNOWN_HOSTS" EXIT
956 # note pre update file checksum
957 fileCheck=$(file_hash "$KNOWN_HOSTS")
961 process_host_known_hosts "$host" || returnCode="$?"
963 case "$returnCode" in
965 nHostsOK=$((nHostsOK+1))
968 nHostsBAD=$((nHostsBAD+1))
972 # touch the lockfile, for good measure.
973 lock touch "$KNOWN_HOSTS"
976 # remove the lockfile and the trap
977 lock remove "$KNOWN_HOSTS"
980 # note if the known_hosts file was updated
981 if [ "$(file_hash "$KNOWN_HOSTS")" != "$fileCheck" ] ; then
982 log debug "known_hosts file updated."
985 # if an acceptable host was found, return 0
986 if [ "$nHostsOK" -gt 0 ] ; then
988 # else if no ok hosts were found...
990 # if no bad host were found then no hosts were found at all,
992 if [ "$nHostsBAD" -eq 0 ] ; then
994 # else if at least one bad host was found, return 2
1001 # process hosts from a known_hosts file
1002 process_known_hosts() {
1005 # exit if the known_hosts file does not exist
1006 if [ ! -e "$KNOWN_HOSTS" ] ; then
1007 failure "known_hosts file '$KNOWN_HOSTS' does not exist."
1010 log debug "processing known_hosts file:"
1011 log debug " $KNOWN_HOSTS"
1013 hosts=$(meat "$KNOWN_HOSTS" | cut -d ' ' -f 1 | grep -v '^|.*$' | tr , ' ' | tr '\n' ' ')
1015 if [ -z "$hosts" ] ; then
1016 log debug "no hosts to process."
1020 # take all the hosts from the known_hosts file (first
1021 # field), grep out all the hashed hosts (lines starting
1023 update_known_hosts $hosts
1026 # process uids for the authorized_keys file
1027 process_uid_authorized_keys() {
1034 # set the key processing mode
1035 export REQUIRED_KEY_CAPABILITY="$REQUIRED_USER_KEY_CAPABILITY"
1039 log verbose "processing: $userID"
1045 for line in $(process_user_id "$userID") ; do
1046 # note that key was found
1049 ok=$(echo "$line" | cut -d: -f1)
1050 sshKey=$(echo "$line" | cut -d: -f2)
1052 if [ -z "$sshKey" ] ; then
1056 # remove the old host key line
1057 remove_line "$AUTHORIZED_KEYS" "$sshKey"
1059 # if key OK, add new host line
1060 if [ "$ok" -eq '0' ] ; then
1061 # note that key was found ok
1062 nKeysOK=$((nKeysOK+1))
1064 ssh2authorized_keys "$userID" "$sshKey" >> "$AUTHORIZED_KEYS"
1068 # if at least one key was found...
1069 if [ "$nKeys" -gt 0 ] ; then
1070 # if ok keys were found, return 0
1071 if [ "$nKeysOK" -gt 0 ] ; then
1077 # if no keys were found, return 1
1083 # update the authorized_keys files from a list of user IDs on command
1085 update_authorized_keys() {
1093 # the number of ids specified on command line
1099 log debug "updating authorized_keys file:"
1100 log debug " $AUTHORIZED_KEYS"
1102 # check permissions on the authorized_keys file path
1103 check_key_file_permissions $(whoami) "$AUTHORIZED_KEYS" || failure
1105 # create a lockfile on authorized_keys
1106 lock create "$AUTHORIZED_KEYS"
1107 # FIXME: we're discarding any pre-existing EXIT trap; is this bad?
1108 trap "lock remove $AUTHORIZED_KEYS" EXIT
1110 # note pre update file checksum
1111 fileCheck="$(file_hash "$AUTHORIZED_KEYS")"
1113 # remove any monkeysphere lines from authorized_keys file
1114 remove_monkeysphere_lines "$AUTHORIZED_KEYS"
1117 # process the user ID, change return code if key not found for
1119 process_uid_authorized_keys "$userID" || returnCode="$?"
1122 case "$returnCode" in
1124 nIDsOK=$((nIDsOK+1))
1127 nIDsBAD=$((nIDsBAD+1))
1131 # touch the lockfile, for good measure.
1132 lock touch "$AUTHORIZED_KEYS"
1135 # remove the lockfile and the trap
1136 lock remove "$AUTHORIZED_KEYS"
1141 # note if the authorized_keys file was updated
1142 if [ "$(file_hash "$AUTHORIZED_KEYS")" != "$fileCheck" ] ; then
1143 log debug "authorized_keys file updated."
1146 # if an acceptable id was found, return 0
1147 if [ "$nIDsOK" -gt 0 ] ; then
1149 # else if no ok ids were found...
1151 # if no bad ids were found then no ids were found at all, and
1153 if [ "$nIDsBAD" -eq 0 ] ; then
1155 # else if at least one bad id was found, return 2
1162 # process an authorized_user_ids file for authorized_keys
1163 process_authorized_user_ids() {
1168 authorizedUserIDs="$1"
1170 # exit if the authorized_user_ids file is empty
1171 if [ ! -e "$authorizedUserIDs" ] ; then
1172 failure "authorized_user_ids file '$authorizedUserIDs' does not exist."
1175 log debug "processing authorized_user_ids file:"
1176 log debug " $authorizedUserIDs"
1178 # check permissions on the authorized_user_ids file path
1179 check_key_file_permissions $(whoami) "$authorizedUserIDs" || failure
1181 if ! meat "$authorizedUserIDs" >/dev/null ; then
1182 log debug " no user IDs to process."
1188 # extract user IDs from authorized_user_ids file
1190 for line in $(meat "$authorizedUserIDs") ; do
1191 userIDs["$nline"]="$line"
1195 update_authorized_keys "${userIDs[@]}"
1198 # takes a gpg key or keys on stdin, and outputs a list of
1199 # fingerprints, one per line:
1200 list_primary_fingerprints() {
1201 local fake=$(msmktempdir)
1202 trap "rm -rf $fake" EXIT
1203 GNUPGHOME="$fake" gpg --no-tty --quiet --import --ignore-time-conflict 2>/dev/null
1204 GNUPGHOME="$fake" gpg --with-colons --fingerprint --list-keys | \
1205 awk -F: '/^fpr:/{ print $10 }'
1210 # takes an OpenPGP key or set of keys on stdin, a fingerprint or other
1211 # key identifier as $1, and outputs the gpg-formatted information for
1212 # the requested keys from the material on stdin
1214 local fake=$(msmktempdir)
1215 trap "rm -rf $fake" EXIT
1216 GNUPGHOME="$fake" gpg --no-tty --quiet --import --ignore-time-conflict 2>/dev/null
1217 GNUPGHOME="$fake" gpg --with-colons --fingerprint --fixed-list-mode --list-keys "$1"
1223 check_cruft_file() {
1227 if [ -e "$loc" ] ; then
1228 printf "! The file '%s' is no longer used by\n monkeysphere (as of version %s), and can be removed.\n\n" "$loc" "$version" | log info
1232 check_upgrade_dir() {
1236 if [ -d "$loc" ] ; then
1237 printf "The presence of directory '%s' indicates that you have\nnot yet completed a monkeysphere upgrade.\nYou should probably run the following script:\n %s/transitions/%s\n\n" "$loc" "$SYSSHAREDIR" "$version" | log info
1241 ## look for cruft from old versions of the monkeysphere, and notice if
1242 ## upgrades have not been run:
1244 check_upgrade_dir "${SYSCONFIGDIR}/gnupg-host" 0.23
1245 check_upgrade_dir "${SYSCONFIGDIR}/gnupg-authentication" 0.23
1247 check_cruft_file "${SYSCONFIGDIR}/gnupg-authentication.conf" 0.23
1248 check_cruft_file "${SYSCONFIGDIR}/gnupg-host.conf" 0.23
1251 for foo in "${SYSDATADIR}/backup-from-"*"-transition" ; do
1252 if [ -d "$foo" ] ; then
1253 printf "! %s\n" "$foo" | log info
1257 if [ "$found" ] ; then
1258 printf "The directories above are backups left over from a monkeysphere transition.\nThey may contain copies of sensitive data (host keys, certifier lists), but\nthey are no longer needed by monkeysphere.\nYou may remove them at any time.\n\n" | log info