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 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 301 | auto openHandler = [&conn, asyncResp]( |
| 302 | const boost::system::error_code ec, |
| 303 | const dbus::utility::ManagedObjectType& |
| 304 | objects) { |
| 305 | const std::string* socketValue = nullptr; |
| 306 | const std::string* endpointValue = nullptr; |
| 307 | const std::string* endpointObjectPath = nullptr; |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 308 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 309 | if (ec) |
| 310 | { |
| 311 | BMCWEB_LOG_ERROR << "DBus error: " << ec.message(); |
| 312 | conn.close("Failed to create mount point"); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | for (const auto& [objectPath, interfaces] : objects) |
| 317 | { |
| 318 | for (const auto& [interface, properties] : interfaces) |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 319 | { |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 320 | if (interface != |
| 321 | "xyz.openbmc_project.VirtualMedia.MountPoint") |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 322 | { |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 323 | continue; |
| 324 | } |
| 325 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 326 | for (const auto& [name, value] : properties) |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 327 | { |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 328 | if (name == "EndpointId") |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 329 | { |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 330 | endpointValue = |
| 331 | std::get_if<std::string>(&value); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 332 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 333 | if (endpointValue == nullptr) |
| 334 | { |
| 335 | BMCWEB_LOG_ERROR |
| 336 | << "EndpointId property value is null"; |
| 337 | } |
| 338 | } |
| 339 | if (name == "Socket") |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 340 | { |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 341 | socketValue = |
| 342 | std::get_if<std::string>(&value); |
| 343 | if (socketValue == nullptr) |
| 344 | { |
| 345 | BMCWEB_LOG_ERROR |
| 346 | << "Socket property value is null"; |
| 347 | } |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 348 | } |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 349 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 350 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 351 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 352 | if (endpointValue && socketValue && |
| 353 | *endpointValue == conn.req.target()) |
| 354 | { |
| 355 | endpointObjectPath = &objectPath.str; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if (objects.empty() || endpointObjectPath == nullptr) |
| 361 | { |
| 362 | BMCWEB_LOG_ERROR << "Cannot find requested EndpointId"; |
| 363 | conn.close("Failed to match EndpointId"); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | for (const auto& session : sessions) |
| 368 | { |
| 369 | if (session.second->getEndpointId() == |
| 370 | conn.req.target()) |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 371 | { |
| 372 | BMCWEB_LOG_ERROR |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 373 | << "Cannot open new connection - socket is in use"; |
| 374 | conn.close("Slot is in use"); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 375 | return; |
| 376 | } |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 377 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 378 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 379 | // If the socket file exists (i.e. after bmcweb crash), |
| 380 | // we cannot reuse it. |
| 381 | std::remove((*socketValue).c_str()); |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 382 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 383 | sessions[&conn] = std::make_shared<NbdProxyServer>( |
| 384 | conn, *socketValue, *endpointValue, |
| 385 | *endpointObjectPath); |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 386 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame^] | 387 | sessions[&conn]->run(); |
| 388 | }; |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 389 | crow::connections::systemBus->async_method_call( |
| 390 | std::move(openHandler), "xyz.openbmc_project.VirtualMedia", |
| 391 | "/xyz/openbmc_project/VirtualMedia", |
| 392 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 393 | }; |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 394 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 395 | crow::connections::systemBus->async_method_call( |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 396 | std::move(getUserInfoHandler), |
| 397 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 398 | "xyz.openbmc_project.User.Manager", "GetUserInfo", |
| 399 | conn.getUserName()); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 400 | }) |
| 401 | .onclose( |
| 402 | [](crow::websocket::Connection& conn, const std::string& reason) { |
| 403 | BMCWEB_LOG_DEBUG << "nbd-proxy.onclose(reason = '" << reason |
| 404 | << "')"; |
| 405 | auto session = sessions.find(&conn); |
| 406 | if (session == sessions.end()) |
| 407 | { |
| 408 | BMCWEB_LOG_DEBUG << "No session to close"; |
| 409 | return; |
| 410 | } |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 411 | // Remove reference to session in global map |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 412 | sessions.erase(session); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 413 | session->second->close(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 414 | }) |
| 415 | .onmessage([](crow::websocket::Connection& conn, |
Vikram Bodireddy | f5b16f0 | 2020-08-26 14:54:51 +0530 | [diff] [blame] | 416 | const std::string& data, bool) { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 417 | BMCWEB_LOG_DEBUG << "nbd-proxy.onmessage(len = " << data.length() |
| 418 | << ")"; |
| 419 | // Acquire proxy from sessions |
| 420 | auto session = sessions.find(&conn); |
| 421 | if (session != sessions.end()) |
| 422 | { |
| 423 | if (session->second) |
| 424 | { |
| 425 | session->second->send(data); |
| 426 | return; |
| 427 | } |
| 428 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 429 | }); |
| 430 | } |
| 431 | } // namespace nbd_proxy |
| 432 | } // namespace crow |