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 fs = entry({"rpc", "sys"}, call("rpc_sys"))
46 fs.sysauth_authenticator = authenticator
48 uci = entry({"rpc", "auth"}, call("rpc_auth"))
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"
63 server.login = function(user, pass)
66 if sys.user.checkpasswd(user, pass) then
67 sid = sys.uniqueid(16)
68 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
69 sauth.write(sid, user)
75 http.prepare_content("application/json")
76 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
80 local uci = require "luci.controller.rpc.uci"
81 local jsonrpc = require "luci.jsonrpc"
82 local http = require "luci.http"
83 local ltn12 = require "luci.ltn12"
85 http.prepare_content("application/json")
86 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
90 local util = require "luci.util"
91 local io = require "io"
92 local fs2 = util.clone(require "luci.fs")
93 local jsonrpc = require "luci.jsonrpc"
94 local http = require "luci.http"
95 local ltn12 = require "luci.ltn12"
97 function fs2.readfile(filename)
98 local stat, mime = pcall(require, "mime")
100 error("Base64 support not available. Please install LuaSocket.")
103 local fp = io.open(filename)
109 local sink = ltn12.sink.table(output)
110 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
111 return ltn12.pump.all(source, sink) and table.concat(output)
114 function fs2.writefile(filename, data)
115 local stat, mime = pcall(require, "mime")
117 error("Base64 support not available. Please install LuaSocket.")
120 local file = io.open(filename, "w")
121 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
122 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
125 http.prepare_content("application/json")
126 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
130 local sys = require "luci.sys"
131 local jsonrpc = require "luci.jsonrpc"
132 local http = require "luci.http"
133 local ltn12 = require "luci.ltn12"
135 http.prepare_content("application/json")
136 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)