luci-0.8: merge r4729
[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 local util = require "luci.util"
21 local sys = require "luci.sys"
22
23
24 -------------------- View --------------------
25 f = SimpleForm("ffwizward", "Freifunkassistent",
26  "Dieser Assistent unterstüzt bei der Einrichtung des Routers für das Freifunknetz.")
27
28
29 dev = f:field(ListValue, "device", "WLAN-Gerät")
30 uci:foreach("wireless", "wifi-device",
31         function(section)
32                 dev:value(section[".name"])
33         end)
34
35
36 main = f:field(Flag, "wifi", "Freifunkzugang einrichten")
37
38 net = f:field(Value, "net", "Freifunknetz", "1. Teil der IP-Adresse")
39 net.rmempty = true
40 net:depends("wifi", "1")
41 uci:foreach("freifunk", "community", function(s)
42         net:value(s[".name"], "%s (%s)" % {s.name, s.prefix})
43 end)
44
45 function net.cfgvalue(self, section)
46         return uci:get("freifunk", "wizard", "net")
47 end
48 function net.write(self, section, value)
49         uci:set("freifunk", "wizard", "net", value)
50         uci:save("freifunk")
51 end
52
53
54 subnet = f:field(Value, "subnet", "Subnetz (Projekt)", "2. Teil der IP-Adresse")
55 subnet.rmempty = true
56 subnet:depends("wifi", "1")
57 function subnet.cfgvalue(self, section)
58         return uci:get("freifunk", "wizard", "subnet")
59 end
60 function subnet.write(self, section, value)
61         uci:set("freifunk", "wizard", "subnet", value)
62         uci:save("freifunk")
63 end
64
65 node = f:field(Value, "node", "Knoten", "3. Teil der IP-Adresse")
66 node.rmempty = true
67 node:depends("wifi", "1")
68 for i=1, 51 do
69         node:value(i)
70 end
71 function node.cfgvalue(self, section)
72         return uci:get("freifunk", "wizard", "node")
73 end
74 function node.write(self, section, value)
75         uci:set("freifunk", "wizard", "node", value)
76         uci:save("freifunk")
77 end
78
79 client = f:field(Flag, "client", "WLAN-DHCP anbieten")
80 client:depends("wifi", "1")
81 client.rmempty = true
82
83 olsr = f:field(Flag, "olsr", "OLSR einrichten")
84 olsr.rmempty = true
85
86 lat = f:field(Value, "lat", "Latitude")
87 lat:depends("olsr", "1")
88
89 lon = f:field(Value, "lon", "Longitude")
90 lon:depends("olsr", "1")
91
92 share = f:field(Flag, "sharenet", "Eigenen Internetzugang freigeben")
93 share.rmempty = true
94
95
96
97 -------------------- Control --------------------
98 function f.handle(self, state, data)
99         if state == FORM_VALID then
100                 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
101                 return false
102         elseif state == FORM_INVALID then
103                 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
104         end
105         return true
106 end
107
108 local function _strip_internals(tbl)
109         tbl = tbl or {}
110         for k, v in pairs(tbl) do
111                 if k:sub(1, 1) == "." then
112                         tbl[k] = nil
113                 end
114         end
115         return tbl
116 end
117
118 -- Configure Freifunk checked
119 function main.write(self, section, value)
120         if value == "0" then
121                 return
122         end
123
124         local device = dev:formvalue(section)
125         local community, external
126
127         -- Collect IP-Address
128         local inet = net:formvalue(section)
129         local isubnet = subnet:formvalue(section)
130         local inode = node:formvalue(section)
131
132         -- Invalidate fields
133         if not inet then
134                 net.tag_missing[section] = true
135         else
136                 community = inet
137                 external  = uci:get("freifunk", community, "external") or ""
138                 inet = uci:get("freifunk", community, "prefix") or inet
139         end
140         if not isubnet then
141                 subnet.tag_missing[section] = true
142         end
143         if not inode then
144                 node.tag_missing[section] = true
145         end
146
147         if not inet or not isubnet or not inode then
148                 return
149         end
150
151         local ip = "%s.%s.%s" % {inet, isubnet, inode}
152
153
154         -- Cleanup
155         tools.wifi_delete_ifaces(device)
156         tools.network_remove_interface(device)
157         tools.firewall_zone_remove_interface("freifunk", device)
158
159         -- Tune community settings
160         if community and uci:get("freifunk", community) then
161                 uci:tset("freifunk", "community", uci:get_all("freifunk", community))
162         end
163
164         -- Tune wifi device
165         local devconfig = uci:get_all("freifunk", "wifi_device")
166         util.update(devconfig, uci:get_all(external, "wifi_device") or {})
167         uci:tset("wireless", device, devconfig)
168
169         -- Create wifi iface
170         local ifconfig = uci:get_all("freifunk", "wifi_iface")
171         util.update(ifconfig, uci:get_all(external, "wifi_iface") or {})
172         ifconfig.device = device
173         ifconfig.network = device
174         ifconfig.ssid = uci:get("freifunk", community, "ssid")
175         uci:section("wireless", "wifi-iface", nil, ifconfig)
176
177         -- Save wifi
178         uci:save("wireless")
179
180         -- Create firewall zone and add default rules (first time)
181         local newzone = tools.firewall_create_zone("freifunk", "REJECT", "ACCEPT", "REJECT", true)
182         if newzone then
183                 uci:foreach("freifunk", "fw_forwarding", function(section)
184                         uci:section("firewall", "forwarding", nil, section)
185                 end)
186                 uci:foreach(external, "fw_forwarding", function(section)
187                         uci:section("firewall", "forwarding", nil, section)
188                 end)
189
190                 uci:foreach("freifunk", "fw_rule", function(section)
191                         uci:section("firewall", "rule", nil, section)
192                 end)
193                 uci:foreach(external, "fw_rule", function(section)
194                         uci:section("firewall", "rule", nil, section)
195                 end)
196         end
197
198         -- Enforce firewall include
199         local has_include = false
200         uci:foreach("firewall", "include",
201                 function(section)
202                         if section.path == "/etc/firewall.freifunk" then
203                                 has_include = true
204                         end
205                 end)
206
207         if not has_include then
208                 uci:section("firewall", "include", nil,
209                         { path = "/etc/firewall.freifunk" })
210         end
211
212         -- Allow state: invalid packets
213         uci:foreach("firewall", "defaults",
214                 function(section)
215                         uci:set("firewall", section[".name"], "drop_invalid", "0")
216                 end)
217
218         -- Prepare advanced config
219         local has_advanced = false
220         uci:foreach("firewall", "advanced",
221                 function(section) has_advanced = true end)
222
223         if not has_advanced then
224                 uci:section("firewall", "advanced", nil,
225                         { tcp_ecn = "0", ip_conntrack_max = "8192", tcp_westwood = "1" })
226         end
227
228         uci:save("firewall")
229
230
231         -- Create network interface
232         local netconfig = uci:get_all("freifunk", "interface")
233         util.update(netconfig, uci:get_all(external, "interface") or {})
234         netconfig.proto = "static"
235         netconfig.ipaddr = ip
236         uci:section("network", "interface", device, netconfig)
237
238         uci:save("network")
239
240         tools.firewall_zone_add_interface("freifunk", device)
241
242
243         local new_hostname = ip:gsub("%.", "-")
244         local old_hostname = sys.hostname()
245
246         uci:foreach("system", "system",
247                 function(s)
248                         -- Make crond silent
249                         uci:set("system", s['.name'], "cronloglevel", "10")
250
251                         -- Set hostname
252                         if old_hostname == "OpenWrt" or old_hostname:match("^%d+-%d+-%d+-%d+$") then
253                                 uci:set("system", s['.name'], "hostname", new_hostname)
254                                 sys.hostname(new_hostname)
255                         end
256                 end)
257
258         uci:save("system")
259 end
260
261
262 function olsr.write(self, section, value)
263         if value == "0" then
264                 return
265         end
266
267
268         local device = dev:formvalue(section)
269
270         local community = net:formvalue(section)
271         local external  = community and uci:get("freifunk", community, "external") or ""
272
273         local latval = tonumber(lat:formvalue(section))
274         local lonval = tonumber(lon:formvalue(section))
275
276
277         -- Delete old interface
278         uci:delete_all("olsrd", "Interface", {interface=device})
279
280         -- Write new interface
281         local olsrbase = uci:get_all("freifunk", "olsr_interface")
282         util.update(olsrbase, uci:get_all(external, "olsr_interface") or {})
283         olsrbase.interface = device
284         olsrbase.ignore    = "0"
285         uci:section("olsrd", "Interface", nil, olsrbase)
286
287         -- Delete old watchdog settings
288         uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_watchdog.so.0.1"})
289
290         -- Write new watchdog settings
291         uci:section("olsrd", "LoadPlugin", nil, {
292                 library  = "olsrd_watchdog.so.0.1",
293                 file     = "/var/run/olsrd.watchdog",
294                 interval = "30"
295         })
296
297         -- Delete old nameservice settings
298         uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_nameservice.so.0.3"})
299
300         -- Write new nameservice settings
301         uci:section("olsrd", "LoadPlugin", nil, {
302                 library     = "olsrd_nameservice.so.0.3",
303                 suffix      = ".olsr",
304                 hosts_file  = "/var/etc/hosts.olsr",
305                 latlon_file = "/var/run/latlon.js",
306                 lat         = latval and string.format("%.15f", latval) or "",
307                 lon         = lonval and string.format("%.15f", lonval) or ""
308         })
309
310         -- Save latlon to system too
311         if latval and lonval then
312                 uci:foreach("system", "system", function(s)
313                         uci:set("system", s[".name"], "latlon",
314                                 string.format("%.15f %.15f", latval, lonval))
315                 end)
316         else
317                 uci:foreach("system", "system", function(s)
318                         uci:delete("system", s[".name"], "latlon")
319                 end)
320         end
321
322         -- Import hosts
323         uci:foreach("dhcp", "dnsmasq", function(s)
324                 uci:set("dhcp", s[".name"], "addnhosts", "/var/etc/hosts.olsr")
325         end)
326
327         -- Make sure that OLSR is enabled
328         sys.exec("/etc/init.d/olsrd enable")
329
330         uci:save("olsrd")
331         uci:save("dhcp")
332 end
333
334
335 function share.write(self, section, value)
336         uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
337         uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_dyn_gw_plain.so.0.4"})
338
339         if value == "1" then
340                 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
341                 uci:section("olsrd", "LoadPlugin", nil, {library="olsrd_dyn_gw_plain.so.0.4"})
342         end
343         uci:save("firewall")
344         uci:save("olsrd")
345         uci:save("system")
346 end
347
348
349 function client.write(self, section, value)
350         if value == "0" then
351                 return
352         end
353
354         local device = dev:formvalue(section)
355
356         -- Collect IP-Address
357         local inet = net:formvalue(section)
358         local isubnet = subnet:formvalue(section)
359         local inode = node:formvalue(section)
360
361         if not inet or not isubnet or not inode then
362                 return
363         end
364         local community = inet
365         local external  = community and uci:get("freifunk", community, "external") or ""
366         inet = uci:get("freifunk", community, "prefix") or inet
367
368         local dhcpbeg = 48 + tonumber(inode) * 4
369         local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
370         local limit = dhcpbeg < 252 and 3 or 2
371
372         -- Delete old alias
373         uci:delete("network", device .. "dhcp")
374
375         -- Create alias
376         local aliasbase = uci:get_all("freifunk", "alias")
377         util.update(aliasbase, uci:get_all(external, "alias") or {})
378         aliasbase.interface = device
379         aliasbase.ipaddr = dclient
380         aliasbase.proto = "static"
381         uci:section("network", "alias", device .. "dhcp", aliasbase)
382         uci:save("network")
383
384
385         -- Create dhcp
386         local dhcpbase = uci:get_all("freifunk", "dhcp")
387         util.update(dhcpbase, uci:get_all(external, "dhcp") or {})
388         dhcpbase.interface = device .. "dhcp"
389         dhcpbase.start = dhcpbeg
390         dhcpbase.limit = limit
391         dhcpbase.force = 1
392
393         uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
394         uci:save("dhcp")
395
396         uci:delete_all("firewall", "rule", {
397                 src="freifunk",
398                 proto="udp",
399                 dest_port="53"
400         })
401         uci:section("firewall", "rule", nil, {
402                 src="freifunk",
403                 proto="udp",
404                 dest_port="53",
405                 target="ACCEPT"
406         })
407         uci:delete_all("firewall", "rule", {
408                 src="freifunk",
409                 proto="udp",
410                 src_port="68",
411                 dest_port="67"
412         })
413         uci:section("firewall", "rule", nil, {
414                 src="freifunk",
415                 proto="udp",
416                 src_port="68",
417                 dest_port="67",
418                 target="ACCEPT"
419         })
420         uci:delete_all("firewall", "rule", {
421                 src="freifunk",
422                 proto="tcp",
423                 dest_port="8082",
424         })
425         uci:section("firewall", "rule", nil, {
426                 src="freifunk",
427                 proto="tcp",
428                 dest_port="8082",
429                 target="ACCEPT"
430         })
431
432         uci:save("firewall")
433
434         -- Delete old splash
435         uci:delete_all("luci_splash", "iface", {network=device.."dhcp", zone="freifunk"})
436
437         -- Register splash
438         uci:section("luci_splash", "iface", nil, {network=device.."dhcp", zone="freifunk"})
439         uci:save("luci_splash")
440         
441         -- Make sure that luci_splash is enabled
442         sys.exec("/etc/init.d/luci_splash enable")
443 end
444
445 return f