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