libs/web: fix error in wep key validation
[project/luci.git] / applications / luci-olsr / luasrc / controller / olsr.lua
1 module("luci.controller.olsr", package.seeall)
2
3 function index()
4         if not nixio.fs.access("/etc/config/olsrd") then
5                 return
6         end
7
8         require("luci.i18n").loadc("olsr")
9         local i18n = luci.i18n.translate
10
11         local page  = node("admin", "status", "olsr")
12         page.target = call("action_index")
13         page.title  = "OLSR"
14         page.i18n   = "olsr"
15         page.subindex = true
16
17         local page  = node("admin", "status", "olsr", "routes")
18         page.target = call("action_routes")
19         page.title  = i18n("Routen")
20         page.order  = 10
21
22         local page  = node("admin", "status", "olsr", "topology")
23         page.target = call("action_topology")
24         page.title  = i18n("Topologie")
25         page.order  = 20
26
27         local page  = node("admin", "status", "olsr", "hna")
28         page.target = call("action_hna")
29         page.title  = "HNA"
30         page.order  = 30
31
32         local page  = node("admin", "status", "olsr", "mid")
33         page.target = call("action_mid")
34         page.title  = "MID"
35         page.order  = 50
36
37         local page  = node("admin", "status", "olsr", "smartgw")
38         page.target = call("action_smartgw")
39         page.title  = "SmartGW"
40         page.order  = 60
41
42         local ol = entry(
43                 {"admin", "services", "olsrd"},
44                 cbi("olsr/olsrd"), "OLSR"
45         )
46         ol.i18n = "olsr"
47         ol.subindex = true
48
49         entry(
50                 {"admin", "services", "olsrd", "iface"},
51                 cbi("olsr/olsrdiface")
52         ).leaf = true
53
54         entry(
55                 {"admin", "services", "olsrd", "hna"},
56                 cbi("olsr/olsrdhna"), "HNA Announcements"
57         )
58
59         oplg = entry(
60                 {"admin", "services", "olsrd", "plugins"},
61                 cbi("olsr/olsrdplugins"), "Plugins"
62         )
63         oplg.i18n = "olsr"
64         oplg.leaf = true
65         oplg.subindex = true
66
67         local uci = require("luci.model.uci").cursor()
68         uci:foreach("olsrd", "LoadPlugin",
69                 function (section)
70                         local lib = section.library
71                         entry(
72                                 {"admin", "services", "olsrd", "plugins", lib },
73                                 cbi("olsr/olsrdplugins"),
74                                 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
75                         )
76                 end
77         )
78 end
79
80 function action_index()
81         local data = fetch_txtinfo("links")
82
83         if not data or not data.Links then
84                 luci.template.render("status-olsr/error_olsr")
85                 return nil
86         end
87
88         local function compare(a, b)
89                 local c = tonumber(a.Cost)
90                 local d = tonumber(b.Cost)
91
92                 if not c or c == 0 then
93                         return false
94                 end
95
96                 if not d or d == 0 then
97                         return true
98                 end
99
100                 return c < d
101         end
102
103         table.sort(data.Links, compare)
104
105         luci.template.render("status-olsr/index", {links=data.Links})
106 end
107
108 function action_routes()
109         local data = fetch_txtinfo("routes")
110
111         if not data or not data.Routes then
112                 luci.template.render("status-olsr/error_olsr")
113                 return nil
114         end
115
116         local function compare(a, b)
117                 local c = tonumber(a.ETX)
118                 local d = tonumber(b.ETX)
119
120                 if not c or c == 0 then
121                         return false
122                 end
123
124                 if not d or d == 0 then
125                         return true
126                 end
127
128                 return c < d
129         end
130
131         table.sort(data.Routes, compare)
132
133         luci.template.render("status-olsr/routes", {routes=data.Routes})
134 end
135
136 function action_topology()
137         local data = fetch_txtinfo("topology")
138
139         if not data or not data.Topology then
140                 luci.template.render("status-olsr/error_olsr")
141                 return nil
142         end
143
144         local function compare(a, b)
145                 return a["Dest. IP"] < b["Dest. IP"]
146         end
147
148         table.sort(data.Topology, compare)
149
150         luci.template.render("status-olsr/topology", {routes=data.Topology})
151 end
152
153 function action_hna()
154         local data = fetch_txtinfo("hna")
155
156         if not data or not data.HNA then
157                 luci.template.render("status-olsr/error_olsr")
158                 return nil
159         end
160
161         local function compare(a, b)
162                 return a.Destination < b.Destination
163         end
164
165         table.sort(data.HNA, compare)
166
167         luci.template.render("status-olsr/hna", {routes=data.HNA})
168 end
169
170 function action_mid()
171         local data = fetch_txtinfo("mid")
172
173         if not data or not data.MID then
174                 luci.template.render("status-olsr/error_olsr")
175                 return nil
176         end
177
178         local function compare(a, b)
179                 return a["IP address"] < b["IP address"]
180         end
181
182         table.sort(data.MID, compare)
183
184         luci.template.render("status-olsr/mid", {mids=data.MID})
185 end
186
187 function action_smartgw()
188         local data = fetch_txtinfo("gateways")
189
190         if not data or not data.Gateways then
191                 luci.template.render("status-olsr/error_olsr")
192                 return nil
193         end
194
195         local function compare(a, b)
196                 return a["ETX"] < b["ETX"]
197         end
198
199         table.sort(data.Gateways, compare)
200
201         luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
202 end
203
204
205
206 -- Internal
207 function fetch_txtinfo(otable)
208         require("luci.sys")
209         local uci = require "luci.model.uci".cursor_state()
210         otable = otable or ""
211         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
212         local rawdatav6 = luci.sys.httpget("http://[::1]:2006/"..otable)
213         local data = {}
214         local dataindex = 0
215         local name = ""
216
217         if #rawdata ~= 0 then
218             local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
219
220             for i, tbl in ipairs(tables) do
221                         local lines = luci.util.split(tbl, "\r?\n", nil, true)
222                         name  = table.remove(lines, 1):sub(8)
223                         local keys  = luci.util.split(table.remove(lines, 1), "\t")
224                         local split = #keys - 1
225                         if not data[name] then
226                                 data[name] = {}
227                         end
228
229                         for j, line in ipairs(lines) do
230                                 dataindex = ( dataindex + 1 )
231                                 di = dataindex
232                                 local fields = luci.util.split(line, "\t", split)
233                                 data[name][di] = {}
234                                 for k, key in pairs(keys) do
235                                         if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
236                                                 hostname = nixio.getnameinfo(fields[k], "inet")
237                                                 if hostname then
238                                                         data[name][di][key] = fields[k]
239                                                         data[name][di]["Hostname"] = hostname
240                                                 else
241                                                         data[name][di][key] = fields[k]
242                                                 end
243                                         elseif key == "Local IP" then
244                                                 data[name][di][key] = fields[k]
245                                                 data[name][di]['Local Device'] = fields[k]
246                                                 uci:foreach("network", "interface",
247                                                         function(s)
248                                                                 localip = string.gsub(fields[k], '      ', '')
249                                                                 if s.ipaddr == localip then
250                                                                         data[name][di]['Local Device'] = s['.name'] or interface
251                                                                 end
252                                                         end)
253                                         elseif key == "Interface" then
254                                                 data[name][di][key] = fields[k]
255                                                 uci:foreach("network", "interface",
256                                                 function(s)
257                                                         interface = string.gsub(fields[k], '    ', '')
258                                                         if s.ifname == interface then
259                                                                 data[name][di][key] = s['.name'] or interface
260                                                         end
261                                                 end)
262                                         else
263                                             data[name][di][key] = fields[k]
264                                 end
265                                 end
266                                 if data[name][di].Linkcost then
267                                   data[name][di].LinkQuality,
268                                   data[name][di].NLQ,
269                                   data[name][di].ETX =
270                                   data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
271                                 end
272                         end
273                 end
274         end
275
276         if #rawdatav6 ~= 0 then
277             local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
278             for i, tbl in ipairs(tables) do
279                   local lines = luci.util.split(tbl, "\r?\n", nil, true)
280                   name  = table.remove(lines, 1):sub(8)
281                   local keys  = luci.util.split(table.remove(lines, 1), "\t")
282                   local split = #keys - 1
283                   if not data[name] then
284                         data[name] = {}
285                   end
286                   for j, line in ipairs(lines) do
287                         dataindex = ( dataindex + 1 )
288                         di = dataindex
289                         local fields = luci.util.split(line, "\t", split)
290                         data[name][di] = {}
291                         for k, key in pairs(keys) do
292                                 if key == "Remote IP" then
293                                         hostname = nixio.getnameinfo(fields[k], "inet6")
294                                         if hostname then
295                                                 data[name][di][key] = "[" .. fields[k] .. "]"
296                                                 data[name][di]["Hostname"] = hostname
297                                         else
298                                                 data[name][di][key] = "[" .. fields[k] .. "]"
299                                         end
300                                 elseif key == "Local IP" then
301                                         data[name][di][key] = fields[k]
302                                         data[name][di]['Local Device'] = fields[k]
303                                         uci:foreach("network", "interface",
304                                         function(s)
305                                                 local localip = string.gsub(fields[k], '        ', '')
306                                                 if s.ip6addr then
307                                                         local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
308                                                         if ip6addr == localip then
309                                                                 data[name][di]['Local Device'] = s['.name'] or s.interface
310                                                         end
311                                                 end
312                                         end)
313                                 elseif key == "Dest. IP" then
314                                     data[name][di][key] = "[" .. fields[k] .. "]"
315                                 elseif key == "Last hop IP" then
316                                     data[name][di][key] = "[" .. fields[k] .. "]"
317                                 elseif key == "IP address" then
318                                     data[name][di][key] = "[" .. fields[k] .. "]"
319                                 elseif key == "Gateway" then
320                                     data[name][di][key] = "[" .. fields[k] .. "]"
321                                 else
322                                     data[name][di][key] = fields[k]
323                                 end
324                         end
325                         
326                         if data[name][di].Linkcost then
327                                 data[name][di].LinkQuality,
328                                 data[name][di].NLQ,
329                                 data[name][di].ETX =
330                                 data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
331                         end
332                 end
333         end
334 end
335
336
337         if data then
338             return data
339         end
340 end