[packages] openvpn: stop instances even if they are disabled (#9404)
[openwrt/svn-archive/archive.git] / net / openvpn / files / openvpn.init
1 #!/bin/sh /etc/rc.common
2 # OpenVPN init script
3 # Copyright (C) 2008 Jo-Philipp Wich
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6
7 START=95
8 BIN=/usr/sbin/openvpn
9 SSD=start-stop-daemon
10 EXTRA_COMMANDS="up down"
11
12 LIST_SEP="
13 "
14
15 append_param() {
16 local v="$1"
17 case "$v" in
18 *_*_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
19 *_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
20 *_*) v=${v%%_*}-${v#*_} ;;
21 esac
22 ARGS="$ARGS --$v"
23 return 0
24 }
25
26 append_bools() {
27 local p; local v; local s="$1"; shift
28 for p in $*; do
29 config_get_bool v "$s" "$p"
30 [ "$v" == 1 ] && append_param "$p"
31 done
32 }
33
34 append_params() {
35 local p; local v; local s="$1"; shift
36 for p in $*; do
37 config_get v "$s" "$p"
38 IFS="$LIST_SEP"
39 for v in $v; do
40 [ -n "$v" ] && append_param "$p" && ARGS="$ARGS $v"
41 done
42 unset IFS
43 done
44 }
45
46 append_params_quoted() {
47 local p; local v; local s="$1"; shift
48 for p in $*; do
49 config_get v "$s" "$p"
50 IFS="$LIST_SEP"
51 for v in $v; do
52 [ -n "$v" ] && append_param "$p" && ARGS="$ARGS '$v'"
53 done
54 unset IFS
55 done
56 }
57
58 start_service() {
59 local s="$1"
60 local enable=0
61
62 # disabled?
63 config_get_bool enable "$s" enable 0
64 [ "$enable" == 0 ] && return 0
65
66 PID="/var/run/openvpn-$s.pid"
67 ARGS=""
68
69 # append flags
70 append_bools "$s" \
71 auth_nocache auth_retry auth_user_pass_optional bind ccd_exclusive client client_cert_not_required \
72 client_disconnect client_to_client comp_lzo comp_noadapt disable \
73 disable_occ down_pre duplicate_cn fast_io float http_proxy_retry \
74 ifconfig_noexec ifconfig_nowarn ifconfig_pool_linear management_forget_disconnect management_hold \
75 management_query_passwords management_signal mktun mlock mtu_test multihome mute_replay_warnings \
76 nobind no_iv no_name_remapping no_replay opt_verify passtos persist_key persist_local_ip \
77 persist_remote_ip persist_tun ping_timer_rem pull push_reset \
78 remote_random rmtun route_noexec route_nopull single_session socks_proxy_retry \
79 suppress_timestamps tcp_nodelay test_crypto tls_client tls_exit tls_server \
80 tun_ipv6 up_delay up_restart username_as_common_name
81
82 # append params
83 append_params "$s" \
84 cd askpass auth auth_user_pass auth_user_pass_verify bcast_buffers ca cert \
85 chroot cipher client_config_dir client_connect config connect_freq \
86 connect_retry connect_timeout connect_retry_max crl_verify dev dev_node dev_type dh \
87 echo engine explicit_exit_notify fragment group hand_window hash_size \
88 http_proxy http_proxy_option http_proxy_timeout ifconfig ifconfig_pool \
89 ifconfig_pool_persist ifconfig_push inactive ipchange iroute keepalive \
90 key key_method keysize learn_address link_mtu lladdr local log log_append \
91 lport management management_log_cache max_clients \
92 max_routes_per_client mode mssfix mtu_disc mute nice ns_cert_type ping \
93 ping_exit ping_restart pkcs12 plugin port port_share prng proto rcvbuf \
94 redirect_gateway remap_usr1 remote remote_cert_eku remote_cert_ku remote_cert_tls \
95 reneg_bytes reneg_pkts reneg_sec \
96 replay_persist replay_window resolv_retry route route_delay route_gateway \
97 route_metric route_up rport script_security secret server server_bridge setenv shaper sndbuf \
98 socks_proxy status status_version syslog tcp_queue_limit tls_auth \
99 tls_cipher tls_remote tls_timeout tls_verify tmp_dir topology tran_window \
100 tun_mtu tun_mtu_extra txqueuelen user verb
101
102 # append multi-value params
103 append_params_quoted "$s" \
104 down push up
105
106
107 [ -n "$ARGS" ] && \
108 eval "$SSD -q -b -p '$PID' -x $BIN -S -- --syslog 'openvpn($s)' --writepid '$PID' $ARGS"
109 }
110
111 kill_service() {
112 local s="$1"
113 local S="${2:-TERM}"
114 local enable=0
115
116 # disabled?
117 config_get_bool enable "$s" enable 0
118 [ "$enable" == 1 ] || [ "$S" == "TERM" ] || return 0
119
120 PID="/var/run/openvpn-$s.pid"
121
122 $SSD -q -p $PID -x $BIN -K -s $S
123 [ "$S" == "TERM" ] && rm -f "$PID"
124 }
125
126 hup_service() { kill_service "$1" HUP; }
127 stop_service() { kill_service "$1" TERM; }
128
129 start() {
130 config_load openvpn
131 config_foreach start_service openvpn
132 }
133
134 stop() {
135 config_load openvpn
136 config_foreach stop_service openvpn
137 }
138
139 reload() {
140 config_load openvpn
141 config_foreach hup_service openvpn
142 }
143
144 restart() {
145 stop; sleep 5; start
146 }
147
148 up() {
149 local exists
150 local INSTANCE
151 config_load openvpn
152 for INSTANCE in "$@"; do
153 config_get exists "$INSTANCE" TYPE
154 if [ "$exists" == "openvpn" ]; then
155 start_service "$INSTANCE"
156 fi
157 done
158 }
159
160 down() {
161 local exists
162 local INSTANCE
163 config_load openvpn
164 for INSTANCE in "$@"; do
165 config_get exists "$INSTANCE" TYPE
166 if [ "$exists" == "openvpn" ]; then
167 stop_service "$INSTANCE"
168 fi
169 done
170 }