5df5e375a7c2b06b2a30c07c66f5b1f48cb8a4ce
[project/luci.git] / modules / luci-mod-rpc / luasrc / controller / rpc.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.rpc", package.seeall)
6
7
8 function session_retrieve(sid, allowed_users)
9         local util = require "luci.util"
10         local sdat = util.ubus("session", "get", {
11                 ubus_rpc_session = sid
12         })
13
14         if type(sdat) == "table" and
15            type(sdat.values) == "table" and
16            type(sdat.values.token) == "string" and
17            type(sdat.values.secret) == "string" and
18            type(sdat.values.username) == "string" and
19            util.contains(allowed_users, sdat.values.username)
20         then
21                 return sid, sdat.values
22         end
23
24         return nil
25 end
26
27 function authenticator(validator, accs)
28         local http = require "luci.http"
29         local ctrl = require "luci.controller.rpc"
30         local auth = http.formvalue("auth", true) or http.getcookie("sysauth")
31
32         if auth then -- if authentication token was given
33                 local sid, sdat = ctrl.session_retrieve(auth, accs)
34                 if sdat then -- if given token is valid
35                         return sdat.username, sid
36                 end
37                 http.status(403, "Forbidden")
38         end
39 end
40
41
42 function index()
43         local ctrl = require "luci.controller.rpc"
44
45         local rpc = node("rpc")
46         rpc.sysauth = "root"
47         rpc.sysauth_authenticator = ctrl.authenticator
48         rpc.notemplate = true
49
50         entry({"rpc", "uci"}, call("rpc_uci"))
51         entry({"rpc", "fs"}, call("rpc_fs"))
52         entry({"rpc", "sys"}, call("rpc_sys"))
53         entry({"rpc", "ipkg"}, call("rpc_ipkg"))
54         entry({"rpc", "ip"}, call("rpc_ip"))
55         entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
56 end
57
58 function rpc_auth()
59         local jsonrpc = require "luci.jsonrpc"
60         local http    = require "luci.http"
61         local sys     = require "luci.sys"
62         local ltn12   = require "luci.ltn12"
63         local util    = require "luci.util"
64
65         local server = {}
66         server.challenge = function(user, pass)
67                 local config = require "luci.config"
68                 local login = util.ubus("session", "login", {
69                         username = user,
70                         password = pass,
71                         timeout  = tonumber(config.sauth.sessiontime)
72                 })
73
74                 if type(login) == "table" and
75                    type(login.ubus_rpc_session) == "string"
76                 then
77                         util.ubus("session", "set", {
78                                 ubus_rpc_session = login.ubus_rpc_session,
79                                 values = {
80                                         token = sys.uniqueid(16),
81                                         secret = sys.uniqueid(16)
82                                 }
83                         })
84
85                         local sid, sdat = ctrl.session_retrieve(login.ubus_rpc_session, { user })
86                         if sdat then
87                                 return {
88                                         sid = sid,
89                                         token = sdat.token,
90                                         secret = sdat.secret
91                                 }
92                         end
93                 end
94
95                 return nil
96         end
97
98         server.login = function(...)
99                 local challenge = server.challenge(...)
100                 if challenge then
101                         http.header("Set-Cookie", 'sysauth=%s; path=%s' %{
102                                 challenge.sid,
103                                 http.getenv("SCRIPT_NAME")
104                         })
105                         return challenge.sid
106                 end
107         end
108
109         http.prepare_content("application/json")
110         ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
111 end
112
113 function rpc_uci()
114         if not pcall(require, "luci.model.uci") then
115                 luci.http.status(404, "Not Found")
116                 return nil
117         end
118         local uci     = require "luci.jsonrpcbind.uci"
119         local jsonrpc = require "luci.jsonrpc"
120         local http    = require "luci.http"
121         local ltn12   = require "luci.ltn12"
122
123         http.prepare_content("application/json")
124         ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
125 end
126
127 function rpc_fs()
128         local util    = require "luci.util"
129         local io      = require "io"
130         local fs2     = util.clone(require "nixio.fs")
131         local jsonrpc = require "luci.jsonrpc"
132         local http    = require "luci.http"
133         local ltn12   = require "luci.ltn12"
134
135         function fs2.readfile(filename)
136                 local stat, mime = pcall(require, "mime")
137                 if not stat then
138                         error("Base64 support not available. Please install LuaSocket.")
139                 end
140
141                 local fp = io.open(filename)
142                 if not fp then
143                         return nil
144                 end
145
146                 local output = {}
147                 local sink = ltn12.sink.table(output)
148                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
149                 return ltn12.pump.all(source, sink) and table.concat(output)
150         end
151
152         function fs2.writefile(filename, data)
153                 local stat, mime = pcall(require, "mime")
154                 if not stat then
155                         error("Base64 support not available. Please install LuaSocket.")
156                 end
157
158                 local  file = io.open(filename, "w")
159                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
160                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
161         end
162
163         http.prepare_content("application/json")
164         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
165 end
166
167 function rpc_sys()
168         local util    = require "luci.util"
169         local sys     = require "luci.sys"
170         local jsonrpc = require "luci.jsonrpc"
171         local http    = require "luci.http"
172         local ltn12   = require "luci.ltn12"
173
174         local sys2 = util.clone(sys)
175               sys2.net = util.clone(sys.net)
176               sys2.wifi = util.clone(sys.wifi)
177
178         function sys2.wifi.getiwinfo(ifname, operation)
179                 local iw = sys.wifi.getiwinfo(ifname)
180                 if iw then
181                         if operation then
182                                 assert(type(iwinfo[iw.type][operation]) == "function")
183                                 return iw[operation]
184                         end
185
186                         local n, f
187                         local rv = { ifname = ifname }
188                         for n, f in pairs(iwinfo[iw.type]) do
189                                 if type(f) == "function" and
190                                    n ~= "scanlist" and n ~= "countrylist"
191                                 then
192                                         rv[n] = iw[n]
193                                 end
194                         end
195                         return rv
196                 end
197                 return nil
198         end
199
200         http.prepare_content("application/json")
201         ltn12.pump.all(jsonrpc.handle(sys2, http.source()), http.write)
202 end
203
204 function rpc_ipkg()
205         if not pcall(require, "luci.model.ipkg") then
206                 luci.http.status(404, "Not Found")
207                 return nil
208         end
209         local ipkg    = require "luci.model.ipkg"
210         local jsonrpc = require "luci.jsonrpc"
211         local http    = require "luci.http"
212         local ltn12   = require "luci.ltn12"
213
214         http.prepare_content("application/json")
215         ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
216 end
217
218 function rpc_ip()
219         if not pcall(require, "luci.ip") then
220                 luci.http.status(404, "Not Found")
221                 return nil
222         end
223
224         local util    = require "luci.util"
225         local ip      = require "luci.ip"
226         local jsonrpc = require "luci.jsonrpc"
227         local http    = require "luci.http"
228         local ltn12   = require "luci.ltn12"
229
230         local ip2 = util.clone(ip)
231
232         local _, n
233         for _, n in ipairs({ "new", "IPv4", "IPv6", "MAC" }) do
234                 ip2[n] = function(address, netmask, operation, argument)
235                         local cidr = ip[n](address, netmask)
236                         if cidr and operation then
237                                 assert(type(cidr[operation]) == "function")
238                                 local cidr2 = cidr[operation](cidr, argument)
239                                 return (type(cidr2) == "userdata") and cidr2:string() or cidr2
240                         end
241                         return (type(cidr) == "userdata") and cidr:string() or cidr
242                 end
243         end
244
245         http.prepare_content("application/json")
246         ltn12.pump.all(jsonrpc.handle(ip2, http.source()), http.write)
247 end