X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=modules%2Frpc%2Fluasrc%2Fcontroller%2Frpc.lua;h=359184c744c11e1e6eb992fcb6acd5cc3b135c36;hb=9edd0e46c3f880727738ce8ca6ff1c8b85f99ef4;hp=0fcd263f5dad3767cf20c072170957bf5295b235;hpb=bda994c32e8afda1cac2e0e4ae4c66c50d82c3f1;p=project%2Fluci.git diff --git a/modules/rpc/luasrc/controller/rpc.lua b/modules/rpc/luasrc/controller/rpc.lua index 0fcd263f5..359184c74 100644 --- a/modules/rpc/luasrc/controller/rpc.lua +++ b/modules/rpc/luasrc/controller/rpc.lua @@ -24,28 +24,27 @@ module "luci.controller.rpc" function index() 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 + if auth then -- if authentication token was given + local sdat = luci.sauth.read(auth) + if sdat then -- if given token is valid + if sdat.user and luci.util.contains(accs, sdat.user) then + return sdat.user, auth + end end end luci.http.status(403, "Forbidden") end - - uci = entry({"rpc", "uci"}, call("rpc_uci")) - uci.sysauth = "root" - uci.sysauth_authenticator = authenticator - - fs = entry({"rpc", "fs"}, call("rpc_fs")) - fs.sysauth = "root" - fs.sysauth_authenticator = authenticator - - fs = entry({"rpc", "sys"}, call("rpc_sys")) - fs.sysauth = "root" - fs.sysauth_authenticator = authenticator - - uci = entry({"rpc", "auth"}, call("rpc_auth")) + + local rpc = node("rpc") + rpc.sysauth = "root" + rpc.sysauth_authenticator = authenticator + rpc.notemplate = true + + 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 end function rpc_auth() @@ -54,34 +53,50 @@ function rpc_auth() local http = require "luci.http" local sys = require "luci.sys" local ltn12 = require "luci.ltn12" - - http.setfilehandler() - + local util = require "luci.util" + local loginstat - + local server = {} - server.login = function(user, pass) - local sid - + server.challenge = function(user, pass) + local sid, token, secret + if sys.user.checkpasswd(user, pass) then sid = sys.uniqueid(16) + token = sys.uniqueid(16) + secret = sys.uniqueid(16) + http.header("Set-Cookie", "sysauth=" .. sid.."; path=/") - sauth.write(sid, user) + sauth.reap() + sauth.write(sid, { + user=user, + token=token, + secret=secret + }) end - - return sid + + return sid and {sid=sid, token=token, secret=secret} + end + + server.login = function(...) + local challenge = server.challenge(...) + return challenge and challenge.sid end - + http.prepare_content("application/json") ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write) end function rpc_uci() - local uci = require "luci.controller.rpc.uci" + if not pcall(require, "luci.model.uci") then + luci.http.status(404, "Not Found") + return nil + end + local uci = require "luci.jsonrpcbind.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 @@ -89,7 +104,7 @@ end function rpc_fs() local util = require "luci.util" local io = require "io" - local fs2 = util.clone(require "luci.fs") + local fs2 = util.clone(require "nixio.fs") local jsonrpc = require "luci.jsonrpc" local http = require "luci.http" local ltn12 = require "luci.ltn12" @@ -110,7 +125,7 @@ function rpc_fs() 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 @@ -121,7 +136,7 @@ function rpc_fs() 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 @@ -131,7 +146,21 @@ function rpc_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