Add the function afb_req_get_uid
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 10 Oct 2017 16:21:54 +0000 (18:21 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 10 Oct 2017 18:28:40 +0000 (20:28 +0200)
Change-Id: I9caf38ee3811cf10b546489094f0bb5b3d844c40
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
bindings/samples/HelloWorld.c
include/afb/afb-req.h
include/afb/afb-request-itf.h
include/afb/afb-request.h
src/afb-hook.c
src/afb-hook.h
src/afb-trace.c
src/afb-xreq.c

index 0b0d1a9..134ea62 100644 (file)
@@ -428,6 +428,12 @@ static void appid (afb_req request)
        free(aid);
 }
 
+static void uid (afb_req request)
+{
+       int uid = afb_req_get_uid(request);
+       afb_req_success_f(request, json_object_new_int(uid), "uid is %d", uid);
+}
+
 static int preinit()
 {
        AFB_NOTICE("hello binding comes to live");
@@ -468,6 +474,7 @@ static const afb_verb_v2 verbs[]= {
   { .verb="broadcast",   .callback=broadcast },
   { .verb="hasperm",     .callback=hasperm },
   { .verb="appid",       .callback=appid },
+  { .verb="uid",         .callback=uid },
   { .verb="exit",        .callback=exitnow },
   { .verb=NULL}
 };
index 6f98e90..af33108 100644 (file)
@@ -420,3 +420,14 @@ static inline char *afb_req_get_application_id(struct afb_req req)
        return req.itf->get_application_id(req.closure);
 }
 
+/*
+ * Get the user identifier (UID) of the client application for the
+ * request 'req'.
+ *
+ * Returns -1 when the application can not be identified.
+ */
+static inline int afb_req_get_uid(struct afb_req req)
+{
+       return req.itf->get_uid(req.closure);
+}
+
index e542442..d9d9c83 100644 (file)
@@ -198,5 +198,8 @@ struct afb_request_itf
                        void (*callback)(void*, int, struct json_object*, struct afb_request *request),
                        void *cb_closure);
 
+       int (*get_uid)(
+                       struct afb_request *request);
+
 };
 
index fcab03c..f9a1bbb 100644 (file)
@@ -376,3 +376,14 @@ static inline char *afb_request_get_application_id(struct afb_request *request)
        return request->itf->get_application_id(request);
 }
 
+/*
+ * Get the user identifier (UID) of the client for the
+ * request 'request'.
+ *
+ * Returns -1 when the application can not be identified.
+ */
+static inline int afb_request_get_uid(struct afb_request *request)
+{
+       return request->itf->get_uid(request);
+}
+
index 7ae713e..54058de 100644 (file)
@@ -364,6 +364,11 @@ static void hook_xreq_context_make_default_cb(void *closure, const struct afb_ho
        _hook_xreq_(xreq, "context_make(replace=%s, %p, %p, %p) -> %p", replace?"yes":"no", create_value, free_value, create_closure, result);
 }
 
+static void hook_xreq_get_uid_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
+{
+       _hook_xreq_(xreq, "get_uid() -> %d", result);
+}
+
 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
        .hook_xreq_begin = hook_xreq_begin_default_cb,
        .hook_xreq_end = hook_xreq_end_default_cb,
@@ -390,7 +395,8 @@ static struct afb_hook_xreq_itf hook_xreq_default_itf = {
        .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb,
        .hook_xreq_has_permission = hook_xreq_has_permission_default_cb,
        .hook_xreq_get_application_id = hook_xreq_get_application_id_default_cb,
-       .hook_xreq_context_make = hook_xreq_context_make_default_cb
+       .hook_xreq_context_make = hook_xreq_context_make_default_cb,
+       .hook_xreq_get_uid = hook_xreq_get_uid_default_cb,
 };
 
 /******************************************************************************
@@ -556,6 +562,12 @@ void *afb_hook_xreq_context_make(const struct afb_xreq *xreq, int replace, void
        return result;
 }
 
+int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
+{
+       _HOOK_XREQ_(get_uid, xreq, result);
+       return result;
+}
+
 /******************************************************************************
  * section: hooking xreqs
  *****************************************************************************/
