summaryrefslogtreecommitdiff
path: root/src/share/m/update_known_hosts
blob: 57176b8f8346041157ad7e0a1792c74f6931e808 (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. touch_key_file_or_fail "$KNOWN_HOSTS"
  22. check_key_file_permissions $(whoami) "$KNOWN_HOSTS" \
  23. || failure "Bad permissions governing known_hosts file $KNOWN_HOSTS"
  24. lock create "$KNOWN_HOSTS"
  25. # FIXME: we're discarding any pre-existing EXIT trap; is this bad?
  26. trap "log debug TRAP; lock remove $KNOWN_HOSTS" EXIT
  27. tmpFile=$(mktemp "${KNOWN_HOSTS}.monkeysphere.XXXXXX")
  28. trap "log debug TRAP; lock remove $KNOWN_HOSTS; rm -f $tmpFile" EXIT
  29. cat "$KNOWN_HOSTS" >"$tmpFile"
  30. for host ; do
  31. FILE_TYPE='known_hosts' process_keys_for_file "$tmpFile" "ssh://${host}"
  32. lock touch "$KNOWN_HOSTS"
  33. done
  34. if [ "$(file_hash "$KNOWN_HOSTS")" != "$(file_hash "$tmpFile")" ] ; then
  35. mv -f "$tmpFile" "$KNOWN_HOSTS"
  36. log debug "known_hosts file updated."
  37. else
  38. rm -f "$tmpFile"
  39. fi
  40. lock remove "$KNOWN_HOSTS"
  41. trap - EXIT
  42. }
  43. # process hosts from a known_hosts file
  44. process_known_hosts() {
  45. local hosts
  46. if [ ! -e "$KNOWN_HOSTS" ] ; then
  47. failure "known_hosts file '$KNOWN_HOSTS' does not exist."
  48. fi
  49. log debug "processing known_hosts file:"
  50. log debug " $KNOWN_HOSTS"
  51. hosts=$(meat "$KNOWN_HOSTS" | cut -d ' ' -f 1 | grep -v '^|.*$' | tr , ' ' | tr '\n' ' ')
  52. if [ -z "$hosts" ] ; then
  53. log debug "no hosts to process."
  54. return
  55. fi
  56. # take all the hosts from the known_hosts file (first
  57. # field), grep out all the hashed hosts (lines starting
  58. # with '|')...
  59. update_known_hosts $hosts
  60. }