xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 177612aaa0633cf9d5aef0b763a43135cf552d9b)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
470141561SBorawski.Lukasz #pragma once
570141561SBorawski.Lukasz 
6d7857201SEd Tanous #include "bmcweb_config.h"
7d7857201SEd Tanous 
83ccb3adbSEd Tanous #include "app.hpp"
9d7857201SEd Tanous #include "async_resp.hpp"
103ccb3adbSEd Tanous #include "dbus_utility.hpp"
113a8a0088SKowalski, Kamil #include "error_messages.hpp"
12539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
13d7857201SEd Tanous #include "http_request.hpp"
144d7b5ddbSMalik Akbar Hashemi Rafsanjani #include "identity.hpp"
15d7857201SEd Tanous #include "logging.hpp"
16d7857201SEd Tanous #include "privileges.hpp"
173ccb3adbSEd Tanous #include "query.hpp"
18b4bec66bSAbhishek Patel #include "redfish_util.hpp"
193ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
20d7857201SEd Tanous #include "utils/dbus_utils.hpp"
213ccb3adbSEd Tanous #include "utils/json_utils.hpp"
223ccb3adbSEd Tanous #include "utils/stl_utils.hpp"
2370141561SBorawski.Lukasz 
24d7857201SEd Tanous #include <boost/beast/http/field.hpp>
25d7857201SEd Tanous #include <boost/beast/http/status.hpp>
26d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
27e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
28253f11b8SEd Tanous #include <boost/url/format.hpp>
29d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
301214b7e7SGunnar Mills 
31e99073f5SGeorge Liu #include <array>
32d7857201SEd Tanous #include <cstddef>
33d7857201SEd Tanous #include <functional>
34d7857201SEd Tanous #include <memory>
351214b7e7SGunnar Mills #include <optional>
36d7857201SEd Tanous #include <string>
37e99073f5SGeorge Liu #include <string_view>
38d7857201SEd Tanous #include <tuple>
39d7857201SEd Tanous #include <utility>
40abf2add6SEd Tanous #include <variant>
410eebcefbSJishnu CM #include <vector>
425f4c798dSJiaqing Zhao 
431abe55efSEd Tanous namespace redfish
441abe55efSEd Tanous {
4570141561SBorawski.Lukasz 
467e860f15SJohn Edward Broadbent void getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp);
477e860f15SJohn Edward Broadbent 
485c3e9272SAbhishek Patel static constexpr std::string_view sshServiceName = "dropbear";
495c3e9272SAbhishek Patel static constexpr std::string_view httpsServiceName = "bmcweb";
505c3e9272SAbhishek Patel static constexpr std::string_view ipmiServiceName = "phosphor-ipmi-net";
515c3e9272SAbhishek Patel 
525c3e9272SAbhishek Patel // Mapping from Redfish NetworkProtocol key name to backend service that hosts
535c3e9272SAbhishek Patel // that protocol.
545c3e9272SAbhishek Patel static constexpr std::array<std::pair<std::string_view, std::string_view>, 3>
555c3e9272SAbhishek Patel     networkProtocolToDbus = {{{"SSH", sshServiceName},
5669320d54SJiaqing Zhao                               {"HTTPS", httpsServiceName},
5769320d54SJiaqing Zhao                               {"IPMI", ipmiServiceName}}};
583a8a0088SKowalski, Kamil 
59711ac7a9SEd Tanous inline void extractNTPServersAndDomainNamesData(
60711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
610eebcefbSJishnu CM     std::vector<std::string>& ntpData, std::vector<std::string>& dynamicNtpData,
620eebcefbSJishnu CM     std::vector<std::string>& dnData)
6320e6ea5dSraviteja-b {
6481ce609eSEd Tanous     for (const auto& obj : dbusData)
6520e6ea5dSraviteja-b     {
6620e6ea5dSraviteja-b         for (const auto& ifacePair : obj.second)
6720e6ea5dSraviteja-b         {
680a052baaSGeorge Liu             if (ifacePair.first !=
6920e6ea5dSraviteja-b                 "xyz.openbmc_project.Network.EthernetInterface")
7020e6ea5dSraviteja-b             {
710a052baaSGeorge Liu                 continue;
720a052baaSGeorge Liu             }
730a052baaSGeorge Liu 
7420e6ea5dSraviteja-b             for (const auto& propertyPair : ifacePair.second)
7520e6ea5dSraviteja-b             {
76fcd2682aSEd Tanous                 if (propertyPair.first == "StaticNTPServers")
7720e6ea5dSraviteja-b                 {
7820e6ea5dSraviteja-b                     const std::vector<std::string>* ntpServers =
798d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
8020e6ea5dSraviteja-b                             &propertyPair.second);
8120e6ea5dSraviteja-b                     if (ntpServers != nullptr)
8220e6ea5dSraviteja-b                     {
83c251fe81SJian Zhang                         ntpData.insert(ntpData.end(), ntpServers->begin(),
84c251fe81SJian Zhang                                        ntpServers->end());
8520e6ea5dSraviteja-b                     }
8620e6ea5dSraviteja-b                 }
870eebcefbSJishnu CM                 else if (propertyPair.first == "NTPServers")
880eebcefbSJishnu CM                 {
890eebcefbSJishnu CM                     const std::vector<std::string>* dynamicNtpServers =
900eebcefbSJishnu CM                         std::get_if<std::vector<std::string>>(
910eebcefbSJishnu CM                             &propertyPair.second);
920eebcefbSJishnu CM                     if (dynamicNtpServers != nullptr)
930eebcefbSJishnu CM                     {
940eebcefbSJishnu CM                         dynamicNtpData = *dynamicNtpServers;
950eebcefbSJishnu CM                     }
960eebcefbSJishnu CM                 }
97d24bfc7aSJennifer Lee                 else if (propertyPair.first == "DomainName")
98d24bfc7aSJennifer Lee                 {
99d24bfc7aSJennifer Lee                     const std::vector<std::string>* domainNames =
1008d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
101d24bfc7aSJennifer Lee                             &propertyPair.second);
102d24bfc7aSJennifer Lee                     if (domainNames != nullptr)
103d24bfc7aSJennifer Lee                     {
104c251fe81SJian Zhang                         dnData.insert(dnData.end(), domainNames->begin(),
105c251fe81SJian Zhang                                       domainNames->end());
106d24bfc7aSJennifer Lee                     }
107d24bfc7aSJennifer Lee                 }
10820e6ea5dSraviteja-b             }
10920e6ea5dSraviteja-b         }
11020e6ea5dSraviteja-b     }
1110225b87bSEd Tanous     stl_utils::removeDuplicate(ntpData);
112c251fe81SJian Zhang     stl_utils::removeDuplicate(dnData);
11320e6ea5dSraviteja-b }
11420e6ea5dSraviteja-b 
11520e6ea5dSraviteja-b template <typename CallbackFunc>
11620e6ea5dSraviteja-b void getEthernetIfaceData(CallbackFunc&& callback)
11720e6ea5dSraviteja-b {
1185eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1195eb468daSGeorge Liu     dbus::utility::getManagedObjects(
1205eb468daSGeorge Liu         "xyz.openbmc_project.Network", path,
1218cb2c024SEd Tanous         [callback = std::forward<CallbackFunc>(callback)](
1228b24275dSEd Tanous             const boost::system::error_code& ec,
123711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& dbusData) {
12420e6ea5dSraviteja-b             std::vector<std::string> ntpServers;
1250eebcefbSJishnu CM             std::vector<std::string> dynamicNtpServers;
126d24bfc7aSJennifer Lee             std::vector<std::string> domainNames;
12720e6ea5dSraviteja-b 
1288b24275dSEd Tanous             if (ec)
12920e6ea5dSraviteja-b             {
1300eebcefbSJishnu CM                 callback(false, ntpServers, dynamicNtpServers, domainNames);
13120e6ea5dSraviteja-b                 return;
13220e6ea5dSraviteja-b             }
13320e6ea5dSraviteja-b 
134bd79bce8SPatrick Williams             extractNTPServersAndDomainNamesData(dbusData, ntpServers,
1350eebcefbSJishnu CM                                                 dynamicNtpServers, domainNames);
13620e6ea5dSraviteja-b 
1370eebcefbSJishnu CM             callback(true, ntpServers, dynamicNtpServers, domainNames);
1385eb468daSGeorge Liu         });
139271584abSEd Tanous }
14020e6ea5dSraviteja-b 
1415c3e9272SAbhishek Patel inline void afterNetworkPortRequest(
1425c3e9272SAbhishek Patel     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1435c3e9272SAbhishek Patel     const boost::system::error_code& ec,
1445c3e9272SAbhishek Patel     const std::vector<std::tuple<std::string, std::string, bool>>& socketData)
1455c3e9272SAbhishek Patel {
1465c3e9272SAbhishek Patel     if (ec)
1475c3e9272SAbhishek Patel     {
1485c3e9272SAbhishek Patel         messages::internalError(asyncResp->res);
1495c3e9272SAbhishek Patel         return;
1505c3e9272SAbhishek Patel     }
1515c3e9272SAbhishek Patel     for (const auto& data : socketData)
1525c3e9272SAbhishek Patel     {
1535c3e9272SAbhishek Patel         const std::string& socketPath = get<0>(data);
1545c3e9272SAbhishek Patel         const std::string& protocolName = get<1>(data);
1555c3e9272SAbhishek Patel         bool isProtocolEnabled = get<2>(data);
1565c3e9272SAbhishek Patel 
1575c3e9272SAbhishek Patel         asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
1585c3e9272SAbhishek Patel             isProtocolEnabled;
1595c3e9272SAbhishek Patel         asyncResp->res.jsonValue[protocolName]["Port"] = nullptr;
1605c3e9272SAbhishek Patel         getPortNumber(socketPath, [asyncResp, protocolName](
1615c3e9272SAbhishek Patel                                       const boost::system::error_code& ec2,
1625c3e9272SAbhishek Patel                                       int portNumber) {
1635c3e9272SAbhishek Patel             if (ec2)
1645c3e9272SAbhishek Patel             {
1655c3e9272SAbhishek Patel                 messages::internalError(asyncResp->res);
1665c3e9272SAbhishek Patel                 return;
1675c3e9272SAbhishek Patel             }
1685c3e9272SAbhishek Patel             asyncResp->res.jsonValue[protocolName]["Port"] = portNumber;
1695c3e9272SAbhishek Patel         });
1705c3e9272SAbhishek Patel     }
1715c3e9272SAbhishek Patel }
1725c3e9272SAbhishek Patel 
1734f48d5f6SEd Tanous inline void getNetworkData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17472048780SAbhishek Patel                            const crow::Request& req)
1751abe55efSEd Tanous {
1763e72c202SNinad Palsule     if (req.session == nullptr)
1773e72c202SNinad Palsule     {
1783e72c202SNinad Palsule         messages::internalError(asyncResp->res);
1793e72c202SNinad Palsule         return;
1803e72c202SNinad Palsule     }
1813e72c202SNinad Palsule 
182e9f71672SEd Tanous     asyncResp->res.addHeader(
183e9f71672SEd Tanous         boost::beast::http::field::link,
184e9f71672SEd Tanous         "</redfish/v1/JsonSchemas/ManagerNetworkProtocol/NetworkProtocol.json>; rel=describedby");
1850f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
1860eebcefbSJishnu CM         "#ManagerNetworkProtocol.v1_9_0.ManagerNetworkProtocol";
1870f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
188253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
189253f11b8SEd Tanous                             BMCWEB_REDFISH_MANAGER_URI_NAME);
1900f74e643SEd Tanous     asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
1910f74e643SEd Tanous     asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
1920f74e643SEd Tanous     asyncResp->res.jsonValue["Description"] = "Manager Network Service";
193539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
194539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["HealthRollup"] = resource::Health::OK;
195539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
1960f74e643SEd Tanous 
19761932318SXiaochao Ma     // HTTP is Mandatory attribute as per OCP Baseline Profile - v1.0.0,
198818ea7b8SJoshi-Mansi     // but from security perspective it is not recommended to use.
199818ea7b8SJoshi-Mansi     // Hence using protocolEnabled as false to make it OCP and security-wise
200818ea7b8SJoshi-Mansi     // compliant
2015c3e9272SAbhishek Patel     asyncResp->res.jsonValue["HTTP"]["Port"] = nullptr;
202818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["ProtocolEnabled"] = false;
203818ea7b8SJoshi-Mansi 
2041ee2db7fSAndrew Geissler     // The ProtocolEnabled of the following protocols is determined by
2051ee2db7fSAndrew Geissler     // inspecting the state of associated systemd sockets. If these protocols
2061ee2db7fSAndrew Geissler     // have been disabled, then the systemd socket unit files will not be found
2071ee2db7fSAndrew Geissler     // and the protocols will not be returned in this Redfish query. Set some
2081ee2db7fSAndrew Geissler     // defaults to ensure something is always returned.
2091ee2db7fSAndrew Geissler     for (const auto& nwkProtocol : networkProtocolToDbus)
2101ee2db7fSAndrew Geissler     {
2111ee2db7fSAndrew Geissler         asyncResp->res.jsonValue[nwkProtocol.first]["Port"] = nullptr;
2121ee2db7fSAndrew Geissler         asyncResp->res.jsonValue[nwkProtocol.first]["ProtocolEnabled"] = false;
2131ee2db7fSAndrew Geissler     }
2141ee2db7fSAndrew Geissler 
215d24bfc7aSJennifer Lee     std::string hostName = getHostName();
216d24bfc7aSJennifer Lee 
217d24bfc7aSJennifer Lee     asyncResp->res.jsonValue["HostName"] = hostName;
2183a8a0088SKowalski, Kamil 
21920e6ea5dSraviteja-b     getNTPProtocolEnabled(asyncResp);
22020e6ea5dSraviteja-b 
221bd79bce8SPatrick Williams     getEthernetIfaceData([hostName, asyncResp](
222bd79bce8SPatrick Williams                              const bool& success,
22302cad96eSEd Tanous                              const std::vector<std::string>& ntpServers,
2240eebcefbSJishnu CM                              const std::vector<std::string>& dynamicNtpServers,
225d24bfc7aSJennifer Lee                              const std::vector<std::string>& domainNames) {
22620e6ea5dSraviteja-b         if (!success)
22720e6ea5dSraviteja-b         {
2280a052baaSGeorge Liu             messages::resourceNotFound(asyncResp->res, "ManagerNetworkProtocol",
2290a052baaSGeorge Liu                                        "NetworkProtocol");
23020e6ea5dSraviteja-b             return;
23120e6ea5dSraviteja-b         }
23220e6ea5dSraviteja-b         asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
2330eebcefbSJishnu CM         asyncResp->res.jsonValue["NTP"]["NetworkSuppliedServers"] =
2340eebcefbSJishnu CM             dynamicNtpServers;
23526f6976fSEd Tanous         if (!hostName.empty())
236d24bfc7aSJennifer Lee         {
237f23b7296SEd Tanous             std::string fqdn = hostName;
23826f6976fSEd Tanous             if (!domainNames.empty())
239d24bfc7aSJennifer Lee             {
240f23b7296SEd Tanous                 fqdn += ".";
241f23b7296SEd Tanous                 fqdn += domainNames[0];
242d24bfc7aSJennifer Lee             }
2432c70f800SEd Tanous             asyncResp->res.jsonValue["FQDN"] = std::move(fqdn);
244d24bfc7aSJennifer Lee         }
24520e6ea5dSraviteja-b     });
24620e6ea5dSraviteja-b 
24772048780SAbhishek Patel     Privileges effectiveUserPrivileges =
2483e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
24972048780SAbhishek Patel 
25072048780SAbhishek Patel     // /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates is
25172048780SAbhishek Patel     // something only ConfigureManager can access then only display when
25272048780SAbhishek Patel     // the user has permissions ConfigureManager
25372048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
25472048780SAbhishek Patel                                          effectiveUserPrivileges))
25572048780SAbhishek Patel     {
2561476687dSEd Tanous         asyncResp->res.jsonValue["HTTPS"]["Certificates"]["@odata.id"] =
257253f11b8SEd Tanous             boost::urls::format(
258253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates",
259253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
26070141561SBorawski.Lukasz     }
26170141561SBorawski.Lukasz 
2625c3e9272SAbhishek Patel     getPortStatusAndPath(std::span(networkProtocolToDbus),
2635c3e9272SAbhishek Patel                          std::bind_front(afterNetworkPortRequest, asyncResp));
264b4bec66bSAbhishek Patel } // namespace redfish
265501be32bSraviteja-b 
2668e157735SEd Tanous inline void afterSetNTP(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2678e157735SEd Tanous                         const boost::system::error_code& ec)
26820e6ea5dSraviteja-b {
2698e157735SEd Tanous     if (ec)
27020e6ea5dSraviteja-b     {
2718e157735SEd Tanous         BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
2728e157735SEd Tanous                          ec);
2738e157735SEd Tanous         messages::internalError(asyncResp->res);
2748e157735SEd Tanous         return;
27520e6ea5dSraviteja-b     }
2768e157735SEd Tanous     asyncResp->res.result(boost::beast::http::status::no_content);
27720e6ea5dSraviteja-b }
27820e6ea5dSraviteja-b 
2798e157735SEd Tanous inline void handleNTPProtocolEnabled(
2808e157735SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, bool ntpEnabled)
2818e157735SEd Tanous {
2828e157735SEd Tanous     bool interactive = false;
2838e157735SEd Tanous     auto callback = [asyncResp](const boost::system::error_code& ec) {
2848e157735SEd Tanous         afterSetNTP(asyncResp, ec);
2858e157735SEd Tanous     };
286*177612aaSEd Tanous     dbus::utility::async_method_call(
287*177612aaSEd Tanous         asyncResp, std::move(callback), "org.freedesktop.timedate1",
2888e157735SEd Tanous         "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "SetNTP",
2898e157735SEd Tanous         ntpEnabled, interactive);
29020e6ea5dSraviteja-b }
29120e6ea5dSraviteja-b 
292ed4de7a8SEd Tanous // Redfish states that ip addresses can be
293ed4de7a8SEd Tanous // string, to set a value
294ed4de7a8SEd Tanous // null, to delete the value
295ed4de7a8SEd Tanous // object_t, empty json object, to ignore the value
296ed4de7a8SEd Tanous using IpAddress =
297ed4de7a8SEd Tanous     std::variant<std::string, nlohmann::json::object_t, std::nullptr_t>;
298ed4de7a8SEd Tanous 
299504af5a0SPatrick Williams inline void handleNTPServersPatch(
300504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
301ed4de7a8SEd Tanous     const std::vector<IpAddress>& ntpServerObjects,
302b9e15228SEd Tanous     std::vector<std::string> currentNtpServers)
30320e6ea5dSraviteja-b {
304b9e15228SEd Tanous     std::vector<std::string>::iterator currentNtpServer =
305b9e15228SEd Tanous         currentNtpServers.begin();
306b9e15228SEd Tanous     for (size_t index = 0; index < ntpServerObjects.size(); index++)
307287ece64SGeorge Liu     {
308ed4de7a8SEd Tanous         const IpAddress& ntpServer = ntpServerObjects[index];
309ed4de7a8SEd Tanous         if (std::holds_alternative<std::nullptr_t>(ntpServer))
310b9e15228SEd Tanous         {
311b9e15228SEd Tanous             // Can't delete an item that doesn't exist
312b9e15228SEd Tanous             if (currentNtpServer == currentNtpServers.end())
313b9e15228SEd Tanous             {
314bd79bce8SPatrick Williams                 messages::propertyValueNotInList(
315bd79bce8SPatrick Williams                     asyncResp->res, "null",
316bd79bce8SPatrick Williams                     "NTP/NTPServers/" + std::to_string(index));
317b9e15228SEd Tanous 
318287ece64SGeorge Liu                 return;
319287ece64SGeorge Liu             }
320b9e15228SEd Tanous             currentNtpServer = currentNtpServers.erase(currentNtpServer);
321b9e15228SEd Tanous             continue;
322b9e15228SEd Tanous         }
323b9e15228SEd Tanous         const nlohmann::json::object_t* ntpServerObject =
324ed4de7a8SEd Tanous             std::get_if<nlohmann::json::object_t>(&ntpServer);
325b9e15228SEd Tanous         if (ntpServerObject != nullptr)
326b9e15228SEd Tanous         {
327b9e15228SEd Tanous             if (!ntpServerObject->empty())
328b9e15228SEd Tanous             {
329ed4de7a8SEd Tanous                 messages::propertyValueNotInList(
330ed4de7a8SEd Tanous                     asyncResp->res, *ntpServerObject,
331ed4de7a8SEd Tanous                     "NTP/NTPServers/" + std::to_string(index));
332b9e15228SEd Tanous                 return;
333b9e15228SEd Tanous             }
334b9e15228SEd Tanous             // Can't retain an item that doesn't exist
335b9e15228SEd Tanous             if (currentNtpServer == currentNtpServers.end())
336b9e15228SEd Tanous             {
337ed4de7a8SEd Tanous                 messages::propertyValueOutOfRange(
338ed4de7a8SEd Tanous                     asyncResp->res, *ntpServerObject,
339ed4de7a8SEd Tanous                     "NTP/NTPServers/" + std::to_string(index));
340b9e15228SEd Tanous 
341b9e15228SEd Tanous                 return;
342b9e15228SEd Tanous             }
343b9e15228SEd Tanous             // empty objects should leave the NtpServer unmodified
344b9e15228SEd Tanous             currentNtpServer++;
345b9e15228SEd Tanous             continue;
346b9e15228SEd Tanous         }
347b9e15228SEd Tanous 
348ed4de7a8SEd Tanous         const std::string* ntpServerStr = std::get_if<std::string>(&ntpServer);
349b9e15228SEd Tanous         if (ntpServerStr == nullptr)
350b9e15228SEd Tanous         {
351ed4de7a8SEd Tanous             messages::internalError(asyncResp->res);
352b9e15228SEd Tanous             return;
353b9e15228SEd Tanous         }
354b9e15228SEd Tanous         if (currentNtpServer == currentNtpServers.end())
355b9e15228SEd Tanous         {
356b9e15228SEd Tanous             // if we're at the end of the list, append to the end
357b9e15228SEd Tanous             currentNtpServers.push_back(*ntpServerStr);
358b9e15228SEd Tanous             currentNtpServer = currentNtpServers.end();
359b9e15228SEd Tanous             continue;
360b9e15228SEd Tanous         }
361b9e15228SEd Tanous         *currentNtpServer = *ntpServerStr;
362b9e15228SEd Tanous         currentNtpServer++;
363b9e15228SEd Tanous     }
364b9e15228SEd Tanous 
365b9e15228SEd Tanous     // Any remaining array elements should be removed
366b9e15228SEd Tanous     currentNtpServers.erase(currentNtpServer, currentNtpServers.end());
367287ece64SGeorge Liu 
368e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> ethInterfaces = {
369e99073f5SGeorge Liu         "xyz.openbmc_project.Network.EthernetInterface"};
370e99073f5SGeorge Liu     dbus::utility::getSubTree(
371e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, ethInterfaces,
372b9e15228SEd Tanous         [asyncResp, currentNtpServers](
3732138483cSGeorge Liu             const boost::system::error_code& ec,
374b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
3750a052baaSGeorge Liu             if (ec)
3760a052baaSGeorge Liu             {
37762598e31SEd Tanous                 BMCWEB_LOG_WARNING("D-Bus error: {}, {}", ec, ec.message());
3780a052baaSGeorge Liu                 messages::internalError(asyncResp->res);
3790a052baaSGeorge Liu                 return;
3800a052baaSGeorge Liu             }
3810a052baaSGeorge Liu 
3820a052baaSGeorge Liu             for (const auto& [objectPath, serviceMap] : subtree)
3830a052baaSGeorge Liu             {
3840a052baaSGeorge Liu                 for (const auto& [service, interfaces] : serviceMap)
3850a052baaSGeorge Liu                 {
3860a052baaSGeorge Liu                     for (const auto& interface : interfaces)
3870a052baaSGeorge Liu                     {
3880a052baaSGeorge Liu                         if (interface !=
3890a052baaSGeorge Liu                             "xyz.openbmc_project.Network.EthernetInterface")
3900a052baaSGeorge Liu                         {
3910a052baaSGeorge Liu                             continue;
3920a052baaSGeorge Liu                         }
3930a052baaSGeorge Liu 
394e93abac6SGinu George                         setDbusProperty(asyncResp, "NTP/NTPServers/", service,
395bd79bce8SPatrick Williams                                         objectPath, interface,
396bd79bce8SPatrick Williams                                         "StaticNTPServers", currentNtpServers);
39720e6ea5dSraviteja-b                     }
3980a052baaSGeorge Liu                 }
3990a052baaSGeorge Liu             }
400e99073f5SGeorge Liu         });
4010a052baaSGeorge Liu }
40220e6ea5dSraviteja-b 
403504af5a0SPatrick Williams inline void handleProtocolEnabled(
404504af5a0SPatrick Williams     const bool protocolEnabled,
405e5a99777SAlbert Zhang     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
40669320d54SJiaqing Zhao     const std::string& netBasePath)
40767a78d87STom Joseph {
408e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
409e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Service.Attributes"};
410e99073f5SGeorge Liu     dbus::utility::getSubTree(
411e99073f5SGeorge Liu         "/xyz/openbmc_project/control/service", 0, interfaces,
412e5a99777SAlbert Zhang         [protocolEnabled, asyncResp,
4132138483cSGeorge Liu          netBasePath](const boost::system::error_code& ec,
414b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
41567a78d87STom Joseph             if (ec)
41667a78d87STom Joseph             {
41767a78d87STom Joseph                 messages::internalError(asyncResp->res);
41867a78d87STom Joseph                 return;
41967a78d87STom Joseph             }
42067a78d87STom Joseph 
42167a78d87STom Joseph             for (const auto& entry : subtree)
42267a78d87STom Joseph             {
42318f8f608SEd Tanous                 if (entry.first.starts_with(netBasePath))
42467a78d87STom Joseph                 {
42587c44966SAsmitha Karunanithi                     setDbusProperty(
426e93abac6SGinu George                         asyncResp, "IPMI/ProtocolEnabled",
427e93abac6SGinu George                         entry.second.begin()->first, entry.first,
428bd79bce8SPatrick Williams                         "xyz.openbmc_project.Control.Service.Attributes",
429bd79bce8SPatrick Williams                         "Running", protocolEnabled);
43087c44966SAsmitha Karunanithi                     setDbusProperty(
431e93abac6SGinu George                         asyncResp, "IPMI/ProtocolEnabled",
432e93abac6SGinu George                         entry.second.begin()->first, entry.first,
433bd79bce8SPatrick Williams                         "xyz.openbmc_project.Control.Service.Attributes",
434bd79bce8SPatrick Williams                         "Enabled", protocolEnabled);
43567a78d87STom Joseph                 }
43667a78d87STom Joseph             }
437e99073f5SGeorge Liu         });
43867a78d87STom Joseph }
43967a78d87STom Joseph 
440504af5a0SPatrick Williams inline void getNTPProtocolEnabled(
441504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
4427e860f15SJohn Edward Broadbent {
443deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
444deae6a78SEd Tanous         "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
445deae6a78SEd Tanous         "org.freedesktop.timedate1", "NTP",
4468e157735SEd Tanous         [asyncResp](const boost::system::error_code& ec, bool enabled) {
4478b24275dSEd Tanous             if (ec)
4487e860f15SJohn Edward Broadbent             {
4498e157735SEd Tanous                 BMCWEB_LOG_WARNING(
4508e157735SEd Tanous                     "Failed to get NTP status, assuming not supported");
4517e860f15SJohn Edward Broadbent                 return;
4527e860f15SJohn Edward Broadbent             }
4537e860f15SJohn Edward Broadbent 
4548e157735SEd Tanous             asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = enabled;
4551e1e598dSJonathan Doman         });
4567e860f15SJohn Edward Broadbent }
4577e860f15SJohn Edward Broadbent 
4585c3e9272SAbhishek Patel inline std::string encodeServiceObjectPath(std::string_view serviceName)
45969320d54SJiaqing Zhao {
46069320d54SJiaqing Zhao     sdbusplus::message::object_path objPath(
46169320d54SJiaqing Zhao         "/xyz/openbmc_project/control/service");
46269320d54SJiaqing Zhao     objPath /= serviceName;
46369320d54SJiaqing Zhao     return objPath.str;
46469320d54SJiaqing Zhao }
46569320d54SJiaqing Zhao 
4660f55d946SEd Tanous inline void handleBmcNetworkProtocolHead(
467e9f71672SEd Tanous     crow::App& app, const crow::Request& req,
468e9f71672SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
469e9f71672SEd Tanous {
470e9f71672SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
471e9f71672SEd Tanous     {
472e9f71672SEd Tanous         return;
473e9f71672SEd Tanous     }
474e9f71672SEd Tanous     asyncResp->res.addHeader(
475e9f71672SEd Tanous         boost::beast::http::field::link,
476e9f71672SEd Tanous         "</redfish/v1/JsonSchemas/ManagerNetworkProtocol/ManagerNetworkProtocol.json>; rel=describedby");
477e9f71672SEd Tanous }
478e9f71672SEd Tanous 
479e634b34cSEd Tanous inline void handleManagersNetworkProtocolPatch(
480e634b34cSEd Tanous     App& app, const crow::Request& req,
481253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
482253f11b8SEd Tanous     const std::string& managerId)
4837e860f15SJohn Edward Broadbent {
4843ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
48545ca1b86SEd Tanous     {
48645ca1b86SEd Tanous         return;
48745ca1b86SEd Tanous     }
488253f11b8SEd Tanous 
489253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
490253f11b8SEd Tanous     {
491253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
492253f11b8SEd Tanous         return;
493253f11b8SEd Tanous     }
494253f11b8SEd Tanous 
495501be32bSraviteja-b     std::optional<std::string> newHostName;
496ed4de7a8SEd Tanous 
497ed4de7a8SEd Tanous     std::optional<std::vector<IpAddress>> ntpServerObjects;
4985f4c798dSJiaqing Zhao     std::optional<bool> ntpEnabled;
4995f4c798dSJiaqing Zhao     std::optional<bool> ipmiEnabled;
5005f4c798dSJiaqing Zhao     std::optional<bool> sshEnabled;
501501be32bSraviteja-b 
5025f4c798dSJiaqing Zhao     if (!json_util::readJsonPatch(
503afc474aeSMyung Bae             req, asyncResp->res,                 //
504afc474aeSMyung Bae             "HostName", newHostName,             //
505afc474aeSMyung Bae             "NTP/NTPServers", ntpServerObjects,  //
506afc474aeSMyung Bae             "NTP/ProtocolEnabled", ntpEnabled,   //
507afc474aeSMyung Bae             "IPMI/ProtocolEnabled", ipmiEnabled, //
508afc474aeSMyung Bae             "SSH/ProtocolEnabled", sshEnabled    //
509afc474aeSMyung Bae             ))
510501be32bSraviteja-b     {
511501be32bSraviteja-b         return;
512501be32bSraviteja-b     }
513cf05f9dcSJohnathan Mantey 
5148d1b46d7Szhanghch05     asyncResp->res.result(boost::beast::http::status::no_content);
515501be32bSraviteja-b     if (newHostName)
516501be32bSraviteja-b     {
5172db77d34SJohnathan Mantey         messages::propertyNotWritable(asyncResp->res, "HostName");
51844fad2aaSEd Tanous         return;
519cf05f9dcSJohnathan Mantey     }
520cf05f9dcSJohnathan Mantey 
52120e6ea5dSraviteja-b     if (ntpEnabled)
52220e6ea5dSraviteja-b     {
5238e157735SEd Tanous         handleNTPProtocolEnabled(asyncResp, *ntpEnabled);
52420e6ea5dSraviteja-b     }
525b9e15228SEd Tanous     if (ntpServerObjects)
52620e6ea5dSraviteja-b     {
527b9e15228SEd Tanous         getEthernetIfaceData(
528b9e15228SEd Tanous             [asyncResp, ntpServerObjects](
529e634b34cSEd Tanous                 const bool success, std::vector<std::string>& currentNtpServers,
5300eebcefbSJishnu CM                 const std::vector<std::string>& /*dynamicNtpServers*/,
531b9e15228SEd Tanous                 const std::vector<std::string>& /*domainNames*/) {
532b9e15228SEd Tanous                 if (!success)
533b9e15228SEd Tanous                 {
534b9e15228SEd Tanous                     messages::internalError(asyncResp->res);
535b9e15228SEd Tanous                     return;
536b9e15228SEd Tanous                 }
537b9e15228SEd Tanous                 handleNTPServersPatch(asyncResp, *ntpServerObjects,
538b9e15228SEd Tanous                                       std::move(currentNtpServers));
539b9e15228SEd Tanous             });
54020e6ea5dSraviteja-b     }
54167a78d87STom Joseph 
5425f4c798dSJiaqing Zhao     if (ipmiEnabled)
54367a78d87STom Joseph     {
544e5a99777SAlbert Zhang         handleProtocolEnabled(
5455f4c798dSJiaqing Zhao             *ipmiEnabled, asyncResp,
54669320d54SJiaqing Zhao             encodeServiceObjectPath(std::string(ipmiServiceName) + '@'));
547e5a99777SAlbert Zhang     }
548e5a99777SAlbert Zhang 
5495f4c798dSJiaqing Zhao     if (sshEnabled)
550e5a99777SAlbert Zhang     {
55169320d54SJiaqing Zhao         handleProtocolEnabled(*sshEnabled, asyncResp,
55269320d54SJiaqing Zhao                               encodeServiceObjectPath(sshServiceName));
55367a78d87STom Joseph     }
554e634b34cSEd Tanous }
555e634b34cSEd Tanous 
556af20dd1cSEd Tanous inline void handleManagersNetworkProtocolHead(
557e634b34cSEd Tanous     App& app, const crow::Request& req,
558253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
559253f11b8SEd Tanous     const std::string& managerId)
560e634b34cSEd Tanous {
561e634b34cSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
562e634b34cSEd Tanous     {
563e634b34cSEd Tanous         return;
564e634b34cSEd Tanous     }
565af20dd1cSEd Tanous     asyncResp->res.addHeader(
566af20dd1cSEd Tanous         boost::beast::http::field::link,
567af20dd1cSEd Tanous         "</redfish/v1/JsonSchemas/ManagerNetworkProtocol/ManagerNetworkProtocol.json>; rel=describedby");
568253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
569253f11b8SEd Tanous     {
570253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
571253f11b8SEd Tanous         return;
572253f11b8SEd Tanous     }
573af20dd1cSEd Tanous }
574af20dd1cSEd Tanous 
575af20dd1cSEd Tanous inline void handleManagersNetworkProtocolGet(
576af20dd1cSEd Tanous     App& app, const crow::Request& req,
577253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
578253f11b8SEd Tanous     const std::string& managerId)
579af20dd1cSEd Tanous {
580253f11b8SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
581253f11b8SEd Tanous     {
582253f11b8SEd Tanous         return;
583253f11b8SEd Tanous     }
584253f11b8SEd Tanous     asyncResp->res.addHeader(
585253f11b8SEd Tanous         boost::beast::http::field::link,
586253f11b8SEd Tanous         "</redfish/v1/JsonSchemas/ManagerNetworkProtocol/ManagerNetworkProtocol.json>; rel=describedby");
587253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
588253f11b8SEd Tanous     {
589253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
590253f11b8SEd Tanous         return;
591253f11b8SEd Tanous     }
592253f11b8SEd Tanous 
593e634b34cSEd Tanous     getNetworkData(asyncResp, req);
594e634b34cSEd Tanous }
595e634b34cSEd Tanous 
596e634b34cSEd Tanous inline void requestRoutesNetworkProtocol(App& app)
597e634b34cSEd Tanous {
598253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/NetworkProtocol/")
599e634b34cSEd Tanous         .privileges(redfish::privileges::patchManagerNetworkProtocol)
600e634b34cSEd Tanous         .methods(boost::beast::http::verb::patch)(
601e634b34cSEd Tanous             std::bind_front(handleManagersNetworkProtocolPatch, std::ref(app)));
6027e860f15SJohn Edward Broadbent 
603253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/NetworkProtocol/")
604e9f71672SEd Tanous         .privileges(redfish::privileges::headManagerNetworkProtocol)
605e9f71672SEd Tanous         .methods(boost::beast::http::verb::head)(
606af20dd1cSEd Tanous             std::bind_front(handleManagersNetworkProtocolHead, std::ref(app)));
607e9f71672SEd Tanous 
608253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/NetworkProtocol/")
609ed398213SEd Tanous         .privileges(redfish::privileges::getManagerNetworkProtocol)
6107e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
611e634b34cSEd Tanous             std::bind_front(handleManagersNetworkProtocolGet, std::ref(app)));
612cf05f9dcSJohnathan Mantey }
61370141561SBorawski.Lukasz 
61470141561SBorawski.Lukasz } // namespace redfish
615