libs/core: Reworked some basic libraries to not use package.seeall
[project/luci.git] / modules / rpc / luasrc / controller / rpc.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local require = require
17 local pairs = pairs
18 local print = print
19
20 module "luci.controller.rpc"
21
22 function index()
23         local function authenticator(validator, accs)
24                 local auth = luci.http.formvalue("auth", true)
25                 if auth then
26                         local user = luci.sauth.read(auth)
27                         if user and luci.util.contains(accs, user) then
28                                 return user, auth
29                         end
30                 end
31                 luci.http.status(403, "Forbidden")
32         end
33         
34         uci = entry({"rpc", "uci"}, call("rpc_uci"))
35         uci.sysauth = "root"
36         uci.sysauth_authenticator = authenticator
37         
38         fs = entry({"rpc", "fs"}, call("rpc_fs"))
39         fs.sysauth = "root"
40         fs.sysauth_authenticator = authenticator
41
42         fs = entry({"rpc", "sys"}, call("rpc_sys"))
43         fs.sysauth = "root"
44         fs.sysauth_authenticator = authenticator
45         
46         uci = entry({"rpc", "auth"}, call("rpc_auth"))
47 end
48
49 function rpc_auth()
50         local jsonrpc = require "luci.jsonrpc"
51         local sauth   = require "luci.sauth"
52         local http    = require "luci.http"
53         local sys     = require "luci.sys"
54         
55         http.setfilehandler()
56         
57         local loginstat
58         
59         local server = {}
60         server.login = function(user, pass)
61                 local sid
62                 
63                 if sys.user.checkpasswd(user, pass) then
64                         sid = sys.uniqueid(16)
65                         http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
66                         sauth.write(sid, user)
67                 end
68                 
69                 return sid
70         end
71         
72         http.prepare_content("application/json")
73         http.write(jsonrpc.handle(server, http.content()))
74 end
75
76 function rpc_uci()
77         local uci     = require "luci.controller.rpc.uci"
78         local jsonrpc = require "luci.jsonrpc"
79         local http    = require "luci.http"
80         
81         http.setfilehandler()
82         http.prepare_content("application/json")
83         http.write(jsonrpc.handle(uci, http.content()))
84 end
85
86 function rpc_fs()
87         local fs      = require "luci.fs"
88         local jsonrpc = require "luci.jsonrpc"
89         local http    = require "luci.http"
90         
91         http.setfilehandler()
92         http.prepare_content("application/json")
93         http.write(jsonrpc.handle(fs, http.content()))
94 end
95
96 function rpc_sys()
97         local sys     = require "luci.sys"
98         local jsonrpc = require "luci.jsonrpc"
99         local http    = require "luci.http"
100         
101         http.setfilehandler()
102         http.prepare_content("application/json")
103         http.write(jsonrpc.handle(sys, http.content()))
104 end