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
41 fs = entry({"rpc", "fs"}, call("rpc_fs"))
43 fs.sysauth_authenticator = authenticator
46 sys = entry({"rpc", "sys"}, call("rpc_sys"))
48 sys.sysauth_authenticator = authenticator
51 ipkg = entry({"rpc", "ipkg"}, call("rpc_ipkg"))
53 ipkg.sysauth_authenticator = authenticator
54 ipkg.notemplate = true
56 entry({"rpc", "auth"}, call("rpc_auth")).notemplate = true
60 local jsonrpc = require "luci.jsonrpc"
61 local sauth = require "luci.sauth"
62 local http = require "luci.http"
63 local sys = require "luci.sys"
64 local ltn12 = require "luci.ltn12"
71 server.login = function(user, pass)
74 if sys.user.checkpasswd(user, pass) then
75 sid = sys.uniqueid(16)
76 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
77 sauth.write(sid, user)
83 http.prepare_content("application/json")
84 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
88 if not pcall(require, "luci.model.uci") then
89 luci.http.status(404, "Not Found")
92 local uci = require "luci.controller.rpc.uci"
93 local jsonrpc = require "luci.jsonrpc"
94 local http = require "luci.http"
95 local ltn12 = require "luci.ltn12"
97 http.prepare_content("application/json")
98 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
102 local util = require "luci.util"
103 local io = require "io"
104 local fs2 = util.clone(require "luci.fs")
105 local jsonrpc = require "luci.jsonrpc"
106 local http = require "luci.http"
107 local ltn12 = require "luci.ltn12"
109 function fs2.readfile(filename)
110 local stat, mime = pcall(require, "mime")
112 error("Base64 support not available. Please install LuaSocket.")
115 local fp = io.open(filename)
121 local sink = ltn12.sink.table(output)
122 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
123 return ltn12.pump.all(source, sink) and table.concat(output)
126 function fs2.writefile(filename, data)
127 local stat, mime = pcall(require, "mime")
129 error("Base64 support not available. Please install LuaSocket.")
132 local file = io.open(filename, "w")
133 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
134 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
137 http.prepare_content("application/json")
138 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
142 local sys = require "luci.sys"
143 local jsonrpc = require "luci.jsonrpc"
144 local http = require "luci.http"
145 local ltn12 = require "luci.ltn12"
147 http.prepare_content("application/json")
148 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
152 if not pcall(require, "luci.model.ipkg") then
153 luci.http.status(404, "Not Found")
156 local ipkg = require "luci.model.ipkg"
157 local jsonrpc = require "luci.jsonrpc"
158 local http = require "luci.http"
159 local ltn12 = require "luci.ltn12"
161 http.prepare_content("application/json")
162 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)