From 04241c8168fe8826b4fc7a80500577b50936b57a Mon Sep 17 00:00:00 2001 From: José Bollo Date: Tue, 10 Oct 2017 18:21:54 +0200 Subject: Add the function afb_req_get_uid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I9caf38ee3811cf10b546489094f0bb5b3d844c40 Signed-off-by: José Bollo --- bindings/samples/HelloWorld.c | 7 +++++++ include/afb/afb-req.h | 11 +++++++++++ include/afb/afb-request-itf.h | 3 +++ include/afb/afb-request.h | 11 +++++++++++ src/afb-hook.c | 14 +++++++++++++- src/afb-hook.h | 6 +++++- src/afb-trace.c | 10 +++++++++- src/afb-xreq.c | 15 +++++++++++++++ 8 files changed, 74 insertions(+), 3 deletions(-) diff --git a/bindings/samples/HelloWorld.c b/bindings/samples/HelloWorld.c index 0b0d1a96..134ea620 100644 --- a/bindings/samples/HelloWorld.c +++ b/bindings/samples/HelloWorld.c @@ -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} }; diff --git a/include/afb/afb-req.h b/include/afb/afb-req.h index 6f98e900..af33108c 100644 --- a/include/afb/afb-req.h +++ b/include/afb/afb-req.h @@ -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); +} + diff --git a/include/afb/afb-request-itf.h b/include/afb/afb-request-itf.h index e542442c..d9d9c835 100644 --- a/include/afb/afb-request-itf.h +++ b/include/afb/afb-request-itf.h @@ -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); + }; diff --git a/include/afb/afb-request.h b/include/afb/afb-request.h index fcab03c3..f9a1bbbb 100644 --- a/include/afb/afb-request.h +++ b/include/afb/afb-request.h @@ -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); +} + diff --git a/src/afb-hook.c b/src/afb-hook.c index 7ae713e2..54058dea 100644 --- a/src/afb-hook.c +++ b/src/afb-hook.c @@ -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 *****************************************************************************/ diff --git a/src/afb-hook.h b/src/afb-hook.h index b7fd88d8..c9ecb331 100644 --- a/src/afb-hook.h +++ b/src/afb-hook.h @@ -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) diff --git a/src/afb-trace.c b/src/afb-trace.c index 40f27a20..fb077956 100644 --- a/src/afb-trace.c +++ b/src/afb-trace.c @@ -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, }; /*******************************************************************************/ diff --git a/src/afb-xreq.c b/src/afb-xreq.c index 2e6108d8..47f2ec67 100644 --- a/src/afb-xreq.c +++ b/src/afb-xreq.c @@ -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, }; /******************************************************************************/ -- cgit 1.2.3-korg