blob: 0ab39084731d51bff7403e09bb7cd96b6f9d6f4e [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 Tanous911ac312017-08-15 09:37:42 -070016#include <crow/app.h>
Ed Tanous911ac312017-08-15 09:37:42 -070017#include <tinyxml2.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -070018
Ed Tanouse3cb5a32018-08-08 14:16:49 -070019#include <async_resp.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070020#include <boost/algorithm/string.hpp>
21#include <boost/container/flat_set.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070022#include <dbus_singleton.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070023#include <dbus_utility.hpp>
Ed Tanousd4bb9bb2018-05-16 13:36:42 -070024#include <experimental/filesystem>
25#include <fstream>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -070026#include <sdbusplus/message/types.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace crow
29{
30namespace openbmc_mapper
31{
Ed Tanousba9f9a62017-10-11 16:40:35 -070032
Matt Spinler3ae4ba72018-12-05 14:01:22 -060033using GetSubTreeType = std::vector<
34 std::pair<std::string,
35 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
36
Matt Spinler2ae60092018-12-06 10:35:36 -060037const std::string notFoundMsg = "404 Not Found";
38const std::string notFoundDesc =
39 "org.freedesktop.DBus.Error.FileNotFound: path or object not found";
Matt Spinlerdc2f9f12018-12-06 13:53:53 -060040const std::string propNotFoundDesc = "The specified property cannot be found";
Matt Spinler2ae60092018-12-06 10:35:36 -060041
42void setErrorResponse(crow::Response &res, boost::beast::http::status result,
43 const std::string &desc, const std::string &msg)
44{
45 res.result(result);
46 res.jsonValue = {{"data", {{"description", desc}}},
47 {"message", msg},
48 {"status", "error"}};
49}
50
Ed Tanouse3cb5a32018-08-08 14:16:49 -070051void introspectObjects(const std::string &processName,
52 const std::string &objectPath,
53 std::shared_ptr<bmcweb::AsyncResp> transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -070054{
Ed Tanouse3cb5a32018-08-08 14:16:49 -070055 if (transaction->res.jsonValue.is_null())
56 {
57 transaction->res.jsonValue = {{"status", "ok"},
58 {"bus_name", processName},
59 {"objects", nlohmann::json::array()}};
60 }
61
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -070063 [transaction, processName{std::string(processName)},
64 objectPath{std::string(objectPath)}](
65 const boost::system::error_code ec,
66 const std::string &introspect_xml) {
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 if (ec)
68 {
69 BMCWEB_LOG_ERROR
70 << "Introspect call failed with error: " << ec.message()
71 << " on process: " << processName << " path: " << objectPath
72 << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -070073 return;
74 }
75 transaction->res.jsonValue["objects"].push_back(
76 {{"path", objectPath}});
77
78 tinyxml2::XMLDocument doc;
79
80 doc.Parse(introspect_xml.c_str());
81 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
82 if (pRoot == nullptr)
83 {
84 BMCWEB_LOG_ERROR << "XML document failed to parse "
85 << processName << " " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -070086 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 else
88 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -070089 tinyxml2::XMLElement *node = pRoot->FirstChildElement("node");
90 while (node != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -070091 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -070092 const char *childPath = node->Attribute("name");
93 if (childPath != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -070094 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 std::string newpath;
96 if (objectPath != "/")
97 {
98 newpath += objectPath;
99 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700100 newpath += std::string("/") + childPath;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 // introspect the subobjects as well
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700102 introspectObjects(processName, newpath, transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700104
105 node = node->NextSiblingElement("node");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 }
107 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700109 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -0700111}
Ed Tanous64530012018-02-06 17:08:16 -0800112
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600113void getPropertiesForEnumerate(const std::string &objectPath,
114 const std::string &service,
115 const std::string &interface,
116 std::shared_ptr<bmcweb::AsyncResp> asyncResp)
117{
118 BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " "
119 << service << " " << interface;
120
121 crow::connections::systemBus->async_method_call(
122 [asyncResp, objectPath, service,
123 interface](const boost::system::error_code ec,
124 const std::vector<
125 std::pair<std::string, dbus::utility::DbusVariantType>>
126 &propertiesList) {
127 if (ec)
128 {
129 BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface "
130 << interface << " service " << service
131 << " failed with code " << ec;
132 return;
133 }
134
135 nlohmann::json &dataJson = asyncResp->res.jsonValue["data"];
136 nlohmann::json &objectJson = dataJson[objectPath];
137 if (objectJson.is_null())
138 {
139 objectJson = nlohmann::json::object();
140 }
141
142 for (const auto &[name, value] : propertiesList)
143 {
144 nlohmann::json &propertyJson = objectJson[name];
145 sdbusplus::message::variant_ns::visit(
146 [&propertyJson](auto &&val) { propertyJson = val; }, value);
147 }
148 },
149 service, objectPath, "org.freedesktop.DBus.Properties", "GetAll",
150 interface);
151}
152
153// Find any results that weren't picked up by ObjectManagers, to be
154// called after all ObjectManagers are searched for and called.
155void findRemainingObjectsForEnumerate(
156 const std::string &objectPath, std::shared_ptr<GetSubTreeType> subtree,
157 std::shared_ptr<bmcweb::AsyncResp> asyncResp)
158{
159 BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate";
160 const nlohmann::json &dataJson = asyncResp->res.jsonValue["data"];
161
162 for (const auto &[path, interface_map] : *subtree)
163 {
164 if (path == objectPath)
165 {
166 // An enumerate does not return the target path's properties
167 continue;
168 }
169 if (dataJson.find(path) == dataJson.end())
170 {
171 for (const auto &[service, interfaces] : interface_map)
172 {
173 for (const auto &interface : interfaces)
174 {
175 if (!boost::starts_with(interface, "org.freedesktop.DBus"))
176 {
177 getPropertiesForEnumerate(path, service, interface,
178 asyncResp);
179 }
180 }
181 }
182 }
183 }
184}
185
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600186struct InProgressEnumerateData
187{
188 InProgressEnumerateData(const std::string &objectPath,
189 std::shared_ptr<bmcweb::AsyncResp> asyncResp) :
190 objectPath(objectPath),
191 asyncResp(asyncResp)
192 {
193 }
194
195 ~InProgressEnumerateData()
196 {
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600197 findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp);
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600198 }
199
200 const std::string objectPath;
201 std::shared_ptr<GetSubTreeType> subtree;
202 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
203};
204
205void getManagedObjectsForEnumerate(
206 const std::string &object_name, const std::string &object_manager_path,
207 const std::string &connection_name,
208 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209{
Ed Tanous049a0512018-11-01 13:58:42 -0700210 BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << object_name
211 << " object_manager_path " << object_manager_path
212 << " connection_name " << connection_name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600214 [transaction, object_name,
Ed Tanous049a0512018-11-01 13:58:42 -0700215 connection_name](const boost::system::error_code ec,
216 const dbus::utility::ManagedObjectType &objects) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 if (ec)
218 {
Ed Tanous049a0512018-11-01 13:58:42 -0700219 BMCWEB_LOG_ERROR << "GetManagedObjects on path " << object_name
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600220 << " on connection " << connection_name
Ed Tanous049a0512018-11-01 13:58:42 -0700221 << " failed with code " << ec;
222 return;
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700223 }
Ed Tanous64530012018-02-06 17:08:16 -0800224
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600225 nlohmann::json &dataJson =
226 transaction->asyncResp->res.jsonValue["data"];
Ed Tanous049a0512018-11-01 13:58:42 -0700227
228 for (const auto &objectPath : objects)
229 {
230 if (boost::starts_with(objectPath.first.str, object_name))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700231 {
Ed Tanous049a0512018-11-01 13:58:42 -0700232 BMCWEB_LOG_DEBUG << "Reading object "
233 << objectPath.first.str;
234 nlohmann::json &objectJson = dataJson[objectPath.first.str];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 if (objectJson.is_null())
236 {
237 objectJson = nlohmann::json::object();
238 }
239 for (const auto &interface : objectPath.second)
240 {
241 for (const auto &property : interface.second)
242 {
243 nlohmann::json &propertyJson =
244 objectJson[property.first];
William A. Kennington III0a63b1c2018-10-18 13:37:19 -0700245 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246 [&propertyJson](auto &&val) {
247 propertyJson = val;
248 },
249 property.second);
250 }
251 }
252 }
Ed Tanous049a0512018-11-01 13:58:42 -0700253 for (const auto &interface : objectPath.second)
254 {
255 if (interface.first == "org.freedesktop.DBus.ObjectManager")
256 {
257 getManagedObjectsForEnumerate(
258 objectPath.first.str, objectPath.first.str,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600259 connection_name, transaction);
Ed Tanous049a0512018-11-01 13:58:42 -0700260 }
261 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 }
263 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700264 connection_name, object_manager_path,
265 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
266}
267
268void findObjectManagerPathForEnumerate(
269 const std::string &object_name, const std::string &connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600270 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700271{
Ed Tanous049a0512018-11-01 13:58:42 -0700272 BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << object_name
273 << " on connection:" << connection_name;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700274 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600275 [transaction, object_name, connection_name](
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700276 const boost::system::error_code ec,
277 const boost::container::flat_map<
278 std::string, boost::container::flat_map<
279 std::string, std::vector<std::string>>>
280 &objects) {
281 if (ec)
282 {
Ed Tanous049a0512018-11-01 13:58:42 -0700283 BMCWEB_LOG_ERROR << "GetAncestors on path " << object_name
284 << " failed with code " << ec;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700285 return;
286 }
287
Ed Tanousf254ba72018-10-12 13:40:35 -0700288 for (const auto &pathGroup : objects)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700289 {
Ed Tanousf254ba72018-10-12 13:40:35 -0700290 for (const auto &connectionGroup : pathGroup.second)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700291 {
292 if (connectionGroup.first == connection_name)
293 {
294 // Found the object manager path for this resource.
295 getManagedObjectsForEnumerate(
Ed Tanous049a0512018-11-01 13:58:42 -0700296 object_name, pathGroup.first, connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600297 transaction);
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700298 return;
299 }
300 }
301 }
302 },
303 "xyz.openbmc_project.ObjectMapper",
304 "/xyz/openbmc_project/object_mapper",
305 "xyz.openbmc_project.ObjectMapper", "GetAncestors", object_name,
306 std::array<const char *, 1>{"org.freedesktop.DBus.ObjectManager"});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700307}
Ed Tanous64530012018-02-06 17:08:16 -0800308
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600309// Uses GetObject to add the object info about the target /enumerate path to the
310// results of GetSubTree, as GetSubTree will not return info for the
311// target path, and then continues on enumerating the rest of the tree.
312void getObjectAndEnumerate(std::shared_ptr<InProgressEnumerateData> transaction)
313{
314 using GetObjectType =
315 std::vector<std::pair<std::string, std::vector<std::string>>>;
316
317 crow::connections::systemBus->async_method_call(
318 [transaction](const boost::system::error_code ec,
319 const GetObjectType &objects) {
320 if (ec)
321 {
322 BMCWEB_LOG_ERROR << "GetObject for path "
323 << transaction->objectPath
324 << " failed with code " << ec;
325 return;
326 }
327
328 BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath
329 << " has " << objects.size() << " entries";
330 if (!objects.empty())
331 {
332 transaction->subtree->emplace_back(transaction->objectPath,
333 objects);
334 }
335
336 // Map indicating connection name, and the path where the object
337 // manager exists
338 boost::container::flat_map<std::string, std::string> connections;
339
340 for (const auto &object : *(transaction->subtree))
341 {
342 for (const auto &connection : object.second)
343 {
344 std::string &objectManagerPath =
345 connections[connection.first];
346 for (const auto &interface : connection.second)
347 {
348 BMCWEB_LOG_DEBUG << connection.first
349 << " has interface " << interface;
350 if (interface == "org.freedesktop.DBus.ObjectManager")
351 {
352 BMCWEB_LOG_DEBUG << "found object manager path "
353 << object.first;
354 objectManagerPath = object.first;
355 }
356 }
357 }
358 }
359 BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections";
360
361 for (const auto &connection : connections)
362 {
363 // If we already know where the object manager is, we don't need
364 // to search for it, we can call directly in to
365 // getManagedObjects
366 if (!connection.second.empty())
367 {
368 getManagedObjectsForEnumerate(
369 transaction->objectPath, connection.second,
370 connection.first, transaction);
371 }
372 else
373 {
374 // otherwise we need to find the object manager path before
375 // we can continue
376 findObjectManagerPathForEnumerate(
377 transaction->objectPath, connection.first, transaction);
378 }
379 }
380 },
381 "xyz.openbmc_project.ObjectMapper",
382 "/xyz/openbmc_project/object_mapper",
383 "xyz.openbmc_project.ObjectMapper", "GetObject",
384 transaction->objectPath, std::array<const char *, 0>());
385}
Ed Tanous64530012018-02-06 17:08:16 -0800386
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700387// Structure for storing data on an in progress action
Ed Tanous1abe55e2018-09-05 08:30:59 -0700388struct InProgressActionData
389{
390 InProgressActionData(crow::Response &res) : res(res){};
391 ~InProgressActionData()
392 {
393 if (res.result() == boost::beast::http::status::internal_server_error)
394 {
395 // Reset the json object to clear out any data that made it in
396 // before the error happened todo(ed) handle error condition with
397 // proper code
398 res.jsonValue = nlohmann::json::object();
399 }
400 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700401 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700402
Ed Tanous1abe55e2018-09-05 08:30:59 -0700403 void setErrorStatus()
404 {
405 res.result(boost::beast::http::status::internal_server_error);
406 }
407 crow::Response &res;
408 std::string path;
409 std::string methodName;
410 nlohmann::json arguments;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700411};
412
Ed Tanous1abe55e2018-09-05 08:30:59 -0700413std::vector<std::string> dbusArgSplit(const std::string &string)
414{
415 std::vector<std::string> ret;
416 if (string.empty())
417 {
418 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700419 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700420 ret.push_back("");
421 int containerDepth = 0;
422
423 for (std::string::const_iterator character = string.begin();
424 character != string.end(); character++)
425 {
426 ret.back() += *character;
427 switch (*character)
428 {
429 case ('a'):
430 break;
431 case ('('):
432 case ('{'):
433 containerDepth++;
434 break;
435 case ('}'):
436 case (')'):
437 containerDepth--;
438 if (containerDepth == 0)
439 {
440 if (character + 1 != string.end())
441 {
442 ret.push_back("");
443 }
444 }
445 break;
446 default:
447 if (containerDepth == 0)
448 {
449 if (character + 1 != string.end())
450 {
451 ret.push_back("");
452 }
453 }
454 break;
455 }
456 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700457}
458
Ed Tanousd76323e2018-08-07 14:35:40 -0700459int convertJsonToDbus(sd_bus_message *m, const std::string &arg_type,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700460 const nlohmann::json &input_json)
461{
462 int r = 0;
463 BMCWEB_LOG_DEBUG << "Converting " << input_json.dump()
464 << " to type: " << arg_type;
465 const std::vector<std::string> argTypes = dbusArgSplit(arg_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700466
Ed Tanous1abe55e2018-09-05 08:30:59 -0700467 // Assume a single object for now.
468 const nlohmann::json *j = &input_json;
469 nlohmann::json::const_iterator jIt = input_json.begin();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700470
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700471 for (const std::string &argCode : argTypes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700472 {
473 // If we are decoding multiple objects, grab the pointer to the
474 // iterator, and increment it for the next loop
475 if (argTypes.size() > 1)
476 {
477 if (jIt == input_json.end())
478 {
479 return -2;
480 }
481 j = &*jIt;
482 jIt++;
483 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700484 const int64_t *intValue = j->get_ptr<const int64_t *>();
485 const uint64_t *uintValue = j->get_ptr<const uint64_t *>();
486 const std::string *stringValue = j->get_ptr<const std::string *>();
487 const double *doubleValue = j->get_ptr<const double *>();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700488 const bool *b = j->get_ptr<const bool *>();
489 int64_t v = 0;
490 double d = 0.0;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700491
Ed Tanous1abe55e2018-09-05 08:30:59 -0700492 // Do some basic type conversions that make sense. uint can be
493 // converted to int. int and uint can be converted to double
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700494 if (uintValue != nullptr && intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700495 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700496 v = static_cast<int64_t>(*uintValue);
497 intValue = &v;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700498 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700499 if (uintValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700500 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700501 d = static_cast<double>(*uintValue);
502 doubleValue = &d;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700503 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700504 if (intValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700505 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700506 d = static_cast<double>(*intValue);
507 doubleValue = &d;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700508 }
509
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700510 if (argCode == "s")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700511 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700512 if (stringValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700513 {
514 return -1;
515 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700516 r = sd_bus_message_append_basic(m, argCode[0],
517 (void *)stringValue->c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518 if (r < 0)
519 {
520 return r;
521 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700522 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700523 else if (argCode == "i")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700524 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700525 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 {
527 return -1;
528 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700529 int32_t i = static_cast<int32_t>(*intValue);
530 r = sd_bus_message_append_basic(m, argCode[0], &i);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700531 if (r < 0)
532 {
533 return r;
534 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700535 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700536 else if (argCode == "b")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700537 {
538 // lots of ways bool could be represented here. Try them all
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700539 int boolInt = false;
540 if (intValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700542 boolInt = *intValue > 0 ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543 }
544 else if (b != nullptr)
545 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700546 boolInt = b ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700548 else if (stringValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700549 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700550 boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700551 }
552 else
553 {
554 return -1;
555 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700556 r = sd_bus_message_append_basic(m, argCode[0], &boolInt);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700557 if (r < 0)
558 {
559 return r;
560 }
561 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700562 else if (argCode == "n")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700563 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700564 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700565 {
566 return -1;
567 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700568 int16_t n = static_cast<int16_t>(*intValue);
569 r = sd_bus_message_append_basic(m, argCode[0], &n);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700570 if (r < 0)
571 {
572 return r;
573 }
574 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700575 else if (argCode == "x")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700576 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700577 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700578 {
579 return -1;
580 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700581 r = sd_bus_message_append_basic(m, argCode[0], intValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700582 if (r < 0)
583 {
584 return r;
585 }
586 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700587 else if (argCode == "y")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700588 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700589 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700590 {
591 return -1;
592 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700593 uint8_t y = static_cast<uint8_t>(*uintValue);
594 r = sd_bus_message_append_basic(m, argCode[0], &y);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700595 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700596 else if (argCode == "q")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700597 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700598 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700599 {
600 return -1;
601 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700602 uint16_t q = static_cast<uint16_t>(*uintValue);
603 r = sd_bus_message_append_basic(m, argCode[0], &q);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700604 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700605 else if (argCode == "u")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700606 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700607 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608 {
609 return -1;
610 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700611 uint32_t u = static_cast<uint32_t>(*uintValue);
612 r = sd_bus_message_append_basic(m, argCode[0], &u);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700614 else if (argCode == "t")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700616 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700617 {
618 return -1;
619 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700620 r = sd_bus_message_append_basic(m, argCode[0], uintValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700622 else if (argCode == "d")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700624 sd_bus_message_append_basic(m, argCode[0], doubleValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700626 else if (boost::starts_with(argCode, "a"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700627 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700628 std::string containedType = argCode.substr(1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700629 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700630 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700631 if (r < 0)
632 {
633 return r;
634 }
635
636 for (nlohmann::json::const_iterator it = j->begin(); it != j->end();
637 ++it)
638 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700639 r = convertJsonToDbus(m, containedType, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640 if (r < 0)
641 {
642 return r;
643 }
644
645 it++;
646 }
647 sd_bus_message_close_container(m);
648 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700649 else if (boost::starts_with(argCode, "v"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700651 std::string containedType = argCode.substr(1);
652 BMCWEB_LOG_DEBUG << "variant type: " << argCode
653 << " appending variant of type: " << containedType;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700654 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700655 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700656 if (r < 0)
657 {
658 return r;
659 }
660
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700661 r = convertJsonToDbus(m, containedType, input_json);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662 if (r < 0)
663 {
664 return r;
665 }
666
667 r = sd_bus_message_close_container(m);
668 if (r < 0)
669 {
670 return r;
671 }
672 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700673 else if (boost::starts_with(argCode, "(") &&
674 boost::ends_with(argCode, ")"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700675 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700676 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700677 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700678 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 nlohmann::json::const_iterator it = j->begin();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700680 for (const std::string &argCode : dbusArgSplit(arg_type))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 {
682 if (it == j->end())
683 {
684 return -1;
685 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700686 r = convertJsonToDbus(m, argCode, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700687 if (r < 0)
688 {
689 return r;
690 }
691 it++;
692 }
693 r = sd_bus_message_close_container(m);
694 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700695 else if (boost::starts_with(argCode, "{") &&
696 boost::ends_with(argCode, "}"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700697 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700698 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700699 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700700 containedType.c_str());
701 std::vector<std::string> codes = dbusArgSplit(containedType);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700702 if (codes.size() != 2)
703 {
704 return -1;
705 }
706 const std::string &key_type = codes[0];
707 const std::string &value_type = codes[1];
708 for (auto it : j->items())
709 {
710 r = convertJsonToDbus(m, key_type, it.key());
711 if (r < 0)
712 {
713 return r;
714 }
715
716 r = convertJsonToDbus(m, value_type, it.value());
717 if (r < 0)
718 {
719 return r;
720 }
721 }
722 r = sd_bus_message_close_container(m);
723 }
724 else
725 {
726 return -2;
727 }
728 if (r < 0)
729 {
730 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700731 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700732
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733 if (argTypes.size() > 1)
734 {
735 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700736 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700737 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700738}
739
Ed Tanousd76323e2018-08-07 14:35:40 -0700740void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700741 const std::string &connectionName)
742{
743 BMCWEB_LOG_DEBUG << "findActionOnInterface for connection "
744 << connectionName;
745 crow::connections::systemBus->async_method_call(
746 [transaction, connectionName{std::string(connectionName)}](
747 const boost::system::error_code ec,
748 const std::string &introspect_xml) {
749 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
750 if (ec)
751 {
752 BMCWEB_LOG_ERROR
753 << "Introspect call failed with error: " << ec.message()
754 << " on process: " << connectionName << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700755 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700756 else
757 {
758 tinyxml2::XMLDocument doc;
759
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700760 doc.Parse(introspect_xml.data(), introspect_xml.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700761 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
762 if (pRoot == nullptr)
763 {
764 BMCWEB_LOG_ERROR << "XML document failed to parse "
765 << connectionName << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700766 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700767 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700768 tinyxml2::XMLElement *interfaceNode =
769 pRoot->FirstChildElement("interface");
770 while (interfaceNode != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700771 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700772 const char *thisInterfaceName =
773 interfaceNode->Attribute("name");
774 if (thisInterfaceName != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700775 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700776 tinyxml2::XMLElement *methodNode =
777 interfaceNode->FirstChildElement("method");
778 while (methodNode != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700779 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700780 const char *thisMethodName =
781 methodNode->Attribute("name");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700782 BMCWEB_LOG_DEBUG << "Found method: "
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700783 << thisMethodName;
784 if (thisMethodName != nullptr &&
785 thisMethodName == transaction->methodName)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700786 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700787 BMCWEB_LOG_DEBUG
788 << "Found method named " << thisMethodName
789 << " on interface " << thisInterfaceName;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700790 sdbusplus::message::message m =
791 crow::connections::systemBus
792 ->new_method_call(
793 connectionName.c_str(),
794 transaction->path.c_str(),
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700795 thisInterfaceName,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700796 transaction->methodName.c_str());
797
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700798 tinyxml2::XMLElement *argumentNode =
799 methodNode->FirstChildElement("arg");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700801 nlohmann::json::const_iterator argIt =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700802 transaction->arguments.begin();
803
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700804 while (argumentNode != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700805 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700806 const char *argDirection =
807 argumentNode->Attribute("direction");
808 const char *argType =
809 argumentNode->Attribute("type");
810 if (argDirection != nullptr &&
811 argType != nullptr &&
812 std::string(argDirection) == "in")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700813 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700814
815 if (argIt ==
Ed Tanous1abe55e2018-09-05 08:30:59 -0700816 transaction->arguments.end())
817 {
818 transaction->setErrorStatus();
819 return;
820 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700821 if (convertJsonToDbus(
822 m.get(), std::string(argType),
823 *argIt) < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700824 {
825 transaction->setErrorStatus();
826 return;
827 }
828
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700829 argIt++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700830 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700831 argumentNode =
832 methodNode->NextSiblingElement("arg");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700833 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700834
Ed Tanous1abe55e2018-09-05 08:30:59 -0700835 crow::connections::systemBus->async_send(
836 m, [transaction](
837 boost::system::error_code ec,
838 sdbusplus::message::message &m) {
839 if (ec)
840 {
841 transaction->setErrorStatus();
842 return;
843 }
844 transaction->res.jsonValue = {
845 {"status", "ok"},
846 {"message", "200 OK"},
847 {"data", nullptr}};
848 });
849 break;
850 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700851 methodNode =
852 methodNode->NextSiblingElement("method");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700854 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700855 interfaceNode =
856 interfaceNode->NextSiblingElement("interface");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 }
858 }
859 },
860 connectionName, transaction->path,
861 "org.freedesktop.DBus.Introspectable", "Introspect");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700862}
863
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700864void handleAction(const crow::Request &req, crow::Response &res,
865 const std::string &objectPath, const std::string &methodName)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700866{
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700867 BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method "
868 << methodName;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700869 nlohmann::json requestDbusData =
870 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700871
Ed Tanous1abe55e2018-09-05 08:30:59 -0700872 if (requestDbusData.is_discarded())
873 {
874 res.result(boost::beast::http::status::bad_request);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700875 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700876 return;
877 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700878 nlohmann::json::iterator data = requestDbusData.find("data");
879 if (data == requestDbusData.end())
880 {
881 res.result(boost::beast::http::status::bad_request);
882 res.end();
883 return;
884 }
885
886 if (!data->is_array())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887 {
888 res.result(boost::beast::http::status::bad_request);
889 res.end();
890 return;
891 }
892 auto transaction = std::make_shared<InProgressActionData>(res);
893
894 transaction->path = objectPath;
895 transaction->methodName = methodName;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700896 transaction->arguments = std::move(*data);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700897 crow::connections::systemBus->async_method_call(
898 [transaction](
899 const boost::system::error_code ec,
900 const std::vector<std::pair<std::string, std::vector<std::string>>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700901 &interfaceNames) {
902 if (ec || interfaceNames.size() <= 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700904 BMCWEB_LOG_ERROR << "Can't find object";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 transaction->setErrorStatus();
906 return;
907 }
908
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700909 BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size()
910 << " object(s)";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700911
912 for (const std::pair<std::string, std::vector<std::string>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700913 &object : interfaceNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700914 {
915 findActionOnInterface(transaction, object.first);
916 }
917 },
918 "xyz.openbmc_project.ObjectMapper",
919 "/xyz/openbmc_project/object_mapper",
920 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
921 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700922}
923
Ed Tanousf839dfe2018-11-12 11:11:15 -0800924void handleList(crow::Response &res, const std::string &objectPath,
925 int32_t depth = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700926{
927 crow::connections::systemBus->async_method_call(
928 [&res](const boost::system::error_code ec,
929 std::vector<std::string> &objectPaths) {
930 if (ec)
931 {
932 res.result(boost::beast::http::status::internal_server_error);
933 }
934 else
935 {
936 res.jsonValue = {{"status", "ok"},
937 {"message", "200 OK"},
938 {"data", std::move(objectPaths)}};
939 }
940 res.end();
941 },
942 "xyz.openbmc_project.ObjectMapper",
943 "/xyz/openbmc_project/object_mapper",
944 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousf839dfe2018-11-12 11:11:15 -0800945 depth, std::array<std::string, 0>());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700946}
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700947
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700948void handleEnumerate(crow::Response &res, const std::string &objectPath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700949{
Ed Tanous049a0512018-11-01 13:58:42 -0700950 BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath;
951 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
952
953 asyncResp->res.jsonValue = {{"message", "200 OK"},
954 {"status", "ok"},
955 {"data", nlohmann::json::object()}};
956
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600958 [objectPath, asyncResp](const boost::system::error_code ec,
959 GetSubTreeType &object_names) {
960 auto transaction = std::make_shared<InProgressEnumerateData>(
961 objectPath, asyncResp);
962
963 transaction->subtree =
964 std::make_shared<GetSubTreeType>(std::move(object_names));
965
Ed Tanous1abe55e2018-09-05 08:30:59 -0700966 if (ec)
967 {
Matt Spinler2ae60092018-12-06 10:35:36 -0600968 BMCWEB_LOG_ERROR << "GetSubTree failed on "
969 << transaction->objectPath;
970 setErrorResponse(transaction->asyncResp->res,
971 boost::beast::http::status::not_found,
972 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973 return;
974 }
Ed Tanous64530012018-02-06 17:08:16 -0800975
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600976 // Add the data for the path passed in to the results
977 // as if GetSubTree returned it, and continue on enumerating
978 getObjectAndEnumerate(transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700979 },
980 "xyz.openbmc_project.ObjectMapper",
981 "/xyz/openbmc_project/object_mapper",
982 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath,
Ed Tanous049a0512018-11-01 13:58:42 -0700983 static_cast<int32_t>(0), std::array<const char *, 0>());
Ed Tanous64530012018-02-06 17:08:16 -0800984}
Ed Tanous911ac312017-08-15 09:37:42 -0700985
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700986void handleGet(crow::Response &res, std::string &objectPath,
987 std::string &destProperty)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700988{
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700989 BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty;
990 std::shared_ptr<std::string> propertyName =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 std::make_shared<std::string>(std::move(destProperty));
Ed Tanous75db20e2018-07-27 13:44:44 -0700992
Ed Tanous1abe55e2018-09-05 08:30:59 -0700993 std::shared_ptr<std::string> path =
994 std::make_shared<std::string>(std::move(objectPath));
Ed Tanous75db20e2018-07-27 13:44:44 -0700995
Ed Tanous1abe55e2018-09-05 08:30:59 -0700996 using GetObjectType =
997 std::vector<std::pair<std::string, std::vector<std::string>>>;
998 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700999 [&res, path, propertyName](const boost::system::error_code ec,
1000 const GetObjectType &object_names) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001001 if (ec || object_names.size() <= 0)
1002 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001003 setErrorResponse(res, boost::beast::http::status::not_found,
1004 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001005 res.end();
1006 return;
1007 }
1008 std::shared_ptr<nlohmann::json> response =
1009 std::make_shared<nlohmann::json>(nlohmann::json::object());
1010 // The mapper should never give us an empty interface names list,
1011 // but check anyway
1012 for (const std::pair<std::string, std::vector<std::string>>
1013 connection : object_names)
1014 {
1015 const std::vector<std::string> &interfaceNames =
1016 connection.second;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001017
Ed Tanous1abe55e2018-09-05 08:30:59 -07001018 if (interfaceNames.size() <= 0)
1019 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001020 setErrorResponse(res, boost::beast::http::status::not_found,
1021 notFoundDesc, notFoundMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001022 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001023 return;
1024 }
1025
1026 for (const std::string &interface : interfaceNames)
1027 {
1028 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001029 [&res, response, propertyName](
Ed Tanous1abe55e2018-09-05 08:30:59 -07001030 const boost::system::error_code ec,
James Feist5b4aa862018-08-16 14:07:01 -07001031 const std::vector<std::pair<
1032 std::string, dbus::utility::DbusVariantType>>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001033 &properties) {
1034 if (ec)
1035 {
1036 BMCWEB_LOG_ERROR << "Bad dbus request error: "
1037 << ec;
1038 }
1039 else
1040 {
James Feist5b4aa862018-08-16 14:07:01 -07001041 for (const std::pair<
1042 std::string,
1043 dbus::utility::DbusVariantType>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001044 &property : properties)
1045 {
1046 // if property name is empty, or matches our
1047 // search query, add it to the response json
1048
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001049 if (propertyName->empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001050 {
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07001051 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001052 [&response, &property](auto &&val) {
1053 (*response)[property.first] =
1054 val;
1055 },
1056 property.second);
1057 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001058 else if (property.first == *propertyName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001059 {
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07001060 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001061 [&response](auto &&val) {
1062 (*response) = val;
1063 },
1064 property.second);
1065 }
1066 }
1067 }
1068 if (response.use_count() == 1)
1069 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001070 if (!propertyName->empty() && response->empty())
1071 {
1072 setErrorResponse(
1073 res,
1074 boost::beast::http::status::not_found,
1075 propNotFoundDesc, notFoundMsg);
1076 }
1077 else
1078 {
1079 res.jsonValue = {{"status", "ok"},
1080 {"message", "200 OK"},
1081 {"data", *response}};
1082 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001083 res.end();
1084 }
1085 },
1086 connection.first, *path,
1087 "org.freedesktop.DBus.Properties", "GetAll", interface);
1088 }
1089 }
1090 },
1091 "xyz.openbmc_project.ObjectMapper",
1092 "/xyz/openbmc_project/object_mapper",
1093 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
1094 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001095}
1096
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097struct AsyncPutRequest
1098{
1099 AsyncPutRequest(crow::Response &res) : res(res)
1100 {
1101 res.jsonValue = {
1102 {"status", "ok"}, {"message", "200 OK"}, {"data", nullptr}};
1103 }
1104 ~AsyncPutRequest()
1105 {
1106 if (res.result() == boost::beast::http::status::internal_server_error)
1107 {
1108 // Reset the json object to clear out any data that made it in
1109 // before the error happened todo(ed) handle error condition with
1110 // proper code
1111 res.jsonValue = nlohmann::json::object();
1112 }
1113
1114 if (res.jsonValue.empty())
1115 {
1116 res.result(boost::beast::http::status::forbidden);
1117 res.jsonValue = {
1118 {"status", "error"},
1119 {"message", "403 Forbidden"},
1120 {"data",
1121 {{"message", "The specified property cannot be created: " +
1122 propertyName}}}};
1123 }
1124
1125 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001126 }
1127
Ed Tanous1abe55e2018-09-05 08:30:59 -07001128 void setErrorStatus()
1129 {
1130 res.result(boost::beast::http::status::internal_server_error);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001131 }
1132
Ed Tanous1abe55e2018-09-05 08:30:59 -07001133 crow::Response &res;
1134 std::string objectPath;
1135 std::string propertyName;
1136 nlohmann::json propertyValue;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001137};
1138
Ed Tanousd76323e2018-08-07 14:35:40 -07001139void handlePut(const crow::Request &req, crow::Response &res,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001140 const std::string &objectPath, const std::string &destProperty)
1141{
1142 nlohmann::json requestDbusData =
1143 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001144
Ed Tanous1abe55e2018-09-05 08:30:59 -07001145 if (requestDbusData.is_discarded())
1146 {
1147 res.result(boost::beast::http::status::bad_request);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001148 res.end();
1149 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001150 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001151
Ed Tanous1abe55e2018-09-05 08:30:59 -07001152 nlohmann::json::const_iterator propertyIt = requestDbusData.find("data");
1153 if (propertyIt == requestDbusData.end())
1154 {
1155 res.result(boost::beast::http::status::bad_request);
1156 res.end();
1157 return;
1158 }
1159 const nlohmann::json &propertySetValue = *propertyIt;
1160 auto transaction = std::make_shared<AsyncPutRequest>(res);
1161 transaction->objectPath = objectPath;
1162 transaction->propertyName = destProperty;
1163 transaction->propertyValue = propertySetValue;
Ed Tanous911ac312017-08-15 09:37:42 -07001164
Ed Tanous1abe55e2018-09-05 08:30:59 -07001165 using GetObjectType =
1166 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous911ac312017-08-15 09:37:42 -07001167
Ed Tanous1abe55e2018-09-05 08:30:59 -07001168 crow::connections::systemBus->async_method_call(
1169 [transaction](const boost::system::error_code ec,
1170 const GetObjectType &object_names) {
1171 if (!ec && object_names.size() <= 0)
1172 {
1173 transaction->res.result(boost::beast::http::status::not_found);
1174 return;
1175 }
Ed Tanous911ac312017-08-15 09:37:42 -07001176
Ed Tanous1abe55e2018-09-05 08:30:59 -07001177 for (const std::pair<std::string, std::vector<std::string>>
1178 connection : object_names)
1179 {
1180 const std::string &connectionName = connection.first;
Ed Tanous911ac312017-08-15 09:37:42 -07001181
Ed Tanous1abe55e2018-09-05 08:30:59 -07001182 crow::connections::systemBus->async_method_call(
1183 [connectionName{std::string(connectionName)},
1184 transaction](const boost::system::error_code ec,
1185 const std::string &introspectXml) {
1186 if (ec)
1187 {
1188 BMCWEB_LOG_ERROR
1189 << "Introspect call failed with error: "
1190 << ec.message()
1191 << " on process: " << connectionName;
1192 transaction->setErrorStatus();
1193 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001194 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001195 tinyxml2::XMLDocument doc;
Ed Tanous911ac312017-08-15 09:37:42 -07001196
Ed Tanous1abe55e2018-09-05 08:30:59 -07001197 doc.Parse(introspectXml.c_str());
1198 tinyxml2::XMLNode *pRoot =
1199 doc.FirstChildElement("node");
1200 if (pRoot == nullptr)
1201 {
1202 BMCWEB_LOG_ERROR << "XML document failed to parse: "
1203 << introspectXml;
1204 transaction->setErrorStatus();
1205 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001206 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001207 tinyxml2::XMLElement *ifaceNode =
1208 pRoot->FirstChildElement("interface");
1209 while (ifaceNode != nullptr)
1210 {
1211 const char *interfaceName =
1212 ifaceNode->Attribute("name");
1213 BMCWEB_LOG_DEBUG << "found interface "
1214 << interfaceName;
1215 tinyxml2::XMLElement *propNode =
1216 ifaceNode->FirstChildElement("property");
1217 while (propNode != nullptr)
1218 {
1219 const char *propertyName =
1220 propNode->Attribute("name");
1221 BMCWEB_LOG_DEBUG << "Found property "
1222 << propertyName;
1223 if (propertyName == transaction->propertyName)
1224 {
1225 const char *argType =
1226 propNode->Attribute("type");
1227 if (argType != nullptr)
1228 {
1229 sdbusplus::message::message m =
1230 crow::connections::systemBus
1231 ->new_method_call(
1232 connectionName.c_str(),
1233 transaction->objectPath
1234 .c_str(),
1235 "org.freedesktop.DBus."
1236 "Properties",
1237 "Set");
1238 m.append(interfaceName,
1239 transaction->propertyName);
1240 int r = sd_bus_message_open_container(
1241 m.get(), SD_BUS_TYPE_VARIANT,
1242 argType);
1243 if (r < 0)
1244 {
1245 transaction->setErrorStatus();
1246 return;
1247 }
1248 r = convertJsonToDbus(
1249 m.get(), argType,
1250 transaction->propertyValue);
1251 if (r < 0)
1252 {
1253 transaction->setErrorStatus();
1254 return;
1255 }
1256 r = sd_bus_message_close_container(
1257 m.get());
1258 if (r < 0)
1259 {
1260 transaction->setErrorStatus();
1261 return;
1262 }
Ed Tanous911ac312017-08-15 09:37:42 -07001263
Ed Tanous1abe55e2018-09-05 08:30:59 -07001264 crow::connections::systemBus
1265 ->async_send(
1266 m,
1267 [transaction](
1268 boost::system::error_code
1269 ec,
1270 sdbusplus::message::message
1271 &m) {
1272 BMCWEB_LOG_DEBUG << "sent";
1273 if (ec)
1274 {
1275 transaction->res
1276 .jsonValue
1277 ["status"] =
1278 "error";
1279 transaction->res
1280 .jsonValue
1281 ["message"] =
1282 ec.message();
1283 }
1284 });
1285 }
1286 }
1287 propNode =
1288 propNode->NextSiblingElement("property");
1289 }
1290 ifaceNode =
1291 ifaceNode->NextSiblingElement("interface");
1292 }
1293 },
1294 connectionName, transaction->objectPath,
1295 "org.freedesktop.DBus.Introspectable", "Introspect");
1296 }
1297 },
1298 "xyz.openbmc_project.ObjectMapper",
1299 "/xyz/openbmc_project/object_mapper",
1300 "xyz.openbmc_project.ObjectMapper", "GetObject",
1301 transaction->objectPath, std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001302}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001303
Ed Tanous049a0512018-11-01 13:58:42 -07001304inline void handleDBusUrl(const crow::Request &req, crow::Response &res,
1305 std::string &objectPath)
1306{
Ed Tanous049a0512018-11-01 13:58:42 -07001307
1308 // If accessing a single attribute, fill in and update objectPath,
1309 // otherwise leave destProperty blank
1310 std::string destProperty = "";
1311 const char *attrSeperator = "/attr/";
1312 size_t attrPosition = objectPath.find(attrSeperator);
1313 if (attrPosition != objectPath.npos)
1314 {
1315 destProperty = objectPath.substr(attrPosition + strlen(attrSeperator),
1316 objectPath.length());
1317 objectPath = objectPath.substr(0, attrPosition);
1318 }
1319
1320 if (req.method() == "POST"_method)
1321 {
1322 constexpr const char *actionSeperator = "/action/";
1323 size_t actionPosition = objectPath.find(actionSeperator);
1324 if (actionPosition != objectPath.npos)
1325 {
1326 std::string postProperty =
1327 objectPath.substr((actionPosition + strlen(actionSeperator)),
1328 objectPath.length());
1329 objectPath = objectPath.substr(0, actionPosition);
1330 handleAction(req, res, objectPath, postProperty);
1331 return;
1332 }
1333 }
1334 else if (req.method() == "GET"_method)
1335 {
1336 if (boost::ends_with(objectPath, "/enumerate"))
1337 {
1338 objectPath.erase(objectPath.end() - sizeof("enumerate"),
1339 objectPath.end());
1340 handleEnumerate(res, objectPath);
1341 }
1342 else if (boost::ends_with(objectPath, "/list"))
1343 {
1344 objectPath.erase(objectPath.end() - sizeof("list"),
1345 objectPath.end());
1346 handleList(res, objectPath);
1347 }
1348 else
1349 {
Ed Tanousf839dfe2018-11-12 11:11:15 -08001350 // Trim any trailing "/" at the end
1351 if (boost::ends_with(objectPath, "/"))
1352 {
1353 objectPath.pop_back();
1354 handleList(res, objectPath, 1);
1355 }
1356 else
1357 {
1358 handleGet(res, objectPath, destProperty);
1359 }
Ed Tanous049a0512018-11-01 13:58:42 -07001360 }
1361 return;
1362 }
1363 else if (req.method() == "PUT"_method)
1364 {
1365 handlePut(req, res, objectPath, destProperty);
1366 return;
1367 }
1368
1369 res.result(boost::beast::http::status::method_not_allowed);
1370 res.end();
1371}
1372
Ed Tanous1abe55e2018-09-05 08:30:59 -07001373template <typename... Middlewares> void requestRoutes(Crow<Middlewares...> &app)
1374{
1375 BMCWEB_ROUTE(app, "/bus/")
1376 .methods("GET"_method)(
1377 [](const crow::Request &req, crow::Response &res) {
1378 res.jsonValue = {{"busses", {{{"name", "system"}}}},
1379 {"status", "ok"}};
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001380 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001381 });
1382
1383 BMCWEB_ROUTE(app, "/bus/system/")
1384 .methods("GET"_method)(
1385 [](const crow::Request &req, crow::Response &res) {
1386 auto myCallback = [&res](const boost::system::error_code ec,
1387 std::vector<std::string> &names) {
1388 if (ec)
1389 {
1390 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
1391 res.result(
1392 boost::beast::http::status::internal_server_error);
1393 }
1394 else
1395 {
1396 std::sort(names.begin(), names.end());
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001397 res.jsonValue = {{"status", "ok"}};
1398 auto &objectsSub = res.jsonValue["objects"];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001399 for (auto &name : names)
1400 {
1401 objectsSub.push_back({{"name", name}});
1402 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001403 }
1404 res.end();
1405 };
1406 crow::connections::systemBus->async_method_call(
1407 std::move(myCallback), "org.freedesktop.DBus", "/",
1408 "org.freedesktop.DBus", "ListNames");
1409 });
1410
1411 BMCWEB_ROUTE(app, "/list/")
1412 .methods("GET"_method)(
1413 [](const crow::Request &req, crow::Response &res) {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001414 handleList(res, "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001415 });
1416
1417 BMCWEB_ROUTE(app, "/xyz/<path>")
Ed Tanous049a0512018-11-01 13:58:42 -07001418 .methods("GET"_method, "PUT"_method, "POST"_method)(
1419 [](const crow::Request &req, crow::Response &res,
1420 const std::string &path) {
1421 std::string objectPath = "/xyz/" + path;
1422 handleDBusUrl(req, res, objectPath);
1423 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001424
Ed Tanous049a0512018-11-01 13:58:42 -07001425 BMCWEB_ROUTE(app, "/org/<path>")
1426 .methods("GET"_method, "PUT"_method, "POST"_method)(
1427 [](const crow::Request &req, crow::Response &res,
1428 const std::string &path) {
1429 std::string objectPath = "/org/" + path;
1430 handleDBusUrl(req, res, objectPath);
1431 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001432
Ed Tanous1abe55e2018-09-05 08:30:59 -07001433 BMCWEB_ROUTE(app, "/download/dump/<str>/")
1434 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
1435 const std::string &dumpId) {
1436 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]+)$");
1437 if (!std::regex_match(dumpId, validFilename))
1438 {
1439 res.result(boost::beast::http::status::not_found);
1440 res.end();
1441 return;
1442 }
1443 std::experimental::filesystem::path loc(
1444 "/var/lib/phosphor-debug-collector/dumps");
1445
1446 loc += dumpId;
1447
1448 if (!std::experimental::filesystem::exists(loc) ||
1449 !std::experimental::filesystem::is_directory(loc))
1450 {
1451 res.result(boost::beast::http::status::not_found);
1452 res.end();
1453 return;
1454 }
1455 std::experimental::filesystem::directory_iterator files(loc);
1456 for (auto &file : files)
1457 {
1458 std::ifstream readFile(file.path());
1459 if (readFile.good())
1460 {
1461 continue;
1462 }
1463 res.addHeader("Content-Type", "application/octet-stream");
1464 res.body() = {std::istreambuf_iterator<char>(readFile),
1465 std::istreambuf_iterator<char>()};
1466 res.end();
1467 }
1468 res.result(boost::beast::http::status::not_found);
1469 res.end();
1470 return;
1471 });
1472
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001473 BMCWEB_ROUTE(app, "/bus/system/<str>/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001474 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001475 const std::string &Connection) {
1476 introspectObjects(Connection, "/",
1477 std::make_shared<bmcweb::AsyncResp>(res));
1478 });
1479
1480 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
1481 .methods("GET"_method,
1482 "POST"_method)([](const crow::Request &req,
1483 crow::Response &res,
1484 const std::string &processName,
1485 const std::string &requestedPath) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001486 std::vector<std::string> strs;
1487 boost::split(strs, requestedPath, boost::is_any_of("/"));
1488 std::string objectPath;
1489 std::string interfaceName;
1490 std::string methodName;
1491 auto it = strs.begin();
1492 if (it == strs.end())
1493 {
1494 objectPath = "/";
1495 }
1496 while (it != strs.end())
1497 {
1498 // Check if segment contains ".". If it does, it must be an
1499 // interface
1500 if (it->find(".") != std::string::npos)
1501 {
1502 break;
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001503 // This check is neccesary as the trailing slash gets parsed
Ed Tanous1abe55e2018-09-05 08:30:59 -07001504 // as part of our <path> specifier above, which causes the
1505 // normal trailing backslash redirector to fail.
1506 }
1507 else if (!it->empty())
1508 {
1509 objectPath += "/" + *it;
1510 }
1511 it++;
1512 }
1513 if (it != strs.end())
1514 {
1515 interfaceName = *it;
1516 it++;
1517
1518 // after interface, we might have a method name
1519 if (it != strs.end())
1520 {
1521 methodName = *it;
1522 it++;
1523 }
1524 }
1525 if (it != strs.end())
1526 {
1527 // if there is more levels past the method name, something went
1528 // wrong, return not found
1529 res.result(boost::beast::http::status::not_found);
1530 res.end();
1531 return;
1532 }
1533 if (interfaceName.empty())
1534 {
1535 crow::connections::systemBus->async_method_call(
1536 [&, processName,
1537 objectPath](const boost::system::error_code ec,
1538 const std::string &introspect_xml) {
1539 if (ec)
1540 {
1541 BMCWEB_LOG_ERROR
1542 << "Introspect call failed with error: "
1543 << ec.message()
1544 << " on process: " << processName
1545 << " path: " << objectPath << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001546 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001547 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001548 tinyxml2::XMLDocument doc;
1549
1550 doc.Parse(introspect_xml.c_str());
1551 tinyxml2::XMLNode *pRoot =
1552 doc.FirstChildElement("node");
1553 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001554 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001555 BMCWEB_LOG_ERROR << "XML document failed to parse "
1556 << processName << " " << objectPath
1557 << "\n";
1558 res.jsonValue = {{"status", "XML parse error"}};
1559 res.result(boost::beast::http::status::
1560 internal_server_error);
1561 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001562 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001563
1564 BMCWEB_LOG_DEBUG << introspect_xml;
1565 res.jsonValue = {{"status", "ok"},
1566 {"bus_name", processName},
1567 {"object_path", objectPath}};
1568 nlohmann::json &interfacesArray =
1569 res.jsonValue["interfaces"];
1570 interfacesArray = nlohmann::json::array();
1571 tinyxml2::XMLElement *interface =
1572 pRoot->FirstChildElement("interface");
1573
1574 while (interface != nullptr)
1575 {
1576 const char *ifaceName =
1577 interface->Attribute("name");
1578 if (ifaceName != nullptr)
1579 {
1580 interfacesArray.push_back(
1581 {{"name", ifaceName}});
1582 }
1583
1584 interface =
1585 interface->NextSiblingElement("interface");
1586 }
1587
Ed Tanous1abe55e2018-09-05 08:30:59 -07001588 res.end();
1589 },
1590 processName, objectPath,
1591 "org.freedesktop.DBus.Introspectable", "Introspect");
1592 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001593 else if (methodName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001594 {
1595 crow::connections::systemBus->async_method_call(
1596 [&, processName, objectPath,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001597 interfaceName{std::move(interfaceName)}](
Ed Tanous1abe55e2018-09-05 08:30:59 -07001598 const boost::system::error_code ec,
1599 const std::string &introspect_xml) {
1600 if (ec)
1601 {
1602 BMCWEB_LOG_ERROR
1603 << "Introspect call failed with error: "
1604 << ec.message()
1605 << " on process: " << processName
1606 << " path: " << objectPath << "\n";
1607 }
1608 else
1609 {
1610 tinyxml2::XMLDocument doc;
1611
1612 doc.Parse(introspect_xml.c_str());
1613 tinyxml2::XMLNode *pRoot =
1614 doc.FirstChildElement("node");
1615 if (pRoot == nullptr)
1616 {
1617 BMCWEB_LOG_ERROR
1618 << "XML document failed to parse "
1619 << processName << " " << objectPath << "\n";
1620 res.result(boost::beast::http::status::
1621 internal_server_error);
1622 }
1623 else
1624 {
1625 tinyxml2::XMLElement *node =
1626 pRoot->FirstChildElement("node");
1627
1628 // if we know we're the only call, build the
1629 // json directly
Ed Tanous1abe55e2018-09-05 08:30:59 -07001630 tinyxml2::XMLElement *interface =
1631 pRoot->FirstChildElement("interface");
1632
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001633 res.jsonValue = {
1634 {"status", "ok"},
1635 {"bus_name", processName},
1636 {"interface", interfaceName},
1637 {"object_path", objectPath},
1638 {"properties", nlohmann::json::object()}};
1639
1640 nlohmann::json &methodsArray =
1641 res.jsonValue["methods"];
1642 methodsArray = nlohmann::json::array();
1643
1644 nlohmann::json &signalsArray =
1645 res.jsonValue["signals"];
1646 signalsArray = nlohmann::json::array();
1647
Ed Tanous1abe55e2018-09-05 08:30:59 -07001648 while (interface != nullptr)
1649 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001650 const char *ifaceName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001651 interface->Attribute("name");
1652
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001653 if (ifaceName != nullptr &&
1654 ifaceName == interfaceName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001655 {
1656 tinyxml2::XMLElement *methods =
1657 interface->FirstChildElement(
1658 "method");
1659 while (methods != nullptr)
1660 {
1661 nlohmann::json argsArray =
1662 nlohmann::json::array();
1663 tinyxml2::XMLElement *arg =
1664 methods->FirstChildElement(
1665 "arg");
1666 while (arg != nullptr)
1667 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001668 nlohmann::json thisArg;
1669 for (const char *fieldName :
1670 std::array<const char *,
1671 3>{"name",
1672 "direction",
1673 "type"})
1674 {
1675 const char *fieldValue =
1676 arg->Attribute(
1677 fieldName);
1678 if (fieldValue != nullptr)
1679 {
1680 thisArg[fieldName] =
1681 fieldValue;
1682 }
1683 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001684 argsArray.push_back(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001685 std::move(thisArg));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001686 arg = arg->NextSiblingElement(
1687 "arg");
1688 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001689
1690 const char *name =
1691 methods->Attribute("name");
1692 if (name != nullptr)
1693 {
1694 methodsArray.push_back(
1695 {{"name", name},
1696 {"uri", "/bus/system/" +
1697 processName +
1698 objectPath +
1699 "/" +
1700 interfaceName +
1701 "/" + name},
1702 {"args", argsArray}});
1703 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001704 methods =
1705 methods->NextSiblingElement(
1706 "method");
1707 }
1708 tinyxml2::XMLElement *signals =
1709 interface->FirstChildElement(
1710 "signal");
1711 while (signals != nullptr)
1712 {
1713 nlohmann::json argsArray =
1714 nlohmann::json::array();
1715
1716 tinyxml2::XMLElement *arg =
1717 signals->FirstChildElement(
1718 "arg");
1719 while (arg != nullptr)
1720 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001721 const char *name =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001722 arg->Attribute("name");
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001723 const char *type =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001724 arg->Attribute("type");
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001725 if (name != nullptr &&
1726 type != nullptr)
1727 {
1728 argsArray.push_back({
1729 {"name", name},
1730 {"type", type},
1731 });
1732 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001733 arg = arg->NextSiblingElement(
1734 "arg");
1735 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001736 const char *name =
1737 signals->Attribute("name");
1738 if (name != nullptr)
1739 {
1740 signalsArray.push_back(
1741 {{"name", name},
1742 {"args", argsArray}});
1743 }
1744
Ed Tanous1abe55e2018-09-05 08:30:59 -07001745 signals =
1746 signals->NextSiblingElement(
1747 "signal");
1748 }
1749
Ed Tanous1abe55e2018-09-05 08:30:59 -07001750 break;
1751 }
1752
1753 interface = interface->NextSiblingElement(
1754 "interface");
1755 }
1756 if (interface == nullptr)
1757 {
1758 // if we got to the end of the list and
1759 // never found a match, throw 404
1760 res.result(
1761 boost::beast::http::status::not_found);
1762 }
1763 }
1764 }
1765 res.end();
1766 },
1767 processName, objectPath,
1768 "org.freedesktop.DBus.Introspectable", "Introspect");
1769 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001770 else
1771 {
1772 if (req.method() != "POST"_method)
1773 {
1774 res.result(boost::beast::http::status::not_found);
1775 res.end();
1776 return;
1777 }
1778
1779 nlohmann::json requestDbusData =
1780 nlohmann::json::parse(req.body, nullptr, false);
1781
1782 if (requestDbusData.is_discarded())
1783 {
1784 res.result(boost::beast::http::status::bad_request);
1785 res.end();
1786 return;
1787 }
1788 if (!requestDbusData.is_array())
1789 {
1790 res.result(boost::beast::http::status::bad_request);
1791 res.end();
1792 return;
1793 }
1794 auto transaction = std::make_shared<InProgressActionData>(res);
1795
1796 transaction->path = objectPath;
1797 transaction->methodName = methodName;
1798 transaction->arguments = std::move(requestDbusData);
1799
1800 findActionOnInterface(transaction, processName);
1801 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001802 });
1803}
1804} // namespace openbmc_mapper
1805} // namespace crow