xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 532d769725d346d5339387016ce14d75d41ee3b2)
170141561SBorawski.Lukasz /*
270141561SBorawski.Lukasz // Copyright (c) 2018 Intel Corporation
370141561SBorawski.Lukasz //
470141561SBorawski.Lukasz // Licensed under the Apache License, Version 2.0 (the "License");
570141561SBorawski.Lukasz // you may not use this file except in compliance with the License.
670141561SBorawski.Lukasz // You may obtain a copy of the License at
770141561SBorawski.Lukasz //
870141561SBorawski.Lukasz //      http://www.apache.org/licenses/LICENSE-2.0
970141561SBorawski.Lukasz //
1070141561SBorawski.Lukasz // Unless required by applicable law or agreed to in writing, software
1170141561SBorawski.Lukasz // distributed under the License is distributed on an "AS IS" BASIS,
1270141561SBorawski.Lukasz // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1370141561SBorawski.Lukasz // See the License for the specific language governing permissions and
1470141561SBorawski.Lukasz // limitations under the License.
1570141561SBorawski.Lukasz */
1670141561SBorawski.Lukasz #pragma once
1770141561SBorawski.Lukasz 
183a8a0088SKowalski, Kamil #include "error_messages.hpp"
1967a78d87STom Joseph #include "openbmc_dbus_rest.hpp"
20b4bec66bSAbhishek Patel #include "redfish_util.hpp"
2170141561SBorawski.Lukasz 
227e860f15SJohn Edward Broadbent #include <app.hpp>
23168e20c1SEd Tanous #include <dbus_utility.hpp>
2445ca1b86SEd Tanous #include <query.hpp>
25ed398213SEd Tanous #include <registries/privilege_registry.hpp>
261e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
2720e6ea5dSraviteja-b #include <utils/json_utils.hpp>
28287ece64SGeorge Liu #include <utils/stl_utils.hpp>
291214b7e7SGunnar Mills 
301214b7e7SGunnar Mills #include <optional>
31abf2add6SEd Tanous #include <variant>
325f4c798dSJiaqing Zhao 
331abe55efSEd Tanous namespace redfish
341abe55efSEd Tanous {
3570141561SBorawski.Lukasz 
367e860f15SJohn Edward Broadbent void getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp);
377e860f15SJohn Edward Broadbent std::string getHostName();
387e860f15SJohn Edward Broadbent 
3969320d54SJiaqing Zhao static constexpr const char* sshServiceName = "dropbear";
4069320d54SJiaqing Zhao static constexpr const char* httpsServiceName = "bmcweb";
4169320d54SJiaqing Zhao static constexpr const char* ipmiServiceName = "phosphor-ipmi-net";
4269320d54SJiaqing Zhao static constexpr std::array<std::pair<const char*, const char*>, 3>
4369320d54SJiaqing Zhao     protocolToService = {{{"SSH", sshServiceName},
4469320d54SJiaqing Zhao                           {"HTTPS", httpsServiceName},
4569320d54SJiaqing Zhao                           {"IPMI", ipmiServiceName}}};
463a8a0088SKowalski, Kamil 
47711ac7a9SEd Tanous inline void extractNTPServersAndDomainNamesData(
48711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
49*532d7697SGunnar Mills     std::vector<std::string>& ntpData, std::vector<std::string>& dnData)
5020e6ea5dSraviteja-b {
5181ce609eSEd Tanous     for (const auto& obj : dbusData)
5220e6ea5dSraviteja-b     {
5320e6ea5dSraviteja-b         for (const auto& ifacePair : obj.second)
5420e6ea5dSraviteja-b         {
550a052baaSGeorge Liu             if (ifacePair.first !=
5620e6ea5dSraviteja-b                 "xyz.openbmc_project.Network.EthernetInterface")
5720e6ea5dSraviteja-b             {
580a052baaSGeorge Liu                 continue;
590a052baaSGeorge Liu             }
600a052baaSGeorge Liu 
6120e6ea5dSraviteja-b             for (const auto& propertyPair : ifacePair.second)
6220e6ea5dSraviteja-b             {
63*532d7697SGunnar Mills                 if (propertyPair.first == "NTPServers")
6420e6ea5dSraviteja-b                 {
6520e6ea5dSraviteja-b                     const std::vector<std::string>* ntpServers =
668d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
6720e6ea5dSraviteja-b                             &propertyPair.second);
6820e6ea5dSraviteja-b                     if (ntpServers != nullptr)
6920e6ea5dSraviteja-b                     {
70f23b7296SEd Tanous                         ntpData = *ntpServers;
7120e6ea5dSraviteja-b                     }
7220e6ea5dSraviteja-b                 }
73d24bfc7aSJennifer Lee                 else if (propertyPair.first == "DomainName")
74d24bfc7aSJennifer Lee                 {
75d24bfc7aSJennifer Lee                     const std::vector<std::string>* domainNames =
768d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
77d24bfc7aSJennifer Lee                             &propertyPair.second);
78d24bfc7aSJennifer Lee                     if (domainNames != nullptr)
79d24bfc7aSJennifer Lee                     {
80f23b7296SEd Tanous                         dnData = *domainNames;
81d24bfc7aSJennifer Lee                     }
82d24bfc7aSJennifer Lee                 }
8320e6ea5dSraviteja-b             }
8420e6ea5dSraviteja-b         }
8520e6ea5dSraviteja-b     }
860225b87bSEd Tanous     stl_utils::removeDuplicate(ntpData);
8720e6ea5dSraviteja-b }
8820e6ea5dSraviteja-b 
8920e6ea5dSraviteja-b template <typename CallbackFunc>
9020e6ea5dSraviteja-b void getEthernetIfaceData(CallbackFunc&& callback)
9120e6ea5dSraviteja-b {
9220e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
93f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
9481ce609eSEd Tanous             const boost::system::error_code errorCode,
95711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& dbusData) {
9620e6ea5dSraviteja-b         std::vector<std::string> ntpServers;
97d24bfc7aSJennifer Lee         std::vector<std::string> domainNames;
9820e6ea5dSraviteja-b 
9981ce609eSEd Tanous         if (errorCode)
10020e6ea5dSraviteja-b         {
101*532d7697SGunnar Mills             callback(false, ntpServers, domainNames);
10220e6ea5dSraviteja-b             return;
10320e6ea5dSraviteja-b         }
10420e6ea5dSraviteja-b 
105*532d7697SGunnar Mills         extractNTPServersAndDomainNamesData(dbusData, ntpServers, domainNames);
10620e6ea5dSraviteja-b 
107*532d7697SGunnar Mills         callback(true, ntpServers, domainNames);
10820e6ea5dSraviteja-b         },
10920e6ea5dSraviteja-b         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
11020e6ea5dSraviteja-b         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
111271584abSEd Tanous }
11220e6ea5dSraviteja-b 
1134f48d5f6SEd Tanous inline void getNetworkData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11472048780SAbhishek Patel                            const crow::Request& req)
1151abe55efSEd Tanous {
1160f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
117*532d7697SGunnar Mills         "#ManagerNetworkProtocol.v1_5_0.ManagerNetworkProtocol";
1180f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
1190f74e643SEd Tanous         "/redfish/v1/Managers/bmc/NetworkProtocol";
1200f74e643SEd Tanous     asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
1210f74e643SEd Tanous     asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
1220f74e643SEd Tanous     asyncResp->res.jsonValue["Description"] = "Manager Network Service";
1230f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1240f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
1250f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1260f74e643SEd Tanous 
12761932318SXiaochao Ma     // HTTP is Mandatory attribute as per OCP Baseline Profile - v1.0.0,
128818ea7b8SJoshi-Mansi     // but from security perspective it is not recommended to use.
129818ea7b8SJoshi-Mansi     // Hence using protocolEnabled as false to make it OCP and security-wise
130818ea7b8SJoshi-Mansi     // compliant
131818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["Port"] = 0;
132818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["ProtocolEnabled"] = false;
133818ea7b8SJoshi-Mansi 
134d24bfc7aSJennifer Lee     std::string hostName = getHostName();
135d24bfc7aSJennifer Lee 
136d24bfc7aSJennifer Lee     asyncResp->res.jsonValue["HostName"] = hostName;
1373a8a0088SKowalski, Kamil 
13820e6ea5dSraviteja-b     getNTPProtocolEnabled(asyncResp);
13920e6ea5dSraviteja-b 
140002d39b4SEd Tanous     getEthernetIfaceData(
141002d39b4SEd Tanous         [hostName, asyncResp](const bool& success,
14202cad96eSEd Tanous                               const std::vector<std::string>& ntpServers,
143d24bfc7aSJennifer Lee                               const std::vector<std::string>& domainNames) {
14420e6ea5dSraviteja-b         if (!success)
14520e6ea5dSraviteja-b         {
1460a052baaSGeorge Liu             messages::resourceNotFound(asyncResp->res, "ManagerNetworkProtocol",
1470a052baaSGeorge Liu                                        "NetworkProtocol");
14820e6ea5dSraviteja-b             return;
14920e6ea5dSraviteja-b         }
15020e6ea5dSraviteja-b         asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
15126f6976fSEd Tanous         if (!hostName.empty())
152d24bfc7aSJennifer Lee         {
153f23b7296SEd Tanous             std::string fqdn = hostName;
15426f6976fSEd Tanous             if (!domainNames.empty())
155d24bfc7aSJennifer Lee             {
156f23b7296SEd Tanous                 fqdn += ".";
157f23b7296SEd Tanous                 fqdn += domainNames[0];
158d24bfc7aSJennifer Lee             }
1592c70f800SEd Tanous             asyncResp->res.jsonValue["FQDN"] = std::move(fqdn);
160d24bfc7aSJennifer Lee         }
16120e6ea5dSraviteja-b     });
16220e6ea5dSraviteja-b 
16372048780SAbhishek Patel     Privileges effectiveUserPrivileges =
16472048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
16572048780SAbhishek Patel 
16672048780SAbhishek Patel     // /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates is
16772048780SAbhishek Patel     // something only ConfigureManager can access then only display when
16872048780SAbhishek Patel     // the user has permissions ConfigureManager
16972048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
17072048780SAbhishek Patel                                          effectiveUserPrivileges))
17172048780SAbhishek Patel     {
1721476687dSEd Tanous         asyncResp->res.jsonValue["HTTPS"]["Certificates"]["@odata.id"] =
1731476687dSEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates";
17470141561SBorawski.Lukasz     }
17570141561SBorawski.Lukasz 
17669320d54SJiaqing Zhao     for (const auto& protocol : protocolToService)
177ec4974ddSAppaRao Puli     {
178b4bec66bSAbhishek Patel         const std::string& protocolName = protocol.first;
179b4bec66bSAbhishek Patel         const std::string& serviceName = protocol.second;
180b4bec66bSAbhishek Patel         getPortStatusAndPath(
181b4bec66bSAbhishek Patel             serviceName,
182b4bec66bSAbhishek Patel             [asyncResp, protocolName](const boost::system::error_code ec,
183b4bec66bSAbhishek Patel                                       const std::string& socketPath,
184b4bec66bSAbhishek Patel                                       bool isProtocolEnabled) {
1854d875bd8SEd Tanous             // If the service is not installed, that is not an error
1864d875bd8SEd Tanous             if (ec == boost::system::errc::no_such_process)
1874d875bd8SEd Tanous             {
1884d875bd8SEd Tanous                 asyncResp->res.jsonValue[protocolName]["Port"] =
1894d875bd8SEd Tanous                     nlohmann::detail::value_t::null;
1904d875bd8SEd Tanous                 asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
1914d875bd8SEd Tanous                     false;
1924d875bd8SEd Tanous                 return;
1934d875bd8SEd Tanous             }
1941abe55efSEd Tanous             if (ec)
1951abe55efSEd Tanous             {
196a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
197865fbb75SEd Tanous                 return;
1983a8a0088SKowalski, Kamil             }
199b4bec66bSAbhishek Patel             asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
200b4bec66bSAbhishek Patel                 isProtocolEnabled;
201002d39b4SEd Tanous             getPortNumber(socketPath, [asyncResp, protocolName](
2028a592810SEd Tanous                                           const boost::system::error_code ec2,
203002d39b4SEd Tanous                                           int portNumber) {
2048a592810SEd Tanous                 if (ec2)
2051abe55efSEd Tanous                 {
206b4bec66bSAbhishek Patel                     messages::internalError(asyncResp->res);
207865fbb75SEd Tanous                     return;
20870141561SBorawski.Lukasz                 }
209002d39b4SEd Tanous                 asyncResp->res.jsonValue[protocolName]["Port"] = portNumber;
210b4bec66bSAbhishek Patel             });
211b4bec66bSAbhishek Patel             });
212865fbb75SEd Tanous     }
213b4bec66bSAbhishek Patel } // namespace redfish
214501be32bSraviteja-b 
2154f48d5f6SEd Tanous inline void handleNTPProtocolEnabled(
2167e860f15SJohn Edward Broadbent     const bool& ntpEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21720e6ea5dSraviteja-b {
21820e6ea5dSraviteja-b     std::string timeSyncMethod;
21920e6ea5dSraviteja-b     if (ntpEnabled)
22020e6ea5dSraviteja-b     {
2217e860f15SJohn Edward Broadbent         timeSyncMethod = "xyz.openbmc_project.Time.Synchronization.Method.NTP";
22220e6ea5dSraviteja-b     }
22320e6ea5dSraviteja-b     else
22420e6ea5dSraviteja-b     {
22520e6ea5dSraviteja-b         timeSyncMethod =
22620e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization.Method.Manual";
22720e6ea5dSraviteja-b     }
22820e6ea5dSraviteja-b 
22920e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
23081ce609eSEd Tanous         [asyncResp](const boost::system::error_code errorCode) {
23181ce609eSEd Tanous         if (errorCode)
232cb13a392SEd Tanous         {
233cb13a392SEd Tanous             messages::internalError(asyncResp->res);
234cb13a392SEd Tanous         }
235cb13a392SEd Tanous         },
2367e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Settings", "/xyz/openbmc_project/time/sync_method",
23720e6ea5dSraviteja-b         "org.freedesktop.DBus.Properties", "Set",
23820e6ea5dSraviteja-b         "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
239168e20c1SEd Tanous         dbus::utility::DbusVariantType{timeSyncMethod});
24020e6ea5dSraviteja-b }
24120e6ea5dSraviteja-b 
2424f48d5f6SEd Tanous inline void
243287ece64SGeorge Liu     handleNTPServersPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
244b9e15228SEd Tanous                           const std::vector<nlohmann::json>& ntpServerObjects,
245b9e15228SEd Tanous                           std::vector<std::string> currentNtpServers)
24620e6ea5dSraviteja-b {
247b9e15228SEd Tanous     std::vector<std::string>::iterator currentNtpServer =
248b9e15228SEd Tanous         currentNtpServers.begin();
249b9e15228SEd Tanous     for (size_t index = 0; index < ntpServerObjects.size(); index++)
250287ece64SGeorge Liu     {
251b9e15228SEd Tanous         const nlohmann::json& ntpServer = ntpServerObjects[index];
252b9e15228SEd Tanous         if (ntpServer.is_null())
253b9e15228SEd Tanous         {
254b9e15228SEd Tanous             // Can't delete an item that doesn't exist
255b9e15228SEd Tanous             if (currentNtpServer == currentNtpServers.end())
256b9e15228SEd Tanous             {
257b9e15228SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "null",
258b9e15228SEd Tanous                                                  "NTP/NTPServers/" +
259b9e15228SEd Tanous                                                      std::to_string(index));
260b9e15228SEd Tanous 
261287ece64SGeorge Liu                 return;
262287ece64SGeorge Liu             }
263b9e15228SEd Tanous             currentNtpServer = currentNtpServers.erase(currentNtpServer);
264b9e15228SEd Tanous             continue;
265b9e15228SEd Tanous         }
266b9e15228SEd Tanous         const nlohmann::json::object_t* ntpServerObject =
267b9e15228SEd Tanous             ntpServer.get_ptr<const nlohmann::json::object_t*>();
268b9e15228SEd Tanous         if (ntpServerObject != nullptr)
269b9e15228SEd Tanous         {
270b9e15228SEd Tanous             if (!ntpServerObject->empty())
271b9e15228SEd Tanous             {
272b9e15228SEd Tanous                 messages::propertyValueNotInList(
273b9e15228SEd Tanous                     asyncResp->res,
274b9e15228SEd Tanous                     ntpServer.dump(2, ' ', true,
275b9e15228SEd Tanous                                    nlohmann::json::error_handler_t::replace),
276b9e15228SEd Tanous                     "NTP/NTPServers/" + std::to_string(index));
277b9e15228SEd Tanous                 return;
278b9e15228SEd Tanous             }
279b9e15228SEd Tanous             // Can't retain an item that doesn't exist
280b9e15228SEd Tanous             if (currentNtpServer == currentNtpServers.end())
281b9e15228SEd Tanous             {
282b9e15228SEd Tanous                 messages::propertyValueOutOfRange(
283b9e15228SEd Tanous                     asyncResp->res,
284b9e15228SEd Tanous                     ntpServer.dump(2, ' ', true,
285b9e15228SEd Tanous                                    nlohmann::json::error_handler_t::replace),
286b9e15228SEd Tanous                     "NTP/NTPServers/" + std::to_string(index));
287b9e15228SEd Tanous 
288b9e15228SEd Tanous                 return;
289b9e15228SEd Tanous             }
290b9e15228SEd Tanous             // empty objects should leave the NtpServer unmodified
291b9e15228SEd Tanous             currentNtpServer++;
292b9e15228SEd Tanous             continue;
293b9e15228SEd Tanous         }
294b9e15228SEd Tanous 
295b9e15228SEd Tanous         const std::string* ntpServerStr =
296b9e15228SEd Tanous             ntpServer.get_ptr<const std::string*>();
297b9e15228SEd Tanous         if (ntpServerStr == nullptr)
298b9e15228SEd Tanous         {
299b9e15228SEd Tanous             messages::propertyValueTypeError(
300b9e15228SEd Tanous                 asyncResp->res,
301b9e15228SEd Tanous                 ntpServer.dump(2, ' ', true,
302b9e15228SEd Tanous                                nlohmann::json::error_handler_t::replace),
303b9e15228SEd Tanous                 "NTP/NTPServers/" + std::to_string(index));
304b9e15228SEd Tanous             return;
305b9e15228SEd Tanous         }
306b9e15228SEd Tanous         if (currentNtpServer == currentNtpServers.end())
307b9e15228SEd Tanous         {
308b9e15228SEd Tanous             // if we're at the end of the list, append to the end
309b9e15228SEd Tanous             currentNtpServers.push_back(*ntpServerStr);
310b9e15228SEd Tanous             currentNtpServer = currentNtpServers.end();
311b9e15228SEd Tanous             continue;
312b9e15228SEd Tanous         }
313b9e15228SEd Tanous         *currentNtpServer = *ntpServerStr;
314b9e15228SEd Tanous         currentNtpServer++;
315b9e15228SEd Tanous     }
316b9e15228SEd Tanous 
317b9e15228SEd Tanous     // Any remaining array elements should be removed
318b9e15228SEd Tanous     currentNtpServers.erase(currentNtpServer, currentNtpServers.end());
319287ece64SGeorge Liu 
32020e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
321b9e15228SEd Tanous         [asyncResp, currentNtpServers](
322b9e15228SEd Tanous             boost::system::error_code ec,
323b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
3240a052baaSGeorge Liu         if (ec)
3250a052baaSGeorge Liu         {
326002d39b4SEd Tanous             BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
3270a052baaSGeorge Liu             messages::internalError(asyncResp->res);
3280a052baaSGeorge Liu             return;
3290a052baaSGeorge Liu         }
3300a052baaSGeorge Liu 
3310a052baaSGeorge Liu         for (const auto& [objectPath, serviceMap] : subtree)
3320a052baaSGeorge Liu         {
3330a052baaSGeorge Liu             for (const auto& [service, interfaces] : serviceMap)
3340a052baaSGeorge Liu             {
3350a052baaSGeorge Liu                 for (const auto& interface : interfaces)
3360a052baaSGeorge Liu                 {
3370a052baaSGeorge Liu                     if (interface !=
3380a052baaSGeorge Liu                         "xyz.openbmc_project.Network.EthernetInterface")
3390a052baaSGeorge Liu                     {
3400a052baaSGeorge Liu                         continue;
3410a052baaSGeorge Liu                     }
3420a052baaSGeorge Liu 
3430a052baaSGeorge Liu                     crow::connections::systemBus->async_method_call(
3448a592810SEd Tanous                         [asyncResp](const boost::system::error_code ec2) {
3458a592810SEd Tanous                         if (ec2)
34620e6ea5dSraviteja-b                         {
34720e6ea5dSraviteja-b                             messages::internalError(asyncResp->res);
34820e6ea5dSraviteja-b                             return;
34920e6ea5dSraviteja-b                         }
35020e6ea5dSraviteja-b                         },
351002d39b4SEd Tanous                         service, objectPath, "org.freedesktop.DBus.Properties",
352*532d7697SGunnar Mills                         "Set", interface, "NTPServers",
353b9e15228SEd Tanous                         dbus::utility::DbusVariantType{currentNtpServers});
35420e6ea5dSraviteja-b                 }
3550a052baaSGeorge Liu             }
3560a052baaSGeorge Liu         }
3570a052baaSGeorge Liu         },
3580a052baaSGeorge Liu         "xyz.openbmc_project.ObjectMapper",
3590a052baaSGeorge Liu         "/xyz/openbmc_project/object_mapper",
3600a052baaSGeorge Liu         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3610a052baaSGeorge Liu         "/xyz/openbmc_project", 0,
3620a052baaSGeorge Liu         std::array<const char*, 1>{
3630a052baaSGeorge Liu             "xyz.openbmc_project.Network.EthernetInterface"});
3640a052baaSGeorge Liu }
36520e6ea5dSraviteja-b 
3664f48d5f6SEd Tanous inline void
3674f48d5f6SEd Tanous     handleProtocolEnabled(const bool protocolEnabled,
368e5a99777SAlbert Zhang                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36969320d54SJiaqing Zhao                           const std::string& netBasePath)
37067a78d87STom Joseph {
37167a78d87STom Joseph     crow::connections::systemBus->async_method_call(
372e5a99777SAlbert Zhang         [protocolEnabled, asyncResp,
373e5a99777SAlbert Zhang          netBasePath](const boost::system::error_code ec,
374b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
37567a78d87STom Joseph         if (ec)
37667a78d87STom Joseph         {
37767a78d87STom Joseph             messages::internalError(asyncResp->res);
37867a78d87STom Joseph             return;
37967a78d87STom Joseph         }
38067a78d87STom Joseph 
38167a78d87STom Joseph         for (const auto& entry : subtree)
38267a78d87STom Joseph         {
383e5a99777SAlbert Zhang             if (boost::algorithm::starts_with(entry.first, netBasePath))
38467a78d87STom Joseph             {
38567a78d87STom Joseph                 crow::connections::systemBus->async_method_call(
38623a21a1cSEd Tanous                     [asyncResp](const boost::system::error_code ec2) {
38723a21a1cSEd Tanous                     if (ec2)
38867a78d87STom Joseph                     {
38967a78d87STom Joseph                         messages::internalError(asyncResp->res);
39067a78d87STom Joseph                         return;
39167a78d87STom Joseph                     }
39267a78d87STom Joseph                     },
39367a78d87STom Joseph                     entry.second.begin()->first, entry.first,
39467a78d87STom Joseph                     "org.freedesktop.DBus.Properties", "Set",
395002d39b4SEd Tanous                     "xyz.openbmc_project.Control.Service.Attributes", "Running",
396168e20c1SEd Tanous                     dbus::utility::DbusVariantType{protocolEnabled});
39767a78d87STom Joseph 
39867a78d87STom Joseph                 crow::connections::systemBus->async_method_call(
39923a21a1cSEd Tanous                     [asyncResp](const boost::system::error_code ec2) {
40023a21a1cSEd Tanous                     if (ec2)
40167a78d87STom Joseph                     {
40267a78d87STom Joseph                         messages::internalError(asyncResp->res);
40367a78d87STom Joseph                         return;
40467a78d87STom Joseph                     }
40567a78d87STom Joseph                     },
40667a78d87STom Joseph                     entry.second.begin()->first, entry.first,
40767a78d87STom Joseph                     "org.freedesktop.DBus.Properties", "Set",
408002d39b4SEd Tanous                     "xyz.openbmc_project.Control.Service.Attributes", "Enabled",
409168e20c1SEd Tanous                     dbus::utility::DbusVariantType{protocolEnabled});
41067a78d87STom Joseph             }
41167a78d87STom Joseph         }
41267a78d87STom Joseph         },
41367a78d87STom Joseph         "xyz.openbmc_project.ObjectMapper",
41467a78d87STom Joseph         "/xyz/openbmc_project/object_mapper",
41567a78d87STom Joseph         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
41667a78d87STom Joseph         "/xyz/openbmc_project/control/service", 0,
41767a78d87STom Joseph         std::array<const char*, 1>{
41867a78d87STom Joseph             "xyz.openbmc_project.Control.Service.Attributes"});
41967a78d87STom Joseph }
42067a78d87STom Joseph 
4214f48d5f6SEd Tanous inline std::string getHostName()
422501be32bSraviteja-b {
4237e860f15SJohn Edward Broadbent     std::string hostName;
4248d1b46d7Szhanghch05 
425d3a9e084SEd Tanous     std::array<char, HOST_NAME_MAX> hostNameCStr{};
4267e860f15SJohn Edward Broadbent     if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
4277e860f15SJohn Edward Broadbent     {
4287e860f15SJohn Edward Broadbent         hostName = hostNameCStr.data();
4297e860f15SJohn Edward Broadbent     }
4307e860f15SJohn Edward Broadbent     return hostName;
4317e860f15SJohn Edward Broadbent }
4327e860f15SJohn Edward Broadbent 
4334f48d5f6SEd Tanous inline void
4344f48d5f6SEd Tanous     getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
4357e860f15SJohn Edward Broadbent {
4361e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
4371e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
4381e1e598dSJonathan Doman         "/xyz/openbmc_project/time/sync_method",
4391e1e598dSJonathan Doman         "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
4407e860f15SJohn Edward Broadbent         [asyncResp](const boost::system::error_code errorCode,
4411e1e598dSJonathan Doman                     const std::string& timeSyncMethod) {
4427e860f15SJohn Edward Broadbent         if (errorCode)
4437e860f15SJohn Edward Broadbent         {
4447e860f15SJohn Edward Broadbent             return;
4457e860f15SJohn Edward Broadbent         }
4467e860f15SJohn Edward Broadbent 
4471e1e598dSJonathan Doman         if (timeSyncMethod ==
4481e1e598dSJonathan Doman             "xyz.openbmc_project.Time.Synchronization.Method.NTP")
4497e860f15SJohn Edward Broadbent         {
4507e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = true;
4517e860f15SJohn Edward Broadbent         }
452002d39b4SEd Tanous         else if (timeSyncMethod == "xyz.openbmc_project.Time.Synchronization."
4531e1e598dSJonathan Doman                                    "Method.Manual")
4547e860f15SJohn Edward Broadbent         {
4557e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = false;
4567e860f15SJohn Edward Broadbent         }
4571e1e598dSJonathan Doman         });
4587e860f15SJohn Edward Broadbent }
4597e860f15SJohn Edward Broadbent 
46069320d54SJiaqing Zhao inline std::string encodeServiceObjectPath(const std::string& serviceName)
46169320d54SJiaqing Zhao {
46269320d54SJiaqing Zhao     sdbusplus::message::object_path objPath(
46369320d54SJiaqing Zhao         "/xyz/openbmc_project/control/service");
46469320d54SJiaqing Zhao     objPath /= serviceName;
46569320d54SJiaqing Zhao     return objPath.str;
46669320d54SJiaqing Zhao }
46769320d54SJiaqing Zhao 
4687e860f15SJohn Edward Broadbent inline void requestRoutesNetworkProtocol(App& app)
4697e860f15SJohn Edward Broadbent {
4707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
471ed398213SEd Tanous         .privileges(redfish::privileges::patchManagerNetworkProtocol)
472002d39b4SEd Tanous         .methods(boost::beast::http::verb::patch)(
473002d39b4SEd Tanous             [&app](const crow::Request& req,
474002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
4753ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
47645ca1b86SEd Tanous         {
47745ca1b86SEd Tanous             return;
47845ca1b86SEd Tanous         }
479501be32bSraviteja-b         std::optional<std::string> newHostName;
480b9e15228SEd Tanous         std::optional<std::vector<nlohmann::json>> ntpServerObjects;
4815f4c798dSJiaqing Zhao         std::optional<bool> ntpEnabled;
4825f4c798dSJiaqing Zhao         std::optional<bool> ipmiEnabled;
4835f4c798dSJiaqing Zhao         std::optional<bool> sshEnabled;
484501be32bSraviteja-b 
4855f4c798dSJiaqing Zhao         // clang-format off
4865f4c798dSJiaqing Zhao         if (!json_util::readJsonPatch(
4875f4c798dSJiaqing Zhao                 req, asyncResp->res,
4885f4c798dSJiaqing Zhao                 "HostName", newHostName,
489b9e15228SEd Tanous                 "NTP/NTPServers", ntpServerObjects,
4905f4c798dSJiaqing Zhao                 "NTP/ProtocolEnabled", ntpEnabled,
4915f4c798dSJiaqing Zhao                 "IPMI/ProtocolEnabled", ipmiEnabled,
4925f4c798dSJiaqing Zhao                 "SSH/ProtocolEnabled", sshEnabled))
493501be32bSraviteja-b         {
494501be32bSraviteja-b             return;
495501be32bSraviteja-b         }
4965f4c798dSJiaqing Zhao         // clang-format on
497cf05f9dcSJohnathan Mantey 
4988d1b46d7Szhanghch05         asyncResp->res.result(boost::beast::http::status::no_content);
499501be32bSraviteja-b         if (newHostName)
500501be32bSraviteja-b         {
5012db77d34SJohnathan Mantey             messages::propertyNotWritable(asyncResp->res, "HostName");
50244fad2aaSEd Tanous             return;
503cf05f9dcSJohnathan Mantey         }
504cf05f9dcSJohnathan Mantey 
50520e6ea5dSraviteja-b         if (ntpEnabled)
50620e6ea5dSraviteja-b         {
50720e6ea5dSraviteja-b             handleNTPProtocolEnabled(*ntpEnabled, asyncResp);
50820e6ea5dSraviteja-b         }
509b9e15228SEd Tanous         if (ntpServerObjects)
51020e6ea5dSraviteja-b         {
511b9e15228SEd Tanous             getEthernetIfaceData(
512b9e15228SEd Tanous                 [asyncResp, ntpServerObjects](
513b9e15228SEd Tanous                     const bool success,
514b9e15228SEd Tanous                     std::vector<std::string>& currentNtpServers,
515b9e15228SEd Tanous                     const std::vector<std::string>& /*domainNames*/) {
516b9e15228SEd Tanous                 if (!success)
517b9e15228SEd Tanous                 {
518b9e15228SEd Tanous                     messages::internalError(asyncResp->res);
519b9e15228SEd Tanous                     return;
520b9e15228SEd Tanous                 }
521b9e15228SEd Tanous                 handleNTPServersPatch(asyncResp, *ntpServerObjects,
522b9e15228SEd Tanous                                       std::move(currentNtpServers));
523b9e15228SEd Tanous             });
52420e6ea5dSraviteja-b         }
52567a78d87STom Joseph 
5265f4c798dSJiaqing Zhao         if (ipmiEnabled)
52767a78d87STom Joseph         {
528e5a99777SAlbert Zhang             handleProtocolEnabled(
5295f4c798dSJiaqing Zhao                 *ipmiEnabled, asyncResp,
53069320d54SJiaqing Zhao                 encodeServiceObjectPath(std::string(ipmiServiceName) + '@'));
531e5a99777SAlbert Zhang         }
532e5a99777SAlbert Zhang 
5335f4c798dSJiaqing Zhao         if (sshEnabled)
534e5a99777SAlbert Zhang         {
53569320d54SJiaqing Zhao             handleProtocolEnabled(*sshEnabled, asyncResp,
53669320d54SJiaqing Zhao                                   encodeServiceObjectPath(sshServiceName));
53767a78d87STom Joseph         }
5387e860f15SJohn Edward Broadbent         });
5397e860f15SJohn Edward Broadbent 
5407e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
541ed398213SEd Tanous         .privileges(redfish::privileges::getManagerNetworkProtocol)
5427e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
54345ca1b86SEd Tanous             [&app](const crow::Request& req,
5447e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
5453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
54645ca1b86SEd Tanous         {
54745ca1b86SEd Tanous             return;
54845ca1b86SEd Tanous         }
54972048780SAbhishek Patel         getNetworkData(asyncResp, req);
5507e860f15SJohn Edward Broadbent         });
551cf05f9dcSJohnathan Mantey }
55270141561SBorawski.Lukasz 
55370141561SBorawski.Lukasz } // namespace redfish
554