2008-08-22 17:18:36 +00:00
|
|
|
--[[
|
|
|
|
LuCI - Lua Configuration Interface
|
|
|
|
|
|
|
|
Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
]]--
|
2008-08-26 00:53:28 +00:00
|
|
|
|
|
|
|
local require = require
|
|
|
|
local pairs = pairs
|
|
|
|
local print = print
|
2008-08-29 12:27:54 +00:00
|
|
|
local pcall = pcall
|
|
|
|
local table = table
|
2008-08-26 00:53:28 +00:00
|
|
|
|
|
|
|
module "luci.controller.rpc"
|
2008-08-22 17:18:36 +00:00
|
|
|
|
2008-08-22 20:04:04 +00:00
|
|
|
function index()
|
2008-08-22 22:13:54 +00:00
|
|
|
local function authenticator(validator, accs)
|
2008-08-26 00:53:28 +00:00
|
|
|
local auth = luci.http.formvalue("auth", true)
|
2012-08-07 19:11:52 +00:00
|
|
|
if auth then -- if authentication token was given
|
2015-01-15 09:55:53 +00:00
|
|
|
local sdat = (luci.util.ubus("session", "get", { ubus_rpc_session = auth }) or { }).values
|
2012-08-07 19:11:52 +00:00
|
|
|
if sdat then -- if given token is valid
|
2012-08-08 09:48:53 +00:00
|
|
|
if sdat.user and luci.util.contains(accs, sdat.user) then
|
|
|
|
return sdat.user, auth
|
2012-08-07 19:11:52 +00:00
|
|
|
end
|
2008-08-22 22:13:54 +00:00
|
|
|
end
|
2008-08-22 20:04:04 +00:00
|
|
|
end
|
2008-08-22 22:13:54 +00:00
|
|
|
luci.http.status(403, "Forbidden")
|
2008-08-22 20:04:04 +00:00
|
|
|
end
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-09-05 15:38:53 +00:00
|
|
|
local rpc = node("rpc")
|
|
|
|
rpc.sysauth = "root"
|
|
|
|
rpc.sysauth_authenticator = authenticator
|
|
|
|
rpc.notemplate = true
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-09-05 15:38:53 +00:00
|
|
|
entry({"rpc", "uci"}, call("rpc_uci"))
|
|
|
|
entry({"rpc", "fs"}, call("rpc_fs"))
|
|
|
|
entry({"rpc", "sys"}, call("rpc_sys"))
|
|
|
|
entry({"rpc", "ipkg"}, call("rpc_ipkg"))
|
|
|
|
entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
|
2008-08-22 22:13:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function rpc_auth()
|
2008-08-26 00:53:28 +00:00
|
|
|
local jsonrpc = require "luci.jsonrpc"
|
|
|
|
local http = require "luci.http"
|
|
|
|
local sys = require "luci.sys"
|
2008-08-26 17:50:32 +00:00
|
|
|
local ltn12 = require "luci.ltn12"
|
2008-12-14 21:43:10 +00:00
|
|
|
local util = require "luci.util"
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-08-22 22:13:54 +00:00
|
|
|
local loginstat
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-08-22 22:13:54 +00:00
|
|
|
local server = {}
|
2008-12-14 21:43:10 +00:00
|
|
|
server.challenge = function(user, pass)
|
|
|
|
local sid, token, secret
|
|
|
|
|
2008-08-26 00:53:28 +00:00
|
|
|
if sys.user.checkpasswd(user, pass) then
|
2015-01-15 09:55:53 +00:00
|
|
|
local sdat = util.ubus("session", "create", { timeout = luci.config.sauth.sessiontime })
|
|
|
|
if sdat then
|
|
|
|
sid = sdat.ubus_rpc_session
|
|
|
|
token = sys.uniqueid(16)
|
|
|
|
secret = sys.uniqueid(16)
|
|
|
|
|
|
|
|
http.header("Set-Cookie", "sysauth="..sid.."; path=/")
|
|
|
|
util.ubus("session", "set", {
|
|
|
|
ubus_rpc_session = sid,
|
|
|
|
values = {
|
|
|
|
user = user,
|
|
|
|
token = token,
|
|
|
|
secret = secret
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
2008-08-22 22:13:54 +00:00
|
|
|
end
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-12-14 21:43:10 +00:00
|
|
|
return sid and {sid=sid, token=token, secret=secret}
|
|
|
|
end
|
|
|
|
|
|
|
|
server.login = function(...)
|
|
|
|
local challenge = server.challenge(...)
|
|
|
|
return challenge and challenge.sid
|
2008-08-22 22:13:54 +00:00
|
|
|
end
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-08-26 00:53:28 +00:00
|
|
|
http.prepare_content("application/json")
|
2008-08-26 17:50:32 +00:00
|
|
|
ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
|
2008-08-22 17:18:36 +00:00
|
|
|
end
|
|
|
|
|
2008-08-22 20:04:04 +00:00
|
|
|
function rpc_uci()
|
2008-08-29 17:12:35 +00:00
|
|
|
if not pcall(require, "luci.model.uci") then
|
|
|
|
luci.http.status(404, "Not Found")
|
|
|
|
return nil
|
|
|
|
end
|
2008-09-05 19:25:57 +00:00
|
|
|
local uci = require "luci.jsonrpcbind.uci"
|
2008-08-26 00:53:28 +00:00
|
|
|
local jsonrpc = require "luci.jsonrpc"
|
|
|
|
local http = require "luci.http"
|
2008-08-26 17:50:32 +00:00
|
|
|
local ltn12 = require "luci.ltn12"
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-08-26 00:53:28 +00:00
|
|
|
http.prepare_content("application/json")
|
2008-08-26 17:50:32 +00:00
|
|
|
ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
|
2008-08-26 00:53:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function rpc_fs()
|
2008-08-26 17:50:32 +00:00
|
|
|
local util = require "luci.util"
|
2008-08-29 12:27:54 +00:00
|
|
|
local io = require "io"
|
2009-07-19 00:24:58 +00:00
|
|
|
local fs2 = util.clone(require "nixio.fs")
|
2008-08-26 00:53:28 +00:00
|
|
|
local jsonrpc = require "luci.jsonrpc"
|
|
|
|
local http = require "luci.http"
|
2008-08-26 17:50:32 +00:00
|
|
|
local ltn12 = require "luci.ltn12"
|
2008-08-29 12:27:54 +00:00
|
|
|
|
|
|
|
function fs2.readfile(filename)
|
|
|
|
local stat, mime = pcall(require, "mime")
|
|
|
|
if not stat then
|
2008-08-26 17:50:32 +00:00
|
|
|
error("Base64 support not available. Please install LuaSocket.")
|
|
|
|
end
|
2008-08-29 12:27:54 +00:00
|
|
|
|
|
|
|
local fp = io.open(filename)
|
|
|
|
if not fp then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local output = {}
|
|
|
|
local sink = ltn12.sink.table(output)
|
|
|
|
local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
|
|
|
|
return ltn12.pump.all(source, sink) and table.concat(output)
|
2008-08-26 17:50:32 +00:00
|
|
|
end
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-08-29 12:27:54 +00:00
|
|
|
function fs2.writefile(filename, data)
|
|
|
|
local stat, mime = pcall(require, "mime")
|
|
|
|
if not stat then
|
2008-08-26 17:50:32 +00:00
|
|
|
error("Base64 support not available. Please install LuaSocket.")
|
|
|
|
end
|
2008-08-29 12:27:54 +00:00
|
|
|
|
|
|
|
local file = io.open(filename, "w")
|
|
|
|
local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
|
|
|
|
return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
|
2008-08-26 17:50:32 +00:00
|
|
|
end
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-08-26 00:53:28 +00:00
|
|
|
http.prepare_content("application/json")
|
2008-08-29 12:27:54 +00:00
|
|
|
ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
|
2008-08-26 00:53:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function rpc_sys()
|
|
|
|
local sys = require "luci.sys"
|
|
|
|
local jsonrpc = require "luci.jsonrpc"
|
|
|
|
local http = require "luci.http"
|
2008-08-26 17:50:32 +00:00
|
|
|
local ltn12 = require "luci.ltn12"
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-08-26 00:53:28 +00:00
|
|
|
http.prepare_content("application/json")
|
2008-08-26 17:50:32 +00:00
|
|
|
ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
|
2008-08-29 12:27:54 +00:00
|
|
|
end
|
2008-08-29 15:54:13 +00:00
|
|
|
|
|
|
|
function rpc_ipkg()
|
2008-08-29 17:12:35 +00:00
|
|
|
if not pcall(require, "luci.model.ipkg") then
|
|
|
|
luci.http.status(404, "Not Found")
|
|
|
|
return nil
|
|
|
|
end
|
2008-08-29 15:54:13 +00:00
|
|
|
local ipkg = require "luci.model.ipkg"
|
|
|
|
local jsonrpc = require "luci.jsonrpc"
|
|
|
|
local http = require "luci.http"
|
|
|
|
local ltn12 = require "luci.ltn12"
|
|
|
|
|
|
|
|
http.prepare_content("application/json")
|
|
|
|
ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
|
|
|
|
end
|