summaryrefslogtreecommitdiffstats
path: root/src/afb-api-dbus.c
blob: eda7985f50fc5b056e75c27cc44345d6374059e6 (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
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
/* 
 * Copyright (C) 2015, 2016 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#define NO_PLUGIN_VERBOSE_MACRO

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

#include <systemd/sd-bus.h>
#include <json-c/json.h>

#include <afb/afb-plugin.h>
#include <afb/afb-req-itf.h>

#include "afb-common.h"

#include "session.h"
#include "afb-apis.h"
#include "afb-api-so.h"
#include "afb-context.h"
#include "afb-evt.h"
#include "verbose.h"

static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";

/*
 * The path given are of the form
 *     system:/org/agl/afb/api/...
 * or
 *     user:/org/agl/afb/api/...
 */
struct api_dbus
{
	struct sd_bus *sdbus;	/* the bus */
	struct sd_bus_slot *slot; /* the slot */
	char *path;		/* path of the object for the API */
	char *name;		/* name/interface of the object */
	char *api;		/* api name of the interface */
	struct afb_evt_listener *listener;
};

#define RETOK   1
#define RETERR  2
#define RETRAW  3

/******************* common part **********************************/

/*
 * create a structure api_dbus connected on either the system
 * bus if 'system' is not null or on the user bus. The connection
 * is established for either emiting/receiving on 'path' being of length
 * 'pathlen'.
 */
static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pathlen)
{
	struct api_dbus *api;
	struct sd_bus *sdbus;
	char *ptr;

	/* allocates the structure */
	api = calloc(1, sizeof *api + 1 + pathlen + pathlen);
	if (api == NULL) {
		errno = ENOMEM;
		goto error;
	}

	/* init the structure's strings */

	/* path is copied after the struct */
	api->path = (void*)(api+1);
	strcpy(api->path, path);

	/* api name is at the end of the path */
	api->api = strrchr(api->path, '/');
	if (api->api == NULL) {
		errno = EINVAL;
		goto error2;
	}
	api->api++;
	if (!afb_apis_is_valid_api_name(api->api)) {
		errno = EINVAL;
		goto error2;
	}

	/* the name/interface is copied after the path */
	api->name = &api->path[pathlen + 1];
	strcpy(api->name, &path[1]);
	ptr = strchr(api->name, '/');
	while(ptr != NULL) {
		*ptr = '.';
		ptr = strchr(ptr, '/');
	}

	/* choose the bus */
	sdbus = (system ? afb_common_get_system_bus : afb_common_get_user_bus)();
	if (sdbus == NULL)
		goto error2;

	api->sdbus = sdbus;
	return api;

error2:
	free(api);
error:
	return NULL;
}

/*
 * create a structure api_dbus connected on either the system
 * bus if 'system' is not null or on the user bus. The connection
 * is established for either emiting/receiving on 'path'.
 * If 'path' is not absolute, it is prefixed with DEFAULT_PATH_PREFIX.
 */
static struct api_dbus *make_api_dbus_2(int system, const char *path)
{
	size_t len;
	char *ptr;

	/* check the length of the path */
	len = strlen(path);
	if (len == 0) {
		errno = EINVAL;
		return NULL;
	}

	/* if the path is absolute, creation now */
	if (path[0] == '/')
		return make_api_dbus_3(system, path, len);

	/* compute the path prefixed with DEFAULT_PATH_PREFIX */
	assert(strlen(DEFAULT_PATH_PREFIX) > 0);
	assert(DEFAULT_PATH_PREFIX[strlen(DEFAULT_PATH_PREFIX) - 1] == '/');
	len += strlen(DEFAULT_PATH_PREFIX);
	ptr = alloca(len + 1);
	strcpy(stpcpy(ptr, DEFAULT_PATH_PREFIX), path);

	/* creation for prefixed path */
	return make_api_dbus_3(system, ptr, len);
}

/*
 * create a structure api_dbus connected either emiting/receiving
 * on 'path'.
 * The path can be prefixed with "system:" or "user:" to select
 * either the user or the system D-Bus. If none is set then user's
 * bus is selected.
 * If remaining 'path' is not absolute, it is prefixed with
 * DEFAULT_PATH_PREFIX.
 */
static struct api_dbus *make_api_dbus(const char *path)
{
	const char *ptr;
	size_t preflen;

	/* retrieves the prefix "scheme-like" part */
	ptr = strchr(path, ':');
	if (ptr == NULL)
		return make_api_dbus_2(0, path);

	/* check the prefix part */
	preflen = (size_t)(ptr - path);
	if (strncmp(path, "system", preflen) == 0)
		return make_api_dbus_2(1, ptr + 1);

	if (strncmp(path, "user", preflen) == 0)
		return make_api_dbus_2(0, ptr + 1);

	/* TODO: connect to a foreign D-Bus? */
	errno = EINVAL;
	return NULL;
}

