17
17
18
18
#include " error_messages.hpp"
19
19
#include " openbmc_dbus_rest.hpp"
20
+ #include " redfish_util.hpp"
20
21
21
22
#include < app.hpp>
22
23
#include < registries/privilege_registry.hpp>
@@ -30,35 +31,7 @@ namespace redfish
30
31
void getNTPProtocolEnabled (const std::shared_ptr<bmcweb::AsyncResp>& asyncResp);
31
32
std::string getHostName ();
32
33
33
- enum NetworkProtocolUnitStructFields
34
- {
35
- NET_PROTO_UNIT_NAME,
36
- NET_PROTO_UNIT_DESC,
37
- NET_PROTO_UNIT_LOAD_STATE,
38
- NET_PROTO_UNIT_ACTIVE_STATE,
39
- NET_PROTO_UNIT_SUB_STATE,
40
- NET_PROTO_UNIT_DEVICE,
41
- NET_PROTO_UNIT_OBJ_PATH,
42
- NET_PROTO_UNIT_ALWAYS_0,
43
- NET_PROTO_UNIT_ALWAYS_EMPTY,
44
- NET_PROTO_UNIT_ALWAYS_ROOT_PATH
45
- };
46
-
47
- enum NetworkProtocolListenResponseElements
48
- {
49
- NET_PROTO_LISTEN_TYPE,
50
- NET_PROTO_LISTEN_STREAM
51
- };
52
-
53
- /* *
54
- * @brief D-Bus Unit structure returned in array from ListUnits Method
55
- */
56
- using UnitStruct =
57
- std::tuple<std::string, std::string, std::string, std::string, std::string,
58
- std::string, sdbusplus::message::object_path, uint32_t ,
59
- std::string, sdbusplus::message::object_path>;
60
-
61
- const static std::array<std::pair<const char *, const char *>, 3 > protocolToDBus{
34
+ const static std::array<std::pair<std::string, std::string>, 3 > protocolToDBus{
62
35
{{" SSH" , " dropbear" }, {" HTTPS" , " bmcweb" }, {" IPMI" , " phosphor-ipmi-net" }}};
63
36
64
37
inline void
@@ -191,114 +164,48 @@ inline void getNetworkData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
191
164
Privileges effectiveUserPrivileges =
192
165
redfish::getUserPrivileges (req.userRole );
193
166
194
- crow::connections::systemBus->async_method_call (
195
- [asyncResp,
196
- &effectiveUserPrivileges](const boost::system ::error_code e,
197
- const std::vector<UnitStruct>& r) {
198
- if (e)
199
- {
200
- asyncResp->res .jsonValue = nlohmann::json::object ();
201
- messages::internalError (asyncResp->res );
202
- return ;
203
- }
204
- // /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates is
205
- // something only ConfigureManager can access then only display when
206
- // the user has permissions ConfigureManager
207
- if (isOperationAllowedWithPrivileges ({{" ConfigureManager" }},
208
- effectiveUserPrivileges))
209
- {
210
- asyncResp->res .jsonValue [" HTTPS" ][" Certificates" ] = {
211
- {" @odata.id" , " /redfish/v1/Managers/bmc/NetworkProtocol/"
212
- " HTTPS/Certificates" }};
213
- }
214
- for (auto & unit : r)
215
- {
216
- /* Only traverse through <xyz>.socket units */
217
- const std::string& unitName =
218
- std::get<NET_PROTO_UNIT_NAME>(unit);
219
- if (!boost::ends_with (unitName, " .socket" ))
220
- {
221
- continue ;
222
- }
167
+ // /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates is
168
+ // something only ConfigureManager can access then only display when
169
+ // the user has permissions ConfigureManager
170
+ if (isOperationAllowedWithPrivileges ({{" ConfigureManager" }},
171
+ effectiveUserPrivileges))
172
+ {
173
+ asyncResp->res .jsonValue [" HTTPS" ][" Certificates" ] = {
174
+ {" @odata.id" ,
175
+ " /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates" }};
176
+ }
223
177
224
- for (auto & kv : protocolToDBus)
178
+ for (const auto & protocol : protocolToDBus)
179
+ {
180
+ const std::string& protocolName = protocol.first ;
181
+ const std::string& serviceName = protocol.second ;
182
+ getPortStatusAndPath (
183
+ serviceName,
184
+ [asyncResp, protocolName](const boost::system ::error_code ec,
185
+ const std::string& socketPath,
186
+ bool isProtocolEnabled) {
187
+ if (ec)
225
188
{
226
- // We are interested in services, which starts with
227
- // mapped service name
228
- if (!boost::starts_with (unitName, kv.second ))
229
- {
230
- continue ;
231
- }
232
- const char * rfServiceKey = kv.first ;
233
- const std::string& socketPath =
234
- std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
235
- const std::string& unitState =
236
- std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
237
-
238
- asyncResp->res .jsonValue [rfServiceKey][" ProtocolEnabled" ] =
239
- (unitState == " running" ) || (unitState == " listening" );
240
-
241
- crow::connections::systemBus->async_method_call (
242
- [asyncResp, rfServiceKey{std::string (rfServiceKey)}](
243
- const boost::system ::error_code ec,
244
- const std::variant<std::vector<
245
- std::tuple<std::string, std::string>>>& resp) {
246
- if (ec)
247
- {
248
- messages::internalError (asyncResp->res );
249
- return ;
250
- }
251
- const std::vector<
252
- std::tuple<std::string, std::string>>*
253
- responsePtr = std::get_if<std::vector<
254
- std::tuple<std::string, std::string>>>(
255
- &resp);
256
- if (responsePtr == nullptr ||
257
- responsePtr->size () < 1 )
258
- {
259
- return ;
260
- }
261
-
262
- const std::string& listenStream =
263
- std::get<NET_PROTO_LISTEN_STREAM>(
264
- (*responsePtr)[0 ]);
265
- std::size_t lastColonPos = listenStream.rfind (' :' );
266
- if (lastColonPos == std::string::npos)
267
- {
268
- // Not a port
269
- return ;
270
- }
271
- std::string portStr =
272
- listenStream.substr (lastColonPos + 1 );
273
- if (portStr.empty ())
274
- {
275
- return ;
276
- }
277
- char * endPtr = nullptr ;
278
- errno = 0 ;
279
- // Use strtol instead of stroi to avoid
280
- // exceptions
281
- long port =
282
- std::strtol (portStr.c_str (), &endPtr, 10 );
283
- if ((errno == 0 ) && (*endPtr == ' \0 ' ))
284
- {
285
- asyncResp->res .jsonValue [rfServiceKey][" Port" ] =
286
- port;
287
- }
288
- return ;
289
- },
290
- " org.freedesktop.systemd1" , socketPath,
291
- " org.freedesktop.DBus.Properties" , " Get" ,
292
- " org.freedesktop.systemd1.Socket" , " Listen" );
293
-
294
- // We found service, break the inner loop.
295
- break ;
189
+ messages::internalError (asyncResp->res );
190
+ return ;
296
191
}
297
- }
298
- },
299
- " org.freedesktop.systemd1" , " /org/freedesktop/systemd1" ,
300
- " org.freedesktop.systemd1.Manager" , " ListUnits" );
301
- }
192
+ asyncResp->res .jsonValue [protocolName][" ProtocolEnabled" ] =
193
+ isProtocolEnabled;
194
+ getPortNumber (
195
+ socketPath,
196
+ [asyncResp, protocolName](
197
+ const boost::system ::error_code ec, int portNumber) {
198
+ if (ec)
199
+ {
200
+ messages::internalError (asyncResp->res );
201
+ return ;
202
+ }
203
+ asyncResp->res .jsonValue [protocolName][" Port" ] =
204
+ portNumber;
205
+ });
206
+ });
207
+ }
208
+ } // namespace redfish
302
209
303
210
#ifdef BMCWEB_ALLOW_DEPRECATED_HOSTNAME_PATCH
304
211
inline void
0 commit comments