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