- # hash userid for cache file name
- userIDHash=$(echo "$userID" | sha1sum | awk '{ print $1 }')
-
- # touch/clear key cache file
- # (will be left empty if there are noacceptable keys)
- > "$cacheDir"/"$userIDHash"."$pubKeyID"
-
- # for each acceptable key, write an ssh key line to the
- # key cache file
- if [ "$keyOK" -a "$uidOK" -a "${keyIDs[*]}" ] ; then
- for keyID in ${keyIDs[@]} ; do
- log " acceptable key/uid found."
-
- if [ "$MODE" = 'known_hosts' ] ; then
- # export the key
- gpg2known_hosts "$keyID" "$userID" >> \
- "$cacheDir"/"$userIDHash"."$pubKeyID"
- # hash the cache file if specified
- if [ "$HASH_KNOWN_HOSTS" ] ; then
- ssh-keygen -H -f "$cacheDir"/"$userIDHash"."$pubKeyID" > /dev/null 2>&1
- rm "$cacheDir"/"$userIDHash"."$pubKeyID".old
- fi
- elif [ "$MODE" = 'authorized_keys' ] ; then
- # export the key
- # FIXME: needs to apply extra options for authorized_keys
- # lines if specified
- gpg2authorized_keys "$keyID" "$userID" >> \
- "$cacheDir"/"$userIDHash"."$pubKeyID"
+# process a single host in the known_host file
+process_host_known_hosts() {
+ local host
+ local userID
+ local ok
+ local keyid
+ local idOK
+ local idRemoved
+ local tmpfile
+
+ host="$1"
+
+ log "processing host: $host"
+
+ userID="ssh://${host}"
+
+ for line in $(process_user_id "ssh://${host}") ; do
+ ok=$(echo "$line" | cut -d: -f1)
+ keyid=$(echo "$line" | cut -d: -f2)
+
+ sshKey=$(gpg2ssh "$keyid")
+
+ # remove the old host key line, and note if removed
+ remove_line "$KNOWN_HOSTS" "$sshKey" && idRemoved=true
+
+ # if key OK, add new host line
+ if [ "$ok" -eq '0' ] ; then
+ # hash if specified
+ if [ "$HASH_KNOWN_HOSTS" = 'true' ] ; then
+ # FIXME: this is really hackish cause ssh-keygen won't
+ # hash from stdin to stdout
+ tmpfile=$(mktemp)
+ ssh2known_hosts "$host" "$sshKey" > "$tmpfile"
+ ssh-keygen -H -f "$tmpfile" 2> /dev/null
+ cat "$tmpfile" >> "$KNOWN_HOSTS"
+ rm -f "$tmpfile" "${tmpfile}.old"
+ else
+ ssh2known_hosts "$host" "$sshKey" >> "$KNOWN_HOSTS"