static void destroy_api_dbus(struct api_dbus *api)
{
	free(api);
}

/******************* client part **********************************/

/*
 * structure for recording query data
 */
struct dbus_memo {
	struct afb_req req;		/* the request handle */
	struct afb_context *context;	/* the context of the query */
};

/* allocates and init the memorizing data */
static struct dbus_memo *api_dbus_client_make_memo(struct afb_req req, struct afb_context *context)
{
	struct dbus_memo *memo;

	memo = malloc(sizeof *memo);
	if (memo != NULL) {
		afb_req_addref(req);
		memo->req = req;
		memo->context = context;
	}
	return memo;
}

/* free and release the memorizing data */
static void api_dbus_client_free_memo(struct dbus_memo *memo)
{
	afb_req_unref(memo->req);
	free(memo);
}

/* callback when received answer */
static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
{
	int rc;
	struct dbus_memo *memo;
	const char *first, *second;
	uint8_t type;
	uint32_t flags;

	/* retrieve the recorded data */
	memo = userdata;

	/* get the answer */
	rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags);
	if (rc < 0) {
		/* failing to have the answer */
		afb_req_fail(memo->req, "error", "dbus error");
	} else {
		/* report the answer */
		memo->context->flags = (unsigned)flags;
		switch(type) {
		case RETOK:
			afb_req_success(memo->req, json_tokener_parse(first), second);
			break;
		case RETERR:
			afb_req_fail(memo->req, first, second);
			break;
		case RETRAW:
			afb_req_send(memo->req, first, strlen(first));
			break;
		default:
			afb_req_fail(memo->req, "error", "dbus link broken");
			break;
		}
	}
	api_dbus_client_free_memo(memo);
	return 1;
}

/* on call, propagate it to the dbus service */
static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
{
	size_t size;
	int rc;
	char *method = strndupa(verb, lenverb);
	struct dbus_memo *memo;

	/* create the recording data */
	memo = api_dbus_client_make_memo(req, context);
	if (memo == NULL) {
		afb_req_fail(req, "error", "out of memory");
		return;
	}

	/* makes the call */
	rc = sd_bus_call_method_async(api->sdbus, NULL,
		api->name, api->path, api->name, method,
		api_dbus_client_on_reply, memo,
		"ssu",
			afb_req_raw(req, &size),
			ctxClientGetUuid(context->session),
			(uint32_t)context->flags);

	/* if there was an error report it directly */
	if (rc < 0) {
		errno = -rc;
		afb_req_fail(req, "error", "dbus error");
		api_dbus_client_free_memo(memo);
	}
}

/* receives events */
static int api_dbus_client_on_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
{
	struct json_object *object;
	const char *event, *data;
	int rc = sd_bus_message_read(m, "ss", &event, &data);
	if (rc < 0)
		ERROR("unreadable event");
	else {
		object = json_tokener_parse(data);
		afb_evt_broadcast(event, object);
		json_object_put(object);
	}
	return 1;
}

/* adds a afb-dbus-service client api */
int afb_api_dbus_add_client(const char *path)
{
	int rc;
	struct api_dbus *api;
	struct afb_api afb_api;
	char *match;

	/* create the dbus client api */
	api = make_api_dbus(path);
	if (api == NULL)
		goto error;

	/* connect to events */
	rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='event'", api->path, api->name);
	if (rc < 0) {
		errno = ENOMEM;
		ERROR("out of memory");
		goto error;
	}
	rc = sd_bus_add_match(api->sdbus, &api->slot, match, api_dbus_client_on_event, api);
	free(match);
	if (rc < 0) {
		errno = -rc;
		ERROR("can't add dbus object %s for %s", api->path, api->name);
		goto error;
	}

	/* record it as an API */
	afb_api.closure = api;
	afb_api.call = (void*)api_dbus_client_call;
	if (afb_apis_add(api->api, afb_api) < 0)
		goto error2;

	return 0;

error2:
	destroy_api_dbus(api);
error:
	return -1;
}

/******************* dbus request part for server *****************/

/*
 * structure for a dbus request
 */
struct dbus_req {
	struct afb_context context;	/* the context, should be THE FIRST */
	sd_bus_message *message;	/* the incoming request message */
	const char *request;		/* the readen request as string */
	struct json_object *json;	/* the readen request as object */
	int refcount;			/* reference count of the request */
};

/* increment the reference count of the request */
static void dbus_req_addref(struct dbus_req *dreq)
{
	dreq->refcount++;
}

/* decrement the reference count of the request and free/release it on falling to null */
static void dbus_req_unref(struct dbus_req *dreq)
{
	if (dreq == NULL || --dreq->refcount)
		return;

	afb_context_disconnect(&dreq->context);
	json_object_put(dreq->json);
	sd_bus_message_unref(dreq->message);
	free(dreq);
}

