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         Node::json["@odata.type"] =
77             "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol";
78         Node::json["@odata.id"] = "/redfish/v1/Managers/bmc/NetworkProtocol";
79         Node::json["@odata.context"] =
80             "/redfish/v1/"
81             "$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol";
82         Node::json["Id"] = "NetworkProtocol";
83         Node::json["Name"] = "Manager Network Protocol";
84         Node::json["Description"] = "Manager Network Service";
85         Node::json["Status"]["Health"] = "OK";
86         Node::json["Status"]["HealthRollup"] = "OK";
87         Node::json["Status"]["State"] = "Enabled";
88 
89         for (auto& protocol : protocolToDBus)
90         {
91             Node::json[protocol.first]["ProtocolEnabled"] = false;
92         }
93 
94         entityPrivileges = {
95             {boost::beast::http::verb::get, {{"Login"}}},
96             {boost::beast::http::verb::head, {{"Login"}}},
97             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
98             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
99             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
100             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
101     }
102 
103   private:
104     void doGet(crow::Response& res, const crow::Request& req,
105                const std::vector<std::string>& params) override
106     {
107         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
108 
109         getData(asyncResp);
110     }
111 
112     std::string getHostName() const
113     {
114         std::string hostName;
115 
116         std::array<char, HOST_NAME_MAX> hostNameCStr;
117         if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
118         {
119             hostName = hostNameCStr.data();
120         }
121         return hostName;
122     }
123 
124     void getData(const std::shared_ptr<AsyncResp>& asyncResp)
125     {
126         Node::json["HostName"] = getHostName();
127         asyncResp->res.jsonValue = Node::json;
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::addMessageToErrorJson(asyncResp->res.jsonValue,
136                                                     messages::internalError());
137                     asyncResp->res.result(
138                         boost::beast::http::status::internal_server_error);
139                     return;
140                 }
141 
142                 for (auto& unit : resp)
143                 {
144                     for (auto& kv : protocolToDBus)
145                     {
146                         if (kv.second.serviceName ==
147                             std::get<NET_PROTO_UNIT_NAME>(unit))
148                         {
149                             continue;
150                         }
151                         const char* service = kv.first;
152                         const char* socketPath = kv.second.socketPath;
153 
154                         asyncResp->res.jsonValue[service]["ProtocolEnabled"] =
155                             std::get<NET_PROTO_UNIT_SUB_STATE>(unit) ==
156                             "running";
157 
158                         crow::connections::systemBus->async_method_call(
159                             [asyncResp, service{std::string(service)},
160                              socketPath](
161                                 const boost::system::error_code ec,
162                                 const sdbusplus::message::variant<std::vector<
163                                     std::tuple<std::string, std::string>>>&
164                                     resp) {
165                                 if (ec)
166                                 {
167                                     messages::addMessageToJson(
168                                         asyncResp->res.jsonValue,
169                                         messages::internalError(),
170                                         "/" + service);
171                                     return;
172                                 }
173                                 const std::vector<std::tuple<
174                                     std::string, std::string>>* responsePtr =
175                                     mapbox::getPtr<const std::vector<
176                                         std::tuple<std::string, std::string>>>(
177                                         resp);
178                                 if (responsePtr == nullptr ||
179                                     responsePtr->size() < 1)
180                                 {
181                                     return;
182                                 }
183 
184                                 const std::string& listenStream =
185                                     std::get<NET_PROTO_LISTEN_STREAM>(
186                                         (*responsePtr)[0]);
187                                 std::size_t lastColonPos =
188                                     listenStream.rfind(":");
189                                 if (lastColonPos == std::string::npos)
190                                 {
191                                     // Not a port
192                                     return;
193                                 }
194                                 std::string portStr =
195                                     listenStream.substr(lastColonPos + 1);
196                                 char* endPtr = nullptr;
197                                 // Use strtol instead of stroi to avoid
198                                 // exceptions
199                                 long port =
200                                     std::strtol(portStr.c_str(), &endPtr, 10);
201 
202                                 if (*endPtr != '\0' || portStr.empty())
203                                 {
204                                     // Invalid value
205                                     asyncResp->res.jsonValue[service]["Port"] =
206                                         nullptr;
207                                 }
208                                 else
209                                 {
210                                     // Everything OK
211                                     asyncResp->res.jsonValue[service]["Port"] =
212                                         port;
213                                 }
214                             },
215                             "org.freedesktop.systemd1", socketPath,
216                             "org.freedesktop.DBus.Properties", "Get",
217                             "org.freedesktop.systemd1.Socket", "Listen");
218                     }
219                 }
220             },
221             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
222             "org.freedesktop.systemd1.Manager", "ListUnits");
223     }
224 };
225 
226 } // namespace redfish
227