]> git.pld-linux.org Git - packages/openvpn.git/blob - openvpn.init
edd3be90eeffdc41ddf613f3c6004379fe6d5587
[packages/openvpn.git] / openvpn.init
1 #!/bin/sh
2 #
3 # openvpn       Start/stop the VPN daemon.
4 #
5 # chkconfig:    2345 11 89
6 #
7 # description:  OpenVPN is a robust and highly configurable VPN (Virtual \
8 #               Private Network) daemon
9 #
10
11 # Get service config
12 [ -f /etc/sysconfig/openvpn ] && . /etc/sysconfig/openvpn
13
14 [ -n "$2" ] && TUNNELS="$2"
15
16 # no tunnels. exit silently
17 if [ -z "$TUNNELS" ]; then
18         case "$1" in
19         start|stop|restart|reload|force-reload)
20                 exit 0
21                 ;;
22         esac
23 fi
24
25 # Source function library
26 . /etc/rc.d/init.d/functions
27
28 # Source networking configuration.
29 . /etc/sysconfig/network
30
31 # Check that networking is up.
32 if is_yes "${NETWORKING}"; then
33         if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
34                 msg_network_down OpenVPN
35                 exit 1
36         fi
37 else
38         exit 0
39 fi
40
41 # check if the tunnel $1 is up
42 tunlup() {
43         local tun="$1"
44         local pidfile=/var/run/openvpn/$tun.pid
45         local pid=$(cat $pidfile 2>/dev/null)
46         kill -0 $pid 2>/dev/null
47         return $?
48 }
49
50 # check if all the configured tunnels are up
51 tunlsup() {
52         ret=0
53         for tun in $TUNNELS; do
54                 tunlup $tun && continue
55                 ret=1
56         done
57         return $ret
58 }
59
60 start() {
61         # Check if the service is already running?
62         if ! tunlsup; then
63                 msg_starting "OpenVPN"; started
64                 for tun in $TUNNELS; do
65                         config="/etc/openvpn/$tun.conf"
66                         if [ ! -f "$config" ]; then
67                                 nls "Invalid tunnel \`%s': missing config: %s" $tun "$config"
68                                 fail
69                                 RET=1
70                         else
71                                 show "Starting OpenVPN tunnel %s" "$tun"
72                                 if tunlup $tun; then
73                                         started
74                                         continue
75                                 fi
76
77                                 daemon openvpn --daemon --writepid "/var/run/openvpn/$tun.pid" \
78                                         --config $config --cd /etc/openvpn
79                                 RET=$?
80                         fi
81                         [ $RETVAL -eq 0 ] && RETVAL=$RET
82                 done
83                 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/openvpn
84         else
85                 msg_already_running "OpenVPN"
86         fi
87 }
88
89 stop() {
90         if tunlsup; then
91                 # Stop daemons.
92                 msg_stopping "OpenVPN"; started
93                 for tun in $TUNNELS; do
94                         pidfile=/var/run/openvpn/$tun.pid
95                         [ -f "$pidfile" ] || continue
96                         pid=`cat "$pidfile"`
97                         show "Stopping OpenVPN tunnel %s" "$tun"; busy
98                         killproc --pidfile openvpn/$tun.pid || err=1
99                 done
100                 rm -f /var/lock/subsys/openvpn >/dev/null 2>&1
101         else
102                 msg_not_running "OpenVPN"
103         fi
104 }
105
106 RETVAL=0
107 # See how we were called.
108 case "$1" in
109   start)
110         start
111         ;;
112   stop)
113         stop
114         ;;
115   reload|force-reload)
116         if tunlsup; then
117                 msg_reloading "OpenVPN"; started
118                 for tun in $TUNNELS; do
119                         show "Reloading OpenVPN tunnel %s" "$tun"
120                         killproc --pidfile openvpn/$tun.pid openvpn -HUP
121                         [ $? -ne 0 -a $RETVAL -eq 0 ] && RETVAL=7
122                 done
123         else
124                 msg_not_running OpenVPN
125                 exit 7
126         fi
127         ;;
128   restart)
129         stop
130         sleep 1
131         start
132         exit $?
133         ;;
134   status)
135         nls "Configured tunnels:"
136         echo " $TUNNELS"
137         nls "Currently active tunnels:"
138         for pidfile in /var/run/openvpn/*.pid; do
139                 [ -f "$pidfile" ] || continue
140                 tun=${pidfile#/var/run/openvpn/}
141                 tun=${tun%.pid}
142                 tunlup $tun && echo -n " $tun($(cat $pidfile))"
143         done
144         echo ""
145         tunlsup
146         exit $?
147         ;;
148   *)
149         msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
150         exit 3
151         ;;
152 esac
153
154 exit $RETVAL
This page took 0.043444 seconds and 2 git commands to generate.