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