summaryrefslogtreecommitdiff
path: root/debian/openvpn-systemd-helper
blob: be03932944dab5e015573cf8b0c9fdd6efdb59dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/sh
# Copyright (C) 2014 Alberto Gonzalez Iniesta <agi@inittab.org>
#
#  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
# 

echo "$*" > /tmp/ovpn

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
  ;;
restart)
  for NAME in $VPNS ; do
    systemctl restart openvpn@${NAME}
    [ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; }
  done
  ;;
cond-restart|soft-restart)
  for NAME in $VPNS ; do
    systemctl condrestart openvpn@${NAME}
    [ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; }
  done
  ;;
*)
  echo "Usage: $0 {start|stop|reload|restart|cond-restart}" >&2
  exit 1
  ;;
esac

exit $STATUS

# vim:set ai sts=2 sw=2 tw=0: