X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=modules%2Frpc%2Fluasrc%2Fcontroller%2Frpc.lua;h=1aeea69b418631d0c7d452a5cc4cb0a5f3d4f176;hp=dd00f63c370de26ac6112b614ee50a8504fd7eb9;hb=ffd5c4ec656bd3f216c66b43587abcfdaf2b5c37;hpb=4eead17501584a09679841d75d451b19a7c234d0 diff --git a/modules/rpc/luasrc/controller/rpc.lua b/modules/rpc/luasrc/controller/rpc.lua index dd00f63c3..1aeea69b4 100644 --- a/modules/rpc/luasrc/controller/rpc.lua +++ b/modules/rpc/luasrc/controller/rpc.lua @@ -12,33 +12,152 @@ You may obtain a copy of the License at $Id$ ]]-- -module("luci.controller.rpc", package.seeall) + +local require = require +local pairs = pairs +local print = print +local pcall = pcall +local table = table + +module "luci.controller.rpc" function index() - local authenticator = function(validator) - require "luci.jsonrpc" - require "luci.http" - luci.http.setfilehandler() - - local loginstat - - local server = {} - server.login = function(...) - loginstat = validator(...) - return loginstat + local function authenticator(validator, accs) + local auth = luci.http.formvalue("auth", true) + if auth then + local user = luci.sauth.read(auth) + if user and luci.util.contains(accs, user) then + return user, auth + end end - - luci.http.prepare_content("application/json") - luci.http.write(luci.jsonrpc.handle(server, luci.http.content())) - - return loginstat + luci.http.status(403, "Forbidden") end uci = entry({"rpc", "uci"}, call("rpc_uci")) uci.sysauth = "root" uci.sysauth_authenticator = authenticator + uci.notemplate = true + + fs = entry({"rpc", "fs"}, call("rpc_fs")) + fs.sysauth = "root" + fs.sysauth_authenticator = authenticator + fs.notemplate = true + + sys = entry({"rpc", "sys"}, call("rpc_sys")) + sys.sysauth = "root" + sys.sysauth_authenticator = authenticator + sys.notemplate = true + + ipkg = entry({"rpc", "ipkg"}, call("rpc_ipkg")) + ipkg.sysauth = "root" + ipkg.sysauth_authenticator = authenticator + ipkg.notemplate = true + + entry({"rpc", "auth"}, call("rpc_auth")).notemplate = true +end + +function rpc_auth() + local jsonrpc = require "luci.jsonrpc" + local sauth = require "luci.sauth" + local http = require "luci.http" + local sys = require "luci.sys" + local ltn12 = require "luci.ltn12" + + http.setfilehandler() + + local loginstat + + local server = {} + server.login = function(user, pass) + local sid + + if sys.user.checkpasswd(user, pass) then + sid = sys.uniqueid(16) + http.header("Set-Cookie", "sysauth=" .. sid.."; path=/") + sauth.write(sid, user) + end + + return sid + end + + http.prepare_content("application/json") + ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write) end function rpc_uci() - luci.http.write("HELLO THAR!") -end \ No newline at end of file + if not pcall(require, "luci.model.uci") then + luci.http.status(404, "Not Found") + return nil + end + local uci = require "luci.controller.rpc.uci" + 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(uci, http.source()), http.write) +end + +function rpc_fs() + local util = require "luci.util" + local io = require "io" + local fs2 = util.clone(require "luci.fs") + local jsonrpc = require "luci.jsonrpc" + local http = require "luci.http" + local ltn12 = require "luci.ltn12" + + function fs2.readfile(filename) + local stat, mime = pcall(require, "mime") + if not stat then + error("Base64 support not available. Please install LuaSocket.") + end + + 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) + end + + function fs2.writefile(filename, data) + local stat, mime = pcall(require, "mime") + if not stat then + error("Base64 support not available. Please install LuaSocket.") + end + + 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 + end + + http.prepare_content("application/json") + ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write) +end + +function rpc_sys() + local sys = require "luci.sys" + 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(sys, http.source()), http.write) +end + +function rpc_ipkg() + if not pcall(require, "luci.model.ipkg") then + luci.http.status(404, "Not Found") + return nil + end + 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