summaryrefslogtreecommitdiff
path: root/src/monkeysphere-ssh-proxycommand
blob: a7b87ca76f9abff956e1f8a1ea885f963332c2c0 (plain)
  1. #!/bin/bash
  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. ########################################################################
  14. usage() {
  15. cat <<EOF >&2
  16. usage: ssh -o ProxyCommand="$(basename $0) %h %p" ...
  17. EOF
  18. }
  19. ########################################################################
  20. # export the monkeysphere log level
  21. export MONKEYSPHERE_LOG_LEVEL
  22. if [ "$1" = '--no-connect' ] ; then
  23. NO_CONNECT='true'
  24. shift 1
  25. fi
  26. HOST="$1"
  27. PORT="$2"
  28. if [ -z "$HOST" ] ; then
  29. echo "Host not specified." >&2
  30. usage
  31. exit 255
  32. fi
  33. if [ -z "$PORT" ] ; then
  34. PORT=22
  35. fi
  36. # set the host URI
  37. if [ "$PORT" != '22' ] ; then
  38. HOSTP="${HOST}:${PORT}"
  39. else
  40. HOSTP="${HOST}"
  41. fi
  42. URI="ssh://${HOSTP}"
  43. # specify keyserver checking. the behavior of this proxy command is
  44. # intentionally different than that of running monkeyesphere normally,
  45. # and keyserver checking is intentionally done under certain
  46. # circumstances. This can be overridden by setting the
  47. # MONKEYSPHERE_CHECK_KEYSERVER environment variable.
  48. # if the host is in the gpg keyring...
  49. if gpg --list-key ="${URI}" 2>&1 >/dev/null ; then
  50. # do not check the keyserver
  51. CHECK_KEYSERVER="false"
  52. # if the host is NOT in the keyring...
  53. else
  54. # if the host key is found in the known_hosts file...
  55. # FIXME: this only works for default known_hosts location
  56. hostKey=$(ssh-keygen -F "$HOST" 2>/dev/null)
  57. if [ "$hostKey" ] ; then
  58. # do not check the keyserver
  59. # FIXME: more nuanced checking should be done here to properly
  60. # take into consideration hosts that join monkeysphere by
  61. # converting an existing and known ssh key
  62. CHECK_KEYSERVER="false"
  63. # if the host key is not found in the known_hosts file...
  64. else
  65. # check the keyserver
  66. CHECK_KEYSERVER="true"
  67. fi
  68. fi
  69. # set and export the variable for use by monkeysphere
  70. MONKEYSPHERE_CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="$CHECK_KEYSERVER"}
  71. export MONKEYSPHERE_CHECK_KEYSERVER
  72. # update the known_hosts file for the host
  73. monkeysphere update-known_hosts "$HOSTP"
  74. # exec a netcat passthrough to host for the ssh connection
  75. if [ -z "$NO_CONNECT" ] ; then
  76. if (which nc 2>/dev/null >/dev/null); then
  77. exec nc "$HOST" "$PORT"
  78. elif (which socat 2>/dev/null >/dev/null); then
  79. exec socat STDIO "TCP:$HOST:$PORT"
  80. else
  81. echo "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT" >&2
  82. exit 255
  83. fi
  84. fi