applications/luci-splash: fix crontab setup at boot time
[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" ] && iptables -t nat -I luci_splash_leases -m mac --mac-source "$mac" -j DROP
39 }
40
41 whitelist_add() {
42         local cfg="$1"
43         
44         config_get mac "$cfg" mac
45         [ -n "$mac" ] && iptables -t nat -I luci_splash_leases -m mac --mac-source "$mac" -j RETURN
46 }
47
48 boot() {
49         ### We are started by the firewall include
50         exit 0
51 }
52
53 start() {
54         ### Read chains from config
55         include /lib/network
56         scan_interfaces
57         config_load luci_splash
58         
59         ### Create subchains
60         iptables -t nat -N luci_splash_portal
61         iptables -t nat -N luci_splash_leases
62         iptables -t nat -N luci_splash_prerouting
63         
64         ### Build the main and portal rule
65         config_foreach blacklist_add blacklist
66         config_foreach whitelist_add whitelist
67         config_foreach whitelist_add lease
68         config_foreach iface_add iface
69         
70         ### Build the portal rule
71         iptables -t nat -A luci_splash_portal -p udp --dport 33434:33523 -j RETURN
72         iptables -t nat -A luci_splash_portal -p icmp -j RETURN
73         iptables -t nat -A luci_splash_portal -p udp --dport 53 -j RETURN
74         iptables -t nat -A luci_splash_portal -j luci_splash_leases
75         
76         ### Build the leases rule
77         iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
78         iptables -t nat -A luci_splash_leases -j DROP
79         
80         ### Add crontab entry
81         test -f /etc/crontabs/root || touch /etc/crontabs/root
82         grep -q luci-splash /etc/crontabs/root || {
83                 echo '*/5 * * * *       /usr/sbin/luci-splash sync' >> /etc/crontabs/root
84         }
85
86         ### Start the splash httpd
87         start-stop-daemon -S -m -p /var/run/luci-splashd.pid -b -q -x /usr/bin/luci-splashd
88 }
89
90 stop() {
91         ### Clear interface rules
92         config_load luci_splash
93         config_foreach iface_del iface
94         
95         ### Clear subchains
96         iptables -t nat -F luci_splash_leases
97         iptables -t nat -F luci_splash_portal
98         iptables -t nat -F luci_splash_prerouting
99         
100         ### Delete subchains
101         iptables -t nat -X luci_splash_leases
102         iptables -t nat -X luci_splash_portal
103         iptables -t nat -X luci_splash_prerouting
104
105         ### Stop the splash httpd
106         start-stop-daemon -K -p /var/run/luci-splashd.pid -s KILL -q
107         
108         sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
109 }
110
111         
112 clear_leases() {
113         stop
114         while uci -P /var/state del luci_splash.@lease[0] 2>&-;do :; done
115         start
116 }
117