Update copyright dates
[src/app-framework-binder.git] / src / afb-api-dbus.c
index 13cbf7d..7b6134f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Copyright (C) 2015-2020 "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 <string.h>
 #include <assert.h>
 #include <errno.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-req-itf.h>
-
-#include "afb-common.h"
+#include <afb/afb-event-x2.h>
 
 #include "afb-session.h"
 #include "afb-msg-json.h"
-#include "afb-apis.h"
-#include "afb-api-so.h"
+#include "afb-api.h"
+#include "afb-apiset.h"
+#include "afb-api-dbus.h"
 #include "afb-context.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/";
 
@@ -69,13 +76,11 @@ struct api_dbus
                        struct sd_bus_slot *slot_call;
                        struct afb_evt_listener *listener; /* listener for broadcasted events */
                        struct origin *origins;
+                       struct afb_apiset *apiset;
                } server;
        };
 };
 
-#define RETOK   1
-#define RETERR  2
-
 /******************* common part **********************************/
 
 /*
@@ -110,7 +115,7 @@ static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pat
                goto error2;
        }
        api->api++;
-       if (!afb_apis_is_valid_api_name(api->api)) {
+       if (!afb_api_is_valid_name(api->api)) {
                errno = EINVAL;
                goto error2;
        }
@@ -125,7 +130,8 @@ static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pat
        }
 
        /* choose the bus */
-       sdbus = (system ? afb_common_get_system_bus : afb_common_get_user_bus)();
+       jobs_acquire_event_manager();
+       sdbus = (system ? systemd_get_system_bus : systemd_get_user_bus)();
        if (sdbus == NULL)
                goto error2;
 
@@ -223,7 +229,7 @@ struct dbus_memo {
 struct dbus_event
 {
        struct dbus_event *next;
-       struct afb_event event;
+       struct afb_event_x2 *event;
        int id;
        int refcount;
 };
@@ -235,7 +241,7 @@ static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct
 
        memo = malloc(sizeof *memo);
        if (memo != NULL) {
-               afb_xreq_addref(xreq);
+               afb_xreq_unhooked_addref(xreq);
                memo->xreq = xreq;
                memo->msgid = 0;
                memo->api = api;
@@ -259,7 +265,7 @@ static void api_dbus_client_memo_destroy(struct dbus_memo *memo)
                prv = &(*prv)->next;
        }
 
-       afb_xreq_unref(memo->xreq);
+       afb_xreq_unhooked_unref(memo->xreq);
        free(memo);
 }
 
@@ -280,32 +286,28 @@ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_
 {
        int rc;
        struct dbus_memo *memo;
-       const char *first, *second;
-       uint8_t type;
-       uint32_t flags;
+       const char *json, *error, *info;
+       struct json_object *object;
+       enum json_tokener_error jerr;
 
        /* retrieve the recorded data */
        memo = userdata;
 
        /* get the answer */
-       rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags);
+       rc = sd_bus_message_read(message, "sss", &json, &error, &info);
        if (rc < 0) {
                /* failing to have the answer */
-               afb_xreq_fail(memo->xreq, "error", "dbus error");
+               afb_xreq_reply(memo->xreq, NULL, "error", "dbus error");
        } else {
                /* report the answer */
-               memo->xreq->context.flags = (unsigned)flags;
-               switch(type) {
-               case RETOK:
-                       afb_xreq_success(memo->xreq, json_tokener_parse(first), *second ? second : NULL);
-                       break;
-               case RETERR:
-                       afb_xreq_fail(memo->xreq, first, *second ? second : NULL);
-                       break;
-               default:
-                       afb_xreq_fail(memo->xreq, "error", "dbus link broken");
-                       break;
+               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;
@@ -319,24 +321,27 @@ static void api_dbus_client_call(void *closure, struct afb_xreq *xreq)
        int rc;
        struct dbus_memo *memo;
        struct sd_bus_message *msg;
+       const char *creds;
 
        /* create the recording data */
        memo = api_dbus_client_memo_make(api, xreq);
        if (memo == NULL) {
-               afb_xreq_fail(xreq, "error", "out of memory");
+               afb_xreq_reply(memo->xreq, NULL, "error", "out of memory");
                return;
        }
 
        /* creates the message */
        msg = NULL;
-       rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, xreq->verb);
+       rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, xreq->request.called_verb);
        if (rc < 0)
                goto error;
 
