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 namespace redfish
22 {
23 
24 enum NetworkProtocolUnitStructFields
25 {
26     NET_PROTO_UNIT_NAME,
27     NET_PROTO_UNIT_DESC,
28     NET_PROTO_UNIT_LOAD_STATE,
29     NET_PROTO_UNIT_ACTIVE_STATE,
30     NET_PROTO_UNIT_SUB_STATE,
31     NET_PROTO_UNIT_DEVICE,
32     NET_PROTO_UNIT_OBJ_PATH,
33     NET_PROTO_UNIT_ALWAYS_0,
34     NET_PROTO_UNIT_ALWAYS_EMPTY,
35     NET_PROTO_UNIT_ALWAYS_ROOT_PATH
36 };
37 
38 enum NetworkProtocolListenResponseElements
39 {
40     NET_PROTO_LISTEN_TYPE,
41     NET_PROTO_LISTEN_STREAM
42 };
43 
44 /**
45  * @brief D-Bus Unit structure returned in array from ListUnits Method
46  */
47 using UnitStruct =
48     std::tuple<std::string, std::string, std::string, std::string, std::string,
49                std::string, sdbusplus::message::object_path, uint32_t,
50                std::string, sdbusplus::message::object_path>;
51 
52 struct ServiceConfiguration
53 {
54     const char* serviceName;
55     const char* socketPath;
56 };
57 
58 const static boost::container::flat_map<const char*, ServiceConfiguration>
59     protocolToDBus{
60         {"SSH",
61          {"dropbear.service",
62           "/org/freedesktop/systemd1/unit/dropbear_2esocket"}},
63         {"HTTPS",
64          {"phosphor-gevent.service",
65           "/org/freedesktop/systemd1/unit/phosphor_2dgevent_2esocket"}},
66         {"IPMI",
67          {"phosphor-ipmi-net.service",
68           "/org/freedesktop/systemd1/unit/phosphor_2dipmi_2dnet_2esocket"}}};
69 
70 class NetworkProtocol : public Node
71 {
72   public:
73     NetworkProtocol(CrowApp& app) :
74         Node(app, "/redfish/v1/Managers/bmc/NetworkProtocol")
75     {
76         entityPrivileges = {
77             {boost::beast::http::verb::get, {{"Login"}}},
78             {boost::beast::http::verb::head, {{"Login"}}},
79             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
80             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
81             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
82             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
83     }
84 
85   private:
86     void doGet(crow::Response& res, const crow::Request& req,
87                const std::vector<std::string>& params) override
88     {
89         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
90 
91         getData(asyncResp);
92     }
93 
94     std::string getHostName() const
95     {
96         std::string hostName;
97 
98         std::array<char, HOST_NAME_MAX> hostNameCStr;
99         if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
100         {
101             hostName = hostNameCStr.data();
102         }
103         return hostName;
104     }
105 
106     void getData(const std::shared_ptr<AsyncResp>& asyncResp)
107     {
108         asyncResp->res.jsonValue["@odata.type"] =
109             "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol";
110         asyncResp->res.jsonValue["@odata.id"] =
111             "/redfish/v1/Managers/bmc/NetworkProtocol";
112         asyncResp->res.jsonValue["@odata.context"] =
113             "/redfish/v1/"
114             "$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol";
115         asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
116         asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
117         asyncResp->res.jsonValue["Description"] = "Manager Network Service";
118         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
119         asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
120         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
121 
122         for (auto& protocol : protocolToDBus)
123         {
124             asyncResp->res.jsonValue[protocol.first]["ProtocolEnabled"] = false;
125         }
126 
127         asyncResp->res.jsonValue["HostName"] = getHostName();
128 
129         crow::connections::systemBus->async_method_call(
130             [asyncResp](const boost::system::error_code ec,
131                         const std::vector<UnitStruct>& resp) {
132                 if (ec)
133                 {
134                     asyncResp->res.jsonValue = nlohmann::json::object();
135                     messages::internalError(asyncResp->res);
136                     return;
137                 }
138 
139                 for (auto& unit : resp)
140                 {
141                     for (auto& kv : protocolToDBus)
142                     {
143                         if (kv.second.serviceName ==
144                             std::get<NET_PROTO_UNIT_NAME>(unit))
145                         {
146                             continue;
147                         }
148                         const char* service = kv.first;
149                         const char* socketPath = kv.second.socketPath;
150 
151                         asyncResp->res.jsonValue[service]["ProtocolEnabled"] =
152                             std::get<NET_PROTO_UNIT_SUB_STATE>(unit) ==
153                             "running";
154 
155                         crow::connections::systemBus->async_method_call(
156                             [asyncResp, service{std::string(service)},
157                              socketPath](
158                                 const boost::system::error_code ec,
159                                 const sdbusplus::message::variant<std::vector<
160                                     std::tuple<std::string, std::string>>>&
161                                     resp) {
162                                 if (ec)
163                                 {
164                                     messages::internalError(asyncResp->res);
165                                     return;
166                                 }
167                                 const std::vector<std::tuple<
168                                     std::string, std::string>>* responsePtr =
169                                     sdbusplus::message::variant_ns::get_if<
170                                         std::vector<std::tuple<std::string,
171                                                                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