xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision abf2add6a35aaf42ba60c4ae955a4d8925b13b19)
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 
18 #include "error_messages.hpp"
19 #include "node.hpp"
20 
21 #include <variant>
22 
23 namespace redfish
24 {
25 
26 enum NetworkProtocolUnitStructFields
27 {
28     NET_PROTO_UNIT_NAME,
29     NET_PROTO_UNIT_DESC,
30     NET_PROTO_UNIT_LOAD_STATE,
31     NET_PROTO_UNIT_ACTIVE_STATE,
32     NET_PROTO_UNIT_SUB_STATE,
33     NET_PROTO_UNIT_DEVICE,
34     NET_PROTO_UNIT_OBJ_PATH,
35     NET_PROTO_UNIT_ALWAYS_0,
36     NET_PROTO_UNIT_ALWAYS_EMPTY,
37     NET_PROTO_UNIT_ALWAYS_ROOT_PATH
38 };
39 
40 enum NetworkProtocolListenResponseElements
41 {
42     NET_PROTO_LISTEN_TYPE,
43     NET_PROTO_LISTEN_STREAM
44 };
45 
46 /**
47  * @brief D-Bus Unit structure returned in array from ListUnits Method
48  */
49 using UnitStruct =
50     std::tuple<std::string, std::string, std::string, std::string, std::string,
51                std::string, sdbusplus::message::object_path, uint32_t,
52                std::string, sdbusplus::message::object_path>;
53 
54 struct ServiceConfiguration
55 {
56     const char* serviceName;
57     const char* socketPath;
58 };
59 
60 const static boost::container::flat_map<const char*, ServiceConfiguration>
61     protocolToDBus{
62         {"SSH",
63          {"dropbear.service",
64           "/org/freedesktop/systemd1/unit/dropbear_2esocket"}},
65         {"HTTPS",
66          {"phosphor-gevent.service",
67           "/org/freedesktop/systemd1/unit/phosphor_2dgevent_2esocket"}},
68         {"IPMI",
69          {"phosphor-ipmi-net.service",
70           "/org/freedesktop/systemd1/unit/phosphor_2dipmi_2dnet_2esocket"}}};
71 
72 class NetworkProtocol : public Node
73 {
74   public:
75     NetworkProtocol(CrowApp& app) :
76         Node(app, "/redfish/v1/Managers/bmc/NetworkProtocol")
77     {
78         entityPrivileges = {
79             {boost::beast::http::verb::get, {{"Login"}}},
80             {boost::beast::http::verb::head, {{"Login"}}},
81             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
82             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
83             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
84             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
85     }
86 
87   private:
88     void doGet(crow::Response& res, const crow::Request& req,
89                const std::vector<std::string>& params) override
90     {
91         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
92 
93         getData(asyncResp);
94     }
95 
96     std::string getHostName() const
97     {
98         std::string hostName;
99 
100         std::array<char, HOST_NAME_MAX> hostNameCStr;
101         if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
102         {
103             hostName = hostNameCStr.data();
104         }
105         return hostName;
106     }
107 
108     void getData(const std::shared_ptr<AsyncResp>& asyncResp)
109     {
110         asyncResp->res.jsonValue["@odata.type"] =
111             "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol";
112         asyncResp->res.jsonValue["@odata.id"] =
113             "/redfish/v1/Managers/bmc/NetworkProtocol";
114         asyncResp->res.jsonValue["@odata.context"] =
115             "/redfish/v1/"
116             "$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol";
117         asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
118         asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
119         asyncResp->res.jsonValue["Description"] = "Manager Network Service";
120         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
121         asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
122         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
123 
124         for (auto& protocol : protocolToDBus)
125         {
126             asyncResp->res.jsonValue[protocol.first]["ProtocolEnabled"] = false;
127         }
128 
129         asyncResp->res.jsonValue["HostName"] = getHostName();
130 
131         crow::connections::systemBus->async_method_call(
132             [asyncResp](const boost::system::error_code ec,
133                         const std::vector<UnitStruct>& resp) {
134                 if (ec)
135                 {
136                     asyncResp->res.jsonValue = nlohmann::json::object();
137                     messages::internalError(asyncResp->res);
138                     return;
139                 }
140 
141                 for (auto& unit : resp)
142                 {
143                     for (auto& kv : protocolToDBus)
144                     {
145                         if (kv.second.serviceName ==
146                             std::get<NET_PROTO_UNIT_NAME>(unit))
147                         {
148                             continue;
149                         }
150                         const char* service = kv.first;
151                         const char* socketPath = kv.second.socketPath;
152 
153                         asyncResp->res.jsonValue[service]["ProtocolEnabled"] =
154                             std::get<NET_PROTO_UNIT_SUB_STATE>(unit) ==
155                             "running";
156 
157                         crow::connections::systemBus->async_method_call(
158                             [asyncResp, service{std::string(service)},
159                              socketPath](
160                                 const boost::system::error_code ec,
161                                 const std::variant<std::vector<std::tuple<
162                                     std::string, std::string>>>& resp) {
163                                 if (ec)
164                                 {
165                                     messages::internalError(asyncResp->res);
166                                     return;
167                                 }
168                                 const std::vector<
169                                     std::tuple<std::string, std::string>>*
170                                     responsePtr = std::get_if<std::vector<
171                                         std::tuple<std::string, std::string>>>(
172                                         &resp);
173                                 if (responsePtr == nullptr ||
174                                     responsePtr->size() < 1)
175                                 {
176                                     return;
177                                 }
178 
179                                 const std::string& listenStream =
180                                     std::get<NET_PROTO_LISTEN_STREAM>(
181                                         (*responsePtr)[0]);
182                                 std::size_t lastColonPos =
183                                     listenStream.rfind(":");
184                                 if (lastColonPos == std::string::npos)
185                                 {
186                                     // Not a port
187                                     return;
188                                 }
189                                 std::string portStr =
190                                     listenStream.substr(lastColonPos + 1);
191                                 char* endPtr = nullptr;
192                                 // Use strtol instead of stroi to avoid
193                                 // exceptions
194                                 long port =
195                                     std::strtol(portStr.c_str(), &endPtr, 10);
196 
197                                 if (*endPtr != '\0' || portStr.empty())
198                                 {
199                                     // Invalid value
200                                     asyncResp->res.jsonValue[service]["Port"] =
201                                         nullptr;
202                                 }
203                                 else
204                                 {
205                                     // Everything OK
206                                     asyncResp->res.jsonValue[service]["Port"] =
207                                         port;
208                                 }
209                             },
210                             "org.freedesktop.systemd1", socketPath,
211                             "org.freedesktop.DBus.Properties", "Get",
212                             "org.freedesktop.systemd1.Socket", "Listen");
213                     }
214                 }
215             },
216             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
217             "org.freedesktop.systemd1.Manager", "ListUnits");
218     }
219 };
220 
221 } // namespace redfish
222