blob: 8ab1b6a4ad6358da146419677a13301c5aaa112f [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;
Ed Tanousd76323e2018-08-07 14:35:40 -0700148 std::string methodName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700149 nlohmann::json arguments;
150};
151
Ed Tanousd76323e2018-08-07 14:35:40 -0700152std::vector<std::string> dbusArgSplit(const std::string &string) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700153 std::vector<std::string> ret;
154 if (string.empty()) {
155 return ret;
156 }
157 ret.push_back("");
Ed Tanousd76323e2018-08-07 14:35:40 -0700158 int containerDepth = 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 Tanousd76323e2018-08-07 14:35:40 -0700168 containerDepth++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700169 break;
170 case ('}'):
171 case (')'):
Ed Tanousd76323e2018-08-07 14:35:40 -0700172 containerDepth--;
173 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 default:
Ed Tanousd76323e2018-08-07 14:35:40 -0700180 if (containerDepth == 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
Ed Tanousd76323e2018-08-07 14:35:40 -0700190int convertJsonToDbus(sd_bus_message *m, const std::string &arg_type,
191 const nlohmann::json &input_json) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700192 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 Tanousd76323e2018-08-07 14:35:40 -0700195 const std::vector<std::string> argTypes = dbusArgSplit(arg_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700196
197 // Assume a single object for now.
198 const nlohmann::json *j = &input_json;
Ed Tanousd76323e2018-08-07 14:35:40 -0700199 nlohmann::json::const_iterator jIt = input_json.begin();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700200
Ed Tanousd76323e2018-08-07 14:35:40 -0700201 for (const std::string &arg_code : argTypes) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700202 // If we are decoding multiple objects, grab the pointer to the iterator,
203 // and increment it for the next loop
Ed Tanousd76323e2018-08-07 14:35:40 -0700204 if (argTypes.size() > 1) {
205 if (jIt == input_json.end()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700206 return -2;
207 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700208 j = &*jIt;
209 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700210 }
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) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700320 r = convertJsonToDbus(m, contained_type, *it);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700321 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
Ed Tanousd76323e2018-08-07 14:35:40 -0700338 r = convertJsonToDbus(m, contained_type, input_json);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700339 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();
Ed Tanousd76323e2018-08-07 14:35:40 -0700353 for (const std::string &arg_code : dbusArgSplit(arg_type)) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700354 if (it == j->end()) {
355 return -1;
356 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700357 r = convertJsonToDbus(m, arg_code, *it);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700358 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());
Ed Tanousd76323e2018-08-07 14:35:40 -0700369 std::vector<std::string> codes = dbusArgSplit(contained_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700370 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()) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700376 r = convertJsonToDbus(m, key_type, it.key());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700377 if (r < 0) {
378 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700379 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700380
Ed Tanousd76323e2018-08-07 14:35:40 -0700381 r = convertJsonToDbus(m, value_type, it.value());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700382 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
Ed Tanousd76323e2018-08-07 14:35:40 -0700394 if (argTypes.size() > 1) {
395 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700396 }
397 }
398}
399
Ed Tanousd76323e2018-08-07 14:35:40 -0700400void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction,
401 const std::string &connectionName) {
402 BMCWEB_LOG_DEBUG << "findActionOnInterface for connection " << connectionName;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700403 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700404 [
405 transaction, connectionName{std::string(connectionName)}
406 ](const boost::system::error_code ec, const std::string &introspect_xml) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700407 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700408 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700409 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700410 << ec.message() << " on process: " << connectionName
411 << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700412 } else {
413 tinyxml2::XMLDocument doc;
414
415 doc.Parse(introspect_xml.c_str());
416 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
417 if (pRoot == nullptr) {
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700418 BMCWEB_LOG_ERROR << "XML document failed to parse "
419 << connectionName << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700420
421 } else {
422 tinyxml2::XMLElement *interface_node =
423 pRoot->FirstChildElement("interface");
424 while (interface_node != nullptr) {
425 std::string this_interface_name =
426 interface_node->Attribute("name");
427 tinyxml2::XMLElement *method_node =
428 interface_node->FirstChildElement("method");
429 while (method_node != nullptr) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700430 std::string this_methodName = method_node->Attribute("name");
431 BMCWEB_LOG_DEBUG << "Found method: " << this_methodName;
432 if (this_methodName == transaction->methodName) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700433 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700434 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700435 connectionName.c_str(), transaction->path.c_str(),
436 this_interface_name.c_str(),
Ed Tanousd76323e2018-08-07 14:35:40 -0700437 transaction->methodName.c_str());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700438
439 tinyxml2::XMLElement *argument_node =
440 method_node->FirstChildElement("arg");
441
442 nlohmann::json::const_iterator arg_it =
443 transaction->arguments.begin();
444
445 while (argument_node != nullptr) {
446 std::string arg_direction =
447 argument_node->Attribute("direction");
448 if (arg_direction == "in") {
449 std::string arg_type = argument_node->Attribute("type");
450 if (arg_it == transaction->arguments.end()) {
451 transaction->setErrorStatus();
452 return;
453 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700454 if (convertJsonToDbus(m.get(), arg_type, *arg_it) < 0) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700455 transaction->setErrorStatus();
456 return;
457 }
458
459 arg_it++;
460 }
461 argument_node = method_node->NextSiblingElement("arg");
462 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700463 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700464 m, [transaction](boost::system::error_code ec,
465 sdbusplus::message::message &m) {
466 if (ec) {
467 transaction->setErrorStatus();
468 return;
469 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700470 transaction->res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700471 {"message", "200 OK"},
472 {"data", nullptr}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700473 });
474 break;
475 }
476 method_node = method_node->NextSiblingElement("method");
477 }
478 interface_node = interface_node->NextSiblingElement("interface");
479 }
480 }
481 }
482 },
483 connectionName, transaction->path, "org.freedesktop.DBus.Introspectable",
484 "Introspect");
485}
486
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700487void handle_action(const crow::Request &req, crow::Response &res,
Ed Tanousd76323e2018-08-07 14:35:40 -0700488 const std::string &objectPath,
489 const std::string &methodName) {
490 nlohmann::json requestDbusData =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700491 nlohmann::json::parse(req.body, nullptr, false);
492
Ed Tanousd76323e2018-08-07 14:35:40 -0700493 if (requestDbusData.is_discarded()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700494 res.result(boost::beast::http::status::bad_request);
495 res.end();
496 return;
497 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700498 if (!requestDbusData.is_array()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700499 res.result(boost::beast::http::status::bad_request);
500 res.end();
501 return;
502 }
503 auto transaction = std::make_shared<InProgressActionData>(res);
504
Ed Tanousd76323e2018-08-07 14:35:40 -0700505 transaction->path = objectPath;
506 transaction->methodName = methodName;
507 transaction->arguments = std::move(requestDbusData);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700508 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700509 [transaction](
510 const boost::system::error_code ec,
511 const std::vector<std::pair<std::string, std::vector<std::string>>>
512 &interface_names) {
513 if (ec || interface_names.size() <= 0) {
514 transaction->setErrorStatus();
515 return;
516 }
517
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700518 BMCWEB_LOG_DEBUG << "GetObject returned objects "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700519 << interface_names.size();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700520
521 for (const std::pair<std::string, std::vector<std::string>> &object :
522 interface_names) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700523 findActionOnInterface(transaction, object.first);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700524 }
525 },
526 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanousd76323e2018-08-07 14:35:40 -0700527 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700528 std::array<std::string, 0>());
529}
530
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700531void handle_list(crow::Response &res, const std::string &objectPath) {
532 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700533 [&res](const boost::system::error_code ec,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700534 std::vector<std::string> &objectPaths) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700535 if (ec) {
536 res.result(boost::beast::http::status::internal_server_error);
537 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700538 res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700539 {"message", "200 OK"},
540 {"data", std::move(objectPaths)}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700541 }
542 res.end();
543 },
544 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700545 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700546 static_cast<int32_t>(99), std::array<std::string, 0>());
547}
548
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700549void handle_enumerate(crow::Response &res, const std::string &objectPath) {
550 crow::connections::systemBus->async_method_call(
551 [&res, objectPath{std::string(objectPath)} ](
Ed Tanous64530012018-02-06 17:08:16 -0800552 const boost::system::error_code ec,
553 const GetSubTreeType &object_names) {
554 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700555 res.jsonValue = {{"message", "200 OK"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700556 {"status", "ok"},
557 {"data", nlohmann::json::object()}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700558
Ed Tanous64530012018-02-06 17:08:16 -0800559 res.end();
560 return;
561 }
562
563 boost::container::flat_set<std::string> connections;
564
565 for (const auto &object : object_names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700566 for (const auto &Connection : object.second) {
567 connections.insert(Connection.first);
Ed Tanous64530012018-02-06 17:08:16 -0800568 }
569 }
570
571 if (connections.size() <= 0) {
Ed Tanouse0d918b2018-03-27 17:41:04 -0700572 res.result(boost::beast::http::status::not_found);
Ed Tanous64530012018-02-06 17:08:16 -0800573 res.end();
574 return;
575 }
576 auto transaction =
577 std::make_shared<nlohmann::json>(nlohmann::json::object());
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700578 for (const std::string &Connection : connections) {
579 getManagedObjectsForEnumerate(objectPath, Connection, res,
580 transaction);
Ed Tanous64530012018-02-06 17:08:16 -0800581 }
Ed Tanous64530012018-02-06 17:08:16 -0800582 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700583 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700584 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, (int32_t)0,
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700585 std::array<std::string, 0>());
Ed Tanous64530012018-02-06 17:08:16 -0800586}
Ed Tanous911ac312017-08-15 09:37:42 -0700587
Ed Tanousd76323e2018-08-07 14:35:40 -0700588void handle_get(crow::Response &res, std::string &objectPath,
589 std::string &destProperty) {
590 BMCWEB_LOG_DEBUG << "handle_get: " << objectPath << " prop:" << destProperty;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700591 std::shared_ptr<std::string> property_name =
Ed Tanousd76323e2018-08-07 14:35:40 -0700592 std::make_shared<std::string>(std::move(destProperty));
Ed Tanous75db20e2018-07-27 13:44:44 -0700593
594 std::shared_ptr<std::string> path =
Ed Tanousd76323e2018-08-07 14:35:40 -0700595 std::make_shared<std::string>(std::move(objectPath));
Ed Tanous75db20e2018-07-27 13:44:44 -0700596
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700597 using GetObjectType =
598 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700599 crow::connections::systemBus->async_method_call(
Ed Tanous75db20e2018-07-27 13:44:44 -0700600 [&res, path, property_name](const boost::system::error_code ec,
601 const GetObjectType &object_names) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700602 if (ec || object_names.size() <= 0) {
603 res.result(boost::beast::http::status::not_found);
604 res.end();
605 return;
606 }
607 std::shared_ptr<nlohmann::json> response =
608 std::make_shared<nlohmann::json>(nlohmann::json::object());
609 // The mapper should never give us an empty interface names list, but
610 // check anyway
611 for (const std::pair<std::string, std::vector<std::string>> connection :
612 object_names) {
613 const std::vector<std::string> &interfaceNames = connection.second;
614
615 if (interfaceNames.size() <= 0) {
616 res.result(boost::beast::http::status::not_found);
617 res.end();
618 return;
619 }
620
621 for (const std::string &interface : interfaceNames) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700622 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700623 [&res, response, property_name](
624 const boost::system::error_code ec,
625 const std::vector<std::pair<
626 std::string, DbusRestVariantType>> &properties) {
627 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700628 BMCWEB_LOG_ERROR << "Bad dbus request error: " << ec;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700629 } else {
630 for (const std::pair<std::string, DbusRestVariantType>
631 &property : properties) {
632 // if property name is empty, or matches our search query,
633 // add it to the response json
634
635 if (property_name->empty()) {
636 mapbox::util::apply_visitor(
637 [&response, &property](auto &&val) {
638 (*response)[property.first] = val;
639 },
640 property.second);
641 } else if (property.first == *property_name) {
642 mapbox::util::apply_visitor(
643 [&response](auto &&val) { (*response) = val; },
644 property.second);
645 }
646 }
647 }
648 if (response.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700649 res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700650 {"message", "200 OK"},
651 {"data", *response}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700652
653 res.end();
654 }
655 },
Ed Tanous75db20e2018-07-27 13:44:44 -0700656 connection.first, *path, "org.freedesktop.DBus.Properties",
657 "GetAll", interface);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700658 }
659 }
660 },
661 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous75db20e2018-07-27 13:44:44 -0700662 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700663 std::array<std::string, 0>());
664}
665
666struct AsyncPutRequest {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700667 AsyncPutRequest(crow::Response &res) : res(res) {
668 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700669 {"status", "ok"}, {"message", "200 OK"}, {"data", nullptr}};
670 }
671 ~AsyncPutRequest() {
672 if (res.result() == boost::beast::http::status::internal_server_error) {
673 // Reset the json object to clear out any data that made it in before the
674 // error happened
675 // todo(ed) handle error condition with proper code
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700676 res.jsonValue = nlohmann::json::object();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700677 }
678
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700679 if (res.jsonValue.empty()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700680 res.result(boost::beast::http::status::forbidden);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700681 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700682 {"status", "error"},
683 {"message", "403 Forbidden"},
684 {"data",
685 {{"message",
686 "The specified property cannot be created: " + propertyName}}}};
687 }
688
689 res.end();
690 }
691
692 void setErrorStatus() {
693 res.result(boost::beast::http::status::internal_server_error);
694 }
695
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700696 crow::Response &res;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700697 std::string objectPath;
698 std::string propertyName;
699 nlohmann::json propertyValue;
700};
701
Ed Tanousd76323e2018-08-07 14:35:40 -0700702void handlePut(const crow::Request &req, crow::Response &res,
703 const std::string &objectPath, const std::string &destProperty) {
704 nlohmann::json requestDbusData =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700705 nlohmann::json::parse(req.body, nullptr, false);
706
Ed Tanousd76323e2018-08-07 14:35:40 -0700707 if (requestDbusData.is_discarded()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700708 res.result(boost::beast::http::status::bad_request);
709 res.end();
710 return;
711 }
712
Ed Tanousd76323e2018-08-07 14:35:40 -0700713 nlohmann::json::const_iterator propertyIt = requestDbusData.find("data");
714 if (propertyIt == requestDbusData.end()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700715 res.result(boost::beast::http::status::bad_request);
716 res.end();
717 return;
718 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700719 const nlohmann::json &propertySetValue = *propertyIt;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700720 auto transaction = std::make_shared<AsyncPutRequest>(res);
721 transaction->objectPath = objectPath;
722 transaction->propertyName = destProperty;
723 transaction->propertyValue = propertySetValue;
724
725 using GetObjectType =
726 std::vector<std::pair<std::string, std::vector<std::string>>>;
727
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700728 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700729 [transaction](const boost::system::error_code ec,
730 const GetObjectType &object_names) {
731 if (!ec && object_names.size() <= 0) {
732 transaction->res.result(boost::beast::http::status::not_found);
733 return;
734 }
735
736 for (const std::pair<std::string, std::vector<std::string>> connection :
737 object_names) {
738 const std::string &connectionName = connection.first;
739
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700740 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700741 [ connectionName{std::string(connectionName)}, transaction ](
742 const boost::system::error_code ec,
743 const std::string &introspectXml) {
744 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700745 BMCWEB_LOG_ERROR
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700746 << "Introspect call failed with error: " << ec.message()
747 << " on process: " << connectionName;
748 transaction->setErrorStatus();
749 return;
750 }
751 tinyxml2::XMLDocument doc;
752
753 doc.Parse(introspectXml.c_str());
754 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
755 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700756 BMCWEB_LOG_ERROR << "XML document failed to parse: "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700757 << introspectXml;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700758 transaction->setErrorStatus();
759 return;
760 }
761 tinyxml2::XMLElement *ifaceNode =
762 pRoot->FirstChildElement("interface");
763 while (ifaceNode != nullptr) {
764 const char *interfaceName = ifaceNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700765 BMCWEB_LOG_DEBUG << "found interface " << interfaceName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700766 tinyxml2::XMLElement *propNode =
767 ifaceNode->FirstChildElement("property");
768 while (propNode != nullptr) {
769 const char *propertyName = propNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700770 BMCWEB_LOG_DEBUG << "Found property " << propertyName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700771 if (propertyName == transaction->propertyName) {
772 const char *argType = propNode->Attribute("type");
773 if (argType != nullptr) {
774 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700775 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700776 connectionName.c_str(),
777 transaction->objectPath.c_str(),
778 "org.freedesktop.DBus.Properties", "Set");
779 m.append(interfaceName, transaction->propertyName);
780 int r = sd_bus_message_open_container(
781 m.get(), SD_BUS_TYPE_VARIANT, argType);
782 if (r < 0) {
783 transaction->setErrorStatus();
784 return;
785 }
Ed Tanousd76323e2018-08-07 14:35:40 -0700786 r = convertJsonToDbus(m.get(), argType,
787 transaction->propertyValue);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700788 if (r < 0) {
789 transaction->setErrorStatus();
790 return;
791 }
792 r = sd_bus_message_close_container(m.get());
793 if (r < 0) {
794 transaction->setErrorStatus();
795 return;
796 }
797
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700798 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700799 m, [transaction](boost::system::error_code ec,
800 sdbusplus::message::message &m) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700801 BMCWEB_LOG_DEBUG << "sent";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700802 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700803 transaction->res.jsonValue["status"] = "error";
804 transaction->res.jsonValue["message"] =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700805 ec.message();
806 }
807 });
808 }
809 }
810 propNode = propNode->NextSiblingElement("property");
811 }
812 ifaceNode = ifaceNode->NextSiblingElement("interface");
813 }
814 },
815 connectionName, transaction->objectPath,
816 "org.freedesktop.DBus.Introspectable", "Introspect");
817 }
818 },
819 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
820 "xyz.openbmc_project.ObjectMapper", "GetObject", transaction->objectPath,
821 std::array<std::string, 0>());
822}
823
Ed Tanous911ac312017-08-15 09:37:42 -0700824template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700825void requestRoutes(Crow<Middlewares...> &app) {
826 BMCWEB_ROUTE(app, "/bus/")
827 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
828 res.jsonValue = {{"busses", {{{"name", "system"}}}}, {"status", "ok"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700829 });
Ed Tanous911ac312017-08-15 09:37:42 -0700830
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700831 BMCWEB_ROUTE(app, "/bus/system/")
832 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
833
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700834 auto myCallback = [&res](const boost::system::error_code ec,
835 std::vector<std::string> &names) {
836 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700837 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
Ed Tanouse0d918b2018-03-27 17:41:04 -0700838 res.result(boost::beast::http::status::internal_server_error);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700839 } else {
840 std::sort(names.begin(), names.end());
841 nlohmann::json j{{"status", "ok"}};
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700842 auto &objectsSub = j["objects"];
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700843 for (auto &name : names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700844 objectsSub.push_back({{"name", name}});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700845 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700846 res.jsonValue = std::move(j);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700847 }
848 res.end();
849 };
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700850 crow::connections::systemBus->async_method_call(
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700851 std::move(myCallback), "org.freedesktop.DBus", "/",
852 "org.freedesktop.DBus", "ListNames");
Ed Tanous911ac312017-08-15 09:37:42 -0700853 });
854
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700855 BMCWEB_ROUTE(app, "/list/")
856 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700857 handle_list(res, "/");
Ed Tanousba9f9a62017-10-11 16:40:35 -0700858 });
859
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700860 BMCWEB_ROUTE(app, "/xyz/<path>")
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700861 .methods("GET"_method, "PUT"_method,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700862 "POST"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700863 const std::string &path) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700864 std::string objectPath = "/xyz/" + path;
Ed Tanousa4e18f22018-04-27 10:25:29 -0700865
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700866 // Trim any trailing "/" at the end
Ed Tanousd76323e2018-08-07 14:35:40 -0700867 if (boost::ends_with(objectPath, "/")) {
868 objectPath.pop_back();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700869 }
870
Ed Tanousd76323e2018-08-07 14:35:40 -0700871 // If accessing a single attribute, fill in and update objectPath,
872 // otherwise leave destProperty blank
873 std::string destProperty = "";
874 const char *attrSeperator = "/attr/";
875 size_t attrPosition = path.find(attrSeperator);
876 if (attrPosition != path.npos) {
877 objectPath = "/xyz/" + path.substr(0, attrPosition);
878 destProperty =
879 path.substr(attrPosition + strlen(attrSeperator), path.length());
shiyilei00b92f72017-11-12 16:21:16 +0800880 }
881
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700882 if (req.method() == "POST"_method) {
883 constexpr const char *action_seperator = "/action/";
884 size_t action_position = path.find(action_seperator);
885 if (action_position != path.npos) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700886 objectPath = "/xyz/" + path.substr(0, action_position);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700887 std::string post_property = path.substr(
888 (action_position + strlen(action_seperator)), path.length());
Ed Tanousd76323e2018-08-07 14:35:40 -0700889 handle_action(req, res, objectPath, post_property);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700890 return;
891 }
892 } else if (req.method() == "GET"_method) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700893 if (boost::ends_with(objectPath, "/enumerate")) {
894 objectPath.erase(objectPath.end() - 10, objectPath.end());
895 handle_enumerate(res, objectPath);
896 } else if (boost::ends_with(objectPath, "/list")) {
897 objectPath.erase(objectPath.end() - 5, objectPath.end());
898 handle_list(res, objectPath);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700899 } else {
Ed Tanousd76323e2018-08-07 14:35:40 -0700900 handle_get(res, objectPath, destProperty);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700901 }
902 return;
903 } else if (req.method() == "PUT"_method) {
Ed Tanousd76323e2018-08-07 14:35:40 -0700904 handlePut(req, res, objectPath, destProperty);
Ed Tanous64530012018-02-06 17:08:16 -0800905 return;
906 }
907
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700908 res.result(boost::beast::http::status::method_not_allowed);
909 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -0700910 });
shiyilei00b92f72017-11-12 16:21:16 +0800911
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700912 BMCWEB_ROUTE(app, "/bus/system/<str>/")
913 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
914 const std::string &Connection) {
Ed Tanous64530012018-02-06 17:08:16 -0800915 std::shared_ptr<nlohmann::json> transaction;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700916 introspectObjects(res, Connection, "/", transaction);
Ed Tanous911ac312017-08-15 09:37:42 -0700917 });
918
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700919 BMCWEB_ROUTE(app, "/download/dump/<str>/")
920 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700921 const std::string &dumpId) {
922 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]+)$");
923 if (!std::regex_match(dumpId, validFilename)) {
924 res.result(boost::beast::http::status::not_found);
925 res.end();
926 return;
927 }
928 std::experimental::filesystem::path loc(
929 "/var/lib/phosphor-debug-collector/dumps");
930
931 loc += dumpId;
932
933 if (!std::experimental::filesystem::exists(loc) ||
934 !std::experimental::filesystem::is_directory(loc)) {
935 res.result(boost::beast::http::status::not_found);
936 res.end();
937 return;
938 }
939 std::experimental::filesystem::directory_iterator files(loc);
940 for (auto &file : files) {
941 std::ifstream readFile(file.path());
942 if (readFile.good()) {
943 continue;
944 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700945 res.addHeader("Content-Type", "application/octet-stream");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700946 res.body() = {std::istreambuf_iterator<char>(readFile),
947 std::istreambuf_iterator<char>()};
948 res.end();
949 }
950 res.result(boost::beast::http::status::not_found);
951 res.end();
952 return;
953 });
954
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700955 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
956 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
957 const std::string &processName,
958 const std::string &requestedPath) {
Ed Tanous911ac312017-08-15 09:37:42 -0700959 std::vector<std::string> strs;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700960 boost::split(strs, requestedPath, boost::is_any_of("/"));
961 std::string objectPath;
962 std::string interfaceName;
963 std::string methodName;
Ed Tanous911ac312017-08-15 09:37:42 -0700964 auto it = strs.begin();
965 if (it == strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700966 objectPath = "/";
Ed Tanous911ac312017-08-15 09:37:42 -0700967 }
968 while (it != strs.end()) {
969 // Check if segment contains ".". If it does, it must be an
970 // interface
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700971 if (it->find(".") != std::string::npos) {
Ed Tanous911ac312017-08-15 09:37:42 -0700972 break;
Ed Tanousba9f9a62017-10-11 16:40:35 -0700973 // THis check is neccesary as the trailing slash gets parsed as
Ed Tanous64530012018-02-06 17:08:16 -0800974 // part of our <path> specifier above, which causes the normal
975 // trailing backslash redirector to fail.
Ed Tanous911ac312017-08-15 09:37:42 -0700976 } else if (!it->empty()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700977 objectPath += "/" + *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700978 }
979 it++;
980 }
981 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700982 interfaceName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700983 it++;
984
985 // after interface, we might have a method name
986 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700987 methodName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700988 it++;
989 }
990 }
991 if (it != strs.end()) {
992 // if there is more levels past the method name, something went
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700993 // wrong, return not found
Ed Tanouse0d918b2018-03-27 17:41:04 -0700994 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -0700995 res.end();
996 return;
997 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700998 if (interfaceName.empty()) {
999 crow::connections::systemBus->async_method_call(
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07001000 [&, processName, objectPath](const boost::system::error_code ec,
1001 const std::string &introspect_xml) {
Ed Tanous911ac312017-08-15 09:37:42 -07001002 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001003 BMCWEB_LOG_ERROR
Ed Tanous911ac312017-08-15 09:37:42 -07001004 << "Introspect call failed with error: " << ec.message()
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001005 << " on process: " << processName
1006 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -07001007
1008 } else {
1009 tinyxml2::XMLDocument doc;
1010
1011 doc.Parse(introspect_xml.c_str());
1012 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1013 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001014 BMCWEB_LOG_ERROR << "XML document failed to parse "
1015 << processName << " " << objectPath
1016 << "\n";
1017 res.jsonValue = {{"status", "XML parse error"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -07001018 res.result(
1019 boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -07001020 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001021 nlohmann::json interfacesArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001022 tinyxml2::XMLElement *interface =
1023 pRoot->FirstChildElement("interface");
1024
1025 while (interface != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001026 std::string ifaceName = interface->Attribute("name");
1027 interfacesArray.push_back({{"name", ifaceName}});
Ed Tanous911ac312017-08-15 09:37:42 -07001028
1029 interface = interface->NextSiblingElement("interface");
1030 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001031 res.jsonValue = {{"status", "ok"},
1032 {"bus_name", processName},
1033 {"interfaces", interfacesArray},
Ed Tanousd76323e2018-08-07 14:35:40 -07001034 {"objectPath", objectPath}};
Ed Tanous911ac312017-08-15 09:37:42 -07001035 }
1036 }
1037 res.end();
1038 },
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001039 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanousaa2e59c2018-04-12 12:17:20 -07001040 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -07001041 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001042 crow::connections::systemBus->async_method_call(
Ed Tanous911ac312017-08-15 09:37:42 -07001043 [
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001044 &, processName, objectPath,
1045 interface_name{std::move(interfaceName)}
Ed Tanous911ac312017-08-15 09:37:42 -07001046 ](const boost::system::error_code ec,
1047 const std::string &introspect_xml) {
1048 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001049 BMCWEB_LOG_ERROR
Ed Tanous911ac312017-08-15 09:37:42 -07001050 << "Introspect call failed with error: " << ec.message()
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001051 << " on process: " << processName
1052 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -07001053
1054 } else {
1055 tinyxml2::XMLDocument doc;
1056
1057 doc.Parse(introspect_xml.c_str());
1058 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1059 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001060 BMCWEB_LOG_ERROR << "XML document failed to parse "
1061 << processName << " " << objectPath
1062 << "\n";
Ed Tanouse0d918b2018-03-27 17:41:04 -07001063 res.result(
1064 boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -07001065
1066 } else {
1067 tinyxml2::XMLElement *node =
1068 pRoot->FirstChildElement("node");
1069
1070 // if we know we're the only call, build the json directly
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001071 nlohmann::json methodsArray = nlohmann::json::array();
1072 nlohmann::json signalsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001073 tinyxml2::XMLElement *interface =
1074 pRoot->FirstChildElement("interface");
1075
1076 while (interface != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001077 std::string ifaceName = interface->Attribute("name");
Ed Tanous911ac312017-08-15 09:37:42 -07001078
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001079 if (ifaceName == interfaceName) {
Ed Tanous911ac312017-08-15 09:37:42 -07001080 tinyxml2::XMLElement *methods =
1081 interface->FirstChildElement("method");
1082 while (methods != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001083 nlohmann::json argsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001084 tinyxml2::XMLElement *arg =
1085 methods->FirstChildElement("arg");
1086 while (arg != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001087 argsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001088 {{"name", arg->Attribute("name")},
1089 {"type", arg->Attribute("type")},
1090 {"direction", arg->Attribute("direction")}});
1091 arg = arg->NextSiblingElement("arg");
1092 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001093 methodsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001094 {{"name", methods->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001095 {"uri", "/bus/system/" + processName +
1096 objectPath + "/" + interfaceName +
Ed Tanous64530012018-02-06 17:08:16 -08001097 "/" + methods->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001098 {"args", argsArray}});
Ed Tanous911ac312017-08-15 09:37:42 -07001099 methods = methods->NextSiblingElement("method");
1100 }
1101 tinyxml2::XMLElement *signals =
1102 interface->FirstChildElement("signal");
1103 while (signals != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001104 nlohmann::json argsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001105
1106 tinyxml2::XMLElement *arg =
1107 signals->FirstChildElement("arg");
1108 while (arg != nullptr) {
1109 std::string name = arg->Attribute("name");
1110 std::string type = arg->Attribute("type");
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001111 argsArray.push_back({
Ed Tanous64530012018-02-06 17:08:16 -08001112 {"name", name},
1113 {"type", type},
Ed Tanous911ac312017-08-15 09:37:42 -07001114 });
1115 arg = arg->NextSiblingElement("arg");
1116 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001117 signalsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001118 {{"name", signals->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001119 {"args", argsArray}});
Ed Tanous911ac312017-08-15 09:37:42 -07001120 signals = signals->NextSiblingElement("signal");
1121 }
1122
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001123 res.jsonValue = {
Ed Tanous911ac312017-08-15 09:37:42 -07001124 {"status", "ok"},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001125 {"bus_name", processName},
1126 {"interface", interfaceName},
1127 {"methods", methodsArray},
Ed Tanousd76323e2018-08-07 14:35:40 -07001128 {"objectPath", objectPath},
Ed Tanous911ac312017-08-15 09:37:42 -07001129 {"properties", nlohmann::json::object()},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001130 {"signals", signalsArray}};
Ed Tanous911ac312017-08-15 09:37:42 -07001131
Ed Tanous911ac312017-08-15 09:37:42 -07001132 break;
1133 }
1134
1135 interface = interface->NextSiblingElement("interface");
1136 }
1137 if (interface == nullptr) {
1138 // if we got to the end of the list and never found a
1139 // match, throw 404
Ed Tanouse0d918b2018-03-27 17:41:04 -07001140 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -07001141 }
1142 }
1143 }
1144 res.end();
1145 },
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001146 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanousaa2e59c2018-04-12 12:17:20 -07001147 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -07001148 }
Ed Tanous911ac312017-08-15 09:37:42 -07001149 });
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001150}
Ed Tanous911ac312017-08-15 09:37:42 -07001151} // namespace openbmc_mapper
1152} // namespace crow