nodogsplash: remove option ExternalInterface as there is no implementation behind it
[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
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"
142 return 0
143 fi
144 cat $val > CONFIGFILE
145 fi
146
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'"
151 return 1
152 fi
153 fi
154
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"
159 return 1
160 fi
161 ifname="$val"
162 fi
163
164 if [ -z "$ifname" ] ; then
165 nolog error "$cfg option network or gatewayinterface missing"
166 return 1
167 fi
168
169 echo "GatewayInterface $ifname" >> $CONFIGFILE
170
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
184
185 config_get download "$cfg" downloadlimit
186 config_get upload "$cfg" uploadlimit
187 [ -n "$upload" -o -n "$download" ] && echo "TrafficControl yes" >> $CONFIGFILE
188
189 setup_mac_lists "$cfg"
190 setup_user_authentication "$cfg"
191 setup_firewall "$cfg"
192 }
193
194 # setup configuration and start instance
195 create_instance() {
196 local cfg="$1"
197 local manual_config
198 local val
199
200 config_get_bool val "$cfg" enabled 0
201 [ $val -gt 0 ] || return 0
202
203 generate_uci_config "$cfg"
204
205 if ! test_module ; then
206 logger -s -t nodogsplash -p daemon.error "nodogsplash is missing some kernel modules"
207 fi
208
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
213 procd_close_instance
214 }
215
216 start_service() {
217 include /lib/functions
218
219 mkdir -p /tmp/etc/
220 config_load nodogsplash
221
222 config_foreach create_instance instance
223 }
224
225 stop_service() {
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
228 sleep 1
229 }
230
231 status() {
232 $WD_DIR/ndsctl status
233 }
234
235 # Test if we got all modules loaded
236 test_module() {
237 ### Test ipt_mark with iptables
238 test_ipt_mark () {
239 ($IPT -A FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
240 IPTABLES_OK=$?
241 if [ "$IPTABLES_OK" -eq 0 ]; then
242 ($IPT -D FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
243 return 0
244 else
245 return 1
246 fi
247 }
248
249 ### Test ipt_mac with iptables
250 test_ipt_mac () {
251 ($IPT -A INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
252 IPTABLES_OK=$?
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
255 return 0
256 else
257 return 1
258 fi
259 }
260
261 ### Test ipt_IMQ with iptables
262 test_ipt_IMQ () {
263 ($IPT -t mangle -A PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
264 IPTABLES_OK=$?
265 if [ "$IPTABLES_OK" -eq 0 ]; then
266 ($IPT -t mangle -D PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
267 return 0
268 else
269 return 1
270 fi
271 }
272
273 ### Test imq with ip
274 test_imq () {
275 (ip link set imq0 up 2>&1) > /dev/null
276 IMQ0_OK=$?
277 (ip link set imq1 up 2>&1) > /dev/null
278 IMQ1_OK=$?
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
282 return 0
283 else
284 return 1
285 fi
286 }
287
288 ### Test sch_htb with tc; requires imq0
289 test_sch_htb () {
290 (tc qdisc del dev imq0 root 2>&1) > /dev/null
291 (tc qdisc add dev imq0 root htb 2>&1) > /dev/null
292 TC_OK=$?
293 if [ "$TC_OK" -eq 0 ]; then
294 (tc qdisc del dev imq0 root 2>&1) > /dev/null
295 return 0
296 else
297 return 1
298 fi
299 }
300
301 ### Find a module on disk
302 module_exists () {
303 EXIST=$(find /lib/modules/`uname -r` -name $1.*o 2> /dev/null)
304 if [ -n "$EXIST" ]; then
305 return 0
306 else
307 return 1
308 fi
309 }
310
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
315 return 0
316 else
317 return 1
318 fi
319 }
320
321 ### Test functionality of a module; load if necessary
322 do_module_tests () {
323 echo " Testing module $1 $2"
324 "test_$1"
325 if [ $? -ne 0 ]; then
326 echo " Module $1 $2 needed"
327 echo " Scanning disk for $1 module"
328 module_exists $1
329 if [ $? -ne 0 ]; then
330 echo " $1 module missing: please install it"
331 exit 1
332 else
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"
337 exit 1
338 else
339 echo " $1 $2 loaded successfully"
340 fi
341 fi
342 else
343 echo " $1 is working"
344 fi
345 }
346
347 echo " Testing required modules"
348
349 do_module_tests "ipt_mac"
350 do_module_tests "ipt_mark"
351
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"
357 fi
358 }