blob: afb6493a44f6e414f554674b3b5e1805915c5824 [file] [log] [blame]
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
Ed Tanous04e438c2020-10-03 08:06:26 -070017#include <app.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070018#include <boost/asio/buffer.hpp>
19#include <boost/asio/local/stream_protocol.hpp>
20#include <boost/asio/write.hpp>
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020021#include <boost/beast/core/buffers_to_string.hpp>
22#include <boost/beast/core/multi_buffer.hpp>
23#include <boost/container/flat_map.hpp>
24#include <dbus_utility.hpp>
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010025#include <privileges.hpp>
Ed Tanous04e438c2020-10-03 08:06:26 -070026#include <websocket.hpp>
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020027
28namespace crow
29{
30
31namespace nbd_proxy
32{
33
34using boost::asio::local::stream_protocol;
35
36static constexpr auto nbdBufferSize = 131088;
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010037static const char* requiredPrivilegeString = "ConfigureManager";
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020038
39struct NbdProxyServer : std::enable_shared_from_this<NbdProxyServer>
40{
41 NbdProxyServer(crow::websocket::Connection& connIn,
42 const std::string& socketIdIn,
43 const std::string& endpointIdIn, const std::string& pathIn) :
44 socketId(socketIdIn),
45 endpointId(endpointIdIn), path(pathIn),
Ed Tanous2c70f802020-09-28 14:29:23 -070046 acceptor(connIn.getIoContext(), stream_protocol::endpoint(socketId)),
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020047 connection(connIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050048 {}
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020049
Ed Tanous08e9fa92022-02-16 16:18:08 -080050 NbdProxyServer(const NbdProxyServer&) = delete;
51 NbdProxyServer(NbdProxyServer&&) = delete;
52 NbdProxyServer& operator=(const NbdProxyServer&) = delete;
53 NbdProxyServer& operator=(NbdProxyServer&&) = delete;
54
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020055 ~NbdProxyServer()
56 {
57 BMCWEB_LOG_DEBUG << "NbdProxyServer destructor";
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020058 }
59
60 std::string getEndpointId() const
61 {
62 return endpointId;
63 }
64
65 void run()
66 {
67 acceptor.async_accept(
68 [this, self(shared_from_this())](boost::system::error_code ec,
69 stream_protocol::socket socket) {
70 if (ec)
71 {
Iwona Winiarska123e8232019-11-29 12:34:33 +010072 BMCWEB_LOG_ERROR << "UNIX socket: async_accept error = "
73 << ec.message();
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020074 return;
75 }
76 if (peerSocket)
77 {
78 // Something is wrong - socket shouldn't be acquired at this
79 // point
80 BMCWEB_LOG_ERROR
81 << "Failed to open connection - socket already used";
82 return;
83 }
84
85 BMCWEB_LOG_DEBUG << "Connection opened";
86 peerSocket = std::move(socket);
87 doRead();
88
89 // Trigger Write if any data was sent from server
90 // Initially this is negotiation chunk
91 doWrite();
92 });
93
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +020094 auto mountHandler = [this, self(shared_from_this())](
95 const boost::system::error_code ec,
Vikram Bodireddyf5b16f02020-08-26 14:54:51 +053096 const bool) {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020097 if (ec)
98 {
Iwona Winiarska123e8232019-11-29 12:34:33 +010099 BMCWEB_LOG_ERROR << "DBus error: cannot call mount method = "
100 << ec.message();
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200101 connection.close("Failed to mount media");
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200102 return;
103 }
104 };
105
106 crow::connections::systemBus->async_method_call(
107 std::move(mountHandler), "xyz.openbmc_project.VirtualMedia", path,
108 "xyz.openbmc_project.VirtualMedia.Proxy", "Mount");
109 }
110
111 void send(const std::string_view data)
112 {
113 boost::asio::buffer_copy(ws2uxBuf.prepare(data.size()),
114 boost::asio::buffer(data));
115 ws2uxBuf.commit(data.size());
116 doWrite();
117 }
118
119 void close()
120 {
Iwona Winiarska123e8232019-11-29 12:34:33 +0100121 acceptor.close();
122 if (peerSocket)
123 {
124 BMCWEB_LOG_DEBUG << "peerSocket->close()";
125 peerSocket->close();
126 peerSocket.reset();
127 BMCWEB_LOG_DEBUG << "std::remove(" << socketId << ")";
128 std::remove(socketId.c_str());
129 }
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200130 // The reference to session should exists until unmount is
131 // called
132 auto unmountHandler = [](const boost::system::error_code ec) {
133 if (ec)
134 {
135 BMCWEB_LOG_ERROR << "DBus error: " << ec
136 << ", cannot call unmount method";
137 return;
138 }
139 };
140
141 crow::connections::systemBus->async_method_call(
142 std::move(unmountHandler), "xyz.openbmc_project.VirtualMedia", path,
143 "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
144 }
145
146 private:
147 void doRead()
148 {
149 if (!peerSocket)
150 {
151 BMCWEB_LOG_DEBUG << "UNIX socket isn't created yet";
152 // Skip if UNIX socket is not created yet.
153 return;
154 }
155
156 // Trigger async read
157 peerSocket->async_read_some(
158 ux2wsBuf.prepare(nbdBufferSize),
159 [this, self(shared_from_this())](boost::system::error_code ec,
160 std::size_t bytesRead) {
161 if (ec)
162 {
163 BMCWEB_LOG_ERROR << "UNIX socket: async_read_some error = "
Iwona Winiarska123e8232019-11-29 12:34:33 +0100164 << ec.message();
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200165 // UNIX socket has been closed by peer, best we can do is to
166 // break all connections
167 close();
168 return;
169 }
170
171 // Fetch data from UNIX socket
172
173 ux2wsBuf.commit(bytesRead);
174
175 // Paste it to WebSocket as binary
176 connection.sendBinary(
177 boost::beast::buffers_to_string(ux2wsBuf.data()));
178 ux2wsBuf.consume(bytesRead);
179
180 // Allow further reads
181 doRead();
182 });
183 }
184
185 void doWrite()
186 {
187 if (!peerSocket)
188 {
189 BMCWEB_LOG_DEBUG << "UNIX socket isn't created yet";
190 // Skip if UNIX socket is not created yet. Collect data, and wait
191 // for nbd-client connection
192 return;
193 }
194
195 if (uxWriteInProgress)
196 {
197 BMCWEB_LOG_ERROR << "Write in progress";
198 return;
199 }
200
Jason M. Bills9c0b4e52022-02-14 07:23:30 -0800201 if (ws2uxBuf.size() == 0)
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200202 {
203 BMCWEB_LOG_ERROR << "No data to write to UNIX socket";
204 return;
205 }
206
207 uxWriteInProgress = true;
208 boost::asio::async_write(
209 *peerSocket, ws2uxBuf.data(),
210 [this, self(shared_from_this())](boost::system::error_code ec,
211 std::size_t bytesWritten) {
212 ws2uxBuf.consume(bytesWritten);
213 uxWriteInProgress = false;
214 if (ec)
215 {
Iwona Winiarska123e8232019-11-29 12:34:33 +0100216 BMCWEB_LOG_ERROR << "UNIX: async_write error = "
217 << ec.message();
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200218 return;
219 }
220 // Retrigger doWrite if there is something in buffer
Iwona Winiarska123e8232019-11-29 12:34:33 +0100221 if (ws2uxBuf.size() > 0)
222 {
223 doWrite();
224 }
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200225 });
226 }
227
228 // Keeps UNIX socket endpoint file path
229 const std::string socketId;
230 const std::string endpointId;
231 const std::string path;
232
233 bool uxWriteInProgress = false;
234
235 // UNIX => WebSocket buffer
236 boost::beast::multi_buffer ux2wsBuf;
237
238 // WebSocket <= UNIX buffer
239 boost::beast::multi_buffer ws2uxBuf;
240
241 // Default acceptor for UNIX socket
242 stream_protocol::acceptor acceptor;
243
244 // The socket used to communicate with the client.
245 std::optional<stream_protocol::socket> peerSocket;
246
247 crow::websocket::Connection& connection;
248};
249
250static boost::container::flat_map<crow::websocket::Connection*,
251 std::shared_ptr<NbdProxyServer>>
252 sessions;
253
Ed Tanous81ce6092020-12-17 16:54:55 +0000254inline void requestRoutes(App& app)
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200255{
256 BMCWEB_ROUTE(app, "/nbd/<str>")
257 .websocket()
Gunnar Millsccd584f2021-11-16 11:36:33 -0600258 .onopen([](crow::websocket::Connection& conn,
259 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200260 BMCWEB_LOG_DEBUG << "nbd-proxy.onopen(" << &conn << ")";
261
Ed Tanous168e20c2021-12-13 14:39:53 -0800262 auto getUserInfoHandler = [&conn, asyncResp](
263 const boost::system::error_code ec,
264 boost::container::flat_map<
265 std::string,
266 dbus::utility::DbusVariantType>
267 userInfo) {
268 if (ec)
269 {
270 BMCWEB_LOG_ERROR << "GetUserInfo failed...";
271 conn.close("Failed to get user information");
272 return;
273 }
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100274
Ed Tanous168e20c2021-12-13 14:39:53 -0800275 const std::string* userRolePtr = nullptr;
276 auto userInfoIter = userInfo.find("UserPrivilege");
277 if (userInfoIter != userInfo.end())
278 {
279 userRolePtr =
280 std::get_if<std::string>(&userInfoIter->second);
281 }
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100282
Ed Tanous168e20c2021-12-13 14:39:53 -0800283 std::string userRole{};
284 if (userRolePtr != nullptr)
285 {
286 userRole = *userRolePtr;
287 BMCWEB_LOG_DEBUG << "userName = " << conn.getUserName()
288 << " userRole = " << *userRolePtr;
289 }
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200290
Ed Tanous168e20c2021-12-13 14:39:53 -0800291 // Get the user privileges from the role
292 ::redfish::Privileges userPrivileges =
293 ::redfish::getUserPrivileges(userRole);
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200294
Ed Tanous168e20c2021-12-13 14:39:53 -0800295 const ::redfish::Privileges requiredPrivileges{
296 requiredPrivilegeString};
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200297
Ed Tanous168e20c2021-12-13 14:39:53 -0800298 if (!userPrivileges.isSupersetOf(requiredPrivileges))
299 {
300 BMCWEB_LOG_DEBUG << "User " << conn.getUserName()
301 << " not authorized for nbd connection";
302 conn.close("Unathourized access");
303 return;
304 }
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200305
Jason M. Bills613ea982022-02-16 14:24:30 -0800306 auto openHandler = [&conn, asyncResp](
307 const boost::system::error_code ec,
308 const dbus::utility::ManagedObjectType&
309 objects) {
310 const std::string* socketValue = nullptr;
311 const std::string* endpointValue = nullptr;
312 const std::string* endpointObjectPath = nullptr;
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200313
Jason M. Bills613ea982022-02-16 14:24:30 -0800314 if (ec)
315 {
316 BMCWEB_LOG_ERROR << "DBus error: " << ec.message();
317 conn.close("Failed to create mount point");
318 return;
319 }
320
321 for (const auto& [objectPath, interfaces] : objects)
322 {
323 for (const auto& [interface, properties] : interfaces)
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100324 {
Jason M. Bills613ea982022-02-16 14:24:30 -0800325 if (interface !=
326 "xyz.openbmc_project.VirtualMedia.MountPoint")
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100327 {
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100328 continue;
329 }
330
Jason M. Bills613ea982022-02-16 14:24:30 -0800331 for (const auto& [name, value] : properties)
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200332 {
Jason M. Bills613ea982022-02-16 14:24:30 -0800333 if (name == "EndpointId")
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200334 {
Jason M. Bills613ea982022-02-16 14:24:30 -0800335 endpointValue =
336 std::get_if<std::string>(&value);
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200337
Jason M. Bills613ea982022-02-16 14:24:30 -0800338 if (endpointValue == nullptr)
339 {
340 BMCWEB_LOG_ERROR
341 << "EndpointId property value is null";
342 }
343 }
344 if (name == "Socket")
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200345 {
Jason M. Bills613ea982022-02-16 14:24:30 -0800346 socketValue =
347 std::get_if<std::string>(&value);
348 if (socketValue == nullptr)
349 {
350 BMCWEB_LOG_ERROR
351 << "Socket property value is null";
352 }
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200353 }
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200354 }
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100355 }
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100356
Ed Tanous08e9fa92022-02-16 16:18:08 -0800357 if ((endpointValue != nullptr) &&
358 (socketValue != nullptr) &&
Jason M. Bills613ea982022-02-16 14:24:30 -0800359 *endpointValue == conn.req.target())
360 {
361 endpointObjectPath = &objectPath.str;
362 break;
363 }
364 }
365
366 if (objects.empty() || endpointObjectPath == nullptr)
367 {
368 BMCWEB_LOG_ERROR << "Cannot find requested EndpointId";
369 conn.close("Failed to match EndpointId");
370 return;
371 }
372
373 for (const auto& session : sessions)
374 {
375 if (session.second->getEndpointId() ==
376 conn.req.target())
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200377 {
378 BMCWEB_LOG_ERROR
Jason M. Bills613ea982022-02-16 14:24:30 -0800379 << "Cannot open new connection - socket is in use";
380 conn.close("Slot is in use");
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200381 return;
382 }
Jason M. Bills613ea982022-02-16 14:24:30 -0800383 }
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100384
Jason M. Bills613ea982022-02-16 14:24:30 -0800385 // If the socket file exists (i.e. after bmcweb crash),
386 // we cannot reuse it.
387 std::remove((*socketValue).c_str());
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100388
Jason M. Bills613ea982022-02-16 14:24:30 -0800389 sessions[&conn] = std::make_shared<NbdProxyServer>(
390 conn, *socketValue, *endpointValue,
391 *endpointObjectPath);
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100392
Jason M. Bills613ea982022-02-16 14:24:30 -0800393 sessions[&conn]->run();
394 };
Ed Tanous168e20c2021-12-13 14:39:53 -0800395 crow::connections::systemBus->async_method_call(
396 std::move(openHandler), "xyz.openbmc_project.VirtualMedia",
397 "/xyz/openbmc_project/VirtualMedia",
398 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
399 };
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100400
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200401 crow::connections::systemBus->async_method_call(
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +0100402 std::move(getUserInfoHandler),
403 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
404 "xyz.openbmc_project.User.Manager", "GetUserInfo",
405 conn.getUserName());
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200406 })
407 .onclose(
408 [](crow::websocket::Connection& conn, const std::string& reason) {
409 BMCWEB_LOG_DEBUG << "nbd-proxy.onclose(reason = '" << reason
410 << "')";
411 auto session = sessions.find(&conn);
412 if (session == sessions.end())
413 {
414 BMCWEB_LOG_DEBUG << "No session to close";
415 return;
416 }
Iwona Winiarska123e8232019-11-29 12:34:33 +0100417 // Remove reference to session in global map
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200418 sessions.erase(session);
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200419 session->second->close();
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200420 })
421 .onmessage([](crow::websocket::Connection& conn,
Vikram Bodireddyf5b16f02020-08-26 14:54:51 +0530422 const std::string& data, bool) {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200423 BMCWEB_LOG_DEBUG << "nbd-proxy.onmessage(len = " << data.length()
424 << ")";
425 // Acquire proxy from sessions
426 auto session = sessions.find(&conn);
427 if (session != sessions.end())
428 {
429 if (session->second)
430 {
431 session->second->send(data);
432 return;
433 }
434 }
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200435 });
436}
437} // namespace nbd_proxy
438} // namespace crow