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", "services")
55 page.title = _("Services")
58 toplevel_page(page, false, false)
60 -- Even for mod-admin-full network just uses first submenu item as landing
61 page = node("admin", "network")
62 page.title = _("Network")
65 toplevel_page(page, false, false)
67 if nixio.fs.access("/etc/config/dhcp") then
68 page = entry({"admin", "dhcplease_status"}, call("lease_status"), nil)
72 local has_wifi = false
74 uci:foreach("wireless", "wifi-device",
81 page = entry({"admin", "wireless_assoclist"}, call("wifi_assoclist"), nil)
84 page = entry({"admin", "wireless_deauth"}, post("wifi_deauth"), nil)
88 page = entry({"admin", "translations"}, call("action_translations"), nil)
91 page = entry({"admin", "ubus"}, call("action_ubus"), nil)
95 entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
98 function action_logout()
99 local dsp = require "luci.dispatcher"
100 local utl = require "luci.util"
101 local sid = dsp.context.authsession
104 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
106 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
107 '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
111 luci.http.redirect(dsp.build_url())
114 function action_translations(lang)
115 local i18n = require "luci.i18n"
116 local http = require "luci.http"
117 local fs = require "nixio".fs
119 if lang and #lang > 0 then
120 lang = i18n.setlanguage(lang)
122 local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
124 http.header("Cache-Control", "public, max-age=31536000")
125 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
130 http.prepare_content("application/javascript; charset=utf-8")
131 http.write("window.TR=")
132 http.write_json(i18n.dump())
135 local function ubus_reply(id, data, code, errmsg)
136 local reply = { jsonrpc = "2.0", id = id }
143 reply.result = { code, data }
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")
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")
173 if sid == "00000000000000000000000000000000" then
174 sid = luci.dispatcher.context.authsession
177 arg.ubus_rpc_session = sid
179 local res, code = luci.util.ubus(obj, fun, arg)
180 return ubus_reply(req.id, res, code or 0)
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)
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")
193 local sig = luci.util.ubus(params[n])
194 if sig and type(sig) == "table" then
198 for m, p in pairs(sig) do
199 if type(p) == "table" then
200 rv[params[n]][m] = {}
203 for pn, pt in pairs(p) do
204 rv[params[n]][m][pn] = ubus_types[pt] or "unknown"
210 return ubus_reply(req.id, rv, 0)
214 return ubus_reply(req.id, nil, -32601, "Method not found")
217 function action_ubus()
218 local parser = require "luci.jsonc".new()
220 luci.http.context.request:setfilehandler(function(_, s)
225 local ok, err = parser:parse(s)
226 return (not err or nil)
229 luci.http.context.request:content()
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"))
240 response = ubus_request(json)
245 for _, request in ipairs(json) do
246 response[_] = ubus_request(request)
250 luci.http.prepare_content("application/json")
251 luci.http.write_json(response)
254 function lease_status()
255 local s = require "luci.tools.status"
257 luci.http.prepare_content("application/json")
259 luci.http.write_json(s.dhcp_leases())
261 luci.http.write_json(s.dhcp6_leases())
265 function wifi_assoclist()
266 local s = require "luci.tools.status"
268 luci.http.prepare_content("application/json")
269 luci.http.write_json(s.wifi_assoclist())
272 function wifi_deauth()
273 local iface = luci.http.formvalue("iface")
274 local bssid = luci.http.formvalue("bssid")
276 if iface and bssid then
277 luci.util.ubus("hostapd.%s" % iface, "del_client", {
284 luci.http.status(200, "OK")