cdace2b73e230e974dbd686d4b5a06f642088273
[project/luci.git] / applications / luci-ffwizard-leipzig / luasrc / model / cbi / 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
18 local uci = require "luci.model.uci".cursor()
19 local tools = require "luci.tools.ffwizard"
20
21
22 -------------------- View --------------------
23 f = SimpleForm("ffwizward", "Freifunkassistent",
24  "Dieser Assistent unterstüzt bei der Einrichtung des Routers für das Freifunknetz.")
25
26
27 dev = f:field(ListValue, "device", "WLAN-Gerät")
28 uci:foreach("wireless", "wifi-device",
29         function(section)
30                 dev:value(section[".name"])
31         end)
32
33
34 main = f:field(Flag, "wifi", "Freifunkzugang einrichten")
35
36 net = f:field(Value, "net", "Freifunknetz")
37 net.rmempty = true
38 net:depends("wifi", "1")
39 net:value("104.61", "Leipzig (104.61)")
40 net:value("104.62", "Halle (104.62)")
41 function net.cfgvalue(self, section)
42         return uci:get("freifunk", "wizard", "net")
43 end
44 function net.write(self, section, value)
45         uci:set("freifunk", "wizard", "net", value)
46         uci:save("freifunk")
47 end
48
49
50 subnet = f:field(ListValue, "subnet", "Subnetz (Projekt)")
51 subnet.rmempty = true
52 subnet:depends("wifi", "1")
53 for i=0, 255 do
54         subnet:value(i)
55 end 
56 function subnet.cfgvalue(self, section)
57         return uci:get("freifunk", "wizard", "subnet")
58 end
59 function subnet.write(self, section, value)
60         uci:set("freifunk", "wizard", "subnet", value)
61         uci:save("freifunk")
62 end
63
64 node = f:field(Value, "node", "Knoten")
65 node.rmempty = true
66 node:depends("wifi", "1")
67 for i=1, 51 do
68         node:value(i)
69 end
70 function node.cfgvalue(self, section)
71         return uci:get("freifunk", "wizard", "node")
72 end
73 function node.write(self, section, value)
74         uci:set("freifunk", "wizard", "node", value)
75         uci:save("freifunk")
76 end
77
78 client = f:field(Flag, "client", "WLAN-DHCP anbieten")
79 client:depends("wifi", "1")
80
81
82 olsr = f:field(Flag, "olsr", "OLSR einrichten")
83
84 share = f:field(ListValue, "sharenet", "Eigenen Internetzugang freigeben")
85 share:value("maybe", "-- keine Aktion --")
86 share:value("yes", "einschalten")
87 share:value("no", "ausschalten")
88
89
90
91 -------------------- Control --------------------
92 function f.handle(self, state, data)
93         if state == FORM_VALID then
94                 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
95                 return false
96         elseif state == FORM_INVALID then
97                 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
98         end
99         return true
100 end
101
102 local function _strip_internals(tbl)
103         tbl = tbl or {}
104         for k, v in pairs(tbl) do
105                 if k:sub(1, 1) == "." then
106                         tbl[k] = nil
107                 end
108         end
109         return tbl
110 end
111
112 -- Configure Freifunk checked
113 function main.write(self, section, value)
114         if value == "0" then
115                 return
116         end
117         
118         local device = dev:formvalue(section)
119
120         -- Collect IP-Address
121         local inet = net:formvalue(section)
122         local isubnet = subnet:formvalue(section)
123         local inode = node:formvalue(section)
124         
125         -- Invalidate fields
126         if not inet then
127                 net.tag_missing[section] = true
128         end
129         if not isubnet then
130                 subnet.tag_missing[section] = true
131         end
132         if not inode then
133                 node.tag_missing[section] = true
134         end
135         
136         if not inet or not isubnet or not inode then
137                 return
138         end
139         
140         local ip = "%s.%s.%s" % {inet, isubnet, inode}
141         
142         
143         -- Cleanup
144         luci.util.perror("1")
145         tools.wifi_delete_ifaces(device)
146         luci.util.perror("2")
147         tools.network_remove_interface(device)
148         luci.util.perror("3")
149         tools.firewall_zone_remove_interface("freifunk", device)
150                 
151         
152         -- Tune wifi device
153         local devconfig = _strip_internals(uci:get_all("freifunk", "wifi_device"))
154         uci:tset("wireless", device, devconfig)
155         
156         -- Create wifi iface
157         local ifconfig = _strip_internals(uci:get_all("freifunk", "wifi_iface"))
158         ifconfig.device = device
159         uci:section("wireless", "wifi-iface", nil, ifconfig)
160         
161         -- Save wifi
162         uci:save("wireless")    
163         
164         -- Create firewall zone and add default rules (first time)
165         local newzone = tools.firewall_create_zone("freifunk", "DROP", "ACCEPT", "DROP", true)
166         if newzone then
167                 uci:foreach("freifunk", "fw_forwarding", function(section)
168                         uci:section("firewall", "forwarding", nil, _strip_internals(section))
169                 end)
170                 
171                 uci:foreach("freifunk", "fw_rule", function(section)
172                         uci:section("firewall", "rule", nil, _strip_internals(section))
173                 end)
174                 
175                 uci:save("firewall")
176         end
177         
178         
179         -- Crate network interface
180         local netconfig = _strip_internals(uci:get_all("freifunk", "interface"))
181         netconfig.proto = "static"
182         netconfig.ipaddr = ip
183         uci:section("network", "interface", device, netconfig)
184         
185         uci:save("network")
186         
187         tools.firewall_zone_add_interface("freifunk", device)
188 end
189
190
191 function olsr.write(self, section, value)
192         if value == "0" then
193                 return
194         end
195         
196         local device = dev:formvalue(section)
197         
198         -- Delete old interface
199         uci:delete_all("freifunk", "Interface", {Interface=device})
200         
201         -- Write new interface
202         local olsrbase = _strip_internals(uci:get_all("freifunk", "olsr_interface"))
203         olsrbase.Interface = device
204         uci:section("olsr", "Interface", nil, olsrbase)
205         uci:save("olsr")
206 end
207
208
209 function share.write(self, section, value)
210         if value == "maybe" then
211                 return
212         end
213         
214         uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
215         
216         if value == "yes" then
217                 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
218         end
219         uci:save("firewall")
220 end
221
222
223 function client.write(self, section, value)
224         if value == "0" then
225                 return
226         end
227         
228         local device = dev:formvalue(section)
229
230         -- Collect IP-Address
231         local inet = net:formvalue(section)
232         local isubnet = subnet:formvalue(section)
233         local inode = node:formvalue(section)
234         
235         if not inet or not isubnet or not inode then
236                 return
237         end
238         
239         local dhcpbeg = 48 + tonumber(inode) * 4 
240         local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
241         local limit = dhcpbeg < 252 and 3 or 2
242         
243         -- Delete old alias
244         uci:delete("network", device .. "dhcp")
245         
246         -- Create alias
247         local aliasbase = _strip_internals(uci:get_all("freifunk", "alias"))
248         aliasbase.interface = device
249         aliasbase.ipaddr = dclient
250         aliasbase.proto = "static"
251         uci:section("network", "alias", device .. "dhcp", aliasbase)
252         uci:save("network")
253         
254         
255         -- Create dhcp
256         local dhcpbase = _strip_internals(uci:get_all("freifunk", "dhcp"))
257         dhcpbase.interface = device .. "dhcp"
258         dhcpbase.start = dhcpbeg
259         dhcpbase.limit = limit
260
261         uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
262         uci:save("dhcp")
263         
264         
265         -- Delete old splash
266         uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"})
267         
268         -- Register splash
269         uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"})
270         uci:save("luci_splash")
271 end
272
273 return f