1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.controller.admin.index", package.seeall)
6 function action_logout()
7 local dsp = require "luci.dispatcher"
8 local utl = require "luci.util"
9 local sid = dsp.context.authsession
12 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
14 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
15 '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
19 luci.http.redirect(dsp.build_url())
22 function action_translations(lang)
23 local i18n = require "luci.i18n"
24 local http = require "luci.http"
25 local fs = require "nixio".fs
27 if lang and #lang > 0 then
28 lang = i18n.setlanguage(lang)
30 local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
32 http.header("Cache-Control", "public, max-age=31536000")
33 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
38 http.prepare_content("application/javascript; charset=utf-8")
39 http.write("window.TR=")
40 http.write_json(i18n.dump())
43 local function ubus_reply(id, data, code, errmsg)
44 local reply = { jsonrpc = "2.0", id = id }
50 elseif type(code) == "table" then
53 reply.result = { code, data }
71 local function ubus_access(sid, obj, fun)
72 local res, code = luci.util.ubus("session", "access", {
73 ubus_rpc_session = sid,
79 return (type(res) == "table" and res.access == true)
82 local function ubus_request(req)
83 if type(req) ~= "table" or type(req.method) ~= "string" or req.jsonrpc ~= "2.0" or req.id == nil then
84 return ubus_reply(nil, nil, -32600, "Invalid request")
86 elseif req.method == "call" then
87 if type(req.params) ~= "table" or #req.params < 3 then
88 return ubus_reply(nil, nil, -32600, "Invalid parameters")
91 local sid, obj, fun, arg =
92 req.params[1], req.params[2], req.params[3], req.params[4] or {}
93 if type(arg) ~= "table" or arg.ubus_rpc_session ~= nil then
94 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
97 if sid == "00000000000000000000000000000000" and luci.dispatcher.context.authsession then
98 sid = luci.dispatcher.context.authsession
101 if not ubus_access(sid, obj, fun) then
102 return ubus_reply(req.id, nil, -32002, "Access denied")
105 arg.ubus_rpc_session = sid
107 local res, code = luci.util.ubus(obj, fun, arg)
108 return ubus_reply(req.id, res, code or 0)
110 elseif req.method == "list" then
111 if req.params == nil or (type(req.params) == "table" and #req.params == 0) then
112 local objs = luci.util.ubus()
113 return ubus_reply(req.id, nil, objs)
115 elseif type(req.params) == "table" then
116 local n, rv = nil, {}
117 for n = 1, #req.params do
118 if type(req.params[n]) ~= "string" then
119 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
122 local sig = luci.util.ubus(req.params[n])
123 if sig and type(sig) == "table" then
124 rv[req.params[n]] = {}
127 for m, p in pairs(sig) do
128 if type(p) == "table" then
129 rv[req.params[n]][m] = {}
132 for pn, pt in pairs(p) do
133 rv[req.params[n]][m][pn] = ubus_types[pt] or "unknown"
139 return ubus_reply(req.id, nil, rv)
142 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
146 return ubus_reply(req.id, nil, -32601, "Method not found")
149 function action_ubus()
150 local parser = require "luci.jsonc".new()
152 luci.http.context.request:setfilehandler(function(_, s)
157 local ok, err = parser:parse(s)
158 return (not err or nil)
161 luci.http.context.request:content()
163 local json = parser:get()
164 if json == nil or type(json) ~= "table" then
165 luci.http.prepare_content("application/json")
166 luci.http.write_json(ubus_reply(nil, nil, -32700, "Parse error"))
172 response = ubus_request(json)
177 for _, request in ipairs(json) do
178 response[_] = ubus_request(request)
182 luci.http.prepare_content("application/json")
183 luci.http.write_json(response)