1e0f384f97806da84eafe24f3a4db1aef411d802
[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 append_param() {
17         local s="$1"
18         local v="$2"
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         echo -n "$v" >> "/var/etc/openvpn-$s.conf"
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 "$s" "$p" && echo >> "/var/etc/openvpn-$s.conf"
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 "$s" "$p" && echo " $v" >> "/var/etc/openvpn-$s.conf"
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 openvpn_add_instance() {
55         local name="$1"
56         local dir="$2"
57         local conf="$3"
58
59         procd_open_instance
60         procd_set_param command "$PROG" \
61                 --syslog "openvpn($name)" \
62                 --cd "$dir" \
63                 --config "$conf"
64         procd_set_param file "$dir/$conf"
65         procd_set_param respawn
66         procd_close_instance
67 }
68
69 start_instance() {
70         local s="$1"
71
72         section_enabled "$s" || return 1
73
74         config_get config "$s" config
75
76         [ ! -d "/var/run" ] && mkdir -p "/var/run"
77
78         if [ ! -z "$config" ]; then
79                 openvpn_add_instance "$s" "$(dirname "$config")" "$(basename "$config")"
80                 return
81         fi
82
83         [ ! -d "/var/etc" ] && mkdir -p "/var/etc"
84         [ -f "/var/etc/openvpn-$s.conf" ] && rm "/var/etc/openvpn-$s.conf"
85
86         # append flags
87         append_bools "$s" \
88                 auth_nocache auth_retry auth_user_pass_optional bind ccd_exclusive client client_cert_not_required \
89                 client_to_client comp_noadapt disable \
90                 disable_occ down_pre duplicate_cn fast_io float http_proxy_retry \
91                 ifconfig_noexec ifconfig_nowarn ifconfig_pool_linear management_forget_disconnect management_hold \
92                 management_query_passwords management_signal mktun mlock mtu_test multihome mute_replay_warnings \
93                 nobind no_iv no_name_remapping no_replay opt_verify passtos persist_key persist_local_ip \
94                 persist_remote_ip persist_tun ping_timer_rem pull push_reset \
95                 remote_random rmtun route_noexec route_nopull single_session socks_proxy_retry \
96                 suppress_timestamps tcp_nodelay test_crypto tls_client tls_exit tls_server \
97                 tun_ipv6 up_delay up_restart username_as_common_name
98
99         # append params
100         append_params "$s" \
101                 cd askpass auth auth_user_pass auth_user_pass_verify bcast_buffers ca cert \
102                 chroot cipher client_config_dir client_connect client_disconnect comp_lzo connect_freq \
103                 connect_retry connect_timeout connect_retry_max crl_verify dev dev_node dev_type dh \
104                 echo engine explicit_exit_notify fragment group hand_window hash_size \
105                 http_proxy http_proxy_option http_proxy_timeout ifconfig ifconfig_pool \
106                 ifconfig_pool_persist ifconfig_push inactive ipchange iroute keepalive \
107                 key key_method keysize learn_address link_mtu lladdr local log log_append \
108                 lport management management_log_cache max_clients \
109                 max_routes_per_client mode mssfix mtu_disc mute nice ns_cert_type ping \
110                 ping_exit ping_restart pkcs12 plugin port port_share prng proto rcvbuf \
111                 redirect_gateway remap_usr1 remote remote_cert_eku remote_cert_ku remote_cert_tls \
112                 reneg_bytes reneg_pkts reneg_sec \
113                 replay_persist replay_window resolv_retry route route_delay route_gateway \
114                 route_metric route_up rport script_security secret server server_bridge setenv shaper sndbuf \
115                 socks_proxy status status_version syslog tcp_queue_limit tls_auth \
116                 tls_cipher tls_remote tls_timeout tls_verify tmp_dir topology tran_window \
117                 tun_mtu tun_mtu_extra txqueuelen user verb down push up \
118                 ifconfig_ipv6 route_ipv6 server_ipv6 ifconfig_ipv6_pool ifconfig_ipv6_push iroute_ipv6
119
120         openvpn_add_instance "$s" "/var/etc" "openvpn-$s.conf"
121 }
122
123 start_service() {
124         config_load 'openvpn'
125         config_foreach start_instance 'openvpn'
126 }