xref: /openbmc/bmcweb/include/dbus_monitor.hpp (revision a529a6aa)
1 #pragma once
2 #include "app.hpp"
3 #include "async_resp.hpp"
4 #include "dbus_singleton.hpp"
5 #include "openbmc_dbus_rest.hpp"
6 #include "websocket.hpp"
7 
8 #include <boost/container/flat_map.hpp>
9 #include <boost/container/flat_set.hpp>
10 #include <sdbusplus/bus/match.hpp>
11 #include <sdbusplus/message/types.hpp>
12 
13 #include <variant>
14 
15 namespace crow
16 {
17 namespace dbus_monitor
18 {
19 
20 struct DbusWebsocketSession
21 {
22     std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
23     boost::container::flat_set<std::string, std::less<>,
24                                std::vector<std::string>>
25         interfaces;
26 };
27 
28 using SessionMap = boost::container::flat_map<crow::websocket::Connection*,
29                                               DbusWebsocketSession>;
30 
31 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
32 static SessionMap sessions;
33 
34 inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
35                             sd_bus_error* retError)
36 {
37     if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
38     {
39         BMCWEB_LOG_ERROR("Got sdbus error on match");
40         return 0;
41     }
42     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 {}",
48                          logPtr(connection));
49         return 0;
50     }
51     sdbusplus::message_t message(m);
52     nlohmann::json json;
53     json["event"] = message.get_member();
54     json["path"] = message.get_path();
55     if (strcmp(message.get_member(), "PropertiesChanged") == 0)
56     {
57         nlohmann::json data;
58         int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
59         if (r < 0)
60         {
61             BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
62             return 0;
63         }
64         if (!data.is_array())
65         {
66             BMCWEB_LOG_ERROR("No data in PropertiesChanged signal");
67             return 0;
68         }
69 
70         // data is type sa{sv}as and is an array[3] of string, object, array
71         json["interface"] = data[0];
72         json["properties"] = data[1];
73     }
74     else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
75     {
76         nlohmann::json data;
77         int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
78         if (r < 0)
79         {
80             BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
81             return 0;
82         }
83         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)
90         {
91             BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
92             return 0;
93         }
94 
95         nlohmann::json::object_t* obj =
96             (*arr)[1].get_ptr<nlohmann::json::object_t*>();
97         if (obj == nullptr)
98         {
99             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);
106             if (it != thisSession->second.interfaces.end())
107             {
108                 json["interfaces"][entry.first] = entry.second;
109             }
110         }
111     }
112     else
113     {
114         BMCWEB_LOG_CRITICAL("message {} was unexpected", message.get_member());
115         return 0;
116     }
117 
118     connection->sendText(
119         json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
120     return 0;
121 }
122 
123 inline void requestRoutes(App& app)
124 {
125     BMCWEB_ROUTE(app, "/subscribe")
126         .privileges({{"Login"}})
127         .websocket()
128         .onopen([&](crow::websocket::Connection& conn) {
129         BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
130         sessions.try_emplace(&conn);
131     })
132         .onclose([&](crow::websocket::Connection& conn, const std::string&) {
133         sessions.erase(&conn);
134     })
135         .onmessage([&](crow::websocket::Connection& conn,
136                        const std::string& data, bool) {
137         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)
156             {
157                 const std::string* str =
158                     interface.get_ptr<const std::string*>();
159                 if (str != nullptr)
160                 {
161                     thisSession.interfaces.insert(*str);
162                 }
163             }
164         }
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         }
179 
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)
191             {
192                 BMCWEB_LOG_ERROR("subscribe path isn't a string?");
193                 conn.close();
194                 return;
195             }
196             if (!std::regex_match(*thisPathString, validPath))
197             {
198                 BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString);
199                 conn.close();
200                 return;
201             }
202             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())
212             {
213                 BMCWEB_LOG_DEBUG("Creating match {}", propertiesMatchString);
214 
215                 thisSession.matches.emplace_back(
216                     std::make_unique<sdbusplus::bus::match_t>(
217                         *crow::connections::systemBus, propertiesMatchString,
218                         onPropertyUpdate, &conn));
219             }
220             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     });
258 }
259 } // namespace dbus_monitor
260 } // namespace crow
261