blob: b1f24b009124cd3025eb8488d441f3b5e238f221 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#include <crow/app.h>
2
3#include <tinyxml2.h>
Ed Tanous911ac312017-08-15 09:37:42 -07004#include <dbus_singleton.hpp>
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07005#include <experimental/filesystem>
6#include <fstream>
Ed Tanouse0d918b2018-03-27 17:41:04 -07007#include <boost/algorithm/string.hpp>
Ed Tanous64530012018-02-06 17:08:16 -08008#include <boost/container/flat_set.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07009
10namespace crow {
11namespace openbmc_mapper {
Ed Tanousba9f9a62017-10-11 16:40:35 -070012
Ed Tanous55c7b7a2018-05-22 15:27:24 -070013void introspectObjects(crow::Response &res, std::string process_name,
14 std::string path,
15 std::shared_ptr<nlohmann::json> transaction) {
16 crow::connections::systemBus->async_method_call(
Ed Tanousaa2e59c2018-04-12 12:17:20 -070017 [
Ed Tanous55c7b7a2018-05-22 15:27:24 -070018 &res, transaction, processName{std::move(process_name)},
19 objectPath{std::move(path)}
Ed Tanousaa2e59c2018-04-12 12:17:20 -070020 ](const boost::system::error_code ec, const std::string &introspect_xml) {
Ed Tanous911ac312017-08-15 09:37:42 -070021 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070022 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
23 << ec.message() << " on process: " << processName
24 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -070025
26 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070027 transaction->push_back({{"path", objectPath}});
Ed Tanous911ac312017-08-15 09:37:42 -070028
29 tinyxml2::XMLDocument doc;
30
31 doc.Parse(introspect_xml.c_str());
32 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
33 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070034 BMCWEB_LOG_ERROR << "XML document failed to parse " << processName
35 << " " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -070036
37 } else {
38 tinyxml2::XMLElement *node = pRoot->FirstChildElement("node");
39 while (node != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070040 std::string childPath = node->Attribute("name");
Ed Tanous911ac312017-08-15 09:37:42 -070041 std::string newpath;
Ed Tanous55c7b7a2018-05-22 15:27:24 -070042 if (objectPath != "/") {
43 newpath += objectPath;
Ed Tanous911ac312017-08-15 09:37:42 -070044 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070045 newpath += "/" + childPath;
Ed Tanous64530012018-02-06 17:08:16 -080046 // introspect the subobjects as well
Ed Tanous55c7b7a2018-05-22 15:27:24 -070047 introspectObjects(res, processName, newpath, transaction);
Ed Tanous911ac312017-08-15 09:37:42 -070048
49 node = node->NextSiblingElement("node");
50 }
51 }
52 }
53 // if we're the last outstanding caller, finish the request
Ed Tanous64530012018-02-06 17:08:16 -080054 if (transaction.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070055 res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -070056 {"bus_name", processName},
57 {"objects", std::move(*transaction)}};
Ed Tanous911ac312017-08-15 09:37:42 -070058 res.end();
59 }
60 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -070061 process_name, path, "org.freedesktop.DBus.Introspectable", "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -070062}
Ed Tanous64530012018-02-06 17:08:16 -080063
Ed Tanousaa2e59c2018-04-12 12:17:20 -070064// A smattering of common types to unpack. TODO(ed) this should really iterate
65// the sdbusplus object directly and build the json response
66using DbusRestVariantType = sdbusplus::message::variant<
67 std::vector<std::tuple<std::string, std::string, std::string>>, std::string,
68 int64_t, uint64_t, double, int32_t, uint32_t, int16_t, uint16_t, uint8_t,
69 bool>;
70
71using ManagedObjectType = std::vector<std::pair<
72 sdbusplus::message::object_path,
73 boost::container::flat_map<
74 std::string,
75 boost::container::flat_map<std::string, DbusRestVariantType>>>>;
76
Ed Tanous55c7b7a2018-05-22 15:27:24 -070077void getManagedObjectsForEnumerate(
Ed Tanous64530012018-02-06 17:08:16 -080078 const std::string &object_name, const std::string &connection_name,
Ed Tanous55c7b7a2018-05-22 15:27:24 -070079 crow::Response &res, std::shared_ptr<nlohmann::json> transaction) {
80 crow::connections::systemBus->async_method_call(
Ed Tanous64530012018-02-06 17:08:16 -080081 [&res, transaction](const boost::system::error_code ec,
82 const ManagedObjectType &objects) {
83 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070084 BMCWEB_LOG_ERROR << ec;
Ed Tanous64530012018-02-06 17:08:16 -080085 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070086 nlohmann::json &dataJson = *transaction;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070087
Ed Tanous55c7b7a2018-05-22 15:27:24 -070088 for (auto &objectPath : objects) {
89 BMCWEB_LOG_DEBUG
90 << "Reading object "
91 << static_cast<const std::string &>(objectPath.first);
92 nlohmann::json &objectJson =
93 dataJson[static_cast<const std::string &>(objectPath.first)];
94 if (objectJson.is_null()) {
95 objectJson = nlohmann::json::object();
Ed Tanousaa2e59c2018-04-12 12:17:20 -070096 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070097 for (const auto &interface : objectPath.second) {
Ed Tanous64530012018-02-06 17:08:16 -080098 for (const auto &property : interface.second) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070099 nlohmann::json &propertyJson = objectJson[property.first];
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700100 mapbox::util::apply_visitor(
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700101 [&propertyJson](auto &&val) { propertyJson = val; },
Ed Tanous64530012018-02-06 17:08:16 -0800102 property.second);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700103
104 // dbus-rest represents booleans as 1 or 0, implement to match
105 // TODO(ed) see if dbus-rest should be changed
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700106 const bool *propertyBool = propertyJson.get_ptr<const bool *>();
107 if (propertyBool != nullptr) {
108 propertyJson = *propertyBool ? 1 : 0;
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700109 }
Ed Tanous64530012018-02-06 17:08:16 -0800110 }
111 }
112 }
113 }
114
115 if (transaction.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700116 res.jsonValue = {{"message", "200 OK"},
117 {"status", "ok"},
118 {"data", std::move(*transaction)}};
Ed Tanous64530012018-02-06 17:08:16 -0800119 res.end();
120 }
121 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700122 connection_name, object_name, "org.freedesktop.DBus.ObjectManager",
123 "GetManagedObjects");
124}
Ed Tanous64530012018-02-06 17:08:16 -0800125
126using GetSubTreeType = std::vector<
127 std::pair<std::string,
128 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
129
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700130// Structure for storing data on an in progress action
131struct InProgressActionData {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700132 InProgressActionData(crow::Response &res) : res(res){};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700133 ~InProgressActionData() {
134 if (res.result() == boost::beast::http::status::internal_server_error) {
135 // Reset the json object to clear out any data that made it in before the
136 // error happened
137 // todo(ed) handle error condition with proper code
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700138 res.jsonValue = nlohmann::json::object();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700139 }
140 res.end();
141 }
142
143 void setErrorStatus() {
144 res.result(boost::beast::http::status::internal_server_error);
145 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700146 crow::Response &res;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700147 std::string path;
148 std::string method_name;
149 nlohmann::json arguments;
150};
151
152std::vector<std::string> dbus_arg_split(const std::string &string) {
153 std::vector<std::string> ret;
154 if (string.empty()) {
155 return ret;
156 }
157 ret.push_back("");
158 int container_depth = 0;
Ed Tanous75db20e2018-07-27 13:44:44 -0700159
160 for (std::string::const_iterator character = string.begin();
161 character != string.end(); character++) {
162 ret.back() += *character;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700163 switch (*character) {
164 case ('a'):
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700165 break;
166 case ('('):
167 case ('{'):
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700168 container_depth++;
169 break;
170 case ('}'):
171 case (')'):
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700172 container_depth--;
173 if (container_depth == 0) {
Ed Tanous75db20e2018-07-27 13:44:44 -0700174 if (character + 1 != string.end()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700175 ret.push_back("");
176 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700177 }
178 break;
179 default:
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700180 if (container_depth == 0) {
Ed Tanous75db20e2018-07-27 13:44:44 -0700181 if (character + 1 != string.end()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700182 ret.push_back("");
183 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700184 }
185 break;
186 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700187 }
188}
189
190int convert_json_to_dbus(sd_bus_message *m, const std::string &arg_type,
191 const nlohmann::json &input_json) {
192 int r = 0;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700193 BMCWEB_LOG_DEBUG << "Converting " << input_json.dump()
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700194 << " to type: " << arg_type;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700195 const std::vector<std::string> arg_types = dbus_arg_split(arg_type);
196
197 // Assume a single object for now.
198 const nlohmann::json *j = &input_json;
199 nlohmann::json::const_iterator j_it = input_json.begin();
200
201 for (const std::string &arg_code : arg_types) {
202 // If we are decoding multiple objects, grab the pointer to the iterator,
203 // and increment it for the next loop
204 if (arg_types.size() > 1) {
205 if (j_it == input_json.end()) {
206 return -2;
207 }
208 j = &*j_it;
209 j_it++;
210 }
211 const int64_t *int_value = j->get_ptr<const int64_t *>();
212 const uint64_t *uint_value = j->get_ptr<const uint64_t *>();
213 const std::string *string_value = j->get_ptr<const std::string *>();
214 const double *double_value = j->get_ptr<const double *>();
215 const bool *b = j->get_ptr<const bool *>();
216 int64_t v = 0;
217 double d = 0.0;
218
219 // Do some basic type conversions that make sense. uint can be converted to
220 // int. int and uint can be converted to double
221 if (uint_value != nullptr && int_value == nullptr) {
222 v = static_cast<int64_t>(*uint_value);
223 int_value = &v;
224 }
225 if (uint_value != nullptr && double_value == nullptr) {
226 d = static_cast<double>(*uint_value);
227 double_value = &d;
228 }
229 if (int_value != nullptr && double_value == nullptr) {
230 d = static_cast<double>(*int_value);
231 double_value = &d;
232 }
233
234 if (arg_code == "s") {
235 if (string_value == nullptr) {
236 return -1;
237 }
238 r = sd_bus_message_append_basic(m, arg_code[0],
239 (void *)string_value->c_str());
240 if (r < 0) {
241 return r;
242 }
243 } else if (arg_code == "i") {
244 if (int_value == nullptr) {
245 return -1;
246 }
247 int32_t i = static_cast<int32_t>(*int_value);
248 r = sd_bus_message_append_basic(m, arg_code[0], &i);
249 if (r < 0) {
250 return r;
251 }
252 } else if (arg_code == "b") {
253 // lots of ways bool could be represented here. Try them all
254 int bool_int = false;
255 if (int_value != nullptr) {
256 bool_int = *int_value > 0 ? 1 : 0;
257 } else if (b != nullptr) {
258 bool_int = b ? 1 : 0;
259 } else if (string_value != nullptr) {
260 bool_int = boost::istarts_with(*string_value, "t") ? 1 : 0;
261 } else {
262 return -1;
263 }
264 r = sd_bus_message_append_basic(m, arg_code[0], &bool_int);
265 if (r < 0) {
266 return r;
267 }
268 } else if (arg_code == "n") {
269 if (int_value == nullptr) {
270 return -1;
271 }
272 int16_t n = static_cast<int16_t>(*int_value);
273 r = sd_bus_message_append_basic(m, arg_code[0], &n);
274 if (r < 0) {
275 return r;
276 }
277 } else if (arg_code == "x") {
278 if (int_value == nullptr) {
279 return -1;
280 }
281 r = sd_bus_message_append_basic(m, arg_code[0], int_value);
282 if (r < 0) {
283 return r;
284 }
285 } else if (arg_code == "y") {
286 if (uint_value == nullptr) {
287 return -1;
288 }
289 uint8_t y = static_cast<uint8_t>(*uint_value);
290 r = sd_bus_message_append_basic(m, arg_code[0], &y);
291 } else if (arg_code == "q") {
292 if (uint_value == nullptr) {
293 return -1;
294 }
295 uint16_t q = static_cast<uint16_t>(*uint_value);
296 r = sd_bus_message_append_basic(m, arg_code[0], &q);
297 } else if (arg_code == "u") {
298 if (uint_value == nullptr) {
299 return -1;
300 }
301 uint32_t u = static_cast<uint32_t>(*uint_value);
302 r = sd_bus_message_append_basic(m, arg_code[0], &u);
303 } else if (arg_code == "t") {
304 if (uint_value == nullptr) {
305 return -1;
306 }
307 r = sd_bus_message_append_basic(m, arg_code[0], uint_value);
308 } else if (arg_code == "d") {
309 sd_bus_message_append_basic(m, arg_code[0], double_value);
310 } else if (boost::starts_with(arg_code, "a")) {
311 std::string contained_type = arg_code.substr(1);
312 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
313 contained_type.c_str());
314 if (r < 0) {
315 return r;
316 }
317
318 for (nlohmann::json::const_iterator it = j->begin(); it != j->end();
319 ++it) {
320 r = convert_json_to_dbus(m, contained_type, *it);
321 if (r < 0) {
322 return r;
323 }
324
325 it++;
326 }
327 sd_bus_message_close_container(m);
328 } else if (boost::starts_with(arg_code, "v")) {
329 std::string contained_type = arg_code.substr(1);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700330 BMCWEB_LOG_DEBUG << "variant type: " << arg_code
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700331 << " appending variant of type: " << contained_type;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700332 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
333 contained_type.c_str());
334 if (r < 0) {
335 return r;
336 }
337
338 r = convert_json_to_dbus(m, contained_type, input_json);
339 if (r < 0) {
340 return r;
341 }
342
343 r = sd_bus_message_close_container(m);
344 if (r < 0) {
345 return r;
346 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700347 } else if (boost::starts_with(arg_code, "(") &&
348 boost::ends_with(arg_code, ")")) {
349 std::string contained_type = arg_code.substr(1, arg_code.size() - 1);
350 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
351 contained_type.c_str());
352 nlohmann::json::const_iterator it = j->begin();
353 for (const std::string &arg_code : dbus_arg_split(arg_type)) {
354 if (it == j->end()) {
355 return -1;
356 }
357 r = convert_json_to_dbus(m, arg_code, *it);
358 if (r < 0) {
359 return r;
360 }
361 it++;
362 }
363 r = sd_bus_message_close_container(m);
364 } else if (boost::starts_with(arg_code, "{") &&
365 boost::ends_with(arg_code, "}")) {
366 std::string contained_type = arg_code.substr(1, arg_code.size() - 1);
367 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
368 contained_type.c_str());
369 std::vector<std::string> codes = dbus_arg_split(contained_type);
370 if (codes.size() != 2) {
371 return -1;
372 }
373 const std::string &key_type = codes[0];
374 const std::string &value_type = codes[1];
375 for (auto it : j->items()) {
376 r = convert_json_to_dbus(m, key_type, it.key());
377 if (r < 0) {
378 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700379 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700380
381 r = convert_json_to_dbus(m, value_type, it.value());
382 if (r < 0) {
383 return r;
384 }
385 }
386 r = sd_bus_message_close_container(m);
387 } else {
388 return -2;
389 }
390 if (r < 0) {
391 return r;
392 }
393
394 if (arg_types.size() > 1) {
395 j_it++;
396 }
397 }
398}
399
400void find_action_on_interface(std::shared_ptr<InProgressActionData> transaction,
401 const std::string &connectionName) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700402 BMCWEB_LOG_DEBUG << "find_action_on_interface for connection "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700403 << connectionName;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700404 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700405 [
406 transaction, connectionName{std::string(connectionName)}
407 ](const boost::system::error_code ec, const std::string &introspect_xml) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700408 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700409 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700410 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700411 << ec.message() << " on process: " << connectionName
412 << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700413 } else {
414 tinyxml2::XMLDocument doc;
415
416 doc.Parse(introspect_xml.c_str());
417 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
418 if (pRoot == nullptr) {
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700419 BMCWEB_LOG_ERROR << "XML document failed to parse "
420 << connectionName << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700421
422 } else {
423 tinyxml2::XMLElement *interface_node =
424 pRoot->FirstChildElement("interface");
425 while (interface_node != nullptr) {
426 std::string this_interface_name =
427 interface_node->Attribute("name");
428 tinyxml2::XMLElement *method_node =
429 interface_node->FirstChildElement("method");
430 while (method_node != nullptr) {
431 std::string this_method_name = method_node->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700432 BMCWEB_LOG_DEBUG << "Found method: " << this_method_name;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700433 if (this_method_name == transaction->method_name) {
434 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700435 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700436 connectionName.c_str(), transaction->path.c_str(),
437 this_interface_name.c_str(),
438 transaction->method_name.c_str());
439
440 tinyxml2::XMLElement *argument_node =
441 method_node->FirstChildElement("arg");
442
443 nlohmann::json::const_iterator arg_it =
444 transaction->arguments.begin();
445
446 while (argument_node != nullptr) {
447 std::string arg_direction =
448 argument_node->Attribute("direction");
449 if (arg_direction == "in") {
450 std::string arg_type = argument_node->Attribute("type");
451 if (arg_it == transaction->arguments.end()) {
452 transaction->setErrorStatus();
453 return;
454 }
455 if (convert_json_to_dbus(m.get(), arg_type, *arg_it) <
456 0) {
457 transaction->setErrorStatus();
458 return;
459 }
460
461 arg_it++;
462 }
463 argument_node = method_node->NextSiblingElement("arg");
464 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700465 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700466 m, [transaction](boost::system::error_code ec,
467 sdbusplus::message::message &m) {
468 if (ec) {
469 transaction->setErrorStatus();
470 return;
471 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700472 transaction->res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700473 {"message", "200 OK"},
474 {"data", nullptr}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700475 });
476 break;
477 }
478 method_node = method_node->NextSiblingElement("method");
479 }
480 interface_node = interface_node->NextSiblingElement("interface");
481 }
482 }
483 }
484 },
485 connectionName, transaction->path, "org.freedesktop.DBus.Introspectable",
486 "Introspect");
487}
488
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700489void handle_action(const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700490 const std::string &object_path,
491 const std::string &method_name) {
492 nlohmann::json request_dbus_data =
493 nlohmann::json::parse(req.body, nullptr, false);
494
495 if (request_dbus_data.is_discarded()) {
496 res.result(boost::beast::http::status::bad_request);
497 res.end();
498 return;
499 }
500 if (!request_dbus_data.is_array()) {
501 res.result(boost::beast::http::status::bad_request);
502 res.end();
503 return;
504 }
505 auto transaction = std::make_shared<InProgressActionData>(res);
506
507 transaction->path = object_path;
508 transaction->method_name = method_name;
509 transaction->arguments = std::move(request_dbus_data);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700510 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700511 [transaction](
512 const boost::system::error_code ec,
513 const std::vector<std::pair<std::string, std::vector<std::string>>>
514 &interface_names) {
515 if (ec || interface_names.size() <= 0) {
516 transaction->setErrorStatus();
517 return;
518 }
519
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700520 BMCWEB_LOG_DEBUG << "GetObject returned objects "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700521 << interface_names.size();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700522
523 for (const std::pair<std::string, std::vector<std::string>> &object :
524 interface_names) {
525 find_action_on_interface(transaction, object.first);
526 }
527 },
528 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
529 "xyz.openbmc_project.ObjectMapper", "GetObject", object_path,
530 std::array<std::string, 0>());
531}
532
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700533void handle_list(crow::Response &res, const std::string &objectPath) {
534 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700535 [&res](const boost::system::error_code ec,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700536 std::vector<std::string> &objectPaths) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700537 if (ec) {
538 res.result(boost::beast::http::status::internal_server_error);
539 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700540 res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700541 {"message", "200 OK"},
542 {"data", std::move(objectPaths)}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700543 }
544 res.end();
545 },
546 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700547 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700548 static_cast<int32_t>(99), std::array<std::string, 0>());
549}
550
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700551void handle_enumerate(crow::Response &res, const std::string &objectPath) {
552 crow::connections::systemBus->async_method_call(
553 [&res, objectPath{std::string(objectPath)} ](
Ed Tanous64530012018-02-06 17:08:16 -0800554 const boost::system::error_code ec,
555 const GetSubTreeType &object_names) {
556 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700557 res.jsonValue = {{"message", "200 OK"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700558 {"status", "ok"},
559 {"data", nlohmann::json::object()}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700560
Ed Tanous64530012018-02-06 17:08:16 -0800561 res.end();
562 return;
563 }
564
565 boost::container::flat_set<std::string> connections;
566
567 for (const auto &object : object_names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700568 for (const auto &Connection : object.second) {
569 connections.insert(Connection.first);
Ed Tanous64530012018-02-06 17:08:16 -0800570 }
571 }
572
573 if (connections.size() <= 0) {
Ed Tanouse0d918b2018-03-27 17:41:04 -0700574 res.result(boost::beast::http::status::not_found);
Ed Tanous64530012018-02-06 17:08:16 -0800575 res.end();
576 return;
577 }
578 auto transaction =
579 std::make_shared<nlohmann::json>(nlohmann::json::object());
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700580 for (const std::string &Connection : connections) {
581 getManagedObjectsForEnumerate(objectPath, Connection, res,
582 transaction);
Ed Tanous64530012018-02-06 17:08:16 -0800583 }
Ed Tanous64530012018-02-06 17:08:16 -0800584 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700585 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700586 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, (int32_t)0,
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700587 std::array<std::string, 0>());
Ed Tanous64530012018-02-06 17:08:16 -0800588}
Ed Tanous911ac312017-08-15 09:37:42 -0700589
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700590void handle_get(crow::Response &res, std::string &object_path,
591 std::string &dest_property) {
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700592 BMCWEB_LOG_DEBUG << "handle_get: " << object_path
593 << " prop:" << dest_property;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700594 std::shared_ptr<std::string> property_name =
Ed Tanous75db20e2018-07-27 13:44:44 -0700595 std::make_shared<std::string>(std::move(dest_property));
596
597 std::shared_ptr<std::string> path =
598 std::make_shared<std::string>(std::move(object_path));
599
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700600 using GetObjectType =
601 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700602 crow::connections::systemBus->async_method_call(
Ed Tanous75db20e2018-07-27 13:44:44 -0700603 [&res, path, property_name](const boost::system::error_code ec,
604 const GetObjectType &object_names) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700605 if (ec || object_names.size() <= 0) {
606 res.result(boost::beast::http::status::not_found);
607 res.end();
608 return;
609 }
610 std::shared_ptr<nlohmann::json> response =
611 std::make_shared<nlohmann::json>(nlohmann::json::object());
612 // The mapper should never give us an empty interface names list, but
613 // check anyway
614 for (const std::pair<std::string, std::vector<std::string>> connection :
615 object_names) {
616 const std::vector<std::string> &interfaceNames = connection.second;
617
618 if (interfaceNames.size() <= 0) {
619 res.result(boost::beast::http::status::not_found);
620 res.end();
621 return;
622 }
623
624 for (const std::string &interface : interfaceNames) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700625 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700626 [&res, response, property_name](
627 const boost::system::error_code ec,
628 const std::vector<std::pair<
629 std::string, DbusRestVariantType>> &properties) {
630 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700631 BMCWEB_LOG_ERROR << "Bad dbus request error: " << ec;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700632 } else {
633 for (const std::pair<std::string, DbusRestVariantType>
634 &property : properties) {
635 // if property name is empty, or matches our search query,
636 // add it to the response json
637
638 if (property_name->empty()) {
639 mapbox::util::apply_visitor(
640 [&response, &property](auto &&val) {
641 (*response)[property.first] = val;
642 },
643 property.second);
644 } else if (property.first == *property_name) {
645 mapbox::util::apply_visitor(
646 [&response](auto &&val) { (*response) = val; },
647 property.second);
648 }
649 }
650 }
651 if (response.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700652 res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700653 {"message", "200 OK"},
654 {"data", *response}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700655
656 res.end();
657 }
658 },
Ed Tanous75db20e2018-07-27 13:44:44 -0700659 connection.first, *path, "org.freedesktop.DBus.Properties",
660 "GetAll", interface);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700661 }
662 }
663 },
664 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous75db20e2018-07-27 13:44:44 -0700665 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700666 std::array<std::string, 0>());
667}
668
669struct AsyncPutRequest {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700670 AsyncPutRequest(crow::Response &res) : res(res) {
671 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700672 {"status", "ok"}, {"message", "200 OK"}, {"data", nullptr}};
673 }
674 ~AsyncPutRequest() {
675 if (res.result() == boost::beast::http::status::internal_server_error) {
676 // Reset the json object to clear out any data that made it in before the
677 // error happened
678 // todo(ed) handle error condition with proper code
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700679 res.jsonValue = nlohmann::json::object();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700680 }
681
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700682 if (res.jsonValue.empty()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700683 res.result(boost::beast::http::status::forbidden);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700684 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700685 {"status", "error"},
686 {"message", "403 Forbidden"},
687 {"data",
688 {{"message",
689 "The specified property cannot be created: " + propertyName}}}};
690 }
691
692 res.end();
693 }
694
695 void setErrorStatus() {
696 res.result(boost::beast::http::status::internal_server_error);
697 }
698
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700699 crow::Response &res;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700700 std::string objectPath;
701 std::string propertyName;
702 nlohmann::json propertyValue;
703};
704
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700705void handle_put(const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700706 const std::string &objectPath,
707 const std::string &destProperty) {
708 nlohmann::json request_dbus_data =
709 nlohmann::json::parse(req.body, nullptr, false);
710
711 if (request_dbus_data.is_discarded()) {
712 res.result(boost::beast::http::status::bad_request);
713 res.end();
714 return;
715 }
716
717 nlohmann::json::const_iterator property_it = request_dbus_data.find("data");
718 if (property_it == request_dbus_data.end()) {
719 res.result(boost::beast::http::status::bad_request);
720 res.end();
721 return;
722 }
723 const nlohmann::json &propertySetValue = *property_it;
724 auto transaction = std::make_shared<AsyncPutRequest>(res);
725 transaction->objectPath = objectPath;
726 transaction->propertyName = destProperty;
727 transaction->propertyValue = propertySetValue;
728
729 using GetObjectType =
730 std::vector<std::pair<std::string, std::vector<std::string>>>;
731
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700732 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700733 [transaction](const boost::system::error_code ec,
734 const GetObjectType &object_names) {
735 if (!ec && object_names.size() <= 0) {
736 transaction->res.result(boost::beast::http::status::not_found);
737 return;
738 }
739
740 for (const std::pair<std::string, std::vector<std::string>> connection :
741 object_names) {
742 const std::string &connectionName = connection.first;
743
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700744 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700745 [ connectionName{std::string(connectionName)}, transaction ](
746 const boost::system::error_code ec,
747 const std::string &introspectXml) {
748 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700749 BMCWEB_LOG_ERROR
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700750 << "Introspect call failed with error: " << ec.message()
751 << " on process: " << connectionName;
752 transaction->setErrorStatus();
753 return;
754 }
755 tinyxml2::XMLDocument doc;
756
757 doc.Parse(introspectXml.c_str());
758 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
759 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700760 BMCWEB_LOG_ERROR << "XML document failed to parse: "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700761 << introspectXml;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700762 transaction->setErrorStatus();
763 return;
764 }
765 tinyxml2::XMLElement *ifaceNode =
766 pRoot->FirstChildElement("interface");
767 while (ifaceNode != nullptr) {
768 const char *interfaceName = ifaceNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700769 BMCWEB_LOG_DEBUG << "found interface " << interfaceName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700770 tinyxml2::XMLElement *propNode =
771 ifaceNode->FirstChildElement("property");
772 while (propNode != nullptr) {
773 const char *propertyName = propNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700774 BMCWEB_LOG_DEBUG << "Found property " << propertyName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700775 if (propertyName == transaction->propertyName) {
776 const char *argType = propNode->Attribute("type");
777 if (argType != nullptr) {
778 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700779 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700780 connectionName.c_str(),
781 transaction->objectPath.c_str(),
782 "org.freedesktop.DBus.Properties", "Set");
783 m.append(interfaceName, transaction->propertyName);
784 int r = sd_bus_message_open_container(
785 m.get(), SD_BUS_TYPE_VARIANT, argType);
786 if (r < 0) {
787 transaction->setErrorStatus();
788 return;
789 }
790 r = convert_json_to_dbus(m.get(), argType,
791 transaction->propertyValue);
792 if (r < 0) {
793 transaction->setErrorStatus();
794 return;
795 }
796 r = sd_bus_message_close_container(m.get());
797 if (r < 0) {
798 transaction->setErrorStatus();
799 return;
800 }
801
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700802 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700803 m, [transaction](boost::system::error_code ec,
804 sdbusplus::message::message &m) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700805 BMCWEB_LOG_DEBUG << "sent";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700806 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700807 transaction->res.jsonValue["status"] = "error";
808 transaction->res.jsonValue["message"] =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700809 ec.message();
810 }
811 });
812 }
813 }
814 propNode = propNode->NextSiblingElement("property");
815 }
816 ifaceNode = ifaceNode->NextSiblingElement("interface");
817 }
818 },
819 connectionName, transaction->objectPath,
820 "org.freedesktop.DBus.Introspectable", "Introspect");
821 }
822 },
823 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
824 "xyz.openbmc_project.ObjectMapper", "GetObject", transaction->objectPath,
825 std::array<std::string, 0>());
826}
827
Ed Tanous911ac312017-08-15 09:37:42 -0700828template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700829void requestRoutes(Crow<Middlewares...> &app) {
830 BMCWEB_ROUTE(app, "/bus/")
831 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
832 res.jsonValue = {{"busses", {{{"name", "system"}}}}, {"status", "ok"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700833 });
Ed Tanous911ac312017-08-15 09:37:42 -0700834
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700835 BMCWEB_ROUTE(app, "/bus/system/")
836 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
837
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700838 auto myCallback = [&res](const boost::system::error_code ec,
839 std::vector<std::string> &names) {
840 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700841 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
Ed Tanouse0d918b2018-03-27 17:41:04 -0700842 res.result(boost::beast::http::status::internal_server_error);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700843 } else {
844 std::sort(names.begin(), names.end());
845 nlohmann::json j{{"status", "ok"}};
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700846 auto &objectsSub = j["objects"];
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700847 for (auto &name : names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700848 objectsSub.push_back({{"name", name}});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700849 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700850 res.jsonValue = std::move(j);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700851 }
852 res.end();
853 };
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700854 crow::connections::systemBus->async_method_call(
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700855 std::move(myCallback), "org.freedesktop.DBus", "/",
856 "org.freedesktop.DBus", "ListNames");
Ed Tanous911ac312017-08-15 09:37:42 -0700857 });
858
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700859 BMCWEB_ROUTE(app, "/list/")
860 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700861 handle_list(res, "/");
Ed Tanousba9f9a62017-10-11 16:40:35 -0700862 });
863
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700864 BMCWEB_ROUTE(app, "/xyz/<path>")
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700865 .methods("GET"_method, "PUT"_method,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700866 "POST"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700867 const std::string &path) {
Ed Tanousa4e18f22018-04-27 10:25:29 -0700868 std::string object_path = "/xyz/" + path;
869
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700870 // Trim any trailing "/" at the end
871 if (boost::ends_with(object_path, "/")) {
872 object_path.pop_back();
873 }
874
875 // If accessing a single attribute, fill in and update object_path,
876 // otherwise leave dest_property blank
877 std::string dest_property = "";
878 const char *attr_seperator = "/attr/";
Ed Tanousa4e18f22018-04-27 10:25:29 -0700879 size_t attr_position = path.find(attr_seperator);
880 if (attr_position != path.npos) {
shiyilei00b92f72017-11-12 16:21:16 +0800881 object_path = "/xyz/" + path.substr(0, attr_position);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700882 dest_property = path.substr(attr_position + strlen(attr_seperator),
Ed Tanousa4e18f22018-04-27 10:25:29 -0700883 path.length());
shiyilei00b92f72017-11-12 16:21:16 +0800884 }
885
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700886 if (req.method() == "POST"_method) {
887 constexpr const char *action_seperator = "/action/";
888 size_t action_position = path.find(action_seperator);
889 if (action_position != path.npos) {
890 object_path = "/xyz/" + path.substr(0, action_position);
891 std::string post_property = path.substr(
892 (action_position + strlen(action_seperator)), path.length());
893 handle_action(req, res, object_path, post_property);
894 return;
895 }
896 } else if (req.method() == "GET"_method) {
897 if (boost::ends_with(object_path, "/enumerate")) {
898 object_path.erase(object_path.end() - 10, object_path.end());
899 handle_enumerate(res, object_path);
900 } else if (boost::ends_with(object_path, "/list")) {
901 object_path.erase(object_path.end() - 5, object_path.end());
902 handle_list(res, object_path);
903 } else {
904 handle_get(res, object_path, dest_property);
905 }
906 return;
907 } else if (req.method() == "PUT"_method) {
908 handle_put(req, res, object_path, dest_property);
Ed Tanous64530012018-02-06 17:08:16 -0800909 return;
910 }
911
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700912 res.result(boost::beast::http::status::method_not_allowed);
913 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -0700914 });
shiyilei00b92f72017-11-12 16:21:16 +0800915
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700916 BMCWEB_ROUTE(app, "/bus/system/<str>/")
917 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
918 const std::string &Connection) {
Ed Tanous64530012018-02-06 17:08:16 -0800919 std::shared_ptr<nlohmann::json> transaction;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700920 introspectObjects(res, Connection, "/", transaction);
Ed Tanous911ac312017-08-15 09:37:42 -0700921 });
922
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700923 BMCWEB_ROUTE(app, "/download/dump/<str>/")
924 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700925 const std::string &dumpId) {
926 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]+)$");
927 if (!std::regex_match(dumpId, validFilename)) {
928 res.result(boost::beast::http::status::not_found);
929 res.end();
930 return;
931 }
932 std::experimental::filesystem::path loc(
933 "/var/lib/phosphor-debug-collector/dumps");
934
935 loc += dumpId;
936
937 if (!std::experimental::filesystem::exists(loc) ||
938 !std::experimental::filesystem::is_directory(loc)) {
939 res.result(boost::beast::http::status::not_found);
940 res.end();
941 return;
942 }
943 std::experimental::filesystem::directory_iterator files(loc);
944 for (auto &file : files) {
945 std::ifstream readFile(file.path());
946 if (readFile.good()) {
947 continue;
948 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700949 res.addHeader("Content-Type", "application/octet-stream");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700950 res.body() = {std::istreambuf_iterator<char>(readFile),
951 std::istreambuf_iterator<char>()};
952 res.end();
953 }
954 res.result(boost::beast::http::status::not_found);
955 res.end();
956 return;
957 });
958
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700959 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
960 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
961 const std::string &processName,
962 const std::string &requestedPath) {
Ed Tanous911ac312017-08-15 09:37:42 -0700963 std::vector<std::string> strs;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700964 boost::split(strs, requestedPath, boost::is_any_of("/"));
965 std::string objectPath;
966 std::string interfaceName;
967 std::string methodName;
Ed Tanous911ac312017-08-15 09:37:42 -0700968 auto it = strs.begin();
969 if (it == strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700970 objectPath = "/";
Ed Tanous911ac312017-08-15 09:37:42 -0700971 }
972 while (it != strs.end()) {
973 // Check if segment contains ".". If it does, it must be an
974 // interface
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700975 if (it->find(".") != std::string::npos) {
Ed Tanous911ac312017-08-15 09:37:42 -0700976 break;
Ed Tanousba9f9a62017-10-11 16:40:35 -0700977 // THis check is neccesary as the trailing slash gets parsed as
Ed Tanous64530012018-02-06 17:08:16 -0800978 // part of our <path> specifier above, which causes the normal
979 // trailing backslash redirector to fail.
Ed Tanous911ac312017-08-15 09:37:42 -0700980 } else if (!it->empty()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700981 objectPath += "/" + *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700982 }
983 it++;
984 }
985 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700986 interfaceName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700987 it++;
988
989 // after interface, we might have a method name
990 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700991 methodName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700992 it++;
993 }
994 }
995 if (it != strs.end()) {
996 // if there is more levels past the method name, something went
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700997 // wrong, return not found
Ed Tanouse0d918b2018-03-27 17:41:04 -0700998 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -0700999 res.end();
1000 return;
1001 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001002 if (interfaceName.empty()) {
1003 crow::connections::systemBus->async_method_call(
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07001004 [&, processName, objectPath](const boost::system::error_code ec,
1005 const std::string &introspect_xml) {
Ed Tanous911ac312017-08-15 09:37:42 -07001006 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001007 BMCWEB_LOG_ERROR
Ed Tanous911ac312017-08-15 09:37:42 -07001008 << "Introspect call failed with error: " << ec.message()
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001009 << " on process: " << processName
1010 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -07001011
1012 } else {
1013 tinyxml2::XMLDocument doc;
1014
1015 doc.Parse(introspect_xml.c_str());
1016 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1017 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001018 BMCWEB_LOG_ERROR << "XML document failed to parse "
1019 << processName << " " << objectPath
1020 << "\n";
1021 res.jsonValue = {{"status", "XML parse error"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -07001022 res.result(
1023 boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -07001024 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001025 nlohmann::json interfacesArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001026 tinyxml2::XMLElement *interface =
1027 pRoot->FirstChildElement("interface");
1028
1029 while (interface != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001030 std::string ifaceName = interface->Attribute("name");
1031 interfacesArray.push_back({{"name", ifaceName}});
Ed Tanous911ac312017-08-15 09:37:42 -07001032
1033 interface = interface->NextSiblingElement("interface");
1034 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001035 res.jsonValue = {{"status", "ok"},
1036 {"bus_name", processName},
1037 {"interfaces", interfacesArray},
1038 {"object_path", objectPath}};
Ed Tanous911ac312017-08-15 09:37:42 -07001039 }
1040 }
1041 res.end();
1042 },
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001043 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanousaa2e59c2018-04-12 12:17:20 -07001044 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -07001045 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001046 crow::connections::systemBus->async_method_call(
Ed Tanous911ac312017-08-15 09:37:42 -07001047 [
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001048 &, processName, objectPath,
1049 interface_name{std::move(interfaceName)}
Ed Tanous911ac312017-08-15 09:37:42 -07001050 ](const boost::system::error_code ec,
1051 const std::string &introspect_xml) {
1052 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001053 BMCWEB_LOG_ERROR
Ed Tanous911ac312017-08-15 09:37:42 -07001054 << "Introspect call failed with error: " << ec.message()
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001055 << " on process: " << processName
1056 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -07001057
1058 } else {
1059 tinyxml2::XMLDocument doc;
1060
1061 doc.Parse(introspect_xml.c_str());
1062 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1063 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001064 BMCWEB_LOG_ERROR << "XML document failed to parse "
1065 << processName << " " << objectPath
1066 << "\n";
Ed Tanouse0d918b2018-03-27 17:41:04 -07001067 res.result(
1068 boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -07001069
1070 } else {
1071 tinyxml2::XMLElement *node =
1072 pRoot->FirstChildElement("node");
1073
1074 // if we know we're the only call, build the json directly
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001075 nlohmann::json methodsArray = nlohmann::json::array();
1076 nlohmann::json signalsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001077 tinyxml2::XMLElement *interface =
1078 pRoot->FirstChildElement("interface");
1079
1080 while (interface != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001081 std::string ifaceName = interface->Attribute("name");
Ed Tanous911ac312017-08-15 09:37:42 -07001082
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001083 if (ifaceName == interfaceName) {
Ed Tanous911ac312017-08-15 09:37:42 -07001084 tinyxml2::XMLElement *methods =
1085 interface->FirstChildElement("method");
1086 while (methods != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001087 nlohmann::json argsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001088 tinyxml2::XMLElement *arg =
1089 methods->FirstChildElement("arg");
1090 while (arg != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001091 argsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001092 {{"name", arg->Attribute("name")},
1093 {"type", arg->Attribute("type")},
1094 {"direction", arg->Attribute("direction")}});
1095 arg = arg->NextSiblingElement("arg");
1096 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001097 methodsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001098 {{"name", methods->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001099 {"uri", "/bus/system/" + processName +
1100 objectPath + "/" + interfaceName +
Ed Tanous64530012018-02-06 17:08:16 -08001101 "/" + methods->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001102 {"args", argsArray}});
Ed Tanous911ac312017-08-15 09:37:42 -07001103 methods = methods->NextSiblingElement("method");
1104 }
1105 tinyxml2::XMLElement *signals =
1106 interface->FirstChildElement("signal");
1107 while (signals != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001108 nlohmann::json argsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001109
1110 tinyxml2::XMLElement *arg =
1111 signals->FirstChildElement("arg");
1112 while (arg != nullptr) {
1113 std::string name = arg->Attribute("name");
1114 std::string type = arg->Attribute("type");
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001115 argsArray.push_back({
Ed Tanous64530012018-02-06 17:08:16 -08001116 {"name", name},
1117 {"type", type},
Ed Tanous911ac312017-08-15 09:37:42 -07001118 });
1119 arg = arg->NextSiblingElement("arg");
1120 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001121 signalsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001122 {{"name", signals->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001123 {"args", argsArray}});
Ed Tanous911ac312017-08-15 09:37:42 -07001124 signals = signals->NextSiblingElement("signal");
1125 }
1126
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001127 res.jsonValue = {
Ed Tanous911ac312017-08-15 09:37:42 -07001128 {"status", "ok"},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001129 {"bus_name", processName},
1130 {"interface", interfaceName},
1131 {"methods", methodsArray},
1132 {"object_path", objectPath},
Ed Tanous911ac312017-08-15 09:37:42 -07001133 {"properties", nlohmann::json::object()},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001134 {"signals", signalsArray}};
Ed Tanous911ac312017-08-15 09:37:42 -07001135
Ed Tanous911ac312017-08-15 09:37:42 -07001136 break;
1137 }
1138
1139 interface = interface->NextSiblingElement("interface");
1140 }
1141 if (interface == nullptr) {
1142 // if we got to the end of the list and never found a
1143 // match, throw 404
Ed Tanouse0d918b2018-03-27 17:41:04 -07001144 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -07001145 }
1146 }
1147 }
1148 res.end();
1149 },
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001150 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanousaa2e59c2018-04-12 12:17:20 -07001151 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -07001152 }
Ed Tanous911ac312017-08-15 09:37:42 -07001153 });
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001154}
Ed Tanous911ac312017-08-15 09:37:42 -07001155} // namespace openbmc_mapper
1156} // namespace crow