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)
28 local sdat = luci.sauth.read(auth)
29 user = loadstring(sdat)().user
30 if user and luci.util.contains(accs, user) then
34 luci.http.status(403, "Forbidden")
37 local rpc = node("rpc")
39 rpc.sysauth_authenticator = authenticator
42 entry({"rpc", "uci"}, call("rpc_uci"))
43 entry({"rpc", "uvl"}, call("rpc_uvl"))
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 sauth = require "luci.sauth"
53 local http = require "luci.http"
54 local sys = require "luci.sys"
55 local ltn12 = require "luci.ltn12"
56 local util = require "luci.util"
61 server.challenge = function(user, pass)
62 local sid, token, secret
64 if sys.user.checkpasswd(user, pass) then
65 sid = sys.uniqueid(16)
66 token = sys.uniqueid(16)
67 secret = sys.uniqueid(16)
69 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
70 sauth.write(sid, util.get_bytecode({
77 return sid and {sid=sid, token=token, secret=secret}
80 server.login = function(...)
81 local challenge = server.challenge(...)
82 return challenge and challenge.sid
85 http.prepare_content("application/json")
86 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
90 if not pcall(require, "luci.model.uci") then
91 luci.http.status(404, "Not Found")
94 local uci = require "luci.jsonrpcbind.uci"
95 local jsonrpc = require "luci.jsonrpc"
96 local http = require "luci.http"
97 local ltn12 = require "luci.ltn12"
99 http.prepare_content("application/json")
100 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
104 if not pcall(require, "luci.uvl") then
105 luci.http.status(404, "Not Found")
108 local uvl = require "luci.jsonrpcbind.uvl"
109 local jsonrpc = require "luci.jsonrpc"
110 local http = require "luci.http"
111 local ltn12 = require "luci.ltn12"
113 http.prepare_content("application/json")
114 ltn12.pump.all(jsonrpc.handle(uvl, http.source()), http.write)
118 local util = require "luci.util"
119 local io = require "io"
120 local fs2 = util.clone(require "luci.fs")
121 local jsonrpc = require "luci.jsonrpc"
122 local http = require "luci.http"
123 local ltn12 = require "luci.ltn12"
125 function fs2.readfile(filename)
126 local stat, mime = pcall(require, "mime")
128 error("Base64 support not available. Please install LuaSocket.")
131 local fp = io.open(filename)
137 local sink = ltn12.sink.table(output)
138 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
139 return ltn12.pump.all(source, sink) and table.concat(output)
142 function fs2.writefile(filename, data)
143 local stat, mime = pcall(require, "mime")
145 error("Base64 support not available. Please install LuaSocket.")
148 local file = io.open(filename, "w")
149 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
150 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
153 http.prepare_content("application/json")
154 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
158 local sys = require "luci.sys"
159 local jsonrpc = require "luci.jsonrpc"
160 local http = require "luci.http"
161 local ltn12 = require "luci.ltn12"
163 http.prepare_content("application/json")
164 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
168 if not pcall(require, "luci.model.ipkg") then
169 luci.http.status(404, "Not Found")
172 local ipkg = require "luci.model.ipkg"
173 local jsonrpc = require "luci.jsonrpc"
174 local http = require "luci.http"
175 local ltn12 = require "luci.ltn12"
177 http.prepare_content("application/json")
178 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)