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)
7 function toplevel_page(page, preflookup, preftarget)
8 if preflookup and preftarget then
9 if lookup(preflookup) then
10 page.target = preftarget
14 if not page.target then
15 page.target = firstchild()
19 local uci = require("luci.model.uci").cursor()
22 if not root.target then
23 root.target = alias("admin")
27 local page = node("admin")
29 page.title = _("Administration")
32 page.sysauth_authenticator = "htmlauth"
35 page.target = firstnode()
37 -- Empty menu tree to be populated by addons and modules
39 page = node("admin", "status")
40 page.title = _("Status")
43 -- overview is from mod-admin-full
44 toplevel_page(page, "admin/status/overview", alias("admin", "status", "overview"))
46 page = node("admin", "system")
47 page.title = _("System")
50 -- system/system is from mod-admin-full
51 toplevel_page(page, "admin/system/system", alias("admin", "system", "system"))
53 -- Only used if applications add items
54 page = node("admin", "vpn")
58 toplevel_page(page, false, false)
60 -- Only used if applications add items
61 page = node("admin", "services")
62 page.title = _("Services")
65 toplevel_page(page, false, false)
67 -- Even for mod-admin-full network just uses first submenu item as landing
68 page = node("admin", "network")
69 page.title = _("Network")
72 toplevel_page(page, false, false)
74 if nixio.fs.access("/etc/config/dhcp") then
75 page = entry({"admin", "dhcplease_status"}, call("lease_status"), nil)
79 local has_wifi = false
81 uci:foreach("wireless", "wifi-device",
88 page = entry({"admin", "wireless_assoclist"}, call("wifi_assoclist"), nil)
91 page = entry({"admin", "wireless_deauth"}, post("wifi_deauth"), nil)
95 page = entry({"admin", "translations"}, call("action_translations"), nil)
98 page = entry({"admin", "ubus"}, call("action_ubus"), nil)
103 entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
106 function action_logout()
107 local dsp = require "luci.dispatcher"
108 local utl = require "luci.util"
109 local sid = dsp.context.authsession
112 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
114 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
115 '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
119 luci.http.redirect(dsp.build_url())
122 function action_translations(lang)
123 local i18n = require "luci.i18n"
124 local http = require "luci.http"
125 local fs = require "nixio".fs
127 if lang and #lang > 0 then
128 lang = i18n.setlanguage(lang)
130 local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
132 http.header("Cache-Control", "public, max-age=31536000")
133 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
138 http.prepare_content("application/javascript; charset=utf-8")
139 http.write("window.TR=")
140 http.write_json(i18n.dump())
143 local function ubus_reply(id, data, code, errmsg)
144 local reply = { jsonrpc = "2.0", id = id }
150 elseif type(code) == "table" then
153 reply.result = { code, data }
171 local function ubus_access(sid, obj, fun)
172 local res, code = luci.util.ubus("session", "access", {
173 ubus_rpc_session = sid,
179 return (type(res) == "table" and res.access == true)
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")
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")
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")
197 if sid == "00000000000000000000000000000000" and luci.dispatcher.context.authsession then
198 sid = luci.dispatcher.context.authsession
201 if not ubus_access(sid, obj, fun) then
202 return ubus_reply(req.id, nil, -32002, "Access denied")
205 arg.ubus_rpc_session = sid
207 local res, code = luci.util.ubus(obj, fun, arg)
208 return ubus_reply(req.id, res, code or 0)
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)
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")
222 local sig = luci.util.ubus(req.params[n])
223 if sig and type(sig) == "table" then
224 rv[req.params[n]] = {}
227 for m, p in pairs(sig) do
228 if type(p) == "table" then
229 rv[req.params[n]][m] = {}
232 for pn, pt in pairs(p) do
233 rv[req.params[n]][m][pn] = ubus_types[pt] or "unknown"
239 return ubus_reply(req.id, nil, rv)
242 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
246 return ubus_reply(req.id, nil, -32601, "Method not found")
249 function action_ubus()
250 local parser = require "luci.jsonc".new()
252 luci.http.context.request:setfilehandler(function(_, s)
257 local ok, err = parser:parse(s)
258 return (not err or nil)
261 luci.http.context.request:content()
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"))
272 response = ubus_request(json)
277 for _, request in ipairs(json) do
278 response[_] = ubus_request(request)
282 luci.http.prepare_content("application/json")
283 luci.http.write_json(response)
286 function lease_status()
287 local s = require "luci.tools.status"
289 luci.http.prepare_content("application/json")
291 luci.http.write_json(s.dhcp_leases())
293 luci.http.write_json(s.dhcp6_leases())
297 function wifi_assoclist()
298 local s = require "luci.tools.status"
300 luci.http.prepare_content("application/json")
301 luci.http.write_json(s.wifi_assoclist())
304 function wifi_deauth()
305 local iface = luci.http.formvalue("iface")
306 local bssid = luci.http.formvalue("bssid")
308 if iface and bssid then
309 luci.util.ubus("hostapd.%s" % iface, "del_client", {
316 luci.http.status(200, "OK")