1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
-- Copyright 2008-2015 Jo-Philipp Wich <jow@openwrt.org>
-- Licensed to the public under the Apache License 2.0.
module("luci.dispatcher", package.seeall)
local http = _G.L.http
context = setmetatable({}, {
__index = function(t, k)
if k == "request" or k == "requestpath" then
return _G.L.ctx.request_path
elseif k == "requestargs" then
return _G.L.ctx.request_args
else
return _G.L.ctx[k]
end
end
})
uci = require "luci.model.uci"
uci:set_session_id(_G.L.ctx.authsession)
i18n = require "luci.i18n"
i18n.setlanguage(_G.L.dispatcher.lang)
build_url = _G.L.dispatcher.build_url
menu_json = _G.L.dispatcher.menu_json
error404 = _G.L.dispatcher.error404
error500 = _G.L.dispatcher.error500
function is_authenticated(auth)
local session = _G.L.dispatcher.is_authenticated(auth)
if session then
return session.sid, session.data, session.acls
end
end
function assign(path, clone, title, order)
local obj = node(unpack(path))
obj.title = title
obj.order = order
setmetatable(obj, {__index = node(unpack(clone))})
return obj
end
function entry(path, target, title, order)
local c = node(unpack(path))
c.title = title
c.order = order
c.action = target
return c
end
-- enabling the node.
function get(...)
return node(...)
end
function node(...)
local p = table.concat({ ... }, "/")
if not __entries[p] then
__entries[p] = {}
end
return __entries[p]
end
function lookup(...)
local i, path = nil, {}
for i = 1, select('#', ...) do
local name, arg = nil, tostring(select(i, ...))
for name in arg:gmatch("[^/]+") do
path[#path+1] = name
end
end
local node = menu_json()
for i = 1, #path do
node = node.children[path[i]]
if not node then
return nil
elseif node.leaf then
break
end
end
return node, build_url(unpack(path))
end
function process_lua_controller(path)
local base = "/usr/lib/lua/luci/controller/"
local modname = "luci.controller." .. path:sub(#base+1, #path-4):gsub("/", ".")
local mod = require(modname)
assert(mod ~= true,
"Invalid controller file found\n" ..
"The file '" .. path .. "' contains an invalid module line.\n" ..
"Please verify whether the module name is set to '" .. modname ..
"' - It must correspond to the file path!")
local idx = mod.index
if type(idx) ~= "function" then
return nil
end
local entries = {}
__entries = entries
__controller = modname
setfenv(idx, setmetatable({}, { __index = luci.dispatcher }))()
__entries = nil
__controller = nil
-- fixup gathered node specs
for path, entry in pairs(entries) do
if entry.leaf then
entry.wildcard = true
end
if type(entry.file_depends) == "table" then
for _, v in ipairs(entry.file_depends) do
entry.depends = entry.depends or {}
entry.depends.fs = entry.depends.fs or {}
local ft = fs.stat(v, "type")
if ft == "dir" then
entry.depends.fs[v] = "directory"
elseif v:match("/s?bin/") then
entry.depends.fs[v] = "executable"
else
entry.depends.fs[v] = "file"
end
end
end
if type(entry.uci_depends) == "table" then
for k, v in pairs(entry.uci_depends) do
entry.depends = entry.depends or {}
entry.depends.uci = entry.depends.uci or {}
entry.depends.uci[k] = v
end
end
if type(entry.acl_depends) == "table" then
for _, acl in ipairs(entry.acl_depends) do
entry.depends = entry.depends or {}
entry.depends.acl = entry.depends.acl or {}
entry.depends.acl[#entry.depends.acl + 1] = acl
end
end
if (entry.sysauth_authenticator ~= nil) or
(entry.sysauth ~= nil and entry.sysauth ~= false)
then
if entry.sysauth_authenticator == "htmlauth" then
entry.auth = {
login = true,
methods = { "cookie:sysauth_https", "cookie:sysauth_http" }
}
elseif path == "rpc" and modname == "luci.controller.rpc" then
entry.auth = {
login = false,
methods = { "query:auth", "cookie:sysauth_https", "cookie:sysauth_http", "cookie:sysauth" }
}
elseif modname == "luci.controller.admin.uci" then
entry.auth = {
login = false,
methods = { "param:sid" }
}
end
elseif entry.sysauth == false then
entry.auth = {}
end
if entry.action == nil and type(entry.target) == "table" then
entry.action = entry.target
entry.target = nil
end
entry.leaf = nil
entry.file_depends = nil
entry.uci_depends = nil
entry.acl_depends = nil
entry.sysauth = nil
entry.sysauth_authenticator = nil
end
return entries
end
function invoke_cbi_action(model, config, ...)
local cbi = require "luci.cbi"
local tpl = require "luci.template"
local util = require "luci.util"
if not config then
config = {}
end
local maps = cbi.load(model, ...)
local state = nil
local function has_uci_access(config, level)
local rv = util.ubus("session", "access", {
ubus_rpc_session = context.authsession,
scope = "uci", object = config,
["function"] = level
})
return (type(rv) == "table" and rv.access == true) or false
end
local i, res
for i, res in ipairs(maps) do
if util.instanceof(res, cbi.SimpleForm) then
io.stderr:write("Model %s returns SimpleForm but is dispatched via cbi(),\n"
% model)
io.stderr:write("please change %s to use the form() action instead.\n"
% table.concat(context.request, "/"))
end
res.flow = config
local cstate = res:parse()
if cstate and (not state or cstate < state) then
state = cstate
end
end
local function _resolve_path(path)
return type(path) == "table" and build_url(unpack(path)) or path
end
if config.on_valid_to and state and state > 0 and state < 2 then
http:redirect(_resolve_path(config.on_valid_to))
return
end
if config.on_changed_to and state and state > 1 then
http:redirect(_resolve_path(config.on_changed_to))
return
end
if config.on_success_to and state and state > 0 then
http:redirect(_resolve_path(config.on_success_to))
return
end
if config.state_handler then
if not config.state_handler(state, maps) then
return
end
end
http:header("X-CBI-State", state or 0)
if not config.noheader then
_G.L.include("cbi/header", {state = state})
end
local redirect
local messages
local applymap = false
local pageaction = true
local parsechain = { }
local writable = false
for i, res in ipairs(maps) do
if res.apply_needed and res.parsechain then
local c
for _, c in ipairs(res.parsechain) do
parsechain[#parsechain+1] = c
end
applymap = true
end
if res.redirect then
redirect = redirect or res.redirect
end
if res.pageaction == false then
pageaction = false
end
if res.message then
messages = messages or { }
messages[#messages+1] = res.message
end
end
for i, res in ipairs(maps) do
local is_readable_map = has_uci_access(res.config, "read")
local is_writable_map = has_uci_access(res.config, "write")
writable = writable or is_writable_map
res:render({
firstmap = (i == 1),
redirect = redirect,
messages = messages,
pageaction = pageaction,
parsechain = parsechain,
readable = is_readable_map,
writable = is_writable_map
})
end
if not config.nofooter then
_G.L.include("cbi/footer", {
flow = config,
pageaction = pageaction,
redirect = redirect,
state = state,
autoapply = config.autoapply,
trigger_apply = applymap,
writable = writable
})
end
end
function invoke_form_action(model, ...)
local cbi = require "luci.cbi"
local tpl = require "luci.template"
local maps = luci.cbi.load(model, ...)
local state = nil
local i, res
for i, res in ipairs(maps) do
local cstate = res:parse()
if cstate and (not state or cstate < state) then
state = cstate
end
end
http:header("X-CBI-State", state or 0)
_G.L.include("header")
for i, res in ipairs(maps) do
res:render()
end
_G.L.include("footer")
end
function render_lua_template(path)
local tpl = require "luci.template"
tpl.render(path, getfenv(1))
end
function call(name, ...)
return {
["type"] = "call",
["module"] = __controller,
["function"] = name,
["parameters"] = select('#', ...) > 0 and {...} or nil
}
end
function post(name, ...)
return {
["type"] = "call",
["module"] = __controller,
["function"] = name,
["parameters"] = select('#', ...) > 0 and {...} or nil,
["post"] = true
}
end
function view(name)
return {
["type"] = "view",
["path"] = name
}
end
function template(name)
return {
["type"] = "template",
["path"] = name
}
end
function cbi(model, config)
return {
["type"] = "call",
["module"] = "luci.dispatcher",
["function"] = "invoke_cbi_action",
["parameters"] = { model, config or {} },
["post"] = {
["cbi.submit"] = true
}
}
end
function form(model)
return {
["type"] = "call",
["module"] = "luci.dispatcher",
["function"] = "invoke_form_action",
["parameters"] = { model },
["post"] = {
["cbi.submit"] = true
}
}
end
function firstchild()
return {
["type"] = "firstchild"
}
end
function firstnode()
return {
["type"] = "firstchild",
["recurse"] = true
}
end
function arcombine(trg1, trg2)
return {
["type"] = "arcombine",
["targets"] = { trg1, trg2 } --,
--env = getfenv(),
}
end
function alias(...)
return {
["type"] = "alias",
["path"] = table.concat({ ... }, "/")
}
end
function rewrite(n, ...)
return {
["type"] = "rewrite",
["path"] = table.concat({ ... }, "/"),
["remove"] = n
}
end
translate = i18n.translate
-- This function does not actually translate the given argument but
-- is used by build/i18n-scan.pl to find translatable entries.
function _(text)
return text
end
|