blob: 6ec43d07fb19a9b207981d1d40c5a0a0163f9123 [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 {
Ed Tanous62598e32023-07-17 17:06:25 -070039 BMCWEB_LOG_ERROR("Got sdbus error on match");
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 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 {
Ed Tanous62598e32023-07-17 17:06:25 -070047 BMCWEB_LOG_ERROR("Couldn't find dbus connection {}",
48 logPtr(connection));
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 return 0;
50 }
Patrick Williams59d494e2022-07-22 19:26:55 -050051 sdbusplus::message_t message(m);
Ed Tanous14766872022-03-15 10:44:42 -070052 nlohmann::json json;
53 json["event"] = message.get_member();
54 json["path"] = message.get_path();
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
56 {
Matt Spinler715748a2019-01-30 13:22:28 -060057 nlohmann::json data;
58 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
59 if (r < 0)
60 {
Ed Tanous62598e32023-07-17 17:06:25 -070061 BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
Matt Spinler715748a2019-01-30 13:22:28 -060062 return 0;
63 }
64 if (!data.is_array())
65 {
Ed Tanous62598e32023-07-17 17:06:25 -070066 BMCWEB_LOG_ERROR("No data in PropertiesChanged signal");
Matt Spinler715748a2019-01-30 13:22:28 -060067 return 0;
68 }
69
70 // data is type sa{sv}as and is an array[3] of string, object, array
Ed Tanous14766872022-03-15 10:44:42 -070071 json["interface"] = data[0];
72 json["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 }
74 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
75 {
Matt Spinler715748a2019-01-30 13:22:28 -060076 nlohmann::json data;
77 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
78 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 {
Ed Tanous62598e32023-07-17 17:06:25 -070080 BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
Matt Spinler715748a2019-01-30 13:22:28 -060081 return 0;
82 }
Ed Tanous0bdda662023-08-03 17:27:34 -070083 nlohmann::json::array_t* arr = data.get_ptr<nlohmann::json::array_t*>();
84 if (arr == nullptr)
85 {
86 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
87 return 0;
88 }
89 if (arr->size() < 2)
Matt Spinler715748a2019-01-30 13:22:28 -060090 {
Ed Tanous62598e32023-07-17 17:06:25 -070091 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
Matt Spinler715748a2019-01-30 13:22:28 -060092 return 0;
93 }
94
Ed Tanous0bdda662023-08-03 17:27:34 -070095 nlohmann::json::object_t* obj =
96 (*arr)[1].get_ptr<nlohmann::json::object_t*>();
97 if (obj == nullptr)
Matt Spinler715748a2019-01-30 13:22:28 -060098 {
Ed Tanous0bdda662023-08-03 17:27:34 -070099 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
100 return 0;
101 }
102 // data is type oa{sa{sv}} which is an array[2] of string, object
103 for (const auto& entry : *obj)
104 {
105 auto it = thisSession->second.interfaces.find(entry.first);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 if (it != thisSession->second.interfaces.end())
107 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700108 json["interfaces"][entry.first] = entry.second;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 }
110 }
111 }
112 else
113 {
Ed Tanous62598e32023-07-17 17:06:25 -0700114 BMCWEB_LOG_CRITICAL("message {} was unexpected", message.get_member());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 return 0;
116 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700117
Ed Tanous71f52d92021-02-19 08:51:17 -0800118 connection->sendText(
Ed Tanous14766872022-03-15 10:44:42 -0700119 json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700121}
Ed Tanous911ac312017-08-15 09:37:42 -0700122
Ed Tanous23a21a12020-07-25 04:45:05 +0000123inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700124{
125 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous432a8902021-06-14 15:28:56 -0700126 .privileges({{"Login"}})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127 .websocket()
zhanghch0577726382021-10-21 14:07:57 +0800128 .onopen([&](crow::websocket::Connection& conn) {
Patrick Williams5a39f772023-10-20 11:20:21 -0500129 BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
130 sessions.try_emplace(&conn);
131 })
Ed Tanouscb13a392020-07-25 19:02:03 +0000132 .onclose([&](crow::websocket::Connection& conn, const std::string&) {
Patrick Williams5a39f772023-10-20 11:20:21 -0500133 sessions.erase(&conn);
134 })
Ed Tanous62598e32023-07-17 17:06:25 -0700135 .onmessage([&](crow::websocket::Connection& conn,
136 const std::string& data, bool) {
Patrick Williams5a39f772023-10-20 11:20:21 -0500137 const auto sessionPair = sessions.find(&conn);
138 if (sessionPair == sessions.end())
139 {
140 conn.close("Internal error");
141 }
142 DbusWebsocketSession& thisSession = sessionPair->second;
143 BMCWEB_LOG_DEBUG("Connection {} received {}", logPtr(&conn), data);
144 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
145 if (j.is_discarded())
146 {
147 BMCWEB_LOG_ERROR("Unable to parse json data for monitor");
148 conn.close("Unable to parse json request");
149 return;
150 }
151 nlohmann::json::iterator interfaces = j.find("interfaces");
152 if (interfaces != j.end())
153 {
154 thisSession.interfaces.reserve(interfaces->size());
155 for (auto& interface : *interfaces)
Ed Tanous24b2fe82022-01-06 12:45:54 -0800156 {
Patrick Williams5a39f772023-10-20 11:20:21 -0500157 const std::string* str =
158 interface.get_ptr<const std::string*>();
159 if (str != nullptr)
160 {
161 thisSession.interfaces.insert(*str);
162 }
Ed Tanous62598e32023-07-17 17:06:25 -0700163 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500164 }
165
166 nlohmann::json::iterator paths = j.find("paths");
167 if (paths == j.end())
168 {
169 BMCWEB_LOG_ERROR("Unable to find paths in json data");
170 conn.close("Unable to find paths in json data");
171 return;
172 }
173
174 size_t interfaceCount = thisSession.interfaces.size();
175 if (interfaceCount == 0)
176 {
177 interfaceCount = 1;
178 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500179
180 // These regexes derived on the rules here:
181 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
182 static std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
183 static std::regex validInterface(
184 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
185
186 for (const auto& thisPath : *paths)
187 {
188 const std::string* thisPathString =
189 thisPath.get_ptr<const std::string*>();
190 if (thisPathString == nullptr)
Ed Tanous62598e32023-07-17 17:06:25 -0700191 {
Patrick Williams5a39f772023-10-20 11:20:21 -0500192 BMCWEB_LOG_ERROR("subscribe path isn't a string?");
193 conn.close();
Ed Tanous62598e32023-07-17 17:06:25 -0700194 return;
195 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500196 if (!std::regex_match(*thisPathString, validPath))
Ed Tanous62598e32023-07-17 17:06:25 -0700197 {
Patrick Williams5a39f772023-10-20 11:20:21 -0500198 BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString);
199 conn.close();
Ed Tanous9b243a42018-08-03 14:33:10 -0700200 return;
Ed Tanous9b243a42018-08-03 14:33:10 -0700201 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500202 std::string propertiesMatchString =
203 ("type='signal',"
204 "interface='org.freedesktop.DBus.Properties',"
205 "path_namespace='" +
206 *thisPathString +
207 "',"
208 "member='PropertiesChanged'");
209 // If interfaces weren't specified, add a single match for all
210 // interfaces
211 if (thisSession.interfaces.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -0700212 {
Patrick Williams5a39f772023-10-20 11:20:21 -0500213 BMCWEB_LOG_DEBUG("Creating match {}", propertiesMatchString);
Ed Tanous62598e32023-07-17 17:06:25 -0700214
Ed Tanous62598e32023-07-17 17:06:25 -0700215 thisSession.matches.emplace_back(
216 std::make_unique<sdbusplus::bus::match_t>(
Patrick Williams5a39f772023-10-20 11:20:21 -0500217 *crow::connections::systemBus, propertiesMatchString,
Ed Tanous62598e32023-07-17 17:06:25 -0700218 onPropertyUpdate, &conn));
Ed Tanous002d39b2022-05-31 08:59:27 -0700219 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500220 else
221 {
222 // If interfaces were specified, add a match for each
223 // interface
224 for (const std::string& interface : thisSession.interfaces)
225 {
226 if (!std::regex_match(interface, validInterface))
227 {
228 BMCWEB_LOG_ERROR("Invalid interface name {}",
229 interface);
230 conn.close();
231 return;
232 }
233 std::string ifaceMatchString = propertiesMatchString;
234 ifaceMatchString += ",arg0='";
235 ifaceMatchString += interface;
236 ifaceMatchString += "'";
237 BMCWEB_LOG_DEBUG("Creating match {}", ifaceMatchString);
238 thisSession.matches.emplace_back(
239 std::make_unique<sdbusplus::bus::match_t>(
240 *crow::connections::systemBus, ifaceMatchString,
241 onPropertyUpdate, &conn));
242 }
243 }
244 std::string objectManagerMatchString =
245 ("type='signal',"
246 "interface='org.freedesktop.DBus.ObjectManager',"
247 "path_namespace='" +
248 *thisPathString +
249 "',"
250 "member='InterfacesAdded'");
251 BMCWEB_LOG_DEBUG("Creating match {}", objectManagerMatchString);
252 thisSession.matches.emplace_back(
253 std::make_unique<sdbusplus::bus::match_t>(
254 *crow::connections::systemBus, objectManagerMatchString,
255 onPropertyUpdate, &conn));
256 }
257 });
Ed Tanous911ac312017-08-15 09:37:42 -0700258}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259} // namespace dbus_monitor
260} // namespace crow