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 log "OpenPGP keys with*out* full validity found for this host:"
51 # retrieve the actual ssh key
52 sshKeyOffered=$(ssh-keyscan -t rsa -p "$PORT" "$HOST" 2>/dev/null | awk '{ print $2, $3 }')
54 userID="ssh://${HOSTP}"
56 # output gpg info for (exact) userid and store
57 gpgOut=$(gpg --list-key --fixed-list-mode --with-colon \
58 --with-fingerprint --with-fingerprint \
59 ="$userID" 2>/dev/null)
61 # loop over all lines in the gpg output and process.
62 echo "$gpgOut" | cut -d: -f1,2,5,10,12 | \
63 while IFS=: read -r type validity keyid uidfpr usage ; do
66 # get the ssh key of the gpg key
67 sshKeyGPG=$(gpg2ssh "$keyid")
69 # if one of keys found matches the one offered by the
70 # host, then output info
71 if [ "$sshKeyGPG" = "$sshKeyOffered" ] ; then
73 # get the fingerprint of the ssh key
74 tmpkey=$(mktemp ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)
75 echo "$sshKeyGPG" > "$tmpkey"
76 sshFingerprint=$(ssh-keygen -l -f "$tmpkey" | awk '{ print $2 }')
81 --list-options show-uid-validity \
84 # output ssh fingerprint
85 log "RSA key fingerprint is ${sshFingerprint}."
86 log "Falling through to standard ssh host checking."
94 ########################################################################
96 # export the monkeysphere log level
97 export MONKEYSPHERE_LOG_LEVEL
99 if [ "$1" = '--no-connect' ] ; then
107 if [ -z "$HOST" ] ; then
108 log "Host not specified."
112 if [ -z "$PORT" ] ; then
117 if [ "$PORT" != '22' ] ; then
118 HOSTP="${HOST}:${PORT}"
124 # specify keyserver checking. the behavior of this proxy command is
125 # intentionally different than that of running monkeyesphere normally,
126 # and keyserver checking is intentionally done under certain
127 # circumstances. This can be overridden by setting the
128 # MONKEYSPHERE_CHECK_KEYSERVER environment variable.
130 # if the host is in the gpg keyring...
131 if gpg --list-key ="${URI}" 2>&1 >/dev/null ; then
132 # do not check the keyserver
133 CHECK_KEYSERVER="false"
135 # if the host is NOT in the keyring...
137 # if the host key is found in the known_hosts file...
138 # FIXME: this only works for default known_hosts location
139 hostKey=$(ssh-keygen -F "$HOST" 2>/dev/null)
141 if [ "$hostKey" ] ; then
142 # do not check the keyserver
143 # FIXME: more nuanced checking should be done here to properly
144 # take into consideration hosts that join monkeysphere by
145 # converting an existing and known ssh key
146 CHECK_KEYSERVER="false"
148 # if the host key is not found in the known_hosts file...
150 # check the keyserver
151 CHECK_KEYSERVER="true"
154 # set and export the variable for use by monkeysphere
155 MONKEYSPHERE_CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="$CHECK_KEYSERVER"}
156 export MONKEYSPHERE_CHECK_KEYSERVER
158 # update the known_hosts file for the host
159 monkeysphere update-known_hosts "$HOSTP"
161 # output on depending on the return of the update-known_hosts
162 # subcommand, which is (ultimately) the return code of the
163 # update_known_hosts function in common
166 # acceptable host key found so continue to ssh
170 # no hosts at all found so also continue (drop through to
171 # regular ssh host verification)
175 # at least one *bad* host key (and no good host keys) was
176 # found, so output some usefull information
180 # anything else drop through
185 # exec a netcat passthrough to host for the ssh connection
186 if [ -z "$NO_CONNECT" ] ; then
187 if (which nc 2>/dev/null >/dev/null); then
188 exec nc "$HOST" "$PORT"
189 elif (which socat 2>/dev/null >/dev/null); then
190 exec socat STDIO "TCP:$HOST:$PORT"
192 echo "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT" >&2