1107077deSPrzemyslaw Czarnowski /* 2107077deSPrzemyslaw Czarnowski // Copyright (c) 2018 Intel Corporation 3107077deSPrzemyslaw Czarnowski // 4107077deSPrzemyslaw Czarnowski // Licensed under the Apache License, Version 2.0 (the "License"); 5107077deSPrzemyslaw Czarnowski // you may not use this file except in compliance with the License. 6107077deSPrzemyslaw Czarnowski // You may obtain a copy of the License at 7107077deSPrzemyslaw Czarnowski // 8107077deSPrzemyslaw Czarnowski // http://www.apache.org/licenses/LICENSE-2.0 9107077deSPrzemyslaw Czarnowski // 10107077deSPrzemyslaw Czarnowski // Unless required by applicable law or agreed to in writing, software 11107077deSPrzemyslaw Czarnowski // distributed under the License is distributed on an "AS IS" BASIS, 12107077deSPrzemyslaw Czarnowski // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13107077deSPrzemyslaw Czarnowski // See the License for the specific language governing permissions and 14107077deSPrzemyslaw Czarnowski // limitations under the License. 15107077deSPrzemyslaw Czarnowski */ 16107077deSPrzemyslaw Czarnowski #pragma once 17107077deSPrzemyslaw Czarnowski 183ccb3adbSEd Tanous #include "account_service.hpp" 193ccb3adbSEd Tanous #include "app.hpp" 2079fdf63eSPrzemyslaw Czarnowski #include "async_resp.hpp" 212b73119cSGeorge Liu #include "dbus_utility.hpp" 22739b87efSEd Tanous #include "generated/enums/virtual_media.hpp" 233ccb3adbSEd Tanous #include "query.hpp" 243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 253ccb3adbSEd Tanous #include "utils/json_utils.hpp" 263ccb3adbSEd Tanous 27988fb7b2SAdrian Ambrożewicz #include <boost/process/async_pipe.hpp> 28ef4c65b7SEd Tanous #include <boost/url/format.hpp> 299e319cf0SAnna Platash #include <boost/url/url_view.hpp> 30107077deSPrzemyslaw Czarnowski 312b73119cSGeorge Liu #include <array> 323544d2a7SEd Tanous #include <ranges> 332b73119cSGeorge Liu #include <string_view> 342b73119cSGeorge Liu 35107077deSPrzemyslaw Czarnowski namespace redfish 36107077deSPrzemyslaw Czarnowski { 37365a73f4SEd Tanous 38365a73f4SEd Tanous enum class VmMode 39365a73f4SEd Tanous { 40365a73f4SEd Tanous Invalid, 41365a73f4SEd Tanous Legacy, 42365a73f4SEd Tanous Proxy 43365a73f4SEd Tanous }; 44365a73f4SEd Tanous 45365a73f4SEd Tanous inline VmMode 46365a73f4SEd Tanous parseObjectPathAndGetMode(const sdbusplus::message::object_path& itemPath, 47365a73f4SEd Tanous const std::string& resName) 48365a73f4SEd Tanous { 49365a73f4SEd Tanous std::string thisPath = itemPath.filename(); 5062598e31SEd Tanous BMCWEB_LOG_DEBUG("Filename: {}, ThisPath: {}", itemPath.str, thisPath); 51365a73f4SEd Tanous 52365a73f4SEd Tanous if (thisPath.empty()) 53365a73f4SEd Tanous { 54365a73f4SEd Tanous return VmMode::Invalid; 55365a73f4SEd Tanous } 56365a73f4SEd Tanous 57365a73f4SEd Tanous if (thisPath != resName) 58365a73f4SEd Tanous { 59365a73f4SEd Tanous return VmMode::Invalid; 60365a73f4SEd Tanous } 61365a73f4SEd Tanous 62365a73f4SEd Tanous auto mode = itemPath.parent_path(); 63365a73f4SEd Tanous auto type = mode.parent_path(); 64365a73f4SEd Tanous 65365a73f4SEd Tanous if (mode.filename().empty() || type.filename().empty()) 66365a73f4SEd Tanous { 67365a73f4SEd Tanous return VmMode::Invalid; 68365a73f4SEd Tanous } 69365a73f4SEd Tanous 70365a73f4SEd Tanous if (type.filename() != "VirtualMedia") 71365a73f4SEd Tanous { 72365a73f4SEd Tanous return VmMode::Invalid; 73365a73f4SEd Tanous } 74365a73f4SEd Tanous std::string modeStr = mode.filename(); 75365a73f4SEd Tanous if (modeStr == "Legacy") 76365a73f4SEd Tanous { 77365a73f4SEd Tanous return VmMode::Legacy; 78365a73f4SEd Tanous } 79365a73f4SEd Tanous if (modeStr == "Proxy") 80365a73f4SEd Tanous { 81365a73f4SEd Tanous return VmMode::Proxy; 82365a73f4SEd Tanous } 83365a73f4SEd Tanous return VmMode::Invalid; 84365a73f4SEd Tanous } 85365a73f4SEd Tanous 8679fdf63eSPrzemyslaw Czarnowski using CheckItemHandler = 8779fdf63eSPrzemyslaw Czarnowski std::function<void(const std::string& service, const std::string& resName, 8879fdf63eSPrzemyslaw Czarnowski const std::shared_ptr<bmcweb::AsyncResp>&, 8970cbdf53SGeorge Liu const std::pair<sdbusplus::message::object_path, 9080f79a40SMichael Shen dbus::utility::DBusInterfacesMap>&)>; 9179fdf63eSPrzemyslaw Czarnowski 92ac106bf6SEd Tanous inline void 93ac106bf6SEd Tanous findAndParseObject(const std::string& service, const std::string& resName, 94ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 9579fdf63eSPrzemyslaw Czarnowski CheckItemHandler&& handler) 9679fdf63eSPrzemyslaw Czarnowski { 975eb468daSGeorge Liu sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia"); 985eb468daSGeorge Liu dbus::utility::getManagedObjects( 995eb468daSGeorge Liu service, path, 100ac106bf6SEd Tanous [service, resName, asyncResp, 101746c5b8aSLakshmi Yadlapati handler](const boost::system::error_code& ec, 10270cbdf53SGeorge Liu const dbus::utility::ManagedObjectType& subtree) { 10379fdf63eSPrzemyslaw Czarnowski if (ec) 10479fdf63eSPrzemyslaw Czarnowski { 10562598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error"); 10679fdf63eSPrzemyslaw Czarnowski 10779fdf63eSPrzemyslaw Czarnowski return; 10879fdf63eSPrzemyslaw Czarnowski } 10979fdf63eSPrzemyslaw Czarnowski 11070cbdf53SGeorge Liu for (const auto& item : subtree) 11179fdf63eSPrzemyslaw Czarnowski { 11279fdf63eSPrzemyslaw Czarnowski VmMode mode = parseObjectPathAndGetMode(item.first, resName); 11379fdf63eSPrzemyslaw Czarnowski if (mode != VmMode::Invalid) 11479fdf63eSPrzemyslaw Czarnowski { 115ac106bf6SEd Tanous handler(service, resName, asyncResp, item); 11679fdf63eSPrzemyslaw Czarnowski return; 11779fdf63eSPrzemyslaw Czarnowski } 11879fdf63eSPrzemyslaw Czarnowski } 11979fdf63eSPrzemyslaw Czarnowski 12062598e31SEd Tanous BMCWEB_LOG_DEBUG("Parent item not found"); 121ac106bf6SEd Tanous asyncResp->res.result(boost::beast::http::status::not_found); 1225eb468daSGeorge Liu }); 12379fdf63eSPrzemyslaw Czarnowski } 12479fdf63eSPrzemyslaw Czarnowski 1259e319cf0SAnna Platash /** 1269e319cf0SAnna Platash * @brief Function extracts transfer protocol name from URI. 1279e319cf0SAnna Platash */ 12867df073bSEd Tanous inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri) 12967df073bSEd Tanous { 13067df073bSEd Tanous boost::urls::result<boost::urls::url_view> url = 131079360aeSEd Tanous boost::urls::parse_uri(imageUri); 13267df073bSEd Tanous if (!url) 13367df073bSEd Tanous { 13467df073bSEd Tanous return "None"; 13567df073bSEd Tanous } 136079360aeSEd Tanous std::string_view scheme = url->scheme(); 13767df073bSEd Tanous if (scheme == "smb") 13867df073bSEd Tanous { 13967df073bSEd Tanous return "CIFS"; 14067df073bSEd Tanous } 14167df073bSEd Tanous if (scheme == "https") 14267df073bSEd Tanous { 14367df073bSEd Tanous return "HTTPS"; 14467df073bSEd Tanous } 14567df073bSEd Tanous 14667df073bSEd Tanous return "None"; 14767df073bSEd Tanous } 148107077deSPrzemyslaw Czarnowski 149107077deSPrzemyslaw Czarnowski /** 150107077deSPrzemyslaw Czarnowski * @brief Read all known properties from VM object interfaces 151107077deSPrzemyslaw Czarnowski */ 15222db1728SEd Tanous inline void 15380f79a40SMichael Shen vmParseInterfaceObject(const dbus::utility::DBusInterfacesMap& interfaces, 154ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 155107077deSPrzemyslaw Czarnowski { 1568a592810SEd Tanous for (const auto& [interface, values] : interfaces) 157107077deSPrzemyslaw Czarnowski { 158711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint") 159107077deSPrzemyslaw Czarnowski { 160711ac7a9SEd Tanous for (const auto& [property, value] : values) 161107077deSPrzemyslaw Czarnowski { 162711ac7a9SEd Tanous if (property == "EndpointId") 163107077deSPrzemyslaw Czarnowski { 164107077deSPrzemyslaw Czarnowski const std::string* endpointIdValue = 165711ac7a9SEd Tanous std::get_if<std::string>(&value); 166711ac7a9SEd Tanous if (endpointIdValue == nullptr) 167107077deSPrzemyslaw Czarnowski { 168711ac7a9SEd Tanous continue; 169711ac7a9SEd Tanous } 170107077deSPrzemyslaw Czarnowski if (!endpointIdValue->empty()) 171107077deSPrzemyslaw Czarnowski { 172107077deSPrzemyslaw Czarnowski // Proxy mode 173ac106bf6SEd Tanous asyncResp->res 174711ac7a9SEd Tanous .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] = 175d04ba325SPrzemyslaw Czarnowski *endpointIdValue; 176ac106bf6SEd Tanous asyncResp->res.jsonValue["TransferProtocolType"] = 177ac106bf6SEd Tanous "OEM"; 178107077deSPrzemyslaw Czarnowski } 179107077deSPrzemyslaw Czarnowski } 180711ac7a9SEd Tanous if (property == "ImageURL") 181107077deSPrzemyslaw Czarnowski { 182107077deSPrzemyslaw Czarnowski const std::string* imageUrlValue = 183711ac7a9SEd Tanous std::get_if<std::string>(&value); 18426f6976fSEd Tanous if (imageUrlValue != nullptr && !imageUrlValue->empty()) 185107077deSPrzemyslaw Czarnowski { 186da4784d8SPrzemyslaw Czarnowski std::filesystem::path filePath = *imageUrlValue; 187da4784d8SPrzemyslaw Czarnowski if (!filePath.has_filename()) 188da4784d8SPrzemyslaw Czarnowski { 1899e319cf0SAnna Platash // this will handle https share, which not 1909e319cf0SAnna Platash // necessarily has to have filename given. 191ac106bf6SEd Tanous asyncResp->res.jsonValue["ImageName"] = ""; 192da4784d8SPrzemyslaw Czarnowski } 193da4784d8SPrzemyslaw Czarnowski else 194da4784d8SPrzemyslaw Czarnowski { 195ac106bf6SEd Tanous asyncResp->res.jsonValue["ImageName"] = 1969e319cf0SAnna Platash filePath.filename(); 197da4784d8SPrzemyslaw Czarnowski } 198da4784d8SPrzemyslaw Czarnowski 199ac106bf6SEd Tanous asyncResp->res.jsonValue["Image"] = *imageUrlValue; 200ac106bf6SEd Tanous asyncResp->res.jsonValue["TransferProtocolType"] = 2019e319cf0SAnna Platash getTransferProtocolTypeFromUri(*imageUrlValue); 2029e319cf0SAnna Platash 203ac106bf6SEd Tanous asyncResp->res.jsonValue["ConnectedVia"] = 204739b87efSEd Tanous virtual_media::ConnectedVia::URI; 205107077deSPrzemyslaw Czarnowski } 206107077deSPrzemyslaw Czarnowski } 207711ac7a9SEd Tanous if (property == "WriteProtected") 2089e319cf0SAnna Platash { 209711ac7a9SEd Tanous const bool* writeProtectedValue = std::get_if<bool>(&value); 210e662eae8SEd Tanous if (writeProtectedValue != nullptr) 2119e319cf0SAnna Platash { 212ac106bf6SEd Tanous asyncResp->res.jsonValue["WriteProtected"] = 2139e319cf0SAnna Platash *writeProtectedValue; 2149e319cf0SAnna Platash } 2159e319cf0SAnna Platash } 2169e319cf0SAnna Platash } 217107077deSPrzemyslaw Czarnowski } 218711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.VirtualMedia.Process") 219711ac7a9SEd Tanous { 220711ac7a9SEd Tanous for (const auto& [property, value] : values) 221711ac7a9SEd Tanous { 222711ac7a9SEd Tanous if (property == "Active") 223711ac7a9SEd Tanous { 224711ac7a9SEd Tanous const bool* activeValue = std::get_if<bool>(&value); 225e662eae8SEd Tanous if (activeValue == nullptr) 226711ac7a9SEd Tanous { 22762598e31SEd Tanous BMCWEB_LOG_DEBUG("Value Active not found"); 228711ac7a9SEd Tanous return; 229711ac7a9SEd Tanous } 230ac106bf6SEd Tanous asyncResp->res.jsonValue["Inserted"] = *activeValue; 231711ac7a9SEd Tanous 232e05aec50SEd Tanous if (*activeValue) 233711ac7a9SEd Tanous { 234ac106bf6SEd Tanous asyncResp->res.jsonValue["ConnectedVia"] = 235739b87efSEd Tanous virtual_media::ConnectedVia::Applet; 236711ac7a9SEd Tanous } 237711ac7a9SEd Tanous } 238711ac7a9SEd Tanous } 239711ac7a9SEd Tanous } 240107077deSPrzemyslaw Czarnowski } 241107077deSPrzemyslaw Czarnowski } 242107077deSPrzemyslaw Czarnowski 243107077deSPrzemyslaw Czarnowski /** 244107077deSPrzemyslaw Czarnowski * @brief Fill template for Virtual Media Item. 245107077deSPrzemyslaw Czarnowski */ 24622db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name, 247107077deSPrzemyslaw Czarnowski const std::string& resName) 248107077deSPrzemyslaw Czarnowski { 249107077deSPrzemyslaw Czarnowski nlohmann::json item; 250ef4c65b7SEd Tanous item["@odata.id"] = boost::urls::format( 251ef4c65b7SEd Tanous "/redfish/v1/Managers/{}/VirtualMedia/{}", name, resName); 25222db1728SEd Tanous 253d04ba325SPrzemyslaw Czarnowski item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia"; 254107077deSPrzemyslaw Czarnowski item["Name"] = "Virtual Removable Media"; 255107077deSPrzemyslaw Czarnowski item["Id"] = resName; 256107077deSPrzemyslaw Czarnowski item["WriteProtected"] = true; 257739b87efSEd Tanous item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected; 258613dabeaSEd Tanous item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"}); 259107077deSPrzemyslaw Czarnowski item["TransferMethod"] = "Stream"; 260d04ba325SPrzemyslaw Czarnowski item["Oem"]["OpenBMC"]["@odata.type"] = 261d04ba325SPrzemyslaw Czarnowski "#OemVirtualMedia.v1_0_0.VirtualMedia"; 26215b89725SV-Sanjana item["Oem"]["OpenBMC"]["@odata.id"] = boost::urls::format( 26315b89725SV-Sanjana "/redfish/v1/Managers/{}/VirtualMedia/{}#/Oem/OpenBMC", name, resName); 264107077deSPrzemyslaw Czarnowski 265107077deSPrzemyslaw Czarnowski return item; 266107077deSPrzemyslaw Czarnowski } 267107077deSPrzemyslaw Czarnowski 268107077deSPrzemyslaw Czarnowski /** 269107077deSPrzemyslaw Czarnowski * @brief Fills collection data 270107077deSPrzemyslaw Czarnowski */ 271ac106bf6SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> asyncResp, 272107077deSPrzemyslaw Czarnowski const std::string& service, 273107077deSPrzemyslaw Czarnowski const std::string& name) 274107077deSPrzemyslaw Czarnowski { 27562598e31SEd Tanous BMCWEB_LOG_DEBUG("Get available Virtual Media resources."); 2765eb468daSGeorge Liu sdbusplus::message::object_path objPath( 2775eb468daSGeorge Liu "/xyz/openbmc_project/VirtualMedia"); 2785eb468daSGeorge Liu dbus::utility::getManagedObjects( 2795eb468daSGeorge Liu service, objPath, 280ac106bf6SEd Tanous [name, asyncResp{std::move(asyncResp)}]( 2815e7e2dc5SEd Tanous const boost::system::error_code& ec, 28202cad96eSEd Tanous const dbus::utility::ManagedObjectType& subtree) { 283107077deSPrzemyslaw Czarnowski if (ec) 284107077deSPrzemyslaw Czarnowski { 28562598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error"); 286107077deSPrzemyslaw Czarnowski return; 287107077deSPrzemyslaw Czarnowski } 288ac106bf6SEd Tanous nlohmann::json& members = asyncResp->res.jsonValue["Members"]; 289107077deSPrzemyslaw Czarnowski members = nlohmann::json::array(); 290107077deSPrzemyslaw Czarnowski 291107077deSPrzemyslaw Czarnowski for (const auto& object : subtree) 292107077deSPrzemyslaw Czarnowski { 293107077deSPrzemyslaw Czarnowski nlohmann::json item; 2942dfd18efSEd Tanous std::string path = object.first.filename(); 2952dfd18efSEd Tanous if (path.empty()) 296107077deSPrzemyslaw Czarnowski { 297107077deSPrzemyslaw Czarnowski continue; 298107077deSPrzemyslaw Czarnowski } 299107077deSPrzemyslaw Czarnowski 300ef4c65b7SEd Tanous item["@odata.id"] = boost::urls::format( 301ef4c65b7SEd Tanous "/redfish/v1/Managers/{}/VirtualMedia/{}", name, path); 302107077deSPrzemyslaw Czarnowski members.emplace_back(std::move(item)); 303107077deSPrzemyslaw Czarnowski } 304ac106bf6SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = members.size(); 3055eb468daSGeorge Liu }); 306107077deSPrzemyslaw Czarnowski } 307107077deSPrzemyslaw Czarnowski 30870cbdf53SGeorge Liu inline void 30970cbdf53SGeorge Liu afterGetVmData(const std::string& name, const std::string& /*service*/, 31079fdf63eSPrzemyslaw Czarnowski const std::string& resName, 31179fdf63eSPrzemyslaw Czarnowski const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 31270cbdf53SGeorge Liu const std::pair<sdbusplus::message::object_path, 31380f79a40SMichael Shen dbus::utility::DBusInterfacesMap>& item) 31479fdf63eSPrzemyslaw Czarnowski { 31579fdf63eSPrzemyslaw Czarnowski VmMode mode = parseObjectPathAndGetMode(item.first, resName); 31679fdf63eSPrzemyslaw Czarnowski if (mode == VmMode::Invalid) 31779fdf63eSPrzemyslaw Czarnowski { 31879fdf63eSPrzemyslaw Czarnowski return; 31979fdf63eSPrzemyslaw Czarnowski } 32079fdf63eSPrzemyslaw Czarnowski 32179fdf63eSPrzemyslaw Czarnowski asyncResp->res.jsonValue = vmItemTemplate(name, resName); 32279fdf63eSPrzemyslaw Czarnowski 32379fdf63eSPrzemyslaw Czarnowski // Check if dbus path is Legacy type 32479fdf63eSPrzemyslaw Czarnowski if (mode == VmMode::Legacy) 32579fdf63eSPrzemyslaw Czarnowski { 326ef4c65b7SEd Tanous asyncResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"] 327ef4c65b7SEd Tanous ["target"] = boost::urls::format( 328ef4c65b7SEd Tanous "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.InsertMedia", 329ef4c65b7SEd Tanous name, resName); 33079fdf63eSPrzemyslaw Czarnowski } 33179fdf63eSPrzemyslaw Czarnowski 33279fdf63eSPrzemyslaw Czarnowski vmParseInterfaceObject(item.second, asyncResp); 33379fdf63eSPrzemyslaw Czarnowski 334ef4c65b7SEd Tanous asyncResp->res.jsonValue["Actions"]["#VirtualMedia.EjectMedia"] 335ef4c65b7SEd Tanous ["target"] = boost::urls::format( 336ef4c65b7SEd Tanous "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.EjectMedia", 337ef4c65b7SEd Tanous name, resName); 33879fdf63eSPrzemyslaw Czarnowski } 33979fdf63eSPrzemyslaw Czarnowski 340107077deSPrzemyslaw Czarnowski /** 341107077deSPrzemyslaw Czarnowski * @brief Fills data for specific resource 342107077deSPrzemyslaw Czarnowski */ 343ac106bf6SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 344107077deSPrzemyslaw Czarnowski const std::string& service, const std::string& name, 345107077deSPrzemyslaw Czarnowski const std::string& resName) 346107077deSPrzemyslaw Czarnowski { 34762598e31SEd Tanous BMCWEB_LOG_DEBUG("Get Virtual Media resource data."); 348107077deSPrzemyslaw Czarnowski 349ac106bf6SEd Tanous findAndParseObject(service, resName, asyncResp, 35070cbdf53SGeorge Liu std::bind_front(afterGetVmData, name)); 351107077deSPrzemyslaw Czarnowski } 352107077deSPrzemyslaw Czarnowski 353e13c2760SPrzemyslaw Czarnowski /** 354c6f4e017SAgata Olender * @brief Transfer protocols supported for InsertMedia action. 355c6f4e017SAgata Olender * 356c6f4e017SAgata Olender */ 357c6f4e017SAgata Olender enum class TransferProtocol 358c6f4e017SAgata Olender { 359c6f4e017SAgata Olender https, 360c6f4e017SAgata Olender smb, 361c6f4e017SAgata Olender invalid 362c6f4e017SAgata Olender }; 363c6f4e017SAgata Olender 364c6f4e017SAgata Olender /** 365c6f4e017SAgata Olender * @brief Function extracts transfer protocol type from URI. 366c6f4e017SAgata Olender * 367c6f4e017SAgata Olender */ 36867df073bSEd Tanous inline std::optional<TransferProtocol> 369d9f466b3SEd Tanous getTransferProtocolFromUri(boost::urls::url_view imageUri) 37067df073bSEd Tanous { 371079360aeSEd Tanous std::string_view scheme = imageUri.scheme(); 37267df073bSEd Tanous if (scheme == "smb") 37367df073bSEd Tanous { 37467df073bSEd Tanous return TransferProtocol::smb; 37567df073bSEd Tanous } 37667df073bSEd Tanous if (scheme == "https") 37767df073bSEd Tanous { 37867df073bSEd Tanous return TransferProtocol::https; 37967df073bSEd Tanous } 38067df073bSEd Tanous if (!scheme.empty()) 38167df073bSEd Tanous { 38267df073bSEd Tanous return TransferProtocol::invalid; 38367df073bSEd Tanous } 38467df073bSEd Tanous 38567df073bSEd Tanous return {}; 38667df073bSEd Tanous } 387c6f4e017SAgata Olender 388c6f4e017SAgata Olender /** 389c6f4e017SAgata Olender * @brief Function convert transfer protocol from string param. 390c6f4e017SAgata Olender * 391c6f4e017SAgata Olender */ 39222db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam( 393c6f4e017SAgata Olender const std::optional<std::string>& transferProtocolType) 394c6f4e017SAgata Olender { 395e01d0c36SEd Tanous if (!transferProtocolType) 396c6f4e017SAgata Olender { 397c6f4e017SAgata Olender return {}; 398c6f4e017SAgata Olender } 399c6f4e017SAgata Olender 400c6f4e017SAgata Olender if (*transferProtocolType == "CIFS") 401c6f4e017SAgata Olender { 402c6f4e017SAgata Olender return TransferProtocol::smb; 403c6f4e017SAgata Olender } 404c6f4e017SAgata Olender 405c6f4e017SAgata Olender if (*transferProtocolType == "HTTPS") 406c6f4e017SAgata Olender { 407c6f4e017SAgata Olender return TransferProtocol::https; 408c6f4e017SAgata Olender } 409c6f4e017SAgata Olender 410c6f4e017SAgata Olender return TransferProtocol::invalid; 411c6f4e017SAgata Olender } 412c6f4e017SAgata Olender 413c6f4e017SAgata Olender /** 414c6f4e017SAgata Olender * @brief Function extends URI with transfer protocol type. 415c6f4e017SAgata Olender * 416c6f4e017SAgata Olender */ 41722db1728SEd Tanous inline std::string 418c6f4e017SAgata Olender getUriWithTransferProtocol(const std::string& imageUri, 419c6f4e017SAgata Olender const TransferProtocol& transferProtocol) 420c6f4e017SAgata Olender { 421c6f4e017SAgata Olender if (transferProtocol == TransferProtocol::smb) 422c6f4e017SAgata Olender { 423c6f4e017SAgata Olender return "smb://" + imageUri; 424c6f4e017SAgata Olender } 425c6f4e017SAgata Olender 426c6f4e017SAgata Olender if (transferProtocol == TransferProtocol::https) 427c6f4e017SAgata Olender { 428c6f4e017SAgata Olender return "https://" + imageUri; 429c6f4e017SAgata Olender } 430c6f4e017SAgata Olender 431c6f4e017SAgata Olender return imageUri; 432c6f4e017SAgata Olender } 433c6f4e017SAgata Olender 4341f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams 4351f2a40ceSPrzemyslaw Czarnowski { 436120fa86aSPrzemyslaw Czarnowski std::optional<std::string> imageUrl; 4371f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> userName; 4381f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> password; 4391f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> transferMethod; 4401f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> transferProtocolType; 4411f2a40ceSPrzemyslaw Czarnowski std::optional<bool> writeProtected = true; 4421f2a40ceSPrzemyslaw Czarnowski std::optional<bool> inserted; 4431f2a40ceSPrzemyslaw Czarnowski }; 4441f2a40ceSPrzemyslaw Czarnowski 4451214b7e7SGunnar Mills template <typename T> 4461214b7e7SGunnar Mills static void secureCleanup(T& value) 447988fb7b2SAdrian Ambrożewicz { 4484ecc618fSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) 449988fb7b2SAdrian Ambrożewicz auto raw = const_cast<typename T::value_type*>(value.data()); 450988fb7b2SAdrian Ambrożewicz explicit_bzero(raw, value.size() * sizeof(*raw)); 451988fb7b2SAdrian Ambrożewicz } 452988fb7b2SAdrian Ambrożewicz 453988fb7b2SAdrian Ambrożewicz class Credentials 454988fb7b2SAdrian Ambrożewicz { 455988fb7b2SAdrian Ambrożewicz public: 456988fb7b2SAdrian Ambrożewicz Credentials(std::string&& user, std::string&& password) : 457988fb7b2SAdrian Ambrożewicz userBuf(std::move(user)), passBuf(std::move(password)) 4581214b7e7SGunnar Mills {} 459988fb7b2SAdrian Ambrożewicz 460988fb7b2SAdrian Ambrożewicz ~Credentials() 461988fb7b2SAdrian Ambrożewicz { 462988fb7b2SAdrian Ambrożewicz secureCleanup(userBuf); 463988fb7b2SAdrian Ambrożewicz secureCleanup(passBuf); 464988fb7b2SAdrian Ambrożewicz } 465988fb7b2SAdrian Ambrożewicz 466988fb7b2SAdrian Ambrożewicz const std::string& user() 467988fb7b2SAdrian Ambrożewicz { 468988fb7b2SAdrian Ambrożewicz return userBuf; 469988fb7b2SAdrian Ambrożewicz } 470988fb7b2SAdrian Ambrożewicz 471988fb7b2SAdrian Ambrożewicz const std::string& password() 472988fb7b2SAdrian Ambrożewicz { 473988fb7b2SAdrian Ambrożewicz return passBuf; 474988fb7b2SAdrian Ambrożewicz } 475988fb7b2SAdrian Ambrożewicz 476988fb7b2SAdrian Ambrożewicz Credentials() = delete; 477988fb7b2SAdrian Ambrożewicz Credentials(const Credentials&) = delete; 478988fb7b2SAdrian Ambrożewicz Credentials& operator=(const Credentials&) = delete; 479ecd6a3a2SEd Tanous Credentials(Credentials&&) = delete; 480ecd6a3a2SEd Tanous Credentials& operator=(Credentials&&) = delete; 481988fb7b2SAdrian Ambrożewicz 48222db1728SEd Tanous private: 483988fb7b2SAdrian Ambrożewicz std::string userBuf; 484988fb7b2SAdrian Ambrożewicz std::string passBuf; 485988fb7b2SAdrian Ambrożewicz }; 486988fb7b2SAdrian Ambrożewicz 487988fb7b2SAdrian Ambrożewicz class CredentialsProvider 488988fb7b2SAdrian Ambrożewicz { 489988fb7b2SAdrian Ambrożewicz public: 4901214b7e7SGunnar Mills template <typename T> 4911214b7e7SGunnar Mills struct Deleter 492988fb7b2SAdrian Ambrożewicz { 493988fb7b2SAdrian Ambrożewicz void operator()(T* buff) const 494988fb7b2SAdrian Ambrożewicz { 495988fb7b2SAdrian Ambrożewicz if (buff) 496988fb7b2SAdrian Ambrożewicz { 497988fb7b2SAdrian Ambrożewicz secureCleanup(*buff); 498988fb7b2SAdrian Ambrożewicz delete buff; 499988fb7b2SAdrian Ambrożewicz } 500988fb7b2SAdrian Ambrożewicz } 501988fb7b2SAdrian Ambrożewicz }; 502988fb7b2SAdrian Ambrożewicz 503988fb7b2SAdrian Ambrożewicz using Buffer = std::vector<char>; 504988fb7b2SAdrian Ambrożewicz using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>; 505988fb7b2SAdrian Ambrożewicz // Using explicit definition instead of std::function to avoid implicit 506988fb7b2SAdrian Ambrożewicz // conversions eg. stack copy instead of reference 507988fb7b2SAdrian Ambrożewicz using FormatterFunc = void(const std::string& username, 508988fb7b2SAdrian Ambrożewicz const std::string& password, Buffer& dest); 509988fb7b2SAdrian Ambrożewicz 510988fb7b2SAdrian Ambrożewicz CredentialsProvider(std::string&& user, std::string&& password) : 511988fb7b2SAdrian Ambrożewicz credentials(std::move(user), std::move(password)) 5121214b7e7SGunnar Mills {} 513988fb7b2SAdrian Ambrożewicz 514988fb7b2SAdrian Ambrożewicz const std::string& user() 515988fb7b2SAdrian Ambrożewicz { 516988fb7b2SAdrian Ambrożewicz return credentials.user(); 517988fb7b2SAdrian Ambrożewicz } 518988fb7b2SAdrian Ambrożewicz 519988fb7b2SAdrian Ambrożewicz const std::string& password() 520988fb7b2SAdrian Ambrożewicz { 521988fb7b2SAdrian Ambrożewicz return credentials.password(); 522988fb7b2SAdrian Ambrożewicz } 523988fb7b2SAdrian Ambrożewicz 5241917ee95SEd Tanous SecureBuffer pack(FormatterFunc* formatter) 525988fb7b2SAdrian Ambrożewicz { 526988fb7b2SAdrian Ambrożewicz SecureBuffer packed{new Buffer{}}; 527e662eae8SEd Tanous if (formatter != nullptr) 528988fb7b2SAdrian Ambrożewicz { 529988fb7b2SAdrian Ambrożewicz formatter(credentials.user(), credentials.password(), *packed); 530988fb7b2SAdrian Ambrożewicz } 531988fb7b2SAdrian Ambrożewicz 532988fb7b2SAdrian Ambrożewicz return packed; 533988fb7b2SAdrian Ambrożewicz } 534988fb7b2SAdrian Ambrożewicz 535988fb7b2SAdrian Ambrożewicz private: 536988fb7b2SAdrian Ambrożewicz Credentials credentials; 537988fb7b2SAdrian Ambrożewicz }; 538988fb7b2SAdrian Ambrożewicz 539988fb7b2SAdrian Ambrożewicz // Wrapper for boost::async_pipe ensuring proper pipe cleanup 5400a48306bSEd Tanous class SecurePipe 541988fb7b2SAdrian Ambrożewicz { 542988fb7b2SAdrian Ambrożewicz public: 543988fb7b2SAdrian Ambrożewicz using unix_fd = sdbusplus::message::unix_fd; 544988fb7b2SAdrian Ambrożewicz 5450a48306bSEd Tanous SecurePipe(boost::asio::io_context& io, 5460a48306bSEd Tanous CredentialsProvider::SecureBuffer&& bufferIn) : 5470a48306bSEd Tanous impl(io), 5480a48306bSEd Tanous buffer{std::move(bufferIn)} 5491214b7e7SGunnar Mills {} 550988fb7b2SAdrian Ambrożewicz 5510a48306bSEd Tanous ~SecurePipe() 552988fb7b2SAdrian Ambrożewicz { 553988fb7b2SAdrian Ambrożewicz // Named pipe needs to be explicitly removed 554988fb7b2SAdrian Ambrożewicz impl.close(); 555988fb7b2SAdrian Ambrożewicz } 556988fb7b2SAdrian Ambrożewicz 5570a48306bSEd Tanous SecurePipe(const SecurePipe&) = delete; 5580a48306bSEd Tanous SecurePipe(SecurePipe&&) = delete; 5590a48306bSEd Tanous SecurePipe& operator=(const SecurePipe&) = delete; 5600a48306bSEd Tanous SecurePipe& operator=(SecurePipe&&) = delete; 561ecd6a3a2SEd Tanous 5620a48306bSEd Tanous unix_fd fd() const 563988fb7b2SAdrian Ambrożewicz { 564988fb7b2SAdrian Ambrożewicz return unix_fd{impl.native_source()}; 565988fb7b2SAdrian Ambrożewicz } 566988fb7b2SAdrian Ambrożewicz 567988fb7b2SAdrian Ambrożewicz template <typename WriteHandler> 56881ce609eSEd Tanous void asyncWrite(WriteHandler&& handler) 569988fb7b2SAdrian Ambrożewicz { 5700a48306bSEd Tanous impl.async_write_some(boost::asio::buffer(*buffer), 5710a48306bSEd Tanous std::forward<WriteHandler>(handler)); 572988fb7b2SAdrian Ambrożewicz } 573988fb7b2SAdrian Ambrożewicz 574988fb7b2SAdrian Ambrożewicz const std::string name; 575988fb7b2SAdrian Ambrożewicz boost::process::async_pipe impl; 5760a48306bSEd Tanous CredentialsProvider::SecureBuffer buffer; 577988fb7b2SAdrian Ambrożewicz }; 578988fb7b2SAdrian Ambrożewicz 579e13c2760SPrzemyslaw Czarnowski /** 580e13c2760SPrzemyslaw Czarnowski * @brief Function transceives data with dbus directly. 581e13c2760SPrzemyslaw Czarnowski * 582e13c2760SPrzemyslaw Czarnowski * All BMC state properties will be retrieved before sending reset request. 583e13c2760SPrzemyslaw Czarnowski */ 58422db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 585e13c2760SPrzemyslaw Czarnowski const std::string& service, const std::string& name, 586988fb7b2SAdrian Ambrożewicz const std::string& imageUrl, const bool rw, 587988fb7b2SAdrian Ambrożewicz std::string&& userName, std::string&& password) 588e13c2760SPrzemyslaw Czarnowski { 589988fb7b2SAdrian Ambrożewicz constexpr const size_t secretLimit = 1024; 590988fb7b2SAdrian Ambrożewicz 591988fb7b2SAdrian Ambrożewicz std::shared_ptr<SecurePipe> secretPipe; 592168e20c1SEd Tanous dbus::utility::DbusVariantType unixFd = -1; 593988fb7b2SAdrian Ambrożewicz 594988fb7b2SAdrian Ambrożewicz if (!userName.empty() || !password.empty()) 595988fb7b2SAdrian Ambrożewicz { 596988fb7b2SAdrian Ambrożewicz // Encapsulate in safe buffer 597988fb7b2SAdrian Ambrożewicz CredentialsProvider credentials(std::move(userName), 598988fb7b2SAdrian Ambrożewicz std::move(password)); 599988fb7b2SAdrian Ambrożewicz 600988fb7b2SAdrian Ambrożewicz // Payload must contain data + NULL delimiters 601988fb7b2SAdrian Ambrożewicz if (credentials.user().size() + credentials.password().size() + 2 > 602988fb7b2SAdrian Ambrożewicz secretLimit) 603988fb7b2SAdrian Ambrożewicz { 60462598e31SEd Tanous BMCWEB_LOG_ERROR("Credentials too long to handle"); 605988fb7b2SAdrian Ambrożewicz messages::unrecognizedRequestBody(asyncResp->res); 606988fb7b2SAdrian Ambrożewicz return; 607988fb7b2SAdrian Ambrożewicz } 608988fb7b2SAdrian Ambrożewicz 609988fb7b2SAdrian Ambrożewicz // Pack secret 61022db1728SEd Tanous auto secret = credentials.pack( 61122db1728SEd Tanous [](const auto& user, const auto& pass, auto& buff) { 6123544d2a7SEd Tanous std::ranges::copy(user, std::back_inserter(buff)); 613988fb7b2SAdrian Ambrożewicz buff.push_back('\0'); 6143544d2a7SEd Tanous std::ranges::copy(pass, std::back_inserter(buff)); 615988fb7b2SAdrian Ambrożewicz buff.push_back('\0'); 616988fb7b2SAdrian Ambrożewicz }); 617988fb7b2SAdrian Ambrożewicz 618988fb7b2SAdrian Ambrożewicz // Open pipe 619988fb7b2SAdrian Ambrożewicz secretPipe = std::make_shared<SecurePipe>( 62022db1728SEd Tanous crow::connections::systemBus->get_io_context(), std::move(secret)); 621988fb7b2SAdrian Ambrożewicz unixFd = secretPipe->fd(); 622988fb7b2SAdrian Ambrożewicz 623988fb7b2SAdrian Ambrożewicz // Pass secret over pipe 62481ce609eSEd Tanous secretPipe->asyncWrite( 625f5b16f03SVikram Bodireddy [asyncResp](const boost::system::error_code& ec, std::size_t) { 626988fb7b2SAdrian Ambrożewicz if (ec) 627988fb7b2SAdrian Ambrożewicz { 62862598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to pass secret: {}", ec); 629988fb7b2SAdrian Ambrożewicz messages::internalError(asyncResp->res); 630988fb7b2SAdrian Ambrożewicz } 631988fb7b2SAdrian Ambrożewicz }); 632988fb7b2SAdrian Ambrożewicz } 633988fb7b2SAdrian Ambrożewicz 634e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 6355e7e2dc5SEd Tanous [asyncResp, secretPipe](const boost::system::error_code& ec, 636988fb7b2SAdrian Ambrożewicz bool success) { 637e13c2760SPrzemyslaw Czarnowski if (ec) 638e13c2760SPrzemyslaw Czarnowski { 63962598e31SEd Tanous BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); 640e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 641d6da5bebSAdrian Ambrożewicz } 642d6da5bebSAdrian Ambrożewicz else if (!success) 643d6da5bebSAdrian Ambrożewicz { 64462598e31SEd Tanous BMCWEB_LOG_ERROR("Service responded with error"); 645d6da5bebSAdrian Ambrożewicz messages::generalError(asyncResp->res); 646e13c2760SPrzemyslaw Czarnowski } 647e13c2760SPrzemyslaw Czarnowski }, 648e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, 649988fb7b2SAdrian Ambrożewicz "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw, 650988fb7b2SAdrian Ambrożewicz unixFd); 651e13c2760SPrzemyslaw Czarnowski } 652e13c2760SPrzemyslaw Czarnowski 653e13c2760SPrzemyslaw Czarnowski /** 654120fa86aSPrzemyslaw Czarnowski * @brief Function validate parameters of insert media request. 655120fa86aSPrzemyslaw Czarnowski * 656120fa86aSPrzemyslaw Czarnowski */ 657120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 658120fa86aSPrzemyslaw Czarnowski const std::string& service, 659120fa86aSPrzemyslaw Czarnowski const std::string& resName, 660120fa86aSPrzemyslaw Czarnowski InsertMediaActionParams& actionParams) 661120fa86aSPrzemyslaw Czarnowski { 66262598e31SEd Tanous BMCWEB_LOG_DEBUG("Validation started"); 663120fa86aSPrzemyslaw Czarnowski // required param imageUrl must not be empty 664120fa86aSPrzemyslaw Czarnowski if (!actionParams.imageUrl) 665120fa86aSPrzemyslaw Czarnowski { 66662598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter Image is empty."); 667120fa86aSPrzemyslaw Czarnowski 668120fa86aSPrzemyslaw Czarnowski messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image"); 669120fa86aSPrzemyslaw Czarnowski 670120fa86aSPrzemyslaw Czarnowski return; 671120fa86aSPrzemyslaw Czarnowski } 672120fa86aSPrzemyslaw Czarnowski 673120fa86aSPrzemyslaw Czarnowski // optional param inserted must be true 674e01d0c36SEd Tanous if (actionParams.inserted && !*actionParams.inserted) 675120fa86aSPrzemyslaw Czarnowski { 67662598e31SEd Tanous BMCWEB_LOG_ERROR( 67762598e31SEd Tanous "Request action optional parameter Inserted must be true."); 678120fa86aSPrzemyslaw Czarnowski 679120fa86aSPrzemyslaw Czarnowski messages::actionParameterNotSupported(asyncResp->res, "Inserted", 680120fa86aSPrzemyslaw Czarnowski "InsertMedia"); 681120fa86aSPrzemyslaw Czarnowski 682120fa86aSPrzemyslaw Czarnowski return; 683120fa86aSPrzemyslaw Czarnowski } 684120fa86aSPrzemyslaw Czarnowski 685120fa86aSPrzemyslaw Czarnowski // optional param transferMethod must be stream 686e01d0c36SEd Tanous if (actionParams.transferMethod && 687120fa86aSPrzemyslaw Czarnowski (*actionParams.transferMethod != "Stream")) 688120fa86aSPrzemyslaw Czarnowski { 68962598e31SEd Tanous BMCWEB_LOG_ERROR("Request action optional parameter " 69062598e31SEd Tanous "TransferMethod must be Stream."); 691120fa86aSPrzemyslaw Czarnowski 692120fa86aSPrzemyslaw Czarnowski messages::actionParameterNotSupported(asyncResp->res, "TransferMethod", 693120fa86aSPrzemyslaw Czarnowski "InsertMedia"); 694120fa86aSPrzemyslaw Czarnowski 695120fa86aSPrzemyslaw Czarnowski return; 696120fa86aSPrzemyslaw Czarnowski } 697120fa86aSPrzemyslaw Czarnowski boost::urls::result<boost::urls::url_view> url = 698120fa86aSPrzemyslaw Czarnowski boost::urls::parse_uri(*actionParams.imageUrl); 699120fa86aSPrzemyslaw Czarnowski if (!url) 700120fa86aSPrzemyslaw Czarnowski { 701120fa86aSPrzemyslaw Czarnowski messages::actionParameterValueFormatError( 702120fa86aSPrzemyslaw Czarnowski asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia"); 703120fa86aSPrzemyslaw Czarnowski return; 704120fa86aSPrzemyslaw Czarnowski } 705120fa86aSPrzemyslaw Czarnowski std::optional<TransferProtocol> uriTransferProtocolType = 706120fa86aSPrzemyslaw Czarnowski getTransferProtocolFromUri(*url); 707120fa86aSPrzemyslaw Czarnowski 708120fa86aSPrzemyslaw Czarnowski std::optional<TransferProtocol> paramTransferProtocolType = 709120fa86aSPrzemyslaw Czarnowski getTransferProtocolFromParam(actionParams.transferProtocolType); 710120fa86aSPrzemyslaw Czarnowski 711120fa86aSPrzemyslaw Czarnowski // ImageUrl does not contain valid protocol type 712e01d0c36SEd Tanous if (uriTransferProtocolType && 713e01d0c36SEd Tanous *uriTransferProtocolType == TransferProtocol::invalid) 714120fa86aSPrzemyslaw Czarnowski { 71562598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter ImageUrl must " 716120fa86aSPrzemyslaw Czarnowski "contain specified protocol type from list: " 71762598e31SEd Tanous "(smb, https)."); 718120fa86aSPrzemyslaw Czarnowski 719120fa86aSPrzemyslaw Czarnowski messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); 720120fa86aSPrzemyslaw Czarnowski 721120fa86aSPrzemyslaw Czarnowski return; 722120fa86aSPrzemyslaw Czarnowski } 723120fa86aSPrzemyslaw Czarnowski 724120fa86aSPrzemyslaw Czarnowski // transferProtocolType should contain value from list 725e01d0c36SEd Tanous if (paramTransferProtocolType && 726e01d0c36SEd Tanous *paramTransferProtocolType == TransferProtocol::invalid) 727120fa86aSPrzemyslaw Czarnowski { 72862598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter TransferProtocolType " 729120fa86aSPrzemyslaw Czarnowski "must be provided with value from list: " 73062598e31SEd Tanous "(CIFS, HTTPS)."); 731120fa86aSPrzemyslaw Czarnowski 732e01d0c36SEd Tanous messages::propertyValueNotInList( 733e01d0c36SEd Tanous asyncResp->res, actionParams.transferProtocolType.value_or(""), 734120fa86aSPrzemyslaw Czarnowski "TransferProtocolType"); 735120fa86aSPrzemyslaw Czarnowski return; 736120fa86aSPrzemyslaw Czarnowski } 737120fa86aSPrzemyslaw Czarnowski 738120fa86aSPrzemyslaw Czarnowski // valid transfer protocol not provided either with URI nor param 739e01d0c36SEd Tanous if (!uriTransferProtocolType && !paramTransferProtocolType) 740120fa86aSPrzemyslaw Czarnowski { 74162598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter ImageUrl must " 742120fa86aSPrzemyslaw Czarnowski "contain specified protocol type or param " 74362598e31SEd Tanous "TransferProtocolType must be provided."); 744120fa86aSPrzemyslaw Czarnowski 745120fa86aSPrzemyslaw Czarnowski messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); 746120fa86aSPrzemyslaw Czarnowski 747120fa86aSPrzemyslaw Czarnowski return; 748120fa86aSPrzemyslaw Czarnowski } 749120fa86aSPrzemyslaw Czarnowski 750120fa86aSPrzemyslaw Czarnowski // valid transfer protocol provided both with URI and param 751e01d0c36SEd Tanous if (paramTransferProtocolType && uriTransferProtocolType) 752120fa86aSPrzemyslaw Czarnowski { 753120fa86aSPrzemyslaw Czarnowski // check if protocol is the same for URI and param 754120fa86aSPrzemyslaw Czarnowski if (*paramTransferProtocolType != *uriTransferProtocolType) 755120fa86aSPrzemyslaw Czarnowski { 75662598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter " 757120fa86aSPrzemyslaw Czarnowski "TransferProtocolType must contain the " 758120fa86aSPrzemyslaw Czarnowski "same protocol type as protocol type " 75962598e31SEd Tanous "provided with param imageUrl."); 760120fa86aSPrzemyslaw Czarnowski 761120fa86aSPrzemyslaw Czarnowski messages::actionParameterValueTypeError( 762e01d0c36SEd Tanous asyncResp->res, actionParams.transferProtocolType.value_or(""), 763120fa86aSPrzemyslaw Czarnowski "TransferProtocolType", "InsertMedia"); 764120fa86aSPrzemyslaw Czarnowski 765120fa86aSPrzemyslaw Czarnowski return; 766120fa86aSPrzemyslaw Czarnowski } 767120fa86aSPrzemyslaw Czarnowski } 768120fa86aSPrzemyslaw Czarnowski 769120fa86aSPrzemyslaw Czarnowski // validation passed, add protocol to URI if needed 770*7ead48e6SBoleslaw Ogonczyk Makowski if (!uriTransferProtocolType && paramTransferProtocolType) 771120fa86aSPrzemyslaw Czarnowski { 772120fa86aSPrzemyslaw Czarnowski actionParams.imageUrl = getUriWithTransferProtocol( 773120fa86aSPrzemyslaw Czarnowski *actionParams.imageUrl, *paramTransferProtocolType); 774120fa86aSPrzemyslaw Czarnowski } 775120fa86aSPrzemyslaw Czarnowski 776452bd8d8SJayaprakash Mutyala if (!actionParams.userName) 777452bd8d8SJayaprakash Mutyala { 778452bd8d8SJayaprakash Mutyala actionParams.userName = ""; 779452bd8d8SJayaprakash Mutyala } 780452bd8d8SJayaprakash Mutyala 781452bd8d8SJayaprakash Mutyala if (!actionParams.password) 782452bd8d8SJayaprakash Mutyala { 783452bd8d8SJayaprakash Mutyala actionParams.password = ""; 784452bd8d8SJayaprakash Mutyala } 785452bd8d8SJayaprakash Mutyala 786120fa86aSPrzemyslaw Czarnowski doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl, 787e01d0c36SEd Tanous !(actionParams.writeProtected.value_or(false)), 788120fa86aSPrzemyslaw Czarnowski std::move(*actionParams.userName), 789120fa86aSPrzemyslaw Czarnowski std::move(*actionParams.password)); 790120fa86aSPrzemyslaw Czarnowski } 791120fa86aSPrzemyslaw Czarnowski 792120fa86aSPrzemyslaw Czarnowski /** 793e13c2760SPrzemyslaw Czarnowski * @brief Function transceives data with dbus directly. 794e13c2760SPrzemyslaw Czarnowski * 795e13c2760SPrzemyslaw Czarnowski * All BMC state properties will be retrieved before sending reset request. 796e13c2760SPrzemyslaw Czarnowski */ 79724e740a7SEd Tanous inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 798e13c2760SPrzemyslaw Czarnowski const std::string& service, const std::string& name, 799e13c2760SPrzemyslaw Czarnowski bool legacy) 800e13c2760SPrzemyslaw Czarnowski { 801e13c2760SPrzemyslaw Czarnowski // Legacy mount requires parameter with image 802e13c2760SPrzemyslaw Czarnowski if (legacy) 803e13c2760SPrzemyslaw Czarnowski { 804e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 8055e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 806e13c2760SPrzemyslaw Czarnowski if (ec) 807e13c2760SPrzemyslaw Czarnowski { 80862598e31SEd Tanous BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); 809e13c2760SPrzemyslaw Czarnowski 810e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 811e13c2760SPrzemyslaw Czarnowski return; 812e13c2760SPrzemyslaw Czarnowski } 813e13c2760SPrzemyslaw Czarnowski }, 814e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, 815e13c2760SPrzemyslaw Czarnowski "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount"); 816e13c2760SPrzemyslaw Czarnowski } 817e13c2760SPrzemyslaw Czarnowski else // proxy 818e13c2760SPrzemyslaw Czarnowski { 819e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 8205e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 821e13c2760SPrzemyslaw Czarnowski if (ec) 822e13c2760SPrzemyslaw Czarnowski { 82362598e31SEd Tanous BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); 824e13c2760SPrzemyslaw Czarnowski 825e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 826e13c2760SPrzemyslaw Czarnowski return; 827e13c2760SPrzemyslaw Czarnowski } 828e13c2760SPrzemyslaw Czarnowski }, 829e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name, 830e13c2760SPrzemyslaw Czarnowski "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); 831e13c2760SPrzemyslaw Czarnowski } 832e13c2760SPrzemyslaw Czarnowski } 833e13c2760SPrzemyslaw Czarnowski 83496825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost( 83596825bebSEd Tanous crow::App& app, const crow::Request& req, 83622db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 83796825bebSEd Tanous const std::string& name, const std::string& resName) 83896825bebSEd Tanous { 8393ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 84045ca1b86SEd Tanous { 84145ca1b86SEd Tanous return; 84245ca1b86SEd Tanous } 84379fdf63eSPrzemyslaw Czarnowski 84479fdf63eSPrzemyslaw Czarnowski constexpr std::string_view action = "VirtualMedia.InsertMedia"; 84522db1728SEd Tanous if (name != "bmc") 846107077deSPrzemyslaw Czarnowski { 84779fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 848107077deSPrzemyslaw Czarnowski 849107077deSPrzemyslaw Czarnowski return; 850107077deSPrzemyslaw Czarnowski } 85179fdf63eSPrzemyslaw Czarnowski InsertMediaActionParams actionParams; 85298be3e39SEd Tanous 853120fa86aSPrzemyslaw Czarnowski // Read obligatory parameters (url of image) 85415ed6780SWilly Tu if (!json_util::readJsonAction( 85579fdf63eSPrzemyslaw Czarnowski req, asyncResp->res, "Image", actionParams.imageUrl, 85679fdf63eSPrzemyslaw Czarnowski "WriteProtected", actionParams.writeProtected, "UserName", 85779fdf63eSPrzemyslaw Czarnowski actionParams.userName, "Password", actionParams.password, 85879fdf63eSPrzemyslaw Czarnowski "Inserted", actionParams.inserted, "TransferMethod", 85979fdf63eSPrzemyslaw Czarnowski actionParams.transferMethod, "TransferProtocolType", 86079fdf63eSPrzemyslaw Czarnowski actionParams.transferProtocolType)) 86198be3e39SEd Tanous { 86298be3e39SEd Tanous return; 86398be3e39SEd Tanous } 864107077deSPrzemyslaw Czarnowski 8652b73119cSGeorge Liu dbus::utility::getDbusObject( 8662b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 86779fdf63eSPrzemyslaw Czarnowski [asyncResp, action, actionParams, 8682b73119cSGeorge Liu resName](const boost::system::error_code& ec, 869002d39b4SEd Tanous const dbus::utility::MapperGetObject& getObjectType) mutable { 87022db1728SEd Tanous if (ec) 87122db1728SEd Tanous { 87262598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); 87379fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 874107077deSPrzemyslaw Czarnowski 87522db1728SEd Tanous return; 87622db1728SEd Tanous } 87779fdf63eSPrzemyslaw Czarnowski 87822db1728SEd Tanous std::string service = getObjectType.begin()->first; 87962598e31SEd Tanous BMCWEB_LOG_DEBUG("GetObjectType: {}", service); 88022db1728SEd Tanous 8815eb468daSGeorge Liu sdbusplus::message::object_path path( 8825eb468daSGeorge Liu "/xyz/openbmc_project/VirtualMedia"); 8835eb468daSGeorge Liu dbus::utility::getManagedObjects( 8845eb468daSGeorge Liu service, path, 8855eb468daSGeorge Liu [service, resName, action, actionParams, asyncResp]( 8865eb468daSGeorge Liu const boost::system::error_code& ec2, 8875eb468daSGeorge Liu const dbus::utility::ManagedObjectType& subtree) mutable { 8888a592810SEd Tanous if (ec2) 88922db1728SEd Tanous { 89079fdf63eSPrzemyslaw Czarnowski // Not possible in proxy mode 89162598e31SEd Tanous BMCWEB_LOG_DEBUG("InsertMedia not " 89262598e31SEd Tanous "allowed in proxy mode"); 89379fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 89422db1728SEd Tanous 89522db1728SEd Tanous return; 89622db1728SEd Tanous } 89722db1728SEd Tanous for (const auto& object : subtree) 89822db1728SEd Tanous { 899365a73f4SEd Tanous VmMode mode = parseObjectPathAndGetMode(object.first, resName); 9005880f0c5SBoleslaw Ogonczyk Makowski if (mode == VmMode::Legacy) 90122db1728SEd Tanous { 90279fdf63eSPrzemyslaw Czarnowski validateParams(asyncResp, service, resName, actionParams); 90322db1728SEd Tanous 90422db1728SEd Tanous return; 90522db1728SEd Tanous } 90622db1728SEd Tanous } 90762598e31SEd Tanous BMCWEB_LOG_DEBUG("Parent item not found"); 90896825bebSEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 9095eb468daSGeorge Liu }); 9102b73119cSGeorge Liu }); 91196825bebSEd Tanous } 91222db1728SEd Tanous 91396825bebSEd Tanous inline void handleManagersVirtualMediaActionEject( 91496825bebSEd Tanous crow::App& app, const crow::Request& req, 91522db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 91696825bebSEd Tanous const std::string& managerName, const std::string& resName) 91796825bebSEd Tanous { 9183ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 91945ca1b86SEd Tanous { 92045ca1b86SEd Tanous return; 92145ca1b86SEd Tanous } 92279fdf63eSPrzemyslaw Czarnowski 92379fdf63eSPrzemyslaw Czarnowski constexpr std::string_view action = "VirtualMedia.EjectMedia"; 92496825bebSEd Tanous if (managerName != "bmc") 925107077deSPrzemyslaw Czarnowski { 92679fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 92722db1728SEd Tanous 92822db1728SEd Tanous return; 92922db1728SEd Tanous } 93022db1728SEd Tanous 9312b73119cSGeorge Liu dbus::utility::getDbusObject( 9322b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 93379fdf63eSPrzemyslaw Czarnowski [asyncResp, action, 9342b73119cSGeorge Liu resName](const boost::system::error_code& ec2, 935b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 9368a592810SEd Tanous if (ec2) 93722db1728SEd Tanous { 93862598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec2); 93922db1728SEd Tanous messages::internalError(asyncResp->res); 94022db1728SEd Tanous 94122db1728SEd Tanous return; 94222db1728SEd Tanous } 94322db1728SEd Tanous std::string service = getObjectType.begin()->first; 94462598e31SEd Tanous BMCWEB_LOG_DEBUG("GetObjectType: {}", service); 94522db1728SEd Tanous 9465eb468daSGeorge Liu sdbusplus::message::object_path path( 9475eb468daSGeorge Liu "/xyz/openbmc_project/VirtualMedia"); 9485eb468daSGeorge Liu dbus::utility::getManagedObjects( 9495eb468daSGeorge Liu service, path, 95079fdf63eSPrzemyslaw Czarnowski [resName, service, action, 95179fdf63eSPrzemyslaw Czarnowski asyncResp](const boost::system::error_code& ec, 95202cad96eSEd Tanous const dbus::utility::ManagedObjectType& subtree) { 95322db1728SEd Tanous if (ec) 95422db1728SEd Tanous { 95562598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper : No Service found"); 95679fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 95722db1728SEd Tanous return; 95822db1728SEd Tanous } 95922db1728SEd Tanous 96022db1728SEd Tanous for (const auto& object : subtree) 96122db1728SEd Tanous { 962365a73f4SEd Tanous VmMode mode = parseObjectPathAndGetMode(object.first, resName); 963365a73f4SEd Tanous if (mode != VmMode::Invalid) 96422db1728SEd Tanous { 965365a73f4SEd Tanous doEjectAction(asyncResp, service, resName, 966365a73f4SEd Tanous mode == VmMode::Legacy); 9675880f0c5SBoleslaw Ogonczyk Makowski return; 96822db1728SEd Tanous } 96922db1728SEd Tanous } 97062598e31SEd Tanous BMCWEB_LOG_DEBUG("Parent item not found"); 97196825bebSEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 9725eb468daSGeorge Liu }); 9732b73119cSGeorge Liu }); 97496825bebSEd Tanous } 97596825bebSEd Tanous 97696825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet( 97796825bebSEd Tanous crow::App& app, const crow::Request& req, 97822db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 97996825bebSEd Tanous const std::string& name) 98096825bebSEd Tanous { 9813ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 98245ca1b86SEd Tanous { 98345ca1b86SEd Tanous return; 98445ca1b86SEd Tanous } 98522db1728SEd Tanous if (name != "bmc") 98622db1728SEd Tanous { 987002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", name); 988107077deSPrzemyslaw Czarnowski 989107077deSPrzemyslaw Czarnowski return; 990107077deSPrzemyslaw Czarnowski } 991107077deSPrzemyslaw Czarnowski 9928d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 993107077deSPrzemyslaw Czarnowski "#VirtualMediaCollection.VirtualMediaCollection"; 9948d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Virtual Media Services"; 995ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 996ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia", name); 997107077deSPrzemyslaw Czarnowski 9982b73119cSGeorge Liu dbus::utility::getDbusObject( 9992b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 10002b73119cSGeorge Liu [asyncResp, name](const boost::system::error_code& ec, 1001b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 1002107077deSPrzemyslaw Czarnowski if (ec) 1003107077deSPrzemyslaw Czarnowski { 100462598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); 1005107077deSPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 1006107077deSPrzemyslaw Czarnowski 1007107077deSPrzemyslaw Czarnowski return; 1008107077deSPrzemyslaw Czarnowski } 1009107077deSPrzemyslaw Czarnowski std::string service = getObjectType.begin()->first; 101062598e31SEd Tanous BMCWEB_LOG_DEBUG("GetObjectType: {}", service); 1011107077deSPrzemyslaw Czarnowski 1012107077deSPrzemyslaw Czarnowski getVmResourceList(asyncResp, service, name); 10132b73119cSGeorge Liu }); 101496825bebSEd Tanous } 1015107077deSPrzemyslaw Czarnowski 101696825bebSEd Tanous inline void 101796825bebSEd Tanous handleVirtualMediaGet(crow::App& app, const crow::Request& req, 101822db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 101996825bebSEd Tanous const std::string& name, const std::string& resName) 102096825bebSEd Tanous { 10213ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 102245ca1b86SEd Tanous { 102345ca1b86SEd Tanous return; 102445ca1b86SEd Tanous } 1025107077deSPrzemyslaw Czarnowski if (name != "bmc") 1026107077deSPrzemyslaw Czarnowski { 1027002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 1028107077deSPrzemyslaw Czarnowski 1029107077deSPrzemyslaw Czarnowski return; 1030107077deSPrzemyslaw Czarnowski } 1031107077deSPrzemyslaw Czarnowski 10322b73119cSGeorge Liu dbus::utility::getDbusObject( 10332b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 1034002d39b4SEd Tanous [asyncResp, name, 10352b73119cSGeorge Liu resName](const boost::system::error_code& ec, 1036b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 1037107077deSPrzemyslaw Czarnowski if (ec) 1038107077deSPrzemyslaw Czarnowski { 103962598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); 1040107077deSPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 1041107077deSPrzemyslaw Czarnowski 1042107077deSPrzemyslaw Czarnowski return; 1043107077deSPrzemyslaw Czarnowski } 1044107077deSPrzemyslaw Czarnowski std::string service = getObjectType.begin()->first; 104562598e31SEd Tanous BMCWEB_LOG_DEBUG("GetObjectType: {}", service); 1046107077deSPrzemyslaw Czarnowski 1047107077deSPrzemyslaw Czarnowski getVmData(asyncResp, service, name, resName); 10482b73119cSGeorge Liu }); 104996825bebSEd Tanous } 105096825bebSEd Tanous 105196825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app) 105296825bebSEd Tanous { 105396825bebSEd Tanous BMCWEB_ROUTE( 105496825bebSEd Tanous app, 105596825bebSEd Tanous "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia") 105696825bebSEd Tanous .privileges(redfish::privileges::postVirtualMedia) 105796825bebSEd Tanous .methods(boost::beast::http::verb::post)(std::bind_front( 105896825bebSEd Tanous handleManagersVirtualMediaActionInsertPost, std::ref(app))); 105996825bebSEd Tanous 106096825bebSEd Tanous BMCWEB_ROUTE( 106196825bebSEd Tanous app, 106296825bebSEd Tanous "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia") 106396825bebSEd Tanous .privileges(redfish::privileges::postVirtualMedia) 106496825bebSEd Tanous .methods(boost::beast::http::verb::post)(std::bind_front( 106596825bebSEd Tanous handleManagersVirtualMediaActionEject, std::ref(app))); 106696825bebSEd Tanous 106796825bebSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/") 106896825bebSEd Tanous .privileges(redfish::privileges::getVirtualMediaCollection) 106996825bebSEd Tanous .methods(boost::beast::http::verb::get)(std::bind_front( 107096825bebSEd Tanous handleManagersVirtualMediaCollectionGet, std::ref(app))); 107196825bebSEd Tanous 107296825bebSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/") 107396825bebSEd Tanous .privileges(redfish::privileges::getVirtualMedia) 107496825bebSEd Tanous .methods(boost::beast::http::verb::get)( 107596825bebSEd Tanous std::bind_front(handleVirtualMediaGet, std::ref(app))); 1076107077deSPrzemyslaw Czarnowski } 1077107077deSPrzemyslaw Czarnowski 1078107077deSPrzemyslaw Czarnowski } // namespace redfish 1079