luci-base: switch to ubus sessions
[project/luci.git] / modules / luci-mod-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.util.ubus("session", "get", { ubus_rpc_session = auth }) or { }).values
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 http    = require "luci.http"
53         local sys     = require "luci.sys"
54         local ltn12   = require "luci.ltn12"
55         local util    = require "luci.util"
56
57         local loginstat
58
59         local server = {}
60         server.challenge = function(user, pass)
61                 local sid, token, secret
62
63                 if sys.user.checkpasswd(user, pass) then
64                         local sdat = util.ubus("session", "create", { timeout = luci.config.sauth.sessiontime })
65                         if sdat then
66                                 sid = sdat.ubus_rpc_session
67                                 token = sys.uniqueid(16)
68                                 secret = sys.uniqueid(16)
69
70                                 http.header("Set-Cookie", "sysauth="..sid.."; path=/")
71                                 util.ubus("session", "set", {
72                                         ubus_rpc_session = sid,
73                                         values = {
74                                                 user = user,
75                                                 token = token,
76                                                 secret = secret
77                                         }
78                                 })
79                         end
80                 end
81
82                 return sid and {sid=sid, token=token, secret=secret}
83         end
84
85         server.login = function(...)
86                 local challenge = server.challenge(...)
87                 return challenge and challenge.sid
88         end
89
90         http.prepare_content("application/json")
91         ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
92 end
93
94 function rpc_uci()
95         if not pcall(require, "luci.model.uci") then
96                 luci.http.status(404, "Not Found")
97                 return nil
98         end
99         local uci     = require "luci.jsonrpcbind.uci"
100         local jsonrpc = require "luci.jsonrpc"
101         local http    = require "luci.http"
102         local ltn12   = require "luci.ltn12"
103
104         http.prepare_content("application/json")
105         ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
106 end
107
108 function rpc_fs()
109         local util    = require "luci.util"
110         local io      = require "io"
111         local fs2     = util.clone(require "nixio.fs")
112         local jsonrpc = require "luci.jsonrpc"
113         local http    = require "luci.http"
114         local ltn12   = require "luci.ltn12"
115
116         function fs2.readfile(filename)
117                 local stat, mime = pcall(require, "mime")
118                 if not stat then
119                         error("Base64 support not available. Please install LuaSocket.")
120                 end
121
122                 local fp = io.open(filename)
123                 if not fp then
124                         return nil
125                 end
126
127                 local output = {}
128                 local sink = ltn12.sink.table(output)
129                 local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
130                 return ltn12.pump.all(source, sink) and table.concat(output)
131         end
132
133         function fs2.writefile(filename, data)
134                 local stat, mime = pcall(require, "mime")
135                 if not stat then
136                         error("Base64 support not available. Please install LuaSocket.")
137                 end
138
139                 local  file = io.open(filename, "w")
140                 local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
141                 return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
142         end
143
144         http.prepare_content("application/json")
145         ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
146 end
147
148 function rpc_sys()
149         local sys     = require "luci.sys"
150         local jsonrpc = require "luci.jsonrpc"
151         local http    = require "luci.http"
152         local ltn12   = require "luci.ltn12"
153
154         http.prepare_content("application/json")
155         ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
156 end
157
158 function rpc_ipkg()
159         if not pcall(require, "luci.model.ipkg") then
160                 luci.http.status(404, "Not Found")
161                 return nil
162         end
163         local ipkg    = require "luci.model.ipkg"
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(ipkg, http.source()), http.write)
170 end