FF-Wizard: assign matching network interface to wireless configuration
[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         local old = net or (cursor:get("network", name) and name)
92         cursor:set("firewall", zone, "network", (old and old .. " " or "") .. interface)
93         cursor:save("firewall")
94 end
95
96 -- Removes interface from zone
97 function firewall_zone_remove_interface(name, interface)
98         local cursor = uci.cursor()
99         local zone = firewall_find_zone(name)
100         if zone then
101                 local net = cursor:get("firewall", zone, "network")
102                 local new = remove_list_entry(net, interface)
103                 if new then
104                         if #new > 0 then
105                                 cursor:set("firewall", zone, "network", new)
106                         else
107                                 cursor:delete("firewall", zone, "network")
108                         end
109                         cursor:save("firewall")
110                 end
111         end
112 end
113
114
115 -- Finds the firewall zone with given name
116 function firewall_find_zone(name)
117         local find
118         
119         uci.cursor():foreach("firewall", "zone", 
120                 function (section)
121                         if section.name == name then
122                                 find = section[".name"]
123                         end
124                 end)
125                 
126         return find
127 end
128
129
130
131 -- Helpers --
132
133 -- Removes a listentry, handles real and pseduo lists transparently
134 function remove_list_entry(value, entry)
135         if type(value) == "nil" then
136                 return nil
137         end
138         
139         local result = type(value) == "table" and value or util.split(value, " ")
140         local key = util.contains(result, entry)
141         
142         while key do
143                 table.remove(result, key)
144                 key = util.contains(result, entry)
145         end
146         
147         result = type(value) == "table" and result or table.concat(result, " ")
148         return result ~= value and result 
149 end