summaryrefslogtreecommitdiff
path: root/src/monkeysphere-ssh-proxycommand
blob: 6dcb723e244a6d32d5f114dc02a6f336fcae2a58 (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. MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
  28. if [ -z "$HOST" ] ; then
  29. log "host must be specified."
  30. usage
  31. exit 1
  32. fi
  33. if [ -z "$PORT" ] ; then
  34. log "port must be specified."
  35. usage
  36. exit 1
  37. fi
  38. # set the host URI
  39. URI="ssh://${HOST}"
  40. if [ "$PORT" != '22' ] ; then
  41. URI="${URI}:$PORT"
  42. fi
  43. # if the host is in the gpg keyring...
  44. if gpg --list-key ="${URI}" 2>&1 >/dev/null ; then
  45. # do not check the keyserver
  46. CHECK_KEYSERVER="false"
  47. # if the host is NOT in the keyring...
  48. else
  49. # if the host key is found in the known_hosts file...
  50. # FIXME: this only works for default known_hosts location
  51. hostKey=$(ssh-keygen -F "$HOST")
  52. if [ "$hostKey" ] ; then
  53. # do not check the keyserver
  54. # FIXME: more nuanced checking should be done here to properly
  55. # take into consideration hosts that join monkeysphere by
  56. # converting an existing and known ssh key
  57. CHECK_KEYSERVER="false"
  58. # if the host key is not found in the known_hosts file...
  59. else
  60. # check the keyserver
  61. CHECK_KEYSERVER="true"
  62. fi
  63. fi
  64. export CHECK_KEYSERVER
  65. # update the known_hosts file for the host
  66. monkeysphere update-known_hosts "$HOST"
  67. # exec a netcat passthrough to host for the ssh connection
  68. if [ -z "$NO_CONNECT" ] ; then
  69. exec nc "$HOST" "$PORT"
  70. fi