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
|
--[[
LuCI - Lua Development Framework
Copyright 2009 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
$Id$
]]
local type, ipairs = type, ipairs
local srv = require "luci.lucid.rpc.server"
local nixio = require "nixio"
local lucid = require "luci.lucid"
--- Internal system functions.
module "luci.lucid.rpc.system"
-- Prepare the RPC module.
function _factory()
local mod = srv.Module("System functions"):register({
echo = echo,
void = void,
multicall = srv.Method.extended(multicall),
authenticate = srv.Method.extended(authenticate)
})
mod.checkrestricted = function(self, session, request, ...)
if request ~= "authenticate" then
return srv.Module.checkrestricted(self, session, request, ...)
end
end
return mod
end
--- Simple echo test function.
-- @param object to be echoed object
-- @return echo object
function echo(object)
return object
end
--- Simple void test function.
function void()
end
--- Accumulate different requests and execute them.
-- @param session Session object
-- @param ...
-- @return overall response object
function multicall(session, ...)
local server, responses, response = session.server, {}, nil
for k, req in ipairs({...}) do
response = nil
if type(req) == "table" and type(req.method) == "string"
and (not req.params or type(req.params) == "table") then
req.params = req.params or {}
result = server.root:process(session, req.method, req.params)
if type(result) == "table" then
if req.id ~= nil then
response = {jsonrpc=req.jsonrpc, id=req.id,
result=result.result, error=result.error}
end
else
if req.id ~= nil then
response = {jsonrpc=req.jsonrpc, id=req.id,
result=nil, error={code=srv.ERRNO_INTERNAL,
message=srv.ERRMSG[ERRNO_INTERNAL]}}
end
end
end
responses[k] = response
end
return responses
end
--- Create or use a new authentication token.
-- @param session Session object
-- @param type Authentication type
-- @param entity Authentication enttity (username)
-- @param key Authentication key (password)
-- @return boolean status
function authenticate(session, type, entity, key)
if not type then
session.user = nil
return true
elseif type == "plain" then
local pwe = nixio.getsp and nixio.getsp(entity) or nixio.getpw(entity)
local pwh = pwe and (pwe.pwdp or pwe.passwd)
if not pwh or #pwh < 1 or nixio.crypt(key, pwh) ~= pwh then
return nil
else
session.user = entity
return true
end
end
end
|