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