blob: 5e177ab8c3e6ccbba3b9e78a1c70965c2ea15851 [file] [log] [blame]
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +02001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "account_service.hpp"
19#include "app.hpp"
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +020020#include "async_resp.hpp"
George Liu2b731192023-01-11 16:27:13 +080021#include "dbus_utility.hpp"
Ed Tanous739b87e2023-02-24 13:13:33 -080022#include "generated/enums/virtual_media.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "query.hpp"
24#include "registries/privilege_registry.hpp"
25#include "utils/json_utils.hpp"
26
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +010027#include <boost/process/async_pipe.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070028#include <boost/url/format.hpp>
Anna Platash9e319cf2020-11-17 10:18:31 +010029#include <boost/url/url_view.hpp>
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +020030
George Liu2b731192023-01-11 16:27:13 +080031#include <array>
32#include <string_view>
33
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +020034namespace redfish
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +020035{
Ed Tanous365a73f2023-02-24 12:16:49 -080036
37enum class VmMode
38{
39 Invalid,
40 Legacy,
41 Proxy
42};
43
44inline VmMode
45 parseObjectPathAndGetMode(const sdbusplus::message::object_path& itemPath,
46 const std::string& resName)
47{
48 std::string thisPath = itemPath.filename();
49 BMCWEB_LOG_DEBUG << "Filename: " << itemPath.str
50 << ", ThisPath: " << thisPath;
51
52 if (thisPath.empty())
53 {
54 return VmMode::Invalid;
55 }
56
57 if (thisPath != resName)
58 {
59 return VmMode::Invalid;
60 }
61
62 auto mode = itemPath.parent_path();
63 auto type = mode.parent_path();
64
65 if (mode.filename().empty() || type.filename().empty())
66 {
67 return VmMode::Invalid;
68 }
69
70 if (type.filename() != "VirtualMedia")
71 {
72 return VmMode::Invalid;
73 }
74 std::string modeStr = mode.filename();
75 if (modeStr == "Legacy")
76 {
77 return VmMode::Legacy;
78 }
79 if (modeStr == "Proxy")
80 {
81 return VmMode::Proxy;
82 }
83 return VmMode::Invalid;
84}
85
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +020086using CheckItemHandler =
87 std::function<void(const std::string& service, const std::string& resName,
88 const std::shared_ptr<bmcweb::AsyncResp>&,
George Liu70cbdf52023-03-04 12:07:25 +080089 const std::pair<sdbusplus::message::object_path,
90 dbus::utility::DBusInteracesMap>&)>;
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +020091
Ed Tanousac106bf2023-06-07 09:24:59 -070092inline void
93 findAndParseObject(const std::string& service, const std::string& resName,
94 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
95 CheckItemHandler&& handler)
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +020096{
97 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -070098 [service, resName, asyncResp,
Lakshmi Yadlapati746c5b82023-03-06 16:07:28 -060099 handler](const boost::system::error_code& ec,
George Liu70cbdf52023-03-04 12:07:25 +0800100 const dbus::utility::ManagedObjectType& subtree) {
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200101 if (ec)
102 {
103 BMCWEB_LOG_DEBUG << "DBUS response error";
104
105 return;
106 }
107
George Liu70cbdf52023-03-04 12:07:25 +0800108 for (const auto& item : subtree)
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200109 {
110 VmMode mode = parseObjectPathAndGetMode(item.first, resName);
111 if (mode != VmMode::Invalid)
112 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700113 handler(service, resName, asyncResp, item);
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200114 return;
115 }
116 }
117
118 BMCWEB_LOG_DEBUG << "Parent item not found";
Ed Tanousac106bf2023-06-07 09:24:59 -0700119 asyncResp->res.result(boost::beast::http::status::not_found);
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200120 },
121 service, "/xyz/openbmc_project/VirtualMedia",
122 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
123}
124
Anna Platash9e319cf2020-11-17 10:18:31 +0100125/**
126 * @brief Function extracts transfer protocol name from URI.
127 */
Ed Tanous67df0732021-10-26 11:23:56 -0700128inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri)
129{
130 boost::urls::result<boost::urls::url_view> url =
Ed Tanous079360a2022-06-29 10:05:19 -0700131 boost::urls::parse_uri(imageUri);
Ed Tanous67df0732021-10-26 11:23:56 -0700132 if (!url)
133 {
134 return "None";
135 }
Ed Tanous079360a2022-06-29 10:05:19 -0700136 std::string_view scheme = url->scheme();
Ed Tanous67df0732021-10-26 11:23:56 -0700137 if (scheme == "smb")
138 {
139 return "CIFS";
140 }
141 if (scheme == "https")
142 {
143 return "HTTPS";
144 }
145
146 return "None";
147}
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200148
149/**
150 * @brief Read all known properties from VM object interfaces
151 */
Ed Tanous22db1722021-06-09 10:53:51 -0700152inline void
Ed Tanous8a592812022-06-04 09:06:59 -0700153 vmParseInterfaceObject(const dbus::utility::DBusInteracesMap& interfaces,
Ed Tanousac106bf2023-06-07 09:24:59 -0700154 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200155{
Ed Tanous8a592812022-06-04 09:06:59 -0700156 for (const auto& [interface, values] : interfaces)
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200157 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800158 if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint")
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200159 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800160 for (const auto& [property, value] : values)
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200161 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800162 if (property == "EndpointId")
163 {
164 const std::string* endpointIdValue =
165 std::get_if<std::string>(&value);
166 if (endpointIdValue == nullptr)
167 {
168 continue;
169 }
170 if (!endpointIdValue->empty())
171 {
172 // Proxy mode
Ed Tanousac106bf2023-06-07 09:24:59 -0700173 asyncResp->res
Ed Tanous711ac7a2021-12-20 09:34:41 -0800174 .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] =
175 *endpointIdValue;
Ed Tanousac106bf2023-06-07 09:24:59 -0700176 asyncResp->res.jsonValue["TransferProtocolType"] =
177 "OEM";
Ed Tanous711ac7a2021-12-20 09:34:41 -0800178 }
179 }
180 if (property == "ImageURL")
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200181 {
Anna Platash9e319cf2020-11-17 10:18:31 +0100182 const std::string* imageUrlValue =
Ed Tanous711ac7a2021-12-20 09:34:41 -0800183 std::get_if<std::string>(&value);
Ed Tanous26f69762022-01-25 09:49:11 -0800184 if (imageUrlValue != nullptr && !imageUrlValue->empty())
Przemyslaw Czarnowskida4784d2020-11-06 09:58:25 +0100185 {
Anna Platash9e319cf2020-11-17 10:18:31 +0100186 std::filesystem::path filePath = *imageUrlValue;
187 if (!filePath.has_filename())
188 {
189 // this will handle https share, which not
190 // necessarily has to have filename given.
Ed Tanousac106bf2023-06-07 09:24:59 -0700191 asyncResp->res.jsonValue["ImageName"] = "";
Anna Platash9e319cf2020-11-17 10:18:31 +0100192 }
193 else
194 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700195 asyncResp->res.jsonValue["ImageName"] =
Anna Platash9e319cf2020-11-17 10:18:31 +0100196 filePath.filename();
197 }
Przemyslaw Czarnowskida4784d2020-11-06 09:58:25 +0100198
Ed Tanousac106bf2023-06-07 09:24:59 -0700199 asyncResp->res.jsonValue["Image"] = *imageUrlValue;
200 asyncResp->res.jsonValue["TransferProtocolType"] =
Anna Platash9e319cf2020-11-17 10:18:31 +0100201 getTransferProtocolTypeFromUri(*imageUrlValue);
202
Ed Tanousac106bf2023-06-07 09:24:59 -0700203 asyncResp->res.jsonValue["ConnectedVia"] =
Ed Tanous739b87e2023-02-24 13:13:33 -0800204 virtual_media::ConnectedVia::URI;
Anna Platash9e319cf2020-11-17 10:18:31 +0100205 }
206 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800207 if (property == "WriteProtected")
Anna Platash9e319cf2020-11-17 10:18:31 +0100208 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800209 const bool* writeProtectedValue = std::get_if<bool>(&value);
Ed Tanouse662eae2022-01-25 10:39:19 -0800210 if (writeProtectedValue != nullptr)
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200211 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700212 asyncResp->res.jsonValue["WriteProtected"] =
Anna Platash9e319cf2020-11-17 10:18:31 +0100213 *writeProtectedValue;
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200214 }
215 }
216 }
217 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800218 if (interface == "xyz.openbmc_project.VirtualMedia.Process")
219 {
220 for (const auto& [property, value] : values)
221 {
222 if (property == "Active")
223 {
224 const bool* activeValue = std::get_if<bool>(&value);
Ed Tanouse662eae2022-01-25 10:39:19 -0800225 if (activeValue == nullptr)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800226 {
227 BMCWEB_LOG_DEBUG << "Value Active not found";
228 return;
229 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700230 asyncResp->res.jsonValue["Inserted"] = *activeValue;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800231
Ed Tanouse05aec52022-01-25 10:28:56 -0800232 if (*activeValue)
Ed Tanous711ac7a2021-12-20 09:34:41 -0800233 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700234 asyncResp->res.jsonValue["ConnectedVia"] =
Ed Tanous739b87e2023-02-24 13:13:33 -0800235 virtual_media::ConnectedVia::Applet;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800236 }
237 }
238 }
239 }
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200240 }
241}
242
243/**
244 * @brief Fill template for Virtual Media Item.
245 */
Ed Tanous22db1722021-06-09 10:53:51 -0700246inline nlohmann::json vmItemTemplate(const std::string& name,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500247 const std::string& resName)
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200248{
249 nlohmann::json item;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700250 item["@odata.id"] = boost::urls::format(
251 "/redfish/v1/Managers/{}/VirtualMedia/{}", name, resName);
Ed Tanous22db1722021-06-09 10:53:51 -0700252
Przemyslaw Czarnowskid04ba322020-01-21 12:41:56 +0100253 item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia";
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200254 item["Name"] = "Virtual Removable Media";
255 item["Id"] = resName;
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200256 item["WriteProtected"] = true;
Ed Tanous739b87e2023-02-24 13:13:33 -0800257 item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected;
Ed Tanous613dabe2022-07-09 11:17:36 -0700258 item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"});
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200259 item["TransferMethod"] = "Stream";
Przemyslaw Czarnowskid04ba322020-01-21 12:41:56 +0100260 item["Oem"]["OpenBMC"]["@odata.type"] =
261 "#OemVirtualMedia.v1_0_0.VirtualMedia";
V-Sanjana15b89722023-05-11 16:27:03 +0530262 item["Oem"]["OpenBMC"]["@odata.id"] = boost::urls::format(
263 "/redfish/v1/Managers/{}/VirtualMedia/{}#/Oem/OpenBMC", name, resName);
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200264
265 return item;
266}
267
268/**
269 * @brief Fills collection data
270 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700271inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500272 const std::string& service,
273 const std::string& name)
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200274{
275 BMCWEB_LOG_DEBUG << "Get available Virtual Media resources.";
276 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -0700277 [name, asyncResp{std::move(asyncResp)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800278 const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700279 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700280 if (ec)
281 {
282 BMCWEB_LOG_DEBUG << "DBUS response error";
283 return;
284 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700285 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
Ed Tanous002d39b2022-05-31 08:59:27 -0700286 members = nlohmann::json::array();
287
288 for (const auto& object : subtree)
289 {
290 nlohmann::json item;
291 std::string path = object.first.filename();
292 if (path.empty())
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200293 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700294 continue;
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200295 }
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200296
Ed Tanousef4c65b2023-04-24 15:28:50 -0700297 item["@odata.id"] = boost::urls::format(
298 "/redfish/v1/Managers/{}/VirtualMedia/{}", name, path);
Ed Tanous002d39b2022-05-31 08:59:27 -0700299 members.emplace_back(std::move(item));
300 }
Ed Tanousac106bf2023-06-07 09:24:59 -0700301 asyncResp->res.jsonValue["Members@odata.count"] = members.size();
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200302 },
303 service, "/xyz/openbmc_project/VirtualMedia",
304 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
305}
306
George Liu70cbdf52023-03-04 12:07:25 +0800307inline void
308 afterGetVmData(const std::string& name, const std::string& /*service*/,
309 const std::string& resName,
310 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
311 const std::pair<sdbusplus::message::object_path,
312 dbus::utility::DBusInteracesMap>& item)
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200313{
314 VmMode mode = parseObjectPathAndGetMode(item.first, resName);
315 if (mode == VmMode::Invalid)
316 {
317 return;
318 }
319
320 asyncResp->res.jsonValue = vmItemTemplate(name, resName);
321
322 // Check if dbus path is Legacy type
323 if (mode == VmMode::Legacy)
324 {
Ed Tanousef4c65b2023-04-24 15:28:50 -0700325 asyncResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"]
326 ["target"] = boost::urls::format(
327 "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.InsertMedia",
328 name, resName);
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200329 }
330
331 vmParseInterfaceObject(item.second, asyncResp);
332
Ed Tanousef4c65b2023-04-24 15:28:50 -0700333 asyncResp->res.jsonValue["Actions"]["#VirtualMedia.EjectMedia"]
334 ["target"] = boost::urls::format(
335 "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.EjectMedia",
336 name, resName);
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200337}
338
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200339/**
340 * @brief Fills data for specific resource
341 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700342inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500343 const std::string& service, const std::string& name,
344 const std::string& resName)
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200345{
346 BMCWEB_LOG_DEBUG << "Get Virtual Media resource data.";
347
Ed Tanousac106bf2023-06-07 09:24:59 -0700348 findAndParseObject(service, resName, asyncResp,
George Liu70cbdf52023-03-04 12:07:25 +0800349 std::bind_front(afterGetVmData, name));
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +0200350}
351
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200352/**
Ed Tanous22db1722021-06-09 10:53:51 -0700353 * @brief Transfer protocols supported for InsertMedia action.
354 *
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200355 */
Ed Tanous22db1722021-06-09 10:53:51 -0700356enum class TransferProtocol
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200357{
Ed Tanous22db1722021-06-09 10:53:51 -0700358 https,
359 smb,
360 invalid
361};
362
363/**
364 * @brief Function extracts transfer protocol type from URI.
365 *
366 */
Ed Tanous67df0732021-10-26 11:23:56 -0700367inline std::optional<TransferProtocol>
Ed Tanousd9f466b2023-03-06 15:04:25 -0800368 getTransferProtocolFromUri(boost::urls::url_view imageUri)
Ed Tanous67df0732021-10-26 11:23:56 -0700369{
Ed Tanous079360a2022-06-29 10:05:19 -0700370 std::string_view scheme = imageUri.scheme();
Ed Tanous67df0732021-10-26 11:23:56 -0700371 if (scheme == "smb")
372 {
373 return TransferProtocol::smb;
374 }
375 if (scheme == "https")
376 {
377 return TransferProtocol::https;
378 }
379 if (!scheme.empty())
380 {
381 return TransferProtocol::invalid;
382 }
383
384 return {};
385}
Ed Tanous22db1722021-06-09 10:53:51 -0700386
387/**
388 * @brief Function convert transfer protocol from string param.
389 *
390 */
391inline std::optional<TransferProtocol> getTransferProtocolFromParam(
392 const std::optional<std::string>& transferProtocolType)
393{
394 if (transferProtocolType == std::nullopt)
Agata Olenderc6f4e012020-03-11 15:19:07 +0100395 {
Ed Tanous22db1722021-06-09 10:53:51 -0700396 return {};
Agata Olenderc6f4e012020-03-11 15:19:07 +0100397 }
398
Ed Tanous22db1722021-06-09 10:53:51 -0700399 if (*transferProtocolType == "CIFS")
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200400 {
Ed Tanous22db1722021-06-09 10:53:51 -0700401 return TransferProtocol::smb;
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200402 }
403
Ed Tanous22db1722021-06-09 10:53:51 -0700404 if (*transferProtocolType == "HTTPS")
405 {
406 return TransferProtocol::https;
407 }
408
409 return TransferProtocol::invalid;
410}
411
412/**
413 * @brief Function extends URI with transfer protocol type.
414 *
415 */
416inline std::string
417 getUriWithTransferProtocol(const std::string& imageUri,
418 const TransferProtocol& transferProtocol)
419{
420 if (transferProtocol == TransferProtocol::smb)
421 {
422 return "smb://" + imageUri;
423 }
424
425 if (transferProtocol == TransferProtocol::https)
426 {
427 return "https://" + imageUri;
428 }
429
430 return imageUri;
431}
432
Przemyslaw Czarnowski1f2a40c2022-06-24 13:47:08 +0200433struct InsertMediaActionParams
434{
Przemyslaw Czarnowski120fa862022-06-24 15:10:48 +0200435 std::optional<std::string> imageUrl;
Przemyslaw Czarnowski1f2a40c2022-06-24 13:47:08 +0200436 std::optional<std::string> userName;
437 std::optional<std::string> password;
438 std::optional<std::string> transferMethod;
439 std::optional<std::string> transferProtocolType;
440 std::optional<bool> writeProtected = true;
441 std::optional<bool> inserted;
442};
443
Ed Tanous22db1722021-06-09 10:53:51 -0700444template <typename T>
445static void secureCleanup(T& value)
446{
Ed Tanous4ecc6182022-01-07 09:36:26 -0800447 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
Ed Tanous22db1722021-06-09 10:53:51 -0700448 auto raw = const_cast<typename T::value_type*>(value.data());
449 explicit_bzero(raw, value.size() * sizeof(*raw));
450}
451
452class Credentials
453{
454 public:
455 Credentials(std::string&& user, std::string&& password) :
456 userBuf(std::move(user)), passBuf(std::move(password))
457 {}
458
459 ~Credentials()
460 {
461 secureCleanup(userBuf);
462 secureCleanup(passBuf);
463 }
464
465 const std::string& user()
466 {
467 return userBuf;
468 }
469
470 const std::string& password()
471 {
472 return passBuf;
473 }
474
475 Credentials() = delete;
476 Credentials(const Credentials&) = delete;
477 Credentials& operator=(const Credentials&) = delete;
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800478 Credentials(Credentials&&) = delete;
479 Credentials& operator=(Credentials&&) = delete;
Ed Tanous22db1722021-06-09 10:53:51 -0700480
481 private:
482 std::string userBuf;
483 std::string passBuf;
484};
485
486class CredentialsProvider
487{
488 public:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500489 template <typename T>
Ed Tanous22db1722021-06-09 10:53:51 -0700490 struct Deleter
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100491 {
Ed Tanous22db1722021-06-09 10:53:51 -0700492 void operator()(T* buff) const
493 {
494 if (buff)
495 {
496 secureCleanup(*buff);
497 delete buff;
498 }
499 }
500 };
501
502 using Buffer = std::vector<char>;
503 using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>;
504 // Using explicit definition instead of std::function to avoid implicit
505 // conversions eg. stack copy instead of reference
506 using FormatterFunc = void(const std::string& username,
507 const std::string& password, Buffer& dest);
508
509 CredentialsProvider(std::string&& user, std::string&& password) :
510 credentials(std::move(user), std::move(password))
511 {}
512
513 const std::string& user()
514 {
515 return credentials.user();
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100516 }
517
Ed Tanous22db1722021-06-09 10:53:51 -0700518 const std::string& password()
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100519 {
Ed Tanous22db1722021-06-09 10:53:51 -0700520 return credentials.password();
521 }
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100522
Ed Tanous1917ee92022-06-30 22:30:50 -0700523 SecureBuffer pack(FormatterFunc* formatter)
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100524 {
Ed Tanous22db1722021-06-09 10:53:51 -0700525 SecureBuffer packed{new Buffer{}};
Ed Tanouse662eae2022-01-25 10:39:19 -0800526 if (formatter != nullptr)
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100527 {
Ed Tanous22db1722021-06-09 10:53:51 -0700528 formatter(credentials.user(), credentials.password(), *packed);
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100529 }
530
Ed Tanous22db1722021-06-09 10:53:51 -0700531 return packed;
532 }
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100533
Ed Tanous22db1722021-06-09 10:53:51 -0700534 private:
535 Credentials credentials;
536};
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100537
Ed Tanous22db1722021-06-09 10:53:51 -0700538// Wrapper for boost::async_pipe ensuring proper pipe cleanup
Ed Tanous0a483062022-07-11 10:18:50 -0700539class SecurePipe
Ed Tanous22db1722021-06-09 10:53:51 -0700540{
541 public:
542 using unix_fd = sdbusplus::message::unix_fd;
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100543
Ed Tanous0a483062022-07-11 10:18:50 -0700544 SecurePipe(boost::asio::io_context& io,
545 CredentialsProvider::SecureBuffer&& bufferIn) :
546 impl(io),
547 buffer{std::move(bufferIn)}
Ed Tanous22db1722021-06-09 10:53:51 -0700548 {}
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100549
Ed Tanous0a483062022-07-11 10:18:50 -0700550 ~SecurePipe()
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100551 {
Ed Tanous22db1722021-06-09 10:53:51 -0700552 // Named pipe needs to be explicitly removed
553 impl.close();
554 }
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100555
Ed Tanous0a483062022-07-11 10:18:50 -0700556 SecurePipe(const SecurePipe&) = delete;
557 SecurePipe(SecurePipe&&) = delete;
558 SecurePipe& operator=(const SecurePipe&) = delete;
559 SecurePipe& operator=(SecurePipe&&) = delete;
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800560
Ed Tanous0a483062022-07-11 10:18:50 -0700561 unix_fd fd() const
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200562 {
Ed Tanous22db1722021-06-09 10:53:51 -0700563 return unix_fd{impl.native_source()};
564 }
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100565
Ed Tanous22db1722021-06-09 10:53:51 -0700566 template <typename WriteHandler>
567 void asyncWrite(WriteHandler&& handler)
568 {
Ed Tanous0a483062022-07-11 10:18:50 -0700569 impl.async_write_some(boost::asio::buffer(*buffer),
570 std::forward<WriteHandler>(handler));
Ed Tanous22db1722021-06-09 10:53:51 -0700571 }
572
573 const std::string name;
574 boost::process::async_pipe impl;
Ed Tanous0a483062022-07-11 10:18:50 -0700575 CredentialsProvider::SecureBuffer buffer;
Ed Tanous22db1722021-06-09 10:53:51 -0700576};
577
578/**
579 * @brief Function transceives data with dbus directly.
580 *
581 * All BMC state properties will be retrieved before sending reset request.
582 */
583inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
584 const std::string& service, const std::string& name,
585 const std::string& imageUrl, const bool rw,
586 std::string&& userName, std::string&& password)
587{
Ed Tanous22db1722021-06-09 10:53:51 -0700588 constexpr const size_t secretLimit = 1024;
589
590 std::shared_ptr<SecurePipe> secretPipe;
Ed Tanous168e20c2021-12-13 14:39:53 -0800591 dbus::utility::DbusVariantType unixFd = -1;
Ed Tanous22db1722021-06-09 10:53:51 -0700592
593 if (!userName.empty() || !password.empty())
594 {
595 // Encapsulate in safe buffer
596 CredentialsProvider credentials(std::move(userName),
597 std::move(password));
598
599 // Payload must contain data + NULL delimiters
600 if (credentials.user().size() + credentials.password().size() + 2 >
601 secretLimit)
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100602 {
Ed Tanous22db1722021-06-09 10:53:51 -0700603 BMCWEB_LOG_ERROR << "Credentials too long to handle";
604 messages::unrecognizedRequestBody(asyncResp->res);
605 return;
606 }
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100607
Ed Tanous22db1722021-06-09 10:53:51 -0700608 // Pack secret
609 auto secret = credentials.pack(
610 [](const auto& user, const auto& pass, auto& buff) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700611 std::copy(user.begin(), user.end(), std::back_inserter(buff));
612 buff.push_back('\0');
613 std::copy(pass.begin(), pass.end(), std::back_inserter(buff));
614 buff.push_back('\0');
615 });
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100616
Ed Tanous22db1722021-06-09 10:53:51 -0700617 // Open pipe
618 secretPipe = std::make_shared<SecurePipe>(
619 crow::connections::systemBus->get_io_context(), std::move(secret));
620 unixFd = secretPipe->fd();
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100621
Ed Tanous22db1722021-06-09 10:53:51 -0700622 // Pass secret over pipe
623 secretPipe->asyncWrite(
624 [asyncResp](const boost::system::error_code& ec, std::size_t) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700625 if (ec)
626 {
627 BMCWEB_LOG_ERROR << "Failed to pass secret: " << ec;
628 messages::internalError(asyncResp->res);
629 }
630 });
Ed Tanous22db1722021-06-09 10:53:51 -0700631 }
Adrian Ambrożewicz988fb7b2020-01-13 18:52:46 +0100632
Ed Tanous22db1722021-06-09 10:53:51 -0700633 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800634 [asyncResp, secretPipe](const boost::system::error_code& ec,
Ed Tanous22db1722021-06-09 10:53:51 -0700635 bool success) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700636 if (ec)
637 {
638 BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
639 messages::internalError(asyncResp->res);
640 }
641 else if (!success)
642 {
643 BMCWEB_LOG_ERROR << "Service responded with error";
644 messages::generalError(asyncResp->res);
645 }
Ed Tanous22db1722021-06-09 10:53:51 -0700646 },
647 service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
648 "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw,
649 unixFd);
650}
651
652/**
Przemyslaw Czarnowski120fa862022-06-24 15:10:48 +0200653 * @brief Function validate parameters of insert media request.
654 *
655 */
656inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
657 const std::string& service,
658 const std::string& resName,
659 InsertMediaActionParams& actionParams)
660{
661 BMCWEB_LOG_DEBUG << "Validation started";
662 // required param imageUrl must not be empty
663 if (!actionParams.imageUrl)
664 {
665 BMCWEB_LOG_ERROR << "Request action parameter Image is empty.";
666
667 messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image");
668
669 return;
670 }
671
672 // optional param inserted must be true
673 if ((actionParams.inserted != std::nullopt) && !*actionParams.inserted)
674 {
675 BMCWEB_LOG_ERROR
676 << "Request action optional parameter Inserted must be true.";
677
678 messages::actionParameterNotSupported(asyncResp->res, "Inserted",
679 "InsertMedia");
680
681 return;
682 }
683
684 // optional param transferMethod must be stream
685 if ((actionParams.transferMethod != std::nullopt) &&
686 (*actionParams.transferMethod != "Stream"))
687 {
688 BMCWEB_LOG_ERROR << "Request action optional parameter "
689 "TransferMethod must be Stream.";
690
691 messages::actionParameterNotSupported(asyncResp->res, "TransferMethod",
692 "InsertMedia");
693
694 return;
695 }
696 boost::urls::result<boost::urls::url_view> url =
697 boost::urls::parse_uri(*actionParams.imageUrl);
698 if (!url)
699 {
700 messages::actionParameterValueFormatError(
701 asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia");
702 return;
703 }
704 std::optional<TransferProtocol> uriTransferProtocolType =
705 getTransferProtocolFromUri(*url);
706
707 std::optional<TransferProtocol> paramTransferProtocolType =
708 getTransferProtocolFromParam(actionParams.transferProtocolType);
709
710 // ImageUrl does not contain valid protocol type
711 if (*uriTransferProtocolType == TransferProtocol::invalid)
712 {
713 BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must "
714 "contain specified protocol type from list: "
715 "(smb, https).";
716
717 messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
718
719 return;
720 }
721
722 // transferProtocolType should contain value from list
723 if (*paramTransferProtocolType == TransferProtocol::invalid)
724 {
725 BMCWEB_LOG_ERROR << "Request action parameter TransferProtocolType "
726 "must be provided with value from list: "
727 "(CIFS, HTTPS).";
728
729 messages::propertyValueNotInList(asyncResp->res,
730 *actionParams.transferProtocolType,
731 "TransferProtocolType");
732 return;
733 }
734
735 // valid transfer protocol not provided either with URI nor param
736 if ((uriTransferProtocolType == std::nullopt) &&
737 (paramTransferProtocolType == std::nullopt))
738 {
739 BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must "
740 "contain specified protocol type or param "
741 "TransferProtocolType must be provided.";
742
743 messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
744
745 return;
746 }
747
748 // valid transfer protocol provided both with URI and param
749 if ((paramTransferProtocolType != std::nullopt) &&
750 (uriTransferProtocolType != std::nullopt))
751 {
752 // check if protocol is the same for URI and param
753 if (*paramTransferProtocolType != *uriTransferProtocolType)
754 {
755 BMCWEB_LOG_ERROR << "Request action parameter "
756 "TransferProtocolType must contain the "
757 "same protocol type as protocol type "
758 "provided with param imageUrl.";
759
760 messages::actionParameterValueTypeError(
761 asyncResp->res, *actionParams.transferProtocolType,
762 "TransferProtocolType", "InsertMedia");
763
764 return;
765 }
766 }
767
768 // validation passed, add protocol to URI if needed
769 if (uriTransferProtocolType == std::nullopt)
770 {
771 actionParams.imageUrl = getUriWithTransferProtocol(
772 *actionParams.imageUrl, *paramTransferProtocolType);
773 }
774
Jayaprakash Mutyala452bd8d2023-04-18 12:28:38 +0000775 if (!actionParams.userName)
776 {
777 actionParams.userName = "";
778 }
779
780 if (!actionParams.password)
781 {
782 actionParams.password = "";
783 }
784
Przemyslaw Czarnowski120fa862022-06-24 15:10:48 +0200785 doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
786 !(*actionParams.writeProtected),
787 std::move(*actionParams.userName),
788 std::move(*actionParams.password));
789}
790
791/**
Ed Tanous22db1722021-06-09 10:53:51 -0700792 * @brief Function transceives data with dbus directly.
793 *
794 * All BMC state properties will be retrieved before sending reset request.
795 */
Ed Tanous24e740a2023-02-24 12:08:58 -0800796inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
797 const std::string& service, const std::string& name,
798 bool legacy)
Ed Tanous22db1722021-06-09 10:53:51 -0700799{
Ed Tanous22db1722021-06-09 10:53:51 -0700800 // Legacy mount requires parameter with image
801 if (legacy)
802 {
Adrian Ambrożewiczd6da5be2020-01-13 18:31:01 +0100803 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800804 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700805 if (ec)
806 {
807 BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
Ed Tanous22db1722021-06-09 10:53:51 -0700808
Ed Tanous002d39b2022-05-31 08:59:27 -0700809 messages::internalError(asyncResp->res);
810 return;
811 }
Adrian Ambrożewiczd6da5be2020-01-13 18:31:01 +0100812 },
813 service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
Ed Tanous22db1722021-06-09 10:53:51 -0700814 "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount");
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200815 }
Ed Tanous22db1722021-06-09 10:53:51 -0700816 else // proxy
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200817 {
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +0200818 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800819 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700820 if (ec)
821 {
822 BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
Ed Tanous22db1722021-06-09 10:53:51 -0700823
Ed Tanous002d39b2022-05-31 08:59:27 -0700824 messages::internalError(asyncResp->res);
825 return;
826 }
Ed Tanous22db1722021-06-09 10:53:51 -0700827 },
828 service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name,
829 "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
830 }
831}
832
Ed Tanous96825be2022-06-03 09:43:38 -0700833inline void handleManagersVirtualMediaActionInsertPost(
834 crow::App& app, const crow::Request& req,
835 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
836 const std::string& name, const std::string& resName)
837{
838 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
839 {
840 return;
841 }
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200842
843 constexpr std::string_view action = "VirtualMedia.InsertMedia";
Ed Tanous96825be2022-06-03 09:43:38 -0700844 if (name != "bmc")
845 {
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200846 messages::resourceNotFound(asyncResp->res, action, resName);
Ed Tanous96825be2022-06-03 09:43:38 -0700847
848 return;
849 }
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200850 InsertMediaActionParams actionParams;
Ed Tanous96825be2022-06-03 09:43:38 -0700851
Przemyslaw Czarnowski120fa862022-06-24 15:10:48 +0200852 // Read obligatory parameters (url of image)
Ed Tanous96825be2022-06-03 09:43:38 -0700853 if (!json_util::readJsonAction(
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200854 req, asyncResp->res, "Image", actionParams.imageUrl,
855 "WriteProtected", actionParams.writeProtected, "UserName",
856 actionParams.userName, "Password", actionParams.password,
857 "Inserted", actionParams.inserted, "TransferMethod",
858 actionParams.transferMethod, "TransferProtocolType",
859 actionParams.transferProtocolType))
Ed Tanous96825be2022-06-03 09:43:38 -0700860 {
861 return;
862 }
863
George Liu2b731192023-01-11 16:27:13 +0800864 dbus::utility::getDbusObject(
865 "/xyz/openbmc_project/VirtualMedia", {},
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200866 [asyncResp, action, actionParams,
George Liu2b731192023-01-11 16:27:13 +0800867 resName](const boost::system::error_code& ec,
Ed Tanous96825be2022-06-03 09:43:38 -0700868 const dbus::utility::MapperGetObject& getObjectType) mutable {
869 if (ec)
870 {
871 BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200872 messages::resourceNotFound(asyncResp->res, action, resName);
Ed Tanous96825be2022-06-03 09:43:38 -0700873
874 return;
875 }
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200876
Ed Tanous96825be2022-06-03 09:43:38 -0700877 std::string service = getObjectType.begin()->first;
878 BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
879
880 crow::connections::systemBus->async_method_call(
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200881 [service, resName, action, actionParams,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800882 asyncResp](const boost::system::error_code& ec2,
Ed Tanous96825be2022-06-03 09:43:38 -0700883 dbus::utility::ManagedObjectType& subtree) mutable {
Ed Tanous8a592812022-06-04 09:06:59 -0700884 if (ec2)
Ed Tanous96825be2022-06-03 09:43:38 -0700885 {
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200886 // Not possible in proxy mode
887 BMCWEB_LOG_DEBUG << "InsertMedia not "
888 "allowed in proxy mode";
889 messages::resourceNotFound(asyncResp->res, action, resName);
Ed Tanous96825be2022-06-03 09:43:38 -0700890
891 return;
892 }
Ed Tanous96825be2022-06-03 09:43:38 -0700893 for (const auto& object : subtree)
894 {
Ed Tanous365a73f2023-02-24 12:16:49 -0800895 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
Boleslaw Ogonczyk Makowski5880f0c2023-04-14 15:32:40 +0200896 if (mode == VmMode::Legacy)
Ed Tanous96825be2022-06-03 09:43:38 -0700897 {
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200898 validateParams(asyncResp, service, resName, actionParams);
Ed Tanous96825be2022-06-03 09:43:38 -0700899
900 return;
901 }
902 }
903 BMCWEB_LOG_DEBUG << "Parent item not found";
904 messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
905 },
906 service, "/xyz/openbmc_project/VirtualMedia",
907 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
George Liu2b731192023-01-11 16:27:13 +0800908 });
Ed Tanous96825be2022-06-03 09:43:38 -0700909}
910
911inline void handleManagersVirtualMediaActionEject(
912 crow::App& app, const crow::Request& req,
913 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
914 const std::string& managerName, const std::string& resName)
915{
916 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
917 {
918 return;
919 }
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200920
921 constexpr std::string_view action = "VirtualMedia.EjectMedia";
Ed Tanous96825be2022-06-03 09:43:38 -0700922 if (managerName != "bmc")
923 {
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200924 messages::resourceNotFound(asyncResp->res, action, resName);
Ed Tanous96825be2022-06-03 09:43:38 -0700925
926 return;
927 }
928
George Liu2b731192023-01-11 16:27:13 +0800929 dbus::utility::getDbusObject(
930 "/xyz/openbmc_project/VirtualMedia", {},
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200931 [asyncResp, action,
George Liu2b731192023-01-11 16:27:13 +0800932 resName](const boost::system::error_code& ec2,
Ed Tanous96825be2022-06-03 09:43:38 -0700933 const dbus::utility::MapperGetObject& getObjectType) {
Ed Tanous8a592812022-06-04 09:06:59 -0700934 if (ec2)
Ed Tanous96825be2022-06-03 09:43:38 -0700935 {
Ed Tanous8a592812022-06-04 09:06:59 -0700936 BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec2;
Ed Tanous96825be2022-06-03 09:43:38 -0700937 messages::internalError(asyncResp->res);
938
939 return;
940 }
941 std::string service = getObjectType.begin()->first;
942 BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
943
944 crow::connections::systemBus->async_method_call(
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200945 [resName, service, action,
946 asyncResp](const boost::system::error_code& ec,
947 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous96825be2022-06-03 09:43:38 -0700948 if (ec)
949 {
Przemyslaw Czarnowski79fdf632022-06-28 18:11:59 +0200950 BMCWEB_LOG_ERROR << "ObjectMapper : No Service found";
951 messages::resourceNotFound(asyncResp->res, action, resName);
Ed Tanous96825be2022-06-03 09:43:38 -0700952 return;
953 }
954
955 for (const auto& object : subtree)
956 {
Ed Tanous365a73f2023-02-24 12:16:49 -0800957 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
958 if (mode != VmMode::Invalid)
Ed Tanous96825be2022-06-03 09:43:38 -0700959 {
Ed Tanous365a73f2023-02-24 12:16:49 -0800960 doEjectAction(asyncResp, service, resName,
961 mode == VmMode::Legacy);
Boleslaw Ogonczyk Makowski5880f0c2023-04-14 15:32:40 +0200962 return;
Ed Tanous96825be2022-06-03 09:43:38 -0700963 }
964 }
965 BMCWEB_LOG_DEBUG << "Parent item not found";
966 messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
967 },
968 service, "/xyz/openbmc_project/VirtualMedia",
969 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
George Liu2b731192023-01-11 16:27:13 +0800970 });
Ed Tanous96825be2022-06-03 09:43:38 -0700971}
972
973inline void handleManagersVirtualMediaCollectionGet(
974 crow::App& app, const crow::Request& req,
975 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
976 const std::string& name)
977{
978 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
979 {
980 return;
981 }
982 if (name != "bmc")
983 {
984 messages::resourceNotFound(asyncResp->res, "VirtualMedia", name);
985
986 return;
987 }
988
989 asyncResp->res.jsonValue["@odata.type"] =
990 "#VirtualMediaCollection.VirtualMediaCollection";
991 asyncResp->res.jsonValue["Name"] = "Virtual Media Services";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700992 asyncResp->res.jsonValue["@odata.id"] =
993 boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia", name);
Ed Tanous96825be2022-06-03 09:43:38 -0700994
George Liu2b731192023-01-11 16:27:13 +0800995 dbus::utility::getDbusObject(
996 "/xyz/openbmc_project/VirtualMedia", {},
997 [asyncResp, name](const boost::system::error_code& ec,
Ed Tanous96825be2022-06-03 09:43:38 -0700998 const dbus::utility::MapperGetObject& getObjectType) {
999 if (ec)
1000 {
1001 BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
1002 messages::internalError(asyncResp->res);
1003
1004 return;
1005 }
1006 std::string service = getObjectType.begin()->first;
1007 BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
1008
1009 getVmResourceList(asyncResp, service, name);
George Liu2b731192023-01-11 16:27:13 +08001010 });
Ed Tanous96825be2022-06-03 09:43:38 -07001011}
1012
1013inline void
1014 handleVirtualMediaGet(crow::App& app, const crow::Request& req,
1015 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1016 const std::string& name, const std::string& resName)
1017{
1018 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1019 {
1020 return;
1021 }
1022 if (name != "bmc")
1023 {
1024 messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
1025
1026 return;
1027 }
1028
George Liu2b731192023-01-11 16:27:13 +08001029 dbus::utility::getDbusObject(
1030 "/xyz/openbmc_project/VirtualMedia", {},
Ed Tanous96825be2022-06-03 09:43:38 -07001031 [asyncResp, name,
George Liu2b731192023-01-11 16:27:13 +08001032 resName](const boost::system::error_code& ec,
Ed Tanous96825be2022-06-03 09:43:38 -07001033 const dbus::utility::MapperGetObject& getObjectType) {
1034 if (ec)
1035 {
1036 BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
1037 messages::internalError(asyncResp->res);
1038
1039 return;
1040 }
1041 std::string service = getObjectType.begin()->first;
1042 BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
1043
1044 getVmData(asyncResp, service, name, resName);
George Liu2b731192023-01-11 16:27:13 +08001045 });
Ed Tanous96825be2022-06-03 09:43:38 -07001046}
1047
Ed Tanous22db1722021-06-09 10:53:51 -07001048inline void requestNBDVirtualMediaRoutes(App& app)
1049{
George Liu0fda0f12021-11-16 10:06:17 +08001050 BMCWEB_ROUTE(
1051 app,
1052 "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia")
Ed Tanoused398212021-06-09 17:05:54 -07001053 .privileges(redfish::privileges::postVirtualMedia)
Ed Tanous96825be2022-06-03 09:43:38 -07001054 .methods(boost::beast::http::verb::post)(std::bind_front(
1055 handleManagersVirtualMediaActionInsertPost, std::ref(app)));
Przemyslaw Czarnowskie13c2762019-09-02 17:32:43 +02001056
George Liu0fda0f12021-11-16 10:06:17 +08001057 BMCWEB_ROUTE(
1058 app,
1059 "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia")
Ed Tanoused398212021-06-09 17:05:54 -07001060 .privileges(redfish::privileges::postVirtualMedia)
Ed Tanous96825be2022-06-03 09:43:38 -07001061 .methods(boost::beast::http::verb::post)(std::bind_front(
1062 handleManagersVirtualMediaActionEject, std::ref(app)));
Ed Tanous002d39b2022-05-31 08:59:27 -07001063
Ed Tanous22db1722021-06-09 10:53:51 -07001064 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/")
Ed Tanoused398212021-06-09 17:05:54 -07001065 .privileges(redfish::privileges::getVirtualMediaCollection)
Ed Tanous96825be2022-06-03 09:43:38 -07001066 .methods(boost::beast::http::verb::get)(std::bind_front(
1067 handleManagersVirtualMediaCollectionGet, std::ref(app)));
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +02001068
Ed Tanous22db1722021-06-09 10:53:51 -07001069 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001070 .privileges(redfish::privileges::getVirtualMedia)
Ed Tanous22db1722021-06-09 10:53:51 -07001071 .methods(boost::beast::http::verb::get)(
Ed Tanous96825be2022-06-03 09:43:38 -07001072 std::bind_front(handleVirtualMediaGet, std::ref(app)));
Ed Tanous22db1722021-06-09 10:53:51 -07001073}
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +02001074
1075} // namespace redfish