libs/core: fix type in luci.model.wireless
[project/luci.git] / libs / core / luasrc / model / firewall.lua
index c1a8a45..a1daf5a 100644 (file)
@@ -37,8 +37,8 @@ function init(cursor)
        end
 end
 
        end
 end
 
-function add_zone(n)
-       if n then
+function add_zone(self, n)
+       if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not self:get_zone(n) then
                local z = ub.uci:section("firewall", "zone", nil, {
                        name    = n,
                        network = " ",
                local z = ub.uci:section("firewall", "zone", nil, {
                        name    = n,
                        network = " ",
@@ -51,7 +51,7 @@ function add_zone(n)
        end
 end
 
        end
 end
 
-function get_zone(n)
+function get_zone(self, n)
        local z
        ub.uci:foreach("firewall", "zone",
                function(s)
        local z
        ub.uci:foreach("firewall", "zone",
                function(s)
@@ -63,7 +63,7 @@ function get_zone(n)
        return z and zone(z)
 end
 
        return z and zone(z)
 end
 
-function get_zones()
+function get_zones(self)
        local zones = { }
        ub.uci:foreach("firewall", "zone",
                function(s)
        local zones = { }
        ub.uci:foreach("firewall", "zone",
                function(s)
@@ -74,24 +74,24 @@ function get_zones()
        return zones
 end
 
        return zones
 end
 
-function get_zones_by_network(net)
-       local zones = { }
+function get_zone_by_network(self, net)
+       local z
        ub.uci:foreach("firewall", "zone",
                function(s)
        ub.uci:foreach("firewall", "zone",
                function(s)
-                       if s.name then
+                       if s.name and net then
                                local n
                                for _, n in ipairs(ub:list(s.network or s.name)) do
                                        if n == net then
                                local n
                                for _, n in ipairs(ub:list(s.network or s.name)) do
                                        if n == net then
-                                               zones[#zones+1] = zone(s['.name'])
-                                               return true
+                                               z = s['.name']
+                                               return false
                                        end
                                end
                        end
                end)
                                        end
                                end
                        end
                end)
-       return zones
+       return z and zone(z)
 end
 
 end
 
-function del_zone(n)
+function del_zone(self, n)
        local r = false
        ub.uci:foreach("firewall", "zone",
                function(s)
        local r = false
        ub.uci:foreach("firewall", "zone",
                function(s)
@@ -123,10 +123,47 @@ function del_zone(n)
        return r
 end
 
        return r
 end
 
-function del_network(net)
+function rename_zone(self, old, new)
+       local r = false
+       if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_zone(new) then
+               ub.uci:foreach("firewall", "zone",
+                       function(s)
+                               if n and s.name == old then
+                                       ub.uci:set("firewall", s['.name'], "name", new)
+                                       r = true
+                                       return false
+                               end
+                       end)
+               if r then
+                       ub.uci:foreach("firewall", "rule",
+                               function(s)
+                                       if s.src == old then
+                                               ub.uci:set("firewall", s['.name'], "src", new)
+                                       elseif s.dest == old then
+                                               ub.uci:set("firewall", s['.name'], "dest", new)
+                                       end
+                               end)
+                       ub.uci:foreach("firewall", "redirect",
+                               function(s)
+                                       if s.src == old then
+                                               ub.uci:set("firewall", s['.name'], "src", new)
+                                       end
+                               end)
+                       ub.uci:foreach("firewall", "forwarding",
+                               function(s)
+                                       if s.src == old then
+                                               ub.uci:set("firewall", s['.name'], "src", new)
+                                       end
+                               end)
+               end
+       end
+       return r
+end
+
+function del_network(self, net)
        local z
        if net then
        local z
        if net then
-               for _, z in ipairs(get_zones()) do
+               for _, z in ipairs(self:get_zones()) do
                        z:del_network(net)
                end
        end
                        z:del_network(net)
                end
        end
@@ -197,7 +234,7 @@ function zone.add_forwarding_to(self, dest, with_mtu_fix)
                local s = ub.uci:section("firewall", "forwarding", nil, {
                        src     = self:name(),
                        dest    = dest,
                local s = ub.uci:section("firewall", "forwarding", nil, {
                        src     = self:name(),
                        dest    = dest,
-                       mtu_fix = with_mtu_fix and true or false
+                       mtu_fix = with_mtu_fix and "1" or "0"
                })
                return s and forwarding(s)
        end
                })
                return s and forwarding(s)
        end
@@ -215,12 +252,22 @@ function zone.add_forwarding_from(self, src, with_mtu_fix)
                local s = ub.uci:section("firewall", "forwarding", nil, {
                        src     = src,
                        dest    = self:name(),
                local s = ub.uci:section("firewall", "forwarding", nil, {
                        src     = src,
                        dest    = self:name(),
-                       mtu_fix = with_mtu_fix and true or false
+                       mtu_fix = with_mtu_fix and "1" or "0"
                })
                return s and forwarding(s)
        end
 end
 
                })
                return s and forwarding(s)
        end
 end
 
+function zone.del_forwardings_by(self, what)
+       local name = self:name()
+       ub.uci:foreach("firewall", "forwarding",
+               function(s)
+                       if s.src and s.dest and s[what] == name then
+                               ub.uci:delete("firewall", s['.name'])
+                       end
+               end)
+end
+
 function zone.add_redirect(self, options)
        options = options or { }
        options.src = self:name()
 function zone.add_redirect(self, options)
        options = options or { }
        options.src = self:name()