diff options
Diffstat (limited to 'etc/init.d')
-rwxr-xr-x | etc/init.d/anytun | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/etc/init.d/anytun b/etc/init.d/anytun new file mode 100755 index 0000000..93c56f0 --- /dev/null +++ b/etc/init.d/anytun @@ -0,0 +1,192 @@ +#! /bin/sh +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +DAEMON=/usr/sbin/anytun +ANYTUNCONFIG=/usr/bin/anytun-config +CONTROLDAEMON=/usr/bin/anytun-controld +CHROOTDIR='/var/run/anytun' +# DAEMONOPTS="--chroot --username nobody --chroot-dir $CHROOTDIR" +NAME=anytun +DESC=anytun +CONFIG_DIR=/etc/anytun +VARCONFIG_DIR=/var/run/anytun-controld + +test -x $DAEMON || exit 0 + +# Include anytun defaults if available +if [ -f /etc/default/anytun ] ; then + . /etc/default/anytun +fi + +start_vpn () { + STATUS=OK + if [ -f $CONFIG_DIR/$NAME/config ] ; then + POSTUP='' + test -f $CONFIG_DIR/$NAME/post-up.sh && POSTUP="-x $CONFIG_DIR/$NAME/post-up.sh" + DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$NAME/config | grep -e '\w' | sed 's/^/ --/' | xargs echo` + $DAEMON --write-pid /var/run/anytun.$NAME.pid $POSTUP \ + $DAEMONOPTS $DAEMONARG || STATUS="FAILED" + else + STATUS="no config found" + fi + start_configd + echo -n "($STATUS)" +} +stop_vpn () { + kill `cat $PIDFILE` || true + rm $PIDFILE + stop_configd +} + +start_configd () { + if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then + test -d $VARCONFIG_DIR || mkdir -p $VARCONFIG_DIR + chmod 700 $VARCONFIG_DIR + rm -f $VARCONFIG_DIR/$NAME 2>/dev/null + for CLIENTNAME in `ls $CONFIG_DIR/$NAME/conf.d`; do + echo -n " ($CLIENTNAME)" + DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$NAME/conf.d/$CLIENTNAME | grep -e '\w' | sed 's/^/ --/' | xargs echo` + $ANYTUNCONFIG $DAEMONARG >> $VARCONFIG_DIR/$NAME + done + CONTROLHOST=`sed 's/#.*//' < $CONFIG_DIR/$NAME/config | grep -e 'control-host' | sed 's/^/ --/'` + $CONTROLDAEMON -f $VARCONFIG_DIR/$NAME $DAEMONOPTS $CONTROLHOST \ + --write-pid $VARCONFIG_DIR/$NAME.pid + # rm -f $VARCONFIG_DIR/$NAME + fi +} +stop_configd () { + if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then + echo -n " ($NAME)" + kill `cat $VARCONFIG_DIR/$NAME.pid` || true + rm $VARCONFIG_DIR/$NAME.pid + fi +} + +set -e +case "$1" in + start) + echo -n "Starting $DESC: " + test -d $CHROOTDIR || mkdir -p $CHROOTDIR + if test -z "$2" ; then + if [ -f $CONFIG_DIR/autostart ] ; then + for NAME in `sed 's/#.*//' < $CONFIG_DIR/autostart | grep -e '\w'`; do + echo -n "$NAME" + start_vpn + done + else + echo "no config found" + exit 1; + fi + else + while shift ; do + if [ ! -z $1 ] ; then + NAME=$1 + echo -n "$NAME" + start_vpn + fi + done + fi + echo "." + ;; + stop) + echo -n "Stoping $DESC: " + if test -z "$2" ; then + for PIDFILE in `ls /var/run/anytun.*.pid 2> /dev/null`; do + NAME=`echo $PIDFILE | cut -c17-` + NAME=${NAME%%.pid} + echo -n " $NAME" + stop_vpn + done + else + while shift ; do + [ -z "$1" ] && break + if test -e /var/run/anytun.$1.pid ; then + PIDFILE=`ls /var/run/anytun.$1.pid 2> /dev/null` + NAME=`echo $PIDFILE | cut -c17-` + NAME=${NAME%%.pid} + echo -n " $NAME" + stop_vpn + else + echo -n " (failure: No such tunnel is running: $1)" + fi + done + fi + echo . + ;; + reload) + if test -z "$2" ; then + for PIDFILE in `ls /var/run/anytun.*.pid 2> /dev/null`; do + NAME=`echo $PIDFILE | cut -c17-` + NAME=${NAME%%.pid} + if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then + stop_vpn + start_vpn + else + stop_config + start_config + fi + echo -n " $NAME" + done + else + while shift ; do + [ -z "$1" ] && break + if test -e /var/run/anytun.$1.pid ; then + PIDFILE=`ls /var/run/anytun.$1.pid 2> /dev/null` + NAME=`echo $PIDFILE | cut -c17-` + NAME=${NAME%%.pid} + echo -n " $NAME" + if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then + stop_vpn + start_vpn + else + stop_config + start_config + fi + else + echo -n " (failure: No such tunnel is running: $1)" + fi + done + fi + ;; + force-reload) + echo -n "Restarting $DESC: " + if test -z "$2" ; then + for PIDFILE in `ls /var/run/anytun.*.pid 2> /dev/null`; do + NAME=`echo $PIDFILE | cut -c17-` + NAME=${NAME%%.pid} + echo -n " $NAME" + stop_vpn + sleep 1 + start_vpn + done + else + while shift ; do + [ -z "$1" ] && break + if test -e /var/run/anytun.$1.pid ; then + PIDFILE=`ls /var/run/anytun.$1.pid 2> /dev/null` + NAME=`echo $PIDFILE | cut -c17-` + NAME=${NAME%%.pid} + echo -n " $NAME" + stop_vpn + start_vpn + else + echo -n " (failure: No such tunnel is running: $1)" + fi + done + fi + echo . + ;; + restart) + SCRIPT=$0 + shift + $SCRIPT stop $* + sleep 1 + $SCRIPT start $* + ;; + *) + N=/etc/init.d/$NAME + echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 + exit 1 + ;; +esac + +exit 0 |