3 # Shared sh functions for the monkeysphere
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
10 # Copyright 2008, released under the GPL, version 3 or later
12 # all-caps variables are meant to be user supplied (ie. from config
13 # file) and are considered global
15 ########################################################################
19 ETC="/etc/monkeysphere"
22 ########################################################################
25 # failure function. exits with code 255, unless specified otherwise.
31 # write output to stderr based on specified LOG_LEVEL the first
32 # parameter is the priority of the output, and everything else is what
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
84 # cut out all comments(#) and blank lines from standard input
86 grep -v -e "^[[:space:]]*#" -e '^$' "$1"
89 # cut a specified line from standard input
91 head --line="$1" "$2" | tail -1
94 # check that characters are in a string (in an AND fashion).
95 # used for checking key capability
96 # check_capability capability a [b...]
105 if echo "$usage" | grep -q -v "$capcheck" ; then
114 md5sum "$1" 2> /dev/null
117 # convert escaped characters in pipeline from gpg output back into
119 # FIXME: undo all escape character translation in with-colons gpg
125 # convert nasty chars into gpg-friendly form in pipeline
126 # FIXME: escape everything, not just colons!
131 # prompt for GPG-formatted expiration, and emit result on stdout
132 get_gpg_expiration() {
137 if [ -z "$keyExpire" ]; then
139 Please specify how long the key should be valid.
140 0 = key does not expire
141 <n> = key expires in n days
142 <n>w = key expires in n weeks
143 <n>m = key expires in n months
144 <n>y = key expires in n years
146 while [ -z "$keyExpire" ] ; do
147 read -p "Key is valid for? (0) " keyExpire
148 if ! test_gpg_expire ${keyExpire:=0} ; then
149 echo "invalid value" >&2
153 elif ! test_gpg_expire "$keyExpire" ; then
154 failure "invalid key expiration value '$keyExpire'."
160 passphrase_prompt() {
165 if [ "$DISPLAY" ] && which "${SSH_ASKPASS:-ssh-askpass}" >/dev/null; then
166 "${SSH_ASKPASS:-ssh-askpass}" "$prompt" > "$fifo"
168 read -s -p "$prompt" PASS
169 # Uses the builtin echo, so should not put the passphrase into
170 # the process table. I think. --dkg
171 echo "$PASS" > "$fifo"
175 test_gnu_dummy_s2k_extension() {
177 # this block contains a demonstration private key that has had the
178 # primary key stripped out using the GNU S2K extension known as
179 # "gnu-dummy" (see /usr/share/doc/gnupg/DETAILS.gz). The subkey is
180 # present in cleartext, however.
182 # openpgp2ssh will be able to deal with this based on whether the
183 # local copy of GnuTLS contains read_s2k support that can handle it.
185 # read up on that here:
187 # http://lists.gnu.org/archive/html/gnutls-devel/2008-08/msg00005.html
190 -----BEGIN PGP PRIVATE KEY BLOCK-----
191 Version: GnuPG v1.4.9 (GNU/Linux)
193 lQCVBEO3YdABBACRqqEnucag4+vyZny2M67Pai5+5suIRRvY+Ly8Ms5MvgCi3EVV
194 xT05O/+0ShiRaf+QicCOFrhbU9PZzzU+seEvkeW2UCu4dQfILkmj+HBEIltGnHr3
195 G0yegHj5pnqrcezERURf2e17gGFWX91cXB9Cm721FPXczuKraphKwCA9PwARAQAB
196 /gNlAkdOVQG0OURlbW9uc3RyYXRpb24gS2V5IGZvciBTMksgR05VIGV4dGVuc2lv
197 biAxMDAxIC0tIGdudS1kdW1teYi8BBMBAgAmBQJDt2HQAhsDBQkB4TOABgsJCAcD
198 AgQVAggDBBYCAw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428d/ZjkIc2ju8dRsn4=
216 -----END PGP PRIVATE KEY BLOCK-----
217 " | openpgp2ssh 4129E89D17C1D591 >/dev/null 2>/dev/null
221 # remove all lines with specified string from specified file
230 if [ -z "$file" -o -z "$string" ] ; then
234 if [ ! -e "$file" ] ; then
238 # if the string is in the file...
239 if grep -q -F "$string" "$file" 2> /dev/null ; then
240 tempfile=$(mktemp "${file}.XXXXXXX") || \
241 failure "Unable to make temp file '${file}.XXXXXXX'"
243 # remove the line with the string, and return 0
244 grep -v -F "$string" "$file" >"$tempfile"
245 cat "$tempfile" > "$file"
254 # remove all lines with MonkeySphere strings in file
255 remove_monkeysphere_lines() {
261 if [ -z "$file" ] ; then
265 if [ ! -e "$file" ] ; then
269 tempfile=$(mktemp "${file}.XXXXXXX") || \
270 failure "Could not make temporary file '${file}.XXXXXXX'."
272 egrep -v '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$' \
274 cat "$tempfile" > "$file"
278 # translate ssh-style path variables %h and %u
279 translate_ssh_variables() {
286 # get the user's home directory
287 userHome=$(getent passwd "$uname" | cut -d: -f6)
289 # translate '%u' to user name
290 path=${path/\%u/"$uname"}
291 # translate '%h' to user home directory
292 path=${path/\%h/"$userHome"}
297 # test that a string to conforms to GPG's expiration format
299 echo "$1" | egrep -q "^[0-9]+[mwy]?$"
302 # check that a file is properly owned, and that all it's parent
303 # directories are not group/other writable
304 check_key_file_permissions() {
311 # function to check that an octal corresponds to writability
313 [ "$1" -eq 2 -o "$1" -eq 3 -o "$1" -eq 6 -o "$1" -eq 7 ]
319 # return 0 is path does not exist
320 [ -e "$path" ] || return 0
322 owner=$(stat --format '%U' "$path")
323 access=$(stat --format '%a' "$path")
324 gAccess=$(echo "$access" | cut -c2)
325 oAccess=$(echo "$access" | cut -c3)
328 if [ "$owner" != "$user" -a "$owner" != 'root' ] ; then
332 # check group/other writability
333 if is_write "$gAccess" || is_write "$oAccess" ; then
337 if [ "$path" = '/' ] ; then
340 check_key_file_permissions $(dirname "$path")
344 ### CONVERSION UTILITIES
346 # output the ssh key for a given key ID
352 gpg --export "$keyID" | openpgp2ssh "$keyID" 2> /dev/null
355 # output known_hosts line from ssh key
364 echo -n "$key" | tr -d '\n'
365 echo " MonkeySphere${DATE}"
368 # output authorized_keys line from ssh key
369 ssh2authorized_keys() {
376 echo -n "$key" | tr -d '\n'
377 echo " MonkeySphere${DATE} ${userID}"
380 # convert key from gpg to ssh known_hosts format
388 # NOTE: it seems that ssh-keygen -R removes all comment fields from
389 # all lines in the known_hosts file. why?
390 # NOTE: just in case, the COMMENT can be matched with the
392 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
394 gpg2ssh "$keyID" | tr -d '\n'
395 echo " MonkeySphere${DATE}"
398 # convert key from gpg to ssh authorized_keys format
399 gpg2authorized_keys() {
406 # NOTE: just in case, the COMMENT can be matched with the
408 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
409 gpg2ssh "$keyID" | tr -d '\n'
410 echo " MonkeySphere${DATE} ${userID}"
415 # retrieve all keys with given user id from keyserver
416 # FIXME: need to figure out how to retrieve all matching keys
417 # (not just first N (5 in this case))
422 if [ "$CHECK_KEYSERVER" != 'true' ] ; then
428 log info " checking keyserver $KEYSERVER... "
430 gpg --quiet --batch --with-colons \
431 --command-fd 0 --keyserver "$KEYSERVER" \
432 --search ="$userID" > /dev/null 2>&1
435 # if the user is the monkeysphere user, then update the
436 # monkeysphere user's trustdb
437 if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
438 gpg_authentication "--check-trustdb" > /dev/null 2>&1
444 ########################################################################
445 ### PROCESSING FUNCTIONS
447 # userid and key policy checking
448 # the following checks policy on the returned keys
449 # - checks that full key has appropriate valididy (u|f)
450 # - checks key has specified capability (REQUIRED_*_KEY_CAPABILITY)
451 # - checks that requested user ID has appropriate validity
452 # (see /usr/share/doc/gnupg/DETAILS.gz)
453 # output is one line for every found key, in the following format:
457 # "flag" is an acceptability flag, 0 = ok, 1 = bad
458 # "sshKey" is the translated gpg key
460 # all log output must go to stderr, as stdout is used to pass the
461 # flag:sshKey to the calling function.
463 # expects global variable: "MODE"
466 local requiredCapability
467 local requiredPubCapability
482 # set the required key capability based on the mode
483 if [ "$MODE" = 'known_hosts' ] ; then
484 requiredCapability="$REQUIRED_HOST_KEY_CAPABILITY"
485 elif [ "$MODE" = 'authorized_keys' ] ; then
486 requiredCapability="$REQUIRED_USER_KEY_CAPABILITY"
488 requiredPubCapability=$(echo "$requiredCapability" | tr "[:lower:]" "[:upper:]")
490 # fetch the user ID if necessary/requested
491 gpg_fetch_userid "$userID"
493 # output gpg info for (exact) userid and store
494 gpgOut=$(gpg --list-key --fixed-list-mode --with-colon \
495 --with-fingerprint --with-fingerprint \
496 ="$userID" 2>/dev/null)
498 # if the gpg query return code is not 0, return 1
499 if [ "$?" -ne 0 ] ; then
500 log verbose " no primary keys found."
504 # loop over all lines in the gpg output and process.
505 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
506 while IFS=: read -r type validity keyid uidfpr usage ; do
507 # process based on record type
509 'pub') # primary keys
510 # new key, wipe the slate
517 log verbose " primary key found: $keyid"
519 # if overall key is not valid, skip
520 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
521 log error " - unacceptable primary key validity ($validity)."
524 # if overall key is disabled, skip
525 if check_capability "$usage" 'D' ; then
526 log error " - key disabled."
529 # if overall key capability is not ok, skip
530 if ! check_capability "$usage" $requiredPubCapability ; then
531 log error " - unacceptable primary key capability ($usage)."
535 # mark overall key as ok
538 # mark primary key as ok if capability is ok
539 if check_capability "$usage" $requiredCapability ; then
544 if [ "$lastKey" != pub ] ; then
545 log error " - got a user ID after a sub key?! user IDs should only follow primary keys!"
548 # if an acceptable user ID was already found, skip
549 if [ "$uidOK" = 'true' ] ; then
552 # if the user ID does matches...
553 if [ "$(echo "$uidfpr" | gpg_unescape)" = "$userID" ] ; then
554 # and the user ID validity is ok
555 if [ "$validity" = 'u' -o "$validity" = 'f' ] ; then
556 # mark user ID acceptable
563 # output a line for the primary key
565 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
566 log verbose " * acceptable primary key."
567 if [ -z "$sshKey" ] ; then
568 log error " ! primary key could not be translated (not RSA or DSA?)."
573 log error " - unacceptable primary key."
574 if [ -z "$sshKey" ] ; then
575 log error " ! primary key could not be translated (not RSA or DSA?)."
582 # unset acceptability of last key
587 # don't bother with sub keys if the primary key is not valid
588 if [ "$keyOK" != true ] ; then
592 # don't bother with sub keys if no user ID is acceptable:
593 if [ "$uidOK" != true ] ; then
597 # if sub key validity is not ok, skip
598 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
601 # if sub key capability is not ok, skip
602 if ! check_capability "$usage" $requiredCapability ; then
609 'fpr') # key fingerprint
610 fingerprint="$uidfpr"
612 sshKey=$(gpg2ssh "$fingerprint")
614 # if the last key was the pub key, skip
615 if [ "$lastKey" = pub ] ; then
619 # output a line for the sub key
621 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
622 log verbose " * acceptable sub key."
623 if [ -z "$sshKey" ] ; then
624 log error " ! sub key could not be translated (not RSA or DSA?)."
629 log error " - unacceptable sub key."
630 if [ -z "$sshKey" ] ; then
631 log error " ! sub key could not be translated (not RSA or DSA?)."
638 done | sort -t: -k1 -n -r
639 # NOTE: this last sort is important so that the "good" keys (key
640 # flag '0') come last. This is so that they take precedence when
641 # being processed in the key files over "bad" keys (key flag '1')
644 # process a single host in the known_host file
645 process_host_known_hosts() {
655 userID="ssh://${host}"
657 log verbose "processing: $host"
663 for line in $(process_user_id "${userID}") ; do
664 # note that key was found
667 ok=$(echo "$line" | cut -d: -f1)
668 sshKey=$(echo "$line" | cut -d: -f2)
670 if [ -z "$sshKey" ] ; then
674 # remove the old host key line, and note if removed
675 remove_line "$KNOWN_HOSTS" "$sshKey"
677 # if key OK, add new host line
678 if [ "$ok" -eq '0' ] ; then
679 # note that key was found ok
680 nKeysOK=$((nKeysOK+1))
683 if [ "$HASH_KNOWN_HOSTS" = 'true' ] ; then
684 # FIXME: this is really hackish cause ssh-keygen won't
685 # hash from stdin to stdout
687 ssh2known_hosts "$host" "$sshKey" > "$tmpfile"
688 ssh-keygen -H -f "$tmpfile" 2> /dev/null
689 cat "$tmpfile" >> "$KNOWN_HOSTS"
690 rm -f "$tmpfile" "${tmpfile}.old"
692 ssh2known_hosts "$host" "$sshKey" >> "$KNOWN_HOSTS"
697 # if at least one key was found...
698 if [ "$nKeys" -gt 0 ] ; then
699 # if ok keys were found, return 0
700 if [ "$nKeysOK" -gt 0 ] ; then
706 # if no keys were found, return 1
712 # update the known_hosts file for a set of hosts listed on command
714 update_known_hosts() {
721 # the number of hosts specified on command line
727 # set the trap to remove any lockfiles on exit
728 trap "lockfile-remove $KNOWN_HOSTS" EXIT
730 # create a lockfile on known_hosts
731 lockfile-create "$KNOWN_HOSTS"
733 # note pre update file checksum
734 fileCheck="$(file_hash "$KNOWN_HOSTS")"
738 process_host_known_hosts "$host"
742 nHostsOK=$((nHostsOK+1))
745 nHostsBAD=$((nHostsBAD+1))
749 # touch the lockfile, for good measure.
750 lockfile-touch --oneshot "$KNOWN_HOSTS"
753 # remove the lockfile
754 lockfile-remove "$KNOWN_HOSTS"
756 # note if the known_hosts file was updated
757 if [ "$(file_hash "$KNOWN_HOSTS")" != "$fileCheck" ] ; then
758 log verbose "known_hosts file updated."
761 # if an acceptable host was found, return 0
762 if [ "$nHostsOK" -gt 0 ] ; then
764 # else if no ok hosts were found...
766 # if no bad host were found then no hosts were found at all,
768 if [ "$nHostsBAD" -eq 0 ] ; then
770 # else if at least one bad host was found, return 2
777 # process hosts from a known_hosts file
778 process_known_hosts() {
781 log verbose "processing known_hosts file..."
783 hosts=$(meat "$KNOWN_HOSTS" | cut -d ' ' -f 1 | grep -v '^|.*$' | tr , ' ' | tr '\n' ' ')
785 if [ -z "$hosts" ] ; then
786 log error "no hosts to process."
790 # take all the hosts from the known_hosts file (first
791 # field), grep out all the hashed hosts (lines starting
793 update_known_hosts $hosts
796 # process uids for the authorized_keys file
797 process_uid_authorized_keys() {
806 log verbose "processing: $userID"
812 for line in $(process_user_id "$userID") ; do
813 # note that key was found
816 ok=$(echo "$line" | cut -d: -f1)
817 sshKey=$(echo "$line" | cut -d: -f2)
819 if [ -z "$sshKey" ] ; then
823 # remove the old host key line
824 remove_line "$AUTHORIZED_KEYS" "$sshKey"
826 # if key OK, add new host line
827 if [ "$ok" -eq '0' ] ; then
828 # note that key was found ok
829 nKeysOK=$((nKeysOK+1))
831 ssh2authorized_keys "$userID" "$sshKey" >> "$AUTHORIZED_KEYS"
835 # if at least one key was found...
836 if [ "$nKeys" -gt 0 ] ; then
837 # if ok keys were found, return 0
838 if [ "$nKeysOK" -gt 0 ] ; then
844 # if no keys were found, return 1
850 # update the authorized_keys files from a list of user IDs on command
852 update_authorized_keys() {
859 # the number of ids specified on command line
865 # set the trap to remove any lockfiles on exit
866 trap "lockfile-remove $AUTHORIZED_KEYS" EXIT
868 # create a lockfile on authorized_keys
869 lockfile-create "$AUTHORIZED_KEYS"
871 # note pre update file checksum
872 fileCheck="$(file_hash "$AUTHORIZED_KEYS")"
874 # remove any monkeysphere lines from authorized_keys file
875 remove_monkeysphere_lines "$AUTHORIZED_KEYS"
878 # process the user ID, change return code if key not found for
880 process_uid_authorized_keys "$userID"
888 nIDsBAD=$((nIDsBAD+1))
892 # touch the lockfile, for good measure.
893 lockfile-touch --oneshot "$AUTHORIZED_KEYS"
896 # remove the lockfile
897 lockfile-remove "$AUTHORIZED_KEYS"
899 # note if the authorized_keys file was updated
900 if [ "$(file_hash "$AUTHORIZED_KEYS")" != "$fileCheck" ] ; then
901 log verbose "authorized_keys file updated."
904 # if an acceptable id was found, return 0
905 if [ "$nIDsOK" -gt 0 ] ; then
907 # else if no ok ids were found...
909 # if no bad ids were found then no ids were found at all, and
911 if [ "$nIDsBAD" -eq 0 ] ; then
913 # else if at least one bad id was found, return 2
920 # process an authorized_user_ids file for authorized_keys
921 process_authorized_user_ids() {
926 authorizedUserIDs="$1"
928 log verbose "processing authorized_user_ids file..."
930 if ! meat "$authorizedUserIDs" > /dev/null ; then
931 log error "no user IDs to process."
937 # extract user IDs from authorized_user_ids file
939 for line in $(meat "$authorizedUserIDs") ; do
940 userIDs["$nline"]="$line"
944 update_authorized_keys "${userIDs[@]}"