ffpi-tools/init.d/fastd

213 lines
5.8 KiB
Plaintext
Raw Normal View History

2016-10-03 10:25:27 +02:00
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: fastd
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Should-Start: network-manager
# Should-Stop: network-manager
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Fast and Secure Tunneling Daemon
# Description: This script will start fastd tunnels as specified
# in /etc/default/fastd and /etc/fastd/*/fastd.conf
### END INIT INFO
# Derived from the OpenVPN init script
# Original version by Robert Leslie
# <rob@mars.org>, edited by iwj and cs
# Modified for openvpn by Alberto Gonzalez Iniesta <agi@inittab.org>
# Modified for restarting / starting / stopping single tunnels by Richard Mueller <mueller@teamix.net>
# Modified for fastd by Nils Schneider <nils@nilsschneider.net>
# Beautified by Thomas Hooge <pirat@hoogi.de>
. /lib/lsb/init-functions
test $DEBIAN_SCRIPT_DEBUG && set -v -x
DESC="Fast and Secure Tunneling Daemon"
NAME=fastd
DAEMON=/usr/local/bin/$NAME
CONFIG_DIR=/etc/fastd
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Exit if the configuration directory is not existent
[ -d $CONFIG_DIR ] || exit 0
# Source defaults file; edit that file to configure this script.
AUTOSTART="all"
if test -e /etc/default/$NAME ; then
. /etc/default/$NAME
fi
start_vpn () {
STATUS=0
start-stop-daemon --start --quiet --oknodo \
--pidfile /var/run/fastd/$INSTANCE.pid \
--make-pidfile \
--background \
--exec $DAEMON -- \
--syslog-level info \
--config $CONFIG_DIR/$INSTANCE/fastd.conf \
|| STATUS=1
}
stop_vpn () {
kill `cat $PIDFILE` || true
rm -f $PIDFILE
log_end_msg 0
}
case "$1" in
start)
log_action_begin_msg "Starting $DESC"
# autostart fastds
if test -z "$2" ; then
# check if automatic startup is disabled by AUTOSTART=none
if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then
log_warning_msg " Autostart disabled, no $NAME will be started."
exit 0
fi
if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
# all fastds shall be started automatically
for CONFIG in `cd $CONFIG_DIR; ls */fastd.conf 2> /dev/null`; do
INSTANCE=${CONFIG%%/fastd.conf}
log_daemon_msg " Autostarting $NAME '$INSTANCE'"
start_vpn
done
else
# start only specified fastds
for INSTANCE in $AUTOSTART ; do
if test -e $CONFIG_DIR/$INSTANCE/fastd.conf ; then
log_daemon_msg " Autostarting $NAME '$INSTANCE'"
start_vpn
else
log_failure_msg " Autostarting $NAME '$INSTANCE': missing $CONFIG_DIR/$INSTANCE/fastd.conf file!"
STATUS=1
fi
done
fi
#start fastds from command line
else
while shift ; do
[ -z "$1" ] && break
INSTANCE=$1
if test -e $CONFIG_DIR/$INSTANCE/fastd.conf ; then
log_daemon_msg " Starting $NAME '$INSTANCE'"
start_vpn
else
log_failure_msg " Starting $NAME '$INSTANCE': missing $CONFIG_DIR/$INSTANCE/fastd.conf file!"
STATUS=1
fi
done
fi
log_end_msg ${STATUS:-0}
;;
stop)
log_action_begin_msg "Stopping $DESC"
if test -z "$2" ; then
PIDFILE=
for PIDFILE in `ls /var/run/fastd/*.pid 2> /dev/null`; do
INSTANCE=`echo $PIDFILE | cut -c16-`
INSTANCE=${INSTANCE%%.pid}
log_daemon_msg " Stopping $NAME '$INSTANCE'"
stop_vpn
done
if test -z "$PIDFILE" ; then
log_warning_msg " No $NAME is running."
fi
else
while shift ; do
[ -z "$1" ] && break
if test -e /var/run/fastd/$1.pid ; then
log_daemon_msg " Stopping $NAME '$1'"
PIDFILE=`ls /var/run/fastd/$1.pid 2> /dev/null`
INSTANCE=`echo $PIDFILE | cut -c16-`
INSTANCE=${INSTANCE%%.pid}
stop_vpn
else
log_failure_msg " Stopping $NAME '$1': No such $NAME is running."
fi
done
fi
;;
restart)
shift
$0 stop ${@}
sleep 1
$0 start ${@}
;;
status)
GLOBAL_STATUS=0
if test -z "$2" ; then
# We want status for all defined fastd.
# Returns success if all autostarted fastds are defined and running
if test "x$AUTOSTART" = "xnone" ; then
# Consider it a failure if AUTOSTART=none
log_warning_msg "No fastds autostarted"
GLOBAL_STATUS=1
else
if ! test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
# Consider it a failure if one of the autostarted fastd is not defined
for VPN in $AUTOSTART ; do
if ! test -f $CONFIG_DIR/$VPN/fastd.conf ; then
log_warning_msg "fastd '$VPN' is in AUTOSTART but is not defined"
GLOBAL_STATUS=1
fi
done
fi
fi
for CONFIG in `cd $CONFIG_DIR; ls */fastd.conf 2> /dev/null`; do
INSTANCE=${CONFIG%%/fastd.conf}
# Is it an autostarted fastd?
if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
AUTOVPN=1
else
if test "x$AUTOSTART" = "xnone" ; then
AUTOVPN=0
else
AUTOVPN=0
for VPN in $AUTOSTART; do
if test "x$VPN" = "x$NAME" ; then
AUTOVPN=1
fi
done
fi
fi
if test "x$AUTOVPN" = "x1" ; then
# If it is autostarted, then it contributes to global status
status_of_proc -p /var/run/fastd/${INSTANCE}.pid $NAME "$NAME '${INSTANCE}'" || GLOBAL_STATUS=1
else
status_of_proc -p /var/run/fastd/${INSTANCE}.pid $NAME "$NAME '${INSTANCE}' (non autostarted)" || true
fi
done
else
# We just want status for specified fastd.
# Returns success if all specified fastds are defined and running
while shift ; do
[ -z "$1" ] && break
INSTANCE=$1
if test -e $CONFIG_DIR/$INSTANCE/fastd.conf ; then
# Config exists
status_of_proc -p /var/run/fastd/${INSTANCE}.pid $NAME "$NAME '${INSTANCE}'" || GLOBAL_STATUS=1
else
# Config does not exist
log_warning_msg "$NAME '$INSTANCE': missing $CONFIG_DIR/$INSTANCE/fastd.conf file!"
GLOBAL_STATUS=1
fi
done
fi
exit $GLOBAL_STATUS
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0