Rework authentication system
[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                                 user = luci.sauth.decode(sdat).user
31                                 if user and luci.util.contains(accs, user) then
32                                         return user, auth
33                                 end
34                         end
35                 end
36                 luci.http.status(403, "Forbidden")
37         end
38         
39         local rpc = node("rpc")
40         rpc.sysauth = "root"
41         rpc.sysauth_authenticator = authenticator
42         rpc.notemplate = true
43         
44         entry({"rpc", "uci"}, call("rpc_uci"))
45         entry({"rpc", "fs"}, call("rpc_fs"))
46         entry({"rpc", "sys"}, call("rpc_sys"))
47         entry({"rpc", "ipkg"}, call("rpc_ipkg"))
48         entry({"rpc", "auth"}, call("rpc_auth")).sysauth = false
49 end
50
51 function rpc_auth()
52         local jsonrpc = require "luci.jsonrpc"
53         local sauth   = require "luci.sauth"
54         local http    = require "luci.http"
55         local sys     = require "luci.sys"
56         local ltn12   = require "luci.ltn12"
57         local util    = require "luci.util"
58         
59         local loginstat
60         
61         local server = {}
62         server.challenge = function(user, pass)
63                 local sid, token, secret
64
65                 if sys.user.checkpasswd(user, pass) then
66                         sid = sys.uniqueid(16)
67                         token = sys.uniqueid(16)
68                         secret = sys.uniqueid(16)
69
70                         http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
71                         sauth.write(sid, sauth.encode({
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