3 # Shared bash 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 LIB="/var/lib/monkeysphere"
19 ########################################################################
26 # write output to stdout
32 # write output to stderr
38 # cut out all comments(#) and blank lines from standard input
40 grep -v -e "^[[:space:]]*#" -e '^$'
43 # cut a specified line from standard input
45 head --line="$1" | tail -1
48 # retrieve all keys with given user id from keyserver
49 # FIXME: need to figure out how to retrieve all matching keys
55 gpg --quiet --batch --command-fd 0 --with-colons \
56 --keyserver "$KEYSERVER" \
57 --search ="$id" >/dev/null 2>&1
60 # check that characters are in a string (in an AND fashion).
61 # used for checking key capability
62 # check_capability capability a [b...]
71 if echo "$capability" | grep -q -v "$capcheck" ; then
78 # convert escaped characters from gpg output back into original
80 # FIXME: undo all escape character translation in with-colons gpg output
82 echo "$1" | sed 's/\\x3a/:/'
85 # convert key from gpg to ssh known_hosts format
91 host=$(echo "$2" | sed -e "s|ssh://||")
93 # NOTE: it seems that ssh-keygen -R removes all comment fields from
94 # all lines in the known_hosts file. why?
95 # NOTE: just in case, the COMMENT can be matched with the
97 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
99 gpg --export "$keyID" | \
100 openpgp2ssh "$keyID" | tr -d '\n'
101 echo "MonkeySphere${DATE}"
104 # convert key from gpg to ssh authorized_keys format
105 gpg2authorized_keys() {
112 echo -n "MonkeySphere${DATE}:${userID}"
113 gpg --export "$keyID" | \
117 # userid and key policy checking
118 # the following checks policy on the returned keys
119 # - checks that full key has appropriate valididy (u|f)
120 # - checks key has specified capability (REQUIRED_KEY_CAPABILITY)
121 # - checks that particular desired user id has appropriate validity
122 # see /usr/share/doc/gnupg/DETAILS.gz
123 # expects global variable: "MODE"
127 local requiredPubCapability
145 requiredPubCapability=$(echo "$REQUIRED_KEY_CAPABILITY" | tr "[:lower:]" "[:upper:]")
147 # fetch keys from keyserver, return 1 if none found
148 gpg_fetch_keys "$userID" || return 1
150 # output gpg info for (exact) userid and store
151 gpgOut=$(gpg --fixed-list-mode --list-key --with-colons \
152 ="$userID" 2> /dev/null)
154 # return 1 if there only "tru" lines are output from gpg
155 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
156 loge " key not found."
160 # loop over all lines in the gpg output and process.
161 # need to do it this way (as opposed to "while read...") so that
162 # variables set in loop will be visible outside of loop
163 for line in $(seq 1 $(echo "$gpgOut" | wc -l)) ; do
165 # read the contents of the line
166 type=$(echo "$gpgOut" | cutline "$line" | cut -d: -f1)
167 validity=$(echo "$gpgOut" | cutline "$line" | cut -d: -f2)
168 keyid=$(echo "$gpgOut" | cutline "$line" | cut -d: -f5)
169 uidfpr=$(echo "$gpgOut" | cutline "$line" | cut -d: -f10)
170 capability=$(echo "$gpgOut" | cutline "$line" | cut -d: -f12)
172 # process based on record type
174 'pub') # primary keys
175 # new key, wipe the slate
183 # check primary key validity
184 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
185 loge " unacceptable primary key validity ($validity)."
188 # check capability is not Disabled...
189 if check_capability "$capability" 'D' ; then
190 loge " key disabled."
193 # check overall key capability
194 # must be Encryption and Authentication
195 if ! check_capability "$capability" $requiredPubCapability ; then
196 loge " unacceptable primary key capability ($capability)."
200 # mark if primary key is acceptable
203 # add primary key ID to key list if it has required capability
204 if check_capability "$capability" $REQUIRED_KEY_CAPABILITY ; then
205 keyIDs[${#keyIDs[*]}]="$keyid"
209 # check key ok and we have key fingerprint
210 if [ -z "$keyOK" ] ; then
214 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
217 # check the uid matches
218 if [ "$(unescape "$uidfpr")" != "$userID" ] ; then
222 # mark if uid acceptable
226 # add sub key ID to key list if it has required capability
227 if check_capability "$capability" $REQUIRED_KEY_CAPABILITY ; then
228 keyIDs[${#keyIDs[*]}]="$keyid"
234 # hash userid for cache file name
235 userIDHash=$(echo "$userID" | sha1sum | awk '{ print $1 }')
237 # touch/clear key cache file
238 # (will be left empty if there are noacceptable keys)
239 > "$cacheDir"/"$userIDHash"."$pubKeyID"
241 # for each acceptable key, write an ssh key line to the
243 if [ "$keyOK" -a "$uidOK" -a "${keyIDs[*]}" ] ; then
244 for keyID in ${keyIDs[@]} ; do
245 loge " acceptable key/uid found."
247 if [ "$MODE" = 'known_hosts' ] ; then
249 gpg2known_hosts "$keyID" "$userID" >> \
250 "$cacheDir"/"$userIDHash"."$pubKeyID"
251 # hash the cache file if specified
252 if [ "$HASH_KNOWN_HOSTS" ] ; then
253 ssh-keygen -H -f "$cacheDir"/"$userIDHash"."$pubKeyID" > /dev/null 2>&1
254 rm "$cacheDir"/"$userIDHash"."$pubKeyID".old
256 elif [ "$MODE" = 'authorized_keys' ] ; then
258 # FIXME: needs to apply extra options for authorized_keys
260 gpg2authorized_keys "$keyID" "$userID" >> \
261 "$cacheDir"/"$userIDHash"."$pubKeyID"
266 # echo the path to the key cache file
267 echo "$cacheDir"/"$userIDHash"."$pubKeyID"
270 # process a host for addition to a known_host file
274 local hostKeyCachePath
279 log "processing host: '$host'"
281 hostKeyCachePath=$(process_user_id "ssh://${host}" "$cacheDir")
283 ssh-keygen -R "$host" -f "$USER_KNOWN_HOSTS"
284 cat "$hostKeyCachePath" >> "$USER_KNOWN_HOSTS"
288 # process known_hosts file
289 # go through line-by-line, extract each host, and process with the
290 # host processing function
291 process_known_hosts() {
300 # take all the hosts from the known_hosts file (first field),
301 # grep out all the hashed hosts (lines starting with '|')
302 cut -d ' ' -f 1 "$knownHosts" | \
304 while IFS=, read -r -a hosts ; do
306 for host in ${hosts[*]} ; do
307 process_host "$host" "$cacheDir"
312 # update an authorized_keys file after first processing the
313 # authorized_user_ids file
314 update_authorized_keys() {
316 local msAuthorizedKeys
317 local userAuthorizedKeys
320 msAuthorizedKeys="$2"
321 userAuthorizedKeys="$3"
323 process_authorized_ids "$AUTHORIZED_USER_IDS" "$cacheDir"
325 # write output key file
326 log "writing monkeysphere authorized_keys file... "
327 touch "$msAuthorizedKeys"
328 if [ "$(ls "$cacheDir")" ] ; then
329 log -n "adding gpg keys... "
330 cat "$cacheDir"/* > "$msAuthorizedKeys"
333 log "no gpg keys to add."
335 if [ "$userAuthorizedKeys" -a -s "$userAuthorizedKeys" ] ; then
336 log -n "adding user authorized_keys file... "
337 cat "$userAuthorizedKeys" >> "$msAuthorizedKeys"
340 log "monkeysphere authorized_keys file generated: $msAuthorizedKeys"
343 # process an authorized_*_ids file
344 # go through line-by-line, extract each userid, and process
345 process_authorized_ids() {
353 # clean out keys file and remake keys directory
357 # loop through all user ids in file
358 # FIXME: needs to handle authorized_keys options
359 cat "$authorizedIDs" | meat | \
360 while read -r userID ; do
362 log "processing userid: '$userID'"
363 process_user_id "$userID" "$cacheDir" > /dev/null
367 # EXPERIMENTAL (unused) process userids found in authorized_keys file
368 # go through line-by-line, extract monkeysphere userids from comment
369 # fields, and process each userid
370 process_userids_from_authorized_keys() {
378 # take all the monkeysphere userids from the authorized_keys file
379 # comment field (third field) that starts with "MonkeySphere uid:"
380 # FIXME: needs to handle authorized_keys options (field 0)
381 cat "$authorizedKeys" | \
382 while read -r options keytype key comment ; do
383 # if the comment field is empty, assume the third field was
385 if [ -z "$comment" ] ; then
388 if ! echo "$comment" | grep '^MonkeySphere userID:.*$' ; then
391 userID=$(echo "$comment" | sed -e "/^MonkeySphere userID://")
392 if [ -z "$userID" ] ; then
396 log "processing userid: '$userID'"
397 process_user_id "$userID" "$cacheDir" > /dev/null
401 # update the cache for userid, and prompt to add file to
402 # authorized_user_ids file if the userid is found in gpg
403 # and not already in file.
412 log "processing userid: '$userID'"
413 userIDKeyCache=$(process_user_id "$userID" "$cacheDir")
414 if [ -z "$userIDKeyCache" ] ; then
417 if ! grep -q "^${userID}\$" "$AUTHORIZED_USER_IDS" ; then
418 echo "the following userid is not in the authorized_user_ids file:"
420 read -p "would you like to add? [Y|n]: " OK; OK=${OK:=Y}
421 if [ ${OK/y/Y} = 'Y' ] ; then
422 log -n " adding userid to authorized_user_ids file... "
423 echo "$userID" >> "$AUTHORIZED_USER_IDS"
429 # retrieve key from web of trust, and set owner trust to "full"
432 # get the key from the key server
433 gpg --keyserver "$KEYSERVER" --recv-key "$keyID" || failure "could not retrieve key '$keyID'"
435 # edit the key to change trust
436 # FIXME: need to figure out how to automate this,
437 # in a batch mode or something.
438 gpg --edit-key "$keyID"