summaryrefslogtreecommitdiff
path: root/include/dbus_monitor.hpp
blob: db75fce8238d8029b07a114748c5665bafe90216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#pragma once
#include "app.hpp"
#include "async_resp.hpp"
#include "dbus_singleton.hpp"
#include "openbmc_dbus_rest.hpp"
#include "websocket.hpp"

#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>
#include <sdbusplus/bus/match.hpp>
#include <sdbusplus/message/types.hpp>

#include <variant>

namespace crow
{
namespace dbus_monitor
{

struct DbusWebsocketSession
{
    std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
    boost::container::flat_set<std::string, std::less<>,
                               std::vector<std::string>>
        interfaces;
};

using SessionMap = boost::container::flat_map<crow::websocket::Connection*,
                                              DbusWebsocketSession>;

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static SessionMap sessions;

inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
                            sd_bus_error* retError)
{
    if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
    {
        BMCWEB_LOG_ERROR("Got sdbus error on match");
        return 0;
    }
    crow::websocket::Connection* connection =
        static_cast<crow::websocket::Connection*>(userdata);
    auto thisSession = sessions.find(connection);
    if (thisSession == sessions.end())
    {
        BMCWEB_LOG_ERROR("Couldn't find dbus connection {}",
                         logPtr(connection));
        return 0;
    }
    sdbusplus::message_t message(m);
    nlohmann::json json;
    json["event"] = message.get_member();
    json["path"] = message.get_path();
    if (strcmp(message.get_member(), "PropertiesChanged") == 0)
    {
        nlohmann::json data;
        int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
        if (r < 0)
        {
            BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
            return 0;
        }
        if (!data.is_array())
        {
            BMCWEB_LOG_ERROR("No data in PropertiesChanged signal");
            return 0;
        }

        // data is type sa{sv}as and is an array[3] of string, object, array
        json["interface"] = data[0];
        json["properties"] = data[1];
    }
    else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
    {
        nlohmann::json data;
        int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
        if (r < 0)
        {
            BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
            return 0;
        }

        if (!data.is_array())
        {
            BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
            return 0;
        }

        // data is type oa{sa{sv}} which is an array[2] of string, object
        for (const auto& entry : data[1].items())
        {
            auto it = thisSession->second.interfaces.find(entry.key());
            if (it != thisSession->second.interfaces.end())
            {
                json["interfaces"][entry.key()] = entry.value();
            }
        }
    }
    else
    {
        BMCWEB_LOG_CRITICAL("message {} was unexpected", message.get_member());
        return 0;
    }

    connection->sendText(
        json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace));
    return 0;
}

inline void requestRoutes(App& app)
{
    BMCWEB_ROUTE(app, "/subscribe")
        .privileges({{"Login"}})
        .websocket()
        .onopen([&](crow::websocket::Connection& conn) {
        BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
        sessions.try_emplace(&conn);
    })
        .onclose([&](crow::websocket::Connection& conn, const std::string&) {
        sessions.erase(&conn);
    })
        .onmessage([&](crow::websocket::Connection& conn,
                       const std::string& data, bool) {
        const auto sessionPair = sessions.find(&conn);
        if (sessionPair == sessions.end())
        {
            conn.close("Internal error");
        }
        DbusWebsocketSession& thisSession = sessionPair->second;
        BMCWEB_LOG_DEBUG("Connection {} received {}", logPtr(&conn), data);
        nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
        if (j.is_discarded())
        {
            BMCWEB_LOG_ERROR("Unable to parse json data for monitor");
            conn.close("Unable to parse json request");
            return;
        }
        nlohmann::json::iterator interfaces = j.find("interfaces");
        if (interfaces != j.end())
        {
            thisSession.interfaces.reserve(interfaces->size());
            for (auto& interface : *interfaces)
            {
                const std::string* str =
                    interface.get_ptr<const std::string*>();
                if (str != nullptr)
                {
                    thisSession.interfaces.insert(*str);
                }
            }
        }

        nlohmann::json::iterator paths = j.find("paths");
        if (paths == j.end())
        {
            BMCWEB_LOG_ERROR("Unable to find paths in json data");
            conn.close("Unable to find paths in json data");
            return;
        }

        size_t interfaceCount = thisSession.interfaces.size();
        if (interfaceCount == 0)
        {
            interfaceCount = 1;
        }

        // These regexes derived on the rules here:
        // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
        static std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
        static std::regex validInterface(
            "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");

        for (const auto& thisPath : *paths)
        {
            const std::string* thisPathString =
                thisPath.get_ptr<const std::string*>();
            if (thisPathString == nullptr)
            {
                BMCWEB_LOG_ERROR("subscribe path isn't a string?");
                conn.close();
                return;
            }
            if (!std::regex_match(*thisPathString, validPath))
            {
                BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString);
                conn.close();
                return;
            }
            std::string propertiesMatchString =
                ("type='signal',"
                 "interface='org.freedesktop.DBus.Properties',"
                 "path_namespace='" +
                 *thisPathString +
                 "',"
                 "member='PropertiesChanged'");
            // If interfaces weren't specified, add a single match for all
            // interfaces
            if (thisSession.interfaces.empty())
            {
                BMCWEB_LOG_DEBUG("Creating match {}", propertiesMatchString);

                thisSession.matches.emplace_back(
                    std::make_unique<sdbusplus::bus::match_t>(
                        *crow::connections::systemBus, propertiesMatchString,
                        onPropertyUpdate, &conn));
            }
            else
            {
                // If interfaces were specified, add a match for each
                // interface
                for (const std::string& interface : thisSession.interfaces)
                {
                    if (!std::regex_match(interface, validInterface))
                    {
                        BMCWEB_LOG_ERROR("Invalid interface name {}",
                                         interface);
                        conn.close();
                        return;
                    }
                    std::string ifaceMatchString = propertiesMatchString;
                    ifaceMatchString += ",arg0='";
                    ifaceMatchString += interface;
                    ifaceMatchString += "'";
                    BMCWEB_LOG_DEBUG("Creating match {}", ifaceMatchString);
                    thisSession.matches.emplace_back(
                        std::make_unique<sdbusplus::bus::match_t>(
                            *crow::connections::systemBus, ifaceMatchString,
                            onPropertyUpdate, &conn));
                }
            }
            std::string objectManagerMatchString =
                ("type='signal',"
                 "interface='org.freedesktop.DBus.ObjectManager',"
                 "path_namespace='" +
                 *thisPathString +
                 "',"
                 "member='InterfacesAdded'");
            BMCWEB_LOG_DEBUG("Creating match {}", objectManagerMatchString);
            thisSession.matches.emplace_back(
                std::make_unique<sdbusplus::bus::match_t>(
                    *crow::connections::systemBus, objectManagerMatchString,
                    onPropertyUpdate, &conn));
        }
    });
}
} // namespace dbus_monitor
} // namespace crow