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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
/*
Authors:
Pavel Březina <pbrezina@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 <string.h>
#include <stdio.h>
#include "util/util.h"
#include "util/dlinklist.h"
#include "sbus/sbus_request.h"
#include "sbus/sbus_private.h"
#include "sbus/sbus_interface.h"
#include "sbus/interface_dbus/sbus_dbus_server.h"
#define FMT_DOCTYPE \
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n" \
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
#define FMT_NODE "<node name=\"%s\">\n"
#define FMT_IFACE " <interface name=\"%s\">\n"
#define FMT_ANNOTATION " %s<annotation name=\"%s\" value=\"%s\" />\n"
#define FMT_METHOD_EMPTY " <method name=\"%s\" />\n"
#define FMT_METHOD_OPEN " <method name=\"%s\">\n"
#define FMT_METHOD_ARG " <arg type=\"%s\" name=\"%s\" direction=\"%s\" />\n"
#define FMT_METHOD_CLOSE " </method>\n"
#define FMT_SIGNAL_EMPTY " <signal name=\"%s\" />\n"
#define FMT_SIGNAL_OPEN " <signal name=\"%s\">\n"
#define FMT_SIGNAL_ARG " <arg type=\"%s\" name=\"%s\" />\n"
#define FMT_SIGNAL_CLOSE " </signal>\n"
#define FMT_PROPERTY_EMPTY " <property name=\"%s\" type=\"%s\" access=\"%s\" />\n"
#define FMT_PROPERTY_OPEN " <property name=\"%s\" type=\"%s\" access=\"%s\">\n"
#define FMT_PROPERTY_CLOSE " </property>\n"
#define FMT_IFACE_CLOSE " </interface>\n"
#define FMT_CHILD_NODE " <node name=\"%s\" />\n"
#define FMT_NODE_CLOSE "</node>\n"
#define WRITE_OR_FAIL(file, ret, label, fmt, ...) do { \
ret = fprintf(file, fmt, ##__VA_ARGS__); \
if (ret < 0) { \
ret = EIO; \
goto label; \
} \
} while (0)
#define EMPTY(field) ((field) == NULL || (field)[0].name == NULL)
enum sbus_arg_type {
SBUS_ARG_IN,
SBUS_ARG_OUT,
SBUS_ARG_SIGNAL
};
static errno_t
sbus_introspect_annotations(FILE *file,
bool inside,
const struct sbus_annotation *annotations)
{
errno_t ret;
const char *indent = inside ? " " : "";
int i;
if (annotations == NULL) {
return EOK;
}
for (i = 0; annotations[i].name != NULL; i++) {
WRITE_OR_FAIL(file, ret, done, FMT_ANNOTATION, indent,
annotations[i].name, annotations[i].value);
}
ret = EOK;
done:
return ret;
}
static errno_t
sbus_introspect_args(FILE *file,
enum sbus_arg_type type,
const struct sbus_argument *args)
{
errno_t ret;
int i;
if (args == NULL) {
return EOK;
}
for (i = 0; args[i].name != NULL; i++) {
switch (type) {
case SBUS_ARG_SIGNAL:
WRITE_OR_FAIL(file, ret, done, FMT_SIGNAL_ARG,
args[i].type, args[i].name);
break;
case SBUS_ARG_IN:
WRITE_OR_FAIL(file, ret, done, FMT_METHOD_ARG,
args[i].type, args[i].name, "in");
break;
case SBUS_ARG_OUT:
WRITE_OR_FAIL(file, ret, done, FMT_METHOD_ARG,
args[i].type, args[i].name, "out");
break;
}
}
ret = EOK;
done:
return ret;
}
static errno_t
sbus_introspect_methods(FILE *file,
const struct sbus_method *methods)
{
errno_t ret;
int i;
if (methods == NULL) {
return EOK;
}
for (i = 0; methods[i].name != NULL; i++) {
if (EMPTY(methods[i].annotations)
&& EMPTY(methods[i].arguments->input)
&& EMPTY(methods[i].arguments->output)) {
WRITE_OR_FAIL(file, ret, done, FMT_METHOD_EMPTY, methods[i].name);
continue;
}
WRITE_OR_FAIL(file, ret, done, FMT_METHOD_OPEN, methods[i].name);
ret = sbus_introspect_annotations(file, true, methods[i].annotations);
if (ret != EOK) {
goto done;
}
ret = sbus_introspect_args(file, SBUS_ARG_IN,
methods[i].arguments->input);
if (ret != EOK) {
goto done;
}
ret = sbus_introspect_args(file, SBUS_ARG_OUT,
methods[i].arguments->output);
if (ret != EOK) {
goto done;
}
WRITE_OR_FAIL(file, ret, done, FMT_METHOD_CLOSE);
}
ret = EOK;
done:
return ret;
}
static errno_t
sbus_introspect_signals(FILE *file,
const struct sbus_signal *signals)
{
errno_t ret;
int i;
if (signals == NULL) {
return EOK;
}
for (i = 0; signals[i].name != NULL; i++) {
if (EMPTY(signals[i].annotations) && EMPTY(signals[i].arguments)) {
WRITE_OR_FAIL(file, ret, done, FMT_SIGNAL_EMPTY, signals[i].name);
continue;
}
WRITE_OR_FAIL(file, ret, done, FMT_SIGNAL_OPEN, signals[i].name);
ret = sbus_introspect_annotations(file, true, signals[i].annotations);
if (ret != EOK) {
goto done;
}
ret = sbus_introspect_args(file, SBUS_ARG_SIGNAL, signals[i].arguments);
if (ret != EOK) {
goto done;
}
WRITE_OR_FAIL(file, ret, done, FMT_SIGNAL_CLOSE);
}
ret = EOK;
done:
return ret;
}
struct sbus_introspect_property {
const char *name;
const char *type;
const struct sbus_annotation *annotations;
enum sbus_property_access access;
};
static void
sbus_introspect_property_set(struct sbus_introspect_property *properties,
const struct sbus_property *property)
{
int i;
for (i = 0; properties[i].name != NULL; i++) {
if (strcmp(properties[i].name, property->name) == 0) {
break;
}
}
/* Name, type and annotation is the same for both getter and setter.
* We just need to update access mode. */
properties[i].name = property->name;
properties[i].type = property->type;
properties[i].annotations = property->annotations;
properties[i].access |= property->access;
}
static const char *
sbus_introspect_property_mode(struct sbus_introspect_property *property)
{
switch (property->access) {
case SBUS_PROPERTY_READABLE:
return "read";
case SBUS_PROPERTY_WRITABLE:
return "write";
default:
return "readwrite";
}
}
static errno_t
sbus_introspect_properties(FILE *file,
const struct sbus_property *properties)
{
struct sbus_introspect_property *props;
const char *mode;
errno_t ret;
int len;
int i;
if (properties == NULL) {
return EOK;
}
for (len = 0; properties[len].name != NULL ; len++);
props = talloc_zero_array(NULL, struct sbus_introspect_property, len + 1);
if (props == NULL) {
return ENOMEM;
}
for (i = 0; properties[i].name != NULL; i++) {
sbus_introspect_property_set(props, &properties[i]);
}
for (i = 0; props[i].name != NULL; i++) {
mode = sbus_introspect_property_mode(&props[i]);
if (EMPTY(props[i].annotations)) {
WRITE_OR_FAIL(file, ret, done, FMT_PROPERTY_EMPTY,
props[i].name, props[i].type, mode);
continue;
}
WRITE_OR_FAIL(file, ret, done, FMT_PROPERTY_OPEN,
props[i].name, props[i].type, mode);
ret = sbus_introspect_annotations(file, true, props[i].annotations);
if (ret != EOK) {
goto done;
}
WRITE_OR_FAIL(file, ret, done, FMT_PROPERTY_CLOSE);
}
ret = EOK;
done:
talloc_free(props);
return ret;
}
static int
sbus_introspect_iface(FILE *file, struct sbus_interface *iface)
{
errno_t ret;
WRITE_OR_FAIL(file, ret, done, FMT_IFACE, iface->name);
ret = sbus_introspect_annotations(file, false, iface->annotations);
if (ret != EOK) {
goto done;
}
ret = sbus_introspect_methods(file, iface->methods);
if (ret != EOK) {
goto done;
}
ret = sbus_introspect_signals(file, iface->signals);
if (ret != EOK) {
goto done;
}
ret = sbus_introspect_properties(file, iface->properties);
if (ret != EOK) {
goto done;
}
WRITE_OR_FAIL(file, ret, done, FMT_IFACE_CLOSE);
ret = EOK;
done:
return ret;
}
static int
sbus_introspect_nodes(FILE *file, const char **nodes)
{
errno_t ret;
int i;
if (nodes == NULL) {
return EOK;
}
for (i = 0; nodes[i] != NULL; i++) {
WRITE_OR_FAIL(file, ret, done, FMT_CHILD_NODE, nodes[i]);
}
ret = EOK;
done:
return ret;
}
static char *
sbus_introspect(TALLOC_CTX *mem_ctx,
const char *node,
const char **nodes,
struct sbus_interface_list *list)
{
struct sbus_interface_list *item;
char *introspection = NULL;
FILE *memstream;
char *buffer;
size_t size;
errno_t ret;
memstream = open_memstream(&buffer, &size);
if (memstream == NULL) {
goto done;
}
WRITE_OR_FAIL(memstream, ret, done, FMT_DOCTYPE);
WRITE_OR_FAIL(memstream, ret, done, FMT_NODE, node);
DLIST_FOR_EACH(item, list) {
ret = sbus_introspect_iface(memstream, item->interface);
if (ret != EOK) {
goto done;
}
}
ret = sbus_introspect_nodes(memstream, nodes);
if (ret != EOK) {
goto done;
}
WRITE_OR_FAIL(memstream, ret, done, FMT_NODE_CLOSE);
fflush(memstream);
introspection = talloc_memdup(mem_ctx, buffer, size + 1);
done:
if (memstream != NULL) {
fclose(memstream);
free(buffer);
}
return introspection;
}
typedef errno_t
(*sbus_node_factory_sync)(TALLOC_CTX *, const char *, void *, const char ***);
typedef struct tevent_req *
(*sbus_node_factory_send)(TALLOC_CTX *, struct tevent_context *,
const char *, void *);
typedef errno_t
(*sbus_node_factory_recv)(TALLOC_CTX *, struct tevent_req *, const char ***);
struct sbus_acquire_nodes_state {
const char **nodes;
struct sbus_handler *handler;
};
static void sbus_acquire_nodes_done(struct tevent_req *subreq);
static struct tevent_req *
sbus_acquire_nodes_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct sbus_router *router,
const char *path)
{
struct sbus_acquire_nodes_state *state;
struct tevent_req *subreq;
struct tevent_req *req;
struct sbus_node *node;
sbus_node_factory_sync handler_sync;
sbus_node_factory_send handler_send;
errno_t ret;
req = tevent_req_create(mem_ctx, &state, struct sbus_acquire_nodes_state);
if (req == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
return NULL;
}
node = sbus_router_nodes_lookup(router->nodes, path);
if (node == NULL) {
/* If there is no node factory registered and it is a root path,
* we return all known paths to the router. */
if (strcmp(path, "/") == 0) {
state->nodes = sbus_router_paths_nodes(state, router->paths);
} else {
state->nodes = NULL;
}
ret = EOK;
goto done;
}
state->handler = &node->factory;
switch (node->factory.type) {
case SBUS_HANDLER_SYNC:
handler_sync = node->factory.sync;
if (handler_sync == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Bug: sync handler is not specified!\n");
ret = ERR_INTERNAL;
goto done;
}
ret = handler_sync(state, path, node->factory.data, &state->nodes);
goto done;
case SBUS_HANDLER_ASYNC:
handler_send = node->factory.async_send;
if (handler_send == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Bug: async handler is not specified!\n");
ret = ERR_INTERNAL;
goto done;
}
subreq = handler_send(state, ev, path, node->factory.data);
if (subreq == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create subrequest!\n");
ret = ENOMEM;
goto done;
}
tevent_req_set_callback(subreq, sbus_acquire_nodes_done, req);
break;
}
ret = EAGAIN;
done:
if (ret == EOK) {
tevent_req_done(req);
tevent_req_post(req, ev);
} else if (ret != EAGAIN) {
tevent_req_error(req, ret);
tevent_req_post(req, ev);
}
return req;
}
static void sbus_acquire_nodes_done(struct tevent_req *subreq)
{
struct sbus_acquire_nodes_state *state;
sbus_node_factory_recv handler_recv;
struct tevent_req *req;
errno_t ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct sbus_acquire_nodes_state);
handler_recv = state->handler->async_recv;
if (handler_recv == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Bug: async handler is not specified!\n");
tevent_req_error(req, ERR_INTERNAL);
return;
}
ret = handler_recv(state, subreq, &state->nodes);
talloc_zfree(subreq);
if (ret != EOK) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
return;
}
static errno_t
sbus_acquire_nodes_recv(struct tevent_req *req,
const char ***_nodes)
{
struct sbus_acquire_nodes_state *state;
state = tevent_req_data(req, struct sbus_acquire_nodes_state);
TEVENT_REQ_RETURN_ON_ERROR(req);
/* We keep the nodes allocated on this request state, so we do not have
* to expect that state->nodes is a talloc context. This way, it may
* be static array. */
*_nodes = state->nodes;
return EOK;
}
struct sbus_introspection_state {
struct sbus_interface_list *list;
const char *introspection;
const char *path;
};
static void sbus_introspection_done(struct tevent_req *subreq);
static struct tevent_req *
sbus_introspection_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct sbus_request *sbus_req,
struct sbus_router *router)
{
struct sbus_introspection_state *state;
struct tevent_req *subreq;
struct tevent_req *req;
errno_t ret;
req = tevent_req_create(mem_ctx, &state, struct sbus_introspection_state);
if (req == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
return NULL;
}
state->path = sbus_req->path;
state->introspection = NULL;
ret = sbus_router_paths_supported(state, router->paths,
sbus_req->path, &state->list);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to acquire interface list "
"[%d]: %s\n", ret, sss_strerror(ret));
goto done;
}
subreq = sbus_acquire_nodes_send(mem_ctx, ev, router, sbus_req->path);
if (subreq == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create subrequest!\n");
ret = ENOMEM;
goto done;
}
tevent_req_set_callback(subreq, sbus_introspection_done, req);
ret = EAGAIN;
done:
if (ret != EAGAIN) {
tevent_req_error(req, ret);
tevent_req_post(req, ev);
}
return req;
}
static void sbus_introspection_done(struct tevent_req *subreq)
{
struct sbus_introspection_state *state;
struct tevent_req *req;
const char **nodes;
errno_t ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct sbus_introspection_state);
/* We keep the nodes allocated on subrequest state, so we do not have
* to expect that it is a talloc context and allow it also as a static
* array. Therefore we must free subreq later. */
ret = sbus_acquire_nodes_recv(subreq, &nodes);
if (ret != EOK) {
goto done;
}
state->introspection = sbus_introspect(state, state->path,
nodes, state->list);
if (state->introspection == NULL) {
ret = ENOMEM;
goto done;
}
done:
talloc_zfree(subreq);
if (ret != EOK) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
return;
}
static errno_t
sbus_introspection_recv(TALLOC_CTX *mem_ctx,
struct tevent_req *req,
const char **_introspection)
{
struct sbus_introspection_state *state;
state = tevent_req_data(req, struct sbus_introspection_state);
TEVENT_REQ_RETURN_ON_ERROR(req);
*_introspection = talloc_steal(mem_ctx, state->introspection);
return EOK;
}
errno_t
sbus_register_introspection(struct sbus_router *router)
{
SBUS_INTERFACE(iface,
org_freedesktop_DBus_Introspectable,
SBUS_METHODS(
SBUS_ASYNC(METHOD, org_freedesktop_DBus_Introspectable, Introspect,
sbus_introspection_send, sbus_introspection_recv,
router)
),
SBUS_WITHOUT_SIGNALS,
SBUS_WITHOUT_PROPERTIES
);
struct sbus_path paths[] = {
{"/", &iface},
{"/*", &iface},
{NULL, NULL}
};
return sbus_router_add_path_map(router, paths);
}
|