/* get the object of the request */
static struct json_object *dbus_req_json(struct dbus_req *dreq)
{
	if (dreq->json == NULL) {
		dreq->json = json_tokener_parse(dreq->request);
		if (dreq->json == NULL) {
			/* lazy error detection of json request. Is it to improve? */
			dreq->json = json_object_new_string(dreq->request);
		}
	}
	return dreq->json;
}

/* get the argument of the request of 'name' */
static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name)
{
	struct afb_arg arg;
	struct json_object *value, *root;

	root = dbus_req_json(dreq);
	if (root != NULL && json_object_object_get_ex(root, name, &value)) {
		arg.name = name;
		arg.value = json_object_get_string(value);
	} else {
		arg.name = NULL;
		arg.value = NULL;
	}
	arg.path = NULL;
	return arg;
}

static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
{
	int rc;
	rc = sd_bus_reply_method_return(dreq->message,
			"yssu", type, first, second, (uint32_t)dreq->context.flags);
	if (rc < 0)
		ERROR("sending the reply failed");
}

static void dbus_req_success(struct dbus_req *dreq, struct json_object *obj, const char *info)
{
	dbus_req_reply(dreq, RETOK, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN), info);
}

static void dbus_req_fail(struct dbus_req *dreq, const char *status, const char *info)
{
	dbus_req_reply(dreq, RETERR, status, info);
}

static const char *dbus_req_raw(struct dbus_req *dreq, size_t *size)
{
	if (size != NULL)
		*size = strlen(dreq->request);
	return dreq->request;
}

static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size)
{
	/* TODO: how to put sized buffer as strings? things aren't clear here!!! */
	dbus_req_reply(dreq, RETRAW, buffer, "");
}

const struct afb_req_itf afb_api_dbus_req_itf = {
	.json = (void*)dbus_req_json,
	.get = (void*)dbus_req_get,
	.success = (void*)dbus_req_success,
	.fail = (void*)dbus_req_fail,
	.raw = (void*)dbus_req_raw,
	.send = (void*)dbus_req_send,
	.context_get = (void*)afb_context_get,
	.context_set = (void*)afb_context_set,
	.addref = (void*)dbus_req_addref,
	.unref = (void*)dbus_req_unref
};

/******************* server part **********************************/

/* called when the object for the service is called */
static int api_dbus_server_on_object_called(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
{
	int rc;
	const char *method;
	const char *uuid;
	struct dbus_req *dreq;
	struct api_dbus *api = userdata;
	struct afb_req areq;
	uint32_t flags;

	/* check the interface */
	if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
		return 0;

	/* get the method */
	method = sd_bus_message_get_member(message);

	/* create the request */
	dreq = calloc(1 , sizeof *dreq);
	if (dreq == NULL) {
		sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
		return 1;
	}

	/* get the data */
	rc = sd_bus_message_read(message, "ssu", &dreq->request, &uuid, &flags);
	if (rc < 0) {
		sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
		free(dreq);
		return 1;
	}

	/* connect to the context */
	if (afb_context_connect(&dreq->context, uuid, NULL) < 0) {
		sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
		free(dreq);
		return 1;
	}

	/* fulfill the request and emit it */
	dreq->context.flags = flags;
	dreq->message = sd_bus_message_ref(message);
	dreq->json = NULL;
	dreq->refcount = 1;
	areq.itf = &afb_api_dbus_req_itf;
	areq.closure = dreq;
	afb_apis_call_(areq, &dreq->context, api->api, method);
	dbus_req_unref(dreq);
	return 1;
}

static void afb_api_dbus_server_send_event(struct api_dbus *api, const char *event, struct json_object *object)
{
	int rc;

	rc = sd_bus_emit_signal(api->sdbus, api->path, api->name,
			"event", "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN));
	if (rc < 0)
		ERROR("error while emiting event %s", event);
	json_object_put(object);
}

/* create the service */
int afb_api_dbus_add_server(const char *path)
{
	int rc;
	struct api_dbus *api;

	/* get the dbus api object connected */
	api = make_api_dbus(path);
	if (api == NULL)
		goto error;

	/* request the service object name */
	rc = sd_bus_request_name(api->sdbus, api->name, 0);
	if (rc < 0) {
		errno = -rc;
		ERROR("can't register name %s", api->name);
		goto error2;
	}

	/* connect the service to the dbus object */
	rc = sd_bus_add_object(api->sdbus, &api->slot, api->path, api_dbus_server_on_object_called, api);
	if (rc < 0) {
		errno = -rc;
		ERROR("can't add dbus object %s for %s", api->path, api->name);
		goto error3;
	}
	INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);

	api->listener = afb_evt_listener_create((void*)afb_api_dbus_server_send_event, api);

	return 0;
error3:
	sd_bus_release_name(api->sdbus, api->name);
error2:
	destroy_api_dbus(api);
error:
	return -1;
}