Merge commit 'micah/master'
[monkeysphere.git] / src / monkeysphere-ssh-proxycommand
1 #!/bin/sh -e
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 usage() {
17 cat <<EOF >&2
18 usage: ssh -o ProxyCommand="$(basename $0) %h %p" ...
19 EOF
20 }
21
22 log() {
23     echo "$@" >&2
24 }
25
26 if [ "$1" = '--no-connect' ] ; then
27     NO_CONNECT='true'
28     shift 1
29 fi
30
31 HOST="$1"
32 PORT="$2"
33
34 if [ -z "$HOST" ] ; then
35     log "host must be specified."
36     usage
37     exit 1
38 fi
39 if [ -z "$PORT" ] ; then
40     log "port must be specified."
41     usage
42     exit 1
43 fi
44
45 # set the host URI
46 URI="ssh://${HOST}"
47 if [ "$PORT" != '22' ] ; then
48     URI="${URI}:$PORT"
49 fi
50
51 # if the host is in the gpg keyring...
52 if gpg --list-key ="${URI}" >/dev/null ; then
53     # do not check the keyserver
54     CHECK_KEYSERVER="false"
55 # if the host is NOT in the keyring...
56 else
57     # if the host key is found in the known_hosts file...
58     # FIXME: this only works for default known_hosts location
59     hostKey=$(ssh-keygen -F "$HOST")
60     if [ "$hostKey" ] ; then
61         # if the check keyserver variable is NOT set to true...
62         if  [ "$CHECK_KEYSERVER" != 'true' ] ; then
63             # schedule a keyserver check for host at a later time
64             echo "monkeysphere update-known_hosts $HOST" | at noon
65         fi
66     # if the host key is not found in the known_hosts file...
67     else
68         # check the keyserver
69         CHECK_KEYSERVER="true"
70     fi
71 fi
72 export CHECK_KEYSERVER
73
74 # update the known_hosts file for the host
75 monkeysphere update-known_hosts "$HOST"
76
77 # exec a netcat passthrough to host for the ssh connection
78 if [ -z "$NO_CONNECT" ] ; then
79     exec nc "$HOST" "$PORT"
80 fi