add file locking to known_hosts and authorized_keys
authorJameson Graef Rollins <jrollins@phys.columbia.edu>
Fri, 20 Jun 2008 04:44:36 +0000 (00:44 -0400)
committerJameson Graef Rollins <jrollins@phys.columbia.edu>
Fri, 20 Jun 2008 04:44:36 +0000 (00:44 -0400)
debian/changelog
src/common
src/monkeysphere

index bd12e1a741b00437e3ea4b8833699ce65dbe3fd3..2133d2d6018404e38e61a918d9ce602b5d37ec25 100644 (file)
@@ -10,8 +10,9 @@ monkeysphere (0.2-1) UNRELEASED; urgency=low
   * Remove {update,remove}-userids functions, since we decided they
     weren't useful enough to be worth maintaining.
   * Better handling of unknown users in server update-users
+  * Add file locking when modifying known_hosts or authorized_keys
 
- -- Jameson Graef Rollins <jrollins@phys.columbia.edu>  Thu, 19 Jun 2008 18:08:57 -0400
+ -- Jameson Graef Rollins <jrollins@phys.columbia.edu>  Fri, 20 Jun 2008 00:43:44 -0400
 
 monkeysphere (0.1-1) experimental; urgency=low
 
index e98f1bcb35369d04af794d5eba49c6aeaf7cde51..7df6908b348bc88f1f805de47f426179017cd667 100644 (file)
@@ -376,62 +376,79 @@ process_user_id() {
     done
 }
 
-# process a host in known_host file
-process_host_known_hosts() {
+# process hosts in the known_host file
+process_hosts_known_hosts() {
     local host
     local userID
     local ok
     local keyid
     local tmpfile
 
-    host="$1"
-    userID="ssh://${host}"
-
-    log "processing host: $host"
-
-    process_user_id "ssh://${host}" | \
-    while read -r ok keyid ; do
-       sshKey=$(gpg2ssh "$keyid")
-       # remove the old host key line
-       remove_line "$KNOWN_HOSTS" "$sshKey"
-       # 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"
+    # create a lockfile on known_hosts
+    lockfile-create "$KNOWN_HOSTS"
+
+    for host ; do
+       log "processing host: $host"
+
+       userID="ssh://${host}"
+
+       process_user_id "ssh://${host}" | \
+       while read -r ok keyid ; do
+           sshKey=$(gpg2ssh "$keyid")
+           # remove the old host key line
+           remove_line "$KNOWN_HOSTS" "$sshKey"
+           # 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"
+               fi
            fi
-       fi
+       done
+       # touch the lockfile, for good measure.
+       lockfile-touch --oneshot "$KNOWN_HOSTS"
     done
+
+    # remove the lockfile
+    lockfile-remove "$KNOWN_HOSTS"
 }
 
-# process a uid in an authorized_keys file
-process_uid_authorized_keys() {
+# process uids for the authorized_keys file
+process_uids_authorized_keys() {
     local userID
     local ok
     local keyid
 
-    userID="$1"
+    # create a lockfile on authorized_keys
+    lockfile-create "$AUTHORIZED_KEYS"
 
-    log "processing user ID: $userID"
+    for userID ; do
+       log "processing user ID: $userID"
 
-    process_user_id "$userID" | \
-    while read -r ok keyid ; do
-       sshKey=$(gpg2ssh "$keyid")
-       # remove the old host key line
-       remove_line "$AUTHORIZED_KEYS" "$sshKey"
-       # if key OK, add new host line
-       if [ "$ok" -eq '0' ] ; then
-           ssh2authorized_keys "$userID" "$sshKey" >> "$AUTHORIZED_KEYS"
-       fi
+       process_user_id "$userID" | \
+       while read -r ok keyid ; do
+           sshKey=$(gpg2ssh "$keyid")
+           # remove the old host key line
+           remove_line "$AUTHORIZED_KEYS" "$sshKey"
+           # if key OK, add new host line
+           if [ "$ok" -eq '0' ] ; then
+               ssh2authorized_keys "$userID" "$sshKey" >> "$AUTHORIZED_KEYS"
+           fi
+       done
+       # touch the lockfile, for good measure.
+       lockfile-touch --oneshot "$AUTHORIZED_KEYS"
     done
+
+    # remove the lockfile
+    lockfile-remove "$AUTHORIZED_KEYS"
 }
 
 # process known_hosts file
@@ -446,10 +463,7 @@ process_known_hosts() {
     cat "$KNOWN_HOSTS" | meat | \
        cut -d ' ' -f 1 | grep -v '^|.*$' | \
     while IFS=, read -r -a hosts ; do
-       # and process each host
-       for host in ${hosts[*]} ; do
-           process_host_known_hosts "$host"
-       done
+       process_hosts_known_hosts ${hosts[@]}
     done
 }
 
@@ -461,7 +475,7 @@ process_authorized_user_ids() {
 
     cat "$authorizedUserIDs" | meat | \
     while read -r userid ; do
-       process_uid_authorized_keys "$userid"
+       process_uids_authorized_keys "$userid"
     done
 }
 
index a433701a1c6fb4da462e9b29bdd6bae6e4d644db..58f0fdc632edaeb8f7aa679b24dbfa6e6c81809c 100755 (executable)
@@ -140,12 +140,9 @@ case $COMMAND in
         # if hosts are specified on the command line, process just
         # those hosts
        if [ "$1" ] ; then
-            for host ; do
-               process_host_known_hosts "$host"
-           done
-           log "known_hosts file updated."
+           process_hosts_known_hosts "$@"
 
-        # otherwise, if no hosts are specified, process every user
+        # otherwise, if no hosts are specified, process every host
         # in the user's known_hosts file
        else
            if [ ! -s "$KNOWN_HOSTS" ] ; then
@@ -153,8 +150,9 @@ case $COMMAND in
            fi
            log "processing known_hosts file..."
            process_known_hosts
-           log "known_hosts file updated."
        fi
+
+       log "known_hosts file updated."
        ;;
 
     'update-authorized_keys'|'update-authorized-keys'|'a')