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