#!/bin/sh # Copyright (C) 2014 Alberto Gonzalez Iniesta # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # This scripts tries to mimic the behaviour of the SysV init script # in systemd. It's an alternative to declaring an instance for each # VPN you want to start on boot # test $DEBIAN_SCRIPT_DEBUG && set -v -x DAEMON=/usr/sbin/openvpn CONFIG_DIR=/etc/openvpn test -x $DAEMON || exit 0 test -d $CONFIG_DIR || exit 0 # Source defaults file; edit that file to configure this script. AUTOSTART="all" STATUSREFRESH=10 STATUS=0 VPNS="" if test -e /etc/default/openvpn ; then . /etc/default/openvpn fi # Nothing to do if no VPN is set in AUTOSTART if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then exit 0 fi # Create list with VPNs to be managed if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then # all of them for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do NAME=${CONFIG%%.conf} VPNS="$VPNS $NAME" done else # only manage specified VPNs for NAME in $AUTOSTART ; do [ -e "${CONFIG_DIR}/${NAME}.conf" ] && VPNS="$VPNS $NAME" done fi case "$1" in start) for NAME in $VPNS ; do systemctl start openvpn@${NAME} [ "$?" -ne 0 ] && { echo "Error starting $NAME"; STATUS=1; } done ;; stop) for NAME in $VPNS ; do systemctl stop openvpn@${NAME} [ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; } done ;; # Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'. reload) for NAME in $VPNS ; do # If openvpn if running under a different user than root we'll need to restart if egrep '^[[:blank:]]*user[[:blank:]]' $CONFIG_DIR/$NAME.conf > /dev/null 2>&1 ; then systemctl restart openvpn@${NAME} [ "$?" -ne 0 ] && { echo "Error restarting $NAME"; STATUS=1; } else systemctl reload openvpn@${NAME} fi done ;; *) echo "Usage: $0 {start|stop|reload}" >&2 exit 1 ;; esac exit $STATUS # vim:set ai sts=2 sw=2 tw=0: