xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 4d7b5ddb3a2b6cc42b7bbc0c710f297e6df4fd55)
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"
10d7857201SEd Tanous #include "dbus_singleton.hpp"
113ccb3adbSEd Tanous #include "dbus_utility.hpp"
123a8a0088SKowalski, Kamil #include "error_messages.hpp"
13539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
14d7857201SEd Tanous #include "http_request.hpp"
15*4d7b5ddbSMalik Akbar Hashemi Rafsanjani #include "identity.hpp"
16d7857201SEd Tanous #include "logging.hpp"
17d7857201SEd Tanous #include "privileges.hpp"
183ccb3adbSEd Tanous #include "query.hpp"
19b4bec66bSAbhishek Patel #include "redfish_util.hpp"
203ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
21d7857201SEd Tanous #include "utils/dbus_utils.hpp"
223ccb3adbSEd Tanous #include "utils/json_utils.hpp"
233ccb3adbSEd Tanous #include "utils/stl_utils.hpp"
2470141561SBorawski.Lukasz 
25d7857201SEd Tanous #include <boost/beast/http/field.hpp>
26d7857201SEd Tanous #include <boost/beast/http/status.hpp>
27d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
28e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
29253f11b8SEd Tanous #include <boost/url/format.hpp>
30d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
311214b7e7SGunnar Mills 
32e99073f5SGeorge Liu #include <array>
33d7857201SEd Tanous #include <cstddef>
34d7857201SEd Tanous #include <functional>
35d7857201SEd Tanous #include <memory>
361214b7e7SGunnar Mills #include <optional>
37d7857201SEd Tanous #include <string>
38e99073f5SGeorge Liu #include <string_view>
39d7857201SEd Tanous #include <tuple>
40d7857201SEd Tanous #include <utility>
41abf2add6SEd Tanous #include <variant>
420eebcefbSJishnu CM #include <vector>
435f4c798dSJiaqing Zhao 
441abe55efSEd Tanous namespace redfish
451abe55efSEd Tanous {
4670141561SBorawski.Lukasz 
477e860f15SJohn Edward Broadbent void getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp);
487e860f15SJohn Edward Broadbent 
495c3e9272SAbhishek Patel static constexpr std::string_view sshServiceName = "dropbear";
505c3e9272SAbhishek Patel static constexpr std::string_view httpsServiceName = "bmcweb";
515c3e9272SAbhishek Patel static constexpr std::string_view ipmiServiceName = "phosphor-ipmi-net";
525c3e9272SAbhishek Patel 
535c3e9272SAbhishek Patel // Mapping from Redfish NetworkProtocol key name to backend service that hosts
545c3e9272SAbhishek Patel // that protocol.
555c3e9272SAbhishek Patel static constexpr std::array<std::pair<std::string_view, std::string_view>, 3>
565c3e9272SAbhishek Patel     networkProtocolToDbus = {{{"SSH", sshServiceName},
5769320d54SJiaqing Zhao                               {"HTTPS", httpsServiceName},
5869320d54SJiaqing Zhao                               {"IPMI", ipmiServiceName}}};
593a8a0088SKowalski, Kamil 
60711ac7a9SEd Tanous inline void extractNTPServersAndDomainNamesData(
61711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
620eebcefbSJishnu CM     std::vector<std::string>& ntpData, std::vector<std::string>& dynamicNtpData,
630eebcefbSJishnu CM     std::vector<std::string>& dnData)
6420e6ea5dSraviteja-b {
6581ce609eSEd Tanous     for (const auto& obj : dbusData)
6620e6ea5dSraviteja-b     {
6720e6ea5dSraviteja-b         for (const auto& ifacePair : obj.second)
6820e6ea5dSraviteja-b         {
690a052baaSGeorge Liu             if (ifacePair.first !=
7020e6ea5dSraviteja-b                 "xyz.openbmc_project.Network.EthernetInterface")
7120e6ea5dSraviteja-b             {
720a052baaSGeorge Liu                 continue;
730a052baaSGeorge Liu             }
740a052baaSGeorge Liu 
7520e6ea5dSraviteja-b             for (const auto& propertyPair : ifacePair.second)
7620e6ea5dSraviteja-b             {
77fcd2682aSEd Tanous                 if (propertyPair.first == "StaticNTPServers")
7820e6ea5dSraviteja-b                 {
7920e6ea5dSraviteja-b                     const std::vector<std::string>* ntpServers =
808d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
8120e6ea5dSraviteja-b                             &propertyPair.second);
8220e6ea5dSraviteja-b                     if (ntpServers != nullptr)
8320e6ea5dSraviteja-b                     {
84c251fe81SJian Zhang                         ntpData.insert(ntpData.end(), ntpServers->begin(),
85c251fe81SJian Zhang                                        ntpServers->end());
8620e6ea5dSraviteja-b                     }
8720e6ea5dSraviteja-b                 }
880eebcefbSJishnu CM                 else if (propertyPair.first == "NTPServers")
890eebcefbSJishnu CM                 {
900eebcefbSJishnu CM                     const std::vector<std::string>* dynamicNtpServers =
910eebcefbSJishnu CM                         std::get_if<std::vector<std::string>>(
920eebcefbSJishnu CM                             &propertyPair.second);
930eebcefbSJishnu CM                     if (dynamicNtpServers != nullptr)
940eebcefbSJishnu CM                     {
950eebcefbSJishnu CM                         dynamicNtpData = *dynamicNtpServers;
960eebcefbSJishnu CM                     }
970eebcefbSJishnu CM                 }
98d24bfc7aSJennifer Lee                 else if (propertyPair.first == "DomainName")
99d24bfc7aSJennifer Lee                 {
100d24bfc7aSJennifer Lee                     const std::vector<std::string>* domainNames =
1018d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
102d24bfc7aSJennifer Lee                             &propertyPair.second);
103d24bfc7aSJennifer Lee                     if (domainNames != nullptr)
104d24bfc7aSJennifer Lee                     {
105c251fe81SJian Zhang                         dnData.insert(dnData.end(), domainNames->begin(),
106c251fe81SJian Zhang                                       domainNames->end());
107d24bfc7aSJennifer Lee                     }
108d24bfc7aSJennifer Lee                 }
10920e6ea5dSraviteja-b             }
11020e6ea5dSraviteja-b         }
11120e6ea5dSraviteja-b     }
1120225b87bSEd Tanous     stl_utils::removeDuplicate(ntpData);
113c251fe81SJian Zhang     stl_utils::removeDuplicate(dnData);
11420e6ea5dSraviteja-b }
11520e6ea5dSraviteja-b 
11620e6ea5dSraviteja-b template <typename CallbackFunc>
11720e6ea5dSraviteja-b void getEthernetIfaceData(CallbackFunc&& callback)
11820e6ea5dSraviteja-b {
1195eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
1205eb468daSGeorge Liu     dbus::utility::getManagedObjects(
1215eb468daSGeorge Liu         "xyz.openbmc_project.Network", path,
1228cb2c024SEd Tanous         [callback = std::forward<CallbackFunc>(callback)](
1238b24275dSEd Tanous             const boost::system::error_code& ec,
124711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& dbusData) {
12520e6ea5dSraviteja-b             std::vector<std::string> ntpServers;
1260eebcefbSJishnu CM             std::vector<std::string> dynamicNtpServers;
127d24bfc7aSJennifer Lee             std::vector<std::string> domainNames;
12820e6ea5dSraviteja-b 
1298b24275dSEd Tanous             if (ec)
13020e6ea5dSraviteja-b             {
1310eebcefbSJishnu CM                 callback(false, ntpServers, dynamicNtpServers, domainNames);
13220e6ea5dSraviteja-b                 return;
13320e6ea5dSraviteja-b             }
13420e6ea5dSraviteja-b 
135bd79bce8SPatrick Williams             extractNTPServersAndDomainNamesData(dbusData, ntpServers,
1360eebcefbSJishnu CM                                                 dynamicNtpServers, domainNames);
13720e6ea5dSraviteja-b 
1380eebcefbSJishnu CM             callback(true, ntpServers, dynamicNtpServers, domainNames);
1395eb468daSGeorge Liu         });
140271584abSEd Tanous }
14120e6ea5dSraviteja-b 
1425c3e9272SAbhishek Patel inline void afterNetworkPortRequest(
1435c3e9272SAbhishek Patel     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1445c3e9272SAbhishek Patel     const boost::system::error_code& ec,
1455c3e9272SAbhishek Patel     const std::vector<std::tuple<std::string, std::string, bool>>& socketData)
1465c3e9272SAbhishek Patel {
1475c3e9272SAbhishek Patel     if (ec)
1485c3e9272SAbhishek Patel     {
1495c3e9272SAbhishek Patel         messages::internalError(asyncResp->res);
1505c3e9272SAbhishek Patel         return;
1515c3e9272SAbhishek Patel     }
1525c3e9272SAbhishek Patel     for (const auto& data : socketData)
1535c3e9272SAbhishek Patel     {
1545c3e9272SAbhishek Patel         const std::string& socketPath = get<0>(data);
1555c3e9272SAbhishek Patel         const std::string& protocolName = get<1>(data);
1565c3e9272SAbhishek Patel         bool isProtocolEnabled = get<2>(data);
1575c3e9272SAbhishek Patel 
1585c3e9272SAbhishek Patel         asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
1595c3e9272SAbhishek Patel             isProtocolEnabled;
1605c3e9272SAbhishek Patel         asyncResp->res.jsonValue[protocolName]["Port"] = nullptr;
1615c3e9272SAbhishek Patel         getPortNumber(socketPath, [asyncResp, protocolName](
1625c3e9272SAbhishek Patel                                       const boost::system::error_code& ec2,
1635c3e9272SAbhishek Patel                                       int portNumber) {
1645c3e9272SAbhishek Patel             if (ec2)
1655c3e9272SAbhishek Patel             {
1665c3e9272SAbhishek Patel                 messages::internalError(asyncResp->res);
1675c3e9272SAbhishek Patel                 return;
1685c3e9272SAbhishek Patel             }
1695c3e9272SAbhishek Patel             asyncResp->res.jsonValue[protocolName]["Port"] = portNumber;
1705c3e9272SAbhishek Patel         });
1715c3e9272SAbhishek Patel     }
1725c3e9272SAbhishek Patel }
1735c3e9272SAbhishek Patel 
1744f48d5f6SEd Tanous inline void getNetworkData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17572048780SAbhishek Patel                            const crow::Request& req)
1761abe55efSEd Tanous {
1773e72c202SNinad Palsule     if (req.session == nullptr)
1783e72c202SNinad Palsule     {
1793e72c202SNinad Palsule         messages::internalError(asyncResp->res);
1803e72c202SNinad Palsule         return;
1813e72c202SNinad Palsule     }
1823e72c202SNinad Palsule 
183e9f71672SEd Tanous     asyncResp->res.addHeader(
184e9f71672SEd Tanous         boost::beast::http::field::link,
185e9f71672SEd Tanous         "</redfish/v1/JsonSchemas/ManagerNetworkProtocol/NetworkProtocol.json>; rel=describedby");
1860f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
1870eebcefbSJishnu CM         "#ManagerNetworkProtocol.v1_9_0.ManagerNetworkProtocol";
1880f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
189253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
190253f11b8SEd Tanous                             BMCWEB_REDFISH_MANAGER_URI_NAME);
1910f74e643SEd Tanous     asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
1920f74e643SEd Tanous     asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
1930f74e643SEd Tanous     asyncResp->res.jsonValue["Description"] = "Manager Network Service";
194539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
195539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["HealthRollup"] = resource::Health::OK;
196539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
1970f74e643SEd Tanous 
19861932318SXiaochao Ma     // HTTP is Mandatory attribute as per OCP Baseline Profile - v1.0.0,
199818ea7b8SJoshi-Mansi     // but from security perspective it is not recommended to use.
200818ea7b8SJoshi-Mansi     // Hence using protocolEnabled as false to make it OCP and security-wise
201818ea7b8SJoshi-Mansi     // compliant
2025c3e9272SAbhishek Patel     asyncResp->res.jsonValue["HTTP"]["Port"] = nullptr;
203818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["ProtocolEnabled"] = false;
204818ea7b8SJoshi-Mansi 
2051ee2db7fSAndrew Geissler     // The ProtocolEnabled of the following protocols is determined by
2061ee2db7fSAndrew Geissler     // inspecting the state of associated systemd sockets. If these protocols
2071ee2db7fSAndrew Geissler     // have been disabled, then the systemd socket unit files will not be found
2081ee2db7fSAndrew Geissler     // and the protocols will not be returned in this Redfish query. Set some
2091ee2db7fSAndrew Geissler     // defaults to ensure something is always returned.
2101ee2db7fSAndrew Geissler     for (const auto& nwkProtocol : networkProtocolToDbus)
2111ee2db7fSAndrew Geissler     {
2121ee2db7fSAndrew Geissler         asyncResp->res.jsonValue[nwkProtocol.first]["Port"] = nullptr;
2131ee2db7fSAndrew Geissler         asyncResp->res.jsonValue[nwkProtocol.first]["ProtocolEnabled"] = false;
2141ee2db7fSAndrew Geissler     }
2151ee2db7fSAndrew Geissler 
216d24bfc7aSJennifer Lee     std::string hostName = getHostName();
217d24bfc7aSJennifer Lee 
218d24bfc7aSJennifer Lee     asyncResp->res.jsonValue["HostName"] = hostName;
2193a8a0088SKowalski, Kamil 
22020e6ea5dSraviteja-b     getNTPProtocolEnabled(asyncResp);
22120e6ea5dSraviteja-b 
222bd79bce8SPatrick Williams     getEthernetIfaceData([hostName, asyncResp](
223bd79bce8SPatrick Williams                              const bool& success,
22402cad96eSEd Tanous                              const std::vector<std::string>& ntpServers,
2250eebcefbSJishnu CM                              const std::vector<std::string>& dynamicNtpServers,
226d24bfc7aSJennifer Lee                              const std::vector<std::string>& domainNames) {
22720e6ea5dSraviteja-b         if (!success)
22820e6ea5dSraviteja-b         {
2290a052baaSGeorge Liu             messages::resourceNotFound(asyncResp->res, "ManagerNetworkProtocol",
2300a052baaSGeorge Liu                                        "NetworkProtocol");
23120e6ea5dSraviteja-b             return;
23220e6ea5dSraviteja-b         }
23320e6ea5dSraviteja-b         asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
2340eebcefbSJishnu CM         asyncResp->res.jsonValue["NTP"]["NetworkSuppliedServers"] =
2350eebcefbSJishnu CM             dynamicNtpServers;
23626f6976fSEd Tanous         if (!hostName.empty())
237d24bfc7aSJennifer Lee         {
238f23b7296SEd Tanous             std::string fqdn = hostName;
23926f6976fSEd Tanous             if (!domainNames.empty())
240d24bfc7aSJennifer Lee             {
241f23b7296SEd Tanous                 fqdn += ".";
242f23b7296SEd Tanous                 fqdn += domainNames[0];
243d24bfc7aSJennifer Lee             }
2442c70f800SEd Tanous             asyncResp->res.jsonValue["FQDN"] = std::move(fqdn);
245d24bfc7aSJennifer Lee         }
24620e6ea5dSraviteja-b     });
24720e6ea5dSraviteja-b 
24872048780SAbhishek Patel     Privileges effectiveUserPrivileges =
2493e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
25072048780SAbhishek Patel 
25172048780SAbhishek Patel     // /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates is
25272048780SAbhishek Patel     // something only ConfigureManager can access then only display when
25372048780SAbhishek Patel     // the user has permissions ConfigureManager
25472048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
25572048780SAbhishek Patel                                          effectiveUserPrivileges))
25672048780SAbhishek Patel     {
2571476687dSEd Tanous         asyncResp->res.jsonValue["HTTPS"]["Certificates"]["@odata.id"] =
258253f11b8SEd Tanous             boost::urls::format(
259253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates",
260253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
26170141561SBorawski.Lukasz     }
26270141561SBorawski.Lukasz 
2635c3e9272SAbhishek Patel     getPortStatusAndPath(std::span(networkProtocolToDbus),
2645c3e9272SAbhishek Patel                          std::bind_front(afterNetworkPortRequest, asyncResp));
265b4bec66bSAbhishek Patel } // namespace redfish
266501be32bSraviteja-b 
2678e157735SEd Tanous inline void afterSetNTP(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2688e157735SEd Tanous                         const boost::system::error_code& ec)
26920e6ea5dSraviteja-b {
2708e157735SEd Tanous     if (ec)
27120e6ea5dSraviteja-b     {
2728e157735SEd Tanous         BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
2738e157735SEd Tanous                          ec);
2748e157735SEd Tanous         messages::internalError(asyncResp->res);
2758e157735SEd Tanous         return;
27620e6ea5dSraviteja-b     }
2778e157735SEd Tanous     asyncResp->res.result(boost::beast::http::status::no_content);
27820e6ea5dSraviteja-b }
27920e6ea5dSraviteja-b 
2808e157735SEd Tanous inline void handleNTPProtocolEnabled(
2818e157735SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, bool ntpEnabled)
2828e157735SEd Tanous {
2838e157735SEd Tanous     bool interactive = false;
2848e157735SEd Tanous     auto callback = [asyncResp](const boost::system::error_code& ec) {
2858e157735SEd Tanous         afterSetNTP(asyncResp, ec);
2868e157735SEd Tanous     };
2878e157735SEd Tanous     crow::connections::systemBus->async_method_call(
2888e157735SEd Tanous         std::move(callback), "org.freedesktop.timedate1",
2898e157735SEd Tanous         "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "SetNTP",
2908e157735SEd Tanous         ntpEnabled, interactive);
29120e6ea5dSraviteja-b }
29220e6ea5dSraviteja-b 
293ed4de7a8SEd Tanous // Redfish states that ip addresses can be
294ed4de7a8SEd Tanous // string, to set a value
295ed4de7a8SEd Tanous // null, to delete the value
296ed4de7a8SEd Tanous // object_t, empty json object, to ignore the value
297ed4de7a8SEd Tanous using IpAddress =
298ed4de7a8SEd Tanous     std::variant<std::string, nlohmann::json::object_t, std::nullptr_t>;
299ed4de7a8SEd Tanous 
300504af5a0SPatrick Williams inline void handleNTPServersPatch(
301504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
302ed4de7a8SEd Tanous     const std::vector<IpAddress>& ntpServerObjects,
303b9e15228SEd Tanous     std::vector<std::string> currentNtpServers)
30420e6ea5dSraviteja-b {
305b9e15228SEd Tanous     std::vector<std::string>::iterator currentNtpServer =
306b9e15228SEd Tanous         currentNtpServers.begin();
307b9e15228SEd Tanous     for (size_t index = 0; index < ntpServerObjects.size(); index++)
308287ece64SGeorge Liu     {
309ed4de7a8SEd Tanous         const IpAddress& ntpServer = ntpServerObjects[index];
310ed4de7a8SEd Tanous         if (std::holds_alternative<std::nullptr_t>(ntpServer))
311b9e15228SEd Tanous         {
312b9e15228SEd Tanous             // Can't delete an item that doesn't exist
313b9e15228SEd Tanous             if (currentNtpServer == currentNtpServers.end())
314b9e15228SEd Tanous             {
315bd79bce8SPatrick Williams                 messages::propertyValueNotInList(
316bd79bce8SPatrick Williams                     asyncResp->res, "null",
317bd79bce8SPatrick Williams                     "NTP/NTPServers/" + std::to_string(index));
318b9e15228SEd Tanous 
319287ece64SGeorge Liu                 return;
320287ece64SGeorge Liu             }
321b9e15228SEd Tanous             currentNtpServer = currentNtpServers.erase(currentNtpServer);
322b9e15228SEd Tanous             continue;
323b9e15228SEd Tanous         }
324b9e15228SEd Tanous         const nlohmann::json::object_t* ntpServerObject =
325ed4de7a8SEd Tanous             std::get_if<nlohmann::json::object_t>(&ntpServer);
326b9e15228SEd Tanous         if (ntpServerObject != nullptr)
327b9e15228SEd Tanous         {
328b9e15228SEd Tanous             if (!ntpServerObject->empty())
329b9e15228SEd Tanous             {
330ed4de7a8SEd Tanous                 messages::propertyValueNotInList(
331ed4de7a8SEd Tanous                     asyncResp->res, *ntpServerObject,
332ed4de7a8SEd Tanous                     "NTP/NTPServers/" + std::to_string(index));
333b9e15228SEd Tanous                 return;
334b9e15228SEd Tanous             }
335b9e15228SEd Tanous             // Can't retain an item that doesn't exist
336b9e15228SEd Tanous             if (currentNtpServer == currentNtpServers.end())
337b9e15228SEd Tanous             {
338ed4de7a8SEd Tanous                 messages::propertyValueOutOfRange(
339ed4de7a8SEd Tanous                     asyncResp->res, *ntpServerObject,
340ed4de7a8SEd Tanous                     "NTP/NTPServers/" + std::to_string(index));
341b9e15228SEd Tanous 
342b9e15228SEd Tanous                 return;
343b9e15228SEd Tanous             }
344b9e15228SEd Tanous             // empty objects should leave the NtpServer unmodified
345b9e15228SEd Tanous             currentNtpServer++;
346b9e15228SEd Tanous             continue;
347b9e15228SEd Tanous         }
348b9e15228SEd Tanous 
349ed4de7a8SEd Tanous         const std::string* ntpServerStr = std::get_if<std::string>(&ntpServer);
350b9e15228SEd Tanous         if (ntpServerStr == nullptr)
351b9e15228SEd Tanous         {
352ed4de7a8SEd Tanous             messages::internalError(asyncResp->res);
353b9e15228SEd Tanous             return;
354b9e15228SEd Tanous         }
355b9e15228SEd Tanous         if (currentNtpServer == currentNtpServers.end())
356b9e15228SEd Tanous         {
357b9e15228SEd Tanous             // if we're at the end of the list, append to the end
358b9e15228SEd Tanous             currentNtpServers.push_back(*ntpServerStr);
359b9e15228SEd Tanous             currentNtpServer = currentNtpServers.end();
360b9e15228SEd Tanous             continue;
361b9e15228SEd Tanous         }
362b9e15228SEd Tanous         *currentNtpServer = *ntpServerStr;
363b9e15228SEd Tanous         currentNtpServer++;
364b9e15228SEd Tanous     }
365b9e15228SEd Tanous 
366b9e15228SEd Tanous     // Any remaining array elements should be removed
367b9e15228SEd Tanous     currentNtpServers.erase(currentNtpServer, currentNtpServers.end());
368287ece64SGeorge Liu 
369e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> ethInterfaces = {
370e99073f5SGeorge Liu         "xyz.openbmc_project.Network.EthernetInterface"};
371e99073f5SGeorge Liu     dbus::utility::getSubTree(
372e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, ethInterfaces,
373b9e15228SEd Tanous         [asyncResp, currentNtpServers](
3742138483cSGeorge Liu             const boost::system::error_code& ec,
375b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
3760a052baaSGeorge Liu             if (ec)
3770a052baaSGeorge Liu             {
37862598e31SEd Tanous                 BMCWEB_LOG_WARNING("D-Bus error: {}, {}", ec, ec.message());
3790a052baaSGeorge Liu                 messages::internalError(asyncResp->res);
3800a052baaSGeorge Liu                 return;
3810a052baaSGeorge Liu             }
3820a052baaSGeorge Liu 
3830a052baaSGeorge Liu             for (const auto& [objectPath, serviceMap] : subtree)
3840a052baaSGeorge Liu             {
3850a052baaSGeorge Liu                 for (const auto& [service, interfaces] : serviceMap)
3860a052baaSGeorge Liu                 {
3870a052baaSGeorge Liu                     for (const auto& interface : interfaces)
3880a052baaSGeorge Liu                     {
3890a052baaSGeorge Liu                         if (interface !=
3900a052baaSGeorge Liu                             "xyz.openbmc_project.Network.EthernetInterface")
3910a052baaSGeorge Liu                         {
3920a052baaSGeorge Liu                             continue;
3930a052baaSGeorge Liu                         }
3940a052baaSGeorge Liu 
395e93abac6SGinu George                         setDbusProperty(asyncResp, "NTP/NTPServers/", service,
396bd79bce8SPatrick Williams                                         objectPath, interface,
397bd79bce8SPatrick Williams                                         "StaticNTPServers", currentNtpServers);
39820e6ea5dSraviteja-b                     }
3990a052baaSGeorge Liu                 }
4000a052baaSGeorge Liu             }
401e99073f5SGeorge Liu         });
4020a052baaSGeorge Liu }
40320e6ea5dSraviteja-b 
404504af5a0SPatrick Williams inline void handleProtocolEnabled(
405504af5a0SPatrick Williams     const bool protocolEnabled,
406e5a99777SAlbert Zhang     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
40769320d54SJiaqing Zhao     const std::string& netBasePath)
40867a78d87STom Joseph {
409e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
410e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Service.Attributes"};
411e99073f5SGeorge Liu     dbus::utility::getSubTree(
412e99073f5SGeorge Liu         "/xyz/openbmc_project/control/service", 0, interfaces,
413e5a99777SAlbert Zhang         [protocolEnabled, asyncResp,
4142138483cSGeorge Liu          netBasePath](const boost::system::error_code& ec,
415b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
41667a78d87STom Joseph             if (ec)
41767a78d87STom Joseph             {
41867a78d87STom Joseph                 messages::internalError(asyncResp->res);
41967a78d87STom Joseph                 return;
42067a78d87STom Joseph             }
42167a78d87STom Joseph 
42267a78d87STom Joseph             for (const auto& entry : subtree)
42367a78d87STom Joseph             {
42418f8f608SEd Tanous                 if (entry.first.starts_with(netBasePath))
42567a78d87STom Joseph                 {
42687c44966SAsmitha Karunanithi                     setDbusProperty(
427e93abac6SGinu George                         asyncResp, "IPMI/ProtocolEnabled",
428e93abac6SGinu George                         entry.second.begin()->first, entry.first,
429bd79bce8SPatrick Williams                         "xyz.openbmc_project.Control.Service.Attributes",
430bd79bce8SPatrick Williams                         "Running", protocolEnabled);
43187c44966SAsmitha Karunanithi                     setDbusProperty(
432e93abac6SGinu George                         asyncResp, "IPMI/ProtocolEnabled",
433e93abac6SGinu George                         entry.second.begin()->first, entry.first,
434bd79bce8SPatrick Williams                         "xyz.openbmc_project.Control.Service.Attributes",
435bd79bce8SPatrick Williams                         "Enabled", protocolEnabled);
43667a78d87STom Joseph                 }
43767a78d87STom Joseph             }
438e99073f5SGeorge Liu         });
43967a78d87STom Joseph }
44067a78d87STom Joseph 
441504af5a0SPatrick Williams inline void getNTPProtocolEnabled(
442504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
4437e860f15SJohn Edward Broadbent {
444deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
445deae6a78SEd Tanous         "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
446deae6a78SEd Tanous         "org.freedesktop.timedate1", "NTP",
4478e157735SEd Tanous         [asyncResp](const boost::system::error_code& ec, bool enabled) {
4488b24275dSEd Tanous             if (ec)
4497e860f15SJohn Edward Broadbent             {
4508e157735SEd Tanous                 BMCWEB_LOG_WARNING(
4518e157735SEd Tanous                     "Failed to get NTP status, assuming not supported");
4527e860f15SJohn Edward Broadbent                 return;
4537e860f15SJohn Edward Broadbent             }
4547e860f15SJohn Edward Broadbent 
4558e157735SEd Tanous             asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = enabled;
4561e1e598dSJonathan Doman         });
4577e860f15SJohn Edward Broadbent }
4587e860f15SJohn Edward Broadbent 
4595c3e9272SAbhishek Patel inline std::string encodeServiceObjectPath(std::string_view serviceName)
46069320d54SJiaqing Zhao {
46169320d54SJiaqing Zhao     sdbusplus::message::object_path objPath(
46269320d54SJiaqing Zhao         "/xyz/openbmc_project/control/service");
46369320d54SJiaqing Zhao     objPath /= serviceName;
46469320d54SJiaqing Zhao     return objPath.str;
46569320d54SJiaqing Zhao }
46669320d54SJiaqing Zhao 
4670f55d946SEd Tanous inline void handleBmcNetworkProtocolHead(
468e9f71672SEd Tanous     crow::App& app, const crow::Request& req,
469e9f71672SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
470e9f71672SEd Tanous {
471e9f71672SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
472e9f71672SEd Tanous     {
473e9f71672SEd Tanous         return;
474e9f71672SEd Tanous     }
475e9f71672SEd Tanous     asyncResp->res.addHeader(
476e9f71672SEd Tanous         boost::beast::http::field::link,
477e9f71672SEd Tanous         "</redfish/v1/JsonSchemas/ManagerNetworkProtocol/ManagerNetworkProtocol.json>; rel=describedby");
478e9f71672SEd Tanous }
479e9f71672SEd Tanous 
480e634b34cSEd Tanous inline void handleManagersNetworkProtocolPatch(
481e634b34cSEd Tanous     App& app, const crow::Request& req,
482253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
483253f11b8SEd Tanous     const std::string& managerId)
4847e860f15SJohn Edward Broadbent {
4853ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
48645ca1b86SEd Tanous     {
48745ca1b86SEd Tanous         return;
48845ca1b86SEd Tanous     }
489253f11b8SEd Tanous 
490253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
491253f11b8SEd Tanous     {
492253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
493253f11b8SEd Tanous         return;
494253f11b8SEd Tanous     }
495253f11b8SEd Tanous 
496501be32bSraviteja-b     std::optional<std::string> newHostName;
497ed4de7a8SEd Tanous 
498ed4de7a8SEd Tanous     std::optional<std::vector<IpAddress>> ntpServerObjects;
4995f4c798dSJiaqing Zhao     std::optional<bool> ntpEnabled;
5005f4c798dSJiaqing Zhao     std::optional<bool> ipmiEnabled;
5015f4c798dSJiaqing Zhao     std::optional<bool> sshEnabled;
502501be32bSraviteja-b 
5035f4c798dSJiaqing Zhao     if (!json_util::readJsonPatch(
504afc474aeSMyung Bae             req, asyncResp->res,                 //
505afc474aeSMyung Bae             "HostName", newHostName,             //
506afc474aeSMyung Bae             "NTP/NTPServers", ntpServerObjects,  //
507afc474aeSMyung Bae             "NTP/ProtocolEnabled", ntpEnabled,   //
508afc474aeSMyung Bae             "IPMI/ProtocolEnabled", ipmiEnabled, //
509afc474aeSMyung Bae             "SSH/ProtocolEnabled", sshEnabled    //
510afc474aeSMyung Bae             ))
511501be32bSraviteja-b     {
512501be32bSraviteja-b         return;
513501be32bSraviteja-b     }
514cf05f9dcSJohnathan Mantey 
5158d1b46d7Szhanghch05     asyncResp->res.result(boost::beast::http::status::no_content);
516501be32bSraviteja-b     if (newHostName)
517501be32bSraviteja-b     {
5182db77d34SJohnathan Mantey         messages::propertyNotWritable(asyncResp->res, "HostName");
51944fad2aaSEd Tanous         return;
520cf05f9dcSJohnathan Mantey     }
521cf05f9dcSJohnathan Mantey 
52220e6ea5dSraviteja-b     if (ntpEnabled)
52320e6ea5dSraviteja-b     {
5248e157735SEd Tanous         handleNTPProtocolEnabled(asyncResp, *ntpEnabled);
52520e6ea5dSraviteja-b     }
526b9e15228SEd Tanous     if (ntpServerObjects)
52720e6ea5dSraviteja-b     {
528b9e15228SEd Tanous         getEthernetIfaceData(
529b9e15228SEd Tanous             [asyncResp, ntpServerObjects](
530e634b34cSEd Tanous                 const bool success, std::vector<std::string>& currentNtpServers,
5310eebcefbSJishnu CM                 const std::vector<std::string>& /*dynamicNtpServers*/,
532b9e15228SEd Tanous                 const std::vector<std::string>& /*domainNames*/) {
533b9e15228SEd Tanous                 if (!success)
534b9e15228SEd Tanous                 {
535b9e15228SEd Tanous                     messages::internalError(asyncResp->res);
536b9e15228SEd Tanous                     return;
537b9e15228SEd Tanous                 }
538b9e15228SEd Tanous                 handleNTPServersPatch(asyncResp, *ntpServerObjects,
539b9e15228SEd Tanous                                       std::move(currentNtpServers));
540b9e15228SEd Tanous             });
54120e6ea5dSraviteja-b     }
54267a78d87STom Joseph 
5435f4c798dSJiaqing Zhao     if (ipmiEnabled)
54467a78d87STom Joseph     {
545e5a99777SAlbert Zhang         handleProtocolEnabled(
5465f4c798dSJiaqing Zhao             *ipmiEnabled, asyncResp,
54769320d54SJiaqing Zhao             encodeServiceObjectPath(std::string(ipmiServiceName) + '@'));
548e5a99777SAlbert Zhang     }
549e5a99777SAlbert Zhang 
5505f4c798dSJiaqing Zhao     if (sshEnabled)
551e5a99777SAlbert Zhang     {
55269320d54SJiaqing Zhao         handleProtocolEnabled(*sshEnabled, asyncResp,
55369320d54SJiaqing Zhao                               encodeServiceObjectPath(sshServiceName));
55467a78d87STom Joseph     }
555e634b34cSEd Tanous }
556e634b34cSEd Tanous 
557af20dd1cSEd Tanous inline void handleManagersNetworkProtocolHead(
558e634b34cSEd Tanous     App& app, const crow::Request& req,
559253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
560253f11b8SEd Tanous     const std::string& managerId)
561e634b34cSEd Tanous {
562e634b34cSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
563e634b34cSEd Tanous     {
564e634b34cSEd Tanous         return;
565e634b34cSEd Tanous     }
566af20dd1cSEd Tanous     asyncResp->res.addHeader(
567af20dd1cSEd Tanous         boost::beast::http::field::link,
568af20dd1cSEd Tanous         "</redfish/v1/JsonSchemas/ManagerNetworkProtocol/ManagerNetworkProtocol.json>; rel=describedby");
569253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
570253f11b8SEd Tanous     {
571253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
572253f11b8SEd Tanous         return;
573253f11b8SEd Tanous     }
574af20dd1cSEd Tanous }
575af20dd1cSEd Tanous 
576af20dd1cSEd Tanous inline void handleManagersNetworkProtocolGet(
577af20dd1cSEd Tanous     App& app, const crow::Request& req,
578253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
579253f11b8SEd Tanous     const std::string& managerId)
580af20dd1cSEd Tanous {
581253f11b8SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
582253f11b8SEd Tanous     {
583253f11b8SEd Tanous         return;
584253f11b8SEd Tanous     }
585253f11b8SEd Tanous     asyncResp->res.addHeader(
586253f11b8SEd Tanous         boost::beast::http::field::link,
587253f11b8SEd Tanous         "</redfish/v1/JsonSchemas/ManagerNetworkProtocol/ManagerNetworkProtocol.json>; rel=describedby");
588253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
589253f11b8SEd Tanous     {
590253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
591253f11b8SEd Tanous         return;
592253f11b8SEd Tanous     }
593253f11b8SEd Tanous 
594e634b34cSEd Tanous     getNetworkData(asyncResp, req);
595e634b34cSEd Tanous }
596e634b34cSEd Tanous 
597e634b34cSEd Tanous inline void requestRoutesNetworkProtocol(App& app)
598e634b34cSEd Tanous {
599253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/NetworkProtocol/")
600e634b34cSEd Tanous         .privileges(redfish::privileges::patchManagerNetworkProtocol)
601e634b34cSEd Tanous         .methods(boost::beast::http::verb::patch)(
602e634b34cSEd Tanous             std::bind_front(handleManagersNetworkProtocolPatch, std::ref(app)));
6037e860f15SJohn Edward Broadbent 
604253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/NetworkProtocol/")
605e9f71672SEd Tanous         .privileges(redfish::privileges::headManagerNetworkProtocol)
606e9f71672SEd Tanous         .methods(boost::beast::http::verb::head)(
607af20dd1cSEd Tanous             std::bind_front(handleManagersNetworkProtocolHead, std::ref(app)));
608e9f71672SEd Tanous 
609253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/NetworkProtocol/")
610ed398213SEd Tanous         .privileges(redfish::privileges::getManagerNetworkProtocol)
6117e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
612e634b34cSEd Tanous             std::bind_front(handleManagersNetworkProtocolGet, std::ref(app)));
613cf05f9dcSJohnathan Mantey }
61470141561SBorawski.Lukasz 
61570141561SBorawski.Lukasz } // namespace redfish
616