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