Merge pull request #66 from lynxis/upstream_nodogsplash
[feed/routing.git] / nodogsplash / 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 <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() {
38 local val=""
39 local cfg="$1"
40 local config_file="$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_file
46 }
47
48 setup_user_authentication() {
49 local cfg="$1"
50 local val
51
52 config_get_bool val "$cfg" authenticate_immediately 0
53 [ $val -gt 0 ] && echo "AuthenticateImmediately yes" >> $CONFIGFILE
54
55 config_get val "$cfg" username
56 if [ -n "${val}" ] ; then
57 echo "UsernameAuthentication" >> $CONFIGFILE
58 echo "Username ${val}" >> $CONFIGFILE
59 fi
60
61 config_get val "$cfg" password
62 if [ -n "${val}" ] ; then
63 echo "PasswordAuthentication" >> $CONFIGFILE
64 echo "Password ${val}" >> $CONFIGFILE
65 fi
66 }
67
68 setup_mac_lists() {
69 local cfg="$1"
70 local MAC=""
71 local val
72
73 append_mac() {
74 append MAC $1 ,
75 }
76
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
83 fi
84
85 config_get val "$cfg" blockedmac
86 if [ -n "${val}" ] ; then
87 echo "Ignoring blockedmac - macmechanism not \"block\"" >&2
88 fi
89 elif [ "${val}" == "allow" ] ; then
90 MAC=""
91 config_list_foreach "$cfg" allowedmac append_mac
92 echo "AllowedMACList $MAC" >> $CONFIGFILE
93 elif [ "${val}" == "block" ] ; then
94 MAC=""
95 config_list_foreach "$cfg" blockedmac append_mac
96 echo "BlockedMACList $MAC" >> $CONFIGFILE
97 else
98 nolog error "$cfg Invalid macmechanism '$val' - allow or block are valid."
99 return 1
100 fi
101 MAC=""
102 config_list_foreach "$cfg" trustedmac append_mac
103 [ -n "$MAC" ] && echo "TrustedMACList $MAC" >> $CONFIGFILE
104 }
105
106 setup_firewall() {
107 local cfg="$1"
108 local uciname
109 local val
110
111 append_firewall() {
112 echo " FirewallRule $1" >> $CONFIGFILE
113 }
114
115 for rule in $(echo authenticated-users preauthenticated-users users-to-router trusted-users trusted-users-to-router)
116 do
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
124 done
125 }
126
127 generate_uci_config() {
128 local cfg="$1"
129 local val
130 local ifname
131 local download
132 local upload
133
134 CONFIGFILE="/tmp/etc/nodogsplash_$cfg.conf"
135
136 echo "# auto-generated config file from /etc/config/nodogsplash" > $CONFIGFILE
137 config_get val "$cfg" network
138 if [ ! -n "${val}" ] ; then
139 nolog error "$cfg missing network"
140 return 1
141 fi
142
143 if ! network_get_device ifname $val ; then
144 nolog error "$cfg can not find ifname for network '${val}'"
145 return 1
146 fi
147
148 echo "GatewayInterface $ifname" >> $CONFIGFILE
149 config_get val "$cfg" externalnetwork
150 [ -n "${val}" ] && network_get_device ifname ${val} && echo "ExternalInterface $ifname" >> $CONFIGFILE
151
152 append_config_option "$CONFIGFILE" "$cfg" gatewayname GatewayName
153 append_config_option "$CONFIGFILE" "$cfg" gatewayaddress GatewayAddress
154 append_config_option "$CONFIGFILE" "$cfg" gatewayport GatewayPort
155 append_config_option "$CONFIGFILE" "$cfg" maxclients MaxClients
156 append_config_option "$CONFIGFILE" "$cfg" imagedir ImagesDir
157 append_config_option "$CONFIGFILE" "$cfg" redirecturl RedirectURL
158 append_config_option "$CONFIGFILE" "$cfg" clientidletimeout ClientIdleTimeout
159 append_config_option "$CONFIGFILE" "$cfg" clientforcetimeout ClientForceTimeout
160 append_config_option "$CONFIGFILE" "$cfg" gatewayiprange GatewayIPRange
161 append_config_option "$CONFIGFILE" "$cfg" passwordattempts PasswordAttempts
162 append_config_option "$CONFIGFILE" "$cfg" macmechanism MACMechanism
163 append_config_option "$CONFIGFILE" "$cfg" uploadlimit UploadLimit
164 append_config_option "$CONFIGFILE" "$cfg" downloadlimit DownloadLimit
165
166 config_get download "$cfg" downloadlimit
167 config_get upload "$cfg" uploadlimit
168 [ -n "$upload" -o -n "$download" ] && echo "TrafficControl yes" >> $CONFIGFILE
169
170 setup_mac_lists "$cfg"
171 setup_user_authentication "$cfg"
172 setup_firewall "$cfg"
173 }
174
175 # setup configuration and start instance
176 create_instance() {
177 local cfg="$1"
178 local manual_config
179 local val
180 CONFIGFILE="/tmp/etc/nodogsplash_$cfg.conf"
181
182 config_get_bool val "$cfg" disabled 0
183 [ $val -gt 0 ] && return 0
184
185 config_get manual_config "$cfg" config ""
186 if [ ! -n "$manual_config" ] ; then
187 generate_uci_config "$cfg"
188 else
189 # check if configration exists
190 if [ ! -f "$manual_config" ] ; then
191 nolog error "Configuration file '$file' doesn't exists"
192 return 0
193 fi
194 CONFIGFILE="$manual_config"
195 fi
196
197 if ! test_module ; then
198 logger -s -t nodogsplash -p daemon.error "nodogsplash is missing some kernel modules"
199 fi
200
201 procd_open_instance $cfg
202 procd_set_param command /usr/bin/nodogsplash -c $CONFIGFILE $OPTIONS
203 procd_set_param respawn
204 procd_set_param file $CONFIGFILE
205 procd_close_instance
206 }
207
208 start_service() {
209 include /lib/functions
210
211 mkdir -p /tmp/etc/
212 config_load nodogsplash
213
214 config_foreach create_instance instance
215 }
216
217 stop_service() {
218 # nodogsplash doesn't exit fast enought, when procd terminates it.
219 # otherwise procd will restart nodogsplash twice. first time starting nodogsplash fails, second time it succeeds
220 sleep 1
221 }
222
223 status() {
224 $WD_DIR/ndsctl status
225 }
226
227 # Test if we got all modules loaded
228 test_module() {
229
230 ### Test ipt_mark with iptables
231 test_ipt_mark () {
232 ($IPT -A FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
233 IPTABLES_OK=$?
234 if [ "$IPTABLES_OK" -eq 0 ]; then
235 ($IPT -D FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
236 return 0
237 else
238 return 1
239 fi
240 }
241 ### Test ipt_mac with iptables
242 test_ipt_mac () {
243 ($IPT -A INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
244 IPTABLES_OK=$?
245 if [ "$IPTABLES_OK" -eq 0 ]; then
246 ($IPT -D INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
247 return 0
248 else
249 return 1
250 fi
251 }
252
253 ### Test ipt_IMQ with iptables
254 test_ipt_IMQ () {
255 ($IPT -t mangle -A PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
256 IPTABLES_OK=$?
257 if [ "$IPTABLES_OK" -eq 0 ]; then
258 ($IPT -t mangle -D PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
259 return 0
260 else
261 return 1
262 fi
263 }
264
265 ### Test imq with ip
266 test_imq () {
267 (ip link set imq0 up 2>&1) > /dev/null
268 IMQ0_OK=$?
269 (ip link set imq1 up 2>&1) > /dev/null
270 IMQ1_OK=$?
271 if [ "$IMQ0_OK" -eq 0 -a "$IMQ1_OK" -eq 0 ]; then
272 (ip link set imq0 down 2>&1) > /dev/null
273 (ip link set imq1 down 2>&1) > /dev/null
274 return 0
275 else
276 return 1
277 fi
278 }
279
280 ### Test sch_htb with tc; requires imq0
281 test_sch_htb () {
282 (tc qdisc del dev imq0 root 2>&1) > /dev/null
283 (tc qdisc add dev imq0 root htb 2>&1) > /dev/null
284 TC_OK=$?
285 if [ "$TC_OK" -eq 0 ]; then
286 (tc qdisc del dev imq0 root 2>&1) > /dev/null
287 return 0
288 else
289 return 1
290 fi
291 }
292
293
294 ### Find a module on disk
295 module_exists () {
296 EXIST=$(find /lib/modules/`uname -r` -name $1.*o 2> /dev/null)
297 if [ -n "$EXIST" ]; then
298 return 0
299 else
300 return 1
301 fi
302 }
303
304 ### Test if a module is in memory
305 module_in_memory () {
306 MODULE=$(lsmod | grep $1 | awk '{print $1}')
307 if [ "$MODULE" = "$1" ]; then
308 return 0
309 else
310 return 1
311 fi
312 }
313
314 ### Test functionality of a module; load if necessary
315 do_module_tests () {
316 echo " Testing module $1 $2"
317 "test_$1"
318 if [ $? -ne 0 ]; then
319 echo " Module $1 $2 needed"
320 echo " Scanning disk for $1 module"
321 module_exists $1
322 if [ $? -ne 0 ]; then
323 echo " $1 module missing: please install it"
324 exit 1
325 else
326 echo " $1 exists, trying to load"
327 insmod $1 $2 > /dev/null
328 if [ $? -ne 0 ]; then
329 echo " Error: insmod $1 $2 failed"
330 exit 1
331 else
332 echo " $1 $2 loaded successfully"
333 fi
334 fi
335 else
336 echo " $1 is working"
337 fi
338
339 }
340
341 echo " Testing required modules"
342
343 do_module_tests "ipt_mac"
344 do_module_tests "ipt_mark"
345
346 # test for imq modules, only if TrafficControl is enabled in conf
347 if ( grep -q -E '^[[:space:]]*TrafficControl[[:space:]]+(yes|true|1)' "$NDS_CONF" ) ; then
348 do_module_tests "imq" "numdevs=2"
349 do_module_tests "ipt_IMQ"
350 do_module_tests "sch_htb"
351 fi
352 }