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.
5 module("luci.controller.rpc", package.seeall)
9 local function session_retrieve(sid, allowed_users)
10 local util = require "luci.util"
11 local sdat = util.ubus("session", "get", {
12 ubus_rpc_session = sid
15 if type(sdat) == "table" and
16 type(sdat.values) == "table" and
17 type(sdat.values.token) == "string" and
18 type(sdat.values.secret) == "string" and
19 type(sdat.values.username) == "string" and
20 util.contains(allowed_users, sdat.values.username)
22 return sid, sdat.values
28 local function authenticator(validator, accs)
29 local http = require "luci.http"
30 local auth = http.formvalue("auth", true) or http.getcookie("sysauth")
32 if auth then -- if authentication token was given
33 local sid, sdat = session_retrieve(auth, accs)
34 if sdat then -- if given token is valid
35 return sdat.username, sid
37 http.status(403, "Forbidden")
41 local rpc = node("rpc")
43 rpc.sysauth_authenticator = authenticator
46 entry({"rpc", "uci"}, call("rpc_uci"))
47 entry({"rpc", "fs"}, call("rpc_fs"))
48 entry({"rpc", "sys"}, call("rpc_sys"))
49 entry({"rpc", "ipkg"}, call("rpc_ipkg"))
50 entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
54 local jsonrpc = require "luci.jsonrpc"
55 local http = require "luci.http"
56 local sys = require "luci.sys"
57 local ltn12 = require "luci.ltn12"
58 local util = require "luci.util"
61 server.challenge = function(user, pass)
62 local config = require "luci.config"
63 local login = util.ubus("session", "login", {
66 timeout = tonumber(config.sauth.sessiontime)
69 if type(login) == "table" and
70 type(login.ubus_rpc_session) == "string"
72 util.ubus("session", "set", {
73 ubus_rpc_session = login.ubus_rpc_session,
75 token = sys.uniqueid(16),
76 secret = sys.uniqueid(16)
80 local sid, sdat = session_retrieve(login.ubus_rpc_session, { user })
93 server.login = function(...)
94 local challenge = server.challenge(...)
96 http.header("Set-Cookie", 'sysauth=%s; path=%s' %{
98 http.getenv("SCRIPT_NAME")
104 http.prepare_content("application/json")
105 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
109 if not pcall(require, "luci.model.uci") then
110 luci.http.status(404, "Not Found")
113 local uci = require "luci.jsonrpcbind.uci"
114 local jsonrpc = require "luci.jsonrpc"
115 local http = require "luci.http"
116 local ltn12 = require "luci.ltn12"
118 http.prepare_content("application/json")
119 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
123 local util = require "luci.util"
124 local io = require "io"
125 local fs2 = util.clone(require "nixio.fs")
126 local jsonrpc = require "luci.jsonrpc"
127 local http = require "luci.http"
128 local ltn12 = require "luci.ltn12"
130 function fs2.readfile(filename)
131 local stat, mime = pcall(require, "mime")
133 error("Base64 support not available. Please install LuaSocket.")
136 local fp = io.open(filename)
142 local sink = ltn12.sink.table(output)
143 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
144 return ltn12.pump.all(source, sink) and table.concat(output)
147 function fs2.writefile(filename, data)
148 local stat, mime = pcall(require, "mime")
150 error("Base64 support not available. Please install LuaSocket.")
153 local file = io.open(filename, "w")
154 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
155 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
158 http.prepare_content("application/json")
159 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
163 local sys = require "luci.sys"
164 local jsonrpc = require "luci.jsonrpc"
165 local http = require "luci.http"
166 local ltn12 = require "luci.ltn12"
168 http.prepare_content("application/json")
169 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
173 if not pcall(require, "luci.model.ipkg") then
174 luci.http.status(404, "Not Found")
177 local ipkg = require "luci.model.ipkg"
178 local jsonrpc = require "luci.jsonrpc"
179 local http = require "luci.http"
180 local ltn12 = require "luci.ltn12"
182 http.prepare_content("application/json")
183 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)