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