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"
19 CACHE="/var/cache/monkeysphere"
22 ########################################################################
35 # write output to stderr
45 # cut out all comments(#) and blank lines from standard input
47 grep -v -e "^[[:space:]]*#" -e '^$'
50 # cut a specified line from standard input
52 head --line="$1" | tail -1
55 # check that characters are in a string (in an AND fashion).
56 # used for checking key capability
57 # check_capability capability a [b...]
66 if echo "$usage" | grep -q -v "$capcheck" ; then
73 # convert escaped characters from gpg output back into original
75 # FIXME: undo all escape character translation in with-colons gpg output
77 echo "$1" | sed 's/\\x3a/:/'
80 # remove all lines with specified string from specified file
88 if [ "$file" -a "$string" ] ; then
89 grep -v "$string" "$file" | sponge "$file"
93 # translate ssh-style path variables %h and %u
94 translate_ssh_variables() {
101 # get the user's home directory
102 userHome=$(getent passwd "$uname" | cut -d: -f6)
104 # translate '%u' to user name
105 path=${path/\%u/"$uname"}
106 # translate '%h' to user home directory
107 path=${path/\%h/"$userHome"}
112 ### CONVERTION UTILITIES
114 # output the ssh key for a given key ID
119 # only use last 16 characters until openpgp2ssh can take all 40 #TMP
120 keyID=$(echo "$1" | cut -c 25-) #TMP
122 gpg --export "$keyID" | openpgp2ssh "$keyID" 2> /dev/null
125 # output the ssh key for a given secret key ID
130 # only use last 16 characters until openpgp2ssh can take all 40 #TMP
131 keyID=$(echo "$1" | cut -c 25-) #TMP
133 gpg --export-secret-key "$keyID" | openpgp2ssh "$keyID" 2> /dev/null
136 # output known_hosts line from ssh key
145 echo -n "$key" | tr -d '\n'
146 echo " MonkeySphere${DATE}"
149 # output authorized_keys line from ssh key
150 ssh2authorized_keys() {
157 echo -n "$key" | tr -d '\n'
158 echo " MonkeySphere${DATE} ${userID}"
161 # convert key from gpg to ssh known_hosts format
169 # NOTE: it seems that ssh-keygen -R removes all comment fields from
170 # all lines in the known_hosts file. why?
171 # NOTE: just in case, the COMMENT can be matched with the
173 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
175 gpg2ssh "$keyID" | tr -d '\n'
176 echo " MonkeySphere${DATE}"
179 # convert key from gpg to ssh authorized_keys format
180 gpg2authorized_keys() {
187 # NOTE: just in case, the COMMENT can be matched with the
189 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
190 gpg2ssh "$keyID" | tr -d '\n'
191 echo " MonkeySphere${DATE} ${userID}"
196 # retrieve all keys with given user id from keyserver
197 # FIXME: need to figure out how to retrieve all matching keys
198 # (not just first N (5 in this case))
204 log -n " checking keyserver $KEYSERVER... "
206 gpg --quiet --batch --with-colons \
207 --command-fd 0 --keyserver "$KEYSERVER" \
208 --search ="$userID" > /dev/null 2>&1
212 # get the full fingerprint of a key ID
213 get_key_fingerprint() {
218 gpg --list-key --with-colons --fixed-list-mode \
219 --with-fingerprint --with-fingerprint "$keyID" | \
220 grep '^fpr:' | grep "$keyID" | cut -d: -f10
223 ########################################################################
224 ### PROCESSING FUNCTIONS
226 # userid and key policy checking
227 # the following checks policy on the returned keys
228 # - checks that full key has appropriate valididy (u|f)
229 # - checks key has specified capability (REQUIRED_*_KEY_CAPABILITY)
230 # - checks that requested user ID has appropriate validity
231 # (see /usr/share/doc/gnupg/DETAILS.gz)
232 # output is one line for every found key, in the following format:
236 # "flag" is an acceptability flag, 0 = ok, 1 = bad
237 # "fingerprint" is the fingerprint of the key
239 # expects global variable: "MODE"
242 local requiredCapability
243 local requiredPubCapability
258 # set the required key capability based on the mode
259 if [ "$MODE" = 'known_hosts' ] ; then
260 requiredCapability="$REQUIRED_HOST_KEY_CAPABILITY"
261 elif [ "$MODE" = 'authorized_keys' ] ; then
262 requiredCapability="$REQUIRED_USER_KEY_CAPABILITY"
264 requiredPubCapability=$(echo "$requiredCapability" | tr "[:lower:]" "[:upper:]")
266 # if CHECK_KEYSERVER variable set, check the keyserver
268 if [ "$CHECK_KEYSERVER" = "true" ] ; then
269 gpg_fetch_userid "$userID"
272 # output gpg info for (exact) userid and store
273 gpgOut=$(gpg --list-key --fixed-list-mode --with-colon \
274 --with-fingerprint --with-fingerprint \
275 ="$userID" 2>/dev/null)
277 # if the gpg query return code is not 0, return 1
278 if [ "$?" -ne 0 ] ; then
279 log " - key not found."
283 # loop over all lines in the gpg output and process.
284 # need to do it this way (as opposed to "while read...") so that
285 # variables set in loop will be visible outside of loop
286 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
287 while IFS=: read -r type validity keyid uidfpr usage ; do
288 # process based on record type
290 'pub') # primary keys
291 # new key, wipe the slate
298 log " primary key found: $keyid"
300 # if overall key is not valid, skip
301 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
302 log " - unacceptable primary key validity ($validity)."
305 # if overall key is disabled, skip
306 if check_capability "$usage" 'D' ; then
307 log " - key disabled."
310 # if overall key capability is not ok, skip
311 if ! check_capability "$usage" $requiredPubCapability ; then
312 log " - unacceptable primary key capability ($usage)."
316 # mark overall key as ok
319 # mark primary key as ok if capability is ok
320 if check_capability "$usage" $requiredCapability ; then
325 # if an acceptable user ID was already found, skip
326 if [ "$uidOK" ] ; then
329 # if the user ID does not match, skip
330 if [ "$(unescape "$uidfpr")" != "$userID" ] ; then
333 # if the user ID validity is not ok, skip
334 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
338 # mark user ID acceptable
341 # output a line for the primary key
343 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
344 log " * acceptable key found."
345 echo "0:${fingerprint}"
347 echo "1:${fingerprint}"
351 # unset acceptability of last key
356 # if sub key validity is not ok, skip
357 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
360 # if sub key capability is not ok, skip
361 if ! check_capability "$usage" $requiredCapability ; then
368 'fpr') # key fingerprint
369 fingerprint="$uidfpr"
371 # if the last key was the pub key, skip
372 if [ "$lastKey" = pub ] ; then
376 # output a line for the last subkey
378 if [ "$keyOK" -a "$uidOK" -a "$lastKeyOK" ] ; then
379 log " * acceptable key found."
380 echo "0:${fingerprint}"
382 echo "1:${fingerprint}"
389 # process a single host in the known_host file
390 process_host_known_hosts() {
398 # default return code is 1, which assumes no key was found
403 log "processing host: $host"
405 userID="ssh://${host}"
407 for line in $(process_user_id "ssh://${host}") ; do
408 ok=$(echo "$line" | cut -d: -f1)
409 keyid=$(echo "$line" | cut -d: -f2)
411 sshKey=$(gpg2ssh "$keyid")
412 # remove the old host key line
413 remove_line "$KNOWN_HOSTS" "$sshKey"
414 # if key OK, add new host line
415 if [ "$ok" -eq '0' ] ; then
417 if [ "$HASH_KNOWN_HOSTS" = 'true' ] ; then
418 # FIXME: this is really hackish cause ssh-keygen won't
419 # hash from stdin to stdout
421 ssh2known_hosts "$host" "$sshKey" > "$tmpfile"
422 ssh-keygen -H -f "$tmpfile" 2> /dev/null
423 cat "$tmpfile" >> "$KNOWN_HOSTS"
424 rm -f "$tmpfile" "${tmpfile}.old"
426 ssh2known_hosts "$host" "$sshKey" >> "$KNOWN_HOSTS"
428 # set return code to be 0, since a key was found
437 # update the known_hosts file for a set of hosts listed on command
439 update_known_hosts() {
443 # default return code is 0, which assumes a key was found for
444 # every host. code will be set to 1 if a key is not found for at
448 # set the trap to remove any lockfiles on exit
449 trap "lockfile-remove $KNOWN_HOSTS" EXIT
451 # create a lockfile on known_hosts
452 lockfile-create "$KNOWN_HOSTS"
455 # process the host, change return code if host key not found
456 process_host_known_hosts "$host" || returnCode=1
458 # touch the lockfile, for good measure.
459 lockfile-touch --oneshot "$KNOWN_HOSTS"
462 # remove the lockfile
463 lockfile-remove "$KNOWN_HOSTS"
468 # process known_hosts file, going through line-by-line, extract each
469 # host, and process with the host processing function
470 process_known_hosts() {
473 # default return code is 0, which assumes a key was found for
474 # every host. code will be set to 1 if a key is not found for at
478 # take all the hosts from the known_hosts file (first field), grep
479 # out all the hashed hosts (lines starting with '|')...
480 for line in $(cat "$KNOWN_HOSTS" | meat | cut -d ' ' -f 1 | grep -v '^|.*$') ; do
481 # break up hosts into separate words
482 update_known_hosts $(echo "$line" | tr , ' ') || returnCode=1
488 # process uids for the authorized_keys file
489 process_uid_authorized_keys() {
495 # default return code is 1, which assumes no key was found
500 log "processing user ID: $userID"
502 for line in $(process_user_id "$userID") ; do
503 ok=$(echo "$line" | cut -d: -f1)
504 keyid=$(echo "$line" | cut -d: -f2)
506 sshKey=$(gpg2ssh "$keyid")
507 # remove the old host key line
508 remove_line "$AUTHORIZED_KEYS" "$sshKey"
509 # if key OK, add new host line
510 if [ "$ok" -eq '0' ] ; then
511 ssh2authorized_keys "$userID" "$sshKey" >> "$AUTHORIZED_KEYS"
513 # set return code to be 0, since a key was found
521 # update the authorized_keys files from a list of user IDs on command
523 update_authorized_keys() {
527 # default return code is 0, which assumes a key was found for
528 # every user ID. code will be set to 1 if a key is not found for
529 # at least one user ID
532 # set the trap to remove any lockfiles on exit
533 trap "lockfile-remove $AUTHORIZED_KEYS" EXIT
535 # create a lockfile on authorized_keys
536 lockfile-create "$AUTHORIZED_KEYS"
539 # process the user ID, change return code if key not found for
541 process_uid_authorized_keys "$userID" || returnCode=1
543 # touch the lockfile, for good measure.
544 lockfile-touch --oneshot "$AUTHORIZED_KEYS"
547 # remove the lockfile
548 lockfile-remove "$AUTHORIZED_KEYS"
553 # process an authorized_user_ids file for authorized_keys
554 process_authorized_user_ids() {
558 # default return code is 0, and is set to 1 if a key for a user ID
562 authorizedUserIDs="$1"
564 # set the IFS to be newline for parsing the authorized_user_ids
565 # file. can't find it in BASH(1) (found it on the net), but it
568 for userid in $(cat "$authorizedUserIDs" | meat) ; do
569 update_authorized_keys "$userid" || returnCode=1
575 # EXPERIMENTAL (unused) process userids found in authorized_keys file
576 # go through line-by-line, extract monkeysphere userids from comment
577 # fields, and process each userid
579 process_authorized_keys() {
584 # default return code is 0, and is set to 1 if a key for a user
590 # take all the monkeysphere userids from the authorized_keys file
591 # comment field (third field) that starts with "MonkeySphere uid:"
592 # FIXME: needs to handle authorized_keys options (field 0)
593 cat "$authorizedKeys" | meat | \
594 while read -r options keytype key comment ; do
595 # if the comment field is empty, assume the third field was
597 if [ -z "$comment" ] ; then
601 if echo "$comment" | egrep -v -q '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}' ; then
604 userID=$(echo "$comment" | awk "{ print $2 }")
605 if [ -z "$userID" ] ; then
610 log "processing userid: '$userID'"
611 process_user_id "$userID" > /dev/null || returnCode=1
617 ##################################################
618 ### GPG HELPER FUNCTIONS
620 # retrieve key from web of trust, and set owner trust to "full"
629 if [ -z "$keyID" ] ; then
630 failure "You must specify key to trust."
633 # get the key from the key server
634 if ! gpg --keyserver "$KEYSERVER" --recv-key "$keyID" ; then
635 failure "Could not retrieve key '$keyID'."
638 # get key fingerprint
639 fingerprint=$(get_key_fingerprint "$keyID")
642 gpg --fingerprint "$fingerprint"
644 while [ -z "$trustLevel" ] ; do
646 Please decide how far you trust this user to correctly verify other users' keys
647 (by looking at passports, checking fingerprints from different sources, etc.)
649 1 = I don't know or won't say
651 3 = I trust marginally
653 5 = I trust ultimately
656 read -p "Your decision? " trustLevel
657 if echo "$trustLevel" | grep -v "[1-5]" ; then
658 echo "Unknown trust level '$trustLevel'."
660 elif [ "$trustLevel" = 'q' ] ; then
665 # attach a "non-exportable" signature to the key
666 # this is required for the key to have any validity at all
667 # the 'y's on stdin indicates "yes, i really want to sign"
668 echo -e 'y\ny' | gpg --quiet --lsign-key --command-fd 0 "$fingerprint"
670 # index trustLevel by one to difference between level in ui and level
672 trustLevel=$((trustLevel+1))
674 # import new owner trust level for key
675 echo "${fingerprint}:${trustLevel}:" | gpg --import-ownertrust
677 log "Owner trust updated."
679 failure "There was a problem changing owner trust."
683 # publish server key to keyserver
684 publish_server_key() {
685 read -p "really publish key to $KEYSERVER? [y|N]: " OK; OK=${OK:=N}
686 if [ ${OK/y/Y} != 'Y' ] ; then
691 # FIXME: need to figure out better way to identify host key
692 # dummy command so as not to publish fakes keys during testing
694 #gpg --keyserver "$KEYSERVER" --send-keys $(hostname -f)
695 failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development).
696 To publish manually, do: gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"