xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 43b761d0c8f5c4c39199093ee4bcd60698e77138)
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                                 const boost::system::error_code ec,
160                                 const std::variant<std::vector<std::tuple<
161                                     std::string, std::string>>>& resp) {
162                                 if (ec)
163                                 {
164                                     messages::internalError(asyncResp->res);
165                                     return;
166                                 }
167                                 const std::vector<
168                                     std::tuple<std::string, std::string>>*
169                                     responsePtr = std::get_if<std::vector<
170                                         std::tuple<std::string, std::string>>>(
171                                         &resp);
172                                 if (responsePtr == nullptr ||
173                                     responsePtr->size() < 1)
174                                 {
175                                     return;
176                                 }
177 
178                                 const std::string& listenStream =
179                                     std::get<NET_PROTO_LISTEN_STREAM>(
180                                         (*responsePtr)[0]);
181                                 std::size_t lastColonPos =
182                                     listenStream.rfind(":");
183                                 if (lastColonPos == std::string::npos)
184                                 {
185                                     // Not a port
186                                     return;
187                                 }
188                                 std::string portStr =
189                                     listenStream.substr(lastColonPos + 1);
190                                 char* endPtr = nullptr;
191                                 // Use strtol instead of stroi to avoid
192                                 // exceptions
193                                 long port =
194                                     std::strtol(portStr.c_str(), &endPtr, 10);
195 
196                                 if (*endPtr != '\0' || portStr.empty())
197                                 {
198                                     // Invalid value
199                                     asyncResp->res.jsonValue[service]["Port"] =
200                                         nullptr;
201                                 }
202                                 else
203                                 {
204                                     // Everything OK
205                                     asyncResp->res.jsonValue[service]["Port"] =
206                                         port;
207                                 }
208                             },
209                             "org.freedesktop.systemd1", socketPath,
210                             "org.freedesktop.DBus.Properties", "Get",
211                             "org.freedesktop.systemd1.Socket", "Listen");
212                     }
213                 }
214             },
215             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
216             "org.freedesktop.systemd1.Manager", "ListUnits");
217     }
218 };
219 
220 } // namespace redfish
221