[packages] openvpn: stop instances even if they are disabled (#9404)
[openwrt.org/packages.git] / net / openvpn / files / openvpn.init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/sh /etc/rc.common
# OpenVPN init script
# Copyright (C) 2008 Jo-Philipp Wich
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.

START=95
BIN=/usr/sbin/openvpn
SSD=start-stop-daemon
EXTRA_COMMANDS="up down"

LIST_SEP="
"

append_param() {
	local v="$1"
	case "$v" in
		*_*_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
		*_*_*)   v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
		*_*)     v=${v%%_*}-${v#*_} ;;
	esac
	ARGS="$ARGS --$v"
	return 0
}

append_bools() {
	local p; local v; local s="$1"; shift
	for p in $*; do
		config_get_bool v "$s" "$p"
		[ "$v" == 1 ] && append_param "$p"
	done
}

append_params() {
	local p; local v; local s="$1"; shift
	for p in $*; do
		config_get v "$s" "$p"
		IFS="$LIST_SEP"
		for v in $v; do
			[ -n "$v" ] && append_param "$p" && ARGS="$ARGS $v"
		done
		unset IFS
	done
}

append_params_quoted() {
	local p; local v; local s="$1"; shift
	for p in $*; do
		config_get v "$s" "$p"
		IFS="$LIST_SEP"
		for v in $v; do
			[ -n "$v" ] && append_param "$p" && ARGS="$ARGS '$v'"
		done
		unset IFS
	done
}

start_service() {
	local s="$1"
	local enable=0

	# disabled?
	config_get_bool enable "$s" enable 0
	[ "$enable" == 0 ] && return 0

	PID="/var/run/openvpn-$s.pid"
	ARGS=""

	# append flags
	append_bools "$s" \
		auth_nocache auth_retry auth_user_pass_optional bind ccd_exclusive client client_cert_not_required \
		client_disconnect client_to_client comp_lzo comp_noadapt disable \
		disable_occ down_pre duplicate_cn fast_io float http_proxy_retry \
		ifconfig_noexec ifconfig_nowarn ifconfig_pool_linear management_forget_disconnect management_hold \
		management_query_passwords management_signal mktun mlock mtu_test multihome mute_replay_warnings \
		nobind no_iv no_name_remapping no_replay opt_verify passtos persist_key persist_local_ip \
		persist_remote_ip persist_tun ping_timer_rem pull push_reset \
		remote_random rmtun route_noexec route_nopull single_session socks_proxy_retry \
		suppress_timestamps tcp_nodelay test_crypto tls_client tls_exit tls_server \
		tun_ipv6 up_delay up_restart username_as_common_name

	# append params
	append_params "$s" \
		cd askpass auth auth_user_pass auth_user_pass_verify bcast_buffers ca cert \
		chroot cipher client_config_dir client_connect config connect_freq \
		connect_retry connect_timeout connect_retry_max crl_verify dev dev_node dev_type dh \
		echo engine explicit_exit_notify fragment group hand_window hash_size \
		http_proxy http_proxy_option http_proxy_timeout ifconfig ifconfig_pool \
		ifconfig_pool_persist ifconfig_push inactive ipchange iroute keepalive \
		key key_method keysize learn_address link_mtu lladdr local log log_append \
		lport management management_log_cache max_clients \
		max_routes_per_client mode mssfix mtu_disc mute nice ns_cert_type ping \
		ping_exit ping_restart pkcs12 plugin port port_share prng proto rcvbuf \
		redirect_gateway remap_usr1 remote remote_cert_eku remote_cert_ku remote_cert_tls \
		reneg_bytes reneg_pkts reneg_sec \
		replay_persist replay_window resolv_retry route route_delay route_gateway \
		route_metric route_up rport script_security secret server server_bridge setenv shaper sndbuf \
		socks_proxy status status_version syslog tcp_queue_limit tls_auth \
		tls_cipher tls_remote tls_timeout tls_verify tmp_dir topology tran_window \
		tun_mtu tun_mtu_extra txqueuelen user verb

	# append multi-value params
	append_params_quoted "$s" \
		down push up


	[ -n "$ARGS" ] && \
		eval "$SSD -q -b -p '$PID' -x $BIN -S -- --syslog 'openvpn($s)' --writepid '$PID' $ARGS"
}

kill_service() {
	local s="$1"
	local S="${2:-TERM}"
	local enable=0

	# disabled?
	config_get_bool enable "$s" enable 0
	[ "$enable" == 1 ] || [ "$S" == "TERM" ] || return 0

	PID="/var/run/openvpn-$s.pid"

	$SSD -q -p $PID -x $BIN -K -s $S
	[ "$S" == "TERM" ] && rm -f "$PID"
}

hup_service()  { kill_service "$1" HUP;  }
stop_service() { kill_service "$1" TERM; }

start() {
	config_load openvpn
	config_foreach start_service openvpn
}

stop() {
	config_load openvpn
	config_foreach stop_service openvpn
}

reload() {
	config_load openvpn
	config_foreach hup_service openvpn
}

restart() {
	stop; sleep 5; start
}

up() {
	local exists
	local INSTANCE
	config_load openvpn
	for INSTANCE in "$@"; do
		config_get exists "$INSTANCE" TYPE
		if [ "$exists" == "openvpn" ]; then
			start_service "$INSTANCE"
		fi
	done
}

down() {
	local exists
	local INSTANCE
	config_load openvpn
	for INSTANCE in "$@"; do
		config_get exists "$INSTANCE" TYPE
		if [ "$exists" == "openvpn" ]; then
			stop_service "$INSTANCE"
		fi
	done
}

comments