4f7f0b5c29ceb17a7cbc95f49900b8c7f6921b92
[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 module "luci.lucid.rpc.system"
21
22 function _factory()
23         local mod = srv.Module("System functions"):register({
24                 echo = echo,
25                 void = void,
26                 multicall = srv.Method.extended(multicall),
27                 authenticate = srv.Method.extended(authenticate)
28         })
29         mod.checkrestricted = function(self, session, request, ...)
30                 if request ~= "authenticate" then
31                         return srv.Module.checkrestricted(self, session, request, ...)
32                 end
33         end
34         return mod
35 end
36
37
38 function echo(object)
39         return object
40 end
41
42 function void()
43
44 end
45
46 function multicall(session, ...)
47         local server, responses, response = session.server, {}, nil
48         for k, req in ipairs({...}) do
49                 response = nil
50                 if type(req) == "table" and type(req.method) == "string"
51                  and (not req.params or type(req.params) == "table") then
52                         req.params = req.params or {}
53                         result = server.root:process(session, req.method, req.params)
54                         if type(result) == "table" then
55                                 if req.id ~= nil then
56                                         response = {jsonrpc=req.jsonrpc, id=req.id,
57                                                 result=result.result, error=result.error}
58                                 end
59                         else
60                                 if req.id ~= nil then
61                                         response = {jsonrpc=req.jsonrpc, id=req.id,
62                                          result=nil, error={code=srv.ERRNO_INTERNAL,
63                                          message=srv.ERRMSG[ERRNO_INTERNAL]}}
64                                 end
65                         end
66                 end
67                 responses[k] = response
68         end
69         return responses
70 end
71
72 function authenticate(session, type, entity, key)
73         if not type then
74                 session.user = nil
75                 return true
76         elseif type == "plain" then
77                 local pwe = nixio.getsp and nixio.getsp(entity) or nixio.getpw(entity)
78                 local pwh = pwe and (pwe.pwdp or pwe.passwd)
79                 if not pwh or #pwh < 1 or nixio.crypt(key, pwh) ~= pwh then
80                         return nil
81                 else
82                         session.user = entity
83                         return true
84                 end
85         end
86 end