index b7fd88d..c9ecb33 100644 (file)
@@ -77,6 +77,7 @@ struct afb_hookid
 #define afb_hook_flag_req_has_permission       0x00800000
 #define afb_hook_flag_req_get_application_id   0x01000000
 #define afb_hook_flag_req_context_make         0x02000000
+#define afb_hook_flag_req_get_uid              0x04000000
 
 /* common flags */
 #define afb_hook_flags_req_life                (afb_hook_flag_req_begin|afb_hook_flag_req_end)
@@ -87,7 +88,8 @@ struct afb_hookid
 #define afb_hook_flags_req_subcalls    (afb_hook_flag_req_subcall|afb_hook_flag_req_subcall_result\
                                        |afb_hook_flag_req_subcall_req|afb_hook_flag_req_subcall_req_result\
                                        |afb_hook_flag_req_subcallsync|afb_hook_flag_req_subcallsync_result)
-#define afb_hook_flags_req_security    (afb_hook_flag_req_has_permission|afb_hook_flag_req_get_application_id)
+#define afb_hook_flags_req_security    (afb_hook_flag_req_has_permission|afb_hook_flag_req_get_application_id\
+                                       |afb_hook_flag_req_get_uid)
 
 /* extra flags */
 #define afb_hook_flags_req_ref         (afb_hook_flag_req_addref|afb_hook_flag_req_unref)
@@ -130,6 +132,7 @@ struct afb_hook_xreq_itf {
        void (*hook_xreq_has_permission)(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *permission, int result);
        void (*hook_xreq_get_application_id)(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, char *result);
        void (*hook_xreq_context_make)(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure, void *result);
+       void (*hook_xreq_get_uid)(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result);
 };
 
 extern void afb_hook_init_xreq(struct afb_xreq *xreq);
@@ -165,6 +168,7 @@ extern void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int st
 extern int afb_hook_xreq_has_permission(const struct afb_xreq *xreq, const char *permission, int result);
 extern char *afb_hook_xreq_get_application_id(const struct afb_xreq *xreq, char *result);
 extern void *afb_hook_xreq_context_make(const struct afb_xreq *xreq, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure, void *result);
+extern int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result);
 
 /*********************************************************
 * section hooking export (daemon interface)
index 40f27a2..fb07795 100644 (file)
@@ -230,6 +230,7 @@ static struct flag xreq_flags[] = { /* must be sorted by names */
                { "fail",               afb_hook_flag_req_fail },
                { "get",                afb_hook_flag_req_get },
                { "get_application_id", afb_hook_flag_req_get_application_id },
+               { "get_uid",            afb_hook_flag_req_get_uid },
                { "has_permission",     afb_hook_flag_req_has_permission },
                { "json",               afb_hook_flag_req_json },
                { "life",               afb_hook_flags_req_life },
@@ -489,6 +490,12 @@ static void hook_xreq_context_make(void *closure, const struct afb_hookid *hooki
                                        "result", pr);
 }
 
+static void hook_xreq_get_uid(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
+{
+       hook_xreq(closure, hookid, xreq, "get_uid", "{si}",
+                                       "result", result);
+}
+
 static struct afb_hook_xreq_itf hook_xreq_itf = {
        .hook_xreq_begin = hook_xreq_begin,
        .hook_xreq_end = hook_xreq_end,
@@ -515,7 +522,8 @@ static struct afb_hook_xreq_itf hook_xreq_itf = {
        .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result,
        .hook_xreq_has_permission = hook_xreq_has_permission,
        .hook_xreq_get_application_id = hook_xreq_get_application_id,
-       .hook_xreq_context_make = hook_xreq_context_make
+       .hook_xreq_context_make = hook_xreq_context_make,
+       .hook_xreq_get_uid = hook_xreq_get_uid,
 };
 
 /*******************************************************************************/
index 2e6108d..47f2ec6 100644 (file)
@@ -599,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)
@@ -803,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 = {
@@ -831,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 = {
@@ -859,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,
 };
 
 /******************************************************************************/