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 local require = require
11 local tonumber = tonumber
13 module "luci.controller.rpc"
16 local function session_retrieve(sid, allowed_users)
17 local util = require "luci.util"
18 local sdat = util.ubus("session", "get", {
19 ubus_rpc_session = sid
22 if type(sdat) == "table" and
23 type(sdat.values) == "table" and
24 type(sdat.values.token) == "string" and
25 type(sdat.values.secret) == "string" and
26 type(sdat.values.username) == "string" and
27 util.contains(allowed_users, sdat.values.username)
29 return sid, sdat.values
35 local function authenticator(validator, accs)
36 local auth = luci.http.formvalue("auth", true)
37 or luci.http.getcookie("sysauth")
39 if auth then -- if authentication token was given
40 local sid, sdat = session_retrieve(auth, accs)
41 if sdat then -- if given token is valid
42 return sdat.username, sid
44 luci.http.status(403, "Forbidden")
49 local rpc = node("rpc")
51 rpc.sysauth_authenticator = authenticator
54 entry({"rpc", "uci"}, call("rpc_uci"))
55 entry({"rpc", "fs"}, call("rpc_fs"))
56 entry({"rpc", "sys"}, call("rpc_sys"))
57 entry({"rpc", "ipkg"}, call("rpc_ipkg"))
58 entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
62 local jsonrpc = require "luci.jsonrpc"
63 local http = require "luci.http"
64 local sys = require "luci.sys"
65 local ltn12 = require "luci.ltn12"
66 local util = require "luci.util"
69 server.challenge = function(user, pass)
70 local config = require "luci.config"
71 local login = util.ubus("session", "login", {
74 timeout = tonumber(config.sauth.sessiontime)
77 if type(login) == "table" and
78 type(login.ubus_rpc_session) == "string"
80 util.ubus("session", "set", {
81 ubus_rpc_session = login.ubus_rpc_session,
83 token = sys.uniqueid(16),
84 secret = sys.uniqueid(16)
88 local sid, sdat = session_retrieve(login.ubus_rpc_session, { user })
101 server.login = function(...)
102 local challenge = server.challenge(...)
104 http.header("Set-Cookie", 'sysauth=%s; path=%s' %{
106 http.getenv("SCRIPT_NAME")
112 http.prepare_content("application/json")
113 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
117 if not pcall(require, "luci.model.uci") then
118 luci.http.status(404, "Not Found")
121 local uci = require "luci.jsonrpcbind.uci"
122 local jsonrpc = require "luci.jsonrpc"
123 local http = require "luci.http"
124 local ltn12 = require "luci.ltn12"
126 http.prepare_content("application/json")
127 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
131 local util = require "luci.util"
132 local io = require "io"
133 local fs2 = util.clone(require "nixio.fs")
134 local jsonrpc = require "luci.jsonrpc"
135 local http = require "luci.http"
136 local ltn12 = require "luci.ltn12"
138 function fs2.readfile(filename)
139 local stat, mime = pcall(require, "mime")
141 error("Base64 support not available. Please install LuaSocket.")
144 local fp = io.open(filename)
150 local sink = ltn12.sink.table(output)
151 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
152 return ltn12.pump.all(source, sink) and table.concat(output)
155 function fs2.writefile(filename, data)
156 local stat, mime = pcall(require, "mime")
158 error("Base64 support not available. Please install LuaSocket.")
161 local file = io.open(filename, "w")
162 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
163 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
166 http.prepare_content("application/json")
167 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
171 local sys = require "luci.sys"
172 local jsonrpc = require "luci.jsonrpc"
173 local http = require "luci.http"
174 local ltn12 = require "luci.ltn12"
176 http.prepare_content("application/json")
177 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
181 if not pcall(require, "luci.model.ipkg") then
182 luci.http.status(404, "Not Found")
185 local ipkg = require "luci.model.ipkg"
186 local jsonrpc = require "luci.jsonrpc"
187 local http = require "luci.http"
188 local ltn12 = require "luci.ltn12"
190 http.prepare_content("application/json")
191 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)