summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-mod-rpc
diff options
context:
space:
mode:
Diffstat (limited to 'modules/luci-mod-rpc')
-rw-r--r--modules/luci-mod-rpc/Makefile15
-rw-r--r--modules/luci-mod-rpc/luasrc/controller/rpc.lua166
-rw-r--r--modules/luci-mod-rpc/luasrc/jsonrpc.lua94
-rw-r--r--modules/luci-mod-rpc/luasrc/jsonrpcbind/uci.lua94
4 files changed, 369 insertions, 0 deletions
diff --git a/modules/luci-mod-rpc/Makefile b/modules/luci-mod-rpc/Makefile
new file mode 100644
index 000000000..e64c86c62
--- /dev/null
+++ b/modules/luci-mod-rpc/Makefile
@@ -0,0 +1,15 @@
+#
+# Copyright (C) 2008-2014 The LuCI Team <luci@lists.subsignal.org>
+#
+# This is free software, licensed under the Apache License, Version 2.0 .
+#
+
+include $(TOPDIR)/rules.mk
+
+LUCI_TITLE:=LuCI RPC - JSON-RPC API
+LUCI_DEPENDS:=+luci-lib-json
+
+include ../../luci.mk
+
+# call BuildPackage - OpenWrt buildroot signature
+
diff --git a/modules/luci-mod-rpc/luasrc/controller/rpc.lua b/modules/luci-mod-rpc/luasrc/controller/rpc.lua
new file mode 100644
index 000000000..359184c74
--- /dev/null
+++ b/modules/luci-mod-rpc/luasrc/controller/rpc.lua
@@ -0,0 +1,166 @@
+--[[
+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 require = require
+local pairs = pairs
+local print = print
+local pcall = pcall
+local table = table
+
+module "luci.controller.rpc"
+
+function index()
+ local function authenticator(validator, accs)
+ local auth = luci.http.formvalue("auth", true)
+ 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
+
+ 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()
+ 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"
+ local util = require "luci.util"
+
+ local loginstat
+
+ local server = {}
+ 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.reap()
+ sauth.write(sid, {
+ user=user,
+ token=token,
+ secret=secret
+ })
+ end
+
+ 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()
+ 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
+
+function rpc_fs()
+ local util = require "luci.util"
+ local io = require "io"
+ local fs2 = util.clone(require "nixio.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
diff --git a/modules/luci-mod-rpc/luasrc/jsonrpc.lua b/modules/luci-mod-rpc/luasrc/jsonrpc.lua
new file mode 100644
index 000000000..71c95d549
--- /dev/null
+++ b/modules/luci-mod-rpc/luasrc/jsonrpc.lua
@@ -0,0 +1,94 @@
+--[[
+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$
+]]--
+
+module("luci.jsonrpc", package.seeall)
+require "luci.json"
+
+function resolve(mod, method)
+ local path = luci.util.split(method, ".")
+
+ for j=1, #path-1 do
+ if not type(mod) == "table" then
+ break
+ end
+ mod = rawget(mod, path[j])
+ if not mod then
+ break
+ end
+ end
+ mod = type(mod) == "table" and rawget(mod, path[#path]) or nil
+ if type(mod) == "function" then
+ return mod
+ end
+end
+
+function handle(tbl, rawsource, ...)
+ local decoder = luci.json.Decoder()
+ local stat = luci.ltn12.pump.all(rawsource, decoder:sink())
+ local json = decoder:get()
+ local response
+ local success = false
+
+ if stat then
+ if type(json.method) == "string"
+ and (not json.params or type(json.params) == "table") then
+ local method = resolve(tbl, json.method)
+ if method then
+ response = reply(json.jsonrpc, json.id,
+ proxy(method, unpack(json.params or {})))
+ else
+ response = reply(json.jsonrpc, json.id,
+ nil, {code=-32601, message="Method not found."})
+ end
+ else
+ response = reply(json.jsonrpc, json.id,
+ nil, {code=-32600, message="Invalid request."})
+ end
+ else
+ response = reply("2.0", nil,
+ nil, {code=-32700, message="Parse error."})
+ end
+
+ return luci.json.Encoder(response, ...):source()
+end
+
+function reply(jsonrpc, id, res, err)
+ require "luci.json"
+ id = id or luci.json.null
+
+ -- 1.0 compatibility
+ if jsonrpc ~= "2.0" then
+ jsonrpc = nil
+ res = res or luci.json.null
+ err = err or luci.json.null
+ end
+
+ return {id=id, result=res, error=err, jsonrpc=jsonrpc}
+end
+
+function proxy(method, ...)
+ local res = {luci.util.copcall(method, ...)}
+ local stat = table.remove(res, 1)
+
+ if not stat then
+ return nil, {code=-32602, message="Invalid params.", data=table.remove(res, 1)}
+ else
+ if #res <= 1 then
+ return res[1] or luci.json.null
+ else
+ return res
+ end
+ end
+end
diff --git a/modules/luci-mod-rpc/luasrc/jsonrpcbind/uci.lua b/modules/luci-mod-rpc/luasrc/jsonrpcbind/uci.lua
new file mode 100644
index 000000000..195398674
--- /dev/null
+++ b/modules/luci-mod-rpc/luasrc/jsonrpcbind/uci.lua
@@ -0,0 +1,94 @@
+--[[
+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".cursor()
+local ucis = require "luci.model.uci".cursor_state()
+local table = require "table"
+
+
+module "luci.jsonrpcbind.uci"
+_M, _PACKAGE, _NAME = nil, nil, nil
+
+function add(config, ...)
+ uci:load(config)
+ local stat = uci:add(config, ...)
+ return uci:save(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)
+ local sections = {}
+
+ return uci:foreach(config, stype, function(section)
+ table.insert(sections, section)
+ end) and sections
+end
+
+function get(config, ...)
+ uci:load(config)
+ return uci:get(config, ...)
+end
+
+function get_all(config, ...)
+ uci:load(config)
+ return uci:get_all(config, ...)
+end
+
+function get_state(config, ...)
+ ucis:load(config)
+ return ucis:get(config, ...)
+end
+
+function revert(config)
+ return uci:load(config) and uci:revert(config)
+end
+
+function section(config, ...)
+ uci:load(config)
+ return uci:section(config, ...) and uci:save(config)
+end
+
+function set(config, ...)
+ uci:load(config)
+ return uci:set(config, ...) and uci:save(config)
+end
+
+function tset(config, ...)
+ uci:load(config)
+ return uci:tset(config, ...) and uci:save(config)
+end
+