xref: /openbmc/bmcweb/features/redfish/lib/virtual_media.hpp (revision 0a48306bb144d26b69f1e9bfcc826908e24c55fe)
1107077deSPrzemyslaw Czarnowski /*
2107077deSPrzemyslaw Czarnowski // Copyright (c) 2018 Intel Corporation
3107077deSPrzemyslaw Czarnowski //
4107077deSPrzemyslaw Czarnowski // Licensed under the Apache License, Version 2.0 (the "License");
5107077deSPrzemyslaw Czarnowski // you may not use this file except in compliance with the License.
6107077deSPrzemyslaw Czarnowski // You may obtain a copy of the License at
7107077deSPrzemyslaw Czarnowski //
8107077deSPrzemyslaw Czarnowski //      http://www.apache.org/licenses/LICENSE-2.0
9107077deSPrzemyslaw Czarnowski //
10107077deSPrzemyslaw Czarnowski // Unless required by applicable law or agreed to in writing, software
11107077deSPrzemyslaw Czarnowski // distributed under the License is distributed on an "AS IS" BASIS,
12107077deSPrzemyslaw Czarnowski // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13107077deSPrzemyslaw Czarnowski // See the License for the specific language governing permissions and
14107077deSPrzemyslaw Czarnowski // limitations under the License.
15107077deSPrzemyslaw Czarnowski */
16107077deSPrzemyslaw Czarnowski #pragma once
17107077deSPrzemyslaw Czarnowski 
183ccb3adbSEd Tanous #include "account_service.hpp"
193ccb3adbSEd Tanous #include "app.hpp"
202b73119cSGeorge Liu #include "dbus_utility.hpp"
21739b87efSEd Tanous #include "generated/enums/virtual_media.hpp"
223ccb3adbSEd Tanous #include "query.hpp"
233ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
243ccb3adbSEd Tanous #include "utils/json_utils.hpp"
253ccb3adbSEd Tanous 
26988fb7b2SAdrian Ambrożewicz #include <boost/process/async_pipe.hpp>
279e319cf0SAnna Platash #include <boost/url/url_view.hpp>
28107077deSPrzemyslaw Czarnowski 
292b73119cSGeorge Liu #include <array>
302b73119cSGeorge Liu #include <string_view>
312b73119cSGeorge Liu 
32107077deSPrzemyslaw Czarnowski namespace redfish
33107077deSPrzemyslaw Czarnowski {
34365a73f4SEd Tanous 
35365a73f4SEd Tanous enum class VmMode
36365a73f4SEd Tanous {
37365a73f4SEd Tanous     Invalid,
38365a73f4SEd Tanous     Legacy,
39365a73f4SEd Tanous     Proxy
40365a73f4SEd Tanous };
41365a73f4SEd Tanous 
42365a73f4SEd Tanous inline VmMode
43365a73f4SEd Tanous     parseObjectPathAndGetMode(const sdbusplus::message::object_path& itemPath,
44365a73f4SEd Tanous                               const std::string& resName)
45365a73f4SEd Tanous {
46365a73f4SEd Tanous     std::string thisPath = itemPath.filename();
47365a73f4SEd Tanous     BMCWEB_LOG_DEBUG << "Filename: " << itemPath.str
48365a73f4SEd Tanous                      << ", ThisPath: " << thisPath;
49365a73f4SEd Tanous 
50365a73f4SEd Tanous     if (thisPath.empty())
51365a73f4SEd Tanous     {
52365a73f4SEd Tanous         return VmMode::Invalid;
53365a73f4SEd Tanous     }
54365a73f4SEd Tanous 
55365a73f4SEd Tanous     if (thisPath != resName)
56365a73f4SEd Tanous     {
57365a73f4SEd Tanous         return VmMode::Invalid;
58365a73f4SEd Tanous     }
59365a73f4SEd Tanous 
60365a73f4SEd Tanous     auto mode = itemPath.parent_path();
61365a73f4SEd Tanous     auto type = mode.parent_path();
62365a73f4SEd Tanous 
63365a73f4SEd Tanous     if (mode.filename().empty() || type.filename().empty())
64365a73f4SEd Tanous     {
65365a73f4SEd Tanous         return VmMode::Invalid;
66365a73f4SEd Tanous     }
67365a73f4SEd Tanous 
68365a73f4SEd Tanous     if (type.filename() != "VirtualMedia")
69365a73f4SEd Tanous     {
70365a73f4SEd Tanous         return VmMode::Invalid;
71365a73f4SEd Tanous     }
72365a73f4SEd Tanous     std::string modeStr = mode.filename();
73365a73f4SEd Tanous     if (modeStr == "Legacy")
74365a73f4SEd Tanous     {
75365a73f4SEd Tanous         return VmMode::Legacy;
76365a73f4SEd Tanous     }
77365a73f4SEd Tanous     if (modeStr == "Proxy")
78365a73f4SEd Tanous     {
79365a73f4SEd Tanous         return VmMode::Proxy;
80365a73f4SEd Tanous     }
81365a73f4SEd Tanous     return VmMode::Invalid;
82365a73f4SEd Tanous }
83365a73f4SEd Tanous 
849e319cf0SAnna Platash /**
859e319cf0SAnna Platash  * @brief Function extracts transfer protocol name from URI.
869e319cf0SAnna Platash  */
8767df073bSEd Tanous inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri)
8867df073bSEd Tanous {
8967df073bSEd Tanous     boost::urls::result<boost::urls::url_view> url =
90079360aeSEd Tanous         boost::urls::parse_uri(imageUri);
9167df073bSEd Tanous     if (!url)
9267df073bSEd Tanous     {
9367df073bSEd Tanous         return "None";
9467df073bSEd Tanous     }
95079360aeSEd Tanous     std::string_view scheme = url->scheme();
9667df073bSEd Tanous     if (scheme == "smb")
9767df073bSEd Tanous     {
9867df073bSEd Tanous         return "CIFS";
9967df073bSEd Tanous     }
10067df073bSEd Tanous     if (scheme == "https")
10167df073bSEd Tanous     {
10267df073bSEd Tanous         return "HTTPS";
10367df073bSEd Tanous     }
10467df073bSEd Tanous 
10567df073bSEd Tanous     return "None";
10667df073bSEd Tanous }
107107077deSPrzemyslaw Czarnowski 
108107077deSPrzemyslaw Czarnowski /**
109107077deSPrzemyslaw Czarnowski  * @brief Read all known properties from VM object interfaces
110107077deSPrzemyslaw Czarnowski  */
11122db1728SEd Tanous inline void
1128a592810SEd Tanous     vmParseInterfaceObject(const dbus::utility::DBusInteracesMap& interfaces,
1138d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& aResp)
114107077deSPrzemyslaw Czarnowski {
1158a592810SEd Tanous     for (const auto& [interface, values] : interfaces)
116107077deSPrzemyslaw Czarnowski     {
117711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint")
118107077deSPrzemyslaw Czarnowski         {
119711ac7a9SEd Tanous             for (const auto& [property, value] : values)
120107077deSPrzemyslaw Czarnowski             {
121711ac7a9SEd Tanous                 if (property == "EndpointId")
122107077deSPrzemyslaw Czarnowski                 {
123107077deSPrzemyslaw Czarnowski                     const std::string* endpointIdValue =
124711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
125711ac7a9SEd Tanous                     if (endpointIdValue == nullptr)
126107077deSPrzemyslaw Czarnowski                     {
127711ac7a9SEd Tanous                         continue;
128711ac7a9SEd Tanous                     }
129107077deSPrzemyslaw Czarnowski                     if (!endpointIdValue->empty())
130107077deSPrzemyslaw Czarnowski                     {
131107077deSPrzemyslaw Czarnowski                         // Proxy mode
132711ac7a9SEd Tanous                         aResp->res
133711ac7a9SEd Tanous                             .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] =
134d04ba325SPrzemyslaw Czarnowski                             *endpointIdValue;
135107077deSPrzemyslaw Czarnowski                         aResp->res.jsonValue["TransferProtocolType"] = "OEM";
136107077deSPrzemyslaw Czarnowski                     }
137107077deSPrzemyslaw Czarnowski                 }
138711ac7a9SEd Tanous                 if (property == "ImageURL")
139107077deSPrzemyslaw Czarnowski                 {
140107077deSPrzemyslaw Czarnowski                     const std::string* imageUrlValue =
141711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
14226f6976fSEd Tanous                     if (imageUrlValue != nullptr && !imageUrlValue->empty())
143107077deSPrzemyslaw Czarnowski                     {
144da4784d8SPrzemyslaw Czarnowski                         std::filesystem::path filePath = *imageUrlValue;
145da4784d8SPrzemyslaw Czarnowski                         if (!filePath.has_filename())
146da4784d8SPrzemyslaw Czarnowski                         {
1479e319cf0SAnna Platash                             // this will handle https share, which not
1489e319cf0SAnna Platash                             // necessarily has to have filename given.
149da4784d8SPrzemyslaw Czarnowski                             aResp->res.jsonValue["ImageName"] = "";
150da4784d8SPrzemyslaw Czarnowski                         }
151da4784d8SPrzemyslaw Czarnowski                         else
152da4784d8SPrzemyslaw Czarnowski                         {
1539e319cf0SAnna Platash                             aResp->res.jsonValue["ImageName"] =
1549e319cf0SAnna Platash                                 filePath.filename();
155da4784d8SPrzemyslaw Czarnowski                         }
156da4784d8SPrzemyslaw Czarnowski 
157da4784d8SPrzemyslaw Czarnowski                         aResp->res.jsonValue["Image"] = *imageUrlValue;
1589e319cf0SAnna Platash                         aResp->res.jsonValue["TransferProtocolType"] =
1599e319cf0SAnna Platash                             getTransferProtocolTypeFromUri(*imageUrlValue);
1609e319cf0SAnna Platash 
161739b87efSEd Tanous                         aResp->res.jsonValue["ConnectedVia"] =
162739b87efSEd Tanous                             virtual_media::ConnectedVia::URI;
163107077deSPrzemyslaw Czarnowski                     }
164107077deSPrzemyslaw Czarnowski                 }
165711ac7a9SEd Tanous                 if (property == "WriteProtected")
1669e319cf0SAnna Platash                 {
167711ac7a9SEd Tanous                     const bool* writeProtectedValue = std::get_if<bool>(&value);
168e662eae8SEd Tanous                     if (writeProtectedValue != nullptr)
1699e319cf0SAnna Platash                     {
1709e319cf0SAnna Platash                         aResp->res.jsonValue["WriteProtected"] =
1719e319cf0SAnna Platash                             *writeProtectedValue;
1729e319cf0SAnna Platash                     }
1739e319cf0SAnna Platash                 }
1749e319cf0SAnna Platash             }
175107077deSPrzemyslaw Czarnowski         }
176711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.Process")
177711ac7a9SEd Tanous         {
178711ac7a9SEd Tanous             for (const auto& [property, value] : values)
179711ac7a9SEd Tanous             {
180711ac7a9SEd Tanous                 if (property == "Active")
181711ac7a9SEd Tanous                 {
182711ac7a9SEd Tanous                     const bool* activeValue = std::get_if<bool>(&value);
183e662eae8SEd Tanous                     if (activeValue == nullptr)
184711ac7a9SEd Tanous                     {
185711ac7a9SEd Tanous                         BMCWEB_LOG_DEBUG << "Value Active not found";
186711ac7a9SEd Tanous                         return;
187711ac7a9SEd Tanous                     }
188711ac7a9SEd Tanous                     aResp->res.jsonValue["Inserted"] = *activeValue;
189711ac7a9SEd Tanous 
190e05aec50SEd Tanous                     if (*activeValue)
191711ac7a9SEd Tanous                     {
192739b87efSEd Tanous                         aResp->res.jsonValue["ConnectedVia"] =
193739b87efSEd Tanous                             virtual_media::ConnectedVia::Applet;
194711ac7a9SEd Tanous                     }
195711ac7a9SEd Tanous                 }
196711ac7a9SEd Tanous             }
197711ac7a9SEd Tanous         }
198107077deSPrzemyslaw Czarnowski     }
199107077deSPrzemyslaw Czarnowski }
200107077deSPrzemyslaw Czarnowski 
201107077deSPrzemyslaw Czarnowski /**
202107077deSPrzemyslaw Czarnowski  * @brief Fill template for Virtual Media Item.
203107077deSPrzemyslaw Czarnowski  */
20422db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name,
205107077deSPrzemyslaw Czarnowski                                      const std::string& resName)
206107077deSPrzemyslaw Czarnowski {
207107077deSPrzemyslaw Czarnowski     nlohmann::json item;
208fdb20347SEd Tanous     item["@odata.id"] = crow::utility::urlFromPieces(
209fdb20347SEd Tanous         "redfish", "v1", "Managers", name, "VirtualMedia", resName);
21022db1728SEd Tanous 
211d04ba325SPrzemyslaw Czarnowski     item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia";
212107077deSPrzemyslaw Czarnowski     item["Name"] = "Virtual Removable Media";
213107077deSPrzemyslaw Czarnowski     item["Id"] = resName;
214107077deSPrzemyslaw Czarnowski     item["WriteProtected"] = true;
215739b87efSEd Tanous     item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected;
216613dabeaSEd Tanous     item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"});
217107077deSPrzemyslaw Czarnowski     item["TransferMethod"] = "Stream";
218d04ba325SPrzemyslaw Czarnowski     item["Oem"]["OpenBMC"]["@odata.type"] =
219d04ba325SPrzemyslaw Czarnowski         "#OemVirtualMedia.v1_0_0.VirtualMedia";
220107077deSPrzemyslaw Czarnowski 
221107077deSPrzemyslaw Czarnowski     return item;
222107077deSPrzemyslaw Czarnowski }
223107077deSPrzemyslaw Czarnowski 
224107077deSPrzemyslaw Czarnowski /**
225107077deSPrzemyslaw Czarnowski  *  @brief Fills collection data
226107077deSPrzemyslaw Czarnowski  */
22722db1728SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> aResp,
228107077deSPrzemyslaw Czarnowski                               const std::string& service,
229107077deSPrzemyslaw Czarnowski                               const std::string& name)
230107077deSPrzemyslaw Czarnowski {
231107077deSPrzemyslaw Czarnowski     BMCWEB_LOG_DEBUG << "Get available Virtual Media resources.";
232107077deSPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
23302cad96eSEd Tanous         [name, aResp{std::move(aResp)}](
2345e7e2dc5SEd Tanous             const boost::system::error_code& ec,
23502cad96eSEd Tanous             const dbus::utility::ManagedObjectType& subtree) {
236107077deSPrzemyslaw Czarnowski         if (ec)
237107077deSPrzemyslaw Czarnowski         {
238107077deSPrzemyslaw Czarnowski             BMCWEB_LOG_DEBUG << "DBUS response error";
239107077deSPrzemyslaw Czarnowski             return;
240107077deSPrzemyslaw Czarnowski         }
241107077deSPrzemyslaw Czarnowski         nlohmann::json& members = aResp->res.jsonValue["Members"];
242107077deSPrzemyslaw Czarnowski         members = nlohmann::json::array();
243107077deSPrzemyslaw Czarnowski 
244107077deSPrzemyslaw Czarnowski         for (const auto& object : subtree)
245107077deSPrzemyslaw Czarnowski         {
246107077deSPrzemyslaw Czarnowski             nlohmann::json item;
2472dfd18efSEd Tanous             std::string path = object.first.filename();
2482dfd18efSEd Tanous             if (path.empty())
249107077deSPrzemyslaw Czarnowski             {
250107077deSPrzemyslaw Czarnowski                 continue;
251107077deSPrzemyslaw Czarnowski             }
252107077deSPrzemyslaw Czarnowski 
253fdb20347SEd Tanous             item["@odata.id"] = crow::utility::urlFromPieces(
254fdb20347SEd Tanous                 "redfish", "v1", "Managers", name, "VirtualMedia", path);
255107077deSPrzemyslaw Czarnowski             members.emplace_back(std::move(item));
256107077deSPrzemyslaw Czarnowski         }
257107077deSPrzemyslaw Czarnowski         aResp->res.jsonValue["Members@odata.count"] = members.size();
258107077deSPrzemyslaw Czarnowski         },
259107077deSPrzemyslaw Czarnowski         service, "/xyz/openbmc_project/VirtualMedia",
260107077deSPrzemyslaw Czarnowski         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
261107077deSPrzemyslaw Czarnowski }
262107077deSPrzemyslaw Czarnowski 
263107077deSPrzemyslaw Czarnowski /**
264107077deSPrzemyslaw Czarnowski  *  @brief Fills data for specific resource
265107077deSPrzemyslaw Czarnowski  */
26622db1728SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
267107077deSPrzemyslaw Czarnowski                       const std::string& service, const std::string& name,
268107077deSPrzemyslaw Czarnowski                       const std::string& resName)
269107077deSPrzemyslaw Czarnowski {
270107077deSPrzemyslaw Czarnowski     BMCWEB_LOG_DEBUG << "Get Virtual Media resource data.";
271107077deSPrzemyslaw Czarnowski 
272107077deSPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
273914e2d5dSEd Tanous         [resName, name,
2745e7e2dc5SEd Tanous          aResp](const boost::system::error_code& ec,
275914e2d5dSEd Tanous                 const dbus::utility::ManagedObjectType& subtree) {
276107077deSPrzemyslaw Czarnowski         if (ec)
277107077deSPrzemyslaw Czarnowski         {
278107077deSPrzemyslaw Czarnowski             BMCWEB_LOG_DEBUG << "DBUS response error";
279e13c2760SPrzemyslaw Czarnowski 
280107077deSPrzemyslaw Czarnowski             return;
281107077deSPrzemyslaw Czarnowski         }
282107077deSPrzemyslaw Czarnowski 
283914e2d5dSEd Tanous         for (const auto& item : subtree)
284107077deSPrzemyslaw Czarnowski         {
285365a73f4SEd Tanous             VmMode mode = parseObjectPathAndGetMode(item.first, resName);
286365a73f4SEd Tanous             if (mode == VmMode::Invalid)
2871a6258dcSPrzemyslaw Czarnowski             {
2881a6258dcSPrzemyslaw Czarnowski                 continue;
2891a6258dcSPrzemyslaw Czarnowski             }
2901a6258dcSPrzemyslaw Czarnowski 
291107077deSPrzemyslaw Czarnowski             aResp->res.jsonValue = vmItemTemplate(name, resName);
292107077deSPrzemyslaw Czarnowski 
293e13c2760SPrzemyslaw Czarnowski             // Check if dbus path is Legacy type
294365a73f4SEd Tanous             if (mode == VmMode::Legacy)
295e13c2760SPrzemyslaw Czarnowski             {
296e13c2760SPrzemyslaw Czarnowski                 aResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"]
297fdb20347SEd Tanous                                     ["target"] = crow::utility::urlFromPieces(
298fdb20347SEd Tanous                     "redfish", "v1", "Managers", name, "VirtualMedia", resName,
299fdb20347SEd Tanous                     "Actions", "VirtualMedia.InsertMedia");
300e13c2760SPrzemyslaw Czarnowski             }
301e13c2760SPrzemyslaw Czarnowski 
302107077deSPrzemyslaw Czarnowski             vmParseInterfaceObject(item.second, aResp);
303107077deSPrzemyslaw Czarnowski 
304002d39b4SEd Tanous             aResp->res
305002d39b4SEd Tanous                 .jsonValue["Actions"]["#VirtualMedia.EjectMedia"]["target"] =
306fdb20347SEd Tanous                 crow::utility::urlFromPieces("redfish", "v1", "Managers", name,
307fdb20347SEd Tanous                                              "VirtualMedia", resName, "Actions",
308fdb20347SEd Tanous                                              "VirtualMedia.EjectMedia");
309107077deSPrzemyslaw Czarnowski             return;
310107077deSPrzemyslaw Czarnowski         }
311107077deSPrzemyslaw Czarnowski 
312d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(aResp->res, "VirtualMedia", resName);
313107077deSPrzemyslaw Czarnowski         },
314107077deSPrzemyslaw Czarnowski         service, "/xyz/openbmc_project/VirtualMedia",
315107077deSPrzemyslaw Czarnowski         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
316107077deSPrzemyslaw Czarnowski }
317107077deSPrzemyslaw Czarnowski 
318e13c2760SPrzemyslaw Czarnowski /**
319c6f4e017SAgata Olender  * @brief Transfer protocols supported for InsertMedia action.
320c6f4e017SAgata Olender  *
321c6f4e017SAgata Olender  */
322c6f4e017SAgata Olender enum class TransferProtocol
323c6f4e017SAgata Olender {
324c6f4e017SAgata Olender     https,
325c6f4e017SAgata Olender     smb,
326c6f4e017SAgata Olender     invalid
327c6f4e017SAgata Olender };
328c6f4e017SAgata Olender 
329c6f4e017SAgata Olender /**
330c6f4e017SAgata Olender  * @brief Function extracts transfer protocol type from URI.
331c6f4e017SAgata Olender  *
332c6f4e017SAgata Olender  */
33367df073bSEd Tanous inline std::optional<TransferProtocol>
334ace85d60SEd Tanous     getTransferProtocolFromUri(const boost::urls::url_view& imageUri)
33567df073bSEd Tanous {
336079360aeSEd Tanous     std::string_view scheme = imageUri.scheme();
33767df073bSEd Tanous     if (scheme == "smb")
33867df073bSEd Tanous     {
33967df073bSEd Tanous         return TransferProtocol::smb;
34067df073bSEd Tanous     }
34167df073bSEd Tanous     if (scheme == "https")
34267df073bSEd Tanous     {
34367df073bSEd Tanous         return TransferProtocol::https;
34467df073bSEd Tanous     }
34567df073bSEd Tanous     if (!scheme.empty())
34667df073bSEd Tanous     {
34767df073bSEd Tanous         return TransferProtocol::invalid;
34867df073bSEd Tanous     }
34967df073bSEd Tanous 
35067df073bSEd Tanous     return {};
35167df073bSEd Tanous }
352c6f4e017SAgata Olender 
353c6f4e017SAgata Olender /**
354c6f4e017SAgata Olender  * @brief Function convert transfer protocol from string param.
355c6f4e017SAgata Olender  *
356c6f4e017SAgata Olender  */
35722db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam(
358c6f4e017SAgata Olender     const std::optional<std::string>& transferProtocolType)
359c6f4e017SAgata Olender {
360c6f4e017SAgata Olender     if (transferProtocolType == std::nullopt)
361c6f4e017SAgata Olender     {
362c6f4e017SAgata Olender         return {};
363c6f4e017SAgata Olender     }
364c6f4e017SAgata Olender 
365c6f4e017SAgata Olender     if (*transferProtocolType == "CIFS")
366c6f4e017SAgata Olender     {
367c6f4e017SAgata Olender         return TransferProtocol::smb;
368c6f4e017SAgata Olender     }
369c6f4e017SAgata Olender 
370c6f4e017SAgata Olender     if (*transferProtocolType == "HTTPS")
371c6f4e017SAgata Olender     {
372c6f4e017SAgata Olender         return TransferProtocol::https;
373c6f4e017SAgata Olender     }
374c6f4e017SAgata Olender 
375c6f4e017SAgata Olender     return TransferProtocol::invalid;
376c6f4e017SAgata Olender }
377c6f4e017SAgata Olender 
378c6f4e017SAgata Olender /**
379c6f4e017SAgata Olender  * @brief Function extends URI with transfer protocol type.
380c6f4e017SAgata Olender  *
381c6f4e017SAgata Olender  */
38222db1728SEd Tanous inline std::string
383c6f4e017SAgata Olender     getUriWithTransferProtocol(const std::string& imageUri,
384c6f4e017SAgata Olender                                const TransferProtocol& transferProtocol)
385c6f4e017SAgata Olender {
386c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::smb)
387c6f4e017SAgata Olender     {
388c6f4e017SAgata Olender         return "smb://" + imageUri;
389c6f4e017SAgata Olender     }
390c6f4e017SAgata Olender 
391c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::https)
392c6f4e017SAgata Olender     {
393c6f4e017SAgata Olender         return "https://" + imageUri;
394c6f4e017SAgata Olender     }
395c6f4e017SAgata Olender 
396c6f4e017SAgata Olender     return imageUri;
397c6f4e017SAgata Olender }
398c6f4e017SAgata Olender 
3991f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams
4001f2a40ceSPrzemyslaw Czarnowski {
401120fa86aSPrzemyslaw Czarnowski     std::optional<std::string> imageUrl;
4021f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> userName;
4031f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> password;
4041f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferMethod;
4051f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferProtocolType;
4061f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> writeProtected = true;
4071f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> inserted;
4081f2a40ceSPrzemyslaw Czarnowski };
4091f2a40ceSPrzemyslaw Czarnowski 
4101214b7e7SGunnar Mills template <typename T>
4111214b7e7SGunnar Mills static void secureCleanup(T& value)
412988fb7b2SAdrian Ambrożewicz {
4134ecc618fSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
414988fb7b2SAdrian Ambrożewicz     auto raw = const_cast<typename T::value_type*>(value.data());
415988fb7b2SAdrian Ambrożewicz     explicit_bzero(raw, value.size() * sizeof(*raw));
416988fb7b2SAdrian Ambrożewicz }
417988fb7b2SAdrian Ambrożewicz 
418988fb7b2SAdrian Ambrożewicz class Credentials
419988fb7b2SAdrian Ambrożewicz {
420988fb7b2SAdrian Ambrożewicz   public:
421988fb7b2SAdrian Ambrożewicz     Credentials(std::string&& user, std::string&& password) :
422988fb7b2SAdrian Ambrożewicz         userBuf(std::move(user)), passBuf(std::move(password))
4231214b7e7SGunnar Mills     {}
424988fb7b2SAdrian Ambrożewicz 
425988fb7b2SAdrian Ambrożewicz     ~Credentials()
426988fb7b2SAdrian Ambrożewicz     {
427988fb7b2SAdrian Ambrożewicz         secureCleanup(userBuf);
428988fb7b2SAdrian Ambrożewicz         secureCleanup(passBuf);
429988fb7b2SAdrian Ambrożewicz     }
430988fb7b2SAdrian Ambrożewicz 
431988fb7b2SAdrian Ambrożewicz     const std::string& user()
432988fb7b2SAdrian Ambrożewicz     {
433988fb7b2SAdrian Ambrożewicz         return userBuf;
434988fb7b2SAdrian Ambrożewicz     }
435988fb7b2SAdrian Ambrożewicz 
436988fb7b2SAdrian Ambrożewicz     const std::string& password()
437988fb7b2SAdrian Ambrożewicz     {
438988fb7b2SAdrian Ambrożewicz         return passBuf;
439988fb7b2SAdrian Ambrożewicz     }
440988fb7b2SAdrian Ambrożewicz 
441988fb7b2SAdrian Ambrożewicz     Credentials() = delete;
442988fb7b2SAdrian Ambrożewicz     Credentials(const Credentials&) = delete;
443988fb7b2SAdrian Ambrożewicz     Credentials& operator=(const Credentials&) = delete;
444ecd6a3a2SEd Tanous     Credentials(Credentials&&) = delete;
445ecd6a3a2SEd Tanous     Credentials& operator=(Credentials&&) = delete;
446988fb7b2SAdrian Ambrożewicz 
44722db1728SEd Tanous   private:
448988fb7b2SAdrian Ambrożewicz     std::string userBuf;
449988fb7b2SAdrian Ambrożewicz     std::string passBuf;
450988fb7b2SAdrian Ambrożewicz };
451988fb7b2SAdrian Ambrożewicz 
452988fb7b2SAdrian Ambrożewicz class CredentialsProvider
453988fb7b2SAdrian Ambrożewicz {
454988fb7b2SAdrian Ambrożewicz   public:
4551214b7e7SGunnar Mills     template <typename T>
4561214b7e7SGunnar Mills     struct Deleter
457988fb7b2SAdrian Ambrożewicz     {
458988fb7b2SAdrian Ambrożewicz         void operator()(T* buff) const
459988fb7b2SAdrian Ambrożewicz         {
460988fb7b2SAdrian Ambrożewicz             if (buff)
461988fb7b2SAdrian Ambrożewicz             {
462988fb7b2SAdrian Ambrożewicz                 secureCleanup(*buff);
463988fb7b2SAdrian Ambrożewicz                 delete buff;
464988fb7b2SAdrian Ambrożewicz             }
465988fb7b2SAdrian Ambrożewicz         }
466988fb7b2SAdrian Ambrożewicz     };
467988fb7b2SAdrian Ambrożewicz 
468988fb7b2SAdrian Ambrożewicz     using Buffer = std::vector<char>;
469988fb7b2SAdrian Ambrożewicz     using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>;
470988fb7b2SAdrian Ambrożewicz     // Using explicit definition instead of std::function to avoid implicit
471988fb7b2SAdrian Ambrożewicz     // conversions eg. stack copy instead of reference
472988fb7b2SAdrian Ambrożewicz     using FormatterFunc = void(const std::string& username,
473988fb7b2SAdrian Ambrożewicz                                const std::string& password, Buffer& dest);
474988fb7b2SAdrian Ambrożewicz 
475988fb7b2SAdrian Ambrożewicz     CredentialsProvider(std::string&& user, std::string&& password) :
476988fb7b2SAdrian Ambrożewicz         credentials(std::move(user), std::move(password))
4771214b7e7SGunnar Mills     {}
478988fb7b2SAdrian Ambrożewicz 
479988fb7b2SAdrian Ambrożewicz     const std::string& user()
480988fb7b2SAdrian Ambrożewicz     {
481988fb7b2SAdrian Ambrożewicz         return credentials.user();
482988fb7b2SAdrian Ambrożewicz     }
483988fb7b2SAdrian Ambrożewicz 
484988fb7b2SAdrian Ambrożewicz     const std::string& password()
485988fb7b2SAdrian Ambrożewicz     {
486988fb7b2SAdrian Ambrożewicz         return credentials.password();
487988fb7b2SAdrian Ambrożewicz     }
488988fb7b2SAdrian Ambrożewicz 
4891917ee95SEd Tanous     SecureBuffer pack(FormatterFunc* formatter)
490988fb7b2SAdrian Ambrożewicz     {
491988fb7b2SAdrian Ambrożewicz         SecureBuffer packed{new Buffer{}};
492e662eae8SEd Tanous         if (formatter != nullptr)
493988fb7b2SAdrian Ambrożewicz         {
494988fb7b2SAdrian Ambrożewicz             formatter(credentials.user(), credentials.password(), *packed);
495988fb7b2SAdrian Ambrożewicz         }
496988fb7b2SAdrian Ambrożewicz 
497988fb7b2SAdrian Ambrożewicz         return packed;
498988fb7b2SAdrian Ambrożewicz     }
499988fb7b2SAdrian Ambrożewicz 
500988fb7b2SAdrian Ambrożewicz   private:
501988fb7b2SAdrian Ambrożewicz     Credentials credentials;
502988fb7b2SAdrian Ambrożewicz };
503988fb7b2SAdrian Ambrożewicz 
504988fb7b2SAdrian Ambrożewicz // Wrapper for boost::async_pipe ensuring proper pipe cleanup
505*0a48306bSEd Tanous class SecurePipe
506988fb7b2SAdrian Ambrożewicz {
507988fb7b2SAdrian Ambrożewicz   public:
508988fb7b2SAdrian Ambrożewicz     using unix_fd = sdbusplus::message::unix_fd;
509988fb7b2SAdrian Ambrożewicz 
510*0a48306bSEd Tanous     SecurePipe(boost::asio::io_context& io,
511*0a48306bSEd Tanous                CredentialsProvider::SecureBuffer&& bufferIn) :
512*0a48306bSEd Tanous         impl(io),
513*0a48306bSEd Tanous         buffer{std::move(bufferIn)}
5141214b7e7SGunnar Mills     {}
515988fb7b2SAdrian Ambrożewicz 
516*0a48306bSEd Tanous     ~SecurePipe()
517988fb7b2SAdrian Ambrożewicz     {
518988fb7b2SAdrian Ambrożewicz         // Named pipe needs to be explicitly removed
519988fb7b2SAdrian Ambrożewicz         impl.close();
520988fb7b2SAdrian Ambrożewicz     }
521988fb7b2SAdrian Ambrożewicz 
522*0a48306bSEd Tanous     SecurePipe(const SecurePipe&) = delete;
523*0a48306bSEd Tanous     SecurePipe(SecurePipe&&) = delete;
524*0a48306bSEd Tanous     SecurePipe& operator=(const SecurePipe&) = delete;
525*0a48306bSEd Tanous     SecurePipe& operator=(SecurePipe&&) = delete;
526ecd6a3a2SEd Tanous 
527*0a48306bSEd Tanous     unix_fd fd() const
528988fb7b2SAdrian Ambrożewicz     {
529988fb7b2SAdrian Ambrożewicz         return unix_fd{impl.native_source()};
530988fb7b2SAdrian Ambrożewicz     }
531988fb7b2SAdrian Ambrożewicz 
532988fb7b2SAdrian Ambrożewicz     template <typename WriteHandler>
53381ce609eSEd Tanous     void asyncWrite(WriteHandler&& handler)
534988fb7b2SAdrian Ambrożewicz     {
535*0a48306bSEd Tanous         impl.async_write_some(boost::asio::buffer(*buffer),
536*0a48306bSEd Tanous                               std::forward<WriteHandler>(handler));
537988fb7b2SAdrian Ambrożewicz     }
538988fb7b2SAdrian Ambrożewicz 
539988fb7b2SAdrian Ambrożewicz     const std::string name;
540988fb7b2SAdrian Ambrożewicz     boost::process::async_pipe impl;
541*0a48306bSEd Tanous     CredentialsProvider::SecureBuffer buffer;
542988fb7b2SAdrian Ambrożewicz };
543988fb7b2SAdrian Ambrożewicz 
544e13c2760SPrzemyslaw Czarnowski /**
545e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
546e13c2760SPrzemyslaw Czarnowski  *
547e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
548e13c2760SPrzemyslaw Czarnowski  */
54922db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
550e13c2760SPrzemyslaw Czarnowski                             const std::string& service, const std::string& name,
551988fb7b2SAdrian Ambrożewicz                             const std::string& imageUrl, const bool rw,
552988fb7b2SAdrian Ambrożewicz                             std::string&& userName, std::string&& password)
553e13c2760SPrzemyslaw Czarnowski {
554988fb7b2SAdrian Ambrożewicz     constexpr const size_t secretLimit = 1024;
555988fb7b2SAdrian Ambrożewicz 
556988fb7b2SAdrian Ambrożewicz     std::shared_ptr<SecurePipe> secretPipe;
557168e20c1SEd Tanous     dbus::utility::DbusVariantType unixFd = -1;
558988fb7b2SAdrian Ambrożewicz 
559988fb7b2SAdrian Ambrożewicz     if (!userName.empty() || !password.empty())
560988fb7b2SAdrian Ambrożewicz     {
561988fb7b2SAdrian Ambrożewicz         // Encapsulate in safe buffer
562988fb7b2SAdrian Ambrożewicz         CredentialsProvider credentials(std::move(userName),
563988fb7b2SAdrian Ambrożewicz                                         std::move(password));
564988fb7b2SAdrian Ambrożewicz 
565988fb7b2SAdrian Ambrożewicz         // Payload must contain data + NULL delimiters
566988fb7b2SAdrian Ambrożewicz         if (credentials.user().size() + credentials.password().size() + 2 >
567988fb7b2SAdrian Ambrożewicz             secretLimit)
568988fb7b2SAdrian Ambrożewicz         {
569988fb7b2SAdrian Ambrożewicz             BMCWEB_LOG_ERROR << "Credentials too long to handle";
570988fb7b2SAdrian Ambrożewicz             messages::unrecognizedRequestBody(asyncResp->res);
571988fb7b2SAdrian Ambrożewicz             return;
572988fb7b2SAdrian Ambrożewicz         }
573988fb7b2SAdrian Ambrożewicz 
574988fb7b2SAdrian Ambrożewicz         // Pack secret
57522db1728SEd Tanous         auto secret = credentials.pack(
57622db1728SEd Tanous             [](const auto& user, const auto& pass, auto& buff) {
577988fb7b2SAdrian Ambrożewicz             std::copy(user.begin(), user.end(), std::back_inserter(buff));
578988fb7b2SAdrian Ambrożewicz             buff.push_back('\0');
579988fb7b2SAdrian Ambrożewicz             std::copy(pass.begin(), pass.end(), std::back_inserter(buff));
580988fb7b2SAdrian Ambrożewicz             buff.push_back('\0');
581988fb7b2SAdrian Ambrożewicz         });
582988fb7b2SAdrian Ambrożewicz 
583988fb7b2SAdrian Ambrożewicz         // Open pipe
584988fb7b2SAdrian Ambrożewicz         secretPipe = std::make_shared<SecurePipe>(
58522db1728SEd Tanous             crow::connections::systemBus->get_io_context(), std::move(secret));
586988fb7b2SAdrian Ambrożewicz         unixFd = secretPipe->fd();
587988fb7b2SAdrian Ambrożewicz 
588988fb7b2SAdrian Ambrożewicz         // Pass secret over pipe
58981ce609eSEd Tanous         secretPipe->asyncWrite(
590f5b16f03SVikram Bodireddy             [asyncResp](const boost::system::error_code& ec, std::size_t) {
591988fb7b2SAdrian Ambrożewicz             if (ec)
592988fb7b2SAdrian Ambrożewicz             {
593988fb7b2SAdrian Ambrożewicz                 BMCWEB_LOG_ERROR << "Failed to pass secret: " << ec;
594988fb7b2SAdrian Ambrożewicz                 messages::internalError(asyncResp->res);
595988fb7b2SAdrian Ambrożewicz             }
596988fb7b2SAdrian Ambrożewicz         });
597988fb7b2SAdrian Ambrożewicz     }
598988fb7b2SAdrian Ambrożewicz 
599e13c2760SPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
6005e7e2dc5SEd Tanous         [asyncResp, secretPipe](const boost::system::error_code& ec,
601988fb7b2SAdrian Ambrożewicz                                 bool success) {
602e13c2760SPrzemyslaw Czarnowski         if (ec)
603e13c2760SPrzemyslaw Czarnowski         {
604e13c2760SPrzemyslaw Czarnowski             BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
605e13c2760SPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
606d6da5bebSAdrian Ambrożewicz         }
607d6da5bebSAdrian Ambrożewicz         else if (!success)
608d6da5bebSAdrian Ambrożewicz         {
609d6da5bebSAdrian Ambrożewicz             BMCWEB_LOG_ERROR << "Service responded with error";
610d6da5bebSAdrian Ambrożewicz             messages::generalError(asyncResp->res);
611e13c2760SPrzemyslaw Czarnowski         }
612e13c2760SPrzemyslaw Czarnowski         },
613e13c2760SPrzemyslaw Czarnowski         service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
614988fb7b2SAdrian Ambrożewicz         "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw,
615988fb7b2SAdrian Ambrożewicz         unixFd);
616e13c2760SPrzemyslaw Czarnowski }
617e13c2760SPrzemyslaw Czarnowski 
618e13c2760SPrzemyslaw Czarnowski /**
619120fa86aSPrzemyslaw Czarnowski  * @brief Function validate parameters of insert media request.
620120fa86aSPrzemyslaw Czarnowski  *
621120fa86aSPrzemyslaw Czarnowski  */
622120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
623120fa86aSPrzemyslaw Czarnowski                            const std::string& service,
624120fa86aSPrzemyslaw Czarnowski                            const std::string& resName,
625120fa86aSPrzemyslaw Czarnowski                            InsertMediaActionParams& actionParams)
626120fa86aSPrzemyslaw Czarnowski {
627120fa86aSPrzemyslaw Czarnowski     BMCWEB_LOG_DEBUG << "Validation started";
628120fa86aSPrzemyslaw Czarnowski     // required param imageUrl must not be empty
629120fa86aSPrzemyslaw Czarnowski     if (!actionParams.imageUrl)
630120fa86aSPrzemyslaw Czarnowski     {
631120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action parameter Image is empty.";
632120fa86aSPrzemyslaw Czarnowski 
633120fa86aSPrzemyslaw Czarnowski         messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image");
634120fa86aSPrzemyslaw Czarnowski 
635120fa86aSPrzemyslaw Czarnowski         return;
636120fa86aSPrzemyslaw Czarnowski     }
637120fa86aSPrzemyslaw Czarnowski 
638120fa86aSPrzemyslaw Czarnowski     // optional param inserted must be true
639120fa86aSPrzemyslaw Czarnowski     if ((actionParams.inserted != std::nullopt) && !*actionParams.inserted)
640120fa86aSPrzemyslaw Czarnowski     {
641120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR
642120fa86aSPrzemyslaw Czarnowski             << "Request action optional parameter Inserted must be true.";
643120fa86aSPrzemyslaw Czarnowski 
644120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "Inserted",
645120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
646120fa86aSPrzemyslaw Czarnowski 
647120fa86aSPrzemyslaw Czarnowski         return;
648120fa86aSPrzemyslaw Czarnowski     }
649120fa86aSPrzemyslaw Czarnowski 
650120fa86aSPrzemyslaw Czarnowski     // optional param transferMethod must be stream
651120fa86aSPrzemyslaw Czarnowski     if ((actionParams.transferMethod != std::nullopt) &&
652120fa86aSPrzemyslaw Czarnowski         (*actionParams.transferMethod != "Stream"))
653120fa86aSPrzemyslaw Czarnowski     {
654120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action optional parameter "
655120fa86aSPrzemyslaw Czarnowski                             "TransferMethod must be Stream.";
656120fa86aSPrzemyslaw Czarnowski 
657120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "TransferMethod",
658120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
659120fa86aSPrzemyslaw Czarnowski 
660120fa86aSPrzemyslaw Czarnowski         return;
661120fa86aSPrzemyslaw Czarnowski     }
662120fa86aSPrzemyslaw Czarnowski     boost::urls::result<boost::urls::url_view> url =
663120fa86aSPrzemyslaw Czarnowski         boost::urls::parse_uri(*actionParams.imageUrl);
664120fa86aSPrzemyslaw Czarnowski     if (!url)
665120fa86aSPrzemyslaw Czarnowski     {
666120fa86aSPrzemyslaw Czarnowski         messages::actionParameterValueFormatError(
667120fa86aSPrzemyslaw Czarnowski             asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia");
668120fa86aSPrzemyslaw Czarnowski         return;
669120fa86aSPrzemyslaw Czarnowski     }
670120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> uriTransferProtocolType =
671120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromUri(*url);
672120fa86aSPrzemyslaw Czarnowski 
673120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> paramTransferProtocolType =
674120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromParam(actionParams.transferProtocolType);
675120fa86aSPrzemyslaw Czarnowski 
676120fa86aSPrzemyslaw Czarnowski     // ImageUrl does not contain valid protocol type
677120fa86aSPrzemyslaw Czarnowski     if (*uriTransferProtocolType == TransferProtocol::invalid)
678120fa86aSPrzemyslaw Czarnowski     {
679120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must "
680120fa86aSPrzemyslaw Czarnowski                             "contain specified protocol type from list: "
681120fa86aSPrzemyslaw Czarnowski                             "(smb, https).";
682120fa86aSPrzemyslaw Czarnowski 
683120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
684120fa86aSPrzemyslaw Czarnowski 
685120fa86aSPrzemyslaw Czarnowski         return;
686120fa86aSPrzemyslaw Czarnowski     }
687120fa86aSPrzemyslaw Czarnowski 
688120fa86aSPrzemyslaw Czarnowski     // transferProtocolType should contain value from list
689120fa86aSPrzemyslaw Czarnowski     if (*paramTransferProtocolType == TransferProtocol::invalid)
690120fa86aSPrzemyslaw Czarnowski     {
691120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action parameter TransferProtocolType "
692120fa86aSPrzemyslaw Czarnowski                             "must be provided with value from list: "
693120fa86aSPrzemyslaw Czarnowski                             "(CIFS, HTTPS).";
694120fa86aSPrzemyslaw Czarnowski 
695120fa86aSPrzemyslaw Czarnowski         messages::propertyValueNotInList(asyncResp->res,
696120fa86aSPrzemyslaw Czarnowski                                          *actionParams.transferProtocolType,
697120fa86aSPrzemyslaw Czarnowski                                          "TransferProtocolType");
698120fa86aSPrzemyslaw Czarnowski         return;
699120fa86aSPrzemyslaw Czarnowski     }
700120fa86aSPrzemyslaw Czarnowski 
701120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol not provided either with URI nor param
702120fa86aSPrzemyslaw Czarnowski     if ((uriTransferProtocolType == std::nullopt) &&
703120fa86aSPrzemyslaw Czarnowski         (paramTransferProtocolType == std::nullopt))
704120fa86aSPrzemyslaw Czarnowski     {
705120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must "
706120fa86aSPrzemyslaw Czarnowski                             "contain specified protocol type or param "
707120fa86aSPrzemyslaw Czarnowski                             "TransferProtocolType must be provided.";
708120fa86aSPrzemyslaw Czarnowski 
709120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
710120fa86aSPrzemyslaw Czarnowski 
711120fa86aSPrzemyslaw Czarnowski         return;
712120fa86aSPrzemyslaw Czarnowski     }
713120fa86aSPrzemyslaw Czarnowski 
714120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol provided both with URI and param
715120fa86aSPrzemyslaw Czarnowski     if ((paramTransferProtocolType != std::nullopt) &&
716120fa86aSPrzemyslaw Czarnowski         (uriTransferProtocolType != std::nullopt))
717120fa86aSPrzemyslaw Czarnowski     {
718120fa86aSPrzemyslaw Czarnowski         // check if protocol is the same for URI and param
719120fa86aSPrzemyslaw Czarnowski         if (*paramTransferProtocolType != *uriTransferProtocolType)
720120fa86aSPrzemyslaw Czarnowski         {
721120fa86aSPrzemyslaw Czarnowski             BMCWEB_LOG_ERROR << "Request action parameter "
722120fa86aSPrzemyslaw Czarnowski                                 "TransferProtocolType must  contain the "
723120fa86aSPrzemyslaw Czarnowski                                 "same protocol type as protocol type "
724120fa86aSPrzemyslaw Czarnowski                                 "provided with param imageUrl.";
725120fa86aSPrzemyslaw Czarnowski 
726120fa86aSPrzemyslaw Czarnowski             messages::actionParameterValueTypeError(
727120fa86aSPrzemyslaw Czarnowski                 asyncResp->res, *actionParams.transferProtocolType,
728120fa86aSPrzemyslaw Czarnowski                 "TransferProtocolType", "InsertMedia");
729120fa86aSPrzemyslaw Czarnowski 
730120fa86aSPrzemyslaw Czarnowski             return;
731120fa86aSPrzemyslaw Czarnowski         }
732120fa86aSPrzemyslaw Czarnowski     }
733120fa86aSPrzemyslaw Czarnowski 
734120fa86aSPrzemyslaw Czarnowski     // validation passed, add protocol to URI if needed
735120fa86aSPrzemyslaw Czarnowski     if (uriTransferProtocolType == std::nullopt)
736120fa86aSPrzemyslaw Czarnowski     {
737120fa86aSPrzemyslaw Czarnowski         actionParams.imageUrl = getUriWithTransferProtocol(
738120fa86aSPrzemyslaw Czarnowski             *actionParams.imageUrl, *paramTransferProtocolType);
739120fa86aSPrzemyslaw Czarnowski     }
740120fa86aSPrzemyslaw Czarnowski 
741120fa86aSPrzemyslaw Czarnowski     doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
742120fa86aSPrzemyslaw Czarnowski                     !(*actionParams.writeProtected),
743120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.userName),
744120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.password));
745120fa86aSPrzemyslaw Czarnowski }
746120fa86aSPrzemyslaw Czarnowski 
747120fa86aSPrzemyslaw Czarnowski /**
748e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
749e13c2760SPrzemyslaw Czarnowski  *
750e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
751e13c2760SPrzemyslaw Czarnowski  */
75224e740a7SEd Tanous inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
753e13c2760SPrzemyslaw Czarnowski                           const std::string& service, const std::string& name,
754e13c2760SPrzemyslaw Czarnowski                           bool legacy)
755e13c2760SPrzemyslaw Czarnowski {
756e13c2760SPrzemyslaw Czarnowski 
757e13c2760SPrzemyslaw Czarnowski     // Legacy mount requires parameter with image
758e13c2760SPrzemyslaw Czarnowski     if (legacy)
759e13c2760SPrzemyslaw Czarnowski     {
760e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
7615e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
762e13c2760SPrzemyslaw Czarnowski             if (ec)
763e13c2760SPrzemyslaw Czarnowski             {
764e13c2760SPrzemyslaw Czarnowski                 BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
765e13c2760SPrzemyslaw Czarnowski 
766e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
767e13c2760SPrzemyslaw Czarnowski                 return;
768e13c2760SPrzemyslaw Czarnowski             }
769e13c2760SPrzemyslaw Czarnowski             },
770e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
771e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount");
772e13c2760SPrzemyslaw Czarnowski     }
773e13c2760SPrzemyslaw Czarnowski     else // proxy
774e13c2760SPrzemyslaw Czarnowski     {
775e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
7765e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
777e13c2760SPrzemyslaw Czarnowski             if (ec)
778e13c2760SPrzemyslaw Czarnowski             {
779e13c2760SPrzemyslaw Czarnowski                 BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
780e13c2760SPrzemyslaw Czarnowski 
781e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
782e13c2760SPrzemyslaw Czarnowski                 return;
783e13c2760SPrzemyslaw Czarnowski             }
784e13c2760SPrzemyslaw Czarnowski             },
785e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name,
786e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
787e13c2760SPrzemyslaw Czarnowski     }
788e13c2760SPrzemyslaw Czarnowski }
789e13c2760SPrzemyslaw Czarnowski 
79096825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost(
79196825bebSEd Tanous     crow::App& app, const crow::Request& req,
79222db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
79396825bebSEd Tanous     const std::string& name, const std::string& resName)
79496825bebSEd Tanous {
7953ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
79645ca1b86SEd Tanous     {
79745ca1b86SEd Tanous         return;
79845ca1b86SEd Tanous     }
79922db1728SEd Tanous     if (name != "bmc")
800107077deSPrzemyslaw Czarnowski     {
8011f2a40ceSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, "VirtualMedia.InsertMedia",
802002d39b4SEd Tanous                                    resName);
803107077deSPrzemyslaw Czarnowski 
804107077deSPrzemyslaw Czarnowski         return;
805107077deSPrzemyslaw Czarnowski     }
806120fa86aSPrzemyslaw Czarnowski     std::optional<InsertMediaActionParams> actionParams =
807120fa86aSPrzemyslaw Czarnowski         InsertMediaActionParams();
80898be3e39SEd Tanous 
809120fa86aSPrzemyslaw Czarnowski     // Read obligatory parameters (url of image)
81015ed6780SWilly Tu     if (!json_util::readJsonAction(
811120fa86aSPrzemyslaw Czarnowski             req, asyncResp->res, "Image", actionParams->imageUrl,
812120fa86aSPrzemyslaw Czarnowski             "WriteProtected", actionParams->writeProtected, "UserName",
813120fa86aSPrzemyslaw Czarnowski             actionParams->userName, "Password", actionParams->password,
814120fa86aSPrzemyslaw Czarnowski             "Inserted", actionParams->inserted, "TransferMethod",
815120fa86aSPrzemyslaw Czarnowski             actionParams->transferMethod, "TransferProtocolType",
816120fa86aSPrzemyslaw Czarnowski             actionParams->transferProtocolType))
81798be3e39SEd Tanous     {
81898be3e39SEd Tanous         return;
81998be3e39SEd Tanous     }
820107077deSPrzemyslaw Czarnowski 
8212b73119cSGeorge Liu     dbus::utility::getDbusObject(
8222b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
82396825bebSEd Tanous         [asyncResp, actionParams,
8242b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
825002d39b4SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) mutable {
82622db1728SEd Tanous         if (ec)
82722db1728SEd Tanous         {
82896825bebSEd Tanous             BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
82922db1728SEd Tanous             messages::internalError(asyncResp->res);
830107077deSPrzemyslaw Czarnowski 
83122db1728SEd Tanous             return;
83222db1728SEd Tanous         }
83322db1728SEd Tanous         std::string service = getObjectType.begin()->first;
83422db1728SEd Tanous         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
83522db1728SEd Tanous 
83622db1728SEd Tanous         crow::connections::systemBus->async_method_call(
83798be3e39SEd Tanous             [service, resName, actionParams,
8385e7e2dc5SEd Tanous              asyncResp](const boost::system::error_code& ec2,
839002d39b4SEd Tanous                         dbus::utility::ManagedObjectType& subtree) mutable {
8408a592810SEd Tanous             if (ec2)
84122db1728SEd Tanous             {
84222db1728SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
8431f2a40ceSPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
84422db1728SEd Tanous 
84522db1728SEd Tanous                 return;
84622db1728SEd Tanous             }
84722db1728SEd Tanous 
84822db1728SEd Tanous             for (const auto& object : subtree)
84922db1728SEd Tanous             {
850365a73f4SEd Tanous                 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
851365a73f4SEd Tanous                 if (mode == VmMode::Proxy)
85222db1728SEd Tanous                 {
853120fa86aSPrzemyslaw Czarnowski                     validateParams(asyncResp, service, resName, *actionParams);
85422db1728SEd Tanous 
85522db1728SEd Tanous                     return;
85622db1728SEd Tanous                 }
85722db1728SEd Tanous             }
85822db1728SEd Tanous             BMCWEB_LOG_DEBUG << "Parent item not found";
85996825bebSEd Tanous             messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
86022db1728SEd Tanous             },
86122db1728SEd Tanous             service, "/xyz/openbmc_project/VirtualMedia",
862002d39b4SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
8632b73119cSGeorge Liu         });
86496825bebSEd Tanous }
86522db1728SEd Tanous 
86696825bebSEd Tanous inline void handleManagersVirtualMediaActionEject(
86796825bebSEd Tanous     crow::App& app, const crow::Request& req,
86822db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
86996825bebSEd Tanous     const std::string& managerName, const std::string& resName)
87096825bebSEd Tanous {
8713ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
87245ca1b86SEd Tanous     {
87345ca1b86SEd Tanous         return;
87445ca1b86SEd Tanous     }
87596825bebSEd Tanous     if (managerName != "bmc")
876107077deSPrzemyslaw Czarnowski     {
877120fa86aSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, "VirtualMedia.EjectMedia",
878002d39b4SEd Tanous                                    resName);
87922db1728SEd Tanous 
88022db1728SEd Tanous         return;
88122db1728SEd Tanous     }
88222db1728SEd Tanous 
8832b73119cSGeorge Liu     dbus::utility::getDbusObject(
8842b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
885002d39b4SEd Tanous         [asyncResp,
8862b73119cSGeorge Liu          resName](const boost::system::error_code& ec2,
887b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
8888a592810SEd Tanous         if (ec2)
88922db1728SEd Tanous         {
8908a592810SEd Tanous             BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec2;
89122db1728SEd Tanous             messages::internalError(asyncResp->res);
89222db1728SEd Tanous 
89322db1728SEd Tanous             return;
89422db1728SEd Tanous         }
89522db1728SEd Tanous         std::string service = getObjectType.begin()->first;
89622db1728SEd Tanous         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
89722db1728SEd Tanous 
89822db1728SEd Tanous         crow::connections::systemBus->async_method_call(
89902cad96eSEd Tanous             [resName, service, asyncResp{asyncResp}](
9005e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
90102cad96eSEd Tanous                 const dbus::utility::ManagedObjectType& subtree) {
90222db1728SEd Tanous             if (ec)
90322db1728SEd Tanous             {
90422db1728SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
9051f2a40ceSPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
90622db1728SEd Tanous 
90722db1728SEd Tanous                 return;
90822db1728SEd Tanous             }
90922db1728SEd Tanous 
91022db1728SEd Tanous             for (const auto& object : subtree)
91122db1728SEd Tanous             {
91222db1728SEd Tanous 
913365a73f4SEd Tanous                 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
914365a73f4SEd Tanous                 if (mode != VmMode::Invalid)
91522db1728SEd Tanous                 {
916365a73f4SEd Tanous                     doEjectAction(asyncResp, service, resName,
917365a73f4SEd Tanous                                   mode == VmMode::Legacy);
91822db1728SEd Tanous                 }
91922db1728SEd Tanous             }
92022db1728SEd Tanous             BMCWEB_LOG_DEBUG << "Parent item not found";
92196825bebSEd Tanous             messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
92222db1728SEd Tanous             },
92322db1728SEd Tanous             service, "/xyz/openbmc_project/VirtualMedia",
924002d39b4SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
9252b73119cSGeorge Liu         });
92696825bebSEd Tanous }
92796825bebSEd Tanous 
92896825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet(
92996825bebSEd Tanous     crow::App& app, const crow::Request& req,
93022db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
93196825bebSEd Tanous     const std::string& name)
93296825bebSEd Tanous {
9333ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
93445ca1b86SEd Tanous     {
93545ca1b86SEd Tanous         return;
93645ca1b86SEd Tanous     }
93722db1728SEd Tanous     if (name != "bmc")
93822db1728SEd Tanous     {
939002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", name);
940107077deSPrzemyslaw Czarnowski 
941107077deSPrzemyslaw Czarnowski         return;
942107077deSPrzemyslaw Czarnowski     }
943107077deSPrzemyslaw Czarnowski 
9448d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
945107077deSPrzemyslaw Czarnowski         "#VirtualMediaCollection.VirtualMediaCollection";
9468d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Virtual Media Services";
947fdb20347SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
948fdb20347SEd Tanous         "redfish", "v1", "Managers", name, "VirtualMedia");
949107077deSPrzemyslaw Czarnowski 
9502b73119cSGeorge Liu     dbus::utility::getDbusObject(
9512b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
9522b73119cSGeorge Liu         [asyncResp, name](const boost::system::error_code& ec,
953b9d36b47SEd Tanous                           const dbus::utility::MapperGetObject& getObjectType) {
954107077deSPrzemyslaw Czarnowski         if (ec)
955107077deSPrzemyslaw Czarnowski         {
95696825bebSEd Tanous             BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
957107077deSPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
958107077deSPrzemyslaw Czarnowski 
959107077deSPrzemyslaw Czarnowski             return;
960107077deSPrzemyslaw Czarnowski         }
961107077deSPrzemyslaw Czarnowski         std::string service = getObjectType.begin()->first;
962107077deSPrzemyslaw Czarnowski         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
963107077deSPrzemyslaw Czarnowski 
964107077deSPrzemyslaw Czarnowski         getVmResourceList(asyncResp, service, name);
9652b73119cSGeorge Liu         });
96696825bebSEd Tanous }
967107077deSPrzemyslaw Czarnowski 
96896825bebSEd Tanous inline void
96996825bebSEd Tanous     handleVirtualMediaGet(crow::App& app, const crow::Request& req,
97022db1728SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
97196825bebSEd Tanous                           const std::string& name, const std::string& resName)
97296825bebSEd Tanous {
9733ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
97445ca1b86SEd Tanous     {
97545ca1b86SEd Tanous         return;
97645ca1b86SEd Tanous     }
977107077deSPrzemyslaw Czarnowski     if (name != "bmc")
978107077deSPrzemyslaw Czarnowski     {
979002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
980107077deSPrzemyslaw Czarnowski 
981107077deSPrzemyslaw Czarnowski         return;
982107077deSPrzemyslaw Czarnowski     }
983107077deSPrzemyslaw Czarnowski 
9842b73119cSGeorge Liu     dbus::utility::getDbusObject(
9852b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
986002d39b4SEd Tanous         [asyncResp, name,
9872b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
988b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
989107077deSPrzemyslaw Czarnowski         if (ec)
990107077deSPrzemyslaw Czarnowski         {
99196825bebSEd Tanous             BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
992107077deSPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
993107077deSPrzemyslaw Czarnowski 
994107077deSPrzemyslaw Czarnowski             return;
995107077deSPrzemyslaw Czarnowski         }
996107077deSPrzemyslaw Czarnowski         std::string service = getObjectType.begin()->first;
997107077deSPrzemyslaw Czarnowski         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
998107077deSPrzemyslaw Czarnowski 
999107077deSPrzemyslaw Czarnowski         getVmData(asyncResp, service, name, resName);
10002b73119cSGeorge Liu         });
100196825bebSEd Tanous }
100296825bebSEd Tanous 
100396825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app)
100496825bebSEd Tanous {
100596825bebSEd Tanous     BMCWEB_ROUTE(
100696825bebSEd Tanous         app,
100796825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia")
100896825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
100996825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
101096825bebSEd Tanous             handleManagersVirtualMediaActionInsertPost, std::ref(app)));
101196825bebSEd Tanous 
101296825bebSEd Tanous     BMCWEB_ROUTE(
101396825bebSEd Tanous         app,
101496825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia")
101596825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
101696825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
101796825bebSEd Tanous             handleManagersVirtualMediaActionEject, std::ref(app)));
101896825bebSEd Tanous 
101996825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/")
102096825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMediaCollection)
102196825bebSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
102296825bebSEd Tanous             handleManagersVirtualMediaCollectionGet, std::ref(app)));
102396825bebSEd Tanous 
102496825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/")
102596825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMedia)
102696825bebSEd Tanous         .methods(boost::beast::http::verb::get)(
102796825bebSEd Tanous             std::bind_front(handleVirtualMediaGet, std::ref(app)));
1028107077deSPrzemyslaw Czarnowski }
1029107077deSPrzemyslaw Czarnowski 
1030107077deSPrzemyslaw Czarnowski } // namespace redfish
1031