2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
4 # Monkeysphere ssh-proxycommand subcommand
6 # The monkeysphere scripts are written by:
7 # Jameson Rollins <jrollins@finestructure.net>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
10 # They are Copyright 2008-2009, and are all released under the GPL,
13 # This is meant to be run as an ssh ProxyCommand to initiate a
14 # monkeysphere known_hosts update before an ssh connection to host is
15 # established. Can be added to ~/.ssh/config as follows:
16 # ProxyCommand monkeysphere ssh-proxycommand %h %p
18 # the ssh proxycommand function itself
26 if [[ "$1" == '--no-connect' ]] ; then
34 if [ -z "$HOST" ] ; then
35 log error "Host not specified."
39 if [ -z "$PORT" ] ; then
44 if [ "$PORT" != '22' ] ; then
45 HOSTP="${HOST}:${PORT}"
51 # passed HOST/PORT/HOSTP/URI
54 # exec a netcat passthrough to host for the ssh connection
55 if [[ "$connect" == 'true' ]] ; then
56 if (type nc &>/dev/null); then
57 exec nc "$HOST" "$PORT"
58 elif (type socat &>/dev/null); then
59 exec socat STDIO "TCP:$HOST:$PORT"
61 echo "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT" >&2
67 validate_monkeysphere() {
70 # specify keyserver checking. the behavior of this proxy command
71 # is intentionally different than that of running monkeyesphere
72 # normally, and keyserver checking is intentionally done under
73 # certain circumstances. This can be overridden by setting the
74 # MONKEYSPHERE_CHECK_KEYSERVER environment variable, or by setting
75 # the CHECK_KEYSERVER variable in the monkeysphere.conf file.
77 # if the host is in the gpg keyring...
78 if gpg_user --list-key ="${URI}" &>/dev/null ; then
79 # do not check the keyserver
80 CHECK_KEYSERVER=${CHECK_KEYSERVER:="false"}
82 # if the host is NOT in the keyring...
84 # FIXME: what about system-wide known_hosts file (/etc/ssh/known_hosts)?
86 if [ -r "$KNOWN_HOSTS" ]; then
87 # look up the host key is found in the known_hosts file...
88 if (type ssh-keygen &>/dev/null) ; then
89 hostKey=$(ssh-keygen -F "$HOST" -f "$KNOWN_HOSTS" 2>/dev/null)
91 # FIXME: we're not dealing with digested known_hosts
92 # if we don't have ssh-keygen
94 # But we could do this without needing ssh-keygen.
95 # hashed known_hosts looks like: |1|X|Y where 1 means
96 # SHA1 (nothing else is defined in openssh sources), X
97 # is the salt (same length as the digest output),
98 # base64-encoded, and Y is the digested hostname (also
101 # see hostfile.{c,h} in openssh sources.
103 hostKey=$(cut -f1 -d\ < .ssh/known_hosts | tr ',' '\n' | grep -Fx -e "$HOST" || :)
107 if [ "$hostKey" ] ; then
108 # do not check the keyserver
109 # FIXME: more nuanced checking should be done here to properly
110 # take into consideration hosts that join monkeysphere by
111 # converting an existing and known ssh key
112 CHECK_KEYSERVER=${CHECK_KEYSERVER:="false"}
114 # if the host key is not found in the known_hosts file...
116 # check the keyserver
117 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
121 # finally look in the MONKEYSPHERE_ environment variable for a
122 # CHECK_KEYSERVER setting to override all else
123 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
125 declare -i KEYS_PROCESSED=0
126 declare -i KEYS_VALID=0
128 # update the known_hosts file for the host
129 source "${MSHAREDIR}/update_known_hosts"
130 update_known_hosts "$HOSTP"
132 if ((KEYS_PROCESSED > 0)) && ((KEYS_VALID == 0)) ; then
133 log debug "output ssh marginal ui..."
137 # FIXME: what about the case where monkeysphere successfully finds
138 # a valid key for the host and adds it to the known_hosts file,
139 # but a different non-monkeysphere key for the host already exists
140 # in the known_hosts, and it is this non-ms key that is offered by
141 # the host? monkeysphere will succeed, and the ssh connection
142 # will succeed, and the user will be left with the impression that
143 # they are dealing with a OpenPGP/PKI host key when in fact they
144 # are not. should we use ssh-keyscan to compare the keys first?
147 # output the key info, including the RSA fingerprint
155 # get the ssh key of the gpg key
156 sshFingerprint=$(gpg2ssh "$keyid" | "$SYSSHAREDIR/keytrans" sshfpr)
158 # get the sigs for the matching key
159 gpgSigOut=$(gpg_user --check-sigs \
160 --list-options show-uid-validity \
165 # output the sigs, but only those on the user ID
167 echo "$gpgSigOut" | awk '
169 if (match($0,"^pub")) { print; }
170 if (match($0,"^uid")) { ok=0; }
171 if (match($0,"^uid.*'$userID'$")) { ok=1; print; }
172 if (ok) { if (match($0,"^sig")) { print; } }
176 # output ssh fingerprint
178 RSA key fingerprint is ${sshFingerprint}.
181 # output the other user IDs for reference
182 otherUids=$(echo "$gpgSigOut" | grep "^uid" | grep -v "$userID")
183 if [ "$otherUids" ] ; then
186 Other user IDs on this key:
188 echo "$otherUids" | log info
193 # "marginal case" ouput in the case that there is not a full
194 # validation path to the host
195 output_no_valid_key() {
208 userID="ssh://${HOSTP}"
212 # if we don't have ssh-keyscan, we just don't scan:
213 if ( type ssh-keyscan &>/dev/null ) ; then
214 # retrieve the ssh key being offered by the host
215 sshKeyOffered=$(ssh-keyscan -t rsa -p "$PORT" "$HOST" 2>/dev/null \
216 | awk '{ print $2, $3 }')
219 # get the gpg info for userid
220 gpgOut=$(gpg_user --list-key --with-colons \
221 --with-fingerprint --with-fingerprint \
222 ="$userID" 2>/dev/null)
226 -------------------- Monkeysphere warning -------------------
227 Monkeysphere found OpenPGP keys for this hostname, but none had full validity.
230 # output message if host key could not be retrieved from the host
231 if [ -z "$sshKeyOffered" ] ; then
233 Could not retrieve RSA host key from $HOST.
235 # check that there are any marginally valid keys
236 if echo "$gpgOut" | egrep -q '^(pub|sub):(m|f|u):' ; then
238 The following keys were found with marginal validity:
243 # find all keys in the gpg output ('pub' and 'sub' lines) and
244 # output the ones that match the host key or that have marginal
246 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
247 while IFS=: read -r type validity keyid uidfpr usage ; do
250 # get the ssh key of the gpg key
251 sshKeyGPG=$(gpg2ssh "$keyid")
252 # if a key was retrieved from the host...
253 if [ "$sshKeyOffered" ] ; then
254 # if one of the keys matches the one offered by
255 # the host, then output info and return
256 if [ "$sshKeyGPG" = "$sshKeyOffered" ] ; then
258 An OpenPGP key matching the ssh key offered by the host was found:
260 show_key_info "$keyid" | log info
261 # this whole process is in a "while read"
262 # subshell. the only way to get information
263 # out of the subshell is to change the return
264 # code. therefore we return 1 here to
265 # indicate that a matching gpg key was found
266 # for the ssh key offered by the host
269 # else if a key was not retrieved from the host...
271 # and the current key is marginal, show info
272 if [ "$validity" = 'm' ] \
273 || [ "$validity" = 'f' ] \
274 || [ "$validity" = 'u' ] ; then
275 show_key_info "$keyid" | log info
280 done || returnCode="$?"
282 # if no key match was made (and the "while read" subshell
283 # returned 1) output how many keys were found
284 if (( returnCode == 1 )) ; then
287 # if a key was retrieved, but didn't match, note this
288 if [ "$sshKeyOffered" ] ; then
290 None of the found keys matched the key offered by the host.
294 # note how many invalid keys were found
295 nInvalidKeys=$(echo "$gpgOut" | egrep '^(pub|sub):[^(m|f|u)]:' | wc -l)
296 if ((nInvalidKeys > 0)) ; then
298 Keys found with less than marginal validity: $nInvalidKeys
303 Run the following command for more info about the found keys:
304 gpg --check-sigs --list-options show-uid-validity =${userID}
307 # FIXME: should we do anything extra here if the retrieved
308 # host key is actually in the known_hosts file and the ssh
309 # connection will succeed? Should the user be warned?
315 -------------------- ssh continues below --------------------