blob: 3bad07002d8083b2e8a3a457741e5033b0c45e91 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
Ed Tanous04e438c2020-10-03 08:06:26 -07002#include <app.hpp>
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02003#include <async_resp.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07004#include <boost/container/flat_map.hpp>
Ed Tanous9b243a42018-08-03 14:33:10 -07005#include <boost/container/flat_set.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07006#include <dbus_singleton.hpp>
Matt Spinler715748a2019-01-30 13:22:28 -06007#include <openbmc_dbus_rest.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07008#include <sdbusplus/bus/match.hpp>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07009#include <sdbusplus/message/types.hpp>
Ed Tanous04e438c2020-10-03 08:06:26 -070010#include <websocket.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050011
Ed Tanousabf2add2019-01-22 16:40:12 -080012#include <variant>
Ed Tanous9b243a42018-08-03 14:33:10 -070013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014namespace crow
15{
16namespace dbus_monitor
17{
Ed Tanous911ac312017-08-15 09:37:42 -070018
Ed Tanous1abe55e2018-09-05 08:30:59 -070019struct DbusWebsocketSession
20{
Patrick Williams59d494e2022-07-22 19:26:55 -050021 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
Ed Tanous24b2fe82022-01-06 12:45:54 -080022 boost::container::flat_set<std::string, std::less<>,
23 std::vector<std::string>>
24 interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070025};
26
Ed Tanouscf9e4172022-12-21 09:30:16 -080027using SessionMap = boost::container::flat_map<crow::websocket::Connection*,
28 DbusWebsocketSession>;
29
30// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
31static SessionMap sessions;
Ed Tanous911ac312017-08-15 09:37:42 -070032
Ed Tanous9b243a42018-08-03 14:33:10 -070033inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous81ce6092020-12-17 16:54:55 +000034 sd_bus_error* retError)
Ed Tanous1abe55e2018-09-05 08:30:59 -070035{
Ed Tanouse662eae2022-01-25 10:39:19 -080036 if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
Ed Tanous1abe55e2018-09-05 08:30:59 -070037 {
38 BMCWEB_LOG_ERROR << "Got sdbus error on match";
39 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070040 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070041 crow::websocket::Connection* connection =
42 static_cast<crow::websocket::Connection*>(userdata);
43 auto thisSession = sessions.find(connection);
44 if (thisSession == sessions.end())
45 {
46 BMCWEB_LOG_ERROR << "Couldn't find dbus connection " << connection;
47 return 0;
48 }
Patrick Williams59d494e2022-07-22 19:26:55 -050049 sdbusplus::message_t message(m);
Ed Tanous14766872022-03-15 10:44:42 -070050 nlohmann::json json;
51 json["event"] = message.get_member();
52 json["path"] = message.get_path();
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
54 {
Matt Spinler715748a2019-01-30 13:22:28 -060055 nlohmann::json data;
56 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
57 if (r < 0)
58 {
59 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
60 return 0;
61 }
62 if (!data.is_array())
63 {
64 BMCWEB_LOG_ERROR << "No data in PropertiesChanged signal";
65 return 0;
66 }
67
68 // data is type sa{sv}as and is an array[3] of string, object, array
Ed Tanous14766872022-03-15 10:44:42 -070069 json["interface"] = data[0];
70 json["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 }
72 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
73 {
Matt Spinler715748a2019-01-30 13:22:28 -060074 nlohmann::json data;
75 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
76 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 {
Matt Spinler715748a2019-01-30 13:22:28 -060078 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
79 return 0;
80 }
81
82 if (!data.is_array())
83 {
84 BMCWEB_LOG_ERROR << "No data in InterfacesAdded signal";
85 return 0;
86 }
87
88 // data is type oa{sa{sv}} which is an array[2] of string, object
Patrick Williams62bafc02022-09-08 17:35:35 -050089 for (const auto& entry : data[1].items())
Matt Spinler715748a2019-01-30 13:22:28 -060090 {
91 auto it = thisSession->second.interfaces.find(entry.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 if (it != thisSession->second.interfaces.end())
93 {
Ed Tanous14766872022-03-15 10:44:42 -070094 json["interfaces"][entry.key()] = entry.value();
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 }
96 }
97 }
98 else
99 {
100 BMCWEB_LOG_CRITICAL << "message " << message.get_member()
101 << " was unexpected";
102 return 0;
103 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700104
Ed Tanous71f52d92021-02-19 08:51:17 -0800105 connection->sendText(
Ed Tanous14766872022-03-15 10:44:42 -0700106 json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700108}
Ed Tanous911ac312017-08-15 09:37:42 -0700109
Ed Tanous23a21a12020-07-25 04:45:05 +0000110inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111{
112 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous432a8902021-06-14 15:28:56 -0700113 .privileges({{"Login"}})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 .websocket()
zhanghch0577726382021-10-21 14:07:57 +0800115 .onopen([&](crow::websocket::Connection& conn) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
Ed Tanous24b2fe82022-01-06 12:45:54 -0800117 sessions.try_emplace(&conn);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 })
Ed Tanouscb13a392020-07-25 19:02:03 +0000119 .onclose([&](crow::websocket::Connection& conn, const std::string&) {
120 sessions.erase(&conn);
121 })
Ed Tanous002d39b2022-05-31 08:59:27 -0700122 .onmessage(
123 [&](crow::websocket::Connection& conn, const std::string& data,
124 bool) {
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 " << &conn << " received " << 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 Tanous24b2fe82022-01-06 12:45:54 -0800144 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700145 const std::string* str =
146 interface.get_ptr<const std::string*>();
147 if (str != nullptr)
148 {
149 thisSession.interfaces.insert(*str);
150 }
Ed Tanous24b2fe82022-01-06 12:45:54 -0800151 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700152 }
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));
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700172
Ed Tanous002d39b2022-05-31 08:59:27 -0700173 // These regexes derived on the rules here:
174 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
175 std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
176 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 Tanous1abe55e2018-09-05 08:30:59 -0700184 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700185 BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
186 conn.close();
Ed Tanous9b243a42018-08-03 14:33:10 -0700187 return;
Ed Tanous9b243a42018-08-03 14:33:10 -0700188 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700189 if (!std::regex_match(*thisPathString, validPath))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700190 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700191 BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
192 conn.close();
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530193 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700194 }
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700195 std::string propertiesMatchString =
Ed Tanous002d39b2022-05-31 08:59:27 -0700196 ("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())
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530205 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700206 BMCWEB_LOG_DEBUG << "Creating match " << propertiesMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700207
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 thisSession.matches.emplace_back(
Patrick Williams59d494e2022-07-22 19:26:55 -0500209 std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous002d39b2022-05-31 08:59:27 -0700210 *crow::connections::systemBus, propertiesMatchString,
Ed Tanous2c70f802020-09-28 14:29:23 -0700211 onPropertyUpdate, &conn));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700212 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700213 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(
Patrick Williams59d494e2022-07-22 19:26:55 -0500232 std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous002d39b2022-05-31 08:59:27 -0700233 *crow::connections::systemBus, ifaceMatchString,
234 onPropertyUpdate, &conn));
235 }
236 }
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700237 std::string objectManagerMatchString =
Ed Tanous002d39b2022-05-31 08:59:27 -0700238 ("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(
Patrick Williams59d494e2022-07-22 19:26:55 -0500246 std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous002d39b2022-05-31 08:59:27 -0700247 *crow::connections::systemBus, objectManagerMatchString,
248 onPropertyUpdate, &conn));
249 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700250 });
Ed Tanous911ac312017-08-15 09:37:42 -0700251}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252} // namespace dbus_monitor
253} // namespace crow