Improve server logging.
[tripe] / contrib / knock.in
1 #! /bin/sh
2
3 set -e
4
5 ### This script performs the passive side of a dynamic association. It is
6 ### intended to be set as the `tripe' user's shell, and invoked via ssh(1).
7 ### Specifically, for each dynamic peer, add a line to `.ssh/authorized_keys'
8 ### of the form
9 ###
10 ### command="PEER" ssh-rsa ...
11 ###
12 ### There's an additional wrinkle. Suppose that the passive TrIPE endpoint
13 ### is behind a NAT, and the SSH gateway is on a different machine. The
14 ### gateway should have its own `tripe' user, and this script should again be
15 ### its shell. On the gateway, add a `.ssh/authorized_keys' entry
16 ###
17 ### command="tripe@SERVER:PEER" ssh-rsa ...
18 ###
19 ### for the dynamic endpoint. On the passive endpoint itself, you need an
20 ### entry for the gateway's `tripe' user's key, with no command.
21
22 : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
23 : ${bindir=@bindir@}
24 : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
25 : ${tripectl=$bindir/tripectl}
26 export TRIPEDIR TRIPESOCK
27
28 ## Make sure we're being called properly, and figure out the peer identity.
29 case "$#,$1" in
30 2,-c) ;;
31 *)
32 echo >&2 "usage: $0 -c '[SERVER:]PEER [hello|goodbye]'"
33 exit 1
34 ;;
35 esac
36
37 ## SSH has smushed all of our arguments together, so let's split them apart
38 ## again.
39 set -- $2
40
41 ## Examine the peer identifier and work out how to proceed.
42 case "$#,$1" in
43 0,*) echo >&2 "$0: missing peer identifier"; exit 1 ;;
44 *:*) mode=proxy server=${1%:*} user=${1##*:} ;;
45 *) mode=local user=$1 ;;
46 esac
47 shift
48
49 ## If there's no action then check to see whether SSH has hidden one
50 ## somewhere. Make sure the command looks sensible.
51 case "$#" in 0) set -- $SSH_ORIGINAL_COMMAND ;; esac
52 case "$#,$1" in
53 0, | 1,hello) act=hello ;;
54 1,goodbye) act=goodbye ;;
55 *) echo >&2 "$0: unknown action spec \`$*'"; exit 1 ;;
56 esac
57
58 ## Now actually do something.
59 case "$mode,$act" in
60 proxy,*)
61 exec ssh "$server" "$user" "$act"
62 ;;
63 local,hello)
64 exec $tripectl SVCSUBMIT connect passive "$user"
65 ;;
66 local,goodbye)
67 peer=$($tripectl SVCSUBMIT connect userpeer "$user")
68 exec $tripectl KILL "$peer"
69 ;;
70 *)
71 echo >&2 "$0: unknown mode/action $mode/$act"
72 exit 1
73 ;;
74 esac