editing comments in monkeysphere-ssh-proxycommand.
[monkeysphere.git] / src / monkeysphere-ssh-proxycommand
1 #!/bin/bash
2
3 # monkeysphere-ssh-proxycommand: MonkeySphere ssh ProxyCommand hook
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 #
8 # They are Copyright 2008, and are all released under the GPL, version 3
9 # or later.
10
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
16 ########################################################################
17
18 usage() {
19 cat <<EOF >&2
20 usage: ssh -o ProxyCommand="$(basename $0) %h %p" ...
21 EOF
22 }
23
24 ########################################################################
25
26 # export the monkeysphere log level
27 export MONKEYSPHERE_LOG_LEVEL
28
29 if [ "$1" = '--no-connect' ] ; then
30     NO_CONNECT='true'
31     shift 1
32 fi
33
34 HOST="$1"
35 PORT="$2"
36
37 if [ -z "$HOST" ] ; then
38     echo "Host not specified." >&2
39     usage
40     exit 255
41 fi
42 if [ -z "$PORT" ] ; then
43     PORT=22
44 fi
45
46 # set the host URI
47 if [ "$PORT" != '22' ] ; then
48     HOSTP="${HOST}:${PORT}"
49 else
50     HOSTP="${HOST}"
51 fi
52 URI="ssh://${HOSTP}"
53
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.
59
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"
64
65 # if the host is NOT in the keyring...
66 else
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)
70
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"
77
78     # if the host key is not found in the known_hosts file...
79     else
80         # check the keyserver
81         CHECK_KEYSERVER="true"
82     fi
83 fi
84 # set and export the variable for use by monkeysphere
85 MONKEYSPHERE_CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="$CHECK_KEYSERVER"}
86 export MONKEYSPHERE_CHECK_KEYSERVER
87
88 # update the known_hosts file for the host
89 monkeysphere update-known_hosts "$HOSTP"
90
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"
97     else
98         echo "Neither netcat nor socat found -- could not complete monkeysphere-ssh-proxycommand connection to $HOST:$PORT" >&2
99         exit 255
100     fi
101 fi