xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 6193231855a08068dc74c33ae95efd23782d4157)
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"
1970141561SBorawski.Lukasz #include "node.hpp"
2070141561SBorawski.Lukasz 
2120e6ea5dSraviteja-b #include <optional>
2220e6ea5dSraviteja-b #include <utils/json_utils.hpp>
23abf2add6SEd Tanous #include <variant>
241abe55efSEd Tanous namespace redfish
251abe55efSEd Tanous {
2670141561SBorawski.Lukasz 
271abe55efSEd Tanous enum NetworkProtocolUnitStructFields
281abe55efSEd Tanous {
293a8a0088SKowalski, Kamil     NET_PROTO_UNIT_NAME,
303a8a0088SKowalski, Kamil     NET_PROTO_UNIT_DESC,
313a8a0088SKowalski, Kamil     NET_PROTO_UNIT_LOAD_STATE,
323a8a0088SKowalski, Kamil     NET_PROTO_UNIT_ACTIVE_STATE,
333a8a0088SKowalski, Kamil     NET_PROTO_UNIT_SUB_STATE,
343a8a0088SKowalski, Kamil     NET_PROTO_UNIT_DEVICE,
353a8a0088SKowalski, Kamil     NET_PROTO_UNIT_OBJ_PATH,
363a8a0088SKowalski, Kamil     NET_PROTO_UNIT_ALWAYS_0,
373a8a0088SKowalski, Kamil     NET_PROTO_UNIT_ALWAYS_EMPTY,
383a8a0088SKowalski, Kamil     NET_PROTO_UNIT_ALWAYS_ROOT_PATH
393a8a0088SKowalski, Kamil };
403a8a0088SKowalski, Kamil 
411abe55efSEd Tanous enum NetworkProtocolListenResponseElements
421abe55efSEd Tanous {
433a8a0088SKowalski, Kamil     NET_PROTO_LISTEN_TYPE,
443a8a0088SKowalski, Kamil     NET_PROTO_LISTEN_STREAM
453a8a0088SKowalski, Kamil };
463a8a0088SKowalski, Kamil 
473a8a0088SKowalski, Kamil /**
483a8a0088SKowalski, Kamil  * @brief D-Bus Unit structure returned in array from ListUnits Method
493a8a0088SKowalski, Kamil  */
503a8a0088SKowalski, Kamil using UnitStruct =
513a8a0088SKowalski, Kamil     std::tuple<std::string, std::string, std::string, std::string, std::string,
523a8a0088SKowalski, Kamil                std::string, sdbusplus::message::object_path, uint32_t,
533a8a0088SKowalski, Kamil                std::string, sdbusplus::message::object_path>;
543a8a0088SKowalski, Kamil 
55ec4974ddSAppaRao Puli const static boost::container::flat_map<const char*, std::string>
56ec4974ddSAppaRao Puli     protocolToDBus{{"SSH", "dropbear"},
57ec4974ddSAppaRao Puli                    {"HTTPS", "bmcweb"},
58ec4974ddSAppaRao Puli                    {"IPMI", "phosphor-ipmi-net"}};
593a8a0088SKowalski, Kamil 
60d24bfc7aSJennifer Lee inline void
61d24bfc7aSJennifer Lee     extractNTPServersAndDomainNamesData(const GetManagedObjects& dbus_data,
62d24bfc7aSJennifer Lee                                         std::vector<std::string>& ntpData,
63d24bfc7aSJennifer Lee                                         std::vector<std::string>& dnData)
6420e6ea5dSraviteja-b {
6520e6ea5dSraviteja-b     for (const auto& obj : dbus_data)
6620e6ea5dSraviteja-b     {
6720e6ea5dSraviteja-b         for (const auto& ifacePair : obj.second)
6820e6ea5dSraviteja-b         {
6920e6ea5dSraviteja-b             if (obj.first == "/xyz/openbmc_project/network/eth0")
7020e6ea5dSraviteja-b             {
7120e6ea5dSraviteja-b                 if (ifacePair.first ==
7220e6ea5dSraviteja-b                     "xyz.openbmc_project.Network.EthernetInterface")
7320e6ea5dSraviteja-b                 {
7420e6ea5dSraviteja-b                     for (const auto& propertyPair : ifacePair.second)
7520e6ea5dSraviteja-b                     {
7620e6ea5dSraviteja-b                         if (propertyPair.first == "NTPServers")
7720e6ea5dSraviteja-b                         {
7820e6ea5dSraviteja-b                             const std::vector<std::string>* ntpServers =
7920e6ea5dSraviteja-b                                 sdbusplus::message::variant_ns::get_if<
8020e6ea5dSraviteja-b                                     std::vector<std::string>>(
8120e6ea5dSraviteja-b                                     &propertyPair.second);
8220e6ea5dSraviteja-b                             if (ntpServers != nullptr)
8320e6ea5dSraviteja-b                             {
8420e6ea5dSraviteja-b                                 ntpData = std::move(*ntpServers);
8520e6ea5dSraviteja-b                             }
8620e6ea5dSraviteja-b                         }
87d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
88d24bfc7aSJennifer Lee                         {
89d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
90d24bfc7aSJennifer Lee                                 sdbusplus::message::variant_ns::get_if<
91d24bfc7aSJennifer Lee                                     std::vector<std::string>>(
92d24bfc7aSJennifer Lee                                     &propertyPair.second);
93d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
94d24bfc7aSJennifer Lee                             {
95d24bfc7aSJennifer Lee                                 dnData = std::move(*domainNames);
96d24bfc7aSJennifer Lee                             }
97d24bfc7aSJennifer Lee                         }
9820e6ea5dSraviteja-b                     }
9920e6ea5dSraviteja-b                 }
10020e6ea5dSraviteja-b             }
10120e6ea5dSraviteja-b         }
10220e6ea5dSraviteja-b     }
10320e6ea5dSraviteja-b }
10420e6ea5dSraviteja-b 
10520e6ea5dSraviteja-b template <typename CallbackFunc>
10620e6ea5dSraviteja-b void getEthernetIfaceData(CallbackFunc&& callback)
10720e6ea5dSraviteja-b {
10820e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
10920e6ea5dSraviteja-b         [callback{std::move(callback)}](
11020e6ea5dSraviteja-b             const boost::system::error_code error_code,
11120e6ea5dSraviteja-b             const GetManagedObjects& dbus_data) {
11220e6ea5dSraviteja-b             std::vector<std::string> ntpServers;
113d24bfc7aSJennifer Lee             std::vector<std::string> domainNames;
11420e6ea5dSraviteja-b 
11520e6ea5dSraviteja-b             if (error_code)
11620e6ea5dSraviteja-b             {
117d24bfc7aSJennifer Lee                 callback(false, ntpServers, domainNames);
11820e6ea5dSraviteja-b                 return;
11920e6ea5dSraviteja-b             }
12020e6ea5dSraviteja-b 
121d24bfc7aSJennifer Lee             extractNTPServersAndDomainNamesData(dbus_data, ntpServers,
122d24bfc7aSJennifer Lee                                                 domainNames);
12320e6ea5dSraviteja-b 
124d24bfc7aSJennifer Lee             callback(true, ntpServers, domainNames);
12520e6ea5dSraviteja-b         },
12620e6ea5dSraviteja-b         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
12720e6ea5dSraviteja-b         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
128271584abSEd Tanous }
12920e6ea5dSraviteja-b 
1301abe55efSEd Tanous class NetworkProtocol : public Node
1311abe55efSEd Tanous {
13270141561SBorawski.Lukasz   public:
1331abe55efSEd Tanous     NetworkProtocol(CrowApp& app) :
1347af91514SGunnar Mills         Node(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
1351abe55efSEd Tanous     {
1364b1b8683SBorawski.Lukasz         entityPrivileges = {
1374b1b8683SBorawski.Lukasz             {boost::beast::http::verb::get, {{"Login"}}},
138e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
139e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
140e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
141e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
142e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
14370141561SBorawski.Lukasz     }
14470141561SBorawski.Lukasz 
14570141561SBorawski.Lukasz   private:
14655c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
1471abe55efSEd Tanous                const std::vector<std::string>& params) override
1481abe55efSEd Tanous     {
1493a8a0088SKowalski, Kamil         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1503a8a0088SKowalski, Kamil 
1513a8a0088SKowalski, Kamil         getData(asyncResp);
15270141561SBorawski.Lukasz     }
15370141561SBorawski.Lukasz 
1541abe55efSEd Tanous     std::string getHostName() const
1551abe55efSEd Tanous     {
15670141561SBorawski.Lukasz         std::string hostName;
15770141561SBorawski.Lukasz 
15870141561SBorawski.Lukasz         std::array<char, HOST_NAME_MAX> hostNameCStr;
1591abe55efSEd Tanous         if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
1601abe55efSEd Tanous         {
16170141561SBorawski.Lukasz             hostName = hostNameCStr.data();
16270141561SBorawski.Lukasz         }
16370141561SBorawski.Lukasz         return hostName;
16470141561SBorawski.Lukasz     }
16570141561SBorawski.Lukasz 
16620e6ea5dSraviteja-b     void getNTPProtocolEnabled(const std::shared_ptr<AsyncResp>& asyncResp)
16720e6ea5dSraviteja-b     {
16820e6ea5dSraviteja-b         crow::connections::systemBus->async_method_call(
16920e6ea5dSraviteja-b             [asyncResp](const boost::system::error_code error_code,
17020e6ea5dSraviteja-b                         const std::variant<std::string>& timeSyncMethod) {
17120e6ea5dSraviteja-b                 const std::string* s =
17220e6ea5dSraviteja-b                     std::get_if<std::string>(&timeSyncMethod);
17320e6ea5dSraviteja-b 
17420e6ea5dSraviteja-b                 if (*s == "xyz.openbmc_project.Time.Synchronization.Method.NTP")
17520e6ea5dSraviteja-b                 {
17620e6ea5dSraviteja-b                     asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = true;
17720e6ea5dSraviteja-b                 }
17820e6ea5dSraviteja-b                 else if (*s == "xyz.openbmc_project.Time.Synchronization."
17920e6ea5dSraviteja-b                                "Method.Manual")
18020e6ea5dSraviteja-b                 {
18120e6ea5dSraviteja-b                     asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = false;
18220e6ea5dSraviteja-b                 }
18320e6ea5dSraviteja-b             },
18420e6ea5dSraviteja-b             "xyz.openbmc_project.Settings",
18520e6ea5dSraviteja-b             "/xyz/openbmc_project/time/sync_method",
18620e6ea5dSraviteja-b             "org.freedesktop.DBus.Properties", "Get",
18720e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod");
18820e6ea5dSraviteja-b     }
18920e6ea5dSraviteja-b 
1901abe55efSEd Tanous     void getData(const std::shared_ptr<AsyncResp>& asyncResp)
1911abe55efSEd Tanous     {
1920f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
193*61932318SXiaochao Ma             "#ManagerNetworkProtocol.v1_5_0.ManagerNetworkProtocol";
1940f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1950f74e643SEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol";
1960f74e643SEd Tanous         asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
1970f74e643SEd Tanous         asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
1980f74e643SEd Tanous         asyncResp->res.jsonValue["Description"] = "Manager Network Service";
1990f74e643SEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2000f74e643SEd Tanous         asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
2010f74e643SEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
202*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["ProtocolEnabled"] = true;
203*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["Port"] = 161;
204*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["AuthenticationProtocol"] =
205*61932318SXiaochao Ma             "CommunityString";
206*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["CommunityAccessMode"] = "Full";
207*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["HideCommunityStrings"] = true;
208*61932318SXiaochao Ma         asyncResp->res
209*61932318SXiaochao Ma             .jsonValue["SNMP"]["EngineId"]["EnterpriseSpecificMethod"] =
210*61932318SXiaochao Ma             nullptr;
211*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EngineId"]["PrivateEnterpriseId"] =
212*61932318SXiaochao Ma             nullptr;
213*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EnableSNMPv1"] = false;
214*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EnableSNMPv2c"] = true;
215*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EnableSNMPv3"] = false;
216*61932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EncryptionProtocol"] = "None";
217*61932318SXiaochao Ma         nlohmann::json& memberArray =
218*61932318SXiaochao Ma             asyncResp->res.jsonValue["SNMP"]["CommunityStrings"];
219*61932318SXiaochao Ma         memberArray = nlohmann::json::array();
220*61932318SXiaochao Ma         memberArray.push_back({{"AccessMode", "Full"}});
221*61932318SXiaochao Ma         memberArray.push_back({{"CommunityString", ""}});
222*61932318SXiaochao Ma         memberArray.push_back({{"Name", ""}});
2230f74e643SEd Tanous 
224*61932318SXiaochao Ma         // HTTP is Mandatory attribute as per OCP Baseline Profile - v1.0.0,
225818ea7b8SJoshi-Mansi         // but from security perspective it is not recommended to use.
226818ea7b8SJoshi-Mansi         // Hence using protocolEnabled as false to make it OCP and security-wise
227818ea7b8SJoshi-Mansi         // compliant
228818ea7b8SJoshi-Mansi         asyncResp->res.jsonValue["HTTP"]["Port"] = 0;
229818ea7b8SJoshi-Mansi         asyncResp->res.jsonValue["HTTP"]["ProtocolEnabled"] = false;
230818ea7b8SJoshi-Mansi 
2310f74e643SEd Tanous         for (auto& protocol : protocolToDBus)
2320f74e643SEd Tanous         {
2330f74e643SEd Tanous             asyncResp->res.jsonValue[protocol.first]["ProtocolEnabled"] = false;
2340f74e643SEd Tanous         }
2350f74e643SEd Tanous 
236d24bfc7aSJennifer Lee         std::string hostName = getHostName();
237d24bfc7aSJennifer Lee 
238d24bfc7aSJennifer Lee         asyncResp->res.jsonValue["HostName"] = hostName;
2393a8a0088SKowalski, Kamil 
24020e6ea5dSraviteja-b         getNTPProtocolEnabled(asyncResp);
24120e6ea5dSraviteja-b 
24220e6ea5dSraviteja-b         // TODO Get eth0 interface data, and call the below callback for JSON
24320e6ea5dSraviteja-b         // preparation
244271584abSEd Tanous         getEthernetIfaceData(
245271584abSEd Tanous             [hostName, asyncResp](const bool& success,
246d24bfc7aSJennifer Lee                                   const std::vector<std::string>& ntpServers,
247d24bfc7aSJennifer Lee                                   const std::vector<std::string>& domainNames) {
24820e6ea5dSraviteja-b                 if (!success)
24920e6ea5dSraviteja-b                 {
250271584abSEd Tanous                     messages::resourceNotFound(asyncResp->res,
251271584abSEd Tanous                                                "EthernetInterface", "eth0");
25220e6ea5dSraviteja-b                     return;
25320e6ea5dSraviteja-b                 }
25420e6ea5dSraviteja-b                 asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
255d24bfc7aSJennifer Lee                 if (hostName.empty() == false)
256d24bfc7aSJennifer Lee                 {
257749dad7dSRatan Gupta                     std::string FQDN = std::move(hostName);
258d24bfc7aSJennifer Lee                     if (domainNames.empty() == false)
259d24bfc7aSJennifer Lee                     {
260749dad7dSRatan Gupta                         FQDN += "." + domainNames[0];
261d24bfc7aSJennifer Lee                     }
262749dad7dSRatan Gupta                     asyncResp->res.jsonValue["FQDN"] = std::move(FQDN);
263d24bfc7aSJennifer Lee                 }
26420e6ea5dSraviteja-b             });
26520e6ea5dSraviteja-b 
266865fbb75SEd Tanous         crow::connections::systemBus->async_method_call(
267271584abSEd Tanous             [asyncResp](const boost::system::error_code e,
268271584abSEd Tanous                         const std::vector<UnitStruct>& r) {
269271584abSEd Tanous                 if (e)
2701abe55efSEd Tanous                 {
2713a8a0088SKowalski, Kamil                     asyncResp->res.jsonValue = nlohmann::json::object();
272f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
273865fbb75SEd Tanous                     return;
2743a8a0088SKowalski, Kamil                 }
2755968caeeSMarri Devender Rao                 asyncResp->res.jsonValue["HTTPS"]["Certificates"] = {
2765968caeeSMarri Devender Rao                     {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol/"
277659dd62eSJason M. Bills                                   "HTTPS/Certificates"}};
2783a8a0088SKowalski, Kamil 
279271584abSEd Tanous                 for (auto& unit : r)
2801abe55efSEd Tanous                 {
281ec4974ddSAppaRao Puli                     /* Only traverse through <xyz>.socket units */
282ec4974ddSAppaRao Puli                     std::string unitName = std::get<NET_PROTO_UNIT_NAME>(unit);
283ec4974ddSAppaRao Puli                     if (!boost::ends_with(unitName, ".socket"))
2841abe55efSEd Tanous                     {
285865fbb75SEd Tanous                         continue;
28670141561SBorawski.Lukasz                     }
28770141561SBorawski.Lukasz 
288ec4974ddSAppaRao Puli                     for (auto& kv : protocolToDBus)
289ec4974ddSAppaRao Puli                     {
290ec4974ddSAppaRao Puli                         // We are interested in services, which starts with
291ec4974ddSAppaRao Puli                         // mapped service name
292ec4974ddSAppaRao Puli                         if (!boost::starts_with(unitName, kv.second))
293ec4974ddSAppaRao Puli                         {
294ec4974ddSAppaRao Puli                             continue;
295ec4974ddSAppaRao Puli                         }
296ec4974ddSAppaRao Puli                         const char* rfServiceKey = kv.first;
297ec4974ddSAppaRao Puli                         std::string socketPath =
298ec4974ddSAppaRao Puli                             std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
299ec4974ddSAppaRao Puli                         std::string unitState =
300ec4974ddSAppaRao Puli                             std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
301ec4974ddSAppaRao Puli 
302ec4974ddSAppaRao Puli                         asyncResp->res
303ec4974ddSAppaRao Puli                             .jsonValue[rfServiceKey]["ProtocolEnabled"] =
304ec4974ddSAppaRao Puli                             (unitState == "running") ||
305ec4974ddSAppaRao Puli                             (unitState == "listening");
306ec4974ddSAppaRao Puli 
307865fbb75SEd Tanous                         crow::connections::systemBus->async_method_call(
308ec4974ddSAppaRao Puli                             [asyncResp,
309ec4974ddSAppaRao Puli                              rfServiceKey{std::string(rfServiceKey)}](
310865fbb75SEd Tanous                                 const boost::system::error_code ec,
311abf2add6SEd Tanous                                 const std::variant<std::vector<std::tuple<
312abf2add6SEd Tanous                                     std::string, std::string>>>& resp) {
3131abe55efSEd Tanous                                 if (ec)
3141abe55efSEd Tanous                                 {
315a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
316865fbb75SEd Tanous                                     return;
3173a8a0088SKowalski, Kamil                                 }
318abf2add6SEd Tanous                                 const std::vector<
319abf2add6SEd Tanous                                     std::tuple<std::string, std::string>>*
320abf2add6SEd Tanous                                     responsePtr = std::get_if<std::vector<
321abf2add6SEd Tanous                                         std::tuple<std::string, std::string>>>(
3221b6b96c5SEd Tanous                                         &resp);
3231abe55efSEd Tanous                                 if (responsePtr == nullptr ||
3241abe55efSEd Tanous                                     responsePtr->size() < 1)
3251abe55efSEd Tanous                                 {
326865fbb75SEd Tanous                                     return;
32770141561SBorawski.Lukasz                                 }
32870141561SBorawski.Lukasz 
329865fbb75SEd Tanous                                 const std::string& listenStream =
3301abe55efSEd Tanous                                     std::get<NET_PROTO_LISTEN_STREAM>(
3311abe55efSEd Tanous                                         (*responsePtr)[0]);
3321abe55efSEd Tanous                                 std::size_t lastColonPos =
3331abe55efSEd Tanous                                     listenStream.rfind(":");
3341abe55efSEd Tanous                                 if (lastColonPos == std::string::npos)
3351abe55efSEd Tanous                                 {
336865fbb75SEd Tanous                                     // Not a port
337865fbb75SEd Tanous                                     return;
338865fbb75SEd Tanous                                 }
3391abe55efSEd Tanous                                 std::string portStr =
3401abe55efSEd Tanous                                     listenStream.substr(lastColonPos + 1);
341ec4974ddSAppaRao Puli                                 if (portStr.empty())
342ec4974ddSAppaRao Puli                                 {
343ec4974ddSAppaRao Puli                                     return;
344ec4974ddSAppaRao Puli                                 }
345865fbb75SEd Tanous                                 char* endPtr = nullptr;
346ec4974ddSAppaRao Puli                                 errno = 0;
3471abe55efSEd Tanous                                 // Use strtol instead of stroi to avoid
3481abe55efSEd Tanous                                 // exceptions
3491abe55efSEd Tanous                                 long port =
3501abe55efSEd Tanous                                     std::strtol(portStr.c_str(), &endPtr, 10);
351ec4974ddSAppaRao Puli                                 if ((errno == 0) && (*endPtr == '\0'))
3521abe55efSEd Tanous                                 {
353ec4974ddSAppaRao Puli                                     asyncResp->res
354ec4974ddSAppaRao Puli                                         .jsonValue[rfServiceKey]["Port"] = port;
3551abe55efSEd Tanous                                 }
356ec4974ddSAppaRao Puli                                 return;
357865fbb75SEd Tanous                             },
358865fbb75SEd Tanous                             "org.freedesktop.systemd1", socketPath,
359865fbb75SEd Tanous                             "org.freedesktop.DBus.Properties", "Get",
360865fbb75SEd Tanous                             "org.freedesktop.systemd1.Socket", "Listen");
361ec4974ddSAppaRao Puli 
362ec4974ddSAppaRao Puli                         // We found service, break the inner loop.
363ec4974ddSAppaRao Puli                         break;
364865fbb75SEd Tanous                     }
365865fbb75SEd Tanous                 }
366865fbb75SEd Tanous             },
367865fbb75SEd Tanous             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
368865fbb75SEd Tanous             "org.freedesktop.systemd1.Manager", "ListUnits");
369865fbb75SEd Tanous     }
370501be32bSraviteja-b 
371501be32bSraviteja-b     void handleHostnamePatch(const std::string& hostName,
372501be32bSraviteja-b                              const std::shared_ptr<AsyncResp>& asyncResp)
373501be32bSraviteja-b     {
374501be32bSraviteja-b         crow::connections::systemBus->async_method_call(
375501be32bSraviteja-b             [asyncResp](const boost::system::error_code ec) {
376501be32bSraviteja-b                 if (ec)
377501be32bSraviteja-b                 {
378501be32bSraviteja-b                     messages::internalError(asyncResp->res);
379501be32bSraviteja-b                     return;
380501be32bSraviteja-b                 }
381501be32bSraviteja-b             },
382501be32bSraviteja-b             "xyz.openbmc_project.Network",
383501be32bSraviteja-b             "/xyz/openbmc_project/network/config",
384501be32bSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
385501be32bSraviteja-b             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
386501be32bSraviteja-b             std::variant<std::string>(hostName));
387501be32bSraviteja-b     }
388501be32bSraviteja-b 
38920e6ea5dSraviteja-b     void handleNTPProtocolEnabled(const bool& ntpEnabled,
39020e6ea5dSraviteja-b                                   const std::shared_ptr<AsyncResp>& asyncResp)
39120e6ea5dSraviteja-b     {
39220e6ea5dSraviteja-b         std::string timeSyncMethod;
39320e6ea5dSraviteja-b         if (ntpEnabled)
39420e6ea5dSraviteja-b         {
39520e6ea5dSraviteja-b             timeSyncMethod =
39620e6ea5dSraviteja-b                 "xyz.openbmc_project.Time.Synchronization.Method.NTP";
39720e6ea5dSraviteja-b         }
39820e6ea5dSraviteja-b         else
39920e6ea5dSraviteja-b         {
40020e6ea5dSraviteja-b             timeSyncMethod =
40120e6ea5dSraviteja-b                 "xyz.openbmc_project.Time.Synchronization.Method.Manual";
40220e6ea5dSraviteja-b         }
40320e6ea5dSraviteja-b 
40420e6ea5dSraviteja-b         crow::connections::systemBus->async_method_call(
405cf05f9dcSJohnathan Mantey             [asyncResp](const boost::system::error_code error_code) {},
40620e6ea5dSraviteja-b             "xyz.openbmc_project.Settings",
40720e6ea5dSraviteja-b             "/xyz/openbmc_project/time/sync_method",
40820e6ea5dSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
40920e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
41020e6ea5dSraviteja-b             std::variant<std::string>{timeSyncMethod});
41120e6ea5dSraviteja-b     }
41220e6ea5dSraviteja-b 
41320e6ea5dSraviteja-b     void handleNTPServersPatch(const std::vector<std::string>& ntpServers,
41420e6ea5dSraviteja-b                                const std::shared_ptr<AsyncResp>& asyncResp)
41520e6ea5dSraviteja-b     {
41620e6ea5dSraviteja-b         crow::connections::systemBus->async_method_call(
417cf05f9dcSJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
41820e6ea5dSraviteja-b                 if (ec)
41920e6ea5dSraviteja-b                 {
42020e6ea5dSraviteja-b                     messages::internalError(asyncResp->res);
42120e6ea5dSraviteja-b                     return;
42220e6ea5dSraviteja-b                 }
42320e6ea5dSraviteja-b             },
42420e6ea5dSraviteja-b             "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/eth0",
42520e6ea5dSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
42620e6ea5dSraviteja-b             "xyz.openbmc_project.Network.EthernetInterface", "NTPServers",
42720e6ea5dSraviteja-b             std::variant<std::vector<std::string>>{ntpServers});
42820e6ea5dSraviteja-b     }
42920e6ea5dSraviteja-b 
430501be32bSraviteja-b     void doPatch(crow::Response& res, const crow::Request& req,
431501be32bSraviteja-b                  const std::vector<std::string>& params) override
432501be32bSraviteja-b     {
433501be32bSraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
434501be32bSraviteja-b         std::optional<std::string> newHostName;
435cf05f9dcSJohnathan Mantey         std::optional<nlohmann::json> ntp;
436501be32bSraviteja-b 
437cf05f9dcSJohnathan Mantey         if (!json_util::readJson(req, res, "HostName", newHostName, "NTP", ntp))
438501be32bSraviteja-b         {
439501be32bSraviteja-b             return;
440501be32bSraviteja-b         }
441cf05f9dcSJohnathan Mantey 
442cf05f9dcSJohnathan Mantey         res.result(boost::beast::http::status::no_content);
443501be32bSraviteja-b         if (newHostName)
444501be32bSraviteja-b         {
445501be32bSraviteja-b             handleHostnamePatch(*newHostName, asyncResp);
446cf05f9dcSJohnathan Mantey         }
447cf05f9dcSJohnathan Mantey 
448cf05f9dcSJohnathan Mantey         if (ntp)
449cf05f9dcSJohnathan Mantey         {
450cf05f9dcSJohnathan Mantey             std::optional<std::vector<std::string>> ntpServers;
451cf05f9dcSJohnathan Mantey             std::optional<bool> ntpEnabled;
452cf05f9dcSJohnathan Mantey             if (!json_util::readJson(*ntp, res, "NTPServers", ntpServers,
453cf05f9dcSJohnathan Mantey                                      "ProtocolEnabled", ntpEnabled))
454cf05f9dcSJohnathan Mantey             {
455501be32bSraviteja-b                 return;
456501be32bSraviteja-b             }
457cf05f9dcSJohnathan Mantey 
45820e6ea5dSraviteja-b             if (ntpEnabled)
45920e6ea5dSraviteja-b             {
46020e6ea5dSraviteja-b                 handleNTPProtocolEnabled(*ntpEnabled, asyncResp);
46120e6ea5dSraviteja-b             }
462cf05f9dcSJohnathan Mantey 
46320e6ea5dSraviteja-b             if (ntpServers)
46420e6ea5dSraviteja-b             {
46520e6ea5dSraviteja-b                 handleNTPServersPatch(*ntpServers, asyncResp);
46620e6ea5dSraviteja-b             }
467501be32bSraviteja-b         }
468cf05f9dcSJohnathan Mantey     }
46970141561SBorawski.Lukasz };
47070141561SBorawski.Lukasz 
47170141561SBorawski.Lukasz } // namespace redfish
472