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 <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
() {
40 local config_file
="$2"
41 local option_name
="$3"
42 local config_counterpart
="$4"
44 config_get val
"$cfg" "$option_name" "$default"
45 [ -n "$val" ] && echo "" >> $config_file
48 setup_user_authentication
() {
52 config_get_bool val
"$cfg" authenticate_immediately
0
53 [ $val -gt 0 ] && echo "AuthenticateImmediately yes" >> $CONFIGFILE
55 config_get val
"$cfg" username
56 if [ -n "$val" ] ; then
57 echo "UsernameAuthentication" >> $CONFIGFILE
58 echo "Username $val" >> $CONFIGFILE
61 config_get val
"$cfg" password
62 if [ -n "$val" ] ; then
63 echo "PasswordAuthentication" >> $CONFIGFILE
64 echo "Password $val" >> $CONFIGFILE
77 config_get val
"$cfg" macmechanism
78 if [ -z "$val" ] ; then
79 # check if we have AllowedMACList or BlockedMACList defined they will be ignored
80 config_get val
"$cfg" allowedmac
81 if [ -n "$val" ] ; then
82 echo "Ignoring allowedmac - macmechanism not \"allow\"" >&2
85 config_get val
"$cfg" blockedmac
86 if [ -n "$val" ] ; then
87 echo "Ignoring blockedmac - macmechanism not \"block\"" >&2
89 elif [ "$val" == "allow" ] ; then
91 config_list_foreach
"$cfg" allowedmac append_mac
92 echo "AllowedMACList $MAC" >> $CONFIGFILE
93 elif [ "$val" == "block" ] ; then
95 config_list_foreach
"$cfg" blockedmac append_mac
96 echo "BlockedMACList $MAC" >> $CONFIGFILE
98 nolog error
"$cfg Invalid macmechanism '$val' - allow or block are valid."
102 config_list_foreach
"$cfg" trustedmac append_mac
103 [ -n "$MAC" ] && echo "TrustedMACList $MAC" >> $CONFIGFILE
112 echo " FirewallRule $1" >> $CONFIGFILE
115 for rule
in $
(echo authenticated-users preauthenticated-users users-to-router trusted-users trusted-users-to-router
)
117 uci_name
=${rule//-/_}
118 # uci does not allow - dashes
119 echo "FirewallRuleSet $rule {" >> $CONFIGFILE
120 config_list_foreach
"$cfg" ${uci_name} append_firewall
121 echo "}" >> $CONFIGFILE
122 config_get val
"$cfg" policy_
${uci_name}
123 [ -n "$val" ] && echo "EmptyRuleSetPolicy $rule $val" >> $CONFIGFILE
127 generate_uci_config
() {
134 CONFIGFILE
="/tmp/etc/nodogsplash_$cfg.conf"
136 echo "# auto-generated config file from /etc/config/nodogsplash" > $CONFIGFILE
138 config_get val
"$cfg" config
139 if [ -n "$val" ] ; then
140 if [ -f "$val" ] ; then
141 nolog error
"Configuration file '$file' doesn't exist"
144 cat $val > CONFIGFILE
147 config_get val
"$cfg" network
148 if [ -n "$val" ] ; then
149 if ! network_get_device ifname
"$val" ; then
150 nolog error
"$cfg can not find ifname for network '$val'"
155 config_get val
"$cfg" gatewayinterface
156 if [ -n "$val" ] ; then
157 if [ -n "$ifname" ] ; then
158 nolog error
"$cfg cannot use both option network and gatewayinterface"
164 if [ -z "$ifname" ] ; then
165 nolog error
"$cfg option network or gatewayinterface missing"
169 echo "GatewayInterface $ifname" >> $CONFIGFILE
171 append_config_option
"$CONFIGFILE" "$cfg" gatewayname GatewayName
172 append_config_option
"$CONFIGFILE" "$cfg" gatewayaddress GatewayAddress
173 append_config_option
"$CONFIGFILE" "$cfg" gatewayport GatewayPort
174 append_config_option
"$CONFIGFILE" "$cfg" maxclients MaxClients
175 append_config_option
"$CONFIGFILE" "$cfg" imagedir ImagesDir
176 append_config_option
"$CONFIGFILE" "$cfg" redirecturl RedirectURL
177 append_config_option
"$CONFIGFILE" "$cfg" clientidletimeout ClientIdleTimeout
178 append_config_option
"$CONFIGFILE" "$cfg" clientforcetimeout ClientForceTimeout
179 append_config_option
"$CONFIGFILE" "$cfg" gatewayiprange GatewayIPRange
180 append_config_option
"$CONFIGFILE" "$cfg" passwordattempts PasswordAttempts
181 append_config_option
"$CONFIGFILE" "$cfg" macmechanism MACMechanism
182 append_config_option
"$CONFIGFILE" "$cfg" uploadlimit UploadLimit
183 append_config_option
"$CONFIGFILE" "$cfg" downloadlimit DownloadLimit
185 config_get download
"$cfg" downloadlimit
186 config_get upload
"$cfg" uploadlimit
187 [ -n "$upload" -o -n "$download" ] && echo "TrafficControl yes" >> $CONFIGFILE
189 setup_mac_lists
"$cfg"
190 setup_user_authentication
"$cfg"
191 setup_firewall
"$cfg"
194 # setup configuration and start instance
200 config_get_bool val
"$cfg" enabled
0
201 [ $val -gt 0 ] ||
return 0
203 generate_uci_config
"$cfg"
205 if ! test_module
; then
206 logger
-s -t nodogsplash
-p daemon.error
"nodogsplash is missing some kernel modules"
209 procd_open_instance
$cfg
210 procd_set_param
command /usr
/bin
/nodogsplash
-c $CONFIGFILE $OPTIONS
211 procd_set_param respawn
212 procd_set_param
file $CONFIGFILE
217 include
/lib
/functions
220 config_load nodogsplash
222 config_foreach create_instance instance
226 # nodogsplash doesn't exit fast enought, when procd terminates it.
227 # otherwise procd will restart nodogsplash twice. first time starting nodogsplash fails, second time it succeeds
232 $WD_DIR/ndsctl status
235 # Test if we got all modules loaded
237 ### Test ipt_mark with iptables
239 ($IPT -A FORWARD
-m mark
--mark 2 -j ACCEPT
2>&1) > /dev
/null
241 if [ "$IPTABLES_OK" -eq 0 ]; then
242 ($IPT -D FORWARD
-m mark
--mark 2 -j ACCEPT
2>&1) > /dev
/null
249 ### Test ipt_mac with iptables
251 ($IPT -A INPUT
-m mac
--mac-source 00:00:00:00:00:00 -j ACCEPT
2>&1) > /dev
/null
253 if [ "$IPTABLES_OK" -eq 0 ]; then
254 ($IPT -D INPUT
-m mac
--mac-source 00:00:00:00:00:00 -j ACCEPT
2>&1) > /dev
/null
261 ### Test ipt_IMQ with iptables
263 ($IPT -t mangle
-A PREROUTING
-j IMQ
--todev 0 2>&1) > /dev
/null
265 if [ "$IPTABLES_OK" -eq 0 ]; then
266 ($IPT -t mangle
-D PREROUTING
-j IMQ
--todev 0 2>&1) > /dev
/null
275 (ip link
set imq0 up
2>&1) > /dev
/null
277 (ip link
set imq1 up
2>&1) > /dev
/null
279 if [ "$IMQ0_OK" -eq 0 -a "$IMQ1_OK" -eq 0 ]; then
280 (ip link
set imq0 down
2>&1) > /dev
/null
281 (ip link
set imq1 down
2>&1) > /dev
/null
288 ### Test sch_htb with tc; requires imq0
290 (tc qdisc del dev imq0 root
2>&1) > /dev
/null
291 (tc qdisc add dev imq0 root htb
2>&1) > /dev
/null
293 if [ "$TC_OK" -eq 0 ]; then
294 (tc qdisc del dev imq0 root
2>&1) > /dev
/null
301 ### Find a module on disk
303 EXIST
=$
(find /lib
/modules
/`uname -r` -name $1.
*o
2> /dev
/null
)
304 if [ -n "$EXIST" ]; then
311 ### Test if a module is in memory
312 module_in_memory
() {
313 MODULE
=$
(lsmod |
grep $1 |
awk '{print $1}')
314 if [ "$MODULE" = "$1" ]; then
321 ### Test functionality of a module; load if necessary
323 echo " Testing module $1 $2"
325 if [ $?
-ne 0 ]; then
326 echo " Module $1 $2 needed"
327 echo " Scanning disk for $1 module"
329 if [ $?
-ne 0 ]; then
330 echo " $1 module missing: please install it"
333 echo " $1 exists, trying to load"
334 insmod
$1 $2 > /dev
/null
335 if [ $?
-ne 0 ]; then
336 echo " Error: insmod $1 $2 failed"
339 echo " $1 $2 loaded successfully"
343 echo " $1 is working"
347 echo " Testing required modules"
349 do_module_tests
"ipt_mac"
350 do_module_tests
"ipt_mark"
352 # test for imq modules, only if TrafficControl is enabled in conf
353 if ( grep -q -E '^[[:space:]]*TrafficControl[[:space:]]+(yes|true|1)' "$CONFIGFILE" ) ; then
354 do_module_tests
"imq" "numdevs=2"
355 do_module_tests
"ipt_IMQ"
356 do_module_tests
"sch_htb"