-       rc = sd_bus_message_append(msg, "ssu",
+       creds = xreq_on_behalf_cred_export(xreq);
+       rc = sd_bus_message_append(msg, "ssus",
                        afb_xreq_raw(xreq, &size),
                        afb_session_uuid(xreq->context.session),
-                       (uint32_t)xreq->context.flags);
+                       (uint32_t)xreq->context.flags,
+                       creds ?: "");
        if (rc < 0)
                goto error;
 
@@ -352,53 +357,47 @@ static void api_dbus_client_call(void *closure, struct afb_xreq *xreq)
 error:
        /* if there was an error report it directly */
        errno = -rc;
-       afb_xreq_fail(xreq, "error", "dbus error");
+       afb_xreq_reply(memo->xreq, NULL, "error", "dbus error");
        api_dbus_client_memo_destroy(memo);
 end:
        sd_bus_message_unref(msg);
 }
 
-static int api_dbus_service_start(void *closure, int share_session, int onneed)
-{
-       struct api_dbus *api = closure;
-
-       /* not an error when onneed */
-       if (onneed != 0)
-               return 0;
-
-       /* already started: it is an error */
-       ERROR("The Dbus binding %s is not a startable service", api->name);
-       return -1;
-}
-
 /* receives broadcasted events */
 static int api_dbus_client_on_broadcast_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);
+       const unsigned char *uuid;
+       size_t szuuid;
+       uint8_t hop;
+       enum json_tokener_error jerr;
+
+       int rc = sd_bus_message_read(m, "ssayy", &event, &data, &uuid, &szuuid, &hop);
        if (rc < 0)
                ERROR("unreadable broadcasted event");
        else {
-               object = json_tokener_parse(data);
-               afb_evt_broadcast(event, object);
+               object = json_tokener_parse_verbose(data, &jerr);
+               if (jerr != json_tokener_success)
+                       object = json_object_new_string(data);
+               afb_evt_rebroadcast(event, object, uuid, hop);
        }
        return 1;
 }
 
