0fcd263f5dad3767cf20c072170957bf5295b235
[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 local pcall = pcall
20 local table = table
21
22 module "luci.controller.rpc"
23
24 function index()
25         local function authenticator(validator, accs)
26                 local auth = luci.http.formvalue("auth", true)
27                 if auth then
28                         local user = luci.sauth.read(auth)
29                         if user and luci.util.contains(accs, user) then
30                                 return user, auth
31                         end
32                 end
33                 luci.http.status(403, "Forbidden")
34         end
35         
36         uci = entry({"rpc", "uci"}, call("rpc_uci"))
37         uci.sysauth = "root"
38         uci.sysauth_authenticator = authenticator
39         
40         fs = entry({"rpc", "fs"}, call("rpc_fs"))
41         fs.sysauth = "root"
42         fs.sysauth_authenticator = authenticator
43
44         fs = entry({"rpc", "sys"}, call("rpc_sys"))
45         fs.sysauth = "root"
46         fs.sysauth_authenticator = authenticator
47         
48         uci = entry({"rpc", "auth"}, call("rpc_auth"))
49 end
50
51 function rpc_auth()
52         local jsonrpc = require "luci.jsonrpc"
53         local sauth   = require "luci.sauth"
54         local http    = require "luci.http"
55         local sys     = require "luci.sys"
56         local ltn12   = require "luci.ltn12"
57         
58         http.setfilehandler()
59         
60         local loginstat
61         
62         local server = {}
63         server.login = function(user, pass)
64                 local sid
65                 
66                 if sys.user.checkpasswd(user, pass) then
67                         sid = sys.uniqueid(16)
68                         http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
69                         sauth.write(sid, user)
70                 end
71                 
72                 return sid
73         end
74         
75         http.prepare_content("application/json")
76         ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
77 end
78
79 function rpc_uci()
80         local uci     = require "luci.controller.rpc.uci"
81         local jsonrpc = require "luci.jsonrpc"
82         local http    = require "luci.http"
83         local ltn12   = require "luci.ltn12"
84         
85         http.prepare_content("application/json")
86         ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
87 end
88
89 function rpc_fs()
90         local util    = require "luci.util"
91         local io      = require "io"
92         local fs2     = util.clone(require "luci.fs")
93         local jsonrpc = require "luci.jsonrpc"
94         local http    = require "luci.http"
95         local ltn12   = require "luci.ltn12"
96
97         function fs2.readfile(filename)
98                 local stat, mime = pcall(require, "mime")
99                 if not stat then
100                         error("Base64 support not available. Please install LuaSocket.")
101                 end
102
103                 local fp = io.open(filename)
104                 if not fp then
105                         return nil
106                 end
107
108                 local output = {}
109                 local sink = ltn12.sink.table(output)
110                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
111                 return ltn12.pump.all(source, sink) and table.concat(output)
112         end
113         
114         function fs2.writefile(filename, data)
115                 local stat, mime = pcall(require, "mime")
116                 if not stat then
117                         error("Base64 support not available. Please install LuaSocket.")
118                 end
119
120                 local  file = io.open(filename, "w")
121                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
122                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
123         end
124         
125         http.prepare_content("application/json")
126         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
127 end
128
129 function rpc_sys()
130         local sys     = require "luci.sys"
131         local jsonrpc = require "luci.jsonrpc"
132         local http    = require "luci.http"
133         local ltn12   = require "luci.ltn12"
134         
135         http.prepare_content("application/json")
136         ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
137 end