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/${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 # process authorized_keys file
298 # go through line-by-line, extract monkeysphere userids from comment
299 # fields, and process each userid
300 process_authorized_keys() {
308 # take all the monkeysphere userids from the authorized_keys file
309 # comment field (third field) that starts with "MonkeySphere uid:"
310 # FIXME: needs to handle authorized_keys options (field 0)
311 cat "$authorizedKeys" | \
312 while read -r options keytype key comment ; do
313 # if the comment field is empty, assume the third field was
315 if [ -z "$comment" ] ; then
318 if ! echo "$comment" | grep '^MonkeySphere userID:.*$' ; then
321 userID=$(echo "$comment" | sed -e "/^MonkeySphere userID://")
322 if [ -z "$userID" ] ; then
326 log "processing userid: '$userID'"
327 process_user_id "$userID" "$cacheDir" > /dev/null
331 # process an authorized_*_ids file
332 # go through line-by-line, extract each userid, and process
333 process_authorized_ids() {
341 # clean out keys file and remake keys directory
345 # loop through all user ids in file
346 # FIXME: needs to handle authorized_keys options
347 cat "$authorizedIDs" | meat | \
348 while read -r userID ; do
350 log "processing userid: '$userID'"
351 process_user_id "$userID" "$cacheDir" > /dev/null