summaryrefslogtreecommitdiff
path: root/src/monkeysphere-ssh-proxycommand
blob: f4d4b0d9affb8835ae812e22c9fa58f5388c3fbf (plain)
  1. #!/bin/sh -e
  2. # monkeysphere-ssh-proxycommand: MonkeySphere ssh ProxyCommand hook
  3. #
  4. # The monkeysphere scripts are written by:
  5. # Jameson Rollins <jrollins@fifthhorseman.net>
  6. #
  7. # They are Copyright 2008, and are all released under the GPL, version 3
  8. # or later.
  9. # This is meant to be run as an ssh ProxyCommand to initiate a
  10. # monkeysphere known_hosts update before an ssh connection to host is
  11. # established. Can be added to ~/.ssh/config as follows:
  12. # ProxyCommand monkeysphere-ssh-proxycommand %h %p
  13. usage() {
  14. cat <<EOF >&2
  15. usage: ssh -o ProxyCommand="$(basename $0) %h %p" ...
  16. EOF
  17. }
  18. log() {
  19. echo "$@" >&2
  20. }
  21. if [ "$1" = '--no-connect' ] ; then
  22. NO_CONNECT='true'
  23. shift 1
  24. fi
  25. HOST="$1"
  26. PORT="$2"
  27. if [ -z "$HOST" ] ; then
  28. log "host must be specified."
  29. usage
  30. exit 1
  31. fi
  32. if [ -z "$PORT" ] ; then
  33. log "port must be specified."
  34. usage
  35. exit 1
  36. fi
  37. # set the host URI
  38. URI="ssh://${HOST}"
  39. if [ "$PORT" != '22' ] ; then
  40. URI="${URI}:$PORT"
  41. fi
  42. # if the host is in the gpg keyring...
  43. if gpg --list-key ="${URI}" 2>&1 >/dev/null ; then
  44. # do not check the keyserver
  45. CHECK_KEYSERVER="false"
  46. # if the host is NOT in the keyring...
  47. else
  48. # if the host key is found in the known_hosts file...
  49. # FIXME: this only works for default known_hosts location
  50. hostKey=$(ssh-keygen -F "$HOST")
  51. if [ "$hostKey" ] ; then
  52. # if the check keyserver variable is NOT set to true...
  53. if [ "$CHECK_KEYSERVER" != 'true' ] ; then
  54. # schedule a keyserver check for host at a later time
  55. echo "monkeysphere update-known_hosts $HOST" | at noon
  56. fi
  57. # if the host key is not found in the known_hosts file...
  58. else
  59. # check the keyserver
  60. CHECK_KEYSERVER="true"
  61. fi
  62. fi
  63. export CHECK_KEYSERVER
  64. # update the known_hosts file for the host
  65. monkeysphere update-known_hosts "$HOST"
  66. # exec a netcat passthrough to host for the ssh connection
  67. if [ -z "$NO_CONNECT" ] ; then
  68. exec nc "$HOST" "$PORT"
  69. fi