summaryrefslogtreecommitdiff
path: root/src/share/m/ssh_proxycommand
blob: 96326dae60ab51cdff421dee27d424b0ddb58b47 (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. EOF
  89. # check that there are any marginally valid keys
  90. if echo "$gpgOut" | egrep -q '^(pub|sub):(m|f|u):' ; then
  91. log info <<EOF
  92. The following keys were found with marginal validity:
  93. EOF
  94. fi
  95. fi
  96. # find all keys in the gpg output ('pub' and 'sub' lines) and
  97. # output the ones that match the host key or that have marginal
  98. # validity
  99. echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
  100. while IFS=: read -r type validity keyid uidfpr usage ; do
  101. case $type in
  102. 'pub'|'sub')
  103. # get the ssh key of the gpg key
  104. sshKeyGPG=$(gpg2ssh "$keyid")
  105. # if a key was retrieved from the host...
  106. if [ "$sshKeyOffered" ] ; then
  107. # if one of the keys matches the one offered by
  108. # the host, then output info and return
  109. if [ "$sshKeyGPG" = "$sshKeyOffered" ] ; then
  110. log info <<EOF
  111. An OpenPGP key matching the ssh key offered by the host was found:
  112. EOF
  113. show_key_info "$keyid" | log info
  114. # this whole process is in a "while read"
  115. # subshell. the only way to get information
  116. # out of the subshell is to change the return
  117. # code. therefore we return 1 here to
  118. # indicate that a matching gpg key was found
  119. # for the ssh key offered by the host
  120. return 1
  121. fi
  122. # else if a key was not retrieved from the host...
  123. else
  124. # and the current key is marginal, show info
  125. if [ "$validity" = 'm' ] \
  126. || [ "$validity" = 'f' ] \
  127. || [ "$validity" = 'u' ] ; then
  128. show_key_info "$keyid" | log info
  129. fi
  130. fi
  131. ;;
  132. esac
  133. done || returnCode="$?"
  134. # if no key match was made (and the "while read" subshell
  135. # returned 1) output how many keys were found
  136. if (( returnCode == 1 )) ; then
  137. echo | log info
  138. else
  139. # if a key was retrieved, but didn't match, note this
  140. if [ "$sshKeyOffered" ] ; then
  141. log info <<EOF
  142. None of the found keys matched the key offered by the host.
  143. EOF
  144. fi
  145. # note how many invalid keys were found
  146. nInvalidKeys=$(echo "$gpgOut" | egrep '^(pub|sub):[^(m|f|u)]:' | wc -l)
  147. if ((nInvalidKeys > 0)) ; then
  148. log info <<EOF
  149. Keys found with less than marginal validity: $nInvalidKeys
  150. EOF
  151. fi
  152. log info <<EOF
  153. Run the following command for more info about the found keys:
  154. gpg --check-sigs --list-options show-uid-validity =${userID}
  155. EOF
  156. # FIXME: should we do anything extra here if the retrieved
  157. # host key is actually in the known_hosts file and the ssh
  158. # connection will succeed? Should the user be warned?
  159. # prompted?
  160. fi
  161. # output footer
  162. log info <<EOF
  163. -------------------- ssh continues below --------------------
  164. EOF
  165. }
  166. # the ssh proxycommand function itself
  167. ssh_proxycommand() {
  168. if [ "$1" = '--no-connect' ] ; then
  169. NO_CONNECT='true'
  170. shift 1
  171. fi
  172. HOST="$1"
  173. PORT="$2"
  174. if [ -z "$HOST" ] ; then
  175. log error "Host not specified."
  176. usage
  177. exit 255
  178. fi
  179. if [ -z "$PORT" ] ; then
  180. PORT=22
  181. fi
  182. # set the host URI
  183. if [ "$PORT" != '22' ] ; then
  184. HOSTP="${HOST}:${PORT}"
  185. else
  186. HOSTP="${HOST}"
  187. fi
  188. URI="ssh://${HOSTP}"
  189. # specify keyserver checking. the behavior of this proxy command is
  190. # intentionally different than that of running monkeyesphere normally,
  191. # and keyserver checking is intentionally done under certain
  192. # circumstances. This can be overridden by setting the
  193. # MONKEYSPHERE_CHECK_KEYSERVER environment variable, or by setting the
  194. # CHECK_KEYSERVER variable in the monkeysphere.conf file.
  195. # if the host is in the gpg keyring...
  196. if gpg_user --list-key ="${URI}" &>/dev/null ; then
  197. # do not check the keyserver
  198. CHECK_KEYSERVER=${CHECK_KEYSERVER:="false"}
  199. # if the host is NOT in the keyring...
  200. else
  201. # if the host key is found in the known_hosts file...
  202. hostKey=$( [ ! -r "$KNOWN_HOSTS" ] || ssh-keygen -F "$HOST" -f "$KNOWN_HOSTS" 2>/dev/null)
  203. if [ "$hostKey" ] ; then
  204. # do not check the keyserver
  205. # FIXME: more nuanced checking should be done here to properly
  206. # take into consideration hosts that join monkeysphere by
  207. # converting an existing and known ssh key
  208. CHECK_KEYSERVER=${CHECK_KEYSERVER:="false"}
  209. # if the host key is not found in the known_hosts file...
  210. else
  211. # check the keyserver
  212. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  213. fi
  214. fi
  215. # finally look in the MONKEYSPHERE_ environment variable for a
  216. # CHECK_KEYSERVER setting to override all else
  217. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  218. # update the known_hosts file for the host
  219. local returnCode=0
  220. update_known_hosts "$HOSTP" || returnCode="$?"
  221. # output on depending on the return of the update-known_hosts
  222. # subcommand, which is (ultimately) the return code of the
  223. # update_known_hosts function in common
  224. case "$returnCode" in
  225. 0)
  226. # acceptable host key found so continue to ssh
  227. true
  228. ;;
  229. 1)
  230. # no hosts at all found so also continue (drop through to
  231. # regular ssh host verification)
  232. true
  233. ;;
  234. 2)
  235. # at least one *bad* host key (and no good host keys) was
  236. # found, so output some usefull information
  237. output_no_valid_key
  238. ;;
  239. *)
  240. # anything else drop through
  241. true
  242. ;;
  243. esac
  244. # FIXME: what about the case where monkeysphere successfully finds a
  245. # valid key for the host and adds it to the known_hosts file, but a
  246. # different non-monkeysphere key for the host already exists in the
  247. # known_hosts, and it is this non-ms key that is offered by the host?
  248. # monkeysphere will succeed, and the ssh connection will succeed, and
  249. # the user will be left with the impression that they are dealing with
  250. # a OpenPGP/PKI host key when in fact they are not. should we use
  251. # ssh-keyscan to compare the keys first?
  252. # exec a netcat passthrough to host for the ssh connection
  253. if [ -z "$NO_CONNECT" ] ; then
  254. if (type nc &>/dev/null); then
  255. exec nc "$HOST" "$PORT"
  256. elif (type socat &>/dev/null); then
  257. exec socat STDIO "TCP:$HOST:$PORT"
  258. else
  259. echo "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT" >&2
  260. exit 255
  261. fi
  262. fi
  263. }