049d121ea53a8dea2da75a4f95706ca453923115
[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 }
24
25 blacklist_add() {
26         local cfg="$1"
27         
28         config_get mac "$cfg" mac
29         [ -n "$mac" ] && iptables -t nat -A luci_splash_portal -m mac --mac-source "$mac" -j REJECT
30 }
31
32 whitelist_add() {
33         local cfg="$1"
34         
35         config_get mac "$cfg" mac
36         [ -n "$mac" ] && iptables -t nat -A luci_splash_portal -m mac --mac-source "$mac" -j RETURN
37 }
38
39 start() {
40         ### Read chains from config
41         include /lib/network
42         scan_interfaces
43         config_load luci_splash
44         
45         ### Create subchains
46         iptables -t nat -N luci_splash
47         iptables -t nat -N luci_splash_portal
48         iptables -t nat -N luci_splash_leases
49         
50         ### Build the main rule
51         config_foreach iface_add iface
52         
53         ### Build the portal rule
54         config_foreach blacklist_add blacklist
55         config_foreach whitelist_add whitelist
56         iptables -t nat -A luci_splash_portal -j luci_splash_leases
57         
58         ### Build the leases rule
59         iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
60         iptables -t nat -A luci_splash_leases -j REJECT
61         
62         ### Start the splash httpd
63         httpd -c /etc/luci_splash_httpd.conf -p 8082 -h /usr/lib/luci_splash/htdocs
64         
65         ### Sync leases
66         /usr/lib/luci_splash/sync.lua
67         
68         ### Hook in the chain
69         iptables -t nat -A prerouting_rule -j luci_splash
70 }
71
72 stop() {
73         ### Hook out the chain
74         iptables -t nat -D prerouting_rule -j luci_splash
75         
76         ### Clear subchains
77         iptables -t nat -F luci_splash
78         iptables -t nat -F luci_splash_portal
79         iptables -t nat -F luci_splash_leases
80         
81         ### Delete subchains
82         iptables -t nat -X luci_splash
83         iptables -t nat -X luci_splash_portal
84         iptables -t nat -X luci_splash_leases
85 }
86