blob: 4ecc4367d94edfd0628a79fe90a0327508f6bfb4 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08002#include "app.hpp"
3#include "async_resp.hpp"
4#include "dbus_singleton.hpp"
5#include "openbmc_dbus_rest.hpp"
Ed Tanousfaf100f2023-05-25 10:03:14 -07006#include "websocket.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007
Ed Tanous911ac312017-08-15 09:37:42 -07008#include <boost/container/flat_map.hpp>
Ed Tanous9b243a42018-08-03 14:33:10 -07009#include <boost/container/flat_set.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{
Patrick Williams59d494e2022-07-22 19:26:55 -050022 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
Ed Tanous24b2fe82022-01-06 12:45:54 -080023 boost::container::flat_set<std::string, std::less<>,
24 std::vector<std::string>>
25 interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070026};
27
Ed Tanouscf9e4172022-12-21 09:30:16 -080028using SessionMap = boost::container::flat_map<crow::websocket::Connection*,
29 DbusWebsocketSession>;
30
31// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
32static SessionMap sessions;
Ed Tanous911ac312017-08-15 09:37:42 -070033
Ed Tanous9b243a42018-08-03 14:33:10 -070034inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous81ce6092020-12-17 16:54:55 +000035 sd_bus_error* retError)
Ed Tanous1abe55e2018-09-05 08:30:59 -070036{
Ed Tanouse662eae2022-01-25 10:39:19 -080037 if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 {
39 BMCWEB_LOG_ERROR << "Got sdbus error on match";
40 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070041 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070042 crow::websocket::Connection* connection =
43 static_cast<crow::websocket::Connection*>(userdata);
44 auto thisSession = sessions.find(connection);
45 if (thisSession == sessions.end())
46 {
47 BMCWEB_LOG_ERROR << "Couldn't find dbus connection " << connection;
48 return 0;
49 }
Patrick Williams59d494e2022-07-22 19:26:55 -050050 sdbusplus::message_t message(m);
Ed Tanous14766872022-03-15 10:44:42 -070051 nlohmann::json json;
52 json["event"] = message.get_member();
53 json["path"] = message.get_path();
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
55 {
Matt Spinler715748a2019-01-30 13:22:28 -060056 nlohmann::json data;
57 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
58 if (r < 0)
59 {
60 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
61 return 0;
62 }
63 if (!data.is_array())
64 {
65 BMCWEB_LOG_ERROR << "No data in PropertiesChanged signal";
66 return 0;
67 }
68
69 // data is type sa{sv}as and is an array[3] of string, object, array
Ed Tanous14766872022-03-15 10:44:42 -070070 json["interface"] = data[0];
71 json["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 }
73 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
74 {
Matt Spinler715748a2019-01-30 13:22:28 -060075 nlohmann::json data;
76 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
77 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 {
Matt Spinler715748a2019-01-30 13:22:28 -060079 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
80 return 0;
81 }
82
83 if (!data.is_array())
84 {
85 BMCWEB_LOG_ERROR << "No data in InterfacesAdded signal";
86 return 0;
87 }
88
89 // data is type oa{sa{sv}} which is an array[2] of string, object
Patrick Williams62bafc02022-09-08 17:35:35 -050090 for (const auto& entry : data[1].items())
Matt Spinler715748a2019-01-30 13:22:28 -060091 {
92 auto it = thisSession->second.interfaces.find(entry.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 if (it != thisSession->second.interfaces.end())
94 {
Ed Tanous14766872022-03-15 10:44:42 -070095 json["interfaces"][entry.key()] = entry.value();
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 }
97 }
98 }
99 else
100 {
101 BMCWEB_LOG_CRITICAL << "message " << message.get_member()
102 << " was unexpected";
103 return 0;
104 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700105
Ed Tanous71f52d92021-02-19 08:51:17 -0800106 connection->sendText(
Ed Tanous14766872022-03-15 10:44:42 -0700107 json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700109}
Ed Tanous911ac312017-08-15 09:37:42 -0700110
Ed Tanous23a21a12020-07-25 04:45:05 +0000111inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112{
113 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous432a8902021-06-14 15:28:56 -0700114 .privileges({{"Login"}})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 .websocket()
zhanghch0577726382021-10-21 14:07:57 +0800116 .onopen([&](crow::websocket::Connection& conn) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
Ed Tanous24b2fe82022-01-06 12:45:54 -0800118 sessions.try_emplace(&conn);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 })
Ed Tanouscb13a392020-07-25 19:02:03 +0000120 .onclose([&](crow::websocket::Connection& conn, const std::string&) {
121 sessions.erase(&conn);
122 })
Ed Tanous002d39b2022-05-31 08:59:27 -0700123 .onmessage(
124 [&](crow::websocket::Connection& conn, const std::string& data,
125 bool) {
126 const auto sessionPair = sessions.find(&conn);
127 if (sessionPair == sessions.end())
128 {
129 conn.close("Internal error");
130 }
131 DbusWebsocketSession& thisSession = sessionPair->second;
132 BMCWEB_LOG_DEBUG << "Connection " << &conn << " received " << data;
133 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
134 if (j.is_discarded())
135 {
136 BMCWEB_LOG_ERROR << "Unable to parse json data for monitor";
137 conn.close("Unable to parse json request");
138 return;
139 }
140 nlohmann::json::iterator interfaces = j.find("interfaces");
141 if (interfaces != j.end())
142 {
143 thisSession.interfaces.reserve(interfaces->size());
144 for (auto& interface : *interfaces)
Ed Tanous24b2fe82022-01-06 12:45:54 -0800145 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700146 const std::string* str =
147 interface.get_ptr<const std::string*>();
148 if (str != nullptr)
149 {
150 thisSession.interfaces.insert(*str);
151 }
Ed Tanous24b2fe82022-01-06 12:45:54 -0800152 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700153 }
154
155 nlohmann::json::iterator paths = j.find("paths");
156 if (paths == j.end())
157 {
158 BMCWEB_LOG_ERROR << "Unable to find paths in json data";
159 conn.close("Unable to find paths in json data");
160 return;
161 }
162
163 size_t interfaceCount = thisSession.interfaces.size();
164 if (interfaceCount == 0)
165 {
166 interfaceCount = 1;
167 }
168 // Reserve our matches upfront. For each path there is 1 for
169 // interfacesAdded, and InterfaceCount number for
170 // PropertiesChanged
171 thisSession.matches.reserve(thisSession.matches.size() +
172 paths->size() * (1U + interfaceCount));
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700173
Ed Tanous002d39b2022-05-31 08:59:27 -0700174 // These regexes derived on the rules here:
175 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
Ed Tanous4b242742023-05-11 09:51:51 -0700176 static std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
177 static std::regex validInterface(
Ed Tanous002d39b2022-05-31 08:59:27 -0700178 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
179
180 for (const auto& thisPath : *paths)
181 {
182 const std::string* thisPathString =
183 thisPath.get_ptr<const std::string*>();
184 if (thisPathString == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700185 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700186 BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
187 conn.close();
Ed Tanous9b243a42018-08-03 14:33:10 -0700188 return;
Ed Tanous9b243a42018-08-03 14:33:10 -0700189 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700190 if (!std::regex_match(*thisPathString, validPath))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700192 BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
193 conn.close();
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530194 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 }
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700196 std::string propertiesMatchString =
Ed Tanous002d39b2022-05-31 08:59:27 -0700197 ("type='signal',"
198 "interface='org.freedesktop.DBus.Properties',"
199 "path_namespace='" +
200 *thisPathString +
201 "',"
202 "member='PropertiesChanged'");
203 // If interfaces weren't specified, add a single match for all
204 // interfaces
205 if (thisSession.interfaces.empty())
P Dheeraj Srujan Kumar418b9342021-07-13 03:36:20 +0530206 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700207 BMCWEB_LOG_DEBUG << "Creating match " << propertiesMatchString;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209 thisSession.matches.emplace_back(
Patrick Williams59d494e2022-07-22 19:26:55 -0500210 std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous002d39b2022-05-31 08:59:27 -0700211 *crow::connections::systemBus, propertiesMatchString,
Ed Tanous2c70f802020-09-28 14:29:23 -0700212 onPropertyUpdate, &conn));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700214 else
215 {
216 // If interfaces were specified, add a match for each
217 // interface
218 for (const std::string& interface : thisSession.interfaces)
219 {
220 if (!std::regex_match(interface, validInterface))
221 {
222 BMCWEB_LOG_ERROR << "Invalid interface name "
223 << interface;
224 conn.close();
225 return;
226 }
227 std::string ifaceMatchString = propertiesMatchString;
228 ifaceMatchString += ",arg0='";
229 ifaceMatchString += interface;
230 ifaceMatchString += "'";
231 BMCWEB_LOG_DEBUG << "Creating match " << ifaceMatchString;
232 thisSession.matches.emplace_back(
Patrick Williams59d494e2022-07-22 19:26:55 -0500233 std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous002d39b2022-05-31 08:59:27 -0700234 *crow::connections::systemBus, ifaceMatchString,
235 onPropertyUpdate, &conn));
236 }
237 }
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700238 std::string objectManagerMatchString =
Ed Tanous002d39b2022-05-31 08:59:27 -0700239 ("type='signal',"
240 "interface='org.freedesktop.DBus.ObjectManager',"
241 "path_namespace='" +
242 *thisPathString +
243 "',"
244 "member='InterfacesAdded'");
245 BMCWEB_LOG_DEBUG << "Creating match " << objectManagerMatchString;
246 thisSession.matches.emplace_back(
Patrick Williams59d494e2022-07-22 19:26:55 -0500247 std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous002d39b2022-05-31 08:59:27 -0700248 *crow::connections::systemBus, objectManagerMatchString,
249 onPropertyUpdate, &conn));
250 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251 });
Ed Tanous911ac312017-08-15 09:37:42 -0700252}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253} // namespace dbus_monitor
254} // namespace crow