openvpn: fix shell compare operator in openvpn.init
[openwrt/staging/pepe2k.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" ] && [ "$p" != "push" ] && append_param "$s" "$p" && echo " $v" >> "/var/etc/openvpn-$s.conf"
46 [ -n "$v" ] && [ "$p" = "push" ] && append_param "$s" "$p" && echo " \"$v\"" >> "/var/etc/openvpn-$s.conf"
47 done
48 unset IFS
49 done
50 }
51
52 append_list() {
53 local p; local v; local s="$1"; shift
54
55 list_cb_append() {
56 v="${v}:$1"
57 }
58
59 for p in $*; do
60 unset v
61 config_list_foreach "$s" "$p" list_cb_append
62 [ -n "$v" ] && append_param "$s" "$p" && echo " ${v:1}" >> "/var/etc/openvpn-$s.conf"
63 done
64 }
65
66 section_enabled() {
67 config_get_bool enable "$1" 'enable' 0
68 config_get_bool enabled "$1" 'enabled' 0
69 [ $enable -gt 0 ] || [ $enabled -gt 0 ]
70 }
71
72 openvpn_get_dev() {
73 local dev dev_type
74 local name="$1"
75 local conf="$2"
76
77 # Do override only for configurations with config_file
78 config_get config_file "$name" config
79 [ -n "$config_file" ] || return
80
81 # Check there is someething to override
82 config_get dev "$name" dev
83 config_get dev_type "$name" dev_type
84 [ -n "$dev" ] || return
85
86 # If there is a no dev_type, try to guess it
87 if [ -z "$dev_type" ]; then
88 . /lib/functions/openvpn.sh
89
90 local odev odev_type
91 get_openvpn_option "$conf" odev dev
92 get_openvpn_option "$conf" odev_type dev-type
93 [ -n "$odev_type" ] || odev_type="$odev"
94
95 case "$odev_type" in
96 tun*) dev_type="tun" ;;
97 tap*) dev_type="tap" ;;
98 *) return;;
99 esac
100 fi
101
102 # Return overrides
103 echo "--dev-type $dev_type --dev $dev"
104 }
105
106 openvpn_add_instance() {
107 local name="$1"
108 local dir="$2"
109 local conf="$3"
110 local security="$4"
111
112 procd_open_instance "$name"
113 procd_set_param command "$PROG" \
114 --syslog "openvpn($name)" \
115 --status "/var/run/openvpn.$name.status" \
116 --cd "$dir" \
117 --config "$conf" \
118 --up "/usr/libexec/openvpn-hotplug up $name" \
119 --down "/usr/libexec/openvpn-hotplug down $name" \
120 --script-security "${security:-2}" \
121 $(openvpn_get_dev "$name" "$conf")
122 procd_set_param file "$dir/$conf"
123 procd_set_param term_timeout 15
124 procd_set_param respawn
125 procd_append_param respawn 3600
126 procd_append_param respawn 5
127 procd_append_param respawn -1
128 procd_close_instance
129 }
130
131 start_instance() {
132 local s="$1"
133
134 config_get config "$s" config
135 config="${config:+$(readlink -f "$config")}"
136
137 section_enabled "$s" || {
138 append UCI_DISABLED "$config" "$LIST_SEP"
139 return 1
140 }
141
142 local script_security
143 config_get script_security "$s" script_security
144
145 [ ! -d "/var/run" ] && mkdir -p "/var/run"
146
147 if [ ! -z "$config" ]; then
148 append UCI_STARTED "$config" "$LIST_SEP"
149 openvpn_add_instance "$s" "${config%/*}" "$config" "$script_security"
150 return
151 fi
152
153 [ ! -d "/var/etc" ] && mkdir -p "/var/etc"
154 [ -f "/var/etc/openvpn-$s.conf" ] && rm "/var/etc/openvpn-$s.conf"
155
156 append_bools "$s" $OPENVPN_BOOLS
157 append_params "$s" $OPENVPN_PARAMS
158 append_list "$s" $OPENVPN_LIST
159
160 openvpn_add_instance "$s" "/var/etc" "openvpn-$s.conf" "$script_security"
161 }
162
163 start_service() {
164 local instance="$1"
165 local instance_found=0
166
167 config_cb() {
168 local type="$1"
169 local name="$2"
170 if [ "$type" = "openvpn" ]; then
171 if [ -n "$instance" -a "$instance" = "$name" ]; then
172 instance_found=1
173 fi
174 fi
175 }
176
177 . /usr/share/openvpn/openvpn.options
178 config_load 'openvpn'
179
180 if [ -n "$instance" ]; then
181 [ "$instance_found" -gt 0 ] || return
182 start_instance "$instance"
183 else
184 config_foreach start_instance 'openvpn'
185
186 local path name
187 for path in /etc/openvpn/*.conf; do
188 if [ -f "$path" ]; then
189 name="${path##*/}"; name="${name%.conf}"
190
191 # don't start configs again that are already started by uci
192 if echo "$UCI_STARTED" | grep -qxF "$path"; then
193 continue
194
195 # don't start configs which are set to disabled in uci
196 elif echo "$UCI_DISABLED" | grep -qxF "$path"; then
197 logger -t openvpn "$name.conf is disabled in /etc/config/openvpn"
198 continue
199 fi
200
201 openvpn_add_instance "$name" "${path%/*}" "$path"
202 fi
203 done
204 fi
205 }
206
207 service_triggers() {
208 procd_add_reload_trigger openvpn
209 }