98fafe3f1bb6c6eda2f9b9799e39281c3601ea8d
[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 local ltn12 = require "luci.ltn12"
55
56 http.setfilehandler()
57
58 local loginstat
59
60 local server = {}
61 server.login = function(user, pass)
62 local sid
63
64 if sys.user.checkpasswd(user, pass) then
65 sid = sys.uniqueid(16)
66 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
67 sauth.write(sid, user)
68 end
69
70 return sid
71 end
72
73 http.prepare_content("application/json")
74 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
75 end
76
77 function rpc_uci()
78 local uci = require "luci.controller.rpc.uci"
79 local jsonrpc = require "luci.jsonrpc"
80 local http = require "luci.http"
81 local ltn12 = require "luci.ltn12"
82
83 http.prepare_content("application/json")
84 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
85 end
86
87 function rpc_fs()
88 local util = require "luci.util"
89 local fs = util.clone(require "luci.fs")
90 local jsonrpc = require "luci.jsonrpc"
91 local http = require "luci.http"
92 local ltn12 = require "luci.ltn12"
93
94 function fs.readfile(filename)
95 if not pcall(require, "mime") then
96 error("Base64 support not available. Please install LuaSocket.")
97 end
98
99 return ltn12.source.chain(ltn12.source.file(filename), mime.encode("base64"))
100 end
101
102 function fs.writefile(filename, data)
103 if not pcall(require, "mime") then
104 error("Base64 support not available. Please install LuaSocket.")
105 end
106
107 local sink = ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(filename))
108 return ltn12.pump.all(ltn12.source.string(data), sink)
109 end
110
111 http.prepare_content("application/json")
112 ltn12.pump.all(jsonrpc.handle(fs, http.source()), http.write)
113 end
114
115 function rpc_sys()
116 local sys = require "luci.sys"
117 local jsonrpc = require "luci.jsonrpc"
118 local http = require "luci.http"
119 local ltn12 = require "luci.ltn12"
120
121 http.prepare_content("application/json")
122 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
123 end