c58f005f3651433278f5d7bb9d99999bb43f281c
[openwrt.git] / package / network / services / openvpn / files / openvpn.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2011 OpenWrt.org
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
9 SERVICE_DAEMONIZE=1
10 SERVICE_WRITE_PID=1
11
12 EXTRA_COMMANDS="up down"
13
14 LIST_SEP="
15 "
16
17 append_param() {
18         local v="$1"
19         case "$v" in
20                 *_*_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
21                 *_*_*)   v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
22                 *_*)     v=${v%%_*}-${v#*_} ;;
23         esac
24         ARGS="$ARGS --$v"
25         return 0
26 }
27
28 append_bools() {
29         local p; local v; local s="$1"; shift
30         for p in $*; do
31                 config_get_bool v "$s" "$p"
32                 [ "$v" == 1 ] && append_param "$p"
33         done
34 }
35
36 append_params() {
37         local p; local v; local s="$1"; shift
38         for p in $*; do
39                 config_get v "$s" "$p"
40                 IFS="$LIST_SEP"
41                 for v in $v; do
42                         [ -n "$v" ] && append_param "$p" && ARGS="$ARGS $v"
43                 done
44                 unset IFS
45         done
46 }
47
48 section_enabled() {
49         config_get_bool enable  "$1" 'enable'  0
50         config_get_bool enabled "$1" 'enabled' 0
51         [ $enable -gt 0 ] || [ $enabled -gt 0 ]
52 }
53
54 start_instance() {
55         local s="$1"
56
57         section_enabled "$s" || return 1
58
59         ARGS=""
60
61         # append flags
62         append_bools "$s" \
63                 auth_nocache auth_retry auth_user_pass_optional bind ccd_exclusive client client_cert_not_required \
64                 client_to_client comp_lzo comp_noadapt disable \
65                 disable_occ down_pre duplicate_cn fast_io float http_proxy_retry \
66                 ifconfig_noexec ifconfig_nowarn ifconfig_pool_linear management_forget_disconnect management_hold \
67                 management_query_passwords management_signal mktun mlock mtu_test multihome mute_replay_warnings \
68                 nobind no_iv no_name_remapping no_replay opt_verify passtos persist_key persist_local_ip \
69                 persist_remote_ip persist_tun ping_timer_rem pull push_reset \
70                 remote_random rmtun route_noexec route_nopull single_session socks_proxy_retry \
71                 suppress_timestamps tcp_nodelay test_crypto tls_client tls_exit tls_server \
72                 tun_ipv6 up_delay up_restart username_as_common_name
73
74         # append params
75         append_params "$s" \
76                 cd askpass auth auth_user_pass auth_user_pass_verify bcast_buffers ca cert \
77                 chroot cipher client_config_dir client_connect client_disconnect config connect_freq \
78                 connect_retry connect_timeout connect_retry_max crl_verify dev dev_node dev_type dh \
79                 echo engine explicit_exit_notify fragment group hand_window hash_size \
80                 http_proxy http_proxy_option http_proxy_timeout ifconfig ifconfig_pool \
81                 ifconfig_pool_persist ifconfig_push inactive ipchange iroute keepalive \
82                 key key_method keysize learn_address link_mtu lladdr local log log_append \
83                 lport management management_log_cache max_clients \
84                 max_routes_per_client mode mssfix mtu_disc mute nice ns_cert_type ping \
85                 ping_exit ping_restart pkcs12 plugin port port_share prng proto rcvbuf \
86                 redirect_gateway remap_usr1 remote remote_cert_eku remote_cert_ku remote_cert_tls \
87                 reneg_bytes reneg_pkts reneg_sec \
88                 replay_persist replay_window resolv_retry route route_delay route_gateway \
89                 route_metric route_up rport script_security secret server server_bridge setenv shaper sndbuf \
90                 socks_proxy status status_version syslog tcp_queue_limit tls_auth \
91                 tls_cipher tls_remote tls_timeout tls_verify tmp_dir topology tran_window \
92                 tun_mtu tun_mtu_extra txqueuelen user verb down push up
93
94
95         SERVICE_PID_FILE="/var/run/openvpn-$s.pid"
96         service_start /usr/sbin/openvpn --syslog "openvpn($s)" --writepid "$SERVICE_PID_FILE" $ARGS
97 }
98
99 stop_instance() {
100         local s="$1"
101
102         section_enabled "$s" || return 1
103
104         SERVICE_PID_FILE="/var/run/openvpn-$s.pid"
105         service_stop /usr/sbin/openvpn
106 }
107
108 reload_instance() {
109         local s="$1"
110
111         section_enabled "$s" || return 1
112
113         SERVICE_PID_FILE="/var/run/openvpn-$s.pid"
114         service_reload /usr/sbin/openvpn
115 }
116
117 start() {
118         config_load 'openvpn'
119         config_foreach start_instance 'openvpn'
120 }
121
122 stop() {
123         config_load 'openvpn'
124         config_foreach stop_instance 'openvpn'
125 }
126
127 reload() {
128         config_load 'openvpn'
129         config_foreach reload_instance 'openvpn'
130 }
131
132 up() {
133         local exists
134         local instance
135         config_load 'openvpn'
136         for instance in "$@"; do
137                 config_get exists "$instance" 'TYPE'
138                 if [ "$exists" == "openvpn" ]; then
139                         start_instance "$instance"
140                 fi
141         done
142 }
143
144 down() {
145         local exists
146         local instance
147         config_load 'openvpn'
148         for instance in "$@"; do
149                 config_get exists "$instance" 'TYPE'
150                 if [ "$exists" == "openvpn" ]; then
151                         stop_instance "$instance"
152                 fi
153         done
154 }