xref: /openbmc/bmcweb/features/redfish/lib/virtual_media.hpp (revision 365a73f4725a6cefa3d69168ae5500fd05ff8866)
1107077deSPrzemyslaw Czarnowski /*
2107077deSPrzemyslaw Czarnowski // Copyright (c) 2018 Intel Corporation
3107077deSPrzemyslaw Czarnowski //
4107077deSPrzemyslaw Czarnowski // Licensed under the Apache License, Version 2.0 (the "License");
5107077deSPrzemyslaw Czarnowski // you may not use this file except in compliance with the License.
6107077deSPrzemyslaw Czarnowski // You may obtain a copy of the License at
7107077deSPrzemyslaw Czarnowski //
8107077deSPrzemyslaw Czarnowski //      http://www.apache.org/licenses/LICENSE-2.0
9107077deSPrzemyslaw Czarnowski //
10107077deSPrzemyslaw Czarnowski // Unless required by applicable law or agreed to in writing, software
11107077deSPrzemyslaw Czarnowski // distributed under the License is distributed on an "AS IS" BASIS,
12107077deSPrzemyslaw Czarnowski // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13107077deSPrzemyslaw Czarnowski // See the License for the specific language governing permissions and
14107077deSPrzemyslaw Czarnowski // limitations under the License.
15107077deSPrzemyslaw Czarnowski */
16107077deSPrzemyslaw Czarnowski #pragma once
17107077deSPrzemyslaw Czarnowski 
183ccb3adbSEd Tanous #include "account_service.hpp"
193ccb3adbSEd Tanous #include "app.hpp"
202b73119cSGeorge Liu #include "dbus_utility.hpp"
213ccb3adbSEd Tanous #include "query.hpp"
223ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
233ccb3adbSEd Tanous #include "utils/json_utils.hpp"
243ccb3adbSEd Tanous 
25988fb7b2SAdrian Ambrożewicz #include <boost/process/async_pipe.hpp>
26988fb7b2SAdrian Ambrożewicz #include <boost/type_traits/has_dereference.hpp>
279e319cf0SAnna Platash #include <boost/url/url_view.hpp>
28107077deSPrzemyslaw Czarnowski 
292b73119cSGeorge Liu #include <array>
302b73119cSGeorge Liu #include <string_view>
312b73119cSGeorge Liu 
32107077deSPrzemyslaw Czarnowski namespace redfish
33107077deSPrzemyslaw Czarnowski {
34*365a73f4SEd Tanous 
35*365a73f4SEd Tanous enum class VmMode
36*365a73f4SEd Tanous {
37*365a73f4SEd Tanous     Invalid,
38*365a73f4SEd Tanous     Legacy,
39*365a73f4SEd Tanous     Proxy
40*365a73f4SEd Tanous };
41*365a73f4SEd Tanous 
42*365a73f4SEd Tanous inline VmMode
43*365a73f4SEd Tanous     parseObjectPathAndGetMode(const sdbusplus::message::object_path& itemPath,
44*365a73f4SEd Tanous                               const std::string& resName)
45*365a73f4SEd Tanous {
46*365a73f4SEd Tanous     std::string thisPath = itemPath.filename();
47*365a73f4SEd Tanous     BMCWEB_LOG_DEBUG << "Filename: " << itemPath.str
48*365a73f4SEd Tanous                      << ", ThisPath: " << thisPath;
49*365a73f4SEd Tanous 
50*365a73f4SEd Tanous     if (thisPath.empty())
51*365a73f4SEd Tanous     {
52*365a73f4SEd Tanous         return VmMode::Invalid;
53*365a73f4SEd Tanous     }
54*365a73f4SEd Tanous 
55*365a73f4SEd Tanous     if (thisPath != resName)
56*365a73f4SEd Tanous     {
57*365a73f4SEd Tanous         return VmMode::Invalid;
58*365a73f4SEd Tanous     }
59*365a73f4SEd Tanous 
60*365a73f4SEd Tanous     auto mode = itemPath.parent_path();
61*365a73f4SEd Tanous     auto type = mode.parent_path();
62*365a73f4SEd Tanous 
63*365a73f4SEd Tanous     if (mode.filename().empty() || type.filename().empty())
64*365a73f4SEd Tanous     {
65*365a73f4SEd Tanous         return VmMode::Invalid;
66*365a73f4SEd Tanous     }
67*365a73f4SEd Tanous 
68*365a73f4SEd Tanous     if (type.filename() != "VirtualMedia")
69*365a73f4SEd Tanous     {
70*365a73f4SEd Tanous         return VmMode::Invalid;
71*365a73f4SEd Tanous     }
72*365a73f4SEd Tanous     std::string modeStr = mode.filename();
73*365a73f4SEd Tanous     if (modeStr == "Legacy")
74*365a73f4SEd Tanous     {
75*365a73f4SEd Tanous         return VmMode::Legacy;
76*365a73f4SEd Tanous     }
77*365a73f4SEd Tanous     if (modeStr == "Proxy")
78*365a73f4SEd Tanous     {
79*365a73f4SEd Tanous         return VmMode::Proxy;
80*365a73f4SEd Tanous     }
81*365a73f4SEd Tanous     return VmMode::Invalid;
82*365a73f4SEd Tanous }
83*365a73f4SEd 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 
161107077deSPrzemyslaw Czarnowski                         aResp->res.jsonValue["ConnectedVia"] = "URI";
162107077deSPrzemyslaw Czarnowski                     }
163107077deSPrzemyslaw Czarnowski                 }
164711ac7a9SEd Tanous                 if (property == "WriteProtected")
1659e319cf0SAnna Platash                 {
166711ac7a9SEd Tanous                     const bool* writeProtectedValue = std::get_if<bool>(&value);
167e662eae8SEd Tanous                     if (writeProtectedValue != nullptr)
1689e319cf0SAnna Platash                     {
1699e319cf0SAnna Platash                         aResp->res.jsonValue["WriteProtected"] =
1709e319cf0SAnna Platash                             *writeProtectedValue;
1719e319cf0SAnna Platash                     }
1729e319cf0SAnna Platash                 }
1739e319cf0SAnna Platash             }
174107077deSPrzemyslaw Czarnowski         }
175711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.Process")
176711ac7a9SEd Tanous         {
177711ac7a9SEd Tanous             for (const auto& [property, value] : values)
178711ac7a9SEd Tanous             {
179711ac7a9SEd Tanous                 if (property == "Active")
180711ac7a9SEd Tanous                 {
181711ac7a9SEd Tanous                     const bool* activeValue = std::get_if<bool>(&value);
182e662eae8SEd Tanous                     if (activeValue == nullptr)
183711ac7a9SEd Tanous                     {
184711ac7a9SEd Tanous                         BMCWEB_LOG_DEBUG << "Value Active not found";
185711ac7a9SEd Tanous                         return;
186711ac7a9SEd Tanous                     }
187711ac7a9SEd Tanous                     aResp->res.jsonValue["Inserted"] = *activeValue;
188711ac7a9SEd Tanous 
189e05aec50SEd Tanous                     if (*activeValue)
190711ac7a9SEd Tanous                     {
191711ac7a9SEd Tanous                         aResp->res.jsonValue["ConnectedVia"] = "Applet";
192711ac7a9SEd Tanous                     }
193711ac7a9SEd Tanous                 }
194711ac7a9SEd Tanous             }
195711ac7a9SEd Tanous         }
196107077deSPrzemyslaw Czarnowski     }
197107077deSPrzemyslaw Czarnowski }
198107077deSPrzemyslaw Czarnowski 
199107077deSPrzemyslaw Czarnowski /**
200107077deSPrzemyslaw Czarnowski  * @brief Fill template for Virtual Media Item.
201107077deSPrzemyslaw Czarnowski  */
20222db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name,
203107077deSPrzemyslaw Czarnowski                                      const std::string& resName)
204107077deSPrzemyslaw Czarnowski {
205107077deSPrzemyslaw Czarnowski     nlohmann::json item;
206fdb20347SEd Tanous     item["@odata.id"] = crow::utility::urlFromPieces(
207fdb20347SEd Tanous         "redfish", "v1", "Managers", name, "VirtualMedia", resName);
20822db1728SEd Tanous 
209d04ba325SPrzemyslaw Czarnowski     item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia";
210107077deSPrzemyslaw Czarnowski     item["Name"] = "Virtual Removable Media";
211107077deSPrzemyslaw Czarnowski     item["Id"] = resName;
212107077deSPrzemyslaw Czarnowski     item["WriteProtected"] = true;
213613dabeaSEd Tanous     item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"});
214107077deSPrzemyslaw Czarnowski     item["TransferMethod"] = "Stream";
215d04ba325SPrzemyslaw Czarnowski     item["Oem"]["OpenBMC"]["@odata.type"] =
216d04ba325SPrzemyslaw Czarnowski         "#OemVirtualMedia.v1_0_0.VirtualMedia";
217107077deSPrzemyslaw Czarnowski 
218107077deSPrzemyslaw Czarnowski     return item;
219107077deSPrzemyslaw Czarnowski }
220107077deSPrzemyslaw Czarnowski 
221107077deSPrzemyslaw Czarnowski /**
222107077deSPrzemyslaw Czarnowski  *  @brief Fills collection data
223107077deSPrzemyslaw Czarnowski  */
22422db1728SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> aResp,
225107077deSPrzemyslaw Czarnowski                               const std::string& service,
226107077deSPrzemyslaw Czarnowski                               const std::string& name)
227107077deSPrzemyslaw Czarnowski {
228107077deSPrzemyslaw Czarnowski     BMCWEB_LOG_DEBUG << "Get available Virtual Media resources.";
229107077deSPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
23002cad96eSEd Tanous         [name, aResp{std::move(aResp)}](
2315e7e2dc5SEd Tanous             const boost::system::error_code& ec,
23202cad96eSEd Tanous             const dbus::utility::ManagedObjectType& subtree) {
233107077deSPrzemyslaw Czarnowski         if (ec)
234107077deSPrzemyslaw Czarnowski         {
235107077deSPrzemyslaw Czarnowski             BMCWEB_LOG_DEBUG << "DBUS response error";
236107077deSPrzemyslaw Czarnowski             return;
237107077deSPrzemyslaw Czarnowski         }
238107077deSPrzemyslaw Czarnowski         nlohmann::json& members = aResp->res.jsonValue["Members"];
239107077deSPrzemyslaw Czarnowski         members = nlohmann::json::array();
240107077deSPrzemyslaw Czarnowski 
241107077deSPrzemyslaw Czarnowski         for (const auto& object : subtree)
242107077deSPrzemyslaw Czarnowski         {
243107077deSPrzemyslaw Czarnowski             nlohmann::json item;
2442dfd18efSEd Tanous             std::string path = object.first.filename();
2452dfd18efSEd Tanous             if (path.empty())
246107077deSPrzemyslaw Czarnowski             {
247107077deSPrzemyslaw Czarnowski                 continue;
248107077deSPrzemyslaw Czarnowski             }
249107077deSPrzemyslaw Czarnowski 
250fdb20347SEd Tanous             item["@odata.id"] = crow::utility::urlFromPieces(
251fdb20347SEd Tanous                 "redfish", "v1", "Managers", name, "VirtualMedia", path);
252107077deSPrzemyslaw Czarnowski             members.emplace_back(std::move(item));
253107077deSPrzemyslaw Czarnowski         }
254107077deSPrzemyslaw Czarnowski         aResp->res.jsonValue["Members@odata.count"] = members.size();
255107077deSPrzemyslaw Czarnowski         },
256107077deSPrzemyslaw Czarnowski         service, "/xyz/openbmc_project/VirtualMedia",
257107077deSPrzemyslaw Czarnowski         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
258107077deSPrzemyslaw Czarnowski }
259107077deSPrzemyslaw Czarnowski 
260107077deSPrzemyslaw Czarnowski /**
261107077deSPrzemyslaw Czarnowski  *  @brief Fills data for specific resource
262107077deSPrzemyslaw Czarnowski  */
26322db1728SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
264107077deSPrzemyslaw Czarnowski                       const std::string& service, const std::string& name,
265107077deSPrzemyslaw Czarnowski                       const std::string& resName)
266107077deSPrzemyslaw Czarnowski {
267107077deSPrzemyslaw Czarnowski     BMCWEB_LOG_DEBUG << "Get Virtual Media resource data.";
268107077deSPrzemyslaw Czarnowski 
269107077deSPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
270914e2d5dSEd Tanous         [resName, name,
2715e7e2dc5SEd Tanous          aResp](const boost::system::error_code& ec,
272914e2d5dSEd Tanous                 const dbus::utility::ManagedObjectType& subtree) {
273107077deSPrzemyslaw Czarnowski         if (ec)
274107077deSPrzemyslaw Czarnowski         {
275107077deSPrzemyslaw Czarnowski             BMCWEB_LOG_DEBUG << "DBUS response error";
276e13c2760SPrzemyslaw Czarnowski 
277107077deSPrzemyslaw Czarnowski             return;
278107077deSPrzemyslaw Czarnowski         }
279107077deSPrzemyslaw Czarnowski 
280914e2d5dSEd Tanous         for (const auto& item : subtree)
281107077deSPrzemyslaw Czarnowski         {
282*365a73f4SEd Tanous             VmMode mode = parseObjectPathAndGetMode(item.first, resName);
283*365a73f4SEd Tanous             if (mode == VmMode::Invalid)
2841a6258dcSPrzemyslaw Czarnowski             {
2851a6258dcSPrzemyslaw Czarnowski                 continue;
2861a6258dcSPrzemyslaw Czarnowski             }
2871a6258dcSPrzemyslaw Czarnowski 
288107077deSPrzemyslaw Czarnowski             aResp->res.jsonValue = vmItemTemplate(name, resName);
289107077deSPrzemyslaw Czarnowski 
290e13c2760SPrzemyslaw Czarnowski             // Check if dbus path is Legacy type
291*365a73f4SEd Tanous             if (mode == VmMode::Legacy)
292e13c2760SPrzemyslaw Czarnowski             {
293e13c2760SPrzemyslaw Czarnowski                 aResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"]
294fdb20347SEd Tanous                                     ["target"] = crow::utility::urlFromPieces(
295fdb20347SEd Tanous                     "redfish", "v1", "Managers", name, "VirtualMedia", resName,
296fdb20347SEd Tanous                     "Actions", "VirtualMedia.InsertMedia");
297e13c2760SPrzemyslaw Czarnowski             }
298e13c2760SPrzemyslaw Czarnowski 
299107077deSPrzemyslaw Czarnowski             vmParseInterfaceObject(item.second, aResp);
300107077deSPrzemyslaw Czarnowski 
301002d39b4SEd Tanous             aResp->res
302002d39b4SEd Tanous                 .jsonValue["Actions"]["#VirtualMedia.EjectMedia"]["target"] =
303fdb20347SEd Tanous                 crow::utility::urlFromPieces("redfish", "v1", "Managers", name,
304fdb20347SEd Tanous                                              "VirtualMedia", resName, "Actions",
305fdb20347SEd Tanous                                              "VirtualMedia.EjectMedia");
306107077deSPrzemyslaw Czarnowski             return;
307107077deSPrzemyslaw Czarnowski         }
308107077deSPrzemyslaw Czarnowski 
309d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(aResp->res, "VirtualMedia", resName);
310107077deSPrzemyslaw Czarnowski         },
311107077deSPrzemyslaw Czarnowski         service, "/xyz/openbmc_project/VirtualMedia",
312107077deSPrzemyslaw Czarnowski         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
313107077deSPrzemyslaw Czarnowski }
314107077deSPrzemyslaw Czarnowski 
315e13c2760SPrzemyslaw Czarnowski /**
316c6f4e017SAgata Olender  * @brief Transfer protocols supported for InsertMedia action.
317c6f4e017SAgata Olender  *
318c6f4e017SAgata Olender  */
319c6f4e017SAgata Olender enum class TransferProtocol
320c6f4e017SAgata Olender {
321c6f4e017SAgata Olender     https,
322c6f4e017SAgata Olender     smb,
323c6f4e017SAgata Olender     invalid
324c6f4e017SAgata Olender };
325c6f4e017SAgata Olender 
326c6f4e017SAgata Olender /**
327c6f4e017SAgata Olender  * @brief Function extracts transfer protocol type from URI.
328c6f4e017SAgata Olender  *
329c6f4e017SAgata Olender  */
33067df073bSEd Tanous inline std::optional<TransferProtocol>
331ace85d60SEd Tanous     getTransferProtocolFromUri(const boost::urls::url_view& imageUri)
33267df073bSEd Tanous {
333079360aeSEd Tanous     std::string_view scheme = imageUri.scheme();
33467df073bSEd Tanous     if (scheme == "smb")
33567df073bSEd Tanous     {
33667df073bSEd Tanous         return TransferProtocol::smb;
33767df073bSEd Tanous     }
33867df073bSEd Tanous     if (scheme == "https")
33967df073bSEd Tanous     {
34067df073bSEd Tanous         return TransferProtocol::https;
34167df073bSEd Tanous     }
34267df073bSEd Tanous     if (!scheme.empty())
34367df073bSEd Tanous     {
34467df073bSEd Tanous         return TransferProtocol::invalid;
34567df073bSEd Tanous     }
34667df073bSEd Tanous 
34767df073bSEd Tanous     return {};
34867df073bSEd Tanous }
349c6f4e017SAgata Olender 
350c6f4e017SAgata Olender /**
351c6f4e017SAgata Olender  * @brief Function convert transfer protocol from string param.
352c6f4e017SAgata Olender  *
353c6f4e017SAgata Olender  */
35422db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam(
355c6f4e017SAgata Olender     const std::optional<std::string>& transferProtocolType)
356c6f4e017SAgata Olender {
357c6f4e017SAgata Olender     if (transferProtocolType == std::nullopt)
358c6f4e017SAgata Olender     {
359c6f4e017SAgata Olender         return {};
360c6f4e017SAgata Olender     }
361c6f4e017SAgata Olender 
362c6f4e017SAgata Olender     if (*transferProtocolType == "CIFS")
363c6f4e017SAgata Olender     {
364c6f4e017SAgata Olender         return TransferProtocol::smb;
365c6f4e017SAgata Olender     }
366c6f4e017SAgata Olender 
367c6f4e017SAgata Olender     if (*transferProtocolType == "HTTPS")
368c6f4e017SAgata Olender     {
369c6f4e017SAgata Olender         return TransferProtocol::https;
370c6f4e017SAgata Olender     }
371c6f4e017SAgata Olender 
372c6f4e017SAgata Olender     return TransferProtocol::invalid;
373c6f4e017SAgata Olender }
374c6f4e017SAgata Olender 
375c6f4e017SAgata Olender /**
376c6f4e017SAgata Olender  * @brief Function extends URI with transfer protocol type.
377c6f4e017SAgata Olender  *
378c6f4e017SAgata Olender  */
37922db1728SEd Tanous inline std::string
380c6f4e017SAgata Olender     getUriWithTransferProtocol(const std::string& imageUri,
381c6f4e017SAgata Olender                                const TransferProtocol& transferProtocol)
382c6f4e017SAgata Olender {
383c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::smb)
384c6f4e017SAgata Olender     {
385c6f4e017SAgata Olender         return "smb://" + imageUri;
386c6f4e017SAgata Olender     }
387c6f4e017SAgata Olender 
388c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::https)
389c6f4e017SAgata Olender     {
390c6f4e017SAgata Olender         return "https://" + imageUri;
391c6f4e017SAgata Olender     }
392c6f4e017SAgata Olender 
393c6f4e017SAgata Olender     return imageUri;
394c6f4e017SAgata Olender }
395c6f4e017SAgata Olender 
3961f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams
3971f2a40ceSPrzemyslaw Czarnowski {
398120fa86aSPrzemyslaw Czarnowski     std::optional<std::string> imageUrl;
3991f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> userName;
4001f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> password;
4011f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferMethod;
4021f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferProtocolType;
4031f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> writeProtected = true;
4041f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> inserted;
4051f2a40ceSPrzemyslaw Czarnowski };
4061f2a40ceSPrzemyslaw Czarnowski 
4071214b7e7SGunnar Mills template <typename T>
4081214b7e7SGunnar Mills static void secureCleanup(T& value)
409988fb7b2SAdrian Ambrożewicz {
4104ecc618fSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
411988fb7b2SAdrian Ambrożewicz     auto raw = const_cast<typename T::value_type*>(value.data());
412988fb7b2SAdrian Ambrożewicz     explicit_bzero(raw, value.size() * sizeof(*raw));
413988fb7b2SAdrian Ambrożewicz }
414988fb7b2SAdrian Ambrożewicz 
415988fb7b2SAdrian Ambrożewicz class Credentials
416988fb7b2SAdrian Ambrożewicz {
417988fb7b2SAdrian Ambrożewicz   public:
418988fb7b2SAdrian Ambrożewicz     Credentials(std::string&& user, std::string&& password) :
419988fb7b2SAdrian Ambrożewicz         userBuf(std::move(user)), passBuf(std::move(password))
4201214b7e7SGunnar Mills     {}
421988fb7b2SAdrian Ambrożewicz 
422988fb7b2SAdrian Ambrożewicz     ~Credentials()
423988fb7b2SAdrian Ambrożewicz     {
424988fb7b2SAdrian Ambrożewicz         secureCleanup(userBuf);
425988fb7b2SAdrian Ambrożewicz         secureCleanup(passBuf);
426988fb7b2SAdrian Ambrożewicz     }
427988fb7b2SAdrian Ambrożewicz 
428988fb7b2SAdrian Ambrożewicz     const std::string& user()
429988fb7b2SAdrian Ambrożewicz     {
430988fb7b2SAdrian Ambrożewicz         return userBuf;
431988fb7b2SAdrian Ambrożewicz     }
432988fb7b2SAdrian Ambrożewicz 
433988fb7b2SAdrian Ambrożewicz     const std::string& password()
434988fb7b2SAdrian Ambrożewicz     {
435988fb7b2SAdrian Ambrożewicz         return passBuf;
436988fb7b2SAdrian Ambrożewicz     }
437988fb7b2SAdrian Ambrożewicz 
438988fb7b2SAdrian Ambrożewicz     Credentials() = delete;
439988fb7b2SAdrian Ambrożewicz     Credentials(const Credentials&) = delete;
440988fb7b2SAdrian Ambrożewicz     Credentials& operator=(const Credentials&) = delete;
441ecd6a3a2SEd Tanous     Credentials(Credentials&&) = delete;
442ecd6a3a2SEd Tanous     Credentials& operator=(Credentials&&) = delete;
443988fb7b2SAdrian Ambrożewicz 
44422db1728SEd Tanous   private:
445988fb7b2SAdrian Ambrożewicz     std::string userBuf;
446988fb7b2SAdrian Ambrożewicz     std::string passBuf;
447988fb7b2SAdrian Ambrożewicz };
448988fb7b2SAdrian Ambrożewicz 
449988fb7b2SAdrian Ambrożewicz class CredentialsProvider
450988fb7b2SAdrian Ambrożewicz {
451988fb7b2SAdrian Ambrożewicz   public:
4521214b7e7SGunnar Mills     template <typename T>
4531214b7e7SGunnar Mills     struct Deleter
454988fb7b2SAdrian Ambrożewicz     {
455988fb7b2SAdrian Ambrożewicz         void operator()(T* buff) const
456988fb7b2SAdrian Ambrożewicz         {
457988fb7b2SAdrian Ambrożewicz             if (buff)
458988fb7b2SAdrian Ambrożewicz             {
459988fb7b2SAdrian Ambrożewicz                 secureCleanup(*buff);
460988fb7b2SAdrian Ambrożewicz                 delete buff;
461988fb7b2SAdrian Ambrożewicz             }
462988fb7b2SAdrian Ambrożewicz         }
463988fb7b2SAdrian Ambrożewicz     };
464988fb7b2SAdrian Ambrożewicz 
465988fb7b2SAdrian Ambrożewicz     using Buffer = std::vector<char>;
466988fb7b2SAdrian Ambrożewicz     using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>;
467988fb7b2SAdrian Ambrożewicz     // Using explicit definition instead of std::function to avoid implicit
468988fb7b2SAdrian Ambrożewicz     // conversions eg. stack copy instead of reference
469988fb7b2SAdrian Ambrożewicz     using FormatterFunc = void(const std::string& username,
470988fb7b2SAdrian Ambrożewicz                                const std::string& password, Buffer& dest);
471988fb7b2SAdrian Ambrożewicz 
472988fb7b2SAdrian Ambrożewicz     CredentialsProvider(std::string&& user, std::string&& password) :
473988fb7b2SAdrian Ambrożewicz         credentials(std::move(user), std::move(password))
4741214b7e7SGunnar Mills     {}
475988fb7b2SAdrian Ambrożewicz 
476988fb7b2SAdrian Ambrożewicz     const std::string& user()
477988fb7b2SAdrian Ambrożewicz     {
478988fb7b2SAdrian Ambrożewicz         return credentials.user();
479988fb7b2SAdrian Ambrożewicz     }
480988fb7b2SAdrian Ambrożewicz 
481988fb7b2SAdrian Ambrożewicz     const std::string& password()
482988fb7b2SAdrian Ambrożewicz     {
483988fb7b2SAdrian Ambrożewicz         return credentials.password();
484988fb7b2SAdrian Ambrożewicz     }
485988fb7b2SAdrian Ambrożewicz 
4861917ee95SEd Tanous     SecureBuffer pack(FormatterFunc* formatter)
487988fb7b2SAdrian Ambrożewicz     {
488988fb7b2SAdrian Ambrożewicz         SecureBuffer packed{new Buffer{}};
489e662eae8SEd Tanous         if (formatter != nullptr)
490988fb7b2SAdrian Ambrożewicz         {
491988fb7b2SAdrian Ambrożewicz             formatter(credentials.user(), credentials.password(), *packed);
492988fb7b2SAdrian Ambrożewicz         }
493988fb7b2SAdrian Ambrożewicz 
494988fb7b2SAdrian Ambrożewicz         return packed;
495988fb7b2SAdrian Ambrożewicz     }
496988fb7b2SAdrian Ambrożewicz 
497988fb7b2SAdrian Ambrożewicz   private:
498988fb7b2SAdrian Ambrożewicz     Credentials credentials;
499988fb7b2SAdrian Ambrożewicz };
500988fb7b2SAdrian Ambrożewicz 
501988fb7b2SAdrian Ambrożewicz // Wrapper for boost::async_pipe ensuring proper pipe cleanup
5021214b7e7SGunnar Mills template <typename Buffer>
5031214b7e7SGunnar Mills class Pipe
504988fb7b2SAdrian Ambrożewicz {
505988fb7b2SAdrian Ambrożewicz   public:
506988fb7b2SAdrian Ambrożewicz     using unix_fd = sdbusplus::message::unix_fd;
507988fb7b2SAdrian Ambrożewicz 
5088a592810SEd Tanous     Pipe(boost::asio::io_context& io, Buffer&& bufferIn) :
5098a592810SEd Tanous         impl(io), buffer{std::move(bufferIn)}
5101214b7e7SGunnar Mills     {}
511988fb7b2SAdrian Ambrożewicz 
512988fb7b2SAdrian Ambrożewicz     ~Pipe()
513988fb7b2SAdrian Ambrożewicz     {
514988fb7b2SAdrian Ambrożewicz         // Named pipe needs to be explicitly removed
515988fb7b2SAdrian Ambrożewicz         impl.close();
516988fb7b2SAdrian Ambrożewicz     }
517988fb7b2SAdrian Ambrożewicz 
518ecd6a3a2SEd Tanous     Pipe(const Pipe&) = delete;
519ecd6a3a2SEd Tanous     Pipe(Pipe&&) = delete;
520ecd6a3a2SEd Tanous     Pipe& operator=(const Pipe&) = delete;
521ecd6a3a2SEd Tanous     Pipe& operator=(Pipe&&) = delete;
522ecd6a3a2SEd Tanous 
523988fb7b2SAdrian Ambrożewicz     unix_fd fd()
524988fb7b2SAdrian Ambrożewicz     {
525988fb7b2SAdrian Ambrożewicz         return unix_fd{impl.native_source()};
526988fb7b2SAdrian Ambrożewicz     }
527988fb7b2SAdrian Ambrożewicz 
528988fb7b2SAdrian Ambrożewicz     template <typename WriteHandler>
52981ce609eSEd Tanous     void asyncWrite(WriteHandler&& handler)
530988fb7b2SAdrian Ambrożewicz     {
531988fb7b2SAdrian Ambrożewicz         impl.async_write_some(data(), std::forward<WriteHandler>(handler));
532988fb7b2SAdrian Ambrożewicz     }
533988fb7b2SAdrian Ambrożewicz 
534988fb7b2SAdrian Ambrożewicz   private:
535988fb7b2SAdrian Ambrożewicz     // Specialization for pointer types
536988fb7b2SAdrian Ambrożewicz     template <typename B = Buffer>
537988fb7b2SAdrian Ambrożewicz     typename std::enable_if<boost::has_dereference<B>::value,
538988fb7b2SAdrian Ambrożewicz                             boost::asio::const_buffer>::type
539988fb7b2SAdrian Ambrożewicz         data()
540988fb7b2SAdrian Ambrożewicz     {
541988fb7b2SAdrian Ambrożewicz         return boost::asio::buffer(*buffer);
542988fb7b2SAdrian Ambrożewicz     }
543988fb7b2SAdrian Ambrożewicz 
544988fb7b2SAdrian Ambrożewicz     template <typename B = Buffer>
545988fb7b2SAdrian Ambrożewicz     typename std::enable_if<!boost::has_dereference<B>::value,
546988fb7b2SAdrian Ambrożewicz                             boost::asio::const_buffer>::type
547988fb7b2SAdrian Ambrożewicz         data()
548988fb7b2SAdrian Ambrożewicz     {
549988fb7b2SAdrian Ambrożewicz         return boost::asio::buffer(buffer);
550988fb7b2SAdrian Ambrożewicz     }
551988fb7b2SAdrian Ambrożewicz 
552988fb7b2SAdrian Ambrożewicz     const std::string name;
553988fb7b2SAdrian Ambrożewicz     boost::process::async_pipe impl;
554988fb7b2SAdrian Ambrożewicz     Buffer buffer;
555988fb7b2SAdrian Ambrożewicz };
556988fb7b2SAdrian Ambrożewicz 
557e13c2760SPrzemyslaw Czarnowski /**
558e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
559e13c2760SPrzemyslaw Czarnowski  *
560e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
561e13c2760SPrzemyslaw Czarnowski  */
56222db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
563e13c2760SPrzemyslaw Czarnowski                             const std::string& service, const std::string& name,
564988fb7b2SAdrian Ambrożewicz                             const std::string& imageUrl, const bool rw,
565988fb7b2SAdrian Ambrożewicz                             std::string&& userName, std::string&& password)
566e13c2760SPrzemyslaw Czarnowski {
567988fb7b2SAdrian Ambrożewicz     using SecurePipe = Pipe<CredentialsProvider::SecureBuffer>;
568988fb7b2SAdrian Ambrożewicz     constexpr const size_t secretLimit = 1024;
569988fb7b2SAdrian Ambrożewicz 
570988fb7b2SAdrian Ambrożewicz     std::shared_ptr<SecurePipe> secretPipe;
571168e20c1SEd Tanous     dbus::utility::DbusVariantType unixFd = -1;
572988fb7b2SAdrian Ambrożewicz 
573988fb7b2SAdrian Ambrożewicz     if (!userName.empty() || !password.empty())
574988fb7b2SAdrian Ambrożewicz     {
575988fb7b2SAdrian Ambrożewicz         // Encapsulate in safe buffer
576988fb7b2SAdrian Ambrożewicz         CredentialsProvider credentials(std::move(userName),
577988fb7b2SAdrian Ambrożewicz                                         std::move(password));
578988fb7b2SAdrian Ambrożewicz 
579988fb7b2SAdrian Ambrożewicz         // Payload must contain data + NULL delimiters
580988fb7b2SAdrian Ambrożewicz         if (credentials.user().size() + credentials.password().size() + 2 >
581988fb7b2SAdrian Ambrożewicz             secretLimit)
582988fb7b2SAdrian Ambrożewicz         {
583988fb7b2SAdrian Ambrożewicz             BMCWEB_LOG_ERROR << "Credentials too long to handle";
584988fb7b2SAdrian Ambrożewicz             messages::unrecognizedRequestBody(asyncResp->res);
585988fb7b2SAdrian Ambrożewicz             return;
586988fb7b2SAdrian Ambrożewicz         }
587988fb7b2SAdrian Ambrożewicz 
588988fb7b2SAdrian Ambrożewicz         // Pack secret
58922db1728SEd Tanous         auto secret = credentials.pack(
59022db1728SEd Tanous             [](const auto& user, const auto& pass, auto& buff) {
591988fb7b2SAdrian Ambrożewicz             std::copy(user.begin(), user.end(), std::back_inserter(buff));
592988fb7b2SAdrian Ambrożewicz             buff.push_back('\0');
593988fb7b2SAdrian Ambrożewicz             std::copy(pass.begin(), pass.end(), std::back_inserter(buff));
594988fb7b2SAdrian Ambrożewicz             buff.push_back('\0');
595988fb7b2SAdrian Ambrożewicz         });
596988fb7b2SAdrian Ambrożewicz 
597988fb7b2SAdrian Ambrożewicz         // Open pipe
598988fb7b2SAdrian Ambrożewicz         secretPipe = std::make_shared<SecurePipe>(
59922db1728SEd Tanous             crow::connections::systemBus->get_io_context(), std::move(secret));
600988fb7b2SAdrian Ambrożewicz         unixFd = secretPipe->fd();
601988fb7b2SAdrian Ambrożewicz 
602988fb7b2SAdrian Ambrożewicz         // Pass secret over pipe
60381ce609eSEd Tanous         secretPipe->asyncWrite(
604f5b16f03SVikram Bodireddy             [asyncResp](const boost::system::error_code& ec, std::size_t) {
605988fb7b2SAdrian Ambrożewicz             if (ec)
606988fb7b2SAdrian Ambrożewicz             {
607988fb7b2SAdrian Ambrożewicz                 BMCWEB_LOG_ERROR << "Failed to pass secret: " << ec;
608988fb7b2SAdrian Ambrożewicz                 messages::internalError(asyncResp->res);
609988fb7b2SAdrian Ambrożewicz             }
610988fb7b2SAdrian Ambrożewicz         });
611988fb7b2SAdrian Ambrożewicz     }
612988fb7b2SAdrian Ambrożewicz 
613e13c2760SPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
6145e7e2dc5SEd Tanous         [asyncResp, secretPipe](const boost::system::error_code& ec,
615988fb7b2SAdrian Ambrożewicz                                 bool success) {
616e13c2760SPrzemyslaw Czarnowski         if (ec)
617e13c2760SPrzemyslaw Czarnowski         {
618e13c2760SPrzemyslaw Czarnowski             BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
619e13c2760SPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
620d6da5bebSAdrian Ambrożewicz         }
621d6da5bebSAdrian Ambrożewicz         else if (!success)
622d6da5bebSAdrian Ambrożewicz         {
623d6da5bebSAdrian Ambrożewicz             BMCWEB_LOG_ERROR << "Service responded with error";
624d6da5bebSAdrian Ambrożewicz             messages::generalError(asyncResp->res);
625e13c2760SPrzemyslaw Czarnowski         }
626e13c2760SPrzemyslaw Czarnowski         },
627e13c2760SPrzemyslaw Czarnowski         service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
628988fb7b2SAdrian Ambrożewicz         "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw,
629988fb7b2SAdrian Ambrożewicz         unixFd);
630e13c2760SPrzemyslaw Czarnowski }
631e13c2760SPrzemyslaw Czarnowski 
632e13c2760SPrzemyslaw Czarnowski /**
633120fa86aSPrzemyslaw Czarnowski  * @brief Function validate parameters of insert media request.
634120fa86aSPrzemyslaw Czarnowski  *
635120fa86aSPrzemyslaw Czarnowski  */
636120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
637120fa86aSPrzemyslaw Czarnowski                            const std::string& service,
638120fa86aSPrzemyslaw Czarnowski                            const std::string& resName,
639120fa86aSPrzemyslaw Czarnowski                            InsertMediaActionParams& actionParams)
640120fa86aSPrzemyslaw Czarnowski {
641120fa86aSPrzemyslaw Czarnowski     BMCWEB_LOG_DEBUG << "Validation started";
642120fa86aSPrzemyslaw Czarnowski     // required param imageUrl must not be empty
643120fa86aSPrzemyslaw Czarnowski     if (!actionParams.imageUrl)
644120fa86aSPrzemyslaw Czarnowski     {
645120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action parameter Image is empty.";
646120fa86aSPrzemyslaw Czarnowski 
647120fa86aSPrzemyslaw Czarnowski         messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image");
648120fa86aSPrzemyslaw Czarnowski 
649120fa86aSPrzemyslaw Czarnowski         return;
650120fa86aSPrzemyslaw Czarnowski     }
651120fa86aSPrzemyslaw Czarnowski 
652120fa86aSPrzemyslaw Czarnowski     // optional param inserted must be true
653120fa86aSPrzemyslaw Czarnowski     if ((actionParams.inserted != std::nullopt) && !*actionParams.inserted)
654120fa86aSPrzemyslaw Czarnowski     {
655120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR
656120fa86aSPrzemyslaw Czarnowski             << "Request action optional parameter Inserted must be true.";
657120fa86aSPrzemyslaw Czarnowski 
658120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "Inserted",
659120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
660120fa86aSPrzemyslaw Czarnowski 
661120fa86aSPrzemyslaw Czarnowski         return;
662120fa86aSPrzemyslaw Czarnowski     }
663120fa86aSPrzemyslaw Czarnowski 
664120fa86aSPrzemyslaw Czarnowski     // optional param transferMethod must be stream
665120fa86aSPrzemyslaw Czarnowski     if ((actionParams.transferMethod != std::nullopt) &&
666120fa86aSPrzemyslaw Czarnowski         (*actionParams.transferMethod != "Stream"))
667120fa86aSPrzemyslaw Czarnowski     {
668120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action optional parameter "
669120fa86aSPrzemyslaw Czarnowski                             "TransferMethod must be Stream.";
670120fa86aSPrzemyslaw Czarnowski 
671120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "TransferMethod",
672120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
673120fa86aSPrzemyslaw Czarnowski 
674120fa86aSPrzemyslaw Czarnowski         return;
675120fa86aSPrzemyslaw Czarnowski     }
676120fa86aSPrzemyslaw Czarnowski     boost::urls::result<boost::urls::url_view> url =
677120fa86aSPrzemyslaw Czarnowski         boost::urls::parse_uri(*actionParams.imageUrl);
678120fa86aSPrzemyslaw Czarnowski     if (!url)
679120fa86aSPrzemyslaw Czarnowski     {
680120fa86aSPrzemyslaw Czarnowski         messages::actionParameterValueFormatError(
681120fa86aSPrzemyslaw Czarnowski             asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia");
682120fa86aSPrzemyslaw Czarnowski         return;
683120fa86aSPrzemyslaw Czarnowski     }
684120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> uriTransferProtocolType =
685120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromUri(*url);
686120fa86aSPrzemyslaw Czarnowski 
687120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> paramTransferProtocolType =
688120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromParam(actionParams.transferProtocolType);
689120fa86aSPrzemyslaw Czarnowski 
690120fa86aSPrzemyslaw Czarnowski     // ImageUrl does not contain valid protocol type
691120fa86aSPrzemyslaw Czarnowski     if (*uriTransferProtocolType == TransferProtocol::invalid)
692120fa86aSPrzemyslaw Czarnowski     {
693120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must "
694120fa86aSPrzemyslaw Czarnowski                             "contain specified protocol type from list: "
695120fa86aSPrzemyslaw Czarnowski                             "(smb, https).";
696120fa86aSPrzemyslaw Czarnowski 
697120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
698120fa86aSPrzemyslaw Czarnowski 
699120fa86aSPrzemyslaw Czarnowski         return;
700120fa86aSPrzemyslaw Czarnowski     }
701120fa86aSPrzemyslaw Czarnowski 
702120fa86aSPrzemyslaw Czarnowski     // transferProtocolType should contain value from list
703120fa86aSPrzemyslaw Czarnowski     if (*paramTransferProtocolType == TransferProtocol::invalid)
704120fa86aSPrzemyslaw Czarnowski     {
705120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action parameter TransferProtocolType "
706120fa86aSPrzemyslaw Czarnowski                             "must be provided with value from list: "
707120fa86aSPrzemyslaw Czarnowski                             "(CIFS, HTTPS).";
708120fa86aSPrzemyslaw Czarnowski 
709120fa86aSPrzemyslaw Czarnowski         messages::propertyValueNotInList(asyncResp->res,
710120fa86aSPrzemyslaw Czarnowski                                          *actionParams.transferProtocolType,
711120fa86aSPrzemyslaw Czarnowski                                          "TransferProtocolType");
712120fa86aSPrzemyslaw Czarnowski         return;
713120fa86aSPrzemyslaw Czarnowski     }
714120fa86aSPrzemyslaw Czarnowski 
715120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol not provided either with URI nor param
716120fa86aSPrzemyslaw Czarnowski     if ((uriTransferProtocolType == std::nullopt) &&
717120fa86aSPrzemyslaw Czarnowski         (paramTransferProtocolType == std::nullopt))
718120fa86aSPrzemyslaw Czarnowski     {
719120fa86aSPrzemyslaw Czarnowski         BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must "
720120fa86aSPrzemyslaw Czarnowski                             "contain specified protocol type or param "
721120fa86aSPrzemyslaw Czarnowski                             "TransferProtocolType must be provided.";
722120fa86aSPrzemyslaw Czarnowski 
723120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
724120fa86aSPrzemyslaw Czarnowski 
725120fa86aSPrzemyslaw Czarnowski         return;
726120fa86aSPrzemyslaw Czarnowski     }
727120fa86aSPrzemyslaw Czarnowski 
728120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol provided both with URI and param
729120fa86aSPrzemyslaw Czarnowski     if ((paramTransferProtocolType != std::nullopt) &&
730120fa86aSPrzemyslaw Czarnowski         (uriTransferProtocolType != std::nullopt))
731120fa86aSPrzemyslaw Czarnowski     {
732120fa86aSPrzemyslaw Czarnowski         // check if protocol is the same for URI and param
733120fa86aSPrzemyslaw Czarnowski         if (*paramTransferProtocolType != *uriTransferProtocolType)
734120fa86aSPrzemyslaw Czarnowski         {
735120fa86aSPrzemyslaw Czarnowski             BMCWEB_LOG_ERROR << "Request action parameter "
736120fa86aSPrzemyslaw Czarnowski                                 "TransferProtocolType must  contain the "
737120fa86aSPrzemyslaw Czarnowski                                 "same protocol type as protocol type "
738120fa86aSPrzemyslaw Czarnowski                                 "provided with param imageUrl.";
739120fa86aSPrzemyslaw Czarnowski 
740120fa86aSPrzemyslaw Czarnowski             messages::actionParameterValueTypeError(
741120fa86aSPrzemyslaw Czarnowski                 asyncResp->res, *actionParams.transferProtocolType,
742120fa86aSPrzemyslaw Czarnowski                 "TransferProtocolType", "InsertMedia");
743120fa86aSPrzemyslaw Czarnowski 
744120fa86aSPrzemyslaw Czarnowski             return;
745120fa86aSPrzemyslaw Czarnowski         }
746120fa86aSPrzemyslaw Czarnowski     }
747120fa86aSPrzemyslaw Czarnowski 
748120fa86aSPrzemyslaw Czarnowski     // validation passed, add protocol to URI if needed
749120fa86aSPrzemyslaw Czarnowski     if (uriTransferProtocolType == std::nullopt)
750120fa86aSPrzemyslaw Czarnowski     {
751120fa86aSPrzemyslaw Czarnowski         actionParams.imageUrl = getUriWithTransferProtocol(
752120fa86aSPrzemyslaw Czarnowski             *actionParams.imageUrl, *paramTransferProtocolType);
753120fa86aSPrzemyslaw Czarnowski     }
754120fa86aSPrzemyslaw Czarnowski 
755120fa86aSPrzemyslaw Czarnowski     doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
756120fa86aSPrzemyslaw Czarnowski                     !(*actionParams.writeProtected),
757120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.userName),
758120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.password));
759120fa86aSPrzemyslaw Czarnowski }
760120fa86aSPrzemyslaw Czarnowski 
761120fa86aSPrzemyslaw Czarnowski /**
762e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
763e13c2760SPrzemyslaw Czarnowski  *
764e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
765e13c2760SPrzemyslaw Czarnowski  */
76624e740a7SEd Tanous inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
767e13c2760SPrzemyslaw Czarnowski                           const std::string& service, const std::string& name,
768e13c2760SPrzemyslaw Czarnowski                           bool legacy)
769e13c2760SPrzemyslaw Czarnowski {
770e13c2760SPrzemyslaw Czarnowski 
771e13c2760SPrzemyslaw Czarnowski     // Legacy mount requires parameter with image
772e13c2760SPrzemyslaw Czarnowski     if (legacy)
773e13c2760SPrzemyslaw Czarnowski     {
774e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
7755e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
776e13c2760SPrzemyslaw Czarnowski             if (ec)
777e13c2760SPrzemyslaw Czarnowski             {
778e13c2760SPrzemyslaw Czarnowski                 BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
779e13c2760SPrzemyslaw Czarnowski 
780e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
781e13c2760SPrzemyslaw Czarnowski                 return;
782e13c2760SPrzemyslaw Czarnowski             }
783e13c2760SPrzemyslaw Czarnowski             },
784e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
785e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount");
786e13c2760SPrzemyslaw Czarnowski     }
787e13c2760SPrzemyslaw Czarnowski     else // proxy
788e13c2760SPrzemyslaw Czarnowski     {
789e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
7905e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
791e13c2760SPrzemyslaw Czarnowski             if (ec)
792e13c2760SPrzemyslaw Czarnowski             {
793e13c2760SPrzemyslaw Czarnowski                 BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
794e13c2760SPrzemyslaw Czarnowski 
795e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
796e13c2760SPrzemyslaw Czarnowski                 return;
797e13c2760SPrzemyslaw Czarnowski             }
798e13c2760SPrzemyslaw Czarnowski             },
799e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name,
800e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
801e13c2760SPrzemyslaw Czarnowski     }
802e13c2760SPrzemyslaw Czarnowski }
803e13c2760SPrzemyslaw Czarnowski 
80496825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost(
80596825bebSEd Tanous     crow::App& app, const crow::Request& req,
80622db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
80796825bebSEd Tanous     const std::string& name, const std::string& resName)
80896825bebSEd Tanous {
8093ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
81045ca1b86SEd Tanous     {
81145ca1b86SEd Tanous         return;
81245ca1b86SEd Tanous     }
81322db1728SEd Tanous     if (name != "bmc")
814107077deSPrzemyslaw Czarnowski     {
8151f2a40ceSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, "VirtualMedia.InsertMedia",
816002d39b4SEd Tanous                                    resName);
817107077deSPrzemyslaw Czarnowski 
818107077deSPrzemyslaw Czarnowski         return;
819107077deSPrzemyslaw Czarnowski     }
820120fa86aSPrzemyslaw Czarnowski     std::optional<InsertMediaActionParams> actionParams =
821120fa86aSPrzemyslaw Czarnowski         InsertMediaActionParams();
82298be3e39SEd Tanous 
823120fa86aSPrzemyslaw Czarnowski     // Read obligatory parameters (url of image)
82415ed6780SWilly Tu     if (!json_util::readJsonAction(
825120fa86aSPrzemyslaw Czarnowski             req, asyncResp->res, "Image", actionParams->imageUrl,
826120fa86aSPrzemyslaw Czarnowski             "WriteProtected", actionParams->writeProtected, "UserName",
827120fa86aSPrzemyslaw Czarnowski             actionParams->userName, "Password", actionParams->password,
828120fa86aSPrzemyslaw Czarnowski             "Inserted", actionParams->inserted, "TransferMethod",
829120fa86aSPrzemyslaw Czarnowski             actionParams->transferMethod, "TransferProtocolType",
830120fa86aSPrzemyslaw Czarnowski             actionParams->transferProtocolType))
83198be3e39SEd Tanous     {
83298be3e39SEd Tanous         return;
83398be3e39SEd Tanous     }
834107077deSPrzemyslaw Czarnowski 
8352b73119cSGeorge Liu     dbus::utility::getDbusObject(
8362b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
83796825bebSEd Tanous         [asyncResp, actionParams,
8382b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
839002d39b4SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) mutable {
84022db1728SEd Tanous         if (ec)
84122db1728SEd Tanous         {
84296825bebSEd Tanous             BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
84322db1728SEd Tanous             messages::internalError(asyncResp->res);
844107077deSPrzemyslaw Czarnowski 
84522db1728SEd Tanous             return;
84622db1728SEd Tanous         }
84722db1728SEd Tanous         std::string service = getObjectType.begin()->first;
84822db1728SEd Tanous         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
84922db1728SEd Tanous 
85022db1728SEd Tanous         crow::connections::systemBus->async_method_call(
85198be3e39SEd Tanous             [service, resName, actionParams,
8525e7e2dc5SEd Tanous              asyncResp](const boost::system::error_code& ec2,
853002d39b4SEd Tanous                         dbus::utility::ManagedObjectType& subtree) mutable {
8548a592810SEd Tanous             if (ec2)
85522db1728SEd Tanous             {
85622db1728SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
8571f2a40ceSPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
85822db1728SEd Tanous 
85922db1728SEd Tanous                 return;
86022db1728SEd Tanous             }
86122db1728SEd Tanous 
86222db1728SEd Tanous             for (const auto& object : subtree)
86322db1728SEd Tanous             {
864*365a73f4SEd Tanous                 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
865*365a73f4SEd Tanous                 if (mode == VmMode::Proxy)
86622db1728SEd Tanous                 {
867120fa86aSPrzemyslaw Czarnowski                     validateParams(asyncResp, service, resName, *actionParams);
86822db1728SEd Tanous 
86922db1728SEd Tanous                     return;
87022db1728SEd Tanous                 }
87122db1728SEd Tanous             }
87222db1728SEd Tanous             BMCWEB_LOG_DEBUG << "Parent item not found";
87396825bebSEd Tanous             messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
87422db1728SEd Tanous             },
87522db1728SEd Tanous             service, "/xyz/openbmc_project/VirtualMedia",
876002d39b4SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
8772b73119cSGeorge Liu         });
87896825bebSEd Tanous }
87922db1728SEd Tanous 
88096825bebSEd Tanous inline void handleManagersVirtualMediaActionEject(
88196825bebSEd Tanous     crow::App& app, const crow::Request& req,
88222db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
88396825bebSEd Tanous     const std::string& managerName, const std::string& resName)
88496825bebSEd Tanous {
8853ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
88645ca1b86SEd Tanous     {
88745ca1b86SEd Tanous         return;
88845ca1b86SEd Tanous     }
88996825bebSEd Tanous     if (managerName != "bmc")
890107077deSPrzemyslaw Czarnowski     {
891120fa86aSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, "VirtualMedia.EjectMedia",
892002d39b4SEd Tanous                                    resName);
89322db1728SEd Tanous 
89422db1728SEd Tanous         return;
89522db1728SEd Tanous     }
89622db1728SEd Tanous 
8972b73119cSGeorge Liu     dbus::utility::getDbusObject(
8982b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
899002d39b4SEd Tanous         [asyncResp,
9002b73119cSGeorge Liu          resName](const boost::system::error_code& ec2,
901b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
9028a592810SEd Tanous         if (ec2)
90322db1728SEd Tanous         {
9048a592810SEd Tanous             BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec2;
90522db1728SEd Tanous             messages::internalError(asyncResp->res);
90622db1728SEd Tanous 
90722db1728SEd Tanous             return;
90822db1728SEd Tanous         }
90922db1728SEd Tanous         std::string service = getObjectType.begin()->first;
91022db1728SEd Tanous         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
91122db1728SEd Tanous 
91222db1728SEd Tanous         crow::connections::systemBus->async_method_call(
91302cad96eSEd Tanous             [resName, service, asyncResp{asyncResp}](
9145e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
91502cad96eSEd Tanous                 const dbus::utility::ManagedObjectType& subtree) {
91622db1728SEd Tanous             if (ec)
91722db1728SEd Tanous             {
91822db1728SEd Tanous                 BMCWEB_LOG_DEBUG << "DBUS response error";
9191f2a40ceSPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
92022db1728SEd Tanous 
92122db1728SEd Tanous                 return;
92222db1728SEd Tanous             }
92322db1728SEd Tanous 
92422db1728SEd Tanous             for (const auto& object : subtree)
92522db1728SEd Tanous             {
92622db1728SEd Tanous 
927*365a73f4SEd Tanous                 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
928*365a73f4SEd Tanous                 if (mode != VmMode::Invalid)
92922db1728SEd Tanous                 {
930*365a73f4SEd Tanous                     doEjectAction(asyncResp, service, resName,
931*365a73f4SEd Tanous                                   mode == VmMode::Legacy);
93222db1728SEd Tanous                 }
93322db1728SEd Tanous             }
93422db1728SEd Tanous             BMCWEB_LOG_DEBUG << "Parent item not found";
93596825bebSEd Tanous             messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
93622db1728SEd Tanous             },
93722db1728SEd Tanous             service, "/xyz/openbmc_project/VirtualMedia",
938002d39b4SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
9392b73119cSGeorge Liu         });
94096825bebSEd Tanous }
94196825bebSEd Tanous 
94296825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet(
94396825bebSEd Tanous     crow::App& app, const crow::Request& req,
94422db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
94596825bebSEd Tanous     const std::string& name)
94696825bebSEd Tanous {
9473ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
94845ca1b86SEd Tanous     {
94945ca1b86SEd Tanous         return;
95045ca1b86SEd Tanous     }
95122db1728SEd Tanous     if (name != "bmc")
95222db1728SEd Tanous     {
953002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", name);
954107077deSPrzemyslaw Czarnowski 
955107077deSPrzemyslaw Czarnowski         return;
956107077deSPrzemyslaw Czarnowski     }
957107077deSPrzemyslaw Czarnowski 
9588d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
959107077deSPrzemyslaw Czarnowski         "#VirtualMediaCollection.VirtualMediaCollection";
9608d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Virtual Media Services";
961fdb20347SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
962fdb20347SEd Tanous         "redfish", "v1", "Managers", name, "VirtualMedia");
963107077deSPrzemyslaw Czarnowski 
9642b73119cSGeorge Liu     dbus::utility::getDbusObject(
9652b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
9662b73119cSGeorge Liu         [asyncResp, name](const boost::system::error_code& ec,
967b9d36b47SEd Tanous                           const dbus::utility::MapperGetObject& getObjectType) {
968107077deSPrzemyslaw Czarnowski         if (ec)
969107077deSPrzemyslaw Czarnowski         {
97096825bebSEd Tanous             BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
971107077deSPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
972107077deSPrzemyslaw Czarnowski 
973107077deSPrzemyslaw Czarnowski             return;
974107077deSPrzemyslaw Czarnowski         }
975107077deSPrzemyslaw Czarnowski         std::string service = getObjectType.begin()->first;
976107077deSPrzemyslaw Czarnowski         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
977107077deSPrzemyslaw Czarnowski 
978107077deSPrzemyslaw Czarnowski         getVmResourceList(asyncResp, service, name);
9792b73119cSGeorge Liu         });
98096825bebSEd Tanous }
981107077deSPrzemyslaw Czarnowski 
98296825bebSEd Tanous inline void
98396825bebSEd Tanous     handleVirtualMediaGet(crow::App& app, const crow::Request& req,
98422db1728SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
98596825bebSEd Tanous                           const std::string& name, const std::string& resName)
98696825bebSEd Tanous {
9873ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
98845ca1b86SEd Tanous     {
98945ca1b86SEd Tanous         return;
99045ca1b86SEd Tanous     }
991107077deSPrzemyslaw Czarnowski     if (name != "bmc")
992107077deSPrzemyslaw Czarnowski     {
993002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
994107077deSPrzemyslaw Czarnowski 
995107077deSPrzemyslaw Czarnowski         return;
996107077deSPrzemyslaw Czarnowski     }
997107077deSPrzemyslaw Czarnowski 
9982b73119cSGeorge Liu     dbus::utility::getDbusObject(
9992b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
1000002d39b4SEd Tanous         [asyncResp, name,
10012b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
1002b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
1003107077deSPrzemyslaw Czarnowski         if (ec)
1004107077deSPrzemyslaw Czarnowski         {
100596825bebSEd Tanous             BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec;
1006107077deSPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
1007107077deSPrzemyslaw Czarnowski 
1008107077deSPrzemyslaw Czarnowski             return;
1009107077deSPrzemyslaw Czarnowski         }
1010107077deSPrzemyslaw Czarnowski         std::string service = getObjectType.begin()->first;
1011107077deSPrzemyslaw Czarnowski         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
1012107077deSPrzemyslaw Czarnowski 
1013107077deSPrzemyslaw Czarnowski         getVmData(asyncResp, service, name, resName);
10142b73119cSGeorge Liu         });
101596825bebSEd Tanous }
101696825bebSEd Tanous 
101796825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app)
101896825bebSEd Tanous {
101996825bebSEd Tanous     BMCWEB_ROUTE(
102096825bebSEd Tanous         app,
102196825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia")
102296825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
102396825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
102496825bebSEd Tanous             handleManagersVirtualMediaActionInsertPost, std::ref(app)));
102596825bebSEd Tanous 
102696825bebSEd Tanous     BMCWEB_ROUTE(
102796825bebSEd Tanous         app,
102896825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia")
102996825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
103096825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
103196825bebSEd Tanous             handleManagersVirtualMediaActionEject, std::ref(app)));
103296825bebSEd Tanous 
103396825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/")
103496825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMediaCollection)
103596825bebSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
103696825bebSEd Tanous             handleManagersVirtualMediaCollectionGet, std::ref(app)));
103796825bebSEd Tanous 
103896825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/")
103996825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMedia)
104096825bebSEd Tanous         .methods(boost::beast::http::verb::get)(
104196825bebSEd Tanous             std::bind_front(handleVirtualMediaGet, std::ref(app)));
1042107077deSPrzemyslaw Czarnowski }
1043107077deSPrzemyslaw Czarnowski 
1044107077deSPrzemyslaw Czarnowski } // namespace redfish
1045