Add the function afb_req_get_uid
[src/app-framework-binder.git] / src / afb-xreq.c
index 8bfb363..47f2ec6 100644 (file)
@@ -34,6 +34,7 @@
 #include "afb-cred.h"
 #include "afb-hook.h"
 #include "afb-api.h"
+#include "afb-api-dyn.h"
 #include "afb-apiset.h"
 #include "afb-auth.h"
 #include "jobs.h"
@@ -172,8 +173,8 @@ static struct subcall *subcall_alloc(
                afb_context_subinit(&subcall->xreq.context, &caller->context);
                subcall->xreq.cred = afb_cred_addref(caller->cred);
                subcall->xreq.json = args;
-               subcall->xreq.api = api;
-               subcall->xreq.verb = verb;
+               subcall->xreq.request.api = api;
+               subcall->xreq.request.verb = verb;
                subcall->xreq.caller = caller;
                afb_xreq_unhooked_addref(caller);
        }
@@ -232,7 +233,7 @@ static void subcall_process(struct subcall *subcall, void (*completion)(struct s
        subcall->completion = completion;
        if (subcall->xreq.caller->queryitf->subcall) {
                subcall->xreq.caller->queryitf->subcall(
-                       subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb,
+                       subcall->xreq.caller, subcall->xreq.request.api, subcall->xreq.request.verb,
                        subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
        } else {
                afb_xreq_unhooked_addref(&subcall->xreq);
@@ -462,7 +463,7 @@ static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eve
 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
 {
        if (xreq->listener)
-               return afb_evt_add_watch(xreq->listener, eventid);
+               return afb_evt_eventid_add_watch(xreq->listener, eventid);
        if (xreq->queryitf->subscribe)
                return xreq->queryitf->subscribe(xreq, eventid);
        ERROR("no event listener, subscription impossible");
@@ -485,7 +486,7 @@ static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_e
 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
 {
        if (xreq->listener)
-               return afb_evt_remove_watch(xreq->listener, eventid);
+               return afb_evt_eventid_remove_watch(xreq->listener, eventid);
        if (xreq->queryitf->unsubscribe)
                return xreq->queryitf->unsubscribe(xreq, eventid);
        ERROR("no event listener, unsubscription impossible");
@@ -569,7 +570,7 @@ static void xreq_vverbose_cb(struct afb_request *closure, int level, const char
        if (!fmt || vasprintf(&p, fmt, args) < 0)
                vverbose(level, file, line, func, fmt, args);
        else {
-               verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
+               verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.api, p);
                free(p);
        }
 }
@@ -598,6 +599,12 @@ static void *xreq_context_make_cb(struct afb_request *closure, int replace, void
        return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
 }
 
+static int xreq_get_uid_cb(struct afb_request *closure)
+{
+       struct afb_xreq *xreq = from_request(closure);
+       return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
+}
+
 /******************************************************************************/
 
 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
@@ -802,6 +809,13 @@ static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replac
        return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
 }
 
+static int xreq_hooked_get_uid_cb(struct afb_request *closure)
+{
+       struct afb_xreq *xreq = from_request(closure);
+       int r = xreq_get_uid_cb(closure);
+       return afb_hook_xreq_get_uid(xreq, r);
+}
+
 /******************************************************************************/
 
 const struct afb_request_itf xreq_itf = {
@@ -830,6 +844,7 @@ const struct afb_request_itf xreq_itf = {
        .subscribe_eventid = xreq_subscribe_eventid_cb,
        .unsubscribe_eventid = xreq_unsubscribe_eventid_cb,
        .subcall_request = xreq_subcall_request_cb,
+       .get_uid = xreq_get_uid_cb,
 };
 
 const struct afb_request_itf xreq_hooked_itf = {
@@ -858,6 +873,7 @@ const struct afb_request_itf xreq_hooked_itf = {
        .subscribe_eventid = xreq_hooked_subscribe_eventid_cb,
        .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb,
        .subcall_request = xreq_hooked_subcall_request_cb,
+       .get_uid = xreq_hooked_get_uid_cb,
 };
 
 /******************************************************************************/
@@ -1039,6 +1055,15 @@ void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb
                        verb->callback(to_req(xreq));
 }
 
+void afb_xreq_call_verb_vdyn(struct afb_xreq *xreq, const struct afb_api_dyn_verb *verb)
+{
+       if (!verb)
+               afb_xreq_fail_unknown_verb(xreq);
+       else
+               if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
+                       verb->callback(to_request(xreq));
+}
+
 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
 {
        memset(xreq, 0, sizeof *xreq);
@@ -1049,12 +1074,12 @@ void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *query
 
 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
 {
-       afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
+       afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
 }
 
 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
 {
-       afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
+       afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api);
 }
 
 static void init_hooking(struct afb_xreq *xreq)
@@ -1118,12 +1143,12 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
 
        /* lookup at the api */
        xreq->apiset = apiset;
-       api = afb_apiset_lookup_started(apiset, xreq->api, 1);
+       api = afb_apiset_lookup_started(apiset, xreq->request.api, 1);
        if (!api) {
                if (errno == ENOENT)
-                       early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
+                       early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
                else
-                       early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
+                       early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api);
                goto end;
        }
        xreq->context.api_key = api;
@@ -1134,8 +1159,8 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
                while (caller) {
                        if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
                                /* noconcurrency lock detected */
-                               ERROR("self-lock detected in call stack for API %s", xreq->api);
-                               early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api);
+                               ERROR("self-lock detected in call stack for API %s", xreq->request.api);
+                               early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api);
                                goto end;
                        }
                        caller = caller->caller;