* Added experimental version of DHCP-Splash for Kamikaze
[project/luci.git] / contrib / package / luci-splash / src / luci_splash / splash.lua
1 package.path  = "/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua;" .. package.path
2 package.cpath = "/usr/lib/lua/?.so;" .. package.cpath
3
4 require("ffluci.http")
5 require("ffluci.sys")
6 require("ffluci.model.uci")
7
8 ucis = ffluci.model.uci.Session("/var/state")
9
10 function add_lease(mac)
11         local key = ucis:add("luci_splash", "lease")
12         ucis:set("luci_splash", key, "mac", mac)
13         add_rule(mac)
14 end
15
16 function add_rule(mac)
17         return os.execute("iptables -t nat -I luci_splash_leases -m mac --source-mac '"..mac.."' -j RETURN")
18 end
19
20 function remove_rule(mac)
21         return os.execute("iptables -t nat -D luci_splash_leases -m mac --source-mac '"..mac.."' -j RETURN")
22 end
23
24 function get_usermac()
25         local ip  = ffluci.http.remote_addr()
26         local mac = nil
27         
28         for i, l in ipairs(ffluci.sys.net.arptable()) do
29                 if l["IP address"] == ip then
30                         mac = l["HW address"]
31                 end
32         end
33         
34         return mac
35 end
36
37 function haslease(mac)
38         mac = mac:lower()
39         local list = ucis:show("luci_splash").luci_splash
40         
41         for k, v in pairs(list) do
42                 if v[".type"] == "lease" and v.mac and v.mac:lower() == mac then
43                         return true
44                 end
45         end
46         
47         return false
48 end
49
50 function isblacklisted(mac)
51         mac = mac:lower()
52         local list = ucis:show("luci_splash").luci_splash
53         
54         for k, v in pairs(list) do
55                 if v[".type"] == "blacklist" and v.mac and v.mac:lower() == mac then
56                         return true
57                 end
58         end
59         
60         return false
61 end
62
63 function iswhitelisted(mac)
64         mac = mac:lower()
65         local list = ucis:show("luci_splash").luci_splash
66         
67         for k, v in pairs(list) do
68                 if v[".type"] == "whitelist" and v.mac and v.mac:lower() == mac then
69                         return true
70                 end
71         end
72         
73         return false
74 end