2 package.path = "/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua;" .. package.path
3 package.cpath = "/usr/lib/lua/?.so;" .. package.cpath
7 require("ffluci.model.uci")
10 uci = ffluci.model.uci.Session("/var/state")
13 -- Parse stdin and do something
19 print("Usage: " .. argv[0] .. " <status|add|remove|sync> [MAC]")
21 elseif cmd == "status" then
26 if iswhitelisted(arg) then
38 elseif cmd == "add" then
43 if not haslease(arg) then
46 print("already leased!")
50 elseif cmd == "remove" then
57 elseif cmd == "sync" then
63 -- Add a lease to state and invoke add_rule
64 function add_lease(mac)
65 local key = uci:add("luci_splash", "lease")
66 uci:set("luci_splash", key, "mac", mac)
67 uci:set("luci_splash", key, "start", os.time())
72 -- Remove a lease from state and invoke remove_rule
73 function remove_lease(mac)
76 for k, v in pairs(uci:show("luci_splash").luci_splash) do
77 if v.mac:lower() == mac then
79 uci:del("luci_splash", k)
85 -- Add an iptables rule
86 function add_rule(mac)
87 return os.execute("iptables -t nat -I luci_splash_leases -m mac --mac-source '"..mac.."' -j RETURN")
91 -- Remove an iptables rule
92 function remove_rule(mac)
93 return os.execute("iptables -t nat -D luci_splash_leases -m mac --mac-source '"..mac.."' -j RETURN")
97 -- Check whether a MAC-Address is listed in the lease state list
98 function haslease(mac)
101 for k, v in pairs(uci:show("luci_splash").luci_splash) do
102 if v[".type"] == "lease" and v.mac and v.mac:lower() == mac then
111 -- Check whether a MAC-Address is whitelisted
112 function iswhitelisted(mac)
115 for k, v in pairs(uci:show("luci_splash").luci_splash) do
116 if v[".type"] == "whitelist" and v.mac and v.mac:lower() == mac then
125 -- Returns a list of MAC-Addresses for which a rule is existing
127 local cmd = "iptables -t nat -L luci_splash_leases | grep RETURN |"
128 cmd = cmd .. "egrep -io [0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+"
129 return ffluci.util.split(ffluci.sys.exec(cmd))
133 -- Synchronise leases, remove abandoned rules
136 local time = os.time()
138 -- Current leases in state files
139 local leases = uci:show("luci_splash").luci_splash
141 -- Convert leasetime to seconds
142 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime")) * 3600
145 uci:revert("luci_splash")
149 for k, v in pairs(uci:show("luci_splash")) do
150 if v[".type"] == "lease" then
151 if os.difftime(time, tonumber(v.start)) > leasetime then
156 local n = uci:add("luci_splash", "lease")
157 uci:set("luci_splash", n, "mac", v.mac)
158 uci:set("luci_splash", n, "start", v.start)
165 -- Delete rules without state
166 for i, r in ipairs(listrules()) do
167 if #r > 0 and not written[r] then