]> git.pld-linux.org Git - packages/openvpn.git/blob - openvpn.init
- updated to 2.1.4
[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_already_running "OpenVPN"
64                 return
65         fi
66
67         msg_starting "OpenVPN"; started
68         for tun in $TUNNELS; do
69                 config="/etc/openvpn/$tun.conf"
70                 if [ ! -f "$config" ]; then
71                         nls "Invalid tunnel \`%s': missing config: %s" $tun "$config"
72                         fail
73                         RET=1
74                 else
75                         show "Starting OpenVPN tunnel %s" "$tun"
76                         if tunlup $tun; then
77                                 started
78                                 continue
79                         fi
80
81                         daemon --pidfile /var/run/openvpn/$tun.pid /usr/sbin/openvpn --daemon --writepid /var/run/openvpn/$tun.pid \
82                                 --config $config --cd /etc/openvpn ${OPENVPN_OPT}
83                         RET=$?
84                 fi
85                 [ $RETVAL -eq 0 ] && RETVAL=$RET
86         done
87         [ $RETVAL -eq 0 ] && touch /var/lock/subsys/openvpn
88 }
89
90 stop() {
91         if ! tunlsup; then
92                 msg_not_running "OpenVPN"
93                 return
94         fi
95
96         # Stop daemons.
97         msg_stopping "OpenVPN"; started
98         for tun in $TUNNELS; do
99                 pidfile=/var/run/openvpn/$tun.pid
100                 [ -f "$pidfile" ] || continue
101                 pid=`cat "$pidfile"`
102                 show "Stopping OpenVPN tunnel %s" "$tun"; busy
103                 killproc --pidfile openvpn/$tun.pid || err=1
104         done
105         rm -f /var/lock/subsys/openvpn >/dev/null 2>&1
106 }
107
108 reload() {
109         if ! tunlsup; then
110                 msg_not_running "OpenVPN"
111                 RETVAL=7
112                 return
113         fi
114
115         msg_reloading "OpenVPN"; started
116         for tun in $TUNNELS; do
117                 show "Reloading OpenVPN tunnel %s" "$tun"
118                 killproc --pidfile openvpn/$tun.pid openvpn -HUP
119                 [ $? -ne 0 -a $RETVAL -eq 0 ] && RETVAL=7
120         done
121 }
122
123 status() {
124         nls "Configured tunnels:"
125         echo " $TUNNELS"
126         nls "Currently active tunnels:"
127         for pidfile in /var/run/openvpn/*.pid; do
128                 [ -f "$pidfile" ] || continue
129                 tun=${pidfile#/var/run/openvpn/}
130                 tun=${tun%.pid}
131                 tunlup $tun && echo -n " $tun($(cat $pidfile))"
132         done
133         echo ""
134         nm_ovpn_pid=$(ps -o pid= -C nm-openvpn-service | xargs)
135         if [ "$nm_ovpn_pid" ]; then
136                 nls "NM ($nm_ovpn_pid) managed OpenVPN sessions"
137                 ps -o pid,user,command --ppid=$nm_ovpn_pid
138         fi
139         tunlsup
140         RETVAL=$?
141 }
142
143 RETVAL=0
144 # See how we were called.
145 case "$1" in
146   start)
147         start
148         ;;
149   stop)
150         stop
151         ;;
152   reload|force-reload)
153         reload
154         ;;
155   restart)
156         stop
157         sleep 1
158         start
159         ;;
160   status)
161         status
162         ;;
163   *)
164         msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
165         exit 3
166         ;;
167 esac
168
169 exit $RETVAL
This page took 0.086944 seconds and 3 git commands to generate.