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 [ ! -e "$file" ] ; then
338 if (($# == 1)) ; then
339 lines=$(grep -F "$1" "$file") || true
341 lines=$(grep -F "$1" "$file" | grep -F "$2") || true
344 # if the string was found, remove it
345 if [ "$lines" ] ; then
346 log debug "removing matching key lines..."
347 tempfile=$(mktemp "${file}.XXXXXXX") || \
348 failure "Unable to make temp file '${file}.XXXXXXX'"
349 grep -v -x -F "$lines" "$file" >"$tempfile" || :
350 mv -f "$tempfile" "$file"
354 # remove all lines with MonkeySphere strings from stdin
355 remove_monkeysphere_lines() {
356 egrep -v ' MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2} '
359 # translate ssh-style path variables %h and %u
360 translate_ssh_variables() {
367 # get the user's home directory
368 userHome=$(get_homedir "$uname")
370 # translate '%u' to user name
371 path=${path/\%u/"$uname"}
372 # translate '%h' to user home directory
373 path=${path/\%h/"$userHome"}
378 # test that a string to conforms to GPG's expiration format
380 echo "$1" | egrep -q "^[0-9]+[mwy]?$"
383 # touch a key file if it doesn't exist, including creating needed
384 # directories with correct permissions
385 touch_key_file_or_fail() {
389 if [ ! -f "$keyFile" ]; then
390 # make sure to create files and directories with the
391 # appropriate write bits turned off:
392 newUmask=$(printf "%04o" $(( 0$(umask) | 0022 )) )
393 [ -d $(dirname "$keyFile") ] \
394 || (umask "$newUmask" && mkdir -p -m 0700 $(dirname "$keyFile") ) \
395 || failure "Could not create path to $keyFile"
396 # make sure to create this file with the appropriate bits turned off:
397 (umask "$newUmask" && touch "$keyFile") \
398 || failure "Unable to create $keyFile"
402 # check that a file is properly owned, and that all it's parent
403 # directories are not group/other writable
404 check_key_file_permissions() {
411 if [ "$STRICT_MODES" = 'false' ] ; then
412 log debug "skipping path permission check for '$path' because STRICT_MODES is false..."
415 log debug "checking path permission '$path'..."
416 "${SYSSHAREDIR}/checkperms" "$uname" "$path"
419 # return a list of all users on the system
421 if type getent &>/dev/null ; then
422 # for linux and FreeBSD systems
423 getent passwd | cut -d: -f1
424 elif type dscl &>/dev/null ; then
426 dscl localhost -list /Search/Users
428 failure "Neither getent or dscl is in the path! Could not determine list of users."
432 # take one argument, a service name. in response, print a series of
433 # lines, each with a unique numeric port number that might be
434 # associated with that service name. (e.g. in: "https", out: "443")
435 # if nothing is found, print nothing, and return 0.
437 # return 1 if there was an error in the search somehow
438 get_port_for_service() {
440 [[ "$1" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]] || \
441 failure $(printf "This is not a valid service name: '%s'" "$1")
442 if type getent &>/dev/null ; then
443 # for linux and FreeBSD systems (getent returns 2 if not found, 0 on success, 1 or 3 on various failures)
444 (getent services "$service" || if [ "$?" -eq 2 ] ; then true ; else false; fi) | awk '{ print $2 }' | cut -f1 -d/ | sort -u
445 elif [ -r /etc/services ] ; then
446 # fall back to /etc/services for systems that don't have getent (MacOS?)
447 # FIXME: doesn't handle aliases like "null" (or "http"?), which don't show up at the beginning of the line.
448 awk $(printf '/^%s[[:space:]]/{ print $2 }' "$1") /etc/services | cut -f1 -d/ | sort -u
454 # return the path to the home directory of a user
456 local uname=${1:-`whoami`}
457 eval "echo ~${uname}"
460 # return the primary group of a user
461 get_primary_group() {
462 local uname=${1:-`whoami`}
463 groups "$uname" | sed 's/^..* : //' | awk '{ print $1 }'
466 ### CONVERSION UTILITIES
468 # output the ssh key for a given key ID
474 gpg --export --no-armor "$keyID" | openpgp2ssh "$keyID" 2>/dev/null
477 # output known_hosts line from ssh key
483 # FIXME this does not properly deal with IPv6 hosts using the
484 # standard port (because it's unclear whether their final
485 # colon-delimited address section is a port number or an address
491 # specify the host and port properly for new ssh known_hosts
493 if [ "$port" != "$host" ] ; then
494 host="[${host}]:${port}"
498 if [ "$HASH_KNOWN_HOSTS" = 'true' ] ; then
499 if (type ssh-keygen >/dev/null) ; then
500 log verbose "hashing known_hosts line"
501 # FIXME: this is really hackish cause
502 # ssh-keygen won't hash from stdin to
504 tmpfile=$(mktemp ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)
505 printf "%s %s MonkeySphere%s\n" "$host" "$key" "$DATE" \
507 ssh-keygen -H -f "$tmpfile" 2>/dev/null
508 if [[ "$keyFile" == '-' ]] ; then
511 cat "$tmpfile" >> "$keyFile"
513 rm -f "$tmpfile" "${tmpfile}.old"
514 # FIXME: we could do this without needing ssh-keygen.
515 # hashed known_hosts looks like: |1|X|Y where 1 means SHA1
516 # (nothing else is defined in openssh sources), X is the
517 # salt (same length as the digest output), base64-encoded,
518 # and Y is the digested hostname (also base64-encoded).
519 # see hostfile.{c,h} in openssh sources.
521 log error "Cannot hash known_hosts line as requested."
524 printf "%s %s MonkeySphere%s\n" "$host" "$key" "$DATE"
528 # output authorized_keys line from ssh key
529 ssh2authorized_keys() {
533 if [[ "$AUTHORIZED_KEYS_OPTIONS" ]]; then
534 printf "%s %s MonkeySphere%s %s\n" "$AUTHORIZED_KEYS_OPTIONS" "$key" "$DATE" "$userID"
536 printf "%s MonkeySphere%s %s\n" "$key" "$DATE" "$userID"
540 # convert key from gpg to ssh known_hosts format
549 key=$(gpg2ssh "$keyID")
551 # NOTE: it seems that ssh-keygen -R removes all comment fields from
552 # all lines in the known_hosts file. why?
553 # NOTE: just in case, the COMMENT can be matched with the
555 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
556 printf "%s %s MonkeySphere%s\n" "$host" "$key" "$DATE"
559 # convert key from gpg to ssh authorized_keys format
560 gpg2authorized_keys() {
568 key=$(gpg2ssh "$keyID")
570 # NOTE: just in case, the COMMENT can be matched with the
572 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
573 printf "%s MonkeySphere%s %s\n" "$key" "$DATE" "$userID"
578 # script to determine if gpg version is equal to or greater than specified version
579 is_gpg_version_greater_equal() {
580 local gpgVersion=$(gpg --version | head -1 | awk '{ print $3 }')
581 local latest=$(printf '%s\n%s\n' "$1" "$gpgVersion" \
582 | tr '.' ' ' | sort -g -k1 -k2 -k3 \
583 | tail -1 | tr ' ' '.')
584 [[ "$gpgVersion" == "$latest" ]]
587 # retrieve all keys with given user id from keyserver
588 # FIXME: need to figure out how to retrieve all matching keys
589 # (not just first N (5 in this case))
594 if [ "$CHECK_KEYSERVER" != 'true' ] ; then
600 log verbose " checking keyserver $KEYSERVER... "
602 gpg --quiet --batch --with-colons \
603 --command-fd 0 --keyserver "$KEYSERVER" \
604 --search ="$userID" &>/dev/null
607 if [ "$returnCode" != 0 ] ; then
608 log error "Failure ($returnCode) searching keyserver $KEYSERVER for user id '$userID'"
614 ########################################################################
615 ### PROCESSING FUNCTIONS
617 # userid and key policy checking
618 # the following checks policy on the returned keys
619 # - checks that full key has appropriate valididy (u|f)
620 # - checks key has specified capability (REQUIRED_KEY_CAPABILITY)
621 # - checks that requested user ID has appropriate validity
622 # (see /usr/share/doc/gnupg/DETAILS.gz)
623 # output is one line for every found key, in the following format:
627 # "flag" is an acceptability flag, 0 = ok, 1 = bad
628 # "sshKey" is the relevant OpenPGP key, in the form accepted by OpenSSH
630 # all log output must go to stderr, as stdout is used to pass the
631 # flag:sshKey to the calling function.
635 local requiredCapability
636 local requiredPubCapability
649 # set the required key capability based on the mode
650 requiredCapability=${REQUIRED_KEY_CAPABILITY:="a"}
651 requiredPubCapability=$(echo "$requiredCapability" | tr "[:lower:]" "[:upper:]")
653 # fetch the user ID if necessary/requested
654 gpg_fetch_userid "$userID"
656 # output gpg info for (exact) userid and store
657 gpgOut=$(gpg --list-key --fixed-list-mode --with-colons \
658 --with-fingerprint --with-fingerprint \
659 ="$userID" 2>/dev/null) || returnCode="$?"
661 # if the gpg query return code is not 0, return 1
662 if [ "$returnCode" -ne 0 ] ; then
663 log verbose " no primary keys found."
667 # loop over all lines in the gpg output and process.
668 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
669 while IFS=: read -r type validity keyid uidfpr usage ; do
670 # process based on record type
672 'pub') # primary keys
673 # new key, wipe the slate
680 log verbose " primary key found: $keyid"
682 # if overall key is not valid, skip
683 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
684 log debug " - unacceptable primary key validity ($validity)."
687 # if overall key is disabled, skip
688 if check_capability "$usage" 'D' ; then
689 log debug " - key disabled."
692 # if overall key capability is not ok, skip
693 if ! check_capability "$usage" $requiredPubCapability ; then
694 log debug " - unacceptable primary key capability ($usage)."
698 # mark overall key as ok
701 # mark primary key as ok if capability is ok
702 if check_capability "$usage" $requiredCapability ; then
707 if [ "$lastKey" != pub ] ; then
708 log verbose " ! got a user ID after a sub key?! user IDs should only follow primary keys!"
711 # if an acceptable user ID was already found, skip
712 if [ "$uidOK" = 'true' ] ; then
715 # if the user ID does matches...
716 if [ "$(echo "$uidfpr" | gpg_unescape)" = "$userID" ] ; then
717 # and the user ID validity is ok
718 if [ "$validity" = 'u' -o "$validity" = 'f' ] ; then
719 # mark user ID acceptable
722 log debug " - unacceptable user ID validity ($validity)."
728 # output a line for the primary key
730 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
731 log verbose " * acceptable primary key."
732 if [ -z "$sshKey" ] ; then
733 log verbose " ! primary key could not be translated (not RSA?)."
738 log debug " - unacceptable primary key."
739 if [ -z "$sshKey" ] ; then
740 log debug " ! primary key could not be translated (not RSA?)."
747 # unset acceptability of last key
752 # don't bother with sub keys if the primary key is not valid
753 if [ "$keyOK" != true ] ; then
757 # don't bother with sub keys if no user ID is acceptable:
758 if [ "$uidOK" != true ] ; then
762 # if sub key validity is not ok, skip
763 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
764 log debug " - unacceptable sub key validity ($validity)."
767 # if sub key capability is not ok, skip
768 if ! check_capability "$usage" $requiredCapability ; then
769 log debug " - unacceptable sub key capability ($usage)."
776 'fpr') # key fingerprint
777 fingerprint="$uidfpr"
779 sshKey=$(gpg2ssh "$fingerprint")
781 # if the last key was the pub key, skip
782 if [ "$lastKey" = pub ] ; then
786 # output a line for the sub key
788 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
789 log verbose " * acceptable sub key."
790 if [ -z "$sshKey" ] ; then
791 log error " ! sub key could not be translated (not RSA?)."
796 log debug " - unacceptable sub key."
797 if [ -z "$sshKey" ] ; then
798 log debug " ! sub key could not be translated (not RSA?)."
805 done | sort -t: -k1 -n -r
806 # NOTE: this last sort is important so that the "good" keys (key
807 # flag '0') come last. This is so that they take precedence when
808 # being processed in the key files over "bad" keys (key flag '1')
811 process_keys_for_file() {
819 log verbose "processing: $userID"
820 log debug "key file: $keyFile"
823 for line in $(process_user_id "$userID") ; do
827 if [ -z "$sshKey" ] ; then
831 # remove the old key line
832 if [[ "$keyFile" != '-' ]] ; then
835 remove_line "$keyFile" "$sshKey"
838 host=${userID#ssh://}
839 remove_line "$keyFile" "$host" "$sshKey"
846 # if key OK, add new key line
847 if [ "$ok" -eq '0' ] ; then
853 keyLine=$(ssh2authorized_keys "$userID" "$sshKey")
856 host=${userID#ssh://}
857 keyLine=$(ssh2known_hosts "$host" "$sshKey")
861 echo "key line: $keyLine" | log debug
862 if [[ "$keyFile" == '-' ]] ; then
865 log debug "adding key line to file..."
866 echo "$keyLine" >>"$keyFile"
873 log debug "KEYS_PROCESSED=$KEYS_PROCESSED"
874 log debug "KEYS_VALID=$KEYS_VALID"
877 # process an authorized_user_ids file on stdin for authorized_keys
878 process_authorized_user_ids() {
879 local authorizedKeys="$1"
885 # extract user IDs from authorized_user_ids file
893 if [[ -z ${koptions[${nline}]} ]]; then
894 koptions[${nline}]=$(echo $line | sed 's/^[ ]*//;s/[ ]$//;')
896 koptions[${nline}]="${koptions[${nline}]},$(echo $line | sed 's/^[ ]*//;s/[ ]$//;')"
901 userIDs[${nline}]="$line"
902 unset koptions[${nline}] || true
907 for i in $(seq 1 $nline); do
908 AUTHORIZED_KEYS_OPTIONS="${koptions[$i]}" FILE_TYPE='authorized_keys' process_keys_for_file "$authorizedKeys" "${userIDs[$i]}" || returnCode="$?"
912 # takes a gpg key or keys on stdin, and outputs a list of
913 # fingerprints, one per line:
914 list_primary_fingerprints() {
915 local fake=$(msmktempdir)
916 trap "rm -rf $fake" EXIT
917 GNUPGHOME="$fake" gpg --no-tty --quiet --import --ignore-time-conflict 2>/dev/null
918 GNUPGHOME="$fake" gpg --with-colons --fingerprint --list-keys | \
919 awk -F: '/^fpr:/{ print $10 }'
924 # takes an OpenPGP key or set of keys on stdin, a fingerprint or other
925 # key identifier as $1, and outputs the gpg-formatted information for
926 # the requested keys from the material on stdin
928 local fake=$(msmktempdir)
929 trap "rm -rf $fake" EXIT
930 GNUPGHOME="$fake" gpg --no-tty --quiet --import --ignore-time-conflict 2>/dev/null
931 GNUPGHOME="$fake" gpg --with-colons --fingerprint --fixed-list-mode --list-keys "$1"
941 if [ -e "$loc" ] ; then
942 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
946 check_upgrade_dir() {
950 if [ -d "$loc" ] ; then
951 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
955 ## look for cruft from old versions of the monkeysphere, and notice if
956 ## upgrades have not been run:
958 check_upgrade_dir "${SYSCONFIGDIR}/gnupg-host" 0.23
959 check_upgrade_dir "${SYSCONFIGDIR}/gnupg-authentication" 0.23
961 check_cruft_file "${SYSCONFIGDIR}/gnupg-authentication.conf" 0.23
962 check_cruft_file "${SYSCONFIGDIR}/gnupg-host.conf" 0.23
965 for foo in "${SYSDATADIR}/backup-from-"*"-transition" ; do
966 if [ -d "$foo" ] ; then
967 printf "! %s\n" "$foo" | log info
971 if [ "$found" ] ; then
972 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