xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 5f4c798d0a1e0827363e167e54f4fb1dbffe9efa)
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>
32*5f4c798dSJiaqing 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 
39b4bec66bSAbhishek Patel const static std::array<std::pair<std::string, std::string>, 3> protocolToDBus{
40b0972a63SEd Tanous     {{"SSH", "dropbear"}, {"HTTPS", "bmcweb"}, {"IPMI", "phosphor-ipmi-net"}}};
413a8a0088SKowalski, Kamil 
42711ac7a9SEd Tanous inline void extractNTPServersAndDomainNamesData(
43711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
44711ac7a9SEd Tanous     std::vector<std::string>& ntpData, std::vector<std::string>& dnData)
4520e6ea5dSraviteja-b {
4681ce609eSEd Tanous     for (const auto& obj : dbusData)
4720e6ea5dSraviteja-b     {
4820e6ea5dSraviteja-b         for (const auto& ifacePair : obj.second)
4920e6ea5dSraviteja-b         {
500a052baaSGeorge Liu             if (ifacePair.first !=
5120e6ea5dSraviteja-b                 "xyz.openbmc_project.Network.EthernetInterface")
5220e6ea5dSraviteja-b             {
530a052baaSGeorge Liu                 continue;
540a052baaSGeorge Liu             }
550a052baaSGeorge Liu 
5620e6ea5dSraviteja-b             for (const auto& propertyPair : ifacePair.second)
5720e6ea5dSraviteja-b             {
5820e6ea5dSraviteja-b                 if (propertyPair.first == "NTPServers")
5920e6ea5dSraviteja-b                 {
6020e6ea5dSraviteja-b                     const std::vector<std::string>* ntpServers =
618d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
6220e6ea5dSraviteja-b                             &propertyPair.second);
6320e6ea5dSraviteja-b                     if (ntpServers != nullptr)
6420e6ea5dSraviteja-b                     {
65f23b7296SEd Tanous                         ntpData = *ntpServers;
6620e6ea5dSraviteja-b                     }
6720e6ea5dSraviteja-b                 }
68d24bfc7aSJennifer Lee                 else if (propertyPair.first == "DomainName")
69d24bfc7aSJennifer Lee                 {
70d24bfc7aSJennifer Lee                     const std::vector<std::string>* domainNames =
718d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
72d24bfc7aSJennifer Lee                             &propertyPair.second);
73d24bfc7aSJennifer Lee                     if (domainNames != nullptr)
74d24bfc7aSJennifer Lee                     {
75f23b7296SEd Tanous                         dnData = *domainNames;
76d24bfc7aSJennifer Lee                     }
77d24bfc7aSJennifer Lee                 }
7820e6ea5dSraviteja-b             }
7920e6ea5dSraviteja-b         }
8020e6ea5dSraviteja-b     }
8120e6ea5dSraviteja-b }
8220e6ea5dSraviteja-b 
8320e6ea5dSraviteja-b template <typename CallbackFunc>
8420e6ea5dSraviteja-b void getEthernetIfaceData(CallbackFunc&& callback)
8520e6ea5dSraviteja-b {
8620e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
87f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
8881ce609eSEd Tanous             const boost::system::error_code errorCode,
89711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& dbusData) {
9020e6ea5dSraviteja-b             std::vector<std::string> ntpServers;
91d24bfc7aSJennifer Lee             std::vector<std::string> domainNames;
9220e6ea5dSraviteja-b 
9381ce609eSEd Tanous             if (errorCode)
9420e6ea5dSraviteja-b             {
95d24bfc7aSJennifer Lee                 callback(false, ntpServers, domainNames);
9620e6ea5dSraviteja-b                 return;
9720e6ea5dSraviteja-b             }
9820e6ea5dSraviteja-b 
9981ce609eSEd Tanous             extractNTPServersAndDomainNamesData(dbusData, ntpServers,
100d24bfc7aSJennifer Lee                                                 domainNames);
10120e6ea5dSraviteja-b 
102d24bfc7aSJennifer Lee             callback(true, ntpServers, domainNames);
10320e6ea5dSraviteja-b         },
10420e6ea5dSraviteja-b         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
10520e6ea5dSraviteja-b         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
106271584abSEd Tanous }
10720e6ea5dSraviteja-b 
1084f48d5f6SEd Tanous inline void getNetworkData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10972048780SAbhishek Patel                            const crow::Request& req)
1101abe55efSEd Tanous {
1110f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
11261932318SXiaochao Ma         "#ManagerNetworkProtocol.v1_5_0.ManagerNetworkProtocol";
1130f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
1140f74e643SEd Tanous         "/redfish/v1/Managers/bmc/NetworkProtocol";
1150f74e643SEd Tanous     asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
1160f74e643SEd Tanous     asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
1170f74e643SEd Tanous     asyncResp->res.jsonValue["Description"] = "Manager Network Service";
1180f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1190f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
1200f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1210f74e643SEd Tanous 
12261932318SXiaochao Ma     // HTTP is Mandatory attribute as per OCP Baseline Profile - v1.0.0,
123818ea7b8SJoshi-Mansi     // but from security perspective it is not recommended to use.
124818ea7b8SJoshi-Mansi     // Hence using protocolEnabled as false to make it OCP and security-wise
125818ea7b8SJoshi-Mansi     // compliant
126818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["Port"] = 0;
127818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["ProtocolEnabled"] = false;
128818ea7b8SJoshi-Mansi 
129d24bfc7aSJennifer Lee     std::string hostName = getHostName();
130d24bfc7aSJennifer Lee 
131d24bfc7aSJennifer Lee     asyncResp->res.jsonValue["HostName"] = hostName;
1323a8a0088SKowalski, Kamil 
13320e6ea5dSraviteja-b     getNTPProtocolEnabled(asyncResp);
13420e6ea5dSraviteja-b 
1350a052baaSGeorge Liu     getEthernetIfaceData([hostName, asyncResp](
1360a052baaSGeorge Liu                              const bool& success,
137927c17cdSRadivoje Jovanovic                              std::vector<std::string>& ntpServers,
138d24bfc7aSJennifer Lee                              const std::vector<std::string>& domainNames) {
13920e6ea5dSraviteja-b         if (!success)
14020e6ea5dSraviteja-b         {
1410a052baaSGeorge Liu             messages::resourceNotFound(asyncResp->res, "ManagerNetworkProtocol",
1420a052baaSGeorge Liu                                        "NetworkProtocol");
14320e6ea5dSraviteja-b             return;
14420e6ea5dSraviteja-b         }
145927c17cdSRadivoje Jovanovic         stl_utils::removeDuplicate(ntpServers);
14620e6ea5dSraviteja-b         asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
14726f6976fSEd Tanous         if (!hostName.empty())
148d24bfc7aSJennifer Lee         {
149f23b7296SEd Tanous             std::string fqdn = hostName;
15026f6976fSEd Tanous             if (!domainNames.empty())
151d24bfc7aSJennifer Lee             {
152f23b7296SEd Tanous                 fqdn += ".";
153f23b7296SEd Tanous                 fqdn += domainNames[0];
154d24bfc7aSJennifer Lee             }
1552c70f800SEd Tanous             asyncResp->res.jsonValue["FQDN"] = std::move(fqdn);
156d24bfc7aSJennifer Lee         }
15720e6ea5dSraviteja-b     });
15820e6ea5dSraviteja-b 
15972048780SAbhishek Patel     Privileges effectiveUserPrivileges =
16072048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
16172048780SAbhishek Patel 
16272048780SAbhishek Patel     // /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates is
16372048780SAbhishek Patel     // something only ConfigureManager can access then only display when
16472048780SAbhishek Patel     // the user has permissions ConfigureManager
16572048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
16672048780SAbhishek Patel                                          effectiveUserPrivileges))
16772048780SAbhishek Patel     {
1685968caeeSMarri Devender Rao         asyncResp->res.jsonValue["HTTPS"]["Certificates"] = {
169b4bec66bSAbhishek Patel             {"@odata.id",
170b4bec66bSAbhishek Patel              "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates"}};
17170141561SBorawski.Lukasz     }
17270141561SBorawski.Lukasz 
173b4bec66bSAbhishek Patel     for (const auto& protocol : protocolToDBus)
174ec4974ddSAppaRao Puli     {
175b4bec66bSAbhishek Patel         const std::string& protocolName = protocol.first;
176b4bec66bSAbhishek Patel         const std::string& serviceName = protocol.second;
177b4bec66bSAbhishek Patel         getPortStatusAndPath(
178b4bec66bSAbhishek Patel             serviceName,
179b4bec66bSAbhishek Patel             [asyncResp, protocolName](const boost::system::error_code ec,
180b4bec66bSAbhishek Patel                                       const std::string& socketPath,
181b4bec66bSAbhishek Patel                                       bool isProtocolEnabled) {
1824d875bd8SEd Tanous                 // If the service is not installed, that is not an error
1834d875bd8SEd Tanous                 if (ec == boost::system::errc::no_such_process)
1844d875bd8SEd Tanous                 {
1854d875bd8SEd Tanous                     asyncResp->res.jsonValue[protocolName]["Port"] =
1864d875bd8SEd Tanous                         nlohmann::detail::value_t::null;
1874d875bd8SEd Tanous                     asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
1884d875bd8SEd Tanous                         false;
1894d875bd8SEd Tanous                     return;
1904d875bd8SEd Tanous                 }
1911abe55efSEd Tanous                 if (ec)
1921abe55efSEd Tanous                 {
193a08b46ccSJason M. Bills                     messages::internalError(asyncResp->res);
194865fbb75SEd Tanous                     return;
1953a8a0088SKowalski, Kamil                 }
196b4bec66bSAbhishek Patel                 asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
197b4bec66bSAbhishek Patel                     isProtocolEnabled;
198b4bec66bSAbhishek Patel                 getPortNumber(
199b4bec66bSAbhishek Patel                     socketPath,
200b4bec66bSAbhishek Patel                     [asyncResp, protocolName](
201b4bec66bSAbhishek Patel                         const boost::system::error_code ec, int portNumber) {
202b4bec66bSAbhishek Patel                         if (ec)
2031abe55efSEd Tanous                         {
204b4bec66bSAbhishek Patel                             messages::internalError(asyncResp->res);
205865fbb75SEd Tanous                             return;
20670141561SBorawski.Lukasz                         }
207b4bec66bSAbhishek Patel                         asyncResp->res.jsonValue[protocolName]["Port"] =
208b4bec66bSAbhishek Patel                             portNumber;
209b4bec66bSAbhishek Patel                     });
210b4bec66bSAbhishek Patel             });
211865fbb75SEd Tanous     }
212b4bec66bSAbhishek Patel } // namespace redfish
213501be32bSraviteja-b 
2144f48d5f6SEd Tanous inline void handleNTPProtocolEnabled(
2157e860f15SJohn Edward Broadbent     const bool& ntpEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21620e6ea5dSraviteja-b {
21720e6ea5dSraviteja-b     std::string timeSyncMethod;
21820e6ea5dSraviteja-b     if (ntpEnabled)
21920e6ea5dSraviteja-b     {
2207e860f15SJohn Edward Broadbent         timeSyncMethod = "xyz.openbmc_project.Time.Synchronization.Method.NTP";
22120e6ea5dSraviteja-b     }
22220e6ea5dSraviteja-b     else
22320e6ea5dSraviteja-b     {
22420e6ea5dSraviteja-b         timeSyncMethod =
22520e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization.Method.Manual";
22620e6ea5dSraviteja-b     }
22720e6ea5dSraviteja-b 
22820e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
22981ce609eSEd Tanous         [asyncResp](const boost::system::error_code errorCode) {
23081ce609eSEd Tanous             if (errorCode)
231cb13a392SEd Tanous             {
232cb13a392SEd Tanous                 messages::internalError(asyncResp->res);
233cb13a392SEd Tanous             }
234cb13a392SEd Tanous         },
2357e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Settings", "/xyz/openbmc_project/time/sync_method",
23620e6ea5dSraviteja-b         "org.freedesktop.DBus.Properties", "Set",
23720e6ea5dSraviteja-b         "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
238168e20c1SEd Tanous         dbus::utility::DbusVariantType{timeSyncMethod});
23920e6ea5dSraviteja-b }
24020e6ea5dSraviteja-b 
2414f48d5f6SEd Tanous inline void
242287ece64SGeorge Liu     handleNTPServersPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
243287ece64SGeorge Liu                           std::vector<std::string>& ntpServers)
24420e6ea5dSraviteja-b {
245287ece64SGeorge Liu     auto iter = stl_utils::firstDuplicate(ntpServers.begin(), ntpServers.end());
246287ece64SGeorge Liu     if (iter != ntpServers.end())
247287ece64SGeorge Liu     {
248287ece64SGeorge Liu         std::string pointer =
249287ece64SGeorge Liu             "NTPServers/" +
250287ece64SGeorge Liu             std::to_string(std::distance(ntpServers.begin(), iter));
251287ece64SGeorge Liu         messages::propertyValueIncorrect(asyncResp->res, pointer, *iter);
252287ece64SGeorge Liu         return;
253287ece64SGeorge Liu     }
254287ece64SGeorge Liu 
25520e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
2560a052baaSGeorge Liu         [asyncResp,
2570a052baaSGeorge Liu          ntpServers](boost::system::error_code ec,
258b9d36b47SEd Tanous                      const dbus::utility::MapperGetSubTreeResponse& subtree) {
2590a052baaSGeorge Liu             if (ec)
2600a052baaSGeorge Liu             {
2610a052baaSGeorge Liu                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
2620a052baaSGeorge Liu                                    << ec.message();
2630a052baaSGeorge Liu                 messages::internalError(asyncResp->res);
2640a052baaSGeorge Liu                 return;
2650a052baaSGeorge Liu             }
2660a052baaSGeorge Liu 
2670a052baaSGeorge Liu             for (const auto& [objectPath, serviceMap] : subtree)
2680a052baaSGeorge Liu             {
2690a052baaSGeorge Liu                 for (const auto& [service, interfaces] : serviceMap)
2700a052baaSGeorge Liu                 {
2710a052baaSGeorge Liu                     for (const auto& interface : interfaces)
2720a052baaSGeorge Liu                     {
2730a052baaSGeorge Liu                         if (interface !=
2740a052baaSGeorge Liu                             "xyz.openbmc_project.Network.EthernetInterface")
2750a052baaSGeorge Liu                         {
2760a052baaSGeorge Liu                             continue;
2770a052baaSGeorge Liu                         }
2780a052baaSGeorge Liu 
2790a052baaSGeorge Liu                         crow::connections::systemBus->async_method_call(
280cf05f9dcSJohnathan Mantey                             [asyncResp](const boost::system::error_code ec) {
28120e6ea5dSraviteja-b                                 if (ec)
28220e6ea5dSraviteja-b                                 {
28320e6ea5dSraviteja-b                                     messages::internalError(asyncResp->res);
28420e6ea5dSraviteja-b                                     return;
28520e6ea5dSraviteja-b                                 }
28620e6ea5dSraviteja-b                             },
2870a052baaSGeorge Liu                             service, objectPath,
2880a052baaSGeorge Liu                             "org.freedesktop.DBus.Properties", "Set", interface,
2890a052baaSGeorge Liu                             "NTPServers",
290168e20c1SEd Tanous                             dbus::utility::DbusVariantType{ntpServers});
29120e6ea5dSraviteja-b                     }
2920a052baaSGeorge Liu                 }
2930a052baaSGeorge Liu             }
2940a052baaSGeorge Liu         },
2950a052baaSGeorge Liu         "xyz.openbmc_project.ObjectMapper",
2960a052baaSGeorge Liu         "/xyz/openbmc_project/object_mapper",
2970a052baaSGeorge Liu         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2980a052baaSGeorge Liu         "/xyz/openbmc_project", 0,
2990a052baaSGeorge Liu         std::array<const char*, 1>{
3000a052baaSGeorge Liu             "xyz.openbmc_project.Network.EthernetInterface"});
3010a052baaSGeorge Liu }
30220e6ea5dSraviteja-b 
3034f48d5f6SEd Tanous inline void
3044f48d5f6SEd Tanous     handleProtocolEnabled(const bool protocolEnabled,
305e5a99777SAlbert Zhang                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
306e5a99777SAlbert Zhang                           const std::string_view netBasePath)
30767a78d87STom Joseph {
30867a78d87STom Joseph     crow::connections::systemBus->async_method_call(
309e5a99777SAlbert Zhang         [protocolEnabled, asyncResp,
310e5a99777SAlbert Zhang          netBasePath](const boost::system::error_code ec,
311b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
31267a78d87STom Joseph             if (ec)
31367a78d87STom Joseph             {
31467a78d87STom Joseph                 messages::internalError(asyncResp->res);
31567a78d87STom Joseph                 return;
31667a78d87STom Joseph             }
31767a78d87STom Joseph 
31867a78d87STom Joseph             for (const auto& entry : subtree)
31967a78d87STom Joseph             {
320e5a99777SAlbert Zhang                 if (boost::algorithm::starts_with(entry.first, netBasePath))
32167a78d87STom Joseph                 {
32267a78d87STom Joseph                     crow::connections::systemBus->async_method_call(
32323a21a1cSEd Tanous                         [asyncResp](const boost::system::error_code ec2) {
32423a21a1cSEd Tanous                             if (ec2)
32567a78d87STom Joseph                             {
32667a78d87STom Joseph                                 messages::internalError(asyncResp->res);
32767a78d87STom Joseph                                 return;
32867a78d87STom Joseph                             }
32967a78d87STom Joseph                         },
33067a78d87STom Joseph                         entry.second.begin()->first, entry.first,
33167a78d87STom Joseph                         "org.freedesktop.DBus.Properties", "Set",
33267a78d87STom Joseph                         "xyz.openbmc_project.Control.Service.Attributes",
333168e20c1SEd Tanous                         "Running",
334168e20c1SEd Tanous                         dbus::utility::DbusVariantType{protocolEnabled});
33567a78d87STom Joseph 
33667a78d87STom Joseph                     crow::connections::systemBus->async_method_call(
33723a21a1cSEd Tanous                         [asyncResp](const boost::system::error_code ec2) {
33823a21a1cSEd Tanous                             if (ec2)
33967a78d87STom Joseph                             {
34067a78d87STom Joseph                                 messages::internalError(asyncResp->res);
34167a78d87STom Joseph                                 return;
34267a78d87STom Joseph                             }
34367a78d87STom Joseph                         },
34467a78d87STom Joseph                         entry.second.begin()->first, entry.first,
34567a78d87STom Joseph                         "org.freedesktop.DBus.Properties", "Set",
34667a78d87STom Joseph                         "xyz.openbmc_project.Control.Service.Attributes",
347168e20c1SEd Tanous                         "Enabled",
348168e20c1SEd Tanous                         dbus::utility::DbusVariantType{protocolEnabled});
34967a78d87STom Joseph                 }
35067a78d87STom Joseph             }
35167a78d87STom Joseph         },
35267a78d87STom Joseph         "xyz.openbmc_project.ObjectMapper",
35367a78d87STom Joseph         "/xyz/openbmc_project/object_mapper",
35467a78d87STom Joseph         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
35567a78d87STom Joseph         "/xyz/openbmc_project/control/service", 0,
35667a78d87STom Joseph         std::array<const char*, 1>{
35767a78d87STom Joseph             "xyz.openbmc_project.Control.Service.Attributes"});
35867a78d87STom Joseph }
35967a78d87STom Joseph 
3604f48d5f6SEd Tanous inline std::string getHostName()
361501be32bSraviteja-b {
3627e860f15SJohn Edward Broadbent     std::string hostName;
3638d1b46d7Szhanghch05 
364d3a9e084SEd Tanous     std::array<char, HOST_NAME_MAX> hostNameCStr{};
3657e860f15SJohn Edward Broadbent     if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
3667e860f15SJohn Edward Broadbent     {
3677e860f15SJohn Edward Broadbent         hostName = hostNameCStr.data();
3687e860f15SJohn Edward Broadbent     }
3697e860f15SJohn Edward Broadbent     return hostName;
3707e860f15SJohn Edward Broadbent }
3717e860f15SJohn Edward Broadbent 
3724f48d5f6SEd Tanous inline void
3734f48d5f6SEd Tanous     getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3747e860f15SJohn Edward Broadbent {
3751e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
3761e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
3771e1e598dSJonathan Doman         "/xyz/openbmc_project/time/sync_method",
3781e1e598dSJonathan Doman         "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
3797e860f15SJohn Edward Broadbent         [asyncResp](const boost::system::error_code errorCode,
3801e1e598dSJonathan Doman                     const std::string& timeSyncMethod) {
3817e860f15SJohn Edward Broadbent             if (errorCode)
3827e860f15SJohn Edward Broadbent             {
3837e860f15SJohn Edward Broadbent                 return;
3847e860f15SJohn Edward Broadbent             }
3857e860f15SJohn Edward Broadbent 
3861e1e598dSJonathan Doman             if (timeSyncMethod ==
3871e1e598dSJonathan Doman                 "xyz.openbmc_project.Time.Synchronization.Method.NTP")
3887e860f15SJohn Edward Broadbent             {
3897e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = true;
3907e860f15SJohn Edward Broadbent             }
3911e1e598dSJonathan Doman             else if (timeSyncMethod ==
3921e1e598dSJonathan Doman                      "xyz.openbmc_project.Time.Synchronization."
3931e1e598dSJonathan Doman                      "Method.Manual")
3947e860f15SJohn Edward Broadbent             {
3957e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = false;
3967e860f15SJohn Edward Broadbent             }
3971e1e598dSJonathan Doman         });
3987e860f15SJohn Edward Broadbent }
3997e860f15SJohn Edward Broadbent 
4007e860f15SJohn Edward Broadbent inline void requestRoutesNetworkProtocol(App& app)
4017e860f15SJohn Edward Broadbent {
4027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
403ed398213SEd Tanous         .privileges(redfish::privileges::patchManagerNetworkProtocol)
4040fda0f12SGeorge Liu         .methods(
40545ca1b86SEd Tanous             boost::beast::http::verb::patch)([&app](const crow::Request& req,
40645ca1b86SEd Tanous                                                     const std::shared_ptr<
40745ca1b86SEd Tanous                                                         bmcweb::AsyncResp>&
40845ca1b86SEd Tanous                                                         asyncResp) {
40945ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
41045ca1b86SEd Tanous             {
41145ca1b86SEd Tanous                 return;
41245ca1b86SEd Tanous             }
413501be32bSraviteja-b             std::optional<std::string> newHostName;
414*5f4c798dSJiaqing Zhao             std::optional<std::vector<std::string>> ntpServers;
415*5f4c798dSJiaqing Zhao             std::optional<bool> ntpEnabled;
416*5f4c798dSJiaqing Zhao             std::optional<bool> ipmiEnabled;
417*5f4c798dSJiaqing Zhao             std::optional<bool> sshEnabled;
418501be32bSraviteja-b 
419*5f4c798dSJiaqing Zhao             // clang-format off
420*5f4c798dSJiaqing Zhao             if (!json_util::readJsonPatch(
421*5f4c798dSJiaqing Zhao                     req, asyncResp->res,
422*5f4c798dSJiaqing Zhao                     "HostName", newHostName,
423*5f4c798dSJiaqing Zhao                     "NTP/NTPServers", ntpServers,
424*5f4c798dSJiaqing Zhao                     "NTP/ProtocolEnabled", ntpEnabled,
425*5f4c798dSJiaqing Zhao                     "IPMI/ProtocolEnabled", ipmiEnabled,
426*5f4c798dSJiaqing Zhao                     "SSH/ProtocolEnabled", sshEnabled))
427501be32bSraviteja-b             {
428501be32bSraviteja-b                 return;
429501be32bSraviteja-b             }
430*5f4c798dSJiaqing Zhao             // clang-format on
431cf05f9dcSJohnathan Mantey 
4328d1b46d7Szhanghch05             asyncResp->res.result(boost::beast::http::status::no_content);
433501be32bSraviteja-b             if (newHostName)
434501be32bSraviteja-b             {
4352db77d34SJohnathan Mantey                 messages::propertyNotWritable(asyncResp->res, "HostName");
43644fad2aaSEd Tanous                 return;
437cf05f9dcSJohnathan Mantey             }
438cf05f9dcSJohnathan Mantey 
43920e6ea5dSraviteja-b             if (ntpEnabled)
44020e6ea5dSraviteja-b             {
44120e6ea5dSraviteja-b                 handleNTPProtocolEnabled(*ntpEnabled, asyncResp);
44220e6ea5dSraviteja-b             }
44320e6ea5dSraviteja-b             if (ntpServers)
44420e6ea5dSraviteja-b             {
445287ece64SGeorge Liu                 stl_utils::removeDuplicate(*ntpServers);
446287ece64SGeorge Liu                 handleNTPServersPatch(asyncResp, *ntpServers);
44720e6ea5dSraviteja-b             }
44867a78d87STom Joseph 
449*5f4c798dSJiaqing Zhao             if (ipmiEnabled)
45067a78d87STom Joseph             {
451e5a99777SAlbert Zhang                 handleProtocolEnabled(
452*5f4c798dSJiaqing Zhao                     *ipmiEnabled, asyncResp,
4530fda0f12SGeorge Liu                     "/xyz/openbmc_project/control/service/phosphor_2dipmi_2dnet_40");
454e5a99777SAlbert Zhang             }
455e5a99777SAlbert Zhang 
456*5f4c798dSJiaqing Zhao             if (sshEnabled)
457e5a99777SAlbert Zhang             {
458e5a99777SAlbert Zhang                 handleProtocolEnabled(
459*5f4c798dSJiaqing Zhao                     *sshEnabled, asyncResp,
460e5a99777SAlbert Zhang                     "/xyz/openbmc_project/control/service/dropbear");
46167a78d87STom Joseph             }
4627e860f15SJohn Edward Broadbent         });
4637e860f15SJohn Edward Broadbent 
4647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
465ed398213SEd Tanous         .privileges(redfish::privileges::getManagerNetworkProtocol)
4667e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
46745ca1b86SEd Tanous             [&app](const crow::Request& req,
4687e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
46945ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
47045ca1b86SEd Tanous                 {
47145ca1b86SEd Tanous                     return;
47245ca1b86SEd Tanous                 }
47372048780SAbhishek Patel                 getNetworkData(asyncResp, req);
4747e860f15SJohn Edward Broadbent             });
475cf05f9dcSJohnathan Mantey }
47670141561SBorawski.Lukasz 
47770141561SBorawski.Lukasz } // namespace redfish
478