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 based on specified LOG_LEVEL the first
30 # parameter is the priority of the output, and everything else is what
36 # don't include SILENT in alllevels: it's handled separately
37 # list in decreasing verbosity (all caps)
38 local alllevels="DEBUG INFO ERROR"
41 # translate lowers to uppers in global log level
42 LOG_LEVEL=$(echo "$LOG_LEVEL" | tr "[:lower:]" "[:upper:]")
44 # just go ahead and return if the log level is silent
45 if [ "$LOG_LEVEL" = 'SILENT' ] ; then
49 for level in $alllevels; do
50 if [ "$LOG_LEVEL" = "$level" ] ; then
54 if [ -z "$found" ] ; then
59 # get priority from first parameter, translating all lower to
61 priority=$(echo "$1" | tr "[:lower:]" "[:upper:]")
64 # scan over available levels
65 for level in $alllevels ; do
66 # output if the log level matches, set output to true
67 # this will output for all subsequent loops as well.
68 if [ "$LOG_LEVEL" = "$level" ] ; then
71 if [ "$priority" = "$level" -a "$output" = 'true' ] ; then
78 # cut out all comments(#) and blank lines from standard input
80 grep -v -e "^[[:space:]]*#" -e '^$' "$1"
83 # cut a specified line from standard input
85 head --line="$1" "$2" | tail -1
88 # check that characters are in a string (in an AND fashion).
89 # used for checking key capability
90 # check_capability capability a [b...]
99 if echo "$usage" | grep -q -v "$capcheck" ; then
108 md5sum "$1" 2> /dev/null
111 # convert escaped characters in pipeline from gpg output back into
113 # FIXME: undo all escape character translation in with-colons gpg
119 # convert nasty chars into gpg-friendly form in pipeline
120 # FIXME: escape everything, not just colons!
125 # prompt for GPG-formatted expiration, and emit result on stdout
126 get_gpg_expiration() {
131 if [ -z "$keyExpire" ]; then
133 Please specify how long the key should be valid.
134 0 = key does not expire
135 <n> = key expires in n days
136 <n>w = key expires in n weeks
137 <n>m = key expires in n months
138 <n>y = key expires in n years
140 while [ -z "$keyExpire" ] ; do
141 read -p "Key is valid for? (0) " keyExpire
142 if ! test_gpg_expire ${keyExpire:=0} ; then
143 echo "invalid value" >&2
147 elif ! test_gpg_expire "$keyExpire" ; then
148 failure "invalid key expiration value '$keyExpire'."
154 passphrase_prompt() {
159 if [ "$DISPLAY" ] && which "${SSH_ASKPASS:-ssh-askpass}" >/dev/null; then
160 "${SSH_ASKPASS:-ssh-askpass}" "$prompt" > "$fifo"
162 read -s -p "$prompt" PASS
163 # Uses the builtin echo, so should not put the passphrase into
164 # the process table. I think. --dkg
165 echo "$PASS" > "$fifo"
169 test_gnu_dummy_s2k_extension() {
171 # this block contains a demonstration private key that has had the
172 # primary key stripped out using the GNU S2K extension known as
173 # "gnu-dummy" (see /usr/share/doc/gnupg/DETAILS.gz). The subkey is
174 # present in cleartext, however.
176 # openpgp2ssh will be able to deal with this based on whether the
177 # local copy of GnuTLS contains read_s2k support that can handle it.
179 # read up on that here:
181 # http://lists.gnu.org/archive/html/gnutls-devel/2008-08/msg00005.html
184 -----BEGIN PGP PRIVATE KEY BLOCK-----
185 Version: GnuPG v1.4.9 (GNU/Linux)
187 lQCVBEO3YdABBACRqqEnucag4+vyZny2M67Pai5+5suIRRvY+Ly8Ms5MvgCi3EVV
188 xT05O/+0ShiRaf+QicCOFrhbU9PZzzU+seEvkeW2UCu4dQfILkmj+HBEIltGnHr3
189 G0yegHj5pnqrcezERURf2e17gGFWX91cXB9Cm721FPXczuKraphKwCA9PwARAQAB
190 /gNlAkdOVQG0OURlbW9uc3RyYXRpb24gS2V5IGZvciBTMksgR05VIGV4dGVuc2lv
191 biAxMDAxIC0tIGdudS1kdW1teYi8BBMBAgAmBQJDt2HQAhsDBQkB4TOABgsJCAcD
192 AgQVAggDBBYCAwECHgECF4AACgkQQZUwSa4UDezTOQP/TMQXUVrWzHYZGopoPZ2+
193 ZS3qddiznBHsgb7MGYg1KlTiVJSroDUBCHIUJvdQKZV9zrzrFl47D07x6hGyUPHV
194 aZXvuITW8t1o5MMHkCy3pmJ2KgfDvdUxrBvLfgPMICA4c6zA0mWquee43syEW9NY
195 g3q61iPlQwD1J1kX1wlimLCdAdgEQ7dh0AEEANAwa63zlQbuy1Meliy8otwiOa+a
196 mH6pxxUgUNggjyjO5qx+rl25mMjvGIRX4/L1QwIBXJBVi3SgvJW1COZxZqBYqj9U
197 8HVT07mWKFEDf0rZLeUE2jTm16cF9fcW4DQhW+sfYm+hi2sY3HeMuwlUBK9KHfW2
198 +bGeDzVZ4pqfUEudABEBAAEAA/0bemib+wxub9IyVFUp7nPobjQC83qxLSNzrGI/
199 RHzgu/5CQi4tfLOnwbcQsLELfker2hYnjsLrT9PURqK4F7udrWEoZ1I1LymOtLG/
200 4tNZ7Mnul3wRC2tCn7FKx8sGJwGh/3li8vZ6ALVJAyOia5TZ/buX0+QZzt6+hPKk
201 7MU1WQIA4bUBjtrsqDwro94DvPj3/jBnMZbXr6WZIItLNeVDUcM8oHL807Am97K1
202 ueO/f6v1sGAHG6lVPTmtekqPSTWBfwIA7CGFvEyvSALfB8NUa6jtk27NCiw0csql
203 kuhCmwXGMVOiryKEfegkIahf2bAd/gnWHPrpWp7bUE20v8YoW22I4wIAhnm5Wr5Q
204 Sy7EHDUxmJm5TzadFp9gq08qNzHBpXSYXXJ3JuWcL1/awUqp3tE1I6zZ0hZ38Ia6
205 SdBMN88idnhDPqPoiKUEGAECAA8FAkO3YdACGyAFCQHhM4AACgkQQZUwSa4UDezm
206 vQP/ZhK+2ly9oI2z7ZcNC/BJRch0/ybQ3haahII8pXXmOThpZohr/LUgoWgCZdXg
207 vP6yiszNk2tIs8KphCAw7Lw/qzDC2hEORjWO4f46qk73RAgSqG/GyzI4ltWiDhqn
208 vnQCFl3+QFSe4zinqykHnLwGPMXv428d/ZjkIc2ju8dRsn4=
210 -----END PGP PRIVATE KEY BLOCK-----
211 " | openpgp2ssh 4129E89D17C1D591 >/dev/null 2>/dev/null
215 # remove all lines with specified string from specified file
223 if [ -z "$file" -o -z "$string" ] ; then
227 if [ ! -e "$file" ] ; then
231 # if the string is in the file...
232 if grep -q -F "$string" "$file" 2> /dev/null ; then
233 # remove the line with the string, and return 0
234 grep -v -F "$string" "$file" | sponge "$file"
242 # remove all lines with MonkeySphere strings in file
243 remove_monkeysphere_lines() {
248 if [ -z "$file" ] ; then
252 if [ ! -e "$file" ] ; then
256 egrep -v '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$' \
257 "$file" | sponge "$file"
260 # translate ssh-style path variables %h and %u
261 translate_ssh_variables() {
268 # get the user's home directory
269 userHome=$(getent passwd "$uname" | cut -d: -f6)
271 # translate '%u' to user name
272 path=${path/\%u/"$uname"}
273 # translate '%h' to user home directory
274 path=${path/\%h/"$userHome"}
279 # test that a string to conforms to GPG's expiration format
281 echo "$1" | egrep -q "^[0-9]+[mwy]?$"
284 # check that a file is properly owned, and that all it's parent
285 # directories are not group/other writable
286 check_key_file_permissions() {
293 # function to check that an octal corresponds to writability
295 [ "$1" -eq 2 -o "$1" -eq 3 -o "$1" -eq 6 -o "$1" -eq 7 ]
301 # return 0 is path does not exist
302 [ -e "$path" ] || return 0
304 owner=$(stat --format '%U' "$path")
305 access=$(stat --format '%a' "$path")
306 gAccess=$(echo "$access" | cut -c2)
307 oAccess=$(echo "$access" | cut -c3)
310 if [ "$owner" != "$user" -a "$owner" != 'root' ] ; then
314 # check group/other writability
315 if is_write "$gAccess" || is_write "$oAccess" ; then
319 if [ "$path" = '/' ] ; then
322 check_key_file_permissions $(dirname "$path")
326 ### CONVERSION UTILITIES
328 # output the ssh key for a given key ID
334 gpg --export "$keyID" | openpgp2ssh "$keyID" 2> /dev/null
337 # output known_hosts line from ssh key
346 echo -n "$key" | tr -d '\n'
347 echo " MonkeySphere${DATE}"
350 # output authorized_keys line from ssh key
351 ssh2authorized_keys() {
358 echo -n "$key" | tr -d '\n'
359 echo " MonkeySphere${DATE} ${userID}"
362 # convert key from gpg to ssh known_hosts format
370 # NOTE: it seems that ssh-keygen -R removes all comment fields from
371 # all lines in the known_hosts file. why?
372 # NOTE: just in case, the COMMENT can be matched with the
374 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
376 gpg2ssh "$keyID" | tr -d '\n'
377 echo " MonkeySphere${DATE}"
380 # convert key from gpg to ssh authorized_keys format
381 gpg2authorized_keys() {
388 # NOTE: just in case, the COMMENT can be matched with the
390 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
391 gpg2ssh "$keyID" | tr -d '\n'
392 echo " MonkeySphere${DATE} ${userID}"
397 # retrieve all keys with given user id from keyserver
398 # FIXME: need to figure out how to retrieve all matching keys
399 # (not just first N (5 in this case))
404 if [ "$CHECK_KEYSERVER" != 'true' ] ; then
410 log info " checking keyserver $KEYSERVER... "
412 gpg --quiet --batch --with-colons \
413 --command-fd 0 --keyserver "$KEYSERVER" \
414 --search ="$userID" > /dev/null 2>&1
417 # if the user is the monkeysphere user, then update the
418 # monkeysphere user's trustdb
419 if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
420 gpg_authentication "--check-trustdb" > /dev/null 2>&1
426 ########################################################################
427 ### PROCESSING FUNCTIONS
429 # userid and key policy checking
430 # the following checks policy on the returned keys
431 # - checks that full key has appropriate valididy (u|f)
432 # - checks key has specified capability (REQUIRED_*_KEY_CAPABILITY)
433 # - checks that requested user ID has appropriate validity
434 # (see /usr/share/doc/gnupg/DETAILS.gz)
435 # output is one line for every found key, in the following format:
439 # "flag" is an acceptability flag, 0 = ok, 1 = bad
440 # "sshKey" is the translated gpg key
442 # all log output must go to stderr, as stdout is used to pass the
443 # flag:sshKey to the calling function.
445 # expects global variable: "MODE"
448 local requiredCapability
449 local requiredPubCapability
464 # set the required key capability based on the mode
465 if [ "$MODE" = 'known_hosts' ] ; then
466 requiredCapability="$REQUIRED_HOST_KEY_CAPABILITY"
467 elif [ "$MODE" = 'authorized_keys' ] ; then
468 requiredCapability="$REQUIRED_USER_KEY_CAPABILITY"
470 requiredPubCapability=$(echo "$requiredCapability" | tr "[:lower:]" "[:upper:]")
472 # fetch the user ID if necessary/requested
473 gpg_fetch_userid "$userID"
475 # output gpg info for (exact) userid and store
476 gpgOut=$(gpg --list-key --fixed-list-mode --with-colon \
477 --with-fingerprint --with-fingerprint \
478 ="$userID" 2>/dev/null)
480 # if the gpg query return code is not 0, return 1
481 if [ "$?" -ne 0 ] ; then
482 log error " no primary keys found."
486 # loop over all lines in the gpg output and process.
487 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
488 while IFS=: read -r type validity keyid uidfpr usage ; do
489 # process based on record type
491 'pub') # primary keys
492 # new key, wipe the slate
499 log error " primary key found: $keyid"
501 # if overall key is not valid, skip
502 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
503 log error " - unacceptable primary key validity ($validity)."
506 # if overall key is disabled, skip
507 if check_capability "$usage" 'D' ; then
508 log error " - key disabled."
511 # if overall key capability is not ok, skip
512 if ! check_capability "$usage" $requiredPubCapability ; then
513 log error " - unacceptable primary key capability ($usage)."
517 # mark overall key as ok
520 # mark primary key as ok if capability is ok
521 if check_capability "$usage" $requiredCapability ; then
526 if [ "$lastKey" != pub ] ; then
527 log error " - got a user ID after a sub key?! user IDs should only follow primary keys!"
530 # if an acceptable user ID was already found, skip
531 if [ "$uidOK" = 'true' ] ; then
534 # if the user ID does matches...
535 if [ "$(echo "$uidfpr" | gpg_unescape)" = "$userID" ] ; then
536 # and the user ID validity is ok
537 if [ "$validity" = 'u' -o "$validity" = 'f' ] ; then
538 # mark user ID acceptable
545 # output a line for the primary key
547 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
548 log error " * acceptable primary key."
549 if [ -z "$sshKey" ] ; then
550 log error " ! primary key could not be translated (not RSA or DSA?)."
555 log error " - unacceptable primary key."
556 if [ -z "$sshKey" ] ; then
557 log error " ! primary key could not be translated (not RSA or DSA?)."
564 # unset acceptability of last key
569 # don't bother with sub keys if the primary key is not valid
570 if [ "$keyOK" != true ] ; then
574 # don't bother with sub keys if no user ID is acceptable:
575 if [ "$uidOK" != true ] ; then
579 # if sub key validity is not ok, skip
580 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
583 # if sub key capability is not ok, skip
584 if ! check_capability "$usage" $requiredCapability ; then
591 'fpr') # key fingerprint
592 fingerprint="$uidfpr"
594 sshKey=$(gpg2ssh "$fingerprint")
596 # if the last key was the pub key, skip
597 if [ "$lastKey" = pub ] ; then
601 # output a line for the sub key
603 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
604 log error " * acceptable sub key."
605 if [ -z "$sshKey" ] ; then
606 log error " ! sub key could not be translated (not RSA or DSA?)."
611 log error " - unacceptable sub key."
612 if [ -z "$sshKey" ] ; then
613 log error " ! sub key could not be translated (not RSA or DSA?)."
620 done | sort -t: -k1 -n -r
621 # NOTE: this last sort is important so that the "good" keys (key
622 # flag '0') come last. This is so that they take precedence when
623 # being processed in the key files over "bad" keys (key flag '1')
626 # process a single host in the known_host file
627 process_host_known_hosts() {
637 userID="ssh://${host}"
639 log info "processing: $host"
645 for line in $(process_user_id "${userID}") ; do
646 # note that key was found
649 ok=$(echo "$line" | cut -d: -f1)
650 sshKey=$(echo "$line" | cut -d: -f2)
652 if [ -z "$sshKey" ] ; then
656 # remove the old host key line, and note if removed
657 remove_line "$KNOWN_HOSTS" "$sshKey"
659 # if key OK, add new host line
660 if [ "$ok" -eq '0' ] ; then
661 # note that key was found ok
662 nKeysOK=$((nKeysOK+1))
665 if [ "$HASH_KNOWN_HOSTS" = 'true' ] ; then
666 # FIXME: this is really hackish cause ssh-keygen won't
667 # hash from stdin to stdout
669 ssh2known_hosts "$host" "$sshKey" > "$tmpfile"
670 ssh-keygen -H -f "$tmpfile" 2> /dev/null
671 cat "$tmpfile" >> "$KNOWN_HOSTS"
672 rm -f "$tmpfile" "${tmpfile}.old"
674 ssh2known_hosts "$host" "$sshKey" >> "$KNOWN_HOSTS"
679 # if at least one key was found...
680 if [ "$nKeys" -gt 0 ] ; then
681 # if ok keys were found, return 0
682 if [ "$nKeysOK" -gt 0 ] ; then
688 # if no keys were found, return 1
694 # update the known_hosts file for a set of hosts listed on command
696 update_known_hosts() {
703 # the number of hosts specified on command line
709 # set the trap to remove any lockfiles on exit
710 trap "lockfile-remove $KNOWN_HOSTS" EXIT
712 # create a lockfile on known_hosts
713 lockfile-create "$KNOWN_HOSTS"
715 # note pre update file checksum
716 fileCheck="$(file_hash "$KNOWN_HOSTS")"
720 process_host_known_hosts "$host"
724 nHostsOK=$((nHostsOK+1))
727 nHostsBAD=$((nHostsBAD+1))
731 # touch the lockfile, for good measure.
732 lockfile-touch --oneshot "$KNOWN_HOSTS"
735 # remove the lockfile
736 lockfile-remove "$KNOWN_HOSTS"
738 # note if the known_hosts file was updated
739 if [ "$(file_hash "$KNOWN_HOSTS")" != "$fileCheck" ] ; then
740 log info "known_hosts file updated."
743 # if an acceptable host was found, return 0
744 if [ "$nHostsOK" -gt 0 ] ; then
746 # else if no ok hosts were found...
748 # if no bad host were found then no hosts were found at all,
750 if [ "$nHostsBAD" -eq 0 ] ; then
752 # else if at least one bad host was found, return 2
759 # process hosts from a known_hosts file
760 process_known_hosts() {
763 log info "processing known_hosts file..."
765 hosts=$(meat "$KNOWN_HOSTS" | cut -d ' ' -f 1 | grep -v '^|.*$' | tr , ' ' | tr '\n' ' ')
767 if [ -z "$hosts" ] ; then
768 log error "no hosts to process."
772 # take all the hosts from the known_hosts file (first
773 # field), grep out all the hashed hosts (lines starting
775 update_known_hosts $hosts
778 # process uids for the authorized_keys file
779 process_uid_authorized_keys() {
788 log info "processing: $userID"
794 for line in $(process_user_id "$userID") ; do
795 # note that key was found
798 ok=$(echo "$line" | cut -d: -f1)
799 sshKey=$(echo "$line" | cut -d: -f2)
801 if [ -z "$sshKey" ] ; then
805 # remove the old host key line
806 remove_line "$AUTHORIZED_KEYS" "$sshKey"
808 # if key OK, add new host line
809 if [ "$ok" -eq '0' ] ; then
810 # note that key was found ok
811 nKeysOK=$((nKeysOK+1))
813 ssh2authorized_keys "$userID" "$sshKey" >> "$AUTHORIZED_KEYS"
817 # if at least one key was found...
818 if [ "$nKeys" -gt 0 ] ; then
819 # if ok keys were found, return 0
820 if [ "$nKeysOK" -gt 0 ] ; then
826 # if no keys were found, return 1
832 # update the authorized_keys files from a list of user IDs on command
834 update_authorized_keys() {
841 # the number of ids specified on command line
847 # set the trap to remove any lockfiles on exit
848 trap "lockfile-remove $AUTHORIZED_KEYS" EXIT
850 # create a lockfile on authorized_keys
851 lockfile-create "$AUTHORIZED_KEYS"
853 # note pre update file checksum
854 fileCheck="$(file_hash "$AUTHORIZED_KEYS")"
856 # remove any monkeysphere lines from authorized_keys file
857 remove_monkeysphere_lines "$AUTHORIZED_KEYS"
860 # process the user ID, change return code if key not found for
862 process_uid_authorized_keys "$userID"
870 nIDsBAD=$((nIDsBAD+1))
874 # touch the lockfile, for good measure.
875 lockfile-touch --oneshot "$AUTHORIZED_KEYS"
878 # remove the lockfile
879 lockfile-remove "$AUTHORIZED_KEYS"
881 # note if the authorized_keys file was updated
882 if [ "$(file_hash "$AUTHORIZED_KEYS")" != "$fileCheck" ] ; then
883 log info "authorized_keys file updated."
886 # if an acceptable id was found, return 0
887 if [ "$nIDsOK" -gt 0 ] ; then
889 # else if no ok ids were found...
891 # if no bad ids were found then no ids were found at all, and
893 if [ "$nIDsBAD" -eq 0 ] ; then
895 # else if at least one bad id was found, return 2
902 # process an authorized_user_ids file for authorized_keys
903 process_authorized_user_ids() {
908 authorizedUserIDs="$1"
910 log info "processing authorized_user_ids file..."
912 if ! meat "$authorizedUserIDs" > /dev/null ; then
913 log error "no user IDs to process."
919 # extract user IDs from authorized_user_ids file
921 for line in $(meat "$authorizedUserIDs") ; do
922 userIDs["$nline"]="$line"
926 update_authorized_keys "${userIDs[@]}"