summaryrefslogtreecommitdiff
path: root/src/monkeysphere-ssh-proxycommand
blob: 780ff035c78af143b7feb709250eac19dae8d163 (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. 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. 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. # 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" 2>/dev/null)
  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. MONKEYSPHERE_CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="$CHECK_KEYSERVER"}
  65. export MONKEYSPHERE_CHECK_KEYSERVER
  66. # update the known_hosts file for the host
  67. monkeysphere update-known_hosts "$HOSTP"
  68. # exec a netcat passthrough to host for the ssh connection
  69. if [ -z "$NO_CONNECT" ] ; then
  70. if (which nc 2>/dev/null >/dev/null); then
  71. exec nc "$HOST" "$PORT"
  72. elif (which socat 2>/dev/null >/dev/null); then
  73. exec socat STDIO "TCP:$HOST:$PORT"
  74. else
  75. log "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT"
  76. exit 1
  77. fi
  78. fi