summaryrefslogtreecommitdiffstats
path: root/src/sbus/server/sbus_server_match.c
blob: 4467c3b9697187a55eeeb22aef82903338f509e8 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>
        Stephen Gallagher <sgallagh@redhat.com>
        Simo Sorce <ssorce@redhat.com>

    Copyright (C) 2017 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 <errno.h>
#include <string.h>
#include <tevent.h>
#include <talloc.h>

#include "util/util.h"
#include "util/sss_ptr_hash.h"
#include "util/sss_ptr_list.h"
#include "sbus/sbus_private.h"

struct sbus_rule {
    const char *type;
    const char *interface;
    const char *member;
};

static struct sbus_connection *
sbus_match_find(struct sss_ptr_list *list,
                struct sbus_connection *conn)
{
    struct sbus_connection *match_conn;

    SSS_PTR_LIST_FOR_EACH(list, match_conn, struct sbus_connection) {
        if (match_conn == conn) {
            return match_conn;
        }
    }

    return NULL;
}

static char *
sbus_match_rule_key(TALLOC_CTX *mem_ctx,
                    const char *interface,
                    const char *member)
{
    if (interface == NULL) {
        return NULL;
    }

    if (member == NULL) {
        return talloc_strdup(mem_ctx, interface);
    }

    return talloc_asprintf(mem_ctx, "%s.%s", interface, member);
}

static struct sss_ptr_list *
sbus_match_rule_create(struct sbus_server *server,
                       const char *key)
{
    struct sss_ptr_list *list;
    errno_t ret;

    list = sss_ptr_list_create(NULL, false);
    if (list == NULL) {
        return NULL;
    }

    ret = sss_ptr_hash_add(server->match_rules, key, list, struct sss_ptr_list);
    if (ret != EOK) {
        talloc_free(list);
        return NULL;
    }

    talloc_steal(server->match_rules, list);

    return list;
}


static struct sss_ptr_list *
sbus_match_rule_get(struct sbus_server *server,
                    const char *interface,
                    const char *member,
                    bool create,
                    bool *_created)
{
    struct sss_ptr_list *list;
    char *key;

    key = sbus_match_rule_key(NULL, interface, member);
    if (key == NULL) {
        return NULL;
    }

    list = sss_ptr_hash_lookup(server->match_rules, key, struct sss_ptr_list);
    if (!create || list != NULL) {
        if (_created != NULL) {
            *_created = false;
        }
        goto done;
    }

    list = sbus_match_rule_create(server, key);
    if (list != NULL && _created != NULL) {
        *_created = true;
    }

done:
    talloc_free(key);
    return list;
}

static errno_t
sbus_match_rule_add(struct sbus_server *server,
                    struct sbus_connection *conn,
                    struct sbus_rule *rule)
{
    struct sbus_connection *match_conn;
    struct sss_ptr_list *list;
    bool created = false;
    errno_t ret;

    DEBUG(SSSDBG_TRACE_ALL, "Adding match rule for %s: %s.%s\n",
          conn->unique_name, rule->interface, rule->member);

    list = sbus_match_rule_get(server, rule->interface, rule->member,
                               true, &created);
    if (list == NULL) {
        return ENOMEM;
    }

    match_conn = sbus_match_find(list, conn);
    if (match_conn != NULL) {
        /* Match was already added. */
        return EOK;
    }

    ret = sss_ptr_list_add(list, conn);
    if (ret != EOK && created) {
        talloc_free(list);
    }

    return ret;
}

static errno_t
sbus_match_rule_remove(struct sbus_server *server,
                       struct sbus_connection *conn,
                       struct sbus_rule *rule)
{
    struct sbus_connection *match_conn;
    struct sss_ptr_list *list;

    DEBUG(SSSDBG_TRACE_ALL, "Removing match rule for %s: %s.%s\n",
          conn->unique_name, rule->interface, rule->member);

    list = sbus_match_rule_get(server, rule->interface, rule->member,
                               false, NULL);
    if (list == NULL) {
        return EOK;
    }

    match_conn = sbus_match_find(list, conn);
    if (match_conn == NULL) {
        return EOK;
    }

    sss_ptr_list_remove(list, match_conn);

    if (sss_ptr_list_is_empty(list)) {
        /* This will remove the list from the hash table. */
        talloc_free(list);
    }

    return EOK;
}

static struct sss_ptr_list *
sbus_match_rule_find(struct sbus_server *server,
                     const char *interface,
                     const char *member)
{
    return sbus_match_rule_get(server, interface, member, false, NULL);
}

static errno_t
sbus_match_rule_parse_value(TALLOC_CTX *mem_ctx,
                            const char *item,
                            const char *name,
                            const char **_value)
{
    size_t name_len = strlen(name);
    size_t iter_len;
    const char *iter;
    char quote;

    if (strncmp(item, name, name_len) != 0) {
        return ENOENT;
    }

    iter = item + name_len;

    if (*iter == '=') {
        iter++;
    } else {
        return ENOENT;
    }

    if (*iter == '"' || *iter == '\'') {
        quote = *iter;
        iter++;
    } else {
        return EINVAL;
    }

    iter_len = strlen(iter);
    if (iter[iter_len - 1] != quote) {
        return EINVAL;
    }

    *_value = talloc_strndup(mem_ctx, iter, iter_len - 1);
    if (*_value == NULL) {
        return ENOMEM;
    }

    return EOK;
}

