blob: 089df76626247ab467255587b09864b5e92b1a36 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
Ed Tanousc94ad492019-10-10 15:39:33 -07002#include <app.h>
3#include <websocket.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -07004
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02005#include <async_resp.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07006#include <boost/container/flat_map.hpp>
Ed Tanous9b243a42018-08-03 14:33:10 -07007#include <boost/container/flat_set.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07008#include <dbus_singleton.hpp>
Matt Spinler715748a2019-01-30 13:22:28 -06009#include <openbmc_dbus_rest.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070010#include <sdbusplus/bus/match.hpp>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -070011#include <sdbusplus/message/types.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050012
Ed Tanousabf2add2019-01-22 16:40:12 -080013#include <variant>
Ed Tanous9b243a42018-08-03 14:33:10 -070014
Ed Tanous1abe55e2018-09-05 08:30:59 -070015namespace crow
16{
17namespace dbus_monitor
18{
Ed Tanous911ac312017-08-15 09:37:42 -070019
Ed Tanous1abe55e2018-09-05 08:30:59 -070020struct DbusWebsocketSession
21{
22 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
23 boost::container::flat_set<std::string> interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070024};
25
Ed Tanous55c7b7a2018-05-22 15:27:24 -070026static boost::container::flat_map<crow::websocket::Connection*,
Ed Tanous911ac312017-08-15 09:37:42 -070027 DbusWebsocketSession>
28 sessions;
29
Ed Tanous9b243a42018-08-03 14:33:10 -070030inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous1abe55e2018-09-05 08:30:59 -070031 sd_bus_error* ret_error)
32{
33 if (ret_error == nullptr || sd_bus_error_is_set(ret_error))
34 {
35 BMCWEB_LOG_ERROR << "Got sdbus error on match";
36 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070037 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 crow::websocket::Connection* connection =
39 static_cast<crow::websocket::Connection*>(userdata);
40 auto thisSession = sessions.find(connection);
41 if (thisSession == sessions.end())
42 {
43 BMCWEB_LOG_ERROR << "Couldn't find dbus connection " << connection;
44 return 0;
45 }
46 sdbusplus::message::message message(m);
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 nlohmann::json j{{"event", message.get_member()},
48 {"path", message.get_path()}};
49 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
50 {
Matt Spinler715748a2019-01-30 13:22:28 -060051 nlohmann::json data;
52 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
53 if (r < 0)
54 {
55 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
56 return 0;
57 }
58 if (!data.is_array())
59 {
60 BMCWEB_LOG_ERROR << "No data in PropertiesChanged signal";
61 return 0;
62 }
63
64 // data is type sa{sv}as and is an array[3] of string, object, array
65 j["interface"] = data[0];
66 j["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 }
68 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
69 {
Matt Spinler715748a2019-01-30 13:22:28 -060070 nlohmann::json data;
71 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
72 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 {
Matt Spinler715748a2019-01-30 13:22:28 -060074 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
75 return 0;
76 }
77
78 if (!data.is_array())
79 {
80 BMCWEB_LOG_ERROR << "No data in InterfacesAdded signal";
81 return 0;
82 }
83
84 // data is type oa{sa{sv}} which is an array[2] of string, object
85 for (auto& entry : data[1].items())
86 {
87 auto it = thisSession->second.interfaces.find(entry.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 if (it != thisSession->second.interfaces.end())
89 {
Matt Spinler715748a2019-01-30 13:22:28 -060090 j["interfaces"][entry.key()] = entry.value();
Ed Tanous1abe55e2018-09-05 08:30:59 -070091 }
92 }
93 }
94 else
95 {
96 BMCWEB_LOG_CRITICAL << "message " << message.get_member()
97 << " was unexpected";
98 return 0;
99 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700100
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 connection->sendText(j.dump());
102 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 Tanous23a21a12020-07-25 04:45:05 +0000108 .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");
145 if (paths != j.end())
146 {
Ed Tanous271584a2019-07-09 16:24:22 -0700147 size_t interfaceCount = thisSession.interfaces.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 if (interfaceCount == 0)
149 {
150 interfaceCount = 1;
151 }
152 // Reserve our matches upfront. For each path there is 1 for
153 // interfacesAdded, and InterfaceCount number for
154 // PropertiesChanged
155 thisSession.matches.reserve(thisSession.matches.size() +
156 paths->size() *
Ed Tanous271584a2019-07-09 16:24:22 -0700157 (1U + interfaceCount));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700159 std::string objectManagerMatchString;
160 std::string propertiesMatchString;
161 std::string objectManagerInterfacesMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162 // These regexes derived on the rules here:
163 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
164 std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
165 std::regex validInterface(
166 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
167
168 for (const auto& thisPath : *paths)
169 {
170 const std::string* thisPathString =
171 thisPath.get_ptr<const std::string*>();
172 if (thisPathString == nullptr)
173 {
174 BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
175 conn.close();
176 return;
177 }
178 if (!std::regex_match(*thisPathString, validPath))
179 {
180 BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
181 conn.close();
182 return;
183 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700184 propertiesMatchString =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700185 ("type='signal',"
186 "interface='org.freedesktop.DBus.Properties',"
187 "path_namespace='" +
188 *thisPathString +
189 "',"
190 "member='PropertiesChanged'");
191 // If interfaces weren't specified, add a single match for all
192 // interfaces
193 if (thisSession.interfaces.size() == 0)
194 {
195 BMCWEB_LOG_DEBUG << "Creating match "
Ed Tanous2c70f802020-09-28 14:29:23 -0700196 << propertiesMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700197
198 thisSession.matches.emplace_back(
199 std::make_unique<sdbusplus::bus::match::match>(
200 *crow::connections::systemBus,
Ed Tanous2c70f802020-09-28 14:29:23 -0700201 propertiesMatchString, onPropertyUpdate, &conn));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 }
203 else
204 {
205 // If interfaces were specified, add a match for each
206 // interface
207 for (const std::string& interface : thisSession.interfaces)
208 {
209 if (!std::regex_match(interface, validInterface))
210 {
211 BMCWEB_LOG_ERROR << "Invalid interface name "
212 << interface;
213 conn.close();
214 return;
215 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700216 std::string ifaceMatchString =
217 propertiesMatchString + ",arg0='" + interface + "'";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 BMCWEB_LOG_DEBUG << "Creating match "
219 << ifaceMatchString;
220 thisSession.matches.emplace_back(
221 std::make_unique<sdbusplus::bus::match::match>(
222 *crow::connections::systemBus, ifaceMatchString,
223 onPropertyUpdate, &conn));
224 }
225 }
Ed Tanous2c70f802020-09-28 14:29:23 -0700226 objectManagerMatchString =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 ("type='signal',"
228 "interface='org.freedesktop.DBus.ObjectManager',"
229 "path_namespace='" +
230 *thisPathString +
231 "',"
232 "member='InterfacesAdded'");
233 BMCWEB_LOG_DEBUG << "Creating match "
Ed Tanous2c70f802020-09-28 14:29:23 -0700234 << objectManagerMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 thisSession.matches.emplace_back(
236 std::make_unique<sdbusplus::bus::match::match>(
Ed Tanous2c70f802020-09-28 14:29:23 -0700237 *crow::connections::systemBus, objectManagerMatchString,
238 onPropertyUpdate, &conn));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 }
240 });
Ed Tanous911ac312017-08-15 09:37:42 -0700241}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242} // namespace dbus_monitor
243} // namespace crow