summaryrefslogtreecommitdiffhomepage
path: root/libs/lucid-rpc/luasrc/lucid/rpc/system.lua
blob: 4f7f0b5c29ceb17a7cbc95f49900b8c7f6921b92 (plain)
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
--[[
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"

module "luci.lucid.rpc.system"

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


function echo(object)
	return object
end

function void()

end

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

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