summaryrefslogtreecommitdiff
path: root/src/share/m/ssh_proxycommand
blob: 7ab4bec1331e265eb5b7185cc7eac0b6b753b871 (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere ssh-proxycommand subcommand
  4. #
  5. # The monkeysphere scripts are written by:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  8. #
  9. # They are Copyright 2008-2009, and are all released under the GPL,
  10. # version 3 or later.
  11. # This is meant to be run as an ssh ProxyCommand to initiate a
  12. # monkeysphere known_hosts update before an ssh connection to host is
  13. # established. Can be added to ~/.ssh/config as follows:
  14. # ProxyCommand monkeysphere ssh-proxycommand %h %p
  15. # "marginal case" ouput in the case that there is not a full
  16. # validation path to the host
  17. output_no_valid_key() {
  18. local userID
  19. local sshKeyOffered
  20. local gpgOut
  21. local type
  22. local validity
  23. local keyid
  24. local uidfpr
  25. local usage
  26. local sshKeyGPG
  27. local tmpkey
  28. local sshFingerprint
  29. local gpgSigOut
  30. local returnCode=0
  31. userID="ssh://${HOSTP}"
  32. LOG_PREFIX=
  33. cat <<EOF | log info
  34. -------------------- Monkeysphere warning -------------------
  35. Monkeysphere found OpenPGP keys for this hostname, but none had full validity.
  36. EOF
  37. # retrieve the actual ssh key
  38. sshKeyOffered=$(ssh-keyscan -t rsa -p "$PORT" "$HOST" 2>/dev/null | awk '{ print $2, $3 }')
  39. # FIXME: should we do any checks for failed keyscans, eg. host not
  40. # found?
  41. # get the gpg info for userid
  42. gpgOut=$(gpg_user --list-key --fixed-list-mode --with-colon \
  43. --with-fingerprint --with-fingerprint \
  44. ="$userID" 2>/dev/null)
  45. # find all 'pub' and 'sub' lines in the gpg output, which each
  46. # represent a retrieved key for the user ID
  47. echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
  48. while IFS=: read -r type validity keyid uidfpr usage ; do
  49. case $type in
  50. 'pub'|'sub')
  51. # get the ssh key of the gpg key
  52. sshKeyGPG=$(gpg2ssh "$keyid")
  53. # if one of keys found matches the one offered by the
  54. # host, then output info
  55. if [ "$sshKeyGPG" = "$sshKeyOffered" ] ; then
  56. cat <<EOF | log info
  57. An OpenPGP key matching the ssh key offered by the host was found:
  58. EOF
  59. sshKeyGPGFile=$(msmktempfile)
  60. printf "%s" "$sshKeyGPG" >"$sshKeyGPGFile"
  61. sshFingerprint=$(ssh-keygen -l -f "$sshKeyGPGFile" | \
  62. awk '{ print $2 }')
  63. rm -f "$sshKeyGPGFile"
  64. # get the sigs for the matching key
  65. gpgSigOut=$(gpg_user --check-sigs \
  66. --list-options show-uid-validity \
  67. "$keyid")
  68. # output the sigs, but only those on the user ID
  69. # we are looking for
  70. echo "$gpgSigOut" | awk '
  71. {
  72. if (match($0,"^pub")) { print; }
  73. if (match($0,"^uid")) { ok=0; }
  74. if (match($0,"^uid.*'$userID'$")) { ok=1; print; }
  75. if (ok) { if (match($0,"^sig")) { print; } }
  76. }
  77. ' | log info
  78. echo | log info
  79. # output the other user IDs for reference
  80. if (echo "$gpgSigOut" | grep "^uid" | grep -v -q "$userID") ; then
  81. cat <<EOF | log info
  82. Other user IDs on this key:
  83. EOF
  84. echo "$gpgSigOut" | grep "^uid" | grep -v "$userID" | log info
  85. echo | log info
  86. fi
  87. # output ssh fingerprint
  88. cat <<EOF | log info
  89. RSA key fingerprint is ${sshFingerprint}.
  90. EOF
  91. # this whole process is in a "while read"
  92. # subshell. the only way to get information out
  93. # of the subshell is to change the return code.
  94. # therefore we return 1 here to indicate that a
  95. # matching gpg key was found for the ssh key
  96. # offered by the host
  97. return 1
  98. fi
  99. ;;
  100. esac
  101. done || returnCode="$?"
  102. # if no key match was made (and the "while read" subshell returned
  103. # 1) output how many keys were found
  104. if (( returnCode != 1 )) ; then
  105. cat <<EOF | log info
  106. None of the found keys matched the key offered by the host.
  107. Run the following command for more info about the found keys:
  108. gpg --check-sigs --list-options show-uid-validity =${userID}
  109. EOF
  110. # FIXME: should we do anything extra here if the retrieved
  111. # host key is actually in the known_hosts file and the ssh
  112. # connection will succeed? Should the user be warned?
  113. # prompted?
  114. fi
  115. cat <<EOF | log info
  116. -------------------- ssh continues below --------------------
  117. EOF
  118. }
  119. # the ssh proxycommand function itself
  120. ssh_proxycommand() {
  121. if [ "$1" = '--no-connect' ] ; then
  122. NO_CONNECT='true'
  123. shift 1
  124. fi
  125. HOST="$1"
  126. PORT="$2"
  127. if [ -z "$HOST" ] ; then
  128. log error "Host not specified."
  129. usage
  130. exit 255
  131. fi
  132. if [ -z "$PORT" ] ; then
  133. PORT=22
  134. fi
  135. # set the host URI
  136. if [ "$PORT" != '22' ] ; then
  137. HOSTP="${HOST}:${PORT}"
  138. else
  139. HOSTP="${HOST}"
  140. fi
  141. URI="ssh://${HOSTP}"
  142. # specify keyserver checking. the behavior of this proxy command is
  143. # intentionally different than that of running monkeyesphere normally,
  144. # and keyserver checking is intentionally done under certain
  145. # circumstances. This can be overridden by setting the
  146. # MONKEYSPHERE_CHECK_KEYSERVER environment variable, or by setting the
  147. # CHECK_KEYSERVER variable in the monkeysphere.conf file.
  148. # if the host is in the gpg keyring...
  149. if gpg_user --list-key ="${URI}" 2>&1 >/dev/null ; then
  150. # do not check the keyserver
  151. CHECK_KEYSERVER=${CHECK_KEYSERVER:="false"}
  152. # if the host is NOT in the keyring...
  153. else
  154. # if the host key is found in the known_hosts file...
  155. # FIXME: this only works for default known_hosts location
  156. hostKey=$(ssh-keygen -F "$HOST" 2>/dev/null)
  157. if [ "$hostKey" ] ; then
  158. # do not check the keyserver
  159. # FIXME: more nuanced checking should be done here to properly
  160. # take into consideration hosts that join monkeysphere by
  161. # converting an existing and known ssh key
  162. CHECK_KEYSERVER=${CHECK_KEYSERVER:="false"}
  163. # if the host key is not found in the known_hosts file...
  164. else
  165. # check the keyserver
  166. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  167. fi
  168. fi
  169. # finally look in the MONKEYSPHERE_ environment variable for a
  170. # CHECK_KEYSERVER setting to override all else
  171. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  172. # update the known_hosts file for the host
  173. local returnCode=0
  174. update_known_hosts "$HOSTP" || returnCode="$?"
  175. # output on depending on the return of the update-known_hosts
  176. # subcommand, which is (ultimately) the return code of the
  177. # update_known_hosts function in common
  178. case "$returnCode" in
  179. 0)
  180. # acceptable host key found so continue to ssh
  181. true
  182. ;;
  183. 1)
  184. # no hosts at all found so also continue (drop through to
  185. # regular ssh host verification)
  186. true
  187. ;;
  188. 2)
  189. # at least one *bad* host key (and no good host keys) was
  190. # found, so output some usefull information
  191. output_no_valid_key
  192. ;;
  193. *)
  194. # anything else drop through
  195. true
  196. ;;
  197. esac
  198. # FIXME: what about the case where monkeysphere successfully finds a
  199. # valid key for the host and adds it to the known_hosts file, but a
  200. # different non-monkeysphere key for the host already exists in the
  201. # known_hosts, and it is this non-ms key that is offered by the host?
  202. # monkeysphere will succeed, and the ssh connection will succeed, and
  203. # the user will be left with the impression that they are dealing with
  204. # a OpenPGP/PKI host key when in fact they are not. should we use
  205. # ssh-keyscan to compare the keys first?
  206. # exec a netcat passthrough to host for the ssh connection
  207. if [ -z "$NO_CONNECT" ] ; then
  208. if (which nc 2>/dev/null >/dev/null); then
  209. exec nc "$HOST" "$PORT"
  210. elif (which socat 2>/dev/null >/dev/null); then
  211. exec socat STDIO "TCP:$HOST:$PORT"
  212. else
  213. echo "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT" >&2
  214. exit 255
  215. fi
  216. fi
  217. }