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