10d6411d12dfb696946ce541665f80e24d282f31
[project/luci.git] / contrib / package / luci-splash / src / luci_splash.init
1 #!/bin/sh /etc/rc.common
2 START=70
3
4 iface_add() {
5         local cfg="$1"
6         
7         config_get net "$cfg" network
8         [ -n "$net" ] || return 0
9         
10         config_get iface "$net" ifname
11         [ -n "$iface" ] || return 0
12         iface="${iface%%:*}"
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 luci_splash -i "$iface" -s "$IP/$PREFIX" -j luci_splash_portal
23         iptables -t nat -A luci_splash_portal -i "$iface" -s "$IP/$PREFIX" -d "$ipaddr" -p tcp --dport 80 -j RETURN 
24 }
25
26 blacklist_add() {
27         local cfg="$1"
28         
29         config_get mac "$cfg" mac
30         [ -n "$mac" ] && iptables -t nat -A luci_splash_portal -m mac --mac-source "$mac" -j DROP
31 }
32
33 whitelist_add() {
34         local cfg="$1"
35         
36         config_get mac "$cfg" mac
37         [ -n "$mac" ] && iptables -t nat -A luci_splash_portal -m mac --mac-source "$mac" -j RETURN
38 }
39
40 start() {
41         ### Read chains from config
42         include /lib/network
43         scan_interfaces
44         config_load luci_splash
45         
46         ### Create subchains
47         iptables -t nat -N luci_splash
48         iptables -t nat -N luci_splash_portal
49         iptables -t nat -N luci_splash_leases
50         
51         ### Build the main rule
52         config_foreach iface_add iface
53         
54         ### Build the portal rule
55         config_foreach blacklist_add blacklist
56         config_foreach whitelist_add whitelist
57         iptables -t nat -A luci_splash_portal -j luci_splash_leases
58         
59         ### Build the leases rule
60         iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
61         iptables -t nat -A luci_splash_leases -j DROP
62         
63         ### Start the splash httpd
64         httpd -c /etc/luci_splash_httpd.conf -p 8082 -h /usr/lib/luci-splash/htdocs
65         
66         ### Sync leases
67         /usr/lib/luci-splash/sync.lua
68         
69         ### Hook in the chain
70         iptables -t nat -A prerouting_rule -j luci_splash
71 }
72
73 stop() {
74         ### Hook out the chain
75         iptables -t nat -D prerouting_rule -j luci_splash
76         
77         ### Clear subchains
78         iptables -t nat -F luci_splash_leases
79         iptables -t nat -F luci_splash_portal
80         iptables -t nat -F luci_splash  
81         
82         ### Delete subchains
83         iptables -t nat -X luci_splash_leases
84         iptables -t nat -X luci_splash_portal
85         iptables -t nat -X luci_splash
86 }
87