applications/luci-splash: merge splash rework to trunk
[project/luci.git] / applications / luci-splash / root / etc / init.d / luci_splash
1 #!/bin/sh /etc/rc.common
2 START=70
3 EXTRA_COMMANDS=clear_leases
4
5 iface_add() {
6         local cfg="$1"
7         
8         config_get zone "$cfg" zone
9         [ -n "$zone" ] || return 0
10         
11         config_get net "$cfg" network
12         [ -n "$net" ] || return 0
13         
14         config_get ipaddr "$net" ipaddr
15         [ -n "$ipaddr" ] || return 0
16         
17         config_get netmask "$net" netmask
18         [ -n "$netmask" ] || return 0
19         
20         eval "$(ipcalc.sh $ipaddr $netmask)"
21
22         iptables -t nat -A prerouting_${zone} -j luci_splash_prerouting
23         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -p ! tcp -j luci_splash_portal
24         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -d ! "$ipaddr" -j luci_splash_portal
25         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -d "$ipaddr" -p tcp -m multiport ! --dport 22,80,443 -j luci_splash_portal
26 }
27
28 iface_del() {
29         config_get zone "$1" zone                                                                
30         [ -n "$zone" ] || return 0
31         while iptables -t nat -D prerouting_${zone} -j luci_splash_prerouting 2>&-; do :; done
32 }
33
34 blacklist_add() {
35         local cfg="$1"
36         
37         config_get mac "$cfg" mac
38         [ -n "$mac" ] && {
39                 iptables -I luci_splash_counter -m mac --mac-source "$mac" -j RETURN
40                 iptables -t nat -I luci_splash_leases -m mac --mac-source "$mac" -j DROP
41         }
42 }
43
44 whitelist_add() {
45         local cfg="$1"
46         
47         config_get mac "$cfg" mac
48         config_get ban "$cfg" kicked
49
50         ban=${ban:+DROP}
51
52         [ -n "$mac" ] && {
53                 iptables -I luci_splash_counter -m mac --mac-source "$mac" -j RETURN
54                 iptables -t nat -I luci_splash_leases -m mac --mac-source "$mac" -j "${ban:-RETURN}"
55         }
56 }
57
58 boot() {
59         ### We are started by the firewall include
60
61         uci get lucid.splashr || {
62 uci batch <<EOF
63         set lucid.splashr=daemon
64         set lucid.splashr.slave=httpd
65         add_list lucid.splashr.address=8082
66         add_list lucid.splashr.publisher=splashredir
67         set lucid.splashr.enabled=1
68
69         set lucid.splashredir=Redirector
70         set lucid.splashredir.name=Splashd
71         set lucid.splashredir.virtual='/'
72         set lucid.splashredir.physical=':80/luci/splash'
73
74         commit lucid
75 EOF
76         }
77         exit 0
78 }
79
80 start() {
81         ### Read chains from config
82         include /lib/network
83         scan_interfaces
84         config_load luci_splash
85
86         ### Create subchains
87         iptables -N luci_splash_counter
88         iptables -t nat -N luci_splash_portal
89         iptables -t nat -N luci_splash_leases
90         iptables -t nat -N luci_splash_prerouting
91
92         ### Build the main and portal rule
93         config_foreach blacklist_add blacklist
94         config_foreach whitelist_add whitelist
95         config_foreach whitelist_add lease
96         config_foreach iface_add iface
97
98         ### Build the portal rule
99         iptables -I INPUT -j luci_splash_counter
100         iptables -I FORWARD -j luci_splash_counter
101         iptables -t nat -A luci_splash_portal -p udp --dport 33434:33523 -j RETURN
102         iptables -t nat -A luci_splash_portal -p icmp -j RETURN
103         iptables -t nat -A luci_splash_portal -p udp --dport 53 -j RETURN
104         iptables -t nat -A luci_splash_portal -j luci_splash_leases
105
106         ### Build the leases rule
107         iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
108         iptables -t nat -A luci_splash_leases -j DROP
109
110         ### Add crontab entry
111         test -f /etc/crontabs/root || touch /etc/crontabs/root
112         grep -q luci-splash /etc/crontabs/root || {
113                 echo '*/5 * * * *       /usr/sbin/luci-splash sync' >> /etc/crontabs/root
114         }
115 }
116
117 stop() {
118         ### Clear interface rules
119         config_load luci_splash
120         config_foreach iface_del iface
121         iptables -D INPUT -j luci_splash_counter
122         iptables -D FORWARD -j luci_splash_counter
123
124         ### Clear subchains
125         iptables -t nat -F luci_splash_leases
126         iptables -t nat -F luci_splash_portal
127         iptables -t nat -F luci_splash_prerouting
128         iptables -F luci_splash_counter
129
130         ### Delete subchains
131         iptables -t nat -X luci_splash_leases
132         iptables -t nat -X luci_splash_portal
133         iptables -t nat -X luci_splash_prerouting
134         iptables -X luci_splash_counter
135         
136         sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
137 }
138
139         
140 clear_leases() {
141         stop
142         while uci -P /var/state del luci_splash.@lease[0] 2>&-;do :; done
143         start
144 }
145