Fixed RPC-API
[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         sys = entry({"rpc", "sys"}, call("rpc_sys"))
45         sys.sysauth = "root"
46         sys.sysauth_authenticator = authenticator
47
48         ipkg = entry({"rpc", "ipkg"}, call("rpc_ipkg"))
49         ipkg.sysauth = "root"
50         ipkg.sysauth_authenticator = authenticator
51         
52         uci = entry({"rpc", "auth"}, call("rpc_auth"))
53 end
54
55 function rpc_auth()
56         local jsonrpc = require "luci.jsonrpc"
57         local sauth   = require "luci.sauth"
58         local http    = require "luci.http"
59         local sys     = require "luci.sys"
60         local ltn12   = require "luci.ltn12"
61         
62         http.setfilehandler()
63         
64         local loginstat
65         
66         local server = {}
67         server.login = function(user, pass)
68                 local sid
69                 
70                 if sys.user.checkpasswd(user, pass) then
71                         sid = sys.uniqueid(16)
72                         http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
73                         sauth.write(sid, user)
74                 end
75                 
76                 return sid
77         end
78         
79         http.prepare_content("application/json")
80         ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
81 end
82
83 function rpc_uci()
84         if not pcall(require, "luci.model.uci") then
85                 luci.http.status(404, "Not Found")
86                 return nil
87         end
88         local uci     = require "luci.controller.rpc.uci"
89         local jsonrpc = require "luci.jsonrpc"
90         local http    = require "luci.http"
91         local ltn12   = require "luci.ltn12"
92         
93         http.prepare_content("application/json")
94         ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
95 end
96
97 function rpc_fs()
98         local util    = require "luci.util"
99         local io      = require "io"
100         local fs2     = util.clone(require "luci.fs")
101         local jsonrpc = require "luci.jsonrpc"
102         local http    = require "luci.http"
103         local ltn12   = require "luci.ltn12"
104
105         function fs2.readfile(filename)
106                 local stat, mime = pcall(require, "mime")
107                 if not stat then
108                         error("Base64 support not available. Please install LuaSocket.")
109                 end
110
111                 local fp = io.open(filename)
112                 if not fp then
113                         return nil
114                 end
115
116                 local output = {}
117                 local sink = ltn12.sink.table(output)
118                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
119                 return ltn12.pump.all(source, sink) and table.concat(output)
120         end
121         
122         function fs2.writefile(filename, data)
123                 local stat, mime = pcall(require, "mime")
124                 if not stat then
125                         error("Base64 support not available. Please install LuaSocket.")
126                 end
127
128                 local  file = io.open(filename, "w")
129                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
130                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
131         end
132         
133         http.prepare_content("application/json")
134         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
135 end
136
137 function rpc_sys()
138         local sys     = require "luci.sys"
139         local jsonrpc = require "luci.jsonrpc"
140         local http    = require "luci.http"
141         local ltn12   = require "luci.ltn12"
142         
143         http.prepare_content("application/json")
144         ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
145 end
146
147 function rpc_ipkg()
148         if not pcall(require, "luci.model.ipkg") then
149                 luci.http.status(404, "Not Found")
150                 return nil
151         end
152         local ipkg    = require "luci.model.ipkg"
153         local jsonrpc = require "luci.jsonrpc"
154         local http    = require "luci.http"
155         local ltn12   = require "luci.ltn12"
156
157         http.prepare_content("application/json")
158         ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
159 end