blob: 050bd3421e7cc1433f70e9ee294fc694cc811a27 [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"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "dbus_singleton.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08006#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007#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 Tanousd7857202025-01-28 15:32:26 -080010#include <systemd/sd-bus.h>
11
Ed Tanous911ac312017-08-15 09:37:42 -070012#include <boost/container/flat_map.hpp>
Ed Tanous9b243a42018-08-03 14:33:10 -070013#include <boost/container/flat_set.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080014#include <nlohmann/json.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070015#include <sdbusplus/bus/match.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080016#include <sdbusplus/message.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050017
Ed Tanousd7857202025-01-28 15:32:26 -080018#include <cstddef>
19#include <cstring>
20#include <functional>
21#include <memory>
22#include <regex>
23#include <string>
24#include <vector>
Ed Tanous9b243a42018-08-03 14:33:10 -070025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace crow
27{
28namespace dbus_monitor
29{
Ed Tanous911ac312017-08-15 09:37:42 -070030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031struct DbusWebsocketSession
32{
Patrick Williams59d494e2022-07-22 19:26:55 -050033 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
Ed Tanous24b2fe82022-01-06 12:45:54 -080034 boost::container::flat_set<std::string, std::less<>,
35 std::vector<std::string>>
36 interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070037};
38
Ed Tanouscf9e4172022-12-21 09:30:16 -080039using SessionMap = boost::container::flat_map<crow::websocket::Connection*,
40 DbusWebsocketSession>;
41
42// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
43static SessionMap sessions;
Ed Tanous911ac312017-08-15 09:37:42 -070044
Ed Tanous9b243a42018-08-03 14:33:10 -070045inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous81ce6092020-12-17 16:54:55 +000046 sd_bus_error* retError)
Ed Tanous1abe55e2018-09-05 08:30:59 -070047{
Ed Tanouse662eae2022-01-25 10:39:19 -080048 if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 {
Ed Tanous62598e32023-07-17 17:06:25 -070050 BMCWEB_LOG_ERROR("Got sdbus error on match");
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070052 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 crow::websocket::Connection* connection =
54 static_cast<crow::websocket::Connection*>(userdata);
55 auto thisSession = sessions.find(connection);
56 if (thisSession == sessions.end())
57 {
Ed Tanous62598e32023-07-17 17:06:25 -070058 BMCWEB_LOG_ERROR("Couldn't find dbus connection {}",
59 logPtr(connection));
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 return 0;
61 }
Patrick Williams59d494e2022-07-22 19:26:55 -050062 sdbusplus::message_t message(m);
Ed Tanous14766872022-03-15 10:44:42 -070063 nlohmann::json json;
64 json["event"] = message.get_member();
65 json["path"] = message.get_path();
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
67 {
Matt Spinler715748a2019-01-30 13:22:28 -060068 nlohmann::json data;
69 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
70 if (r < 0)
71 {
Ed Tanous62598e32023-07-17 17:06:25 -070072 BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
Matt Spinler715748a2019-01-30 13:22:28 -060073 return 0;
74 }
75 if (!data.is_array())
76 {
Ed Tanous62598e32023-07-17 17:06:25 -070077 BMCWEB_LOG_ERROR("No data in PropertiesChanged signal");
Matt Spinler715748a2019-01-30 13:22:28 -060078 return 0;
79 }
80
81 // data is type sa{sv}as and is an array[3] of string, object, array
Ed Tanous14766872022-03-15 10:44:42 -070082 json["interface"] = data[0];
83 json["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 }
85 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
86 {
Matt Spinler715748a2019-01-30 13:22:28 -060087 nlohmann::json data;
88 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
89 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 {
Ed Tanous62598e32023-07-17 17:06:25 -070091 BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
Matt Spinler715748a2019-01-30 13:22:28 -060092 return 0;
93 }
Ed Tanous0bdda662023-08-03 17:27:34 -070094 nlohmann::json::array_t* arr = data.get_ptr<nlohmann::json::array_t*>();
95 if (arr == nullptr)
96 {
97 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
98 return 0;
99 }
100 if (arr->size() < 2)
Matt Spinler715748a2019-01-30 13:22:28 -0600101 {
Ed Tanous62598e32023-07-17 17:06:25 -0700102 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
Matt Spinler715748a2019-01-30 13:22:28 -0600103 return 0;
104 }
105
Ed Tanous0bdda662023-08-03 17:27:34 -0700106 nlohmann::json::object_t* obj =
107 (*arr)[1].get_ptr<nlohmann::json::object_t*>();
108 if (obj == nullptr)
Matt Spinler715748a2019-01-30 13:22:28 -0600109 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700110 BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
111 return 0;
112 }
113 // data is type oa{sa{sv}} which is an array[2] of string, object
114 for (const auto& entry : *obj)
115 {
116 auto it = thisSession->second.interfaces.find(entry.first);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 if (it != thisSession->second.interfaces.end())
118 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700119 json["interfaces"][entry.first] = entry.second;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 }
121 }
122 }
123 else
124 {
Ed Tanous62598e32023-07-17 17:06:25 -0700125 BMCWEB_LOG_CRITICAL("message {} was unexpected", message.get_member());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126 return 0;
127 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700128
Ed Tanous71f52d92021-02-19 08:51:17 -0800129 connection->sendText(
Ed Tanous14766872022-03-15 10:44:42 -0700130 json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700132}
Ed Tanous911ac312017-08-15 09:37:42 -0700133
Ed Tanous23a21a12020-07-25 04:45:05 +0000134inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700135{
136 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous432a8902021-06-14 15:28:56 -0700137 .privileges({{"Login"}})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138 .websocket()
Ed Tanous25ce6202024-09-06 16:01:24 -0700139 .onopen([](crow::websocket::Connection& conn) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400140 BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
141 sessions.try_emplace(&conn);
142 })
Ed Tanous25ce6202024-09-06 16:01:24 -0700143 .onclose([](crow::websocket::Connection& conn, const std::string&) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400144 sessions.erase(&conn);
145 })
Ed Tanous25ce6202024-09-06 16:01:24 -0700146 .onmessage([](crow::websocket::Connection& conn,
147 const std::string& data, bool) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400148 const auto sessionPair = sessions.find(&conn);
149 if (sessionPair == sessions.end())
Ed Tanous24b2fe82022-01-06 12:45:54 -0800150 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400151 conn.close("Internal error");
152 }
153 DbusWebsocketSession& thisSession = sessionPair->second;
154 BMCWEB_LOG_DEBUG("Connection {} received {}", logPtr(&conn), data);
155 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
156 if (j.is_discarded())
157 {
158 BMCWEB_LOG_ERROR("Unable to parse json data for monitor");
159 conn.close("Unable to parse json request");
160 return;
161 }
162 nlohmann::json::iterator interfaces = j.find("interfaces");
163 if (interfaces != j.end())
164 {
165 thisSession.interfaces.reserve(interfaces->size());
166 for (auto& interface : *interfaces)
Patrick Williams5a39f772023-10-20 11:20:21 -0500167 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400168 const std::string* str =
169 interface.get_ptr<const std::string*>();
170 if (str != nullptr)
171 {
172 thisSession.interfaces.insert(*str);
173 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500174 }
Ed Tanous62598e32023-07-17 17:06:25 -0700175 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500176
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400177 nlohmann::json::iterator paths = j.find("paths");
178 if (paths == j.end())
Ed Tanous62598e32023-07-17 17:06:25 -0700179 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400180 BMCWEB_LOG_ERROR("Unable to find paths in json data");
181 conn.close("Unable to find paths in json data");
Ed Tanous62598e32023-07-17 17:06:25 -0700182 return;
183 }
Ed Tanous62598e32023-07-17 17:06:25 -0700184
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400185 size_t interfaceCount = thisSession.interfaces.size();
186 if (interfaceCount == 0)
Patrick Williams5a39f772023-10-20 11:20:21 -0500187 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400188 interfaceCount = 1;
189 }
190
191 // These regexes derived on the rules here:
192 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
193 static std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
194 static std::regex validInterface(
195 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
196
197 for (const auto& thisPath : *paths)
198 {
199 const std::string* thisPathString =
200 thisPath.get_ptr<const std::string*>();
201 if (thisPathString == nullptr)
Patrick Williams5a39f772023-10-20 11:20:21 -0500202 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400203 BMCWEB_LOG_ERROR("subscribe path isn't a string?");
204 conn.close();
205 return;
206 }
207 if (!std::regex_match(*thisPathString, validPath))
208 {
209 BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString);
210 conn.close();
211 return;
212 }
213 std::string propertiesMatchString =
214 ("type='signal',"
215 "interface='org.freedesktop.DBus.Properties',"
216 "path_namespace='" +
217 *thisPathString +
218 "',"
219 "member='PropertiesChanged'");
220 // If interfaces weren't specified, add a single match for all
221 // interfaces
222 if (thisSession.interfaces.empty())
223 {
224 BMCWEB_LOG_DEBUG("Creating match {}",
225 propertiesMatchString);
226
Patrick Williams5a39f772023-10-20 11:20:21 -0500227 thisSession.matches.emplace_back(
228 std::make_unique<sdbusplus::bus::match_t>(
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400229 *crow::connections::systemBus,
230 propertiesMatchString, onPropertyUpdate, &conn));
Patrick Williams5a39f772023-10-20 11:20:21 -0500231 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400232 else
233 {
234 // If interfaces were specified, add a match for each
235 // interface
236 for (const std::string& interface : thisSession.interfaces)
237 {
238 if (!std::regex_match(interface, validInterface))
239 {
240 BMCWEB_LOG_ERROR("Invalid interface name {}",
241 interface);
242 conn.close();
243 return;
244 }
245 std::string ifaceMatchString = propertiesMatchString;
246 ifaceMatchString += ",arg0='";
247 ifaceMatchString += interface;
248 ifaceMatchString += "'";
249 BMCWEB_LOG_DEBUG("Creating match {}", ifaceMatchString);
250 thisSession.matches.emplace_back(
251 std::make_unique<sdbusplus::bus::match_t>(
252 *crow::connections::systemBus, ifaceMatchString,
253 onPropertyUpdate, &conn));
254 }
255 }
256 std::string objectManagerMatchString =
257 ("type='signal',"
258 "interface='org.freedesktop.DBus.ObjectManager',"
259 "path_namespace='" +
260 *thisPathString +
261 "',"
262 "member='InterfacesAdded'");
263 BMCWEB_LOG_DEBUG("Creating match {}", objectManagerMatchString);
264 thisSession.matches.emplace_back(
265 std::make_unique<sdbusplus::bus::match_t>(
266 *crow::connections::systemBus, objectManagerMatchString,
267 onPropertyUpdate, &conn));
Patrick Williams5a39f772023-10-20 11:20:21 -0500268 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400269 });
Ed Tanous911ac312017-08-15 09:37:42 -0700270}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700271} // namespace dbus_monitor
272} // namespace crow