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 # stand in until we get dkg's gpg2ssh program
94 if [ "$MODE" = 'authorized_keys' ] ; then
95 gpgkey2ssh "$keyID" | sed -e "s/COMMENT/MonkeySphere userID: ${userID}/"
97 # NOTE: it seems that ssh-keygen -R removes all comment fields from
98 # all lines in the known_hosts file. why?
99 # NOTE: just in case, the COMMENT can be matched with the
101 # '^MonkeySphere[[:digit:]]{4}(-[[:digit:]]{2}){2}T[[:digit:]]{2}(:[[:digit:]]{2}){2}$'
102 elif [ "$MODE" = 'known_hosts' ] ; then
103 host=$(echo "$userID" | sed -e "s|ssh://||")
104 echo -n "$host "; gpgkey2ssh "$keyID" | sed -e "s/COMMENT/MonkeySphere${DATE}/"
108 # userid and key policy checking
109 # the following checks policy on the returned keys
110 # - checks that full key has appropriate valididy (u|f)
111 # - checks key has specified capability (REQUIRED_KEY_CAPABILITY)
112 # - checks that particular desired user id has appropriate validity
113 # see /usr/share/doc/gnupg/DETAILS.gz
114 # expects global variable: "MODE"
118 local requiredPubCapability
136 requiredPubCapability=$(echo "$REQUIRED_KEY_CAPABILITY" | tr "[:lower:]" "[:upper:]")
138 # fetch keys from keyserver, return 1 if none found
139 gpg_fetch_keys "$userID" || return 1
141 # output gpg info for (exact) userid and store
142 gpgOut=$(gpg --fixed-list-mode --list-key --with-colons \
143 ="$userID" 2> /dev/null)
145 # return 1 if there only "tru" lines are output from gpg
146 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
147 loge " key not found."
151 # loop over all lines in the gpg output and process.
152 # need to do it this way (as opposed to "while read...") so that
153 # variables set in loop will be visible outside of loop
154 for line in $(seq 1 $(echo "$gpgOut" | wc -l)) ; do
156 # read the contents of the line
157 type=$(echo "$gpgOut" | cutline "$line" | cut -d: -f1)
158 validity=$(echo "$gpgOut" | cutline "$line" | cut -d: -f2)
159 keyid=$(echo "$gpgOut" | cutline "$line" | cut -d: -f5)
160 uidfpr=$(echo "$gpgOut" | cutline "$line" | cut -d: -f10)
161 capability=$(echo "$gpgOut" | cutline "$line" | cut -d: -f12)
163 # process based on record type
165 'pub') # primary keys
166 # new key, wipe the slate
174 # check primary key validity
175 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
176 loge " unacceptable primary key validity ($validity)."
179 # check capability is not Disabled...
180 if check_capability "$capability" 'D' ; then
181 loge " key disabled."
184 # check overall key capability
185 # must be Encryption and Authentication
186 if ! check_capability "$capability" $requiredPubCapability ; then
187 loge " unacceptable primary key capability ($capability)."
191 # mark if primary key is acceptable
194 # add primary key ID to key list if it has required capability
195 if check_capability "$capability" $REQUIRED_KEY_CAPABILITY ; then
196 keyIDs[${#keyIDs[*]}]="$keyid"
200 # check key ok and we have key fingerprint
201 if [ -z "$keyOK" ] ; then
205 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
208 # check the uid matches
209 if [ "$(unescape "$uidfpr")" != "$userID" ] ; then
213 # mark if uid acceptable
217 # add sub key ID to key list if it has required capability
218 if check_capability "$capability" $REQUIRED_KEY_CAPABILITY ; then
219 keyIDs[${#keyIDs[*]}]="$keyid"
225 # hash userid for cache file name
226 userIDHash=$(echo "$userID" | sha1sum | awk '{ print $1 }')
228 # touch/clear key cache file
229 # (will be left empty if there are noacceptable keys)
230 > "$cacheDir"/"$userIDHash"."$pubKeyID"
232 # for each acceptable key, write an ssh key line to the
234 if [ "$keyOK" -a "$uidOK" -a "${keyIDs[*]}" ] ; then
235 for keyID in ${keyIDs[@]} ; do
236 loge " acceptable key/uid found."
238 # export the key with gpg2ssh
239 # FIXME: needs to apply extra options for authorized_keys
241 gpg2ssh_tmp "$keyID" "$userID" >> "$cacheDir"/"$userIDHash"."$pubKeyID"
243 # hash the cache file if specified
244 if [ "$MODE" = 'known_hosts' -a "$HASH_KNOWN_HOSTS" ] ; then
245 ssh-keygen -H -f "$cacheDir"/"$userIDHash"."$pubKeyID" > /dev/null 2>&1
246 rm "$cacheDir"/"$userIDHash"."$pubKeyID".old
251 # echo the path to the key cache file
252 echo "$cacheDir"/"$userIDHash"."$pubKeyID"
255 # process a host for addition to a known_host file
259 local hostKeyCachePath
264 log "processing host: '$host'"
266 hostKeyCachePath=$(process_user_id "ssh://${host}" "$cacheDir")
268 ssh-keygen -R "$host" -f "$USER_KNOWN_HOSTS"
269 cat "$hostKeyCachePath" >> "$USER_KNOWN_HOSTS"
273 # process known_hosts file
274 # go through line-by-line, extract each host, and process with the
275 # host processing function
276 process_known_hosts() {
285 # take all the hosts from the known_hosts file (first field),
286 # grep out all the hashed hosts (lines starting with '|')
287 cut -d ' ' -f 1 "$knownHosts" | \
289 while IFS=, read -r -a hosts ; do
291 for host in ${hosts[*]} ; do
292 process_host "$host" "$cacheDir"
297 # update an authorized_keys file after first processing the
298 # authorized_user_ids file
299 update_authorized_keys() {
301 local msAuthorizedKeys
302 local userAuthorizedKeys
305 msAuthorizedKeys="$2"
306 userAuthorizedKeys="$3"
308 process_authorized_ids "$AUTHORIZED_USER_IDS" "$cacheDir"
310 # write output key file
311 log "writing monkeysphere authorized_keys file... "
312 touch "$msAuthorizedKeys"
313 if [ "$(ls "$cacheDir")" ] ; then
314 log -n "adding gpg keys... "
315 cat "$cacheDir"/* > "$msAuthorizedKeys"
318 log "no gpg keys to add."
320 if [ "$userAuthorizedKeys" -a -s "$userAuthorizedKeys" ] ; then
321 log -n "adding user authorized_keys file... "
322 cat "$userAuthorizedKeys" >> "$msAuthorizedKeys"
325 log "monkeysphere authorized_keys file generated: $msAuthorizedKeys"
328 # process an authorized_*_ids file
329 # go through line-by-line, extract each userid, and process
330 process_authorized_ids() {
338 # clean out keys file and remake keys directory
342 # loop through all user ids in file
343 # FIXME: needs to handle authorized_keys options
344 cat "$authorizedIDs" | meat | \
345 while read -r userID ; do
347 log "processing userid: '$userID'"
348 process_user_id "$userID" "$cacheDir" > /dev/null
352 # EXPERIMENTAL (unused) process userids found in authorized_keys file
353 # go through line-by-line, extract monkeysphere userids from comment
354 # fields, and process each userid
355 process_userids_from_authorized_keys() {
363 # take all the monkeysphere userids from the authorized_keys file
364 # comment field (third field) that starts with "MonkeySphere uid:"
365 # FIXME: needs to handle authorized_keys options (field 0)
366 cat "$authorizedKeys" | \
367 while read -r options keytype key comment ; do
368 # if the comment field is empty, assume the third field was
370 if [ -z "$comment" ] ; then
373 if ! echo "$comment" | grep '^MonkeySphere userID:.*$' ; then
376 userID=$(echo "$comment" | sed -e "/^MonkeySphere userID://")
377 if [ -z "$userID" ] ; then
381 log "processing userid: '$userID'"
382 process_user_id "$userID" "$cacheDir" > /dev/null
386 # update the cache for userid, and prompt to add file to
387 # authorized_user_ids file if the userid is found in gpg
388 # and not already in file.
397 log "processing userid: '$userID'"
398 userIDKeyCache=$(process_user_id "$userID" "$cacheDir")
399 if [ -z "$userIDKeyCache" ] ; then
402 if ! grep -q "^${userID}\$" "$AUTHORIZED_USER_IDS" ; then
403 echo "the following userid is not in the authorized_user_ids file:"
405 read -p "would you like to add? [Y|n]: " OK; OK=${OK:=Y}
406 if [ ${OK/y/Y} = 'Y' ] ; then
407 log -n " adding userid to authorized_user_ids file... "
408 echo "$userID" >> "$AUTHORIZED_USER_IDS"
414 # retrieve key from web of trust, and set owner trust to "full"
417 # get the key from the key server
418 gpg --keyserver "$KEYSERVER" --recv-key "$keyID" || failure "could not retrieve key '$keyID'"
420 # edit the key to change trust
421 # FIXME: need to figure out how to automate this,
422 # in a batch mode or something.
423 gpg --edit-key "$keyID"