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