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.sauth.read(auth)
29 if sdat then -- if given token is valid
30 user = loadstring(sdat)().user
31 if user and luci.util.contains(accs, user) then
36 luci.http.status(403, "Forbidden")
39 local rpc = node("rpc")
41 rpc.sysauth_authenticator = authenticator
44 entry({"rpc", "uci"}, call("rpc_uci"))
45 entry({"rpc", "fs"}, call("rpc_fs"))
46 entry({"rpc", "sys"}, call("rpc_sys"))
47 entry({"rpc", "ipkg"}, call("rpc_ipkg"))
48 entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
52 local jsonrpc = require "luci.jsonrpc"
53 local sauth = require "luci.sauth"
54 local http = require "luci.http"
55 local sys = require "luci.sys"
56 local ltn12 = require "luci.ltn12"
57 local util = require "luci.util"
62 server.challenge = function(user, pass)
63 local sid, token, secret
65 if sys.user.checkpasswd(user, pass) then
66 sid = sys.uniqueid(16)
67 token = sys.uniqueid(16)
68 secret = sys.uniqueid(16)
70 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
71 sauth.write(sid, util.get_bytecode({
78 return sid and {sid=sid, token=token, secret=secret}
81 server.login = function(...)
82 local challenge = server.challenge(...)
83 return challenge and challenge.sid
86 http.prepare_content("application/json")
87 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
91 if not pcall(require, "luci.model.uci") then
92 luci.http.status(404, "Not Found")
95 local uci = require "luci.jsonrpcbind.uci"
96 local jsonrpc = require "luci.jsonrpc"
97 local http = require "luci.http"
98 local ltn12 = require "luci.ltn12"
100 http.prepare_content("application/json")
101 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
105 local util = require "luci.util"
106 local io = require "io"
107 local fs2 = util.clone(require "nixio.fs")
108 local jsonrpc = require "luci.jsonrpc"
109 local http = require "luci.http"
110 local ltn12 = require "luci.ltn12"
112 function fs2.readfile(filename)
113 local stat, mime = pcall(require, "mime")
115 error("Base64 support not available. Please install LuaSocket.")
118 local fp = io.open(filename)
124 local sink = ltn12.sink.table(output)
125 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
126 return ltn12.pump.all(source, sink) and table.concat(output)
129 function fs2.writefile(filename, data)
130 local stat, mime = pcall(require, "mime")
132 error("Base64 support not available. Please install LuaSocket.")
135 local file = io.open(filename, "w")
136 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
137 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
140 http.prepare_content("application/json")
141 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
145 local sys = require "luci.sys"
146 local jsonrpc = require "luci.jsonrpc"
147 local http = require "luci.http"
148 local ltn12 = require "luci.ltn12"
150 http.prepare_content("application/json")
151 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
155 if not pcall(require, "luci.model.ipkg") then
156 luci.http.status(404, "Not Found")
159 local ipkg = require "luci.model.ipkg"
160 local jsonrpc = require "luci.jsonrpc"
161 local http = require "luci.http"
162 local ltn12 = require "luci.ltn12"
164 http.prepare_content("application/json")
165 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)