b55e960720ce0b0972e94d402e3b5bf4d7144f72
[project/luci.git] / applications / luci-splash / root / usr / sbin / luci-splash
1 #!/usr/bin/lua
2
3 require("luci.util")
4 require("luci.model.uci")
5 require("luci.sys.iptparser")
6
7 -- Init state session
8 local uci = luci.model.uci.cursor_state()
9 local ipt = luci.sys.iptparser.IptParser()
10
11 local splash_interfaces = { }
12 local limit_up = 0
13 local limit_down = 0
14
15 function main(argv)
16         local cmd = argv[1]
17         local arg = argv[2]
18
19         limit_up = tonumber(uci:get("luci_splash", "general", "limit_up")) or 0
20         limit_down = tonumber(uci:get("luci_splash", "general", "limit_down")) or 0
21         
22         uci:foreach("luci_splash", "iface", function(s)
23                 if s.network then
24                         splash_interfaces[#splash_interfaces+1] = uci:get("network", s.network, "ifname")
25                 end
26         end)
27
28         if cmd == "status" and arg then
29                 if islisted("whitelist", arg) then
30                         print("whitelisted")
31                 elseif islisted("blacklist", arg) then
32                         print("blacklisted")
33                 else            
34                         local lease = haslease(arg)
35                         if lease and lease.kicked then
36                                 print("kicked")
37                         elseif lease then
38                                 print("lease")
39                         else
40                                 print("unknown")
41                         end
42                 end
43                 os.exit(0)
44         elseif cmd == "add" and arg then
45                 if not haslease(arg) then
46                         add_lease(arg)
47                 else
48                         print("already leased!")
49                         os.exit(2)
50                 end
51                 os.exit(0)
52         elseif cmd == "remove" and arg then
53                 remove_lease(arg)
54                 os.exit(0)              
55         elseif cmd == "sync" then
56                 sync()
57                 os.exit(0)
58         else
59                 print("Usage: " .. argv[0] .. " <status|add|remove|sync> [MAC]")
60                 os.exit(1)      
61         end
62 end
63
64 -- Add a lease to state and invoke add_rule
65 function add_lease(mac)
66         uci:section("luci_splash", "lease", nil, {
67                 mac = mac,
68                 start = os.time()
69         })
70         add_rule(mac)
71         
72         uci:save("luci_splash")
73 end
74
75
76 -- Remove a lease from state and invoke remove_rule
77 function remove_lease(mac)
78         mac = mac:lower()
79         remove_rule(mac)
80
81         uci:delete_all("luci_splash", "lease",
82                 function(s) return ( s.mac:lower() == mac ) end)
83                 
84         uci:save("luci_splash")
85 end
86
87
88 -- Add an iptables rule
89 function add_rule(mac)
90         local a, b, c, d, e, f = mac:match("(%w+):(%w+):(%w+):(%w+):(%w+):(%w+)")
91         local mac_pre  = "%s%s" %{ a, b }
92         local mac_post = "%s%s%s%s" %{ c, d, e, f }
93         local handle   = f
94
95         if limit_up > 0 and limit_down > 0 then
96                 os.execute("iptables -t mangle -I luci_splash_mark -m mac --mac-source %q -j MARK --set-mark 79" % mac)
97
98                 for _, i in ipairs(splash_interfaces) do
99                         os.execute("tc filter add dev %q parent 77:0 protocol ip prio 2 " % i .. 
100                                 "handle ::%q u32 " % handle ..
101                                 "match u16 0x0800 0xFFFF at -2 match u32 0x%q 0xFFFFFFFF at -12 " % mac_post ..
102                                 "match u16 0x%q 0xFFFF at -14 flowid 77:10" % mac_pre)
103                 end
104         end
105
106         os.execute("iptables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
107         return os.execute("iptables -t nat -I luci_splash_leases -m mac --mac-source %q -j RETURN" % mac)
108 end
109
110
111 -- Remove an iptables rule
112 function remove_rule(mac)
113         local handle = mac:match("%w+:%w+:%w+:%w+:%w+:(%w+)")
114
115         local function ipt_delete_foreach(args)
116                 for _, r in ipairs(ipt:find(args)) do
117                         os.execute("iptables -t %q -D %q -m mac --mac-source %q %s 2>/dev/null"
118                                 %{ r.table, r.chain, mac,
119                                         r.target == "MARK" and "-j MARK --set-mark 79" or
120                                                 r.target and "-j %q" % r.target or "" })
121                 end
122         end
123
124         ipt_delete_foreach({table="filter", chain="luci_splash_filter", options={"MAC", mac:upper()}})
125         ipt_delete_foreach({table="mangle", chain="luci_splash_mark", options={"MAC", mac:upper()}})
126         ipt_delete_foreach({table="nat", chain="luci_splash_leases", options={"MAC", mac:upper()}})
127
128         for _, i in ipairs(splash_interfaces) do
129                 os.execute("tc filter del dev %q parent 77:0 protocol ip prio 2 " % i .. 
130                         "handle 800::%q u32 2>/dev/null" % handle)
131         end
132
133         ipt:resync()
134 end
135
136
137 -- Check whether a MAC-Address is listed in the lease state list
138 function haslease(mac)
139         mac = mac:lower()
140         local lease = nil
141
142         uci:foreach("luci_splash", "lease",
143                 function (section)
144                         if section.mac:lower() == mac then
145                                 lease = section
146                         end
147                 end)
148
149         return lease
150 end
151
152
153 -- Check whether a MAC-Address is in given list
154 function islisted(what, mac)
155         mac = mac:lower()
156
157         uci:foreach("luci_splash", what,
158                 function (section)
159                         if section.mac:lower() == mac then
160                                 stat = true
161                                 return
162                         end
163                 end)
164
165         return false
166 end
167
168
169 -- Returns a list of MAC-Addresses for which a rule is existing
170 function listrules()
171         local macs = { }
172         for i, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases", options={"MAC"}})) do
173                 macs[r.options[2]:lower()] = true
174         end
175         return luci.util.keys(macs)
176 end
177
178
179 -- Synchronise leases, remove abandoned rules
180 function sync()
181         local written = {}
182         local time = os.time()
183         
184         -- Current leases in state files
185         local leases = uci:get_all("luci_splash")
186         
187         -- Convert leasetime to seconds
188         local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime")) * 3600
189         
190         -- Clean state file
191         uci:load("luci_splash")
192         uci:revert("luci_splash")
193         
194         
195         -- For all leases
196         for k, v in pairs(leases) do
197                 if v[".type"] == "lease" then
198                         if os.difftime(time, tonumber(v.start)) > leasetime then
199                                 -- Remove expired
200                                 remove_rule(v.mac)
201                         else
202                                 -- Rewrite state
203                                 uci:section("luci_splash", "lease", nil, {              
204                                         mac    = v.mac,
205                                         start  = v.start,
206                                         kicked = v.kicked
207                                 })
208                                 written[v.mac:lower()] = 1
209                         end
210                 elseif v[".type"] == "whitelist" or v[".type"] == "blacklist" then
211                         written[v.mac:lower()] = 1
212                 end
213         end
214         
215         
216         -- Delete rules without state
217         for i, r in ipairs(listrules()) do
218                 if #r > 0 and not written[r:lower()] then
219                         remove_rule(r)
220                 end
221         end
222         
223         uci:save("luci_splash")
224 end
225
226 main(arg)