luci-base: add vpn menu section
[oweals/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.leaf = true
100
101         -- Logout is last
102         entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
103 end
104
105 function action_logout()
106         local dsp = require "luci.dispatcher"
107         local utl = require "luci.util"
108         local sid = dsp.context.authsession
109
110         if sid then
111                 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
112
113                 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
114                         '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
115                 })
116         end
117
118         luci.http.redirect(dsp.build_url())
119 end
120
121 function action_translations(lang)
122         local i18n = require "luci.i18n"
123         local http = require "luci.http"
124         local fs = require "nixio".fs
125
126         if lang and #lang > 0 then
127                 lang = i18n.setlanguage(lang)
128                 if lang then
129                         local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
130                         if s then
131                                 http.header("Cache-Control", "public, max-age=31536000")
132                                 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
133                         end
134                 end
135         end
136
137         http.prepare_content("application/javascript; charset=utf-8")
138         http.write("window.TR=")
139         http.write_json(i18n.dump())
140 end
141
142 local function ubus_reply(id, data, code, errmsg)
143         local reply = { jsonrpc = "2.0", id = id }
144         if errmsg then
145                 reply.error = {
146                         code = code,
147                         message = errmsg
148                 }
149         else
150                 reply.result = { code, data }
151         end
152
153         return reply
154 end
155
156 local ubus_types = {
157         nil,
158         "array",
159         "object",
160         "string",
161         nil, -- INT64
162         "number",
163         nil, -- INT16,
164         "boolean",
165         "double"
166 }
167
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")
172
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")
178                 end
179
180                 if sid == "00000000000000000000000000000000" then
181                         sid = luci.dispatcher.context.authsession
182                 end
183
184                 arg.ubus_rpc_session = sid
185
186                 local res, code = luci.util.ubus(obj, fun, arg)
187                 return ubus_reply(req.id, res, code or 0)
188
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)
193                 else
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")
198                                 end
199
200                                 local sig = luci.util.ubus(params[n])
201                                 if sig and type(sig) == "table" then
202                                         rv[params[n]] = {}
203
204                                         local m, p
205                                         for m, p in pairs(sig) do
206                                                 if type(p) == "table" then
207                                                         rv[params[n]][m] = {}
208
209                                                         local pn, pt
210                                                         for pn, pt in pairs(p) do
211                                                                 rv[params[n]][m][pn] = ubus_types[pt] or "unknown"
212                                                         end
213                                                 end
214                                         end
215                                 end
216                         end
217                         return ubus_reply(req.id, rv, 0)
218                 end
219         end
220
221         return ubus_reply(req.id, nil, -32601, "Method not found")
222 end
223
224 function action_ubus()
225         local parser = require "luci.jsonc".new()
226
227         luci.http.context.request:setfilehandler(function(_, s)
228                 if not s then
229                         return nil
230                 end
231
232                 local ok, err = parser:parse(s)
233                 return (not err or nil)
234         end)
235
236         luci.http.context.request:content()
237
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"))
242                 return
243         end
244
245         local response
246         if #json == 0 then
247                 response = ubus_request(json)
248         else
249                 response = {}
250
251                 local _, request
252                 for _, request in ipairs(json) do
253                         response[_] = ubus_request(request)
254                 end
255         end
256
257         luci.http.prepare_content("application/json")
258         luci.http.write_json(response)
259 end
260
261 function lease_status()
262         local s = require "luci.tools.status"
263
264         luci.http.prepare_content("application/json")
265         luci.http.write('[')
266         luci.http.write_json(s.dhcp_leases())
267         luci.http.write(',')
268         luci.http.write_json(s.dhcp6_leases())
269         luci.http.write(']')
270 end
271
272 function wifi_assoclist()
273         local s = require "luci.tools.status"
274
275         luci.http.prepare_content("application/json")
276         luci.http.write_json(s.wifi_assoclist())
277 end
278
279 function wifi_deauth()
280         local iface = luci.http.formvalue("iface")
281         local bssid = luci.http.formvalue("bssid")
282
283         if iface and bssid then
284                 luci.util.ubus("hostapd.%s" % iface, "del_client", {
285                         addr = bssid,
286                         deauth = true,
287                         reason = 5,
288                         ban_time = 60000
289                 })
290         end
291         luci.http.status(200, "OK")
292 end