3 # monkeysphere-ssh-proxycommand: MonkeySphere ssh ProxyCommand hook
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # They are Copyright 2008, and are all released under the GPL, version 3
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
16 ########################################################################
19 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
21 . "${SYSSHAREDIR}/common" || exit 1
23 ########################################################################
25 ########################################################################
29 usage: ssh -o ProxyCommand="$(basename $0) %h %p" ...
37 output_no_valid_key() {
48 userID="ssh://${HOSTP}"
50 log "Monkeysphere found only OpenPGP keys for this host with*out* full validity."
54 # retrieve the actual ssh key
55 sshKeyOffered=$(ssh-keyscan -t rsa -p "$PORT" "$HOST" 2>/dev/null | awk '{ print $2, $3 }')
56 # FIXME: should we do any checks for failed keyscans, eg host not
59 # output gpg info for userid and store
60 gpgOut=$(gpg --list-key --fixed-list-mode --with-colon \
61 --with-fingerprint --with-fingerprint \
62 ="$userID" 2>/dev/null)
64 # find all 'pub' and 'sub' lines in the gpg output, which each
65 # represent a retrieved key for the user ID
66 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
67 while IFS=: read -r type validity keyid uidfpr usage ; do
70 # get the ssh key of the gpg key
71 sshKeyGPG=$(gpg2ssh "$keyid")
73 # if one of keys found matches the one offered by the
74 # host, then output info
75 if [ "$sshKeyGPG" = "$sshKeyOffered" ] ; then
77 # get the fingerprint of the ssh key
78 tmpkey=$(mktemp ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)
79 echo "$sshKeyGPG" > "$tmpkey"
80 sshFingerprint=$(ssh-keygen -l -f "$tmpkey" | awk '{ print $2 }')
85 --list-options show-uid-validity \
88 # output ssh fingerprint
89 log "RSA key fingerprint is ${sshFingerprint}."
90 log "Falling through to standard ssh host checking."
98 ########################################################################
100 # export the monkeysphere log level
101 export MONKEYSPHERE_LOG_LEVEL
103 if [ "$1" = '--no-connect' ] ; then
111 if [ -z "$HOST" ] ; then
112 log "Host not specified."
116 if [ -z "$PORT" ] ; then
121 if [ "$PORT" != '22' ] ; then
122 HOSTP="${HOST}:${PORT}"
128 # specify keyserver checking. the behavior of this proxy command is
129 # intentionally different than that of running monkeyesphere normally,
130 # and keyserver checking is intentionally done under certain
131 # circumstances. This can be overridden by setting the
132 # MONKEYSPHERE_CHECK_KEYSERVER environment variable.
134 # if the host is in the gpg keyring...
135 if gpg --list-key ="${URI}" 2>&1 >/dev/null ; then
136 # do not check the keyserver
137 CHECK_KEYSERVER="false"
139 # if the host is NOT in the keyring...
141 # if the host key is found in the known_hosts file...
142 # FIXME: this only works for default known_hosts location
143 hostKey=$(ssh-keygen -F "$HOST" 2>/dev/null)
145 if [ "$hostKey" ] ; then
146 # do not check the keyserver
147 # FIXME: more nuanced checking should be done here to properly
148 # take into consideration hosts that join monkeysphere by
149 # converting an existing and known ssh key
150 CHECK_KEYSERVER="false"
152 # if the host key is not found in the known_hosts file...
154 # check the keyserver
155 CHECK_KEYSERVER="true"
158 # set and export the variable for use by monkeysphere
159 MONKEYSPHERE_CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="$CHECK_KEYSERVER"}
160 export MONKEYSPHERE_CHECK_KEYSERVER
162 # update the known_hosts file for the host
163 monkeysphere update-known_hosts "$HOSTP"
165 # output on depending on the return of the update-known_hosts
166 # subcommand, which is (ultimately) the return code of the
167 # update_known_hosts function in common
170 # acceptable host key found so continue to ssh
174 # no hosts at all found so also continue (drop through to
175 # regular ssh host verification)
179 # at least one *bad* host key (and no good host keys) was
180 # found, so output some usefull information
184 # anything else drop through
189 # exec a netcat passthrough to host for the ssh connection
190 if [ -z "$NO_CONNECT" ] ; then
191 if (which nc 2>/dev/null >/dev/null); then
192 exec nc "$HOST" "$PORT"
193 elif (which socat 2>/dev/null >/dev/null); then
194 exec socat STDIO "TCP:$HOST:$PORT"
196 echo "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT" >&2