blob: 3e6443d29ef6b33adf93d3d3c8124c2eb4b08432 [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);
103 }
104 }
105 }
106 }
107
108 if (transaction.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700109 res.jsonValue = {{"message", "200 OK"},
110 {"status", "ok"},
111 {"data", std::move(*transaction)}};
Ed Tanous64530012018-02-06 17:08:16 -0800112 res.end();
113 }
114 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700115 connection_name, object_name, "org.freedesktop.DBus.ObjectManager",
116 "GetManagedObjects");
117}
Ed Tanous64530012018-02-06 17:08:16 -0800118
119using GetSubTreeType = std::vector<
120 std::pair<std::string,
121 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
122
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700123// Structure for storing data on an in progress action
124struct InProgressActionData {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700125 InProgressActionData(crow::Response &res) : res(res){};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700126 ~InProgressActionData() {
127 if (res.result() == boost::beast::http::status::internal_server_error) {
128 // Reset the json object to clear out any data that made it in before the
129 // error happened
130 // todo(ed) handle error condition with proper code
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700131 res.jsonValue = nlohmann::json::object();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700132 }
133 res.end();
134 }
135
136 void setErrorStatus() {
137 res.result(boost::beast::http::status::internal_server_error);
138 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700139 crow::Response &res;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700140 std::string path;
Ed Tanousd76323e2018-08-07 14:35:40 -0700141 std::string methodName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700142 nlohmann::json arguments;
143};
144
Ed Tanousd76323e2018-08-07 14:35:40 -0700145std::vector<std::string> dbusArgSplit(const std::string &string) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700146 std::vector<std::string> ret;
147 if (string.empty()) {
148 return ret;
149 }
150 ret.push_back("");
Ed Tanousd76323e2018-08-07 14:35:40 -0700151 int containerDepth = 0;
Ed Tanous75db20e2018-07-27 13:44:44 -0700152
153 for (std::string::const_iterator character = string.begin();
154 character != string.end(); character++) {
155 ret.back() += *character;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700156 switch (*character) {
157 case ('a'):
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700158 break;
159 case ('('):
160 case ('{'):
Ed Tanousd76323e2018-08-07 14:35:40 -0700161 containerDepth++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700162 break;
163 case ('}'):
164 case (')'):
Ed Tanousd76323e2018-08-07 14:35:40 -0700165 containerDepth--;
166 if (containerDepth == 0) {
Ed Tanous75db20e2018-07-27 13:44:44 -0700167 if (character + 1 != string.end()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700168 ret.push_back("");
169 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700170 }
171 break;
172 default:
Ed Tanousd76323e2018-08-07 14:35:40 -0700173 if (containerDepth == 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 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700180 }
181}
182
Ed Tanousd76323e2018-08-07 14:35:40 -0700183int convertJsonToDbus(sd_bus_message *m, const std::string &arg_type,
184 const nlohmann::json &input_json) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700185 int r = 0;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700186 BMCWEB_LOG_DEBUG << "Converting " << input_json.dump()
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700187 << " to type: " << arg_type;
Ed Tanousd76323e2018-08-07 14:35:40 -0700188 const std::vector<std::string> argTypes = dbusArgSplit(arg_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700189
190 // Assume a single object for now.
191 const nlohmann::json *j = &input_json;
Ed Tanousd76323e2018-08-07 14:35:40 -0700192 nlohmann::json::const_iterator jIt = input_json.begin();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700193
Ed Tanousd76323e2018-08-07 14:35:40 -0700194 for (const std::string &arg_code : argTypes) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700195 // If we are decoding multiple objects, grab the pointer to the iterator,
196 // and increment it for the next loop
Ed Tanousd76323e2018-08-07 14:35:40 -0700197 if (argTypes.size() > 1) {
198 if (jIt == input_json.end()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700199 return -2;
200 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700201 j = &*jIt;
202 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700203 }
204 const int64_t *int_value = j->get_ptr<const int64_t *>();
205 const uint64_t *uint_value = j->get_ptr<const uint64_t *>();
206 const std::string *string_value = j->get_ptr<const std::string *>();
207 const double *double_value = j->get_ptr<const double *>();
208 const bool *b = j->get_ptr<const bool *>();
209 int64_t v = 0;
210 double d = 0.0;
211
212 // Do some basic type conversions that make sense. uint can be converted to
213 // int. int and uint can be converted to double
214 if (uint_value != nullptr && int_value == nullptr) {
215 v = static_cast<int64_t>(*uint_value);
216 int_value = &v;
217 }
218 if (uint_value != nullptr && double_value == nullptr) {
219 d = static_cast<double>(*uint_value);
220 double_value = &d;
221 }
222 if (int_value != nullptr && double_value == nullptr) {
223 d = static_cast<double>(*int_value);
224 double_value = &d;
225 }
226
227 if (arg_code == "s") {
228 if (string_value == nullptr) {
229 return -1;
230 }
231 r = sd_bus_message_append_basic(m, arg_code[0],
232 (void *)string_value->c_str());
233 if (r < 0) {
234 return r;
235 }
236 } else if (arg_code == "i") {
237 if (int_value == nullptr) {
238 return -1;
239 }
240 int32_t i = static_cast<int32_t>(*int_value);
241 r = sd_bus_message_append_basic(m, arg_code[0], &i);
242 if (r < 0) {
243 return r;
244 }
245 } else if (arg_code == "b") {
246 // lots of ways bool could be represented here. Try them all
247 int bool_int = false;
248 if (int_value != nullptr) {
249 bool_int = *int_value > 0 ? 1 : 0;
250 } else if (b != nullptr) {
251 bool_int = b ? 1 : 0;
252 } else if (string_value != nullptr) {
253 bool_int = boost::istarts_with(*string_value, "t") ? 1 : 0;
254 } else {
255 return -1;
256 }
257 r = sd_bus_message_append_basic(m, arg_code[0], &bool_int);
258 if (r < 0) {
259 return r;
260 }
261 } else if (arg_code == "n") {
262 if (int_value == nullptr) {
263 return -1;
264 }
265 int16_t n = static_cast<int16_t>(*int_value);
266 r = sd_bus_message_append_basic(m, arg_code[0], &n);
267 if (r < 0) {
268 return r;
269 }
270 } else if (arg_code == "x") {
271 if (int_value == nullptr) {
272 return -1;
273 }
274 r = sd_bus_message_append_basic(m, arg_code[0], int_value);
275 if (r < 0) {
276 return r;
277 }
278 } else if (arg_code == "y") {
279 if (uint_value == nullptr) {
280 return -1;
281 }
282 uint8_t y = static_cast<uint8_t>(*uint_value);
283 r = sd_bus_message_append_basic(m, arg_code[0], &y);
284 } else if (arg_code == "q") {
285 if (uint_value == nullptr) {
286 return -1;
287 }
288 uint16_t q = static_cast<uint16_t>(*uint_value);
289 r = sd_bus_message_append_basic(m, arg_code[0], &q);
290 } else if (arg_code == "u") {
291 if (uint_value == nullptr) {
292 return -1;
293 }
294 uint32_t u = static_cast<uint32_t>(*uint_value);
295 r = sd_bus_message_append_basic(m, arg_code[0], &u);
296 } else if (arg_code == "t") {
297 if (uint_value == nullptr) {
298 return -1;
299 }
300 r = sd_bus_message_append_basic(m, arg_code[0], uint_value);
301 } else if (arg_code == "d") {
302 sd_bus_message_append_basic(m, arg_code[0], double_value);
303 } else if (boost::starts_with(arg_code, "a")) {
304 std::string contained_type = arg_code.substr(1);
305 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
306 contained_type.c_str());
307 if (r < 0) {
308 return r;
309 }
310
311 for (nlohmann::json::const_iterator it = j->begin(); it != j->end();
312 ++it) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700313 r = convertJsonToDbus(m, contained_type, *it);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700314 if (r < 0) {
315 return r;
316 }
317
318 it++;
319 }
320 sd_bus_message_close_container(m);
321 } else if (boost::starts_with(arg_code, "v")) {
322 std::string contained_type = arg_code.substr(1);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700323 BMCWEB_LOG_DEBUG << "variant type: " << arg_code
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700324 << " appending variant of type: " << contained_type;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700325 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
326 contained_type.c_str());
327 if (r < 0) {
328 return r;
329 }
330
Ed Tanousd76323e2018-08-07 14:35:40 -0700331 r = convertJsonToDbus(m, contained_type, input_json);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700332 if (r < 0) {
333 return r;
334 }
335
336 r = sd_bus_message_close_container(m);
337 if (r < 0) {
338 return r;
339 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700340 } else if (boost::starts_with(arg_code, "(") &&
341 boost::ends_with(arg_code, ")")) {
342 std::string contained_type = arg_code.substr(1, arg_code.size() - 1);
343 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
344 contained_type.c_str());
345 nlohmann::json::const_iterator it = j->begin();
Ed Tanousd76323e2018-08-07 14:35:40 -0700346 for (const std::string &arg_code : dbusArgSplit(arg_type)) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700347 if (it == j->end()) {
348 return -1;
349 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700350 r = convertJsonToDbus(m, arg_code, *it);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700351 if (r < 0) {
352 return r;
353 }
354 it++;
355 }
356 r = sd_bus_message_close_container(m);
357 } else if (boost::starts_with(arg_code, "{") &&
358 boost::ends_with(arg_code, "}")) {
359 std::string contained_type = arg_code.substr(1, arg_code.size() - 1);
360 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
361 contained_type.c_str());
Ed Tanousd76323e2018-08-07 14:35:40 -0700362 std::vector<std::string> codes = dbusArgSplit(contained_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700363 if (codes.size() != 2) {
364 return -1;
365 }
366 const std::string &key_type = codes[0];
367 const std::string &value_type = codes[1];
368 for (auto it : j->items()) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700369 r = convertJsonToDbus(m, key_type, it.key());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700370 if (r < 0) {
371 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700372 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700373
Ed Tanousd76323e2018-08-07 14:35:40 -0700374 r = convertJsonToDbus(m, value_type, it.value());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700375 if (r < 0) {
376 return r;
377 }
378 }
379 r = sd_bus_message_close_container(m);
380 } else {
381 return -2;
382 }
383 if (r < 0) {
384 return r;
385 }
386
Ed Tanousd76323e2018-08-07 14:35:40 -0700387 if (argTypes.size() > 1) {
388 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700389 }
390 }
391}
392
Ed Tanousd76323e2018-08-07 14:35:40 -0700393void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction,
394 const std::string &connectionName) {
395 BMCWEB_LOG_DEBUG << "findActionOnInterface for connection " << connectionName;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700396 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700397 [
398 transaction, connectionName{std::string(connectionName)}
399 ](const boost::system::error_code ec, const std::string &introspect_xml) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700400 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700401 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700402 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700403 << ec.message() << " on process: " << connectionName
404 << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700405 } else {
406 tinyxml2::XMLDocument doc;
407
408 doc.Parse(introspect_xml.c_str());
409 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
410 if (pRoot == nullptr) {
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700411 BMCWEB_LOG_ERROR << "XML document failed to parse "
412 << connectionName << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700413
414 } else {
415 tinyxml2::XMLElement *interface_node =
416 pRoot->FirstChildElement("interface");
417 while (interface_node != nullptr) {
418 std::string this_interface_name =
419 interface_node->Attribute("name");
420 tinyxml2::XMLElement *method_node =
421 interface_node->FirstChildElement("method");
422 while (method_node != nullptr) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700423 std::string this_methodName = method_node->Attribute("name");
424 BMCWEB_LOG_DEBUG << "Found method: " << this_methodName;
425 if (this_methodName == transaction->methodName) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700426 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700427 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700428 connectionName.c_str(), transaction->path.c_str(),
429 this_interface_name.c_str(),
Ed Tanousd76323e2018-08-07 14:35:40 -0700430 transaction->methodName.c_str());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700431
432 tinyxml2::XMLElement *argument_node =
433 method_node->FirstChildElement("arg");
434
435 nlohmann::json::const_iterator arg_it =
436 transaction->arguments.begin();
437
438 while (argument_node != nullptr) {
439 std::string arg_direction =
440 argument_node->Attribute("direction");
441 if (arg_direction == "in") {
442 std::string arg_type = argument_node->Attribute("type");
443 if (arg_it == transaction->arguments.end()) {
444 transaction->setErrorStatus();
445 return;
446 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700447 if (convertJsonToDbus(m.get(), arg_type, *arg_it) < 0) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700448 transaction->setErrorStatus();
449 return;
450 }
451
452 arg_it++;
453 }
454 argument_node = method_node->NextSiblingElement("arg");
455 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700456 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700457 m, [transaction](boost::system::error_code ec,
458 sdbusplus::message::message &m) {
459 if (ec) {
460 transaction->setErrorStatus();
461 return;
462 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700463 transaction->res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700464 {"message", "200 OK"},
465 {"data", nullptr}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700466 });
467 break;
468 }
469 method_node = method_node->NextSiblingElement("method");
470 }
471 interface_node = interface_node->NextSiblingElement("interface");
472 }
473 }
474 }
475 },
476 connectionName, transaction->path, "org.freedesktop.DBus.Introspectable",
477 "Introspect");
478}
479
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700480void handle_action(const crow::Request &req, crow::Response &res,
Ed Tanousd76323e2018-08-07 14:35:40 -0700481 const std::string &objectPath,
482 const std::string &methodName) {
483 nlohmann::json requestDbusData =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700484 nlohmann::json::parse(req.body, nullptr, false);
485
Ed Tanousd76323e2018-08-07 14:35:40 -0700486 if (requestDbusData.is_discarded()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700487 res.result(boost::beast::http::status::bad_request);
488 res.end();
489 return;
490 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700491 if (!requestDbusData.is_array()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700492 res.result(boost::beast::http::status::bad_request);
493 res.end();
494 return;
495 }
496 auto transaction = std::make_shared<InProgressActionData>(res);
497
Ed Tanousd76323e2018-08-07 14:35:40 -0700498 transaction->path = objectPath;
499 transaction->methodName = methodName;
500 transaction->arguments = std::move(requestDbusData);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700501 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700502 [transaction](
503 const boost::system::error_code ec,
504 const std::vector<std::pair<std::string, std::vector<std::string>>>
505 &interface_names) {
506 if (ec || interface_names.size() <= 0) {
507 transaction->setErrorStatus();
508 return;
509 }
510
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700511 BMCWEB_LOG_DEBUG << "GetObject returned objects "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700512 << interface_names.size();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700513
514 for (const std::pair<std::string, std::vector<std::string>> &object :
515 interface_names) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700516 findActionOnInterface(transaction, object.first);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700517 }
518 },
519 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanousd76323e2018-08-07 14:35:40 -0700520 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700521 std::array<std::string, 0>());
522}
523
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700524void handle_list(crow::Response &res, const std::string &objectPath) {
525 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700526 [&res](const boost::system::error_code ec,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700527 std::vector<std::string> &objectPaths) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700528 if (ec) {
529 res.result(boost::beast::http::status::internal_server_error);
530 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700531 res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700532 {"message", "200 OK"},
533 {"data", std::move(objectPaths)}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700534 }
535 res.end();
536 },
537 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700538 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700539 static_cast<int32_t>(99), std::array<std::string, 0>());
540}
541
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700542void handle_enumerate(crow::Response &res, const std::string &objectPath) {
543 crow::connections::systemBus->async_method_call(
544 [&res, objectPath{std::string(objectPath)} ](
Ed Tanous64530012018-02-06 17:08:16 -0800545 const boost::system::error_code ec,
546 const GetSubTreeType &object_names) {
547 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700548 res.jsonValue = {{"message", "200 OK"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700549 {"status", "ok"},
550 {"data", nlohmann::json::object()}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700551
Ed Tanous64530012018-02-06 17:08:16 -0800552 res.end();
553 return;
554 }
555
556 boost::container::flat_set<std::string> connections;
557
558 for (const auto &object : object_names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700559 for (const auto &Connection : object.second) {
560 connections.insert(Connection.first);
Ed Tanous64530012018-02-06 17:08:16 -0800561 }
562 }
563
564 if (connections.size() <= 0) {
Ed Tanouse0d918b2018-03-27 17:41:04 -0700565 res.result(boost::beast::http::status::not_found);
Ed Tanous64530012018-02-06 17:08:16 -0800566 res.end();
567 return;
568 }
569 auto transaction =
570 std::make_shared<nlohmann::json>(nlohmann::json::object());
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700571 for (const std::string &Connection : connections) {
572 getManagedObjectsForEnumerate(objectPath, Connection, res,
573 transaction);
Ed Tanous64530012018-02-06 17:08:16 -0800574 }
Ed Tanous64530012018-02-06 17:08:16 -0800575 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700576 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700577 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, (int32_t)0,
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700578 std::array<std::string, 0>());
Ed Tanous64530012018-02-06 17:08:16 -0800579}
Ed Tanous911ac312017-08-15 09:37:42 -0700580
Ed Tanousd76323e2018-08-07 14:35:40 -0700581void handle_get(crow::Response &res, std::string &objectPath,
582 std::string &destProperty) {
583 BMCWEB_LOG_DEBUG << "handle_get: " << objectPath << " prop:" << destProperty;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700584 std::shared_ptr<std::string> property_name =
Ed Tanousd76323e2018-08-07 14:35:40 -0700585 std::make_shared<std::string>(std::move(destProperty));
Ed Tanous75db20e2018-07-27 13:44:44 -0700586
587 std::shared_ptr<std::string> path =
Ed Tanousd76323e2018-08-07 14:35:40 -0700588 std::make_shared<std::string>(std::move(objectPath));
Ed Tanous75db20e2018-07-27 13:44:44 -0700589
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700590 using GetObjectType =
591 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700592 crow::connections::systemBus->async_method_call(
Ed Tanous75db20e2018-07-27 13:44:44 -0700593 [&res, path, property_name](const boost::system::error_code ec,
594 const GetObjectType &object_names) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700595 if (ec || object_names.size() <= 0) {
596 res.result(boost::beast::http::status::not_found);
597 res.end();
598 return;
599 }
600 std::shared_ptr<nlohmann::json> response =
601 std::make_shared<nlohmann::json>(nlohmann::json::object());
602 // The mapper should never give us an empty interface names list, but
603 // check anyway
604 for (const std::pair<std::string, std::vector<std::string>> connection :
605 object_names) {
606 const std::vector<std::string> &interfaceNames = connection.second;
607
608 if (interfaceNames.size() <= 0) {
609 res.result(boost::beast::http::status::not_found);
610 res.end();
611 return;
612 }
613
614 for (const std::string &interface : interfaceNames) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700615 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700616 [&res, response, property_name](
617 const boost::system::error_code ec,
618 const std::vector<std::pair<
619 std::string, DbusRestVariantType>> &properties) {
620 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700621 BMCWEB_LOG_ERROR << "Bad dbus request error: " << ec;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700622 } else {
623 for (const std::pair<std::string, DbusRestVariantType>
624 &property : properties) {
625 // if property name is empty, or matches our search query,
626 // add it to the response json
627
628 if (property_name->empty()) {
629 mapbox::util::apply_visitor(
630 [&response, &property](auto &&val) {
631 (*response)[property.first] = val;
632 },
633 property.second);
634 } else if (property.first == *property_name) {
635 mapbox::util::apply_visitor(
636 [&response](auto &&val) { (*response) = val; },
637 property.second);
638 }
639 }
640 }
641 if (response.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700642 res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700643 {"message", "200 OK"},
644 {"data", *response}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700645
646 res.end();
647 }
648 },
Ed Tanous75db20e2018-07-27 13:44:44 -0700649 connection.first, *path, "org.freedesktop.DBus.Properties",
650 "GetAll", interface);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700651 }
652 }
653 },
654 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous75db20e2018-07-27 13:44:44 -0700655 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700656 std::array<std::string, 0>());
657}
658
659struct AsyncPutRequest {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700660 AsyncPutRequest(crow::Response &res) : res(res) {
661 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700662 {"status", "ok"}, {"message", "200 OK"}, {"data", nullptr}};
663 }
664 ~AsyncPutRequest() {
665 if (res.result() == boost::beast::http::status::internal_server_error) {
666 // Reset the json object to clear out any data that made it in before the
667 // error happened
668 // todo(ed) handle error condition with proper code
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700669 res.jsonValue = nlohmann::json::object();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700670 }
671
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700672 if (res.jsonValue.empty()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700673 res.result(boost::beast::http::status::forbidden);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700674 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700675 {"status", "error"},
676 {"message", "403 Forbidden"},
677 {"data",
678 {{"message",
679 "The specified property cannot be created: " + propertyName}}}};
680 }
681
682 res.end();
683 }
684
685 void setErrorStatus() {
686 res.result(boost::beast::http::status::internal_server_error);
687 }
688
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700689 crow::Response &res;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700690 std::string objectPath;
691 std::string propertyName;
692 nlohmann::json propertyValue;
693};
694
Ed Tanousd76323e2018-08-07 14:35:40 -0700695void handlePut(const crow::Request &req, crow::Response &res,
696 const std::string &objectPath, const std::string &destProperty) {
697 nlohmann::json requestDbusData =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700698 nlohmann::json::parse(req.body, nullptr, false);
699
Ed Tanousd76323e2018-08-07 14:35:40 -0700700 if (requestDbusData.is_discarded()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700701 res.result(boost::beast::http::status::bad_request);
702 res.end();
703 return;
704 }
705
Ed Tanousd76323e2018-08-07 14:35:40 -0700706 nlohmann::json::const_iterator propertyIt = requestDbusData.find("data");
707 if (propertyIt == requestDbusData.end()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700708 res.result(boost::beast::http::status::bad_request);
709 res.end();
710 return;
711 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700712 const nlohmann::json &propertySetValue = *propertyIt;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700713 auto transaction = std::make_shared<AsyncPutRequest>(res);
714 transaction->objectPath = objectPath;
715 transaction->propertyName = destProperty;
716 transaction->propertyValue = propertySetValue;
717
718 using GetObjectType =
719 std::vector<std::pair<std::string, std::vector<std::string>>>;
720
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700721 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700722 [transaction](const boost::system::error_code ec,
723 const GetObjectType &object_names) {
724 if (!ec && object_names.size() <= 0) {
725 transaction->res.result(boost::beast::http::status::not_found);
726 return;
727 }
728
729 for (const std::pair<std::string, std::vector<std::string>> connection :
730 object_names) {
731 const std::string &connectionName = connection.first;
732
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700733 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700734 [ connectionName{std::string(connectionName)}, transaction ](
735 const boost::system::error_code ec,
736 const std::string &introspectXml) {
737 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700738 BMCWEB_LOG_ERROR
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700739 << "Introspect call failed with error: " << ec.message()
740 << " on process: " << connectionName;
741 transaction->setErrorStatus();
742 return;
743 }
744 tinyxml2::XMLDocument doc;
745
746 doc.Parse(introspectXml.c_str());
747 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
748 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700749 BMCWEB_LOG_ERROR << "XML document failed to parse: "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700750 << introspectXml;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700751 transaction->setErrorStatus();
752 return;
753 }
754 tinyxml2::XMLElement *ifaceNode =
755 pRoot->FirstChildElement("interface");
756 while (ifaceNode != nullptr) {
757 const char *interfaceName = ifaceNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700758 BMCWEB_LOG_DEBUG << "found interface " << interfaceName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700759 tinyxml2::XMLElement *propNode =
760 ifaceNode->FirstChildElement("property");
761 while (propNode != nullptr) {
762 const char *propertyName = propNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700763 BMCWEB_LOG_DEBUG << "Found property " << propertyName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700764 if (propertyName == transaction->propertyName) {
765 const char *argType = propNode->Attribute("type");
766 if (argType != nullptr) {
767 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700768 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700769 connectionName.c_str(),
770 transaction->objectPath.c_str(),
771 "org.freedesktop.DBus.Properties", "Set");
772 m.append(interfaceName, transaction->propertyName);
773 int r = sd_bus_message_open_container(
774 m.get(), SD_BUS_TYPE_VARIANT, argType);
775 if (r < 0) {
776 transaction->setErrorStatus();
777 return;
778 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700779 r = convertJsonToDbus(m.get(), argType,
780 transaction->propertyValue);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700781 if (r < 0) {
782 transaction->setErrorStatus();
783 return;
784 }
785 r = sd_bus_message_close_container(m.get());
786 if (r < 0) {
787 transaction->setErrorStatus();
788 return;
789 }
790
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700791 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700792 m, [transaction](boost::system::error_code ec,
793 sdbusplus::message::message &m) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700794 BMCWEB_LOG_DEBUG << "sent";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700795 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700796 transaction->res.jsonValue["status"] = "error";
797 transaction->res.jsonValue["message"] =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700798 ec.message();
799 }
800 });
801 }
802 }
803 propNode = propNode->NextSiblingElement("property");
804 }
805 ifaceNode = ifaceNode->NextSiblingElement("interface");
806 }
807 },
808 connectionName, transaction->objectPath,
809 "org.freedesktop.DBus.Introspectable", "Introspect");
810 }
811 },
812 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
813 "xyz.openbmc_project.ObjectMapper", "GetObject", transaction->objectPath,
814 std::array<std::string, 0>());
815}
816
Ed Tanous911ac312017-08-15 09:37:42 -0700817template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700818void requestRoutes(Crow<Middlewares...> &app) {
819 BMCWEB_ROUTE(app, "/bus/")
820 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
821 res.jsonValue = {{"busses", {{{"name", "system"}}}}, {"status", "ok"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700822 });
Ed Tanous911ac312017-08-15 09:37:42 -0700823
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700824 BMCWEB_ROUTE(app, "/bus/system/")
825 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
826
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700827 auto myCallback = [&res](const boost::system::error_code ec,
828 std::vector<std::string> &names) {
829 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700830 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
Ed Tanouse0d918b2018-03-27 17:41:04 -0700831 res.result(boost::beast::http::status::internal_server_error);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700832 } else {
833 std::sort(names.begin(), names.end());
834 nlohmann::json j{{"status", "ok"}};
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700835 auto &objectsSub = j["objects"];
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700836 for (auto &name : names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700837 objectsSub.push_back({{"name", name}});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700838 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700839 res.jsonValue = std::move(j);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700840 }
841 res.end();
842 };
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700843 crow::connections::systemBus->async_method_call(
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700844 std::move(myCallback), "org.freedesktop.DBus", "/",
845 "org.freedesktop.DBus", "ListNames");
Ed Tanous911ac312017-08-15 09:37:42 -0700846 });
847
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700848 BMCWEB_ROUTE(app, "/list/")
849 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700850 handle_list(res, "/");
Ed Tanousba9f9a62017-10-11 16:40:35 -0700851 });
852
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700853 BMCWEB_ROUTE(app, "/xyz/<path>")
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700854 .methods("GET"_method, "PUT"_method,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700855 "POST"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700856 const std::string &path) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700857 std::string objectPath = "/xyz/" + path;
Ed Tanousa4e18f22018-04-27 10:25:29 -0700858
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700859 // Trim any trailing "/" at the end
Ed Tanousd76323e2018-08-07 14:35:40 -0700860 if (boost::ends_with(objectPath, "/")) {
861 objectPath.pop_back();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700862 }
863
Ed Tanousd76323e2018-08-07 14:35:40 -0700864 // If accessing a single attribute, fill in and update objectPath,
865 // otherwise leave destProperty blank
866 std::string destProperty = "";
867 const char *attrSeperator = "/attr/";
868 size_t attrPosition = path.find(attrSeperator);
869 if (attrPosition != path.npos) {
870 objectPath = "/xyz/" + path.substr(0, attrPosition);
871 destProperty =
872 path.substr(attrPosition + strlen(attrSeperator), path.length());
shiyilei00b92f72017-11-12 16:21:16 +0800873 }
874
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700875 if (req.method() == "POST"_method) {
876 constexpr const char *action_seperator = "/action/";
877 size_t action_position = path.find(action_seperator);
878 if (action_position != path.npos) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700879 objectPath = "/xyz/" + path.substr(0, action_position);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700880 std::string post_property = path.substr(
881 (action_position + strlen(action_seperator)), path.length());
Ed Tanousd76323e2018-08-07 14:35:40 -0700882 handle_action(req, res, objectPath, post_property);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700883 return;
884 }
885 } else if (req.method() == "GET"_method) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700886 if (boost::ends_with(objectPath, "/enumerate")) {
887 objectPath.erase(objectPath.end() - 10, objectPath.end());
888 handle_enumerate(res, objectPath);
889 } else if (boost::ends_with(objectPath, "/list")) {
890 objectPath.erase(objectPath.end() - 5, objectPath.end());
891 handle_list(res, objectPath);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700892 } else {
Ed Tanousd76323e2018-08-07 14:35:40 -0700893 handle_get(res, objectPath, destProperty);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700894 }
895 return;
896 } else if (req.method() == "PUT"_method) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700897 handlePut(req, res, objectPath, destProperty);
Ed Tanous64530012018-02-06 17:08:16 -0800898 return;
899 }
900
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700901 res.result(boost::beast::http::status::method_not_allowed);
902 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -0700903 });
shiyilei00b92f72017-11-12 16:21:16 +0800904
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700905 BMCWEB_ROUTE(app, "/bus/system/<str>/")
906 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
907 const std::string &Connection) {
Ed Tanous64530012018-02-06 17:08:16 -0800908 std::shared_ptr<nlohmann::json> transaction;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700909 introspectObjects(res, Connection, "/", transaction);
Ed Tanous911ac312017-08-15 09:37:42 -0700910 });
911
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700912 BMCWEB_ROUTE(app, "/download/dump/<str>/")
913 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700914 const std::string &dumpId) {
915 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]+)$");
916 if (!std::regex_match(dumpId, validFilename)) {
917 res.result(boost::beast::http::status::not_found);
918 res.end();
919 return;
920 }
921 std::experimental::filesystem::path loc(
922 "/var/lib/phosphor-debug-collector/dumps");
923
924 loc += dumpId;
925
926 if (!std::experimental::filesystem::exists(loc) ||
927 !std::experimental::filesystem::is_directory(loc)) {
928 res.result(boost::beast::http::status::not_found);
929 res.end();
930 return;
931 }
932 std::experimental::filesystem::directory_iterator files(loc);
933 for (auto &file : files) {
934 std::ifstream readFile(file.path());
935 if (readFile.good()) {
936 continue;
937 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700938 res.addHeader("Content-Type", "application/octet-stream");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700939 res.body() = {std::istreambuf_iterator<char>(readFile),
940 std::istreambuf_iterator<char>()};
941 res.end();
942 }
943 res.result(boost::beast::http::status::not_found);
944 res.end();
945 return;
946 });
947
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700948 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
949 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
950 const std::string &processName,
951 const std::string &requestedPath) {
Ed Tanous911ac312017-08-15 09:37:42 -0700952 std::vector<std::string> strs;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700953 boost::split(strs, requestedPath, boost::is_any_of("/"));
954 std::string objectPath;
955 std::string interfaceName;
956 std::string methodName;
Ed Tanous911ac312017-08-15 09:37:42 -0700957 auto it = strs.begin();
958 if (it == strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700959 objectPath = "/";
Ed Tanous911ac312017-08-15 09:37:42 -0700960 }
961 while (it != strs.end()) {
962 // Check if segment contains ".". If it does, it must be an
963 // interface
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700964 if (it->find(".") != std::string::npos) {
Ed Tanous911ac312017-08-15 09:37:42 -0700965 break;
Ed Tanousba9f9a62017-10-11 16:40:35 -0700966 // THis check is neccesary as the trailing slash gets parsed as
Ed Tanous64530012018-02-06 17:08:16 -0800967 // part of our <path> specifier above, which causes the normal
968 // trailing backslash redirector to fail.
Ed Tanous911ac312017-08-15 09:37:42 -0700969 } else if (!it->empty()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700970 objectPath += "/" + *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700971 }
972 it++;
973 }
974 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700975 interfaceName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700976 it++;
977
978 // after interface, we might have a method name
979 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700980 methodName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700981 it++;
982 }
983 }
984 if (it != strs.end()) {
985 // if there is more levels past the method name, something went
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700986 // wrong, return not found
Ed Tanouse0d918b2018-03-27 17:41:04 -0700987 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -0700988 res.end();
989 return;
990 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700991 if (interfaceName.empty()) {
992 crow::connections::systemBus->async_method_call(
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700993 [&, processName, objectPath](const boost::system::error_code ec,
994 const std::string &introspect_xml) {
Ed Tanous911ac312017-08-15 09:37:42 -0700995 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700996 BMCWEB_LOG_ERROR
Ed Tanous911ac312017-08-15 09:37:42 -0700997 << "Introspect call failed with error: " << ec.message()
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700998 << " on process: " << processName
999 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -07001000
1001 } else {
1002 tinyxml2::XMLDocument doc;
1003
1004 doc.Parse(introspect_xml.c_str());
1005 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1006 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001007 BMCWEB_LOG_ERROR << "XML document failed to parse "
1008 << processName << " " << objectPath
1009 << "\n";
1010 res.jsonValue = {{"status", "XML parse error"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -07001011 res.result(
1012 boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -07001013 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001014 nlohmann::json interfacesArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001015 tinyxml2::XMLElement *interface =
1016 pRoot->FirstChildElement("interface");
1017
1018 while (interface != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001019 std::string ifaceName = interface->Attribute("name");
1020 interfacesArray.push_back({{"name", ifaceName}});
Ed Tanous911ac312017-08-15 09:37:42 -07001021
1022 interface = interface->NextSiblingElement("interface");
1023 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001024 res.jsonValue = {{"status", "ok"},
1025 {"bus_name", processName},
1026 {"interfaces", interfacesArray},
Ed Tanousd76323e2018-08-07 14:35:40 -07001027 {"objectPath", objectPath}};
Ed Tanous911ac312017-08-15 09:37:42 -07001028 }
1029 }
1030 res.end();
1031 },
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001032 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanousaa2e59c2018-04-12 12:17:20 -07001033 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -07001034 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001035 crow::connections::systemBus->async_method_call(
Ed Tanous911ac312017-08-15 09:37:42 -07001036 [
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001037 &, processName, objectPath,
1038 interface_name{std::move(interfaceName)}
Ed Tanous911ac312017-08-15 09:37:42 -07001039 ](const boost::system::error_code ec,
1040 const std::string &introspect_xml) {
1041 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001042 BMCWEB_LOG_ERROR
Ed Tanous911ac312017-08-15 09:37:42 -07001043 << "Introspect call failed with error: " << ec.message()
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001044 << " on process: " << processName
1045 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -07001046
1047 } else {
1048 tinyxml2::XMLDocument doc;
1049
1050 doc.Parse(introspect_xml.c_str());
1051 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1052 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001053 BMCWEB_LOG_ERROR << "XML document failed to parse "
1054 << processName << " " << objectPath
1055 << "\n";
Ed Tanouse0d918b2018-03-27 17:41:04 -07001056 res.result(
1057 boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -07001058
1059 } else {
1060 tinyxml2::XMLElement *node =
1061 pRoot->FirstChildElement("node");
1062
1063 // if we know we're the only call, build the json directly
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001064 nlohmann::json methodsArray = nlohmann::json::array();
1065 nlohmann::json signalsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001066 tinyxml2::XMLElement *interface =
1067 pRoot->FirstChildElement("interface");
1068
1069 while (interface != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001070 std::string ifaceName = interface->Attribute("name");
Ed Tanous911ac312017-08-15 09:37:42 -07001071
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001072 if (ifaceName == interfaceName) {
Ed Tanous911ac312017-08-15 09:37:42 -07001073 tinyxml2::XMLElement *methods =
1074 interface->FirstChildElement("method");
1075 while (methods != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001076 nlohmann::json argsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001077 tinyxml2::XMLElement *arg =
1078 methods->FirstChildElement("arg");
1079 while (arg != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001080 argsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001081 {{"name", arg->Attribute("name")},
1082 {"type", arg->Attribute("type")},
1083 {"direction", arg->Attribute("direction")}});
1084 arg = arg->NextSiblingElement("arg");
1085 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001086 methodsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001087 {{"name", methods->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001088 {"uri", "/bus/system/" + processName +
1089 objectPath + "/" + interfaceName +
Ed Tanous64530012018-02-06 17:08:16 -08001090 "/" + methods->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001091 {"args", argsArray}});
Ed Tanous911ac312017-08-15 09:37:42 -07001092 methods = methods->NextSiblingElement("method");
1093 }
1094 tinyxml2::XMLElement *signals =
1095 interface->FirstChildElement("signal");
1096 while (signals != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001097 nlohmann::json argsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001098
1099 tinyxml2::XMLElement *arg =
1100 signals->FirstChildElement("arg");
1101 while (arg != nullptr) {
1102 std::string name = arg->Attribute("name");
1103 std::string type = arg->Attribute("type");
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001104 argsArray.push_back({
Ed Tanous64530012018-02-06 17:08:16 -08001105 {"name", name},
1106 {"type", type},
Ed Tanous911ac312017-08-15 09:37:42 -07001107 });
1108 arg = arg->NextSiblingElement("arg");
1109 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001110 signalsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001111 {{"name", signals->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001112 {"args", argsArray}});
Ed Tanous911ac312017-08-15 09:37:42 -07001113 signals = signals->NextSiblingElement("signal");
1114 }
1115
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001116 res.jsonValue = {
Ed Tanous911ac312017-08-15 09:37:42 -07001117 {"status", "ok"},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001118 {"bus_name", processName},
1119 {"interface", interfaceName},
1120 {"methods", methodsArray},
Ed Tanousd76323e2018-08-07 14:35:40 -07001121 {"objectPath", objectPath},
Ed Tanous911ac312017-08-15 09:37:42 -07001122 {"properties", nlohmann::json::object()},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001123 {"signals", signalsArray}};
Ed Tanous911ac312017-08-15 09:37:42 -07001124
Ed Tanous911ac312017-08-15 09:37:42 -07001125 break;
1126 }
1127
1128 interface = interface->NextSiblingElement("interface");
1129 }
1130 if (interface == nullptr) {
1131 // if we got to the end of the list and never found a
1132 // match, throw 404
Ed Tanouse0d918b2018-03-27 17:41:04 -07001133 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -07001134 }
1135 }
1136 }
1137 res.end();
1138 },
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001139 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanousaa2e59c2018-04-12 12:17:20 -07001140 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -07001141 }
Ed Tanous911ac312017-08-15 09:37:42 -07001142 });
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001143}
Ed Tanous911ac312017-08-15 09:37:42 -07001144} // namespace openbmc_mapper
1145} // namespace crow