static errno_t
sbus_match_rule_parse_keys(TALLOC_CTX *mem_ctx,
                           char **tokens,
                           struct sbus_rule **_rule)
{
    struct sbus_rule *rule;
    errno_t ret;
    int i, j;

    rule = talloc_zero(mem_ctx, struct sbus_rule);
    if (rule == NULL) {
        return ENOMEM;
    }

    struct {
        const char *name;
        const char **value;
    } keys[] = {
        {"type", &rule->type},
        {"interface", &rule->interface},
        {"member", &rule->member},
        /* There are more keys in D-Bus specification, such as sender, path
         * and destination. But we are not interested in them yet. */
        {NULL, NULL}
    };

    for (i = 0; tokens[i] != NULL; i++) {
        for (j = 0; keys[j].name != NULL; j++) {
            ret = sbus_match_rule_parse_value(rule, tokens[i],
                                              keys[j].name, keys[j].value);
            if (ret == EOK) {
                break;
            } else if (ret == ENOENT) {
                continue;
            }

            /* Error. */
            talloc_free(rule);
            return ret;
        }
    }

    *_rule = rule;
    return EOK;
}

static errno_t
sbus_match_rule_parse_check(struct sbus_rule *rule)
{
    if (rule->type == NULL || strcmp(rule->type, "signal") != 0) {
        return EINVAL;
    }

    if (rule->interface == NULL || rule->member == NULL) {
        return EINVAL;
    }

    return EOK;
}

static errno_t
sbus_match_rule_parse(TALLOC_CTX *mem_ctx,
                      const char *dbus_rule,
                      struct sbus_rule **_rule)
{
    struct sbus_rule *sbus_rule;
    char **tokens;
    errno_t ret;
    int count;

    ret = split_on_separator(NULL, dbus_rule, ',', true, true, &tokens, &count);
    if (ret != EOK) {
        goto done;
    }

    ret = sbus_match_rule_parse_keys(mem_ctx, tokens, &sbus_rule);
    talloc_free(tokens);
    if (ret != EOK) {
        goto done;
    }

    ret = sbus_match_rule_parse_check(sbus_rule);
    if (ret != EOK) {
        talloc_free(sbus_rule);
        goto done;
    }

    *_rule = sbus_rule;

    ret = EOK;

done:
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "Unable to parse rule [%s] [%d]: %s\n",
              dbus_rule, ret, sss_strerror(ret));
    }


    return ret;
}

errno_t
sbus_server_add_match(struct sbus_server *server,
                      struct sbus_connection *conn,
                      const char *dbus_rule)
{
    struct sbus_rule *sbus_rule;
    errno_t ret;

    ret = sbus_match_rule_parse(NULL, dbus_rule, &sbus_rule);
    if (ret != EOK) {
        return ret;
    }

    ret = sbus_match_rule_add(server, conn, sbus_rule);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "Unable to add rule [%s] [%d]: %s\n",
              dbus_rule, ret, sss_strerror(ret));
    }

    talloc_free(sbus_rule);
    return ret;
}

errno_t
sbus_server_remove_match(struct sbus_server *server,
                         struct sbus_connection *conn,
                         const char *dbus_rule)
{
    struct sbus_rule *sbus_rule;
    errno_t ret;

    ret = sbus_match_rule_parse(NULL, dbus_rule, &sbus_rule);
    if (ret != EOK) {
        return ret;
    }

    ret = sbus_match_rule_remove(server, conn, sbus_rule);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "Unable to remove rule [%s] [%d]: %s\n",
              dbus_rule, ret, sss_strerror(ret));
    }

    talloc_free(sbus_rule);
    return ret;
}

static bool
sbus_server_connection_has_name(struct sbus_server *server,
                                struct sbus_connection *conn,
                                const char *name)
{
    struct sbus_connection *named_conn;

    named_conn = sss_ptr_hash_lookup(server->names, name,
                                     struct sbus_connection);

    if (named_conn == NULL || named_conn != conn) {
        return false;
    }

    return true;
}

errno_t
sbus_server_matchmaker(struct sbus_server *server,
                       struct sbus_connection *conn,
                       const char *avoid_name,
                       DBusMessage *message)
{
    struct sss_ptr_list *list;
    struct sbus_connection *match_conn;
    bool has_name;

    /* We can't really send signals when the server is being destroyed. */
    if (server == NULL || server->disconnecting) {
        return EOK;
    }

    list = sbus_match_rule_find(server,
                                dbus_message_get_interface(message),
                                dbus_message_get_member(message));
    if (list == NULL) {
        /* No connection listens for this signal. */
        return EOK;
    }

    SSS_PTR_LIST_FOR_EACH(list, match_conn, struct sbus_connection) {
        /* Do not send signal back to the sender. */
        if (match_conn == conn) {
            continue;
        }

        /* Sometimes (e.g. when a name is being deleted), we do not want to
         * send the signal to a specific name. */
        if (avoid_name != NULL) {
            has_name = sbus_server_connection_has_name(server, match_conn,
                                                       avoid_name);
            if (has_name) {
                continue;
            }
        }

        dbus_connection_send(match_conn->connection, message, NULL);
    }

    return EOK;
}