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 | |
Ed Tanous | 08e9fa9 | 2022-02-16 16:18:08 -0800 | [diff] [blame] | 50 | NbdProxyServer(const NbdProxyServer&) = delete; |
| 51 | NbdProxyServer(NbdProxyServer&&) = delete; |
| 52 | NbdProxyServer& operator=(const NbdProxyServer&) = delete; |
| 53 | NbdProxyServer& operator=(NbdProxyServer&&) = delete; |
| 54 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 55 | ~NbdProxyServer() |
| 56 | { |
| 57 | BMCWEB_LOG_DEBUG << "NbdProxyServer destructor"; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 58 | } |
| 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 Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 72 | BMCWEB_LOG_ERROR << "UNIX socket: async_accept error = " |
| 73 | << ec.message(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 74 | 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, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 94 | auto mountHandler = [this, self(shared_from_this())]( |
| 95 | const boost::system::error_code ec, |
Vikram Bodireddy | f5b16f0 | 2020-08-26 14:54:51 +0530 | [diff] [blame] | 96 | const bool) { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 97 | if (ec) |
| 98 | { |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 99 | BMCWEB_LOG_ERROR << "DBus error: cannot call mount method = " |
| 100 | << ec.message(); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 101 | connection.close("Failed to mount media"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 102 | 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 Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 121 | 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 Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 130 | // 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 Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 164 | << ec.message(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 165 | // 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. Bills | 9c0b4e5 | 2022-02-14 07:23:30 -0800 | [diff] [blame] | 201 | if (ws2uxBuf.size() == 0) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 202 | { |
| 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 Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 216 | BMCWEB_LOG_ERROR << "UNIX: async_write error = " |
| 217 | << ec.message(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | // Retrigger doWrite if there is something in buffer |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 221 | if (ws2uxBuf.size() > 0) |
| 222 | { |
| 223 | doWrite(); |
| 224 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 225 | }); |
| 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 | |
| 250 | static boost::container::flat_map<crow::websocket::Connection*, |
| 251 | std::shared_ptr<NbdProxyServer>> |
| 252 | sessions; |
| 253 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 254 | inline void requestRoutes(App& app) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 255 | { |
| 256 | BMCWEB_ROUTE(app, "/nbd/<str>") |
| 257 | .websocket() |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 258 | .onopen([](crow::websocket::Connection& conn) { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 259 | BMCWEB_LOG_DEBUG << "nbd-proxy.onopen(" << &conn << ")"; |
| 260 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 261 | auto getUserInfoHandler = [&conn, asyncResp]( |
| 262 | const boost::system::error_code ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 263 | const dbus::utilities:: |
| 264 | DBusPropertiesMap& userInfo) { |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 265 | if (ec) |
| 266 | { |
| 267 | BMCWEB_LOG_ERROR << "GetUserInfo failed..."; |
| 268 | conn.close("Failed to get user information"); |
| 269 | return; |
| 270 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 271 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 272 | const std::string* userRolePtr = nullptr; |
| 273 | auto userInfoIter = userInfo.find("UserPrivilege"); |
| 274 | if (userInfoIter != userInfo.end()) |
| 275 | { |
| 276 | userRolePtr = |
| 277 | std::get_if<std::string>(&userInfoIter->second); |
| 278 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 279 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 280 | std::string userRole{}; |
| 281 | if (userRolePtr != nullptr) |
| 282 | { |
| 283 | userRole = *userRolePtr; |
| 284 | BMCWEB_LOG_DEBUG << "userName = " << conn.getUserName() |
| 285 | << " userRole = " << *userRolePtr; |
| 286 | } |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 287 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 288 | // Get the user privileges from the role |
| 289 | ::redfish::Privileges userPrivileges = |
| 290 | ::redfish::getUserPrivileges(userRole); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 291 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 292 | const ::redfish::Privileges requiredPrivileges{ |
| 293 | requiredPrivilegeString}; |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 294 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 295 | if (!userPrivileges.isSupersetOf(requiredPrivileges)) |
| 296 | { |
| 297 | BMCWEB_LOG_DEBUG << "User " << conn.getUserName() |
| 298 | << " not authorized for nbd connection"; |
| 299 | conn.close("Unathourized access"); |
| 300 | return; |
| 301 | } |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 302 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 303 | auto openHandler = [&conn, asyncResp]( |
| 304 | const boost::system::error_code ec, |
| 305 | const dbus::utility::ManagedObjectType& |
| 306 | objects) { |
| 307 | const std::string* socketValue = nullptr; |
| 308 | const std::string* endpointValue = nullptr; |
| 309 | const std::string* endpointObjectPath = nullptr; |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 310 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 311 | if (ec) |
| 312 | { |
| 313 | BMCWEB_LOG_ERROR << "DBus error: " << ec.message(); |
| 314 | conn.close("Failed to create mount point"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | for (const auto& [objectPath, interfaces] : objects) |
| 319 | { |
| 320 | for (const auto& [interface, properties] : interfaces) |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 321 | { |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 322 | if (interface != |
| 323 | "xyz.openbmc_project.VirtualMedia.MountPoint") |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 324 | { |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 328 | for (const auto& [name, value] : properties) |
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 | if (name == "EndpointId") |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 331 | { |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 332 | endpointValue = |
| 333 | std::get_if<std::string>(&value); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 334 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 335 | if (endpointValue == nullptr) |
| 336 | { |
| 337 | BMCWEB_LOG_ERROR |
| 338 | << "EndpointId property value is null"; |
| 339 | } |
| 340 | } |
| 341 | if (name == "Socket") |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 342 | { |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 343 | socketValue = |
| 344 | std::get_if<std::string>(&value); |
| 345 | if (socketValue == nullptr) |
| 346 | { |
| 347 | BMCWEB_LOG_ERROR |
| 348 | << "Socket property value is null"; |
| 349 | } |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 350 | } |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 351 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 352 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 353 | |
Ed Tanous | 08e9fa9 | 2022-02-16 16:18:08 -0800 | [diff] [blame] | 354 | if ((endpointValue != nullptr) && |
| 355 | (socketValue != nullptr) && |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 356 | *endpointValue == conn.req.target()) |
| 357 | { |
| 358 | endpointObjectPath = &objectPath.str; |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if (objects.empty() || endpointObjectPath == nullptr) |
| 364 | { |
| 365 | BMCWEB_LOG_ERROR << "Cannot find requested EndpointId"; |
| 366 | conn.close("Failed to match EndpointId"); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | for (const auto& session : sessions) |
| 371 | { |
| 372 | if (session.second->getEndpointId() == |
| 373 | conn.req.target()) |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 374 | { |
| 375 | BMCWEB_LOG_ERROR |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 376 | << "Cannot open new connection - socket is in use"; |
| 377 | conn.close("Slot is in use"); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 378 | return; |
| 379 | } |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 380 | } |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 381 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 382 | // If the socket file exists (i.e. after bmcweb crash), |
| 383 | // we cannot reuse it. |
| 384 | std::remove((*socketValue).c_str()); |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 385 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 386 | sessions[&conn] = std::make_shared<NbdProxyServer>( |
| 387 | conn, *socketValue, *endpointValue, |
| 388 | *endpointObjectPath); |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 389 | |
Jason M. Bills | 613ea98 | 2022-02-16 14:24:30 -0800 | [diff] [blame] | 390 | sessions[&conn]->run(); |
| 391 | }; |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 392 | crow::connections::systemBus->async_method_call( |
| 393 | std::move(openHandler), "xyz.openbmc_project.VirtualMedia", |
| 394 | "/xyz/openbmc_project/VirtualMedia", |
| 395 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 396 | }; |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 397 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 398 | crow::connections::systemBus->async_method_call( |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 399 | std::move(getUserInfoHandler), |
| 400 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 401 | "xyz.openbmc_project.User.Manager", "GetUserInfo", |
| 402 | conn.getUserName()); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 403 | }) |
| 404 | .onclose( |
| 405 | [](crow::websocket::Connection& conn, const std::string& reason) { |
| 406 | BMCWEB_LOG_DEBUG << "nbd-proxy.onclose(reason = '" << reason |
| 407 | << "')"; |
| 408 | auto session = sessions.find(&conn); |
| 409 | if (session == sessions.end()) |
| 410 | { |
| 411 | BMCWEB_LOG_DEBUG << "No session to close"; |
| 412 | return; |
| 413 | } |
Iwona Winiarska | 123e823 | 2019-11-29 12:34:33 +0100 | [diff] [blame] | 414 | // Remove reference to session in global map |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 415 | sessions.erase(session); |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 416 | session->second->close(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 417 | }) |
| 418 | .onmessage([](crow::websocket::Connection& conn, |
Vikram Bodireddy | f5b16f0 | 2020-08-26 14:54:51 +0530 | [diff] [blame] | 419 | const std::string& data, bool) { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 420 | BMCWEB_LOG_DEBUG << "nbd-proxy.onmessage(len = " << data.length() |
| 421 | << ")"; |
| 422 | // Acquire proxy from sessions |
| 423 | auto session = sessions.find(&conn); |
| 424 | if (session != sessions.end()) |
| 425 | { |
| 426 | if (session->second) |
| 427 | { |
| 428 | session->second->send(data); |
| 429 | return; |
| 430 | } |
| 431 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 432 | }); |
| 433 | } |
| 434 | } // namespace nbd_proxy |
| 435 | } // namespace crow |