cf3aa6abe43feb83e071d93c5b1ea5bd50a1af08
[project/luci.git] / libs / lucid-rpc / luasrc / lucid / rpc / system.lua
1 --[[
2 LuCI - Lua Development Framework
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]
14
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"
19
20 --- Internal system functions.
21 module "luci.lucid.rpc.system"
22
23 -- Prepare the RPC module.
24 function _factory()
25         local mod = srv.Module("System functions"):register({
26                 echo = echo,
27                 void = void,
28                 multicall = srv.Method.extended(multicall),
29                 authenticate = srv.Method.extended(authenticate)
30         })
31         mod.checkrestricted = function(self, session, request, ...)
32                 if request ~= "authenticate" then
33                         return srv.Module.checkrestricted(self, session, request, ...)
34                 end
35         end
36         return mod
37 end
38
39 --- Simple echo test function.
40 -- @param object to be echoed object
41 -- @return echo object
42 function echo(object)
43         return object
44 end
45
46 --- Simple void test function.
47 function void()
48
49 end
50
51 --- Accumulate different requests and execute them.
52 -- @param session Session object
53 -- @param ...
54 -- @return overall response object
55 function multicall(session, ...)
56         local server, responses, response = session.server, {}, nil
57         for k, req in ipairs({...}) do
58                 response = nil
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
64                                 if req.id ~= nil then
65                                         response = {jsonrpc=req.jsonrpc, id=req.id,
66                                                 result=result.result, error=result.error}
67                                 end
68                         else
69                                 if req.id ~= nil then
70                                         response = {jsonrpc=req.jsonrpc, id=req.id,
71                                          result=nil, error={code=srv.ERRNO_INTERNAL,
72                                          message=srv.ERRMSG[ERRNO_INTERNAL]}}
73                                 end
74                         end
75                 end
76                 responses[k] = response
77         end
78         return responses
79 end
80
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)
88         if not type then
89                 session.user = nil
90                 return true
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
95                         return nil
96                 else
97                         session.user = entity
98                         return true
99                 end
100         end
101 end