Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 1 | /* |
| 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 Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 17 | #include <app.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 18 | #include <boost/asio/buffer.hpp> |
| 19 | #include <boost/asio/local/stream_protocol.hpp> |
| 20 | #include <boost/asio/write.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 21 | #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 Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 25 | #include <privileges.hpp> |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 26 | #include <websocket.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 27 | |
| 28 | namespace crow |
| 29 | { |
| 30 | |
| 31 | namespace nbd_proxy |
| 32 | { |
| 33 | |
| 34 | using boost::asio::local::stream_protocol; |
| 35 | |
| 36 | static constexpr auto nbdBufferSize = 131088; |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 37 | static const char* requiredPrivilegeString = "ConfigureManager"; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 38 | |
| 39 | struct 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 Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 46 | acceptor(connIn.getIoContext(), stream_protocol::endpoint(socketId)), |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 47 | connection(connIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 48 | {} |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 49 | |
| 50 | ~NbdProxyServer() |
| 51 | { |
| 52 | BMCWEB_LOG_DEBUG << "NbdProxyServer destructor"; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | std::string getEndpointId() const |
| 56 | { |
| 57 | return endpointId; |
| 58 | } |
| 59 | |
| 60 | void run() |
| 61 | { |
| 62 | acceptor.async_accept( |
| 63 | [this, self(shared_from_this())](boost::system::error_code ec, |
| 64 | stream_protocol::socket socket) { |
| 65 | if (ec) |
| 66 | { |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 67 | BMCWEB_LOG_ERROR << "UNIX socket: async_accept error = " |
| 68 | << ec.message(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | if (peerSocket) |
| 72 | { |
| 73 | // Something is wrong - socket shouldn't be acquired at this |
| 74 | // point |
| 75 | BMCWEB_LOG_ERROR |
| 76 | << "Failed to open connection - socket already used"; |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | BMCWEB_LOG_DEBUG << "Connection opened"; |
| 81 | peerSocket = std::move(socket); |
| 82 | doRead(); |
| 83 | |
| 84 | // Trigger Write if any data was sent from server |
| 85 | // Initially this is negotiation chunk |
| 86 | doWrite(); |
| 87 | }); |
| 88 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 89 | auto mountHandler = [this, self(shared_from_this())]( |
| 90 | const boost::system::error_code ec, |
Vikram Bodireddy | f5b16f0 | 2020-08-26 14:54:51 +0530 | [diff] [blame] | 91 | const bool) { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 92 | if (ec) |
| 93 | { |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 94 | BMCWEB_LOG_ERROR << "DBus error: cannot call mount method = " |
| 95 | << ec.message(); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 96 | connection.close("Failed to mount media"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | crow::connections::systemBus->async_method_call( |
| 102 | std::move(mountHandler), "xyz.openbmc_project.VirtualMedia", path, |
| 103 | "xyz.openbmc_project.VirtualMedia.Proxy", "Mount"); |
| 104 | } |
| 105 | |
| 106 | void send(const std::string_view data) |
| 107 | { |
| 108 | boost::asio::buffer_copy(ws2uxBuf.prepare(data.size()), |
| 109 | boost::asio::buffer(data)); |
| 110 | ws2uxBuf.commit(data.size()); |
| 111 | doWrite(); |
| 112 | } |
| 113 | |
| 114 | void close() |
| 115 | { |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 116 | acceptor.close(); |
| 117 | if (peerSocket) |
| 118 | { |
| 119 | BMCWEB_LOG_DEBUG << "peerSocket->close()"; |
| 120 | peerSocket->close(); |
| 121 | peerSocket.reset(); |
| 122 | BMCWEB_LOG_DEBUG << "std::remove(" << socketId << ")"; |
| 123 | std::remove(socketId.c_str()); |
| 124 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 125 | // The reference to session should exists until unmount is |
| 126 | // called |
| 127 | auto unmountHandler = [](const boost::system::error_code ec) { |
| 128 | if (ec) |
| 129 | { |
| 130 | BMCWEB_LOG_ERROR << "DBus error: " << ec |
| 131 | << ", cannot call unmount method"; |
| 132 | return; |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | crow::connections::systemBus->async_method_call( |
| 137 | std::move(unmountHandler), "xyz.openbmc_project.VirtualMedia", path, |
| 138 | "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | void doRead() |
| 143 | { |
| 144 | if (!peerSocket) |
| 145 | { |
| 146 | BMCWEB_LOG_DEBUG << "UNIX socket isn't created yet"; |
| 147 | // Skip if UNIX socket is not created yet. |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Trigger async read |
| 152 | peerSocket->async_read_some( |
| 153 | ux2wsBuf.prepare(nbdBufferSize), |
| 154 | [this, self(shared_from_this())](boost::system::error_code ec, |
| 155 | std::size_t bytesRead) { |
| 156 | if (ec) |
| 157 | { |
| 158 | BMCWEB_LOG_ERROR << "UNIX socket: async_read_some error = " |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 159 | << ec.message(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 160 | // UNIX socket has been closed by peer, best we can do is to |
| 161 | // break all connections |
| 162 | close(); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // Fetch data from UNIX socket |
| 167 | |
| 168 | ux2wsBuf.commit(bytesRead); |
| 169 | |
| 170 | // Paste it to WebSocket as binary |
| 171 | connection.sendBinary( |
| 172 | boost::beast::buffers_to_string(ux2wsBuf.data())); |
| 173 | ux2wsBuf.consume(bytesRead); |
| 174 | |
| 175 | // Allow further reads |
| 176 | doRead(); |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | void doWrite() |
| 181 | { |
| 182 | if (!peerSocket) |
| 183 | { |
| 184 | BMCWEB_LOG_DEBUG << "UNIX socket isn't created yet"; |
| 185 | // Skip if UNIX socket is not created yet. Collect data, and wait |
| 186 | // for nbd-client connection |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (uxWriteInProgress) |
| 191 | { |
| 192 | BMCWEB_LOG_ERROR << "Write in progress"; |
| 193 | return; |
| 194 | } |
| 195 | |
Jason M. Bills | 9c0b4e5 | 2022-02-14 07:23:30 -0800 | [diff] [blame] | 196 | if (ws2uxBuf.size() == 0) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 197 | { |
| 198 | BMCWEB_LOG_ERROR << "No data to write to UNIX socket"; |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | uxWriteInProgress = true; |
| 203 | boost::asio::async_write( |
| 204 | *peerSocket, ws2uxBuf.data(), |
| 205 | [this, self(shared_from_this())](boost::system::error_code ec, |
| 206 | std::size_t bytesWritten) { |
| 207 | ws2uxBuf.consume(bytesWritten); |
| 208 | uxWriteInProgress = false; |
| 209 | if (ec) |
| 210 | { |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 211 | BMCWEB_LOG_ERROR << "UNIX: async_write error = " |
| 212 | << ec.message(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | // Retrigger doWrite if there is something in buffer |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 216 | if (ws2uxBuf.size() > 0) |
| 217 | { |
| 218 | doWrite(); |
| 219 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 220 | }); |
| 221 | } |
| 222 | |
| 223 | // Keeps UNIX socket endpoint file path |
| 224 | const std::string socketId; |
| 225 | const std::string endpointId; |
| 226 | const std::string path; |
| 227 | |
| 228 | bool uxWriteInProgress = false; |
| 229 | |
| 230 | // UNIX => WebSocket buffer |
| 231 | boost::beast::multi_buffer ux2wsBuf; |
| 232 | |
| 233 | // WebSocket <= UNIX buffer |
| 234 | boost::beast::multi_buffer ws2uxBuf; |
| 235 | |
| 236 | // Default acceptor for UNIX socket |
| 237 | stream_protocol::acceptor acceptor; |
| 238 | |
| 239 | // The socket used to communicate with the client. |
| 240 | std::optional<stream_protocol::socket> peerSocket; |
| 241 | |
| 242 | crow::websocket::Connection& connection; |
| 243 | }; |
| 244 | |
| 245 | static boost::container::flat_map<crow::websocket::Connection*, |
| 246 | std::shared_ptr<NbdProxyServer>> |
| 247 | sessions; |
| 248 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 249 | inline void requestRoutes(App& app) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 250 | { |
| 251 | BMCWEB_ROUTE(app, "/nbd/<str>") |
| 252 | .websocket() |
Gunnar Mills | ccd584f | 2021-11-16 11:36:33 -0600 | [diff] [blame] | 253 | .onopen([](crow::websocket::Connection& conn, |
| 254 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 255 | BMCWEB_LOG_DEBUG << "nbd-proxy.onopen(" << &conn << ")"; |
| 256 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 257 | auto getUserInfoHandler = [&conn, asyncResp]( |
| 258 | const boost::system::error_code ec, |
| 259 | boost::container::flat_map< |
| 260 | std::string, |
| 261 | dbus::utility::DbusVariantType> |
| 262 | userInfo) { |
| 263 | if (ec) |
| 264 | { |
| 265 | BMCWEB_LOG_ERROR << "GetUserInfo failed..."; |
| 266 | conn.close("Failed to get user information"); |
| 267 | return; |
| 268 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 269 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 270 | const std::string* userRolePtr = nullptr; |
| 271 | auto userInfoIter = userInfo.find("UserPrivilege"); |
| 272 | if (userInfoIter != userInfo.end()) |
| 273 | { |
| 274 | userRolePtr = |
| 275 | std::get_if<std::string>(&userInfoIter->second); |
| 276 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 277 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 278 | std::string userRole{}; |
| 279 | if (userRolePtr != nullptr) |
| 280 | { |
| 281 | userRole = *userRolePtr; |
| 282 | BMCWEB_LOG_DEBUG << "userName = " << conn.getUserName() |
| 283 | << " userRole = " << *userRolePtr; |
| 284 | } |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 285 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 286 | // Get the user privileges from the role |
| 287 | ::redfish::Privileges userPrivileges = |
| 288 | ::redfish::getUserPrivileges(userRole); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 289 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 290 | const ::redfish::Privileges requiredPrivileges{ |
| 291 | requiredPrivilegeString}; |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 292 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 293 | if (!userPrivileges.isSupersetOf(requiredPrivileges)) |
| 294 | { |
| 295 | BMCWEB_LOG_DEBUG << "User " << conn.getUserName() |
| 296 | << " not authorized for nbd connection"; |
| 297 | conn.close("Unathourized access"); |
| 298 | return; |
| 299 | } |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 300 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 301 | auto openHandler = |
| 302 | [&conn, asyncResp]( |
| 303 | const boost::system::error_code ec, |
| 304 | const dbus::utility::ManagedObjectType& objects) { |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 305 | const std::string* socketValue = nullptr; |
| 306 | const std::string* endpointValue = nullptr; |
| 307 | const std::string* endpointObjectPath = nullptr; |
| 308 | |
| 309 | if (ec) |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 310 | { |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 311 | BMCWEB_LOG_ERROR << "DBus error: " << ec.message(); |
| 312 | conn.close("Failed to create mount point"); |
| 313 | return; |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 316 | for (const auto& objectPath : objects) |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 317 | { |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 318 | const auto interfaceMap = objectPath.second.find( |
| 319 | "xyz.openbmc_project.VirtualMedia.MountPoint"); |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 320 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 321 | if (interfaceMap == objectPath.second.end()) |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 322 | { |
| 323 | BMCWEB_LOG_DEBUG |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 324 | << "Cannot find MountPoint object"; |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 328 | const auto endpoint = |
| 329 | interfaceMap->second.find("EndpointId"); |
| 330 | if (endpoint == interfaceMap->second.end()) |
| 331 | { |
| 332 | BMCWEB_LOG_DEBUG |
| 333 | << "Cannot find EndpointId property"; |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | endpointValue = |
| 338 | std::get_if<std::string>(&endpoint->second); |
| 339 | |
| 340 | if (endpointValue == nullptr) |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 341 | { |
| 342 | BMCWEB_LOG_ERROR |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 343 | << "EndpointId property value is null"; |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 344 | continue; |
| 345 | } |
| 346 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 347 | if (*endpointValue == conn.req.target()) |
| 348 | { |
| 349 | const auto socket = |
| 350 | interfaceMap->second.find("Socket"); |
| 351 | if (socket == interfaceMap->second.end()) |
| 352 | { |
| 353 | BMCWEB_LOG_DEBUG |
| 354 | << "Cannot find Socket property"; |
| 355 | continue; |
| 356 | } |
| 357 | |
| 358 | socketValue = |
| 359 | std::get_if<std::string>(&socket->second); |
| 360 | if (socketValue == nullptr) |
| 361 | { |
| 362 | BMCWEB_LOG_ERROR |
| 363 | << "Socket property value is null"; |
| 364 | continue; |
| 365 | } |
| 366 | |
| 367 | endpointObjectPath = &objectPath.first.str; |
| 368 | break; |
| 369 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 370 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 371 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 372 | if (objects.empty() || endpointObjectPath == nullptr) |
| 373 | { |
| 374 | BMCWEB_LOG_ERROR |
| 375 | << "Cannot find requested EndpointId"; |
| 376 | conn.close("Failed to match EndpointId"); |
| 377 | return; |
| 378 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 379 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 380 | for (const auto& session : sessions) |
| 381 | { |
| 382 | if (session.second->getEndpointId() == |
| 383 | conn.req.target()) |
| 384 | { |
| 385 | BMCWEB_LOG_ERROR |
| 386 | << "Cannot open new connection - socket is " |
| 387 | "in use"; |
| 388 | conn.close("Slot is in use"); |
| 389 | return; |
| 390 | } |
| 391 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 392 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 393 | // If the socket file exists (i.e. after bmcweb crash), |
| 394 | // we cannot reuse it. |
| 395 | std::remove((*socketValue).c_str()); |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 396 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 397 | sessions[&conn] = std::make_shared<NbdProxyServer>( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 398 | conn, *socketValue, *endpointValue, |
| 399 | *endpointObjectPath); |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 400 | |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 401 | sessions[&conn]->run(); |
| 402 | }; |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 403 | crow::connections::systemBus->async_method_call( |
| 404 | std::move(openHandler), "xyz.openbmc_project.VirtualMedia", |
| 405 | "/xyz/openbmc_project/VirtualMedia", |
| 406 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 407 | }; |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 408 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 409 | crow::connections::systemBus->async_method_call( |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 410 | std::move(getUserInfoHandler), |
| 411 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 412 | "xyz.openbmc_project.User.Manager", "GetUserInfo", |
| 413 | conn.getUserName()); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 414 | }) |
| 415 | .onclose( |
| 416 | [](crow::websocket::Connection& conn, const std::string& reason) { |
| 417 | BMCWEB_LOG_DEBUG << "nbd-proxy.onclose(reason = '" << reason |
| 418 | << "')"; |
| 419 | auto session = sessions.find(&conn); |
| 420 | if (session == sessions.end()) |
| 421 | { |
| 422 | BMCWEB_LOG_DEBUG << "No session to close"; |
| 423 | return; |
| 424 | } |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 425 | // Remove reference to session in global map |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 426 | sessions.erase(session); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 427 | session->second->close(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 428 | }) |
| 429 | .onmessage([](crow::websocket::Connection& conn, |
Vikram Bodireddy | f5b16f0 | 2020-08-26 14:54:51 +0530 | [diff] [blame] | 430 | const std::string& data, bool) { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 431 | BMCWEB_LOG_DEBUG << "nbd-proxy.onmessage(len = " << data.length() |
| 432 | << ")"; |
| 433 | // Acquire proxy from sessions |
| 434 | auto session = sessions.find(&conn); |
| 435 | if (session != sessions.end()) |
| 436 | { |
| 437 | if (session->second) |
| 438 | { |
| 439 | session->second->send(data); |
| 440 | return; |
| 441 | } |
| 442 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 443 | }); |
| 444 | } |
| 445 | } // namespace nbd_proxy |
| 446 | } // namespace crow |