Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 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 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 18 | #include "account_service.hpp" |
| 19 | #include "app.hpp" |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 20 | #include "async_resp.hpp" |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 21 | #include "dbus_utility.hpp" |
Ed Tanous | 739b87e | 2023-02-24 13:13:33 -0800 | [diff] [blame] | 22 | #include "generated/enums/virtual_media.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 23 | #include "query.hpp" |
| 24 | #include "registries/privilege_registry.hpp" |
| 25 | #include "utils/json_utils.hpp" |
| 26 | |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 27 | #include <boost/process/async_pipe.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 28 | #include <boost/url/format.hpp> |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 29 | #include <boost/url/url_view.hpp> |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 30 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 31 | #include <array> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 32 | #include <ranges> |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 33 | #include <string_view> |
| 34 | |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 35 | namespace redfish |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 36 | { |
Ed Tanous | 365a73f | 2023-02-24 12:16:49 -0800 | [diff] [blame] | 37 | |
| 38 | enum class VmMode |
| 39 | { |
| 40 | Invalid, |
| 41 | Legacy, |
| 42 | Proxy |
| 43 | }; |
| 44 | |
| 45 | inline VmMode |
| 46 | parseObjectPathAndGetMode(const sdbusplus::message::object_path& itemPath, |
| 47 | const std::string& resName) |
| 48 | { |
| 49 | std::string thisPath = itemPath.filename(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 50 | BMCWEB_LOG_DEBUG("Filename: {}, ThisPath: {}", itemPath.str, thisPath); |
Ed Tanous | 365a73f | 2023-02-24 12:16:49 -0800 | [diff] [blame] | 51 | |
| 52 | if (thisPath.empty()) |
| 53 | { |
| 54 | return VmMode::Invalid; |
| 55 | } |
| 56 | |
| 57 | if (thisPath != resName) |
| 58 | { |
| 59 | return VmMode::Invalid; |
| 60 | } |
| 61 | |
| 62 | auto mode = itemPath.parent_path(); |
| 63 | auto type = mode.parent_path(); |
| 64 | |
| 65 | if (mode.filename().empty() || type.filename().empty()) |
| 66 | { |
| 67 | return VmMode::Invalid; |
| 68 | } |
| 69 | |
| 70 | if (type.filename() != "VirtualMedia") |
| 71 | { |
| 72 | return VmMode::Invalid; |
| 73 | } |
| 74 | std::string modeStr = mode.filename(); |
| 75 | if (modeStr == "Legacy") |
| 76 | { |
| 77 | return VmMode::Legacy; |
| 78 | } |
| 79 | if (modeStr == "Proxy") |
| 80 | { |
| 81 | return VmMode::Proxy; |
| 82 | } |
| 83 | return VmMode::Invalid; |
| 84 | } |
| 85 | |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 86 | using CheckItemHandler = |
| 87 | std::function<void(const std::string& service, const std::string& resName, |
| 88 | const std::shared_ptr<bmcweb::AsyncResp>&, |
George Liu | 70cbdf5 | 2023-03-04 12:07:25 +0800 | [diff] [blame] | 89 | const std::pair<sdbusplus::message::object_path, |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 90 | dbus::utility::DBusInterfacesMap>&)>; |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 91 | |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 92 | inline void |
| 93 | findAndParseObject(const std::string& service, const std::string& resName, |
| 94 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 95 | CheckItemHandler&& handler) |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 96 | { |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 97 | sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia"); |
| 98 | dbus::utility::getManagedObjects( |
| 99 | service, path, |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 100 | [service, resName, asyncResp, |
Lakshmi Yadlapati | 746c5b8 | 2023-03-06 16:07:28 -0600 | [diff] [blame] | 101 | handler](const boost::system::error_code& ec, |
George Liu | 70cbdf5 | 2023-03-04 12:07:25 +0800 | [diff] [blame] | 102 | const dbus::utility::ManagedObjectType& subtree) { |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 103 | if (ec) |
| 104 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 105 | BMCWEB_LOG_DEBUG("DBUS response error"); |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 106 | |
| 107 | return; |
| 108 | } |
| 109 | |
George Liu | 70cbdf5 | 2023-03-04 12:07:25 +0800 | [diff] [blame] | 110 | for (const auto& item : subtree) |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 111 | { |
| 112 | VmMode mode = parseObjectPathAndGetMode(item.first, resName); |
| 113 | if (mode != VmMode::Invalid) |
| 114 | { |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 115 | handler(service, resName, asyncResp, item); |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 120 | BMCWEB_LOG_DEBUG("Parent item not found"); |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 121 | asyncResp->res.result(boost::beast::http::status::not_found); |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 122 | }); |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 125 | /** |
| 126 | * @brief Function extracts transfer protocol name from URI. |
| 127 | */ |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 128 | inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri) |
| 129 | { |
| 130 | boost::urls::result<boost::urls::url_view> url = |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 131 | boost::urls::parse_uri(imageUri); |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 132 | if (!url) |
| 133 | { |
| 134 | return "None"; |
| 135 | } |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 136 | std::string_view scheme = url->scheme(); |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 137 | if (scheme == "smb") |
| 138 | { |
| 139 | return "CIFS"; |
| 140 | } |
| 141 | if (scheme == "https") |
| 142 | { |
| 143 | return "HTTPS"; |
| 144 | } |
| 145 | |
| 146 | return "None"; |
| 147 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * @brief Read all known properties from VM object interfaces |
| 151 | */ |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 152 | inline void |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 153 | vmParseInterfaceObject(const dbus::utility::DBusInterfacesMap& interfaces, |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 154 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 155 | { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 156 | for (const auto& [interface, values] : interfaces) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 157 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 158 | if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint") |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 159 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 160 | for (const auto& [property, value] : values) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 161 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 162 | if (property == "EndpointId") |
| 163 | { |
| 164 | const std::string* endpointIdValue = |
| 165 | std::get_if<std::string>(&value); |
| 166 | if (endpointIdValue == nullptr) |
| 167 | { |
| 168 | continue; |
| 169 | } |
| 170 | if (!endpointIdValue->empty()) |
| 171 | { |
| 172 | // Proxy mode |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 173 | asyncResp->res |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 174 | .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] = |
| 175 | *endpointIdValue; |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 176 | asyncResp->res.jsonValue["TransferProtocolType"] = |
| 177 | "OEM"; |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | if (property == "ImageURL") |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 181 | { |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 182 | const std::string* imageUrlValue = |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 183 | std::get_if<std::string>(&value); |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 184 | if (imageUrlValue != nullptr && !imageUrlValue->empty()) |
Przemyslaw Czarnowski | da4784d | 2020-11-06 09:58:25 +0100 | [diff] [blame] | 185 | { |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 186 | std::filesystem::path filePath = *imageUrlValue; |
| 187 | if (!filePath.has_filename()) |
| 188 | { |
| 189 | // this will handle https share, which not |
| 190 | // necessarily has to have filename given. |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 191 | asyncResp->res.jsonValue["ImageName"] = ""; |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 192 | } |
| 193 | else |
| 194 | { |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 195 | asyncResp->res.jsonValue["ImageName"] = |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 196 | filePath.filename(); |
| 197 | } |
Przemyslaw Czarnowski | da4784d | 2020-11-06 09:58:25 +0100 | [diff] [blame] | 198 | |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 199 | asyncResp->res.jsonValue["Image"] = *imageUrlValue; |
| 200 | asyncResp->res.jsonValue["TransferProtocolType"] = |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 201 | getTransferProtocolTypeFromUri(*imageUrlValue); |
| 202 | |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 203 | asyncResp->res.jsonValue["ConnectedVia"] = |
Ed Tanous | 739b87e | 2023-02-24 13:13:33 -0800 | [diff] [blame] | 204 | virtual_media::ConnectedVia::URI; |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 205 | } |
| 206 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 207 | if (property == "WriteProtected") |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 208 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 209 | const bool* writeProtectedValue = std::get_if<bool>(&value); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 210 | if (writeProtectedValue != nullptr) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 211 | { |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 212 | asyncResp->res.jsonValue["WriteProtected"] = |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 213 | *writeProtectedValue; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 218 | if (interface == "xyz.openbmc_project.VirtualMedia.Process") |
| 219 | { |
| 220 | for (const auto& [property, value] : values) |
| 221 | { |
| 222 | if (property == "Active") |
| 223 | { |
| 224 | const bool* activeValue = std::get_if<bool>(&value); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 225 | if (activeValue == nullptr) |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 226 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 227 | BMCWEB_LOG_DEBUG("Value Active not found"); |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 228 | return; |
| 229 | } |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 230 | asyncResp->res.jsonValue["Inserted"] = *activeValue; |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 231 | |
Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 232 | if (*activeValue) |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 233 | { |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 234 | asyncResp->res.jsonValue["ConnectedVia"] = |
Ed Tanous | 739b87e | 2023-02-24 13:13:33 -0800 | [diff] [blame] | 235 | virtual_media::ConnectedVia::Applet; |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @brief Fill template for Virtual Media Item. |
| 245 | */ |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 246 | inline nlohmann::json vmItemTemplate(const std::string& name, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 247 | const std::string& resName) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 248 | { |
| 249 | nlohmann::json item; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 250 | item["@odata.id"] = boost::urls::format( |
| 251 | "/redfish/v1/Managers/{}/VirtualMedia/{}", name, resName); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 252 | |
Przemyslaw Czarnowski | d04ba32 | 2020-01-21 12:41:56 +0100 | [diff] [blame] | 253 | item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia"; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 254 | item["Name"] = "Virtual Removable Media"; |
| 255 | item["Id"] = resName; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 256 | item["WriteProtected"] = true; |
Ed Tanous | 739b87e | 2023-02-24 13:13:33 -0800 | [diff] [blame] | 257 | item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected; |
Ed Tanous | 613dabe | 2022-07-09 11:17:36 -0700 | [diff] [blame] | 258 | item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"}); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 259 | item["TransferMethod"] = "Stream"; |
Przemyslaw Czarnowski | d04ba32 | 2020-01-21 12:41:56 +0100 | [diff] [blame] | 260 | item["Oem"]["OpenBMC"]["@odata.type"] = |
| 261 | "#OemVirtualMedia.v1_0_0.VirtualMedia"; |
V-Sanjana | 15b8972 | 2023-05-11 16:27:03 +0530 | [diff] [blame] | 262 | item["Oem"]["OpenBMC"]["@odata.id"] = boost::urls::format( |
| 263 | "/redfish/v1/Managers/{}/VirtualMedia/{}#/Oem/OpenBMC", name, resName); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 264 | |
| 265 | return item; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @brief Fills collection data |
| 270 | */ |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 271 | inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> asyncResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 272 | const std::string& service, |
| 273 | const std::string& name) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 274 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 275 | BMCWEB_LOG_DEBUG("Get available Virtual Media resources."); |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 276 | sdbusplus::message::object_path objPath( |
| 277 | "/xyz/openbmc_project/VirtualMedia"); |
| 278 | dbus::utility::getManagedObjects( |
| 279 | service, objPath, |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 280 | [name, asyncResp{std::move(asyncResp)}]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 281 | const boost::system::error_code& ec, |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 282 | const dbus::utility::ManagedObjectType& subtree) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 283 | if (ec) |
| 284 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 285 | BMCWEB_LOG_DEBUG("DBUS response error"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 286 | return; |
| 287 | } |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 288 | nlohmann::json& members = asyncResp->res.jsonValue["Members"]; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 289 | members = nlohmann::json::array(); |
| 290 | |
| 291 | for (const auto& object : subtree) |
| 292 | { |
| 293 | nlohmann::json item; |
| 294 | std::string path = object.first.filename(); |
| 295 | if (path.empty()) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 296 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 297 | continue; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 298 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 299 | |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 300 | item["@odata.id"] = boost::urls::format( |
| 301 | "/redfish/v1/Managers/{}/VirtualMedia/{}", name, path); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 302 | members.emplace_back(std::move(item)); |
| 303 | } |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 304 | asyncResp->res.jsonValue["Members@odata.count"] = members.size(); |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 305 | }); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 306 | } |
| 307 | |
George Liu | 70cbdf5 | 2023-03-04 12:07:25 +0800 | [diff] [blame] | 308 | inline void |
| 309 | afterGetVmData(const std::string& name, const std::string& /*service*/, |
| 310 | const std::string& resName, |
| 311 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 312 | const std::pair<sdbusplus::message::object_path, |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 313 | dbus::utility::DBusInterfacesMap>& item) |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 314 | { |
| 315 | VmMode mode = parseObjectPathAndGetMode(item.first, resName); |
| 316 | if (mode == VmMode::Invalid) |
| 317 | { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | asyncResp->res.jsonValue = vmItemTemplate(name, resName); |
| 322 | |
| 323 | // Check if dbus path is Legacy type |
| 324 | if (mode == VmMode::Legacy) |
| 325 | { |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 326 | asyncResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"] |
| 327 | ["target"] = boost::urls::format( |
| 328 | "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.InsertMedia", |
| 329 | name, resName); |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | vmParseInterfaceObject(item.second, asyncResp); |
| 333 | |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 334 | asyncResp->res.jsonValue["Actions"]["#VirtualMedia.EjectMedia"] |
| 335 | ["target"] = boost::urls::format( |
| 336 | "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.EjectMedia", |
| 337 | name, resName); |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 340 | /** |
| 341 | * @brief Fills data for specific resource |
| 342 | */ |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 343 | inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 344 | const std::string& service, const std::string& name, |
| 345 | const std::string& resName) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 346 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 347 | BMCWEB_LOG_DEBUG("Get Virtual Media resource data."); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 348 | |
Ed Tanous | ac106bf | 2023-06-07 09:24:59 -0700 | [diff] [blame] | 349 | findAndParseObject(service, resName, asyncResp, |
George Liu | 70cbdf5 | 2023-03-04 12:07:25 +0800 | [diff] [blame] | 350 | std::bind_front(afterGetVmData, name)); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 353 | /** |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 354 | * @brief Transfer protocols supported for InsertMedia action. |
| 355 | * |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 356 | */ |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 357 | enum class TransferProtocol |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 358 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 359 | https, |
| 360 | smb, |
| 361 | invalid |
| 362 | }; |
| 363 | |
| 364 | /** |
| 365 | * @brief Function extracts transfer protocol type from URI. |
| 366 | * |
| 367 | */ |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 368 | inline std::optional<TransferProtocol> |
Ed Tanous | d9f466b | 2023-03-06 15:04:25 -0800 | [diff] [blame] | 369 | getTransferProtocolFromUri(boost::urls::url_view imageUri) |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 370 | { |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 371 | std::string_view scheme = imageUri.scheme(); |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 372 | if (scheme == "smb") |
| 373 | { |
| 374 | return TransferProtocol::smb; |
| 375 | } |
| 376 | if (scheme == "https") |
| 377 | { |
| 378 | return TransferProtocol::https; |
| 379 | } |
| 380 | if (!scheme.empty()) |
| 381 | { |
| 382 | return TransferProtocol::invalid; |
| 383 | } |
| 384 | |
| 385 | return {}; |
| 386 | } |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 387 | |
| 388 | /** |
| 389 | * @brief Function convert transfer protocol from string param. |
| 390 | * |
| 391 | */ |
| 392 | inline std::optional<TransferProtocol> getTransferProtocolFromParam( |
| 393 | const std::optional<std::string>& transferProtocolType) |
| 394 | { |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 395 | if (!transferProtocolType) |
Agata Olender | c6f4e01 | 2020-03-11 15:19:07 +0100 | [diff] [blame] | 396 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 397 | return {}; |
Agata Olender | c6f4e01 | 2020-03-11 15:19:07 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 400 | if (*transferProtocolType == "CIFS") |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 401 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 402 | return TransferProtocol::smb; |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 405 | if (*transferProtocolType == "HTTPS") |
| 406 | { |
| 407 | return TransferProtocol::https; |
| 408 | } |
| 409 | |
| 410 | return TransferProtocol::invalid; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * @brief Function extends URI with transfer protocol type. |
| 415 | * |
| 416 | */ |
| 417 | inline std::string |
| 418 | getUriWithTransferProtocol(const std::string& imageUri, |
| 419 | const TransferProtocol& transferProtocol) |
| 420 | { |
| 421 | if (transferProtocol == TransferProtocol::smb) |
| 422 | { |
| 423 | return "smb://" + imageUri; |
| 424 | } |
| 425 | |
| 426 | if (transferProtocol == TransferProtocol::https) |
| 427 | { |
| 428 | return "https://" + imageUri; |
| 429 | } |
| 430 | |
| 431 | return imageUri; |
| 432 | } |
| 433 | |
Przemyslaw Czarnowski | 1f2a40c | 2022-06-24 13:47:08 +0200 | [diff] [blame] | 434 | struct InsertMediaActionParams |
| 435 | { |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 436 | std::optional<std::string> imageUrl; |
Przemyslaw Czarnowski | 1f2a40c | 2022-06-24 13:47:08 +0200 | [diff] [blame] | 437 | std::optional<std::string> userName; |
| 438 | std::optional<std::string> password; |
| 439 | std::optional<std::string> transferMethod; |
| 440 | std::optional<std::string> transferProtocolType; |
| 441 | std::optional<bool> writeProtected = true; |
| 442 | std::optional<bool> inserted; |
| 443 | }; |
| 444 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 445 | template <typename T> |
| 446 | static void secureCleanup(T& value) |
| 447 | { |
Ed Tanous | 4ecc618 | 2022-01-07 09:36:26 -0800 | [diff] [blame] | 448 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 449 | auto raw = const_cast<typename T::value_type*>(value.data()); |
| 450 | explicit_bzero(raw, value.size() * sizeof(*raw)); |
| 451 | } |
| 452 | |
| 453 | class Credentials |
| 454 | { |
| 455 | public: |
| 456 | Credentials(std::string&& user, std::string&& password) : |
| 457 | userBuf(std::move(user)), passBuf(std::move(password)) |
| 458 | {} |
| 459 | |
| 460 | ~Credentials() |
| 461 | { |
| 462 | secureCleanup(userBuf); |
| 463 | secureCleanup(passBuf); |
| 464 | } |
| 465 | |
| 466 | const std::string& user() |
| 467 | { |
| 468 | return userBuf; |
| 469 | } |
| 470 | |
| 471 | const std::string& password() |
| 472 | { |
| 473 | return passBuf; |
| 474 | } |
| 475 | |
| 476 | Credentials() = delete; |
| 477 | Credentials(const Credentials&) = delete; |
| 478 | Credentials& operator=(const Credentials&) = delete; |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 479 | Credentials(Credentials&&) = delete; |
| 480 | Credentials& operator=(Credentials&&) = delete; |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 481 | |
| 482 | private: |
| 483 | std::string userBuf; |
| 484 | std::string passBuf; |
| 485 | }; |
| 486 | |
| 487 | class CredentialsProvider |
| 488 | { |
| 489 | public: |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 490 | template <typename T> |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 491 | struct Deleter |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 492 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 493 | void operator()(T* buff) const |
| 494 | { |
| 495 | if (buff) |
| 496 | { |
| 497 | secureCleanup(*buff); |
| 498 | delete buff; |
| 499 | } |
| 500 | } |
| 501 | }; |
| 502 | |
| 503 | using Buffer = std::vector<char>; |
| 504 | using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>; |
| 505 | // Using explicit definition instead of std::function to avoid implicit |
| 506 | // conversions eg. stack copy instead of reference |
| 507 | using FormatterFunc = void(const std::string& username, |
| 508 | const std::string& password, Buffer& dest); |
| 509 | |
| 510 | CredentialsProvider(std::string&& user, std::string&& password) : |
| 511 | credentials(std::move(user), std::move(password)) |
| 512 | {} |
| 513 | |
| 514 | const std::string& user() |
| 515 | { |
| 516 | return credentials.user(); |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 519 | const std::string& password() |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 520 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 521 | return credentials.password(); |
| 522 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 523 | |
Ed Tanous | 1917ee9 | 2022-06-30 22:30:50 -0700 | [diff] [blame] | 524 | SecureBuffer pack(FormatterFunc* formatter) |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 525 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 526 | SecureBuffer packed{new Buffer{}}; |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 527 | if (formatter != nullptr) |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 528 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 529 | formatter(credentials.user(), credentials.password(), *packed); |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 532 | return packed; |
| 533 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 534 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 535 | private: |
| 536 | Credentials credentials; |
| 537 | }; |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 538 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 539 | // Wrapper for boost::async_pipe ensuring proper pipe cleanup |
Ed Tanous | 0a48306 | 2022-07-11 10:18:50 -0700 | [diff] [blame] | 540 | class SecurePipe |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 541 | { |
| 542 | public: |
| 543 | using unix_fd = sdbusplus::message::unix_fd; |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 544 | |
Ed Tanous | 0a48306 | 2022-07-11 10:18:50 -0700 | [diff] [blame] | 545 | SecurePipe(boost::asio::io_context& io, |
| 546 | CredentialsProvider::SecureBuffer&& bufferIn) : |
| 547 | impl(io), |
| 548 | buffer{std::move(bufferIn)} |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 549 | {} |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 550 | |
Ed Tanous | 0a48306 | 2022-07-11 10:18:50 -0700 | [diff] [blame] | 551 | ~SecurePipe() |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 552 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 553 | // Named pipe needs to be explicitly removed |
| 554 | impl.close(); |
| 555 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 556 | |
Ed Tanous | 0a48306 | 2022-07-11 10:18:50 -0700 | [diff] [blame] | 557 | SecurePipe(const SecurePipe&) = delete; |
| 558 | SecurePipe(SecurePipe&&) = delete; |
| 559 | SecurePipe& operator=(const SecurePipe&) = delete; |
| 560 | SecurePipe& operator=(SecurePipe&&) = delete; |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 561 | |
Ed Tanous | 0a48306 | 2022-07-11 10:18:50 -0700 | [diff] [blame] | 562 | unix_fd fd() const |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 563 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 564 | return unix_fd{impl.native_source()}; |
| 565 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 566 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 567 | template <typename WriteHandler> |
| 568 | void asyncWrite(WriteHandler&& handler) |
| 569 | { |
Ed Tanous | 0a48306 | 2022-07-11 10:18:50 -0700 | [diff] [blame] | 570 | impl.async_write_some(boost::asio::buffer(*buffer), |
| 571 | std::forward<WriteHandler>(handler)); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | const std::string name; |
| 575 | boost::process::async_pipe impl; |
Ed Tanous | 0a48306 | 2022-07-11 10:18:50 -0700 | [diff] [blame] | 576 | CredentialsProvider::SecureBuffer buffer; |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | /** |
| 580 | * @brief Function transceives data with dbus directly. |
| 581 | * |
| 582 | * All BMC state properties will be retrieved before sending reset request. |
| 583 | */ |
| 584 | inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 585 | const std::string& service, const std::string& name, |
| 586 | const std::string& imageUrl, const bool rw, |
| 587 | std::string&& userName, std::string&& password) |
| 588 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 589 | constexpr const size_t secretLimit = 1024; |
| 590 | |
| 591 | std::shared_ptr<SecurePipe> secretPipe; |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 592 | dbus::utility::DbusVariantType unixFd = -1; |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 593 | |
| 594 | if (!userName.empty() || !password.empty()) |
| 595 | { |
| 596 | // Encapsulate in safe buffer |
| 597 | CredentialsProvider credentials(std::move(userName), |
| 598 | std::move(password)); |
| 599 | |
| 600 | // Payload must contain data + NULL delimiters |
| 601 | if (credentials.user().size() + credentials.password().size() + 2 > |
| 602 | secretLimit) |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 603 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 604 | BMCWEB_LOG_ERROR("Credentials too long to handle"); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 605 | messages::unrecognizedRequestBody(asyncResp->res); |
| 606 | return; |
| 607 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 608 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 609 | // Pack secret |
| 610 | auto secret = credentials.pack( |
| 611 | [](const auto& user, const auto& pass, auto& buff) { |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 612 | std::ranges::copy(user, std::back_inserter(buff)); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 613 | buff.push_back('\0'); |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 614 | std::ranges::copy(pass, std::back_inserter(buff)); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 615 | buff.push_back('\0'); |
| 616 | }); |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 617 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 618 | // Open pipe |
| 619 | secretPipe = std::make_shared<SecurePipe>( |
| 620 | crow::connections::systemBus->get_io_context(), std::move(secret)); |
| 621 | unixFd = secretPipe->fd(); |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 622 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 623 | // Pass secret over pipe |
| 624 | secretPipe->asyncWrite( |
| 625 | [asyncResp](const boost::system::error_code& ec, std::size_t) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 626 | if (ec) |
| 627 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 628 | BMCWEB_LOG_ERROR("Failed to pass secret: {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 629 | messages::internalError(asyncResp->res); |
| 630 | } |
| 631 | }); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 632 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 633 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 634 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 635 | [asyncResp, secretPipe](const boost::system::error_code& ec, |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 636 | bool success) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 637 | if (ec) |
| 638 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 639 | BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 640 | messages::internalError(asyncResp->res); |
| 641 | } |
| 642 | else if (!success) |
| 643 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 644 | BMCWEB_LOG_ERROR("Service responded with error"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 645 | messages::generalError(asyncResp->res); |
| 646 | } |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 647 | }, |
| 648 | service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, |
| 649 | "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw, |
| 650 | unixFd); |
| 651 | } |
| 652 | |
| 653 | /** |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 654 | * @brief Function validate parameters of insert media request. |
| 655 | * |
| 656 | */ |
| 657 | inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 658 | const std::string& service, |
| 659 | const std::string& resName, |
| 660 | InsertMediaActionParams& actionParams) |
| 661 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 662 | BMCWEB_LOG_DEBUG("Validation started"); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 663 | // required param imageUrl must not be empty |
| 664 | if (!actionParams.imageUrl) |
| 665 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 666 | BMCWEB_LOG_ERROR("Request action parameter Image is empty."); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 667 | |
| 668 | messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image"); |
| 669 | |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | // optional param inserted must be true |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 674 | if (actionParams.inserted && !*actionParams.inserted) |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 675 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 676 | BMCWEB_LOG_ERROR( |
| 677 | "Request action optional parameter Inserted must be true."); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 678 | |
| 679 | messages::actionParameterNotSupported(asyncResp->res, "Inserted", |
| 680 | "InsertMedia"); |
| 681 | |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | // optional param transferMethod must be stream |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 686 | if (actionParams.transferMethod && |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 687 | (*actionParams.transferMethod != "Stream")) |
| 688 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 689 | BMCWEB_LOG_ERROR("Request action optional parameter " |
| 690 | "TransferMethod must be Stream."); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 691 | |
| 692 | messages::actionParameterNotSupported(asyncResp->res, "TransferMethod", |
| 693 | "InsertMedia"); |
| 694 | |
| 695 | return; |
| 696 | } |
| 697 | boost::urls::result<boost::urls::url_view> url = |
| 698 | boost::urls::parse_uri(*actionParams.imageUrl); |
| 699 | if (!url) |
| 700 | { |
| 701 | messages::actionParameterValueFormatError( |
| 702 | asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia"); |
| 703 | return; |
| 704 | } |
| 705 | std::optional<TransferProtocol> uriTransferProtocolType = |
| 706 | getTransferProtocolFromUri(*url); |
| 707 | |
| 708 | std::optional<TransferProtocol> paramTransferProtocolType = |
| 709 | getTransferProtocolFromParam(actionParams.transferProtocolType); |
| 710 | |
| 711 | // ImageUrl does not contain valid protocol type |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 712 | if (uriTransferProtocolType && |
| 713 | *uriTransferProtocolType == TransferProtocol::invalid) |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 714 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 715 | BMCWEB_LOG_ERROR("Request action parameter ImageUrl must " |
| 716 | "contain specified protocol type from list: " |
| 717 | "(smb, https)."); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 718 | |
| 719 | messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); |
| 720 | |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | // transferProtocolType should contain value from list |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 725 | if (paramTransferProtocolType && |
| 726 | *paramTransferProtocolType == TransferProtocol::invalid) |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 727 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 728 | BMCWEB_LOG_ERROR("Request action parameter TransferProtocolType " |
| 729 | "must be provided with value from list: " |
| 730 | "(CIFS, HTTPS)."); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 731 | |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 732 | messages::propertyValueNotInList( |
| 733 | asyncResp->res, actionParams.transferProtocolType.value_or(""), |
| 734 | "TransferProtocolType"); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 735 | return; |
| 736 | } |
| 737 | |
| 738 | // valid transfer protocol not provided either with URI nor param |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 739 | if (!uriTransferProtocolType && !paramTransferProtocolType) |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 740 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 741 | BMCWEB_LOG_ERROR("Request action parameter ImageUrl must " |
| 742 | "contain specified protocol type or param " |
| 743 | "TransferProtocolType must be provided."); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 744 | |
| 745 | messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); |
| 746 | |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | // valid transfer protocol provided both with URI and param |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 751 | if (paramTransferProtocolType && uriTransferProtocolType) |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 752 | { |
| 753 | // check if protocol is the same for URI and param |
| 754 | if (*paramTransferProtocolType != *uriTransferProtocolType) |
| 755 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 756 | BMCWEB_LOG_ERROR("Request action parameter " |
| 757 | "TransferProtocolType must contain the " |
| 758 | "same protocol type as protocol type " |
| 759 | "provided with param imageUrl."); |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 760 | |
| 761 | messages::actionParameterValueTypeError( |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 762 | asyncResp->res, actionParams.transferProtocolType.value_or(""), |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 763 | "TransferProtocolType", "InsertMedia"); |
| 764 | |
| 765 | return; |
| 766 | } |
| 767 | } |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 768 | if (!paramTransferProtocolType) |
| 769 | { |
| 770 | messages::internalError(asyncResp->res); |
| 771 | return; |
| 772 | } |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 773 | |
| 774 | // validation passed, add protocol to URI if needed |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 775 | if (!uriTransferProtocolType) |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 776 | { |
| 777 | actionParams.imageUrl = getUriWithTransferProtocol( |
| 778 | *actionParams.imageUrl, *paramTransferProtocolType); |
| 779 | } |
| 780 | |
Jayaprakash Mutyala | 452bd8d | 2023-04-18 12:28:38 +0000 | [diff] [blame] | 781 | if (!actionParams.userName) |
| 782 | { |
| 783 | actionParams.userName = ""; |
| 784 | } |
| 785 | |
| 786 | if (!actionParams.password) |
| 787 | { |
| 788 | actionParams.password = ""; |
| 789 | } |
| 790 | |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 791 | doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl, |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 792 | !(actionParams.writeProtected.value_or(false)), |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 793 | std::move(*actionParams.userName), |
| 794 | std::move(*actionParams.password)); |
| 795 | } |
| 796 | |
| 797 | /** |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 798 | * @brief Function transceives data with dbus directly. |
| 799 | * |
| 800 | * All BMC state properties will be retrieved before sending reset request. |
| 801 | */ |
Ed Tanous | 24e740a | 2023-02-24 12:08:58 -0800 | [diff] [blame] | 802 | inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 803 | const std::string& service, const std::string& name, |
| 804 | bool legacy) |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 805 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 806 | // Legacy mount requires parameter with image |
| 807 | if (legacy) |
| 808 | { |
Adrian Ambrożewicz | d6da5be | 2020-01-13 18:31:01 +0100 | [diff] [blame] | 809 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 810 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 811 | if (ec) |
| 812 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 813 | BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 814 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 815 | messages::internalError(asyncResp->res); |
| 816 | return; |
| 817 | } |
Adrian Ambrożewicz | d6da5be | 2020-01-13 18:31:01 +0100 | [diff] [blame] | 818 | }, |
| 819 | service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 820 | "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount"); |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 821 | } |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 822 | else // proxy |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 823 | { |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 824 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 825 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 826 | if (ec) |
| 827 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 828 | BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 829 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 830 | messages::internalError(asyncResp->res); |
| 831 | return; |
| 832 | } |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 833 | }, |
| 834 | service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name, |
| 835 | "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); |
| 836 | } |
| 837 | } |
| 838 | |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 839 | inline void handleManagersVirtualMediaActionInsertPost( |
| 840 | crow::App& app, const crow::Request& req, |
| 841 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 842 | const std::string& name, const std::string& resName) |
| 843 | { |
| 844 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 845 | { |
| 846 | return; |
| 847 | } |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 848 | |
| 849 | constexpr std::string_view action = "VirtualMedia.InsertMedia"; |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 850 | if (name != "bmc") |
| 851 | { |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 852 | messages::resourceNotFound(asyncResp->res, action, resName); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 853 | |
| 854 | return; |
| 855 | } |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 856 | InsertMediaActionParams actionParams; |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 857 | |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 858 | // Read obligatory parameters (url of image) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 859 | if (!json_util::readJsonAction( |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 860 | req, asyncResp->res, "Image", actionParams.imageUrl, |
| 861 | "WriteProtected", actionParams.writeProtected, "UserName", |
| 862 | actionParams.userName, "Password", actionParams.password, |
| 863 | "Inserted", actionParams.inserted, "TransferMethod", |
| 864 | actionParams.transferMethod, "TransferProtocolType", |
| 865 | actionParams.transferProtocolType)) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 866 | { |
| 867 | return; |
| 868 | } |
| 869 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 870 | dbus::utility::getDbusObject( |
| 871 | "/xyz/openbmc_project/VirtualMedia", {}, |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 872 | [asyncResp, action, actionParams, |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 873 | resName](const boost::system::error_code& ec, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 874 | const dbus::utility::MapperGetObject& getObjectType) mutable { |
| 875 | if (ec) |
| 876 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 877 | BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 878 | messages::resourceNotFound(asyncResp->res, action, resName); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 879 | |
| 880 | return; |
| 881 | } |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 882 | |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 883 | std::string service = getObjectType.begin()->first; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 884 | BMCWEB_LOG_DEBUG("GetObjectType: {}", service); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 885 | |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 886 | sdbusplus::message::object_path path( |
| 887 | "/xyz/openbmc_project/VirtualMedia"); |
| 888 | dbus::utility::getManagedObjects( |
| 889 | service, path, |
| 890 | [service, resName, action, actionParams, asyncResp]( |
| 891 | const boost::system::error_code& ec2, |
| 892 | const dbus::utility::ManagedObjectType& subtree) mutable { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 893 | if (ec2) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 894 | { |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 895 | // Not possible in proxy mode |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 896 | BMCWEB_LOG_DEBUG("InsertMedia not " |
| 897 | "allowed in proxy mode"); |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 898 | messages::resourceNotFound(asyncResp->res, action, resName); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 899 | |
| 900 | return; |
| 901 | } |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 902 | for (const auto& object : subtree) |
| 903 | { |
Ed Tanous | 365a73f | 2023-02-24 12:16:49 -0800 | [diff] [blame] | 904 | VmMode mode = parseObjectPathAndGetMode(object.first, resName); |
Boleslaw Ogonczyk Makowski | 5880f0c | 2023-04-14 15:32:40 +0200 | [diff] [blame] | 905 | if (mode == VmMode::Legacy) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 906 | { |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 907 | validateParams(asyncResp, service, resName, actionParams); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 908 | |
| 909 | return; |
| 910 | } |
| 911 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 912 | BMCWEB_LOG_DEBUG("Parent item not found"); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 913 | messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 914 | }); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 915 | }); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | inline void handleManagersVirtualMediaActionEject( |
| 919 | crow::App& app, const crow::Request& req, |
| 920 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 921 | const std::string& managerName, const std::string& resName) |
| 922 | { |
| 923 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 924 | { |
| 925 | return; |
| 926 | } |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 927 | |
| 928 | constexpr std::string_view action = "VirtualMedia.EjectMedia"; |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 929 | if (managerName != "bmc") |
| 930 | { |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 931 | messages::resourceNotFound(asyncResp->res, action, resName); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 932 | |
| 933 | return; |
| 934 | } |
| 935 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 936 | dbus::utility::getDbusObject( |
| 937 | "/xyz/openbmc_project/VirtualMedia", {}, |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 938 | [asyncResp, action, |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 939 | resName](const boost::system::error_code& ec2, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 940 | const dbus::utility::MapperGetObject& getObjectType) { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 941 | if (ec2) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 942 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 943 | BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec2); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 944 | messages::internalError(asyncResp->res); |
| 945 | |
| 946 | return; |
| 947 | } |
| 948 | std::string service = getObjectType.begin()->first; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 949 | BMCWEB_LOG_DEBUG("GetObjectType: {}", service); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 950 | |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 951 | sdbusplus::message::object_path path( |
| 952 | "/xyz/openbmc_project/VirtualMedia"); |
| 953 | dbus::utility::getManagedObjects( |
| 954 | service, path, |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 955 | [resName, service, action, |
| 956 | asyncResp](const boost::system::error_code& ec, |
| 957 | const dbus::utility::ManagedObjectType& subtree) { |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 958 | if (ec) |
| 959 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 960 | BMCWEB_LOG_ERROR("ObjectMapper : No Service found"); |
Przemyslaw Czarnowski | 79fdf63 | 2022-06-28 18:11:59 +0200 | [diff] [blame] | 961 | messages::resourceNotFound(asyncResp->res, action, resName); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 962 | return; |
| 963 | } |
| 964 | |
| 965 | for (const auto& object : subtree) |
| 966 | { |
Ed Tanous | 365a73f | 2023-02-24 12:16:49 -0800 | [diff] [blame] | 967 | VmMode mode = parseObjectPathAndGetMode(object.first, resName); |
| 968 | if (mode != VmMode::Invalid) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 969 | { |
Ed Tanous | 365a73f | 2023-02-24 12:16:49 -0800 | [diff] [blame] | 970 | doEjectAction(asyncResp, service, resName, |
| 971 | mode == VmMode::Legacy); |
Boleslaw Ogonczyk Makowski | 5880f0c | 2023-04-14 15:32:40 +0200 | [diff] [blame] | 972 | return; |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 973 | } |
| 974 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 975 | BMCWEB_LOG_DEBUG("Parent item not found"); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 976 | messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 977 | }); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 978 | }); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | inline void handleManagersVirtualMediaCollectionGet( |
| 982 | crow::App& app, const crow::Request& req, |
| 983 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 984 | const std::string& name) |
| 985 | { |
| 986 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 987 | { |
| 988 | return; |
| 989 | } |
| 990 | if (name != "bmc") |
| 991 | { |
| 992 | messages::resourceNotFound(asyncResp->res, "VirtualMedia", name); |
| 993 | |
| 994 | return; |
| 995 | } |
| 996 | |
| 997 | asyncResp->res.jsonValue["@odata.type"] = |
| 998 | "#VirtualMediaCollection.VirtualMediaCollection"; |
| 999 | asyncResp->res.jsonValue["Name"] = "Virtual Media Services"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 1000 | asyncResp->res.jsonValue["@odata.id"] = |
| 1001 | boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia", name); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1002 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1003 | dbus::utility::getDbusObject( |
| 1004 | "/xyz/openbmc_project/VirtualMedia", {}, |
| 1005 | [asyncResp, name](const boost::system::error_code& ec, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1006 | const dbus::utility::MapperGetObject& getObjectType) { |
| 1007 | if (ec) |
| 1008 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1009 | BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1010 | messages::internalError(asyncResp->res); |
| 1011 | |
| 1012 | return; |
| 1013 | } |
| 1014 | std::string service = getObjectType.begin()->first; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1015 | BMCWEB_LOG_DEBUG("GetObjectType: {}", service); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1016 | |
| 1017 | getVmResourceList(asyncResp, service, name); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1018 | }); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | inline void |
| 1022 | handleVirtualMediaGet(crow::App& app, const crow::Request& req, |
| 1023 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1024 | const std::string& name, const std::string& resName) |
| 1025 | { |
| 1026 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1027 | { |
| 1028 | return; |
| 1029 | } |
| 1030 | if (name != "bmc") |
| 1031 | { |
| 1032 | messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); |
| 1033 | |
| 1034 | return; |
| 1035 | } |
| 1036 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1037 | dbus::utility::getDbusObject( |
| 1038 | "/xyz/openbmc_project/VirtualMedia", {}, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1039 | [asyncResp, name, |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1040 | resName](const boost::system::error_code& ec, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1041 | const dbus::utility::MapperGetObject& getObjectType) { |
| 1042 | if (ec) |
| 1043 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1044 | BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1045 | messages::internalError(asyncResp->res); |
| 1046 | |
| 1047 | return; |
| 1048 | } |
| 1049 | std::string service = getObjectType.begin()->first; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1050 | BMCWEB_LOG_DEBUG("GetObjectType: {}", service); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1051 | |
| 1052 | getVmData(asyncResp, service, name, resName); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1053 | }); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1056 | inline void requestNBDVirtualMediaRoutes(App& app) |
| 1057 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1058 | BMCWEB_ROUTE( |
| 1059 | app, |
| 1060 | "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1061 | .privileges(redfish::privileges::postVirtualMedia) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1062 | .methods(boost::beast::http::verb::post)(std::bind_front( |
| 1063 | handleManagersVirtualMediaActionInsertPost, std::ref(app))); |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 1064 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1065 | BMCWEB_ROUTE( |
| 1066 | app, |
| 1067 | "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1068 | .privileges(redfish::privileges::postVirtualMedia) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1069 | .methods(boost::beast::http::verb::post)(std::bind_front( |
| 1070 | handleManagersVirtualMediaActionEject, std::ref(app))); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1071 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1072 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1073 | .privileges(redfish::privileges::getVirtualMediaCollection) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1074 | .methods(boost::beast::http::verb::get)(std::bind_front( |
| 1075 | handleManagersVirtualMediaCollectionGet, std::ref(app))); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 1076 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1077 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1078 | .privileges(redfish::privileges::getVirtualMedia) |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1079 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1080 | std::bind_front(handleVirtualMediaGet, std::ref(app))); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1081 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 1082 | |
| 1083 | } // namespace redfish |