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 local rpc = node("rpc")
38 rpc.sysauth_authenticator = authenticator
41 entry({"rpc", "uci"}, call("rpc_uci"))
42 entry({"rpc", "uvl"}, call("rpc_uvl"))
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"
59 server.login = function(user, pass)
62 if sys.user.checkpasswd(user, pass) then
63 sid = sys.uniqueid(16)
64 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
65 sauth.write(sid, user)
71 http.prepare_content("application/json")
72 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
76 if not pcall(require, "luci.model.uci") then
77 luci.http.status(404, "Not Found")
80 local uci = require "luci.jsonrpcbind.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 if not pcall(require, "luci.uvl") then
91 luci.http.status(404, "Not Found")
94 local uvl = require "luci.jsonrpcbind.uvl"
95 local jsonrpc = require "luci.jsonrpc"
96 local http = require "luci.http"
97 local ltn12 = require "luci.ltn12"
99 http.prepare_content("application/json")
100 ltn12.pump.all(jsonrpc.handle(uvl, http.source()), http.write)
104 local util = require "luci.util"
105 local io = require "io"
106 local fs2 = util.clone(require "luci.fs")
107 local jsonrpc = require "luci.jsonrpc"
108 local http = require "luci.http"
109 local ltn12 = require "luci.ltn12"
111 function fs2.readfile(filename)
112 local stat, mime = pcall(require, "mime")
114 error("Base64 support not available. Please install LuaSocket.")
117 local fp = io.open(filename)
123 local sink = ltn12.sink.table(output)
124 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
125 return ltn12.pump.all(source, sink) and table.concat(output)
128 function fs2.writefile(filename, data)
129 local stat, mime = pcall(require, "mime")
131 error("Base64 support not available. Please install LuaSocket.")
134 local file = io.open(filename, "w")
135 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
136 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
139 http.prepare_content("application/json")
140 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
144 local sys = require "luci.sys"
145 local jsonrpc = require "luci.jsonrpc"
146 local http = require "luci.http"
147 local ltn12 = require "luci.ltn12"
149 http.prepare_content("application/json")
150 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
154 if not pcall(require, "luci.model.ipkg") then
155 luci.http.status(404, "Not Found")
158 local ipkg = require "luci.model.ipkg"
159 local jsonrpc = require "luci.jsonrpc"
160 local http = require "luci.http"
161 local ltn12 = require "luci.ltn12"
163 http.prepare_content("application/json")
164 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)