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" 202b73119cSGeorge Liu #include "dbus_utility.hpp" 213ccb3adbSEd Tanous #include "query.hpp" 223ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 233ccb3adbSEd Tanous #include "utils/json_utils.hpp" 243ccb3adbSEd Tanous 25988fb7b2SAdrian Ambrożewicz #include <boost/process/async_pipe.hpp> 26988fb7b2SAdrian Ambrożewicz #include <boost/type_traits/has_dereference.hpp> 279e319cf0SAnna Platash #include <boost/url/url_view.hpp> 28107077deSPrzemyslaw Czarnowski 292b73119cSGeorge Liu #include <array> 302b73119cSGeorge Liu #include <string_view> 312b73119cSGeorge Liu 32107077deSPrzemyslaw Czarnowski namespace redfish 33107077deSPrzemyslaw Czarnowski { 349e319cf0SAnna Platash /** 359e319cf0SAnna Platash * @brief Function extracts transfer protocol name from URI. 369e319cf0SAnna Platash */ 3767df073bSEd Tanous inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri) 3867df073bSEd Tanous { 3967df073bSEd Tanous boost::urls::result<boost::urls::url_view> url = 40079360aeSEd Tanous boost::urls::parse_uri(imageUri); 4167df073bSEd Tanous if (!url) 4267df073bSEd Tanous { 4367df073bSEd Tanous return "None"; 4467df073bSEd Tanous } 45079360aeSEd Tanous std::string_view scheme = url->scheme(); 4667df073bSEd Tanous if (scheme == "smb") 4767df073bSEd Tanous { 4867df073bSEd Tanous return "CIFS"; 4967df073bSEd Tanous } 5067df073bSEd Tanous if (scheme == "https") 5167df073bSEd Tanous { 5267df073bSEd Tanous return "HTTPS"; 5367df073bSEd Tanous } 5467df073bSEd Tanous 5567df073bSEd Tanous return "None"; 5667df073bSEd Tanous } 57107077deSPrzemyslaw Czarnowski 58107077deSPrzemyslaw Czarnowski /** 59107077deSPrzemyslaw Czarnowski * @brief Read all known properties from VM object interfaces 60107077deSPrzemyslaw Czarnowski */ 6122db1728SEd Tanous inline void 628a592810SEd Tanous vmParseInterfaceObject(const dbus::utility::DBusInteracesMap& interfaces, 638d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& aResp) 64107077deSPrzemyslaw Czarnowski { 658a592810SEd Tanous for (const auto& [interface, values] : interfaces) 66107077deSPrzemyslaw Czarnowski { 67711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint") 68107077deSPrzemyslaw Czarnowski { 69711ac7a9SEd Tanous for (const auto& [property, value] : values) 70107077deSPrzemyslaw Czarnowski { 71711ac7a9SEd Tanous if (property == "EndpointId") 72107077deSPrzemyslaw Czarnowski { 73107077deSPrzemyslaw Czarnowski const std::string* endpointIdValue = 74711ac7a9SEd Tanous std::get_if<std::string>(&value); 75711ac7a9SEd Tanous if (endpointIdValue == nullptr) 76107077deSPrzemyslaw Czarnowski { 77711ac7a9SEd Tanous continue; 78711ac7a9SEd Tanous } 79107077deSPrzemyslaw Czarnowski if (!endpointIdValue->empty()) 80107077deSPrzemyslaw Czarnowski { 81107077deSPrzemyslaw Czarnowski // Proxy mode 82711ac7a9SEd Tanous aResp->res 83711ac7a9SEd Tanous .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] = 84d04ba325SPrzemyslaw Czarnowski *endpointIdValue; 85107077deSPrzemyslaw Czarnowski aResp->res.jsonValue["TransferProtocolType"] = "OEM"; 86107077deSPrzemyslaw Czarnowski } 87107077deSPrzemyslaw Czarnowski } 88711ac7a9SEd Tanous if (property == "ImageURL") 89107077deSPrzemyslaw Czarnowski { 90107077deSPrzemyslaw Czarnowski const std::string* imageUrlValue = 91711ac7a9SEd Tanous std::get_if<std::string>(&value); 9226f6976fSEd Tanous if (imageUrlValue != nullptr && !imageUrlValue->empty()) 93107077deSPrzemyslaw Czarnowski { 94da4784d8SPrzemyslaw Czarnowski std::filesystem::path filePath = *imageUrlValue; 95da4784d8SPrzemyslaw Czarnowski if (!filePath.has_filename()) 96da4784d8SPrzemyslaw Czarnowski { 979e319cf0SAnna Platash // this will handle https share, which not 989e319cf0SAnna Platash // necessarily has to have filename given. 99da4784d8SPrzemyslaw Czarnowski aResp->res.jsonValue["ImageName"] = ""; 100da4784d8SPrzemyslaw Czarnowski } 101da4784d8SPrzemyslaw Czarnowski else 102da4784d8SPrzemyslaw Czarnowski { 1039e319cf0SAnna Platash aResp->res.jsonValue["ImageName"] = 1049e319cf0SAnna Platash filePath.filename(); 105da4784d8SPrzemyslaw Czarnowski } 106da4784d8SPrzemyslaw Czarnowski 107da4784d8SPrzemyslaw Czarnowski aResp->res.jsonValue["Image"] = *imageUrlValue; 1089e319cf0SAnna Platash aResp->res.jsonValue["TransferProtocolType"] = 1099e319cf0SAnna Platash getTransferProtocolTypeFromUri(*imageUrlValue); 1109e319cf0SAnna Platash 111107077deSPrzemyslaw Czarnowski aResp->res.jsonValue["ConnectedVia"] = "URI"; 112107077deSPrzemyslaw Czarnowski } 113107077deSPrzemyslaw Czarnowski } 114711ac7a9SEd Tanous if (property == "WriteProtected") 1159e319cf0SAnna Platash { 116711ac7a9SEd Tanous const bool* writeProtectedValue = std::get_if<bool>(&value); 117e662eae8SEd Tanous if (writeProtectedValue != nullptr) 1189e319cf0SAnna Platash { 1199e319cf0SAnna Platash aResp->res.jsonValue["WriteProtected"] = 1209e319cf0SAnna Platash *writeProtectedValue; 1219e319cf0SAnna Platash } 1229e319cf0SAnna Platash } 1239e319cf0SAnna Platash } 124107077deSPrzemyslaw Czarnowski } 125711ac7a9SEd Tanous if (interface == "xyz.openbmc_project.VirtualMedia.Process") 126711ac7a9SEd Tanous { 127711ac7a9SEd Tanous for (const auto& [property, value] : values) 128711ac7a9SEd Tanous { 129711ac7a9SEd Tanous if (property == "Active") 130711ac7a9SEd Tanous { 131711ac7a9SEd Tanous const bool* activeValue = std::get_if<bool>(&value); 132e662eae8SEd Tanous if (activeValue == nullptr) 133711ac7a9SEd Tanous { 134711ac7a9SEd Tanous BMCWEB_LOG_DEBUG << "Value Active not found"; 135711ac7a9SEd Tanous return; 136711ac7a9SEd Tanous } 137711ac7a9SEd Tanous aResp->res.jsonValue["Inserted"] = *activeValue; 138711ac7a9SEd Tanous 139e05aec50SEd Tanous if (*activeValue) 140711ac7a9SEd Tanous { 141711ac7a9SEd Tanous aResp->res.jsonValue["ConnectedVia"] = "Applet"; 142711ac7a9SEd Tanous } 143711ac7a9SEd Tanous } 144711ac7a9SEd Tanous } 145711ac7a9SEd Tanous } 146107077deSPrzemyslaw Czarnowski } 147107077deSPrzemyslaw Czarnowski } 148107077deSPrzemyslaw Czarnowski 149107077deSPrzemyslaw Czarnowski /** 150107077deSPrzemyslaw Czarnowski * @brief Fill template for Virtual Media Item. 151107077deSPrzemyslaw Czarnowski */ 15222db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name, 153107077deSPrzemyslaw Czarnowski const std::string& resName) 154107077deSPrzemyslaw Czarnowski { 155107077deSPrzemyslaw Czarnowski nlohmann::json item; 156fdb20347SEd Tanous item["@odata.id"] = crow::utility::urlFromPieces( 157fdb20347SEd Tanous "redfish", "v1", "Managers", name, "VirtualMedia", resName); 15822db1728SEd Tanous 159d04ba325SPrzemyslaw Czarnowski item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia"; 160107077deSPrzemyslaw Czarnowski item["Name"] = "Virtual Removable Media"; 161107077deSPrzemyslaw Czarnowski item["Id"] = resName; 162107077deSPrzemyslaw Czarnowski item["WriteProtected"] = true; 163613dabeaSEd Tanous item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"}); 164107077deSPrzemyslaw Czarnowski item["TransferMethod"] = "Stream"; 165d04ba325SPrzemyslaw Czarnowski item["Oem"]["OpenBMC"]["@odata.type"] = 166d04ba325SPrzemyslaw Czarnowski "#OemVirtualMedia.v1_0_0.VirtualMedia"; 167107077deSPrzemyslaw Czarnowski 168107077deSPrzemyslaw Czarnowski return item; 169107077deSPrzemyslaw Czarnowski } 170107077deSPrzemyslaw Czarnowski 171107077deSPrzemyslaw Czarnowski /** 172107077deSPrzemyslaw Czarnowski * @brief Fills collection data 173107077deSPrzemyslaw Czarnowski */ 17422db1728SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> aResp, 175107077deSPrzemyslaw Czarnowski const std::string& service, 176107077deSPrzemyslaw Czarnowski const std::string& name) 177107077deSPrzemyslaw Czarnowski { 178107077deSPrzemyslaw Czarnowski BMCWEB_LOG_DEBUG << "Get available Virtual Media resources."; 179107077deSPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 18002cad96eSEd Tanous [name, aResp{std::move(aResp)}]( 1815e7e2dc5SEd Tanous const boost::system::error_code& ec, 18202cad96eSEd Tanous const dbus::utility::ManagedObjectType& subtree) { 183107077deSPrzemyslaw Czarnowski if (ec) 184107077deSPrzemyslaw Czarnowski { 185107077deSPrzemyslaw Czarnowski BMCWEB_LOG_DEBUG << "DBUS response error"; 186107077deSPrzemyslaw Czarnowski return; 187107077deSPrzemyslaw Czarnowski } 188107077deSPrzemyslaw Czarnowski nlohmann::json& members = aResp->res.jsonValue["Members"]; 189107077deSPrzemyslaw Czarnowski members = nlohmann::json::array(); 190107077deSPrzemyslaw Czarnowski 191107077deSPrzemyslaw Czarnowski for (const auto& object : subtree) 192107077deSPrzemyslaw Czarnowski { 193107077deSPrzemyslaw Czarnowski nlohmann::json item; 1942dfd18efSEd Tanous std::string path = object.first.filename(); 1952dfd18efSEd Tanous if (path.empty()) 196107077deSPrzemyslaw Czarnowski { 197107077deSPrzemyslaw Czarnowski continue; 198107077deSPrzemyslaw Czarnowski } 199107077deSPrzemyslaw Czarnowski 200fdb20347SEd Tanous item["@odata.id"] = crow::utility::urlFromPieces( 201fdb20347SEd Tanous "redfish", "v1", "Managers", name, "VirtualMedia", path); 202107077deSPrzemyslaw Czarnowski members.emplace_back(std::move(item)); 203107077deSPrzemyslaw Czarnowski } 204107077deSPrzemyslaw Czarnowski aResp->res.jsonValue["Members@odata.count"] = members.size(); 205107077deSPrzemyslaw Czarnowski }, 206107077deSPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia", 207107077deSPrzemyslaw Czarnowski "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 208107077deSPrzemyslaw Czarnowski } 209107077deSPrzemyslaw Czarnowski 210107077deSPrzemyslaw Czarnowski /** 211107077deSPrzemyslaw Czarnowski * @brief Fills data for specific resource 212107077deSPrzemyslaw Czarnowski */ 21322db1728SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 214107077deSPrzemyslaw Czarnowski const std::string& service, const std::string& name, 215107077deSPrzemyslaw Czarnowski const std::string& resName) 216107077deSPrzemyslaw Czarnowski { 217107077deSPrzemyslaw Czarnowski BMCWEB_LOG_DEBUG << "Get Virtual Media resource data."; 218107077deSPrzemyslaw Czarnowski 219107077deSPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 220914e2d5dSEd Tanous [resName, name, 2215e7e2dc5SEd Tanous aResp](const boost::system::error_code& ec, 222914e2d5dSEd Tanous const dbus::utility::ManagedObjectType& subtree) { 223107077deSPrzemyslaw Czarnowski if (ec) 224107077deSPrzemyslaw Czarnowski { 225107077deSPrzemyslaw Czarnowski BMCWEB_LOG_DEBUG << "DBUS response error"; 226e13c2760SPrzemyslaw Czarnowski 227107077deSPrzemyslaw Czarnowski return; 228107077deSPrzemyslaw Czarnowski } 229107077deSPrzemyslaw Czarnowski 230914e2d5dSEd Tanous for (const auto& item : subtree) 231107077deSPrzemyslaw Czarnowski { 2322dfd18efSEd Tanous std::string thispath = item.first.filename(); 2332dfd18efSEd Tanous if (thispath.empty()) 234107077deSPrzemyslaw Czarnowski { 235107077deSPrzemyslaw Czarnowski continue; 236107077deSPrzemyslaw Czarnowski } 237107077deSPrzemyslaw Czarnowski 2382dfd18efSEd Tanous if (thispath != resName) 239107077deSPrzemyslaw Czarnowski { 240107077deSPrzemyslaw Czarnowski continue; 241107077deSPrzemyslaw Czarnowski } 242107077deSPrzemyslaw Czarnowski 2431a6258dcSPrzemyslaw Czarnowski // "Legacy"/"Proxy" 2441a6258dcSPrzemyslaw Czarnowski auto mode = item.first.parent_path(); 2451a6258dcSPrzemyslaw Czarnowski // "VirtualMedia" 2461a6258dcSPrzemyslaw Czarnowski auto type = mode.parent_path(); 2471a6258dcSPrzemyslaw Czarnowski if (mode.filename().empty() || type.filename().empty()) 2481a6258dcSPrzemyslaw Czarnowski { 2491a6258dcSPrzemyslaw Czarnowski continue; 2501a6258dcSPrzemyslaw Czarnowski } 2511a6258dcSPrzemyslaw Czarnowski 2521a6258dcSPrzemyslaw Czarnowski if (type.filename() != "VirtualMedia") 2531a6258dcSPrzemyslaw Czarnowski { 2541a6258dcSPrzemyslaw Czarnowski continue; 2551a6258dcSPrzemyslaw Czarnowski } 2561a6258dcSPrzemyslaw Czarnowski 257107077deSPrzemyslaw Czarnowski aResp->res.jsonValue = vmItemTemplate(name, resName); 258107077deSPrzemyslaw Czarnowski 259e13c2760SPrzemyslaw Czarnowski // Check if dbus path is Legacy type 2601a6258dcSPrzemyslaw Czarnowski if (mode.filename() == "Legacy") 261e13c2760SPrzemyslaw Czarnowski { 262e13c2760SPrzemyslaw Czarnowski aResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"] 263fdb20347SEd Tanous ["target"] = crow::utility::urlFromPieces( 264fdb20347SEd Tanous "redfish", "v1", "Managers", name, "VirtualMedia", resName, 265fdb20347SEd Tanous "Actions", "VirtualMedia.InsertMedia"); 266e13c2760SPrzemyslaw Czarnowski } 267e13c2760SPrzemyslaw Czarnowski 268107077deSPrzemyslaw Czarnowski vmParseInterfaceObject(item.second, aResp); 269107077deSPrzemyslaw Czarnowski 270002d39b4SEd Tanous aResp->res 271002d39b4SEd Tanous .jsonValue["Actions"]["#VirtualMedia.EjectMedia"]["target"] = 272fdb20347SEd Tanous crow::utility::urlFromPieces("redfish", "v1", "Managers", name, 273fdb20347SEd Tanous "VirtualMedia", resName, "Actions", 274fdb20347SEd Tanous "VirtualMedia.EjectMedia"); 275107077deSPrzemyslaw Czarnowski return; 276107077deSPrzemyslaw Czarnowski } 277107077deSPrzemyslaw Czarnowski 278d8a5d5d8SJiaqing Zhao messages::resourceNotFound(aResp->res, "VirtualMedia", resName); 279107077deSPrzemyslaw Czarnowski }, 280107077deSPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia", 281107077deSPrzemyslaw Czarnowski "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 282107077deSPrzemyslaw Czarnowski } 283107077deSPrzemyslaw Czarnowski 284e13c2760SPrzemyslaw Czarnowski /** 285c6f4e017SAgata Olender * @brief Transfer protocols supported for InsertMedia action. 286c6f4e017SAgata Olender * 287c6f4e017SAgata Olender */ 288c6f4e017SAgata Olender enum class TransferProtocol 289c6f4e017SAgata Olender { 290c6f4e017SAgata Olender https, 291c6f4e017SAgata Olender smb, 292c6f4e017SAgata Olender invalid 293c6f4e017SAgata Olender }; 294c6f4e017SAgata Olender 295c6f4e017SAgata Olender /** 296c6f4e017SAgata Olender * @brief Function extracts transfer protocol type from URI. 297c6f4e017SAgata Olender * 298c6f4e017SAgata Olender */ 29967df073bSEd Tanous inline std::optional<TransferProtocol> 300ace85d60SEd Tanous getTransferProtocolFromUri(const boost::urls::url_view& imageUri) 30167df073bSEd Tanous { 302079360aeSEd Tanous std::string_view scheme = imageUri.scheme(); 30367df073bSEd Tanous if (scheme == "smb") 30467df073bSEd Tanous { 30567df073bSEd Tanous return TransferProtocol::smb; 30667df073bSEd Tanous } 30767df073bSEd Tanous if (scheme == "https") 30867df073bSEd Tanous { 30967df073bSEd Tanous return TransferProtocol::https; 31067df073bSEd Tanous } 31167df073bSEd Tanous if (!scheme.empty()) 31267df073bSEd Tanous { 31367df073bSEd Tanous return TransferProtocol::invalid; 31467df073bSEd Tanous } 31567df073bSEd Tanous 31667df073bSEd Tanous return {}; 31767df073bSEd Tanous } 318c6f4e017SAgata Olender 319c6f4e017SAgata Olender /** 320c6f4e017SAgata Olender * @brief Function convert transfer protocol from string param. 321c6f4e017SAgata Olender * 322c6f4e017SAgata Olender */ 32322db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam( 324c6f4e017SAgata Olender const std::optional<std::string>& transferProtocolType) 325c6f4e017SAgata Olender { 326c6f4e017SAgata Olender if (transferProtocolType == std::nullopt) 327c6f4e017SAgata Olender { 328c6f4e017SAgata Olender return {}; 329c6f4e017SAgata Olender } 330c6f4e017SAgata Olender 331c6f4e017SAgata Olender if (*transferProtocolType == "CIFS") 332c6f4e017SAgata Olender { 333c6f4e017SAgata Olender return TransferProtocol::smb; 334c6f4e017SAgata Olender } 335c6f4e017SAgata Olender 336c6f4e017SAgata Olender if (*transferProtocolType == "HTTPS") 337c6f4e017SAgata Olender { 338c6f4e017SAgata Olender return TransferProtocol::https; 339c6f4e017SAgata Olender } 340c6f4e017SAgata Olender 341c6f4e017SAgata Olender return TransferProtocol::invalid; 342c6f4e017SAgata Olender } 343c6f4e017SAgata Olender 344c6f4e017SAgata Olender /** 345c6f4e017SAgata Olender * @brief Function extends URI with transfer protocol type. 346c6f4e017SAgata Olender * 347c6f4e017SAgata Olender */ 34822db1728SEd Tanous inline std::string 349c6f4e017SAgata Olender getUriWithTransferProtocol(const std::string& imageUri, 350c6f4e017SAgata Olender const TransferProtocol& transferProtocol) 351c6f4e017SAgata Olender { 352c6f4e017SAgata Olender if (transferProtocol == TransferProtocol::smb) 353c6f4e017SAgata Olender { 354c6f4e017SAgata Olender return "smb://" + imageUri; 355c6f4e017SAgata Olender } 356c6f4e017SAgata Olender 357c6f4e017SAgata Olender if (transferProtocol == TransferProtocol::https) 358c6f4e017SAgata Olender { 359c6f4e017SAgata Olender return "https://" + imageUri; 360c6f4e017SAgata Olender } 361c6f4e017SAgata Olender 362c6f4e017SAgata Olender return imageUri; 363c6f4e017SAgata Olender } 364c6f4e017SAgata Olender 3651f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams 3661f2a40ceSPrzemyslaw Czarnowski { 367*120fa86aSPrzemyslaw Czarnowski std::optional<std::string> imageUrl; 3681f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> userName; 3691f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> password; 3701f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> transferMethod; 3711f2a40ceSPrzemyslaw Czarnowski std::optional<std::string> transferProtocolType; 3721f2a40ceSPrzemyslaw Czarnowski std::optional<bool> writeProtected = true; 3731f2a40ceSPrzemyslaw Czarnowski std::optional<bool> inserted; 3741f2a40ceSPrzemyslaw Czarnowski }; 3751f2a40ceSPrzemyslaw Czarnowski 3761214b7e7SGunnar Mills template <typename T> 3771214b7e7SGunnar Mills static void secureCleanup(T& value) 378988fb7b2SAdrian Ambrożewicz { 3794ecc618fSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) 380988fb7b2SAdrian Ambrożewicz auto raw = const_cast<typename T::value_type*>(value.data()); 381988fb7b2SAdrian Ambrożewicz explicit_bzero(raw, value.size() * sizeof(*raw)); 382988fb7b2SAdrian Ambrożewicz } 383988fb7b2SAdrian Ambrożewicz 384988fb7b2SAdrian Ambrożewicz class Credentials 385988fb7b2SAdrian Ambrożewicz { 386988fb7b2SAdrian Ambrożewicz public: 387988fb7b2SAdrian Ambrożewicz Credentials(std::string&& user, std::string&& password) : 388988fb7b2SAdrian Ambrożewicz userBuf(std::move(user)), passBuf(std::move(password)) 3891214b7e7SGunnar Mills {} 390988fb7b2SAdrian Ambrożewicz 391988fb7b2SAdrian Ambrożewicz ~Credentials() 392988fb7b2SAdrian Ambrożewicz { 393988fb7b2SAdrian Ambrożewicz secureCleanup(userBuf); 394988fb7b2SAdrian Ambrożewicz secureCleanup(passBuf); 395988fb7b2SAdrian Ambrożewicz } 396988fb7b2SAdrian Ambrożewicz 397988fb7b2SAdrian Ambrożewicz const std::string& user() 398988fb7b2SAdrian Ambrożewicz { 399988fb7b2SAdrian Ambrożewicz return userBuf; 400988fb7b2SAdrian Ambrożewicz } 401988fb7b2SAdrian Ambrożewicz 402988fb7b2SAdrian Ambrożewicz const std::string& password() 403988fb7b2SAdrian Ambrożewicz { 404988fb7b2SAdrian Ambrożewicz return passBuf; 405988fb7b2SAdrian Ambrożewicz } 406988fb7b2SAdrian Ambrożewicz 407988fb7b2SAdrian Ambrożewicz Credentials() = delete; 408988fb7b2SAdrian Ambrożewicz Credentials(const Credentials&) = delete; 409988fb7b2SAdrian Ambrożewicz Credentials& operator=(const Credentials&) = delete; 410ecd6a3a2SEd Tanous Credentials(Credentials&&) = delete; 411ecd6a3a2SEd Tanous Credentials& operator=(Credentials&&) = delete; 412988fb7b2SAdrian Ambrożewicz 41322db1728SEd Tanous private: 414988fb7b2SAdrian Ambrożewicz std::string userBuf; 415988fb7b2SAdrian Ambrożewicz std::string passBuf; 416988fb7b2SAdrian Ambrożewicz }; 417988fb7b2SAdrian Ambrożewicz 418988fb7b2SAdrian Ambrożewicz class CredentialsProvider 419988fb7b2SAdrian Ambrożewicz { 420988fb7b2SAdrian Ambrożewicz public: 4211214b7e7SGunnar Mills template <typename T> 4221214b7e7SGunnar Mills struct Deleter 423988fb7b2SAdrian Ambrożewicz { 424988fb7b2SAdrian Ambrożewicz void operator()(T* buff) const 425988fb7b2SAdrian Ambrożewicz { 426988fb7b2SAdrian Ambrożewicz if (buff) 427988fb7b2SAdrian Ambrożewicz { 428988fb7b2SAdrian Ambrożewicz secureCleanup(*buff); 429988fb7b2SAdrian Ambrożewicz delete buff; 430988fb7b2SAdrian Ambrożewicz } 431988fb7b2SAdrian Ambrożewicz } 432988fb7b2SAdrian Ambrożewicz }; 433988fb7b2SAdrian Ambrożewicz 434988fb7b2SAdrian Ambrożewicz using Buffer = std::vector<char>; 435988fb7b2SAdrian Ambrożewicz using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>; 436988fb7b2SAdrian Ambrożewicz // Using explicit definition instead of std::function to avoid implicit 437988fb7b2SAdrian Ambrożewicz // conversions eg. stack copy instead of reference 438988fb7b2SAdrian Ambrożewicz using FormatterFunc = void(const std::string& username, 439988fb7b2SAdrian Ambrożewicz const std::string& password, Buffer& dest); 440988fb7b2SAdrian Ambrożewicz 441988fb7b2SAdrian Ambrożewicz CredentialsProvider(std::string&& user, std::string&& password) : 442988fb7b2SAdrian Ambrożewicz credentials(std::move(user), std::move(password)) 4431214b7e7SGunnar Mills {} 444988fb7b2SAdrian Ambrożewicz 445988fb7b2SAdrian Ambrożewicz const std::string& user() 446988fb7b2SAdrian Ambrożewicz { 447988fb7b2SAdrian Ambrożewicz return credentials.user(); 448988fb7b2SAdrian Ambrożewicz } 449988fb7b2SAdrian Ambrożewicz 450988fb7b2SAdrian Ambrożewicz const std::string& password() 451988fb7b2SAdrian Ambrożewicz { 452988fb7b2SAdrian Ambrożewicz return credentials.password(); 453988fb7b2SAdrian Ambrożewicz } 454988fb7b2SAdrian Ambrożewicz 4551917ee95SEd Tanous SecureBuffer pack(FormatterFunc* formatter) 456988fb7b2SAdrian Ambrożewicz { 457988fb7b2SAdrian Ambrożewicz SecureBuffer packed{new Buffer{}}; 458e662eae8SEd Tanous if (formatter != nullptr) 459988fb7b2SAdrian Ambrożewicz { 460988fb7b2SAdrian Ambrożewicz formatter(credentials.user(), credentials.password(), *packed); 461988fb7b2SAdrian Ambrożewicz } 462988fb7b2SAdrian Ambrożewicz 463988fb7b2SAdrian Ambrożewicz return packed; 464988fb7b2SAdrian Ambrożewicz } 465988fb7b2SAdrian Ambrożewicz 466988fb7b2SAdrian Ambrożewicz private: 467988fb7b2SAdrian Ambrożewicz Credentials credentials; 468988fb7b2SAdrian Ambrożewicz }; 469988fb7b2SAdrian Ambrożewicz 470988fb7b2SAdrian Ambrożewicz // Wrapper for boost::async_pipe ensuring proper pipe cleanup 4711214b7e7SGunnar Mills template <typename Buffer> 4721214b7e7SGunnar Mills class Pipe 473988fb7b2SAdrian Ambrożewicz { 474988fb7b2SAdrian Ambrożewicz public: 475988fb7b2SAdrian Ambrożewicz using unix_fd = sdbusplus::message::unix_fd; 476988fb7b2SAdrian Ambrożewicz 4778a592810SEd Tanous Pipe(boost::asio::io_context& io, Buffer&& bufferIn) : 4788a592810SEd Tanous impl(io), buffer{std::move(bufferIn)} 4791214b7e7SGunnar Mills {} 480988fb7b2SAdrian Ambrożewicz 481988fb7b2SAdrian Ambrożewicz ~Pipe() 482988fb7b2SAdrian Ambrożewicz { 483988fb7b2SAdrian Ambrożewicz // Named pipe needs to be explicitly removed 484988fb7b2SAdrian Ambrożewicz impl.close(); 485988fb7b2SAdrian Ambrożewicz } 486988fb7b2SAdrian Ambrożewicz 487ecd6a3a2SEd Tanous Pipe(const Pipe&) = delete; 488ecd6a3a2SEd Tanous Pipe(Pipe&&) = delete; 489ecd6a3a2SEd Tanous Pipe& operator=(const Pipe&) = delete; 490ecd6a3a2SEd Tanous Pipe& operator=(Pipe&&) = delete; 491ecd6a3a2SEd Tanous 492988fb7b2SAdrian Ambrożewicz unix_fd fd() 493988fb7b2SAdrian Ambrożewicz { 494988fb7b2SAdrian Ambrożewicz return unix_fd{impl.native_source()}; 495988fb7b2SAdrian Ambrożewicz } 496988fb7b2SAdrian Ambrożewicz 497988fb7b2SAdrian Ambrożewicz template <typename WriteHandler> 49881ce609eSEd Tanous void asyncWrite(WriteHandler&& handler) 499988fb7b2SAdrian Ambrożewicz { 500988fb7b2SAdrian Ambrożewicz impl.async_write_some(data(), std::forward<WriteHandler>(handler)); 501988fb7b2SAdrian Ambrożewicz } 502988fb7b2SAdrian Ambrożewicz 503988fb7b2SAdrian Ambrożewicz private: 504988fb7b2SAdrian Ambrożewicz // Specialization for pointer types 505988fb7b2SAdrian Ambrożewicz template <typename B = Buffer> 506988fb7b2SAdrian Ambrożewicz typename std::enable_if<boost::has_dereference<B>::value, 507988fb7b2SAdrian Ambrożewicz boost::asio::const_buffer>::type 508988fb7b2SAdrian Ambrożewicz data() 509988fb7b2SAdrian Ambrożewicz { 510988fb7b2SAdrian Ambrożewicz return boost::asio::buffer(*buffer); 511988fb7b2SAdrian Ambrożewicz } 512988fb7b2SAdrian Ambrożewicz 513988fb7b2SAdrian Ambrożewicz template <typename B = Buffer> 514988fb7b2SAdrian Ambrożewicz typename std::enable_if<!boost::has_dereference<B>::value, 515988fb7b2SAdrian Ambrożewicz boost::asio::const_buffer>::type 516988fb7b2SAdrian Ambrożewicz data() 517988fb7b2SAdrian Ambrożewicz { 518988fb7b2SAdrian Ambrożewicz return boost::asio::buffer(buffer); 519988fb7b2SAdrian Ambrożewicz } 520988fb7b2SAdrian Ambrożewicz 521988fb7b2SAdrian Ambrożewicz const std::string name; 522988fb7b2SAdrian Ambrożewicz boost::process::async_pipe impl; 523988fb7b2SAdrian Ambrożewicz Buffer buffer; 524988fb7b2SAdrian Ambrożewicz }; 525988fb7b2SAdrian Ambrożewicz 526e13c2760SPrzemyslaw Czarnowski /** 527e13c2760SPrzemyslaw Czarnowski * @brief Function transceives data with dbus directly. 528e13c2760SPrzemyslaw Czarnowski * 529e13c2760SPrzemyslaw Czarnowski * All BMC state properties will be retrieved before sending reset request. 530e13c2760SPrzemyslaw Czarnowski */ 53122db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 532e13c2760SPrzemyslaw Czarnowski const std::string& service, const std::string& name, 533988fb7b2SAdrian Ambrożewicz const std::string& imageUrl, const bool rw, 534988fb7b2SAdrian Ambrożewicz std::string&& userName, std::string&& password) 535e13c2760SPrzemyslaw Czarnowski { 536988fb7b2SAdrian Ambrożewicz using SecurePipe = Pipe<CredentialsProvider::SecureBuffer>; 537988fb7b2SAdrian Ambrożewicz constexpr const size_t secretLimit = 1024; 538988fb7b2SAdrian Ambrożewicz 539988fb7b2SAdrian Ambrożewicz std::shared_ptr<SecurePipe> secretPipe; 540168e20c1SEd Tanous dbus::utility::DbusVariantType unixFd = -1; 541988fb7b2SAdrian Ambrożewicz 542988fb7b2SAdrian Ambrożewicz if (!userName.empty() || !password.empty()) 543988fb7b2SAdrian Ambrożewicz { 544988fb7b2SAdrian Ambrożewicz // Encapsulate in safe buffer 545988fb7b2SAdrian Ambrożewicz CredentialsProvider credentials(std::move(userName), 546988fb7b2SAdrian Ambrożewicz std::move(password)); 547988fb7b2SAdrian Ambrożewicz 548988fb7b2SAdrian Ambrożewicz // Payload must contain data + NULL delimiters 549988fb7b2SAdrian Ambrożewicz if (credentials.user().size() + credentials.password().size() + 2 > 550988fb7b2SAdrian Ambrożewicz secretLimit) 551988fb7b2SAdrian Ambrożewicz { 552988fb7b2SAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Credentials too long to handle"; 553988fb7b2SAdrian Ambrożewicz messages::unrecognizedRequestBody(asyncResp->res); 554988fb7b2SAdrian Ambrożewicz return; 555988fb7b2SAdrian Ambrożewicz } 556988fb7b2SAdrian Ambrożewicz 557988fb7b2SAdrian Ambrożewicz // Pack secret 55822db1728SEd Tanous auto secret = credentials.pack( 55922db1728SEd Tanous [](const auto& user, const auto& pass, auto& buff) { 560988fb7b2SAdrian Ambrożewicz std::copy(user.begin(), user.end(), std::back_inserter(buff)); 561988fb7b2SAdrian Ambrożewicz buff.push_back('\0'); 562988fb7b2SAdrian Ambrożewicz std::copy(pass.begin(), pass.end(), std::back_inserter(buff)); 563988fb7b2SAdrian Ambrożewicz buff.push_back('\0'); 564988fb7b2SAdrian Ambrożewicz }); 565988fb7b2SAdrian Ambrożewicz 566988fb7b2SAdrian Ambrożewicz // Open pipe 567988fb7b2SAdrian Ambrożewicz secretPipe = std::make_shared<SecurePipe>( 56822db1728SEd Tanous crow::connections::systemBus->get_io_context(), std::move(secret)); 569988fb7b2SAdrian Ambrożewicz unixFd = secretPipe->fd(); 570988fb7b2SAdrian Ambrożewicz 571988fb7b2SAdrian Ambrożewicz // Pass secret over pipe 57281ce609eSEd Tanous secretPipe->asyncWrite( 573f5b16f03SVikram Bodireddy [asyncResp](const boost::system::error_code& ec, std::size_t) { 574988fb7b2SAdrian Ambrożewicz if (ec) 575988fb7b2SAdrian Ambrożewicz { 576988fb7b2SAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Failed to pass secret: " << ec; 577988fb7b2SAdrian Ambrożewicz messages::internalError(asyncResp->res); 578988fb7b2SAdrian Ambrożewicz } 579988fb7b2SAdrian Ambrożewicz }); 580988fb7b2SAdrian Ambrożewicz } 581988fb7b2SAdrian Ambrożewicz 582e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 5835e7e2dc5SEd Tanous [asyncResp, secretPipe](const boost::system::error_code& ec, 584988fb7b2SAdrian Ambrożewicz bool success) { 585e13c2760SPrzemyslaw Czarnowski if (ec) 586e13c2760SPrzemyslaw Czarnowski { 587e13c2760SPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec; 588e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 589d6da5bebSAdrian Ambrożewicz } 590d6da5bebSAdrian Ambrożewicz else if (!success) 591d6da5bebSAdrian Ambrożewicz { 592d6da5bebSAdrian Ambrożewicz BMCWEB_LOG_ERROR << "Service responded with error"; 593d6da5bebSAdrian Ambrożewicz messages::generalError(asyncResp->res); 594e13c2760SPrzemyslaw Czarnowski } 595e13c2760SPrzemyslaw Czarnowski }, 596e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, 597988fb7b2SAdrian Ambrożewicz "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw, 598988fb7b2SAdrian Ambrożewicz unixFd); 599e13c2760SPrzemyslaw Czarnowski } 600e13c2760SPrzemyslaw Czarnowski 601e13c2760SPrzemyslaw Czarnowski /** 602*120fa86aSPrzemyslaw Czarnowski * @brief Function validate parameters of insert media request. 603*120fa86aSPrzemyslaw Czarnowski * 604*120fa86aSPrzemyslaw Czarnowski */ 605*120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 606*120fa86aSPrzemyslaw Czarnowski const std::string& service, 607*120fa86aSPrzemyslaw Czarnowski const std::string& resName, 608*120fa86aSPrzemyslaw Czarnowski InsertMediaActionParams& actionParams) 609*120fa86aSPrzemyslaw Czarnowski { 610*120fa86aSPrzemyslaw Czarnowski BMCWEB_LOG_DEBUG << "Validation started"; 611*120fa86aSPrzemyslaw Czarnowski // required param imageUrl must not be empty 612*120fa86aSPrzemyslaw Czarnowski if (!actionParams.imageUrl) 613*120fa86aSPrzemyslaw Czarnowski { 614*120fa86aSPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Request action parameter Image is empty."; 615*120fa86aSPrzemyslaw Czarnowski 616*120fa86aSPrzemyslaw Czarnowski messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image"); 617*120fa86aSPrzemyslaw Czarnowski 618*120fa86aSPrzemyslaw Czarnowski return; 619*120fa86aSPrzemyslaw Czarnowski } 620*120fa86aSPrzemyslaw Czarnowski 621*120fa86aSPrzemyslaw Czarnowski // optional param inserted must be true 622*120fa86aSPrzemyslaw Czarnowski if ((actionParams.inserted != std::nullopt) && !*actionParams.inserted) 623*120fa86aSPrzemyslaw Czarnowski { 624*120fa86aSPrzemyslaw Czarnowski BMCWEB_LOG_ERROR 625*120fa86aSPrzemyslaw Czarnowski << "Request action optional parameter Inserted must be true."; 626*120fa86aSPrzemyslaw Czarnowski 627*120fa86aSPrzemyslaw Czarnowski messages::actionParameterNotSupported(asyncResp->res, "Inserted", 628*120fa86aSPrzemyslaw Czarnowski "InsertMedia"); 629*120fa86aSPrzemyslaw Czarnowski 630*120fa86aSPrzemyslaw Czarnowski return; 631*120fa86aSPrzemyslaw Czarnowski } 632*120fa86aSPrzemyslaw Czarnowski 633*120fa86aSPrzemyslaw Czarnowski // optional param transferMethod must be stream 634*120fa86aSPrzemyslaw Czarnowski if ((actionParams.transferMethod != std::nullopt) && 635*120fa86aSPrzemyslaw Czarnowski (*actionParams.transferMethod != "Stream")) 636*120fa86aSPrzemyslaw Czarnowski { 637*120fa86aSPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Request action optional parameter " 638*120fa86aSPrzemyslaw Czarnowski "TransferMethod must be Stream."; 639*120fa86aSPrzemyslaw Czarnowski 640*120fa86aSPrzemyslaw Czarnowski messages::actionParameterNotSupported(asyncResp->res, "TransferMethod", 641*120fa86aSPrzemyslaw Czarnowski "InsertMedia"); 642*120fa86aSPrzemyslaw Czarnowski 643*120fa86aSPrzemyslaw Czarnowski return; 644*120fa86aSPrzemyslaw Czarnowski } 645*120fa86aSPrzemyslaw Czarnowski boost::urls::result<boost::urls::url_view> url = 646*120fa86aSPrzemyslaw Czarnowski boost::urls::parse_uri(*actionParams.imageUrl); 647*120fa86aSPrzemyslaw Czarnowski if (!url) 648*120fa86aSPrzemyslaw Czarnowski { 649*120fa86aSPrzemyslaw Czarnowski messages::actionParameterValueFormatError( 650*120fa86aSPrzemyslaw Czarnowski asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia"); 651*120fa86aSPrzemyslaw Czarnowski return; 652*120fa86aSPrzemyslaw Czarnowski } 653*120fa86aSPrzemyslaw Czarnowski std::optional<TransferProtocol> uriTransferProtocolType = 654*120fa86aSPrzemyslaw Czarnowski getTransferProtocolFromUri(*url); 655*120fa86aSPrzemyslaw Czarnowski 656*120fa86aSPrzemyslaw Czarnowski std::optional<TransferProtocol> paramTransferProtocolType = 657*120fa86aSPrzemyslaw Czarnowski getTransferProtocolFromParam(actionParams.transferProtocolType); 658*120fa86aSPrzemyslaw Czarnowski 659*120fa86aSPrzemyslaw Czarnowski // ImageUrl does not contain valid protocol type 660*120fa86aSPrzemyslaw Czarnowski if (*uriTransferProtocolType == TransferProtocol::invalid) 661*120fa86aSPrzemyslaw Czarnowski { 662*120fa86aSPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must " 663*120fa86aSPrzemyslaw Czarnowski "contain specified protocol type from list: " 664*120fa86aSPrzemyslaw Czarnowski "(smb, https)."; 665*120fa86aSPrzemyslaw Czarnowski 666*120fa86aSPrzemyslaw Czarnowski messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); 667*120fa86aSPrzemyslaw Czarnowski 668*120fa86aSPrzemyslaw Czarnowski return; 669*120fa86aSPrzemyslaw Czarnowski } 670*120fa86aSPrzemyslaw Czarnowski 671*120fa86aSPrzemyslaw Czarnowski // transferProtocolType should contain value from list 672*120fa86aSPrzemyslaw Czarnowski if (*paramTransferProtocolType == TransferProtocol::invalid) 673*120fa86aSPrzemyslaw Czarnowski { 674*120fa86aSPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Request action parameter TransferProtocolType " 675*120fa86aSPrzemyslaw Czarnowski "must be provided with value from list: " 676*120fa86aSPrzemyslaw Czarnowski "(CIFS, HTTPS)."; 677*120fa86aSPrzemyslaw Czarnowski 678*120fa86aSPrzemyslaw Czarnowski messages::propertyValueNotInList(asyncResp->res, 679*120fa86aSPrzemyslaw Czarnowski *actionParams.transferProtocolType, 680*120fa86aSPrzemyslaw Czarnowski "TransferProtocolType"); 681*120fa86aSPrzemyslaw Czarnowski return; 682*120fa86aSPrzemyslaw Czarnowski } 683*120fa86aSPrzemyslaw Czarnowski 684*120fa86aSPrzemyslaw Czarnowski // valid transfer protocol not provided either with URI nor param 685*120fa86aSPrzemyslaw Czarnowski if ((uriTransferProtocolType == std::nullopt) && 686*120fa86aSPrzemyslaw Czarnowski (paramTransferProtocolType == std::nullopt)) 687*120fa86aSPrzemyslaw Czarnowski { 688*120fa86aSPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must " 689*120fa86aSPrzemyslaw Czarnowski "contain specified protocol type or param " 690*120fa86aSPrzemyslaw Czarnowski "TransferProtocolType must be provided."; 691*120fa86aSPrzemyslaw Czarnowski 692*120fa86aSPrzemyslaw Czarnowski messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); 693*120fa86aSPrzemyslaw Czarnowski 694*120fa86aSPrzemyslaw Czarnowski return; 695*120fa86aSPrzemyslaw Czarnowski } 696*120fa86aSPrzemyslaw Czarnowski 697*120fa86aSPrzemyslaw Czarnowski // valid transfer protocol provided both with URI and param 698*120fa86aSPrzemyslaw Czarnowski if ((paramTransferProtocolType != std::nullopt) && 699*120fa86aSPrzemyslaw Czarnowski (uriTransferProtocolType != std::nullopt)) 700*120fa86aSPrzemyslaw Czarnowski { 701*120fa86aSPrzemyslaw Czarnowski // check if protocol is the same for URI and param 702*120fa86aSPrzemyslaw Czarnowski if (*paramTransferProtocolType != *uriTransferProtocolType) 703*120fa86aSPrzemyslaw Czarnowski { 704*120fa86aSPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Request action parameter " 705*120fa86aSPrzemyslaw Czarnowski "TransferProtocolType must contain the " 706*120fa86aSPrzemyslaw Czarnowski "same protocol type as protocol type " 707*120fa86aSPrzemyslaw Czarnowski "provided with param imageUrl."; 708*120fa86aSPrzemyslaw Czarnowski 709*120fa86aSPrzemyslaw Czarnowski messages::actionParameterValueTypeError( 710*120fa86aSPrzemyslaw Czarnowski asyncResp->res, *actionParams.transferProtocolType, 711*120fa86aSPrzemyslaw Czarnowski "TransferProtocolType", "InsertMedia"); 712*120fa86aSPrzemyslaw Czarnowski 713*120fa86aSPrzemyslaw Czarnowski return; 714*120fa86aSPrzemyslaw Czarnowski } 715*120fa86aSPrzemyslaw Czarnowski } 716*120fa86aSPrzemyslaw Czarnowski 717*120fa86aSPrzemyslaw Czarnowski // validation passed, add protocol to URI if needed 718*120fa86aSPrzemyslaw Czarnowski if (uriTransferProtocolType == std::nullopt) 719*120fa86aSPrzemyslaw Czarnowski { 720*120fa86aSPrzemyslaw Czarnowski actionParams.imageUrl = getUriWithTransferProtocol( 721*120fa86aSPrzemyslaw Czarnowski *actionParams.imageUrl, *paramTransferProtocolType); 722*120fa86aSPrzemyslaw Czarnowski } 723*120fa86aSPrzemyslaw Czarnowski 724*120fa86aSPrzemyslaw Czarnowski doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl, 725*120fa86aSPrzemyslaw Czarnowski !(*actionParams.writeProtected), 726*120fa86aSPrzemyslaw Czarnowski std::move(*actionParams.userName), 727*120fa86aSPrzemyslaw Czarnowski std::move(*actionParams.password)); 728*120fa86aSPrzemyslaw Czarnowski } 729*120fa86aSPrzemyslaw Czarnowski 730*120fa86aSPrzemyslaw Czarnowski /** 731e13c2760SPrzemyslaw Czarnowski * @brief Function transceives data with dbus directly. 732e13c2760SPrzemyslaw Czarnowski * 733e13c2760SPrzemyslaw Czarnowski * All BMC state properties will be retrieved before sending reset request. 734e13c2760SPrzemyslaw Czarnowski */ 73522db1728SEd Tanous inline void doVmAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 736e13c2760SPrzemyslaw Czarnowski const std::string& service, const std::string& name, 737e13c2760SPrzemyslaw Czarnowski bool legacy) 738e13c2760SPrzemyslaw Czarnowski { 739e13c2760SPrzemyslaw Czarnowski 740e13c2760SPrzemyslaw Czarnowski // Legacy mount requires parameter with image 741e13c2760SPrzemyslaw Czarnowski if (legacy) 742e13c2760SPrzemyslaw Czarnowski { 743e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 7445e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 745e13c2760SPrzemyslaw Czarnowski if (ec) 746e13c2760SPrzemyslaw Czarnowski { 747e13c2760SPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec; 748e13c2760SPrzemyslaw Czarnowski 749e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 750e13c2760SPrzemyslaw Czarnowski return; 751e13c2760SPrzemyslaw Czarnowski } 752e13c2760SPrzemyslaw Czarnowski }, 753e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, 754e13c2760SPrzemyslaw Czarnowski "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount"); 755e13c2760SPrzemyslaw Czarnowski } 756e13c2760SPrzemyslaw Czarnowski else // proxy 757e13c2760SPrzemyslaw Czarnowski { 758e13c2760SPrzemyslaw Czarnowski crow::connections::systemBus->async_method_call( 7595e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 760e13c2760SPrzemyslaw Czarnowski if (ec) 761e13c2760SPrzemyslaw Czarnowski { 762e13c2760SPrzemyslaw Czarnowski BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec; 763e13c2760SPrzemyslaw Czarnowski 764e13c2760SPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 765e13c2760SPrzemyslaw Czarnowski return; 766e13c2760SPrzemyslaw Czarnowski } 767e13c2760SPrzemyslaw Czarnowski }, 768e13c2760SPrzemyslaw Czarnowski service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name, 769e13c2760SPrzemyslaw Czarnowski "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); 770e13c2760SPrzemyslaw Czarnowski } 771e13c2760SPrzemyslaw Czarnowski } 772e13c2760SPrzemyslaw Czarnowski 77396825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost( 77496825bebSEd Tanous crow::App& app, const crow::Request& req, 77522db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 77696825bebSEd Tanous const std::string& name, const std::string& resName) 77796825bebSEd Tanous { 7783ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 77945ca1b86SEd Tanous { 78045ca1b86SEd Tanous return; 78145ca1b86SEd Tanous } 78222db1728SEd Tanous if (name != "bmc") 783107077deSPrzemyslaw Czarnowski { 7841f2a40ceSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, "VirtualMedia.InsertMedia", 785002d39b4SEd Tanous resName); 786107077deSPrzemyslaw Czarnowski 787107077deSPrzemyslaw Czarnowski return; 788107077deSPrzemyslaw Czarnowski } 789*120fa86aSPrzemyslaw Czarnowski std::optional<InsertMediaActionParams> actionParams = 790*120fa86aSPrzemyslaw Czarnowski InsertMediaActionParams(); 79198be3e39SEd Tanous 792*120fa86aSPrzemyslaw Czarnowski // Read obligatory parameters (url of image) 79315ed6780SWilly Tu if (!json_util::readJsonAction( 794*120fa86aSPrzemyslaw Czarnowski req, asyncResp->res, "Image", actionParams->imageUrl, 795*120fa86aSPrzemyslaw Czarnowski "WriteProtected", actionParams->writeProtected, "UserName", 796*120fa86aSPrzemyslaw Czarnowski actionParams->userName, "Password", actionParams->password, 797*120fa86aSPrzemyslaw Czarnowski "Inserted", actionParams->inserted, "TransferMethod", 798*120fa86aSPrzemyslaw Czarnowski actionParams->transferMethod, "TransferProtocolType", 799*120fa86aSPrzemyslaw Czarnowski actionParams->transferProtocolType)) 80098be3e39SEd Tanous { 80198be3e39SEd Tanous return; 80298be3e39SEd Tanous } 803107077deSPrzemyslaw Czarnowski 8042b73119cSGeorge Liu dbus::utility::getDbusObject( 8052b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 80696825bebSEd Tanous [asyncResp, actionParams, 8072b73119cSGeorge Liu resName](const boost::system::error_code& ec, 808002d39b4SEd Tanous const dbus::utility::MapperGetObject& getObjectType) mutable { 80922db1728SEd Tanous if (ec) 81022db1728SEd Tanous { 81196825bebSEd Tanous BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec; 81222db1728SEd Tanous messages::internalError(asyncResp->res); 813107077deSPrzemyslaw Czarnowski 81422db1728SEd Tanous return; 81522db1728SEd Tanous } 81622db1728SEd Tanous std::string service = getObjectType.begin()->first; 81722db1728SEd Tanous BMCWEB_LOG_DEBUG << "GetObjectType: " << service; 81822db1728SEd Tanous 81922db1728SEd Tanous crow::connections::systemBus->async_method_call( 82098be3e39SEd Tanous [service, resName, actionParams, 8215e7e2dc5SEd Tanous asyncResp](const boost::system::error_code& ec2, 822002d39b4SEd Tanous dbus::utility::ManagedObjectType& subtree) mutable { 8238a592810SEd Tanous if (ec2) 82422db1728SEd Tanous { 82522db1728SEd Tanous BMCWEB_LOG_DEBUG << "DBUS response error"; 8261f2a40ceSPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 82722db1728SEd Tanous 82822db1728SEd Tanous return; 82922db1728SEd Tanous } 83022db1728SEd Tanous 83122db1728SEd Tanous for (const auto& object : subtree) 83222db1728SEd Tanous { 83322db1728SEd Tanous const std::string& path = 834002d39b4SEd Tanous static_cast<const std::string&>(object.first); 83522db1728SEd Tanous 83622db1728SEd Tanous std::size_t lastIndex = path.rfind('/'); 83722db1728SEd Tanous if (lastIndex == std::string::npos) 83822db1728SEd Tanous { 83922db1728SEd Tanous continue; 84022db1728SEd Tanous } 84122db1728SEd Tanous 84222db1728SEd Tanous lastIndex += 1; 84322db1728SEd Tanous 84422db1728SEd Tanous if (path.substr(lastIndex) == resName) 84522db1728SEd Tanous { 84622db1728SEd Tanous lastIndex = path.rfind("Proxy"); 84722db1728SEd Tanous if (lastIndex != std::string::npos) 84822db1728SEd Tanous { 84922db1728SEd Tanous // Not possible in proxy mode 850002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "InsertMedia not " 85122db1728SEd Tanous "allowed in proxy mode"; 85296825bebSEd Tanous messages::resourceNotFound(asyncResp->res, 85396825bebSEd Tanous "VirtualMedia.InsertMedia", 85422db1728SEd Tanous resName); 85522db1728SEd Tanous 85622db1728SEd Tanous return; 85722db1728SEd Tanous } 85822db1728SEd Tanous 85922db1728SEd Tanous lastIndex = path.rfind("Legacy"); 86022db1728SEd Tanous if (lastIndex == std::string::npos) 86122db1728SEd Tanous { 86222db1728SEd Tanous continue; 86322db1728SEd Tanous } 86422db1728SEd Tanous 865*120fa86aSPrzemyslaw Czarnowski if (!actionParams) 866*120fa86aSPrzemyslaw Czarnowski { 867*120fa86aSPrzemyslaw Czarnowski return; 868*120fa86aSPrzemyslaw Czarnowski } 869*120fa86aSPrzemyslaw Czarnowski 870*120fa86aSPrzemyslaw Czarnowski validateParams(asyncResp, service, resName, *actionParams); 87122db1728SEd Tanous 87222db1728SEd Tanous return; 87322db1728SEd Tanous } 87422db1728SEd Tanous } 87522db1728SEd Tanous BMCWEB_LOG_DEBUG << "Parent item not found"; 87696825bebSEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 87722db1728SEd Tanous }, 87822db1728SEd Tanous service, "/xyz/openbmc_project/VirtualMedia", 879002d39b4SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 8802b73119cSGeorge Liu }); 88196825bebSEd Tanous } 88222db1728SEd Tanous 88396825bebSEd Tanous inline void handleManagersVirtualMediaActionEject( 88496825bebSEd Tanous crow::App& app, const crow::Request& req, 88522db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 88696825bebSEd Tanous const std::string& managerName, const std::string& resName) 88796825bebSEd Tanous { 8883ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 88945ca1b86SEd Tanous { 89045ca1b86SEd Tanous return; 89145ca1b86SEd Tanous } 89296825bebSEd Tanous if (managerName != "bmc") 893107077deSPrzemyslaw Czarnowski { 894*120fa86aSPrzemyslaw Czarnowski messages::resourceNotFound(asyncResp->res, "VirtualMedia.EjectMedia", 895002d39b4SEd Tanous resName); 89622db1728SEd Tanous 89722db1728SEd Tanous return; 89822db1728SEd Tanous } 89922db1728SEd Tanous 9002b73119cSGeorge Liu dbus::utility::getDbusObject( 9012b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 902002d39b4SEd Tanous [asyncResp, 9032b73119cSGeorge Liu resName](const boost::system::error_code& ec2, 904b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 9058a592810SEd Tanous if (ec2) 90622db1728SEd Tanous { 9078a592810SEd Tanous BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec2; 90822db1728SEd Tanous messages::internalError(asyncResp->res); 90922db1728SEd Tanous 91022db1728SEd Tanous return; 91122db1728SEd Tanous } 91222db1728SEd Tanous std::string service = getObjectType.begin()->first; 91322db1728SEd Tanous BMCWEB_LOG_DEBUG << "GetObjectType: " << service; 91422db1728SEd Tanous 91522db1728SEd Tanous crow::connections::systemBus->async_method_call( 91602cad96eSEd Tanous [resName, service, asyncResp{asyncResp}]( 9175e7e2dc5SEd Tanous const boost::system::error_code& ec, 91802cad96eSEd Tanous const dbus::utility::ManagedObjectType& subtree) { 91922db1728SEd Tanous if (ec) 92022db1728SEd Tanous { 92122db1728SEd Tanous BMCWEB_LOG_DEBUG << "DBUS response error"; 9221f2a40ceSPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 92322db1728SEd Tanous 92422db1728SEd Tanous return; 92522db1728SEd Tanous } 92622db1728SEd Tanous 92722db1728SEd Tanous for (const auto& object : subtree) 92822db1728SEd Tanous { 92922db1728SEd Tanous const std::string& path = 930002d39b4SEd Tanous static_cast<const std::string&>(object.first); 93122db1728SEd Tanous 93222db1728SEd Tanous std::size_t lastIndex = path.rfind('/'); 93322db1728SEd Tanous if (lastIndex == std::string::npos) 93422db1728SEd Tanous { 93522db1728SEd Tanous continue; 93622db1728SEd Tanous } 93722db1728SEd Tanous 93822db1728SEd Tanous lastIndex += 1; 93922db1728SEd Tanous 94022db1728SEd Tanous if (path.substr(lastIndex) == resName) 94122db1728SEd Tanous { 94222db1728SEd Tanous lastIndex = path.rfind("Proxy"); 94322db1728SEd Tanous if (lastIndex != std::string::npos) 94422db1728SEd Tanous { 94522db1728SEd Tanous // Proxy mode 946002d39b4SEd Tanous doVmAction(asyncResp, service, resName, false); 94722db1728SEd Tanous } 94822db1728SEd Tanous 94922db1728SEd Tanous lastIndex = path.rfind("Legacy"); 95022db1728SEd Tanous if (lastIndex != std::string::npos) 95122db1728SEd Tanous { 95222db1728SEd Tanous // Legacy mode 953002d39b4SEd Tanous doVmAction(asyncResp, service, resName, true); 95422db1728SEd Tanous } 95522db1728SEd Tanous 95622db1728SEd Tanous return; 95722db1728SEd Tanous } 95822db1728SEd Tanous } 95922db1728SEd Tanous BMCWEB_LOG_DEBUG << "Parent item not found"; 96096825bebSEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 96122db1728SEd Tanous }, 96222db1728SEd Tanous service, "/xyz/openbmc_project/VirtualMedia", 963002d39b4SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 9642b73119cSGeorge Liu }); 96596825bebSEd Tanous } 96696825bebSEd Tanous 96796825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet( 96896825bebSEd Tanous crow::App& app, const crow::Request& req, 96922db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 97096825bebSEd Tanous const std::string& name) 97196825bebSEd Tanous { 9723ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 97345ca1b86SEd Tanous { 97445ca1b86SEd Tanous return; 97545ca1b86SEd Tanous } 97622db1728SEd Tanous if (name != "bmc") 97722db1728SEd Tanous { 978002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", name); 979107077deSPrzemyslaw Czarnowski 980107077deSPrzemyslaw Czarnowski return; 981107077deSPrzemyslaw Czarnowski } 982107077deSPrzemyslaw Czarnowski 9838d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 984107077deSPrzemyslaw Czarnowski "#VirtualMediaCollection.VirtualMediaCollection"; 9858d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Virtual Media Services"; 986fdb20347SEd Tanous asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces( 987fdb20347SEd Tanous "redfish", "v1", "Managers", name, "VirtualMedia"); 988107077deSPrzemyslaw Czarnowski 9892b73119cSGeorge Liu dbus::utility::getDbusObject( 9902b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 9912b73119cSGeorge Liu [asyncResp, name](const boost::system::error_code& ec, 992b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 993107077deSPrzemyslaw Czarnowski if (ec) 994107077deSPrzemyslaw Czarnowski { 99596825bebSEd Tanous BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec; 996107077deSPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 997107077deSPrzemyslaw Czarnowski 998107077deSPrzemyslaw Czarnowski return; 999107077deSPrzemyslaw Czarnowski } 1000107077deSPrzemyslaw Czarnowski std::string service = getObjectType.begin()->first; 1001107077deSPrzemyslaw Czarnowski BMCWEB_LOG_DEBUG << "GetObjectType: " << service; 1002107077deSPrzemyslaw Czarnowski 1003107077deSPrzemyslaw Czarnowski getVmResourceList(asyncResp, service, name); 10042b73119cSGeorge Liu }); 100596825bebSEd Tanous } 1006107077deSPrzemyslaw Czarnowski 100796825bebSEd Tanous inline void 100896825bebSEd Tanous handleVirtualMediaGet(crow::App& app, const crow::Request& req, 100922db1728SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 101096825bebSEd Tanous const std::string& name, const std::string& resName) 101196825bebSEd Tanous { 10123ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 101345ca1b86SEd Tanous { 101445ca1b86SEd Tanous return; 101545ca1b86SEd Tanous } 1016107077deSPrzemyslaw Czarnowski if (name != "bmc") 1017107077deSPrzemyslaw Czarnowski { 1018002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); 1019107077deSPrzemyslaw Czarnowski 1020107077deSPrzemyslaw Czarnowski return; 1021107077deSPrzemyslaw Czarnowski } 1022107077deSPrzemyslaw Czarnowski 10232b73119cSGeorge Liu dbus::utility::getDbusObject( 10242b73119cSGeorge Liu "/xyz/openbmc_project/VirtualMedia", {}, 1025002d39b4SEd Tanous [asyncResp, name, 10262b73119cSGeorge Liu resName](const boost::system::error_code& ec, 1027b9d36b47SEd Tanous const dbus::utility::MapperGetObject& getObjectType) { 1028107077deSPrzemyslaw Czarnowski if (ec) 1029107077deSPrzemyslaw Czarnowski { 103096825bebSEd Tanous BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec; 1031107077deSPrzemyslaw Czarnowski messages::internalError(asyncResp->res); 1032107077deSPrzemyslaw Czarnowski 1033107077deSPrzemyslaw Czarnowski return; 1034107077deSPrzemyslaw Czarnowski } 1035107077deSPrzemyslaw Czarnowski std::string service = getObjectType.begin()->first; 1036107077deSPrzemyslaw Czarnowski BMCWEB_LOG_DEBUG << "GetObjectType: " << service; 1037107077deSPrzemyslaw Czarnowski 1038107077deSPrzemyslaw Czarnowski getVmData(asyncResp, service, name, resName); 10392b73119cSGeorge Liu }); 104096825bebSEd Tanous } 104196825bebSEd Tanous 104296825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app) 104396825bebSEd Tanous { 104496825bebSEd Tanous BMCWEB_ROUTE( 104596825bebSEd Tanous app, 104696825bebSEd Tanous "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia") 104796825bebSEd Tanous .privileges(redfish::privileges::postVirtualMedia) 104896825bebSEd Tanous .methods(boost::beast::http::verb::post)(std::bind_front( 104996825bebSEd Tanous handleManagersVirtualMediaActionInsertPost, std::ref(app))); 105096825bebSEd Tanous 105196825bebSEd Tanous BMCWEB_ROUTE( 105296825bebSEd Tanous app, 105396825bebSEd Tanous "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia") 105496825bebSEd Tanous .privileges(redfish::privileges::postVirtualMedia) 105596825bebSEd Tanous .methods(boost::beast::http::verb::post)(std::bind_front( 105696825bebSEd Tanous handleManagersVirtualMediaActionEject, std::ref(app))); 105796825bebSEd Tanous 105896825bebSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/") 105996825bebSEd Tanous .privileges(redfish::privileges::getVirtualMediaCollection) 106096825bebSEd Tanous .methods(boost::beast::http::verb::get)(std::bind_front( 106196825bebSEd Tanous handleManagersVirtualMediaCollectionGet, std::ref(app))); 106296825bebSEd Tanous 106396825bebSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/") 106496825bebSEd Tanous .privileges(redfish::privileges::getVirtualMedia) 106596825bebSEd Tanous .methods(boost::beast::http::verb::get)( 106696825bebSEd Tanous std::bind_front(handleVirtualMediaGet, std::ref(app))); 1067107077deSPrzemyslaw Czarnowski } 1068107077deSPrzemyslaw Czarnowski 1069107077deSPrzemyslaw Czarnowski } // namespace redfish 1070