1d6e98814cc7df87180da37cb24ec28f55347cbd
[project/luci.git] / applications / luci-splash / root / etc / init.d / luci_splash
1 #!/bin/sh /etc/rc.common
2 START=70
3
4 iface_add() {
5         local cfg="$1"
6         
7         config_get zone "$cfg" zone
8         [ -n "$zone" ] || return 0
9         
10         config_get net "$cfg" network
11         [ -n "$net" ] || return 0
12         
13         config_get ipaddr "$net" ipaddr
14         [ -n "$ipaddr" ] || return 0
15         
16         config_get netmask "$net" netmask
17         [ -n "$netmask" ] || return 0
18         
19         eval "$(ipcalc.sh $ipaddr $netmask)"
20
21         iptables -t nat -A prerouting_${zone} -j luci_splash_prerouting
22         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -p ! tcp -j luci_splash_portal
23         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -d ! "$ipaddr" -j luci_splash_portal
24         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -d "$ipaddr" -p tcp -m multiport ! --dport 22,80,443 -j luci_splash_portal
25 }
26
27 blacklist_add() {
28         local cfg="$1"
29         
30         config_get mac "$cfg" mac
31         [ -n "$mac" ] && iptables -t nat -A luci_splash_portal -m mac --mac-source "$mac" -j DROP
32 }
33
34 whitelist_add() {
35         local cfg="$1"
36         
37         config_get mac "$cfg" mac
38         [ -n "$mac" ] && iptables -t nat -A luci_splash_portal -m mac --mac-source "$mac" -j RETURN
39 }
40
41 start() {
42         ### Read chains from config
43         include /lib/network
44         scan_interfaces
45         config_load luci_splash
46         
47         ### Create subchains
48         iptables -t nat -N luci_splash_portal
49         iptables -t nat -N luci_splash_leases
50         iptables -t nat -N luci_splash_prerouting
51         
52         ### Build the main and portal rule
53         config_foreach blacklist_add blacklist
54         config_foreach whitelist_add whitelist
55         config_foreach iface_add iface
56         
57         ### Build the portal rule
58         iptables -t nat -A luci_splash_portal -p udp --dport 53 -j RETURN
59         iptables -t nat -A luci_splash_portal -j luci_splash_leases
60         
61         ### Build the leases rule
62         iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
63         iptables -t nat -A luci_splash_leases -j DROP
64         
65         ### Add crontab entry
66         grep luci-splash /var/spool/cron/crontabs/root >/dev/null 2>&1 || {
67                 echo '*/5 * * * *       /usr/sbin/luci-splash sync' >> /var/spool/cron/crontabs/root
68         }
69
70         ### Start the splash httpd
71         start-stop-daemon -S -b -q -x /usr/bin/luci-splashd
72 }
73
74 iface_del() {
75         config_get zone "$1" zone                                                                
76         [ -n "$zone" ] || return 0
77         iptables -t nat -D prerouting_${zone} -j luci_splash_prerouting
78 }
79
80 stop() {
81         ### Clear interface rules
82         config_load luci_splash
83         config_foreach iface_del iface
84         
85         ### Clear subchains
86         iptables -t nat -F luci_splash_leases
87         iptables -t nat -F luci_splash_portal
88         iptables -t nat -F luci_splash_prerouting
89         
90         ### Delete subchains
91         iptables -t nat -X luci_splash_leases
92         iptables -t nat -X luci_splash_portal
93         iptables -t nat -X luci_splash_prerouting
94
95         ### Stop the splash httpd
96         start-stop-daemon -K -q -x /usr/bin/luci-splashd
97 }
98