b70f3c69292cbfed7f3034c90e65ee93c2810b2f
[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 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
35
36 if [ -z "$HOST" ] ; then
37     log "host must be specified."
38     usage
39     exit 1
40 fi
41 if [ -z "$PORT" ] ; then
42     PORT=22
43 fi
44
45 # set the host URI
46 if [ "$PORT" != '22' ] ; then
47     HOSTP="${HOST}:${PORT}"
48 else
49     HOSTP="${HOST}"
50 fi
51 URI="ssh://${HOSTP}"
52
53 # if the host is in the gpg keyring...
54 if gpg --list-key ="${URI}" 2>&1 >/dev/null ; then
55     # do not check the keyserver
56     CHECK_KEYSERVER=${CHECK_KEYSERVER:-"false"}
57
58 # if the host is NOT in the keyring...
59 else
60     # if the host key is found in the known_hosts file...
61     # FIXME: this only works for default known_hosts location
62     hostKey=$(ssh-keygen -F "$HOST")
63     if [ "$hostKey" ] ; then
64         # do not check the keyserver
65         # FIXME: more nuanced checking should be done here to properly
66         # take into consideration hosts that join monkeysphere by
67         # converting an existing and known ssh key
68         CHECK_KEYSERVER=${CHECK_KEYSERVER:-"false"}
69
70     # if the host key is not found in the known_hosts file...
71     else
72         # check the keyserver
73         CHECK_KEYSERVER=${CHECK_KEYSERVER:-"true"}
74     fi
75 fi
76 export CHECK_KEYSERVER
77
78 # update the known_hosts file for the host
79 monkeysphere update-known_hosts "$HOSTP"
80
81 # exec a netcat passthrough to host for the ssh connection
82 if [ -z "$NO_CONNECT" ] ; then
83     exec nc "$HOST" "$PORT"
84 fi