-/* search the event */
+/* search the eventid */
 static struct dbus_event *api_dbus_client_event_search(struct api_dbus *api, int id, const char *name)
 {
        struct dbus_event *ev;
 
        ev = api->client.events;
-       while (ev != NULL && (ev->id != id || 0 != strcmp(afb_evt_event_name(ev->event), name)))
+       while (ev != NULL && (ev->id != id || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
                ev = ev->next;
 
        return ev;
 }
 
-/* adds an event */
+/* adds an eventid */
 static void api_dbus_client_event_create(struct api_dbus *api, int id, const char *name)
 {
        struct dbus_event *ev;
@@ -413,8 +412,8 @@ static void api_dbus_client_event_create(struct api_dbus *api, int id, const cha
        /* no conflict, try to add it */
        ev = malloc(sizeof *ev);
        if (ev != NULL) {
-               ev->event = afb_evt_create_event(name);
-               if (ev->event.closure == NULL)
+               ev->event = afb_evt_event_x2_create(name);
+               if (ev->event == NULL)
                        free(ev);
                else {
                        ev->refcount = 1;
@@ -427,7 +426,7 @@ static void api_dbus_client_event_create(struct api_dbus *api, int id, const cha
        ERROR("can't create event %s, out of memory", name);
 }
 
-/* removes an event */
+/* removes an eventid */
 static void api_dbus_client_event_drop(struct api_dbus *api, int id, const char *name)
 {
        struct dbus_event *ev, **prv;
@@ -450,7 +449,7 @@ static void api_dbus_client_event_drop(struct api_dbus *api, int id, const char
        *prv = ev->next;
 
        /* destroys the event */
-       afb_event_drop(ev->event);
+       afb_evt_event_x2_unref(ev->event);
        free(ev);
 }
 
@@ -459,6 +458,7 @@ static void api_dbus_client_event_push(struct api_dbus *api, int id, const char
 {
        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);
@@ -468,8 +468,10 @@ static void api_dbus_client_event_push(struct api_dbus *api, int id, const char
        }
 
        /* destroys the event */
-       object = json_tokener_parse(data);
-       afb_event_push(ev->event, object);
+       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);
 }
 
 /* subscribes an event */
@@ -578,16 +580,15 @@ static int api_dbus_client_on_manage_event(sd_bus_message *m, void *userdata, sd
 }
 
 static struct afb_api_itf dbus_api_itf = {
-       .call = api_dbus_client_call,
-       .service_start = api_dbus_service_start
+       .call = api_dbus_client_call
 };
 
 /* adds a afb-dbus-service client api */
-int afb_api_dbus_add_client(const char *path)
+int afb_api_dbus_add_client(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
 {
        int rc;
        struct api_dbus *api;
-       struct afb_api afb_api;
+       struct afb_api_item afb_api;
        char *match;
 
        /* create the dbus client api */
@@ -621,7 +622,8 @@ int afb_api_dbus_add_client(const char *path)
        /* record it as an API */
        afb_api.closure = api;
        afb_api.itf = &dbus_api_itf;
-       if (afb_apis_add(api->api, afb_api) < 0)
+       afb_api.group = NULL;
+       if (afb_apiset_add(declare_set, api->api, afb_api) < 0)
                goto error2;
 
        return 0;
@@ -634,10 +636,10 @@ error:
 
 /******************* event structures for server part **********************************/
 
-static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid);
-static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid);
-static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
-static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
+static void afb_api_dbus_server_event_add(void *closure, const char *event, uint16_t eventid);
+static void afb_api_dbus_server_event_remove(void *closure, const char *event, uint16_t eventid);
+static void afb_api_dbus_server_event_push(void *closure, const char *event, uint16_t eventid, struct json_object *object);
+static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop);
 
 /* the interface for events broadcasting */
 static const struct afb_evt_itf evt_broadcast_itf = {
@@ -668,7 +670,7 @@ struct origin
        struct afb_cred *cred;
 
        /* the origin */
-       char name[1];
+       char name[];
 };
 
 /* get the credentials for the message */
@@ -680,6 +682,7 @@ static void init_origin_creds(struct origin *origin)
        gid_t gid;
        pid_t pid;
        const char *context;
+       struct afb_cred *ocred;
 
        rc = sd_bus_get_name_creds(origin->api->sdbus, origin->name,
                        SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_SELINUX_CONTEXT,
@@ -687,13 +690,14 @@ static void init_origin_creds(struct origin *origin)
        if (rc < 0)
                origin->cred = NULL;
        else {
-               afb_cred_unref(origin->cred);
                sd_bus_creds_get_uid(c, &uid);
                sd_bus_creds_get_gid(c, &gid);
                sd_bus_creds_get_pid(c, &pid);
                sd_bus_creds_get_selinux_context(c, &context);
+               ocred = origin->cred;
                origin->cred = afb_cred_create(uid, gid, pid, context);
                sd_bus_creds_unref(c);
+               afb_cred_unref(ocred);
        }
 }
 
@@ -712,7 +716,7 @@ static struct origin *afb_api_dbus_server_origin_get(struct api_dbus *api, const
        }
 
        /* not found, create it */
-       origin = malloc(strlen(sender) + sizeof *origin);
+       origin = malloc(strlen(sender) + 1 + sizeof *origin);
        if (origin == NULL)
                errno = ENOMEM;
        else {
@@ -826,63 +830,51 @@ static struct json_object *dbus_req_json(struct afb_xreq *xreq)
        return dreq->json;
 }
 
-/* get the argument of the request of 'name' */
-static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
+void dbus_req_raw_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
 {
+       struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
        int rc;
-       rc = sd_bus_reply_method_return(dreq->message,
-                       "yssu", type, first ? : "", second ? : "", (uint32_t)dreq->xreq.context.flags);
+
+       rc = sd_bus_reply_method_return(dreq->message, "sss",
+               obj ? json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE) : "",
+               error ? : "",
+               info ? : "");
        if (rc < 0)
                ERROR("sending the reply failed");
 }
 
-static void dbus_req_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
-{
-       struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
-
-       dbus_req_reply(dreq, RETOK, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN), info);
-}
-
-static void dbus_req_fail(struct afb_xreq *xreq, const char *status, const char *info)
-{
-       struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
-
-       dbus_req_reply(dreq, RETERR, status, info);
-}
-
 static void afb_api_dbus_server_event_send(struct origin *origin, char order, const char *event, int eventid, const char *data, uint64_t msgid);
 
-static int dbus_req_subscribe(struct afb_xreq *xreq, struct afb_event event)
+static int dbus_req_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
 {
        struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
        uint64_t msgid;
        int rc;
 
-       rc = afb_evt_add_watch(dreq->listener->listener, event);
+       rc = afb_evt_listener_watch_x2(dreq->listener->listener, event);
        sd_bus_message_get_cookie(dreq->message, &msgid);
-       afb_api_dbus_server_event_send(dreq->listener->origin, 'S', afb_evt_event_name(event), afb_evt_event_id(event), "", msgid);
+       afb_api_dbus_server_event_send(dreq->listener->origin, 'S', afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event), "", msgid);
        return rc;
 }
 
-static int dbus_req_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
+static int dbus_req_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
 {
        struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
        uint64_t msgid;
        int rc;
 
        sd_bus_message_get_cookie(dreq->message, &msgid);
-       afb_api_dbus_server_event_send(dreq->listener->origin, 'U', afb_evt_event_name(event), afb_evt_event_id(event), "", msgid);
-       rc = afb_evt_remove_watch(dreq->listener->listener, event);
+       afb_api_dbus_server_event_send(dreq->listener->origin, 'U', afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event), "", msgid);
+       rc = afb_evt_listener_unwatch_x2(dreq->listener->listener, event);
        return rc;
 }
 
 const struct afb_xreq_query_itf afb_api_dbus_xreq_itf = {
        .json = dbus_req_json,
-       .success = dbus_req_success,
-       .fail = dbus_req_fail,
+       .reply = dbus_req_raw_reply,
        .unref = dbus_req_destroy,
        .subscribe = dbus_req_subscribe,
-       .unsubscribe = dbus_req_unsubscribe
+       .unsubscribe = dbus_req_unsubscribe,
 };
 
 /******************* server part **********************************/
@@ -914,31 +906,32 @@ end:
        sd_bus_message_unref(msg);
 }
 
