chiark / gitweb /
Explicit close-down notifications.
[tripe] / contrib / knock.in
index be55f2b95a0496b312842a6637c001fcf52cd162..28d8e532d2d80a1557bbfad7ec72a10fbf2b3e1c 100755 (executable)
@@ -25,23 +25,50 @@ set -e
 : ${tripectl=$bindir/tripectl}
 export TRIPEDIR TRIPESOCK
 
-case "$#,$1,$2" in
-
-  2,-c,*:*)
-    ## Proxy through to another server.
-    server=${2%:*} user=${2##*:}
-    exec ssh "$server" "$user"
+## Make sure we're being called properly, and figure out the peer identity.
+case "$#,$1" in
+  2,-c) ;;
+  *)
+    echo >&2 "usage: $0 -c '[SERVER:]PEER [hello|goodbye]'"
+    exit 1
     ;;
+esac
 
-  2,-c,*)
-    ## Connect to the local tripe server.
-    exec $tripectl SVCSUBMIT connect passive "$2"
-    ;;
+## SSH has smushed all of our arguments together, so let's split them apart
+## again.
+set -- $2
+
+## Examine the peer identifier and work out how to proceed.
+case "$#,$1" in
+  0,*) echo >&2 "$0: missing peer identifier"; exit 1 ;;
+  *:*) mode=proxy server=${1%:*} user=${1##*:} ;;
+  *) mode=local user=$1 ;;
+esac
+shift
 
+## If there's no action then check to see whether SSH has hidden one
+## somewhere.  Make sure the command looks sensible.
+case "$#" in 0) set -- $SSH_ORIGINAL_COMMAND ;; esac
+case "$#,$1" in
+  0, | 1,hello) act=hello ;;
+  1,goodbye) act=goodbye ;;
+  *) echo >&2 "$0: unknown action spec \`$*'"; exit 1 ;;
+esac
+
+## Now actually do something.
+case "$mode,$act" in
+  proxy,*)
+    exec ssh "$server" "$user" "$act"
+    ;;
+  local,hello)
+    exec $tripectl SVCSUBMIT connect passive "$user"
+    ;;
+  local,goodbye)
+    peer=$($tripectl SVCSUBMIT connect userpeer "$user")
+    exec $tripectl KILL "$peer"
+    ;;
   *)
-    ## Anything else is an error.
-    echo >&2 "usage: $0 -c [SERVER:]PEER"
+    echo >&2 "$0: unknown mode/action $mode/$act"
     exit 1
     ;;
-
 esac