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 ########################################################################
15 ETC="/etc/monkeysphere"
17 CACHE="/var/cache/monkeysphere"
19 ########################################################################
26 # write output to stderr
32 # cut out all comments(#) and blank lines from standard input
34 grep -v -e "^[[:space:]]*#" -e '^$'
37 # cut a specified line from standard input
39 head --line="$1" | tail -1
42 # retrieve all keys with given user id from keyserver
43 # FIXME: need to figure out how to retrieve all matching keys
49 gpg --quiet --batch --command-fd 0 --with-colons \
50 --keyserver "$KEYSERVER" \
51 --search ="$id" >/dev/null 2>&1
54 # check that characters are in a string (in an AND fashion).
55 # used for checking key capability
56 # check_capability capability a [b...]
65 if echo "$capability" | grep -q -v "$capcheck" ; then
72 # get the full fingerprint of a key ID
73 get_key_fingerprint() {
78 gpg --list-key --with-colons --fixed-list-mode \
79 --with-fingerprint "$keyID" | grep "$keyID" | \
80 grep '^fpr:' | cut -d: -f10
84 # convert escaped characters from gpg output back into original
86 # FIXME: undo all escape character translation in with-colons gpg output
88 echo "$1" | sed 's/\\x3a/:/'
91 # convert key from gpg to ssh known_hosts format
97 host=$(echo "$2" | sed -e "s|ssh://||")
99 # NOTE: it seems that ssh-keygen -R removes all comment fields from
100 # all lines in the known_hosts file. why?
101 # NOTE: just in case, the COMMENT can be matched with the
103 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
105 gpg --export "$keyID" | \
106 openpgp2ssh "$keyID" | tr -d '\n'
107 echo " MonkeySphere${DATE}"
110 # convert key from gpg to ssh authorized_keys format
111 gpg2authorized_keys() {
118 gpg --export "$keyID" | \
119 openpgp2ssh "$keyID" | tr -d '\n'
120 echo " MonkeySphere${DATE}:${userID}"
123 # userid and key policy checking
124 # the following checks policy on the returned keys
125 # - checks that full key has appropriate valididy (u|f)
126 # - checks key has specified capability (REQUIRED_*_KEY_CAPABILITY)
127 # - checks that particular desired user id has appropriate validity
128 # see /usr/share/doc/gnupg/DETAILS.gz
129 # expects global variable: "MODE"
133 local requiredCapability
134 local requiredPubCapability
152 # set the required key capability based on the mode
153 if [ "$MODE" = 'known_hosts' ] ; then
154 requiredCapability="$REQUIRED_HOST_KEY_CAPABILITY"
155 elif [ "$MODE" = 'authorized_keys' ] ; then
156 requiredCapability="$REQUIRED_USER_KEY_CAPABILITY"
158 requiredPubCapability=$(echo "$requiredCapability" | tr "[:lower:]" "[:upper:]")
160 # fetch keys from keyserver, return 1 if none found
161 gpg_fetch_userid "$userID" || return 1
163 # output gpg info for (exact) userid and store
164 gpgOut=$(gpg --fixed-list-mode --list-key --with-colons \
165 ="$userID" 2> /dev/null)
167 # return 1 if there only "tru" lines are output from gpg
168 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
169 log " key not found."
173 # loop over all lines in the gpg output and process.
174 # need to do it this way (as opposed to "while read...") so that
175 # variables set in loop will be visible outside of loop
176 for line in $(seq 1 $(echo "$gpgOut" | wc -l)) ; do
178 # read the contents of the line
179 type=$(echo "$gpgOut" | cutline "$line" | cut -d: -f1)
180 validity=$(echo "$gpgOut" | cutline "$line" | cut -d: -f2)
181 keyid=$(echo "$gpgOut" | cutline "$line" | cut -d: -f5)
182 uidfpr=$(echo "$gpgOut" | cutline "$line" | cut -d: -f10)
183 capability=$(echo "$gpgOut" | cutline "$line" | cut -d: -f12)
185 # process based on record type
187 'pub') # primary keys
188 # new key, wipe the slate
196 # check primary key validity
197 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
198 log " unacceptable primary key validity ($validity)."
201 # check capability is not Disabled...
202 if check_capability "$capability" 'D' ; then
206 # check overall key capability
207 # must be Encryption and Authentication
208 if ! check_capability "$capability" $requiredPubCapability ; then
209 log " unacceptable primary key capability ($capability)."
213 # mark if primary key is acceptable
216 # add primary key ID to key list if it has required capability
217 if check_capability "$capability" $requiredCapability ; then
218 keyIDs[${#keyIDs[*]}]="$keyid"
222 # check key ok and we have key fingerprint
223 if [ -z "$keyOK" ] ; then
227 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
230 # check the uid matches
231 if [ "$(unescape "$uidfpr")" != "$userID" ] ; then
235 # mark if uid acceptable
239 # add sub key ID to key list if it has required capability
240 if check_capability "$capability" $requiredCapability ; then
241 keyIDs[${#keyIDs[*]}]="$keyid"
247 # hash userid for cache file name
248 userIDHash=$(echo "$userID" | sha1sum | awk '{ print $1 }')
250 # make sure the cache directory exists
253 # touch/clear key cache file
254 # (will be left empty if there are noacceptable keys)
255 > "$cacheDir"/"$userIDHash"."$pubKeyID"
257 # for each acceptable key, write an ssh key line to the
259 if [ "$keyOK" -a "$uidOK" -a "${keyIDs[*]}" ] ; then
260 for keyID in ${keyIDs[@]} ; do
261 log " acceptable key/uid found."
263 if [ "$MODE" = 'known_hosts' ] ; then
265 gpg2known_hosts "$keyID" "$userID" >> \
266 "$cacheDir"/"$userIDHash"."$pubKeyID"
267 # hash the cache file if specified
268 if [ "$HASH_KNOWN_HOSTS" ] ; then
269 ssh-keygen -H -f "$cacheDir"/"$userIDHash"."$pubKeyID" > /dev/null 2>&1
270 rm "$cacheDir"/"$userIDHash"."$pubKeyID".old
272 elif [ "$MODE" = 'authorized_keys' ] ; then
274 # FIXME: needs to apply extra options for authorized_keys
276 gpg2authorized_keys "$keyID" "$userID" >> \
277 "$cacheDir"/"$userIDHash"."$pubKeyID"
282 # echo the path to the key cache file
283 echo "$cacheDir"/"$userIDHash"."$pubKeyID"
286 # update the cache for userid, and prompt to add file to
287 # authorized_user_ids file if the userid is found in gpg
288 # and not already in file.
297 log "processing userid: '$userID'"
299 keyCachePath=$(process_user_id "$userID" "$cacheDir")
301 if [ -z "$keyCachePath" ] ; then
304 if ! grep -q "^${userID}\$" "$AUTHORIZED_USER_IDS" ; then
305 read -p "user ID not currently authorized. authorize? [Y|n]: " OK; OK=${OK:=Y}
306 if [ ${OK/y/Y} = 'Y' ] ; then
307 log -n "adding user ID to authorized_user_ids file... "
308 echo "$userID" >> "$AUTHORIZED_USER_IDS"
311 log "authorized_user_ids file untouched."
316 # remove a userid from the authorized_user_ids file
322 log "processing userid: '$userID'"
324 if ! grep -q "^${userID}\$" "$AUTHORIZED_USER_IDS" ; then
325 log "user ID not currently authorized."
329 log -n "removing user ID '$userID'... "
330 grep -v "$userID" "$AUTHORIZED_USER_IDS" | sponge "$AUTHORIZED_USER_IDS"
334 # process a host for addition to a known_host file
343 log "processing host: '$host'"
345 keyCachePath=$(process_user_id "ssh://${host}" "$cacheDir")
347 ssh-keygen -R "$host" -f "$USER_KNOWN_HOSTS"
348 cat "$keyCachePath" >> "$USER_KNOWN_HOSTS"
352 # process known_hosts file
353 # go through line-by-line, extract each host, and process with the
354 # host processing function
355 process_known_hosts() {
364 # take all the hosts from the known_hosts file (first field),
365 # grep out all the hashed hosts (lines starting with '|')...
366 cut -d ' ' -f 1 "$knownHosts" | \
368 while IFS=, read -r -a hosts ; do
369 # ...and process each host
370 for host in ${hosts[*]} ; do
371 process_host "$host" "$cacheDir"
376 # update an authorized_keys file after first processing the
377 # authorized_user_ids file
378 update_authorized_keys() {
379 local msAuthorizedKeys
380 local userAuthorizedKeys
383 msAuthorizedKeys="$1"
384 userAuthorizedKeys="$2"
387 process_authorized_ids "$AUTHORIZED_USER_IDS" "$cacheDir"
389 # write output key file
390 log "writing monkeysphere authorized_keys file... "
391 touch "$msAuthorizedKeys"
392 if [ "$(ls "$cacheDir")" ] ; then
393 log -n "adding gpg keys... "
394 cat "$cacheDir"/* > "$msAuthorizedKeys"
397 log "no gpg keys to add."
399 if [ "$userAuthorizedKeys" -a -s "$userAuthorizedKeys" ] ; then
400 log -n "adding user authorized_keys file... "
401 cat "$userAuthorizedKeys" >> "$msAuthorizedKeys"
404 log "monkeysphere authorized_keys file generated:"
405 log "$msAuthorizedKeys"
408 # process an authorized_*_ids file
409 # go through line-by-line, extract each userid, and process
410 process_authorized_ids() {
418 # clean out keys file and remake keys directory
422 # loop through all user ids in file
423 # FIXME: needs to handle authorized_keys options
424 cat "$authorizedIDs" | meat | \
425 while read -r userID ; do
427 log "processing userid: '$userID'"
428 process_user_id "$userID" "$cacheDir" > /dev/null
432 # EXPERIMENTAL (unused) process userids found in authorized_keys file
433 # go through line-by-line, extract monkeysphere userids from comment
434 # fields, and process each userid
435 process_authorized_keys() {
443 # take all the monkeysphere userids from the authorized_keys file
444 # comment field (third field) that starts with "MonkeySphere uid:"
445 # FIXME: needs to handle authorized_keys options (field 0)
446 cat "$authorizedKeys" | \
447 while read -r options keytype key comment ; do
448 # if the comment field is empty, assume the third field was
450 if [ -z "$comment" ] ; then
453 if ! echo "$comment" | grep '^MonkeySphere userID:.*$' ; then
456 userID=$(echo "$comment" | sed -e "/^MonkeySphere userID://")
457 if [ -z "$userID" ] ; then
461 log "processing userid: '$userID'"
462 process_user_id "$userID" "$cacheDir" > /dev/null
466 # retrieve key from web of trust, and set owner trust to "full"
469 # get the key from the key server
470 if ! gpg --keyserver "$KEYSERVER" --recv-key "$keyID" ; then
471 log "could not retrieve key '$keyID'"
475 # get key fingerprint
476 fingerprint=$(get_key_fingerprint "$keyID")
478 # attach a "non-exportable" signature to the key
479 # this is required for the key to have any validity at all
480 # the 'y's on stdin indicates "yes, i really want to sign"
481 echo -e 'y\ny' | gpg --lsign-key --command-fd 0 "$fingerprint"
483 # import "full" trust for fingerprint into gpg
484 echo ${fingerprint}:5: | gpg --import-ownertrust
486 log "owner trust updated."
488 failure "there was a problem changing owner trust."
492 # publish server key to keyserver
493 publish_server_key() {
494 read -p "really publish key to $KEYSERVER? [y|N]: " OK; OK=${OK:=N}
495 if [ ${OK/y/Y} != 'Y' ] ; then
500 # FIXME: need to figure out better way to identify host key
501 # dummy command so as not to publish fakes keys during testing
503 #gpg --send-keys --keyserver "$KEYSERVER" $(hostname -f)
504 echo "NOT PUBLISHED: gpg --send-keys --keyserver $KEYSERVER $(hostname -f)"