applications/luci-ffwizard-leipzig: add missing uci save for firewall, save is not...
[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
84 olsr = f:field(Flag, "olsr", "OLSR einrichten")
85 olsr.rmempty = true
86
87 share = f:field(Flag, "sharenet", "Eigenen Internetzugang freigeben")
88 share.rmempty = true
89
90
91
92 -------------------- Control --------------------
93 function f.handle(self, state, data)
94         if state == FORM_VALID then
95                 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
96                 return false
97         elseif state == FORM_INVALID then
98                 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
99         end
100         return true
101 end
102
103 local function _strip_internals(tbl)
104         tbl = tbl or {}
105         for k, v in pairs(tbl) do
106                 if k:sub(1, 1) == "." then
107                         tbl[k] = nil
108                 end
109         end
110         return tbl
111 end
112
113 -- Configure Freifunk checked
114 function main.write(self, section, value)
115         if value == "0" then
116                 return
117         end
118
119         local device = dev:formvalue(section)
120         local community, external
121
122         -- Collect IP-Address
123         local inet = net:formvalue(section)
124         local isubnet = subnet:formvalue(section)
125         local inode = node:formvalue(section)
126
127         -- Invalidate fields
128         if not inet then
129                 net.tag_missing[section] = true
130         else
131                 community = inet
132                 external  = uci:get("freifunk", community, "external") or ""
133                 inet = uci:get("freifunk", community, "prefix") or inet
134         end
135         if not isubnet then
136                 subnet.tag_missing[section] = true
137         end
138         if not inode then
139                 node.tag_missing[section] = true
140         end
141
142         if not inet or not isubnet or not inode then
143                 return
144         end
145
146         local ip = "%s.%s.%s" % {inet, isubnet, inode}
147
148
149         -- Cleanup
150         tools.wifi_delete_ifaces(device)
151         tools.network_remove_interface(device)
152         tools.firewall_zone_remove_interface("freifunk", device)
153
154         -- Tune community settings
155         if community and uci:get("freifunk", community) then
156                 uci:tset("freifunk", "community", uci:get_all("freifunk", community))
157         end
158
159         -- Tune wifi device
160         local devconfig = uci:get_all("freifunk", "wifi_device")
161         util.update(devconfig, uci:get_all(external, "wifi_device") or {})
162         uci:tset("wireless", device, devconfig)
163
164         -- Create wifi iface
165         local ifconfig = uci:get_all("freifunk", "wifi_iface")
166         util.update(ifconfig, uci:get_all(external, "wifi_iface") or {})
167         ifconfig.device = device
168         ifconfig.network = device
169         ifconfig.ssid = uci:get("freifunk", community, "ssid")
170         uci:section("wireless", "wifi-iface", nil, ifconfig)
171
172         -- Save wifi
173         uci:save("wireless")
174
175         -- Create firewall zone and add default rules (first time)
176         local newzone = tools.firewall_create_zone("freifunk", "REJECT", "ACCEPT", "REJECT", true)
177         if newzone then
178                 uci:foreach("freifunk", "fw_forwarding", function(section)
179                         uci:section("firewall", "forwarding", nil, section)
180                 end)
181                 uci:foreach(external, "fw_forwarding", function(section)
182                         uci:section("firewall", "forwarding", nil, section)
183                 end)
184
185                 uci:foreach("freifunk", "fw_rule", function(section)
186                         uci:section("firewall", "rule", nil, section)
187                 end)
188                 uci:foreach(external, "fw_rule", function(section)
189                         uci:section("firewall", "rule", nil, section)
190                 end)
191         end
192
193         -- Enforce firewall include
194         local has_include = false
195         uci:foreach("firewall", "include",
196                 function(section)
197                         if section.path == "/etc/firewall.freifunk" then
198                                 has_include = true
199                         end
200                 end)
201
202         if not has_include then
203                 uci:section("firewall", "include", nil,
204                         { path = "/etc/firewall.freifunk" })
205         end
206
207         -- Allow state: invalid packets
208         uci:foreach("firewall", "defaults",
209                 function(section)
210                         uci:set("firewall", section[".name"], "drop_invalid", "0")
211                 end)
212
213         -- Prepare advanced config
214         local has_advanced = false
215         uci:foreach("firewall", "advanced",
216                 function(section) has_advanced = true end)
217
218         if not has_advanced then
219                 uci:section("firewall", "advanced", nil,
220                         { tcp_ecn = "0", ip_conntrack_max = "8192", tcp_westwood = "1" })
221         end
222
223         uci:save("firewall")
224
225
226         -- Create network interface
227         local netconfig = uci:get_all("freifunk", "interface")
228         util.update(netconfig, uci:get_all(external, "interface") or {})
229         netconfig.proto = "static"
230         netconfig.ipaddr = ip
231         uci:section("network", "interface", device, netconfig)
232
233         uci:save("network")
234
235         tools.firewall_zone_add_interface("freifunk", device)
236
237
238         -- Set hostname
239         local new_hostname = ip:gsub("%.", "-")
240         local old_hostname = sys.hostname()
241
242         if old_hostname == "OpenWrt" or old_hostname:match("^%d+-%d+-%d+-%d+$") then
243                 uci:foreach("system", "system",
244                         function(s)
245                                 uci:set("system", s['.name'], "hostname", new_hostname)
246                         end)
247
248                 sys.hostname(new_hostname)
249                 uci:save("system")
250         end
251 end
252
253
254 function olsr.write(self, section, value)
255         if value == "0" then
256                 return
257         end
258
259
260         local device = dev:formvalue(section)
261
262         local community = net:formvalue(section)
263         local external  = community and uci:get("freifunk", community, "external") or ""
264
265         -- Delete old interface
266         uci:delete_all("olsrd", "Interface", {interface=device})
267
268         -- Write new interface
269         local olsrbase = uci:get_all("freifunk", "olsr_interface")
270         util.update(olsrbase, uci:get_all(external, "olsr_interface") or {})
271         olsrbase.interface = device
272         olsrbase.ignore    = "0"
273         uci:section("olsrd", "Interface", nil, olsrbase)
274
275         -- Delete old watchdog settings
276         uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_watchdog.so.0.1"})
277
278         -- Write new watchdog settings
279         uci:section("olsrd", "LoadPlugin", nil, {
280                 library  = "olsrd_watchdog.so.0.1",
281                 file     = "/var/run/olsrd.watchdog",
282                 interval = "30"
283         })
284
285         -- Import hosts
286         uci:foreach("dhcp", "dnsmasq", function(s)
287                 uci:set("dhcp", s[".name"], "addnhosts", "/var/etc/hosts.olsr")
288         end)
289
290         uci:save("olsrd")
291         uci:save("dhcp")
292 end
293
294
295 function share.write(self, section, value)
296         uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
297         uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_dyn_gw_plain.so.0.4"})
298
299         if value == "1" then
300                 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
301                 uci:section("olsrd", "LoadPlugin", nil, {library="olsrd_dyn_gw_plain.so.0.4"})
302         end
303         uci:save("firewall")
304         uci:save("olsrd")
305 end
306
307
308 function client.write(self, section, value)
309         if value == "0" then
310                 return
311         end
312
313         local device = dev:formvalue(section)
314
315         -- Collect IP-Address
316         local inet = net:formvalue(section)
317         local isubnet = subnet:formvalue(section)
318         local inode = node:formvalue(section)
319
320         if not inet or not isubnet or not inode then
321                 return
322         end
323         local community = inet
324         local external  = community and uci:get("freifunk", community, "external") or ""
325         inet = uci:get("freifunk", community, "prefix") or inet
326
327         local dhcpbeg = 48 + tonumber(inode) * 4
328         local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
329         local limit = dhcpbeg < 252 and 3 or 2
330
331         -- Delete old alias
332         uci:delete("network", device .. "dhcp")
333
334         -- Create alias
335         local aliasbase = uci:get_all("freifunk", "alias")
336         util.update(aliasbase, uci:get_all(external, "alias") or {})
337         aliasbase.interface = device
338         aliasbase.ipaddr = dclient
339         aliasbase.proto = "static"
340         uci:section("network", "alias", device .. "dhcp", aliasbase)
341         uci:save("network")
342
343
344         -- Create dhcp
345         local dhcpbase = uci:get_all("freifunk", "dhcp")
346         util.update(dhcpbase, uci:get_all(external, "dhcp") or {})
347         dhcpbase.interface = device .. "dhcp"
348         dhcpbase.start = dhcpbeg
349         dhcpbase.limit = limit
350         dhcpbase.force = 1
351
352         uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
353         uci:save("dhcp")
354
355         uci:delete_all("firewall", "rule", {
356                 src="freifunk",
357                 proto="udp",
358                 dest_port="53"
359         })
360         uci:section("firewall", "rule", nil, {
361                 src="freifunk",
362                 proto="udp",
363                 dest_port="53",
364                 target="ACCEPT"
365         })
366         uci:delete_all("firewall", "rule", {
367                 src="freifunk",
368                 proto="udp",
369                 src_port="68",
370                 dest_port="67"
371         })
372         uci:section("firewall", "rule", nil, {
373                 src="freifunk",
374                 proto="udp",
375                 src_port="68",
376                 dest_port="67",
377                 target="ACCEPT"
378         })
379         uci:delete_all("firewall", "rule", {
380                 src="freifunk",
381                 proto="tcp",
382                 dest_port="8082",
383         })
384         uci:section("firewall", "rule", nil, {
385                 src="freifunk",
386                 proto="tcp",
387                 dest_port="8082",
388                 target="ACCEPT"
389         })
390
391         uci:save("firewall")
392
393         -- Delete old splash
394         uci:delete_all("luci_splash", "iface", {network=device.."dhcp", zone="freifunk"})
395
396         -- Register splash
397         uci:section("luci_splash", "iface", nil, {network=device.."dhcp", zone="freifunk"})
398         uci:save("luci_splash")
399 end
400
401 return f