xref: /openbmc/bmcweb/features/redfish/lib/virtual_media.hpp (revision 4a7fbefdff330f06d5698a1e60ce893225cd389e)
1107077deSPrzemyslaw Czarnowski /*
2107077deSPrzemyslaw Czarnowski // Copyright (c) 2018 Intel Corporation
3107077deSPrzemyslaw Czarnowski //
4107077deSPrzemyslaw Czarnowski // Licensed under the Apache License, Version 2.0 (the "License");
5107077deSPrzemyslaw Czarnowski // you may not use this file except in compliance with the License.
6107077deSPrzemyslaw Czarnowski // You may obtain a copy of the License at
7107077deSPrzemyslaw Czarnowski //
8107077deSPrzemyslaw Czarnowski //      http://www.apache.org/licenses/LICENSE-2.0
9107077deSPrzemyslaw Czarnowski //
10107077deSPrzemyslaw Czarnowski // Unless required by applicable law or agreed to in writing, software
11107077deSPrzemyslaw Czarnowski // distributed under the License is distributed on an "AS IS" BASIS,
12107077deSPrzemyslaw Czarnowski // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13107077deSPrzemyslaw Czarnowski // See the License for the specific language governing permissions and
14107077deSPrzemyslaw Czarnowski // limitations under the License.
15107077deSPrzemyslaw Czarnowski */
16107077deSPrzemyslaw Czarnowski #pragma once
17107077deSPrzemyslaw Czarnowski 
183ccb3adbSEd Tanous #include "account_service.hpp"
193ccb3adbSEd Tanous #include "app.hpp"
2079fdf63eSPrzemyslaw Czarnowski #include "async_resp.hpp"
2111e8f60dSEd Tanous #include "credential_pipe.hpp"
222b73119cSGeorge Liu #include "dbus_utility.hpp"
23739b87efSEd Tanous #include "generated/enums/virtual_media.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
253ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/json_utils.hpp"
273ccb3adbSEd Tanous 
28988fb7b2SAdrian Ambrożewicz #include <boost/process/async_pipe.hpp>
29ef4c65b7SEd Tanous #include <boost/url/format.hpp>
309e319cf0SAnna Platash #include <boost/url/url_view.hpp>
31*4a7fbefdSEd Tanous #include <boost/url/url_view_base.hpp>
32107077deSPrzemyslaw Czarnowski 
332b73119cSGeorge Liu #include <array>
343544d2a7SEd Tanous #include <ranges>
352b73119cSGeorge Liu #include <string_view>
362b73119cSGeorge Liu 
37107077deSPrzemyslaw Czarnowski namespace redfish
38107077deSPrzemyslaw Czarnowski {
39365a73f4SEd Tanous 
40365a73f4SEd Tanous enum class VmMode
41365a73f4SEd Tanous {
42365a73f4SEd Tanous     Invalid,
43365a73f4SEd Tanous     Legacy,
44365a73f4SEd Tanous     Proxy
45365a73f4SEd Tanous };
46365a73f4SEd Tanous 
47365a73f4SEd Tanous inline VmMode
48365a73f4SEd Tanous     parseObjectPathAndGetMode(const sdbusplus::message::object_path& itemPath,
49365a73f4SEd Tanous                               const std::string& resName)
50365a73f4SEd Tanous {
51365a73f4SEd Tanous     std::string thisPath = itemPath.filename();
5262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Filename: {}, ThisPath: {}", itemPath.str, thisPath);
53365a73f4SEd Tanous 
54365a73f4SEd Tanous     if (thisPath.empty())
55365a73f4SEd Tanous     {
56365a73f4SEd Tanous         return VmMode::Invalid;
57365a73f4SEd Tanous     }
58365a73f4SEd Tanous 
59365a73f4SEd Tanous     if (thisPath != resName)
60365a73f4SEd Tanous     {
61365a73f4SEd Tanous         return VmMode::Invalid;
62365a73f4SEd Tanous     }
63365a73f4SEd Tanous 
64365a73f4SEd Tanous     auto mode = itemPath.parent_path();
65365a73f4SEd Tanous     auto type = mode.parent_path();
66365a73f4SEd Tanous 
67365a73f4SEd Tanous     if (mode.filename().empty() || type.filename().empty())
68365a73f4SEd Tanous     {
69365a73f4SEd Tanous         return VmMode::Invalid;
70365a73f4SEd Tanous     }
71365a73f4SEd Tanous 
72365a73f4SEd Tanous     if (type.filename() != "VirtualMedia")
73365a73f4SEd Tanous     {
74365a73f4SEd Tanous         return VmMode::Invalid;
75365a73f4SEd Tanous     }
76365a73f4SEd Tanous     std::string modeStr = mode.filename();
77365a73f4SEd Tanous     if (modeStr == "Legacy")
78365a73f4SEd Tanous     {
79365a73f4SEd Tanous         return VmMode::Legacy;
80365a73f4SEd Tanous     }
81365a73f4SEd Tanous     if (modeStr == "Proxy")
82365a73f4SEd Tanous     {
83365a73f4SEd Tanous         return VmMode::Proxy;
84365a73f4SEd Tanous     }
85365a73f4SEd Tanous     return VmMode::Invalid;
86365a73f4SEd Tanous }
87365a73f4SEd Tanous 
8879fdf63eSPrzemyslaw Czarnowski using CheckItemHandler =
8979fdf63eSPrzemyslaw Czarnowski     std::function<void(const std::string& service, const std::string& resName,
9079fdf63eSPrzemyslaw Czarnowski                        const std::shared_ptr<bmcweb::AsyncResp>&,
9170cbdf53SGeorge Liu                        const std::pair<sdbusplus::message::object_path,
9280f79a40SMichael Shen                                        dbus::utility::DBusInterfacesMap>&)>;
9379fdf63eSPrzemyslaw Czarnowski 
94ac106bf6SEd Tanous inline void
95ac106bf6SEd Tanous     findAndParseObject(const std::string& service, const std::string& resName,
96ac106bf6SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9779fdf63eSPrzemyslaw Czarnowski                        CheckItemHandler&& handler)
9879fdf63eSPrzemyslaw Czarnowski {
995eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia");
1005eb468daSGeorge Liu     dbus::utility::getManagedObjects(
1015eb468daSGeorge Liu         service, path,
1028cb2c024SEd Tanous         [service, resName, asyncResp, handler = std::move(handler)](
1038cb2c024SEd Tanous             const boost::system::error_code& ec,
10470cbdf53SGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
10579fdf63eSPrzemyslaw Czarnowski         if (ec)
10679fdf63eSPrzemyslaw Czarnowski         {
10762598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error");
10879fdf63eSPrzemyslaw Czarnowski 
10979fdf63eSPrzemyslaw Czarnowski             return;
11079fdf63eSPrzemyslaw Czarnowski         }
11179fdf63eSPrzemyslaw Czarnowski 
11270cbdf53SGeorge Liu         for (const auto& item : subtree)
11379fdf63eSPrzemyslaw Czarnowski         {
11479fdf63eSPrzemyslaw Czarnowski             VmMode mode = parseObjectPathAndGetMode(item.first, resName);
11579fdf63eSPrzemyslaw Czarnowski             if (mode != VmMode::Invalid)
11679fdf63eSPrzemyslaw Czarnowski             {
117ac106bf6SEd Tanous                 handler(service, resName, asyncResp, item);
11879fdf63eSPrzemyslaw Czarnowski                 return;
11979fdf63eSPrzemyslaw Czarnowski             }
12079fdf63eSPrzemyslaw Czarnowski         }
12179fdf63eSPrzemyslaw Czarnowski 
12262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Parent item not found");
123ac106bf6SEd Tanous         asyncResp->res.result(boost::beast::http::status::not_found);
1245eb468daSGeorge Liu     });
12579fdf63eSPrzemyslaw Czarnowski }
12679fdf63eSPrzemyslaw Czarnowski 
1279e319cf0SAnna Platash /**
1289e319cf0SAnna Platash  * @brief Function extracts transfer protocol name from URI.
1299e319cf0SAnna Platash  */
13067df073bSEd Tanous inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri)
13167df073bSEd Tanous {
1326fd29553SEd Tanous     boost::system::result<boost::urls::url_view> url =
133079360aeSEd Tanous         boost::urls::parse_uri(imageUri);
13467df073bSEd Tanous     if (!url)
13567df073bSEd Tanous     {
13667df073bSEd Tanous         return "None";
13767df073bSEd Tanous     }
138079360aeSEd Tanous     std::string_view scheme = url->scheme();
13967df073bSEd Tanous     if (scheme == "smb")
14067df073bSEd Tanous     {
14167df073bSEd Tanous         return "CIFS";
14267df073bSEd Tanous     }
14367df073bSEd Tanous     if (scheme == "https")
14467df073bSEd Tanous     {
14567df073bSEd Tanous         return "HTTPS";
14667df073bSEd Tanous     }
14767df073bSEd Tanous 
14867df073bSEd Tanous     return "None";
14967df073bSEd Tanous }
150107077deSPrzemyslaw Czarnowski 
151107077deSPrzemyslaw Czarnowski /**
152107077deSPrzemyslaw Czarnowski  * @brief Read all known properties from VM object interfaces
153107077deSPrzemyslaw Czarnowski  */
15422db1728SEd Tanous inline void
15580f79a40SMichael Shen     vmParseInterfaceObject(const dbus::utility::DBusInterfacesMap& interfaces,
156ac106bf6SEd Tanous                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
157107077deSPrzemyslaw Czarnowski {
1588a592810SEd Tanous     for (const auto& [interface, values] : interfaces)
159107077deSPrzemyslaw Czarnowski     {
160711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint")
161107077deSPrzemyslaw Czarnowski         {
162711ac7a9SEd Tanous             for (const auto& [property, value] : values)
163107077deSPrzemyslaw Czarnowski             {
164711ac7a9SEd Tanous                 if (property == "EndpointId")
165107077deSPrzemyslaw Czarnowski                 {
166107077deSPrzemyslaw Czarnowski                     const std::string* endpointIdValue =
167711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
168711ac7a9SEd Tanous                     if (endpointIdValue == nullptr)
169107077deSPrzemyslaw Czarnowski                     {
170711ac7a9SEd Tanous                         continue;
171711ac7a9SEd Tanous                     }
172107077deSPrzemyslaw Czarnowski                     if (!endpointIdValue->empty())
173107077deSPrzemyslaw Czarnowski                     {
174107077deSPrzemyslaw Czarnowski                         // Proxy mode
175ac106bf6SEd Tanous                         asyncResp->res
176711ac7a9SEd Tanous                             .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] =
177d04ba325SPrzemyslaw Czarnowski                             *endpointIdValue;
178ac106bf6SEd Tanous                         asyncResp->res.jsonValue["TransferProtocolType"] =
179ac106bf6SEd Tanous                             "OEM";
180107077deSPrzemyslaw Czarnowski                     }
181107077deSPrzemyslaw Czarnowski                 }
182711ac7a9SEd Tanous                 if (property == "ImageURL")
183107077deSPrzemyslaw Czarnowski                 {
184107077deSPrzemyslaw Czarnowski                     const std::string* imageUrlValue =
185711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
18626f6976fSEd Tanous                     if (imageUrlValue != nullptr && !imageUrlValue->empty())
187107077deSPrzemyslaw Czarnowski                     {
188da4784d8SPrzemyslaw Czarnowski                         std::filesystem::path filePath = *imageUrlValue;
189da4784d8SPrzemyslaw Czarnowski                         if (!filePath.has_filename())
190da4784d8SPrzemyslaw Czarnowski                         {
1919e319cf0SAnna Platash                             // this will handle https share, which not
1929e319cf0SAnna Platash                             // necessarily has to have filename given.
193ac106bf6SEd Tanous                             asyncResp->res.jsonValue["ImageName"] = "";
194da4784d8SPrzemyslaw Czarnowski                         }
195da4784d8SPrzemyslaw Czarnowski                         else
196da4784d8SPrzemyslaw Czarnowski                         {
197ac106bf6SEd Tanous                             asyncResp->res.jsonValue["ImageName"] =
1989e319cf0SAnna Platash                                 filePath.filename();
199da4784d8SPrzemyslaw Czarnowski                         }
200da4784d8SPrzemyslaw Czarnowski 
201ac106bf6SEd Tanous                         asyncResp->res.jsonValue["Image"] = *imageUrlValue;
202ac106bf6SEd Tanous                         asyncResp->res.jsonValue["TransferProtocolType"] =
2039e319cf0SAnna Platash                             getTransferProtocolTypeFromUri(*imageUrlValue);
2049e319cf0SAnna Platash 
205ac106bf6SEd Tanous                         asyncResp->res.jsonValue["ConnectedVia"] =
206739b87efSEd Tanous                             virtual_media::ConnectedVia::URI;
207107077deSPrzemyslaw Czarnowski                     }
208107077deSPrzemyslaw Czarnowski                 }
209711ac7a9SEd Tanous                 if (property == "WriteProtected")
2109e319cf0SAnna Platash                 {
211711ac7a9SEd Tanous                     const bool* writeProtectedValue = std::get_if<bool>(&value);
212e662eae8SEd Tanous                     if (writeProtectedValue != nullptr)
2139e319cf0SAnna Platash                     {
214ac106bf6SEd Tanous                         asyncResp->res.jsonValue["WriteProtected"] =
2159e319cf0SAnna Platash                             *writeProtectedValue;
2169e319cf0SAnna Platash                     }
2179e319cf0SAnna Platash                 }
2189e319cf0SAnna Platash             }
219107077deSPrzemyslaw Czarnowski         }
220711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.Process")
221711ac7a9SEd Tanous         {
222711ac7a9SEd Tanous             for (const auto& [property, value] : values)
223711ac7a9SEd Tanous             {
224711ac7a9SEd Tanous                 if (property == "Active")
225711ac7a9SEd Tanous                 {
226711ac7a9SEd Tanous                     const bool* activeValue = std::get_if<bool>(&value);
227e662eae8SEd Tanous                     if (activeValue == nullptr)
228711ac7a9SEd Tanous                     {
22962598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Value Active not found");
230711ac7a9SEd Tanous                         return;
231711ac7a9SEd Tanous                     }
232ac106bf6SEd Tanous                     asyncResp->res.jsonValue["Inserted"] = *activeValue;
233711ac7a9SEd Tanous 
234e05aec50SEd Tanous                     if (*activeValue)
235711ac7a9SEd Tanous                     {
236ac106bf6SEd Tanous                         asyncResp->res.jsonValue["ConnectedVia"] =
237739b87efSEd Tanous                             virtual_media::ConnectedVia::Applet;
238711ac7a9SEd Tanous                     }
239711ac7a9SEd Tanous                 }
240711ac7a9SEd Tanous             }
241711ac7a9SEd Tanous         }
242107077deSPrzemyslaw Czarnowski     }
243107077deSPrzemyslaw Czarnowski }
244107077deSPrzemyslaw Czarnowski 
245107077deSPrzemyslaw Czarnowski /**
246107077deSPrzemyslaw Czarnowski  * @brief Fill template for Virtual Media Item.
247107077deSPrzemyslaw Czarnowski  */
24822db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name,
249107077deSPrzemyslaw Czarnowski                                      const std::string& resName)
250107077deSPrzemyslaw Czarnowski {
251107077deSPrzemyslaw Czarnowski     nlohmann::json item;
252ef4c65b7SEd Tanous     item["@odata.id"] = boost::urls::format(
253ef4c65b7SEd Tanous         "/redfish/v1/Managers/{}/VirtualMedia/{}", name, resName);
25422db1728SEd Tanous 
255d04ba325SPrzemyslaw Czarnowski     item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia";
256107077deSPrzemyslaw Czarnowski     item["Name"] = "Virtual Removable Media";
257107077deSPrzemyslaw Czarnowski     item["Id"] = resName;
258107077deSPrzemyslaw Czarnowski     item["WriteProtected"] = true;
259739b87efSEd Tanous     item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected;
260613dabeaSEd Tanous     item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"});
261107077deSPrzemyslaw Czarnowski     item["TransferMethod"] = "Stream";
262d04ba325SPrzemyslaw Czarnowski     item["Oem"]["OpenBMC"]["@odata.type"] =
263d04ba325SPrzemyslaw Czarnowski         "#OemVirtualMedia.v1_0_0.VirtualMedia";
26415b89725SV-Sanjana     item["Oem"]["OpenBMC"]["@odata.id"] = boost::urls::format(
26515b89725SV-Sanjana         "/redfish/v1/Managers/{}/VirtualMedia/{}#/Oem/OpenBMC", name, resName);
266107077deSPrzemyslaw Czarnowski 
267107077deSPrzemyslaw Czarnowski     return item;
268107077deSPrzemyslaw Czarnowski }
269107077deSPrzemyslaw Czarnowski 
270107077deSPrzemyslaw Czarnowski /**
271107077deSPrzemyslaw Czarnowski  *  @brief Fills collection data
272107077deSPrzemyslaw Czarnowski  */
273ac106bf6SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
274107077deSPrzemyslaw Czarnowski                               const std::string& service,
275107077deSPrzemyslaw Czarnowski                               const std::string& name)
276107077deSPrzemyslaw Czarnowski {
27762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get available Virtual Media resources.");
2785eb468daSGeorge Liu     sdbusplus::message::object_path objPath(
2795eb468daSGeorge Liu         "/xyz/openbmc_project/VirtualMedia");
2805eb468daSGeorge Liu     dbus::utility::getManagedObjects(
2815eb468daSGeorge Liu         service, objPath,
282ac106bf6SEd Tanous         [name, asyncResp{std::move(asyncResp)}](
2835e7e2dc5SEd Tanous             const boost::system::error_code& ec,
28402cad96eSEd Tanous             const dbus::utility::ManagedObjectType& subtree) {
285107077deSPrzemyslaw Czarnowski         if (ec)
286107077deSPrzemyslaw Czarnowski         {
28762598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error");
288107077deSPrzemyslaw Czarnowski             return;
289107077deSPrzemyslaw Czarnowski         }
290ac106bf6SEd Tanous         nlohmann::json& members = asyncResp->res.jsonValue["Members"];
291107077deSPrzemyslaw Czarnowski         members = nlohmann::json::array();
292107077deSPrzemyslaw Czarnowski 
293107077deSPrzemyslaw Czarnowski         for (const auto& object : subtree)
294107077deSPrzemyslaw Czarnowski         {
295107077deSPrzemyslaw Czarnowski             nlohmann::json item;
2962dfd18efSEd Tanous             std::string path = object.first.filename();
2972dfd18efSEd Tanous             if (path.empty())
298107077deSPrzemyslaw Czarnowski             {
299107077deSPrzemyslaw Czarnowski                 continue;
300107077deSPrzemyslaw Czarnowski             }
301107077deSPrzemyslaw Czarnowski 
302ef4c65b7SEd Tanous             item["@odata.id"] = boost::urls::format(
303ef4c65b7SEd Tanous                 "/redfish/v1/Managers/{}/VirtualMedia/{}", name, path);
304107077deSPrzemyslaw Czarnowski             members.emplace_back(std::move(item));
305107077deSPrzemyslaw Czarnowski         }
306ac106bf6SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = members.size();
3075eb468daSGeorge Liu     });
308107077deSPrzemyslaw Czarnowski }
309107077deSPrzemyslaw Czarnowski 
31070cbdf53SGeorge Liu inline void
31170cbdf53SGeorge Liu     afterGetVmData(const std::string& name, const std::string& /*service*/,
31279fdf63eSPrzemyslaw Czarnowski                    const std::string& resName,
31379fdf63eSPrzemyslaw Czarnowski                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31470cbdf53SGeorge Liu                    const std::pair<sdbusplus::message::object_path,
31580f79a40SMichael Shen                                    dbus::utility::DBusInterfacesMap>& item)
31679fdf63eSPrzemyslaw Czarnowski {
31779fdf63eSPrzemyslaw Czarnowski     VmMode mode = parseObjectPathAndGetMode(item.first, resName);
31879fdf63eSPrzemyslaw Czarnowski     if (mode == VmMode::Invalid)
31979fdf63eSPrzemyslaw Czarnowski     {
32079fdf63eSPrzemyslaw Czarnowski         return;
32179fdf63eSPrzemyslaw Czarnowski     }
32279fdf63eSPrzemyslaw Czarnowski 
32379fdf63eSPrzemyslaw Czarnowski     asyncResp->res.jsonValue = vmItemTemplate(name, resName);
32479fdf63eSPrzemyslaw Czarnowski 
32579fdf63eSPrzemyslaw Czarnowski     // Check if dbus path is Legacy type
32679fdf63eSPrzemyslaw Czarnowski     if (mode == VmMode::Legacy)
32779fdf63eSPrzemyslaw Czarnowski     {
328ef4c65b7SEd Tanous         asyncResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"]
329ef4c65b7SEd Tanous                                 ["target"] = boost::urls::format(
330ef4c65b7SEd Tanous             "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.InsertMedia",
331ef4c65b7SEd Tanous             name, resName);
33279fdf63eSPrzemyslaw Czarnowski     }
33379fdf63eSPrzemyslaw Czarnowski 
33479fdf63eSPrzemyslaw Czarnowski     vmParseInterfaceObject(item.second, asyncResp);
33579fdf63eSPrzemyslaw Czarnowski 
336ef4c65b7SEd Tanous     asyncResp->res.jsonValue["Actions"]["#VirtualMedia.EjectMedia"]
337ef4c65b7SEd Tanous                             ["target"] = boost::urls::format(
338ef4c65b7SEd Tanous         "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.EjectMedia",
339ef4c65b7SEd Tanous         name, resName);
34079fdf63eSPrzemyslaw Czarnowski }
34179fdf63eSPrzemyslaw Czarnowski 
342107077deSPrzemyslaw Czarnowski /**
343107077deSPrzemyslaw Czarnowski  *  @brief Fills data for specific resource
344107077deSPrzemyslaw Czarnowski  */
345ac106bf6SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
346107077deSPrzemyslaw Czarnowski                       const std::string& service, const std::string& name,
347107077deSPrzemyslaw Czarnowski                       const std::string& resName)
348107077deSPrzemyslaw Czarnowski {
34962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Virtual Media resource data.");
350107077deSPrzemyslaw Czarnowski 
351ac106bf6SEd Tanous     findAndParseObject(service, resName, asyncResp,
35270cbdf53SGeorge Liu                        std::bind_front(afterGetVmData, name));
353107077deSPrzemyslaw Czarnowski }
354107077deSPrzemyslaw Czarnowski 
355e13c2760SPrzemyslaw Czarnowski /**
356c6f4e017SAgata Olender  * @brief Transfer protocols supported for InsertMedia action.
357c6f4e017SAgata Olender  *
358c6f4e017SAgata Olender  */
359c6f4e017SAgata Olender enum class TransferProtocol
360c6f4e017SAgata Olender {
361c6f4e017SAgata Olender     https,
362c6f4e017SAgata Olender     smb,
363c6f4e017SAgata Olender     invalid
364c6f4e017SAgata Olender };
365c6f4e017SAgata Olender 
366c6f4e017SAgata Olender /**
367c6f4e017SAgata Olender  * @brief Function extracts transfer protocol type from URI.
368c6f4e017SAgata Olender  *
369c6f4e017SAgata Olender  */
37067df073bSEd Tanous inline std::optional<TransferProtocol>
371*4a7fbefdSEd Tanous     getTransferProtocolFromUri(const boost::urls::url_view_base& imageUri)
37267df073bSEd Tanous {
373079360aeSEd Tanous     std::string_view scheme = imageUri.scheme();
37467df073bSEd Tanous     if (scheme == "smb")
37567df073bSEd Tanous     {
37667df073bSEd Tanous         return TransferProtocol::smb;
37767df073bSEd Tanous     }
37867df073bSEd Tanous     if (scheme == "https")
37967df073bSEd Tanous     {
38067df073bSEd Tanous         return TransferProtocol::https;
38167df073bSEd Tanous     }
38267df073bSEd Tanous     if (!scheme.empty())
38367df073bSEd Tanous     {
38467df073bSEd Tanous         return TransferProtocol::invalid;
38567df073bSEd Tanous     }
38667df073bSEd Tanous 
38767df073bSEd Tanous     return {};
38867df073bSEd Tanous }
389c6f4e017SAgata Olender 
390c6f4e017SAgata Olender /**
391c6f4e017SAgata Olender  * @brief Function convert transfer protocol from string param.
392c6f4e017SAgata Olender  *
393c6f4e017SAgata Olender  */
39422db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam(
395c6f4e017SAgata Olender     const std::optional<std::string>& transferProtocolType)
396c6f4e017SAgata Olender {
397e01d0c36SEd Tanous     if (!transferProtocolType)
398c6f4e017SAgata Olender     {
399c6f4e017SAgata Olender         return {};
400c6f4e017SAgata Olender     }
401c6f4e017SAgata Olender 
402c6f4e017SAgata Olender     if (*transferProtocolType == "CIFS")
403c6f4e017SAgata Olender     {
404c6f4e017SAgata Olender         return TransferProtocol::smb;
405c6f4e017SAgata Olender     }
406c6f4e017SAgata Olender 
407c6f4e017SAgata Olender     if (*transferProtocolType == "HTTPS")
408c6f4e017SAgata Olender     {
409c6f4e017SAgata Olender         return TransferProtocol::https;
410c6f4e017SAgata Olender     }
411c6f4e017SAgata Olender 
412c6f4e017SAgata Olender     return TransferProtocol::invalid;
413c6f4e017SAgata Olender }
414c6f4e017SAgata Olender 
415c6f4e017SAgata Olender /**
416c6f4e017SAgata Olender  * @brief Function extends URI with transfer protocol type.
417c6f4e017SAgata Olender  *
418c6f4e017SAgata Olender  */
41922db1728SEd Tanous inline std::string
420c6f4e017SAgata Olender     getUriWithTransferProtocol(const std::string& imageUri,
421c6f4e017SAgata Olender                                const TransferProtocol& transferProtocol)
422c6f4e017SAgata Olender {
423c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::smb)
424c6f4e017SAgata Olender     {
425c6f4e017SAgata Olender         return "smb://" + imageUri;
426c6f4e017SAgata Olender     }
427c6f4e017SAgata Olender 
428c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::https)
429c6f4e017SAgata Olender     {
430c6f4e017SAgata Olender         return "https://" + imageUri;
431c6f4e017SAgata Olender     }
432c6f4e017SAgata Olender 
433c6f4e017SAgata Olender     return imageUri;
434c6f4e017SAgata Olender }
435c6f4e017SAgata Olender 
4361f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams
4371f2a40ceSPrzemyslaw Czarnowski {
438120fa86aSPrzemyslaw Czarnowski     std::optional<std::string> imageUrl;
4391f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> userName;
4401f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> password;
4411f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferMethod;
4421f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferProtocolType;
4431f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> writeProtected = true;
4441f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> inserted;
4451f2a40ceSPrzemyslaw Czarnowski };
4461f2a40ceSPrzemyslaw Czarnowski 
447e13c2760SPrzemyslaw Czarnowski /**
448e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
449e13c2760SPrzemyslaw Czarnowski  *
450e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
451e13c2760SPrzemyslaw Czarnowski  */
45222db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
453e13c2760SPrzemyslaw Czarnowski                             const std::string& service, const std::string& name,
45411e8f60dSEd Tanous                             const std::string& imageUrl, bool rw,
455988fb7b2SAdrian Ambrożewicz                             std::string&& userName, std::string&& password)
456e13c2760SPrzemyslaw Czarnowski {
45711e8f60dSEd Tanous     int fd = -1;
45811e8f60dSEd Tanous     std::shared_ptr<CredentialsPipe> secretPipe;
459988fb7b2SAdrian Ambrożewicz     if (!userName.empty() || !password.empty())
460988fb7b2SAdrian Ambrożewicz     {
461988fb7b2SAdrian Ambrożewicz         // Payload must contain data + NULL delimiters
46211e8f60dSEd Tanous         constexpr const size_t secretLimit = 1024;
46311e8f60dSEd Tanous         if (userName.size() + password.size() + 2 > secretLimit)
464988fb7b2SAdrian Ambrożewicz         {
46562598e31SEd Tanous             BMCWEB_LOG_ERROR("Credentials too long to handle");
466988fb7b2SAdrian Ambrożewicz             messages::unrecognizedRequestBody(asyncResp->res);
467988fb7b2SAdrian Ambrożewicz             return;
468988fb7b2SAdrian Ambrożewicz         }
469988fb7b2SAdrian Ambrożewicz 
470988fb7b2SAdrian Ambrożewicz         // Open pipe
47111e8f60dSEd Tanous         secretPipe = std::make_shared<CredentialsPipe>(
47211e8f60dSEd Tanous             crow::connections::systemBus->get_io_context());
47311e8f60dSEd Tanous         fd = secretPipe->fd();
474988fb7b2SAdrian Ambrożewicz 
475988fb7b2SAdrian Ambrożewicz         // Pass secret over pipe
47681ce609eSEd Tanous         secretPipe->asyncWrite(
47711e8f60dSEd Tanous             std::move(userName), std::move(password),
47811e8f60dSEd Tanous             [asyncResp, secretPipe](const boost::system::error_code& ec,
47911e8f60dSEd Tanous                                     std::size_t) {
480988fb7b2SAdrian Ambrożewicz             if (ec)
481988fb7b2SAdrian Ambrożewicz             {
48262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to pass secret: {}", ec);
483988fb7b2SAdrian Ambrożewicz                 messages::internalError(asyncResp->res);
484988fb7b2SAdrian Ambrożewicz             }
485988fb7b2SAdrian Ambrożewicz         });
486988fb7b2SAdrian Ambrożewicz     }
487988fb7b2SAdrian Ambrożewicz 
48811e8f60dSEd Tanous     dbus::utility::DbusVariantType unixFd(
48911e8f60dSEd Tanous         std::in_place_type<sdbusplus::message::unix_fd>, fd);
49011e8f60dSEd Tanous 
49111e8f60dSEd Tanous     sdbusplus::message::object_path path(
49211e8f60dSEd Tanous         "/xyz/openbmc_project/VirtualMedia/Legacy");
49311e8f60dSEd Tanous     path /= name;
494e13c2760SPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
4955e7e2dc5SEd Tanous         [asyncResp, secretPipe](const boost::system::error_code& ec,
496988fb7b2SAdrian Ambrożewicz                                 bool success) {
497e13c2760SPrzemyslaw Czarnowski         if (ec)
498e13c2760SPrzemyslaw Czarnowski         {
49962598e31SEd Tanous             BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
500e13c2760SPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
50111e8f60dSEd Tanous             return;
502d6da5bebSAdrian Ambrożewicz         }
50311e8f60dSEd Tanous         if (!success)
504d6da5bebSAdrian Ambrożewicz         {
50562598e31SEd Tanous             BMCWEB_LOG_ERROR("Service responded with error");
50611e8f60dSEd Tanous             messages::internalError(asyncResp->res);
507e13c2760SPrzemyslaw Czarnowski         }
508e13c2760SPrzemyslaw Czarnowski     },
50911e8f60dSEd Tanous         service, path.str, "xyz.openbmc_project.VirtualMedia.Legacy", "Mount",
51011e8f60dSEd Tanous         imageUrl, rw, unixFd);
511e13c2760SPrzemyslaw Czarnowski }
512e13c2760SPrzemyslaw Czarnowski 
513e13c2760SPrzemyslaw Czarnowski /**
514120fa86aSPrzemyslaw Czarnowski  * @brief Function validate parameters of insert media request.
515120fa86aSPrzemyslaw Czarnowski  *
516120fa86aSPrzemyslaw Czarnowski  */
517120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
518120fa86aSPrzemyslaw Czarnowski                            const std::string& service,
519120fa86aSPrzemyslaw Czarnowski                            const std::string& resName,
520120fa86aSPrzemyslaw Czarnowski                            InsertMediaActionParams& actionParams)
521120fa86aSPrzemyslaw Czarnowski {
52262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Validation started");
523120fa86aSPrzemyslaw Czarnowski     // required param imageUrl must not be empty
524120fa86aSPrzemyslaw Czarnowski     if (!actionParams.imageUrl)
525120fa86aSPrzemyslaw Czarnowski     {
52662598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter Image is empty.");
527120fa86aSPrzemyslaw Czarnowski 
528120fa86aSPrzemyslaw Czarnowski         messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image");
529120fa86aSPrzemyslaw Czarnowski 
530120fa86aSPrzemyslaw Czarnowski         return;
531120fa86aSPrzemyslaw Czarnowski     }
532120fa86aSPrzemyslaw Czarnowski 
533120fa86aSPrzemyslaw Czarnowski     // optional param inserted must be true
534e01d0c36SEd Tanous     if (actionParams.inserted && !*actionParams.inserted)
535120fa86aSPrzemyslaw Czarnowski     {
53662598e31SEd Tanous         BMCWEB_LOG_ERROR(
53762598e31SEd Tanous             "Request action optional parameter Inserted must be true.");
538120fa86aSPrzemyslaw Czarnowski 
539120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "Inserted",
540120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
541120fa86aSPrzemyslaw Czarnowski 
542120fa86aSPrzemyslaw Czarnowski         return;
543120fa86aSPrzemyslaw Czarnowski     }
544120fa86aSPrzemyslaw Czarnowski 
545120fa86aSPrzemyslaw Czarnowski     // optional param transferMethod must be stream
546e01d0c36SEd Tanous     if (actionParams.transferMethod &&
547120fa86aSPrzemyslaw Czarnowski         (*actionParams.transferMethod != "Stream"))
548120fa86aSPrzemyslaw Czarnowski     {
54962598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action optional parameter "
55062598e31SEd Tanous                          "TransferMethod must be Stream.");
551120fa86aSPrzemyslaw Czarnowski 
552120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "TransferMethod",
553120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
554120fa86aSPrzemyslaw Czarnowski 
555120fa86aSPrzemyslaw Czarnowski         return;
556120fa86aSPrzemyslaw Czarnowski     }
5576fd29553SEd Tanous     boost::system::result<boost::urls::url_view> url =
558120fa86aSPrzemyslaw Czarnowski         boost::urls::parse_uri(*actionParams.imageUrl);
559120fa86aSPrzemyslaw Czarnowski     if (!url)
560120fa86aSPrzemyslaw Czarnowski     {
561120fa86aSPrzemyslaw Czarnowski         messages::actionParameterValueFormatError(
562120fa86aSPrzemyslaw Czarnowski             asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia");
563120fa86aSPrzemyslaw Czarnowski         return;
564120fa86aSPrzemyslaw Czarnowski     }
565120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> uriTransferProtocolType =
566120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromUri(*url);
567120fa86aSPrzemyslaw Czarnowski 
568120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> paramTransferProtocolType =
569120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromParam(actionParams.transferProtocolType);
570120fa86aSPrzemyslaw Czarnowski 
571120fa86aSPrzemyslaw Czarnowski     // ImageUrl does not contain valid protocol type
572e01d0c36SEd Tanous     if (uriTransferProtocolType &&
573e01d0c36SEd Tanous         *uriTransferProtocolType == TransferProtocol::invalid)
574120fa86aSPrzemyslaw Czarnowski     {
57562598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
576120fa86aSPrzemyslaw Czarnowski                          "contain specified protocol type from list: "
57762598e31SEd Tanous                          "(smb, https).");
578120fa86aSPrzemyslaw Czarnowski 
579120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
580120fa86aSPrzemyslaw Czarnowski 
581120fa86aSPrzemyslaw Czarnowski         return;
582120fa86aSPrzemyslaw Czarnowski     }
583120fa86aSPrzemyslaw Czarnowski 
584120fa86aSPrzemyslaw Czarnowski     // transferProtocolType should contain value from list
585e01d0c36SEd Tanous     if (paramTransferProtocolType &&
586e01d0c36SEd Tanous         *paramTransferProtocolType == TransferProtocol::invalid)
587120fa86aSPrzemyslaw Czarnowski     {
58862598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter TransferProtocolType "
589120fa86aSPrzemyslaw Czarnowski                          "must be provided with value from list: "
59062598e31SEd Tanous                          "(CIFS, HTTPS).");
591120fa86aSPrzemyslaw Czarnowski 
592e01d0c36SEd Tanous         messages::propertyValueNotInList(
593e01d0c36SEd Tanous             asyncResp->res, actionParams.transferProtocolType.value_or(""),
594120fa86aSPrzemyslaw Czarnowski             "TransferProtocolType");
595120fa86aSPrzemyslaw Czarnowski         return;
596120fa86aSPrzemyslaw Czarnowski     }
597120fa86aSPrzemyslaw Czarnowski 
598120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol not provided either with URI nor param
599e01d0c36SEd Tanous     if (!uriTransferProtocolType && !paramTransferProtocolType)
600120fa86aSPrzemyslaw Czarnowski     {
60162598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
602120fa86aSPrzemyslaw Czarnowski                          "contain specified protocol type or param "
60362598e31SEd Tanous                          "TransferProtocolType must be provided.");
604120fa86aSPrzemyslaw Czarnowski 
605120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
606120fa86aSPrzemyslaw Czarnowski 
607120fa86aSPrzemyslaw Czarnowski         return;
608120fa86aSPrzemyslaw Czarnowski     }
609120fa86aSPrzemyslaw Czarnowski 
610120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol provided both with URI and param
611e01d0c36SEd Tanous     if (paramTransferProtocolType && uriTransferProtocolType)
612120fa86aSPrzemyslaw Czarnowski     {
613120fa86aSPrzemyslaw Czarnowski         // check if protocol is the same for URI and param
614120fa86aSPrzemyslaw Czarnowski         if (*paramTransferProtocolType != *uriTransferProtocolType)
615120fa86aSPrzemyslaw Czarnowski         {
61662598e31SEd Tanous             BMCWEB_LOG_ERROR("Request action parameter "
617120fa86aSPrzemyslaw Czarnowski                              "TransferProtocolType must  contain the "
618120fa86aSPrzemyslaw Czarnowski                              "same protocol type as protocol type "
61962598e31SEd Tanous                              "provided with param imageUrl.");
620120fa86aSPrzemyslaw Czarnowski 
621120fa86aSPrzemyslaw Czarnowski             messages::actionParameterValueTypeError(
622e01d0c36SEd Tanous                 asyncResp->res, actionParams.transferProtocolType.value_or(""),
623120fa86aSPrzemyslaw Czarnowski                 "TransferProtocolType", "InsertMedia");
624120fa86aSPrzemyslaw Czarnowski 
625120fa86aSPrzemyslaw Czarnowski             return;
626120fa86aSPrzemyslaw Czarnowski         }
627120fa86aSPrzemyslaw Czarnowski     }
628120fa86aSPrzemyslaw Czarnowski 
629120fa86aSPrzemyslaw Czarnowski     // validation passed, add protocol to URI if needed
6307ead48e6SBoleslaw Ogonczyk Makowski     if (!uriTransferProtocolType && paramTransferProtocolType)
631120fa86aSPrzemyslaw Czarnowski     {
632120fa86aSPrzemyslaw Czarnowski         actionParams.imageUrl = getUriWithTransferProtocol(
633120fa86aSPrzemyslaw Czarnowski             *actionParams.imageUrl, *paramTransferProtocolType);
634120fa86aSPrzemyslaw Czarnowski     }
635120fa86aSPrzemyslaw Czarnowski 
636452bd8d8SJayaprakash Mutyala     if (!actionParams.userName)
637452bd8d8SJayaprakash Mutyala     {
638452bd8d8SJayaprakash Mutyala         actionParams.userName = "";
639452bd8d8SJayaprakash Mutyala     }
640452bd8d8SJayaprakash Mutyala 
641452bd8d8SJayaprakash Mutyala     if (!actionParams.password)
642452bd8d8SJayaprakash Mutyala     {
643452bd8d8SJayaprakash Mutyala         actionParams.password = "";
644452bd8d8SJayaprakash Mutyala     }
645452bd8d8SJayaprakash Mutyala 
646120fa86aSPrzemyslaw Czarnowski     doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
647e01d0c36SEd Tanous                     !(actionParams.writeProtected.value_or(false)),
648120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.userName),
649120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.password));
650120fa86aSPrzemyslaw Czarnowski }
651120fa86aSPrzemyslaw Czarnowski 
652120fa86aSPrzemyslaw Czarnowski /**
653e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
654e13c2760SPrzemyslaw Czarnowski  *
655e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
656e13c2760SPrzemyslaw Czarnowski  */
65724e740a7SEd Tanous inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
658e13c2760SPrzemyslaw Czarnowski                           const std::string& service, const std::string& name,
659e13c2760SPrzemyslaw Czarnowski                           bool legacy)
660e13c2760SPrzemyslaw Czarnowski {
661e13c2760SPrzemyslaw Czarnowski     // Legacy mount requires parameter with image
662e13c2760SPrzemyslaw Czarnowski     if (legacy)
663e13c2760SPrzemyslaw Czarnowski     {
664e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
6655e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
666e13c2760SPrzemyslaw Czarnowski             if (ec)
667e13c2760SPrzemyslaw Czarnowski             {
66862598e31SEd Tanous                 BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
669e13c2760SPrzemyslaw Czarnowski 
670e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
671e13c2760SPrzemyslaw Czarnowski                 return;
672e13c2760SPrzemyslaw Czarnowski             }
673e13c2760SPrzemyslaw Czarnowski         },
674e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
675e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount");
676e13c2760SPrzemyslaw Czarnowski     }
677e13c2760SPrzemyslaw Czarnowski     else // proxy
678e13c2760SPrzemyslaw Czarnowski     {
679e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
6805e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
681e13c2760SPrzemyslaw Czarnowski             if (ec)
682e13c2760SPrzemyslaw Czarnowski             {
68362598e31SEd Tanous                 BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
684e13c2760SPrzemyslaw Czarnowski 
685e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
686e13c2760SPrzemyslaw Czarnowski                 return;
687e13c2760SPrzemyslaw Czarnowski             }
688e13c2760SPrzemyslaw Czarnowski         },
689e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name,
690e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
691e13c2760SPrzemyslaw Czarnowski     }
692e13c2760SPrzemyslaw Czarnowski }
693e13c2760SPrzemyslaw Czarnowski 
69496825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost(
69596825bebSEd Tanous     crow::App& app, const crow::Request& req,
69622db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
69796825bebSEd Tanous     const std::string& name, const std::string& resName)
69896825bebSEd Tanous {
6993ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
70045ca1b86SEd Tanous     {
70145ca1b86SEd Tanous         return;
70245ca1b86SEd Tanous     }
70379fdf63eSPrzemyslaw Czarnowski 
70479fdf63eSPrzemyslaw Czarnowski     constexpr std::string_view action = "VirtualMedia.InsertMedia";
70522db1728SEd Tanous     if (name != "bmc")
706107077deSPrzemyslaw Czarnowski     {
70779fdf63eSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, action, resName);
708107077deSPrzemyslaw Czarnowski 
709107077deSPrzemyslaw Czarnowski         return;
710107077deSPrzemyslaw Czarnowski     }
71179fdf63eSPrzemyslaw Czarnowski     InsertMediaActionParams actionParams;
71298be3e39SEd Tanous 
713120fa86aSPrzemyslaw Czarnowski     // Read obligatory parameters (url of image)
71415ed6780SWilly Tu     if (!json_util::readJsonAction(
71579fdf63eSPrzemyslaw Czarnowski             req, asyncResp->res, "Image", actionParams.imageUrl,
71679fdf63eSPrzemyslaw Czarnowski             "WriteProtected", actionParams.writeProtected, "UserName",
71779fdf63eSPrzemyslaw Czarnowski             actionParams.userName, "Password", actionParams.password,
71879fdf63eSPrzemyslaw Czarnowski             "Inserted", actionParams.inserted, "TransferMethod",
71979fdf63eSPrzemyslaw Czarnowski             actionParams.transferMethod, "TransferProtocolType",
72079fdf63eSPrzemyslaw Czarnowski             actionParams.transferProtocolType))
72198be3e39SEd Tanous     {
72298be3e39SEd Tanous         return;
72398be3e39SEd Tanous     }
724107077deSPrzemyslaw Czarnowski 
7252b73119cSGeorge Liu     dbus::utility::getDbusObject(
7262b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
72779fdf63eSPrzemyslaw Czarnowski         [asyncResp, action, actionParams,
7282b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
729002d39b4SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) mutable {
73022db1728SEd Tanous         if (ec)
73122db1728SEd Tanous         {
73262598e31SEd Tanous             BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
73379fdf63eSPrzemyslaw Czarnowski             messages::resourceNotFound(asyncResp->res, action, resName);
734107077deSPrzemyslaw Czarnowski 
73522db1728SEd Tanous             return;
73622db1728SEd Tanous         }
73779fdf63eSPrzemyslaw Czarnowski 
73822db1728SEd Tanous         std::string service = getObjectType.begin()->first;
73962598e31SEd Tanous         BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
74022db1728SEd Tanous 
7415eb468daSGeorge Liu         sdbusplus::message::object_path path(
7425eb468daSGeorge Liu             "/xyz/openbmc_project/VirtualMedia");
7435eb468daSGeorge Liu         dbus::utility::getManagedObjects(
7445eb468daSGeorge Liu             service, path,
7455eb468daSGeorge Liu             [service, resName, action, actionParams, asyncResp](
7465eb468daSGeorge Liu                 const boost::system::error_code& ec2,
7475eb468daSGeorge Liu                 const dbus::utility::ManagedObjectType& subtree) mutable {
7488a592810SEd Tanous             if (ec2)
74922db1728SEd Tanous             {
75079fdf63eSPrzemyslaw Czarnowski                 // Not possible in proxy mode
75162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("InsertMedia not "
75262598e31SEd Tanous                                  "allowed in proxy mode");
75379fdf63eSPrzemyslaw Czarnowski                 messages::resourceNotFound(asyncResp->res, action, resName);
75422db1728SEd Tanous 
75522db1728SEd Tanous                 return;
75622db1728SEd Tanous             }
75722db1728SEd Tanous             for (const auto& object : subtree)
75822db1728SEd Tanous             {
759365a73f4SEd Tanous                 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
7605880f0c5SBoleslaw Ogonczyk Makowski                 if (mode == VmMode::Legacy)
76122db1728SEd Tanous                 {
76279fdf63eSPrzemyslaw Czarnowski                     validateParams(asyncResp, service, resName, actionParams);
76322db1728SEd Tanous 
76422db1728SEd Tanous                     return;
76522db1728SEd Tanous                 }
76622db1728SEd Tanous             }
76762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Parent item not found");
76896825bebSEd Tanous             messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
7695eb468daSGeorge Liu         });
7702b73119cSGeorge Liu     });
77196825bebSEd Tanous }
77222db1728SEd Tanous 
77396825bebSEd Tanous inline void handleManagersVirtualMediaActionEject(
77496825bebSEd Tanous     crow::App& app, const crow::Request& req,
77522db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
77696825bebSEd Tanous     const std::string& managerName, const std::string& resName)
77796825bebSEd Tanous {
7783ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
77945ca1b86SEd Tanous     {
78045ca1b86SEd Tanous         return;
78145ca1b86SEd Tanous     }
78279fdf63eSPrzemyslaw Czarnowski 
78379fdf63eSPrzemyslaw Czarnowski     constexpr std::string_view action = "VirtualMedia.EjectMedia";
78496825bebSEd Tanous     if (managerName != "bmc")
785107077deSPrzemyslaw Czarnowski     {
78679fdf63eSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, action, resName);
78722db1728SEd Tanous 
78822db1728SEd Tanous         return;
78922db1728SEd Tanous     }
79022db1728SEd Tanous 
7912b73119cSGeorge Liu     dbus::utility::getDbusObject(
7922b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
79379fdf63eSPrzemyslaw Czarnowski         [asyncResp, action,
7942b73119cSGeorge Liu          resName](const boost::system::error_code& ec2,
795b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
7968a592810SEd Tanous         if (ec2)
79722db1728SEd Tanous         {
79862598e31SEd Tanous             BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec2);
79922db1728SEd Tanous             messages::internalError(asyncResp->res);
80022db1728SEd Tanous 
80122db1728SEd Tanous             return;
80222db1728SEd Tanous         }
80322db1728SEd Tanous         std::string service = getObjectType.begin()->first;
80462598e31SEd Tanous         BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
80522db1728SEd Tanous 
8065eb468daSGeorge Liu         sdbusplus::message::object_path path(
8075eb468daSGeorge Liu             "/xyz/openbmc_project/VirtualMedia");
8085eb468daSGeorge Liu         dbus::utility::getManagedObjects(
8095eb468daSGeorge Liu             service, path,
81079fdf63eSPrzemyslaw Czarnowski             [resName, service, action,
81179fdf63eSPrzemyslaw Czarnowski              asyncResp](const boost::system::error_code& ec,
81202cad96eSEd Tanous                         const dbus::utility::ManagedObjectType& subtree) {
81322db1728SEd Tanous             if (ec)
81422db1728SEd Tanous             {
81562598e31SEd Tanous                 BMCWEB_LOG_ERROR("ObjectMapper : No Service found");
81679fdf63eSPrzemyslaw Czarnowski                 messages::resourceNotFound(asyncResp->res, action, resName);
81722db1728SEd Tanous                 return;
81822db1728SEd Tanous             }
81922db1728SEd Tanous 
82022db1728SEd Tanous             for (const auto& object : subtree)
82122db1728SEd Tanous             {
822365a73f4SEd Tanous                 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
823365a73f4SEd Tanous                 if (mode != VmMode::Invalid)
82422db1728SEd Tanous                 {
825365a73f4SEd Tanous                     doEjectAction(asyncResp, service, resName,
826365a73f4SEd Tanous                                   mode == VmMode::Legacy);
8275880f0c5SBoleslaw Ogonczyk Makowski                     return;
82822db1728SEd Tanous                 }
82922db1728SEd Tanous             }
83062598e31SEd Tanous             BMCWEB_LOG_DEBUG("Parent item not found");
83196825bebSEd Tanous             messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
8325eb468daSGeorge Liu         });
8332b73119cSGeorge Liu     });
83496825bebSEd Tanous }
83596825bebSEd Tanous 
83696825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet(
83796825bebSEd Tanous     crow::App& app, const crow::Request& req,
83822db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
83996825bebSEd Tanous     const std::string& name)
84096825bebSEd Tanous {
8413ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
84245ca1b86SEd Tanous     {
84345ca1b86SEd Tanous         return;
84445ca1b86SEd Tanous     }
84522db1728SEd Tanous     if (name != "bmc")
84622db1728SEd Tanous     {
847002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", name);
848107077deSPrzemyslaw Czarnowski 
849107077deSPrzemyslaw Czarnowski         return;
850107077deSPrzemyslaw Czarnowski     }
851107077deSPrzemyslaw Czarnowski 
8528d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
853107077deSPrzemyslaw Czarnowski         "#VirtualMediaCollection.VirtualMediaCollection";
8548d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Virtual Media Services";
855ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
856ef4c65b7SEd Tanous         boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia", name);
857107077deSPrzemyslaw Czarnowski 
8582b73119cSGeorge Liu     dbus::utility::getDbusObject(
8592b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
8602b73119cSGeorge Liu         [asyncResp, name](const boost::system::error_code& ec,
861b9d36b47SEd Tanous                           const dbus::utility::MapperGetObject& getObjectType) {
862107077deSPrzemyslaw Czarnowski         if (ec)
863107077deSPrzemyslaw Czarnowski         {
86462598e31SEd Tanous             BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
865107077deSPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
866107077deSPrzemyslaw Czarnowski 
867107077deSPrzemyslaw Czarnowski             return;
868107077deSPrzemyslaw Czarnowski         }
869107077deSPrzemyslaw Czarnowski         std::string service = getObjectType.begin()->first;
87062598e31SEd Tanous         BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
871107077deSPrzemyslaw Czarnowski 
872107077deSPrzemyslaw Czarnowski         getVmResourceList(asyncResp, service, name);
8732b73119cSGeorge Liu     });
87496825bebSEd Tanous }
875107077deSPrzemyslaw Czarnowski 
87696825bebSEd Tanous inline void
87796825bebSEd Tanous     handleVirtualMediaGet(crow::App& app, const crow::Request& req,
87822db1728SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
87996825bebSEd Tanous                           const std::string& name, const std::string& resName)
88096825bebSEd Tanous {
8813ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
88245ca1b86SEd Tanous     {
88345ca1b86SEd Tanous         return;
88445ca1b86SEd Tanous     }
885107077deSPrzemyslaw Czarnowski     if (name != "bmc")
886107077deSPrzemyslaw Czarnowski     {
887002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
888107077deSPrzemyslaw Czarnowski 
889107077deSPrzemyslaw Czarnowski         return;
890107077deSPrzemyslaw Czarnowski     }
891107077deSPrzemyslaw Czarnowski 
8922b73119cSGeorge Liu     dbus::utility::getDbusObject(
8932b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
894002d39b4SEd Tanous         [asyncResp, name,
8952b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
896b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
897107077deSPrzemyslaw Czarnowski         if (ec)
898107077deSPrzemyslaw Czarnowski         {
89962598e31SEd Tanous             BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
900107077deSPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
901107077deSPrzemyslaw Czarnowski 
902107077deSPrzemyslaw Czarnowski             return;
903107077deSPrzemyslaw Czarnowski         }
904107077deSPrzemyslaw Czarnowski         std::string service = getObjectType.begin()->first;
90562598e31SEd Tanous         BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
906107077deSPrzemyslaw Czarnowski 
907107077deSPrzemyslaw Czarnowski         getVmData(asyncResp, service, name, resName);
9082b73119cSGeorge Liu     });
90996825bebSEd Tanous }
91096825bebSEd Tanous 
91196825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app)
91296825bebSEd Tanous {
91396825bebSEd Tanous     BMCWEB_ROUTE(
91496825bebSEd Tanous         app,
91596825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia")
91696825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
91796825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
91896825bebSEd Tanous             handleManagersVirtualMediaActionInsertPost, std::ref(app)));
91996825bebSEd Tanous 
92096825bebSEd Tanous     BMCWEB_ROUTE(
92196825bebSEd Tanous         app,
92296825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia")
92396825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
92496825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
92596825bebSEd Tanous             handleManagersVirtualMediaActionEject, std::ref(app)));
92696825bebSEd Tanous 
92796825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/")
92896825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMediaCollection)
92996825bebSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
93096825bebSEd Tanous             handleManagersVirtualMediaCollectionGet, std::ref(app)));
93196825bebSEd Tanous 
93296825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/")
93396825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMedia)
93496825bebSEd Tanous         .methods(boost::beast::http::verb::get)(
93596825bebSEd Tanous             std::bind_front(handleVirtualMediaGet, std::ref(app)));
936107077deSPrzemyslaw Czarnowski }
937107077deSPrzemyslaw Czarnowski 
938107077deSPrzemyslaw Czarnowski } // namespace redfish
939