-static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid)
+static void afb_api_dbus_server_event_add(void *closure, const char *event, uint16_t eventid)
 {
        afb_api_dbus_server_event_send(closure, '+', event, eventid, "", 0);
 }
 
-static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid)
+static void afb_api_dbus_server_event_remove(void *closure, const char *event, uint16_t eventid)
 {
        afb_api_dbus_server_event_send(closure, '-', event, eventid, "", 0);
 }
 
-static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
+static void afb_api_dbus_server_event_push(void *closure, const char *event, uint16_t 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);
 }
 
-static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
+static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
 {
        int rc;
        struct api_dbus *api;
 
        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));
+                       "ssayy", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE),
+                       uuid, UUID_BINARY_LENGTH, hop);
        if (rc < 0)
                ERROR("error while broadcasting event %s", event);
        json_object_put(object);
@@ -950,11 +943,13 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
        int rc;
        const char *method;
        const char *uuid;
+       const char *creds;
        struct dbus_req *dreq;
        struct api_dbus *api = userdata;
        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)
@@ -969,7 +964,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
                goto out_of_memory;
 
        /* get the data */
-       rc = sd_bus_message_read(message, "ssu", &dreq->request, &uuid, &flags);
+       rc = sd_bus_message_read(message, "ssus", &dreq->request, &uuid, &flags, &creds);
        if (rc < 0) {
                sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
                goto error;
@@ -977,7 +972,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
 
        /* connect to the context */
        afb_xreq_init(&dreq->xreq, &afb_api_dbus_xreq_itf);
-       if (afb_context_connect(&dreq->xreq.context, uuid, NULL) < 0)
+       if (afb_context_connect(&dreq->xreq.context, uuid, NULL, NULL) < 0)
                goto out_of_memory;
        session = dreq->xreq.context.session;
 
@@ -987,18 +982,18 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
                goto out_of_memory;
 
        /* fulfill the request and emit it */
-       dreq->xreq.context.flags = flags;
+       afb_context_change_cred(&dreq->xreq.context, listener->origin->cred);
+       afb_context_on_behalf_import(&dreq->xreq.context, creds);
        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);
        }
        dreq->listener = listener;
-       dreq->xreq.api = api->api;
-       dreq->xreq.verb = method;
-       afb_apis_call(&dreq->xreq);
-       afb_xreq_unref(&dreq->xreq);
+       dreq->xreq.request.called_api = api->api;
+       dreq->xreq.request.called_verb = method;
+       afb_xreq_process(&dreq->xreq, api->server.apiset);
        return 1;
 
 out_of_memory:
@@ -1009,7 +1004,7 @@ error:
 }
 
 /* create the service */
-int afb_api_dbus_add_server(const char *path)
+int afb_api_dbus_add_server(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
 {
        int rc;
        struct api_dbus *api;
@@ -1037,6 +1032,7 @@ int afb_api_dbus_add_server(const char *path)
        INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
 
        api->server.listener = afb_evt_listener_create(&evt_broadcast_itf, api);
+       api->server.apiset = afb_apiset_addref(call_set);
        return 0;
 error3:
        sd_bus_release_name(api->sdbus, api->name);
@@ -1046,4 +1042,5 @@ error:
        return -1;
 }
 
+#endif