Merge commit 'jrollins/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 HOST="$1"
17 PORT="$2"
18
19 usage() {
20 cat <<EOF >&2
21 usage: ssh -o ProxyCommand="$(basename $0) %h %p" ...
22 EOF
23 }
24
25 log() {
26     echo "$@" >&2
27 }
28
29 if [ -z "$HOST" ] ; then
30     log "host must be specified."
31     usage
32     exit 1
33 fi
34 if [ -z "$PORT" ] ; then
35     log "port must be specified."
36     usage
37     exit 1
38 fi
39
40 # check for the host key in the known_hosts file
41 hostKey=$(ssh-keygen -F "$HOST")
42
43 # if the host key is found in the known_hosts file,
44 # don't check the keyserver
45 if [ "$hostKey" ] ; then
46     CHECK_KEYSERVER="false"
47 fi
48 export CHECK_KEYSERVER
49
50 # update the known_hosts file for the host
51 monkeysphere update-known-hosts "$HOST"
52
53 # exec a netcat passthrough to host for the ssh connection
54 exec nc "$HOST" "$PORT"