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> 322b73119cSGeorge Liu #include <string_view> 332b73119cSGeorge Liu 34107077deSPrzemyslaw Czarnowski namespace redfish 35107077deSPrzemyslaw Czarnowski { 36365a73f4SEd Tanous 37365a73f4SEd Tanous enum class VmMode 38365a73f4SEd Tanous { 39365a73f4SEd Tanous Invalid, 40365a73f4SEd Tanous Legacy, 41365a73f4SEd Tanous Proxy 42365a73f4SEd Tanous }; 43365a73f4SEd Tanous 44365a73f4SEd Tanous inline VmMode 45365a73f4SEd Tanous parseObjectPathAndGetMode(const sdbusplus::message::object_path& itemPath, 46365a73f4SEd Tanous const std::string& resName) 47365a73f4SEd Tanous { 48365a73f4SEd Tanous std::string thisPath = itemPath.filename(); 4962598e31SEd Tanous BMCWEB_LOG_DEBUG("Filename: {}, ThisPath: {}", itemPath.str, thisPath); 50365a73f4SEd Tanous 51365a73f4SEd Tanous if (thisPath.empty()) 52365a73f4SEd Tanous { 53365a73f4SEd Tanous return VmMode::Invalid; 54365a73f4SEd Tanous } 55365a73f4SEd Tanous 56365a73f4SEd Tanous if (thisPath != resName) 57365a73f4SEd Tanous { 58365a73f4SEd Tanous return VmMode::Invalid; 59365a73f4SEd Tanous } 60365a73f4SEd Tanous 61365a73f4SEd Tanous auto mode = itemPath.parent_path(); 62365a73f4SEd Tanous auto type = mode.parent_path(); 63365a73f4SEd Tanous 64365a73f4SEd Tanous if (mode.filename().empty() || type.filename().empty()) 65365a73f4SEd Tanous { 66365a73f4SEd Tanous return VmMode::Invalid; 67365a73f4SEd Tanous } 68365a73f4SEd Tanous 69365a73f4SEd Tanous if (type.filename() != "VirtualMedia") 70365a73f4SEd Tanous { 71365a73f4SEd Tanous return VmMode::Invalid; 72365a73f4SEd Tanous } 73365a73f4SEd Tanous std::string modeStr = mode.filename(); 74365a73f4SEd Tanous if (modeStr == "Legacy") 75365a73f4SEd Tanous { 76365a73f4SEd Tanous return VmMode::Legacy; 77365a73f4SEd Tanous } 78365a73f4SEd Tanous if (modeStr == "Proxy") 79365a73f4SEd Tanous { 80365a73f4SEd Tanous return VmMode::Proxy; 81365a73f4SEd Tanous } 82365a73f4SEd Tanous return VmMode::Invalid; 83365a73f4SEd Tanous } 84365a73f4SEd Tanous 8579fdf63eSPrzemyslaw Czarnowski using CheckItemHandler = 8679fdf63eSPrzemyslaw Czarnowski std::function<void(const std::string& service, const std::string& resName, 8779fdf63eSPrzemyslaw Czarnowski const std::shared_ptr<bmcweb::AsyncResp>&, 8870cbdf53SGeorge Liu const std::pair<sdbusplus::message::object_path, 8979fdf63eSPrzemyslaw Czarnowski dbus::utility::DBusInteracesMap>&)>; 9079fdf63eSPrzemyslaw Czarnowski 91ac106bf6SEd Tanous inline void 92ac106bf6SEd Tanous findAndParseObject(const std::string& service, const std::string& resName, 93ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 9479fdf63eSPrzemyslaw Czarnowski CheckItemHandler&& handler) 9579fdf63eSPrzemyslaw Czarnowski { 965eb468daSGeorge Liu sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia"); 975eb468daSGeorge Liu dbus::utility::getManagedObjects( 985eb468daSGeorge Liu service, path, 99ac106bf6SEd Tanous [service, resName, asyncResp, 100746c5b8aSLakshmi Yadlapati handler](const boost::system::error_code& ec, 10170cbdf53SGeorge Liu const dbus::utility::ManagedObjectType& subtree) { 10279fdf63eSPrzemyslaw Czarnowski if (ec) 10379fdf63eSPrzemyslaw Czarnowski { 10462598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error"); 10579fdf63eSPrzemyslaw Czarnowski 10679fdf63eSPrzemyslaw Czarnowski return; 10779fdf63eSPrzemyslaw Czarnowski } 10879fdf63eSPrzemyslaw Czarnowski 10970cbdf53SGeorge Liu for (const auto& item : subtree) 11079fdf63eSPrzemyslaw Czarnowski { 11179fdf63eSPrzemyslaw Czarnowski VmMode mode = parseObjectPathAndGetMode(item.first, resName); 11279fdf63eSPrzemyslaw Czarnowski if (mode != VmMode::Invalid) 11379fdf63eSPrzemyslaw Czarnowski { 114ac106bf6SEd Tanous handler(service, resName, asyncResp, item); 11579fdf63eSPrzemyslaw Czarnowski return; 11679fdf63eSPrzemyslaw Czarnowski } 11779fdf63eSPrzemyslaw Czarnowski } 11879fdf63eSPrzemyslaw Czarnowski 11962598e31SEd Tanous BMCWEB_LOG_DEBUG("Parent item not found"); 120ac106bf6SEd Tanous asyncResp->res.result(boost::beast::http::status::not_found); 1215eb468daSGeorge Liu }); 12279fdf63eSPrzemyslaw Czarnowski } 12379fdf63eSPrzemyslaw Czarnowski 1249e319cf0SAnna Platash /** 1259e319cf0SAnna Platash * @brief Function extracts transfer protocol name from URI. 1269e319cf0SAnna Platash */ 12767df073bSEd Tanous inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri) 12867df073bSEd Tanous { 12967df073bSEd Tanous boost::urls::result<boost::urls::url_view> url = 130079360aeSEd Tanous boost::urls::parse_uri(imageUri); 13167df073bSEd Tanous if (!url) 13267df073bSEd Tanous { 13367df073bSEd Tanous return "None"; 13467df073bSEd Tanous } 135079360aeSEd Tanous std::string_view scheme = url->scheme(); 13667df073bSEd Tanous if (scheme == "smb") 13767df073bSEd Tanous { 13867df073bSEd Tanous return "CIFS"; 13967df073bSEd Tanous } 14067df073bSEd Tanous if (scheme == "https") 14167df073bSEd Tanous { 14267df073bSEd Tanous return "HTTPS"; 14367df073bSEd Tanous } 14467df073bSEd Tanous 14567df073bSEd Tanous return "None"; 14667df073bSEd Tanous } 147107077deSPrzemyslaw Czarnowski 148107077deSPrzemyslaw Czarnowski /** 149107077deSPrzemyslaw Czarnowski * @brief Read all known properties from VM object interfaces 150107077deSPrzemyslaw Czarnowski */ 15122db1728SEd Tanous inline void 1528a592810SEd Tanous vmParseInterfaceObject(const dbus::utility::DBusInteracesMap& interfaces, 153ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 154107077deSPrzemyslaw Czarnowski { 1558a592810SEd Tanous for (const auto& [interface, values] : interfaces) 156107077deSPrzemyslaw Czarnowski { 157711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint") 158107077deSPrzemyslaw Czarnowski { 159711ac7a9SEd Tanous for (const auto& [property, value] : values) 160107077deSPrzemyslaw Czarnowski { 161711ac7a9SEd Tanous if (property == "EndpointId") 162107077deSPrzemyslaw Czarnowski { 163107077deSPrzemyslaw Czarnowski const std::string* endpointIdValue = 164711ac7a9SEd Tanous std::get_if<std::string>(&value); 165711ac7a9SEd Tanous if (endpointIdValue == nullptr) 166107077deSPrzemyslaw Czarnowski { 167711ac7a9SEd Tanous continue; 168711ac7a9SEd Tanous } 169107077deSPrzemyslaw Czarnowski if (!endpointIdValue->empty()) 170107077deSPrzemyslaw Czarnowski { 171107077deSPrzemyslaw Czarnowski // Proxy mode 172ac106bf6SEd Tanous asyncResp->res 173711ac7a9SEd Tanous .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] = 174d04ba325SPrzemyslaw Czarnowski *endpointIdValue; 175ac106bf6SEd Tanous asyncResp->res.jsonValue["TransferProtocolType"] = 176ac106bf6SEd Tanous "OEM"; 177107077deSPrzemyslaw Czarnowski } 178107077deSPrzemyslaw Czarnowski } 179711ac7a9SEd Tanous if (property == "ImageURL") 180107077deSPrzemyslaw Czarnowski { 181107077deSPrzemyslaw Czarnowski const std::string* imageUrlValue = 182711ac7a9SEd Tanous std::get_if<std::string>(&value); 18326f6976fSEd Tanous if (imageUrlValue != nullptr && !imageUrlValue->empty()) 184107077deSPrzemyslaw Czarnowski { 185da4784d8SPrzemyslaw Czarnowski std::filesystem::path filePath = *imageUrlValue; 186da4784d8SPrzemyslaw Czarnowski if (!filePath.has_filename()) 187da4784d8SPrzemyslaw Czarnowski { 1889e319cf0SAnna Platash // this will handle https share, which not 1899e319cf0SAnna Platash // necessarily has to have filename given. 190ac106bf6SEd Tanous asyncResp->res.jsonValue["ImageName"] = ""; 191da4784d8SPrzemyslaw Czarnowski } 192da4784d8SPrzemyslaw Czarnowski else 193da4784d8SPrzemyslaw Czarnowski { 194ac106bf6SEd Tanous asyncResp->res.jsonValue["ImageName"] = 1959e319cf0SAnna Platash filePath.filename(); 196da4784d8SPrzemyslaw Czarnowski } 197da4784d8SPrzemyslaw Czarnowski 198ac106bf6SEd Tanous asyncResp->res.jsonValue["Image"] = *imageUrlValue; 199ac106bf6SEd Tanous asyncResp->res.jsonValue["TransferProtocolType"] = 2009e319cf0SAnna Platash getTransferProtocolTypeFromUri(*imageUrlValue); 2019e319cf0SAnna Platash 202ac106bf6SEd Tanous asyncResp->res.jsonValue["ConnectedVia"] = 203739b87efSEd Tanous virtual_media::ConnectedVia::URI; 204107077deSPrzemyslaw Czarnowski } 205107077deSPrzemyslaw Czarnowski } 206711ac7a9SEd Tanous if (property == "WriteProtected") 2079e319cf0SAnna Platash { 208711ac7a9SEd Tanous const bool* writeProtectedValue = std::get_if<bool>(&value); 209e662eae8SEd Tanous if (writeProtectedValue != nullptr) 2109e319cf0SAnna Platash { 211ac106bf6SEd Tanous asyncResp->res.jsonValue["WriteProtected"] = 2129e319cf0SAnna Platash *writeProtectedValue; 2139e319cf0SAnna Platash } 2149e319cf0SAnna Platash } 2159e319cf0SAnna Platash } 216107077deSPrzemyslaw Czarnowski } 217711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.VirtualMedia.Process") 218711ac7a9SEd Tanous { 219711ac7a9SEd Tanous for (const auto& [property, value] : values) 220711ac7a9SEd Tanous { 221711ac7a9SEd Tanous if (property == "Active") 222711ac7a9SEd Tanous { 223711ac7a9SEd Tanous const bool* activeValue = std::get_if<bool>(&value); 224e662eae8SEd Tanous if (activeValue == nullptr) 225711ac7a9SEd Tanous { 22662598e31SEd Tanous BMCWEB_LOG_DEBUG("Value Active not found"); 227711ac7a9SEd Tanous return; 228711ac7a9SEd Tanous } 229ac106bf6SEd Tanous asyncResp->res.jsonValue["Inserted"] = *activeValue; 230711ac7a9SEd Tanous 231e05aec50SEd Tanous if (*activeValue) 232711ac7a9SEd Tanous { 233ac106bf6SEd Tanous asyncResp->res.jsonValue["ConnectedVia"] = 234739b87efSEd Tanous virtual_media::ConnectedVia::Applet; 235711ac7a9SEd Tanous } 236711ac7a9SEd Tanous } 237711ac7a9SEd Tanous } 238711ac7a9SEd Tanous } 239107077deSPrzemyslaw Czarnowski } 240107077deSPrzemyslaw Czarnowski } 241107077deSPrzemyslaw Czarnowski 242107077deSPrzemyslaw Czarnowski /** 243107077deSPrzemyslaw Czarnowski * @brief Fill template for Virtual Media Item. 244107077deSPrzemyslaw Czarnowski */ 24522db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name, 246107077deSPrzemyslaw Czarnowski const std::string& resName) 247107077deSPrzemyslaw Czarnowski { 248107077deSPrzemyslaw Czarnowski nlohmann::json item; 249ef4c65b7SEd Tanous item["@odata.id"] = boost::urls::format( 250ef4c65b7SEd Tanous "/redfish/v1/Managers/{}/VirtualMedia/{}", name, resName); 25122db1728SEd Tanous 252d04ba325SPrzemyslaw Czarnowski item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia"; 253107077deSPrzemyslaw Czarnowski item["Name"] = "Virtual Removable Media"; 254107077deSPrzemyslaw Czarnowski item["Id"] = resName; 255107077deSPrzemyslaw Czarnowski item["WriteProtected"] = true; 256739b87efSEd Tanous item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected; 257613dabeaSEd Tanous item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"}); 258107077deSPrzemyslaw Czarnowski item["TransferMethod"] = "Stream"; 259d04ba325SPrzemyslaw Czarnowski item["Oem"]["OpenBMC"]["@odata.type"] = 260d04ba325SPrzemyslaw Czarnowski "#OemVirtualMedia.v1_0_0.VirtualMedia"; 26115b89725SV-Sanjana item["Oem"]["OpenBMC"]["@odata.id"] = boost::urls::format( 26215b89725SV-Sanjana "/redfish/v1/Managers/{}/VirtualMedia/{}#/Oem/OpenBMC", name, resName); 263107077deSPrzemyslaw Czarnowski 264107077deSPrzemyslaw Czarnowski return item; 265107077deSPrzemyslaw Czarnowski } 266107077deSPrzemyslaw Czarnowski 267107077deSPrzemyslaw Czarnowski /** 268107077deSPrzemyslaw Czarnowski * @brief Fills collection data 269107077deSPrzemyslaw Czarnowski */ 270ac106bf6SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> asyncResp, 271107077deSPrzemyslaw Czarnowski const std::string& service, 272107077deSPrzemyslaw Czarnowski const std::string& name) 273107077deSPrzemyslaw Czarnowski { 27462598e31SEd Tanous BMCWEB_LOG_DEBUG("Get available Virtual Media resources."); 2755eb468daSGeorge Liu sdbusplus::message::object_path objPath( 2765eb468daSGeorge Liu "/xyz/openbmc_project/VirtualMedia"); 2775eb468daSGeorge Liu dbus::utility::getManagedObjects( 2785eb468daSGeorge Liu service, objPath, 279ac106bf6SEd Tanous [name, asyncResp{std::move(asyncResp)}]( 2805e7e2dc5SEd Tanous const boost::system::error_code& ec, 28102cad96eSEd Tanous const dbus::utility::ManagedObjectType& subtree) { 282107077deSPrzemyslaw Czarnowski if (ec) 283107077deSPrzemyslaw Czarnowski { 28462598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error"); 285107077deSPrzemyslaw Czarnowski return; 286107077deSPrzemyslaw Czarnowski } 287ac106bf6SEd Tanous nlohmann::json& members = asyncResp->res.jsonValue["Members"]; 288107077deSPrzemyslaw Czarnowski members = nlohmann::json::array(); 289107077deSPrzemyslaw Czarnowski 290107077deSPrzemyslaw Czarnowski for (const auto& object : subtree) 291107077deSPrzemyslaw Czarnowski { 292107077deSPrzemyslaw Czarnowski nlohmann::json item; 2932dfd18efSEd Tanous std::string path = object.first.filename(); 2942dfd18efSEd Tanous if (path.empty()) 295107077deSPrzemyslaw Czarnowski { 296107077deSPrzemyslaw Czarnowski continue; 297107077deSPrzemyslaw Czarnowski } 298107077deSPrzemyslaw Czarnowski 299ef4c65b7SEd Tanous item["@odata.id"] = boost::urls::format( 300ef4c65b7SEd Tanous "/redfish/v1/Managers/{}/VirtualMedia/{}", name, path); 301107077deSPrzemyslaw Czarnowski members.emplace_back(std::move(item)); 302107077deSPrzemyslaw Czarnowski } 303ac106bf6SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = members.size(); 3045eb468daSGeorge Liu }); 305107077deSPrzemyslaw Czarnowski } 306107077deSPrzemyslaw Czarnowski 30770cbdf53SGeorge Liu inline void 30870cbdf53SGeorge Liu afterGetVmData(const std::string& name, const std::string& /*service*/, 30979fdf63eSPrzemyslaw Czarnowski const std::string& resName, 31079fdf63eSPrzemyslaw Czarnowski const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 31170cbdf53SGeorge Liu const std::pair<sdbusplus::message::object_path, 31279fdf63eSPrzemyslaw Czarnowski dbus::utility::DBusInteracesMap>& item) 31379fdf63eSPrzemyslaw Czarnowski { 31479fdf63eSPrzemyslaw Czarnowski VmMode mode = parseObjectPathAndGetMode(item.first, resName); 31579fdf63eSPrzemyslaw Czarnowski if (mode == VmMode::Invalid) 31679fdf63eSPrzemyslaw Czarnowski { 31779fdf63eSPrzemyslaw Czarnowski return; 31879fdf63eSPrzemyslaw Czarnowski } 31979fdf63eSPrzemyslaw Czarnowski 32079fdf63eSPrzemyslaw Czarnowski asyncResp->res.jsonValue = vmItemTemplate(name, resName); 32179fdf63eSPrzemyslaw Czarnowski 32279fdf63eSPrzemyslaw Czarnowski // Check if dbus path is Legacy type 32379fdf63eSPrzemyslaw Czarnowski if (mode == VmMode::Legacy) 32479fdf63eSPrzemyslaw Czarnowski { 325ef4c65b7SEd Tanous asyncResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"] 326ef4c65b7SEd Tanous ["target"] = boost::urls::format( 327ef4c65b7SEd Tanous "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.InsertMedia", 328ef4c65b7SEd Tanous name, resName); 32979fdf63eSPrzemyslaw Czarnowski } 33079fdf63eSPrzemyslaw Czarnowski 33179fdf63eSPrzemyslaw Czarnowski vmParseInterfaceObject(item.second, asyncResp); 33279fdf63eSPrzemyslaw Czarnowski 333ef4c65b7SEd Tanous asyncResp->res.jsonValue["Actions"]["#VirtualMedia.EjectMedia"] 334ef4c65b7SEd Tanous ["target"] = boost::urls::format( 335ef4c65b7SEd Tanous "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.EjectMedia", 336ef4c65b7SEd Tanous name, resName); 33779fdf63eSPrzemyslaw Czarnowski } 33879fdf63eSPrzemyslaw Czarnowski 339107077deSPrzemyslaw Czarnowski /** 340107077deSPrzemyslaw Czarnowski * @brief Fills data for specific resource 341107077deSPrzemyslaw Czarnowski */ 342ac106bf6SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 343107077deSPrzemyslaw Czarnowski const std::string& service, const std::string& name, 344107077deSPrzemyslaw Czarnowski const std::string& resName) 345107077deSPrzemyslaw Czarnowski { 34662598e31SEd Tanous BMCWEB_LOG_DEBUG("Get Virtual Media resource data."); 347107077deSPrzemyslaw Czarnowski 348ac106bf6SEd Tanous findAndParseObject(service, resName, asyncResp, 34970cbdf53SGeorge Liu std::bind_front(afterGetVmData, name)); 350107077deSPrzemyslaw Czarnowski } 351107077deSPrzemyslaw Czarnowski 352e13c2760SPrzemyslaw Czarnowski /** 353c6f4e017SAgata Olender * @brief Transfer protocols supported for InsertMedia action. 354c6f4e017SAgata Olender * 355c6f4e017SAgata Olender */ 356c6f4e017SAgata Olender enum class TransferProtocol 357c6f4e017SAgata Olender { 358c6f4e017SAgata Olender https, 359c6f4e017SAgata Olender smb, 360c6f4e017SAgata Olender invalid 361c6f4e017SAgata Olender }; 362c6f4e017SAgata Olender 363c6f4e017SAgata Olender /** 364c6f4e017SAgata Olender * @brief Function extracts transfer protocol type from URI. 365c6f4e017SAgata Olender * 366c6f4e017SAgata Olender */ 36767df073bSEd Tanous inline std::optional<TransferProtocol> 368d9f466b3SEd Tanous getTransferProtocolFromUri(boost::urls::url_view imageUri) 36967df073bSEd Tanous { 370079360aeSEd Tanous std::string_view scheme = imageUri.scheme(); 37167df073bSEd Tanous if (scheme == "smb") 37267df073bSEd Tanous { 37367df073bSEd Tanous return TransferProtocol::smb; 37467df073bSEd Tanous } 37567df073bSEd Tanous if (scheme == "https") 37667df073bSEd Tanous { 37767df073bSEd Tanous return TransferProtocol::https; 37867df073bSEd Tanous } 37967df073bSEd Tanous if (!scheme.empty()) 38067df073bSEd Tanous { 38167df073bSEd Tanous return TransferProtocol::invalid; 38267df073bSEd Tanous } 38367df073bSEd Tanous 38467df073bSEd Tanous return {}; 38567df073bSEd Tanous } 386c6f4e017SAgata Olender 387c6f4e017SAgata Olender /** 388c6f4e017SAgata Olender * @brief Function convert transfer protocol from string param. 389c6f4e017SAgata Olender * 390c6f4e017SAgata Olender */ 39122db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam( 392c6f4e017SAgata Olender const std::optional<std::string>& transferProtocolType) 393c6f4e017SAgata Olender { 394*e01d0c36SEd Tanous if (!transferProtocolType) 395c6f4e017SAgata Olender { 396c6f4e017SAgata Olender return {}; 397c6f4e017SAgata Olender } 398c6f4e017SAgata Olender 399c6f4e017SAgata Olender if (*transferProtocolType == "CIFS") 400c6f4e017SAgata Olender { 401c6f4e017SAgata Olender return TransferProtocol::smb; 402c6f4e017SAgata Olender } 403c6f4e017SAgata Olender 404c6f4e017SAgata Olender if (*transferProtocolType == "HTTPS") 405c6f4e017SAgata Olender { 406c6f4e017SAgata Olender return TransferProtocol::https; 407c6f4e017SAgata Olender } 408c6f4e017SAgata Olender 409c6f4e017SAgata Olender return TransferProtocol::invalid; 410c6f4e017SAgata Olender } 411c6f4e017SAgata Olender 412c6f4e017SAgata Olender /** 413c6f4e017SAgata Olender * @brief Function extends URI with transfer protocol type. 414c6f4e017SAgata Olender * 415c6f4e017SAgata Olender */ 41622db1728SEd Tanous inline std::string 417c6f4e017SAgata Olender getUriWithTransferProtocol(const std::string& imageUri, 418c6f4e017SAgata Olender const TransferProtocol& transferProtocol) 419c6f4e017SAgata Olender { 420c6f4e017SAgata Olender if (transferProtocol == TransferProtocol::smb) 421c6f4e017SAgata Olender { 422c6f4e017SAgata Olender return "smb://" + imageUri; 423c6f4e017SAgata Olender } 424c6f4e017SAgata Olender 425c6f4e017SAgata Olender if (transferProtocol == TransferProtocol::https) 426c6f4e017SAgata Olender { 427c6f4e017SAgata Olender return "https://" + imageUri; 428c6f4e017SAgata Olender } 429c6f4e017SAgata Olender 430c6f4e017SAgata Olender return imageUri; 431c6f4e017SAgata Olender } 432c6f4e017SAgata Olender 4331f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams 4341f2a40ceSPrzemyslaw Czarnowski { 435120fa86aSPrzemyslaw Czarnowski std::optional<std::string> imageUrl; 4361f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> userName; 4371f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> password; 4381f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> transferMethod; 4391f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> transferProtocolType; 4401f2a40ceSPrzemyslaw Czarnowski std::optional<bool> writeProtected = true; 4411f2a40ceSPrzemyslaw Czarnowski std::optional<bool> inserted; 4421f2a40ceSPrzemyslaw Czarnowski }; 4431f2a40ceSPrzemyslaw Czarnowski 4441214b7e7SGunnar Mills template <typename T> 4451214b7e7SGunnar Mills static void secureCleanup(T& value) 446988fb7b2SAdrian Ambrożewicz { 4474ecc618fSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) 448988fb7b2SAdrian Ambrożewicz auto raw = const_cast<typename T::value_type*>(value.data()); 449988fb7b2SAdrian Ambrożewicz explicit_bzero(raw, value.size() * sizeof(*raw)); 450988fb7b2SAdrian Ambrożewicz } 451988fb7b2SAdrian Ambrożewicz 452988fb7b2SAdrian Ambrożewicz class Credentials 453988fb7b2SAdrian Ambrożewicz { 454988fb7b2SAdrian Ambrożewicz public: 455988fb7b2SAdrian Ambrożewicz Credentials(std::string&& user, std::string&& password) : 456988fb7b2SAdrian Ambrożewicz userBuf(std::move(user)), passBuf(std::move(password)) 4571214b7e7SGunnar Mills {} 458988fb7b2SAdrian Ambrożewicz 459988fb7b2SAdrian Ambrożewicz ~Credentials() 460988fb7b2SAdrian Ambrożewicz { 461988fb7b2SAdrian Ambrożewicz secureCleanup(userBuf); 462988fb7b2SAdrian Ambrożewicz secureCleanup(passBuf); 463988fb7b2SAdrian Ambrożewicz } 464988fb7b2SAdrian Ambrożewicz 465988fb7b2SAdrian Ambrożewicz const std::string& user() 466988fb7b2SAdrian Ambrożewicz { 467988fb7b2SAdrian Ambrożewicz return userBuf; 468988fb7b2SAdrian Ambrożewicz } 469988fb7b2SAdrian Ambrożewicz 470988fb7b2SAdrian Ambrożewicz const std::string& password() 471988fb7b2SAdrian Ambrożewicz { 472988fb7b2SAdrian Ambrożewicz return passBuf; 473988fb7b2SAdrian Ambrożewicz } 474988fb7b2SAdrian Ambrożewicz 475988fb7b2SAdrian Ambrożewicz Credentials() = delete; 476988fb7b2SAdrian Ambrożewicz Credentials(const Credentials&) = delete; 477988fb7b2SAdrian Ambrożewicz Credentials& operator=(const Credentials&) = delete; 478ecd6a3a2SEd Tanous Credentials(Credentials&&) = delete; 479ecd6a3a2SEd Tanous Credentials& operator=(Credentials&&) = delete; 480988fb7b2SAdrian Ambrożewicz 48122db1728SEd Tanous private: 482988fb7b2SAdrian Ambrożewicz std::string userBuf; 483988fb7b2SAdrian Ambrożewicz std::string passBuf; 484988fb7b2SAdrian Ambrożewicz }; 485988fb7b2SAdrian Ambrożewicz 486988fb7b2SAdrian Ambrożewicz class CredentialsProvider 487988fb7b2SAdrian Ambrożewicz { 488988fb7b2SAdrian Ambrożewicz public: 4891214b7e7SGunnar Mills template <typename T> 4901214b7e7SGunnar Mills struct Deleter 491988fb7b2SAdrian Ambrożewicz { 492988fb7b2SAdrian Ambrożewicz void operator()(T* buff) const 493988fb7b2SAdrian Ambrożewicz { 494988fb7b2SAdrian Ambrożewicz if (buff) 495988fb7b2SAdrian Ambrożewicz { 496988fb7b2SAdrian Ambrożewicz secureCleanup(*buff); 497988fb7b2SAdrian Ambrożewicz delete buff; 498988fb7b2SAdrian Ambrożewicz } 499988fb7b2SAdrian Ambrożewicz } 500988fb7b2SAdrian Ambrożewicz }; 501988fb7b2SAdrian Ambrożewicz 502988fb7b2SAdrian Ambrożewicz using Buffer = std::vector<char>; 503988fb7b2SAdrian Ambrożewicz using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>; 504988fb7b2SAdrian Ambrożewicz // Using explicit definition instead of std::function to avoid implicit 505988fb7b2SAdrian Ambrożewicz // conversions eg. stack copy instead of reference 506988fb7b2SAdrian Ambrożewicz using FormatterFunc = void(const std::string& username, 507988fb7b2SAdrian Ambrożewicz const std::string& password, Buffer& dest); 508988fb7b2SAdrian Ambrożewicz 509988fb7b2SAdrian Ambrożewicz CredentialsProvider(std::string&& user, std::string&& password) : 510988fb7b2SAdrian Ambrożewicz credentials(std::move(user), std::move(password)) 5111214b7e7SGunnar Mills {} 512988fb7b2SAdrian Ambrożewicz 513988fb7b2SAdrian Ambrożewicz const std::string& user() 514988fb7b2SAdrian Ambrożewicz { 515988fb7b2SAdrian Ambrożewicz return credentials.user(); 516988fb7b2SAdrian Ambrożewicz } 517988fb7b2SAdrian Ambrożewicz 518988fb7b2SAdrian Ambrożewicz const std::string& password() 519988fb7b2SAdrian Ambrożewicz { 520988fb7b2SAdrian Ambrożewicz return credentials.password(); 521988fb7b2SAdrian Ambrożewicz } 522988fb7b2SAdrian Ambrożewicz 5231917ee95SEd Tanous SecureBuffer pack(FormatterFunc* formatter) 524988fb7b2SAdrian Ambrożewicz { 525988fb7b2SAdrian Ambrożewicz SecureBuffer packed{new Buffer{}}; 526e662eae8SEd Tanous if (formatter != nullptr) 527988fb7b2SAdrian Ambrożewicz { 528988fb7b2SAdrian Ambrożewicz formatter(credentials.user(), credentials.password(), *packed); 529988fb7b2SAdrian Ambrożewicz } 530988fb7b2SAdrian Ambrożewicz 531988fb7b2SAdrian Ambrożewicz return packed; 532988fb7b2SAdrian Ambrożewicz } 533988fb7b2SAdrian Ambrożewicz 534988fb7b2SAdrian Ambrożewicz private: 535988fb7b2SAdrian Ambrożewicz Credentials credentials; 536988fb7b2SAdrian Ambrożewicz }; 537988fb7b2SAdrian Ambrożewicz 538988fb7b2SAdrian Ambrożewicz // Wrapper for boost::async_pipe ensuring proper pipe cleanup 5390a48306bSEd Tanous class SecurePipe 540988fb7b2SAdrian Ambrożewicz { 541988fb7b2SAdrian Ambrożewicz public: 542988fb7b2SAdrian Ambrożewicz using unix_fd = sdbusplus::message::unix_fd; 543988fb7b2SAdrian Ambrożewicz 5440a48306bSEd Tanous SecurePipe(boost::asio::io_context& io, 5450a48306bSEd Tanous CredentialsProvider::SecureBuffer&& bufferIn) : 5460a48306bSEd Tanous impl(io), 5470a48306bSEd Tanous buffer{std::move(bufferIn)} 5481214b7e7SGunnar Mills {} 549988fb7b2SAdrian Ambrożewicz 5500a48306bSEd Tanous ~SecurePipe() 551988fb7b2SAdrian Ambrożewicz { 552988fb7b2SAdrian Ambrożewicz // Named pipe needs to be explicitly removed 553988fb7b2SAdrian Ambrożewicz impl.close(); 554988fb7b2SAdrian Ambrożewicz } 555988fb7b2SAdrian Ambrożewicz 5560a48306bSEd Tanous SecurePipe(const SecurePipe&) = delete; 5570a48306bSEd Tanous SecurePipe(SecurePipe&&) = delete; 5580a48306bSEd Tanous SecurePipe& operator=(const SecurePipe&) = delete; 5590a48306bSEd Tanous SecurePipe& operator=(SecurePipe&&) = delete; 560ecd6a3a2SEd Tanous 5610a48306bSEd Tanous unix_fd fd() const 562988fb7b2SAdrian Ambrożewicz { 563988fb7b2SAdrian Ambrożewicz return unix_fd{impl.native_source()}; 564988fb7b2SAdrian Ambrożewicz } 565988fb7b2SAdrian Ambrożewicz 566988fb7b2SAdrian Ambrożewicz template <typename WriteHandler> 56781ce609eSEd Tanous void asyncWrite(WriteHandler&& handler) 568988fb7b2SAdrian Ambrożewicz { 5690a48306bSEd Tanous impl.async_write_some(boost::asio::buffer(*buffer), 5700a48306bSEd Tanous std::forward<WriteHandler>(handler)); 571988fb7b2SAdrian Ambrożewicz } 572988fb7b2SAdrian Ambrożewicz 573988fb7b2SAdrian Ambrożewicz const std::string name; 574988fb7b2SAdrian Ambrożewicz boost::process::async_pipe impl; 5750a48306bSEd Tanous CredentialsProvider::SecureBuffer buffer; 576988fb7b2SAdrian Ambrożewicz }; 577988fb7b2SAdrian Ambrożewicz 578e13c2760SPrzemyslaw Czarnowski /** 579e13c2760SPrzemyslaw Czarnowski * @brief Function transceives data with dbus directly. 580e13c2760SPrzemyslaw Czarnowski * 581e13c2760SPrzemyslaw Czarnowski * All BMC state properties will be retrieved before sending reset request. 582e13c2760SPrzemyslaw Czarnowski */ 58322db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 584e13c2760SPrzemyslaw Czarnowski const std::string& service, const std::string& name, 585988fb7b2SAdrian Ambrożewicz const std::string& imageUrl, const bool rw, 586988fb7b2SAdrian Ambrożewicz std::string&& userName, std::string&& password) 587e13c2760SPrzemyslaw Czarnowski { 588988fb7b2SAdrian Ambrożewicz constexpr const size_t secretLimit = 1024; 589988fb7b2SAdrian Ambrożewicz 590988fb7b2SAdrian Ambrożewicz std::shared_ptr<SecurePipe> secretPipe; 591168e20c1SEd Tanous dbus::utility::DbusVariantType unixFd = -1; 592988fb7b2SAdrian Ambrożewicz 593988fb7b2SAdrian Ambrożewicz if (!userName.empty() || !password.empty()) 594988fb7b2SAdrian Ambrożewicz { 595988fb7b2SAdrian Ambrożewicz // Encapsulate in safe buffer 596988fb7b2SAdrian Ambrożewicz CredentialsProvider credentials(std::move(userName), 597988fb7b2SAdrian Ambrożewicz std::move(password)); 598988fb7b2SAdrian Ambrożewicz 599988fb7b2SAdrian Ambrożewicz // Payload must contain data + NULL delimiters 600988fb7b2SAdrian Ambrożewicz if (credentials.user().size() + credentials.password().size() + 2 > 601988fb7b2SAdrian Ambrożewicz secretLimit) 602988fb7b2SAdrian Ambrożewicz { 60362598e31SEd Tanous BMCWEB_LOG_ERROR("Credentials too long to handle"); 604988fb7b2SAdrian Ambrożewicz messages::unrecognizedRequestBody(asyncResp->res); 605988fb7b2SAdrian Ambrożewicz return; 606988fb7b2SAdrian Ambrożewicz } 607988fb7b2SAdrian Ambrożewicz 608988fb7b2SAdrian Ambrożewicz // Pack secret 60922db1728SEd Tanous auto secret = credentials.pack( 61022db1728SEd Tanous [](const auto& user, const auto& pass, auto& buff) { 611988fb7b2SAdrian Ambrożewicz std::copy(user.begin(), user.end(), std::back_inserter(buff)); 612988fb7b2SAdrian Ambrożewicz buff.push_back('\0'); 613988fb7b2SAdrian Ambrożewicz std::copy(pass.begin(), pass.end(), std::back_inserter(buff)); 614988fb7b2SAdrian Ambrożewicz buff.push_back('\0'); 615988fb7b2SAdrian Ambrożewicz }); 616988fb7b2SAdrian Ambrożewicz 617988fb7b2SAdrian Ambrożewicz // Open pipe 618988fb7b2SAdrian Ambrożewicz secretPipe = std::make_shared<SecurePipe>( 61922db1728SEd Tanous crow::connections::systemBus->get_io_context(), std::move(secret)); 620988fb7b2SAdrian Ambrożewicz unixFd = secretPipe->fd(); 621988fb7b2SAdrian Ambrożewicz 622988fb7b2SAdrian Ambrożewicz // Pass secret over pipe 62381ce609eSEd Tanous secretPipe->asyncWrite( 624f5b16f03SVikram Bodireddy [asyncResp](const boost::system::error_code& ec, std::size_t) { 625988fb7b2SAdrian Ambrożewicz if (ec) 626988fb7b2SAdrian Ambrożewicz { 62762598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to pass secret: {}", ec); 628988fb7b2SAdrian Ambrożewicz messages::internalError(asyncResp->res); 629988fb7b2SAdrian Ambrożewicz } 630988fb7b2SAdrian Ambrożewicz }); 631988fb7b2SAdrian Ambrożewicz } 632988fb7b2SAdrian Ambrożewicz 633e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 6345e7e2dc5SEd Tanous [asyncResp, secretPipe](const boost::system::error_code& ec, 635988fb7b2SAdrian Ambrożewicz bool success) { 636e13c2760SPrzemyslaw Czarnowski if (ec) 637e13c2760SPrzemyslaw Czarnowski { 63862598e31SEd Tanous BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); 639e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 640d6da5bebSAdrian Ambrożewicz } 641d6da5bebSAdrian Ambrożewicz else if (!success) 642d6da5bebSAdrian Ambrożewicz { 64362598e31SEd Tanous BMCWEB_LOG_ERROR("Service responded with error"); 644d6da5bebSAdrian Ambrożewicz messages::generalError(asyncResp->res); 645e13c2760SPrzemyslaw Czarnowski } 646e13c2760SPrzemyslaw Czarnowski }, 647e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, 648988fb7b2SAdrian Ambrożewicz "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw, 649988fb7b2SAdrian Ambrożewicz unixFd); 650e13c2760SPrzemyslaw Czarnowski } 651e13c2760SPrzemyslaw Czarnowski 652e13c2760SPrzemyslaw Czarnowski /** 653120fa86aSPrzemyslaw Czarnowski * @brief Function validate parameters of insert media request. 654120fa86aSPrzemyslaw Czarnowski * 655120fa86aSPrzemyslaw Czarnowski */ 656120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 657120fa86aSPrzemyslaw Czarnowski const std::string& service, 658120fa86aSPrzemyslaw Czarnowski const std::string& resName, 659120fa86aSPrzemyslaw Czarnowski InsertMediaActionParams& actionParams) 660120fa86aSPrzemyslaw Czarnowski { 66162598e31SEd Tanous BMCWEB_LOG_DEBUG("Validation started"); 662120fa86aSPrzemyslaw Czarnowski // required param imageUrl must not be empty 663120fa86aSPrzemyslaw Czarnowski if (!actionParams.imageUrl) 664120fa86aSPrzemyslaw Czarnowski { 66562598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter Image is empty."); 666120fa86aSPrzemyslaw Czarnowski 667120fa86aSPrzemyslaw Czarnowski messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image"); 668120fa86aSPrzemyslaw Czarnowski 669120fa86aSPrzemyslaw Czarnowski return; 670120fa86aSPrzemyslaw Czarnowski } 671120fa86aSPrzemyslaw Czarnowski 672120fa86aSPrzemyslaw Czarnowski // optional param inserted must be true 673*e01d0c36SEd Tanous if (actionParams.inserted && !*actionParams.inserted) 674120fa86aSPrzemyslaw Czarnowski { 67562598e31SEd Tanous BMCWEB_LOG_ERROR( 67662598e31SEd Tanous "Request action optional parameter Inserted must be true."); 677120fa86aSPrzemyslaw Czarnowski 678120fa86aSPrzemyslaw Czarnowski messages::actionParameterNotSupported(asyncResp->res, "Inserted", 679120fa86aSPrzemyslaw Czarnowski "InsertMedia"); 680120fa86aSPrzemyslaw Czarnowski 681120fa86aSPrzemyslaw Czarnowski return; 682120fa86aSPrzemyslaw Czarnowski } 683120fa86aSPrzemyslaw Czarnowski 684120fa86aSPrzemyslaw Czarnowski // optional param transferMethod must be stream 685*e01d0c36SEd Tanous if (actionParams.transferMethod && 686120fa86aSPrzemyslaw Czarnowski (*actionParams.transferMethod != "Stream")) 687120fa86aSPrzemyslaw Czarnowski { 68862598e31SEd Tanous BMCWEB_LOG_ERROR("Request action optional parameter " 68962598e31SEd Tanous "TransferMethod must be Stream."); 690120fa86aSPrzemyslaw Czarnowski 691120fa86aSPrzemyslaw Czarnowski messages::actionParameterNotSupported(asyncResp->res, "TransferMethod", 692120fa86aSPrzemyslaw Czarnowski "InsertMedia"); 693120fa86aSPrzemyslaw Czarnowski 694120fa86aSPrzemyslaw Czarnowski return; 695120fa86aSPrzemyslaw Czarnowski } 696120fa86aSPrzemyslaw Czarnowski boost::urls::result<boost::urls::url_view> url = 697120fa86aSPrzemyslaw Czarnowski boost::urls::parse_uri(*actionParams.imageUrl); 698120fa86aSPrzemyslaw Czarnowski if (!url) 699120fa86aSPrzemyslaw Czarnowski { 700120fa86aSPrzemyslaw Czarnowski messages::actionParameterValueFormatError( 701120fa86aSPrzemyslaw Czarnowski asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia"); 702120fa86aSPrzemyslaw Czarnowski return; 703120fa86aSPrzemyslaw Czarnowski } 704120fa86aSPrzemyslaw Czarnowski std::optional<TransferProtocol> uriTransferProtocolType = 705120fa86aSPrzemyslaw Czarnowski getTransferProtocolFromUri(*url); 706120fa86aSPrzemyslaw Czarnowski 707120fa86aSPrzemyslaw Czarnowski std::optional<TransferProtocol> paramTransferProtocolType = 708120fa86aSPrzemyslaw Czarnowski getTransferProtocolFromParam(actionParams.transferProtocolType); 709120fa86aSPrzemyslaw Czarnowski 710120fa86aSPrzemyslaw Czarnowski // ImageUrl does not contain valid protocol type 711*e01d0c36SEd Tanous if (uriTransferProtocolType && 712*e01d0c36SEd Tanous *uriTransferProtocolType == TransferProtocol::invalid) 713120fa86aSPrzemyslaw Czarnowski { 71462598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter ImageUrl must " 715120fa86aSPrzemyslaw Czarnowski "contain specified protocol type from list: " 71662598e31SEd Tanous "(smb, https)."); 717120fa86aSPrzemyslaw Czarnowski 718120fa86aSPrzemyslaw Czarnowski messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); 719120fa86aSPrzemyslaw Czarnowski 720120fa86aSPrzemyslaw Czarnowski return; 721120fa86aSPrzemyslaw Czarnowski } 722120fa86aSPrzemyslaw Czarnowski 723120fa86aSPrzemyslaw Czarnowski // transferProtocolType should contain value from list 724*e01d0c36SEd Tanous if (paramTransferProtocolType && 725*e01d0c36SEd Tanous *paramTransferProtocolType == TransferProtocol::invalid) 726120fa86aSPrzemyslaw Czarnowski { 72762598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter TransferProtocolType " 728120fa86aSPrzemyslaw Czarnowski "must be provided with value from list: " 72962598e31SEd Tanous "(CIFS, HTTPS)."); 730120fa86aSPrzemyslaw Czarnowski 731*e01d0c36SEd Tanous messages::propertyValueNotInList( 732*e01d0c36SEd Tanous asyncResp->res, actionParams.transferProtocolType.value_or(""), 733120fa86aSPrzemyslaw Czarnowski "TransferProtocolType"); 734120fa86aSPrzemyslaw Czarnowski return; 735120fa86aSPrzemyslaw Czarnowski } 736120fa86aSPrzemyslaw Czarnowski 737120fa86aSPrzemyslaw Czarnowski // valid transfer protocol not provided either with URI nor param 738*e01d0c36SEd Tanous if (!uriTransferProtocolType && !paramTransferProtocolType) 739120fa86aSPrzemyslaw Czarnowski { 74062598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter ImageUrl must " 741120fa86aSPrzemyslaw Czarnowski "contain specified protocol type or param " 74262598e31SEd Tanous "TransferProtocolType must be provided."); 743120fa86aSPrzemyslaw Czarnowski 744120fa86aSPrzemyslaw Czarnowski messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); 745120fa86aSPrzemyslaw Czarnowski 746120fa86aSPrzemyslaw Czarnowski return; 747120fa86aSPrzemyslaw Czarnowski } 748120fa86aSPrzemyslaw Czarnowski 749120fa86aSPrzemyslaw Czarnowski // valid transfer protocol provided both with URI and param 750*e01d0c36SEd Tanous if (paramTransferProtocolType && uriTransferProtocolType) 751120fa86aSPrzemyslaw Czarnowski { 752120fa86aSPrzemyslaw Czarnowski // check if protocol is the same for URI and param 753120fa86aSPrzemyslaw Czarnowski if (*paramTransferProtocolType != *uriTransferProtocolType) 754120fa86aSPrzemyslaw Czarnowski { 75562598e31SEd Tanous BMCWEB_LOG_ERROR("Request action parameter " 756120fa86aSPrzemyslaw Czarnowski "TransferProtocolType must contain the " 757120fa86aSPrzemyslaw Czarnowski "same protocol type as protocol type " 75862598e31SEd Tanous "provided with param imageUrl."); 759120fa86aSPrzemyslaw Czarnowski 760120fa86aSPrzemyslaw Czarnowski messages::actionParameterValueTypeError( 761*e01d0c36SEd Tanous asyncResp->res, actionParams.transferProtocolType.value_or(""), 762120fa86aSPrzemyslaw Czarnowski "TransferProtocolType", "InsertMedia"); 763120fa86aSPrzemyslaw Czarnowski 764120fa86aSPrzemyslaw Czarnowski return; 765120fa86aSPrzemyslaw Czarnowski } 766120fa86aSPrzemyslaw Czarnowski } 767*e01d0c36SEd Tanous if (!paramTransferProtocolType) 768*e01d0c36SEd Tanous { 769*e01d0c36SEd Tanous messages::internalError(asyncResp->res); 770*e01d0c36SEd Tanous return; 771*e01d0c36SEd Tanous } 772120fa86aSPrzemyslaw Czarnowski 773120fa86aSPrzemyslaw Czarnowski // validation passed, add protocol to URI if needed 774*e01d0c36SEd Tanous if (!uriTransferProtocolType) 775120fa86aSPrzemyslaw Czarnowski { 776120fa86aSPrzemyslaw Czarnowski actionParams.imageUrl = getUriWithTransferProtocol( 777120fa86aSPrzemyslaw Czarnowski *actionParams.imageUrl, *paramTransferProtocolType); 778120fa86aSPrzemyslaw Czarnowski } 779120fa86aSPrzemyslaw Czarnowski 780452bd8d8SJayaprakash Mutyala if (!actionParams.userName) 781452bd8d8SJayaprakash Mutyala { 782452bd8d8SJayaprakash Mutyala actionParams.userName = ""; 783452bd8d8SJayaprakash Mutyala } 784452bd8d8SJayaprakash Mutyala 785452bd8d8SJayaprakash Mutyala if (!actionParams.password) 786452bd8d8SJayaprakash Mutyala { 787452bd8d8SJayaprakash Mutyala actionParams.password = ""; 788452bd8d8SJayaprakash Mutyala } 789452bd8d8SJayaprakash Mutyala 790120fa86aSPrzemyslaw Czarnowski doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl, 791*e01d0c36SEd Tanous !(actionParams.writeProtected.value_or(false)), 792120fa86aSPrzemyslaw Czarnowski std::move(*actionParams.userName), 793120fa86aSPrzemyslaw Czarnowski std::move(*actionParams.password)); 794120fa86aSPrzemyslaw Czarnowski } 795120fa86aSPrzemyslaw Czarnowski 796120fa86aSPrzemyslaw Czarnowski /** 797e13c2760SPrzemyslaw Czarnowski * @brief Function transceives data with dbus directly. 798e13c2760SPrzemyslaw Czarnowski * 799e13c2760SPrzemyslaw Czarnowski * All BMC state properties will be retrieved before sending reset request. 800e13c2760SPrzemyslaw Czarnowski */ 80124e740a7SEd Tanous inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 802e13c2760SPrzemyslaw Czarnowski const std::string& service, const std::string& name, 803e13c2760SPrzemyslaw Czarnowski bool legacy) 804e13c2760SPrzemyslaw Czarnowski { 805e13c2760SPrzemyslaw Czarnowski // Legacy mount requires parameter with image 806e13c2760SPrzemyslaw Czarnowski if (legacy) 807e13c2760SPrzemyslaw Czarnowski { 808e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 8095e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 810e13c2760SPrzemyslaw Czarnowski if (ec) 811e13c2760SPrzemyslaw Czarnowski { 81262598e31SEd Tanous BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); 813e13c2760SPrzemyslaw Czarnowski 814e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 815e13c2760SPrzemyslaw Czarnowski return; 816e13c2760SPrzemyslaw Czarnowski } 817e13c2760SPrzemyslaw Czarnowski }, 818e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, 819e13c2760SPrzemyslaw Czarnowski "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount"); 820e13c2760SPrzemyslaw Czarnowski } 821e13c2760SPrzemyslaw Czarnowski else // proxy 822e13c2760SPrzemyslaw Czarnowski { 823e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 8245e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 825e13c2760SPrzemyslaw Czarnowski if (ec) 826e13c2760SPrzemyslaw Czarnowski { 82762598e31SEd Tanous BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec); 828e13c2760SPrzemyslaw Czarnowski 829e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 830e13c2760SPrzemyslaw Czarnowski return; 831e13c2760SPrzemyslaw Czarnowski } 832e13c2760SPrzemyslaw Czarnowski }, 833e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name, 834e13c2760SPrzemyslaw Czarnowski "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); 835e13c2760SPrzemyslaw Czarnowski } 836e13c2760SPrzemyslaw Czarnowski } 837e13c2760SPrzemyslaw Czarnowski 83896825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost( 83996825bebSEd Tanous crow::App& app, const crow::Request& req, 84022db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 84196825bebSEd Tanous const std::string& name, const std::string& resName) 84296825bebSEd Tanous { 8433ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 84445ca1b86SEd Tanous { 84545ca1b86SEd Tanous return; 84645ca1b86SEd Tanous } 84779fdf63eSPrzemyslaw Czarnowski 84879fdf63eSPrzemyslaw Czarnowski constexpr std::string_view action = "VirtualMedia.InsertMedia"; 84922db1728SEd Tanous if (name != "bmc") 850107077deSPrzemyslaw Czarnowski { 85179fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 852107077deSPrzemyslaw Czarnowski 853107077deSPrzemyslaw Czarnowski return; 854107077deSPrzemyslaw Czarnowski } 85579fdf63eSPrzemyslaw Czarnowski InsertMediaActionParams actionParams; 85698be3e39SEd Tanous 857120fa86aSPrzemyslaw Czarnowski // Read obligatory parameters (url of image) 85815ed6780SWilly Tu if (!json_util::readJsonAction( 85979fdf63eSPrzemyslaw Czarnowski req, asyncResp->res, "Image", actionParams.imageUrl, 86079fdf63eSPrzemyslaw Czarnowski "WriteProtected", actionParams.writeProtected, "UserName", 86179fdf63eSPrzemyslaw Czarnowski actionParams.userName, "Password", actionParams.password, 86279fdf63eSPrzemyslaw Czarnowski "Inserted", actionParams.inserted, "TransferMethod", 86379fdf63eSPrzemyslaw Czarnowski actionParams.transferMethod, "TransferProtocolType", 86479fdf63eSPrzemyslaw Czarnowski actionParams.transferProtocolType)) 86598be3e39SEd Tanous { 86698be3e39SEd Tanous return; 86798be3e39SEd Tanous } 868107077deSPrzemyslaw Czarnowski 8692b73119cSGeorge Liu dbus::utility::getDbusObject( 8702b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 87179fdf63eSPrzemyslaw Czarnowski [asyncResp, action, actionParams, 8722b73119cSGeorge Liu resName](const boost::system::error_code& ec, 873002d39b4SEd Tanous const dbus::utility::MapperGetObject& getObjectType) mutable { 87422db1728SEd Tanous if (ec) 87522db1728SEd Tanous { 87662598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); 87779fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 878107077deSPrzemyslaw Czarnowski 87922db1728SEd Tanous return; 88022db1728SEd Tanous } 88179fdf63eSPrzemyslaw Czarnowski 88222db1728SEd Tanous std::string service = getObjectType.begin()->first; 88362598e31SEd Tanous BMCWEB_LOG_DEBUG("GetObjectType: {}", service); 88422db1728SEd Tanous 8855eb468daSGeorge Liu sdbusplus::message::object_path path( 8865eb468daSGeorge Liu "/xyz/openbmc_project/VirtualMedia"); 8875eb468daSGeorge Liu dbus::utility::getManagedObjects( 8885eb468daSGeorge Liu service, path, 8895eb468daSGeorge Liu [service, resName, action, actionParams, asyncResp]( 8905eb468daSGeorge Liu const boost::system::error_code& ec2, 8915eb468daSGeorge Liu const dbus::utility::ManagedObjectType& subtree) mutable { 8928a592810SEd Tanous if (ec2) 89322db1728SEd Tanous { 89479fdf63eSPrzemyslaw Czarnowski // Not possible in proxy mode 89562598e31SEd Tanous BMCWEB_LOG_DEBUG("InsertMedia not " 89662598e31SEd Tanous "allowed in proxy mode"); 89779fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 89822db1728SEd Tanous 89922db1728SEd Tanous return; 90022db1728SEd Tanous } 90122db1728SEd Tanous for (const auto& object : subtree) 90222db1728SEd Tanous { 903365a73f4SEd Tanous VmMode mode = parseObjectPathAndGetMode(object.first, resName); 9045880f0c5SBoleslaw Ogonczyk Makowski if (mode == VmMode::Legacy) 90522db1728SEd Tanous { 90679fdf63eSPrzemyslaw Czarnowski validateParams(asyncResp, service, resName, actionParams); 90722db1728SEd Tanous 90822db1728SEd Tanous return; 90922db1728SEd Tanous } 91022db1728SEd Tanous } 91162598e31SEd Tanous BMCWEB_LOG_DEBUG("Parent item not found"); 91296825bebSEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 9135eb468daSGeorge Liu }); 9142b73119cSGeorge Liu }); 91596825bebSEd Tanous } 91622db1728SEd Tanous 91796825bebSEd Tanous inline void handleManagersVirtualMediaActionEject( 91896825bebSEd Tanous crow::App& app, const crow::Request& req, 91922db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 92096825bebSEd Tanous const std::string& managerName, const std::string& resName) 92196825bebSEd Tanous { 9223ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 92345ca1b86SEd Tanous { 92445ca1b86SEd Tanous return; 92545ca1b86SEd Tanous } 92679fdf63eSPrzemyslaw Czarnowski 92779fdf63eSPrzemyslaw Czarnowski constexpr std::string_view action = "VirtualMedia.EjectMedia"; 92896825bebSEd Tanous if (managerName != "bmc") 929107077deSPrzemyslaw Czarnowski { 93079fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 93122db1728SEd Tanous 93222db1728SEd Tanous return; 93322db1728SEd Tanous } 93422db1728SEd Tanous 9352b73119cSGeorge Liu dbus::utility::getDbusObject( 9362b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 93779fdf63eSPrzemyslaw Czarnowski [asyncResp, action, 9382b73119cSGeorge Liu resName](const boost::system::error_code& ec2, 939b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 9408a592810SEd Tanous if (ec2) 94122db1728SEd Tanous { 94262598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec2); 94322db1728SEd Tanous messages::internalError(asyncResp->res); 94422db1728SEd Tanous 94522db1728SEd Tanous return; 94622db1728SEd Tanous } 94722db1728SEd Tanous std::string service = getObjectType.begin()->first; 94862598e31SEd Tanous BMCWEB_LOG_DEBUG("GetObjectType: {}", service); 94922db1728SEd Tanous 9505eb468daSGeorge Liu sdbusplus::message::object_path path( 9515eb468daSGeorge Liu "/xyz/openbmc_project/VirtualMedia"); 9525eb468daSGeorge Liu dbus::utility::getManagedObjects( 9535eb468daSGeorge Liu service, path, 95479fdf63eSPrzemyslaw Czarnowski [resName, service, action, 95579fdf63eSPrzemyslaw Czarnowski asyncResp](const boost::system::error_code& ec, 95602cad96eSEd Tanous const dbus::utility::ManagedObjectType& subtree) { 95722db1728SEd Tanous if (ec) 95822db1728SEd Tanous { 95962598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper : No Service found"); 96079fdf63eSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, action, resName); 96122db1728SEd Tanous return; 96222db1728SEd Tanous } 96322db1728SEd Tanous 96422db1728SEd Tanous for (const auto& object : subtree) 96522db1728SEd Tanous { 966365a73f4SEd Tanous VmMode mode = parseObjectPathAndGetMode(object.first, resName); 967365a73f4SEd Tanous if (mode != VmMode::Invalid) 96822db1728SEd Tanous { 969365a73f4SEd Tanous doEjectAction(asyncResp, service, resName, 970365a73f4SEd Tanous mode == VmMode::Legacy); 9715880f0c5SBoleslaw Ogonczyk Makowski return; 97222db1728SEd Tanous } 97322db1728SEd Tanous } 97462598e31SEd Tanous BMCWEB_LOG_DEBUG("Parent item not found"); 97596825bebSEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 9765eb468daSGeorge Liu }); 9772b73119cSGeorge Liu }); 97896825bebSEd Tanous } 97996825bebSEd Tanous 98096825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet( 98196825bebSEd Tanous crow::App& app, const crow::Request& req, 98222db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 98396825bebSEd Tanous const std::string& name) 98496825bebSEd Tanous { 9853ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 98645ca1b86SEd Tanous { 98745ca1b86SEd Tanous return; 98845ca1b86SEd Tanous } 98922db1728SEd Tanous if (name != "bmc") 99022db1728SEd Tanous { 991002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", name); 992107077deSPrzemyslaw Czarnowski 993107077deSPrzemyslaw Czarnowski return; 994107077deSPrzemyslaw Czarnowski } 995107077deSPrzemyslaw Czarnowski 9968d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 997107077deSPrzemyslaw Czarnowski "#VirtualMediaCollection.VirtualMediaCollection"; 9988d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Virtual Media Services"; 999ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1000ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia", name); 1001107077deSPrzemyslaw Czarnowski 10022b73119cSGeorge Liu dbus::utility::getDbusObject( 10032b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 10042b73119cSGeorge Liu [asyncResp, name](const boost::system::error_code& ec, 1005b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 1006107077deSPrzemyslaw Czarnowski if (ec) 1007107077deSPrzemyslaw Czarnowski { 100862598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); 1009107077deSPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 1010107077deSPrzemyslaw Czarnowski 1011107077deSPrzemyslaw Czarnowski return; 1012107077deSPrzemyslaw Czarnowski } 1013107077deSPrzemyslaw Czarnowski std::string service = getObjectType.begin()->first; 101462598e31SEd Tanous BMCWEB_LOG_DEBUG("GetObjectType: {}", service); 1015107077deSPrzemyslaw Czarnowski 1016107077deSPrzemyslaw Czarnowski getVmResourceList(asyncResp, service, name); 10172b73119cSGeorge Liu }); 101896825bebSEd Tanous } 1019107077deSPrzemyslaw Czarnowski 102096825bebSEd Tanous inline void 102196825bebSEd Tanous handleVirtualMediaGet(crow::App& app, const crow::Request& req, 102222db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 102396825bebSEd Tanous const std::string& name, const std::string& resName) 102496825bebSEd Tanous { 10253ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 102645ca1b86SEd Tanous { 102745ca1b86SEd Tanous return; 102845ca1b86SEd Tanous } 1029107077deSPrzemyslaw Czarnowski if (name != "bmc") 1030107077deSPrzemyslaw Czarnowski { 1031002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 1032107077deSPrzemyslaw Czarnowski 1033107077deSPrzemyslaw Czarnowski return; 1034107077deSPrzemyslaw Czarnowski } 1035107077deSPrzemyslaw Czarnowski 10362b73119cSGeorge Liu dbus::utility::getDbusObject( 10372b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 1038002d39b4SEd Tanous [asyncResp, name, 10392b73119cSGeorge Liu resName](const boost::system::error_code& ec, 1040b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 1041107077deSPrzemyslaw Czarnowski if (ec) 1042107077deSPrzemyslaw Czarnowski { 104362598e31SEd Tanous BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec); 1044107077deSPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 1045107077deSPrzemyslaw Czarnowski 1046107077deSPrzemyslaw Czarnowski return; 1047107077deSPrzemyslaw Czarnowski } 1048107077deSPrzemyslaw Czarnowski std::string service = getObjectType.begin()->first; 104962598e31SEd Tanous BMCWEB_LOG_DEBUG("GetObjectType: {}", service); 1050107077deSPrzemyslaw Czarnowski 1051107077deSPrzemyslaw Czarnowski getVmData(asyncResp, service, name, resName); 10522b73119cSGeorge Liu }); 105396825bebSEd Tanous } 105496825bebSEd Tanous 105596825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app) 105696825bebSEd Tanous { 105796825bebSEd Tanous BMCWEB_ROUTE( 105896825bebSEd Tanous app, 105996825bebSEd Tanous "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia") 106096825bebSEd Tanous .privileges(redfish::privileges::postVirtualMedia) 106196825bebSEd Tanous .methods(boost::beast::http::verb::post)(std::bind_front( 106296825bebSEd Tanous handleManagersVirtualMediaActionInsertPost, std::ref(app))); 106396825bebSEd Tanous 106496825bebSEd Tanous BMCWEB_ROUTE( 106596825bebSEd Tanous app, 106696825bebSEd Tanous "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia") 106796825bebSEd Tanous .privileges(redfish::privileges::postVirtualMedia) 106896825bebSEd Tanous .methods(boost::beast::http::verb::post)(std::bind_front( 106996825bebSEd Tanous handleManagersVirtualMediaActionEject, std::ref(app))); 107096825bebSEd Tanous 107196825bebSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/") 107296825bebSEd Tanous .privileges(redfish::privileges::getVirtualMediaCollection) 107396825bebSEd Tanous .methods(boost::beast::http::verb::get)(std::bind_front( 107496825bebSEd Tanous handleManagersVirtualMediaCollectionGet, std::ref(app))); 107596825bebSEd Tanous 107696825bebSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/") 107796825bebSEd Tanous .privileges(redfish::privileges::getVirtualMedia) 107896825bebSEd Tanous .methods(boost::beast::http::verb::get)( 107996825bebSEd Tanous std::bind_front(handleVirtualMediaGet, std::ref(app))); 1080107077deSPrzemyslaw Czarnowski } 1081107077deSPrzemyslaw Czarnowski 1082107077deSPrzemyslaw Czarnowski } // namespace redfish 1083