summaryrefslogtreecommitdiff
path: root/src/share/m/update_known_hosts
blob: 737666de2d5201c651c878f40f7449636444f570 (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere update_known_hosts subcommand
  4. #
  5. # The monkeysphere scripts are written by:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Jamie McClelland <jm@mayfirst.org>
  8. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  9. #
  10. # They are Copyright 2010, and are all released under the GPL, version
  11. # 3 or later.
  12. # update the known_hosts file for a set of hosts listed on command
  13. # line
  14. update_known_hosts() {
  15. local returnCode=0
  16. local fileCheck
  17. local host
  18. local newUmask
  19. # touch the known_hosts file so that the file permission check
  20. # below won't fail upon not finding the file
  21. if [ ! -f "$KNOWN_HOSTS" ]; then
  22. # make sure to create any files or directories with the appropriate write bits turned off:
  23. newUmask=$(printf "%04o" $(( 0$(umask) | 0022 )) )
  24. [ -d $(dirname "$KNOWN_HOSTS") ] \
  25. || (umask "$newUmask" && mkdir -p -m 0700 $(dirname "$KNOWN_HOSTS") ) \
  26. || failure "Could not create path to known_hosts file '$KNOWN_HOSTS'"
  27. # make sure to create this file with the appropriate bits turned off:
  28. (umask "$newUmask" && touch "$KNOWN_HOSTS") \
  29. || failure "Unable to create known_hosts file '$KNOWN_HOSTS'"
  30. fi
  31. check_key_file_permissions $(whoami) "$KNOWN_HOSTS" \
  32. || failure "Bad permissions governing known_hosts file '$KNOWN_HOSTS'"
  33. lock create "$KNOWN_HOSTS"
  34. # FIXME: we're discarding any pre-existing EXIT trap; is this bad?
  35. trap "lock remove $KNOWN_HOSTS" EXIT
  36. tmpFile=$(mktemp "${KNOWN_HOSTS}.monkeysphere.XXXXXX")
  37. trap "lock remove $KNOWN_HOSTS; rm -f $tmpFile" EXIT
  38. cat "$KNOWN_HOSTS" >"$tmpFile"
  39. for host ; do
  40. FILE_TYPE='known_hosts' process_keys_for_file "$tmpFile" "ssh://${host}"
  41. lock touch "$KNOWN_HOSTS"
  42. done
  43. if [ "$(file_hash "$KNOWN_HOSTS")" != "$(file_hash "$tmpFile")" ] ; then
  44. mv -f "$tmpFile" "$KNOWN_HOSTS"
  45. log debug "known_hosts file updated."
  46. else
  47. rm -f "$tmpFile"
  48. fi
  49. lock remove "$KNOWN_HOSTS"
  50. trap - EXIT
  51. }
  52. # process hosts from a known_hosts file
  53. process_known_hosts() {
  54. local hosts
  55. if [ ! -e "$KNOWN_HOSTS" ] ; then
  56. failure "known_hosts file '$KNOWN_HOSTS' does not exist."
  57. fi
  58. log debug "processing known_hosts file:"
  59. log debug " $KNOWN_HOSTS"
  60. hosts=$(meat "$KNOWN_HOSTS" | cut -d ' ' -f 1 | grep -v '^|.*$' | tr , ' ' | tr '\n' ' ')
  61. if [ -z "$hosts" ] ; then
  62. log debug "no hosts to process."
  63. return
  64. fi
  65. # take all the hosts from the known_hosts file (first
  66. # field), grep out all the hashed hosts (lines starting
  67. # with '|')...
  68. update_known_hosts $hosts
  69. }