modules/freifunk: fix cronjob installation
[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
22
23 -------------------- View --------------------
24 f = SimpleForm("ffwizward", "Freifunkassistent",
25  "Dieser Assistent unterstüzt bei der Einrichtung des Routers für das Freifunknetz.")
26
27
28 dev = f:field(ListValue, "device", "WLAN-Gerät")
29 uci:foreach("wireless", "wifi-device",
30         function(section)
31                 dev:value(section[".name"])
32         end)
33
34
35 main = f:field(Flag, "wifi", "Freifunkzugang einrichten")
36
37 net = f:field(Value, "net", "Freifunknetz", "1. Teil der IP-Adresse")
38 net.rmempty = true
39 net:depends("wifi", "1")
40 uci:foreach("freifunk", "community", function(s)
41         net:value(s[".name"], "%s (%s)" % {s.name, s.prefix})
42 end)
43
44 function net.cfgvalue(self, section)
45         return uci:get("freifunk", "wizard", "net")
46 end
47 function net.write(self, section, value)
48         uci:set("freifunk", "wizard", "net", value)
49         uci:save("freifunk")
50 end
51
52
53 subnet = f:field(Value, "subnet", "Subnetz (Projekt)", "2. Teil der IP-Adresse")
54 subnet.rmempty = true
55 subnet:depends("wifi", "1")
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", "3. Teil der IP-Adresse")
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 client.rmempty = true
81
82
83 olsr = f:field(Flag, "olsr", "OLSR einrichten")
84 olsr.rmempty = true
85
86 share = f:field(Flag, "sharenet", "Eigenen Internetzugang freigeben")
87 share.rmempty = true
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         local community, external
120
121         -- Collect IP-Address
122         local inet = net:formvalue(section)
123         local isubnet = subnet:formvalue(section)
124         local inode = node:formvalue(section)
125
126         -- Invalidate fields
127         if not inet then
128                 net.tag_missing[section] = true
129         else
130                 community = inet
131                 external  = uci:get("freifunk", community, "external") or ""
132                 inet = uci:get("freifunk", community, "prefix") or inet
133         end
134         if not isubnet then
135                 subnet.tag_missing[section] = true
136         end
137         if not inode then
138                 node.tag_missing[section] = true
139         end
140
141         if not inet or not isubnet or not inode then
142                 return
143         end
144
145         local ip = "%s.%s.%s" % {inet, isubnet, inode}
146
147
148         -- Cleanup
149         tools.wifi_delete_ifaces(device)
150         tools.network_remove_interface(device)
151         tools.firewall_zone_remove_interface("freifunk", device)
152
153         -- Tune community settings
154         if community and uci:get("freifunk", community) then
155                 uci:tset("freifunk", "community", uci:get_all("freifunk", community))
156         end
157
158         -- Tune wifi device
159         local devconfig = uci:get_all("freifunk", "wifi_device")
160         util.update(devconfig, uci:get_all(external, "wifi_device") or {})
161         uci:tset("wireless", device, devconfig)
162
163         -- Create wifi iface
164         local ifconfig = uci:get_all("freifunk", "wifi_iface")
165         util.update(ifconfig, uci:get_all(external, "wifi_iface") or {})
166         ifconfig.device = device
167         ifconfig.network = device
168         ifconfig.ssid = uci:get("freifunk", community, "ssid")
169         uci:section("wireless", "wifi-iface", nil, ifconfig)
170
171         -- Save wifi
172         uci:save("wireless")
173
174         -- Create firewall zone and add default rules (first time)
175         local newzone = tools.firewall_create_zone("freifunk", "REJECT", "ACCEPT", "REJECT", true)
176         if newzone then
177                 uci:foreach("freifunk", "fw_forwarding", function(section)
178                         uci:section("firewall", "forwarding", nil, section)
179                 end)
180                 uci:foreach(external, "fw_forwarding", function(section)
181                         uci:section("firewall", "forwarding", nil, section)
182                 end)
183
184                 uci:foreach("freifunk", "fw_rule", function(section)
185                         uci:section("firewall", "rule", nil, section)
186                 end)
187                 uci:foreach(external, "fw_rule", function(section)
188                         uci:section("firewall", "rule", nil, section)
189                 end)
190         end
191
192         -- Enforce firewall include
193         local has_include = false
194         uci:foreach("firewall", "include",
195                 function(section)
196                         if section.path == "/etc/firewall.freifunk" then
197                                 has_include = true
198                         end
199                 end)
200
201         if not has_include then
202                 uci:section("firewall", "include", nil,
203                         { path = "/etc/firewall.freifunk" })
204         end
205
206         -- Allow state: invalid packets
207         uci:foreach("firewall", "defaults",
208                 function(section)
209                         uci:set("firewall", section[".name"], "drop_invalid", "0")
210                 end)
211
212         -- Prepare advanced config
213         local has_advanced = false
214         uci:foreach("firewall", "advanced",
215                 function(section) has_advanced = true end)
216
217         if not has_advanced then
218                 uci:section("firewall", "advanced", nil,
219                         { tcp_ecn = "0" })
220         end
221
222         uci:save("firewall")
223
224
225         -- Crate network interface
226         local netconfig = uci:get_all("freifunk", "interface")
227         util.update(netconfig, uci:get_all(external, "interface") or {})
228         netconfig.proto = "static"
229         netconfig.ipaddr = ip
230         uci:section("network", "interface", device, netconfig)
231
232         uci:save("network")
233
234         tools.firewall_zone_add_interface("freifunk", device)
235 end
236
237
238 function olsr.write(self, section, value)
239         if value == "0" then
240                 return
241         end
242
243
244         local device = dev:formvalue(section)
245
246         local community = net:formvalue(section)
247         local external  = community and uci:get("freifunk", community, "external") or ""
248
249         -- Delete old interface
250         uci:delete_all("olsrd", "Interface", {interface=device})
251
252         -- Write new interface
253         local olsrbase = uci:get_all("freifunk", "olsr_interface")
254         util.update(olsrbase, uci:get_all(external, "olsr_interface") or {})
255         olsrbase.interface = device
256         olsrbase.ignore    = "0"
257         uci:section("olsrd", "Interface", nil, olsrbase)
258         uci:save("olsrd")
259
260         -- Import hosts
261         uci:foreach("dhcp", "dnsmasq", function(s)
262                 uci:set("dhcp", s[".name"], "addnhosts", "/var/etc/hosts.olsr")
263         end)
264         uci:save("dhcp")
265 end
266
267
268 function share.write(self, section, value)
269         uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
270
271         if value == "1" then
272                 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
273         end
274         uci:save("firewall")
275 end
276
277
278 function client.write(self, section, value)
279         if value == "0" then
280                 return
281         end
282
283         local device = dev:formvalue(section)
284
285         -- Collect IP-Address
286         local inet = net:formvalue(section)
287         local isubnet = subnet:formvalue(section)
288         local inode = node:formvalue(section)
289
290         if not inet or not isubnet or not inode then
291                 return
292         end
293         local community = inet
294         local external  = community and uci:get("freifunk", community, "external") or ""
295         inet = uci:get("freifunk", community, "prefix") or inet
296
297         local dhcpbeg = 48 + tonumber(inode) * 4
298         local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
299         local limit = dhcpbeg < 252 and 3 or 2
300
301         -- Delete old alias
302         uci:delete("network", device .. "dhcp")
303
304         -- Create alias
305         local aliasbase = uci:get_all("freifunk", "alias")
306         util.update(aliasbase, uci:get_all(external, "alias") or {})
307         aliasbase.interface = device
308         aliasbase.ipaddr = dclient
309         aliasbase.proto = "static"
310         uci:section("network", "alias", device .. "dhcp", aliasbase)
311         uci:save("network")
312
313
314         -- Create dhcp
315         local dhcpbase = uci:get_all("freifunk", "dhcp")
316         util.update(dhcpbase, uci:get_all(external, "dhcp") or {})
317         dhcpbase.interface = device .. "dhcp"
318         dhcpbase.start = dhcpbeg
319         dhcpbase.limit = limit
320
321         uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
322         uci:save("dhcp")
323
324         uci:delete_all("firewall", "rule", {
325                 src="freifunk",
326                 proto="udp",
327                 src_port="68",
328                 dest_port="67"
329         })
330         uci:section("firewall", "rule", nil, {
331                 src="freifunk",
332                 proto="udp",
333                 src_port="68",
334                 dest_port="67",
335                 target="ACCEPT"
336         })
337         uci:delete_all("firewall", "rule", {
338                 src="freifunk",
339                 proto="tcp",
340                 dest_port="8082",
341         })
342         uci:section("firewall", "rule", nil, {
343                 src="freifunk",
344                 proto="tcp",
345                 dest_port="8082",
346                 target="ACCEPT"
347         })
348
349
350
351         -- Delete old splash
352         uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"})
353
354         -- Register splash
355         uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"})
356         uci:save("luci_splash")
357 end
358
359 return f