xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 0a052baa566b3697419789dbf78934d43efc4ac4)
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>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
2420e6ea5dSraviteja-b #include <utils/json_utils.hpp>
25287ece64SGeorge Liu #include <utils/stl_utils.hpp>
261214b7e7SGunnar Mills 
271214b7e7SGunnar Mills #include <optional>
28abf2add6SEd Tanous #include <variant>
291abe55efSEd Tanous namespace redfish
301abe55efSEd Tanous {
3170141561SBorawski.Lukasz 
327e860f15SJohn Edward Broadbent void getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp);
337e860f15SJohn Edward Broadbent std::string getHostName();
347e860f15SJohn Edward Broadbent 
35b4bec66bSAbhishek Patel const static std::array<std::pair<std::string, std::string>, 3> protocolToDBus{
36b0972a63SEd Tanous     {{"SSH", "dropbear"}, {"HTTPS", "bmcweb"}, {"IPMI", "phosphor-ipmi-net"}}};
373a8a0088SKowalski, Kamil 
38d24bfc7aSJennifer Lee inline void
3981ce609eSEd Tanous     extractNTPServersAndDomainNamesData(const GetManagedObjects& dbusData,
40d24bfc7aSJennifer Lee                                         std::vector<std::string>& ntpData,
41d24bfc7aSJennifer Lee                                         std::vector<std::string>& dnData)
4220e6ea5dSraviteja-b {
4381ce609eSEd Tanous     for (const auto& obj : dbusData)
4420e6ea5dSraviteja-b     {
4520e6ea5dSraviteja-b         for (const auto& ifacePair : obj.second)
4620e6ea5dSraviteja-b         {
47*0a052baaSGeorge Liu             if (ifacePair.first !=
4820e6ea5dSraviteja-b                 "xyz.openbmc_project.Network.EthernetInterface")
4920e6ea5dSraviteja-b             {
50*0a052baaSGeorge Liu                 continue;
51*0a052baaSGeorge Liu             }
52*0a052baaSGeorge Liu 
5320e6ea5dSraviteja-b             for (const auto& propertyPair : ifacePair.second)
5420e6ea5dSraviteja-b             {
5520e6ea5dSraviteja-b                 if (propertyPair.first == "NTPServers")
5620e6ea5dSraviteja-b                 {
5720e6ea5dSraviteja-b                     const std::vector<std::string>* ntpServers =
588d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
5920e6ea5dSraviteja-b                             &propertyPair.second);
6020e6ea5dSraviteja-b                     if (ntpServers != nullptr)
6120e6ea5dSraviteja-b                     {
62f23b7296SEd Tanous                         ntpData = *ntpServers;
6320e6ea5dSraviteja-b                     }
6420e6ea5dSraviteja-b                 }
65d24bfc7aSJennifer Lee                 else if (propertyPair.first == "DomainName")
66d24bfc7aSJennifer Lee                 {
67d24bfc7aSJennifer Lee                     const std::vector<std::string>* domainNames =
688d78b7a9SPatrick Williams                         std::get_if<std::vector<std::string>>(
69d24bfc7aSJennifer Lee                             &propertyPair.second);
70d24bfc7aSJennifer Lee                     if (domainNames != nullptr)
71d24bfc7aSJennifer Lee                     {
72f23b7296SEd Tanous                         dnData = *domainNames;
73d24bfc7aSJennifer Lee                     }
74d24bfc7aSJennifer Lee                 }
7520e6ea5dSraviteja-b             }
7620e6ea5dSraviteja-b         }
7720e6ea5dSraviteja-b     }
7820e6ea5dSraviteja-b }
7920e6ea5dSraviteja-b 
8020e6ea5dSraviteja-b template <typename CallbackFunc>
8120e6ea5dSraviteja-b void getEthernetIfaceData(CallbackFunc&& callback)
8220e6ea5dSraviteja-b {
8320e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
8420e6ea5dSraviteja-b         [callback{std::move(callback)}](
8581ce609eSEd Tanous             const boost::system::error_code errorCode,
8681ce609eSEd Tanous             const GetManagedObjects& dbusData) {
8720e6ea5dSraviteja-b             std::vector<std::string> ntpServers;
88d24bfc7aSJennifer Lee             std::vector<std::string> domainNames;
8920e6ea5dSraviteja-b 
9081ce609eSEd Tanous             if (errorCode)
9120e6ea5dSraviteja-b             {
92d24bfc7aSJennifer Lee                 callback(false, ntpServers, domainNames);
9320e6ea5dSraviteja-b                 return;
9420e6ea5dSraviteja-b             }
9520e6ea5dSraviteja-b 
9681ce609eSEd Tanous             extractNTPServersAndDomainNamesData(dbusData, ntpServers,
97d24bfc7aSJennifer Lee                                                 domainNames);
9820e6ea5dSraviteja-b 
99d24bfc7aSJennifer Lee             callback(true, ntpServers, domainNames);
10020e6ea5dSraviteja-b         },
10120e6ea5dSraviteja-b         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
10220e6ea5dSraviteja-b         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
103271584abSEd Tanous }
10420e6ea5dSraviteja-b 
1054f48d5f6SEd Tanous inline void getNetworkData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10672048780SAbhishek Patel                            const crow::Request& req)
1071abe55efSEd Tanous {
1080f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
10961932318SXiaochao Ma         "#ManagerNetworkProtocol.v1_5_0.ManagerNetworkProtocol";
1100f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
1110f74e643SEd Tanous         "/redfish/v1/Managers/bmc/NetworkProtocol";
1120f74e643SEd Tanous     asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
1130f74e643SEd Tanous     asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
1140f74e643SEd Tanous     asyncResp->res.jsonValue["Description"] = "Manager Network Service";
1150f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1160f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
1170f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1180f74e643SEd Tanous 
11961932318SXiaochao Ma     // HTTP is Mandatory attribute as per OCP Baseline Profile - v1.0.0,
120818ea7b8SJoshi-Mansi     // but from security perspective it is not recommended to use.
121818ea7b8SJoshi-Mansi     // Hence using protocolEnabled as false to make it OCP and security-wise
122818ea7b8SJoshi-Mansi     // compliant
123818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["Port"] = 0;
124818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["ProtocolEnabled"] = false;
125818ea7b8SJoshi-Mansi 
126d24bfc7aSJennifer Lee     std::string hostName = getHostName();
127d24bfc7aSJennifer Lee 
128d24bfc7aSJennifer Lee     asyncResp->res.jsonValue["HostName"] = hostName;
1293a8a0088SKowalski, Kamil 
13020e6ea5dSraviteja-b     getNTPProtocolEnabled(asyncResp);
13120e6ea5dSraviteja-b 
132*0a052baaSGeorge Liu     getEthernetIfaceData([hostName, asyncResp](
133*0a052baaSGeorge Liu                              const bool& success,
134d24bfc7aSJennifer Lee                              const std::vector<std::string>& ntpServers,
135d24bfc7aSJennifer Lee                              const std::vector<std::string>& domainNames) {
13620e6ea5dSraviteja-b         if (!success)
13720e6ea5dSraviteja-b         {
138*0a052baaSGeorge Liu             messages::resourceNotFound(asyncResp->res, "ManagerNetworkProtocol",
139*0a052baaSGeorge Liu                                        "NetworkProtocol");
14020e6ea5dSraviteja-b             return;
14120e6ea5dSraviteja-b         }
14220e6ea5dSraviteja-b         asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
143d24bfc7aSJennifer Lee         if (hostName.empty() == false)
144d24bfc7aSJennifer Lee         {
145f23b7296SEd Tanous             std::string fqdn = hostName;
146d24bfc7aSJennifer Lee             if (domainNames.empty() == false)
147d24bfc7aSJennifer Lee             {
148f23b7296SEd Tanous                 fqdn += ".";
149f23b7296SEd Tanous                 fqdn += domainNames[0];
150d24bfc7aSJennifer Lee             }
1512c70f800SEd Tanous             asyncResp->res.jsonValue["FQDN"] = std::move(fqdn);
152d24bfc7aSJennifer Lee         }
15320e6ea5dSraviteja-b     });
15420e6ea5dSraviteja-b 
15572048780SAbhishek Patel     Privileges effectiveUserPrivileges =
15672048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
15772048780SAbhishek Patel 
15872048780SAbhishek Patel     // /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates is
15972048780SAbhishek Patel     // something only ConfigureManager can access then only display when
16072048780SAbhishek Patel     // the user has permissions ConfigureManager
16172048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
16272048780SAbhishek Patel                                          effectiveUserPrivileges))
16372048780SAbhishek Patel     {
1645968caeeSMarri Devender Rao         asyncResp->res.jsonValue["HTTPS"]["Certificates"] = {
165b4bec66bSAbhishek Patel             {"@odata.id",
166b4bec66bSAbhishek Patel              "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates"}};
16770141561SBorawski.Lukasz     }
16870141561SBorawski.Lukasz 
169b4bec66bSAbhishek Patel     for (const auto& protocol : protocolToDBus)
170ec4974ddSAppaRao Puli     {
171b4bec66bSAbhishek Patel         const std::string& protocolName = protocol.first;
172b4bec66bSAbhishek Patel         const std::string& serviceName = protocol.second;
173b4bec66bSAbhishek Patel         getPortStatusAndPath(
174b4bec66bSAbhishek Patel             serviceName,
175b4bec66bSAbhishek Patel             [asyncResp, protocolName](const boost::system::error_code ec,
176b4bec66bSAbhishek Patel                                       const std::string& socketPath,
177b4bec66bSAbhishek Patel                                       bool isProtocolEnabled) {
1784d875bd8SEd Tanous                 // If the service is not installed, that is not an error
1794d875bd8SEd Tanous                 if (ec == boost::system::errc::no_such_process)
1804d875bd8SEd Tanous                 {
1814d875bd8SEd Tanous                     asyncResp->res.jsonValue[protocolName]["Port"] =
1824d875bd8SEd Tanous                         nlohmann::detail::value_t::null;
1834d875bd8SEd Tanous                     asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
1844d875bd8SEd Tanous                         false;
1854d875bd8SEd Tanous                     return;
1864d875bd8SEd Tanous                 }
1871abe55efSEd Tanous                 if (ec)
1881abe55efSEd Tanous                 {
189a08b46ccSJason M. Bills                     messages::internalError(asyncResp->res);
190865fbb75SEd Tanous                     return;
1913a8a0088SKowalski, Kamil                 }
192b4bec66bSAbhishek Patel                 asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
193b4bec66bSAbhishek Patel                     isProtocolEnabled;
194b4bec66bSAbhishek Patel                 getPortNumber(
195b4bec66bSAbhishek Patel                     socketPath,
196b4bec66bSAbhishek Patel                     [asyncResp, protocolName](
197b4bec66bSAbhishek Patel                         const boost::system::error_code ec, int portNumber) {
198b4bec66bSAbhishek Patel                         if (ec)
1991abe55efSEd Tanous                         {
200b4bec66bSAbhishek Patel                             messages::internalError(asyncResp->res);
201865fbb75SEd Tanous                             return;
20270141561SBorawski.Lukasz                         }
203b4bec66bSAbhishek Patel                         asyncResp->res.jsonValue[protocolName]["Port"] =
204b4bec66bSAbhishek Patel                             portNumber;
205b4bec66bSAbhishek Patel                     });
206b4bec66bSAbhishek Patel             });
207865fbb75SEd Tanous     }
208b4bec66bSAbhishek Patel } // namespace redfish
209501be32bSraviteja-b 
2104f48d5f6SEd Tanous inline void handleNTPProtocolEnabled(
2117e860f15SJohn Edward Broadbent     const bool& ntpEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21220e6ea5dSraviteja-b {
21320e6ea5dSraviteja-b     std::string timeSyncMethod;
21420e6ea5dSraviteja-b     if (ntpEnabled)
21520e6ea5dSraviteja-b     {
2167e860f15SJohn Edward Broadbent         timeSyncMethod = "xyz.openbmc_project.Time.Synchronization.Method.NTP";
21720e6ea5dSraviteja-b     }
21820e6ea5dSraviteja-b     else
21920e6ea5dSraviteja-b     {
22020e6ea5dSraviteja-b         timeSyncMethod =
22120e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization.Method.Manual";
22220e6ea5dSraviteja-b     }
22320e6ea5dSraviteja-b 
22420e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
22581ce609eSEd Tanous         [asyncResp](const boost::system::error_code errorCode) {
22681ce609eSEd Tanous             if (errorCode)
227cb13a392SEd Tanous             {
228cb13a392SEd Tanous                 messages::internalError(asyncResp->res);
229cb13a392SEd Tanous             }
230cb13a392SEd Tanous         },
2317e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Settings", "/xyz/openbmc_project/time/sync_method",
23220e6ea5dSraviteja-b         "org.freedesktop.DBus.Properties", "Set",
23320e6ea5dSraviteja-b         "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
23420e6ea5dSraviteja-b         std::variant<std::string>{timeSyncMethod});
23520e6ea5dSraviteja-b }
23620e6ea5dSraviteja-b 
2374f48d5f6SEd Tanous inline void
238287ece64SGeorge Liu     handleNTPServersPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
239287ece64SGeorge Liu                           std::vector<std::string>& ntpServers)
24020e6ea5dSraviteja-b {
241287ece64SGeorge Liu     auto iter = stl_utils::firstDuplicate(ntpServers.begin(), ntpServers.end());
242287ece64SGeorge Liu     if (iter != ntpServers.end())
243287ece64SGeorge Liu     {
244287ece64SGeorge Liu         std::string pointer =
245287ece64SGeorge Liu             "NTPServers/" +
246287ece64SGeorge Liu             std::to_string(std::distance(ntpServers.begin(), iter));
247287ece64SGeorge Liu         messages::propertyValueIncorrect(asyncResp->res, pointer, *iter);
248287ece64SGeorge Liu         return;
249287ece64SGeorge Liu     }
250287ece64SGeorge Liu 
25120e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
252*0a052baaSGeorge Liu         [asyncResp,
253*0a052baaSGeorge Liu          ntpServers](boost::system::error_code ec,
254*0a052baaSGeorge Liu                      const crow::openbmc_mapper::GetSubTreeType& subtree) {
255*0a052baaSGeorge Liu             if (ec)
256*0a052baaSGeorge Liu             {
257*0a052baaSGeorge Liu                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
258*0a052baaSGeorge Liu                                    << ec.message();
259*0a052baaSGeorge Liu                 messages::internalError(asyncResp->res);
260*0a052baaSGeorge Liu                 return;
261*0a052baaSGeorge Liu             }
262*0a052baaSGeorge Liu 
263*0a052baaSGeorge Liu             for (const auto& [objectPath, serviceMap] : subtree)
264*0a052baaSGeorge Liu             {
265*0a052baaSGeorge Liu                 for (const auto& [service, interfaces] : serviceMap)
266*0a052baaSGeorge Liu                 {
267*0a052baaSGeorge Liu                     for (const auto& interface : interfaces)
268*0a052baaSGeorge Liu                     {
269*0a052baaSGeorge Liu                         if (interface !=
270*0a052baaSGeorge Liu                             "xyz.openbmc_project.Network.EthernetInterface")
271*0a052baaSGeorge Liu                         {
272*0a052baaSGeorge Liu                             continue;
273*0a052baaSGeorge Liu                         }
274*0a052baaSGeorge Liu 
275*0a052baaSGeorge Liu                         crow::connections::systemBus->async_method_call(
276cf05f9dcSJohnathan Mantey                             [asyncResp](const boost::system::error_code ec) {
27720e6ea5dSraviteja-b                                 if (ec)
27820e6ea5dSraviteja-b                                 {
27920e6ea5dSraviteja-b                                     messages::internalError(asyncResp->res);
28020e6ea5dSraviteja-b                                     return;
28120e6ea5dSraviteja-b                                 }
28220e6ea5dSraviteja-b                             },
283*0a052baaSGeorge Liu                             service, objectPath,
284*0a052baaSGeorge Liu                             "org.freedesktop.DBus.Properties", "Set", interface,
285*0a052baaSGeorge Liu                             "NTPServers",
28620e6ea5dSraviteja-b                             std::variant<std::vector<std::string>>{ntpServers});
28720e6ea5dSraviteja-b                     }
288*0a052baaSGeorge Liu                 }
289*0a052baaSGeorge Liu             }
290*0a052baaSGeorge Liu         },
291*0a052baaSGeorge Liu         "xyz.openbmc_project.ObjectMapper",
292*0a052baaSGeorge Liu         "/xyz/openbmc_project/object_mapper",
293*0a052baaSGeorge Liu         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
294*0a052baaSGeorge Liu         "/xyz/openbmc_project", 0,
295*0a052baaSGeorge Liu         std::array<const char*, 1>{
296*0a052baaSGeorge Liu             "xyz.openbmc_project.Network.EthernetInterface"});
297*0a052baaSGeorge Liu }
29820e6ea5dSraviteja-b 
2994f48d5f6SEd Tanous inline void
3004f48d5f6SEd Tanous     handleProtocolEnabled(const bool protocolEnabled,
301e5a99777SAlbert Zhang                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
302e5a99777SAlbert Zhang                           const std::string_view netBasePath)
30367a78d87STom Joseph {
30467a78d87STom Joseph     crow::connections::systemBus->async_method_call(
305e5a99777SAlbert Zhang         [protocolEnabled, asyncResp,
306e5a99777SAlbert Zhang          netBasePath](const boost::system::error_code ec,
30767a78d87STom Joseph                       const crow::openbmc_mapper::GetSubTreeType& subtree) {
30867a78d87STom Joseph             if (ec)
30967a78d87STom Joseph             {
31067a78d87STom Joseph                 messages::internalError(asyncResp->res);
31167a78d87STom Joseph                 return;
31267a78d87STom Joseph             }
31367a78d87STom Joseph 
31467a78d87STom Joseph             for (const auto& entry : subtree)
31567a78d87STom Joseph             {
316e5a99777SAlbert Zhang                 if (boost::algorithm::starts_with(entry.first, netBasePath))
31767a78d87STom Joseph                 {
31867a78d87STom Joseph                     crow::connections::systemBus->async_method_call(
31923a21a1cSEd Tanous                         [asyncResp](const boost::system::error_code ec2) {
32023a21a1cSEd Tanous                             if (ec2)
32167a78d87STom Joseph                             {
32267a78d87STom Joseph                                 messages::internalError(asyncResp->res);
32367a78d87STom Joseph                                 return;
32467a78d87STom Joseph                             }
32567a78d87STom Joseph                         },
32667a78d87STom Joseph                         entry.second.begin()->first, entry.first,
32767a78d87STom Joseph                         "org.freedesktop.DBus.Properties", "Set",
32867a78d87STom Joseph                         "xyz.openbmc_project.Control.Service.Attributes",
329e5a99777SAlbert Zhang                         "Running", std::variant<bool>{protocolEnabled});
33067a78d87STom Joseph 
33167a78d87STom Joseph                     crow::connections::systemBus->async_method_call(
33223a21a1cSEd Tanous                         [asyncResp](const boost::system::error_code ec2) {
33323a21a1cSEd Tanous                             if (ec2)
33467a78d87STom Joseph                             {
33567a78d87STom Joseph                                 messages::internalError(asyncResp->res);
33667a78d87STom Joseph                                 return;
33767a78d87STom Joseph                             }
33867a78d87STom Joseph                         },
33967a78d87STom Joseph                         entry.second.begin()->first, entry.first,
34067a78d87STom Joseph                         "org.freedesktop.DBus.Properties", "Set",
34167a78d87STom Joseph                         "xyz.openbmc_project.Control.Service.Attributes",
342e5a99777SAlbert Zhang                         "Enabled", std::variant<bool>{protocolEnabled});
34367a78d87STom Joseph                 }
34467a78d87STom Joseph             }
34567a78d87STom Joseph         },
34667a78d87STom Joseph         "xyz.openbmc_project.ObjectMapper",
34767a78d87STom Joseph         "/xyz/openbmc_project/object_mapper",
34867a78d87STom Joseph         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
34967a78d87STom Joseph         "/xyz/openbmc_project/control/service", 0,
35067a78d87STom Joseph         std::array<const char*, 1>{
35167a78d87STom Joseph             "xyz.openbmc_project.Control.Service.Attributes"});
35267a78d87STom Joseph }
35367a78d87STom Joseph 
3544f48d5f6SEd Tanous inline std::string getHostName()
355501be32bSraviteja-b {
3567e860f15SJohn Edward Broadbent     std::string hostName;
3578d1b46d7Szhanghch05 
3587e860f15SJohn Edward Broadbent     std::array<char, HOST_NAME_MAX> hostNameCStr;
3597e860f15SJohn Edward Broadbent     if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
3607e860f15SJohn Edward Broadbent     {
3617e860f15SJohn Edward Broadbent         hostName = hostNameCStr.data();
3627e860f15SJohn Edward Broadbent     }
3637e860f15SJohn Edward Broadbent     return hostName;
3647e860f15SJohn Edward Broadbent }
3657e860f15SJohn Edward Broadbent 
3664f48d5f6SEd Tanous inline void
3674f48d5f6SEd Tanous     getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3687e860f15SJohn Edward Broadbent {
3697e860f15SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
3707e860f15SJohn Edward Broadbent         [asyncResp](const boost::system::error_code errorCode,
3717e860f15SJohn Edward Broadbent                     const std::variant<std::string>& timeSyncMethod) {
3727e860f15SJohn Edward Broadbent             if (errorCode)
3737e860f15SJohn Edward Broadbent             {
3747e860f15SJohn Edward Broadbent                 return;
3757e860f15SJohn Edward Broadbent             }
3767e860f15SJohn Edward Broadbent 
3777e860f15SJohn Edward Broadbent             const std::string* s = std::get_if<std::string>(&timeSyncMethod);
3787e860f15SJohn Edward Broadbent 
3797e860f15SJohn Edward Broadbent             if (*s == "xyz.openbmc_project.Time.Synchronization.Method.NTP")
3807e860f15SJohn Edward Broadbent             {
3817e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = true;
3827e860f15SJohn Edward Broadbent             }
3837e860f15SJohn Edward Broadbent             else if (*s == "xyz.openbmc_project.Time.Synchronization."
3847e860f15SJohn Edward Broadbent                            "Method.Manual")
3857e860f15SJohn Edward Broadbent             {
3867e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = false;
3877e860f15SJohn Edward Broadbent             }
3887e860f15SJohn Edward Broadbent         },
3897e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Settings", "/xyz/openbmc_project/time/sync_method",
3907e860f15SJohn Edward Broadbent         "org.freedesktop.DBus.Properties", "Get",
3917e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod");
3927e860f15SJohn Edward Broadbent }
3937e860f15SJohn Edward Broadbent 
3947e860f15SJohn Edward Broadbent inline void requestRoutesNetworkProtocol(App& app)
3957e860f15SJohn Edward Broadbent {
3967e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
397ed398213SEd Tanous         .privileges(redfish::privileges::patchManagerNetworkProtocol)
3987e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
3997e860f15SJohn Edward Broadbent             [](const crow::Request& req,
4007e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
401501be32bSraviteja-b                 std::optional<std::string> newHostName;
402cf05f9dcSJohnathan Mantey                 std::optional<nlohmann::json> ntp;
40367a78d87STom Joseph                 std::optional<nlohmann::json> ipmi;
404e5a99777SAlbert Zhang                 std::optional<nlohmann::json> ssh;
405501be32bSraviteja-b 
4067e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res, "NTP", ntp,
407e5a99777SAlbert Zhang                                          "HostName", newHostName, "IPMI", ipmi,
408e5a99777SAlbert Zhang                                          "SSH", ssh))
409501be32bSraviteja-b                 {
410501be32bSraviteja-b                     return;
411501be32bSraviteja-b                 }
412cf05f9dcSJohnathan Mantey 
4138d1b46d7Szhanghch05                 asyncResp->res.result(boost::beast::http::status::no_content);
414501be32bSraviteja-b                 if (newHostName)
415501be32bSraviteja-b                 {
4162db77d34SJohnathan Mantey                     messages::propertyNotWritable(asyncResp->res, "HostName");
41744fad2aaSEd Tanous                     return;
418cf05f9dcSJohnathan Mantey                 }
419cf05f9dcSJohnathan Mantey 
420cf05f9dcSJohnathan Mantey                 if (ntp)
421cf05f9dcSJohnathan Mantey                 {
422cf05f9dcSJohnathan Mantey                     std::optional<std::vector<std::string>> ntpServers;
423cf05f9dcSJohnathan Mantey                     std::optional<bool> ntpEnabled;
4248d1b46d7Szhanghch05                     if (!json_util::readJson(*ntp, asyncResp->res, "NTPServers",
4257e860f15SJohn Edward Broadbent                                              ntpServers, "ProtocolEnabled",
4267e860f15SJohn Edward Broadbent                                              ntpEnabled))
427cf05f9dcSJohnathan Mantey                     {
428501be32bSraviteja-b                         return;
429501be32bSraviteja-b                     }
430cf05f9dcSJohnathan Mantey 
43120e6ea5dSraviteja-b                     if (ntpEnabled)
43220e6ea5dSraviteja-b                     {
43320e6ea5dSraviteja-b                         handleNTPProtocolEnabled(*ntpEnabled, asyncResp);
43420e6ea5dSraviteja-b                     }
435cf05f9dcSJohnathan Mantey 
43620e6ea5dSraviteja-b                     if (ntpServers)
43720e6ea5dSraviteja-b                     {
438287ece64SGeorge Liu                         stl_utils::removeDuplicate(*ntpServers);
439287ece64SGeorge Liu                         handleNTPServersPatch(asyncResp, *ntpServers);
44020e6ea5dSraviteja-b                     }
441501be32bSraviteja-b                 }
44267a78d87STom Joseph 
44367a78d87STom Joseph                 if (ipmi)
44467a78d87STom Joseph                 {
44567a78d87STom Joseph                     std::optional<bool> ipmiProtocolEnabled;
4467e860f15SJohn Edward Broadbent                     if (!json_util::readJson(*ipmi, asyncResp->res,
4477e860f15SJohn Edward Broadbent                                              "ProtocolEnabled",
44867a78d87STom Joseph                                              ipmiProtocolEnabled))
44967a78d87STom Joseph                     {
45067a78d87STom Joseph                         return;
45167a78d87STom Joseph                     }
45267a78d87STom Joseph 
45367a78d87STom Joseph                     if (ipmiProtocolEnabled)
45467a78d87STom Joseph                     {
455e5a99777SAlbert Zhang                         handleProtocolEnabled(
456e5a99777SAlbert Zhang                             *ipmiProtocolEnabled, asyncResp,
457e5a99777SAlbert Zhang                             "/xyz/openbmc_project/control/service/"
458e5a99777SAlbert Zhang                             "phosphor_2dipmi_2dnet_40");
459e5a99777SAlbert Zhang                     }
460e5a99777SAlbert Zhang                 }
461e5a99777SAlbert Zhang 
462e5a99777SAlbert Zhang                 if (ssh)
463e5a99777SAlbert Zhang                 {
464e5a99777SAlbert Zhang                     std::optional<bool> sshProtocolEnabled;
465e5a99777SAlbert Zhang                     if (!json_util::readJson(*ssh, asyncResp->res,
466e5a99777SAlbert Zhang                                              "ProtocolEnabled",
467e5a99777SAlbert Zhang                                              sshProtocolEnabled))
468e5a99777SAlbert Zhang                     {
469e5a99777SAlbert Zhang                         return;
470e5a99777SAlbert Zhang                     }
471e5a99777SAlbert Zhang 
472e5a99777SAlbert Zhang                     if (sshProtocolEnabled)
473e5a99777SAlbert Zhang                     {
474e5a99777SAlbert Zhang                         handleProtocolEnabled(
475e5a99777SAlbert Zhang                             *sshProtocolEnabled, asyncResp,
476e5a99777SAlbert Zhang                             "/xyz/openbmc_project/control/service/dropbear");
47767a78d87STom Joseph                     }
47867a78d87STom Joseph                 }
4797e860f15SJohn Edward Broadbent             });
4807e860f15SJohn Edward Broadbent 
4817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
482ed398213SEd Tanous         .privileges(redfish::privileges::getManagerNetworkProtocol)
4837e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
48472048780SAbhishek Patel             [](const crow::Request& req,
4857e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
48672048780SAbhishek Patel                 getNetworkData(asyncResp, req);
4877e860f15SJohn Edward Broadbent             });
488cf05f9dcSJohnathan Mantey }
48970141561SBorawski.Lukasz 
49070141561SBorawski.Lukasz } // namespace redfish
491