Merge pull request #278 from nmav/ocserv
[project/luci.git] / modules / rpc / luasrc / controller / rpc.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local require = require
17 local pairs = pairs
18 local print = print
19 local pcall = pcall
20 local table = table
21
22 module "luci.controller.rpc"
23
24 function index()
25 local function authenticator(validator, accs)
26 local auth = luci.http.formvalue("auth", true)
27 if auth then -- if authentication token was given
28 local sdat = luci.sauth.read(auth)
29 if sdat then -- if given token is valid
30 if sdat.user and luci.util.contains(accs, sdat.user) then
31 return sdat.user, auth
32 end
33 end
34 end
35 luci.http.status(403, "Forbidden")
36 end
37
38 local rpc = node("rpc")
39 rpc.sysauth = "root"
40 rpc.sysauth_authenticator = authenticator
41 rpc.notemplate = true
42
43 entry({"rpc", "uci"}, call("rpc_uci"))
44 entry({"rpc", "fs"}, call("rpc_fs"))
45 entry({"rpc", "sys"}, call("rpc_sys"))
46 entry({"rpc", "ipkg"}, call("rpc_ipkg"))
47 entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
48 end
49
50 function rpc_auth()
51 local jsonrpc = require "luci.jsonrpc"
52 local sauth = require "luci.sauth"
53 local http = require "luci.http"
54 local sys = require "luci.sys"
55 local ltn12 = require "luci.ltn12"
56 local util = require "luci.util"
57
58 local loginstat
59
60 local server = {}
61 server.challenge = function(user, pass)
62 local sid, token, secret
63
64 if sys.user.checkpasswd(user, pass) then
65 sid = sys.uniqueid(16)
66 token = sys.uniqueid(16)
67 secret = sys.uniqueid(16)
68
69 http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
70 sauth.reap()
71 sauth.write(sid, {
72 user=user,
73 token=token,
74 secret=secret
75 })
76 end
77
78 return sid and {sid=sid, token=token, secret=secret}
79 end
80
81 server.login = function(...)
82 local challenge = server.challenge(...)
83 return challenge and challenge.sid
84 end
85
86 http.prepare_content("application/json")
87 ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
88 end
89
90 function rpc_uci()
91 if not pcall(require, "luci.model.uci") then
92 luci.http.status(404, "Not Found")
93 return nil
94 end
95 local uci = require "luci.jsonrpcbind.uci"
96 local jsonrpc = require "luci.jsonrpc"
97 local http = require "luci.http"
98 local ltn12 = require "luci.ltn12"
99
100 http.prepare_content("application/json")
101 ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
102 end
103
104 function rpc_fs()
105 local util = require "luci.util"
106 local io = require "io"
107 local fs2 = util.clone(require "nixio.fs")
108 local jsonrpc = require "luci.jsonrpc"
109 local http = require "luci.http"
110 local ltn12 = require "luci.ltn12"
111
112 function fs2.readfile(filename)
113 local stat, mime = pcall(require, "mime")
114 if not stat then
115 error("Base64 support not available. Please install LuaSocket.")
116 end
117
118 local fp = io.open(filename)
119 if not fp then
120 return nil
121 end
122
123 local output = {}
124 local sink = ltn12.sink.table(output)
125 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
126 return ltn12.pump.all(source, sink) and table.concat(output)
127 end
128
129 function fs2.writefile(filename, data)
130 local stat, mime = pcall(require, "mime")
131 if not stat then
132 error("Base64 support not available. Please install LuaSocket.")
133 end
134
135 local file = io.open(filename, "w")
136 local sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
137 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
138 end
139
140 http.prepare_content("application/json")
141 ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
142 end
143
144 function rpc_sys()
145 local sys = require "luci.sys"
146 local jsonrpc = require "luci.jsonrpc"
147 local http = require "luci.http"
148 local ltn12 = require "luci.ltn12"
149
150 http.prepare_content("application/json")
151 ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
152 end
153
154 function rpc_ipkg()
155 if not pcall(require, "luci.model.ipkg") then
156 luci.http.status(404, "Not Found")
157 return nil
158 end
159 local ipkg = require "luci.model.ipkg"
160 local jsonrpc = require "luci.jsonrpc"
161 local http = require "luci.http"
162 local ltn12 = require "luci.ltn12"
163
164 http.prepare_content("application/json")
165 ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
166 end