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 ########################################################################
20 usage: ssh -o ProxyCommand="$(basename $0) %h %p" ...
24 ########################################################################
26 # export the monkeysphere log level
27 export MONKEYSPHERE_LOG_LEVEL
29 if [ "$1" = '--no-connect' ] ; then
37 if [ -z "$HOST" ] ; then
38 echo "Host not specified." >&2
42 if [ -z "$PORT" ] ; then
47 if [ "$PORT" != '22' ] ; then
48 HOSTP="${HOST}:${PORT}"
54 # specify keyserver checking. the behavior of this proxy command is
55 # intentionally different than that of running monkeyesphere normally,
56 # and keyserver checking is intentionally done under certain
57 # circumstances. This can be overridden by setting the
58 # MONKEYSPHERE_CHECK_KEYSERVER environment variable.
60 # if the host is in the gpg keyring...
61 if gpg --list-key ="${URI}" 2>&1 >/dev/null ; then
62 # do not check the keyserver
63 CHECK_KEYSERVER="false"
65 # if the host is NOT in the keyring...
67 # if the host key is found in the known_hosts file...
68 # FIXME: this only works for default known_hosts location
69 hostKey=$(ssh-keygen -F "$HOST" 2>/dev/null)
71 if [ "$hostKey" ] ; then
72 # do not check the keyserver
73 # FIXME: more nuanced checking should be done here to properly
74 # take into consideration hosts that join monkeysphere by
75 # converting an existing and known ssh key
76 CHECK_KEYSERVER="false"
78 # if the host key is not found in the known_hosts file...
81 CHECK_KEYSERVER="true"
84 # set and export the variable for use by monkeysphere
85 MONKEYSPHERE_CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="$CHECK_KEYSERVER"}
86 export MONKEYSPHERE_CHECK_KEYSERVER
88 # update the known_hosts file for the host
89 monkeysphere update-known_hosts "$HOSTP"
91 # exec a netcat passthrough to host for the ssh connection
92 if [ -z "$NO_CONNECT" ] ; then
93 if (which nc 2>/dev/null >/dev/null); then
94 exec nc "$HOST" "$PORT"
95 elif (which socat 2>/dev/null >/dev/null); then
96 exec socat STDIO "TCP:$HOST:$PORT"
98 echo "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT" >&2