2 LuCI - Lua Development Framework
4 Copyright 2009 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 local type, ipairs = type, ipairs
16 local srv = require "luci.lucid.rpc.server"
17 local nixio = require "nixio"
18 local lucid = require "luci.lucid"
20 --- Internal system functions.
21 module "luci.lucid.rpc.system"
23 -- Prepare the RPC module.
25 local mod = srv.Module("System functions"):register({
28 multicall = srv.Method.extended(multicall),
29 authenticate = srv.Method.extended(authenticate)
31 mod.checkrestricted = function(self, session, request, ...)
32 if request ~= "authenticate" then
33 return srv.Module.checkrestricted(self, session, request, ...)
39 --- Simple echo test function.
40 -- @param object to be echoed object
41 -- @return echo object
46 --- Simple void test function.
51 --- Accumulate different requests and execute them.
52 -- @param session Session object
54 -- @return overall response object
55 function multicall(session, ...)
56 local server, responses, response = session.server, {}, nil
57 for k, req in ipairs({...}) do
59 if type(req) == "table" and type(req.method) == "string"
60 and (not req.params or type(req.params) == "table") then
61 req.params = req.params or {}
62 result = server.root:process(session, req.method, req.params)
63 if type(result) == "table" then
65 response = {jsonrpc=req.jsonrpc, id=req.id,
66 result=result.result, error=result.error}
70 response = {jsonrpc=req.jsonrpc, id=req.id,
71 result=nil, error={code=srv.ERRNO_INTERNAL,
72 message=srv.ERRMSG[ERRNO_INTERNAL]}}
76 responses[k] = response
81 --- Create or use a new authentication token.
82 -- @param session Session object
83 -- @param type Authentication type
84 -- @param entity Authentication enttity (username)
85 -- @param key Authentication key (password)
86 -- @return boolean status
87 function authenticate(session, type, entity, key)
91 elseif type == "plain" then
92 local pwe = nixio.getsp and nixio.getsp(entity) or nixio.getpw(entity)
93 local pwh = pwe and (pwe.pwdp or pwe.passwd)
94 if not pwh or #pwh < 1 or nixio.crypt(key, pwh) ~= pwh then