]> TLD Linux GIT Repositories - packages/openvpn.git/blob - openvpn.init
- updated to 2.4.0, PLD merge
[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 if the tunnel $1 is up
32 tunlup() {
33         local tun="$1"
34         local pidfile=/var/run/openvpn/$tun.pid
35         local pid=$(cat $pidfile 2>/dev/null)
36         kill -0 $pid 2>/dev/null
37         return $?
38 }
39
40 # check if all the configured tunnels are up
41 tunlsup() {
42         ret=0
43         for tun in $TUNNELS; do
44                 tunlup $tun && continue
45                 ret=1
46         done
47         return $ret
48 }
49
50 start() {
51         # Check if the service is already running?
52         if tunlsup; then
53                 msg_already_running "OpenVPN"
54                 return
55         fi
56
57         msg_starting "OpenVPN"; started
58         for tun in $TUNNELS; do
59                 config="/etc/openvpn/$tun.conf"
60                 if [ ! -f "$config" ]; then
61                         nls "Invalid tunnel \`%s': missing config: %s" $tun "$config"
62                         fail
63                         RET=1
64                 else
65                         show "Starting OpenVPN tunnel %s" "$tun"
66                         if tunlup $tun; then
67                                 started
68                                 continue
69                         fi
70
71                         daemon --pidfile /var/run/openvpn/$tun.pid /usr/sbin/openvpn --daemon --writepid /var/run/openvpn/$tun.pid \
72                                 --config $config --cd /etc/openvpn ${OPENVPN_OPT}
73                         RET=$?
74                 fi
75                 [ $RETVAL -eq 0 ] && RETVAL=$RET
76         done
77         [ $RETVAL -eq 0 ] && touch /var/lock/subsys/openvpn
78 }
79
80 stop() {
81         if ! tunlsup; then
82                 msg_not_running "OpenVPN"
83                 return
84         fi
85
86         # Stop daemons.
87         msg_stopping "OpenVPN"; started
88         for tun in $TUNNELS; do
89                 pidfile=/var/run/openvpn/$tun.pid
90                 [ -f "$pidfile" ] || continue
91                 pid=`cat "$pidfile"`
92                 show "Stopping OpenVPN tunnel %s" "$tun"; busy
93                 killproc --pidfile openvpn/$tun.pid || err=1
94         done
95         rm -f /var/lock/subsys/openvpn >/dev/null 2>&1
96 }
97
98 reload() {
99         if ! tunlsup; then
100                 msg_not_running "OpenVPN"
101                 RETVAL=7
102                 return
103         fi
104
105         msg_reloading "OpenVPN"; started
106         for tun in $TUNNELS; do
107                 show "Reloading OpenVPN tunnel %s" "$tun"
108                 killproc --pidfile openvpn/$tun.pid openvpn -HUP
109                 [ $? -ne 0 -a $RETVAL -eq 0 ] && RETVAL=7
110         done
111 }
112
113 status() {
114         nls "Configured tunnels:"
115         echo " $TUNNELS"
116         nls "Currently active tunnels:"
117         for pidfile in /var/run/openvpn/*.pid; do
118                 [ -f "$pidfile" ] || continue
119                 tun=${pidfile#/var/run/openvpn/}
120                 tun=${tun%.pid}
121                 tunlup $tun && echo -n " $tun($(cat $pidfile))"
122         done
123         echo ""
124         nm_ovpn_pid=$(ps -o pid= -C nm-openvpn-service | xargs)
125         if [ "$nm_ovpn_pid" ]; then
126                 nls "NM ($nm_ovpn_pid) managed OpenVPN sessions"
127                 ps -o pid,user,command --ppid=$nm_ovpn_pid
128         fi
129         tunlsup
130         RETVAL=$?
131 }
132
133 RETVAL=0
134 # See how we were called.
135 case "$1" in
136   start)
137         start
138         ;;
139   stop)
140         stop
141         ;;
142   reload|force-reload)
143         reload
144         ;;
145   restart)
146         stop
147         sleep 1
148         start
149         ;;
150   status)
151         status
152         ;;
153   *)
154         msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
155         exit 3
156         ;;
157 esac
158
159 exit $RETVAL