blob: cfbe4cb109f8638b07581e7f33d7b2e887434b04 [file] [log] [blame]
James Feist5b4aa862018-08-16 14:07:01 -07001// Copyright (c) 2018 Intel Corporation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Ed Tanousb9b2e0b2018-09-13 13:47:50 -070014
James Feist5b4aa862018-08-16 14:07:01 -070015#pragma once
Ed Tanous911ac312017-08-15 09:37:42 -070016#include <crow/app.h>
Ed Tanous911ac312017-08-15 09:37:42 -070017#include <tinyxml2.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -070018
Ed Tanouse3cb5a32018-08-08 14:16:49 -070019#include <async_resp.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070020#include <boost/algorithm/string.hpp>
21#include <boost/container/flat_set.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070022#include <dbus_singleton.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070023#include <dbus_utility.hpp>
James Feist4418c7f2019-04-15 11:09:15 -070024#include <filesystem>
Ed Tanousd4bb9bb2018-05-16 13:36:42 -070025#include <fstream>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -070026#include <sdbusplus/message/types.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace crow
29{
30namespace openbmc_mapper
31{
Ed Tanousba9f9a62017-10-11 16:40:35 -070032
Matt Spinler3ae4ba72018-12-05 14:01:22 -060033using GetSubTreeType = std::vector<
34 std::pair<std::string,
35 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
36
Matt Spinler2ae60092018-12-06 10:35:36 -060037const std::string notFoundMsg = "404 Not Found";
Matt Spinler6db06242018-12-11 11:21:22 -060038const std::string badReqMsg = "400 Bad Request";
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -060039const std::string methodNotAllowedMsg = "405 Method Not Allowed";
Matt Spinlerfbc19ea2018-12-11 14:03:42 -060040const std::string forbiddenMsg = "403 Forbidden";
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -060041const std::string methodFailedMsg = "500 Method Call Failed";
Matt Spinler16caaee2019-01-15 11:40:34 -060042const std::string methodOutputFailedMsg = "500 Method Output Error";
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];
Ed Tanousabf2add2019-01-22 16:40:12 -0800157 std::visit([&propertyJson](auto &&val) { propertyJson = val; },
158 value);
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600159 }
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];
Ed Tanousabf2add2019-01-22 16:40:12 -0800257 std::visit([&propertyJson](
258 auto &&val) { propertyJson = val; },
259 property.second);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700260 }
261 }
262 }
Ed Tanous049a0512018-11-01 13:58:42 -0700263 for (const auto &interface : objectPath.second)
264 {
265 if (interface.first == "org.freedesktop.DBus.ObjectManager")
266 {
267 getManagedObjectsForEnumerate(
268 objectPath.first.str, objectPath.first.str,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600269 connection_name, transaction);
Ed Tanous049a0512018-11-01 13:58:42 -0700270 }
271 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700272 }
273 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700274 connection_name, object_manager_path,
275 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
276}
277
278void findObjectManagerPathForEnumerate(
279 const std::string &object_name, const std::string &connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600280 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700281{
Ed Tanous049a0512018-11-01 13:58:42 -0700282 BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << object_name
283 << " on connection:" << connection_name;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700284 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600285 [transaction, object_name, connection_name](
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700286 const boost::system::error_code ec,
287 const boost::container::flat_map<
288 std::string, boost::container::flat_map<
289 std::string, std::vector<std::string>>>
290 &objects) {
291 if (ec)
292 {
Ed Tanous049a0512018-11-01 13:58:42 -0700293 BMCWEB_LOG_ERROR << "GetAncestors on path " << object_name
294 << " failed with code " << ec;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700295 return;
296 }
297
Ed Tanousf254ba72018-10-12 13:40:35 -0700298 for (const auto &pathGroup : objects)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700299 {
Ed Tanousf254ba72018-10-12 13:40:35 -0700300 for (const auto &connectionGroup : pathGroup.second)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700301 {
302 if (connectionGroup.first == connection_name)
303 {
304 // Found the object manager path for this resource.
305 getManagedObjectsForEnumerate(
Ed Tanous049a0512018-11-01 13:58:42 -0700306 object_name, pathGroup.first, connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600307 transaction);
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700308 return;
309 }
310 }
311 }
312 },
313 "xyz.openbmc_project.ObjectMapper",
314 "/xyz/openbmc_project/object_mapper",
315 "xyz.openbmc_project.ObjectMapper", "GetAncestors", object_name,
316 std::array<const char *, 1>{"org.freedesktop.DBus.ObjectManager"});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700317}
Ed Tanous64530012018-02-06 17:08:16 -0800318
Ed Tanous7c091622019-05-23 11:42:36 -0700319// Uses GetObject to add the object info about the target /enumerate path to
320// the results of GetSubTree, as GetSubTree will not return info for the
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600321// target path, and then continues on enumerating the rest of the tree.
322void getObjectAndEnumerate(std::shared_ptr<InProgressEnumerateData> transaction)
323{
324 using GetObjectType =
325 std::vector<std::pair<std::string, std::vector<std::string>>>;
326
327 crow::connections::systemBus->async_method_call(
328 [transaction](const boost::system::error_code ec,
329 const GetObjectType &objects) {
330 if (ec)
331 {
332 BMCWEB_LOG_ERROR << "GetObject for path "
333 << transaction->objectPath
334 << " failed with code " << ec;
335 return;
336 }
337
338 BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath
339 << " has " << objects.size() << " entries";
340 if (!objects.empty())
341 {
342 transaction->subtree->emplace_back(transaction->objectPath,
343 objects);
344 }
345
346 // Map indicating connection name, and the path where the object
347 // manager exists
348 boost::container::flat_map<std::string, std::string> connections;
349
350 for (const auto &object : *(transaction->subtree))
351 {
352 for (const auto &connection : object.second)
353 {
354 std::string &objectManagerPath =
355 connections[connection.first];
356 for (const auto &interface : connection.second)
357 {
358 BMCWEB_LOG_DEBUG << connection.first
359 << " has interface " << interface;
360 if (interface == "org.freedesktop.DBus.ObjectManager")
361 {
362 BMCWEB_LOG_DEBUG << "found object manager path "
363 << object.first;
364 objectManagerPath = object.first;
365 }
366 }
367 }
368 }
369 BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections";
370
371 for (const auto &connection : connections)
372 {
Ed Tanous7c091622019-05-23 11:42:36 -0700373 // If we already know where the object manager is, we don't
374 // need to search for it, we can call directly in to
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600375 // getManagedObjects
376 if (!connection.second.empty())
377 {
378 getManagedObjectsForEnumerate(
379 transaction->objectPath, connection.second,
380 connection.first, transaction);
381 }
382 else
383 {
Ed Tanous7c091622019-05-23 11:42:36 -0700384 // otherwise we need to find the object manager path
385 // before we can continue
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600386 findObjectManagerPathForEnumerate(
387 transaction->objectPath, connection.first, transaction);
388 }
389 }
390 },
391 "xyz.openbmc_project.ObjectMapper",
392 "/xyz/openbmc_project/object_mapper",
393 "xyz.openbmc_project.ObjectMapper", "GetObject",
394 transaction->objectPath, std::array<const char *, 0>());
395}
Ed Tanous64530012018-02-06 17:08:16 -0800396
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700397// Structure for storing data on an in progress action
Ed Tanous1abe55e2018-09-05 08:30:59 -0700398struct InProgressActionData
399{
400 InProgressActionData(crow::Response &res) : res(res){};
401 ~InProgressActionData()
402 {
Matt Spinler16caaee2019-01-15 11:40:34 -0600403 // Methods could have been called across different owners
404 // and interfaces, where some calls failed and some passed.
405 //
406 // The rules for this are:
407 // * if no method was called - error
408 // * if a method failed and none passed - error
409 // (converse: if at least one method passed - OK)
410 // * for the method output:
411 // * if output processing didn't fail, return the data
412
413 // Only deal with method returns if nothing failed earlier
414 if (res.result() == boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700415 {
Matt Spinler16caaee2019-01-15 11:40:34 -0600416 if (!methodPassed)
417 {
418 if (methodFailed)
419 {
420 setErrorResponse(res,
421 boost::beast::http::status::bad_request,
422 "Method call failed", methodFailedMsg);
423 }
424 else
425 {
426 setErrorResponse(res, boost::beast::http::status::not_found,
427 methodNotFoundDesc, notFoundMsg);
428 }
429 }
430 else
431 {
432 if (outputFailed)
433 {
434 setErrorResponse(
435 res, boost::beast::http::status::internal_server_error,
436 "Method output failure", methodOutputFailedMsg);
437 }
438 else
439 {
440 res.jsonValue = {{"status", "ok"},
441 {"message", "200 OK"},
442 {"data", methodResponse}};
443 }
444 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700445 }
Matt Spinler16caaee2019-01-15 11:40:34 -0600446
Ed Tanous1abe55e2018-09-05 08:30:59 -0700447 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700448 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700449
Matt Spinler6db06242018-12-11 11:21:22 -0600450 void setErrorStatus(const std::string &desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700451 {
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -0600452 setErrorResponse(res, boost::beast::http::status::bad_request, desc,
453 badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700454 }
455 crow::Response &res;
456 std::string path;
457 std::string methodName;
Matt Spinlerde818812018-12-11 16:39:20 -0600458 std::string interfaceName;
Matt Spinler16caaee2019-01-15 11:40:34 -0600459 bool methodPassed = false;
460 bool methodFailed = false;
461 bool outputFailed = false;
Matt Spinler39a4e392019-01-15 11:53:13 -0600462 bool convertedToArray = false;
Matt Spinler16caaee2019-01-15 11:40:34 -0600463 nlohmann::json methodResponse;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700464 nlohmann::json arguments;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700465};
466
Ed Tanous1abe55e2018-09-05 08:30:59 -0700467std::vector<std::string> dbusArgSplit(const std::string &string)
468{
469 std::vector<std::string> ret;
470 if (string.empty())
471 {
472 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700473 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700474 ret.push_back("");
475 int containerDepth = 0;
476
477 for (std::string::const_iterator character = string.begin();
478 character != string.end(); character++)
479 {
480 ret.back() += *character;
481 switch (*character)
482 {
483 case ('a'):
484 break;
485 case ('('):
486 case ('{'):
487 containerDepth++;
488 break;
489 case ('}'):
490 case (')'):
491 containerDepth--;
492 if (containerDepth == 0)
493 {
494 if (character + 1 != string.end())
495 {
496 ret.push_back("");
497 }
498 }
499 break;
500 default:
501 if (containerDepth == 0)
502 {
503 if (character + 1 != string.end())
504 {
505 ret.push_back("");
506 }
507 }
508 break;
509 }
510 }
Matt Spinler4ae611d2019-01-11 15:37:06 -0600511
512 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700513}
514
Ed Tanousd76323e2018-08-07 14:35:40 -0700515int convertJsonToDbus(sd_bus_message *m, const std::string &arg_type,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700516 const nlohmann::json &input_json)
517{
518 int r = 0;
519 BMCWEB_LOG_DEBUG << "Converting " << input_json.dump()
520 << " to type: " << arg_type;
521 const std::vector<std::string> argTypes = dbusArgSplit(arg_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700522
Ed Tanous1abe55e2018-09-05 08:30:59 -0700523 // Assume a single object for now.
524 const nlohmann::json *j = &input_json;
525 nlohmann::json::const_iterator jIt = input_json.begin();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700526
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700527 for (const std::string &argCode : argTypes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700528 {
529 // If we are decoding multiple objects, grab the pointer to the
530 // iterator, and increment it for the next loop
531 if (argTypes.size() > 1)
532 {
533 if (jIt == input_json.end())
534 {
535 return -2;
536 }
537 j = &*jIt;
538 jIt++;
539 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700540 const int64_t *intValue = j->get_ptr<const int64_t *>();
541 const uint64_t *uintValue = j->get_ptr<const uint64_t *>();
542 const std::string *stringValue = j->get_ptr<const std::string *>();
543 const double *doubleValue = j->get_ptr<const double *>();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700544 const bool *b = j->get_ptr<const bool *>();
545 int64_t v = 0;
546 double d = 0.0;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700547
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 // Do some basic type conversions that make sense. uint can be
549 // converted to int. int and uint can be converted to double
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700550 if (uintValue != nullptr && intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700551 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700552 v = static_cast<int64_t>(*uintValue);
553 intValue = &v;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700554 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700555 if (uintValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700557 d = static_cast<double>(*uintValue);
558 doubleValue = &d;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700559 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700560 if (intValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700562 d = static_cast<double>(*intValue);
563 doubleValue = &d;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700564 }
565
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700566 if (argCode == "s")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700567 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700568 if (stringValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700569 {
570 return -1;
571 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000572 r = sd_bus_message_append_basic(m, argCode[0],
573 (void *)stringValue->c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700574 if (r < 0)
575 {
576 return r;
577 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700578 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700579 else if (argCode == "i")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700580 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700581 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700582 {
583 return -1;
584 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700585 int32_t i = static_cast<int32_t>(*intValue);
586 r = sd_bus_message_append_basic(m, argCode[0], &i);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700587 if (r < 0)
588 {
589 return r;
590 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700591 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700592 else if (argCode == "b")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700593 {
594 // lots of ways bool could be represented here. Try them all
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700595 int boolInt = false;
596 if (intValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700597 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700598 boolInt = *intValue > 0 ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700599 }
600 else if (b != nullptr)
601 {
Matt Spinlera2f02632019-01-18 10:15:35 -0600602 boolInt = *b ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700603 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700604 else if (stringValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700606 boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700607 }
608 else
609 {
610 return -1;
611 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700612 r = sd_bus_message_append_basic(m, argCode[0], &boolInt);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613 if (r < 0)
614 {
615 return r;
616 }
617 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700618 else if (argCode == "n")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700619 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700620 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 {
622 return -1;
623 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700624 int16_t n = static_cast<int16_t>(*intValue);
625 r = sd_bus_message_append_basic(m, argCode[0], &n);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700626 if (r < 0)
627 {
628 return r;
629 }
630 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700631 else if (argCode == "x")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700633 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634 {
635 return -1;
636 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700637 r = sd_bus_message_append_basic(m, argCode[0], intValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700638 if (r < 0)
639 {
640 return r;
641 }
642 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700643 else if (argCode == "y")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700645 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646 {
647 return -1;
648 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700649 uint8_t y = static_cast<uint8_t>(*uintValue);
650 r = sd_bus_message_append_basic(m, argCode[0], &y);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700651 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700652 else if (argCode == "q")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700653 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700654 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 {
656 return -1;
657 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700658 uint16_t q = static_cast<uint16_t>(*uintValue);
659 r = sd_bus_message_append_basic(m, argCode[0], &q);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700660 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700661 else if (argCode == "u")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700663 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 {
665 return -1;
666 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700667 uint32_t u = static_cast<uint32_t>(*uintValue);
668 r = sd_bus_message_append_basic(m, argCode[0], &u);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700669 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700670 else if (argCode == "t")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700671 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700672 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700673 {
674 return -1;
675 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700676 r = sd_bus_message_append_basic(m, argCode[0], uintValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700677 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700678 else if (argCode == "d")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700680 sd_bus_message_append_basic(m, argCode[0], doubleValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700682 else if (boost::starts_with(argCode, "a"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700684 std::string containedType = argCode.substr(1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700686 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700687 if (r < 0)
688 {
689 return r;
690 }
691
692 for (nlohmann::json::const_iterator it = j->begin(); it != j->end();
693 ++it)
694 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700695 r = convertJsonToDbus(m, containedType, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700696 if (r < 0)
697 {
698 return r;
699 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700700 }
701 sd_bus_message_close_container(m);
702 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700703 else if (boost::starts_with(argCode, "v"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700704 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700705 std::string containedType = argCode.substr(1);
706 BMCWEB_LOG_DEBUG << "variant type: " << argCode
707 << " appending variant of type: " << containedType;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700708 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700709 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700710 if (r < 0)
711 {
712 return r;
713 }
714
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700715 r = convertJsonToDbus(m, containedType, input_json);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700716 if (r < 0)
717 {
718 return r;
719 }
720
721 r = sd_bus_message_close_container(m);
722 if (r < 0)
723 {
724 return r;
725 }
726 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700727 else if (boost::starts_with(argCode, "(") &&
728 boost::ends_with(argCode, ")"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700729 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700730 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700731 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700732 containedType.c_str());
Ed Tanousf1eebf02019-03-04 15:57:09 -0800733 if (r < 0)
734 {
735 return r;
736 }
737
Ed Tanous1abe55e2018-09-05 08:30:59 -0700738 nlohmann::json::const_iterator it = j->begin();
Ed Tanousb01bf292019-03-25 19:25:26 +0000739 for (const std::string &argCode : dbusArgSplit(arg_type))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700740 {
741 if (it == j->end())
742 {
743 return -1;
744 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000745 r = convertJsonToDbus(m, argCode, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700746 if (r < 0)
747 {
748 return r;
749 }
750 it++;
751 }
752 r = sd_bus_message_close_container(m);
753 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700754 else if (boost::starts_with(argCode, "{") &&
755 boost::ends_with(argCode, "}"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700756 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700757 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700758 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700759 containedType.c_str());
Ed Tanousf1eebf02019-03-04 15:57:09 -0800760 if (r < 0)
761 {
762 return r;
763 }
764
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700765 std::vector<std::string> codes = dbusArgSplit(containedType);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700766 if (codes.size() != 2)
767 {
768 return -1;
769 }
770 const std::string &key_type = codes[0];
771 const std::string &value_type = codes[1];
772 for (auto it : j->items())
773 {
774 r = convertJsonToDbus(m, key_type, it.key());
775 if (r < 0)
776 {
777 return r;
778 }
779
780 r = convertJsonToDbus(m, value_type, it.value());
781 if (r < 0)
782 {
783 return r;
784 }
785 }
786 r = sd_bus_message_close_container(m);
787 }
788 else
789 {
790 return -2;
791 }
792 if (r < 0)
793 {
794 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700795 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700796
Ed Tanous1abe55e2018-09-05 08:30:59 -0700797 if (argTypes.size() > 1)
798 {
799 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700800 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700801 }
Matt Spinler127ea542019-01-14 11:04:28 -0600802
803 return r;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700804}
805
Matt Spinlerd22a7132019-01-14 12:14:30 -0600806template <typename T>
807int readMessageItem(const std::string &typeCode, sdbusplus::message::message &m,
808 nlohmann::json &data)
809{
810 T value;
811
812 int r = sd_bus_message_read_basic(m.get(), typeCode.front(), &value);
813 if (r < 0)
814 {
815 BMCWEB_LOG_ERROR << "sd_bus_message_read_basic on type " << typeCode
816 << " failed!";
817 return r;
818 }
819
820 data = value;
821 return 0;
822}
823
Matt Spinler16caaee2019-01-15 11:40:34 -0600824int convertDBusToJSON(const std::string &returnType,
Matt Spinler6df8f992019-01-14 12:47:47 -0600825 sdbusplus::message::message &m, nlohmann::json &response);
826
827int readDictEntryFromMessage(const std::string &typeCode,
828 sdbusplus::message::message &m,
829 nlohmann::json &object)
830{
831 std::vector<std::string> types = dbusArgSplit(typeCode);
832 if (types.size() != 2)
833 {
834 BMCWEB_LOG_ERROR << "wrong number contained types in dictionary: "
835 << types.size();
836 return -1;
837 }
838
839 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_DICT_ENTRY,
840 typeCode.c_str());
841 if (r < 0)
842 {
843 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container with rc " << r;
844 return r;
845 }
846
847 nlohmann::json key;
848 r = convertDBusToJSON(types[0], m, key);
849 if (r < 0)
850 {
851 return r;
852 }
853
854 const std::string *keyPtr = key.get_ptr<const std::string *>();
855 if (keyPtr == nullptr)
856 {
857 // json doesn't support non-string keys. If we hit this condition,
858 // convert the result to a string so we can proceed
859 key = key.dump();
860 keyPtr = key.get_ptr<const std::string *>();
Ed Tanous7c091622019-05-23 11:42:36 -0700861 // in theory this can't fail now, but lets be paranoid about it
862 // anyway
Matt Spinler6df8f992019-01-14 12:47:47 -0600863 if (keyPtr == nullptr)
864 {
865 return -1;
866 }
867 }
868 nlohmann::json &value = object[*keyPtr];
869
870 r = convertDBusToJSON(types[1], m, value);
871 if (r < 0)
872 {
873 return r;
874 }
875
876 r = sd_bus_message_exit_container(m.get());
877 if (r < 0)
878 {
879 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
880 return r;
881 }
882
883 return 0;
884}
885
886int readArrayFromMessage(const std::string &typeCode,
887 sdbusplus::message::message &m, nlohmann::json &data)
888{
889 if (typeCode.size() < 2)
890 {
891 BMCWEB_LOG_ERROR << "Type code " << typeCode
892 << " too small for an array";
893 return -1;
894 }
895
896 std::string containedType = typeCode.substr(1);
897
898 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_ARRAY,
899 containedType.c_str());
900 if (r < 0)
901 {
902 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
903 << r;
904 return r;
905 }
906
907 bool dict = boost::starts_with(containedType, "{") &&
908 boost::ends_with(containedType, "}");
909
910 if (dict)
911 {
912 // Remove the { }
913 containedType = containedType.substr(1, containedType.size() - 2);
914 data = nlohmann::json::object();
915 }
916 else
917 {
918 data = nlohmann::json::array();
919 }
920
921 while (true)
922 {
923 r = sd_bus_message_at_end(m.get(), false);
924 if (r < 0)
925 {
926 BMCWEB_LOG_ERROR << "sd_bus_message_at_end failed";
927 return r;
928 }
929
930 if (r > 0)
931 {
932 break;
933 }
934
935 // Dictionaries are only ever seen in an array
936 if (dict)
937 {
938 r = readDictEntryFromMessage(containedType, m, data);
939 if (r < 0)
940 {
941 return r;
942 }
943 }
944 else
945 {
946 data.push_back(nlohmann::json());
947
948 r = convertDBusToJSON(containedType, m, data.back());
949 if (r < 0)
950 {
951 return r;
952 }
953 }
954 }
955
956 r = sd_bus_message_exit_container(m.get());
957 if (r < 0)
958 {
959 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
960 return r;
961 }
962
963 return 0;
964}
965
Matt Spinler75c6c672019-01-14 13:01:46 -0600966int readStructFromMessage(const std::string &typeCode,
967 sdbusplus::message::message &m, nlohmann::json &data)
968{
969 if (typeCode.size() < 3)
970 {
971 BMCWEB_LOG_ERROR << "Type code " << typeCode
972 << " too small for a struct";
973 return -1;
974 }
975
976 std::string containedTypes = typeCode.substr(1, typeCode.size() - 2);
977 std::vector<std::string> types = dbusArgSplit(containedTypes);
978
979 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_STRUCT,
980 containedTypes.c_str());
981 if (r < 0)
982 {
983 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
984 << r;
985 return r;
986 }
987
988 for (const std::string &type : types)
989 {
990 data.push_back(nlohmann::json());
991 r = convertDBusToJSON(type, m, data.back());
992 if (r < 0)
993 {
994 return r;
995 }
996 }
997
998 r = sd_bus_message_exit_container(m.get());
999 if (r < 0)
1000 {
1001 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
1002 return r;
1003 }
1004 return 0;
1005}
1006
Matt Spinler89c19702019-01-14 13:13:00 -06001007int readVariantFromMessage(sdbusplus::message::message &m, nlohmann::json &data)
1008{
1009 const char *containerType;
1010 int r = sd_bus_message_peek_type(m.get(), NULL, &containerType);
1011 if (r < 0)
1012 {
1013 BMCWEB_LOG_ERROR << "sd_bus_message_peek_type failed";
1014 return r;
1015 }
1016
1017 r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_VARIANT,
1018 containerType);
1019 if (r < 0)
1020 {
1021 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
1022 << r;
1023 return r;
1024 }
1025
1026 r = convertDBusToJSON(containerType, m, data);
1027 if (r < 0)
1028 {
1029 return r;
1030 }
1031
1032 r = sd_bus_message_exit_container(m.get());
1033 if (r < 0)
1034 {
1035 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed";
1036 return r;
1037 }
1038
1039 return 0;
1040}
1041
Matt Spinler6df8f992019-01-14 12:47:47 -06001042int convertDBusToJSON(const std::string &returnType,
Matt Spinler16caaee2019-01-15 11:40:34 -06001043 sdbusplus::message::message &m, nlohmann::json &response)
1044{
Matt Spinlerd22a7132019-01-14 12:14:30 -06001045 int r = 0;
1046 const std::vector<std::string> returnTypes = dbusArgSplit(returnType);
1047
Matt Spinlerd22a7132019-01-14 12:14:30 -06001048 for (const std::string &typeCode : returnTypes)
1049 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001050 nlohmann::json *thisElement = &response;
1051 if (returnTypes.size() > 1)
Matt Spinlerd22a7132019-01-14 12:14:30 -06001052 {
1053 response.push_back(nlohmann::json{});
Matt Spinlerf39420c2019-01-30 12:57:18 -06001054 thisElement = &response.back();
Matt Spinlerd22a7132019-01-14 12:14:30 -06001055 }
1056
1057 if (typeCode == "s")
1058 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001059 r = readMessageItem<char *>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001060 if (r < 0)
1061 {
1062 return r;
1063 }
1064 }
1065 else if (typeCode == "g")
1066 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001067 r = readMessageItem<char *>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001068 if (r < 0)
1069 {
1070 return r;
1071 }
1072 }
1073 else if (typeCode == "o")
1074 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001075 r = readMessageItem<char *>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001076 if (r < 0)
1077 {
1078 return r;
1079 }
1080 }
1081 else if (typeCode == "b")
1082 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001083 r = readMessageItem<int>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001084 if (r < 0)
1085 {
1086 return r;
1087 }
1088
Matt Spinlerf39420c2019-01-30 12:57:18 -06001089 *thisElement = static_cast<bool>(thisElement->get<int>());
Matt Spinlerd22a7132019-01-14 12:14:30 -06001090 }
1091 else if (typeCode == "u")
1092 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001093 r = readMessageItem<uint32_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001094 if (r < 0)
1095 {
1096 return r;
1097 }
1098 }
1099 else if (typeCode == "i")
1100 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001101 r = readMessageItem<int32_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001102 if (r < 0)
1103 {
1104 return r;
1105 }
1106 }
1107 else if (typeCode == "x")
1108 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001109 r = readMessageItem<int64_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001110 if (r < 0)
1111 {
1112 return r;
1113 }
1114 }
1115 else if (typeCode == "t")
1116 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001117 r = readMessageItem<uint64_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001118 if (r < 0)
1119 {
1120 return r;
1121 }
1122 }
1123 else if (typeCode == "n")
1124 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001125 r = readMessageItem<int16_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001126 if (r < 0)
1127 {
1128 return r;
1129 }
1130 }
1131 else if (typeCode == "q")
1132 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001133 r = readMessageItem<uint16_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001134 if (r < 0)
1135 {
1136 return r;
1137 }
1138 }
1139 else if (typeCode == "y")
1140 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001141 r = readMessageItem<uint8_t>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001142 if (r < 0)
1143 {
1144 return r;
1145 }
1146 }
1147 else if (typeCode == "d")
1148 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001149 r = readMessageItem<double>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001150 if (r < 0)
1151 {
1152 return r;
1153 }
1154 }
1155 else if (typeCode == "h")
1156 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001157 r = readMessageItem<int>(typeCode, m, *thisElement);
Matt Spinlerd22a7132019-01-14 12:14:30 -06001158 if (r < 0)
1159 {
1160 return r;
1161 }
1162 }
Matt Spinler6df8f992019-01-14 12:47:47 -06001163 else if (boost::starts_with(typeCode, "a"))
1164 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001165 r = readArrayFromMessage(typeCode, m, *thisElement);
Matt Spinler6df8f992019-01-14 12:47:47 -06001166 if (r < 0)
1167 {
1168 return r;
1169 }
1170 }
Matt Spinler75c6c672019-01-14 13:01:46 -06001171 else if (boost::starts_with(typeCode, "(") &&
1172 boost::ends_with(typeCode, ")"))
1173 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001174 r = readStructFromMessage(typeCode, m, *thisElement);
Matt Spinler75c6c672019-01-14 13:01:46 -06001175 if (r < 0)
1176 {
1177 return r;
1178 }
1179 }
Matt Spinler89c19702019-01-14 13:13:00 -06001180 else if (boost::starts_with(typeCode, "v"))
1181 {
Matt Spinlerf39420c2019-01-30 12:57:18 -06001182 r = readVariantFromMessage(m, *thisElement);
Matt Spinler89c19702019-01-14 13:13:00 -06001183 if (r < 0)
1184 {
1185 return r;
1186 }
1187 }
Matt Spinlerd22a7132019-01-14 12:14:30 -06001188 else
1189 {
Matt Spinlerd22a7132019-01-14 12:14:30 -06001190 BMCWEB_LOG_ERROR << "Invalid D-Bus signature type " << typeCode;
1191 return -2;
1192 }
1193 }
1194
Matt Spinler16caaee2019-01-15 11:40:34 -06001195 return 0;
1196}
1197
1198void handleMethodResponse(std::shared_ptr<InProgressActionData> transaction,
1199 sdbusplus::message::message &m,
1200 const std::string &returnType)
1201{
Matt Spinler39a4e392019-01-15 11:53:13 -06001202 nlohmann::json data;
1203
1204 int r = convertDBusToJSON(returnType, m, data);
1205 if (r < 0)
1206 {
1207 transaction->outputFailed = true;
1208 return;
1209 }
1210
1211 if (data.is_null())
1212 {
1213 return;
1214 }
1215
1216 if (transaction->methodResponse.is_null())
1217 {
1218 transaction->methodResponse = std::move(data);
1219 return;
1220 }
1221
1222 // If they're both dictionaries or arrays, merge into one.
1223 // Otherwise, make the results an array with every result
1224 // an entry. Could also just fail in that case, but it
1225 // seems better to get the data back somehow.
1226
1227 if (transaction->methodResponse.is_object() && data.is_object())
1228 {
1229 for (const auto &obj : data.items())
1230 {
1231 // Note: Will overwrite the data for a duplicate key
1232 transaction->methodResponse.emplace(obj.key(),
1233 std::move(obj.value()));
1234 }
1235 return;
1236 }
1237
1238 if (transaction->methodResponse.is_array() && data.is_array())
1239 {
1240 for (auto &obj : data)
1241 {
1242 transaction->methodResponse.push_back(std::move(obj));
1243 }
1244 return;
1245 }
1246
1247 if (!transaction->convertedToArray)
1248 {
1249 // They are different types. May as well turn them into an array
1250 nlohmann::json j = std::move(transaction->methodResponse);
1251 transaction->methodResponse = nlohmann::json::array();
1252 transaction->methodResponse.push_back(std::move(j));
1253 transaction->methodResponse.push_back(std::move(data));
1254 transaction->convertedToArray = true;
1255 }
1256 else
1257 {
1258 transaction->methodResponse.push_back(std::move(data));
1259 }
Matt Spinler16caaee2019-01-15 11:40:34 -06001260}
1261
Ed Tanousd76323e2018-08-07 14:35:40 -07001262void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001263 const std::string &connectionName)
1264{
1265 BMCWEB_LOG_DEBUG << "findActionOnInterface for connection "
1266 << connectionName;
1267 crow::connections::systemBus->async_method_call(
1268 [transaction, connectionName{std::string(connectionName)}](
1269 const boost::system::error_code ec,
1270 const std::string &introspect_xml) {
1271 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
1272 if (ec)
1273 {
1274 BMCWEB_LOG_ERROR
1275 << "Introspect call failed with error: " << ec.message()
1276 << " on process: " << connectionName << "\n";
Matt Spinler318bd892019-01-15 09:59:20 -06001277 return;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001278 }
Matt Spinler318bd892019-01-15 09:59:20 -06001279 tinyxml2::XMLDocument doc;
1280
1281 doc.Parse(introspect_xml.data(), introspect_xml.size());
1282 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1283 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001284 {
Matt Spinler318bd892019-01-15 09:59:20 -06001285 BMCWEB_LOG_ERROR << "XML document failed to parse "
1286 << connectionName << "\n";
1287 return;
1288 }
1289 tinyxml2::XMLElement *interfaceNode =
1290 pRoot->FirstChildElement("interface");
1291 while (interfaceNode != nullptr)
1292 {
1293 const char *thisInterfaceName =
1294 interfaceNode->Attribute("name");
1295 if (thisInterfaceName != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001296 {
Matt Spinler318bd892019-01-15 09:59:20 -06001297 if (!transaction->interfaceName.empty() &&
1298 (transaction->interfaceName != thisInterfaceName))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001299 {
Matt Spinler318bd892019-01-15 09:59:20 -06001300 interfaceNode =
1301 interfaceNode->NextSiblingElement("interface");
1302 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001303 }
Matt Spinler318bd892019-01-15 09:59:20 -06001304
1305 tinyxml2::XMLElement *methodNode =
1306 interfaceNode->FirstChildElement("method");
1307 while (methodNode != nullptr)
1308 {
1309 const char *thisMethodName =
1310 methodNode->Attribute("name");
1311 BMCWEB_LOG_DEBUG << "Found method: " << thisMethodName;
1312 if (thisMethodName != nullptr &&
1313 thisMethodName == transaction->methodName)
1314 {
1315 BMCWEB_LOG_DEBUG
1316 << "Found method named " << thisMethodName
1317 << " on interface " << thisInterfaceName;
1318 sdbusplus::message::message m =
1319 crow::connections::systemBus->new_method_call(
1320 connectionName.c_str(),
1321 transaction->path.c_str(),
1322 thisInterfaceName,
1323 transaction->methodName.c_str());
1324
1325 tinyxml2::XMLElement *argumentNode =
1326 methodNode->FirstChildElement("arg");
1327
Matt Spinler16caaee2019-01-15 11:40:34 -06001328 std::string returnType;
1329
1330 // Find the output type
1331 while (argumentNode != nullptr)
1332 {
1333 const char *argDirection =
1334 argumentNode->Attribute("direction");
1335 const char *argType =
1336 argumentNode->Attribute("type");
1337 if (argDirection != nullptr &&
1338 argType != nullptr &&
1339 std::string(argDirection) == "out")
1340 {
1341 returnType = argType;
1342 break;
1343 }
1344 argumentNode =
1345 argumentNode->NextSiblingElement("arg");
1346 }
1347
Matt Spinler318bd892019-01-15 09:59:20 -06001348 nlohmann::json::const_iterator argIt =
1349 transaction->arguments.begin();
1350
Matt Spinler16caaee2019-01-15 11:40:34 -06001351 argumentNode = methodNode->FirstChildElement("arg");
1352
Matt Spinler318bd892019-01-15 09:59:20 -06001353 while (argumentNode != nullptr)
1354 {
1355 const char *argDirection =
1356 argumentNode->Attribute("direction");
1357 const char *argType =
1358 argumentNode->Attribute("type");
1359 if (argDirection != nullptr &&
1360 argType != nullptr &&
1361 std::string(argDirection) == "in")
1362 {
1363 if (argIt == transaction->arguments.end())
1364 {
1365 transaction->setErrorStatus(
1366 "Invalid method args");
1367 return;
1368 }
1369 if (convertJsonToDbus(m.get(),
1370 std::string(argType),
1371 *argIt) < 0)
1372 {
1373 transaction->setErrorStatus(
1374 "Invalid method arg type");
1375 return;
1376 }
1377
1378 argIt++;
1379 }
1380 argumentNode =
1381 argumentNode->NextSiblingElement("arg");
1382 }
1383
1384 crow::connections::systemBus->async_send(
Matt Spinler16caaee2019-01-15 11:40:34 -06001385 m, [transaction, returnType](
1386 boost::system::error_code ec,
1387 sdbusplus::message::message &m) {
Matt Spinler318bd892019-01-15 09:59:20 -06001388 if (ec)
1389 {
Matt Spinler16caaee2019-01-15 11:40:34 -06001390 transaction->methodFailed = true;
Matt Spinler318bd892019-01-15 09:59:20 -06001391 return;
1392 }
Matt Spinler16caaee2019-01-15 11:40:34 -06001393 else
1394 {
1395 transaction->methodPassed = true;
1396 }
1397
1398 handleMethodResponse(transaction, m,
1399 returnType);
Matt Spinler318bd892019-01-15 09:59:20 -06001400 });
1401 break;
1402 }
1403 methodNode = methodNode->NextSiblingElement("method");
1404 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001405 }
Matt Spinler318bd892019-01-15 09:59:20 -06001406 interfaceNode = interfaceNode->NextSiblingElement("interface");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001407 }
1408 },
1409 connectionName, transaction->path,
1410 "org.freedesktop.DBus.Introspectable", "Introspect");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001411}
1412
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001413void handleAction(const crow::Request &req, crow::Response &res,
1414 const std::string &objectPath, const std::string &methodName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001415{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001416 BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method "
1417 << methodName;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001418 nlohmann::json requestDbusData =
1419 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001420
Ed Tanous1abe55e2018-09-05 08:30:59 -07001421 if (requestDbusData.is_discarded())
1422 {
Matt Spinler6db06242018-12-11 11:21:22 -06001423 setErrorResponse(res, boost::beast::http::status::bad_request,
1424 noJsonDesc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001425 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001426 return;
1427 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001428 nlohmann::json::iterator data = requestDbusData.find("data");
1429 if (data == requestDbusData.end())
1430 {
Matt Spinler6db06242018-12-11 11:21:22 -06001431 setErrorResponse(res, boost::beast::http::status::bad_request,
1432 noJsonDesc, badReqMsg);
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001433 res.end();
1434 return;
1435 }
1436
1437 if (!data->is_array())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001438 {
Matt Spinler6db06242018-12-11 11:21:22 -06001439 setErrorResponse(res, boost::beast::http::status::bad_request,
1440 noJsonDesc, badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001441 res.end();
1442 return;
1443 }
1444 auto transaction = std::make_shared<InProgressActionData>(res);
1445
1446 transaction->path = objectPath;
1447 transaction->methodName = methodName;
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001448 transaction->arguments = std::move(*data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001449 crow::connections::systemBus->async_method_call(
1450 [transaction](
1451 const boost::system::error_code ec,
1452 const std::vector<std::pair<std::string, std::vector<std::string>>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001453 &interfaceNames) {
1454 if (ec || interfaceNames.size() <= 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001455 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001456 BMCWEB_LOG_ERROR << "Can't find object";
Matt Spinler6db06242018-12-11 11:21:22 -06001457 setErrorResponse(transaction->res,
1458 boost::beast::http::status::not_found,
1459 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001460 return;
1461 }
1462
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001463 BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size()
1464 << " object(s)";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001465
1466 for (const std::pair<std::string, std::vector<std::string>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001467 &object : interfaceNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001468 {
1469 findActionOnInterface(transaction, object.first);
1470 }
1471 },
1472 "xyz.openbmc_project.ObjectMapper",
1473 "/xyz/openbmc_project/object_mapper",
1474 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
1475 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001476}
1477
Matt Spinlerde818812018-12-11 16:39:20 -06001478void handleDelete(const crow::Request &req, crow::Response &res,
1479 const std::string &objectPath)
1480{
1481 BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath;
1482
1483 crow::connections::systemBus->async_method_call(
1484 [&res, objectPath](
1485 const boost::system::error_code ec,
1486 const std::vector<std::pair<std::string, std::vector<std::string>>>
1487 &interfaceNames) {
1488 if (ec || interfaceNames.size() <= 0)
1489 {
1490 BMCWEB_LOG_ERROR << "Can't find object";
Matt Spinler62d2e8b2019-01-22 13:45:51 -06001491 setErrorResponse(res,
1492 boost::beast::http::status::method_not_allowed,
1493 methodNotAllowedDesc, methodNotAllowedMsg);
Matt Spinlerde818812018-12-11 16:39:20 -06001494 res.end();
1495 return;
1496 }
1497
1498 auto transaction = std::make_shared<InProgressActionData>(res);
1499 transaction->path = objectPath;
1500 transaction->methodName = "Delete";
1501 transaction->interfaceName = "xyz.openbmc_project.Object.Delete";
1502
1503 for (const std::pair<std::string, std::vector<std::string>>
1504 &object : interfaceNames)
1505 {
1506 findActionOnInterface(transaction, object.first);
1507 }
1508 },
1509 "xyz.openbmc_project.ObjectMapper",
1510 "/xyz/openbmc_project/object_mapper",
1511 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
1512 std::array<const char *, 0>());
1513}
1514
Ed Tanousf839dfe2018-11-12 11:11:15 -08001515void handleList(crow::Response &res, const std::string &objectPath,
1516 int32_t depth = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001517{
1518 crow::connections::systemBus->async_method_call(
1519 [&res](const boost::system::error_code ec,
1520 std::vector<std::string> &objectPaths) {
1521 if (ec)
1522 {
Matt Spinlerd6091dd2018-12-06 14:08:27 -06001523 setErrorResponse(res, boost::beast::http::status::not_found,
1524 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001525 }
1526 else
1527 {
1528 res.jsonValue = {{"status", "ok"},
1529 {"message", "200 OK"},
1530 {"data", std::move(objectPaths)}};
1531 }
1532 res.end();
1533 },
1534 "xyz.openbmc_project.ObjectMapper",
1535 "/xyz/openbmc_project/object_mapper",
1536 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousf839dfe2018-11-12 11:11:15 -08001537 depth, std::array<std::string, 0>());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001538}
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001539
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001540void handleEnumerate(crow::Response &res, const std::string &objectPath)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001541{
Ed Tanous049a0512018-11-01 13:58:42 -07001542 BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath;
1543 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
1544
1545 asyncResp->res.jsonValue = {{"message", "200 OK"},
1546 {"status", "ok"},
1547 {"data", nlohmann::json::object()}};
1548
Ed Tanous1abe55e2018-09-05 08:30:59 -07001549 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001550 [objectPath, asyncResp](const boost::system::error_code ec,
1551 GetSubTreeType &object_names) {
1552 auto transaction = std::make_shared<InProgressEnumerateData>(
1553 objectPath, asyncResp);
1554
1555 transaction->subtree =
1556 std::make_shared<GetSubTreeType>(std::move(object_names));
1557
Ed Tanous1abe55e2018-09-05 08:30:59 -07001558 if (ec)
1559 {
Matt Spinler2ae60092018-12-06 10:35:36 -06001560 BMCWEB_LOG_ERROR << "GetSubTree failed on "
1561 << transaction->objectPath;
1562 setErrorResponse(transaction->asyncResp->res,
1563 boost::beast::http::status::not_found,
1564 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001565 return;
1566 }
Ed Tanous64530012018-02-06 17:08:16 -08001567
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001568 // Add the data for the path passed in to the results
1569 // as if GetSubTree returned it, and continue on enumerating
1570 getObjectAndEnumerate(transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001571 },
1572 "xyz.openbmc_project.ObjectMapper",
1573 "/xyz/openbmc_project/object_mapper",
1574 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath,
Ed Tanous049a0512018-11-01 13:58:42 -07001575 static_cast<int32_t>(0), std::array<const char *, 0>());
Ed Tanous64530012018-02-06 17:08:16 -08001576}
Ed Tanous911ac312017-08-15 09:37:42 -07001577
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001578void handleGet(crow::Response &res, std::string &objectPath,
1579 std::string &destProperty)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001580{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001581 BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty;
1582 std::shared_ptr<std::string> propertyName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001583 std::make_shared<std::string>(std::move(destProperty));
Ed Tanous75db20e2018-07-27 13:44:44 -07001584
Ed Tanous1abe55e2018-09-05 08:30:59 -07001585 std::shared_ptr<std::string> path =
1586 std::make_shared<std::string>(std::move(objectPath));
Ed Tanous75db20e2018-07-27 13:44:44 -07001587
Ed Tanous1abe55e2018-09-05 08:30:59 -07001588 using GetObjectType =
1589 std::vector<std::pair<std::string, std::vector<std::string>>>;
1590 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001591 [&res, path, propertyName](const boost::system::error_code ec,
1592 const GetObjectType &object_names) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001593 if (ec || object_names.size() <= 0)
1594 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001595 setErrorResponse(res, boost::beast::http::status::not_found,
1596 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001597 res.end();
1598 return;
1599 }
1600 std::shared_ptr<nlohmann::json> response =
1601 std::make_shared<nlohmann::json>(nlohmann::json::object());
Ed Tanous7c091622019-05-23 11:42:36 -07001602 // The mapper should never give us an empty interface names
1603 // list, but check anyway
Ed Tanous1abe55e2018-09-05 08:30:59 -07001604 for (const std::pair<std::string, std::vector<std::string>>
1605 connection : object_names)
1606 {
1607 const std::vector<std::string> &interfaceNames =
1608 connection.second;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001609
Ed Tanous1abe55e2018-09-05 08:30:59 -07001610 if (interfaceNames.size() <= 0)
1611 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001612 setErrorResponse(res, boost::beast::http::status::not_found,
1613 notFoundDesc, notFoundMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001614 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001615 return;
1616 }
1617
1618 for (const std::string &interface : interfaceNames)
1619 {
Matt Spinlerfe7e97d2019-01-30 15:33:21 -06001620 sdbusplus::message::message m =
1621 crow::connections::systemBus->new_method_call(
1622 connection.first.c_str(), path->c_str(),
1623 "org.freedesktop.DBus.Properties", "GetAll");
1624 m.append(interface);
1625 crow::connections::systemBus->async_send(
1626 m, [&res, response,
1627 propertyName](const boost::system::error_code ec,
1628 sdbusplus::message::message &msg) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001629 if (ec)
1630 {
1631 BMCWEB_LOG_ERROR << "Bad dbus request error: "
1632 << ec;
1633 }
1634 else
1635 {
Matt Spinlerfe7e97d2019-01-30 15:33:21 -06001636 nlohmann::json properties;
1637 int r =
1638 convertDBusToJSON("a{sv}", msg, properties);
1639 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001640 {
Matt Spinlerfe7e97d2019-01-30 15:33:21 -06001641 BMCWEB_LOG_ERROR
1642 << "convertDBusToJSON failed";
1643 }
1644 else
1645 {
1646 for (auto &prop : properties.items())
1647 {
Ed Tanous7c091622019-05-23 11:42:36 -07001648 // if property name is empty, or
1649 // matches our search query, add it
1650 // to the response json
Ed Tanous1abe55e2018-09-05 08:30:59 -07001651
Matt Spinlerfe7e97d2019-01-30 15:33:21 -06001652 if (propertyName->empty())
1653 {
1654 (*response)[prop.key()] =
1655 std::move(prop.value());
1656 }
1657 else if (prop.key() == *propertyName)
1658 {
1659 *response = std::move(prop.value());
1660 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001661 }
1662 }
1663 }
1664 if (response.use_count() == 1)
1665 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001666 if (!propertyName->empty() && response->empty())
1667 {
1668 setErrorResponse(
1669 res,
1670 boost::beast::http::status::not_found,
1671 propNotFoundDesc, notFoundMsg);
1672 }
1673 else
1674 {
1675 res.jsonValue = {{"status", "ok"},
1676 {"message", "200 OK"},
1677 {"data", *response}};
1678 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001679 res.end();
1680 }
Matt Spinlerfe7e97d2019-01-30 15:33:21 -06001681 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001682 }
1683 }
1684 },
1685 "xyz.openbmc_project.ObjectMapper",
1686 "/xyz/openbmc_project/object_mapper",
1687 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
1688 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001689}
1690
Ed Tanous1abe55e2018-09-05 08:30:59 -07001691struct AsyncPutRequest
1692{
1693 AsyncPutRequest(crow::Response &res) : res(res)
1694 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001695 }
1696 ~AsyncPutRequest()
1697 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001698 if (res.jsonValue.empty())
1699 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001700 setErrorResponse(res, boost::beast::http::status::forbidden,
1701 forbiddenMsg, forbiddenPropDesc);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001702 }
1703
1704 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001705 }
1706
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001707 void setErrorStatus(const std::string &desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001708 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001709 setErrorResponse(res, boost::beast::http::status::internal_server_error,
1710 desc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001711 }
1712
Ed Tanous1abe55e2018-09-05 08:30:59 -07001713 crow::Response &res;
1714 std::string objectPath;
1715 std::string propertyName;
1716 nlohmann::json propertyValue;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001717};
1718
Ed Tanousd76323e2018-08-07 14:35:40 -07001719void handlePut(const crow::Request &req, crow::Response &res,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001720 const std::string &objectPath, const std::string &destProperty)
1721{
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001722 if (destProperty.empty())
1723 {
1724 setErrorResponse(res, boost::beast::http::status::forbidden,
1725 forbiddenResDesc, forbiddenMsg);
1726 res.end();
1727 return;
1728 }
1729
Ed Tanous1abe55e2018-09-05 08:30:59 -07001730 nlohmann::json requestDbusData =
1731 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001732
Ed Tanous1abe55e2018-09-05 08:30:59 -07001733 if (requestDbusData.is_discarded())
1734 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001735 setErrorResponse(res, boost::beast::http::status::bad_request,
1736 noJsonDesc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001737 res.end();
1738 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001739 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001740
Ed Tanous1abe55e2018-09-05 08:30:59 -07001741 nlohmann::json::const_iterator propertyIt = requestDbusData.find("data");
1742 if (propertyIt == requestDbusData.end())
1743 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001744 setErrorResponse(res, boost::beast::http::status::bad_request,
1745 noJsonDesc, badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001746 res.end();
1747 return;
1748 }
1749 const nlohmann::json &propertySetValue = *propertyIt;
1750 auto transaction = std::make_shared<AsyncPutRequest>(res);
1751 transaction->objectPath = objectPath;
1752 transaction->propertyName = destProperty;
1753 transaction->propertyValue = propertySetValue;
Ed Tanous911ac312017-08-15 09:37:42 -07001754
Ed Tanous1abe55e2018-09-05 08:30:59 -07001755 using GetObjectType =
1756 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous911ac312017-08-15 09:37:42 -07001757
Ed Tanous1abe55e2018-09-05 08:30:59 -07001758 crow::connections::systemBus->async_method_call(
1759 [transaction](const boost::system::error_code ec,
1760 const GetObjectType &object_names) {
1761 if (!ec && object_names.size() <= 0)
1762 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001763 setErrorResponse(transaction->res,
1764 boost::beast::http::status::not_found,
1765 propNotFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001766 return;
1767 }
Ed Tanous911ac312017-08-15 09:37:42 -07001768
Ed Tanous1abe55e2018-09-05 08:30:59 -07001769 for (const std::pair<std::string, std::vector<std::string>>
1770 connection : object_names)
1771 {
1772 const std::string &connectionName = connection.first;
Ed Tanous911ac312017-08-15 09:37:42 -07001773
Ed Tanous1abe55e2018-09-05 08:30:59 -07001774 crow::connections::systemBus->async_method_call(
1775 [connectionName{std::string(connectionName)},
1776 transaction](const boost::system::error_code ec,
1777 const std::string &introspectXml) {
1778 if (ec)
1779 {
1780 BMCWEB_LOG_ERROR
1781 << "Introspect call failed with error: "
1782 << ec.message()
1783 << " on process: " << connectionName;
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001784 transaction->setErrorStatus("Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001785 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001786 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001787 tinyxml2::XMLDocument doc;
Ed Tanous911ac312017-08-15 09:37:42 -07001788
Ed Tanous1abe55e2018-09-05 08:30:59 -07001789 doc.Parse(introspectXml.c_str());
1790 tinyxml2::XMLNode *pRoot =
1791 doc.FirstChildElement("node");
1792 if (pRoot == nullptr)
1793 {
1794 BMCWEB_LOG_ERROR << "XML document failed to parse: "
1795 << introspectXml;
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001796 transaction->setErrorStatus("Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001797 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001798 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001799 tinyxml2::XMLElement *ifaceNode =
1800 pRoot->FirstChildElement("interface");
1801 while (ifaceNode != nullptr)
1802 {
1803 const char *interfaceName =
1804 ifaceNode->Attribute("name");
1805 BMCWEB_LOG_DEBUG << "found interface "
1806 << interfaceName;
1807 tinyxml2::XMLElement *propNode =
1808 ifaceNode->FirstChildElement("property");
1809 while (propNode != nullptr)
1810 {
1811 const char *propertyName =
1812 propNode->Attribute("name");
1813 BMCWEB_LOG_DEBUG << "Found property "
1814 << propertyName;
1815 if (propertyName == transaction->propertyName)
1816 {
1817 const char *argType =
1818 propNode->Attribute("type");
1819 if (argType != nullptr)
1820 {
1821 sdbusplus::message::message m =
1822 crow::connections::systemBus
1823 ->new_method_call(
1824 connectionName.c_str(),
1825 transaction->objectPath
1826 .c_str(),
1827 "org.freedesktop.DBus."
1828 "Properties",
1829 "Set");
1830 m.append(interfaceName,
1831 transaction->propertyName);
1832 int r = sd_bus_message_open_container(
1833 m.get(), SD_BUS_TYPE_VARIANT,
1834 argType);
1835 if (r < 0)
1836 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001837 transaction->setErrorStatus(
1838 "Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001839 return;
1840 }
1841 r = convertJsonToDbus(
1842 m.get(), argType,
1843 transaction->propertyValue);
1844 if (r < 0)
1845 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001846 transaction->setErrorStatus(
1847 "Invalid arg type");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001848 return;
1849 }
1850 r = sd_bus_message_close_container(
1851 m.get());
1852 if (r < 0)
1853 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001854 transaction->setErrorStatus(
1855 "Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001856 return;
1857 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001858 crow::connections::systemBus
1859 ->async_send(
1860 m,
1861 [transaction](
1862 boost::system::error_code
1863 ec,
1864 sdbusplus::message::message
1865 &m) {
1866 BMCWEB_LOG_DEBUG << "sent";
1867 if (ec)
1868 {
Lei YU97d2a472019-06-11 17:44:27 +08001869 const sd_bus_error *e =
1870 m.get_error();
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001871 setErrorResponse(
1872 transaction->res,
1873 boost::beast::http::
1874 status::
1875 forbidden,
Lei YU97d2a472019-06-11 17:44:27 +08001876 e->name,
1877 e->message);
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001878 }
1879 else
1880 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001881 transaction->res
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001882 .jsonValue = {
1883 {"status", "ok"},
1884 {"message",
1885 "200 OK"},
1886 {"data", nullptr}};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001887 }
1888 });
1889 }
1890 }
1891 propNode =
1892 propNode->NextSiblingElement("property");
1893 }
1894 ifaceNode =
1895 ifaceNode->NextSiblingElement("interface");
1896 }
1897 },
1898 connectionName, transaction->objectPath,
1899 "org.freedesktop.DBus.Introspectable", "Introspect");
1900 }
1901 },
1902 "xyz.openbmc_project.ObjectMapper",
1903 "/xyz/openbmc_project/object_mapper",
1904 "xyz.openbmc_project.ObjectMapper", "GetObject",
1905 transaction->objectPath, std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001906}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001907
Ed Tanous049a0512018-11-01 13:58:42 -07001908inline void handleDBusUrl(const crow::Request &req, crow::Response &res,
1909 std::string &objectPath)
1910{
Ed Tanous049a0512018-11-01 13:58:42 -07001911
1912 // If accessing a single attribute, fill in and update objectPath,
1913 // otherwise leave destProperty blank
1914 std::string destProperty = "";
1915 const char *attrSeperator = "/attr/";
1916 size_t attrPosition = objectPath.find(attrSeperator);
1917 if (attrPosition != objectPath.npos)
1918 {
1919 destProperty = objectPath.substr(attrPosition + strlen(attrSeperator),
1920 objectPath.length());
1921 objectPath = objectPath.substr(0, attrPosition);
1922 }
1923
1924 if (req.method() == "POST"_method)
1925 {
1926 constexpr const char *actionSeperator = "/action/";
1927 size_t actionPosition = objectPath.find(actionSeperator);
1928 if (actionPosition != objectPath.npos)
1929 {
1930 std::string postProperty =
1931 objectPath.substr((actionPosition + strlen(actionSeperator)),
1932 objectPath.length());
1933 objectPath = objectPath.substr(0, actionPosition);
1934 handleAction(req, res, objectPath, postProperty);
1935 return;
1936 }
1937 }
1938 else if (req.method() == "GET"_method)
1939 {
1940 if (boost::ends_with(objectPath, "/enumerate"))
1941 {
1942 objectPath.erase(objectPath.end() - sizeof("enumerate"),
1943 objectPath.end());
1944 handleEnumerate(res, objectPath);
1945 }
1946 else if (boost::ends_with(objectPath, "/list"))
1947 {
1948 objectPath.erase(objectPath.end() - sizeof("list"),
1949 objectPath.end());
1950 handleList(res, objectPath);
1951 }
1952 else
1953 {
Ed Tanousf839dfe2018-11-12 11:11:15 -08001954 // Trim any trailing "/" at the end
1955 if (boost::ends_with(objectPath, "/"))
1956 {
1957 objectPath.pop_back();
1958 handleList(res, objectPath, 1);
1959 }
1960 else
1961 {
1962 handleGet(res, objectPath, destProperty);
1963 }
Ed Tanous049a0512018-11-01 13:58:42 -07001964 }
1965 return;
1966 }
1967 else if (req.method() == "PUT"_method)
1968 {
1969 handlePut(req, res, objectPath, destProperty);
1970 return;
1971 }
Matt Spinlerde818812018-12-11 16:39:20 -06001972 else if (req.method() == "DELETE"_method)
1973 {
1974 handleDelete(req, res, objectPath);
1975 return;
1976 }
Ed Tanous049a0512018-11-01 13:58:42 -07001977
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -06001978 setErrorResponse(res, boost::beast::http::status::method_not_allowed,
1979 methodNotAllowedDesc, methodNotAllowedMsg);
Ed Tanous049a0512018-11-01 13:58:42 -07001980 res.end();
1981}
1982
Ed Tanous1abe55e2018-09-05 08:30:59 -07001983template <typename... Middlewares> void requestRoutes(Crow<Middlewares...> &app)
1984{
1985 BMCWEB_ROUTE(app, "/bus/")
Tanousf00032d2018-11-05 01:18:10 -03001986 .requires({"Login"})
Ed Tanous1abe55e2018-09-05 08:30:59 -07001987 .methods("GET"_method)(
1988 [](const crow::Request &req, crow::Response &res) {
1989 res.jsonValue = {{"busses", {{{"name", "system"}}}},
1990 {"status", "ok"}};
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001991 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001992 });
1993
1994 BMCWEB_ROUTE(app, "/bus/system/")
Tanousf00032d2018-11-05 01:18:10 -03001995 .requires({"Login"})
Ed Tanous1abe55e2018-09-05 08:30:59 -07001996 .methods("GET"_method)(
1997 [](const crow::Request &req, crow::Response &res) {
1998 auto myCallback = [&res](const boost::system::error_code ec,
1999 std::vector<std::string> &names) {
2000 if (ec)
2001 {
2002 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
2003 res.result(
2004 boost::beast::http::status::internal_server_error);
2005 }
2006 else
2007 {
2008 std::sort(names.begin(), names.end());
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002009 res.jsonValue = {{"status", "ok"}};
2010 auto &objectsSub = res.jsonValue["objects"];
Ed Tanous1abe55e2018-09-05 08:30:59 -07002011 for (auto &name : names)
2012 {
2013 objectsSub.push_back({{"name", name}});
2014 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002015 }
2016 res.end();
2017 };
2018 crow::connections::systemBus->async_method_call(
2019 std::move(myCallback), "org.freedesktop.DBus", "/",
2020 "org.freedesktop.DBus", "ListNames");
2021 });
2022
2023 BMCWEB_ROUTE(app, "/list/")
Tanousf00032d2018-11-05 01:18:10 -03002024 .requires({"Login"})
Ed Tanous1abe55e2018-09-05 08:30:59 -07002025 .methods("GET"_method)(
2026 [](const crow::Request &req, crow::Response &res) {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002027 handleList(res, "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07002028 });
2029
2030 BMCWEB_ROUTE(app, "/xyz/<path>")
Tanousf00032d2018-11-05 01:18:10 -03002031 .requires({"Login"})
2032 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
2033 const std::string &path) {
2034 std::string objectPath = "/xyz/" + path;
2035 handleDBusUrl(req, res, objectPath);
2036 });
2037
2038 BMCWEB_ROUTE(app, "/xyz/<path>")
2039 .requires({"ConfigureComponents", "ConfigureManager"})
2040 .methods("PUT"_method, "POST"_method, "DELETE"_method)(
Ed Tanous049a0512018-11-01 13:58:42 -07002041 [](const crow::Request &req, crow::Response &res,
2042 const std::string &path) {
2043 std::string objectPath = "/xyz/" + path;
2044 handleDBusUrl(req, res, objectPath);
2045 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002046
Ed Tanous049a0512018-11-01 13:58:42 -07002047 BMCWEB_ROUTE(app, "/org/<path>")
Tanousf00032d2018-11-05 01:18:10 -03002048 .requires({"Login"})
2049 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
2050 const std::string &path) {
Ed Tanouse1281402019-04-03 07:07:10 -07002051 std::string objectPath = "/org/" + path;
Tanousf00032d2018-11-05 01:18:10 -03002052 handleDBusUrl(req, res, objectPath);
2053 });
2054
2055 BMCWEB_ROUTE(app, "/org/<path>")
2056 .requires({"ConfigureComponents", "ConfigureManager"})
2057 .methods("PUT"_method, "POST"_method, "DELETE"_method)(
Ed Tanous049a0512018-11-01 13:58:42 -07002058 [](const crow::Request &req, crow::Response &res,
2059 const std::string &path) {
Ed Tanouse1281402019-04-03 07:07:10 -07002060 std::string objectPath = "/org/" + path;
Ed Tanous049a0512018-11-01 13:58:42 -07002061 handleDBusUrl(req, res, objectPath);
2062 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002063
Ed Tanous1abe55e2018-09-05 08:30:59 -07002064 BMCWEB_ROUTE(app, "/download/dump/<str>/")
Tanousf00032d2018-11-05 01:18:10 -03002065 .requires({"ConfigureManager"})
Ed Tanous1abe55e2018-09-05 08:30:59 -07002066 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
2067 const std::string &dumpId) {
Ed Tanousad18f072018-11-14 14:07:48 -08002068 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]*)$");
Ed Tanous1abe55e2018-09-05 08:30:59 -07002069 if (!std::regex_match(dumpId, validFilename))
2070 {
Ed Tanousad18f072018-11-14 14:07:48 -08002071 res.result(boost::beast::http::status::bad_request);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002072 res.end();
2073 return;
2074 }
James Feistf6150402019-01-08 10:36:20 -08002075 std::filesystem::path loc(
Ed Tanous1abe55e2018-09-05 08:30:59 -07002076 "/var/lib/phosphor-debug-collector/dumps");
2077
Ed Tanousad18f072018-11-14 14:07:48 -08002078 loc /= dumpId;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002079
James Feistf6150402019-01-08 10:36:20 -08002080 if (!std::filesystem::exists(loc) ||
2081 !std::filesystem::is_directory(loc))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002082 {
Ed Tanousad18f072018-11-14 14:07:48 -08002083 BMCWEB_LOG_ERROR << loc << "Not found";
Ed Tanous1abe55e2018-09-05 08:30:59 -07002084 res.result(boost::beast::http::status::not_found);
2085 res.end();
2086 return;
2087 }
James Feistf6150402019-01-08 10:36:20 -08002088 std::filesystem::directory_iterator files(loc);
Ed Tanousad18f072018-11-14 14:07:48 -08002089
Ed Tanous1abe55e2018-09-05 08:30:59 -07002090 for (auto &file : files)
2091 {
2092 std::ifstream readFile(file.path());
Ed Tanousad18f072018-11-14 14:07:48 -08002093 if (!readFile.good())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002094 {
2095 continue;
2096 }
2097 res.addHeader("Content-Type", "application/octet-stream");
2098 res.body() = {std::istreambuf_iterator<char>(readFile),
2099 std::istreambuf_iterator<char>()};
2100 res.end();
Ed Tanousad18f072018-11-14 14:07:48 -08002101 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002102 }
2103 res.result(boost::beast::http::status::not_found);
2104 res.end();
2105 return;
2106 });
2107
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002108 BMCWEB_ROUTE(app, "/bus/system/<str>/")
Tanousf00032d2018-11-05 01:18:10 -03002109 .requires({"Login"})
Ed Tanous1abe55e2018-09-05 08:30:59 -07002110 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002111 const std::string &Connection) {
2112 introspectObjects(Connection, "/",
2113 std::make_shared<bmcweb::AsyncResp>(res));
2114 });
2115
2116 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
2117 .methods("GET"_method,
2118 "POST"_method)([](const crow::Request &req,
2119 crow::Response &res,
2120 const std::string &processName,
2121 const std::string &requestedPath) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002122 std::vector<std::string> strs;
2123 boost::split(strs, requestedPath, boost::is_any_of("/"));
2124 std::string objectPath;
2125 std::string interfaceName;
2126 std::string methodName;
2127 auto it = strs.begin();
2128 if (it == strs.end())
2129 {
2130 objectPath = "/";
2131 }
2132 while (it != strs.end())
2133 {
2134 // Check if segment contains ".". If it does, it must be an
2135 // interface
2136 if (it->find(".") != std::string::npos)
2137 {
2138 break;
Ed Tanous7c091622019-05-23 11:42:36 -07002139 // This check is neccesary as the trailing slash gets
2140 // parsed as part of our <path> specifier above, which
2141 // causes the normal trailing backslash redirector to
2142 // fail.
Ed Tanous1abe55e2018-09-05 08:30:59 -07002143 }
2144 else if (!it->empty())
2145 {
2146 objectPath += "/" + *it;
2147 }
2148 it++;
2149 }
2150 if (it != strs.end())
2151 {
2152 interfaceName = *it;
2153 it++;
2154
2155 // after interface, we might have a method name
2156 if (it != strs.end())
2157 {
2158 methodName = *it;
2159 it++;
2160 }
2161 }
2162 if (it != strs.end())
2163 {
Ed Tanous7c091622019-05-23 11:42:36 -07002164 // if there is more levels past the method name, something
2165 // went wrong, return not found
Ed Tanous1abe55e2018-09-05 08:30:59 -07002166 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002167 return;
2168 }
2169 if (interfaceName.empty())
2170 {
Ed Tanous7c091622019-05-23 11:42:36 -07002171 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
2172 std::make_shared<bmcweb::AsyncResp>(res);
2173
Ed Tanous1abe55e2018-09-05 08:30:59 -07002174 crow::connections::systemBus->async_method_call(
Ed Tanous7c091622019-05-23 11:42:36 -07002175 [asyncResp, processName,
Ed Tanous1abe55e2018-09-05 08:30:59 -07002176 objectPath](const boost::system::error_code ec,
2177 const std::string &introspect_xml) {
2178 if (ec)
2179 {
2180 BMCWEB_LOG_ERROR
2181 << "Introspect call failed with error: "
2182 << ec.message()
2183 << " on process: " << processName
2184 << " path: " << objectPath << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002185 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002186 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002187 tinyxml2::XMLDocument doc;
2188
2189 doc.Parse(introspect_xml.c_str());
2190 tinyxml2::XMLNode *pRoot =
2191 doc.FirstChildElement("node");
2192 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002193 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002194 BMCWEB_LOG_ERROR << "XML document failed to parse "
2195 << processName << " " << objectPath
2196 << "\n";
Ed Tanous7c091622019-05-23 11:42:36 -07002197 asyncResp->res.jsonValue = {
2198 {"status", "XML parse error"}};
2199 asyncResp->res.result(boost::beast::http::status::
2200 internal_server_error);
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002201 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002202 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002203
2204 BMCWEB_LOG_DEBUG << introspect_xml;
Ed Tanous7c091622019-05-23 11:42:36 -07002205 asyncResp->res.jsonValue = {
2206 {"status", "ok"},
2207 {"bus_name", processName},
2208 {"object_path", objectPath}};
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002209 nlohmann::json &interfacesArray =
Ed Tanous7c091622019-05-23 11:42:36 -07002210 asyncResp->res.jsonValue["interfaces"];
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002211 interfacesArray = nlohmann::json::array();
2212 tinyxml2::XMLElement *interface =
2213 pRoot->FirstChildElement("interface");
2214
2215 while (interface != nullptr)
2216 {
2217 const char *ifaceName =
2218 interface->Attribute("name");
2219 if (ifaceName != nullptr)
2220 {
2221 interfacesArray.push_back(
2222 {{"name", ifaceName}});
2223 }
2224
2225 interface =
2226 interface->NextSiblingElement("interface");
2227 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002228 },
2229 processName, objectPath,
2230 "org.freedesktop.DBus.Introspectable", "Introspect");
2231 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002232 else if (methodName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002233 {
Ed Tanous7c091622019-05-23 11:42:36 -07002234 std::shared_ptr<bmcweb::AsyncResp> asyncResp =
2235 std::make_shared<bmcweb::AsyncResp>(res);
2236
Ed Tanous1abe55e2018-09-05 08:30:59 -07002237 crow::connections::systemBus->async_method_call(
Ed Tanous7c091622019-05-23 11:42:36 -07002238 [asyncResp, processName, objectPath,
2239 interfaceName](const boost::system::error_code ec,
2240 const std::string &introspect_xml) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002241 if (ec)
2242 {
2243 BMCWEB_LOG_ERROR
2244 << "Introspect call failed with error: "
2245 << ec.message()
2246 << " on process: " << processName
2247 << " path: " << objectPath << "\n";
Ed Tanous7c091622019-05-23 11:42:36 -07002248 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002249 }
Ed Tanous7c091622019-05-23 11:42:36 -07002250 tinyxml2::XMLDocument doc;
2251
2252 doc.Parse(introspect_xml.data(), introspect_xml.size());
2253 tinyxml2::XMLNode *pRoot =
2254 doc.FirstChildElement("node");
2255 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002256 {
Ed Tanous7c091622019-05-23 11:42:36 -07002257 BMCWEB_LOG_ERROR << "XML document failed to parse "
2258 << processName << " " << objectPath
2259 << "\n";
2260 asyncResp->res.result(boost::beast::http::status::
2261 internal_server_error);
2262 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002263 }
Ed Tanous7c091622019-05-23 11:42:36 -07002264 asyncResp->res.jsonValue = {
2265 {"status", "ok"},
2266 {"bus_name", processName},
2267 {"interface", interfaceName},
2268 {"object_path", objectPath}};
2269
2270 nlohmann::json &methodsArray =
2271 asyncResp->res.jsonValue["methods"];
2272 methodsArray = nlohmann::json::array();
2273
2274 nlohmann::json &signalsArray =
2275 asyncResp->res.jsonValue["signals"];
2276 signalsArray = nlohmann::json::array();
2277
2278 nlohmann::json &propertiesObj =
2279 asyncResp->res.jsonValue["properties"];
2280 propertiesObj = nlohmann::json::object();
2281
2282 // if we know we're the only call, build the
2283 // json directly
2284 tinyxml2::XMLElement *interface =
2285 pRoot->FirstChildElement("interface");
2286 while (interface != nullptr)
2287 {
2288 const char *ifaceName =
2289 interface->Attribute("name");
2290
2291 if (ifaceName != nullptr &&
2292 ifaceName == interfaceName)
2293 {
2294 break;
2295 }
2296
2297 interface =
2298 interface->NextSiblingElement("interface");
2299 }
2300 if (interface == nullptr)
2301 {
2302 // if we got to the end of the list and
2303 // never found a match, throw 404
2304 asyncResp->res.result(
2305 boost::beast::http::status::not_found);
2306 return;
2307 }
2308
2309 tinyxml2::XMLElement *methods =
2310 interface->FirstChildElement("method");
2311 while (methods != nullptr)
2312 {
2313 nlohmann::json argsArray = nlohmann::json::array();
2314 tinyxml2::XMLElement *arg =
2315 methods->FirstChildElement("arg");
2316 while (arg != nullptr)
2317 {
2318 nlohmann::json thisArg;
2319 for (const char *fieldName :
2320 std::array<const char *, 3>{
2321 "name", "direction", "type"})
2322 {
2323 const char *fieldValue =
2324 arg->Attribute(fieldName);
2325 if (fieldValue != nullptr)
2326 {
2327 thisArg[fieldName] = fieldValue;
2328 }
2329 }
2330 argsArray.push_back(std::move(thisArg));
2331 arg = arg->NextSiblingElement("arg");
2332 }
2333
2334 const char *name = methods->Attribute("name");
2335 if (name != nullptr)
2336 {
2337 methodsArray.push_back(
2338 {{"name", name},
2339 {"uri", "/bus/system/" + processName +
2340 objectPath + "/" +
2341 interfaceName + "/" + name},
2342 {"args", argsArray}});
2343 }
2344 methods = methods->NextSiblingElement("method");
2345 }
2346 tinyxml2::XMLElement *signals =
2347 interface->FirstChildElement("signal");
2348 while (signals != nullptr)
2349 {
2350 nlohmann::json argsArray = nlohmann::json::array();
2351
2352 tinyxml2::XMLElement *arg =
2353 signals->FirstChildElement("arg");
2354 while (arg != nullptr)
2355 {
2356 const char *name = arg->Attribute("name");
2357 const char *type = arg->Attribute("type");
2358 if (name != nullptr && type != nullptr)
2359 {
2360 argsArray.push_back({
2361 {"name", name},
2362 {"type", type},
2363 });
2364 }
2365 arg = arg->NextSiblingElement("arg");
2366 }
2367 const char *name = signals->Attribute("name");
2368 if (name != nullptr)
2369 {
2370 signalsArray.push_back(
2371 {{"name", name}, {"args", argsArray}});
2372 }
2373
2374 signals = signals->NextSiblingElement("signal");
2375 }
2376
2377 tinyxml2::XMLElement *property =
2378 interface->FirstChildElement("property");
2379 while (property != nullptr)
2380 {
2381 const char *name = property->Attribute("name");
2382 const char *type = property->Attribute("type");
2383 if (type != nullptr && name != nullptr)
2384 {
2385 sdbusplus::message::message m =
2386 crow::connections::systemBus
2387 ->new_method_call(processName.c_str(),
2388 objectPath.c_str(),
2389 "org.freedesktop."
2390 "DBus."
2391 "Properties",
2392 "Get");
2393 m.append(interfaceName, name);
2394 nlohmann::json &propertyItem =
2395 propertiesObj[name];
2396 crow::connections::systemBus->async_send(
2397 m, [&propertyItem, asyncResp](
2398 boost::system::error_code &ec,
2399 sdbusplus::message::message &m) {
2400 if (ec)
2401 {
2402 return;
2403 }
2404
2405 convertDBusToJSON("v", m, propertyItem);
2406 });
2407 }
2408 property = property->NextSiblingElement("property");
2409 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002410 },
2411 processName, objectPath,
2412 "org.freedesktop.DBus.Introspectable", "Introspect");
2413 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002414 else
2415 {
2416 if (req.method() != "POST"_method)
2417 {
2418 res.result(boost::beast::http::status::not_found);
2419 res.end();
2420 return;
2421 }
2422
2423 nlohmann::json requestDbusData =
2424 nlohmann::json::parse(req.body, nullptr, false);
2425
2426 if (requestDbusData.is_discarded())
2427 {
2428 res.result(boost::beast::http::status::bad_request);
2429 res.end();
2430 return;
2431 }
2432 if (!requestDbusData.is_array())
2433 {
2434 res.result(boost::beast::http::status::bad_request);
2435 res.end();
2436 return;
2437 }
2438 auto transaction = std::make_shared<InProgressActionData>(res);
2439
2440 transaction->path = objectPath;
2441 transaction->methodName = methodName;
2442 transaction->arguments = std::move(requestDbusData);
2443
2444 findActionOnInterface(transaction, processName);
2445 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002446 });
2447}
2448} // namespace openbmc_mapper
2449} // namespace crow