blob: a6c86c61ef3417d9ac444c86208714b0d514af90 [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;
22 boost::container::flat_set<std::string> interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070023};
24
Ed Tanous55c7b7a2018-05-22 15:27:24 -070025static boost::container::flat_map<crow::websocket::Connection*,
Ed Tanous911ac312017-08-15 09:37:42 -070026 DbusWebsocketSession>
27 sessions;
28
Ed Tanous9b243a42018-08-03 14:33:10 -070029inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous81ce6092020-12-17 16:54:55 +000030 sd_bus_error* retError)
Ed Tanous1abe55e2018-09-05 08:30:59 -070031{
Ed Tanous81ce6092020-12-17 16:54:55 +000032 if (retError == nullptr || sd_bus_error_is_set(retError))
Ed Tanous1abe55e2018-09-05 08:30:59 -070033 {
34 BMCWEB_LOG_ERROR << "Got sdbus error on match";
35 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070036 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070037 crow::websocket::Connection* connection =
38 static_cast<crow::websocket::Connection*>(userdata);
39 auto thisSession = sessions.find(connection);
40 if (thisSession == sessions.end())
41 {
42 BMCWEB_LOG_ERROR << "Couldn't find dbus connection " << connection;
43 return 0;
44 }
45 sdbusplus::message::message message(m);
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 nlohmann::json j{{"event", message.get_member()},
47 {"path", message.get_path()}};
48 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
49 {
Matt Spinler715748a2019-01-30 13:22:28 -060050 nlohmann::json data;
51 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
52 if (r < 0)
53 {
54 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
55 return 0;
56 }
57 if (!data.is_array())
58 {
59 BMCWEB_LOG_ERROR << "No data in PropertiesChanged signal";
60 return 0;
61 }
62
63 // data is type sa{sv}as and is an array[3] of string, object, array
64 j["interface"] = data[0];
65 j["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 }
67 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
68 {
Matt Spinler715748a2019-01-30 13:22:28 -060069 nlohmann::json data;
70 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
71 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 {
Matt Spinler715748a2019-01-30 13:22:28 -060073 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
74 return 0;
75 }
76
77 if (!data.is_array())
78 {
79 BMCWEB_LOG_ERROR << "No data in InterfacesAdded signal";
80 return 0;
81 }
82
83 // data is type oa{sa{sv}} which is an array[2] of string, object
84 for (auto& entry : data[1].items())
85 {
86 auto it = thisSession->second.interfaces.find(entry.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 if (it != thisSession->second.interfaces.end())
88 {
Matt Spinler715748a2019-01-30 13:22:28 -060089 j["interfaces"][entry.key()] = entry.value();
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 }
91 }
92 }
93 else
94 {
95 BMCWEB_LOG_CRITICAL << "message " << message.get_member()
96 << " was unexpected";
97 return 0;
98 }
Ed Tanous9b243a42018-08-03 14:33:10 -070099
Ed Tanous71f52d92021-02-19 08:51:17 -0800100 connection->sendText(
101 j.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700103}
Ed Tanous911ac312017-08-15 09:37:42 -0700104
Ed Tanous23a21a12020-07-25 04:45:05 +0000105inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106{
107 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous432a8902021-06-14 15:28:56 -0700108 .privileges({{"Login"}})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 .websocket()
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200110 .onopen([&](crow::websocket::Connection& conn,
Ed Tanousb5a76932020-09-29 16:16:58 -0700111 const std::shared_ptr<bmcweb::AsyncResp>&) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
113 sessions[&conn] = DbusWebsocketSession();
114 })
Ed Tanouscb13a392020-07-25 19:02:03 +0000115 .onclose([&](crow::websocket::Connection& conn, const std::string&) {
116 sessions.erase(&conn);
117 })
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 .onmessage([&](crow::websocket::Connection& conn,
Ed Tanouscb13a392020-07-25 19:02:03 +0000119 const std::string& data, bool) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 DbusWebsocketSession& thisSession = sessions[&conn];
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500121 BMCWEB_LOG_DEBUG << "Connection " << &conn << " received " << data;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
123 if (j.is_discarded())
124 {
125 BMCWEB_LOG_ERROR << "Unable to parse json data for monitor";
126 conn.close("Unable to parse json request");
Ed Tanous9b243a42018-08-03 14:33:10 -0700127 return;
Ed Tanous9b243a42018-08-03 14:33:10 -0700128 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129 nlohmann::json::iterator interfaces = j.find("interfaces");
130 if (interfaces != j.end())
131 {
132 thisSession.interfaces.reserve(interfaces->size());
133 for (auto& interface : *interfaces)
134 {
135 const std::string* str =
136 interface.get_ptr<const std::string*>();
137 if (str != nullptr)
138 {
139 thisSession.interfaces.insert(*str);
140 }
141 }
142 }
143
144 nlohmann::json::iterator paths = j.find("paths");
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530145 if (paths == j.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700146 {
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530147 BMCWEB_LOG_ERROR << "Unable to find paths in json data";
148 conn.close("Unable to find paths in json data");
149 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700150 }
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530151
152 size_t interfaceCount = thisSession.interfaces.size();
153 if (interfaceCount == 0)
154 {
155 interfaceCount = 1;
156 }
157 // Reserve our matches upfront. For each path there is 1 for
158 // interfacesAdded, and InterfaceCount number for
159 // PropertiesChanged
160 thisSession.matches.reserve(thisSession.matches.size() +
161 paths->size() * (1U + interfaceCount));
Ed Tanous2c70f802020-09-28 14:29:23 -0700162 std::string objectManagerMatchString;
163 std::string propertiesMatchString;
164 std::string objectManagerInterfacesMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165 // These regexes derived on the rules here:
166 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
167 std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
168 std::regex validInterface(
169 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
170
171 for (const auto& thisPath : *paths)
172 {
173 const std::string* thisPathString =
174 thisPath.get_ptr<const std::string*>();
175 if (thisPathString == nullptr)
176 {
177 BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
178 conn.close();
179 return;
180 }
181 if (!std::regex_match(*thisPathString, validPath))
182 {
183 BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
184 conn.close();
185 return;
186 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700187 propertiesMatchString =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188 ("type='signal',"
189 "interface='org.freedesktop.DBus.Properties',"
190 "path_namespace='" +
191 *thisPathString +
192 "',"
193 "member='PropertiesChanged'");
194 // If interfaces weren't specified, add a single match for all
195 // interfaces
196 if (thisSession.interfaces.size() == 0)
197 {
198 BMCWEB_LOG_DEBUG << "Creating match "
Ed Tanous2c70f802020-09-28 14:29:23 -0700199 << propertiesMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200
201 thisSession.matches.emplace_back(
202 std::make_unique<sdbusplus::bus::match::match>(
203 *crow::connections::systemBus,
Ed Tanous2c70f802020-09-28 14:29:23 -0700204 propertiesMatchString, onPropertyUpdate, &conn));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700205 }
206 else
207 {
208 // If interfaces were specified, add a match for each
209 // interface
210 for (const std::string& interface : thisSession.interfaces)
211 {
212 if (!std::regex_match(interface, validInterface))
213 {
214 BMCWEB_LOG_ERROR << "Invalid interface name "
215 << interface;
216 conn.close();
217 return;
218 }
Ed Tanousf23b7292020-10-15 09:41:17 -0700219 std::string ifaceMatchString = propertiesMatchString;
220 ifaceMatchString += ",arg0='";
221 ifaceMatchString += interface;
222 ifaceMatchString += "'";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 BMCWEB_LOG_DEBUG << "Creating match "
224 << ifaceMatchString;
225 thisSession.matches.emplace_back(
226 std::make_unique<sdbusplus::bus::match::match>(
227 *crow::connections::systemBus, ifaceMatchString,
228 onPropertyUpdate, &conn));
229 }
230 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700231 objectManagerMatchString =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 ("type='signal',"
233 "interface='org.freedesktop.DBus.ObjectManager',"
234 "path_namespace='" +
235 *thisPathString +
236 "',"
237 "member='InterfacesAdded'");
238 BMCWEB_LOG_DEBUG << "Creating match "
Ed Tanous2c70f802020-09-28 14:29:23 -0700239 << objectManagerMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 thisSession.matches.emplace_back(
241 std::make_unique<sdbusplus::bus::match::match>(
Ed Tanous2c70f802020-09-28 14:29:23 -0700242 *crow::connections::systemBus, objectManagerMatchString,
243 onPropertyUpdate, &conn));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700244 }
245 });
Ed Tanous911ac312017-08-15 09:37:42 -0700246}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247} // namespace dbus_monitor
248} // namespace crow