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