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", "fs"}, call("rpc_fs"))
44 entry({"rpc", "sys"}, call("rpc_sys"))
45 entry({"rpc", "ipkg"}, call("rpc_ipkg"))
46 entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
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"
55 local util = require "luci.util"
60 server.challenge = function(user, pass)
61 local sid, token, secret
63 if sys.user.checkpasswd(user, pass) then
64 sid = sys.uniqueid(16)
65 token = sys.uniqueid(16)
66 secret = sys.uniqueid(16)
68 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
69 sauth.write(sid, util.get_bytecode({
76 return sid and {sid=sid, token=token, secret=secret}
79 server.login = function(...)
80 local challenge = server.challenge(...)
81 return challenge and challenge.sid
84 http.prepare_content("application/json")
85 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
89 if not pcall(require, "luci.model.uci") then
90 luci.http.status(404, "Not Found")
93 local uci = require "luci.jsonrpcbind.uci"
94 local jsonrpc = require "luci.jsonrpc"
95 local http = require "luci.http"
96 local ltn12 = require "luci.ltn12"
98 http.prepare_content("application/json")
99 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
103 local util = require "luci.util"
104 local io = require "io"
105 local fs2 = util.clone(require "nixio.fs")
106 local jsonrpc = require "luci.jsonrpc"
107 local http = require "luci.http"
108 local ltn12 = require "luci.ltn12"
110 function fs2.readfile(filename)
111 local stat, mime = pcall(require, "mime")
113 error("Base64 support not available. Please install LuaSocket.")
116 local fp = io.open(filename)
122 local sink = ltn12.sink.table(output)
123 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
124 return ltn12.pump.all(source, sink) and table.concat(output)
127 function fs2.writefile(filename, data)
128 local stat, mime = pcall(require, "mime")
130 error("Base64 support not available. Please install LuaSocket.")
133 local file = io.open(filename, "w")
134 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
135 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
138 http.prepare_content("application/json")
139 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
143 local sys = require "luci.sys"
144 local jsonrpc = require "luci.jsonrpc"
145 local http = require "luci.http"
146 local ltn12 = require "luci.ltn12"
148 http.prepare_content("application/json")
149 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
153 if not pcall(require, "luci.model.ipkg") then
154 luci.http.status(404, "Not Found")
157 local ipkg = require "luci.model.ipkg"
158 local jsonrpc = require "luci.jsonrpc"
159 local http = require "luci.http"
160 local ltn12 = require "luci.ltn12"
162 http.prepare_content("application/json")
163 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)