blob: 18b99dd26661e5adceed56b446f276124a31d903 [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 Spinler16caaee2019-01-15 11:40:34 -060043const std::string methodOutputFailedMsg = "500 Method Output Error";
Matt Spinler6db06242018-12-11 11:21:22 -060044
Matt Spinler2ae60092018-12-06 10:35:36 -060045const std::string notFoundDesc =
46 "org.freedesktop.DBus.Error.FileNotFound: path or object not found";
Matt Spinlerdc2f9f12018-12-06 13:53:53 -060047const std::string propNotFoundDesc = "The specified property cannot be found";
Matt Spinler6db06242018-12-11 11:21:22 -060048const std::string noJsonDesc = "No JSON object could be decoded";
49const std::string methodNotFoundDesc = "The specified method cannot be found";
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -060050const std::string methodNotAllowedDesc = "Method not allowed";
Matt Spinlerfbc19ea2018-12-11 14:03:42 -060051const std::string forbiddenPropDesc =
52 "The specified property cannot be created";
53const std::string forbiddenResDesc = "The specified resource cannot be created";
Matt Spinler2ae60092018-12-06 10:35:36 -060054
55void setErrorResponse(crow::Response &res, boost::beast::http::status result,
56 const std::string &desc, const std::string &msg)
57{
58 res.result(result);
59 res.jsonValue = {{"data", {{"description", desc}}},
60 {"message", msg},
61 {"status", "error"}};
62}
63
Ed Tanouse3cb5a32018-08-08 14:16:49 -070064void introspectObjects(const std::string &processName,
65 const std::string &objectPath,
66 std::shared_ptr<bmcweb::AsyncResp> transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -070067{
Ed Tanouse3cb5a32018-08-08 14:16:49 -070068 if (transaction->res.jsonValue.is_null())
69 {
70 transaction->res.jsonValue = {{"status", "ok"},
71 {"bus_name", processName},
72 {"objects", nlohmann::json::array()}};
73 }
74
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -070076 [transaction, processName{std::string(processName)},
77 objectPath{std::string(objectPath)}](
78 const boost::system::error_code ec,
79 const std::string &introspect_xml) {
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 if (ec)
81 {
82 BMCWEB_LOG_ERROR
83 << "Introspect call failed with error: " << ec.message()
84 << " on process: " << processName << " path: " << objectPath
85 << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -070086 return;
87 }
88 transaction->res.jsonValue["objects"].push_back(
89 {{"path", objectPath}});
90
91 tinyxml2::XMLDocument doc;
92
93 doc.Parse(introspect_xml.c_str());
94 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
95 if (pRoot == nullptr)
96 {
97 BMCWEB_LOG_ERROR << "XML document failed to parse "
98 << processName << " " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -070099 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 else
101 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700102 tinyxml2::XMLElement *node = pRoot->FirstChildElement("node");
103 while (node != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700105 const char *childPath = node->Attribute("name");
106 if (childPath != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 std::string newpath;
109 if (objectPath != "/")
110 {
111 newpath += objectPath;
112 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700113 newpath += std::string("/") + childPath;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 // introspect the subobjects as well
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700115 introspectObjects(processName, newpath, transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700117
118 node = node->NextSiblingElement("node");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 }
120 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700122 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -0700124}
Ed Tanous64530012018-02-06 17:08:16 -0800125
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600126void getPropertiesForEnumerate(const std::string &objectPath,
127 const std::string &service,
128 const std::string &interface,
129 std::shared_ptr<bmcweb::AsyncResp> asyncResp)
130{
131 BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " "
132 << service << " " << interface;
133
134 crow::connections::systemBus->async_method_call(
135 [asyncResp, objectPath, service,
136 interface](const boost::system::error_code ec,
137 const std::vector<
138 std::pair<std::string, dbus::utility::DbusVariantType>>
139 &propertiesList) {
140 if (ec)
141 {
142 BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface "
143 << interface << " service " << service
144 << " failed with code " << ec;
145 return;
146 }
147
148 nlohmann::json &dataJson = asyncResp->res.jsonValue["data"];
149 nlohmann::json &objectJson = dataJson[objectPath];
150 if (objectJson.is_null())
151 {
152 objectJson = nlohmann::json::object();
153 }
154
155 for (const auto &[name, value] : propertiesList)
156 {
157 nlohmann::json &propertyJson = objectJson[name];
158 sdbusplus::message::variant_ns::visit(
159 [&propertyJson](auto &&val) { propertyJson = val; }, value);
160 }
161 },
162 service, objectPath, "org.freedesktop.DBus.Properties", "GetAll",
163 interface);
164}
165
166// Find any results that weren't picked up by ObjectManagers, to be
167// called after all ObjectManagers are searched for and called.
168void findRemainingObjectsForEnumerate(
169 const std::string &objectPath, std::shared_ptr<GetSubTreeType> subtree,
170 std::shared_ptr<bmcweb::AsyncResp> asyncResp)
171{
172 BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate";
173 const nlohmann::json &dataJson = asyncResp->res.jsonValue["data"];
174
175 for (const auto &[path, interface_map] : *subtree)
176 {
177 if (path == objectPath)
178 {
179 // An enumerate does not return the target path's properties
180 continue;
181 }
182 if (dataJson.find(path) == dataJson.end())
183 {
184 for (const auto &[service, interfaces] : interface_map)
185 {
186 for (const auto &interface : interfaces)
187 {
188 if (!boost::starts_with(interface, "org.freedesktop.DBus"))
189 {
190 getPropertiesForEnumerate(path, service, interface,
191 asyncResp);
192 }
193 }
194 }
195 }
196 }
197}
198
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600199struct InProgressEnumerateData
200{
201 InProgressEnumerateData(const std::string &objectPath,
202 std::shared_ptr<bmcweb::AsyncResp> asyncResp) :
203 objectPath(objectPath),
204 asyncResp(asyncResp)
205 {
206 }
207
208 ~InProgressEnumerateData()
209 {
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600210 findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp);
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600211 }
212
213 const std::string objectPath;
214 std::shared_ptr<GetSubTreeType> subtree;
215 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
216};
217
218void getManagedObjectsForEnumerate(
219 const std::string &object_name, const std::string &object_manager_path,
220 const std::string &connection_name,
221 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700222{
Ed Tanous049a0512018-11-01 13:58:42 -0700223 BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << object_name
224 << " object_manager_path " << object_manager_path
225 << " connection_name " << connection_name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600227 [transaction, object_name,
Ed Tanous049a0512018-11-01 13:58:42 -0700228 connection_name](const boost::system::error_code ec,
229 const dbus::utility::ManagedObjectType &objects) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 if (ec)
231 {
Ed Tanous049a0512018-11-01 13:58:42 -0700232 BMCWEB_LOG_ERROR << "GetManagedObjects on path " << object_name
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600233 << " on connection " << connection_name
Ed Tanous049a0512018-11-01 13:58:42 -0700234 << " failed with code " << ec;
235 return;
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700236 }
Ed Tanous64530012018-02-06 17:08:16 -0800237
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600238 nlohmann::json &dataJson =
239 transaction->asyncResp->res.jsonValue["data"];
Ed Tanous049a0512018-11-01 13:58:42 -0700240
241 for (const auto &objectPath : objects)
242 {
243 if (boost::starts_with(objectPath.first.str, object_name))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700244 {
Ed Tanous049a0512018-11-01 13:58:42 -0700245 BMCWEB_LOG_DEBUG << "Reading object "
246 << objectPath.first.str;
247 nlohmann::json &objectJson = dataJson[objectPath.first.str];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 if (objectJson.is_null())
249 {
250 objectJson = nlohmann::json::object();
251 }
252 for (const auto &interface : objectPath.second)
253 {
254 for (const auto &property : interface.second)
255 {
256 nlohmann::json &propertyJson =
257 objectJson[property.first];
William A. Kennington III0a63b1c2018-10-18 13:37:19 -0700258 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259 [&propertyJson](auto &&val) {
260 propertyJson = val;
261 },
262 property.second);
263 }
264 }
265 }
Ed Tanous049a0512018-11-01 13:58:42 -0700266 for (const auto &interface : objectPath.second)
267 {
268 if (interface.first == "org.freedesktop.DBus.ObjectManager")
269 {
270 getManagedObjectsForEnumerate(
271 objectPath.first.str, objectPath.first.str,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600272 connection_name, transaction);
Ed Tanous049a0512018-11-01 13:58:42 -0700273 }
274 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700275 }
276 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700277 connection_name, object_manager_path,
278 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
279}
280
281void findObjectManagerPathForEnumerate(
282 const std::string &object_name, const std::string &connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600283 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700284{
Ed Tanous049a0512018-11-01 13:58:42 -0700285 BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << object_name
286 << " on connection:" << connection_name;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700287 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600288 [transaction, object_name, connection_name](
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700289 const boost::system::error_code ec,
290 const boost::container::flat_map<
291 std::string, boost::container::flat_map<
292 std::string, std::vector<std::string>>>
293 &objects) {
294 if (ec)
295 {
Ed Tanous049a0512018-11-01 13:58:42 -0700296 BMCWEB_LOG_ERROR << "GetAncestors on path " << object_name
297 << " failed with code " << ec;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700298 return;
299 }
300
Ed Tanousf254ba72018-10-12 13:40:35 -0700301 for (const auto &pathGroup : objects)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700302 {
Ed Tanousf254ba72018-10-12 13:40:35 -0700303 for (const auto &connectionGroup : pathGroup.second)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700304 {
305 if (connectionGroup.first == connection_name)
306 {
307 // Found the object manager path for this resource.
308 getManagedObjectsForEnumerate(
Ed Tanous049a0512018-11-01 13:58:42 -0700309 object_name, pathGroup.first, connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600310 transaction);
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700311 return;
312 }
313 }
314 }
315 },
316 "xyz.openbmc_project.ObjectMapper",
317 "/xyz/openbmc_project/object_mapper",
318 "xyz.openbmc_project.ObjectMapper", "GetAncestors", object_name,
319 std::array<const char *, 1>{"org.freedesktop.DBus.ObjectManager"});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700320}
Ed Tanous64530012018-02-06 17:08:16 -0800321
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600322// Uses GetObject to add the object info about the target /enumerate path to the
323// results of GetSubTree, as GetSubTree will not return info for the
324// target path, and then continues on enumerating the rest of the tree.
325void getObjectAndEnumerate(std::shared_ptr<InProgressEnumerateData> transaction)
326{
327 using GetObjectType =
328 std::vector<std::pair<std::string, std::vector<std::string>>>;
329
330 crow::connections::systemBus->async_method_call(
331 [transaction](const boost::system::error_code ec,
332 const GetObjectType &objects) {
333 if (ec)
334 {
335 BMCWEB_LOG_ERROR << "GetObject for path "
336 << transaction->objectPath
337 << " failed with code " << ec;
338 return;
339 }
340
341 BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath
342 << " has " << objects.size() << " entries";
343 if (!objects.empty())
344 {
345 transaction->subtree->emplace_back(transaction->objectPath,
346 objects);
347 }
348
349 // Map indicating connection name, and the path where the object
350 // manager exists
351 boost::container::flat_map<std::string, std::string> connections;
352
353 for (const auto &object : *(transaction->subtree))
354 {
355 for (const auto &connection : object.second)
356 {
357 std::string &objectManagerPath =
358 connections[connection.first];
359 for (const auto &interface : connection.second)
360 {
361 BMCWEB_LOG_DEBUG << connection.first
362 << " has interface " << interface;
363 if (interface == "org.freedesktop.DBus.ObjectManager")
364 {
365 BMCWEB_LOG_DEBUG << "found object manager path "
366 << object.first;
367 objectManagerPath = object.first;
368 }
369 }
370 }
371 }
372 BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections";
373
374 for (const auto &connection : connections)
375 {
376 // If we already know where the object manager is, we don't need
377 // to search for it, we can call directly in to
378 // getManagedObjects
379 if (!connection.second.empty())
380 {
381 getManagedObjectsForEnumerate(
382 transaction->objectPath, connection.second,
383 connection.first, transaction);
384 }
385 else
386 {
387 // otherwise we need to find the object manager path before
388 // we can continue
389 findObjectManagerPathForEnumerate(
390 transaction->objectPath, connection.first, transaction);
391 }
392 }
393 },
394 "xyz.openbmc_project.ObjectMapper",
395 "/xyz/openbmc_project/object_mapper",
396 "xyz.openbmc_project.ObjectMapper", "GetObject",
397 transaction->objectPath, std::array<const char *, 0>());
398}
Ed Tanous64530012018-02-06 17:08:16 -0800399
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700400// Structure for storing data on an in progress action
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401struct InProgressActionData
402{
403 InProgressActionData(crow::Response &res) : res(res){};
404 ~InProgressActionData()
405 {
Matt Spinler16caaee2019-01-15 11:40:34 -0600406 // Methods could have been called across different owners
407 // and interfaces, where some calls failed and some passed.
408 //
409 // The rules for this are:
410 // * if no method was called - error
411 // * if a method failed and none passed - error
412 // (converse: if at least one method passed - OK)
413 // * for the method output:
414 // * if output processing didn't fail, return the data
415
416 // Only deal with method returns if nothing failed earlier
417 if (res.result() == boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700418 {
Matt Spinler16caaee2019-01-15 11:40:34 -0600419 if (!methodPassed)
420 {
421 if (methodFailed)
422 {
423 setErrorResponse(res,
424 boost::beast::http::status::bad_request,
425 "Method call failed", methodFailedMsg);
426 }
427 else
428 {
429 setErrorResponse(res, boost::beast::http::status::not_found,
430 methodNotFoundDesc, notFoundMsg);
431 }
432 }
433 else
434 {
435 if (outputFailed)
436 {
437 setErrorResponse(
438 res, boost::beast::http::status::internal_server_error,
439 "Method output failure", methodOutputFailedMsg);
440 }
441 else
442 {
443 res.jsonValue = {{"status", "ok"},
444 {"message", "200 OK"},
445 {"data", methodResponse}};
446 }
447 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 }
Matt Spinler16caaee2019-01-15 11:40:34 -0600449
Ed Tanous1abe55e2018-09-05 08:30:59 -0700450 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700451 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700452
Matt Spinler6db06242018-12-11 11:21:22 -0600453 void setErrorStatus(const std::string &desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700454 {
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -0600455 setErrorResponse(res, boost::beast::http::status::bad_request, desc,
456 badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700457 }
458 crow::Response &res;
459 std::string path;
460 std::string methodName;
Matt Spinlerde818812018-12-11 16:39:20 -0600461 std::string interfaceName;
Matt Spinler16caaee2019-01-15 11:40:34 -0600462 bool methodPassed = false;
463 bool methodFailed = false;
464 bool outputFailed = false;
465 nlohmann::json methodResponse;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700466 nlohmann::json arguments;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700467};
468
Ed Tanous1abe55e2018-09-05 08:30:59 -0700469std::vector<std::string> dbusArgSplit(const std::string &string)
470{
471 std::vector<std::string> ret;
472 if (string.empty())
473 {
474 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700475 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700476 ret.push_back("");
477 int containerDepth = 0;
478
479 for (std::string::const_iterator character = string.begin();
480 character != string.end(); character++)
481 {
482 ret.back() += *character;
483 switch (*character)
484 {
485 case ('a'):
486 break;
487 case ('('):
488 case ('{'):
489 containerDepth++;
490 break;
491 case ('}'):
492 case (')'):
493 containerDepth--;
494 if (containerDepth == 0)
495 {
496 if (character + 1 != string.end())
497 {
498 ret.push_back("");
499 }
500 }
501 break;
502 default:
503 if (containerDepth == 0)
504 {
505 if (character + 1 != string.end())
506 {
507 ret.push_back("");
508 }
509 }
510 break;
511 }
512 }
Matt Spinler4ae611d2019-01-11 15:37:06 -0600513
514 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700515}
516
Ed Tanousd76323e2018-08-07 14:35:40 -0700517int convertJsonToDbus(sd_bus_message *m, const std::string &arg_type,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518 const nlohmann::json &input_json)
519{
520 int r = 0;
521 BMCWEB_LOG_DEBUG << "Converting " << input_json.dump()
522 << " to type: " << arg_type;
523 const std::vector<std::string> argTypes = dbusArgSplit(arg_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700524
Ed Tanous1abe55e2018-09-05 08:30:59 -0700525 // Assume a single object for now.
526 const nlohmann::json *j = &input_json;
527 nlohmann::json::const_iterator jIt = input_json.begin();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700528
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700529 for (const std::string &argCode : argTypes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530 {
531 // If we are decoding multiple objects, grab the pointer to the
532 // iterator, and increment it for the next loop
533 if (argTypes.size() > 1)
534 {
535 if (jIt == input_json.end())
536 {
537 return -2;
538 }
539 j = &*jIt;
540 jIt++;
541 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700542 const int64_t *intValue = j->get_ptr<const int64_t *>();
543 const uint64_t *uintValue = j->get_ptr<const uint64_t *>();
544 const std::string *stringValue = j->get_ptr<const std::string *>();
545 const double *doubleValue = j->get_ptr<const double *>();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 const bool *b = j->get_ptr<const bool *>();
547 int64_t v = 0;
548 double d = 0.0;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700549
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 // Do some basic type conversions that make sense. uint can be
551 // converted to int. int and uint can be converted to double
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700552 if (uintValue != nullptr && intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700554 v = static_cast<int64_t>(*uintValue);
555 intValue = &v;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700557 if (uintValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700558 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700559 d = static_cast<double>(*uintValue);
560 doubleValue = &d;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700562 if (intValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700563 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700564 d = static_cast<double>(*intValue);
565 doubleValue = &d;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700566 }
567
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700568 if (argCode == "s")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700569 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700570 if (stringValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700571 {
572 return -1;
573 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700574 r = sd_bus_message_append_basic(m, argCode[0],
575 (void *)stringValue->c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700576 if (r < 0)
577 {
578 return r;
579 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700580 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700581 else if (argCode == "i")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700582 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700583 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700584 {
585 return -1;
586 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700587 int32_t i = static_cast<int32_t>(*intValue);
588 r = sd_bus_message_append_basic(m, argCode[0], &i);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589 if (r < 0)
590 {
591 return r;
592 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700593 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700594 else if (argCode == "b")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700595 {
596 // lots of ways bool could be represented here. Try them all
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700597 int boolInt = false;
598 if (intValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700599 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700600 boolInt = *intValue > 0 ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700601 }
602 else if (b != nullptr)
603 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700604 boolInt = b ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700606 else if (stringValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700607 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700608 boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700609 }
610 else
611 {
612 return -1;
613 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700614 r = sd_bus_message_append_basic(m, argCode[0], &boolInt);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615 if (r < 0)
616 {
617 return r;
618 }
619 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700620 else if (argCode == "n")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700622 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623 {
624 return -1;
625 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700626 int16_t n = static_cast<int16_t>(*intValue);
627 r = sd_bus_message_append_basic(m, argCode[0], &n);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700628 if (r < 0)
629 {
630 return r;
631 }
632 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700633 else if (argCode == "x")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700635 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636 {
637 return -1;
638 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700639 r = sd_bus_message_append_basic(m, argCode[0], intValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640 if (r < 0)
641 {
642 return r;
643 }
644 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700645 else if (argCode == "y")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700647 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700648 {
649 return -1;
650 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700651 uint8_t y = static_cast<uint8_t>(*uintValue);
652 r = sd_bus_message_append_basic(m, argCode[0], &y);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700653 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700654 else if (argCode == "q")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700656 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657 {
658 return -1;
659 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700660 uint16_t q = static_cast<uint16_t>(*uintValue);
661 r = sd_bus_message_append_basic(m, argCode[0], &q);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700663 else if (argCode == "u")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700665 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700666 {
667 return -1;
668 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700669 uint32_t u = static_cast<uint32_t>(*uintValue);
670 r = sd_bus_message_append_basic(m, argCode[0], &u);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700671 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700672 else if (argCode == "t")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700673 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700674 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700675 {
676 return -1;
677 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700678 r = sd_bus_message_append_basic(m, argCode[0], uintValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700680 else if (argCode == "d")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700682 sd_bus_message_append_basic(m, argCode[0], doubleValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700684 else if (boost::starts_with(argCode, "a"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700686 std::string containedType = argCode.substr(1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700687 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700688 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 if (r < 0)
690 {
691 return r;
692 }
693
694 for (nlohmann::json::const_iterator it = j->begin(); it != j->end();
695 ++it)
696 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700697 r = convertJsonToDbus(m, containedType, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700698 if (r < 0)
699 {
700 return r;
701 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700702 }
703 sd_bus_message_close_container(m);
704 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700705 else if (boost::starts_with(argCode, "v"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700706 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700707 std::string containedType = argCode.substr(1);
708 BMCWEB_LOG_DEBUG << "variant type: " << argCode
709 << " appending variant of type: " << containedType;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700710 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700711 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700712 if (r < 0)
713 {
714 return r;
715 }
716
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700717 r = convertJsonToDbus(m, containedType, input_json);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700718 if (r < 0)
719 {
720 return r;
721 }
722
723 r = sd_bus_message_close_container(m);
724 if (r < 0)
725 {
726 return r;
727 }
728 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700729 else if (boost::starts_with(argCode, "(") &&
730 boost::ends_with(argCode, ")"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700731 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700732 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700734 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700735 nlohmann::json::const_iterator it = j->begin();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700736 for (const std::string &argCode : dbusArgSplit(arg_type))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700737 {
738 if (it == j->end())
739 {
740 return -1;
741 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700742 r = convertJsonToDbus(m, argCode, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700743 if (r < 0)
744 {
745 return r;
746 }
747 it++;
748 }
749 r = sd_bus_message_close_container(m);
750 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700751 else if (boost::starts_with(argCode, "{") &&
752 boost::ends_with(argCode, "}"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700753 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700754 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700755 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700756 containedType.c_str());
757 std::vector<std::string> codes = dbusArgSplit(containedType);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700758 if (codes.size() != 2)
759 {
760 return -1;
761 }
762 const std::string &key_type = codes[0];
763 const std::string &value_type = codes[1];
764 for (auto it : j->items())
765 {
766 r = convertJsonToDbus(m, key_type, it.key());
767 if (r < 0)
768 {
769 return r;
770 }
771
772 r = convertJsonToDbus(m, value_type, it.value());
773 if (r < 0)
774 {
775 return r;
776 }
777 }
778 r = sd_bus_message_close_container(m);
779 }
780 else
781 {
782 return -2;
783 }
784 if (r < 0)
785 {
786 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700787 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700788
Ed Tanous1abe55e2018-09-05 08:30:59 -0700789 if (argTypes.size() > 1)
790 {
791 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700792 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700793 }
Matt Spinler127ea542019-01-14 11:04:28 -0600794
795 return r;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700796}
797
Matt Spinlerd22a7132019-01-14 12:14:30 -0600798template <typename T>
799int readMessageItem(const std::string &typeCode, sdbusplus::message::message &m,
800 nlohmann::json &data)
801{
802 T value;
803
804 int r = sd_bus_message_read_basic(m.get(), typeCode.front(), &value);
805 if (r < 0)
806 {
807 BMCWEB_LOG_ERROR << "sd_bus_message_read_basic on type " << typeCode
808 << " failed!";
809 return r;
810 }
811
812 data = value;
813 return 0;
814}
815
Matt Spinler16caaee2019-01-15 11:40:34 -0600816int convertDBusToJSON(const std::string &returnType,
817 sdbusplus::message::message &m, nlohmann::json &response)
818{
Matt Spinlerd22a7132019-01-14 12:14:30 -0600819 int r = 0;
820 const std::vector<std::string> returnTypes = dbusArgSplit(returnType);
821
822 nlohmann::json &thisElement = response;
823 for (const std::string &typeCode : returnTypes)
824 {
825 if (returnType.size() > 1)
826 {
827 response.push_back(nlohmann::json{});
828 thisElement = response.back();
829 }
830
831 if (typeCode == "s")
832 {
833 r = readMessageItem<char *>(typeCode, m, thisElement);
834 if (r < 0)
835 {
836 return r;
837 }
838 }
839 else if (typeCode == "g")
840 {
841 r = readMessageItem<char *>(typeCode, m, thisElement);
842 if (r < 0)
843 {
844 return r;
845 }
846 }
847 else if (typeCode == "o")
848 {
849 r = readMessageItem<char *>(typeCode, m, thisElement);
850 if (r < 0)
851 {
852 return r;
853 }
854 }
855 else if (typeCode == "b")
856 {
857 r = readMessageItem<int>(typeCode, m, thisElement);
858 if (r < 0)
859 {
860 return r;
861 }
862
863 thisElement = static_cast<bool>(thisElement.get<int>());
864 }
865 else if (typeCode == "u")
866 {
867 r = readMessageItem<uint32_t>(typeCode, m, thisElement);
868 if (r < 0)
869 {
870 return r;
871 }
872 }
873 else if (typeCode == "i")
874 {
875 r = readMessageItem<int32_t>(typeCode, m, thisElement);
876 if (r < 0)
877 {
878 return r;
879 }
880 }
881 else if (typeCode == "x")
882 {
883 r = readMessageItem<int64_t>(typeCode, m, thisElement);
884 if (r < 0)
885 {
886 return r;
887 }
888 }
889 else if (typeCode == "t")
890 {
891 r = readMessageItem<uint64_t>(typeCode, m, thisElement);
892 if (r < 0)
893 {
894 return r;
895 }
896 }
897 else if (typeCode == "n")
898 {
899 r = readMessageItem<int16_t>(typeCode, m, thisElement);
900 if (r < 0)
901 {
902 return r;
903 }
904 }
905 else if (typeCode == "q")
906 {
907 r = readMessageItem<uint16_t>(typeCode, m, thisElement);
908 if (r < 0)
909 {
910 return r;
911 }
912 }
913 else if (typeCode == "y")
914 {
915 r = readMessageItem<uint8_t>(typeCode, m, thisElement);
916 if (r < 0)
917 {
918 return r;
919 }
920 }
921 else if (typeCode == "d")
922 {
923 r = readMessageItem<double>(typeCode, m, thisElement);
924 if (r < 0)
925 {
926 return r;
927 }
928 }
929 else if (typeCode == "h")
930 {
931 r = readMessageItem<int>(typeCode, m, thisElement);
932 if (r < 0)
933 {
934 return r;
935 }
936 }
937 else
938 {
939 // TODO: add array, dict, variant support
940 BMCWEB_LOG_ERROR << "Invalid D-Bus signature type " << typeCode;
941 return -2;
942 }
943 }
944
Matt Spinler16caaee2019-01-15 11:40:34 -0600945 return 0;
946}
947
948void handleMethodResponse(std::shared_ptr<InProgressActionData> transaction,
949 sdbusplus::message::message &m,
950 const std::string &returnType)
951{
952}
953
Ed Tanousd76323e2018-08-07 14:35:40 -0700954void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700955 const std::string &connectionName)
956{
957 BMCWEB_LOG_DEBUG << "findActionOnInterface for connection "
958 << connectionName;
959 crow::connections::systemBus->async_method_call(
960 [transaction, connectionName{std::string(connectionName)}](
961 const boost::system::error_code ec,
962 const std::string &introspect_xml) {
963 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
964 if (ec)
965 {
966 BMCWEB_LOG_ERROR
967 << "Introspect call failed with error: " << ec.message()
968 << " on process: " << connectionName << "\n";
Matt Spinler318bd892019-01-15 09:59:20 -0600969 return;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700970 }
Matt Spinler318bd892019-01-15 09:59:20 -0600971 tinyxml2::XMLDocument doc;
972
973 doc.Parse(introspect_xml.data(), introspect_xml.size());
974 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
975 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700976 {
Matt Spinler318bd892019-01-15 09:59:20 -0600977 BMCWEB_LOG_ERROR << "XML document failed to parse "
978 << connectionName << "\n";
979 return;
980 }
981 tinyxml2::XMLElement *interfaceNode =
982 pRoot->FirstChildElement("interface");
983 while (interfaceNode != nullptr)
984 {
985 const char *thisInterfaceName =
986 interfaceNode->Attribute("name");
987 if (thisInterfaceName != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700988 {
Matt Spinler318bd892019-01-15 09:59:20 -0600989 if (!transaction->interfaceName.empty() &&
990 (transaction->interfaceName != thisInterfaceName))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 {
Matt Spinler318bd892019-01-15 09:59:20 -0600992 interfaceNode =
993 interfaceNode->NextSiblingElement("interface");
994 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700995 }
Matt Spinler318bd892019-01-15 09:59:20 -0600996
997 tinyxml2::XMLElement *methodNode =
998 interfaceNode->FirstChildElement("method");
999 while (methodNode != nullptr)
1000 {
1001 const char *thisMethodName =
1002 methodNode->Attribute("name");
1003 BMCWEB_LOG_DEBUG << "Found method: " << thisMethodName;
1004 if (thisMethodName != nullptr &&
1005 thisMethodName == transaction->methodName)
1006 {
1007 BMCWEB_LOG_DEBUG
1008 << "Found method named " << thisMethodName
1009 << " on interface " << thisInterfaceName;
1010 sdbusplus::message::message m =
1011 crow::connections::systemBus->new_method_call(
1012 connectionName.c_str(),
1013 transaction->path.c_str(),
1014 thisInterfaceName,
1015 transaction->methodName.c_str());
1016
1017 tinyxml2::XMLElement *argumentNode =
1018 methodNode->FirstChildElement("arg");
1019
Matt Spinler16caaee2019-01-15 11:40:34 -06001020 std::string returnType;
1021
1022 // Find the output type
1023 while (argumentNode != nullptr)
1024 {
1025 const char *argDirection =
1026 argumentNode->Attribute("direction");
1027 const char *argType =
1028 argumentNode->Attribute("type");
1029 if (argDirection != nullptr &&
1030 argType != nullptr &&
1031 std::string(argDirection) == "out")
1032 {
1033 returnType = argType;
1034 break;
1035 }
1036 argumentNode =
1037 argumentNode->NextSiblingElement("arg");
1038 }
1039
Matt Spinler318bd892019-01-15 09:59:20 -06001040 nlohmann::json::const_iterator argIt =
1041 transaction->arguments.begin();
1042
Matt Spinler16caaee2019-01-15 11:40:34 -06001043 argumentNode = methodNode->FirstChildElement("arg");
1044
Matt Spinler318bd892019-01-15 09:59:20 -06001045 while (argumentNode != nullptr)
1046 {
1047 const char *argDirection =
1048 argumentNode->Attribute("direction");
1049 const char *argType =
1050 argumentNode->Attribute("type");
1051 if (argDirection != nullptr &&
1052 argType != nullptr &&
1053 std::string(argDirection) == "in")
1054 {
1055 if (argIt == transaction->arguments.end())
1056 {
1057 transaction->setErrorStatus(
1058 "Invalid method args");
1059 return;
1060 }
1061 if (convertJsonToDbus(m.get(),
1062 std::string(argType),
1063 *argIt) < 0)
1064 {
1065 transaction->setErrorStatus(
1066 "Invalid method arg type");
1067 return;
1068 }
1069
1070 argIt++;
1071 }
1072 argumentNode =
1073 argumentNode->NextSiblingElement("arg");
1074 }
1075
1076 crow::connections::systemBus->async_send(
Matt Spinler16caaee2019-01-15 11:40:34 -06001077 m, [transaction, returnType](
1078 boost::system::error_code ec,
1079 sdbusplus::message::message &m) {
Matt Spinler318bd892019-01-15 09:59:20 -06001080 if (ec)
1081 {
Matt Spinler16caaee2019-01-15 11:40:34 -06001082 transaction->methodFailed = true;
Matt Spinler318bd892019-01-15 09:59:20 -06001083 return;
1084 }
Matt Spinler16caaee2019-01-15 11:40:34 -06001085 else
1086 {
1087 transaction->methodPassed = true;
1088 }
1089
1090 handleMethodResponse(transaction, m,
1091 returnType);
Matt Spinler318bd892019-01-15 09:59:20 -06001092 });
1093 break;
1094 }
1095 methodNode = methodNode->NextSiblingElement("method");
1096 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097 }
Matt Spinler318bd892019-01-15 09:59:20 -06001098 interfaceNode = interfaceNode->NextSiblingElement("interface");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001099 }
1100 },
1101 connectionName, transaction->path,
1102 "org.freedesktop.DBus.Introspectable", "Introspect");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001103}
1104
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001105void handleAction(const crow::Request &req, crow::Response &res,
1106 const std::string &objectPath, const std::string &methodName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001107{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001108 BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method "
1109 << methodName;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001110 nlohmann::json requestDbusData =
1111 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001112
Ed Tanous1abe55e2018-09-05 08:30:59 -07001113 if (requestDbusData.is_discarded())
1114 {
Matt Spinler6db06242018-12-11 11:21:22 -06001115 setErrorResponse(res, boost::beast::http::status::bad_request,
1116 noJsonDesc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001117 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001118 return;
1119 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001120 nlohmann::json::iterator data = requestDbusData.find("data");
1121 if (data == requestDbusData.end())
1122 {
Matt Spinler6db06242018-12-11 11:21:22 -06001123 setErrorResponse(res, boost::beast::http::status::bad_request,
1124 noJsonDesc, badReqMsg);
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001125 res.end();
1126 return;
1127 }
1128
1129 if (!data->is_array())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001130 {
Matt Spinler6db06242018-12-11 11:21:22 -06001131 setErrorResponse(res, boost::beast::http::status::bad_request,
1132 noJsonDesc, badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001133 res.end();
1134 return;
1135 }
1136 auto transaction = std::make_shared<InProgressActionData>(res);
1137
1138 transaction->path = objectPath;
1139 transaction->methodName = methodName;
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001140 transaction->arguments = std::move(*data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001141 crow::connections::systemBus->async_method_call(
1142 [transaction](
1143 const boost::system::error_code ec,
1144 const std::vector<std::pair<std::string, std::vector<std::string>>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001145 &interfaceNames) {
1146 if (ec || interfaceNames.size() <= 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001147 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001148 BMCWEB_LOG_ERROR << "Can't find object";
Matt Spinler6db06242018-12-11 11:21:22 -06001149 setErrorResponse(transaction->res,
1150 boost::beast::http::status::not_found,
1151 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001152 return;
1153 }
1154
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001155 BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size()
1156 << " object(s)";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001157
1158 for (const std::pair<std::string, std::vector<std::string>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001159 &object : interfaceNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001160 {
1161 findActionOnInterface(transaction, object.first);
1162 }
1163 },
1164 "xyz.openbmc_project.ObjectMapper",
1165 "/xyz/openbmc_project/object_mapper",
1166 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
1167 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001168}
1169
Matt Spinlerde818812018-12-11 16:39:20 -06001170void handleDelete(const crow::Request &req, crow::Response &res,
1171 const std::string &objectPath)
1172{
1173 BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath;
1174
1175 crow::connections::systemBus->async_method_call(
1176 [&res, objectPath](
1177 const boost::system::error_code ec,
1178 const std::vector<std::pair<std::string, std::vector<std::string>>>
1179 &interfaceNames) {
1180 if (ec || interfaceNames.size() <= 0)
1181 {
1182 BMCWEB_LOG_ERROR << "Can't find object";
1183 setErrorResponse(res, boost::beast::http::status::not_found,
1184 notFoundDesc, notFoundMsg);
1185 res.end();
1186 return;
1187 }
1188
1189 auto transaction = std::make_shared<InProgressActionData>(res);
1190 transaction->path = objectPath;
1191 transaction->methodName = "Delete";
1192 transaction->interfaceName = "xyz.openbmc_project.Object.Delete";
1193
1194 for (const std::pair<std::string, std::vector<std::string>>
1195 &object : interfaceNames)
1196 {
1197 findActionOnInterface(transaction, object.first);
1198 }
1199 },
1200 "xyz.openbmc_project.ObjectMapper",
1201 "/xyz/openbmc_project/object_mapper",
1202 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
1203 std::array<const char *, 0>());
1204}
1205
Ed Tanousf839dfe2018-11-12 11:11:15 -08001206void handleList(crow::Response &res, const std::string &objectPath,
1207 int32_t depth = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001208{
1209 crow::connections::systemBus->async_method_call(
1210 [&res](const boost::system::error_code ec,
1211 std::vector<std::string> &objectPaths) {
1212 if (ec)
1213 {
Matt Spinlerd6091dd2018-12-06 14:08:27 -06001214 setErrorResponse(res, boost::beast::http::status::not_found,
1215 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001216 }
1217 else
1218 {
1219 res.jsonValue = {{"status", "ok"},
1220 {"message", "200 OK"},
1221 {"data", std::move(objectPaths)}};
1222 }
1223 res.end();
1224 },
1225 "xyz.openbmc_project.ObjectMapper",
1226 "/xyz/openbmc_project/object_mapper",
1227 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousf839dfe2018-11-12 11:11:15 -08001228 depth, std::array<std::string, 0>());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001229}
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001230
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001231void handleEnumerate(crow::Response &res, const std::string &objectPath)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001232{
Ed Tanous049a0512018-11-01 13:58:42 -07001233 BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath;
1234 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
1235
1236 asyncResp->res.jsonValue = {{"message", "200 OK"},
1237 {"status", "ok"},
1238 {"data", nlohmann::json::object()}};
1239
Ed Tanous1abe55e2018-09-05 08:30:59 -07001240 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001241 [objectPath, asyncResp](const boost::system::error_code ec,
1242 GetSubTreeType &object_names) {
1243 auto transaction = std::make_shared<InProgressEnumerateData>(
1244 objectPath, asyncResp);
1245
1246 transaction->subtree =
1247 std::make_shared<GetSubTreeType>(std::move(object_names));
1248
Ed Tanous1abe55e2018-09-05 08:30:59 -07001249 if (ec)
1250 {
Matt Spinler2ae60092018-12-06 10:35:36 -06001251 BMCWEB_LOG_ERROR << "GetSubTree failed on "
1252 << transaction->objectPath;
1253 setErrorResponse(transaction->asyncResp->res,
1254 boost::beast::http::status::not_found,
1255 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001256 return;
1257 }
Ed Tanous64530012018-02-06 17:08:16 -08001258
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001259 // Add the data for the path passed in to the results
1260 // as if GetSubTree returned it, and continue on enumerating
1261 getObjectAndEnumerate(transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001262 },
1263 "xyz.openbmc_project.ObjectMapper",
1264 "/xyz/openbmc_project/object_mapper",
1265 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath,
Ed Tanous049a0512018-11-01 13:58:42 -07001266 static_cast<int32_t>(0), std::array<const char *, 0>());
Ed Tanous64530012018-02-06 17:08:16 -08001267}
Ed Tanous911ac312017-08-15 09:37:42 -07001268
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001269void handleGet(crow::Response &res, std::string &objectPath,
1270 std::string &destProperty)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001271{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001272 BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty;
1273 std::shared_ptr<std::string> propertyName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001274 std::make_shared<std::string>(std::move(destProperty));
Ed Tanous75db20e2018-07-27 13:44:44 -07001275
Ed Tanous1abe55e2018-09-05 08:30:59 -07001276 std::shared_ptr<std::string> path =
1277 std::make_shared<std::string>(std::move(objectPath));
Ed Tanous75db20e2018-07-27 13:44:44 -07001278
Ed Tanous1abe55e2018-09-05 08:30:59 -07001279 using GetObjectType =
1280 std::vector<std::pair<std::string, std::vector<std::string>>>;
1281 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001282 [&res, path, propertyName](const boost::system::error_code ec,
1283 const GetObjectType &object_names) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001284 if (ec || object_names.size() <= 0)
1285 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001286 setErrorResponse(res, boost::beast::http::status::not_found,
1287 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001288 res.end();
1289 return;
1290 }
1291 std::shared_ptr<nlohmann::json> response =
1292 std::make_shared<nlohmann::json>(nlohmann::json::object());
1293 // The mapper should never give us an empty interface names list,
1294 // but check anyway
1295 for (const std::pair<std::string, std::vector<std::string>>
1296 connection : object_names)
1297 {
1298 const std::vector<std::string> &interfaceNames =
1299 connection.second;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001300
Ed Tanous1abe55e2018-09-05 08:30:59 -07001301 if (interfaceNames.size() <= 0)
1302 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001303 setErrorResponse(res, boost::beast::http::status::not_found,
1304 notFoundDesc, notFoundMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001305 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001306 return;
1307 }
1308
1309 for (const std::string &interface : interfaceNames)
1310 {
1311 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001312 [&res, response, propertyName](
Ed Tanous1abe55e2018-09-05 08:30:59 -07001313 const boost::system::error_code ec,
James Feist5b4aa862018-08-16 14:07:01 -07001314 const std::vector<std::pair<
1315 std::string, dbus::utility::DbusVariantType>>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001316 &properties) {
1317 if (ec)
1318 {
1319 BMCWEB_LOG_ERROR << "Bad dbus request error: "
1320 << ec;
1321 }
1322 else
1323 {
James Feist5b4aa862018-08-16 14:07:01 -07001324 for (const std::pair<
1325 std::string,
1326 dbus::utility::DbusVariantType>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001327 &property : properties)
1328 {
1329 // if property name is empty, or matches our
1330 // search query, add it to the response json
1331
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001332 if (propertyName->empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001333 {
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07001334 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001335 [&response, &property](auto &&val) {
1336 (*response)[property.first] =
1337 val;
1338 },
1339 property.second);
1340 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001341 else if (property.first == *propertyName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001342 {
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07001343 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001344 [&response](auto &&val) {
1345 (*response) = val;
1346 },
1347 property.second);
1348 }
1349 }
1350 }
1351 if (response.use_count() == 1)
1352 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001353 if (!propertyName->empty() && response->empty())
1354 {
1355 setErrorResponse(
1356 res,
1357 boost::beast::http::status::not_found,
1358 propNotFoundDesc, notFoundMsg);
1359 }
1360 else
1361 {
1362 res.jsonValue = {{"status", "ok"},
1363 {"message", "200 OK"},
1364 {"data", *response}};
1365 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001366 res.end();
1367 }
1368 },
1369 connection.first, *path,
1370 "org.freedesktop.DBus.Properties", "GetAll", interface);
1371 }
1372 }
1373 },
1374 "xyz.openbmc_project.ObjectMapper",
1375 "/xyz/openbmc_project/object_mapper",
1376 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
1377 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001378}
1379
Ed Tanous1abe55e2018-09-05 08:30:59 -07001380struct AsyncPutRequest
1381{
1382 AsyncPutRequest(crow::Response &res) : res(res)
1383 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001384 }
1385 ~AsyncPutRequest()
1386 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001387 if (res.jsonValue.empty())
1388 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001389 setErrorResponse(res, boost::beast::http::status::forbidden,
1390 forbiddenMsg, forbiddenPropDesc);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001391 }
1392
1393 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001394 }
1395
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001396 void setErrorStatus(const std::string &desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001397 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001398 setErrorResponse(res, boost::beast::http::status::internal_server_error,
1399 desc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001400 }
1401
Ed Tanous1abe55e2018-09-05 08:30:59 -07001402 crow::Response &res;
1403 std::string objectPath;
1404 std::string propertyName;
1405 nlohmann::json propertyValue;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001406};
1407
Ed Tanousd76323e2018-08-07 14:35:40 -07001408void handlePut(const crow::Request &req, crow::Response &res,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001409 const std::string &objectPath, const std::string &destProperty)
1410{
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001411 if (destProperty.empty())
1412 {
1413 setErrorResponse(res, boost::beast::http::status::forbidden,
1414 forbiddenResDesc, forbiddenMsg);
1415 res.end();
1416 return;
1417 }
1418
Ed Tanous1abe55e2018-09-05 08:30:59 -07001419 nlohmann::json requestDbusData =
1420 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001421
Ed Tanous1abe55e2018-09-05 08:30:59 -07001422 if (requestDbusData.is_discarded())
1423 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001424 setErrorResponse(res, boost::beast::http::status::bad_request,
1425 noJsonDesc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001426 res.end();
1427 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001428 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001429
Ed Tanous1abe55e2018-09-05 08:30:59 -07001430 nlohmann::json::const_iterator propertyIt = requestDbusData.find("data");
1431 if (propertyIt == requestDbusData.end())
1432 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001433 setErrorResponse(res, boost::beast::http::status::bad_request,
1434 noJsonDesc, badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001435 res.end();
1436 return;
1437 }
1438 const nlohmann::json &propertySetValue = *propertyIt;
1439 auto transaction = std::make_shared<AsyncPutRequest>(res);
1440 transaction->objectPath = objectPath;
1441 transaction->propertyName = destProperty;
1442 transaction->propertyValue = propertySetValue;
Ed Tanous911ac312017-08-15 09:37:42 -07001443
Ed Tanous1abe55e2018-09-05 08:30:59 -07001444 using GetObjectType =
1445 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous911ac312017-08-15 09:37:42 -07001446
Ed Tanous1abe55e2018-09-05 08:30:59 -07001447 crow::connections::systemBus->async_method_call(
1448 [transaction](const boost::system::error_code ec,
1449 const GetObjectType &object_names) {
1450 if (!ec && object_names.size() <= 0)
1451 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001452 setErrorResponse(transaction->res,
1453 boost::beast::http::status::not_found,
1454 propNotFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001455 return;
1456 }
Ed Tanous911ac312017-08-15 09:37:42 -07001457
Ed Tanous1abe55e2018-09-05 08:30:59 -07001458 for (const std::pair<std::string, std::vector<std::string>>
1459 connection : object_names)
1460 {
1461 const std::string &connectionName = connection.first;
Ed Tanous911ac312017-08-15 09:37:42 -07001462
Ed Tanous1abe55e2018-09-05 08:30:59 -07001463 crow::connections::systemBus->async_method_call(
1464 [connectionName{std::string(connectionName)},
1465 transaction](const boost::system::error_code ec,
1466 const std::string &introspectXml) {
1467 if (ec)
1468 {
1469 BMCWEB_LOG_ERROR
1470 << "Introspect call failed with error: "
1471 << ec.message()
1472 << " on process: " << connectionName;
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001473 transaction->setErrorStatus("Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001474 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001475 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001476 tinyxml2::XMLDocument doc;
Ed Tanous911ac312017-08-15 09:37:42 -07001477
Ed Tanous1abe55e2018-09-05 08:30:59 -07001478 doc.Parse(introspectXml.c_str());
1479 tinyxml2::XMLNode *pRoot =
1480 doc.FirstChildElement("node");
1481 if (pRoot == nullptr)
1482 {
1483 BMCWEB_LOG_ERROR << "XML document failed to parse: "
1484 << introspectXml;
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001485 transaction->setErrorStatus("Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001486 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001487 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001488 tinyxml2::XMLElement *ifaceNode =
1489 pRoot->FirstChildElement("interface");
1490 while (ifaceNode != nullptr)
1491 {
1492 const char *interfaceName =
1493 ifaceNode->Attribute("name");
1494 BMCWEB_LOG_DEBUG << "found interface "
1495 << interfaceName;
1496 tinyxml2::XMLElement *propNode =
1497 ifaceNode->FirstChildElement("property");
1498 while (propNode != nullptr)
1499 {
1500 const char *propertyName =
1501 propNode->Attribute("name");
1502 BMCWEB_LOG_DEBUG << "Found property "
1503 << propertyName;
1504 if (propertyName == transaction->propertyName)
1505 {
1506 const char *argType =
1507 propNode->Attribute("type");
1508 if (argType != nullptr)
1509 {
1510 sdbusplus::message::message m =
1511 crow::connections::systemBus
1512 ->new_method_call(
1513 connectionName.c_str(),
1514 transaction->objectPath
1515 .c_str(),
1516 "org.freedesktop.DBus."
1517 "Properties",
1518 "Set");
1519 m.append(interfaceName,
1520 transaction->propertyName);
1521 int r = sd_bus_message_open_container(
1522 m.get(), SD_BUS_TYPE_VARIANT,
1523 argType);
1524 if (r < 0)
1525 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001526 transaction->setErrorStatus(
1527 "Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001528 return;
1529 }
1530 r = convertJsonToDbus(
1531 m.get(), argType,
1532 transaction->propertyValue);
1533 if (r < 0)
1534 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001535 transaction->setErrorStatus(
1536 "Invalid arg type");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001537 return;
1538 }
1539 r = sd_bus_message_close_container(
1540 m.get());
1541 if (r < 0)
1542 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001543 transaction->setErrorStatus(
1544 "Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001545 return;
1546 }
Ed Tanous911ac312017-08-15 09:37:42 -07001547
Ed Tanous1abe55e2018-09-05 08:30:59 -07001548 crow::connections::systemBus
1549 ->async_send(
1550 m,
1551 [transaction](
1552 boost::system::error_code
1553 ec,
1554 sdbusplus::message::message
1555 &m) {
1556 BMCWEB_LOG_DEBUG << "sent";
1557 if (ec)
1558 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001559 setErrorResponse(
1560 transaction->res,
1561 boost::beast::http::
1562 status::
1563 forbidden,
1564 forbiddenPropDesc,
1565 ec.message());
1566 }
1567 else
1568 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001569 transaction->res
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001570 .jsonValue = {
1571 {"status", "ok"},
1572 {"message",
1573 "200 OK"},
1574 {"data", nullptr}};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001575 }
1576 });
1577 }
1578 }
1579 propNode =
1580 propNode->NextSiblingElement("property");
1581 }
1582 ifaceNode =
1583 ifaceNode->NextSiblingElement("interface");
1584 }
1585 },
1586 connectionName, transaction->objectPath,
1587 "org.freedesktop.DBus.Introspectable", "Introspect");
1588 }
1589 },
1590 "xyz.openbmc_project.ObjectMapper",
1591 "/xyz/openbmc_project/object_mapper",
1592 "xyz.openbmc_project.ObjectMapper", "GetObject",
1593 transaction->objectPath, std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001594}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001595
Ed Tanous049a0512018-11-01 13:58:42 -07001596inline void handleDBusUrl(const crow::Request &req, crow::Response &res,
1597 std::string &objectPath)
1598{
Ed Tanous049a0512018-11-01 13:58:42 -07001599
1600 // If accessing a single attribute, fill in and update objectPath,
1601 // otherwise leave destProperty blank
1602 std::string destProperty = "";
1603 const char *attrSeperator = "/attr/";
1604 size_t attrPosition = objectPath.find(attrSeperator);
1605 if (attrPosition != objectPath.npos)
1606 {
1607 destProperty = objectPath.substr(attrPosition + strlen(attrSeperator),
1608 objectPath.length());
1609 objectPath = objectPath.substr(0, attrPosition);
1610 }
1611
1612 if (req.method() == "POST"_method)
1613 {
1614 constexpr const char *actionSeperator = "/action/";
1615 size_t actionPosition = objectPath.find(actionSeperator);
1616 if (actionPosition != objectPath.npos)
1617 {
1618 std::string postProperty =
1619 objectPath.substr((actionPosition + strlen(actionSeperator)),
1620 objectPath.length());
1621 objectPath = objectPath.substr(0, actionPosition);
1622 handleAction(req, res, objectPath, postProperty);
1623 return;
1624 }
1625 }
1626 else if (req.method() == "GET"_method)
1627 {
1628 if (boost::ends_with(objectPath, "/enumerate"))
1629 {
1630 objectPath.erase(objectPath.end() - sizeof("enumerate"),
1631 objectPath.end());
1632 handleEnumerate(res, objectPath);
1633 }
1634 else if (boost::ends_with(objectPath, "/list"))
1635 {
1636 objectPath.erase(objectPath.end() - sizeof("list"),
1637 objectPath.end());
1638 handleList(res, objectPath);
1639 }
1640 else
1641 {
Ed Tanousf839dfe2018-11-12 11:11:15 -08001642 // Trim any trailing "/" at the end
1643 if (boost::ends_with(objectPath, "/"))
1644 {
1645 objectPath.pop_back();
1646 handleList(res, objectPath, 1);
1647 }
1648 else
1649 {
1650 handleGet(res, objectPath, destProperty);
1651 }
Ed Tanous049a0512018-11-01 13:58:42 -07001652 }
1653 return;
1654 }
1655 else if (req.method() == "PUT"_method)
1656 {
1657 handlePut(req, res, objectPath, destProperty);
1658 return;
1659 }
Matt Spinlerde818812018-12-11 16:39:20 -06001660 else if (req.method() == "DELETE"_method)
1661 {
1662 handleDelete(req, res, objectPath);
1663 return;
1664 }
Ed Tanous049a0512018-11-01 13:58:42 -07001665
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -06001666 setErrorResponse(res, boost::beast::http::status::method_not_allowed,
1667 methodNotAllowedDesc, methodNotAllowedMsg);
Ed Tanous049a0512018-11-01 13:58:42 -07001668 res.end();
1669}
1670
Ed Tanous1abe55e2018-09-05 08:30:59 -07001671template <typename... Middlewares> void requestRoutes(Crow<Middlewares...> &app)
1672{
1673 BMCWEB_ROUTE(app, "/bus/")
1674 .methods("GET"_method)(
1675 [](const crow::Request &req, crow::Response &res) {
1676 res.jsonValue = {{"busses", {{{"name", "system"}}}},
1677 {"status", "ok"}};
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001678 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001679 });
1680
1681 BMCWEB_ROUTE(app, "/bus/system/")
1682 .methods("GET"_method)(
1683 [](const crow::Request &req, crow::Response &res) {
1684 auto myCallback = [&res](const boost::system::error_code ec,
1685 std::vector<std::string> &names) {
1686 if (ec)
1687 {
1688 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
1689 res.result(
1690 boost::beast::http::status::internal_server_error);
1691 }
1692 else
1693 {
1694 std::sort(names.begin(), names.end());
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001695 res.jsonValue = {{"status", "ok"}};
1696 auto &objectsSub = res.jsonValue["objects"];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001697 for (auto &name : names)
1698 {
1699 objectsSub.push_back({{"name", name}});
1700 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001701 }
1702 res.end();
1703 };
1704 crow::connections::systemBus->async_method_call(
1705 std::move(myCallback), "org.freedesktop.DBus", "/",
1706 "org.freedesktop.DBus", "ListNames");
1707 });
1708
1709 BMCWEB_ROUTE(app, "/list/")
1710 .methods("GET"_method)(
1711 [](const crow::Request &req, crow::Response &res) {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001712 handleList(res, "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001713 });
1714
1715 BMCWEB_ROUTE(app, "/xyz/<path>")
Matt Spinlerde818812018-12-11 16:39:20 -06001716 .methods("GET"_method, "PUT"_method, "POST"_method, "DELETE"_method)(
Ed Tanous049a0512018-11-01 13:58:42 -07001717 [](const crow::Request &req, crow::Response &res,
1718 const std::string &path) {
1719 std::string objectPath = "/xyz/" + path;
1720 handleDBusUrl(req, res, objectPath);
1721 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001722
Ed Tanous049a0512018-11-01 13:58:42 -07001723 BMCWEB_ROUTE(app, "/org/<path>")
Matt Spinlerde818812018-12-11 16:39:20 -06001724 .methods("GET"_method, "PUT"_method, "POST"_method, "DELETE"_method)(
Ed Tanous049a0512018-11-01 13:58:42 -07001725 [](const crow::Request &req, crow::Response &res,
1726 const std::string &path) {
1727 std::string objectPath = "/org/" + path;
1728 handleDBusUrl(req, res, objectPath);
1729 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001730
Ed Tanous1abe55e2018-09-05 08:30:59 -07001731 BMCWEB_ROUTE(app, "/download/dump/<str>/")
1732 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
1733 const std::string &dumpId) {
Ed Tanousad18f072018-11-14 14:07:48 -08001734 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]*)$");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001735 if (!std::regex_match(dumpId, validFilename))
1736 {
Ed Tanousad18f072018-11-14 14:07:48 -08001737 res.result(boost::beast::http::status::bad_request);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001738 res.end();
1739 return;
1740 }
James Feistf6150402019-01-08 10:36:20 -08001741 std::filesystem::path loc(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001742 "/var/lib/phosphor-debug-collector/dumps");
1743
Ed Tanousad18f072018-11-14 14:07:48 -08001744 loc /= dumpId;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001745
James Feistf6150402019-01-08 10:36:20 -08001746 if (!std::filesystem::exists(loc) ||
1747 !std::filesystem::is_directory(loc))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001748 {
Ed Tanousad18f072018-11-14 14:07:48 -08001749 BMCWEB_LOG_ERROR << loc << "Not found";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001750 res.result(boost::beast::http::status::not_found);
1751 res.end();
1752 return;
1753 }
James Feistf6150402019-01-08 10:36:20 -08001754 std::filesystem::directory_iterator files(loc);
Ed Tanousad18f072018-11-14 14:07:48 -08001755
Ed Tanous1abe55e2018-09-05 08:30:59 -07001756 for (auto &file : files)
1757 {
1758 std::ifstream readFile(file.path());
Ed Tanousad18f072018-11-14 14:07:48 -08001759 if (!readFile.good())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001760 {
1761 continue;
1762 }
1763 res.addHeader("Content-Type", "application/octet-stream");
1764 res.body() = {std::istreambuf_iterator<char>(readFile),
1765 std::istreambuf_iterator<char>()};
1766 res.end();
Ed Tanousad18f072018-11-14 14:07:48 -08001767 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001768 }
1769 res.result(boost::beast::http::status::not_found);
1770 res.end();
1771 return;
1772 });
1773
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001774 BMCWEB_ROUTE(app, "/bus/system/<str>/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001775 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001776 const std::string &Connection) {
1777 introspectObjects(Connection, "/",
1778 std::make_shared<bmcweb::AsyncResp>(res));
1779 });
1780
1781 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
1782 .methods("GET"_method,
1783 "POST"_method)([](const crow::Request &req,
1784 crow::Response &res,
1785 const std::string &processName,
1786 const std::string &requestedPath) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001787 std::vector<std::string> strs;
1788 boost::split(strs, requestedPath, boost::is_any_of("/"));
1789 std::string objectPath;
1790 std::string interfaceName;
1791 std::string methodName;
1792 auto it = strs.begin();
1793 if (it == strs.end())
1794 {
1795 objectPath = "/";
1796 }
1797 while (it != strs.end())
1798 {
1799 // Check if segment contains ".". If it does, it must be an
1800 // interface
1801 if (it->find(".") != std::string::npos)
1802 {
1803 break;
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001804 // This check is neccesary as the trailing slash gets parsed
Ed Tanous1abe55e2018-09-05 08:30:59 -07001805 // as part of our <path> specifier above, which causes the
1806 // normal trailing backslash redirector to fail.
1807 }
1808 else if (!it->empty())
1809 {
1810 objectPath += "/" + *it;
1811 }
1812 it++;
1813 }
1814 if (it != strs.end())
1815 {
1816 interfaceName = *it;
1817 it++;
1818
1819 // after interface, we might have a method name
1820 if (it != strs.end())
1821 {
1822 methodName = *it;
1823 it++;
1824 }
1825 }
1826 if (it != strs.end())
1827 {
1828 // if there is more levels past the method name, something went
1829 // wrong, return not found
1830 res.result(boost::beast::http::status::not_found);
1831 res.end();
1832 return;
1833 }
1834 if (interfaceName.empty())
1835 {
1836 crow::connections::systemBus->async_method_call(
1837 [&, processName,
1838 objectPath](const boost::system::error_code ec,
1839 const std::string &introspect_xml) {
1840 if (ec)
1841 {
1842 BMCWEB_LOG_ERROR
1843 << "Introspect call failed with error: "
1844 << ec.message()
1845 << " on process: " << processName
1846 << " path: " << objectPath << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001847 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001848 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001849 tinyxml2::XMLDocument doc;
1850
1851 doc.Parse(introspect_xml.c_str());
1852 tinyxml2::XMLNode *pRoot =
1853 doc.FirstChildElement("node");
1854 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001855 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001856 BMCWEB_LOG_ERROR << "XML document failed to parse "
1857 << processName << " " << objectPath
1858 << "\n";
1859 res.jsonValue = {{"status", "XML parse error"}};
1860 res.result(boost::beast::http::status::
1861 internal_server_error);
1862 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001863 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001864
1865 BMCWEB_LOG_DEBUG << introspect_xml;
1866 res.jsonValue = {{"status", "ok"},
1867 {"bus_name", processName},
1868 {"object_path", objectPath}};
1869 nlohmann::json &interfacesArray =
1870 res.jsonValue["interfaces"];
1871 interfacesArray = nlohmann::json::array();
1872 tinyxml2::XMLElement *interface =
1873 pRoot->FirstChildElement("interface");
1874
1875 while (interface != nullptr)
1876 {
1877 const char *ifaceName =
1878 interface->Attribute("name");
1879 if (ifaceName != nullptr)
1880 {
1881 interfacesArray.push_back(
1882 {{"name", ifaceName}});
1883 }
1884
1885 interface =
1886 interface->NextSiblingElement("interface");
1887 }
1888
Ed Tanous1abe55e2018-09-05 08:30:59 -07001889 res.end();
1890 },
1891 processName, objectPath,
1892 "org.freedesktop.DBus.Introspectable", "Introspect");
1893 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001894 else if (methodName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001895 {
1896 crow::connections::systemBus->async_method_call(
1897 [&, processName, objectPath,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001898 interfaceName{std::move(interfaceName)}](
Ed Tanous1abe55e2018-09-05 08:30:59 -07001899 const boost::system::error_code ec,
1900 const std::string &introspect_xml) {
1901 if (ec)
1902 {
1903 BMCWEB_LOG_ERROR
1904 << "Introspect call failed with error: "
1905 << ec.message()
1906 << " on process: " << processName
1907 << " path: " << objectPath << "\n";
1908 }
1909 else
1910 {
1911 tinyxml2::XMLDocument doc;
1912
1913 doc.Parse(introspect_xml.c_str());
1914 tinyxml2::XMLNode *pRoot =
1915 doc.FirstChildElement("node");
1916 if (pRoot == nullptr)
1917 {
1918 BMCWEB_LOG_ERROR
1919 << "XML document failed to parse "
1920 << processName << " " << objectPath << "\n";
1921 res.result(boost::beast::http::status::
1922 internal_server_error);
1923 }
1924 else
1925 {
1926 tinyxml2::XMLElement *node =
1927 pRoot->FirstChildElement("node");
1928
1929 // if we know we're the only call, build the
1930 // json directly
Ed Tanous1abe55e2018-09-05 08:30:59 -07001931 tinyxml2::XMLElement *interface =
1932 pRoot->FirstChildElement("interface");
1933
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001934 res.jsonValue = {
1935 {"status", "ok"},
1936 {"bus_name", processName},
1937 {"interface", interfaceName},
1938 {"object_path", objectPath},
1939 {"properties", nlohmann::json::object()}};
1940
1941 nlohmann::json &methodsArray =
1942 res.jsonValue["methods"];
1943 methodsArray = nlohmann::json::array();
1944
1945 nlohmann::json &signalsArray =
1946 res.jsonValue["signals"];
1947 signalsArray = nlohmann::json::array();
1948
Ed Tanous1abe55e2018-09-05 08:30:59 -07001949 while (interface != nullptr)
1950 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001951 const char *ifaceName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001952 interface->Attribute("name");
1953
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001954 if (ifaceName != nullptr &&
1955 ifaceName == interfaceName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001956 {
1957 tinyxml2::XMLElement *methods =
1958 interface->FirstChildElement(
1959 "method");
1960 while (methods != nullptr)
1961 {
1962 nlohmann::json argsArray =
1963 nlohmann::json::array();
1964 tinyxml2::XMLElement *arg =
1965 methods->FirstChildElement(
1966 "arg");
1967 while (arg != nullptr)
1968 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001969 nlohmann::json thisArg;
1970 for (const char *fieldName :
1971 std::array<const char *,
1972 3>{"name",
1973 "direction",
1974 "type"})
1975 {
1976 const char *fieldValue =
1977 arg->Attribute(
1978 fieldName);
1979 if (fieldValue != nullptr)
1980 {
1981 thisArg[fieldName] =
1982 fieldValue;
1983 }
1984 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001985 argsArray.push_back(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001986 std::move(thisArg));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001987 arg = arg->NextSiblingElement(
1988 "arg");
1989 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001990
1991 const char *name =
1992 methods->Attribute("name");
1993 if (name != nullptr)
1994 {
1995 methodsArray.push_back(
1996 {{"name", name},
1997 {"uri", "/bus/system/" +
1998 processName +
1999 objectPath +
2000 "/" +
2001 interfaceName +
2002 "/" + name},
2003 {"args", argsArray}});
2004 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002005 methods =
2006 methods->NextSiblingElement(
2007 "method");
2008 }
2009 tinyxml2::XMLElement *signals =
2010 interface->FirstChildElement(
2011 "signal");
2012 while (signals != nullptr)
2013 {
2014 nlohmann::json argsArray =
2015 nlohmann::json::array();
2016
2017 tinyxml2::XMLElement *arg =
2018 signals->FirstChildElement(
2019 "arg");
2020 while (arg != nullptr)
2021 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002022 const char *name =
Ed Tanous1abe55e2018-09-05 08:30:59 -07002023 arg->Attribute("name");
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002024 const char *type =
Ed Tanous1abe55e2018-09-05 08:30:59 -07002025 arg->Attribute("type");
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002026 if (name != nullptr &&
2027 type != nullptr)
2028 {
2029 argsArray.push_back({
2030 {"name", name},
2031 {"type", type},
2032 });
2033 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002034 arg = arg->NextSiblingElement(
2035 "arg");
2036 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002037 const char *name =
2038 signals->Attribute("name");
2039 if (name != nullptr)
2040 {
2041 signalsArray.push_back(
2042 {{"name", name},
2043 {"args", argsArray}});
2044 }
2045
Ed Tanous1abe55e2018-09-05 08:30:59 -07002046 signals =
2047 signals->NextSiblingElement(
2048 "signal");
2049 }
2050
Ed Tanous1abe55e2018-09-05 08:30:59 -07002051 break;
2052 }
2053
2054 interface = interface->NextSiblingElement(
2055 "interface");
2056 }
2057 if (interface == nullptr)
2058 {
2059 // if we got to the end of the list and
2060 // never found a match, throw 404
2061 res.result(
2062 boost::beast::http::status::not_found);
2063 }
2064 }
2065 }
2066 res.end();
2067 },
2068 processName, objectPath,
2069 "org.freedesktop.DBus.Introspectable", "Introspect");
2070 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002071 else
2072 {
2073 if (req.method() != "POST"_method)
2074 {
2075 res.result(boost::beast::http::status::not_found);
2076 res.end();
2077 return;
2078 }
2079
2080 nlohmann::json requestDbusData =
2081 nlohmann::json::parse(req.body, nullptr, false);
2082
2083 if (requestDbusData.is_discarded())
2084 {
2085 res.result(boost::beast::http::status::bad_request);
2086 res.end();
2087 return;
2088 }
2089 if (!requestDbusData.is_array())
2090 {
2091 res.result(boost::beast::http::status::bad_request);
2092 res.end();
2093 return;
2094 }
2095 auto transaction = std::make_shared<InProgressActionData>(res);
2096
2097 transaction->path = objectPath;
2098 transaction->methodName = methodName;
2099 transaction->arguments = std::move(requestDbusData);
2100
2101 findActionOnInterface(transaction, processName);
2102 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002103 });
2104}
2105} // namespace openbmc_mapper
2106} // namespace crow