Refined urltokens and XSRF protection
[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
28                         local sdat = luci.sauth.read(auth)
29                         user = loadstring(sdat)().user
30                         if user and luci.util.contains(accs, user) then
31                                 return user, auth
32                         end
33                 end
34                 luci.http.status(403, "Forbidden")
35         end
36         
37         local rpc = node("rpc")
38         rpc.sysauth = "root"
39         rpc.sysauth_authenticator = authenticator
40         rpc.notemplate = true
41         
42         entry({"rpc", "uci"}, call("rpc_uci"))
43         entry({"rpc", "uvl"}, call("rpc_uvl"))
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.write(sid, util.get_bytecode({
71                                 user=user,
72                                 token=token,
73                                 secret=secret
74                         }))
75                 end
76                 
77                 return sid and {sid=sid, token=token, secret=secret}
78         end
79
80         server.login = function(...)
81                 local challenge = server.challenge(...)
82                 return challenge and challenge.sid
83         end
84         
85         http.prepare_content("application/json")
86         ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
87 end
88
89 function rpc_uci()
90         if not pcall(require, "luci.model.uci") then
91                 luci.http.status(404, "Not Found")
92                 return nil
93         end
94         local uci     = require "luci.jsonrpcbind.uci"
95         local jsonrpc = require "luci.jsonrpc"
96         local http    = require "luci.http"
97         local ltn12   = require "luci.ltn12"
98         
99         http.prepare_content("application/json")
100         ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
101 end
102
103 function rpc_uvl()
104         if not pcall(require, "luci.uvl") then
105                 luci.http.status(404, "Not Found")
106                 return nil
107         end
108         local uvl     = require "luci.jsonrpcbind.uvl"
109         local jsonrpc = require "luci.jsonrpc"
110         local http    = require "luci.http"
111         local ltn12   = require "luci.ltn12"
112
113         http.prepare_content("application/json")
114         ltn12.pump.all(jsonrpc.handle(uvl, http.source()), http.write)
115 end
116
117 function rpc_fs()
118         local util    = require "luci.util"
119         local io      = require "io"
120         local fs2     = util.clone(require "luci.fs")
121         local jsonrpc = require "luci.jsonrpc"
122         local http    = require "luci.http"
123         local ltn12   = require "luci.ltn12"
124
125         function fs2.readfile(filename)
126                 local stat, mime = pcall(require, "mime")
127                 if not stat then
128                         error("Base64 support not available. Please install LuaSocket.")
129                 end
130
131                 local fp = io.open(filename)
132                 if not fp then
133                         return nil
134                 end
135
136                 local output = {}
137                 local sink = ltn12.sink.table(output)
138                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
139                 return ltn12.pump.all(source, sink) and table.concat(output)
140         end
141         
142         function fs2.writefile(filename, data)
143                 local stat, mime = pcall(require, "mime")
144                 if not stat then
145                         error("Base64 support not available. Please install LuaSocket.")
146                 end
147
148                 local  file = io.open(filename, "w")
149                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
150                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
151         end
152         
153         http.prepare_content("application/json")
154         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
155 end
156
157 function rpc_sys()
158         local sys     = require "luci.sys"
159         local jsonrpc = require "luci.jsonrpc"
160         local http    = require "luci.http"
161         local ltn12   = require "luci.ltn12"
162         
163         http.prepare_content("application/json")
164         ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
165 end
166
167 function rpc_ipkg()
168         if not pcall(require, "luci.model.ipkg") then
169                 luci.http.status(404, "Not Found")
170                 return nil
171         end
172         local ipkg    = require "luci.model.ipkg"
173         local jsonrpc = require "luci.jsonrpc"
174         local http    = require "luci.http"
175         local ltn12   = require "luci.ltn12"
176
177         http.prepare_content("application/json")
178         ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
179 end