blob: 61eaa6e8c74aef2f961ad2d7c740437cec1280d4 [file] [log] [blame]
James Feist5b4aa862018-08-16 14:07:01 -07001// Copyright (c) 2018 Intel Corporation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Ed Tanousb9b2e0b2018-09-13 13:47:50 -070014
James Feist5b4aa862018-08-16 14:07:01 -070015#pragma once
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080016#include "app.hpp"
17#include "async_resp.hpp"
18#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
Nan Zhoud5c80ad2022-07-11 01:16:31 +000020#include "http_request.hpp"
21#include "http_response.hpp"
22#include "logging.hpp"
23#include "routing.hpp"
24
25#include <systemd/sd-bus-protocol.h>
26#include <systemd/sd-bus.h>
Ed Tanous911ac312017-08-15 09:37:42 -070027#include <tinyxml2.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -070028
Ed Tanous11ba3972022-07-11 09:50:41 -070029#include <boost/algorithm/string/classification.hpp>
30#include <boost/algorithm/string/predicate.hpp>
31#include <boost/algorithm/string/split.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000032#include <boost/beast/http/status.hpp>
33#include <boost/beast/http/verb.hpp>
34#include <boost/container/flat_map.hpp>
35#include <boost/container/vector.hpp>
36#include <boost/iterator/iterator_facade.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000037#include <nlohmann/json.hpp>
38#include <sdbusplus/asio/connection.hpp>
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +020039#include <sdbusplus/asio/property.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000040#include <sdbusplus/exception.hpp>
41#include <sdbusplus/message.hpp>
42#include <sdbusplus/message/native_types.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050043
Nan Zhoud5c80ad2022-07-11 01:16:31 +000044#include <algorithm>
45#include <array>
46#include <cerrno>
47#include <cstdint>
48#include <cstring>
James Feist4418c7f2019-04-15 11:09:15 -070049#include <filesystem>
Ed Tanousd4bb9bb2018-05-16 13:36:42 -070050#include <fstream>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000051#include <functional>
52#include <initializer_list>
53#include <iterator>
54#include <limits>
55#include <map>
56#include <memory>
Ramesh Iyyard9207042019-07-05 08:04:42 -050057#include <regex>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000058#include <string>
59#include <string_view>
60#include <type_traits>
Ed Tanousb5a76932020-09-29 16:16:58 -070061#include <utility>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000062#include <variant>
63#include <vector>
64
65// IWYU pragma: no_include <boost/algorithm/string/detail/classification.hpp>
66// IWYU pragma: no_include <boost/system/detail/error_code.hpp>
67// IWYU pragma: no_include <boost/system/detail/error_category.hpp>
68// IWYU pragma: no_include <errno.h>
69// IWYU pragma: no_include <string.h>
70// IWYU pragma: no_include <ext/alloc_traits.h>
71// IWYU pragma: no_include <exception>
72// IWYU pragma: no_include <boost/type_index/type_index_facade.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070073
Ed Tanous1abe55e2018-09-05 08:30:59 -070074namespace crow
75{
76namespace openbmc_mapper
77{
Ed Tanous23a21a12020-07-25 04:45:05 +000078const constexpr char* notFoundMsg = "404 Not Found";
79const constexpr char* badReqMsg = "400 Bad Request";
80const constexpr char* methodNotAllowedMsg = "405 Method Not Allowed";
81const constexpr char* forbiddenMsg = "403 Forbidden";
82const constexpr char* methodFailedMsg = "500 Method Call Failed";
83const constexpr char* methodOutputFailedMsg = "500 Method Output Error";
84const constexpr char* notFoundDesc =
Matt Spinler2ae60092018-12-06 10:35:36 -060085 "org.freedesktop.DBus.Error.FileNotFound: path or object not found";
Ed Tanous23a21a12020-07-25 04:45:05 +000086const constexpr char* propNotFoundDesc =
87 "The specified property cannot be found";
88const constexpr char* noJsonDesc = "No JSON object could be decoded";
89const constexpr char* methodNotFoundDesc =
90 "The specified method cannot be found";
91const constexpr char* methodNotAllowedDesc = "Method not allowed";
92const constexpr char* forbiddenPropDesc =
93 "The specified property cannot be created";
94const constexpr char* forbiddenResDesc =
95 "The specified resource cannot be created";
Matt Spinler2ae60092018-12-06 10:35:36 -060096
Josh Lehan482c45a2022-03-29 17:10:44 -070097inline bool validateFilename(const std::string& filename)
98{
99 std::regex validFilename(R"(^[\w\- ]+(\.?[\w\- ]*)$)");
100
101 return std::regex_match(filename, validFilename);
102}
103
Ed Tanous23a21a12020-07-25 04:45:05 +0000104inline void setErrorResponse(crow::Response& res,
105 boost::beast::http::status result,
106 const std::string& desc,
107 const std::string_view msg)
Matt Spinler2ae60092018-12-06 10:35:36 -0600108{
109 res.result(result);
Ed Tanous14766872022-03-15 10:44:42 -0700110 res.jsonValue["data"]["description"] = desc;
111 res.jsonValue["message"] = msg;
112 res.jsonValue["status"] = "error";
Matt Spinler2ae60092018-12-06 10:35:36 -0600113}
114
Ed Tanousb5a76932020-09-29 16:16:58 -0700115inline void
116 introspectObjects(const std::string& processName,
117 const std::string& objectPath,
118 const std::shared_ptr<bmcweb::AsyncResp>& transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119{
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700120 if (transaction->res.jsonValue.is_null())
121 {
Ed Tanous14766872022-03-15 10:44:42 -0700122 transaction->res.jsonValue["status"] = "ok";
123 transaction->res.jsonValue["bus_name"] = processName;
124 transaction->res.jsonValue["objects"] = nlohmann::json::array();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700125 }
126
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700128 [transaction, processName{std::string(processName)},
129 objectPath{std::string(objectPath)}](
130 const boost::system::error_code ec,
Ed Tanous81ce6092020-12-17 16:54:55 +0000131 const std::string& introspectXml) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 if (ec)
133 {
134 BMCWEB_LOG_ERROR
135 << "Introspect call failed with error: " << ec.message()
136 << " on process: " << processName << " path: " << objectPath
137 << "\n";
138 return;
139 }
140 nlohmann::json::object_t object;
141 object["path"] = objectPath;
Ed Tanous14766872022-03-15 10:44:42 -0700142
Ed Tanous002d39b2022-05-31 08:59:27 -0700143 transaction->res.jsonValue["objects"].push_back(std::move(object));
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700144
Ed Tanous002d39b2022-05-31 08:59:27 -0700145 tinyxml2::XMLDocument doc;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700146
Ed Tanous002d39b2022-05-31 08:59:27 -0700147 doc.Parse(introspectXml.c_str());
148 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
149 if (pRoot == nullptr)
150 {
151 BMCWEB_LOG_ERROR << "XML document failed to parse " << processName
152 << " " << objectPath << "\n";
153 }
154 else
155 {
156 tinyxml2::XMLElement* node = pRoot->FirstChildElement("node");
157 while (node != nullptr)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700158 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700159 const char* childPath = node->Attribute("name");
160 if (childPath != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700162 std::string newpath;
163 if (objectPath != "/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700164 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700165 newpath += objectPath;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700167 newpath += std::string("/") + childPath;
168 // introspect the subobjects as well
169 introspectObjects(processName, newpath, transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700171
172 node = node->NextSiblingElement("node");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700174 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700176 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700177 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -0700178}
Ed Tanous64530012018-02-06 17:08:16 -0800179
Ed Tanous23a21a12020-07-25 04:45:05 +0000180inline void getPropertiesForEnumerate(
181 const std::string& objectPath, const std::string& service,
Ed Tanousb5a76932020-09-29 16:16:58 -0700182 const std::string& interface,
183 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600184{
185 BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " "
186 << service << " " << interface;
187
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200188 sdbusplus::asio::getAllProperties(
189 *crow::connections::systemBus, service, objectPath, interface,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800190 [asyncResp, objectPath, service,
191 interface](const boost::system::error_code ec,
192 const dbus::utility::DBusPropertiesMap& propertiesList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700193 if (ec)
194 {
195 BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface "
196 << interface << " service " << service
197 << " failed with code " << ec;
198 return;
199 }
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600200
Ed Tanous002d39b2022-05-31 08:59:27 -0700201 nlohmann::json& dataJson = asyncResp->res.jsonValue["data"];
202 nlohmann::json& objectJson = dataJson[objectPath];
203 if (objectJson.is_null())
204 {
205 objectJson = nlohmann::json::object();
206 }
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600207
Ed Tanous002d39b2022-05-31 08:59:27 -0700208 for (const auto& [name, value] : propertiesList)
209 {
210 nlohmann::json& propertyJson = objectJson[name];
211 std::visit(
212 [&propertyJson](auto&& val) {
213 if constexpr (std::is_same_v<std::decay_t<decltype(val)>,
214 sdbusplus::message::unix_fd>)
215 {
216 propertyJson = val.fd;
217 }
218 else
219 {
Ed Tanousd1a64812021-12-13 12:14:05 -0800220
Ed Tanous002d39b2022-05-31 08:59:27 -0700221 propertyJson = val;
222 }
223 },
224 value);
225 }
Krzysztof Grobelnyc1343bf2022-08-31 13:15:26 +0200226 });
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600227}
228
229// Find any results that weren't picked up by ObjectManagers, to be
230// called after all ObjectManagers are searched for and called.
Ed Tanous23a21a12020-07-25 04:45:05 +0000231inline void findRemainingObjectsForEnumerate(
Ed Tanousb5a76932020-09-29 16:16:58 -0700232 const std::string& objectPath,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800233 const std::shared_ptr<dbus::utility::MapperGetSubTreeResponse>& subtree,
Ed Tanousb5a76932020-09-29 16:16:58 -0700234 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600235{
236 BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate";
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500237 const nlohmann::json& dataJson = asyncResp->res.jsonValue["data"];
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600238
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500239 for (const auto& [path, interface_map] : *subtree)
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600240 {
241 if (path == objectPath)
242 {
243 // An enumerate does not return the target path's properties
244 continue;
245 }
246 if (dataJson.find(path) == dataJson.end())
247 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500248 for (const auto& [service, interfaces] : interface_map)
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600249 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500250 for (const auto& interface : interfaces)
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600251 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700252 if (!interface.starts_with("org.freedesktop.DBus"))
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600253 {
254 getPropertiesForEnumerate(path, service, interface,
255 asyncResp);
256 }
257 }
258 }
259 }
260 }
261}
262
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600263struct InProgressEnumerateData
264{
zhanghch058d1b46d2021-04-01 11:18:24 +0800265 InProgressEnumerateData(
266 const std::string& objectPathIn,
267 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +0000268 objectPath(objectPathIn),
zhanghch058d1b46d2021-04-01 11:18:24 +0800269 asyncResp(asyncRespIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500270 {}
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600271
272 ~InProgressEnumerateData()
273 {
Ed Tanous24b2fe82022-01-06 12:45:54 -0800274 try
275 {
276 findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp);
277 }
278 catch (...)
279 {
280 BMCWEB_LOG_CRITICAL
281 << "findRemainingObjectsForEnumerate threw exception";
282 }
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600283 }
284
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800285 InProgressEnumerateData(const InProgressEnumerateData&) = delete;
286 InProgressEnumerateData(InProgressEnumerateData&&) = delete;
287 InProgressEnumerateData& operator=(const InProgressEnumerateData&) = delete;
288 InProgressEnumerateData& operator=(InProgressEnumerateData&&) = delete;
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600289 const std::string objectPath;
Ed Tanousb9d36b42022-02-26 21:42:46 -0800290 std::shared_ptr<dbus::utility::MapperGetSubTreeResponse> subtree;
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600291 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
292};
293
Ed Tanous23a21a12020-07-25 04:45:05 +0000294inline void getManagedObjectsForEnumerate(
Ed Tanous81ce6092020-12-17 16:54:55 +0000295 const std::string& objectName, const std::string& objectManagerPath,
296 const std::string& connectionName,
Ed Tanousb5a76932020-09-29 16:16:58 -0700297 const std::shared_ptr<InProgressEnumerateData>& transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700298{
Ed Tanous81ce6092020-12-17 16:54:55 +0000299 BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << objectName
300 << " object_manager_path " << objectManagerPath
301 << " connection_name " << connectionName;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700302 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +0000303 [transaction, objectName,
304 connectionName](const boost::system::error_code ec,
305 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700306 if (ec)
307 {
308 BMCWEB_LOG_ERROR << "GetManagedObjects on path " << objectName
309 << " on connection " << connectionName
310 << " failed with code " << ec;
311 return;
312 }
Ed Tanous64530012018-02-06 17:08:16 -0800313
Ed Tanous002d39b2022-05-31 08:59:27 -0700314 nlohmann::json& dataJson =
315 transaction->asyncResp->res.jsonValue["data"];
Ed Tanous049a0512018-11-01 13:58:42 -0700316
Ed Tanous002d39b2022-05-31 08:59:27 -0700317 for (const auto& objectPath : objects)
318 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700319 if (objectPath.first.str.starts_with(objectName))
Ed Tanous049a0512018-11-01 13:58:42 -0700320 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700321 BMCWEB_LOG_DEBUG << "Reading object " << objectPath.first.str;
322 nlohmann::json& objectJson = dataJson[objectPath.first.str];
323 if (objectJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700324 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700325 objectJson = nlohmann::json::object();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700326 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500327 for (const auto& interface : objectPath.second)
Ed Tanous049a0512018-11-01 13:58:42 -0700328 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700329 for (const auto& property : interface.second)
Ed Tanous049a0512018-11-01 13:58:42 -0700330 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700331 nlohmann::json& propertyJson =
332 objectJson[property.first];
333 std::visit(
334 [&propertyJson](auto&& val) {
335 if constexpr (std::is_same_v<
336 std::decay_t<decltype(val)>,
337 sdbusplus::message::unix_fd>)
338 {
339 propertyJson = val.fd;
340 }
341 else
342 {
343
344 propertyJson = val;
345 }
346 },
347 property.second);
Ed Tanous049a0512018-11-01 13:58:42 -0700348 }
349 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700351 for (const auto& interface : objectPath.second)
352 {
353 if (interface.first == "org.freedesktop.DBus.ObjectManager")
354 {
355 getManagedObjectsForEnumerate(objectPath.first.str,
356 objectPath.first.str,
357 connectionName, transaction);
358 }
359 }
360 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700361 },
Ed Tanous81ce6092020-12-17 16:54:55 +0000362 connectionName, objectManagerPath, "org.freedesktop.DBus.ObjectManager",
363 "GetManagedObjects");
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700364}
365
Ed Tanous23a21a12020-07-25 04:45:05 +0000366inline void findObjectManagerPathForEnumerate(
Ed Tanous81ce6092020-12-17 16:54:55 +0000367 const std::string& objectName, const std::string& connectionName,
Ed Tanousb5a76932020-09-29 16:16:58 -0700368 const std::shared_ptr<InProgressEnumerateData>& transaction)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700369{
Ed Tanous81ce6092020-12-17 16:54:55 +0000370 BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << objectName
371 << " on connection:" << connectionName;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700372 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +0000373 [transaction, objectName, connectionName](
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700374 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800375 const dbus::utility::MapperGetAncestorsResponse& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700376 if (ec)
377 {
378 BMCWEB_LOG_ERROR << "GetAncestors on path " << objectName
379 << " failed with code " << ec;
380 return;
381 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700382
Ed Tanous002d39b2022-05-31 08:59:27 -0700383 for (const auto& pathGroup : objects)
384 {
385 for (const auto& connectionGroup : pathGroup.second)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700386 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700387 if (connectionGroup.first == connectionName)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700388 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700389 // Found the object manager path for this resource.
390 getManagedObjectsForEnumerate(objectName, pathGroup.first,
391 connectionName, transaction);
392 return;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700393 }
394 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700395 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700396 },
397 "xyz.openbmc_project.ObjectMapper",
398 "/xyz/openbmc_project/object_mapper",
Ed Tanous81ce6092020-12-17 16:54:55 +0000399 "xyz.openbmc_project.ObjectMapper", "GetAncestors", objectName,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500400 std::array<const char*, 1>{"org.freedesktop.DBus.ObjectManager"});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700401}
Ed Tanous64530012018-02-06 17:08:16 -0800402
Ed Tanous7c091622019-05-23 11:42:36 -0700403// Uses GetObject to add the object info about the target /enumerate path to
404// the results of GetSubTree, as GetSubTree will not return info for the
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600405// target path, and then continues on enumerating the rest of the tree.
Ed Tanousb5a76932020-09-29 16:16:58 -0700406inline void getObjectAndEnumerate(
407 const std::shared_ptr<InProgressEnumerateData>& transaction)
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600408{
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600409 crow::connections::systemBus->async_method_call(
410 [transaction](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800411 const dbus::utility::MapperGetObject& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700412 if (ec)
413 {
414 BMCWEB_LOG_ERROR << "GetObject for path " << transaction->objectPath
415 << " failed with code " << ec;
416 return;
417 }
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600418
Ed Tanous002d39b2022-05-31 08:59:27 -0700419 BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath
420 << " has " << objects.size() << " entries";
421 if (!objects.empty())
422 {
423 transaction->subtree->emplace_back(transaction->objectPath,
424 objects);
425 }
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600426
Ed Tanous002d39b2022-05-31 08:59:27 -0700427 // Map indicating connection name, and the path where the object
428 // manager exists
429 boost::container::flat_map<std::string, std::string> connections;
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600430
Ed Tanous002d39b2022-05-31 08:59:27 -0700431 for (const auto& object : *(transaction->subtree))
432 {
433 for (const auto& connection : object.second)
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600434 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700435 for (const auto& interface : connection.second)
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600436 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700437 BMCWEB_LOG_DEBUG << connection.first << " has interface "
438 << interface;
439 if (interface == "org.freedesktop.DBus.ObjectManager")
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600440 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700441 BMCWEB_LOG_DEBUG << "found object manager path "
442 << object.first;
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700443 connections[connection.first] = object.first;
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600444 }
445 }
446 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700447 }
448 BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections";
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600449
Ed Tanous002d39b2022-05-31 08:59:27 -0700450 for (const auto& connection : connections)
451 {
452 // If we already know where the object manager is, we don't
453 // need to search for it, we can call directly in to
454 // getManagedObjects
455 if (!connection.second.empty())
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600456 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700457 getManagedObjectsForEnumerate(transaction->objectPath,
458 connection.second,
459 connection.first, transaction);
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600460 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700461 else
462 {
463 // otherwise we need to find the object manager path
464 // before we can continue
465 findObjectManagerPathForEnumerate(
466 transaction->objectPath, connection.first, transaction);
467 }
468 }
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600469 },
470 "xyz.openbmc_project.ObjectMapper",
471 "/xyz/openbmc_project/object_mapper",
472 "xyz.openbmc_project.ObjectMapper", "GetObject",
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500473 transaction->objectPath, std::array<const char*, 0>());
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600474}
Ed Tanous64530012018-02-06 17:08:16 -0800475
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700476// Structure for storing data on an in progress action
Ed Tanous1abe55e2018-09-05 08:30:59 -0700477struct InProgressActionData
478{
Ed Tanous4e23a442022-06-06 09:57:26 -0700479 explicit InProgressActionData(crow::Response& resIn) : res(resIn)
Ed Tanous23a21a12020-07-25 04:45:05 +0000480 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700481 ~InProgressActionData()
482 {
Matt Spinler16caaee2019-01-15 11:40:34 -0600483 // Methods could have been called across different owners
484 // and interfaces, where some calls failed and some passed.
485 //
486 // The rules for this are:
487 // * if no method was called - error
488 // * if a method failed and none passed - error
489 // (converse: if at least one method passed - OK)
490 // * for the method output:
491 // * if output processing didn't fail, return the data
492
493 // Only deal with method returns if nothing failed earlier
494 if (res.result() == boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700495 {
Matt Spinler16caaee2019-01-15 11:40:34 -0600496 if (!methodPassed)
497 {
Matt Spinler06b1b632019-06-18 16:09:25 -0500498 if (!methodFailed)
Matt Spinler16caaee2019-01-15 11:40:34 -0600499 {
500 setErrorResponse(res, boost::beast::http::status::not_found,
501 methodNotFoundDesc, notFoundMsg);
502 }
503 }
504 else
505 {
506 if (outputFailed)
507 {
508 setErrorResponse(
509 res, boost::beast::http::status::internal_server_error,
510 "Method output failure", methodOutputFailedMsg);
511 }
512 else
513 {
Ed Tanous14766872022-03-15 10:44:42 -0700514 res.jsonValue["status"] = "ok";
515 res.jsonValue["message"] = "200 OK";
516 res.jsonValue["data"] = methodResponse;
Matt Spinler16caaee2019-01-15 11:40:34 -0600517 }
518 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700519 }
Matt Spinler16caaee2019-01-15 11:40:34 -0600520
Ed Tanous1abe55e2018-09-05 08:30:59 -0700521 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700522 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800523 InProgressActionData(const InProgressActionData&) = delete;
524 InProgressActionData(InProgressActionData&&) = delete;
525 InProgressActionData& operator=(const InProgressActionData&) = delete;
526 InProgressActionData& operator=(InProgressActionData&&) = delete;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700527
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500528 void setErrorStatus(const std::string& desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 {
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -0600530 setErrorResponse(res, boost::beast::http::status::bad_request, desc,
531 badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500533 crow::Response& res;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700534 std::string path;
535 std::string methodName;
Matt Spinlerde818812018-12-11 16:39:20 -0600536 std::string interfaceName;
Matt Spinler16caaee2019-01-15 11:40:34 -0600537 bool methodPassed = false;
538 bool methodFailed = false;
539 bool outputFailed = false;
Matt Spinler39a4e392019-01-15 11:53:13 -0600540 bool convertedToArray = false;
Matt Spinler16caaee2019-01-15 11:40:34 -0600541 nlohmann::json methodResponse;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542 nlohmann::json arguments;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700543};
544
Ed Tanous23a21a12020-07-25 04:45:05 +0000545inline std::vector<std::string> dbusArgSplit(const std::string& string)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546{
547 std::vector<std::string> ret;
548 if (string.empty())
549 {
550 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700551 }
Ed Tanous0f0353b2019-10-24 11:37:51 -0700552 ret.emplace_back("");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 int containerDepth = 0;
554
555 for (std::string::const_iterator character = string.begin();
556 character != string.end(); character++)
557 {
558 ret.back() += *character;
559 switch (*character)
560 {
561 case ('a'):
562 break;
563 case ('('):
564 case ('{'):
565 containerDepth++;
566 break;
567 case ('}'):
568 case (')'):
569 containerDepth--;
570 if (containerDepth == 0)
571 {
572 if (character + 1 != string.end())
573 {
Ed Tanous0f0353b2019-10-24 11:37:51 -0700574 ret.emplace_back("");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700575 }
576 }
577 break;
578 default:
579 if (containerDepth == 0)
580 {
581 if (character + 1 != string.end())
582 {
Ed Tanous0f0353b2019-10-24 11:37:51 -0700583 ret.emplace_back("");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700584 }
585 }
586 break;
587 }
588 }
Matt Spinler4ae611d2019-01-11 15:37:06 -0600589
590 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700591}
592
Ed Tanous81ce6092020-12-17 16:54:55 +0000593inline int convertJsonToDbus(sd_bus_message* m, const std::string& argType,
594 const nlohmann::json& inputJson)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700595{
596 int r = 0;
Ed Tanous71f52d92021-02-19 08:51:17 -0800597 BMCWEB_LOG_DEBUG << "Converting "
598 << inputJson.dump(2, ' ', true,
599 nlohmann::json::error_handler_t::replace)
Ed Tanous81ce6092020-12-17 16:54:55 +0000600 << " to type: " << argType;
601 const std::vector<std::string> argTypes = dbusArgSplit(argType);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700602
Ed Tanous1abe55e2018-09-05 08:30:59 -0700603 // Assume a single object for now.
Ed Tanous81ce6092020-12-17 16:54:55 +0000604 const nlohmann::json* j = &inputJson;
605 nlohmann::json::const_iterator jIt = inputJson.begin();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700606
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500607 for (const std::string& argCode : argTypes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608 {
609 // If we are decoding multiple objects, grab the pointer to the
610 // iterator, and increment it for the next loop
611 if (argTypes.size() > 1)
612 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000613 if (jIt == inputJson.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700614 {
615 return -2;
616 }
617 j = &*jIt;
618 jIt++;
619 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500620 const int64_t* intValue = j->get_ptr<const int64_t*>();
621 const std::string* stringValue = j->get_ptr<const std::string*>();
622 const double* doubleValue = j->get_ptr<const double*>();
623 const bool* b = j->get_ptr<const bool*>();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700624 int64_t v = 0;
625 double d = 0.0;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700626
Ed Tanous1abe55e2018-09-05 08:30:59 -0700627 // Do some basic type conversions that make sense. uint can be
628 // converted to int. int and uint can be converted to double
Ed Tanous66664f22019-10-11 13:05:49 -0700629 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700630 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500631 const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
Ed Tanous66664f22019-10-11 13:05:49 -0700632 if (uintValue != nullptr)
633 {
634 v = static_cast<int64_t>(*uintValue);
635 intValue = &v;
636 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637 }
Ed Tanous66664f22019-10-11 13:05:49 -0700638 if (doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700639 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500640 const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
Ed Tanous66664f22019-10-11 13:05:49 -0700641 if (uintValue != nullptr)
642 {
643 d = static_cast<double>(*uintValue);
644 doubleValue = &d;
645 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646 }
Ed Tanous66664f22019-10-11 13:05:49 -0700647 if (doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700648 {
Ed Tanous66664f22019-10-11 13:05:49 -0700649 if (intValue != nullptr)
650 {
651 d = static_cast<double>(*intValue);
652 doubleValue = &d;
653 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700654 }
655
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700656 if (argCode == "s")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700658 if (stringValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 {
660 return -1;
661 }
Ed Tanous271584a2019-07-09 16:24:22 -0700662 r = sd_bus_message_append_basic(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500663 m, argCode[0], static_cast<const void*>(stringValue->data()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 if (r < 0)
665 {
666 return r;
667 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700668 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700669 else if (argCode == "i")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700670 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700671 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700672 {
673 return -1;
674 }
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500675 if ((*intValue < std::numeric_limits<int32_t>::lowest()) ||
676 (*intValue > std::numeric_limits<int32_t>::max()))
677 {
678 return -ERANGE;
679 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700680 int32_t i = static_cast<int32_t>(*intValue);
681 r = sd_bus_message_append_basic(m, argCode[0], &i);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700682 if (r < 0)
683 {
684 return r;
685 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700686 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700687 else if (argCode == "b")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688 {
689 // lots of ways bool could be represented here. Try them all
Ed Tanouse662eae2022-01-25 10:39:19 -0800690 int boolInt = 0;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700691 if (intValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700692 {
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500693 if (*intValue == 1)
694 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800695 boolInt = 1;
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500696 }
697 else if (*intValue == 0)
698 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800699 boolInt = 0;
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500700 }
701 else
702 {
703 return -ERANGE;
704 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700705 }
706 else if (b != nullptr)
707 {
Matt Spinlera2f02632019-01-18 10:15:35 -0600708 boolInt = *b ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700709 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700710 else if (stringValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700711 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700712 boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700713 }
714 else
715 {
716 return -1;
717 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700718 r = sd_bus_message_append_basic(m, argCode[0], &boolInt);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700719 if (r < 0)
720 {
721 return r;
722 }
723 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700724 else if (argCode == "n")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700725 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700726 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700727 {
728 return -1;
729 }
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500730 if ((*intValue < std::numeric_limits<int16_t>::lowest()) ||
731 (*intValue > std::numeric_limits<int16_t>::max()))
732 {
733 return -ERANGE;
734 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700735 int16_t n = static_cast<int16_t>(*intValue);
736 r = sd_bus_message_append_basic(m, argCode[0], &n);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700737 if (r < 0)
738 {
739 return r;
740 }
741 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700742 else if (argCode == "x")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700743 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700744 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700745 {
746 return -1;
747 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700748 r = sd_bus_message_append_basic(m, argCode[0], intValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700749 if (r < 0)
750 {
751 return r;
752 }
753 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700754 else if (argCode == "y")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700755 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500756 const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700757 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700758 {
759 return -1;
760 }
Ed Tanous23a21a12020-07-25 04:45:05 +0000761 if (*uintValue > std::numeric_limits<uint8_t>::max())
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500762 {
763 return -ERANGE;
764 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700765 uint8_t y = static_cast<uint8_t>(*uintValue);
766 r = sd_bus_message_append_basic(m, argCode[0], &y);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700767 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700768 else if (argCode == "q")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700769 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500770 const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700771 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700772 {
773 return -1;
774 }
Ed Tanous23a21a12020-07-25 04:45:05 +0000775 if (*uintValue > std::numeric_limits<uint16_t>::max())
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500776 {
777 return -ERANGE;
778 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700779 uint16_t q = static_cast<uint16_t>(*uintValue);
780 r = sd_bus_message_append_basic(m, argCode[0], &q);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700781 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700782 else if (argCode == "u")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700783 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500784 const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700785 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700786 {
787 return -1;
788 }
Ed Tanous23a21a12020-07-25 04:45:05 +0000789 if (*uintValue > std::numeric_limits<uint32_t>::max())
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500790 {
791 return -ERANGE;
792 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700793 uint32_t u = static_cast<uint32_t>(*uintValue);
794 r = sd_bus_message_append_basic(m, argCode[0], &u);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700795 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700796 else if (argCode == "t")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700797 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500798 const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700799 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800 {
801 return -1;
802 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700803 r = sd_bus_message_append_basic(m, argCode[0], uintValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700804 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700805 else if (argCode == "d")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700806 {
Adriana Kobylakc66c8592019-08-06 15:08:05 -0500807 if (doubleValue == nullptr)
808 {
809 return -1;
810 }
811 if ((*doubleValue < std::numeric_limits<double>::lowest()) ||
812 (*doubleValue > std::numeric_limits<double>::max()))
813 {
814 return -ERANGE;
815 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700816 sd_bus_message_append_basic(m, argCode[0], doubleValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700817 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700818 else if (argCode.starts_with("a"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700820 std::string containedType = argCode.substr(1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700821 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700822 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823 if (r < 0)
824 {
825 return r;
826 }
827
Ed Tanous0dfeda62019-10-24 11:21:38 -0700828 for (const auto& it : *j)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700829 {
Ed Tanous0dfeda62019-10-24 11:21:38 -0700830 r = convertJsonToDbus(m, containedType, it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700831 if (r < 0)
832 {
833 return r;
834 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700835 }
836 sd_bus_message_close_container(m);
837 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700838 else if (argCode.starts_with("v"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700839 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700840 std::string containedType = argCode.substr(1);
841 BMCWEB_LOG_DEBUG << "variant type: " << argCode
842 << " appending variant of type: " << containedType;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700843 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700844 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845 if (r < 0)
846 {
847 return r;
848 }
849
Ed Tanous81ce6092020-12-17 16:54:55 +0000850 r = convertJsonToDbus(m, containedType, inputJson);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700851 if (r < 0)
852 {
853 return r;
854 }
855
856 r = sd_bus_message_close_container(m);
857 if (r < 0)
858 {
859 return r;
860 }
861 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700862 else if (argCode.starts_with("(") && argCode.ends_with(")"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700863 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700864 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700865 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700866 containedType.c_str());
Ed Tanousf1eebf02019-03-04 15:57:09 -0800867 if (r < 0)
868 {
869 return r;
870 }
871
Ed Tanous1abe55e2018-09-05 08:30:59 -0700872 nlohmann::json::const_iterator it = j->begin();
Ed Tanous81ce6092020-12-17 16:54:55 +0000873 for (const std::string& argCode2 : dbusArgSplit(argType))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700874 {
875 if (it == j->end())
876 {
877 return -1;
878 }
Ed Tanouscb13a392020-07-25 19:02:03 +0000879 r = convertJsonToDbus(m, argCode2, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700880 if (r < 0)
881 {
882 return r;
883 }
884 it++;
885 }
886 r = sd_bus_message_close_container(m);
887 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700888 else if (argCode.starts_with("{") && argCode.ends_with("}"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700889 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700890 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700891 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700892 containedType.c_str());
Ed Tanousf1eebf02019-03-04 15:57:09 -0800893 if (r < 0)
894 {
895 return r;
896 }
897
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700898 std::vector<std::string> codes = dbusArgSplit(containedType);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700899 if (codes.size() != 2)
900 {
901 return -1;
902 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700903 const std::string& keyType = codes[0];
904 const std::string& valueType = codes[1];
Ed Tanous3174e4d2020-10-07 11:41:22 -0700905 for (const auto& it : j->items())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700906 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700907 r = convertJsonToDbus(m, keyType, it.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700908 if (r < 0)
909 {
910 return r;
911 }
912
Ed Tanous2c70f802020-09-28 14:29:23 -0700913 r = convertJsonToDbus(m, valueType, it.value());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700914 if (r < 0)
915 {
916 return r;
917 }
918 }
919 r = sd_bus_message_close_container(m);
920 }
921 else
922 {
923 return -2;
924 }
925 if (r < 0)
926 {
927 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700928 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700929
Ed Tanous1abe55e2018-09-05 08:30:59 -0700930 if (argTypes.size() > 1)
931 {
932 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700933 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700934 }
Matt Spinler127ea542019-01-14 11:04:28 -0600935
936 return r;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700937}
938
Matt Spinlerd22a7132019-01-14 12:14:30 -0600939template <typename T>
Patrick Williams59d494e2022-07-22 19:26:55 -0500940int readMessageItem(const std::string& typeCode, sdbusplus::message_t& m,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500941 nlohmann::json& data)
Matt Spinlerd22a7132019-01-14 12:14:30 -0600942{
943 T value;
944
945 int r = sd_bus_message_read_basic(m.get(), typeCode.front(), &value);
946 if (r < 0)
947 {
948 BMCWEB_LOG_ERROR << "sd_bus_message_read_basic on type " << typeCode
949 << " failed!";
950 return r;
951 }
952
953 data = value;
954 return 0;
955}
956
Patrick Williams59d494e2022-07-22 19:26:55 -0500957int convertDBusToJSON(const std::string& returnType, sdbusplus::message_t& m,
958 nlohmann::json& response);
Matt Spinler6df8f992019-01-14 12:47:47 -0600959
Ed Tanous23a21a12020-07-25 04:45:05 +0000960inline int readDictEntryFromMessage(const std::string& typeCode,
Patrick Williams59d494e2022-07-22 19:26:55 -0500961 sdbusplus::message_t& m,
Ed Tanous23a21a12020-07-25 04:45:05 +0000962 nlohmann::json& object)
Matt Spinler6df8f992019-01-14 12:47:47 -0600963{
964 std::vector<std::string> types = dbusArgSplit(typeCode);
965 if (types.size() != 2)
966 {
967 BMCWEB_LOG_ERROR << "wrong number contained types in dictionary: "
968 << types.size();
969 return -1;
970 }
971
972 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_DICT_ENTRY,
973 typeCode.c_str());
974 if (r < 0)
975 {
976 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container with rc " << r;
977 return r;
978 }
979
980 nlohmann::json key;
981 r = convertDBusToJSON(types[0], m, key);
982 if (r < 0)
983 {
984 return r;
985 }
986
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500987 const std::string* keyPtr = key.get_ptr<const std::string*>();
Matt Spinler6df8f992019-01-14 12:47:47 -0600988 if (keyPtr == nullptr)
989 {
990 // json doesn't support non-string keys. If we hit this condition,
991 // convert the result to a string so we can proceed
Ed Tanous71f52d92021-02-19 08:51:17 -0800992 key = key.dump(2, ' ', true, nlohmann::json::error_handler_t::replace);
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500993 keyPtr = key.get_ptr<const std::string*>();
Ed Tanous7c091622019-05-23 11:42:36 -0700994 // in theory this can't fail now, but lets be paranoid about it
995 // anyway
Matt Spinler6df8f992019-01-14 12:47:47 -0600996 if (keyPtr == nullptr)
997 {
998 return -1;
999 }
1000 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001001 nlohmann::json& value = object[*keyPtr];
Matt Spinler6df8f992019-01-14 12:47:47 -06001002
1003 r = convertDBusToJSON(types[1], m, value);
1004 if (r < 0)
1005 {
1006 return r;
1007 }
1008
1009 r = sd_bus_message_exit_container(m.get());
1010 if (r < 0)
1011 {
1012 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
1013 return r;
1014 }
1015
1016 return 0;
1017}
1018
Ed Tanous23a21a12020-07-25 04:45:05 +00001019inline int readArrayFromMessage(const std::string& typeCode,
Patrick Williams59d494e2022-07-22 19:26:55 -05001020 sdbusplus::message_t& m, nlohmann::json& data)
Matt Spinler6df8f992019-01-14 12:47:47 -06001021{
1022 if (typeCode.size() < 2)
1023 {
1024 BMCWEB_LOG_ERROR << "Type code " << typeCode
1025 << " too small for an array";
1026 return -1;
1027 }
1028
1029 std::string containedType = typeCode.substr(1);
1030
1031 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_ARRAY,
1032 containedType.c_str());
1033 if (r < 0)
1034 {
1035 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
1036 << r;
1037 return r;
1038 }
1039
Ed Tanous11ba3972022-07-11 09:50:41 -07001040 bool dict = containedType.starts_with("{") && containedType.ends_with("}");
Matt Spinler6df8f992019-01-14 12:47:47 -06001041
1042 if (dict)
1043 {
1044 // Remove the { }
1045 containedType = containedType.substr(1, containedType.size() - 2);
1046 data = nlohmann::json::object();
1047 }
1048 else
1049 {
1050 data = nlohmann::json::array();
1051 }
1052
1053 while (true)
1054 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001055 r = sd_bus_message_at_end(m.get(), 0);
Matt Spinler6df8f992019-01-14 12:47:47 -06001056 if (r < 0)
1057 {
1058 BMCWEB_LOG_ERROR << "sd_bus_message_at_end failed";
1059 return r;
1060 }
1061
1062 if (r > 0)
1063 {
1064 break;
1065 }
1066
1067 // Dictionaries are only ever seen in an array
1068 if (dict)
1069 {
1070 r = readDictEntryFromMessage(containedType, m, data);
1071 if (r < 0)
1072 {
1073 return r;
1074 }
1075 }
1076 else
1077 {
1078 data.push_back(nlohmann::json());
1079
1080 r = convertDBusToJSON(containedType, m, data.back());
1081 if (r < 0)
1082 {
1083 return r;
1084 }
1085 }
1086 }
1087
1088 r = sd_bus_message_exit_container(m.get());
1089 if (r < 0)
1090 {
1091 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
1092 return r;
1093 }
1094
1095 return 0;
1096}
1097
Ed Tanous23a21a12020-07-25 04:45:05 +00001098inline int readStructFromMessage(const std::string& typeCode,
Patrick Williams59d494e2022-07-22 19:26:55 -05001099 sdbusplus::message_t& m, nlohmann::json& data)
Matt Spinler75c6c672019-01-14 13:01:46 -06001100{
1101 if (typeCode.size() < 3)
1102 {
1103 BMCWEB_LOG_ERROR << "Type code " << typeCode
1104 << " too small for a struct";
1105 return -1;
1106 }
1107
1108 std::string containedTypes = typeCode.substr(1, typeCode.size() - 2);
1109 std::vector<std::string> types = dbusArgSplit(containedTypes);
1110
1111 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_STRUCT,
1112 containedTypes.c_str());
1113 if (r < 0)
1114 {
1115 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
1116 << r;
1117 return r;
1118 }
1119
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001120 for (const std::string& type : types)
Matt Spinler75c6c672019-01-14 13:01:46 -06001121 {
1122 data.push_back(nlohmann::json());
1123 r = convertDBusToJSON(type, m, data.back());
1124 if (r < 0)
1125 {
1126 return r;
1127 }
1128 }
1129
1130 r = sd_bus_message_exit_container(m.get());
1131 if (r < 0)
1132 {
1133 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
1134 return r;
1135 }
1136 return 0;
1137}
1138
Patrick Williams59d494e2022-07-22 19:26:55 -05001139inline int readVariantFromMessage(sdbusplus::message_t& m, nlohmann::json& data)
Matt Spinler89c19702019-01-14 13:13:00 -06001140{
Ed Tanous543f4402022-01-06 13:12:53 -08001141 const char* containerType = nullptr;
Ed Tanous99131cd2019-10-24 11:12:47 -07001142 int r = sd_bus_message_peek_type(m.get(), nullptr, &containerType);
Matt Spinler89c19702019-01-14 13:13:00 -06001143 if (r < 0)
1144 {
1145 BMCWEB_LOG_ERROR << "sd_bus_message_peek_type failed";
1146 return r;
1147 }
1148
1149 r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_VARIANT,
1150 containerType);
1151 if (r < 0)
1152 {
1153 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
1154 << r;
1155 return r;
1156 }
1157
1158 r = convertDBusToJSON(containerType, m, data);
1159 if (r < 0)
1160 {
1161 return r;
1162 }
1163
1164 r = sd_bus_message_exit_container(m.get());
1165 if (r < 0)
1166 {
1167 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed";
1168 return r;
1169 }
1170
1171 return 0;
1172}
1173
Ed Tanous23a21a12020-07-25 04:45:05 +00001174inline int convertDBusToJSON(const std::string& returnType,
Patrick Williams59d494e2022-07-22 19:26:55 -05001175 sdbusplus::message_t& m, nlohmann::json& response)
Matt Spinler16caaee2019-01-15 11:40:34 -06001176{
Matt Spinlerd22a7132019-01-14 12:14:30 -06001177 int r = 0;
1178 const std::vector<std::string> returnTypes = dbusArgSplit(returnType);
1179
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001180 for (const std::string& typeCode : returnTypes)
Matt Spinlerd22a7132019-01-14 12:14:30 -06001181 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001182 nlohmann::json* thisElement = &response;
Matt Spinlerf39420c2019-01-30 12:57:18 -06001183 if (returnTypes.size() > 1)
Matt Spinlerd22a7132019-01-14 12:14:30 -06001184 {
1185 response.push_back(nlohmann::json{});
Matt Spinlerf39420c2019-01-30 12:57:18 -06001186 thisElement = &response.back();
Matt Spinlerd22a7132019-01-14 12:14:30 -06001187 }
1188
Ed Tanousd4d25792020-09-29 15:15:03 -07001189 if (typeCode == "s" || typeCode == "g" || typeCode == "o")
Matt Spinlerd22a7132019-01-14 12:14:30 -06001190 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001191 r = readMessageItem<char*>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001192 if (r < 0)
1193 {
1194 return r;
1195 }
1196 }
1197 else if (typeCode == "b")
1198 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001199 r = readMessageItem<int>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001200 if (r < 0)
1201 {
1202 return r;
1203 }
1204
Matt Spinlerf39420c2019-01-30 12:57:18 -06001205 *thisElement = static_cast<bool>(thisElement->get<int>());
Matt Spinlerd22a7132019-01-14 12:14:30 -06001206 }
1207 else if (typeCode == "u")
1208 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001209 r = readMessageItem<uint32_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001210 if (r < 0)
1211 {
1212 return r;
1213 }
1214 }
1215 else if (typeCode == "i")
1216 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001217 r = readMessageItem<int32_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001218 if (r < 0)
1219 {
1220 return r;
1221 }
1222 }
1223 else if (typeCode == "x")
1224 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001225 r = readMessageItem<int64_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001226 if (r < 0)
1227 {
1228 return r;
1229 }
1230 }
1231 else if (typeCode == "t")
1232 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001233 r = readMessageItem<uint64_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001234 if (r < 0)
1235 {
1236 return r;
1237 }
1238 }
1239 else if (typeCode == "n")
1240 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001241 r = readMessageItem<int16_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001242 if (r < 0)
1243 {
1244 return r;
1245 }
1246 }
1247 else if (typeCode == "q")
1248 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001249 r = readMessageItem<uint16_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001250 if (r < 0)
1251 {
1252 return r;
1253 }
1254 }
1255 else if (typeCode == "y")
1256 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001257 r = readMessageItem<uint8_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001258 if (r < 0)
1259 {
1260 return r;
1261 }
1262 }
1263 else if (typeCode == "d")
1264 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001265 r = readMessageItem<double>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001266 if (r < 0)
1267 {
1268 return r;
1269 }
1270 }
1271 else if (typeCode == "h")
1272 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001273 r = readMessageItem<int>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001274 if (r < 0)
1275 {
1276 return r;
1277 }
1278 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001279 else if (typeCode.starts_with("a"))
Matt Spinler6df8f992019-01-14 12:47:47 -06001280 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001281 r = readArrayFromMessage(typeCode, m, *thisElement);
Matt Spinler6df8f992019-01-14 12:47:47 -06001282 if (r < 0)
1283 {
1284 return r;
1285 }
1286 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001287 else if (typeCode.starts_with("(") && typeCode.ends_with(")"))
Matt Spinler75c6c672019-01-14 13:01:46 -06001288 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001289 r = readStructFromMessage(typeCode, m, *thisElement);
Matt Spinler75c6c672019-01-14 13:01:46 -06001290 if (r < 0)
1291 {
1292 return r;
1293 }
1294 }
Ed Tanous11ba3972022-07-11 09:50:41 -07001295 else if (typeCode.starts_with("v"))
Matt Spinler89c19702019-01-14 13:13:00 -06001296 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001297 r = readVariantFromMessage(m, *thisElement);
Matt Spinler89c19702019-01-14 13:13:00 -06001298 if (r < 0)
1299 {
1300 return r;
1301 }
1302 }
Matt Spinlerd22a7132019-01-14 12:14:30 -06001303 else
1304 {
Matt Spinlerd22a7132019-01-14 12:14:30 -06001305 BMCWEB_LOG_ERROR << "Invalid D-Bus signature type " << typeCode;
1306 return -2;
1307 }
1308 }
1309
Matt Spinler16caaee2019-01-15 11:40:34 -06001310 return 0;
1311}
1312
Ed Tanousb5a76932020-09-29 16:16:58 -07001313inline void handleMethodResponse(
1314 const std::shared_ptr<InProgressActionData>& transaction,
Patrick Williams59d494e2022-07-22 19:26:55 -05001315 sdbusplus::message_t& m, const std::string& returnType)
Matt Spinler16caaee2019-01-15 11:40:34 -06001316{
Matt Spinler39a4e392019-01-15 11:53:13 -06001317 nlohmann::json data;
1318
1319 int r = convertDBusToJSON(returnType, m, data);
1320 if (r < 0)
1321 {
1322 transaction->outputFailed = true;
1323 return;
1324 }
1325
1326 if (data.is_null())
1327 {
1328 return;
1329 }
1330
1331 if (transaction->methodResponse.is_null())
1332 {
1333 transaction->methodResponse = std::move(data);
1334 return;
1335 }
1336
1337 // If they're both dictionaries or arrays, merge into one.
1338 // Otherwise, make the results an array with every result
1339 // an entry. Could also just fail in that case, but it
1340 // seems better to get the data back somehow.
1341
1342 if (transaction->methodResponse.is_object() && data.is_object())
1343 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001344 for (const auto& obj : data.items())
Matt Spinler39a4e392019-01-15 11:53:13 -06001345 {
1346 // Note: Will overwrite the data for a duplicate key
1347 transaction->methodResponse.emplace(obj.key(),
1348 std::move(obj.value()));
1349 }
1350 return;
1351 }
1352
1353 if (transaction->methodResponse.is_array() && data.is_array())
1354 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001355 for (auto& obj : data)
Matt Spinler39a4e392019-01-15 11:53:13 -06001356 {
1357 transaction->methodResponse.push_back(std::move(obj));
1358 }
1359 return;
1360 }
1361
1362 if (!transaction->convertedToArray)
1363 {
1364 // They are different types. May as well turn them into an array
1365 nlohmann::json j = std::move(transaction->methodResponse);
1366 transaction->methodResponse = nlohmann::json::array();
1367 transaction->methodResponse.push_back(std::move(j));
1368 transaction->methodResponse.push_back(std::move(data));
1369 transaction->convertedToArray = true;
1370 }
1371 else
1372 {
1373 transaction->methodResponse.push_back(std::move(data));
1374 }
Matt Spinler16caaee2019-01-15 11:40:34 -06001375}
1376
Ed Tanousb5a76932020-09-29 16:16:58 -07001377inline void findActionOnInterface(
1378 const std::shared_ptr<InProgressActionData>& transaction,
1379 const std::string& connectionName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001380{
1381 BMCWEB_LOG_DEBUG << "findActionOnInterface for connection "
1382 << connectionName;
1383 crow::connections::systemBus->async_method_call(
1384 [transaction, connectionName{std::string(connectionName)}](
1385 const boost::system::error_code ec,
Ed Tanous81ce6092020-12-17 16:54:55 +00001386 const std::string& introspectXml) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001387 BMCWEB_LOG_DEBUG << "got xml:\n " << introspectXml;
1388 if (ec)
1389 {
1390 BMCWEB_LOG_ERROR
1391 << "Introspect call failed with error: " << ec.message()
1392 << " on process: " << connectionName << "\n";
1393 return;
1394 }
1395 tinyxml2::XMLDocument doc;
Matt Spinler318bd892019-01-15 09:59:20 -06001396
Ed Tanous002d39b2022-05-31 08:59:27 -07001397 doc.Parse(introspectXml.data(), introspectXml.size());
1398 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
1399 if (pRoot == nullptr)
1400 {
1401 BMCWEB_LOG_ERROR << "XML document failed to parse "
1402 << connectionName << "\n";
1403 return;
1404 }
1405 tinyxml2::XMLElement* interfaceNode =
1406 pRoot->FirstChildElement("interface");
1407 while (interfaceNode != nullptr)
1408 {
1409 const char* thisInterfaceName = interfaceNode->Attribute("name");
1410 if (thisInterfaceName != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001411 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 if (!transaction->interfaceName.empty() &&
1413 (transaction->interfaceName != thisInterfaceName))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001414 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001415 interfaceNode =
1416 interfaceNode->NextSiblingElement("interface");
1417 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001418 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001419
1420 tinyxml2::XMLElement* methodNode =
1421 interfaceNode->FirstChildElement("method");
1422 while (methodNode != nullptr)
1423 {
1424 const char* thisMethodName = methodNode->Attribute("name");
1425 BMCWEB_LOG_DEBUG << "Found method: " << thisMethodName;
1426 if (thisMethodName != nullptr &&
1427 thisMethodName == transaction->methodName)
1428 {
1429 BMCWEB_LOG_DEBUG << "Found method named "
1430 << thisMethodName << " on interface "
1431 << thisInterfaceName;
Patrick Williams59d494e2022-07-22 19:26:55 -05001432 sdbusplus::message_t m =
Ed Tanous002d39b2022-05-31 08:59:27 -07001433 crow::connections::systemBus->new_method_call(
1434 connectionName.c_str(),
1435 transaction->path.c_str(), thisInterfaceName,
1436 transaction->methodName.c_str());
1437
1438 tinyxml2::XMLElement* argumentNode =
1439 methodNode->FirstChildElement("arg");
1440
1441 std::string returnType;
1442
1443 // Find the output type
1444 while (argumentNode != nullptr)
1445 {
1446 const char* argDirection =
1447 argumentNode->Attribute("direction");
1448 const char* argType =
1449 argumentNode->Attribute("type");
1450 if (argDirection != nullptr && argType != nullptr &&
1451 std::string(argDirection) == "out")
1452 {
1453 returnType = argType;
1454 break;
1455 }
1456 argumentNode =
1457 argumentNode->NextSiblingElement("arg");
1458 }
1459
Nan Zhoub2ec0ce2022-06-02 23:57:38 +00001460 auto argIt = transaction->arguments.begin();
Ed Tanous002d39b2022-05-31 08:59:27 -07001461
1462 argumentNode = methodNode->FirstChildElement("arg");
1463
1464 while (argumentNode != nullptr)
1465 {
1466 const char* argDirection =
1467 argumentNode->Attribute("direction");
1468 const char* argType =
1469 argumentNode->Attribute("type");
1470 if (argDirection != nullptr && argType != nullptr &&
1471 std::string(argDirection) == "in")
1472 {
1473 if (argIt == transaction->arguments.end())
1474 {
1475 transaction->setErrorStatus(
1476 "Invalid method args");
1477 return;
1478 }
1479 if (convertJsonToDbus(m.get(),
1480 std::string(argType),
1481 *argIt) < 0)
1482 {
1483 transaction->setErrorStatus(
1484 "Invalid method arg type");
1485 return;
1486 }
1487
1488 argIt++;
1489 }
1490 argumentNode =
1491 argumentNode->NextSiblingElement("arg");
1492 }
1493
1494 crow::connections::systemBus->async_send(
1495 m,
1496 [transaction,
1497 returnType](boost::system::error_code ec2,
Patrick Williams59d494e2022-07-22 19:26:55 -05001498 sdbusplus::message_t& m2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001499 if (ec2)
1500 {
1501 transaction->methodFailed = true;
1502 const sd_bus_error* e = m2.get_error();
1503
1504 if (e != nullptr)
1505 {
1506 setErrorResponse(
1507 transaction->res,
1508 boost::beast::http::status::bad_request,
1509 e->name, e->message);
1510 }
1511 else
1512 {
1513 setErrorResponse(
1514 transaction->res,
1515 boost::beast::http::status::bad_request,
1516 "Method call failed", methodFailedMsg);
1517 }
1518 return;
1519 }
1520 transaction->methodPassed = true;
1521
1522 handleMethodResponse(transaction, m2, returnType);
1523 });
1524 break;
1525 }
1526 methodNode = methodNode->NextSiblingElement("method");
1527 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001528 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001529 interfaceNode = interfaceNode->NextSiblingElement("interface");
1530 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001531 },
1532 connectionName, transaction->path,
1533 "org.freedesktop.DBus.Introspectable", "Introspect");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001534}
1535
zhanghch058d1b46d2021-04-01 11:18:24 +08001536inline void handleAction(const crow::Request& req,
1537 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous23a21a12020-07-25 04:45:05 +00001538 const std::string& objectPath,
1539 const std::string& methodName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001540{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001541 BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method "
1542 << methodName;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001543 nlohmann::json requestDbusData =
1544 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001545
Ed Tanous1abe55e2018-09-05 08:30:59 -07001546 if (requestDbusData.is_discarded())
1547 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001548 setErrorResponse(asyncResp->res,
1549 boost::beast::http::status::bad_request, noJsonDesc,
1550 badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001551 return;
1552 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001553 nlohmann::json::iterator data = requestDbusData.find("data");
1554 if (data == requestDbusData.end())
1555 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001556 setErrorResponse(asyncResp->res,
1557 boost::beast::http::status::bad_request, noJsonDesc,
1558 badReqMsg);
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001559 return;
1560 }
1561
1562 if (!data->is_array())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001563 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001564 setErrorResponse(asyncResp->res,
1565 boost::beast::http::status::bad_request, noJsonDesc,
1566 badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001567 return;
1568 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001569 auto transaction = std::make_shared<InProgressActionData>(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001570
1571 transaction->path = objectPath;
1572 transaction->methodName = methodName;
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001573 transaction->arguments = std::move(*data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001574 crow::connections::systemBus->async_method_call(
1575 [transaction](
1576 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001577 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1578 interfaceNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001579 if (ec || interfaceNames.empty())
1580 {
1581 BMCWEB_LOG_ERROR << "Can't find object";
1582 setErrorResponse(transaction->res,
1583 boost::beast::http::status::not_found,
1584 notFoundDesc, notFoundMsg);
1585 return;
1586 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001587
Ed Tanous002d39b2022-05-31 08:59:27 -07001588 BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size()
1589 << " object(s)";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001590
Ed Tanous002d39b2022-05-31 08:59:27 -07001591 for (const std::pair<std::string, std::vector<std::string>>& object :
1592 interfaceNames)
1593 {
1594 findActionOnInterface(transaction, object.first);
1595 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001596 },
1597 "xyz.openbmc_project.ObjectMapper",
1598 "/xyz/openbmc_project/object_mapper",
1599 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
1600 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001601}
1602
zhanghch058d1b46d2021-04-01 11:18:24 +08001603inline void handleDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1604 const std::string& objectPath)
Matt Spinlerde818812018-12-11 16:39:20 -06001605{
1606 BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath;
1607
1608 crow::connections::systemBus->async_method_call(
zhanghch058d1b46d2021-04-01 11:18:24 +08001609 [asyncResp, objectPath](
Matt Spinlerde818812018-12-11 16:39:20 -06001610 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001611 const std::vector<std::pair<std::string, std::vector<std::string>>>&
1612 interfaceNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001613 if (ec || interfaceNames.empty())
1614 {
1615 BMCWEB_LOG_ERROR << "Can't find object";
1616 setErrorResponse(asyncResp->res,
1617 boost::beast::http::status::method_not_allowed,
1618 methodNotAllowedDesc, methodNotAllowedMsg);
1619 return;
1620 }
Matt Spinlerde818812018-12-11 16:39:20 -06001621
Ed Tanous002d39b2022-05-31 08:59:27 -07001622 auto transaction =
1623 std::make_shared<InProgressActionData>(asyncResp->res);
1624 transaction->path = objectPath;
1625 transaction->methodName = "Delete";
1626 transaction->interfaceName = "xyz.openbmc_project.Object.Delete";
Matt Spinlerde818812018-12-11 16:39:20 -06001627
Ed Tanous002d39b2022-05-31 08:59:27 -07001628 for (const std::pair<std::string, std::vector<std::string>>& object :
1629 interfaceNames)
1630 {
1631 findActionOnInterface(transaction, object.first);
1632 }
Matt Spinlerde818812018-12-11 16:39:20 -06001633 },
1634 "xyz.openbmc_project.ObjectMapper",
1635 "/xyz/openbmc_project/object_mapper",
1636 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001637 std::array<const char*, 0>());
Matt Spinlerde818812018-12-11 16:39:20 -06001638}
1639
zhanghch058d1b46d2021-04-01 11:18:24 +08001640inline void handleList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1641 const std::string& objectPath, int32_t depth = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001642{
George Liu7a1dbc42022-12-07 16:03:22 +08001643 dbus::utility::getSubTreePaths(
1644 objectPath, depth, {},
Ed Tanousb9d36b42022-02-26 21:42:46 -08001645 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08001646 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001647 const dbus::utility::MapperGetSubTreePathsResponse& objectPaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001648 if (ec)
1649 {
1650 setErrorResponse(asyncResp->res,
1651 boost::beast::http::status::not_found,
1652 notFoundDesc, notFoundMsg);
1653 }
1654 else
1655 {
1656 asyncResp->res.jsonValue["status"] = "ok";
1657 asyncResp->res.jsonValue["message"] = "200 OK";
1658 asyncResp->res.jsonValue["data"] = objectPaths;
1659 }
George Liu7a1dbc42022-12-07 16:03:22 +08001660 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001661}
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001662
zhanghch058d1b46d2021-04-01 11:18:24 +08001663inline void handleEnumerate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1664 const std::string& objectPath)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001665{
Ed Tanous049a0512018-11-01 13:58:42 -07001666 BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath;
Ed Tanous049a0512018-11-01 13:58:42 -07001667
Ed Tanous14766872022-03-15 10:44:42 -07001668 asyncResp->res.jsonValue["message"] = "200 OK";
1669 asyncResp->res.jsonValue["status"] = "ok";
1670 asyncResp->res.jsonValue["data"] = nlohmann::json::object();
Ed Tanous049a0512018-11-01 13:58:42 -07001671
Ed Tanous1abe55e2018-09-05 08:30:59 -07001672 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001673 [objectPath, asyncResp](
1674 const boost::system::error_code ec,
1675 const dbus::utility::MapperGetSubTreeResponse& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001676 auto transaction =
1677 std::make_shared<InProgressEnumerateData>(objectPath, asyncResp);
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001678
Ed Tanous002d39b2022-05-31 08:59:27 -07001679 transaction->subtree =
1680 std::make_shared<dbus::utility::MapperGetSubTreeResponse>(
1681 objectNames);
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001682
Ed Tanous002d39b2022-05-31 08:59:27 -07001683 if (ec)
1684 {
1685 BMCWEB_LOG_ERROR << "GetSubTree failed on "
1686 << transaction->objectPath;
1687 setErrorResponse(transaction->asyncResp->res,
1688 boost::beast::http::status::not_found,
1689 notFoundDesc, notFoundMsg);
1690 return;
1691 }
Ed Tanous64530012018-02-06 17:08:16 -08001692
Ed Tanous002d39b2022-05-31 08:59:27 -07001693 // Add the data for the path passed in to the results
1694 // as if GetSubTree returned it, and continue on enumerating
1695 getObjectAndEnumerate(transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001696 },
1697 "xyz.openbmc_project.ObjectMapper",
1698 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -07001699 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, 0,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001700 std::array<const char*, 0>());
Ed Tanous64530012018-02-06 17:08:16 -08001701}
Ed Tanous911ac312017-08-15 09:37:42 -07001702
zhanghch058d1b46d2021-04-01 11:18:24 +08001703inline void handleGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1704 std::string& objectPath, std::string& destProperty)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001705{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001706 BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty;
1707 std::shared_ptr<std::string> propertyName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001708 std::make_shared<std::string>(std::move(destProperty));
Ed Tanous75db20e2018-07-27 13:44:44 -07001709
Ed Tanous1abe55e2018-09-05 08:30:59 -07001710 std::shared_ptr<std::string> path =
1711 std::make_shared<std::string>(std::move(objectPath));
Ed Tanous75db20e2018-07-27 13:44:44 -07001712
Ed Tanous1abe55e2018-09-05 08:30:59 -07001713 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001714 [asyncResp, path,
1715 propertyName](const boost::system::error_code ec,
1716 const dbus::utility::MapperGetObject& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001717 if (ec || objectNames.empty())
1718 {
1719 setErrorResponse(asyncResp->res,
1720 boost::beast::http::status::not_found,
1721 notFoundDesc, notFoundMsg);
1722 return;
1723 }
1724 std::shared_ptr<nlohmann::json> response =
1725 std::make_shared<nlohmann::json>(nlohmann::json::object());
1726 // The mapper should never give us an empty interface names
1727 // list, but check anyway
1728 for (const std::pair<std::string, std::vector<std::string>>&
1729 connection : objectNames)
1730 {
1731 const std::vector<std::string>& interfaceNames = connection.second;
1732
1733 if (interfaceNames.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001734 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001735 setErrorResponse(asyncResp->res,
1736 boost::beast::http::status::not_found,
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001737 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001738 return;
1739 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001740
1741 for (const std::string& interface : interfaceNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001742 {
Patrick Williams59d494e2022-07-22 19:26:55 -05001743 sdbusplus::message_t m =
Ed Tanous002d39b2022-05-31 08:59:27 -07001744 crow::connections::systemBus->new_method_call(
1745 connection.first.c_str(), path->c_str(),
1746 "org.freedesktop.DBus.Properties", "GetAll");
1747 m.append(interface);
1748 crow::connections::systemBus->async_send(
1749 m, [asyncResp, response,
1750 propertyName](const boost::system::error_code ec2,
Patrick Williams59d494e2022-07-22 19:26:55 -05001751 sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001752 if (ec2)
1753 {
1754 BMCWEB_LOG_ERROR << "Bad dbus request error: "
1755 << ec2;
1756 }
1757 else
1758 {
1759 nlohmann::json properties;
1760 int r = convertDBusToJSON("a{sv}", msg, properties);
1761 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001762 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001763 BMCWEB_LOG_ERROR << "convertDBusToJSON failed";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001764 }
1765 else
1766 {
Patrick Williams62bafc02022-09-08 17:35:35 -05001767 for (const auto& prop : properties.items())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001768 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001769 // if property name is empty, or
1770 // matches our search query, add it
1771 // to the response json
Ed Tanous1abe55e2018-09-05 08:30:59 -07001772
Ed Tanous002d39b2022-05-31 08:59:27 -07001773 if (propertyName->empty())
1774 {
1775 (*response)[prop.key()] =
1776 std::move(prop.value());
1777 }
1778 else if (prop.key() == *propertyName)
1779 {
1780 *response = std::move(prop.value());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001781 }
1782 }
1783 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001784 }
1785 if (response.use_count() == 1)
1786 {
1787 if (!propertyName->empty() && response->empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001788 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001789 setErrorResponse(
1790 asyncResp->res,
1791 boost::beast::http::status::not_found,
1792 propNotFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001793 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001794 else
1795 {
1796 asyncResp->res.jsonValue["status"] = "ok";
1797 asyncResp->res.jsonValue["message"] = "200 OK";
1798 asyncResp->res.jsonValue["data"] = *response;
1799 }
1800 }
1801 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001802 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001804 },
1805 "xyz.openbmc_project.ObjectMapper",
1806 "/xyz/openbmc_project/object_mapper",
1807 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
1808 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001809}
1810
Ed Tanous1abe55e2018-09-05 08:30:59 -07001811struct AsyncPutRequest
1812{
Ed Tanous4e23a442022-06-06 09:57:26 -07001813 explicit AsyncPutRequest(const std::shared_ptr<bmcweb::AsyncResp>& resIn) :
zhanghch058d1b46d2021-04-01 11:18:24 +08001814 asyncResp(resIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001815 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001816 ~AsyncPutRequest()
1817 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001818 if (asyncResp->res.jsonValue.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001819 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001820 setErrorResponse(asyncResp->res,
1821 boost::beast::http::status::forbidden,
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001822 forbiddenMsg, forbiddenPropDesc);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001823 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001824 }
1825
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001826 AsyncPutRequest(const AsyncPutRequest&) = delete;
1827 AsyncPutRequest(AsyncPutRequest&&) = delete;
1828 AsyncPutRequest& operator=(const AsyncPutRequest&) = delete;
1829 AsyncPutRequest& operator=(AsyncPutRequest&&) = delete;
1830
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001831 void setErrorStatus(const std::string& desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001832 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001833 setErrorResponse(asyncResp->res,
1834 boost::beast::http::status::internal_server_error,
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001835 desc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001836 }
1837
zhanghch058d1b46d2021-04-01 11:18:24 +08001838 const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001839 std::string objectPath;
1840 std::string propertyName;
1841 nlohmann::json propertyValue;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001842};
1843
zhanghch058d1b46d2021-04-01 11:18:24 +08001844inline void handlePut(const crow::Request& req,
1845 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous23a21a12020-07-25 04:45:05 +00001846 const std::string& objectPath,
1847 const std::string& destProperty)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001848{
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001849 if (destProperty.empty())
1850 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001851 setErrorResponse(asyncResp->res, boost::beast::http::status::forbidden,
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001852 forbiddenResDesc, forbiddenMsg);
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001853 return;
1854 }
1855
Ed Tanous1abe55e2018-09-05 08:30:59 -07001856 nlohmann::json requestDbusData =
1857 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001858
Ed Tanous1abe55e2018-09-05 08:30:59 -07001859 if (requestDbusData.is_discarded())
1860 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001861 setErrorResponse(asyncResp->res,
1862 boost::beast::http::status::bad_request, noJsonDesc,
1863 badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001864 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001865 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001866
Nan Zhoub2ec0ce2022-06-02 23:57:38 +00001867 auto propertyIt = requestDbusData.find("data");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001868 if (propertyIt == requestDbusData.end())
1869 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001870 setErrorResponse(asyncResp->res,
1871 boost::beast::http::status::bad_request, noJsonDesc,
1872 badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001873 return;
1874 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001875 const nlohmann::json& propertySetValue = *propertyIt;
zhanghch058d1b46d2021-04-01 11:18:24 +08001876 auto transaction = std::make_shared<AsyncPutRequest>(asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001877 transaction->objectPath = objectPath;
1878 transaction->propertyName = destProperty;
1879 transaction->propertyValue = propertySetValue;
Ed Tanous911ac312017-08-15 09:37:42 -07001880
Ed Tanous1abe55e2018-09-05 08:30:59 -07001881 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +00001882 [transaction](const boost::system::error_code ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001883 const dbus::utility::MapperGetObject& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001884 if (!ec2 && objectNames.empty())
1885 {
1886 setErrorResponse(transaction->asyncResp->res,
1887 boost::beast::http::status::not_found,
1888 propNotFoundDesc, notFoundMsg);
1889 return;
1890 }
Ed Tanous911ac312017-08-15 09:37:42 -07001891
Ed Tanous002d39b2022-05-31 08:59:27 -07001892 for (const std::pair<std::string, std::vector<std::string>>&
1893 connection : objectNames)
1894 {
1895 const std::string& connectionName = connection.first;
Ed Tanous911ac312017-08-15 09:37:42 -07001896
Ed Tanous002d39b2022-05-31 08:59:27 -07001897 crow::connections::systemBus->async_method_call(
1898 [connectionName{std::string(connectionName)},
1899 transaction](const boost::system::error_code ec3,
1900 const std::string& introspectXml) {
1901 if (ec3)
1902 {
1903 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
1904 << ec3.message()
1905 << " on process: " << connectionName;
1906 transaction->setErrorStatus("Unexpected Error");
1907 return;
1908 }
1909 tinyxml2::XMLDocument doc;
Ed Tanous911ac312017-08-15 09:37:42 -07001910
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 doc.Parse(introspectXml.c_str());
1912 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
1913 if (pRoot == nullptr)
1914 {
1915 BMCWEB_LOG_ERROR << "XML document failed to parse: "
1916 << introspectXml;
1917 transaction->setErrorStatus("Unexpected Error");
1918 return;
1919 }
1920 tinyxml2::XMLElement* ifaceNode =
1921 pRoot->FirstChildElement("interface");
1922 while (ifaceNode != nullptr)
1923 {
1924 const char* interfaceName = ifaceNode->Attribute("name");
1925 BMCWEB_LOG_DEBUG << "found interface " << interfaceName;
1926 tinyxml2::XMLElement* propNode =
1927 ifaceNode->FirstChildElement("property");
1928 while (propNode != nullptr)
1929 {
1930 const char* propertyName = propNode->Attribute("name");
1931 BMCWEB_LOG_DEBUG << "Found property " << propertyName;
1932 if (propertyName == transaction->propertyName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001933 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001934 const char* argType = propNode->Attribute("type");
1935 if (argType != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001936 {
Patrick Williams59d494e2022-07-22 19:26:55 -05001937 sdbusplus::message_t m =
Ed Tanous002d39b2022-05-31 08:59:27 -07001938 crow::connections::systemBus
1939 ->new_method_call(
1940 connectionName.c_str(),
1941 transaction->objectPath.c_str(),
1942 "org.freedesktop.DBus."
1943 "Properties",
1944 "Set");
1945 m.append(interfaceName,
1946 transaction->propertyName);
1947 int r = sd_bus_message_open_container(
1948 m.get(), SD_BUS_TYPE_VARIANT, argType);
1949 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001950 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001951 transaction->setErrorStatus(
1952 "Unexpected Error");
1953 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001954 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001955 r = convertJsonToDbus(
1956 m.get(), argType,
1957 transaction->propertyValue);
1958 if (r < 0)
1959 {
1960 if (r == -ERANGE)
1961 {
1962 transaction->setErrorStatus(
1963 "Provided property value "
1964 "is out of range for the "
1965 "property type");
1966 }
1967 else
1968 {
1969 transaction->setErrorStatus(
1970 "Invalid arg type");
1971 }
1972 return;
1973 }
1974 r = sd_bus_message_close_container(m.get());
1975 if (r < 0)
1976 {
1977 transaction->setErrorStatus(
1978 "Unexpected Error");
1979 return;
1980 }
1981 crow::connections::systemBus->async_send(
1982 m,
Patrick Williams59d494e2022-07-22 19:26:55 -05001983 [transaction](boost::system::error_code ec,
1984 sdbusplus::message_t& m2) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001985 BMCWEB_LOG_DEBUG << "sent";
1986 if (ec)
1987 {
1988 const sd_bus_error* e = m2.get_error();
1989 setErrorResponse(
1990 transaction->asyncResp->res,
1991 boost::beast::http::status::
1992 forbidden,
1993 (e) != nullptr
1994 ? e->name
1995 : ec.category().name(),
1996 (e) != nullptr ? e->message
1997 : ec.message());
1998 }
1999 else
2000 {
2001 transaction->asyncResp->res
2002 .jsonValue["status"] = "ok";
2003 transaction->asyncResp->res
2004 .jsonValue["message"] = "200 OK";
2005 transaction->asyncResp->res
2006 .jsonValue["data"] = nullptr;
2007 }
2008 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002009 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002010 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002011 propNode = propNode->NextSiblingElement("property");
2012 }
2013 ifaceNode = ifaceNode->NextSiblingElement("interface");
2014 }
2015 },
2016 connectionName, transaction->objectPath,
2017 "org.freedesktop.DBus.Introspectable", "Introspect");
2018 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002019 },
2020 "xyz.openbmc_project.ObjectMapper",
2021 "/xyz/openbmc_project/object_mapper",
2022 "xyz.openbmc_project.ObjectMapper", "GetObject",
2023 transaction->objectPath, std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07002024}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002025
zhanghch058d1b46d2021-04-01 11:18:24 +08002026inline void handleDBusUrl(const crow::Request& req,
2027 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002028 std::string& objectPath)
Ed Tanous049a0512018-11-01 13:58:42 -07002029{
Ed Tanous049a0512018-11-01 13:58:42 -07002030
2031 // If accessing a single attribute, fill in and update objectPath,
2032 // otherwise leave destProperty blank
Ed Tanouse05aec52022-01-25 10:28:56 -08002033 std::string destProperty;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002034 const char* attrSeperator = "/attr/";
Ed Tanous049a0512018-11-01 13:58:42 -07002035 size_t attrPosition = objectPath.find(attrSeperator);
Ed Tanous71d5d8d2022-01-25 11:04:33 -08002036 if (attrPosition != std::string::npos)
Ed Tanous049a0512018-11-01 13:58:42 -07002037 {
2038 destProperty = objectPath.substr(attrPosition + strlen(attrSeperator),
2039 objectPath.length());
Ed Tanous7f57f192022-12-20 09:53:40 -08002040 objectPath.resize(attrPosition);
Ed Tanous049a0512018-11-01 13:58:42 -07002041 }
2042
Ed Tanousb41187f2019-10-24 16:30:02 -07002043 if (req.method() == boost::beast::http::verb::post)
Ed Tanous049a0512018-11-01 13:58:42 -07002044 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002045 constexpr const char* actionSeperator = "/action/";
Ed Tanous049a0512018-11-01 13:58:42 -07002046 size_t actionPosition = objectPath.find(actionSeperator);
Ed Tanous71d5d8d2022-01-25 11:04:33 -08002047 if (actionPosition != std::string::npos)
Ed Tanous049a0512018-11-01 13:58:42 -07002048 {
2049 std::string postProperty =
2050 objectPath.substr((actionPosition + strlen(actionSeperator)),
2051 objectPath.length());
Ed Tanous7f57f192022-12-20 09:53:40 -08002052 objectPath.resize(actionPosition);
zhanghch058d1b46d2021-04-01 11:18:24 +08002053 handleAction(req, asyncResp, objectPath, postProperty);
Ed Tanous049a0512018-11-01 13:58:42 -07002054 return;
2055 }
2056 }
Ed Tanousb41187f2019-10-24 16:30:02 -07002057 else if (req.method() == boost::beast::http::verb::get)
Ed Tanous049a0512018-11-01 13:58:42 -07002058 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002059 if (objectPath.ends_with("/enumerate"))
Ed Tanous049a0512018-11-01 13:58:42 -07002060 {
2061 objectPath.erase(objectPath.end() - sizeof("enumerate"),
2062 objectPath.end());
zhanghch058d1b46d2021-04-01 11:18:24 +08002063 handleEnumerate(asyncResp, objectPath);
Ed Tanous049a0512018-11-01 13:58:42 -07002064 }
Ed Tanous11ba3972022-07-11 09:50:41 -07002065 else if (objectPath.ends_with("/list"))
Ed Tanous049a0512018-11-01 13:58:42 -07002066 {
2067 objectPath.erase(objectPath.end() - sizeof("list"),
2068 objectPath.end());
zhanghch058d1b46d2021-04-01 11:18:24 +08002069 handleList(asyncResp, objectPath);
Ed Tanous049a0512018-11-01 13:58:42 -07002070 }
2071 else
2072 {
Ed Tanousf839dfe2018-11-12 11:11:15 -08002073 // Trim any trailing "/" at the end
Ed Tanous11ba3972022-07-11 09:50:41 -07002074 if (objectPath.ends_with("/"))
Ed Tanousf839dfe2018-11-12 11:11:15 -08002075 {
2076 objectPath.pop_back();
zhanghch058d1b46d2021-04-01 11:18:24 +08002077 handleList(asyncResp, objectPath, 1);
Ed Tanousf839dfe2018-11-12 11:11:15 -08002078 }
2079 else
2080 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002081 handleGet(asyncResp, objectPath, destProperty);
Ed Tanousf839dfe2018-11-12 11:11:15 -08002082 }
Ed Tanous049a0512018-11-01 13:58:42 -07002083 }
2084 return;
2085 }
Ed Tanousb41187f2019-10-24 16:30:02 -07002086 else if (req.method() == boost::beast::http::verb::put)
Ed Tanous049a0512018-11-01 13:58:42 -07002087 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002088 handlePut(req, asyncResp, objectPath, destProperty);
Ed Tanous049a0512018-11-01 13:58:42 -07002089 return;
2090 }
Ed Tanousb41187f2019-10-24 16:30:02 -07002091 else if (req.method() == boost::beast::http::verb::delete_)
Matt Spinlerde818812018-12-11 16:39:20 -06002092 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002093 handleDelete(asyncResp, objectPath);
Matt Spinlerde818812018-12-11 16:39:20 -06002094 return;
2095 }
Ed Tanous049a0512018-11-01 13:58:42 -07002096
zhanghch058d1b46d2021-04-01 11:18:24 +08002097 setErrorResponse(asyncResp->res,
2098 boost::beast::http::status::method_not_allowed,
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -06002099 methodNotAllowedDesc, methodNotAllowedMsg);
Ed Tanous049a0512018-11-01 13:58:42 -07002100}
2101
Ed Tanous1656b292022-05-04 11:33:42 -07002102inline void
2103 handleBusSystemPost(const crow::Request& req,
2104 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2105 const std::string& processName,
2106 const std::string& requestedPath)
2107{
2108 std::vector<std::string> strs;
2109 boost::split(strs, requestedPath, boost::is_any_of("/"));
2110 std::string objectPath;
2111 std::string interfaceName;
2112 std::string methodName;
2113 auto it = strs.begin();
2114 if (it == strs.end())
2115 {
2116 objectPath = "/";
2117 }
2118 while (it != strs.end())
2119 {
2120 // Check if segment contains ".". If it does, it must be an
2121 // interface
2122 if (it->find(".") != std::string::npos)
2123 {
2124 break;
2125 // This check is necessary as the trailing slash gets
2126 // parsed as part of our <path> specifier above, which
2127 // causes the normal trailing backslash redirector to
2128 // fail.
2129 }
2130 if (!it->empty())
2131 {
2132 objectPath += "/" + *it;
2133 }
2134 it++;
2135 }
2136 if (it != strs.end())
2137 {
2138 interfaceName = *it;
2139 it++;
2140
2141 // after interface, we might have a method name
2142 if (it != strs.end())
2143 {
2144 methodName = *it;
2145 it++;
2146 }
2147 }
2148 if (it != strs.end())
2149 {
2150 // if there is more levels past the method name, something
2151 // went wrong, return not found
2152 asyncResp->res.result(boost::beast::http::status::not_found);
2153 return;
2154 }
2155 if (interfaceName.empty())
2156 {
2157 crow::connections::systemBus->async_method_call(
2158 [asyncResp, processName,
2159 objectPath](const boost::system::error_code ec,
2160 const std::string& introspectXml) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002161 if (ec)
2162 {
2163 BMCWEB_LOG_ERROR
2164 << "Introspect call failed with error: " << ec.message()
2165 << " on process: " << processName << " path: " << objectPath
2166 << "\n";
2167 return;
2168 }
2169 tinyxml2::XMLDocument doc;
Ed Tanous1656b292022-05-04 11:33:42 -07002170
Ed Tanous002d39b2022-05-31 08:59:27 -07002171 doc.Parse(introspectXml.c_str());
2172 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
2173 if (pRoot == nullptr)
2174 {
2175 BMCWEB_LOG_ERROR << "XML document failed to parse "
2176 << processName << " " << objectPath << "\n";
2177 asyncResp->res.jsonValue["status"] = "XML parse error";
2178 asyncResp->res.result(
2179 boost::beast::http::status::internal_server_error);
2180 return;
2181 }
2182
2183 BMCWEB_LOG_DEBUG << introspectXml;
2184 asyncResp->res.jsonValue["status"] = "ok";
2185 asyncResp->res.jsonValue["bus_name"] = processName;
2186 asyncResp->res.jsonValue["object_path"] = objectPath;
2187
2188 nlohmann::json& interfacesArray =
2189 asyncResp->res.jsonValue["interfaces"];
2190 interfacesArray = nlohmann::json::array();
2191 tinyxml2::XMLElement* interface =
2192 pRoot->FirstChildElement("interface");
2193
2194 while (interface != nullptr)
2195 {
2196 const char* ifaceName = interface->Attribute("name");
2197 if (ifaceName != nullptr)
Ed Tanous1656b292022-05-04 11:33:42 -07002198 {
Ed Tanous8a592812022-06-04 09:06:59 -07002199 nlohmann::json::object_t interfaceObj;
2200 interfaceObj["name"] = ifaceName;
2201 interfacesArray.push_back(std::move(interfaceObj));
Ed Tanous1656b292022-05-04 11:33:42 -07002202 }
2203
Ed Tanous002d39b2022-05-31 08:59:27 -07002204 interface = interface->NextSiblingElement("interface");
2205 }
Ed Tanous1656b292022-05-04 11:33:42 -07002206 },
2207 processName, objectPath, "org.freedesktop.DBus.Introspectable",
2208 "Introspect");
2209 }
2210 else if (methodName.empty())
2211 {
2212 crow::connections::systemBus->async_method_call(
2213 [asyncResp, processName, objectPath,
2214 interfaceName](const boost::system::error_code ec,
2215 const std::string& introspectXml) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002216 if (ec)
2217 {
2218 BMCWEB_LOG_ERROR
2219 << "Introspect call failed with error: " << ec.message()
2220 << " on process: " << processName << " path: " << objectPath
2221 << "\n";
2222 return;
2223 }
2224 tinyxml2::XMLDocument doc;
Ed Tanous1656b292022-05-04 11:33:42 -07002225
Ed Tanous002d39b2022-05-31 08:59:27 -07002226 doc.Parse(introspectXml.data(), introspectXml.size());
2227 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
2228 if (pRoot == nullptr)
2229 {
2230 BMCWEB_LOG_ERROR << "XML document failed to parse "
2231 << processName << " " << objectPath << "\n";
2232 asyncResp->res.result(
2233 boost::beast::http::status::internal_server_error);
2234 return;
2235 }
2236
2237 asyncResp->res.jsonValue["status"] = "ok";
2238 asyncResp->res.jsonValue["bus_name"] = processName;
2239 asyncResp->res.jsonValue["interface"] = interfaceName;
2240 asyncResp->res.jsonValue["object_path"] = objectPath;
2241
2242 nlohmann::json& methodsArray = asyncResp->res.jsonValue["methods"];
2243 methodsArray = nlohmann::json::array();
2244
2245 nlohmann::json& signalsArray = asyncResp->res.jsonValue["signals"];
2246 signalsArray = nlohmann::json::array();
2247
2248 nlohmann::json& propertiesObj =
2249 asyncResp->res.jsonValue["properties"];
2250 propertiesObj = nlohmann::json::object();
2251
2252 // if we know we're the only call, build the
2253 // json directly
2254 tinyxml2::XMLElement* interface =
2255 pRoot->FirstChildElement("interface");
2256 while (interface != nullptr)
2257 {
2258 const char* ifaceName = interface->Attribute("name");
2259
2260 if (ifaceName != nullptr && ifaceName == interfaceName)
Ed Tanous1656b292022-05-04 11:33:42 -07002261 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002262 break;
Ed Tanous1656b292022-05-04 11:33:42 -07002263 }
Ed Tanous14766872022-03-15 10:44:42 -07002264
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 interface = interface->NextSiblingElement("interface");
2266 }
2267 if (interface == nullptr)
2268 {
2269 // if we got to the end of the list and
2270 // never found a match, throw 404
2271 asyncResp->res.result(boost::beast::http::status::not_found);
2272 return;
2273 }
Ed Tanous1656b292022-05-04 11:33:42 -07002274
Ed Tanous002d39b2022-05-31 08:59:27 -07002275 tinyxml2::XMLElement* methods =
2276 interface->FirstChildElement("method");
2277 while (methods != nullptr)
2278 {
2279 nlohmann::json argsArray = nlohmann::json::array();
2280 tinyxml2::XMLElement* arg = methods->FirstChildElement("arg");
2281 while (arg != nullptr)
Ed Tanous1656b292022-05-04 11:33:42 -07002282 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002283 nlohmann::json thisArg;
2284 for (const char* fieldName : std::array<const char*, 3>{
2285 "name", "direction", "type"})
Ed Tanous1656b292022-05-04 11:33:42 -07002286 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002287 const char* fieldValue = arg->Attribute(fieldName);
2288 if (fieldValue != nullptr)
Ed Tanous1656b292022-05-04 11:33:42 -07002289 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002290 thisArg[fieldName] = fieldValue;
2291 }
2292 }
2293 argsArray.push_back(std::move(thisArg));
2294 arg = arg->NextSiblingElement("arg");
2295 }
2296
2297 const char* name = methods->Attribute("name");
2298 if (name != nullptr)
2299 {
2300 std::string uri;
2301 uri.reserve(14 + processName.size() + objectPath.size() +
2302 interfaceName.size() + strlen(name));
2303 uri += "/bus/system/";
2304 uri += processName;
2305 uri += objectPath;
2306 uri += "/";
2307 uri += interfaceName;
2308 uri += "/";
2309 uri += name;
2310
2311 nlohmann::json::object_t object;
2312 object["name"] = name;
2313 object["uri"] = std::move(uri);
2314 object["args"] = argsArray;
2315
2316 methodsArray.push_back(std::move(object));
2317 }
2318 methods = methods->NextSiblingElement("method");
2319 }
2320 tinyxml2::XMLElement* signals =
2321 interface->FirstChildElement("signal");
2322 while (signals != nullptr)
2323 {
2324 nlohmann::json argsArray = nlohmann::json::array();
2325
2326 tinyxml2::XMLElement* arg = signals->FirstChildElement("arg");
2327 while (arg != nullptr)
2328 {
2329 const char* name = arg->Attribute("name");
2330 const char* type = arg->Attribute("type");
2331 if (name != nullptr && type != nullptr)
2332 {
2333 argsArray.push_back({
2334 {"name", name},
2335 {"type", type},
2336 });
2337 }
2338 arg = arg->NextSiblingElement("arg");
2339 }
2340 const char* name = signals->Attribute("name");
2341 if (name != nullptr)
2342 {
2343 nlohmann::json::object_t object;
2344 object["name"] = name;
2345 object["args"] = argsArray;
2346 signalsArray.push_back(std::move(object));
2347 }
2348
2349 signals = signals->NextSiblingElement("signal");
2350 }
2351
2352 tinyxml2::XMLElement* property =
2353 interface->FirstChildElement("property");
2354 while (property != nullptr)
2355 {
2356 const char* name = property->Attribute("name");
2357 const char* type = property->Attribute("type");
2358 if (type != nullptr && name != nullptr)
2359 {
Patrick Williams59d494e2022-07-22 19:26:55 -05002360 sdbusplus::message_t m =
Ed Tanous002d39b2022-05-31 08:59:27 -07002361 crow::connections::systemBus->new_method_call(
2362 processName.c_str(), objectPath.c_str(),
2363 "org.freedesktop."
2364 "DBus."
2365 "Properties",
2366 "Get");
2367 m.append(interfaceName, name);
2368 nlohmann::json& propertyItem = propertiesObj[name];
2369 crow::connections::systemBus->async_send(
2370 m, [&propertyItem,
Ed Tanous02cad962022-06-30 16:50:15 -07002371 asyncResp](const boost::system::error_code& e,
Patrick Williams59d494e2022-07-22 19:26:55 -05002372 sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002373 if (e)
Ed Tanous1656b292022-05-04 11:33:42 -07002374 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002375 return;
Ed Tanous1656b292022-05-04 11:33:42 -07002376 }
Ed Tanous1656b292022-05-04 11:33:42 -07002377
Ed Tanous002d39b2022-05-31 08:59:27 -07002378 convertDBusToJSON("v", msg, propertyItem);
2379 });
Ed Tanous1656b292022-05-04 11:33:42 -07002380 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002381 property = property->NextSiblingElement("property");
2382 }
Ed Tanous1656b292022-05-04 11:33:42 -07002383 },
2384 processName, objectPath, "org.freedesktop.DBus.Introspectable",
2385 "Introspect");
2386 }
2387 else
2388 {
2389 if (req.method() != boost::beast::http::verb::post)
2390 {
2391 asyncResp->res.result(boost::beast::http::status::not_found);
2392 return;
2393 }
2394
2395 nlohmann::json requestDbusData =
2396 nlohmann::json::parse(req.body, nullptr, false);
2397
2398 if (requestDbusData.is_discarded())
2399 {
2400 asyncResp->res.result(boost::beast::http::status::bad_request);
2401 return;
2402 }
2403 if (!requestDbusData.is_array())
2404 {
2405 asyncResp->res.result(boost::beast::http::status::bad_request);
2406 return;
2407 }
2408 auto transaction =
2409 std::make_shared<InProgressActionData>(asyncResp->res);
2410
2411 transaction->path = objectPath;
2412 transaction->methodName = methodName;
2413 transaction->arguments = std::move(requestDbusData);
2414
2415 findActionOnInterface(transaction, processName);
2416 }
2417}
2418
Ed Tanous23a21a12020-07-25 04:45:05 +00002419inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002420{
2421 BMCWEB_ROUTE(app, "/bus/")
Ed Tanous432a8902021-06-14 15:28:56 -07002422 .privileges({{"Login"}})
Ed Tanousb41187f2019-10-24 16:30:02 -07002423 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +08002424 [](const crow::Request&,
2425 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002426 nlohmann::json::array_t buses;
2427 nlohmann::json& bus = buses.emplace_back();
2428 bus["name"] = "system";
2429 asyncResp->res.jsonValue["busses"] = std::move(buses);
2430 asyncResp->res.jsonValue["status"] = "ok";
2431 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002432
2433 BMCWEB_ROUTE(app, "/bus/system/")
Ed Tanous432a8902021-06-14 15:28:56 -07002434 .privileges({{"Login"}})
Ed Tanousb41187f2019-10-24 16:30:02 -07002435 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +08002436 [](const crow::Request&,
2437 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002438 auto myCallback = [asyncResp](const boost::system::error_code ec,
zhanghch058d1b46d2021-04-01 11:18:24 +08002439 std::vector<std::string>& names) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002440 if (ec)
2441 {
2442 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
2443 asyncResp->res.result(
2444 boost::beast::http::status::internal_server_error);
2445 }
2446 else
2447 {
2448 std::sort(names.begin(), names.end());
2449 asyncResp->res.jsonValue["status"] = "ok";
2450 auto& objectsSub = asyncResp->res.jsonValue["objects"];
Ed Tanous02cad962022-06-30 16:50:15 -07002451 for (const auto& name : names)
Ed Tanous002d39b2022-05-31 08:59:27 -07002452 {
2453 nlohmann::json::object_t object;
2454 object["name"] = name;
2455 objectsSub.push_back(std::move(object));
2456 }
2457 }
2458 };
2459 crow::connections::systemBus->async_method_call(
2460 std::move(myCallback), "org.freedesktop.DBus", "/",
2461 "org.freedesktop.DBus", "ListNames");
2462 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002463
2464 BMCWEB_ROUTE(app, "/list/")
Ed Tanous432a8902021-06-14 15:28:56 -07002465 .privileges({{"Login"}})
Ed Tanousb41187f2019-10-24 16:30:02 -07002466 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +08002467 [](const crow::Request&,
2468 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002469 handleList(asyncResp, "/");
2470 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002471
2472 BMCWEB_ROUTE(app, "/xyz/<path>")
Ed Tanous432a8902021-06-14 15:28:56 -07002473 .privileges({{"Login"}})
zhanghch058d1b46d2021-04-01 11:18:24 +08002474 .methods(boost::beast::http::verb::get)(
2475 [](const crow::Request& req,
2476 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002477 const std::string& path) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002478 std::string objectPath = "/xyz/" + path;
2479 handleDBusUrl(req, asyncResp, objectPath);
2480 });
zhanghch058d1b46d2021-04-01 11:18:24 +08002481
2482 BMCWEB_ROUTE(app, "/xyz/<path>")
Ed Tanous432a8902021-06-14 15:28:56 -07002483 .privileges({{"ConfigureComponents", "ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +08002484 .methods(boost::beast::http::verb::put, boost::beast::http::verb::post,
2485 boost::beast::http::verb::delete_)(
2486 [](const crow::Request& req,
2487 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2488 const std::string& path) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002489 std::string objectPath = "/xyz/" + path;
2490 handleDBusUrl(req, asyncResp, objectPath);
2491 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002492
Ed Tanous049a0512018-11-01 13:58:42 -07002493 BMCWEB_ROUTE(app, "/org/<path>")
Ed Tanous432a8902021-06-14 15:28:56 -07002494 .privileges({{"Login"}})
zhanghch058d1b46d2021-04-01 11:18:24 +08002495 .methods(boost::beast::http::verb::get)(
2496 [](const crow::Request& req,
2497 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2498 const std::string& path) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002499 std::string objectPath = "/org/" + path;
2500 handleDBusUrl(req, asyncResp, objectPath);
2501 });
Tanousf00032d2018-11-05 01:18:10 -03002502
2503 BMCWEB_ROUTE(app, "/org/<path>")
Ed Tanous432a8902021-06-14 15:28:56 -07002504 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -07002505 .methods(boost::beast::http::verb::put, boost::beast::http::verb::post,
2506 boost::beast::http::verb::delete_)(
zhanghch058d1b46d2021-04-01 11:18:24 +08002507 [](const crow::Request& req,
2508 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002509 const std::string& path) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002510 std::string objectPath = "/org/" + path;
2511 handleDBusUrl(req, asyncResp, objectPath);
2512 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002513
Ed Tanous1abe55e2018-09-05 08:30:59 -07002514 BMCWEB_ROUTE(app, "/download/dump/<str>/")
Ed Tanous432a8902021-06-14 15:28:56 -07002515 .privileges({{"ConfigureManager"}})
zhanghch058d1b46d2021-04-01 11:18:24 +08002516 .methods(boost::beast::http::verb::get)(
2517 [](const crow::Request&,
2518 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2519 const std::string& dumpId) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002520 if (!validateFilename(dumpId))
2521 {
2522 asyncResp->res.result(boost::beast::http::status::bad_request);
2523 return;
2524 }
2525 std::filesystem::path loc("/var/lib/phosphor-debug-collector/dumps");
Ramesh Iyyard9207042019-07-05 08:04:42 -05002526
Ed Tanous002d39b2022-05-31 08:59:27 -07002527 loc /= dumpId;
Ramesh Iyyard9207042019-07-05 08:04:42 -05002528
Ed Tanous002d39b2022-05-31 08:59:27 -07002529 if (!std::filesystem::exists(loc) ||
2530 !std::filesystem::is_directory(loc))
2531 {
2532 BMCWEB_LOG_ERROR << loc.string() << "Not found";
2533 asyncResp->res.result(boost::beast::http::status::not_found);
2534 return;
2535 }
2536 std::filesystem::directory_iterator files(loc);
zhanghch058d1b46d2021-04-01 11:18:24 +08002537
Ed Tanous002d39b2022-05-31 08:59:27 -07002538 for (const auto& file : files)
2539 {
2540 std::ifstream readFile(file.path());
2541 if (!readFile.good())
2542 {
2543 continue;
2544 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002545
Ed Tanousd9f6c622022-03-17 09:12:17 -07002546 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07002547 "application/octet-stream");
zhanghch058d1b46d2021-04-01 11:18:24 +08002548
Ed Tanous002d39b2022-05-31 08:59:27 -07002549 // Assuming only one dump file will be present in the dump
2550 // id directory
2551 std::string dumpFileName = file.path().filename().string();
zhanghch058d1b46d2021-04-01 11:18:24 +08002552
Ed Tanous002d39b2022-05-31 08:59:27 -07002553 // Filename should be in alphanumeric, dot and underscore
2554 // Its based on phosphor-debug-collector application
2555 // dumpfile format
2556 std::regex dumpFileRegex("[a-zA-Z0-9\\._]+");
2557 if (!std::regex_match(dumpFileName, dumpFileRegex))
2558 {
2559 BMCWEB_LOG_ERROR << "Invalid dump filename " << dumpFileName;
zhanghch058d1b46d2021-04-01 11:18:24 +08002560 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanousad18f072018-11-14 14:07:48 -08002561 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002562 }
2563 std::string contentDispositionParam =
2564 "attachment; filename=\"" + dumpFileName + "\"";
2565
Ed Tanousd9f6c622022-03-17 09:12:17 -07002566 asyncResp->res.addHeader(
2567 boost::beast::http::field::content_disposition,
2568 contentDispositionParam);
Ed Tanous002d39b2022-05-31 08:59:27 -07002569
2570 asyncResp->res.body() = {std::istreambuf_iterator<char>(readFile),
2571 std::istreambuf_iterator<char>()};
2572 return;
2573 }
2574 asyncResp->res.result(boost::beast::http::status::not_found);
2575 return;
2576 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002577
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002578 BMCWEB_ROUTE(app, "/bus/system/<str>/")
Ed Tanous432a8902021-06-14 15:28:56 -07002579 .privileges({{"Login"}})
Ed Tanousb41187f2019-10-24 16:30:02 -07002580
2581 .methods(boost::beast::http::verb::get)(
zhanghch058d1b46d2021-04-01 11:18:24 +08002582 [](const crow::Request&,
2583 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous81ce6092020-12-17 16:54:55 +00002584 const std::string& connection) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002585 introspectObjects(connection, "/", asyncResp);
2586 });
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002587
2588 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
Ed Tanous432a8902021-06-14 15:28:56 -07002589 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanous1656b292022-05-04 11:33:42 -07002590 .methods(boost::beast::http::verb::get,
2591 boost::beast::http::verb::post)(handleBusSystemPost);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002592}
2593} // namespace openbmc_mapper
2594} // namespace crow