2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 local uci = require "luci.model.uci"
18 local util = require "luci.util"
19 local table = require "table"
23 module "luci.tools.ffwizard"
25 -- Deletes all references of a wifi device
26 function wifi_delete_ifaces(device)
27 local cursor = uci.cursor()
28 cursor:delete_all("wireless", "wifi-iface", {device=device})
29 cursor:save("wireless")
32 -- Deletes a network interface and all occurences of it in firewall zones and dhcp
33 function network_remove_interface(iface)
34 local cursor = uci.cursor()
36 if not cursor:delete("network", iface) then
40 local aliases = {iface}
41 cursor:foreach("network", "alias",
43 if section.interface == iface then
44 table.insert(aliases, section[".name"])
48 -- Delete Aliases and Routes
49 cursor:delete_all("network", "route", {interface=iface})
50 cursor:delete_all("network", "alias", {interface=iface})
52 -- Delete DHCP sections
53 cursor:delete_all("dhcp", "dhcp",
55 return util.contains(aliases, section.interface)
58 -- Remove OLSR sections
59 cursor:delete_all("olsr", "Interface", {Interface=iface})
61 -- Remove Splash sections
62 cursor:delete_all("luci-splash", "iface", {network=iface})
64 cursor:save("network")
67 cursor:save("luci-splash")
70 -- Creates a firewall zone
71 function firewall_create_zone(zone, input, output, forward, masq)
72 local cursor = uci.cursor()
73 if not firewall_find_zone(zone) then
74 local stat = cursor:section("firewall", "zone", nil, {
81 cursor:save("firewall")
86 -- Adds interface to zone, creates zone on-demand
87 function firewall_zone_add_interface(name, interface)
88 local cursor = uci.cursor()
89 local zone = firewall_find_zone(name)
90 local net = cursor:get("firewall", zone, "network")
91 cursor:set("firewall", zone, "network", (net or name .. " ") .. interface)
92 cursor:save("firewall")
95 -- Removes interface from zone
96 function firewall_zone_remove_interface(name, interface)
97 local cursor = uci.cursor()
98 local zone = firewall_find_zone(name)
100 local net = cursor:get("firewall", zone, "network")
101 local new = remove_list_entry(net, interface)
104 cursor:set("firewall", zone, "network", new)
106 cursor:delete("firewall", zone, "network")
108 cursor:save("firewall")
114 -- Finds the firewall zone with given name
115 function firewall_find_zone(name)
118 uci.cursor():foreach("firewall", "zone",
120 if section.name == name then
121 find = section[".name"]
132 -- Removes a listentry, handles real and pseduo lists transparently
133 function remove_list_entry(value, entry)
134 if type(value) == "nil" then
138 local result = type(value) == "table" and value or util.split(value, " ")
139 local key = util.contains(result, entry)
142 table.remove(result, key)
143 key = util.contains(result, entry)
146 result = type(value) == "table" and result or table.concat(result, " ")
147 return result ~= value and result