/*
- * Copyright (C) 2015-2018 "IoT.bzh"
+ * Copyright (C) 2015-2019 "IoT.bzh"
* Author José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* limitations under the License.
*/
+#if WITH_DBUS_TRANSPARENCY
+
#define _GNU_SOURCE
-#define NO_PLUGIN_VERBOSE_MACRO
#include <stdlib.h>
#include <stdio.h>
#include <systemd/sd-bus.h>
#include <json-c/json.h>
+#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
+#define JSON_C_TO_STRING_NOSLASHESCAPE 0
+#endif
#include <afb/afb-event-x2.h>
-#include "afb-systemd.h"
-
#include "afb-session.h"
#include "afb-msg-json.h"
#include "afb-api.h"
#include "afb-cred.h"
#include "afb-evt.h"
#include "afb-xreq.h"
+
#include "verbose.h"
+#include "systemd.h"
+#include "jobs.h"
static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
}
/* choose the bus */
- sdbus = (system ? afb_systemd_get_system_bus : afb_systemd_get_user_bus)();
+ jobs_acquire_event_manager();
+ sdbus = (system ? systemd_get_system_bus : systemd_get_user_bus)();
if (sdbus == NULL)
goto error2;
int rc;
struct dbus_memo *memo;
const char *json, *error, *info;
+ struct json_object *object;
+ enum json_tokener_error jerr;
/* retrieve the recorded data */
memo = userdata;
afb_xreq_reply(memo->xreq, NULL, "error", "dbus error");
} else {
/* report the answer */
- afb_xreq_reply(memo->xreq, *json ? json_tokener_parse(json) : NULL, *error ? error : NULL, *info ? info : NULL);
+ if (!*json)
+ object = NULL;
+ else {
+ object = json_tokener_parse_verbose(json, &jerr);
+ if (jerr != json_tokener_success)
+ object = json_object_new_string(json);
+ }
+ afb_xreq_reply(memo->xreq, object, *error ? error : NULL, *info ? info : NULL);
}
api_dbus_client_memo_destroy(memo);
return 1;
{
struct json_object *object;
const char *event, *data;
+ enum json_tokener_error jerr;
int rc = sd_bus_message_read(m, "ss", &event, &data);
if (rc < 0)
ERROR("unreadable broadcasted event");
else {
- object = json_tokener_parse(data);
+ object = json_tokener_parse_verbose(data, &jerr);
+ if (jerr != json_tokener_success)
+ object = json_object_new_string(data);
afb_evt_broadcast(event, object);
}
return 1;
{
struct json_object *object;
struct dbus_event *ev;
+ enum json_tokener_error jerr;
/* retrieves the event */
ev = api_dbus_client_event_search(api, id, name);
}
/* destroys the event */
- object = json_tokener_parse(data);
+ object = json_tokener_parse_verbose(data, &jerr);
+ if (jerr != json_tokener_success)
+ object = json_object_new_string(data);
afb_evt_event_x2_push(ev->event, object);
}
struct afb_cred *cred;
/* the origin */
- char name[1];
+ char name[];
};
/* get the credentials for the message */
}
/* not found, create it */
- origin = malloc(strlen(sender) + sizeof *origin);
+ origin = malloc(strlen(sender) + 1 + sizeof *origin);
if (origin == NULL)
errno = ENOMEM;
else {
int rc;
rc = sd_bus_reply_method_return(dreq->message, "sss",
- obj ? json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN) : "",
+ obj ? json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE) : "",
error ? : "",
info ? : "");
if (rc < 0)
static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
{
- const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
+ const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE);
afb_api_dbus_server_event_send(closure, '!', event, eventid, data, 0);
json_object_put(object);
}
api = closure;
rc = sd_bus_emit_signal(api->sdbus, api->path, api->name, "broadcast",
- "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN));
+ "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE));
if (rc < 0)
ERROR("error while broadcasting event %s", event);
json_object_put(object);
uint32_t flags;
struct afb_session *session;
struct listener *listener;
+ enum json_tokener_error jerr;
/* check the interface */
if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
dreq->xreq.context.flags = flags;
dreq->xreq.cred = afb_cred_mixed_on_behalf_import(listener->origin->cred, uuid, creds && creds[0] ? creds : NULL);
dreq->message = sd_bus_message_ref(message);
- dreq->json = json_tokener_parse(dreq->request);
- if (dreq->json == NULL && strcmp(dreq->request, "null")) {
+ dreq->json = json_tokener_parse_verbose(dreq->request, &jerr);
+ if (jerr != json_tokener_success) {
/* lazy error detection of json request. Is it to improve? */
dreq->json = json_object_new_string(dreq->request);
}
return -1;
}
+#endif