2008-05-08 16:20:53 +00:00
|
|
|
#!/usr/bin/lua
|
|
|
|
|
2008-08-06 20:11:15 +00:00
|
|
|
require("luci.util")
|
2008-05-25 17:00:30 +00:00
|
|
|
require("luci.model.uci")
|
2009-06-06 09:41:02 +00:00
|
|
|
require("luci.sys.iptparser")
|
2008-05-08 16:20:53 +00:00
|
|
|
|
|
|
|
-- Init state session
|
2008-08-26 23:00:44 +00:00
|
|
|
local uci = luci.model.uci.cursor_state()
|
2009-06-06 09:41:02 +00:00
|
|
|
local ipt = luci.sys.iptparser.IptParser()
|
2008-05-08 16:20:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
function main(argv)
|
|
|
|
local cmd = argv[1]
|
|
|
|
local arg = argv[2]
|
|
|
|
|
2009-06-06 09:41:02 +00:00
|
|
|
if cmd == "status" and arg then
|
|
|
|
if islisted("whitelist", arg) then
|
2008-05-08 16:20:53 +00:00
|
|
|
print("whitelisted")
|
2009-06-06 09:41:02 +00:00
|
|
|
elseif islisted("blacklist", arg) then
|
|
|
|
print("blacklisted")
|
|
|
|
else
|
|
|
|
local lease = haslease(arg)
|
|
|
|
if lease and lease.kicked then
|
|
|
|
print("kicked")
|
|
|
|
elseif lease then
|
|
|
|
print("lease")
|
|
|
|
else
|
|
|
|
print("unknown")
|
|
|
|
end
|
2008-05-08 16:20:53 +00:00
|
|
|
end
|
|
|
|
os.exit(0)
|
2009-06-06 09:41:02 +00:00
|
|
|
elseif cmd == "add" and arg then
|
2008-05-08 16:20:53 +00:00
|
|
|
if not haslease(arg) then
|
|
|
|
add_lease(arg)
|
|
|
|
else
|
|
|
|
print("already leased!")
|
|
|
|
os.exit(2)
|
|
|
|
end
|
|
|
|
os.exit(0)
|
2009-06-06 09:41:02 +00:00
|
|
|
elseif cmd == "remove" and arg then
|
2008-05-08 16:20:53 +00:00
|
|
|
remove_lease(arg)
|
|
|
|
os.exit(0)
|
|
|
|
elseif cmd == "sync" then
|
|
|
|
sync()
|
|
|
|
os.exit(0)
|
|
|
|
else
|
|
|
|
print("Usage: " .. argv[0] .. " <status|add|remove|sync> [MAC]")
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add a lease to state and invoke add_rule
|
|
|
|
function add_lease(mac)
|
2008-08-26 23:00:44 +00:00
|
|
|
uci:section("luci_splash", "lease", nil, {
|
2008-06-06 20:45:33 +00:00
|
|
|
mac = mac,
|
|
|
|
start = os.time()
|
|
|
|
})
|
2008-05-08 16:20:53 +00:00
|
|
|
add_rule(mac)
|
2008-06-05 19:16:38 +00:00
|
|
|
|
2008-08-26 23:00:44 +00:00
|
|
|
uci:save("luci_splash")
|
2008-05-08 16:20:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Remove a lease from state and invoke remove_rule
|
|
|
|
function remove_lease(mac)
|
|
|
|
mac = mac:lower()
|
2009-06-06 09:41:02 +00:00
|
|
|
remove_rule(mac)
|
2008-05-08 16:20:53 +00:00
|
|
|
|
2009-06-06 09:41:02 +00:00
|
|
|
uci:delete_all("luci_splash", "lease",
|
|
|
|
function(s) return ( s.mac:lower() == mac ) end)
|
2008-06-06 21:49:17 +00:00
|
|
|
|
2008-08-26 23:00:44 +00:00
|
|
|
uci:save("luci_splash")
|
2008-05-08 16:20:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Add an iptables rule
|
|
|
|
function add_rule(mac)
|
2009-06-07 13:29:17 +00:00
|
|
|
os.execute("iptables -I luci_splash_counter -m mac --mac-source '"..mac.."' -j RETURN")
|
2008-05-08 16:20:53 +00:00
|
|
|
return os.execute("iptables -t nat -I luci_splash_leases -m mac --mac-source '"..mac.."' -j RETURN")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Remove an iptables rule
|
|
|
|
function remove_rule(mac)
|
2009-06-06 09:41:02 +00:00
|
|
|
for _, r in ipairs(ipt:find({table="filter", chain="luci_splash_counter"})) do
|
|
|
|
if r.options and #r.options >= 2 and r.options[1] == "MAC" and
|
|
|
|
r.options[2]:lower() == mac:lower()
|
|
|
|
then
|
|
|
|
os.execute("iptables -D luci_splash_counter -m mac --mac-source %q -j %s"
|
|
|
|
%{ mac, r.target })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
|
|
|
|
if r.options and #r.options >= 2 and r.options[1] == "MAC" and
|
|
|
|
r.options[2]:lower() == mac:lower()
|
|
|
|
then
|
|
|
|
os.execute("iptables -t nat -D luci_splash_leases -m mac --mac-source %q -j %s"
|
|
|
|
%{ mac, r.target })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ipt:resync()
|
2008-05-08 16:20:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Check whether a MAC-Address is listed in the lease state list
|
|
|
|
function haslease(mac)
|
|
|
|
mac = mac:lower()
|
2009-06-06 09:41:02 +00:00
|
|
|
local lease = nil
|
|
|
|
|
2008-08-26 23:00:44 +00:00
|
|
|
uci:foreach("luci_splash", "lease",
|
2008-06-05 19:16:38 +00:00
|
|
|
function (section)
|
|
|
|
if section.mac:lower() == mac then
|
2009-06-06 09:41:02 +00:00
|
|
|
lease = section
|
2008-06-05 19:16:38 +00:00
|
|
|
end
|
|
|
|
end)
|
2009-06-06 09:41:02 +00:00
|
|
|
|
|
|
|
return lease
|
2008-05-08 16:20:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-06-06 09:41:02 +00:00
|
|
|
-- Check whether a MAC-Address is in given list
|
|
|
|
function islisted(what, mac)
|
2008-05-08 16:20:53 +00:00
|
|
|
mac = mac:lower()
|
2009-06-06 09:41:02 +00:00
|
|
|
|
|
|
|
uci:foreach("luci_splash", what,
|
2008-06-05 19:16:38 +00:00
|
|
|
function (section)
|
|
|
|
if section.mac:lower() == mac then
|
|
|
|
stat = true
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end)
|
2009-06-06 09:41:02 +00:00
|
|
|
|
2008-05-08 16:20:53 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Returns a list of MAC-Addresses for which a rule is existing
|
|
|
|
function listrules()
|
2009-06-06 09:41:02 +00:00
|
|
|
local macs = { }
|
|
|
|
for i, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
|
|
|
|
if r.options and #r.options >= 2 and r.options[1] == "MAC" then
|
|
|
|
macs[r.options[2]:lower()] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return luci.util.keys(macs)
|
2008-05-08 16:20:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Synchronise leases, remove abandoned rules
|
|
|
|
function sync()
|
|
|
|
local written = {}
|
|
|
|
local time = os.time()
|
|
|
|
|
|
|
|
-- Current leases in state files
|
2008-08-26 23:00:44 +00:00
|
|
|
local leases = uci:get_all("luci_splash")
|
2008-05-08 16:20:53 +00:00
|
|
|
|
|
|
|
-- Convert leasetime to seconds
|
2008-08-26 23:00:44 +00:00
|
|
|
local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime")) * 3600
|
2008-05-08 16:20:53 +00:00
|
|
|
|
|
|
|
-- Clean state file
|
2008-08-26 23:00:44 +00:00
|
|
|
uci:load("luci_splash")
|
|
|
|
uci:revert("luci_splash")
|
2008-05-08 16:20:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- For all leases
|
|
|
|
for k, v in pairs(leases) do
|
|
|
|
if v[".type"] == "lease" then
|
|
|
|
if os.difftime(time, tonumber(v.start)) > leasetime then
|
|
|
|
-- Remove expired
|
|
|
|
remove_rule(v.mac)
|
|
|
|
else
|
|
|
|
-- Rewrite state
|
2008-08-26 23:00:44 +00:00
|
|
|
uci:section("luci_splash", "lease", nil, {
|
2009-06-06 09:41:02 +00:00
|
|
|
mac = v.mac,
|
|
|
|
start = v.start,
|
|
|
|
kicked = v.kicked
|
2008-06-06 20:45:33 +00:00
|
|
|
})
|
2008-05-08 16:20:53 +00:00
|
|
|
written[v.mac:lower()] = 1
|
|
|
|
end
|
2009-06-06 09:41:02 +00:00
|
|
|
elseif v[".type"] == "whitelist" or v[".type"] == "blacklist" then
|
|
|
|
written[v.mac:lower()] = 1
|
2008-05-08 16:20:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Delete rules without state
|
|
|
|
for i, r in ipairs(listrules()) do
|
|
|
|
if #r > 0 and not written[r:lower()] then
|
|
|
|
remove_rule(r)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-26 23:00:44 +00:00
|
|
|
uci:save("luci_splash")
|
2008-05-08 16:20:53 +00:00
|
|
|
end
|
|
|
|
|
2009-06-06 09:41:02 +00:00
|
|
|
main(arg)
|