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)
102 entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
105 function action_logout()
106 local dsp = require "luci.dispatcher"
107 local utl = require "luci.util"
108 local sid = dsp.context.authsession
111 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
113 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
114 '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
118 luci.http.redirect(dsp.build_url())
121 function action_translations(lang)
122 local i18n = require "luci.i18n"
123 local http = require "luci.http"
124 local fs = require "nixio".fs
126 if lang and #lang > 0 then
127 lang = i18n.setlanguage(lang)
129 local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
131 http.header("Cache-Control", "public, max-age=31536000")
132 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
137 http.prepare_content("application/javascript; charset=utf-8")
138 http.write("window.TR=")
139 http.write_json(i18n.dump())
142 local function ubus_reply(id, data, code, errmsg)
143 local reply = { jsonrpc = "2.0", id = id }
150 reply.result = { code, data }
168 local function ubus_request(req)
169 if type(req) ~= "table" or type(req.method) ~= "string" or type(req.params) ~= "table" or
170 #req.params < 2 or req.jsonrpc ~= "2.0" or req.id == nil then
171 return ubus_reply(nil, nil, -32600, "Invalid request")
173 elseif req.method == "call" then
174 local sid, obj, fun, arg =
175 req.params[1], req.params[2], req.params[3], req.params[4] or {}
176 if type(arg) ~= "table" or arg.ubus_rpc_session ~= nil then
177 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
180 if sid == "00000000000000000000000000000000" then
181 sid = luci.dispatcher.context.authsession
184 arg.ubus_rpc_session = sid
186 local res, code = luci.util.ubus(obj, fun, arg)
187 return ubus_reply(req.id, res, code or 0)
189 elseif req.method == "list" then
190 if type(params) ~= "table" or #params == 0 then
191 local objs = { luci.util.ubus() }
192 return ubus_reply(req.id, objs, 0)
194 local n, rv = nil, {}
195 for n = 1, #params do
196 if type(params[n]) ~= "string" then
197 return ubus_reply(req.id, nil, -32602, "Invalid parameters")
200 local sig = luci.util.ubus(params[n])
201 if sig and type(sig) == "table" then
205 for m, p in pairs(sig) do
206 if type(p) == "table" then
207 rv[params[n]][m] = {}
210 for pn, pt in pairs(p) do
211 rv[params[n]][m][pn] = ubus_types[pt] or "unknown"
217 return ubus_reply(req.id, rv, 0)
221 return ubus_reply(req.id, nil, -32601, "Method not found")
224 function action_ubus()
225 local parser = require "luci.jsonc".new()
227 luci.http.context.request:setfilehandler(function(_, s)
232 local ok, err = parser:parse(s)
233 return (not err or nil)
236 luci.http.context.request:content()
238 local json = parser:get()
239 if json == nil or type(json) ~= "table" then
240 luci.http.prepare_content("application/json")
241 luci.http.write_json(ubus_reply(nil, nil, -32700, "Parse error"))
247 response = ubus_request(json)
252 for _, request in ipairs(json) do
253 response[_] = ubus_request(request)
257 luci.http.prepare_content("application/json")
258 luci.http.write_json(response)
261 function lease_status()
262 local s = require "luci.tools.status"
264 luci.http.prepare_content("application/json")
266 luci.http.write_json(s.dhcp_leases())
268 luci.http.write_json(s.dhcp6_leases())
272 function wifi_assoclist()
273 local s = require "luci.tools.status"
275 luci.http.prepare_content("application/json")
276 luci.http.write_json(s.wifi_assoclist())
279 function wifi_deauth()
280 local iface = luci.http.formvalue("iface")
281 local bssid = luci.http.formvalue("bssid")
283 if iface and bssid then
284 luci.util.ubus("hostapd.%s" % iface, "del_client", {
291 luci.http.status(200, "OK")