Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 2 | #include "app.hpp" |
| 3 | #include "async_resp.hpp" |
| 4 | #include "dbus_singleton.hpp" |
| 5 | #include "openbmc_dbus_rest.hpp" |
Ed Tanous | faf100f | 2023-05-25 10:03:14 -0700 | [diff] [blame] | 6 | #include "websocket.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 7 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 8 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 9 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 10 | #include <sdbusplus/bus/match.hpp> |
William A. Kennington III | 0a63b1c | 2018-10-18 13:37:19 -0700 | [diff] [blame] | 11 | #include <sdbusplus/message/types.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 12 | |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 13 | #include <variant> |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | namespace crow |
| 16 | { |
| 17 | namespace dbus_monitor |
| 18 | { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | struct DbusWebsocketSession |
| 21 | { |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 22 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches; |
Ed Tanous | 24b2fe8 | 2022-01-06 12:45:54 -0800 | [diff] [blame] | 23 | boost::container::flat_set<std::string, std::less<>, |
| 24 | std::vector<std::string>> |
| 25 | interfaces; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 28 | using SessionMap = boost::container::flat_map<crow::websocket::Connection*, |
| 29 | DbusWebsocketSession>; |
| 30 | |
| 31 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 32 | static SessionMap sessions; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 34 | inline int onPropertyUpdate(sd_bus_message* m, void* userdata, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 35 | sd_bus_error* retError) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 36 | { |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 37 | if (retError == nullptr || (sd_bus_error_is_set(retError) != 0)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 39 | BMCWEB_LOG_ERROR("Got sdbus error on match"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | return 0; |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 41 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | crow::websocket::Connection* connection = |
| 43 | static_cast<crow::websocket::Connection*>(userdata); |
| 44 | auto thisSession = sessions.find(connection); |
| 45 | if (thisSession == sessions.end()) |
| 46 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 47 | BMCWEB_LOG_ERROR("Couldn't find dbus connection {}", |
| 48 | logPtr(connection)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | return 0; |
| 50 | } |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 51 | sdbusplus::message_t message(m); |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 52 | nlohmann::json json; |
| 53 | json["event"] = message.get_member(); |
| 54 | json["path"] = message.get_path(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | if (strcmp(message.get_member(), "PropertiesChanged") == 0) |
| 56 | { |
Matt Spinler | 715748a | 2019-01-30 13:22:28 -0600 | [diff] [blame] | 57 | nlohmann::json data; |
| 58 | int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data); |
| 59 | if (r < 0) |
| 60 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 61 | BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r); |
Matt Spinler | 715748a | 2019-01-30 13:22:28 -0600 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | if (!data.is_array()) |
| 65 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 66 | BMCWEB_LOG_ERROR("No data in PropertiesChanged signal"); |
Matt Spinler | 715748a | 2019-01-30 13:22:28 -0600 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | // data is type sa{sv}as and is an array[3] of string, object, array |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 71 | json["interface"] = data[0]; |
| 72 | json["properties"] = data[1]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | } |
| 74 | else if (strcmp(message.get_member(), "InterfacesAdded") == 0) |
| 75 | { |
Matt Spinler | 715748a | 2019-01-30 13:22:28 -0600 | [diff] [blame] | 76 | nlohmann::json data; |
| 77 | int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data); |
| 78 | if (r < 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 80 | BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r); |
Matt Spinler | 715748a | 2019-01-30 13:22:28 -0600 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | if (!data.is_array()) |
| 85 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 86 | BMCWEB_LOG_ERROR("No data in InterfacesAdded signal"); |
Matt Spinler | 715748a | 2019-01-30 13:22:28 -0600 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | // data is type oa{sa{sv}} which is an array[2] of string, object |
Patrick Williams | 62bafc0 | 2022-09-08 17:35:35 -0500 | [diff] [blame] | 91 | for (const auto& entry : data[1].items()) |
Matt Spinler | 715748a | 2019-01-30 13:22:28 -0600 | [diff] [blame] | 92 | { |
| 93 | auto it = thisSession->second.interfaces.find(entry.key()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 94 | if (it != thisSession->second.interfaces.end()) |
| 95 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 96 | json["interfaces"][entry.key()] = entry.value(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | else |
| 101 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 102 | BMCWEB_LOG_CRITICAL("message {} was unexpected", message.get_member()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | } |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 105 | |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 106 | connection->sendText( |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 107 | json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | return 0; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 109 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 110 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 111 | inline void requestRoutes(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | { |
| 113 | BMCWEB_ROUTE(app, "/subscribe") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 114 | .privileges({{"Login"}}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | .websocket() |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 116 | .onopen([&](crow::websocket::Connection& conn) { |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 117 | BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn)); |
| 118 | sessions.try_emplace(&conn); |
| 119 | }) |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 120 | .onclose([&](crow::websocket::Connection& conn, const std::string&) { |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 121 | sessions.erase(&conn); |
| 122 | }) |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 123 | .onmessage([&](crow::websocket::Connection& conn, |
| 124 | const std::string& data, bool) { |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 125 | const auto sessionPair = sessions.find(&conn); |
| 126 | if (sessionPair == sessions.end()) |
| 127 | { |
| 128 | conn.close("Internal error"); |
| 129 | } |
| 130 | DbusWebsocketSession& thisSession = sessionPair->second; |
| 131 | BMCWEB_LOG_DEBUG("Connection {} received {}", logPtr(&conn), data); |
| 132 | nlohmann::json j = nlohmann::json::parse(data, nullptr, false); |
| 133 | if (j.is_discarded()) |
| 134 | { |
| 135 | BMCWEB_LOG_ERROR("Unable to parse json data for monitor"); |
| 136 | conn.close("Unable to parse json request"); |
| 137 | return; |
| 138 | } |
| 139 | nlohmann::json::iterator interfaces = j.find("interfaces"); |
| 140 | if (interfaces != j.end()) |
| 141 | { |
| 142 | thisSession.interfaces.reserve(interfaces->size()); |
| 143 | for (auto& interface : *interfaces) |
Ed Tanous | 24b2fe8 | 2022-01-06 12:45:54 -0800 | [diff] [blame] | 144 | { |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 145 | const std::string* str = |
| 146 | interface.get_ptr<const std::string*>(); |
| 147 | if (str != nullptr) |
| 148 | { |
| 149 | thisSession.interfaces.insert(*str); |
| 150 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 151 | } |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 152 | } |
| 153 | |
| 154 | nlohmann::json::iterator paths = j.find("paths"); |
| 155 | if (paths == j.end()) |
| 156 | { |
| 157 | BMCWEB_LOG_ERROR("Unable to find paths in json data"); |
| 158 | conn.close("Unable to find paths in json data"); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | size_t interfaceCount = thisSession.interfaces.size(); |
| 163 | if (interfaceCount == 0) |
| 164 | { |
| 165 | interfaceCount = 1; |
| 166 | } |
| 167 | // Reserve our matches upfront. For each path there is 1 for |
| 168 | // interfacesAdded, and InterfaceCount number for |
| 169 | // PropertiesChanged |
| 170 | thisSession.matches.reserve(thisSession.matches.size() + |
| 171 | paths->size() * (1U + interfaceCount)); |
| 172 | |
| 173 | // These regexes derived on the rules here: |
| 174 | // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names |
| 175 | static std::regex validPath("^/([A-Za-z0-9_]+/?)*$"); |
| 176 | static std::regex validInterface( |
| 177 | "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$"); |
| 178 | |
| 179 | for (const auto& thisPath : *paths) |
| 180 | { |
| 181 | const std::string* thisPathString = |
| 182 | thisPath.get_ptr<const std::string*>(); |
| 183 | if (thisPathString == nullptr) |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 184 | { |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 185 | BMCWEB_LOG_ERROR("subscribe path isn't a string?"); |
| 186 | conn.close(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 189 | if (!std::regex_match(*thisPathString, validPath)) |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 190 | { |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 191 | BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString); |
| 192 | conn.close(); |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 193 | return; |
Ed Tanous | 9b243a4 | 2018-08-03 14:33:10 -0700 | [diff] [blame] | 194 | } |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 195 | std::string propertiesMatchString = |
| 196 | ("type='signal'," |
| 197 | "interface='org.freedesktop.DBus.Properties'," |
| 198 | "path_namespace='" + |
| 199 | *thisPathString + |
| 200 | "'," |
| 201 | "member='PropertiesChanged'"); |
| 202 | // If interfaces weren't specified, add a single match for all |
| 203 | // interfaces |
| 204 | if (thisSession.interfaces.empty()) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 205 | { |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 206 | BMCWEB_LOG_DEBUG("Creating match {}", propertiesMatchString); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 207 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 208 | thisSession.matches.emplace_back( |
| 209 | std::make_unique<sdbusplus::bus::match_t>( |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 210 | *crow::connections::systemBus, propertiesMatchString, |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 211 | onPropertyUpdate, &conn)); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 212 | } |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame^] | 213 | else |
| 214 | { |
| 215 | // If interfaces were specified, add a match for each |
| 216 | // interface |
| 217 | for (const std::string& interface : thisSession.interfaces) |
| 218 | { |
| 219 | if (!std::regex_match(interface, validInterface)) |
| 220 | { |
| 221 | BMCWEB_LOG_ERROR("Invalid interface name {}", |
| 222 | interface); |
| 223 | conn.close(); |
| 224 | return; |
| 225 | } |
| 226 | std::string ifaceMatchString = propertiesMatchString; |
| 227 | ifaceMatchString += ",arg0='"; |
| 228 | ifaceMatchString += interface; |
| 229 | ifaceMatchString += "'"; |
| 230 | BMCWEB_LOG_DEBUG("Creating match {}", ifaceMatchString); |
| 231 | thisSession.matches.emplace_back( |
| 232 | std::make_unique<sdbusplus::bus::match_t>( |
| 233 | *crow::connections::systemBus, ifaceMatchString, |
| 234 | onPropertyUpdate, &conn)); |
| 235 | } |
| 236 | } |
| 237 | std::string objectManagerMatchString = |
| 238 | ("type='signal'," |
| 239 | "interface='org.freedesktop.DBus.ObjectManager'," |
| 240 | "path_namespace='" + |
| 241 | *thisPathString + |
| 242 | "'," |
| 243 | "member='InterfacesAdded'"); |
| 244 | BMCWEB_LOG_DEBUG("Creating match {}", objectManagerMatchString); |
| 245 | thisSession.matches.emplace_back( |
| 246 | std::make_unique<sdbusplus::bus::match_t>( |
| 247 | *crow::connections::systemBus, objectManagerMatchString, |
| 248 | onPropertyUpdate, &conn)); |
| 249 | } |
| 250 | }); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 251 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 252 | } // namespace dbus_monitor |
| 253 | } // namespace crow |