luci-base: fix list method handling in ubus-rpc protocol proxy
[project/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", "vpn")
55 page.title = _("VPN")
56 page.order = 30
57 page.index = true
58 toplevel_page(page, false, false)
59
60 -- Only used if applications add items
61 page = node("admin", "services")
62 page.title = _("Services")
63 page.order = 40
64 page.index = true
65 toplevel_page(page, false, false)
66
67 -- Even for mod-admin-full network just uses first submenu item as landing
68 page = node("admin", "network")
69 page.title = _("Network")
70 page.order = 50
71 page.index = true
72 toplevel_page(page, false, false)
73
74 if nixio.fs.access("/etc/config/dhcp") then
75 page = entry({"admin", "dhcplease_status"}, call("lease_status"), nil)
76 page.leaf = true
77 end
78
79 local has_wifi = false
80
81 uci:foreach("wireless", "wifi-device",
82 function(s)
83 has_wifi = true
84 return false
85 end)
86
87 if has_wifi then
88 page = entry({"admin", "wireless_assoclist"}, call("wifi_assoclist"), nil)
89 page.leaf = true
90
91 page = entry({"admin", "wireless_deauth"}, post("wifi_deauth"), nil)
92 page.leaf = true
93 end
94
95 page = entry({"admin", "translations"}, call("action_translations"), nil)
96 page.leaf = true
97
98 page = entry({"admin", "ubus"}, call("action_ubus"), nil)
99 page.sysauth = false
100 page.leaf = true
101
102 -- Logout is last
103 entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
104 end
105
106 function action_logout()
107 local dsp = require "luci.dispatcher"
108 local utl = require "luci.util"
109 local sid = dsp.context.authsession
110
111 if sid then
112 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
113
114 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
115 '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
116 })
117 end
118
119 luci.http.redirect(dsp.build_url())
120 end
121
122 function action_translations(lang)
123 local i18n = require "luci.i18n"
124 local http = require "luci.http"
125 local fs = require "nixio".fs
126
127 if lang and #lang > 0 then
128 lang = i18n.setlanguage(lang)
129 if lang then
130 local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
131 if s then
132 http.header("Cache-Control", "public, max-age=31536000")
133 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
134 end
135 end
136 end
137
138 http.prepare_content("application/javascript; charset=utf-8")
139 http.write("window.TR=")
140 http.write_json(i18n.dump())
141 end
142
143 local function ubus_reply(id, data, code, errmsg)
144 local reply = { jsonrpc = "2.0", id = id }
145 if errmsg then
146 reply.error = {
147 code = code,
148 message = errmsg
149 }
150 elseif type(code) == "table" then
151 reply.result = code
152 else
153 reply.result = { code, data }
154 end
155
156 return reply
157 end
158
159 local ubus_types = {
160 nil,
161 "array",
162 "object",
163 "string",
164 nil, -- INT64
165 "number",
166 nil, -- INT16,
167 "boolean",
168 "double"
169 }
170
171 local function ubus_access(sid, obj, fun)
172 local res, code = luci.util.ubus("session", "access", {
173 ubus_rpc_session = sid,
174 scope = "ubus",
175 object = obj,
176 ["function"] = fun
177 })
178
179 return (type(res) == "table" and res.access == true)
180 end
181
182 local function ubus_request(req)
183 if type(req) ~= "table" or type(req.method) ~= "string" or req.jsonrpc ~= "2.0" or req.id == nil then
184 return ubus_reply(nil, nil, -32600, "Invalid request")
185
186 elseif req.method == "call" then
187 if type(req.params) ~= "table" or #req.params < 3 then
188 return ubus_reply(nil, nil, -32600, "Invalid parameters")
189 end
190
191 local sid, obj, fun, arg =
192 req.params[1], req.params[2], req.params[3], req.params[4] or {}
193 if type(arg) ~= "table" or arg.ubus_rpc_session ~= nil then
194 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
195 end
196
197 if sid == "00000000000000000000000000000000" and luci.dispatcher.context.authsession then
198 sid = luci.dispatcher.context.authsession
199 end
200
201 if not ubus_access(sid, obj, fun) then
202 return ubus_reply(req.id, nil, -32002, "Access denied")
203 end
204
205 arg.ubus_rpc_session = sid
206
207 local res, code = luci.util.ubus(obj, fun, arg)
208 return ubus_reply(req.id, res, code or 0)
209
210 elseif req.method == "list" then
211 if req.params == nil or (type(req.params) == "table" and #req.params == 0) then
212 local objs = luci.util.ubus()
213 return ubus_reply(req.id, nil, objs)
214
215 elseif type(req.params) == "table" then
216 local n, rv = nil, {}
217 for n = 1, #req.params do
218 if type(req.params[n]) ~= "string" then
219 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
220 end
221
222 local sig = luci.util.ubus(req.params[n])
223 if sig and type(sig) == "table" then
224 rv[req.params[n]] = {}
225
226 local m, p
227 for m, p in pairs(sig) do
228 if type(p) == "table" then
229 rv[req.params[n]][m] = {}
230
231 local pn, pt
232 for pn, pt in pairs(p) do
233 rv[req.params[n]][m][pn] = ubus_types[pt] or "unknown"
234 end
235 end
236 end
237 end
238 end
239 return ubus_reply(req.id, nil, rv)
240
241 else
242 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
243 end
244 end
245
246 return ubus_reply(req.id, nil, -32601, "Method not found")
247 end
248
249 function action_ubus()
250 local parser = require "luci.jsonc".new()
251
252 luci.http.context.request:setfilehandler(function(_, s)
253 if not s then
254 return nil
255 end
256
257 local ok, err = parser:parse(s)
258 return (not err or nil)
259 end)
260
261 luci.http.context.request:content()
262
263 local json = parser:get()
264 if json == nil or type(json) ~= "table" then
265 luci.http.prepare_content("application/json")
266 luci.http.write_json(ubus_reply(nil, nil, -32700, "Parse error"))
267 return
268 end
269
270 local response
271 if #json == 0 then
272 response = ubus_request(json)
273 else
274 response = {}
275
276 local _, request
277 for _, request in ipairs(json) do
278 response[_] = ubus_request(request)
279 end
280 end
281
282 luci.http.prepare_content("application/json")
283 luci.http.write_json(response)
284 end
285
286 function lease_status()
287 local s = require "luci.tools.status"
288
289 luci.http.prepare_content("application/json")
290 luci.http.write('[')
291 luci.http.write_json(s.dhcp_leases())
292 luci.http.write(',')
293 luci.http.write_json(s.dhcp6_leases())
294 luci.http.write(']')
295 end
296
297 function wifi_assoclist()
298 local s = require "luci.tools.status"
299
300 luci.http.prepare_content("application/json")
301 luci.http.write_json(s.wifi_assoclist())
302 end
303
304 function wifi_deauth()
305 local iface = luci.http.formvalue("iface")
306 local bssid = luci.http.formvalue("bssid")
307
308 if iface and bssid then
309 luci.util.ubus("hostapd.%s" % iface, "del_client", {
310 addr = bssid,
311 deauth = true,
312 reason = 5,
313 ban_time = 60000
314 })
315 end
316 luci.http.status(200, "OK")
317 end