nodogsplash2: initial package
[feed/routing.git] / nodogsplash2 / files / nodogsplash.init
1 #!/bin/sh /etc/rc.common
2 #
3 # description: Startup/shutdown script for nodogsplash captive portal
4 #
5 # Alexander Couzens <lynxis@fe80.eu> 2014
6 # P. Kube 2007
7 #
8 # (Based on wifidog startup script
9 # Date : 2004-08-25
10 # Version : 1.0
11 # Comment by that author: Could be better, but it's working as expected)
12 #
13
14 START=95
15 STOP=95
16
17 USE_PROCD=1
18
19 IPT=/usr/sbin/iptables
20 WD_DIR=/usr/bin
21 # -s -d 5 runs in background, with level 5 (not so verbose) messages to syslog
22 # -f -d 7 runs in foreground, with level 7 (verbose) debug messages to terminal
23 OPTIONS="-s -f -d 5"
24 CONFIGFILE="/tmp/invalid_nodogsplash.conf"
25
26 # nolog(loglevel message ...)
27 nolog() {
28 local level=$1
29 shift
30 logger -s -t nodogsplash -p daemon.$level $@
31 }
32
33 # append_config_option_map <cfgfile> <uci_cfg_obj> <option_name> <config_counterpart> [<optional default>]
34 # append "$config_counterpart $value" to cfgfile if option_name exists
35 # e.g. append_config_option "$CONFIGFILE" "$cfg" bind_address BindAddress 0.0.0.0
36 # will append "BindAddress 192.168.1.1" if uci bind_address is '192.168.1.1'
37 append_config_option_map() {
38 local val=""
39 local config_file="$1"
40 local cfg="$2"
41 local option_name="$3"
42 local config_counterpart="$4"
43 local default="$5"
44 config_get val "$cfg" "$option_name" "$default"
45 [ -n "$val" ] && echo "$config_counterpart $val" >> $config_file
46 }
47
48 # append_config_option <cfgfile> <uci_cfg_obj> <option_name> [<optional default>]
49 # append "$option_name $value" to cfgfile if option_name exists
50 # e.g. append_config_option "$CONFIGFILE" "$cfg" bind_address 0.0.0.0
51 # will append "bind_address 192.168.1.1" if uci bind_address is '192.168.1.1'
52 # if uci bind_address is unset append "bind_address 0.0.0.0"
53 append_config_option() {
54 local val=""
55 local config_file="$1"
56 local cfg="$2"
57 local option_name="$3"
58 local default="$4"
59 config_get val "$cfg" "$option_name" "$default"
60 [ -n "$val" ] && echo "$option_name $val" >> $config_file
61 }
62
63 setup_mac_lists() {
64 local cfg="$1"
65 local MAC=""
66 local val
67
68 append_mac() {
69 append MAC "$1" ","
70 }
71
72 config_get val "$cfg" macmechanism
73 if [ -z "$val" ] ; then
74 # check if we have AllowedMACList or BlockedMACList defined they will be ignored
75 config_get val "$cfg" allowedmac
76 if [ -n "$val" ] ; then
77 echo "Ignoring allowedmac - macmechanism not \"allow\"" >&2
78 fi
79
80 config_get val "$cfg" blockedmac
81 if [ -n "$val" ] ; then
82 echo "Ignoring blockedmac - macmechanism not \"block\"" >&2
83 fi
84 elif [ "$val" == "allow" ] ; then
85 MAC=""
86 config_list_foreach "$cfg" allowedmac append_mac
87 echo "AllowedMACList $MAC" >> $CONFIGFILE
88 elif [ "$val" == "block" ] ; then
89 MAC=""
90 config_list_foreach "$cfg" blockedmac append_mac
91 echo "BlockedMACList $MAC" >> $CONFIGFILE
92 else
93 nolog error "$cfg Invalid macmechanism '$val' - allow or block are valid."
94 return 1
95 fi
96 MAC=""
97 config_list_foreach "$cfg" trustedmac append_mac
98 [ -n "$MAC" ] && echo "TrustedMACList $MAC" >> $CONFIGFILE
99 }
100
101 setup_firewall() {
102 local cfg="$1"
103 local uciname
104 local val
105
106 append_firewall() {
107 echo " FirewallRule $1" >> $CONFIGFILE
108 }
109
110 for rule in $(echo authenticated-users preauthenticated-users users-to-router trusted-users trusted-users-to-router)
111 do
112 uci_name=${rule//-/_}
113 # uci does not allow - dashes
114 echo "FirewallRuleSet $rule {" >> $CONFIGFILE
115 config_list_foreach "$cfg" ${uci_name} append_firewall
116 echo "}" >> $CONFIGFILE
117 config_get val "$cfg" policy_${uci_name}
118 [ -n "$val" ] && echo "EmptyRuleSetPolicy $rule $val" >> $CONFIGFILE
119 done
120 }
121
122 wait_for_interface()
123 {
124 local ifname="$1"
125 local timeout=10
126 for i in $(seq $timeout); do
127 if [ $(ip -4 addr show dev $ifname 2> /dev/null | grep -c inet) -ne 0 ]; then
128 break
129 fi
130 sleep 1
131 if [ $i == $timeout ] ; then
132 nolog error "$ifname not detected, NoDogSplash not starting."
133 exit 1
134 fi
135 done
136 }
137
138 generate_uci_config() {
139 local cfg="$1"
140 local val
141 local ifname
142 local download
143 local upload
144
145 CONFIGFILE="/tmp/etc/nodogsplash_$cfg.conf"
146
147 echo "# auto-generated config file from /etc/config/nodogsplash" > $CONFIGFILE
148
149 config_get val "$cfg" config
150 if [ -n "$val" ] ; then
151 if [ ! -f "$val" ] ; then
152 nolog error "Configuration file '$file' doesn't exist"
153 return 0
154 fi
155 cat "$val" >> $CONFIGFILE
156 fi
157
158 config_get val "$cfg" network
159 if [ -n "$val" ] ; then
160 if ! network_get_device ifname "$val" ; then
161 nolog error "$cfg can not find ifname for network '$val'"
162 return 1
163 fi
164 fi
165
166 config_get val "$cfg" gatewayinterface
167 if [ -n "$val" ] ; then
168 if [ -n "$ifname" ] ; then
169 nolog error "$cfg cannot use both option network and gatewayinterface"
170 return 1
171 fi
172 ifname="$val"
173 fi
174
175 if [ -z "$ifname" ] ; then
176 nolog error "$cfg option network or gatewayinterface missing"
177 return 1
178 fi
179
180 wait_for_interface "$ifname"
181
182 echo "GatewayInterface $ifname" >> $CONFIGFILE
183
184 append_config_option "$CONFIGFILE" "$cfg" gatewayname
185 append_config_option "$CONFIGFILE" "$cfg" gatewayaddress
186 append_config_option "$CONFIGFILE" "$cfg" gatewayport
187 append_config_option "$CONFIGFILE" "$cfg" maxclients
188 append_config_option "$CONFIGFILE" "$cfg" webroot
189 append_config_option "$CONFIGFILE" "$cfg" debuglevel
190 append_config_option "$CONFIGFILE" "$cfg" splashpage
191 append_config_option "$CONFIGFILE" "$cfg" pagesdir
192 append_config_option "$CONFIGFILE" "$cfg" checkinterval
193 append_config_option "$CONFIGFILE" "$cfg" syslogfacility
194 append_config_option "$CONFIGFILE" "$cfg" gatewayiprange
195 append_config_option "$CONFIGFILE" "$cfg" imagedir
196 append_config_option "$CONFIGFILE" "$cfg" redirecturl
197 append_config_option "$CONFIGFILE" "$cfg" clientidletimeout
198 append_config_option "$CONFIGFILE" "$cfg" clientforcetimeout
199 append_config_option "$CONFIGFILE" "$cfg" gatewayiprange
200 append_config_option "$CONFIGFILE" "$cfg" passwordattempts
201 append_config_option "$CONFIGFILE" "$cfg" macmechanism
202 append_config_option "$CONFIGFILE" "$cfg" uploadlimit
203 append_config_option "$CONFIGFILE" "$cfg" downloadlimit
204 append_config_option "$CONFIGFILE" "$cfg" remoteauthenticatoraction
205 append_config_option "$CONFIGFILE" "$cfg" enablepreauth
206 append_config_option "$CONFIGFILE" "$cfg" binvoucher
207 append_config_option "$CONFIGFILE" "$cfg" forcevoucher
208 append_config_option "$CONFIGFILE" "$cfg" passwordauthentication
209 append_config_option "$CONFIGFILE" "$cfg" usernameauthentication
210 append_config_option "$CONFIGFILE" "$cfg" passwordattempts
211 append_config_option "$CONFIGFILE" "$cfg" username
212 append_config_option "$CONFIGFILE" "$cfg" password
213 append_config_option "$CONFIGFILE" "$cfg" authenticateimmediately
214 append_config_option "$CONFIGFILE" "$cfg" decongesthttpdthreads
215 append_config_option "$CONFIGFILE" "$cfg" httpdthreadthreshold
216 append_config_option "$CONFIGFILE" "$cfg" httpdthreaddelayms
217 append_config_option "$CONFIGFILE" "$cfg" fw_mark_authenticated
218 append_config_option "$CONFIGFILE" "$cfg" fw_mark_trusted
219 append_config_option "$CONFIGFILE" "$cfg" fw_mark_blocked
220
221 config_get download "$cfg" downloadlimit
222 config_get upload "$cfg" uploadlimit
223 [ -n "$upload" -o -n "$download" ] && echo "TrafficControl yes" >> $CONFIGFILE
224
225 setup_mac_lists "$cfg"
226 setup_firewall "$cfg"
227 }
228
229 # setup configuration and start instance
230 create_instance() {
231 local cfg="$1"
232 local manual_config
233 local val
234
235 config_get_bool val "$cfg" enabled 0
236 [ $val -gt 0 ] || return 0
237
238 generate_uci_config "$cfg"
239
240 if ! test_module ; then
241 logger -s -t nodogsplash -p daemon.error "nodogsplash is missing some kernel modules"
242 fi
243
244 procd_open_instance $cfg
245 procd_set_param command /usr/bin/nodogsplash -c $CONFIGFILE $OPTIONS
246 procd_set_param respawn
247 procd_set_param file $CONFIGFILE
248 procd_close_instance
249 }
250
251 start_service() {
252 include /lib/functions
253
254 mkdir -p /tmp/etc/
255 config_load nodogsplash
256
257 config_foreach create_instance nodogsplash
258 }
259
260 stop_service() {
261 # nodogsplash doesn't exit fast enought, when procd terminates it.
262 # otherwise procd will restart nodogsplash twice. first time starting nodogsplash fails, second time it succeeds
263 sleep 1
264 }
265
266 status() {
267 $WD_DIR/ndsctl status
268 }
269
270 # Test if we got all modules loaded
271 test_module() {
272 ### Test ipt_mark with iptables
273 test_ipt_mark () {
274 ($IPT -A FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
275 IPTABLES_OK=$?
276 if [ "$IPTABLES_OK" -eq 0 ]; then
277 ($IPT -D FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
278 return 0
279 else
280 return 1
281 fi
282 }
283
284 ### Test ipt_mac with iptables
285 test_ipt_mac () {
286 ($IPT -A INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
287 IPTABLES_OK=$?
288 if [ "$IPTABLES_OK" -eq 0 ]; then
289 ($IPT -D INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
290 return 0
291 else
292 return 1
293 fi
294 }
295
296 ### Test ipt_IMQ with iptables
297 test_ipt_IMQ () {
298 ($IPT -t mangle -A PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
299 IPTABLES_OK=$?
300 if [ "$IPTABLES_OK" -eq 0 ]; then
301 ($IPT -t mangle -D PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
302 return 0
303 else
304 return 1
305 fi
306 }
307
308 ### Test imq with ip
309 test_imq () {
310 (ip link set imq0 up 2>&1) > /dev/null
311 IMQ0_OK=$?
312 (ip link set imq1 up 2>&1) > /dev/null
313 IMQ1_OK=$?
314 if [ "$IMQ0_OK" -eq 0 -a "$IMQ1_OK" -eq 0 ]; then
315 (ip link set imq0 down 2>&1) > /dev/null
316 (ip link set imq1 down 2>&1) > /dev/null
317 return 0
318 else
319 return 1
320 fi
321 }
322
323 ### Test sch_htb with tc; requires imq0
324 test_sch_htb () {
325 (tc qdisc del dev imq0 root 2>&1) > /dev/null
326 (tc qdisc add dev imq0 root htb 2>&1) > /dev/null
327 TC_OK=$?
328 if [ "$TC_OK" -eq 0 ]; then
329 (tc qdisc del dev imq0 root 2>&1) > /dev/null
330 return 0
331 else
332 return 1
333 fi
334 }
335
336 ### Find a module on disk
337 module_exists () {
338 EXIST=$(find /lib/modules/`uname -r` -name $1.*o 2> /dev/null)
339 if [ -n "$EXIST" ]; then
340 return 0
341 else
342 return 1
343 fi
344 }
345
346 ### Test if a module is in memory
347 module_in_memory () {
348 MODULE=$(lsmod | grep $1 | awk '{print $1}')
349 if [ "$MODULE" = "$1" ]; then
350 return 0
351 else
352 return 1
353 fi
354 }
355 }