2015-01-16 22:38:38 +00:00
|
|
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
|
2015-01-16 22:46:42 +00:00
|
|
|
-- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
|
2015-01-16 22:38:38 +00:00
|
|
|
-- Licensed to the public under the Apache License 2.0.
|
2008-08-26 00:53:28 +00:00
|
|
|
|
2018-05-05 17:51:20 +00:00
|
|
|
module("luci.controller.rpc", package.seeall)
|
2008-08-26 00:53:28 +00:00
|
|
|
|
2008-08-22 17:18:36 +00:00
|
|
|
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
function session_retrieve(sid, allowed_users)
|
|
|
|
local util = require "luci.util"
|
|
|
|
local sdat = util.ubus("session", "get", {
|
|
|
|
ubus_rpc_session = sid
|
|
|
|
})
|
|
|
|
|
|
|
|
if type(sdat) == "table" and
|
|
|
|
type(sdat.values) == "table" and
|
|
|
|
type(sdat.values.token) == "string" and
|
|
|
|
type(sdat.values.username) == "string" and
|
|
|
|
util.contains(allowed_users, sdat.values.username)
|
|
|
|
then
|
|
|
|
return sid, sdat.values
|
2018-04-06 05:56:56 +00:00
|
|
|
end
|
|
|
|
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
return nil
|
|
|
|
end
|
2018-04-06 05:56:56 +00:00
|
|
|
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
function authenticator(validator, accs)
|
|
|
|
local http = require "luci.http"
|
|
|
|
local ctrl = require "luci.controller.rpc"
|
|
|
|
local auth = http.formvalue("auth", true) or http.getcookie("sysauth")
|
|
|
|
|
|
|
|
if auth then -- if authentication token was given
|
|
|
|
local sid, sdat = ctrl.session_retrieve(auth, accs)
|
|
|
|
if sdat then -- if given token is valid
|
|
|
|
return sdat.username, sid
|
2008-08-22 20:04:04 +00:00
|
|
|
end
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
http.status(403, "Forbidden")
|
2008-08-22 20:04:04 +00:00
|
|
|
end
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function index()
|
|
|
|
local ctrl = require "luci.controller.rpc"
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2008-09-05 15:38:53 +00:00
|
|
|
local rpc = node("rpc")
|
|
|
|
rpc.sysauth = "root"
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
rpc.sysauth_authenticator = ctrl.authenticator
|
2008-09-05 15:38:53 +00:00
|
|
|
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"))
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
entry({"rpc", "ip"}, call("rpc_ip"))
|
2008-09-05 15:38:53 +00:00
|
|
|
entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
|
2008-08-22 22:13:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function rpc_auth()
|
2018-05-18 14:58:17 +00:00
|
|
|
local ctrl = require "luci.controller.rpc"
|
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 server = {}
|
2008-12-14 21:43:10 +00:00
|
|
|
server.challenge = function(user, pass)
|
2015-09-12 09:11:18 +00:00
|
|
|
local config = require "luci.config"
|
2018-04-06 05:56:56 +00:00
|
|
|
local login = util.ubus("session", "login", {
|
|
|
|
username = user,
|
|
|
|
password = pass,
|
|
|
|
timeout = tonumber(config.sauth.sessiontime)
|
|
|
|
})
|
|
|
|
|
|
|
|
if type(login) == "table" and
|
|
|
|
type(login.ubus_rpc_session) == "string"
|
|
|
|
then
|
|
|
|
util.ubus("session", "set", {
|
|
|
|
ubus_rpc_session = login.ubus_rpc_session,
|
|
|
|
values = {
|
2019-01-30 15:48:51 +00:00
|
|
|
token = sys.uniqueid(16)
|
2018-04-06 05:56:56 +00:00
|
|
|
}
|
|
|
|
})
|
2015-07-15 21:25:43 +00:00
|
|
|
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
local sid, sdat = ctrl.session_retrieve(login.ubus_rpc_session, { user })
|
2015-01-15 09:55:53 +00:00
|
|
|
if sdat then
|
2018-04-06 05:56:56 +00:00
|
|
|
return {
|
|
|
|
sid = sid,
|
2019-01-30 15:48:51 +00:00
|
|
|
token = sdat.token
|
2018-04-06 05:56:56 +00:00
|
|
|
}
|
2015-01-15 09:55:53 +00:00
|
|
|
end
|
2008-08-22 22:13:54 +00:00
|
|
|
end
|
2012-08-08 09:48:53 +00:00
|
|
|
|
2018-04-06 05:56:56 +00:00
|
|
|
return nil
|
2008-12-14 21:43:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
server.login = function(...)
|
|
|
|
local challenge = server.challenge(...)
|
2018-04-06 05:56:56 +00:00
|
|
|
if challenge then
|
|
|
|
http.header("Set-Cookie", 'sysauth=%s; path=%s' %{
|
|
|
|
challenge.sid,
|
|
|
|
http.getenv("SCRIPT_NAME")
|
|
|
|
})
|
|
|
|
return challenge.sid
|
|
|
|
end
|
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()
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
local util = require "luci.util"
|
2008-08-26 00:53:28 +00:00
|
|
|
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
|
|
|
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
local sys2 = util.clone(sys)
|
|
|
|
sys2.net = util.clone(sys.net)
|
|
|
|
sys2.wifi = util.clone(sys.wifi)
|
|
|
|
|
|
|
|
function sys2.wifi.getiwinfo(ifname, operation)
|
|
|
|
local iw = sys.wifi.getiwinfo(ifname)
|
|
|
|
if iw then
|
|
|
|
if operation then
|
|
|
|
assert(type(iwinfo[iw.type][operation]) == "function")
|
|
|
|
return iw[operation]
|
|
|
|
end
|
|
|
|
|
|
|
|
local n, f
|
|
|
|
local rv = { ifname = ifname }
|
|
|
|
for n, f in pairs(iwinfo[iw.type]) do
|
|
|
|
if type(f) == "function" and
|
|
|
|
n ~= "scanlist" and n ~= "countrylist"
|
|
|
|
then
|
|
|
|
rv[n] = iw[n]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return rv
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2008-08-26 00:53:28 +00:00
|
|
|
http.prepare_content("application/json")
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
ltn12.pump.all(jsonrpc.handle(sys2, 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
|
luci-mod-rpc: more auth/login fixes, expose further libraries
The previous attempt to fix authentication broke login functionality so
rework the code once again, this time with referencing helper functions
directly via the controller scope.
Furthermore, properly expose luci.sys.wifi.getiwinfo() and luci.ip.
For getiwinfo(), the RPC wrapped function accepts one further optional
parameter specifying the operation to invoke on the iwinfo instance.
If no operation is specified, a summary object containing all info
without country and scan list is returned.
Example to obtain iwinfo summary object:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
Example to obtain iwinfo scan list:
curl --cookie sysauth=... \
--data '{"method": "wifi.getiwinfo", "params": ["wlan0", "scanlist"]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/sys"
The exposed luci.ip class uses a similar approach to allow invoking
instance methods on cidr objects. The new(), IPv4(), IPv6() and MAC()
constructors accept two further optional arguments, with the first
specifying the instance method to invoke and the second the value to
pass to the instance method.
Example to get list of IPv4 neighbours (ARP entries):
curl --cookie sysauth=... \
--data '{"method": "neighbors", "params": [{"family": 4}]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Example to add 100 hosts to a network address:
curl --cookie sysauth=... \
--data '{"method": "IPv4", "params": ["192.168.0.1", "255.255.255.0", "add", 1000]}' \
"http://192.168.1.1/cgi-bin/luci/rpc/ip"
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-05-05 20:56:30 +00:00
|
|
|
|
|
|
|
function rpc_ip()
|
|
|
|
if not pcall(require, "luci.ip") then
|
|
|
|
luci.http.status(404, "Not Found")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local util = require "luci.util"
|
|
|
|
local ip = require "luci.ip"
|
|
|
|
local jsonrpc = require "luci.jsonrpc"
|
|
|
|
local http = require "luci.http"
|
|
|
|
local ltn12 = require "luci.ltn12"
|
|
|
|
|
|
|
|
local ip2 = util.clone(ip)
|
|
|
|
|
|
|
|
local _, n
|
|
|
|
for _, n in ipairs({ "new", "IPv4", "IPv6", "MAC" }) do
|
|
|
|
ip2[n] = function(address, netmask, operation, argument)
|
|
|
|
local cidr = ip[n](address, netmask)
|
|
|
|
if cidr and operation then
|
|
|
|
assert(type(cidr[operation]) == "function")
|
|
|
|
local cidr2 = cidr[operation](cidr, argument)
|
|
|
|
return (type(cidr2) == "userdata") and cidr2:string() or cidr2
|
|
|
|
end
|
|
|
|
return (type(cidr) == "userdata") and cidr:string() or cidr
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
http.prepare_content("application/json")
|
|
|
|
ltn12.pump.all(jsonrpc.handle(ip2, http.source()), http.write)
|
|
|
|
end
|