diff options
author | Steven Barth <steven@midlink.org> | 2008-08-26 00:53:28 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2008-08-26 00:53:28 +0000 |
commit | 0c5dc7bc77a4fd7d7f0d965e3185e2df4c608f05 (patch) | |
tree | b6a3a5fd860f43a171ebdb4446974489f4dbbb0b /modules/rpc | |
parent | 8e6d1e682afee81fb4199ad494195c23e4381af5 (diff) |
libs/core: Reworked some basic libraries to not use package.seeall
libs/json: Implemented own JSON-Decoder (Encoder will follow)
modules/rpc: Preliminary implemented RPC-Exports for luci.fs, luci.sys and luci.model.uci
Diffstat (limited to 'modules/rpc')
-rw-r--r-- | modules/rpc/luasrc/controller/rpc.lua | 72 | ||||
-rw-r--r-- | modules/rpc/luasrc/controller/rpc/uci.lua | 93 | ||||
-rw-r--r-- | modules/rpc/luasrc/jsonrpc.lua | 4 |
3 files changed, 150 insertions, 19 deletions
diff --git a/modules/rpc/luasrc/controller/rpc.lua b/modules/rpc/luasrc/controller/rpc.lua index a004d0dd9..aa77a8f24 100644 --- a/modules/rpc/luasrc/controller/rpc.lua +++ b/modules/rpc/luasrc/controller/rpc.lua @@ -12,15 +12,20 @@ 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 + +module "luci.controller.rpc" function index() local function authenticator(validator, accs) - local args = luci.dispatcher.context.args - if args and #args > 0 then - local user = luci.sauth.read(args[1]) + 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 + return user, auth end end luci.http.status(403, "Forbidden") @@ -29,16 +34,25 @@ function index() uci = entry({"rpc", "uci"}, call("rpc_uci")) uci.sysauth = "root" uci.sysauth_authenticator = authenticator - uci.leaf = true + + 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")) end function rpc_auth() - require "luci.jsonrpc" - require "luci.sauth" + local jsonrpc = require "luci.jsonrpc" + local sauth = require "luci.sauth" + local http = require "luci.http" + local sys = require "luci.sys" - luci.http.setfilehandler() + http.setfilehandler() local loginstat @@ -46,21 +60,45 @@ function rpc_auth() server.login = function(user, pass) local sid - if luci.sys.user.checkpasswd(user, pass) then - sid = luci.sys.uniqueid(16) - luci.http.header("Set-Cookie", "sysauth=" .. sid.."; path=/") - luci.sauth.write(sid, user) + 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 - luci.http.prepare_content("application/json") - luci.http.write(luci.jsonrpc.handle(server, luci.http.content())) - - return loginstat + http.prepare_content("application/json") + http.write(jsonrpc.handle(server, http.content())) end function rpc_uci() + local uci = require "luci.controller.rpc.uci" + local jsonrpc = require "luci.jsonrpc" + local http = require "luci.http" + + http.setfilehandler() + http.prepare_content("application/json") + http.write(jsonrpc.handle(uci, http.content())) +end + +function rpc_fs() + local fs = require "luci.fs" + local jsonrpc = require "luci.jsonrpc" + local http = require "luci.http" + + http.setfilehandler() + http.prepare_content("application/json") + http.write(jsonrpc.handle(fs, http.content())) +end + +function rpc_sys() + local sys = require "luci.sys" + local jsonrpc = require "luci.jsonrpc" + local http = require "luci.http" + http.setfilehandler() + http.prepare_content("application/json") + http.write(jsonrpc.handle(sys, http.content())) end
\ No newline at end of file diff --git a/modules/rpc/luasrc/controller/rpc/uci.lua b/modules/rpc/luasrc/controller/rpc/uci.lua new file mode 100644 index 000000000..2dab4e01a --- /dev/null +++ b/modules/rpc/luasrc/controller/rpc/uci.lua @@ -0,0 +1,93 @@ +--[[ +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$ +]]-- + +local uci = require "luci.model.uci" +local table = require "table" + + +module "luci.controller.rpc.uci" +_M, _PACKAGE, _NAME = nil, nil, nil + +function add(config, ...) + uci.load_config(config) + local stat = uci.add(config, ...) + return uci.save_config(config) and stat +end + +function apply(config) + return uci.apply(config) +end + +function changes(...) + return uci.changes(...) +end + +function commit(config) + return uci.load(config) and uci.commit(config) +end + +function delete(config, ...) + uci.load(config) + return uci.delete(config, ...) and uci.save(config) +end + +function delete_all(config, ...) + uci.load(config) + return uci.delete_all(config, ...) and uci.save(config) +end + +function foreach(config, stype) + uci.load_config(config) + local sections = {} + + return uci.foreach(config, stype, function(section) + table.insert(sections, section) + end) and sections +end + +function get(config, ...) + uci.load_config(config) + return uci.get(config, ...) +end + +function get_all(config, ...) + uci.load_config(config) + return uci.get_all(config, ...) +end + +function get_state(config, ...) + uci.load_state(config) + return uci.get(config, ...) +end + +function revert(config) + return uci.load(config) and uci.revert(config) +end + +function section(config, ...) + uci.load_config(config) + return uci.section(config, ...) and uci.save_config(config) +end + +function set(config, ...) + uci.load_config(config) + return uci.set(config, ...) and uci.save_config(config) +end + +function tset(config, ...) + uci.load_config(config) + return uci.tset(config, ...) and uci.save_config(config) +end + diff --git a/modules/rpc/luasrc/jsonrpc.lua b/modules/rpc/luasrc/jsonrpc.lua index 84a7f7056..c4fed2acc 100644 --- a/modules/rpc/luasrc/jsonrpc.lua +++ b/modules/rpc/luasrc/jsonrpc.lua @@ -23,12 +23,12 @@ function resolve(mod, method) if not type(mod) == "table" then break end - mod = mod[path[j]] + mod = rawget(mod, path[j]) if not mod then break end end - mod = type(mod) == "table" and mod[path[#path]] or nil + mod = type(mod) == "table" and rawget(mod, path[#path]) or nil if type(mod) == "function" then return mod end |