blob: 764d1579854639a1662958abaad7033b16bb9cdf [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{
21 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> 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 Tanous55c7b7a2018-05-22 15:27:24 -070027static boost::container::flat_map<crow::websocket::Connection*,
Ed Tanous911ac312017-08-15 09:37:42 -070028 DbusWebsocketSession>
29 sessions;
30
Ed Tanous9b243a42018-08-03 14:33:10 -070031inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous81ce6092020-12-17 16:54:55 +000032 sd_bus_error* retError)
Ed Tanous1abe55e2018-09-05 08:30:59 -070033{
Ed Tanouse662eae2022-01-25 10:39:19 -080034 if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
Ed Tanous1abe55e2018-09-05 08:30:59 -070035 {
36 BMCWEB_LOG_ERROR << "Got sdbus error on match";
37 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070038 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070039 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 Tanous1abe55e2018-09-05 08:30:59 -070048 nlohmann::json j{{"event", message.get_member()},
49 {"path", message.get_path()}};
50 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
51 {
Matt Spinler715748a2019-01-30 13:22:28 -060052 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 Tanous1abe55e2018-09-05 08:30:59 -070068 }
69 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
70 {
Matt Spinler715748a2019-01-30 13:22:28 -060071 nlohmann::json data;
72 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
73 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 {
Matt Spinler715748a2019-01-30 13:22:28 -060075 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 Tanous1abe55e2018-09-05 08:30:59 -070089 if (it != thisSession->second.interfaces.end())
90 {
Matt Spinler715748a2019-01-30 13:22:28 -060091 j["interfaces"][entry.key()] = entry.value();
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 }
93 }
94 }
95 else
96 {
97 BMCWEB_LOG_CRITICAL << "message " << message.get_member()
98 << " was unexpected";
99 return 0;
100 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700101
Ed Tanous71f52d92021-02-19 08:51:17 -0800102 connection->sendText(
103 j.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700105}
Ed Tanous911ac312017-08-15 09:37:42 -0700106
Ed Tanous23a21a12020-07-25 04:45:05 +0000107inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108{
109 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous432a8902021-06-14 15:28:56 -0700110 .privileges({{"Login"}})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 .websocket()
zhanghch0577726382021-10-21 14:07:57 +0800112 .onopen([&](crow::websocket::Connection& conn) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
Ed Tanous24b2fe82022-01-06 12:45:54 -0800114 sessions.try_emplace(&conn);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 })
Ed Tanouscb13a392020-07-25 19:02:03 +0000116 .onclose([&](crow::websocket::Connection& conn, const std::string&) {
117 sessions.erase(&conn);
118 })
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 .onmessage([&](crow::websocket::Connection& conn,
Ed Tanouscb13a392020-07-25 19:02:03 +0000120 const std::string& data, bool) {
Ed Tanous24b2fe82022-01-06 12:45:54 -0800121 const auto sessionPair = sessions.find(&conn);
122 if (sessionPair == sessions.end())
123 {
124 conn.close("Internal error");
125 }
126 DbusWebsocketSession& thisSession = sessionPair->second;
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500127 BMCWEB_LOG_DEBUG << "Connection " << &conn << " received " << data;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
129 if (j.is_discarded())
130 {
131 BMCWEB_LOG_ERROR << "Unable to parse json data for monitor";
132 conn.close("Unable to parse json request");
Ed Tanous9b243a42018-08-03 14:33:10 -0700133 return;
Ed Tanous9b243a42018-08-03 14:33:10 -0700134 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700135 nlohmann::json::iterator interfaces = j.find("interfaces");
136 if (interfaces != j.end())
137 {
138 thisSession.interfaces.reserve(interfaces->size());
139 for (auto& interface : *interfaces)
140 {
141 const std::string* str =
142 interface.get_ptr<const std::string*>();
143 if (str != nullptr)
144 {
145 thisSession.interfaces.insert(*str);
146 }
147 }
148 }
149
150 nlohmann::json::iterator paths = j.find("paths");
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530151 if (paths == j.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700152 {
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530153 BMCWEB_LOG_ERROR << "Unable to find paths in json data";
154 conn.close("Unable to find paths in json data");
155 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700156 }
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530157
158 size_t interfaceCount = thisSession.interfaces.size();
159 if (interfaceCount == 0)
160 {
161 interfaceCount = 1;
162 }
163 // Reserve our matches upfront. For each path there is 1 for
164 // interfacesAdded, and InterfaceCount number for
165 // PropertiesChanged
166 thisSession.matches.reserve(thisSession.matches.size() +
167 paths->size() * (1U + interfaceCount));
Ed Tanous2c70f802020-09-28 14:29:23 -0700168 std::string objectManagerMatchString;
169 std::string propertiesMatchString;
170 std::string objectManagerInterfacesMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700171 // These regexes derived on the rules here:
172 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
173 std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
174 std::regex validInterface(
175 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
176
177 for (const auto& thisPath : *paths)
178 {
179 const std::string* thisPathString =
180 thisPath.get_ptr<const std::string*>();
181 if (thisPathString == nullptr)
182 {
183 BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
184 conn.close();
185 return;
186 }
187 if (!std::regex_match(*thisPathString, validPath))
188 {
189 BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
190 conn.close();
191 return;
192 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700193 propertiesMatchString =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700194 ("type='signal',"
195 "interface='org.freedesktop.DBus.Properties',"
196 "path_namespace='" +
197 *thisPathString +
198 "',"
199 "member='PropertiesChanged'");
200 // If interfaces weren't specified, add a single match for all
201 // interfaces
Ed Tanous26f69762022-01-25 09:49:11 -0800202 if (thisSession.interfaces.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700203 {
204 BMCWEB_LOG_DEBUG << "Creating match "
Ed Tanous2c70f802020-09-28 14:29:23 -0700205 << propertiesMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700206
207 thisSession.matches.emplace_back(
208 std::make_unique<sdbusplus::bus::match::match>(
209 *crow::connections::systemBus,
Ed Tanous2c70f802020-09-28 14:29:23 -0700210 propertiesMatchString, onPropertyUpdate, &conn));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700211 }
212 else
213 {
214 // If interfaces were specified, add a match for each
215 // interface
216 for (const std::string& interface : thisSession.interfaces)
217 {
218 if (!std::regex_match(interface, validInterface))
219 {
220 BMCWEB_LOG_ERROR << "Invalid interface name "
221 << interface;
222 conn.close();
223 return;
224 }
Ed Tanousf23b7292020-10-15 09:41:17 -0700225 std::string ifaceMatchString = propertiesMatchString;
226 ifaceMatchString += ",arg0='";
227 ifaceMatchString += interface;
228 ifaceMatchString += "'";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229 BMCWEB_LOG_DEBUG << "Creating match "
230 << ifaceMatchString;
231 thisSession.matches.emplace_back(
232 std::make_unique<sdbusplus::bus::match::match>(
233 *crow::connections::systemBus, ifaceMatchString,
234 onPropertyUpdate, &conn));
235 }
236 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700237 objectManagerMatchString =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 ("type='signal',"
239 "interface='org.freedesktop.DBus.ObjectManager',"
240 "path_namespace='" +
241 *thisPathString +
242 "',"
243 "member='InterfacesAdded'");
244 BMCWEB_LOG_DEBUG << "Creating match "
Ed Tanous2c70f802020-09-28 14:29:23 -0700245 << objectManagerMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246 thisSession.matches.emplace_back(
247 std::make_unique<sdbusplus::bus::match::match>(
Ed Tanous2c70f802020-09-28 14:29:23 -0700248 *crow::connections::systemBus, objectManagerMatchString,
249 onPropertyUpdate, &conn));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700250 }
251 });
Ed Tanous911ac312017-08-15 09:37:42 -0700252}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253} // namespace dbus_monitor
254} // namespace crow