3 # Shared sh functions for the monkeysphere
6 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # Copyright 2008, released under the GPL, version 3 or later
10 # all-caps variables are meant to be user supplied (ie. from config
11 # file) and are considered global
13 ########################################################################
17 ETC="/etc/monkeysphere"
20 ########################################################################
23 # failure function. exits with code 255, unless specified otherwise.
29 # write output to stderr
39 # cut out all comments(#) and blank lines from standard input
41 grep -v -e "^[[:space:]]*#" -e '^$' "$1"
44 # cut a specified line from standard input
46 head --line="$1" "$2" | tail -1
49 # check that characters are in a string (in an AND fashion).
50 # used for checking key capability
51 # check_capability capability a [b...]
60 if echo "$usage" | grep -q -v "$capcheck" ; then
69 md5sum "$1" 2> /dev/null
72 # convert escaped characters in pipeline from gpg output back into
74 # FIXME: undo all escape character translation in with-colons gpg
80 # convert nasty chars into gpg-friendly form in pipeline
81 # FIXME: escape everything, not just colons!
86 # prompt for GPG-formatted expiration, and emit result on stdout
87 get_gpg_expiration() {
91 Please specify how long the key should be valid.
92 0 = key does not expire
93 <n> = key expires in n days
94 <n>w = key expires in n weeks
95 <n>m = key expires in n months
96 <n>y = key expires in n years
98 while [ -z "$keyExpire" ] ; do
99 read -p "Key is valid for? (0) " keyExpire
100 if ! test_gpg_expire ${keyExpire:=0} ; then
101 echo "invalid value" >&2
108 passphrase_prompt() {
113 if [ "$DISPLAY" ] && which "${SSH_ASKPASS:-ssh-askpass}" >/dev/null; then
114 "${SSH_ASKPASS:-ssh-askpass}" "$prompt" > "$fifo"
116 read -s -p "$prompt" PASS
117 # Uses the builtin echo, so should not put the passphrase into
118 # the process table. I think. --dkg
119 echo "$PASS" > "$fifo"
123 test_gnu_dummy_s2k_extension() {
125 # this block contains a demonstration private key that has had the
126 # primary key stripped out using the GNU S2K extension known as
127 # "gnu-dummy" (see /usr/share/doc/gnupg/DETAILS.gz). The subkey is
128 # present in cleartext, however.
130 # openpgp2ssh will be able to deal with this based on whether the
131 # local copy of GnuTLS contains read_s2k support that can handle it.
133 # read up on that here:
135 # http://lists.gnu.org/archive/html/gnutls-devel/2008-08/msg00005.html
138 -----BEGIN PGP PRIVATE KEY BLOCK-----
139 Version: GnuPG v1.4.9 (GNU/Linux)
141 lQCVBEO3YdABBACRqqEnucag4+vyZny2M67Pai5+5suIRRvY+Ly8Ms5MvgCi3EVV
142 xT05O/+0ShiRaf+QicCOFrhbU9PZzzU+seEvkeW2UCu4dQfILkmj+HBEIltGnHr3
143 G0yegHj5pnqrcezERURf2e17gGFWX91cXB9Cm721FPXczuKraphKwCA9PwARAQAB
144 /gNlAkdOVQG0OURlbW9uc3RyYXRpb24gS2V5IGZvciBTMksgR05VIGV4dGVuc2lv
145 biAxMDAxIC0tIGdudS1kdW1teYi8BBMBAgAmBQJDt2HQAhsDBQkB4TOABgsJCAcD
146 AgQVAggDBBYCAwECHgECF4AACgkQQZUwSa4UDezTOQP/TMQXUVrWzHYZGopoPZ2+
147 ZS3qddiznBHsgb7MGYg1KlTiVJSroDUBCHIUJvdQKZV9zrzrFl47D07x6hGyUPHV
148 aZXvuITW8t1o5MMHkCy3pmJ2KgfDvdUxrBvLfgPMICA4c6zA0mWquee43syEW9NY
149 g3q61iPlQwD1J1kX1wlimLCdAdgEQ7dh0AEEANAwa63zlQbuy1Meliy8otwiOa+a
150 mH6pxxUgUNggjyjO5qx+rl25mMjvGIRX4/L1QwIBXJBVi3SgvJW1COZxZqBYqj9U
151 8HVT07mWKFEDf0rZLeUE2jTm16cF9fcW4DQhW+sfYm+hi2sY3HeMuwlUBK9KHfW2
152 +bGeDzVZ4pqfUEudABEBAAEAA/0bemib+wxub9IyVFUp7nPobjQC83qxLSNzrGI/
153 RHzgu/5CQi4tfLOnwbcQsLELfker2hYnjsLrT9PURqK4F7udrWEoZ1I1LymOtLG/
154 4tNZ7Mnul3wRC2tCn7FKx8sGJwGh/3li8vZ6ALVJAyOia5TZ/buX0+QZzt6+hPKk
155 7MU1WQIA4bUBjtrsqDwro94DvPj3/jBnMZbXr6WZIItLNeVDUcM8oHL807Am97K1
156 ueO/f6v1sGAHG6lVPTmtekqPSTWBfwIA7CGFvEyvSALfB8NUa6jtk27NCiw0csql
157 kuhCmwXGMVOiryKEfegkIahf2bAd/gnWHPrpWp7bUE20v8YoW22I4wIAhnm5Wr5Q
158 Sy7EHDUxmJm5TzadFp9gq08qNzHBpXSYXXJ3JuWcL1/awUqp3tE1I6zZ0hZ38Ia6
159 SdBMN88idnhDPqPoiKUEGAECAA8FAkO3YdACGyAFCQHhM4AACgkQQZUwSa4UDezm
160 vQP/ZhK+2ly9oI2z7ZcNC/BJRch0/ybQ3haahII8pXXmOThpZohr/LUgoWgCZdXg
161 vP6yiszNk2tIs8KphCAw7Lw/qzDC2hEORjWO4f46qk73RAgSqG/GyzI4ltWiDhqn
162 vnQCFl3+QFSe4zinqykHnLwGPMXv428d/ZjkIc2ju8dRsn4=
164 -----END PGP PRIVATE KEY BLOCK-----
165 " | openpgp2ssh 4129E89D17C1D591 >/dev/null 2>/dev/null
169 # remove all lines with specified string from specified file
177 if [ -z "$file" -o -z "$string" ] ; then
181 if [ ! -e "$file" ] ; then
185 # if the string is in the file...
186 if grep -q -F "$string" "$file" 2> /dev/null ; then
187 # remove the line with the string, and return 0
188 grep -v -F "$string" "$file" | sponge "$file"
196 # remove all lines with MonkeySphere strings in file
197 remove_monkeysphere_lines() {
202 if [ -z "$file" ] ; then
206 if [ ! -e "$file" ] ; then
210 egrep -v '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$' \
211 "$file" | sponge "$file"
214 # translate ssh-style path variables %h and %u
215 translate_ssh_variables() {
222 # get the user's home directory
223 userHome=$(getent passwd "$uname" | cut -d: -f6)
225 # translate '%u' to user name
226 path=${path/\%u/"$uname"}
227 # translate '%h' to user home directory
228 path=${path/\%h/"$userHome"}
233 # test that a string to conforms to GPG's expiration format
235 echo "$1" | egrep -q "^[0-9]+[mwy]?$"
238 # check that a file is properly owned, and that all it's parent
239 # directories are not group/other writable
240 check_key_file_permissions() {
247 # function to check that an octal corresponds to writability
249 [ "$1" -eq 2 -o "$1" -eq 3 -o "$1" -eq 6 -o "$1" -eq 7 ]
255 # return 0 is path does not exist
256 [ -e "$path" ] || return 0
258 owner=$(stat --format '%U' "$path")
259 access=$(stat --format '%a' "$path")
260 gAccess=$(echo "$access" | cut -c2)
261 oAccess=$(echo "$access" | cut -c3)
264 if [ "$owner" != "$user" -a "$owner" != 'root' ] ; then
268 # check group/other writability
269 if is_write "$gAccess" || is_write "$oAccess" ; then
273 if [ "$path" = '/' ] ; then
276 check_key_file_permissions $(dirname "$path")
280 ### CONVERSION UTILITIES
282 # output the ssh key for a given key ID
288 gpg --export "$keyID" | openpgp2ssh "$keyID" 2> /dev/null
291 # output known_hosts line from ssh key
300 echo -n "$key" | tr -d '\n'
301 echo " MonkeySphere${DATE}"
304 # output authorized_keys line from ssh key
305 ssh2authorized_keys() {
312 echo -n "$key" | tr -d '\n'
313 echo " MonkeySphere${DATE} ${userID}"
316 # convert key from gpg to ssh known_hosts format
324 # NOTE: it seems that ssh-keygen -R removes all comment fields from
325 # all lines in the known_hosts file. why?
326 # NOTE: just in case, the COMMENT can be matched with the
328 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
330 gpg2ssh "$keyID" | tr -d '\n'
331 echo " MonkeySphere${DATE}"
334 # convert key from gpg to ssh authorized_keys format
335 gpg2authorized_keys() {
342 # NOTE: just in case, the COMMENT can be matched with the
344 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
345 gpg2ssh "$keyID" | tr -d '\n'
346 echo " MonkeySphere${DATE} ${userID}"
351 # retrieve all keys with given user id from keyserver
352 # FIXME: need to figure out how to retrieve all matching keys
353 # (not just first N (5 in this case))
358 if [ "$CHECK_KEYSERVER" != 'true' ] ; then
364 log -n " checking keyserver $KEYSERVER... "
366 gpg --quiet --batch --with-colons \
367 --command-fd 0 --keyserver "$KEYSERVER" \
368 --search ="$userID" > /dev/null 2>&1
372 # if the user is the monkeysphere user, then update the
373 # monkeysphere user's trustdb
374 if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
375 gpg_authentication "--check-trustdb" > /dev/null 2>&1
381 ########################################################################
382 ### PROCESSING FUNCTIONS
384 # userid and key policy checking
385 # the following checks policy on the returned keys
386 # - checks that full key has appropriate valididy (u|f)
387 # - checks key has specified capability (REQUIRED_*_KEY_CAPABILITY)
388 # - checks that requested user ID has appropriate validity
389 # (see /usr/share/doc/gnupg/DETAILS.gz)
390 # output is one line for every found key, in the following format:
394 # "flag" is an acceptability flag, 0 = ok, 1 = bad
395 # "fingerprint" is the fingerprint of the key
397 # expects global variable: "MODE"
400 local requiredCapability
401 local requiredPubCapability
416 # set the required key capability based on the mode
417 if [ "$MODE" = 'known_hosts' ] ; then
418 requiredCapability="$REQUIRED_HOST_KEY_CAPABILITY"
419 elif [ "$MODE" = 'authorized_keys' ] ; then
420 requiredCapability="$REQUIRED_USER_KEY_CAPABILITY"
422 requiredPubCapability=$(echo "$requiredCapability" | tr "[:lower:]" "[:upper:]")
424 # fetch the user ID if necessary/requested
425 gpg_fetch_userid "$userID"
427 # output gpg info for (exact) userid and store
428 gpgOut=$(gpg --list-key --fixed-list-mode --with-colon \
429 --with-fingerprint --with-fingerprint \
430 ="$userID" 2>/dev/null)
432 # if the gpg query return code is not 0, return 1
433 if [ "$?" -ne 0 ] ; then
434 log " no primary keys found."
438 # loop over all lines in the gpg output and process.
439 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
440 while IFS=: read -r type validity keyid uidfpr usage ; do
441 # process based on record type
443 'pub') # primary keys
444 # new key, wipe the slate
451 log " primary key found: $keyid"
453 # if overall key is not valid, skip
454 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
455 log " - unacceptable primary key validity ($validity)."
458 # if overall key is disabled, skip
459 if check_capability "$usage" 'D' ; then
460 log " - key disabled."
463 # if overall key capability is not ok, skip
464 if ! check_capability "$usage" $requiredPubCapability ; then
465 log " - unacceptable primary key capability ($usage)."
469 # mark overall key as ok
472 # mark primary key as ok if capability is ok
473 if check_capability "$usage" $requiredCapability ; then
478 if [ "$lastKey" != pub ] ; then
479 log " - got a user ID after a sub key?! user IDs should only follow primary keys!"
482 # if an acceptable user ID was already found, skip
483 if [ "$uidOK" = 'true' ] ; then
486 # if the user ID does matches...
487 if [ "$(echo "$uidfpr" | gpg_unescape)" = "$userID" ] ; then
488 # and the user ID validity is ok
489 if [ "$validity" = 'u' -o "$validity" = 'f' ] ; then
490 # mark user ID acceptable
497 # output a line for the primary key
499 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
500 log " * acceptable primary key."
501 if [ -z "$sshKey" ] ; then
502 log " ! primary key could not be translated (not RSA or DSA?)."
507 log " - unacceptable primary key."
508 if [ -z "$sshKey" ] ; then
509 log " ! primary key could not be translated (not RSA or DSA?)."
516 # unset acceptability of last key
521 # don't bother with sub keys if the primary key is not valid
522 if [ "$keyOK" != true ] ; then
526 # don't bother with sub keys if no user ID is acceptable:
527 if [ "$uidOK" != true ] ; then
531 # if sub key validity is not ok, skip
532 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
535 # if sub key capability is not ok, skip
536 if ! check_capability "$usage" $requiredCapability ; then
543 'fpr') # key fingerprint
544 fingerprint="$uidfpr"
546 sshKey=$(gpg2ssh "$fingerprint")
548 # if the last key was the pub key, skip
549 if [ "$lastKey" = pub ] ; then
553 # output a line for the sub key
555 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
556 log " * acceptable sub key."
557 if [ -z "$sshKey" ] ; then
558 log " ! sub key could not be translated (not RSA or DSA?)."
563 log " - unacceptable sub key."
564 if [ -z "$sshKey" ] ; then
565 log " ! sub key could not be translated (not RSA or DSA?)."
572 done | sort -t: -k1 -n -r
573 # NOTE: this last sort is important so that the "good" keys (key
574 # flag '0') come last. This is so that they take precedence when
575 # being processed in the key files over "bad" keys (key flag '1')
578 # process a single host in the known_host file
579 process_host_known_hosts() {
589 userID="ssh://${host}"
591 log "processing: $host"
597 for line in $(process_user_id "${userID}") ; do
598 # note that key was found
601 ok=$(echo "$line" | cut -d: -f1)
602 sshKey=$(echo "$line" | cut -d: -f2)
604 if [ -z "$sshKey" ] ; then
608 # remove the old host key line, and note if removed
609 remove_line "$KNOWN_HOSTS" "$sshKey"
611 # if key OK, add new host line
612 if [ "$ok" -eq '0' ] ; then
613 # note that key was found ok
614 nKeysOK=$((nKeysOK+1))
617 if [ "$HASH_KNOWN_HOSTS" = 'true' ] ; then
618 # FIXME: this is really hackish cause ssh-keygen won't
619 # hash from stdin to stdout
621 ssh2known_hosts "$host" "$sshKey" > "$tmpfile"
622 ssh-keygen -H -f "$tmpfile" 2> /dev/null
623 cat "$tmpfile" >> "$KNOWN_HOSTS"
624 rm -f "$tmpfile" "${tmpfile}.old"
626 ssh2known_hosts "$host" "$sshKey" >> "$KNOWN_HOSTS"
631 # if at least one key was found...
632 if [ "$nKeys" -gt 0 ] ; then
633 # if ok keys were found, return 0
634 if [ "$nKeysOK" -gt 0 ] ; then
640 # if no keys were found, return 1
646 # update the known_hosts file for a set of hosts listed on command
648 update_known_hosts() {
655 # the number of hosts specified on command line
661 # set the trap to remove any lockfiles on exit
662 trap "lockfile-remove $KNOWN_HOSTS" EXIT
664 # create a lockfile on known_hosts
665 lockfile-create "$KNOWN_HOSTS"
667 # note pre update file checksum
668 fileCheck="$(file_hash "$KNOWN_HOSTS")"
672 process_host_known_hosts "$host"
676 nHostsOK=$((nHostsOK+1))
679 nHostsBAD=$((nHostsBAD+1))
683 # touch the lockfile, for good measure.
684 lockfile-touch --oneshot "$KNOWN_HOSTS"
687 # remove the lockfile
688 lockfile-remove "$KNOWN_HOSTS"
690 # note if the known_hosts file was updated
691 if [ "$(file_hash "$KNOWN_HOSTS")" != "$fileCheck" ] ; then
692 log "known_hosts file updated."
695 # if an acceptable host was found, return 0
696 if [ "$nHostsOK" -gt 0 ] ; then
698 # else if no ok hosts were found...
700 # if no bad host were found then no hosts were found at all,
702 if [ "$nHostsBAD" -eq 0 ] ; then
704 # else if at least one bad host was found, return 2
711 # process hosts from a known_hosts file
712 process_known_hosts() {
715 log "processing known_hosts file..."
717 hosts=$(meat "$KNOWN_HOSTS" | cut -d ' ' -f 1 | grep -v '^|.*$' | tr , ' ' | tr '\n' ' ')
719 if [ -z "$hosts" ] ; then
720 log "no hosts to process."
724 # take all the hosts from the known_hosts file (first
725 # field), grep out all the hashed hosts (lines starting
727 update_known_hosts $hosts
730 # process uids for the authorized_keys file
731 process_uid_authorized_keys() {
740 log "processing: $userID"
746 for line in $(process_user_id "$userID") ; do
747 # note that key was found
750 ok=$(echo "$line" | cut -d: -f1)
751 sshKey=$(echo "$line" | cut -d: -f2)
753 if [ -z "$sshKey" ] ; then
757 # remove the old host key line
758 remove_line "$AUTHORIZED_KEYS" "$sshKey"
760 # if key OK, add new host line
761 if [ "$ok" -eq '0' ] ; then
762 # note that key was found ok
763 nKeysOK=$((nKeysOK+1))
765 ssh2authorized_keys "$userID" "$sshKey" >> "$AUTHORIZED_KEYS"
769 # if at least one key was found...
770 if [ "$nKeys" -gt 0 ] ; then
771 # if ok keys were found, return 0
772 if [ "$nKeysOK" -gt 0 ] ; then
778 # if no keys were found, return 1
784 # update the authorized_keys files from a list of user IDs on command
786 update_authorized_keys() {
793 # the number of ids specified on command line
799 # set the trap to remove any lockfiles on exit
800 trap "lockfile-remove $AUTHORIZED_KEYS" EXIT
802 # create a lockfile on authorized_keys
803 lockfile-create "$AUTHORIZED_KEYS"
805 # note pre update file checksum
806 fileCheck="$(file_hash "$AUTHORIZED_KEYS")"
808 # remove any monkeysphere lines from authorized_keys file
809 remove_monkeysphere_lines "$AUTHORIZED_KEYS"
812 # process the user ID, change return code if key not found for
814 process_uid_authorized_keys "$userID"
822 nIDsBAD=$((nIDsBAD+1))
826 # touch the lockfile, for good measure.
827 lockfile-touch --oneshot "$AUTHORIZED_KEYS"
830 # remove the lockfile
831 lockfile-remove "$AUTHORIZED_KEYS"
833 # note if the authorized_keys file was updated
834 if [ "$(file_hash "$AUTHORIZED_KEYS")" != "$fileCheck" ] ; then
835 log "authorized_keys file updated."
838 # if an acceptable id was found, return 0
839 if [ "$nIDsOK" -gt 0 ] ; then
841 # else if no ok ids were found...
843 # if no bad ids were found then no ids were found at all, and
845 if [ "$nIDsBAD" -eq 0 ] ; then
847 # else if at least one bad id was found, return 2
854 # process an authorized_user_ids file for authorized_keys
855 process_authorized_user_ids() {
860 authorizedUserIDs="$1"
862 log "processing authorized_user_ids file..."
864 if ! meat "$authorizedUserIDs" > /dev/null ; then
865 log "no user IDs to process."
871 # extract user IDs from authorized_user_ids file
873 for line in $(meat "$authorizedUserIDs") ; do
874 userIDs["$nline"]="$line"
878 update_authorized_keys "${userIDs[@]}"