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 # create a lockfile on known_hosts
449 lockfile-create "$KNOWN_HOSTS"
452 # process the host, change return code if host key not found
453 process_host_known_hosts "$host" || returnCode=1
455 # touch the lockfile, for good measure.
456 lockfile-touch --oneshot "$KNOWN_HOSTS"
459 # remove the lockfile
460 lockfile-remove "$KNOWN_HOSTS"
465 # process known_hosts file, going through line-by-line, extract each
466 # host, and process with the host processing function
467 process_known_hosts() {
470 # default return code is 0, which assumes a key was found for
471 # every host. code will be set to 1 if a key is not found for at
475 # take all the hosts from the known_hosts file (first field), grep
476 # out all the hashed hosts (lines starting with '|')...
477 for line in $(cat "$KNOWN_HOSTS" | meat | cut -d ' ' -f 1 | grep -v '^|.*$') ; do
478 # break up hosts into separate words
479 update_known_hosts $(echo "$line" | tr , ' ') || returnCode=1
485 # process uids for the authorized_keys file
486 process_uid_authorized_keys() {
492 # default return code is 1, which assumes no key was found
497 log "processing user ID: $userID"
499 for line in $(process_user_id "$userID") ; do
500 ok=$(echo "$line" | cut -d: -f1)
501 keyid=$(echo "$line" | cut -d: -f2)
503 sshKey=$(gpg2ssh "$keyid")
504 # remove the old host key line
505 remove_line "$AUTHORIZED_KEYS" "$sshKey"
506 # if key OK, add new host line
507 if [ "$ok" -eq '0' ] ; then
508 ssh2authorized_keys "$userID" "$sshKey" >> "$AUTHORIZED_KEYS"
510 # set return code to be 0, since a key was found
518 # update the authorized_keys files from a list of user IDs on command
520 update_authorized_keys() {
524 # default return code is 0, which assumes a key was found for
525 # every user ID. code will be set to 1 if a key is not found for
526 # at least one user ID
529 # create a lockfile on authorized_keys
530 lockfile-create "$AUTHORIZED_KEYS"
533 # process the user ID, change return code if key not found for
535 process_uid_authorized_keys "$userID" || returnCode=1
537 # touch the lockfile, for good measure.
538 lockfile-touch --oneshot "$AUTHORIZED_KEYS"
541 # remove the lockfile
542 lockfile-remove "$AUTHORIZED_KEYS"
547 # process an authorized_user_ids file for authorized_keys
548 process_authorized_user_ids() {
552 # default return code is 0, and is set to 1 if a key for a user ID
556 authorizedUserIDs="$1"
558 # set the IFS to be newline for parsing the authorized_user_ids
559 # file. can't find it in BASH(1) (found it on the net), but it
562 for userid in $(cat "$authorizedUserIDs" | meat) ; do
563 update_authorized_keys "$userid" || returnCode=1
569 # EXPERIMENTAL (unused) process userids found in authorized_keys file
570 # go through line-by-line, extract monkeysphere userids from comment
571 # fields, and process each userid
573 process_authorized_keys() {
578 # default return code is 0, and is set to 1 if a key for a user
584 # take all the monkeysphere userids from the authorized_keys file
585 # comment field (third field) that starts with "MonkeySphere uid:"
586 # FIXME: needs to handle authorized_keys options (field 0)
587 cat "$authorizedKeys" | meat | \
588 while read -r options keytype key comment ; do
589 # if the comment field is empty, assume the third field was
591 if [ -z "$comment" ] ; then
595 if echo "$comment" | egrep -v -q '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}' ; then
598 userID=$(echo "$comment" | awk "{ print $2 }")
599 if [ -z "$userID" ] ; then
604 log "processing userid: '$userID'"
605 process_user_id "$userID" > /dev/null || returnCode=1
611 ##################################################
612 ### GPG HELPER FUNCTIONS
614 # retrieve key from web of trust, and set owner trust to "full"
623 if [ -z "$keyID" ] ; then
624 failure "You must specify key to trust."
627 # get the key from the key server
628 if ! gpg --keyserver "$KEYSERVER" --recv-key "$keyID" ; then
629 failure "Could not retrieve key '$keyID'."
632 # get key fingerprint
633 fingerprint=$(get_key_fingerprint "$keyID")
636 gpg --fingerprint "$fingerprint"
638 while [ -z "$trustLevel" ] ; do
640 Please decide how far you trust this user to correctly verify other users' keys
641 (by looking at passports, checking fingerprints from different sources, etc.)
643 1 = I don't know or won't say
645 3 = I trust marginally
647 5 = I trust ultimately
650 read -p "Your decision? " trustLevel
651 if echo "$trustLevel" | grep -v "[1-5]" ; then
652 echo "Unknown trust level '$trustLevel'."
654 elif [ "$trustLevel" = 'q' ] ; then
659 # attach a "non-exportable" signature to the key
660 # this is required for the key to have any validity at all
661 # the 'y's on stdin indicates "yes, i really want to sign"
662 echo -e 'y\ny' | gpg --quiet --lsign-key --command-fd 0 "$fingerprint"
664 # index trustLevel by one to difference between level in ui and level
666 trustLevel=$((trustLevel+1))
668 # import new owner trust level for key
669 echo "${fingerprint}:${trustLevel}:" | gpg --import-ownertrust
671 log "Owner trust updated."
673 failure "There was a problem changing owner trust."
677 # publish server key to keyserver
678 publish_server_key() {
679 read -p "really publish key to $KEYSERVER? [y|N]: " OK; OK=${OK:=N}
680 if [ ${OK/y/Y} != 'Y' ] ; then
685 # FIXME: need to figure out better way to identify host key
686 # dummy command so as not to publish fakes keys during testing
688 #gpg --keyserver "$KEYSERVER" --send-keys $(hostname -f)
689 failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development).
690 To publish manually, do: gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"