local type, pairs, ipairs, table, luci, math
= type, pairs, ipairs, table, luci, math
-local lmo = require "lmo"
+local tpl = require "luci.template.parser"
local utl = require "luci.util"
local uci = require "luci.model.uci"
local uci_r, uci_s
-function _strlist(x)
- if x == nil then
- x = ""
- elseif type(x) == "table" then
- x = table.concat(x, " ")
- end
-
- return x:gmatch("%S+")
-end
-
function _valid_id(x)
return (x and #x > 0 and x:match("^[a-zA-Z0-9_]+$"))
end
if type(v) == "boolean" then v = v and "1" or "0" end
return uci_r:set(c, s, o, v)
else
- return uci_r:del(c, s, o, v)
+ return uci_r:delete(c, s, o)
end
end
uci_r:load(...)
end
+function get_defaults()
+ return defaults()
+end
+
+function new_zone(self)
+ local name = "newzone"
+ local count = 1
+
+ while self:get_zone(name) do
+ count = count + 1
+ name = "newzone%d" % count
+ end
+
+ return self:add_zone(name)
+end
+
function add_zone(self, n)
if _valid_id(n) and not self:get_zone(n) then
+ local d = defaults()
local z = uci_r:section("firewall", "zone", nil, {
name = n,
network = " ",
- input = defaults:input() or "DROP",
- forward = defaults:forward() or "DROP",
- output = defaults:output() or "DROP"
+ input = d:input() or "DROP",
+ forward = d:forward() or "DROP",
+ output = d:output() or "DROP"
})
return z and zone(z)
function(s)
if s.name and net then
local n
- for n in _strlist(s.network or s.name) do
+ for n in utl.imatch(s.network or s.name) do
if n == net then
z = s['.name']
return false
if uci_r:get("firewall", n) == "zone" then
local z = uci_r:get("firewall", n, "name")
- r = uci_r:delete("firwall", n)
+ r = uci_r:delete("firewall", n)
n = z
else
uci_r:foreach("firewall", "zone",
uci_r:foreach("firewall", "redirect",
function(s)
- if s.src == n then
+ if s.src == n or s.dest == n then
uci_r:delete("firewall", s['.name'])
end
end)
uci_r:foreach("firewall", "forwarding",
function(s)
- if s.src == n then
+ if s.src == n or s.dest == n then
uci_r:delete("firewall", s['.name'])
end
end)
if _valid_id(new) and not self:get_zone(new) then
uci_r:foreach("firewall", "zone",
function(s)
- if n and s.name == old then
+ if old and s.name == old then
+ if not s.network then
+ uci_r:set("firewall", s['.name'], "network", old)
+ end
uci_r:set("firewall", s['.name'], "name", new)
r = true
return false
end
end)
- ub.uci:foreach("firewall", "forwarding",
+ uci_r:foreach("firewall", "forwarding",
function(s)
if s.src == old then
- ub.uci:set("firewall", s['.name'], "src", new)
+ uci_r:set("firewall", s['.name'], "src", new)
end
if s.dest == old then
uci_r:set("firewall", s['.name'], "dest", new)
end
function zone.input(self)
- return self:get("input") or "DROP"
+ return self:get("input") or defaults():input() or "DROP"
end
function zone.forward(self)
- return self:get("forward") or "DROP"
+ return self:get("forward") or defaults():forward() or "DROP"
end
function zone.output(self)
- return self:get("output") or "DROP"
+ return self:get("output") or defaults():output() or "DROP"
end
function zone.add_network(self, net)
local nets = { }
local n
- for n in _strlist(self:get("network") or self:get("name")) do
+ for n in utl.imatch(self:get("network") or self:get("name")) do
if n ~= net then
nets[#nets+1] = n
end
nets[#nets+1] = net
- if #nets > 0 then
- self:set("network", table.concat(nets, " "))
- else
- self:set("network", " ")
- end
+ _M:del_network(net)
+ self:set("network", table.concat(nets, " "))
end
end
local nets = { }
local n
- for n in _strlist(self:get("network") or self:get("name")) do
+ for n in utl.imatch(self:get("network") or self:get("name")) do
if n ~= net then
nets[#nets+1] = n
end
local nets = { }
local n
- for n in _strlist(self:get("network") or self:get("name")) do
+ for n in utl.imatch(self:get("network") or self:get("name")) do
nets[#nets+1] = n
end
return nets
end
+function zone.clear_networks(self)
+ self:set("network", " ")
+end
+
function zone.get_forwardings_by(self, what)
local name = self:name()
local forwards = { }
end
end
- if not exist and dest ~= self:name() then
+ if not exist and dest ~= self:name() and _valid_id(dest) then
local s = uci_r:section("firewall", "forwarding", nil, {
src = self:name(),
dest = dest
end
end
- if not exist and src ~= self:name() then
+ if not exist and src ~= self:name() and _valid_id(src) then
local s = uci_r:section("firewall", "forwarding", nil, {
src = src,
dest = self:name()
function zone.del_forwardings_by(self, what)
local name = self:name()
- uci_r:foreach("firewall", "forwarding",
+ uci_r:delete_all("firewall", "forwarding",
function(s)
- if s.src and s.dest and s[what] == name then
- uci_r:delete("firewall", s['.name'])
- end
+ return (s.src and s.dest and s[what] == name)
end)
end
elseif self and self:name() == "wan" then
return "#f09090"
elseif self then
- math.randomseed(lmo.hash(self:name()))
+ math.randomseed(tpl.hash(self:name()))
local r = math.random(128)
local g = math.random(128)