d679cc93edfae5c637a2fa598160ce32e7dda7d4
[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
22 module "luci.tools.ffwizard"
23
24 -- Deletes all references of a wifi device
25 function wifi_delete_ifaces(device)
26         local cursor = uci.cursor()
27         cursor:delete_all("wireless", "wifi-iface", {device=device})
28         cursor:save("wireless")
29 end
30
31 -- Deletes a network interface and all occurences of it in firewall zones and dhcp
32 function network_remove_interface(iface)
33         local cursor = uci.cursor()
34         
35         if not cursor:delete("network", iface) then
36                 return false
37         end
38
39         local aliases = {iface}
40         cursor:foreach("network", "alias", 
41                 function(section)
42                         table.insert(aliases, section[".name"])
43                 end)
44         
45         -- Delete Aliases and Routes
46         cursor:delete_all("network", nil, {interface=iface})
47         
48         -- Delete DHCP sections
49         cursor:delete_all("dhcp", "dhcp",
50                  function(section)
51                         return util.contains(aliases, section.interface)
52                  end)
53         
54         -- Remove OLSR sections
55         cursor:delete_all("olsr", "Interface", {Interface=iface})
56         
57         -- Remove Splash sections
58         cursor:delete_all("luci-splash", "iface", {network=iface})
59         
60         cursor:save("network")
61         cursor:save("olsr")
62         cursor:save("dhcp")
63         cursor:save("luci-splash")
64 end
65
66 -- Creates a firewall zone
67 function firewall_create_zone(zone, input, output, forward, masq)
68         local cursor = uci.cursor()
69         if not firewall_find_zone(zone) then
70                 local stat = cursor:section("firewall", "zone", nil, {
71                         input = input,
72                         output = output,
73                         forward = forward,
74                         masq = masq and "1",
75                         name = zone
76                 })
77                 cursor:save("firewall")
78                 return stat
79         end
80 end
81
82 -- Adds interface to zone, creates zone on-demand
83 function firewall_zone_add_interface(name, interface)
84         local cursor = uci.cursor()
85         local zone = firewall_find_zone(name)
86         local net = cursor:get("firewall", zone, "network")
87         cursor:set("firewall", zone, "network", (net or name .. " ") .. interface)
88         cursor:save("firewall")
89 end
90
91 -- Removes interface from zone
92 function firewall_zone_remove_interface(name, interface)
93         local cursor = uci.cursor()
94         local zone = firewall_find_zone(name)
95         if zone then
96                 local net = cursor:get("firewall", zone, "network")
97                 local new = remove_list_entry(net, interface)
98                 if new then
99                         if #new > 0 then
100                                 cursor:set("firewall", zone, "network", new)
101                         else
102                                 cursor:delete("firewall", zone, "network")
103                         end
104                         cursor:save("firewall")
105                 end
106         end
107 end
108
109
110 -- Finds the firewall zone with given name
111 function firewall_find_zone(name)
112         local find
113         
114         uci.cursor():foreach("firewall", "zone", 
115                 function (section)
116                         if section.name == name then
117                                 find = section[".name"]
118                         end
119                 end)
120                 
121         return find
122 end
123
124
125
126 -- Helpers --
127
128 -- Removes a listentry, handles real and pseduo lists transparently
129 function remove_list_entry(value, entry)
130         if type(value) == "nil" then
131                 return nil
132         end
133         
134         local result = type(value) == "table" and value or util.split(value, " ")
135         local key = util.contains(result, entry)
136         
137         while key do
138                 table.remove(result, key)
139                 key = util.contains(result, entry)
140         end
141         
142         result = type(value) == "table" and result or table.concat(result, " ")
143         return result ~= value and result 
144 end