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 user = luci.sauth.read(auth)
29 if user and luci.util.contains(accs, user) then
33 luci.http.status(403, "Forbidden")
36 uci = entry({"rpc", "uci"}, call("rpc_uci"))
38 uci.sysauth_authenticator = authenticator
40 fs = entry({"rpc", "fs"}, call("rpc_fs"))
42 fs.sysauth_authenticator = authenticator
44 sys = entry({"rpc", "sys"}, call("rpc_sys"))
46 sys.sysauth_authenticator = authenticator
48 ipkg = entry({"rpc", "ipkg"}, call("rpc_ipkg"))
50 ipkg.sysauth_authenticator = authenticator
52 uci = entry({"rpc", "auth"}, call("rpc_auth"))
56 local jsonrpc = require "luci.jsonrpc"
57 local sauth = require "luci.sauth"
58 local http = require "luci.http"
59 local sys = require "luci.sys"
60 local ltn12 = require "luci.ltn12"
67 server.login = function(user, pass)
70 if sys.user.checkpasswd(user, pass) then
71 sid = sys.uniqueid(16)
72 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
73 sauth.write(sid, user)
79 http.prepare_content("application/json")
80 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
84 if not pcall(require, "luci.model.uci") then
85 luci.http.status(404, "Not Found")
88 local uci = require "luci.controller.rpc.uci"
89 local jsonrpc = require "luci.jsonrpc"
90 local http = require "luci.http"
91 local ltn12 = require "luci.ltn12"
93 http.prepare_content("application/json")
94 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
98 local util = require "luci.util"
99 local io = require "io"
100 local fs2 = util.clone(require "luci.fs")
101 local jsonrpc = require "luci.jsonrpc"
102 local http = require "luci.http"
103 local ltn12 = require "luci.ltn12"
105 function fs2.readfile(filename)
106 local stat, mime = pcall(require, "mime")
108 error("Base64 support not available. Please install LuaSocket.")
111 local fp = io.open(filename)
117 local sink = ltn12.sink.table(output)
118 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
119 return ltn12.pump.all(source, sink) and table.concat(output)
122 function fs2.writefile(filename, data)
123 local stat, mime = pcall(require, "mime")
125 error("Base64 support not available. Please install LuaSocket.")
128 local file = io.open(filename, "w")
129 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
130 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
133 http.prepare_content("application/json")
134 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
138 local sys = require "luci.sys"
139 local jsonrpc = require "luci.jsonrpc"
140 local http = require "luci.http"
141 local ltn12 = require "luci.ltn12"
143 http.prepare_content("application/json")
144 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
148 if not pcall(require, "luci.model.ipkg") then
149 luci.http.status(404, "Not Found")
152 local ipkg = require "luci.model.ipkg"
153 local jsonrpc = require "luci.jsonrpc"
154 local http = require "luci.http"
155 local ltn12 = require "luci.ltn12"
157 http.prepare_content("application/json")
158 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)