1 #!/bin/sh /etc/rc.common
3 # description: Startup/shutdown script for nodogsplash captive portal
5 # Alexander Couzens <lynxis@fe80.eu> 2014
8 # (Based on wifidog startup script
11 # Comment by that author: Could be better, but it's working as expected)
19 IPT
=/usr
/sbin
/iptables
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
24 CONFIGFILE
="/tmp/invalid_nodogsplash.conf"
26 # nolog(loglevel message ...)
30 logger
-s -t nodogsplash
-p daemon.
$level $@
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
() {
39 local config_file
="$1"
41 local option_name
="$3"
42 local config_counterpart
="$4"
44 config_get val
"$cfg" "$option_name" "$default"
45 [ -n "$val" ] && echo "$config_counterpart $val" >> $config_file
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
() {
55 local config_file
="$1"
57 local option_name
="$3"
59 config_get val
"$cfg" "$option_name" "$default"
60 [ -n "$val" ] && echo "$option_name $val" >> $config_file
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
80 config_get val
"$cfg" blockedmac
81 if [ -n "$val" ] ; then
82 echo "Ignoring blockedmac - macmechanism not \"block\"" >&2
84 elif [ "$val" == "allow" ] ; then
86 config_list_foreach
"$cfg" allowedmac append_mac
87 echo "AllowedMACList $MAC" >> $CONFIGFILE
88 elif [ "$val" == "block" ] ; then
90 config_list_foreach
"$cfg" blockedmac append_mac
91 echo "BlockedMACList $MAC" >> $CONFIGFILE
93 nolog error
"$cfg Invalid macmechanism '$val' - allow or block are valid."
97 config_list_foreach
"$cfg" trustedmac append_mac
98 [ -n "$MAC" ] && echo "TrustedMACList $MAC" >> $CONFIGFILE
107 echo " FirewallRule $1" >> $CONFIGFILE
110 for rule
in $
(echo authenticated-users preauthenticated-users users-to-router trusted-users trusted-users-to-router
)
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
126 for i
in $
(seq $timeout); do
127 if [ $
(ip
-4 addr show dev
$ifname 2> /dev
/null |
grep -c inet
) -ne 0 ]; then
131 if [ $i == $timeout ] ; then
132 nolog error
"$ifname not detected, NoDogSplash not starting."
138 generate_uci_config
() {
145 CONFIGFILE
="/tmp/etc/nodogsplash_$cfg.conf"
147 echo "# auto-generated config file from /etc/config/nodogsplash" > $CONFIGFILE
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"
155 cat "$val" >> $CONFIGFILE
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'"
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"
175 if [ -z "$ifname" ] ; then
176 nolog error
"$cfg option network or gatewayinterface missing"
180 wait_for_interface
"$ifname"
182 echo "GatewayInterface $ifname" >> $CONFIGFILE
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
221 config_get download
"$cfg" downloadlimit
222 config_get upload
"$cfg" uploadlimit
223 [ -n "$upload" -o -n "$download" ] && echo "TrafficControl yes" >> $CONFIGFILE
225 setup_mac_lists
"$cfg"
226 setup_firewall
"$cfg"
229 # setup configuration and start instance
235 config_get_bool val
"$cfg" enabled
0
236 [ $val -gt 0 ] ||
return 0
238 generate_uci_config
"$cfg"
240 if ! test_module
; then
241 logger
-s -t nodogsplash
-p daemon.error
"nodogsplash is missing some kernel modules"
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
252 if [ "$1" = "iptables" ] ; then
253 if [ $
(uci get nodogsplash.@nodogsplash
[0].fwhook_enabled
) = "1" ] ; then
254 if $WD_DIR/ndsctl status
> /dev
/null
; then
255 echo " * Restarting NodogSplash"
256 /etc
/init.d
/nodogsplash restart
259 echo " * NodogSplash fwhook is disabled"
265 include
/lib
/functions
268 if [ $
(uci get nodogsplash.@nodogsplash
[0].fwhook_enabled
) = "1" ] ; then
269 if ! uci get firewall.nodogsplash2.path
&> /dev
/null
; then
270 if [ -f '/tmp/etc/ndshook.include' ] ; then
271 rm /tmp
/etc
/ndshook.include
273 uci delete firewall.nodogsplash2
2> /dev
/null
274 uci
set firewall.nodogsplash2
=include
275 uci
set firewall.nodogsplash2.
type=script
276 uci
set firewall.nodogsplash2.path
='/tmp/etc/ndshook.include'
278 /etc
/init.d
/firewall restart
2>&1 | logger
281 if [ ! -f '/tmp/etc/ndshook.include' ] ; then
282 printf "if [ -f '/etc/init.d/nodogsplash' ] ; then /etc/init.d/nodogsplash depends iptables ; fi\n" > /tmp
/etc
/ndshook.include
283 chmod +x
/tmp
/etc
/ndshook.include
287 config_load nodogsplash
289 config_foreach create_instance nodogsplash
293 # nodogsplash doesn't exit fast enought, when procd terminates it.
294 # otherwise procd will restart nodogsplash twice. first time starting nodogsplash fails, second time it succeeds
299 $WD_DIR/ndsctl status
302 # Test if we got all modules loaded
304 ### Test ipt_mark with iptables
306 ($IPT -A FORWARD
-m mark
--mark 2 -j ACCEPT
2>&1) > /dev
/null
308 if [ "$IPTABLES_OK" -eq 0 ]; then
309 ($IPT -D FORWARD
-m mark
--mark 2 -j ACCEPT
2>&1) > /dev
/null
316 ### Test ipt_mac with iptables
318 ($IPT -A INPUT
-m mac
--mac-source 00:00:00:00:00:00 -j ACCEPT
2>&1) > /dev
/null
320 if [ "$IPTABLES_OK" -eq 0 ]; then
321 ($IPT -D INPUT
-m mac
--mac-source 00:00:00:00:00:00 -j ACCEPT
2>&1) > /dev
/null
328 ### Test ipt_IMQ with iptables
330 ($IPT -t mangle
-A PREROUTING
-j IMQ
--todev 0 2>&1) > /dev
/null
332 if [ "$IPTABLES_OK" -eq 0 ]; then
333 ($IPT -t mangle
-D PREROUTING
-j IMQ
--todev 0 2>&1) > /dev
/null
342 (ip link
set imq0 up
2>&1) > /dev
/null
344 (ip link
set imq1 up
2>&1) > /dev
/null
346 if [ "$IMQ0_OK" -eq 0 -a "$IMQ1_OK" -eq 0 ]; then
347 (ip link
set imq0 down
2>&1) > /dev
/null
348 (ip link
set imq1 down
2>&1) > /dev
/null
355 ### Test sch_htb with tc; requires imq0
357 (tc qdisc del dev imq0 root
2>&1) > /dev
/null
358 (tc qdisc add dev imq0 root htb
2>&1) > /dev
/null
360 if [ "$TC_OK" -eq 0 ]; then
361 (tc qdisc del dev imq0 root
2>&1) > /dev
/null
368 ### Find a module on disk
370 EXIST
=$
(find /lib
/modules
/`uname -r` -name $1.
*o
2> /dev
/null
)
371 if [ -n "$EXIST" ]; then
378 ### Test if a module is in memory
379 module_in_memory
() {
380 MODULE
=$
(lsmod |
grep $1 |
awk '{print $1}')
381 if [ "$MODULE" = "$1" ]; then