#!/bin/sh # loop though arguments for cmd in $SSH_ORIGINAL_COMMAND do # set command flags case "$cmd" in "publish") PUBLISH=1;; "other") OTHER=1;; *) ARG="$cmd";; # the last unkown arg is taken, others are ignored esac done # This approach allows to specify multiple commands and work on them # with the same argument in a define sequence. When "ssh user@server # other publish path" is specified, then the publish event and then # the other event would be processed and both have access to arg. The # parameter sequence doesn't even matter: ssh user@server path publish # would work just as well. # To allow only one event to be specified (publish OR other, but not both): if [ $(( PUBLISH + OTHER )) != 1 ] then echo "Sorry dave, I can't do that. Please specify exactly one event."; echo "Events: - publish < file.tar" echo " - other ..." exit 1 fi # process events in sequence if [ "$PUBLISH" == "1" ] then if test -t 0 then echo "The publish event needs STDIN data, but there was none. Cannot continue" exit 1 fi ARG="$(echo "$ARG" | col -b | tr -d "\"|\\\;(){}'%&[]!?")" # sanitise untrusted argument echo mkdir -p "/projects/$ARG" echo tar xzf - -C "/projects/$ARG/" # || exit 1; in case it's better to abort on a failed extract than continuing with the OTHER event... # another way would be to first save stdin to a file (mktemp!) then check if file -ib returns "application/x-tar" and abort early before # creating the directory or trying to extract. When handing temporary files, install a signal handler (trap), so the script doesn't leave # temp files behind when aborting. # Example: # trap cleanup 1 2 3 4 5 6 11 15 # signal to catch # cleanup() { rm -f "/tmp/$file"; } # function that's called when the signal was caught fi if [ "$OTHER" == "1" ] then echo "Do something else with $ARG" fi