luci-mod-rpc: fix authentication via query string parameter
[project/luci.git] / modules / luci-mod-rpc / luasrc / controller / rpc.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.rpc", package.seeall)
6
7
8 function index()
9         local function session_retrieve(sid, allowed_users)
10                 local util = require "luci.util"
11                 local sdat = util.ubus("session", "get", {
12                         ubus_rpc_session = sid
13                 })
14
15                 if type(sdat) == "table" and
16                    type(sdat.values) == "table" and
17                    type(sdat.values.token) == "string" and
18                    type(sdat.values.secret) == "string" and
19                    type(sdat.values.username) == "string" and
20                    util.contains(allowed_users, sdat.values.username)
21                 then
22                         return sid, sdat.values
23                 end
24
25                 return nil
26         end
27
28         local function authenticator(validator, accs)
29                 local http = require "luci.http"
30                 local auth = http.formvalue("auth", true) or http.getcookie("sysauth")
31
32                 if auth then -- if authentication token was given
33                         local sid, sdat = session_retrieve(auth, accs)
34                         if sdat then -- if given token is valid
35                                 return sdat.username, sid
36                         end
37                         http.status(403, "Forbidden")
38                 end
39         end
40
41         local rpc = node("rpc")
42         rpc.sysauth = "root"
43         rpc.sysauth_authenticator = authenticator
44         rpc.notemplate = true
45
46         entry({"rpc", "uci"}, call("rpc_uci"))
47         entry({"rpc", "fs"}, call("rpc_fs"))
48         entry({"rpc", "sys"}, call("rpc_sys"))
49         entry({"rpc", "ipkg"}, call("rpc_ipkg"))
50         entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
51 end
52
53 function rpc_auth()
54         local jsonrpc = require "luci.jsonrpc"
55         local http    = require "luci.http"
56         local sys     = require "luci.sys"
57         local ltn12   = require "luci.ltn12"
58         local util    = require "luci.util"
59
60         local server = {}
61         server.challenge = function(user, pass)
62                 local config = require "luci.config"
63                 local login = util.ubus("session", "login", {
64                         username = user,
65                         password = pass,
66                         timeout  = tonumber(config.sauth.sessiontime)
67                 })
68
69                 if type(login) == "table" and
70                    type(login.ubus_rpc_session) == "string"
71                 then
72                         util.ubus("session", "set", {
73                                 ubus_rpc_session = login.ubus_rpc_session,
74                                 values = {
75                                         token = sys.uniqueid(16),
76                                         secret = sys.uniqueid(16)
77                                 }
78                         })
79
80                         local sid, sdat = session_retrieve(login.ubus_rpc_session, { user })
81                         if sdat then
82                                 return {
83                                         sid = sid,
84                                         token = sdat.token,
85                                         secret = sdat.secret
86                                 }
87                         end
88                 end
89
90                 return nil
91         end
92
93         server.login = function(...)
94                 local challenge = server.challenge(...)
95                 if challenge then
96                         http.header("Set-Cookie", 'sysauth=%s; path=%s' %{
97                                 challenge.sid,
98                                 http.getenv("SCRIPT_NAME")
99                         })
100                         return challenge.sid
101                 end
102         end
103
104         http.prepare_content("application/json")
105         ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
106 end
107
108 function rpc_uci()
109         if not pcall(require, "luci.model.uci") then
110                 luci.http.status(404, "Not Found")
111                 return nil
112         end
113         local uci     = require "luci.jsonrpcbind.uci"
114         local jsonrpc = require "luci.jsonrpc"
115         local http    = require "luci.http"
116         local ltn12   = require "luci.ltn12"
117
118         http.prepare_content("application/json")
119         ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
120 end
121
122 function rpc_fs()
123         local util    = require "luci.util"
124         local io      = require "io"
125         local fs2     = util.clone(require "nixio.fs")
126         local jsonrpc = require "luci.jsonrpc"
127         local http    = require "luci.http"
128         local ltn12   = require "luci.ltn12"
129
130         function fs2.readfile(filename)
131                 local stat, mime = pcall(require, "mime")
132                 if not stat then
133                         error("Base64 support not available. Please install LuaSocket.")
134                 end
135
136                 local fp = io.open(filename)
137                 if not fp then
138                         return nil
139                 end
140
141                 local output = {}
142                 local sink = ltn12.sink.table(output)
143                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
144                 return ltn12.pump.all(source, sink) and table.concat(output)
145         end
146
147         function fs2.writefile(filename, data)
148                 local stat, mime = pcall(require, "mime")
149                 if not stat then
150                         error("Base64 support not available. Please install LuaSocket.")
151                 end
152
153                 local  file = io.open(filename, "w")
154                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
155                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
156         end
157
158         http.prepare_content("application/json")
159         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
160 end
161
162 function rpc_sys()
163         local sys     = require "luci.sys"
164         local jsonrpc = require "luci.jsonrpc"
165         local http    = require "luci.http"
166         local ltn12   = require "luci.ltn12"
167
168         http.prepare_content("application/json")
169         ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
170 end
171
172 function rpc_ipkg()
173         if not pcall(require, "luci.model.ipkg") then
174                 luci.http.status(404, "Not Found")
175                 return nil
176         end
177         local ipkg    = require "luci.model.ipkg"
178         local jsonrpc = require "luci.jsonrpc"
179         local http    = require "luci.http"
180         local ltn12   = require "luci.ltn12"
181
182         http.prepare_content("application/json")
183         ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
184 end