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