summaryrefslogtreecommitdiffstats
path: root/src/sbus/sssd_dbus_signals.c
blob: 1dc08ae25761881f23a81e7871a8af626497a9a3 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2015 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <talloc.h>
#include <dbus/dbus.h>
#include <dhash.h>

#include "util/util.h"
#include "sbus/sssd_dbus.h"
#include "sbus/sssd_dbus_private.h"

static int sbus_incoming_signal_destructor(struct sbus_incoming_signal *signal)
{
    dbus_message_unref(signal->message);
    return 0;
}

static struct sbus_incoming_signal *
sbus_new_incoming_signal(struct sbus_connection *conn,
                         DBusMessage *message)
{
    struct sbus_incoming_signal *signal;

    signal = talloc_zero(conn, struct sbus_incoming_signal);
    if (signal == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory allocating D-Bus signal\n");
        return NULL;
    }

    signal->conn = conn;
    signal->message = dbus_message_ref(message);
    signal->interface = dbus_message_get_interface(message);
    signal->signal = dbus_message_get_member(message);
    signal->path = dbus_message_get_path(message);

    talloc_set_destructor(signal, sbus_incoming_signal_destructor);

    return signal;
}

struct sbus_incoming_signal_data {
    sbus_incoming_signal_fn handler_fn;
    void *handler_data;
};

errno_t
sbus_incoming_signal_hash_init(TALLOC_CTX *mem_ctx,
                               hash_table_t **_table)
{
    return sss_hash_create(mem_ctx, 10, _table);
}

static errno_t
sbus_incoming_signal_hash_add(hash_table_t *table,
                              const char *iface,
                              const char *signal,
                              sbus_incoming_signal_fn handler_fn,
                              void *handler_data)
{
    TALLOC_CTX *tmp_ctx;
    struct sbus_incoming_signal_data *data;
    hash_key_t key;
    hash_value_t value;
    errno_t ret;
    bool has_key;
    int hret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        return ENOMEM;
    }

    key.type = HASH_KEY_STRING;
    key.str = talloc_asprintf(tmp_ctx, "%s.%s", iface, signal);
    if (key.str == NULL) {
        ret = ENOMEM;
        goto done;
    }

    has_key = hash_has_key(table, &key);
    if (has_key) {
        ret = EEXIST;
        goto done;
    }

    data = talloc_zero(tmp_ctx, struct sbus_incoming_signal_data);
    if (data == NULL) {
        ret = ENOMEM;
        goto done;
    }

    data->handler_data = handler_data;
    data->handler_fn = handler_fn;

    value.type = HASH_VALUE_PTR;
    value.ptr = data;

    hret = hash_enter(table, &key, &value);
    if (hret != HASH_SUCCESS) {
        ret = EIO;
        goto done;
    }

    talloc_steal(table, key.str);
    talloc_steal(table, data);

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

static struct sbus_incoming_signal_data *
sbus_incoming_signal_hash_lookup(hash_table_t *table,
                                 const char *iface,
                                 const char *signal)
{
    struct sbus_incoming_signal_data *data;
    hash_key_t key;
    hash_value_t value;
    int hret;

    key.type = HASH_KEY_STRING;
    key.str = talloc_asprintf(NULL, "%s.%s", iface, signal);
    if (key.str == NULL) {
        return NULL;
    }

    hret = hash_lookup(table, &key, &value);
    if (hret == HASH_ERROR_KEY_NOT_FOUND) {
        data = NULL;
        goto done;
    } else if (hret != HASH_SUCCESS) {
        DEBUG(SSSDBG_OP_FAILURE,
              "Unable to search hash table: hret=%d\n", hret);
        data = NULL;
        goto done;
    }

    data = talloc_get_type(value.ptr, struct sbus_incoming_signal_data);

done:
    talloc_free(key.str);
    return data;
}

errno_t
sbus_signal_listen(struct sbus_connection *conn,
                   const char *iface,
                   const char *signal,
                   sbus_incoming_signal_fn handler_fn,
                   void *handler_data)
{
    TALLOC_CTX *tmp_ctx;
    const char *rule;
    DBusError error;
    errno_t ret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
        return ENOMEM;
    }

    dbus_error_init(&error);

    ret = sbus_incoming_signal_hash_add(conn->incoming_signals, iface,
                                        signal, handler_fn, handler_data);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to register signal handler "
              "[%d]: %s\n", ret, sss_strerror(ret));
        goto done;
    }

    rule = talloc_asprintf(tmp_ctx, "type='signal',interface='%s',member='%s'",
                           iface, signal);
    if (rule == NULL) {
        ret = ENOMEM;
        goto done;
    }

    dbus_bus_add_match(conn->dbus.conn, rule, &error);
    if (dbus_error_is_set(&error)) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Cannot add D-Bus match rule, cause: %s\n", error.message);
        ret = EIO;
        goto done;
    }

    DEBUG(SSSDBG_TRACE_FUNC, "Listening to signal %s.%s\n", iface, signal);

done:
    dbus_error_free(&error);
    talloc_free(tmp_ctx);

    return ret;
}

static void
sbus_signal_handler_got_caller_id(struct tevent_req *req);

DBusHandlerResult
sbus_signal_handler(DBusConnection *dbus_conn,
                    DBusMessage *message,
                    void *handler_data)
{
    struct tevent_req *req;
    struct sbus_connection *conn;
    struct sbus_incoming_signal *signal;
    const char *sender;
    int type;

    type = dbus_message_get_type(message);
    if (type != DBUS_MESSAGE_TYPE_SIGNAL) {
        /* We ignore other types here. */
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    conn = talloc_get_type(handler_data, struct sbus_connection);
    sender = dbus_message_get_sender(message);

    /* we have a valid handler, create D-Bus request */
    signal = sbus_new_incoming_signal(conn, message);
    if (signal == NULL) {
        return DBUS_HANDLER_RESULT_NEED_MEMORY;
    }

    DEBUG(SSSDBG_TRACE_INTERNAL, "Received D-Bus signal %s.%s\n",
          signal->interface, signal->signal);

    /* now get the sender ID */
    req = sbus_get_sender_id_send(signal, conn->ev, conn, sender);
    if (req == NULL) {
        talloc_free(signal);
        return DBUS_HANDLER_RESULT_NEED_MEMORY;
    }
    tevent_req_set_callback(req, sbus_signal_handler_got_caller_id, signal);

    return DBUS_HANDLER_RESULT_HANDLED;
}

static void
sbus_signal_handler_got_caller_id(struct tevent_req *req)
{
    struct sbus_incoming_signal_data *signal_data;
    struct sbus_incoming_signal *signal;
    errno_t ret;

    signal = tevent_req_callback_data(req, struct sbus_incoming_signal);

    ret = sbus_get_sender_id_recv(req, &signal->client);
    if (ret == ERR_SBUS_SENDER_BUS) {
        DEBUG(SSSDBG_TRACE_FUNC, "Got a signal from the bus..\n");
    } else if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Failed to resolve caller's ID: %s\n", sss_strerror(ret));
        goto done;
    }

    signal_data = sbus_incoming_signal_hash_lookup(
                                            signal->conn->incoming_signals,
                                            signal->interface,
                                            signal->signal);
    if (signal_data == NULL) {
        DEBUG(SSSDBG_MINOR_FAILURE, "Received signal %s.%s that we are "
              "not listening to.\n", signal->interface, signal->signal);
        goto done;
    }

    signal_data->handler_fn(signal, signal_data->handler_data);

done:
    talloc_free(signal);
}