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