2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 local require = require
22 module "luci.controller.rpc"
25 local function authenticator(validator, accs)
26 local auth = luci.http.formvalue("auth", true)
27 if auth then -- if authentication token was given
28 local sdat = (luci.util.ubus("session", "get", { ubus_rpc_session = auth }) or { }).values
29 if sdat then -- if given token is valid
30 if sdat.user and luci.util.contains(accs, sdat.user) then
31 return sdat.user, auth
35 luci.http.status(403, "Forbidden")
38 local rpc = node("rpc")
40 rpc.sysauth_authenticator = authenticator
43 entry({"rpc", "uci"}, call("rpc_uci"))
44 entry({"rpc", "fs"}, call("rpc_fs"))
45 entry({"rpc", "sys"}, call("rpc_sys"))
46 entry({"rpc", "ipkg"}, call("rpc_ipkg"))
47 entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
51 local jsonrpc = require "luci.jsonrpc"
52 local http = require "luci.http"
53 local sys = require "luci.sys"
54 local ltn12 = require "luci.ltn12"
55 local util = require "luci.util"
60 server.challenge = function(user, pass)
61 local sid, token, secret
63 if sys.user.checkpasswd(user, pass) then
64 local sdat = util.ubus("session", "create", { timeout = luci.config.sauth.sessiontime })
66 sid = sdat.ubus_rpc_session
67 token = sys.uniqueid(16)
68 secret = sys.uniqueid(16)
70 http.header("Set-Cookie", "sysauth="..sid.."; path=/")
71 util.ubus("session", "set", {
72 ubus_rpc_session = sid,
82 return sid and {sid=sid, token=token, secret=secret}
85 server.login = function(...)
86 local challenge = server.challenge(...)
87 return challenge and challenge.sid
90 http.prepare_content("application/json")
91 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
95 if not pcall(require, "luci.model.uci") then
96 luci.http.status(404, "Not Found")
99 local uci = require "luci.jsonrpcbind.uci"
100 local jsonrpc = require "luci.jsonrpc"
101 local http = require "luci.http"
102 local ltn12 = require "luci.ltn12"
104 http.prepare_content("application/json")
105 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
109 local util = require "luci.util"
110 local io = require "io"
111 local fs2 = util.clone(require "nixio.fs")
112 local jsonrpc = require "luci.jsonrpc"
113 local http = require "luci.http"
114 local ltn12 = require "luci.ltn12"
116 function fs2.readfile(filename)
117 local stat, mime = pcall(require, "mime")
119 error("Base64 support not available. Please install LuaSocket.")
122 local fp = io.open(filename)
128 local sink = ltn12.sink.table(output)
129 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
130 return ltn12.pump.all(source, sink) and table.concat(output)
133 function fs2.writefile(filename, data)
134 local stat, mime = pcall(require, "mime")
136 error("Base64 support not available. Please install LuaSocket.")
139 local file = io.open(filename, "w")
140 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
141 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
144 http.prepare_content("application/json")
145 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
149 local sys = require "luci.sys"
150 local jsonrpc = require "luci.jsonrpc"
151 local http = require "luci.http"
152 local ltn12 = require "luci.ltn12"
154 http.prepare_content("application/json")
155 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
159 if not pcall(require, "luci.model.ipkg") then
160 luci.http.status(404, "Not Found")
163 local ipkg = require "luci.model.ipkg"
164 local jsonrpc = require "luci.jsonrpc"
165 local http = require "luci.http"
166 local ltn12 = require "luci.ltn12"
168 http.prepare_content("application/json")
169 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)