blob: fb23a8bb9df5269e5e0bb3003238812409fc91c7 [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
James Feistf6150402019-01-08 10:36:20 -080016#include "filesystem.hpp"
17
Ed Tanous911ac312017-08-15 09:37:42 -070018#include <crow/app.h>
Ed Tanous911ac312017-08-15 09:37:42 -070019#include <tinyxml2.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -070020
Ed Tanouse3cb5a32018-08-08 14:16:49 -070021#include <async_resp.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070022#include <boost/algorithm/string.hpp>
23#include <boost/container/flat_set.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070024#include <dbus_singleton.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070025#include <dbus_utility.hpp>
Ed Tanousd4bb9bb2018-05-16 13:36:42 -070026#include <fstream>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -070027#include <sdbusplus/message/types.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029namespace crow
30{
31namespace openbmc_mapper
32{
Ed Tanousba9f9a62017-10-11 16:40:35 -070033
Matt Spinler3ae4ba72018-12-05 14:01:22 -060034using GetSubTreeType = std::vector<
35 std::pair<std::string,
36 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
37
Matt Spinler2ae60092018-12-06 10:35:36 -060038const std::string notFoundMsg = "404 Not Found";
Matt Spinler6db06242018-12-11 11:21:22 -060039const std::string badReqMsg = "400 Bad Request";
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -060040const std::string methodNotAllowedMsg = "405 Method Not Allowed";
Matt Spinlerfbc19ea2018-12-11 14:03:42 -060041const std::string forbiddenMsg = "403 Forbidden";
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -060042const std::string methodFailedMsg = "500 Method Call Failed";
Matt Spinler6db06242018-12-11 11:21:22 -060043
Matt Spinler2ae60092018-12-06 10:35:36 -060044const std::string notFoundDesc =
45 "org.freedesktop.DBus.Error.FileNotFound: path or object not found";
Matt Spinlerdc2f9f12018-12-06 13:53:53 -060046const std::string propNotFoundDesc = "The specified property cannot be found";
Matt Spinler6db06242018-12-11 11:21:22 -060047const std::string noJsonDesc = "No JSON object could be decoded";
48const std::string methodNotFoundDesc = "The specified method cannot be found";
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -060049const std::string methodNotAllowedDesc = "Method not allowed";
Matt Spinlerfbc19ea2018-12-11 14:03:42 -060050const std::string forbiddenPropDesc =
51 "The specified property cannot be created";
52const std::string forbiddenResDesc = "The specified resource cannot be created";
Matt Spinler2ae60092018-12-06 10:35:36 -060053
54void setErrorResponse(crow::Response &res, boost::beast::http::status result,
55 const std::string &desc, const std::string &msg)
56{
57 res.result(result);
58 res.jsonValue = {{"data", {{"description", desc}}},
59 {"message", msg},
60 {"status", "error"}};
61}
62
Ed Tanouse3cb5a32018-08-08 14:16:49 -070063void introspectObjects(const std::string &processName,
64 const std::string &objectPath,
65 std::shared_ptr<bmcweb::AsyncResp> transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -070066{
Ed Tanouse3cb5a32018-08-08 14:16:49 -070067 if (transaction->res.jsonValue.is_null())
68 {
69 transaction->res.jsonValue = {{"status", "ok"},
70 {"bus_name", processName},
71 {"objects", nlohmann::json::array()}};
72 }
73
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -070075 [transaction, processName{std::string(processName)},
76 objectPath{std::string(objectPath)}](
77 const boost::system::error_code ec,
78 const std::string &introspect_xml) {
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 if (ec)
80 {
81 BMCWEB_LOG_ERROR
82 << "Introspect call failed with error: " << ec.message()
83 << " on process: " << processName << " path: " << objectPath
84 << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -070085 return;
86 }
87 transaction->res.jsonValue["objects"].push_back(
88 {{"path", objectPath}});
89
90 tinyxml2::XMLDocument doc;
91
92 doc.Parse(introspect_xml.c_str());
93 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
94 if (pRoot == nullptr)
95 {
96 BMCWEB_LOG_ERROR << "XML document failed to parse "
97 << processName << " " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -070098 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 else
100 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700101 tinyxml2::XMLElement *node = pRoot->FirstChildElement("node");
102 while (node != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700104 const char *childPath = node->Attribute("name");
105 if (childPath != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 std::string newpath;
108 if (objectPath != "/")
109 {
110 newpath += objectPath;
111 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700112 newpath += std::string("/") + childPath;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 // introspect the subobjects as well
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700114 introspectObjects(processName, newpath, transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700116
117 node = node->NextSiblingElement("node");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 }
119 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700121 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -0700123}
Ed Tanous64530012018-02-06 17:08:16 -0800124
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600125void getPropertiesForEnumerate(const std::string &objectPath,
126 const std::string &service,
127 const std::string &interface,
128 std::shared_ptr<bmcweb::AsyncResp> asyncResp)
129{
130 BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " "
131 << service << " " << interface;
132
133 crow::connections::systemBus->async_method_call(
134 [asyncResp, objectPath, service,
135 interface](const boost::system::error_code ec,
136 const std::vector<
137 std::pair<std::string, dbus::utility::DbusVariantType>>
138 &propertiesList) {
139 if (ec)
140 {
141 BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface "
142 << interface << " service " << service
143 << " failed with code " << ec;
144 return;
145 }
146
147 nlohmann::json &dataJson = asyncResp->res.jsonValue["data"];
148 nlohmann::json &objectJson = dataJson[objectPath];
149 if (objectJson.is_null())
150 {
151 objectJson = nlohmann::json::object();
152 }
153
154 for (const auto &[name, value] : propertiesList)
155 {
156 nlohmann::json &propertyJson = objectJson[name];
157 sdbusplus::message::variant_ns::visit(
158 [&propertyJson](auto &&val) { propertyJson = val; }, value);
159 }
160 },
161 service, objectPath, "org.freedesktop.DBus.Properties", "GetAll",
162 interface);
163}
164
165// Find any results that weren't picked up by ObjectManagers, to be
166// called after all ObjectManagers are searched for and called.
167void findRemainingObjectsForEnumerate(
168 const std::string &objectPath, std::shared_ptr<GetSubTreeType> subtree,
169 std::shared_ptr<bmcweb::AsyncResp> asyncResp)
170{
171 BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate";
172 const nlohmann::json &dataJson = asyncResp->res.jsonValue["data"];
173
174 for (const auto &[path, interface_map] : *subtree)
175 {
176 if (path == objectPath)
177 {
178 // An enumerate does not return the target path's properties
179 continue;
180 }
181 if (dataJson.find(path) == dataJson.end())
182 {
183 for (const auto &[service, interfaces] : interface_map)
184 {
185 for (const auto &interface : interfaces)
186 {
187 if (!boost::starts_with(interface, "org.freedesktop.DBus"))
188 {
189 getPropertiesForEnumerate(path, service, interface,
190 asyncResp);
191 }
192 }
193 }
194 }
195 }
196}
197
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600198struct InProgressEnumerateData
199{
200 InProgressEnumerateData(const std::string &objectPath,
201 std::shared_ptr<bmcweb::AsyncResp> asyncResp) :
202 objectPath(objectPath),
203 asyncResp(asyncResp)
204 {
205 }
206
207 ~InProgressEnumerateData()
208 {
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600209 findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp);
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600210 }
211
212 const std::string objectPath;
213 std::shared_ptr<GetSubTreeType> subtree;
214 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
215};
216
217void getManagedObjectsForEnumerate(
218 const std::string &object_name, const std::string &object_manager_path,
219 const std::string &connection_name,
220 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221{
Ed Tanous049a0512018-11-01 13:58:42 -0700222 BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << object_name
223 << " object_manager_path " << object_manager_path
224 << " connection_name " << connection_name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600226 [transaction, object_name,
Ed Tanous049a0512018-11-01 13:58:42 -0700227 connection_name](const boost::system::error_code ec,
228 const dbus::utility::ManagedObjectType &objects) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229 if (ec)
230 {
Ed Tanous049a0512018-11-01 13:58:42 -0700231 BMCWEB_LOG_ERROR << "GetManagedObjects on path " << object_name
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600232 << " on connection " << connection_name
Ed Tanous049a0512018-11-01 13:58:42 -0700233 << " failed with code " << ec;
234 return;
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700235 }
Ed Tanous64530012018-02-06 17:08:16 -0800236
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600237 nlohmann::json &dataJson =
238 transaction->asyncResp->res.jsonValue["data"];
Ed Tanous049a0512018-11-01 13:58:42 -0700239
240 for (const auto &objectPath : objects)
241 {
242 if (boost::starts_with(objectPath.first.str, object_name))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700243 {
Ed Tanous049a0512018-11-01 13:58:42 -0700244 BMCWEB_LOG_DEBUG << "Reading object "
245 << objectPath.first.str;
246 nlohmann::json &objectJson = dataJson[objectPath.first.str];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247 if (objectJson.is_null())
248 {
249 objectJson = nlohmann::json::object();
250 }
251 for (const auto &interface : objectPath.second)
252 {
253 for (const auto &property : interface.second)
254 {
255 nlohmann::json &propertyJson =
256 objectJson[property.first];
William A. Kennington III0a63b1c2018-10-18 13:37:19 -0700257 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700258 [&propertyJson](auto &&val) {
259 propertyJson = val;
260 },
261 property.second);
262 }
263 }
264 }
Ed Tanous049a0512018-11-01 13:58:42 -0700265 for (const auto &interface : objectPath.second)
266 {
267 if (interface.first == "org.freedesktop.DBus.ObjectManager")
268 {
269 getManagedObjectsForEnumerate(
270 objectPath.first.str, objectPath.first.str,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600271 connection_name, transaction);
Ed Tanous049a0512018-11-01 13:58:42 -0700272 }
273 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700274 }
275 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700276 connection_name, object_manager_path,
277 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
278}
279
280void findObjectManagerPathForEnumerate(
281 const std::string &object_name, const std::string &connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600282 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700283{
Ed Tanous049a0512018-11-01 13:58:42 -0700284 BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << object_name
285 << " on connection:" << connection_name;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700286 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600287 [transaction, object_name, connection_name](
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700288 const boost::system::error_code ec,
289 const boost::container::flat_map<
290 std::string, boost::container::flat_map<
291 std::string, std::vector<std::string>>>
292 &objects) {
293 if (ec)
294 {
Ed Tanous049a0512018-11-01 13:58:42 -0700295 BMCWEB_LOG_ERROR << "GetAncestors on path " << object_name
296 << " failed with code " << ec;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700297 return;
298 }
299
Ed Tanousf254ba72018-10-12 13:40:35 -0700300 for (const auto &pathGroup : objects)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700301 {
Ed Tanousf254ba72018-10-12 13:40:35 -0700302 for (const auto &connectionGroup : pathGroup.second)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700303 {
304 if (connectionGroup.first == connection_name)
305 {
306 // Found the object manager path for this resource.
307 getManagedObjectsForEnumerate(
Ed Tanous049a0512018-11-01 13:58:42 -0700308 object_name, pathGroup.first, connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600309 transaction);
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700310 return;
311 }
312 }
313 }
314 },
315 "xyz.openbmc_project.ObjectMapper",
316 "/xyz/openbmc_project/object_mapper",
317 "xyz.openbmc_project.ObjectMapper", "GetAncestors", object_name,
318 std::array<const char *, 1>{"org.freedesktop.DBus.ObjectManager"});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700319}
Ed Tanous64530012018-02-06 17:08:16 -0800320
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600321// Uses GetObject to add the object info about the target /enumerate path to the
322// results of GetSubTree, as GetSubTree will not return info for the
323// target path, and then continues on enumerating the rest of the tree.
324void getObjectAndEnumerate(std::shared_ptr<InProgressEnumerateData> transaction)
325{
326 using GetObjectType =
327 std::vector<std::pair<std::string, std::vector<std::string>>>;
328
329 crow::connections::systemBus->async_method_call(
330 [transaction](const boost::system::error_code ec,
331 const GetObjectType &objects) {
332 if (ec)
333 {
334 BMCWEB_LOG_ERROR << "GetObject for path "
335 << transaction->objectPath
336 << " failed with code " << ec;
337 return;
338 }
339
340 BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath
341 << " has " << objects.size() << " entries";
342 if (!objects.empty())
343 {
344 transaction->subtree->emplace_back(transaction->objectPath,
345 objects);
346 }
347
348 // Map indicating connection name, and the path where the object
349 // manager exists
350 boost::container::flat_map<std::string, std::string> connections;
351
352 for (const auto &object : *(transaction->subtree))
353 {
354 for (const auto &connection : object.second)
355 {
356 std::string &objectManagerPath =
357 connections[connection.first];
358 for (const auto &interface : connection.second)
359 {
360 BMCWEB_LOG_DEBUG << connection.first
361 << " has interface " << interface;
362 if (interface == "org.freedesktop.DBus.ObjectManager")
363 {
364 BMCWEB_LOG_DEBUG << "found object manager path "
365 << object.first;
366 objectManagerPath = object.first;
367 }
368 }
369 }
370 }
371 BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections";
372
373 for (const auto &connection : connections)
374 {
375 // If we already know where the object manager is, we don't need
376 // to search for it, we can call directly in to
377 // getManagedObjects
378 if (!connection.second.empty())
379 {
380 getManagedObjectsForEnumerate(
381 transaction->objectPath, connection.second,
382 connection.first, transaction);
383 }
384 else
385 {
386 // otherwise we need to find the object manager path before
387 // we can continue
388 findObjectManagerPathForEnumerate(
389 transaction->objectPath, connection.first, transaction);
390 }
391 }
392 },
393 "xyz.openbmc_project.ObjectMapper",
394 "/xyz/openbmc_project/object_mapper",
395 "xyz.openbmc_project.ObjectMapper", "GetObject",
396 transaction->objectPath, std::array<const char *, 0>());
397}
Ed Tanous64530012018-02-06 17:08:16 -0800398
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700399// Structure for storing data on an in progress action
Ed Tanous1abe55e2018-09-05 08:30:59 -0700400struct InProgressActionData
401{
402 InProgressActionData(crow::Response &res) : res(res){};
403 ~InProgressActionData()
404 {
Matt Spinler6db06242018-12-11 11:21:22 -0600405 // If still no JSON filled in, then we never found the method.
406 if (res.jsonValue.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407 {
Matt Spinler6db06242018-12-11 11:21:22 -0600408 setErrorResponse(res, boost::beast::http::status::not_found,
409 methodNotFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410 }
411 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700412 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700413
Matt Spinler6db06242018-12-11 11:21:22 -0600414 void setErrorStatus(const std::string &desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700415 {
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -0600416 setErrorResponse(res, boost::beast::http::status::bad_request, desc,
417 badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700418 }
419 crow::Response &res;
420 std::string path;
421 std::string methodName;
Matt Spinlerde818812018-12-11 16:39:20 -0600422 std::string interfaceName;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700423 nlohmann::json arguments;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700424};
425
Ed Tanous1abe55e2018-09-05 08:30:59 -0700426std::vector<std::string> dbusArgSplit(const std::string &string)
427{
428 std::vector<std::string> ret;
429 if (string.empty())
430 {
431 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700432 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700433 ret.push_back("");
434 int containerDepth = 0;
435
436 for (std::string::const_iterator character = string.begin();
437 character != string.end(); character++)
438 {
439 ret.back() += *character;
440 switch (*character)
441 {
442 case ('a'):
443 break;
444 case ('('):
445 case ('{'):
446 containerDepth++;
447 break;
448 case ('}'):
449 case (')'):
450 containerDepth--;
451 if (containerDepth == 0)
452 {
453 if (character + 1 != string.end())
454 {
455 ret.push_back("");
456 }
457 }
458 break;
459 default:
460 if (containerDepth == 0)
461 {
462 if (character + 1 != string.end())
463 {
464 ret.push_back("");
465 }
466 }
467 break;
468 }
469 }
Matt Spinler4ae611d2019-01-11 15:37:06 -0600470
471 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700472}
473
Ed Tanousd76323e2018-08-07 14:35:40 -0700474int convertJsonToDbus(sd_bus_message *m, const std::string &arg_type,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700475 const nlohmann::json &input_json)
476{
477 int r = 0;
478 BMCWEB_LOG_DEBUG << "Converting " << input_json.dump()
479 << " to type: " << arg_type;
480 const std::vector<std::string> argTypes = dbusArgSplit(arg_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700481
Ed Tanous1abe55e2018-09-05 08:30:59 -0700482 // Assume a single object for now.
483 const nlohmann::json *j = &input_json;
484 nlohmann::json::const_iterator jIt = input_json.begin();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700485
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700486 for (const std::string &argCode : argTypes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700487 {
488 // If we are decoding multiple objects, grab the pointer to the
489 // iterator, and increment it for the next loop
490 if (argTypes.size() > 1)
491 {
492 if (jIt == input_json.end())
493 {
494 return -2;
495 }
496 j = &*jIt;
497 jIt++;
498 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700499 const int64_t *intValue = j->get_ptr<const int64_t *>();
500 const uint64_t *uintValue = j->get_ptr<const uint64_t *>();
501 const std::string *stringValue = j->get_ptr<const std::string *>();
502 const double *doubleValue = j->get_ptr<const double *>();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700503 const bool *b = j->get_ptr<const bool *>();
504 int64_t v = 0;
505 double d = 0.0;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700506
Ed Tanous1abe55e2018-09-05 08:30:59 -0700507 // Do some basic type conversions that make sense. uint can be
508 // converted to int. int and uint can be converted to double
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700509 if (uintValue != nullptr && intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700510 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700511 v = static_cast<int64_t>(*uintValue);
512 intValue = &v;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700513 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700514 if (uintValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700515 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700516 d = static_cast<double>(*uintValue);
517 doubleValue = &d;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700519 if (intValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700520 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700521 d = static_cast<double>(*intValue);
522 doubleValue = &d;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700523 }
524
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700525 if (argCode == "s")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700527 if (stringValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700528 {
529 return -1;
530 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700531 r = sd_bus_message_append_basic(m, argCode[0],
532 (void *)stringValue->c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700533 if (r < 0)
534 {
535 return r;
536 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700537 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700538 else if (argCode == "i")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700539 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700540 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 {
542 return -1;
543 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700544 int32_t i = static_cast<int32_t>(*intValue);
545 r = sd_bus_message_append_basic(m, argCode[0], &i);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 if (r < 0)
547 {
548 return r;
549 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700550 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700551 else if (argCode == "b")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552 {
553 // lots of ways bool could be represented here. Try them all
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700554 int boolInt = false;
555 if (intValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700557 boolInt = *intValue > 0 ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700558 }
559 else if (b != nullptr)
560 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700561 boolInt = b ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700562 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700563 else if (stringValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700564 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700565 boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700566 }
567 else
568 {
569 return -1;
570 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700571 r = sd_bus_message_append_basic(m, argCode[0], &boolInt);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700572 if (r < 0)
573 {
574 return r;
575 }
576 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700577 else if (argCode == "n")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700578 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700579 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700580 {
581 return -1;
582 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700583 int16_t n = static_cast<int16_t>(*intValue);
584 r = sd_bus_message_append_basic(m, argCode[0], &n);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700585 if (r < 0)
586 {
587 return r;
588 }
589 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700590 else if (argCode == "x")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700591 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700592 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700593 {
594 return -1;
595 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700596 r = sd_bus_message_append_basic(m, argCode[0], intValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700597 if (r < 0)
598 {
599 return r;
600 }
601 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700602 else if (argCode == "y")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700603 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700604 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605 {
606 return -1;
607 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700608 uint8_t y = static_cast<uint8_t>(*uintValue);
609 r = sd_bus_message_append_basic(m, argCode[0], &y);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700610 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700611 else if (argCode == "q")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700612 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700613 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700614 {
615 return -1;
616 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700617 uint16_t q = static_cast<uint16_t>(*uintValue);
618 r = sd_bus_message_append_basic(m, argCode[0], &q);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700619 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700620 else if (argCode == "u")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700622 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623 {
624 return -1;
625 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700626 uint32_t u = static_cast<uint32_t>(*uintValue);
627 r = sd_bus_message_append_basic(m, argCode[0], &u);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700628 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700629 else if (argCode == "t")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700630 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700631 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632 {
633 return -1;
634 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700635 r = sd_bus_message_append_basic(m, argCode[0], uintValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700637 else if (argCode == "d")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700638 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700639 sd_bus_message_append_basic(m, argCode[0], doubleValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700641 else if (boost::starts_with(argCode, "a"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700643 std::string containedType = argCode.substr(1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700645 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646 if (r < 0)
647 {
648 return r;
649 }
650
651 for (nlohmann::json::const_iterator it = j->begin(); it != j->end();
652 ++it)
653 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700654 r = convertJsonToDbus(m, containedType, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 if (r < 0)
656 {
657 return r;
658 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 }
660 sd_bus_message_close_container(m);
661 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700662 else if (boost::starts_with(argCode, "v"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700663 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700664 std::string containedType = argCode.substr(1);
665 BMCWEB_LOG_DEBUG << "variant type: " << argCode
666 << " appending variant of type: " << containedType;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700667 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700668 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700669 if (r < 0)
670 {
671 return r;
672 }
673
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700674 r = convertJsonToDbus(m, containedType, input_json);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700675 if (r < 0)
676 {
677 return r;
678 }
679
680 r = sd_bus_message_close_container(m);
681 if (r < 0)
682 {
683 return r;
684 }
685 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700686 else if (boost::starts_with(argCode, "(") &&
687 boost::ends_with(argCode, ")"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700689 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700690 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700691 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700692 nlohmann::json::const_iterator it = j->begin();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700693 for (const std::string &argCode : dbusArgSplit(arg_type))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700694 {
695 if (it == j->end())
696 {
697 return -1;
698 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700699 r = convertJsonToDbus(m, argCode, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700700 if (r < 0)
701 {
702 return r;
703 }
704 it++;
705 }
706 r = sd_bus_message_close_container(m);
707 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700708 else if (boost::starts_with(argCode, "{") &&
709 boost::ends_with(argCode, "}"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700710 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700711 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700712 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700713 containedType.c_str());
714 std::vector<std::string> codes = dbusArgSplit(containedType);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700715 if (codes.size() != 2)
716 {
717 return -1;
718 }
719 const std::string &key_type = codes[0];
720 const std::string &value_type = codes[1];
721 for (auto it : j->items())
722 {
723 r = convertJsonToDbus(m, key_type, it.key());
724 if (r < 0)
725 {
726 return r;
727 }
728
729 r = convertJsonToDbus(m, value_type, it.value());
730 if (r < 0)
731 {
732 return r;
733 }
734 }
735 r = sd_bus_message_close_container(m);
736 }
737 else
738 {
739 return -2;
740 }
741 if (r < 0)
742 {
743 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700744 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700745
Ed Tanous1abe55e2018-09-05 08:30:59 -0700746 if (argTypes.size() > 1)
747 {
748 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700749 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700750 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700751}
752
Ed Tanousd76323e2018-08-07 14:35:40 -0700753void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700754 const std::string &connectionName)
755{
756 BMCWEB_LOG_DEBUG << "findActionOnInterface for connection "
757 << connectionName;
758 crow::connections::systemBus->async_method_call(
759 [transaction, connectionName{std::string(connectionName)}](
760 const boost::system::error_code ec,
761 const std::string &introspect_xml) {
762 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
763 if (ec)
764 {
765 BMCWEB_LOG_ERROR
766 << "Introspect call failed with error: " << ec.message()
767 << " on process: " << connectionName << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700768 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700769 else
770 {
771 tinyxml2::XMLDocument doc;
772
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700773 doc.Parse(introspect_xml.data(), introspect_xml.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700774 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
775 if (pRoot == nullptr)
776 {
777 BMCWEB_LOG_ERROR << "XML document failed to parse "
778 << connectionName << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700779 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700780 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700781 tinyxml2::XMLElement *interfaceNode =
782 pRoot->FirstChildElement("interface");
783 while (interfaceNode != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700784 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700785 const char *thisInterfaceName =
786 interfaceNode->Attribute("name");
787 if (thisInterfaceName != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700788 {
Matt Spinlerde818812018-12-11 16:39:20 -0600789 if (!transaction->interfaceName.empty() &&
790 (transaction->interfaceName != thisInterfaceName))
791 {
792 interfaceNode =
793 interfaceNode->NextSiblingElement("interface");
794 continue;
795 }
796
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700797 tinyxml2::XMLElement *methodNode =
798 interfaceNode->FirstChildElement("method");
799 while (methodNode != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700801 const char *thisMethodName =
802 methodNode->Attribute("name");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700803 BMCWEB_LOG_DEBUG << "Found method: "
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700804 << thisMethodName;
805 if (thisMethodName != nullptr &&
806 thisMethodName == transaction->methodName)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700807 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700808 BMCWEB_LOG_DEBUG
809 << "Found method named " << thisMethodName
810 << " on interface " << thisInterfaceName;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700811 sdbusplus::message::message m =
812 crow::connections::systemBus
813 ->new_method_call(
814 connectionName.c_str(),
815 transaction->path.c_str(),
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700816 thisInterfaceName,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700817 transaction->methodName.c_str());
818
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700819 tinyxml2::XMLElement *argumentNode =
820 methodNode->FirstChildElement("arg");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700821
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700822 nlohmann::json::const_iterator argIt =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823 transaction->arguments.begin();
824
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700825 while (argumentNode != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700826 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700827 const char *argDirection =
828 argumentNode->Attribute("direction");
829 const char *argType =
830 argumentNode->Attribute("type");
831 if (argDirection != nullptr &&
832 argType != nullptr &&
833 std::string(argDirection) == "in")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700834 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700835 if (argIt ==
Ed Tanous1abe55e2018-09-05 08:30:59 -0700836 transaction->arguments.end())
837 {
Matt Spinler6db06242018-12-11 11:21:22 -0600838 transaction->setErrorStatus(
839 "Invalid method args");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700840 return;
841 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700842 if (convertJsonToDbus(
843 m.get(), std::string(argType),
844 *argIt) < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845 {
Matt Spinler6db06242018-12-11 11:21:22 -0600846 transaction->setErrorStatus(
847 "Invalid method arg type");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700848 return;
849 }
850
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700851 argIt++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700853 argumentNode =
AppaRao Puli4d72dcc2018-12-26 20:26:22 +0530854 argumentNode->NextSiblingElement("arg");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700855 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700856
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 crow::connections::systemBus->async_send(
858 m, [transaction](
859 boost::system::error_code ec,
860 sdbusplus::message::message &m) {
861 if (ec)
862 {
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -0600863 setErrorResponse(
864 transaction->res,
865 boost::beast::http::status::
866 internal_server_error,
867 "Method call failed",
868 methodFailedMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700869 return;
870 }
871 transaction->res.jsonValue = {
872 {"status", "ok"},
873 {"message", "200 OK"},
874 {"data", nullptr}};
875 });
876 break;
877 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700878 methodNode =
879 methodNode->NextSiblingElement("method");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700880 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700882 interfaceNode =
883 interfaceNode->NextSiblingElement("interface");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700884 }
885 }
886 },
887 connectionName, transaction->path,
888 "org.freedesktop.DBus.Introspectable", "Introspect");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700889}
890
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700891void handleAction(const crow::Request &req, crow::Response &res,
892 const std::string &objectPath, const std::string &methodName)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700893{
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700894 BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method "
895 << methodName;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700896 nlohmann::json requestDbusData =
897 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700898
Ed Tanous1abe55e2018-09-05 08:30:59 -0700899 if (requestDbusData.is_discarded())
900 {
Matt Spinler6db06242018-12-11 11:21:22 -0600901 setErrorResponse(res, boost::beast::http::status::bad_request,
902 noJsonDesc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700903 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904 return;
905 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700906 nlohmann::json::iterator data = requestDbusData.find("data");
907 if (data == requestDbusData.end())
908 {
Matt Spinler6db06242018-12-11 11:21:22 -0600909 setErrorResponse(res, boost::beast::http::status::bad_request,
910 noJsonDesc, badReqMsg);
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700911 res.end();
912 return;
913 }
914
915 if (!data->is_array())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Matt Spinler6db06242018-12-11 11:21:22 -0600917 setErrorResponse(res, boost::beast::http::status::bad_request,
918 noJsonDesc, badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 res.end();
920 return;
921 }
922 auto transaction = std::make_shared<InProgressActionData>(res);
923
924 transaction->path = objectPath;
925 transaction->methodName = methodName;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700926 transaction->arguments = std::move(*data);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700927 crow::connections::systemBus->async_method_call(
928 [transaction](
929 const boost::system::error_code ec,
930 const std::vector<std::pair<std::string, std::vector<std::string>>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700931 &interfaceNames) {
932 if (ec || interfaceNames.size() <= 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700933 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700934 BMCWEB_LOG_ERROR << "Can't find object";
Matt Spinler6db06242018-12-11 11:21:22 -0600935 setErrorResponse(transaction->res,
936 boost::beast::http::status::not_found,
937 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938 return;
939 }
940
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700941 BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size()
942 << " object(s)";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943
944 for (const std::pair<std::string, std::vector<std::string>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700945 &object : interfaceNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700946 {
947 findActionOnInterface(transaction, object.first);
948 }
949 },
950 "xyz.openbmc_project.ObjectMapper",
951 "/xyz/openbmc_project/object_mapper",
952 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
953 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700954}
955
Matt Spinlerde818812018-12-11 16:39:20 -0600956void handleDelete(const crow::Request &req, crow::Response &res,
957 const std::string &objectPath)
958{
959 BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath;
960
961 crow::connections::systemBus->async_method_call(
962 [&res, objectPath](
963 const boost::system::error_code ec,
964 const std::vector<std::pair<std::string, std::vector<std::string>>>
965 &interfaceNames) {
966 if (ec || interfaceNames.size() <= 0)
967 {
968 BMCWEB_LOG_ERROR << "Can't find object";
969 setErrorResponse(res, boost::beast::http::status::not_found,
970 notFoundDesc, notFoundMsg);
971 res.end();
972 return;
973 }
974
975 auto transaction = std::make_shared<InProgressActionData>(res);
976 transaction->path = objectPath;
977 transaction->methodName = "Delete";
978 transaction->interfaceName = "xyz.openbmc_project.Object.Delete";
979
980 for (const std::pair<std::string, std::vector<std::string>>
981 &object : interfaceNames)
982 {
983 findActionOnInterface(transaction, object.first);
984 }
985 },
986 "xyz.openbmc_project.ObjectMapper",
987 "/xyz/openbmc_project/object_mapper",
988 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
989 std::array<const char *, 0>());
990}
991
Ed Tanousf839dfe2018-11-12 11:11:15 -0800992void handleList(crow::Response &res, const std::string &objectPath,
993 int32_t depth = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700994{
995 crow::connections::systemBus->async_method_call(
996 [&res](const boost::system::error_code ec,
997 std::vector<std::string> &objectPaths) {
998 if (ec)
999 {
Matt Spinlerd6091dd2018-12-06 14:08:27 -06001000 setErrorResponse(res, boost::beast::http::status::not_found,
1001 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002 }
1003 else
1004 {
1005 res.jsonValue = {{"status", "ok"},
1006 {"message", "200 OK"},
1007 {"data", std::move(objectPaths)}};
1008 }
1009 res.end();
1010 },
1011 "xyz.openbmc_project.ObjectMapper",
1012 "/xyz/openbmc_project/object_mapper",
1013 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousf839dfe2018-11-12 11:11:15 -08001014 depth, std::array<std::string, 0>());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001015}
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001016
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001017void handleEnumerate(crow::Response &res, const std::string &objectPath)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001018{
Ed Tanous049a0512018-11-01 13:58:42 -07001019 BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath;
1020 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
1021
1022 asyncResp->res.jsonValue = {{"message", "200 OK"},
1023 {"status", "ok"},
1024 {"data", nlohmann::json::object()}};
1025
Ed Tanous1abe55e2018-09-05 08:30:59 -07001026 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001027 [objectPath, asyncResp](const boost::system::error_code ec,
1028 GetSubTreeType &object_names) {
1029 auto transaction = std::make_shared<InProgressEnumerateData>(
1030 objectPath, asyncResp);
1031
1032 transaction->subtree =
1033 std::make_shared<GetSubTreeType>(std::move(object_names));
1034
Ed Tanous1abe55e2018-09-05 08:30:59 -07001035 if (ec)
1036 {
Matt Spinler2ae60092018-12-06 10:35:36 -06001037 BMCWEB_LOG_ERROR << "GetSubTree failed on "
1038 << transaction->objectPath;
1039 setErrorResponse(transaction->asyncResp->res,
1040 boost::beast::http::status::not_found,
1041 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001042 return;
1043 }
Ed Tanous64530012018-02-06 17:08:16 -08001044
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001045 // Add the data for the path passed in to the results
1046 // as if GetSubTree returned it, and continue on enumerating
1047 getObjectAndEnumerate(transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001048 },
1049 "xyz.openbmc_project.ObjectMapper",
1050 "/xyz/openbmc_project/object_mapper",
1051 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath,
Ed Tanous049a0512018-11-01 13:58:42 -07001052 static_cast<int32_t>(0), std::array<const char *, 0>());
Ed Tanous64530012018-02-06 17:08:16 -08001053}
Ed Tanous911ac312017-08-15 09:37:42 -07001054
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001055void handleGet(crow::Response &res, std::string &objectPath,
1056 std::string &destProperty)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001057{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001058 BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty;
1059 std::shared_ptr<std::string> propertyName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001060 std::make_shared<std::string>(std::move(destProperty));
Ed Tanous75db20e2018-07-27 13:44:44 -07001061
Ed Tanous1abe55e2018-09-05 08:30:59 -07001062 std::shared_ptr<std::string> path =
1063 std::make_shared<std::string>(std::move(objectPath));
Ed Tanous75db20e2018-07-27 13:44:44 -07001064
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065 using GetObjectType =
1066 std::vector<std::pair<std::string, std::vector<std::string>>>;
1067 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001068 [&res, path, propertyName](const boost::system::error_code ec,
1069 const GetObjectType &object_names) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001070 if (ec || object_names.size() <= 0)
1071 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001072 setErrorResponse(res, boost::beast::http::status::not_found,
1073 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001074 res.end();
1075 return;
1076 }
1077 std::shared_ptr<nlohmann::json> response =
1078 std::make_shared<nlohmann::json>(nlohmann::json::object());
1079 // The mapper should never give us an empty interface names list,
1080 // but check anyway
1081 for (const std::pair<std::string, std::vector<std::string>>
1082 connection : object_names)
1083 {
1084 const std::vector<std::string> &interfaceNames =
1085 connection.second;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001086
Ed Tanous1abe55e2018-09-05 08:30:59 -07001087 if (interfaceNames.size() <= 0)
1088 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001089 setErrorResponse(res, boost::beast::http::status::not_found,
1090 notFoundDesc, notFoundMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001091 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001092 return;
1093 }
1094
1095 for (const std::string &interface : interfaceNames)
1096 {
1097 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001098 [&res, response, propertyName](
Ed Tanous1abe55e2018-09-05 08:30:59 -07001099 const boost::system::error_code ec,
James Feist5b4aa862018-08-16 14:07:01 -07001100 const std::vector<std::pair<
1101 std::string, dbus::utility::DbusVariantType>>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001102 &properties) {
1103 if (ec)
1104 {
1105 BMCWEB_LOG_ERROR << "Bad dbus request error: "
1106 << ec;
1107 }
1108 else
1109 {
James Feist5b4aa862018-08-16 14:07:01 -07001110 for (const std::pair<
1111 std::string,
1112 dbus::utility::DbusVariantType>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001113 &property : properties)
1114 {
1115 // if property name is empty, or matches our
1116 // search query, add it to the response json
1117
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001118 if (propertyName->empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001119 {
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07001120 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001121 [&response, &property](auto &&val) {
1122 (*response)[property.first] =
1123 val;
1124 },
1125 property.second);
1126 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001127 else if (property.first == *propertyName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001128 {
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07001129 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001130 [&response](auto &&val) {
1131 (*response) = val;
1132 },
1133 property.second);
1134 }
1135 }
1136 }
1137 if (response.use_count() == 1)
1138 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001139 if (!propertyName->empty() && response->empty())
1140 {
1141 setErrorResponse(
1142 res,
1143 boost::beast::http::status::not_found,
1144 propNotFoundDesc, notFoundMsg);
1145 }
1146 else
1147 {
1148 res.jsonValue = {{"status", "ok"},
1149 {"message", "200 OK"},
1150 {"data", *response}};
1151 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001152 res.end();
1153 }
1154 },
1155 connection.first, *path,
1156 "org.freedesktop.DBus.Properties", "GetAll", interface);
1157 }
1158 }
1159 },
1160 "xyz.openbmc_project.ObjectMapper",
1161 "/xyz/openbmc_project/object_mapper",
1162 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
1163 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001164}
1165
Ed Tanous1abe55e2018-09-05 08:30:59 -07001166struct AsyncPutRequest
1167{
1168 AsyncPutRequest(crow::Response &res) : res(res)
1169 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001170 }
1171 ~AsyncPutRequest()
1172 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001173 if (res.jsonValue.empty())
1174 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001175 setErrorResponse(res, boost::beast::http::status::forbidden,
1176 forbiddenMsg, forbiddenPropDesc);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001177 }
1178
1179 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001180 }
1181
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001182 void setErrorStatus(const std::string &desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001183 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001184 setErrorResponse(res, boost::beast::http::status::internal_server_error,
1185 desc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001186 }
1187
Ed Tanous1abe55e2018-09-05 08:30:59 -07001188 crow::Response &res;
1189 std::string objectPath;
1190 std::string propertyName;
1191 nlohmann::json propertyValue;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001192};
1193
Ed Tanousd76323e2018-08-07 14:35:40 -07001194void handlePut(const crow::Request &req, crow::Response &res,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001195 const std::string &objectPath, const std::string &destProperty)
1196{
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001197 if (destProperty.empty())
1198 {
1199 setErrorResponse(res, boost::beast::http::status::forbidden,
1200 forbiddenResDesc, forbiddenMsg);
1201 res.end();
1202 return;
1203 }
1204
Ed Tanous1abe55e2018-09-05 08:30:59 -07001205 nlohmann::json requestDbusData =
1206 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001207
Ed Tanous1abe55e2018-09-05 08:30:59 -07001208 if (requestDbusData.is_discarded())
1209 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001210 setErrorResponse(res, boost::beast::http::status::bad_request,
1211 noJsonDesc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001212 res.end();
1213 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001214 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001215
Ed Tanous1abe55e2018-09-05 08:30:59 -07001216 nlohmann::json::const_iterator propertyIt = requestDbusData.find("data");
1217 if (propertyIt == requestDbusData.end())
1218 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001219 setErrorResponse(res, boost::beast::http::status::bad_request,
1220 noJsonDesc, badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001221 res.end();
1222 return;
1223 }
1224 const nlohmann::json &propertySetValue = *propertyIt;
1225 auto transaction = std::make_shared<AsyncPutRequest>(res);
1226 transaction->objectPath = objectPath;
1227 transaction->propertyName = destProperty;
1228 transaction->propertyValue = propertySetValue;
Ed Tanous911ac312017-08-15 09:37:42 -07001229
Ed Tanous1abe55e2018-09-05 08:30:59 -07001230 using GetObjectType =
1231 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous911ac312017-08-15 09:37:42 -07001232
Ed Tanous1abe55e2018-09-05 08:30:59 -07001233 crow::connections::systemBus->async_method_call(
1234 [transaction](const boost::system::error_code ec,
1235 const GetObjectType &object_names) {
1236 if (!ec && object_names.size() <= 0)
1237 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001238 setErrorResponse(transaction->res,
1239 boost::beast::http::status::not_found,
1240 propNotFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001241 return;
1242 }
Ed Tanous911ac312017-08-15 09:37:42 -07001243
Ed Tanous1abe55e2018-09-05 08:30:59 -07001244 for (const std::pair<std::string, std::vector<std::string>>
1245 connection : object_names)
1246 {
1247 const std::string &connectionName = connection.first;
Ed Tanous911ac312017-08-15 09:37:42 -07001248
Ed Tanous1abe55e2018-09-05 08:30:59 -07001249 crow::connections::systemBus->async_method_call(
1250 [connectionName{std::string(connectionName)},
1251 transaction](const boost::system::error_code ec,
1252 const std::string &introspectXml) {
1253 if (ec)
1254 {
1255 BMCWEB_LOG_ERROR
1256 << "Introspect call failed with error: "
1257 << ec.message()
1258 << " on process: " << connectionName;
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001259 transaction->setErrorStatus("Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001260 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001261 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001262 tinyxml2::XMLDocument doc;
Ed Tanous911ac312017-08-15 09:37:42 -07001263
Ed Tanous1abe55e2018-09-05 08:30:59 -07001264 doc.Parse(introspectXml.c_str());
1265 tinyxml2::XMLNode *pRoot =
1266 doc.FirstChildElement("node");
1267 if (pRoot == nullptr)
1268 {
1269 BMCWEB_LOG_ERROR << "XML document failed to parse: "
1270 << introspectXml;
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001271 transaction->setErrorStatus("Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001272 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001273 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001274 tinyxml2::XMLElement *ifaceNode =
1275 pRoot->FirstChildElement("interface");
1276 while (ifaceNode != nullptr)
1277 {
1278 const char *interfaceName =
1279 ifaceNode->Attribute("name");
1280 BMCWEB_LOG_DEBUG << "found interface "
1281 << interfaceName;
1282 tinyxml2::XMLElement *propNode =
1283 ifaceNode->FirstChildElement("property");
1284 while (propNode != nullptr)
1285 {
1286 const char *propertyName =
1287 propNode->Attribute("name");
1288 BMCWEB_LOG_DEBUG << "Found property "
1289 << propertyName;
1290 if (propertyName == transaction->propertyName)
1291 {
1292 const char *argType =
1293 propNode->Attribute("type");
1294 if (argType != nullptr)
1295 {
1296 sdbusplus::message::message m =
1297 crow::connections::systemBus
1298 ->new_method_call(
1299 connectionName.c_str(),
1300 transaction->objectPath
1301 .c_str(),
1302 "org.freedesktop.DBus."
1303 "Properties",
1304 "Set");
1305 m.append(interfaceName,
1306 transaction->propertyName);
1307 int r = sd_bus_message_open_container(
1308 m.get(), SD_BUS_TYPE_VARIANT,
1309 argType);
1310 if (r < 0)
1311 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001312 transaction->setErrorStatus(
1313 "Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001314 return;
1315 }
1316 r = convertJsonToDbus(
1317 m.get(), argType,
1318 transaction->propertyValue);
1319 if (r < 0)
1320 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001321 transaction->setErrorStatus(
1322 "Invalid arg type");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001323 return;
1324 }
1325 r = sd_bus_message_close_container(
1326 m.get());
1327 if (r < 0)
1328 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001329 transaction->setErrorStatus(
1330 "Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001331 return;
1332 }
Ed Tanous911ac312017-08-15 09:37:42 -07001333
Ed Tanous1abe55e2018-09-05 08:30:59 -07001334 crow::connections::systemBus
1335 ->async_send(
1336 m,
1337 [transaction](
1338 boost::system::error_code
1339 ec,
1340 sdbusplus::message::message
1341 &m) {
1342 BMCWEB_LOG_DEBUG << "sent";
1343 if (ec)
1344 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001345 setErrorResponse(
1346 transaction->res,
1347 boost::beast::http::
1348 status::
1349 forbidden,
1350 forbiddenPropDesc,
1351 ec.message());
1352 }
1353 else
1354 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001355 transaction->res
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001356 .jsonValue = {
1357 {"status", "ok"},
1358 {"message",
1359 "200 OK"},
1360 {"data", nullptr}};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001361 }
1362 });
1363 }
1364 }
1365 propNode =
1366 propNode->NextSiblingElement("property");
1367 }
1368 ifaceNode =
1369 ifaceNode->NextSiblingElement("interface");
1370 }
1371 },
1372 connectionName, transaction->objectPath,
1373 "org.freedesktop.DBus.Introspectable", "Introspect");
1374 }
1375 },
1376 "xyz.openbmc_project.ObjectMapper",
1377 "/xyz/openbmc_project/object_mapper",
1378 "xyz.openbmc_project.ObjectMapper", "GetObject",
1379 transaction->objectPath, std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001380}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001381
Ed Tanous049a0512018-11-01 13:58:42 -07001382inline void handleDBusUrl(const crow::Request &req, crow::Response &res,
1383 std::string &objectPath)
1384{
Ed Tanous049a0512018-11-01 13:58:42 -07001385
1386 // If accessing a single attribute, fill in and update objectPath,
1387 // otherwise leave destProperty blank
1388 std::string destProperty = "";
1389 const char *attrSeperator = "/attr/";
1390 size_t attrPosition = objectPath.find(attrSeperator);
1391 if (attrPosition != objectPath.npos)
1392 {
1393 destProperty = objectPath.substr(attrPosition + strlen(attrSeperator),
1394 objectPath.length());
1395 objectPath = objectPath.substr(0, attrPosition);
1396 }
1397
1398 if (req.method() == "POST"_method)
1399 {
1400 constexpr const char *actionSeperator = "/action/";
1401 size_t actionPosition = objectPath.find(actionSeperator);
1402 if (actionPosition != objectPath.npos)
1403 {
1404 std::string postProperty =
1405 objectPath.substr((actionPosition + strlen(actionSeperator)),
1406 objectPath.length());
1407 objectPath = objectPath.substr(0, actionPosition);
1408 handleAction(req, res, objectPath, postProperty);
1409 return;
1410 }
1411 }
1412 else if (req.method() == "GET"_method)
1413 {
1414 if (boost::ends_with(objectPath, "/enumerate"))
1415 {
1416 objectPath.erase(objectPath.end() - sizeof("enumerate"),
1417 objectPath.end());
1418 handleEnumerate(res, objectPath);
1419 }
1420 else if (boost::ends_with(objectPath, "/list"))
1421 {
1422 objectPath.erase(objectPath.end() - sizeof("list"),
1423 objectPath.end());
1424 handleList(res, objectPath);
1425 }
1426 else
1427 {
Ed Tanousf839dfe2018-11-12 11:11:15 -08001428 // Trim any trailing "/" at the end
1429 if (boost::ends_with(objectPath, "/"))
1430 {
1431 objectPath.pop_back();
1432 handleList(res, objectPath, 1);
1433 }
1434 else
1435 {
1436 handleGet(res, objectPath, destProperty);
1437 }
Ed Tanous049a0512018-11-01 13:58:42 -07001438 }
1439 return;
1440 }
1441 else if (req.method() == "PUT"_method)
1442 {
1443 handlePut(req, res, objectPath, destProperty);
1444 return;
1445 }
Matt Spinlerde818812018-12-11 16:39:20 -06001446 else if (req.method() == "DELETE"_method)
1447 {
1448 handleDelete(req, res, objectPath);
1449 return;
1450 }
Ed Tanous049a0512018-11-01 13:58:42 -07001451
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -06001452 setErrorResponse(res, boost::beast::http::status::method_not_allowed,
1453 methodNotAllowedDesc, methodNotAllowedMsg);
Ed Tanous049a0512018-11-01 13:58:42 -07001454 res.end();
1455}
1456
Ed Tanous1abe55e2018-09-05 08:30:59 -07001457template <typename... Middlewares> void requestRoutes(Crow<Middlewares...> &app)
1458{
1459 BMCWEB_ROUTE(app, "/bus/")
1460 .methods("GET"_method)(
1461 [](const crow::Request &req, crow::Response &res) {
1462 res.jsonValue = {{"busses", {{{"name", "system"}}}},
1463 {"status", "ok"}};
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001464 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001465 });
1466
1467 BMCWEB_ROUTE(app, "/bus/system/")
1468 .methods("GET"_method)(
1469 [](const crow::Request &req, crow::Response &res) {
1470 auto myCallback = [&res](const boost::system::error_code ec,
1471 std::vector<std::string> &names) {
1472 if (ec)
1473 {
1474 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
1475 res.result(
1476 boost::beast::http::status::internal_server_error);
1477 }
1478 else
1479 {
1480 std::sort(names.begin(), names.end());
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001481 res.jsonValue = {{"status", "ok"}};
1482 auto &objectsSub = res.jsonValue["objects"];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001483 for (auto &name : names)
1484 {
1485 objectsSub.push_back({{"name", name}});
1486 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001487 }
1488 res.end();
1489 };
1490 crow::connections::systemBus->async_method_call(
1491 std::move(myCallback), "org.freedesktop.DBus", "/",
1492 "org.freedesktop.DBus", "ListNames");
1493 });
1494
1495 BMCWEB_ROUTE(app, "/list/")
1496 .methods("GET"_method)(
1497 [](const crow::Request &req, crow::Response &res) {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001498 handleList(res, "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001499 });
1500
1501 BMCWEB_ROUTE(app, "/xyz/<path>")
Matt Spinlerde818812018-12-11 16:39:20 -06001502 .methods("GET"_method, "PUT"_method, "POST"_method, "DELETE"_method)(
Ed Tanous049a0512018-11-01 13:58:42 -07001503 [](const crow::Request &req, crow::Response &res,
1504 const std::string &path) {
1505 std::string objectPath = "/xyz/" + path;
1506 handleDBusUrl(req, res, objectPath);
1507 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001508
Ed Tanous049a0512018-11-01 13:58:42 -07001509 BMCWEB_ROUTE(app, "/org/<path>")
Matt Spinlerde818812018-12-11 16:39:20 -06001510 .methods("GET"_method, "PUT"_method, "POST"_method, "DELETE"_method)(
Ed Tanous049a0512018-11-01 13:58:42 -07001511 [](const crow::Request &req, crow::Response &res,
1512 const std::string &path) {
1513 std::string objectPath = "/org/" + path;
1514 handleDBusUrl(req, res, objectPath);
1515 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001516
Ed Tanous1abe55e2018-09-05 08:30:59 -07001517 BMCWEB_ROUTE(app, "/download/dump/<str>/")
1518 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
1519 const std::string &dumpId) {
Ed Tanousad18f072018-11-14 14:07:48 -08001520 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]*)$");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001521 if (!std::regex_match(dumpId, validFilename))
1522 {
Ed Tanousad18f072018-11-14 14:07:48 -08001523 res.result(boost::beast::http::status::bad_request);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001524 res.end();
1525 return;
1526 }
James Feistf6150402019-01-08 10:36:20 -08001527 std::filesystem::path loc(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001528 "/var/lib/phosphor-debug-collector/dumps");
1529
Ed Tanousad18f072018-11-14 14:07:48 -08001530 loc /= dumpId;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001531
James Feistf6150402019-01-08 10:36:20 -08001532 if (!std::filesystem::exists(loc) ||
1533 !std::filesystem::is_directory(loc))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001534 {
Ed Tanousad18f072018-11-14 14:07:48 -08001535 BMCWEB_LOG_ERROR << loc << "Not found";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001536 res.result(boost::beast::http::status::not_found);
1537 res.end();
1538 return;
1539 }
James Feistf6150402019-01-08 10:36:20 -08001540 std::filesystem::directory_iterator files(loc);
Ed Tanousad18f072018-11-14 14:07:48 -08001541
Ed Tanous1abe55e2018-09-05 08:30:59 -07001542 for (auto &file : files)
1543 {
1544 std::ifstream readFile(file.path());
Ed Tanousad18f072018-11-14 14:07:48 -08001545 if (!readFile.good())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001546 {
1547 continue;
1548 }
1549 res.addHeader("Content-Type", "application/octet-stream");
1550 res.body() = {std::istreambuf_iterator<char>(readFile),
1551 std::istreambuf_iterator<char>()};
1552 res.end();
Ed Tanousad18f072018-11-14 14:07:48 -08001553 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001554 }
1555 res.result(boost::beast::http::status::not_found);
1556 res.end();
1557 return;
1558 });
1559
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001560 BMCWEB_ROUTE(app, "/bus/system/<str>/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001561 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001562 const std::string &Connection) {
1563 introspectObjects(Connection, "/",
1564 std::make_shared<bmcweb::AsyncResp>(res));
1565 });
1566
1567 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
1568 .methods("GET"_method,
1569 "POST"_method)([](const crow::Request &req,
1570 crow::Response &res,
1571 const std::string &processName,
1572 const std::string &requestedPath) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001573 std::vector<std::string> strs;
1574 boost::split(strs, requestedPath, boost::is_any_of("/"));
1575 std::string objectPath;
1576 std::string interfaceName;
1577 std::string methodName;
1578 auto it = strs.begin();
1579 if (it == strs.end())
1580 {
1581 objectPath = "/";
1582 }
1583 while (it != strs.end())
1584 {
1585 // Check if segment contains ".". If it does, it must be an
1586 // interface
1587 if (it->find(".") != std::string::npos)
1588 {
1589 break;
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001590 // This check is neccesary as the trailing slash gets parsed
Ed Tanous1abe55e2018-09-05 08:30:59 -07001591 // as part of our <path> specifier above, which causes the
1592 // normal trailing backslash redirector to fail.
1593 }
1594 else if (!it->empty())
1595 {
1596 objectPath += "/" + *it;
1597 }
1598 it++;
1599 }
1600 if (it != strs.end())
1601 {
1602 interfaceName = *it;
1603 it++;
1604
1605 // after interface, we might have a method name
1606 if (it != strs.end())
1607 {
1608 methodName = *it;
1609 it++;
1610 }
1611 }
1612 if (it != strs.end())
1613 {
1614 // if there is more levels past the method name, something went
1615 // wrong, return not found
1616 res.result(boost::beast::http::status::not_found);
1617 res.end();
1618 return;
1619 }
1620 if (interfaceName.empty())
1621 {
1622 crow::connections::systemBus->async_method_call(
1623 [&, processName,
1624 objectPath](const boost::system::error_code ec,
1625 const std::string &introspect_xml) {
1626 if (ec)
1627 {
1628 BMCWEB_LOG_ERROR
1629 << "Introspect call failed with error: "
1630 << ec.message()
1631 << " on process: " << processName
1632 << " path: " << objectPath << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001633 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001634 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001635 tinyxml2::XMLDocument doc;
1636
1637 doc.Parse(introspect_xml.c_str());
1638 tinyxml2::XMLNode *pRoot =
1639 doc.FirstChildElement("node");
1640 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001641 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001642 BMCWEB_LOG_ERROR << "XML document failed to parse "
1643 << processName << " " << objectPath
1644 << "\n";
1645 res.jsonValue = {{"status", "XML parse error"}};
1646 res.result(boost::beast::http::status::
1647 internal_server_error);
1648 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001649 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001650
1651 BMCWEB_LOG_DEBUG << introspect_xml;
1652 res.jsonValue = {{"status", "ok"},
1653 {"bus_name", processName},
1654 {"object_path", objectPath}};
1655 nlohmann::json &interfacesArray =
1656 res.jsonValue["interfaces"];
1657 interfacesArray = nlohmann::json::array();
1658 tinyxml2::XMLElement *interface =
1659 pRoot->FirstChildElement("interface");
1660
1661 while (interface != nullptr)
1662 {
1663 const char *ifaceName =
1664 interface->Attribute("name");
1665 if (ifaceName != nullptr)
1666 {
1667 interfacesArray.push_back(
1668 {{"name", ifaceName}});
1669 }
1670
1671 interface =
1672 interface->NextSiblingElement("interface");
1673 }
1674
Ed Tanous1abe55e2018-09-05 08:30:59 -07001675 res.end();
1676 },
1677 processName, objectPath,
1678 "org.freedesktop.DBus.Introspectable", "Introspect");
1679 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001680 else if (methodName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001681 {
1682 crow::connections::systemBus->async_method_call(
1683 [&, processName, objectPath,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001684 interfaceName{std::move(interfaceName)}](
Ed Tanous1abe55e2018-09-05 08:30:59 -07001685 const boost::system::error_code ec,
1686 const std::string &introspect_xml) {
1687 if (ec)
1688 {
1689 BMCWEB_LOG_ERROR
1690 << "Introspect call failed with error: "
1691 << ec.message()
1692 << " on process: " << processName
1693 << " path: " << objectPath << "\n";
1694 }
1695 else
1696 {
1697 tinyxml2::XMLDocument doc;
1698
1699 doc.Parse(introspect_xml.c_str());
1700 tinyxml2::XMLNode *pRoot =
1701 doc.FirstChildElement("node");
1702 if (pRoot == nullptr)
1703 {
1704 BMCWEB_LOG_ERROR
1705 << "XML document failed to parse "
1706 << processName << " " << objectPath << "\n";
1707 res.result(boost::beast::http::status::
1708 internal_server_error);
1709 }
1710 else
1711 {
1712 tinyxml2::XMLElement *node =
1713 pRoot->FirstChildElement("node");
1714
1715 // if we know we're the only call, build the
1716 // json directly
Ed Tanous1abe55e2018-09-05 08:30:59 -07001717 tinyxml2::XMLElement *interface =
1718 pRoot->FirstChildElement("interface");
1719
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001720 res.jsonValue = {
1721 {"status", "ok"},
1722 {"bus_name", processName},
1723 {"interface", interfaceName},
1724 {"object_path", objectPath},
1725 {"properties", nlohmann::json::object()}};
1726
1727 nlohmann::json &methodsArray =
1728 res.jsonValue["methods"];
1729 methodsArray = nlohmann::json::array();
1730
1731 nlohmann::json &signalsArray =
1732 res.jsonValue["signals"];
1733 signalsArray = nlohmann::json::array();
1734
Ed Tanous1abe55e2018-09-05 08:30:59 -07001735 while (interface != nullptr)
1736 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001737 const char *ifaceName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001738 interface->Attribute("name");
1739
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001740 if (ifaceName != nullptr &&
1741 ifaceName == interfaceName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001742 {
1743 tinyxml2::XMLElement *methods =
1744 interface->FirstChildElement(
1745 "method");
1746 while (methods != nullptr)
1747 {
1748 nlohmann::json argsArray =
1749 nlohmann::json::array();
1750 tinyxml2::XMLElement *arg =
1751 methods->FirstChildElement(
1752 "arg");
1753 while (arg != nullptr)
1754 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001755 nlohmann::json thisArg;
1756 for (const char *fieldName :
1757 std::array<const char *,
1758 3>{"name",
1759 "direction",
1760 "type"})
1761 {
1762 const char *fieldValue =
1763 arg->Attribute(
1764 fieldName);
1765 if (fieldValue != nullptr)
1766 {
1767 thisArg[fieldName] =
1768 fieldValue;
1769 }
1770 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001771 argsArray.push_back(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001772 std::move(thisArg));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001773 arg = arg->NextSiblingElement(
1774 "arg");
1775 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001776
1777 const char *name =
1778 methods->Attribute("name");
1779 if (name != nullptr)
1780 {
1781 methodsArray.push_back(
1782 {{"name", name},
1783 {"uri", "/bus/system/" +
1784 processName +
1785 objectPath +
1786 "/" +
1787 interfaceName +
1788 "/" + name},
1789 {"args", argsArray}});
1790 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001791 methods =
1792 methods->NextSiblingElement(
1793 "method");
1794 }
1795 tinyxml2::XMLElement *signals =
1796 interface->FirstChildElement(
1797 "signal");
1798 while (signals != nullptr)
1799 {
1800 nlohmann::json argsArray =
1801 nlohmann::json::array();
1802
1803 tinyxml2::XMLElement *arg =
1804 signals->FirstChildElement(
1805 "arg");
1806 while (arg != nullptr)
1807 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001808 const char *name =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001809 arg->Attribute("name");
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001810 const char *type =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001811 arg->Attribute("type");
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001812 if (name != nullptr &&
1813 type != nullptr)
1814 {
1815 argsArray.push_back({
1816 {"name", name},
1817 {"type", type},
1818 });
1819 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001820 arg = arg->NextSiblingElement(
1821 "arg");
1822 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001823 const char *name =
1824 signals->Attribute("name");
1825 if (name != nullptr)
1826 {
1827 signalsArray.push_back(
1828 {{"name", name},
1829 {"args", argsArray}});
1830 }
1831
Ed Tanous1abe55e2018-09-05 08:30:59 -07001832 signals =
1833 signals->NextSiblingElement(
1834 "signal");
1835 }
1836
Ed Tanous1abe55e2018-09-05 08:30:59 -07001837 break;
1838 }
1839
1840 interface = interface->NextSiblingElement(
1841 "interface");
1842 }
1843 if (interface == nullptr)
1844 {
1845 // if we got to the end of the list and
1846 // never found a match, throw 404
1847 res.result(
1848 boost::beast::http::status::not_found);
1849 }
1850 }
1851 }
1852 res.end();
1853 },
1854 processName, objectPath,
1855 "org.freedesktop.DBus.Introspectable", "Introspect");
1856 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001857 else
1858 {
1859 if (req.method() != "POST"_method)
1860 {
1861 res.result(boost::beast::http::status::not_found);
1862 res.end();
1863 return;
1864 }
1865
1866 nlohmann::json requestDbusData =
1867 nlohmann::json::parse(req.body, nullptr, false);
1868
1869 if (requestDbusData.is_discarded())
1870 {
1871 res.result(boost::beast::http::status::bad_request);
1872 res.end();
1873 return;
1874 }
1875 if (!requestDbusData.is_array())
1876 {
1877 res.result(boost::beast::http::status::bad_request);
1878 res.end();
1879 return;
1880 }
1881 auto transaction = std::make_shared<InProgressActionData>(res);
1882
1883 transaction->path = objectPath;
1884 transaction->methodName = methodName;
1885 transaction->arguments = std::move(requestDbusData);
1886
1887 findActionOnInterface(transaction, processName);
1888 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001889 });
1890}
1891} // namespace openbmc_mapper
1892} // namespace crow