blob: f468cc4831b8abc86a99fccc822efd20a1dd70c7 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous911ac312017-08-15 09:37:42 -07003#pragma once
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08004#include "app.hpp"
5#include "async_resp.hpp"
6#include "dbus_singleton.hpp"
7#include "openbmc_dbus_rest.hpp"
Ed Tanousfaf100f2023-05-25 10:03:14 -07008#include "websocket.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08009
Ed Tanous911ac312017-08-15 09:37:42 -070010#include <boost/container/flat_map.hpp>
Ed Tanous9b243a42018-08-03 14:33:10 -070011#include <boost/container/flat_set.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070012#include <sdbusplus/bus/match.hpp>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -070013#include <sdbusplus/message/types.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050014
Ed Tanousabf2add2019-01-22 16:40:12 -080015#include <variant>
Ed Tanous9b243a42018-08-03 14:33:10 -070016
Ed Tanous1abe55e2018-09-05 08:30:59 -070017namespace crow
18{
19namespace dbus_monitor
20{
Ed Tanous911ac312017-08-15 09:37:42 -070021
Ed Tanous1abe55e2018-09-05 08:30:59 -070022struct DbusWebsocketSession
23{
Patrick Williams59d494e2022-07-22 19:26:55 -050024 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
Ed Tanous24b2fe82022-01-06 12:45:54 -080025 boost::container::flat_set<std::string, std::less<>,
26 std::vector<std::string>>
27 interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070028};
29
Ed Tanouscf9e4172022-12-21 09:30:16 -080030using SessionMap = boost::container::flat_map<crow::websocket::Connection*,
31 DbusWebsocketSession>;
32
33// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
34static SessionMap sessions;
Ed Tanous911ac312017-08-15 09:37:42 -070035
Ed Tanous9b243a42018-08-03 14:33:10 -070036inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous81ce6092020-12-17 16:54:55 +000037 sd_bus_error* retError)
Ed Tanous1abe55e2018-09-05 08:30:59 -070038{
Ed Tanouse662eae2022-01-25 10:39:19 -080039 if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 {
Ed Tanous62598e32023-07-17 17:06:25 -070041 BMCWEB_LOG_ERROR("Got sdbus error on match");
Ed Tanous1abe55e2018-09-05 08:30:59 -070042 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070043 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070044 crow::websocket::Connection* connection =
45 static_cast<crow::websocket::Connection*>(userdata);
46 auto thisSession = sessions.find(connection);
47 if (thisSession == sessions.end())
48 {
Ed Tanous62598e32023-07-17 17:06:25 -070049 BMCWEB_LOG_ERROR("Couldn't find dbus connection {}",
50 logPtr(connection));
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 return 0;
52 }
Patrick Williams59d494e2022-07-22 19:26:55 -050053 sdbusplus::message_t message(m);
Ed Tanous14766872022-03-15 10:44:42 -070054 nlohmann::json json;
55 json["event"] = message.get_member();
56 json["path"] = message.get_path();
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
58 {
Matt Spinler715748a2019-01-30 13:22:28 -060059 nlohmann::json data;
60 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
61 if (r < 0)
62 {
Ed Tanous62598e32023-07-17 17:06:25 -070063 BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
Matt Spinler715748a2019-01-30 13:22:28 -060064 return 0;
65 }
66 if (!data.is_array())
67 {
Ed Tanous62598e32023-07-17 17:06:25 -070068 BMCWEB_LOG_ERROR("No data in PropertiesChanged signal");
Matt Spinler715748a2019-01-30 13:22:28 -060069 return 0;
70 }
71
72 // data is type sa{sv}as and is an array[3] of string, object, array
Ed Tanous14766872022-03-15 10:44:42 -070073 json["interface"] = data[0];
74 json["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 }
76 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
77 {
Matt Spinler715748a2019-01-30 13:22:28 -060078 nlohmann::json data;
79 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
80 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 {
Ed Tanous62598e32023-07-17 17:06:25 -070082 BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
Matt Spinler715748a2019-01-30 13:22:28 -060083 return 0;
84 }
Ed Tanous0bdda662023-08-03 17:27:34 -070085 nlohmann::json::array_t* arr = data.get_ptr<nlohmann::json::array_t*>();
86 if (arr == nullptr)
87 {
88 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
89 return 0;
90 }
91 if (arr->size() < 2)
Matt Spinler715748a2019-01-30 13:22:28 -060092 {
Ed Tanous62598e32023-07-17 17:06:25 -070093 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
Matt Spinler715748a2019-01-30 13:22:28 -060094 return 0;
95 }
96
Ed Tanous0bdda662023-08-03 17:27:34 -070097 nlohmann::json::object_t* obj =
98 (*arr)[1].get_ptr<nlohmann::json::object_t*>();
99 if (obj == nullptr)
Matt Spinler715748a2019-01-30 13:22:28 -0600100 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700101 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
102 return 0;
103 }
104 // data is type oa{sa{sv}} which is an array[2] of string, object
105 for (const auto& entry : *obj)
106 {
107 auto it = thisSession->second.interfaces.find(entry.first);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 if (it != thisSession->second.interfaces.end())
109 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700110 json["interfaces"][entry.first] = entry.second;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 }
112 }
113 }
114 else
115 {
Ed Tanous62598e32023-07-17 17:06:25 -0700116 BMCWEB_LOG_CRITICAL("message {} was unexpected", message.get_member());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 return 0;
118 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700119
Ed Tanous71f52d92021-02-19 08:51:17 -0800120 connection->sendText(
Ed Tanous14766872022-03-15 10:44:42 -0700121 json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700123}
Ed Tanous911ac312017-08-15 09:37:42 -0700124
Ed Tanous23a21a12020-07-25 04:45:05 +0000125inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126{
127 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous432a8902021-06-14 15:28:56 -0700128 .privileges({{"Login"}})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129 .websocket()
Ed Tanous25ce6202024-09-06 16:01:24 -0700130 .onopen([](crow::websocket::Connection& conn) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400131 BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
132 sessions.try_emplace(&conn);
133 })
Ed Tanous25ce6202024-09-06 16:01:24 -0700134 .onclose([](crow::websocket::Connection& conn, const std::string&) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400135 sessions.erase(&conn);
136 })
Ed Tanous25ce6202024-09-06 16:01:24 -0700137 .onmessage([](crow::websocket::Connection& conn,
138 const std::string& data, bool) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400139 const auto sessionPair = sessions.find(&conn);
140 if (sessionPair == sessions.end())
Ed Tanous24b2fe82022-01-06 12:45:54 -0800141 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400142 conn.close("Internal error");
143 }
144 DbusWebsocketSession& thisSession = sessionPair->second;
145 BMCWEB_LOG_DEBUG("Connection {} received {}", logPtr(&conn), data);
146 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
147 if (j.is_discarded())
148 {
149 BMCWEB_LOG_ERROR("Unable to parse json data for monitor");
150 conn.close("Unable to parse json request");
151 return;
152 }
153 nlohmann::json::iterator interfaces = j.find("interfaces");
154 if (interfaces != j.end())
155 {
156 thisSession.interfaces.reserve(interfaces->size());
157 for (auto& interface : *interfaces)
Patrick Williams5a39f772023-10-20 11:20:21 -0500158 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400159 const std::string* str =
160 interface.get_ptr<const std::string*>();
161 if (str != nullptr)
162 {
163 thisSession.interfaces.insert(*str);
164 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500165 }
Ed Tanous62598e32023-07-17 17:06:25 -0700166 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500167
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400168 nlohmann::json::iterator paths = j.find("paths");
169 if (paths == j.end())
Ed Tanous62598e32023-07-17 17:06:25 -0700170 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400171 BMCWEB_LOG_ERROR("Unable to find paths in json data");
172 conn.close("Unable to find paths in json data");
Ed Tanous62598e32023-07-17 17:06:25 -0700173 return;
174 }
Ed Tanous62598e32023-07-17 17:06:25 -0700175
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400176 size_t interfaceCount = thisSession.interfaces.size();
177 if (interfaceCount == 0)
Patrick Williams5a39f772023-10-20 11:20:21 -0500178 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400179 interfaceCount = 1;
180 }
181
182 // These regexes derived on the rules here:
183 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
184 static std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
185 static std::regex validInterface(
186 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
187
188 for (const auto& thisPath : *paths)
189 {
190 const std::string* thisPathString =
191 thisPath.get_ptr<const std::string*>();
192 if (thisPathString == nullptr)
Patrick Williams5a39f772023-10-20 11:20:21 -0500193 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400194 BMCWEB_LOG_ERROR("subscribe path isn't a string?");
195 conn.close();
196 return;
197 }
198 if (!std::regex_match(*thisPathString, validPath))
199 {
200 BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString);
201 conn.close();
202 return;
203 }
204 std::string propertiesMatchString =
205 ("type='signal',"
206 "interface='org.freedesktop.DBus.Properties',"
207 "path_namespace='" +
208 *thisPathString +
209 "',"
210 "member='PropertiesChanged'");
211 // If interfaces weren't specified, add a single match for all
212 // interfaces
213 if (thisSession.interfaces.empty())
214 {
215 BMCWEB_LOG_DEBUG("Creating match {}",
216 propertiesMatchString);
217
Patrick Williams5a39f772023-10-20 11:20:21 -0500218 thisSession.matches.emplace_back(
219 std::make_unique<sdbusplus::bus::match_t>(
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400220 *crow::connections::systemBus,
221 propertiesMatchString, onPropertyUpdate, &conn));
Patrick Williams5a39f772023-10-20 11:20:21 -0500222 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400223 else
224 {
225 // If interfaces were specified, add a match for each
226 // interface
227 for (const std::string& interface : thisSession.interfaces)
228 {
229 if (!std::regex_match(interface, validInterface))
230 {
231 BMCWEB_LOG_ERROR("Invalid interface name {}",
232 interface);
233 conn.close();
234 return;
235 }
236 std::string ifaceMatchString = propertiesMatchString;
237 ifaceMatchString += ",arg0='";
238 ifaceMatchString += interface;
239 ifaceMatchString += "'";
240 BMCWEB_LOG_DEBUG("Creating match {}", ifaceMatchString);
241 thisSession.matches.emplace_back(
242 std::make_unique<sdbusplus::bus::match_t>(
243 *crow::connections::systemBus, ifaceMatchString,
244 onPropertyUpdate, &conn));
245 }
246 }
247 std::string objectManagerMatchString =
248 ("type='signal',"
249 "interface='org.freedesktop.DBus.ObjectManager',"
250 "path_namespace='" +
251 *thisPathString +
252 "',"
253 "member='InterfacesAdded'");
254 BMCWEB_LOG_DEBUG("Creating match {}", objectManagerMatchString);
255 thisSession.matches.emplace_back(
256 std::make_unique<sdbusplus::bus::match_t>(
257 *crow::connections::systemBus, objectManagerMatchString,
258 onPropertyUpdate, &conn));
Patrick Williams5a39f772023-10-20 11:20:21 -0500259 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400260 });
Ed Tanous911ac312017-08-15 09:37:42 -0700261}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262} // namespace dbus_monitor
263} // namespace crow