(no commit message)
[project/luci.git] / contrib / package / ffluci-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 "$NETWORK/$PREFIX" -j luci_splash_portal
23         iptables -t nat -A luci_splash_portal -i "$iface" -s "$NETWORK/$PREFIX" -d "$ipaddr" -p tcp -m multiport --dports 22,80,443 -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 and portal rule
52         config_foreach blacklist_add blacklist
53         config_foreach whitelist_add whitelist
54         config_foreach iface_add iface
55         
56         ### Build the portal rule
57         iptables -t nat -A luci_splash_portal -p udp --dport 53 -j RETURN
58         iptables -t nat -A luci_splash_portal -j luci_splash_leases
59         
60         ### Build the leases rule
61         iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
62         iptables -t nat -A luci_splash_leases -j DROP
63         
64         ### Start the splash httpd
65         httpd -c /etc/luci_splash_httpd.conf -p 8082 -h /usr/lib/luci-splash/htdocs
66         
67         ### Hook in the chain
68         iptables -t nat -A prerouting_rule -j luci_splash
69 }
70
71 stop() {
72         ### Hook out the chain
73         iptables -t nat -D prerouting_rule -j luci_splash
74         
75         ### Clear subchains
76         iptables -t nat -F luci_splash_leases
77         iptables -t nat -F luci_splash_portal
78         iptables -t nat -F luci_splash  
79         
80         ### Delete subchains
81         iptables -t nat -X luci_splash_leases
82         iptables -t nat -X luci_splash_portal
83         iptables -t nat -X luci_splash
84 }
85