luci-app-olsr: handle empty result for non-status tables
[oweals/luci.git] / modules / luci-base / luasrc / controller / admin / index.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.admin.index", package.seeall)
5
6 function index()
7         function toplevel_page(page, preflookup, preftarget)
8                 if preflookup and preftarget then
9                         if lookup(preflookup) then
10                                 page.target = preftarget
11                         end
12                 end
13
14                 if not page.target then
15                         page.target = firstchild()
16                 end
17         end
18
19         local uci = require("luci.model.uci").cursor()
20
21         local root = node()
22         if not root.target then
23                 root.target = alias("admin")
24                 root.index = true
25         end
26
27         local page   = node("admin")
28
29         page.title   = _("Administration")
30         page.order   = 10
31         page.sysauth = "root"
32         page.sysauth_authenticator = "htmlauth"
33         page.ucidata = true
34         page.index = true
35         page.target = firstnode()
36
37         -- Empty menu tree to be populated by addons and modules
38
39         page = node("admin", "status")
40         page.title = _("Status")
41         page.order = 10
42         page.index = true
43         -- overview is from mod-admin-full
44         toplevel_page(page, "admin/status/overview", alias("admin", "status", "overview"))
45
46         page = node("admin", "system")
47         page.title = _("System")
48         page.order = 20
49         page.index = true
50         -- system/system is from mod-admin-full
51         toplevel_page(page, "admin/system/system", alias("admin", "system", "system"))
52
53         -- Only used if applications add items
54         page = node("admin", "services")
55         page.title = _("Services")
56         page.order = 40
57         page.index = true
58         toplevel_page(page, false, false)
59
60         -- Even for mod-admin-full network just uses first submenu item as landing
61         page = node("admin", "network")
62         page.title = _("Network")
63         page.order = 50
64         page.index = true
65         toplevel_page(page, false, false)
66
67         if nixio.fs.access("/etc/config/dhcp") then
68                 page = entry({"admin", "dhcplease_status"}, call("lease_status"), nil)
69                 page.leaf = true
70         end
71
72         local has_wifi = false
73
74         uci:foreach("wireless", "wifi-device",
75                 function(s)
76                         has_wifi = true
77                         return false
78                 end)
79
80         if has_wifi then
81                 page = entry({"admin", "wireless_assoclist"}, call("wifi_assoclist"), nil)
82                 page.leaf = true
83
84                 page = entry({"admin", "wireless_deauth"}, post("wifi_deauth"), nil)
85                 page.leaf = true
86         end
87
88         page = entry({"admin", "translations"}, call("action_translations"), nil)
89         page.leaf = true
90
91         page = entry({"admin", "ubus"}, call("action_ubus"), nil)
92         page.leaf = true
93
94         -- Logout is last
95         entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
96 end
97
98 function action_logout()
99         local dsp = require "luci.dispatcher"
100         local utl = require "luci.util"
101         local sid = dsp.context.authsession
102
103         if sid then
104                 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
105
106                 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
107                         '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
108                 })
109         end
110
111         luci.http.redirect(dsp.build_url())
112 end
113
114 function action_translations(lang)
115         local i18n = require "luci.i18n"
116         local http = require "luci.http"
117         local fs = require "nixio".fs
118
119         if lang and #lang > 0 then
120                 lang = i18n.setlanguage(lang)
121                 if lang then
122                         local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
123                         if s then
124                                 http.header("Cache-Control", "public, max-age=31536000")
125                                 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
126                         end
127                 end
128         end
129
130         http.prepare_content("application/javascript; charset=utf-8")
131         http.write("window.TR=")
132         http.write_json(i18n.dump())
133 end
134
135 local function ubus_reply(id, data, code, errmsg)
136         local reply = { jsonrpc = "2.0", id = id }
137         if errmsg then
138                 reply.error = {
139                         code = code,
140                         message = errmsg
141                 }
142         else
143                 reply.result = { code, data }
144         end
145
146         return reply
147 end
148
149 local ubus_types = {
150         nil,
151         "array",
152         "object",
153         "string",
154         nil, -- INT64
155         "number",
156         nil, -- INT16,
157         "boolean",
158         "double"
159 }
160
161 local function ubus_request(req)
162         if type(req) ~= "table" or type(req.method) ~= "string" or type(req.params) ~= "table" or
163            #req.params < 2 or req.jsonrpc ~= "2.0" or req.id == nil then
164                 return ubus_reply(nil, nil, -32600, "Invalid request")
165
166         elseif req.method == "call" then
167                 local sid, obj, fun, arg =
168                         req.params[1], req.params[2], req.params[3], req.params[4] or {}
169                 if type(arg) ~= "table" or arg.ubus_rpc_session ~= nil then
170                         return ubus_reply(req.id, nil, -32602, "Invalid parameters")
171                 end
172
173                 if sid == "00000000000000000000000000000000" then
174                         sid = luci.dispatcher.context.authsession
175                 end
176
177                 arg.ubus_rpc_session = sid
178
179                 local res, code = luci.util.ubus(obj, fun, arg)
180                 return ubus_reply(req.id, res, code or 0)
181
182         elseif req.method == "list" then
183                 if type(params) ~= "table" or #params == 0 then
184                         local objs = { luci.util.ubus() }
185                         return ubus_reply(req.id, objs, 0)
186                 else
187                         local n, rv = nil, {}
188                         for n = 1, #params do
189                                 if type(params[n]) ~= "string" then
190                                         return ubus_reply(req.id, nil, -32602, "Invalid parameters")
191                                 end
192
193                                 local sig = luci.util.ubus(params[n])
194                                 if sig and type(sig) == "table" then
195                                         rv[params[n]] = {}
196
197                                         local m, p
198                                         for m, p in pairs(sig) do
199                                                 if type(p) == "table" then
200                                                         rv[params[n]][m] = {}
201
202                                                         local pn, pt
203                                                         for pn, pt in pairs(p) do
204                                                                 rv[params[n]][m][pn] = ubus_types[pt] or "unknown"
205                                                         end
206                                                 end
207                                         end
208                                 end
209                         end
210                         return ubus_reply(req.id, rv, 0)
211                 end
212         end
213
214         return ubus_reply(req.id, nil, -32601, "Method not found")
215 end
216
217 function action_ubus()
218         local parser = require "luci.jsonc".new()
219
220         luci.http.context.request:setfilehandler(function(_, s)
221                 if not s then
222                         return nil
223                 end
224
225                 local ok, err = parser:parse(s)
226                 return (not err or nil)
227         end)
228
229         luci.http.context.request:content()
230
231         local json = parser:get()
232         if json == nil or type(json) ~= "table" then
233                 luci.http.prepare_content("application/json")
234                 luci.http.write_json(ubus_reply(nil, nil, -32700, "Parse error"))
235                 return
236         end
237
238         local response
239         if #json == 0 then
240                 response = ubus_request(json)
241         else
242                 response = {}
243
244                 local _, request
245                 for _, request in ipairs(json) do
246                         response[_] = ubus_request(request)
247                 end
248         end
249
250         luci.http.prepare_content("application/json")
251         luci.http.write_json(response)
252 end
253
254 function lease_status()
255         local s = require "luci.tools.status"
256
257         luci.http.prepare_content("application/json")
258         luci.http.write('[')
259         luci.http.write_json(s.dhcp_leases())
260         luci.http.write(',')
261         luci.http.write_json(s.dhcp6_leases())
262         luci.http.write(']')
263 end
264
265 function wifi_assoclist()
266         local s = require "luci.tools.status"
267
268         luci.http.prepare_content("application/json")
269         luci.http.write_json(s.wifi_assoclist())
270 end
271
272 function wifi_deauth()
273         local iface = luci.http.formvalue("iface")
274         local bssid = luci.http.formvalue("bssid")
275
276         if iface and bssid then
277                 luci.util.ubus("hostapd.%s" % iface, "del_client", {
278                         addr = bssid,
279                         deauth = true,
280                         reason = 5,
281                         ban_time = 60000
282                 })
283         end
284         luci.http.status(200, "OK")
285 end