]>
Commit | Line | Data |
---|---|---|
d84d9290 JK |
1 | #!/bin/sh |
2 | # | |
bad028c1 | 3 | # openvpn Start/stop the VPN daemon. |
d84d9290 | 4 | # |
3dfa4b8b | 5 | # chkconfig: 2345 11 89 |
d84d9290 | 6 | # |
086da148 | 7 | # description: OpenVPN is a robust and highly configurable VPN (Virtual \ |
bad028c1 | 8 | # Private Network) daemon |
d84d9290 JK |
9 | # |
10 | ||
e30ecdce ER |
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 | ||
d84d9290 JK |
25 | # Source function library |
26 | . /etc/rc.d/init.d/functions | |
27 | ||
b585287a | 28 | # Source networking configuration. |
29 | . /etc/sysconfig/network | |
30 | ||
31 | # Check that networking is up. | |
32 | if is_yes "${NETWORKING}"; then | |
cb86f1a1 | 33 | if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then |
b585287a | 34 | msg_network_down OpenVPN |
35 | exit 1 | |
36 | fi | |
37 | else | |
38 | exit 0 | |
39 | fi | |
40 | ||
b7a1516e | 41 | # check if the tunnel $1 is up |
bdda45fd | 42 | tunlup() { |
79d8ce42 ER |
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 | ||
ec236b38 | 50 | # check if all the configured tunnels are up |
79d8ce42 | 51 | tunlsup() { |
bdda45fd ER |
52 | ret=0 |
53 | for tun in $TUNNELS; do | |
99cca032 | 54 | tunlup $tun && continue |
bdda45fd ER |
55 | ret=1 |
56 | done | |
57 | return $ret | |
58 | } | |
59 | ||
058f90d9 | 60 | start() { |
d84d9290 | 61 | # Check if the service is already running? |
99cca032 | 62 | if ! tunlsup; then |
bdda45fd | 63 | msg_starting "OpenVPN"; started |
bdda45fd | 64 | for tun in $TUNNELS; do |
fa49419c ER |
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 | ||
35e2dd9b | 77 | daemon --pidfile /var/run/openvpn/$tun.pid /usr/sbin/openvpn --daemon --writepid /var/run/openvpn/$tun.pid \ |
d933e9ce | 78 | --config $config --cd /etc/openvpn ${OPENVPN_OPT} |
fa49419c | 79 | RET=$? |
bdda45fd | 80 | fi |
328e5c61 | 81 | [ $RETVAL -eq 0 ] && RETVAL=$RET |
d84d9290 | 82 | done |
328e5c61 | 83 | [ $RETVAL -eq 0 ] && touch /var/lock/subsys/openvpn |
d84d9290 | 84 | else |
026907a7 | 85 | msg_already_running "OpenVPN" |
d84d9290 | 86 | fi |
058f90d9 ER |
87 | } |
88 | ||
89 | stop() { | |
99cca032 | 90 | if tunlsup; then |
b585287a | 91 | # Stop daemons. |
bad028c1 | 92 | msg_stopping "OpenVPN"; started |
bdda45fd ER |
93 | for tun in $TUNNELS; do |
94 | pidfile=/var/run/openvpn/$tun.pid | |
d84d9290 JK |
95 | [ -f "$pidfile" ] || continue |
96 | pid=`cat "$pidfile"` | |
ebc0a4d6 | 97 | show "Stopping OpenVPN tunnel %s" "$tun"; busy |
bdda45fd | 98 | killproc --pidfile openvpn/$tun.pid || err=1 |
d84d9290 | 99 | done |
bad028c1 | 100 | rm -f /var/lock/subsys/openvpn >/dev/null 2>&1 |
101 | else | |
102 | msg_not_running "OpenVPN" | |
bad028c1 | 103 | fi |
058f90d9 ER |
104 | } |
105 | ||
106 | RETVAL=0 | |
107 | # See how we were called. | |
108 | case "$1" in | |
109 | start) | |
110 | start | |
d84d9290 | 111 | ;; |
058f90d9 ER |
112 | stop) |
113 | stop | |
d84d9290 | 114 | ;; |
ede0a906 | 115 | reload|force-reload) |
99cca032 | 116 | if tunlsup; then |
bdda45fd ER |
117 | msg_reloading "OpenVPN"; started |
118 | for tun in $TUNNELS; do | |
119 | show "Reloading OpenVPN tunnel %s" "$tun" | |
a45908a0 | 120 | killproc --pidfile openvpn/$tun.pid openvpn -HUP |
328e5c61 | 121 | [ $? -ne 0 -a $RETVAL -eq 0 ] && RETVAL=7 |
b585287a | 122 | done |
8d6fb2d1 | 123 | else |
bf3cdf1b | 124 | msg_not_running OpenVPN |
8d6fb2d1 ER |
125 | exit 7 |
126 | fi | |
d84d9290 JK |
127 | ;; |
128 | restart) | |
058f90d9 | 129 | stop |
d84d9290 | 130 | sleep 1 |
058f90d9 ER |
131 | start |
132 | exit $? | |
133 | ;; | |
134 | status) | |
135 | nls "Configured tunnels:" | |
136 | echo " $TUNNELS" | |
137 | nls "Currently active tunnels:" | |
ec236b38 ER |
138 | for pidfile in /var/run/openvpn/*.pid; do |
139 | [ -f "$pidfile" ] || continue | |
140 | tun=${pidfile#/var/run/openvpn/} | |
141 | tun=${tun%.pid} | |
b9d252c1 | 142 | tunlup $tun && echo -n " $tun($(cat $pidfile))" |
058f90d9 ER |
143 | done |
144 | echo "" | |
a568127e ER |
145 | nm_ovpn_pid=$(ps -o pid= -C nm-openvpn-service) |
146 | if [ "$nm_ovpn_pid" ]; then | |
147 | nls "NM ($nm_ovpn_pid) managed OpenVPN sessions" | |
148 | ps -o pid,user,command --ppid=$nm_ovpn_pid | |
149 | fi | |
ec236b38 | 150 | tunlsup |
ede0a906 | 151 | exit $? |
d84d9290 JK |
152 | ;; |
153 | *) | |
ede0a906 | 154 | msg_usage "$0 {start|stop|restart|reload|force-reload|status}" |
155 | exit 3 | |
92e35fb4 | 156 | ;; |
d84d9290 JK |
157 | esac |
158 | ||
159 | exit $RETVAL |