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::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 mapbox::getPtr<const 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