Fixed a few FF-Wizard issues
[project/luci.git] / applications / luci-ffwizard-leipzig / luasrc / tools / ffwizard.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17 local uci = require "luci.model.uci"
18 local util = require "luci.util"
19 local table = require "table"
20
21 local type = type
22
23 module "luci.tools.ffwizard"
24
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")
30 end
31
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()
35         
36         if not cursor:delete("network", iface) then
37                 return false
38         end
39
40         local aliases = {iface}
41         cursor:foreach("network", "alias", 
42                 function(section)
43                         if section.interface == iface then
44                                 table.insert(aliases, section[".name"])
45                         end
46                 end)
47         
48         -- Delete Aliases and Routes
49         cursor:delete_all("network", "route", {interface=iface})
50         cursor:delete_all("network", "alias", {interface=iface})
51         
52         -- Delete DHCP sections
53         cursor:delete_all("dhcp", "dhcp",
54                  function(section)
55                         return util.contains(aliases, section.interface)
56                  end)
57         
58         -- Remove OLSR sections
59         cursor:delete_all("olsr", "Interface", {Interface=iface})
60         
61         -- Remove Splash sections
62         cursor:delete_all("luci-splash", "iface", {network=iface})
63         
64         cursor:save("network")
65         cursor:save("olsr")
66         cursor:save("dhcp")
67         cursor:save("luci-splash")
68 end
69
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, {
75                         input = input,
76                         output = output,
77                         forward = forward,
78                         masq = masq and "1",
79                         name = zone
80                 })
81                 cursor:save("firewall")
82                 return stat
83         end
84 end
85
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")
93 end
94
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)
99         if zone then
100                 local net = cursor:get("firewall", zone, "network")
101                 local new = remove_list_entry(net, interface)
102                 if new then
103                         if #new > 0 then
104                                 cursor:set("firewall", zone, "network", new)
105                         else
106                                 cursor:delete("firewall", zone, "network")
107                         end
108                         cursor:save("firewall")
109                 end
110         end
111 end
112
113
114 -- Finds the firewall zone with given name
115 function firewall_find_zone(name)
116         local find
117         
118         uci.cursor():foreach("firewall", "zone", 
119                 function (section)
120                         if section.name == name then
121                                 find = section[".name"]
122                         end
123                 end)
124                 
125         return find
126 end
127
128
129
130 -- Helpers --
131
132 -- Removes a listentry, handles real and pseduo lists transparently
133 function remove_list_entry(value, entry)
134         if type(value) == "nil" then
135                 return nil
136         end
137         
138         local result = type(value) == "table" and value or util.split(value, " ")
139         local key = util.contains(result, entry)
140         
141         while key do
142                 table.remove(result, key)
143                 key = util.contains(result, entry)
144         end
145         
146         result = type(value) == "table" and result or table.concat(result, " ")
147         return result ~= value and result 
148 end