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
20 module "luci.controller.rpc"
23 local function authenticator(validator, accs)
24 local auth = luci.http.formvalue("auth", true)
26 local user = luci.sauth.read(auth)
27 if user and luci.util.contains(accs, user) then
31 luci.http.status(403, "Forbidden")
34 uci = entry({"rpc", "uci"}, call("rpc_uci"))
36 uci.sysauth_authenticator = authenticator
38 fs = entry({"rpc", "fs"}, call("rpc_fs"))
40 fs.sysauth_authenticator = authenticator
42 fs = entry({"rpc", "sys"}, call("rpc_sys"))
44 fs.sysauth_authenticator = authenticator
46 uci = entry({"rpc", "auth"}, call("rpc_auth"))
50 local jsonrpc = require "luci.jsonrpc"
51 local sauth = require "luci.sauth"
52 local http = require "luci.http"
53 local sys = require "luci.sys"
54 local ltn12 = require "luci.ltn12"
61 server.login = function(user, pass)
64 if sys.user.checkpasswd(user, pass) then
65 sid = sys.uniqueid(16)
66 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
67 sauth.write(sid, user)
73 http.prepare_content("application/json")
74 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
78 local uci = require "luci.controller.rpc.uci"
79 local jsonrpc = require "luci.jsonrpc"
80 local http = require "luci.http"
81 local ltn12 = require "luci.ltn12"
83 http.prepare_content("application/json")
84 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
88 local util = require "luci.util"
89 local fs = util.clone(require "luci.fs")
90 local jsonrpc = require "luci.jsonrpc"
91 local http = require "luci.http"
92 local ltn12 = require "luci.ltn12"
94 function fs.readfile(filename)
95 if not pcall(require, "mime") then
96 error("Base64 support not available. Please install LuaSocket.")
99 return ltn12.source.chain(ltn12.source.file(filename), mime.encode("base64"))
102 function fs.writefile(filename, data)
103 if not pcall(require, "mime") then
104 error("Base64 support not available. Please install LuaSocket.")
107 local sink = ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(filename))
108 return ltn12.pump.all(ltn12.source.string(data), sink)
111 http.prepare_content("application/json")
112 ltn12.pump.all(jsonrpc.handle(fs, http.source()), http.write)
116 local sys = require "luci.sys"
117 local jsonrpc = require "luci.jsonrpc"
118 local http = require "luci.http"
119 local ltn12 = require "luci.ltn12"
121 http.prepare_content("application/json")
122 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)