56762804e0bbbe566c04af826d9efeb9b45f3401
[openwrt/svn-archive/archive.git] / net / nodogsplash / files / nodogsplash.init
1 #!/bin/sh /etc/rc.common
2 #
3 # description: Startup/shutdown script for nodogsplash captive portal
4 #
5 # P. Kube 2007
6 #
7 # (Based on wifidog startup script
8 # Date : 2004-08-25
9 # Version : 1.0
10 # Comment by that author: Could be better, but it's working as expected)
11 #
12
13
14 IPT=/usr/sbin/iptables
15 WD_DIR=/usr/bin
16 OPTIONS=""
17 START=65
18 STOP=65
19 # -s -d 5 runs in background, with level 5 (not so verbose) messages to syslog
20 # -f -d 7 runs in foreground, with level 7 (verbose) debug messages to terminal
21 # N.B.: -f will fail if starting at boot from rcS
22 #OPTIONS="-s -d 5"
23
24 start() {
25 echo "Starting nodogsplash ... "
26 if $WD_DIR/ndsctl status 2> /dev/null; then
27 echo "FAILED: nodogsplash already running"
28 else
29 if $0 test_module && $WD_DIR/nodogsplash $OPTIONS; then
30 echo "OK: nodogsplash started"
31 else
32 echo "FAILED: nodogsplash exited with non 0 status"
33 fi
34 fi
35 }
36
37 stop() {
38 echo "Stopping nodogsplash ... "
39 if $WD_DIR/ndsctl status 2> /dev/null; then
40 if $WD_DIR/ndsctl stop; then
41 echo "OK: nodogsplash stopped"
42 else
43 echo "FAILED: ndsctl stop exited with non 0 status"
44 fi
45 else
46 echo "FAILED: nodogsplash was not running"
47 fi
48 status() {
49 $WD_DIR/ndsctl status
50 }
51
52 test_module() {
53
54 ### Test ipt_mark with iptables
55 test_ipt_mark () {
56 ($IPT -A FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
57 IPTABLES_OK=$?
58 if [ "$IPTABLES_OK" -eq 0 ]; then
59 ($IPT -D FORWARD -m mark --mark 2 -j ACCEPT 2>&1) > /dev/null
60 return 0
61 else
62 return 1
63 fi
64 }
65 ### Test ipt_mac with iptables
66 test_ipt_mac () {
67 ($IPT -A INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
68 IPTABLES_OK=$?
69 if [ "$IPTABLES_OK" -eq 0 ]; then
70 ($IPT -D INPUT -m mac --mac-source 00:00:00:00:00:00 -j ACCEPT 2>&1) > /dev/null
71 return 0
72 else
73 return 1
74 fi
75 }
76
77 ### Test ipt_IMQ with iptables
78 test_ipt_IMQ () {
79 ($IPT -t mangle -A PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
80 IPTABLES_OK=$?
81 if [ "$IPTABLES_OK" -eq 0 ]; then
82 ($IPT -t mangle -D PREROUTING -j IMQ --todev 0 2>&1) > /dev/null
83 return 0
84 else
85 return 1
86 fi
87 }
88
89 ### Test imq with ip
90 test_imq () {
91 (ip link set imq0 up 2>&1) > /dev/null
92 IMQ0_OK=$?
93 (ip link set imq1 up 2>&1) > /dev/null
94 IMQ1_OK=$?
95 if [ "$IMQ0_OK" -eq 0 -a "$IMQ1_OK" -eq 0 ]; then
96 (ip link set imq0 down 2>&1) > /dev/null
97 (ip link set imq1 down 2>&1) > /dev/null
98 return 0
99 else
100 return 1
101 fi
102 }
103
104 ### Test sch_htb with tc; requires imq0
105 test_sch_htb () {
106 (tc qdisc del dev imq0 root 2>&1) > /dev/null
107 (tc qdisc add dev imq0 root htb 2>&1) > /dev/null
108 TC_OK=$?
109 if [ "$TC_OK" -eq 0 ]; then
110 (tc qdisc del dev imq0 root 2>&1) > /dev/null
111 return 0
112 else
113 return 1
114 fi
115 }
116
117
118 ### Find a module on disk
119 module_exists () {
120 EXIST=$(find /lib/modules/`uname -r` -name $1.*o 2> /dev/null)
121 if [ -n "$EXIST" ]; then
122 return 0
123 else
124 return 1
125 fi
126 }
127
128 ### Test if a module is in memory
129 module_in_memory () {
130 MODULE=$(lsmod | grep $1 | awk '{print $1}')
131 if [ "$MODULE" = "$1" ]; then
132 return 0
133 else
134 return 1
135 fi
136 }
137
138 ### Test functionality of a module; load if necessary
139 do_module_tests () {
140 echo " Testing module $1 $2"
141 "test_$1"
142 if [ $? -ne 0 ]; then
143 echo " Module $1 $2 needed"
144 echo " Scanning disk for $1 module"
145 module_exists $1
146 if [ $? -ne 0 ]; then
147 echo " $1 module missing: please install it"
148 exit 1
149 else
150 echo " $1 exists, trying to load"
151 insmod $1 $2 > /dev/null
152 if [ $? -ne 0 ]; then
153 echo " Error: insmod $1 $2 failed"
154 exit 1
155 else
156 echo " $1 $2 loaded successfully"
157 fi
158 fi
159 else
160 echo " $1 is working"
161 fi
162
163 }
164
165 echo " Testing required modules"
166
167 do_module_tests "ipt_mac"
168 do_module_tests "ipt_mark"
169 # if not using traffic control,
170 # you can comment out the following 3 lines:
171 do_module_tests "imq" "numdevs=2"
172 do_module_tests "ipt_IMQ"
173 do_module_tests "sch_htb"
174 }