2 * Copyright (C) 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 #include <json-c/json.h>
26 #include <afb/afb-binding-v1.h>
27 #include <afb/afb-binding-v2.h>
28 #include <afb/afb-request.h>
30 #include "afb-context.h"
33 #include "afb-msg-json.h"
37 #include "afb-api-dyn.h"
38 #include "afb-apiset.h"
43 /******************************************************************************/
45 static void xreq_finalize(struct afb_xreq *xreq)
48 afb_xreq_fail(xreq, "error", "no reply");
50 afb_hook_xreq_end(xreq);
52 afb_xreq_unhooked_unref(xreq->caller);
53 xreq->queryitf->unref(xreq);
56 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
58 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
61 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
63 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
67 /******************************************************************************/
69 static inline struct afb_request *to_request(struct afb_xreq *xreq)
71 return &xreq->request;
74 static inline struct afb_req to_req(struct afb_xreq *xreq)
76 return (struct afb_req){ .itf = xreq->request.itf, .closure = &xreq->request };
79 static inline struct afb_xreq *from_request(struct afb_request *request)
81 return CONTAINER_OF(struct afb_xreq, request, request);
84 /******************************************************************************/
90 void (*completion)(struct subcall*, int, struct json_object*);
94 struct jobloop *jobloop;
95 struct json_object *result;
100 void (*callback)(void*, int, struct json_object*);
101 void (*callback_req)(void*, int, struct json_object*, struct afb_req);
102 void (*callback_request)(void*, int, struct json_object*, struct afb_request*);
109 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
111 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
113 return afb_xreq_subscribe(subcall->xreq.caller, eventid);
116 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
118 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
120 return afb_xreq_unsubscribe(subcall->xreq.caller, eventid);
123 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
125 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
127 subcall->completion(subcall, status, result);
128 json_object_put(result);
129 afb_xreq_unhooked_unref(&subcall->xreq);
132 static void subcall_destroy_cb(struct afb_xreq *xreq)
134 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
136 json_object_put(subcall->xreq.json);
137 afb_cred_unref(subcall->xreq.cred);
141 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
142 .reply = subcall_reply_cb,
143 .unref = subcall_destroy_cb,
144 .subscribe = subcall_subscribe_cb,
145 .unsubscribe = subcall_unsubscribe_cb
148 static struct subcall *subcall_alloc(
149 struct afb_xreq *caller,
152 struct json_object *args
155 struct subcall *subcall;
156 size_t lenapi, lenverb;
159 lenapi = 1 + strlen(api);
160 lenverb = 1 + strlen(verb);
161 subcall = malloc(lenapi + lenverb + sizeof *subcall);
163 ERROR("out of memory");
165 copy = (char*)&subcall[1];
166 memcpy(copy, api, lenapi);
168 copy = ©[lenapi];
169 memcpy(copy, verb, lenverb);
172 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
173 afb_context_subinit(&subcall->xreq.context, &caller->context);
174 subcall->xreq.cred = afb_cred_addref(caller->cred);
175 subcall->xreq.json = args;
176 subcall->xreq.request.api = api;
177 subcall->xreq.request.verb = verb;
178 subcall->xreq.caller = caller;
179 afb_xreq_unhooked_addref(caller);
185 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
187 subcall->callback(subcall->closure, status, result);
190 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
192 subcall->callback_req(subcall->closure, status, result, to_req(subcall->xreq.caller));
195 static void subcall_request_on_reply(struct subcall *subcall, int status, struct json_object *result)
197 subcall->callback_request(subcall->closure, status, result, to_request(subcall->xreq.caller));
200 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
202 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
203 subcall_on_reply(subcall, status, result);
206 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
208 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
209 subcall_req_on_reply(subcall, status, result);
212 static void subcall_request_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
214 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
215 subcall_request_on_reply(subcall, status, result);
218 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
220 struct afb_xreq *xreq = closure;
223 ERROR("subcall replied more than one time!!");
224 json_object_put(result);
227 subcall_reply_cb(xreq, status, result);
231 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
233 subcall->completion = completion;
234 if (subcall->xreq.caller->queryitf->subcall) {
235 subcall->xreq.caller->queryitf->subcall(
236 subcall->xreq.caller, subcall->xreq.request.api, subcall->xreq.request.verb,
237 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
239 afb_xreq_unhooked_addref(&subcall->xreq);
240 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
244 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
246 subcall->callback = callback;
247 subcall->closure = cb_closure;
248 subcall_process(subcall, subcall_on_reply);
251 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
253 subcall->callback_req = callback;
254 subcall->closure = cb_closure;
255 subcall_process(subcall, subcall_req_on_reply);
258 static void subcall_request(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
260 subcall->callback_request = callback;
261 subcall->closure = cb_closure;
262 subcall_process(subcall, subcall_request_on_reply);
265 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
267 subcall->callback = callback;
268 subcall->closure = cb_closure;
269 subcall_process(subcall, subcall_hooked_on_reply);
272 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
274 subcall->callback_req = callback;
275 subcall->closure = cb_closure;
276 subcall_process(subcall, subcall_req_hooked_on_reply);
279 static void subcall_request_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
281 subcall->callback_request = callback;
282 subcall->closure = cb_closure;
283 subcall_process(subcall, subcall_request_hooked_on_reply);
286 static void subcall_sync_leave(struct subcall *subcall)
288 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
293 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
295 subcall->status = status;
296 subcall->result = json_object_get(result);
297 subcall_sync_leave(subcall);
300 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
302 struct subcall *subcall = closure;
305 subcall->jobloop = jobloop;
306 subcall->result = NULL;
308 subcall_process(subcall, subcall_sync_reply);
310 subcall->status = -1;
311 subcall_sync_leave(subcall);
315 static int subcallsync(struct subcall *subcall, struct json_object **result)
319 afb_xreq_unhooked_addref(&subcall->xreq);
320 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
321 *result = subcall->result;
322 if (rc < 0 || subcall->status < 0) {
323 *result = *result ?: afb_msg_json_internal_error();
326 afb_xreq_unhooked_unref(&subcall->xreq);
330 /******************************************************************************/
332 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
335 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
337 fun(first, second, info);
341 /******************************************************************************/
343 static struct json_object *xreq_json_cb(struct afb_request *closure)
345 struct afb_xreq *xreq = from_request(closure);
346 if (!xreq->json && xreq->queryitf->json)
347 xreq->json = xreq->queryitf->json(xreq);
351 static struct afb_arg xreq_get_cb(struct afb_request *closure, const char *name)
353 struct afb_xreq *xreq = from_request(closure);
355 struct json_object *object, *value;
357 if (xreq->queryitf->get)
358 arg = xreq->queryitf->get(xreq, name);
360 object = xreq_json_cb(closure);
361 if (json_object_object_get_ex(object, name, &value)) {
363 arg.value = json_object_get_string(value);
373 static void xreq_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
375 struct afb_xreq *xreq = from_request(closure);
378 ERROR("reply called more than one time!!");
379 json_object_put(obj);
382 if (xreq->queryitf->success)
383 xreq->queryitf->success(xreq, obj, info);
385 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
389 static void xreq_fail_cb(struct afb_request *closure, const char *status, const char *info)
391 struct afb_xreq *xreq = from_request(closure);
394 ERROR("reply called more than one time!!");
397 if (xreq->queryitf->fail)
398 xreq->queryitf->fail(xreq, status, info);
400 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
404 static void xreq_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
406 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
409 static void xreq_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
411 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
414 static void *xreq_context_get_cb(struct afb_request *closure)
416 struct afb_xreq *xreq = from_request(closure);
417 return afb_context_get(&xreq->context);
420 static void xreq_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
422 struct afb_xreq *xreq = from_request(closure);
423 afb_context_set(&xreq->context, value, free_value);
426 static struct afb_request *xreq_addref_cb(struct afb_request *closure)
428 struct afb_xreq *xreq = from_request(closure);
429 afb_xreq_unhooked_addref(xreq);
433 static void xreq_unref_cb(struct afb_request *closure)
435 struct afb_xreq *xreq = from_request(closure);
436 afb_xreq_unhooked_unref(xreq);
439 static void xreq_session_close_cb(struct afb_request *closure)
441 struct afb_xreq *xreq = from_request(closure);
442 afb_context_close(&xreq->context);
445 static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level)
447 struct afb_xreq *xreq = from_request(closure);
448 return afb_context_change_loa(&xreq->context, level);
451 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
452 static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event)
454 return xreq_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
457 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
459 struct afb_xreq *xreq = from_request(closure);
460 return afb_xreq_subscribe(xreq, eventid);
463 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
466 return afb_evt_eventid_add_watch(xreq->listener, eventid);
467 if (xreq->queryitf->subscribe)
468 return xreq->queryitf->subscribe(xreq, eventid);
469 ERROR("no event listener, subscription impossible");
474 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
475 static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
477 return xreq_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
480 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
482 struct afb_xreq *xreq = from_request(closure);
483 return afb_xreq_unsubscribe(xreq, eventid);
486 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
489 return afb_evt_eventid_remove_watch(xreq->listener, eventid);
490 if (xreq->queryitf->unsubscribe)
491 return xreq->queryitf->unsubscribe(xreq, eventid);
492 ERROR("no event listener, unsubscription impossible");
497 static void xreq_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
499 struct afb_xreq *xreq = from_request(closure);
502 sc = subcall_alloc(xreq, api, verb, args);
505 callback(cb_closure, 1, afb_msg_json_internal_error());
506 json_object_put(args);
508 subcall(sc, callback, cb_closure);
512 static void xreq_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
514 struct afb_xreq *xreq = from_request(closure);
517 sc = subcall_alloc(xreq, api, verb, args);
520 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
521 json_object_put(args);
523 subcall_req(sc, callback, cb_closure);
527 static void xreq_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
529 struct afb_xreq *xreq = from_request(closure);
532 sc = subcall_alloc(xreq, api, verb, args);
535 callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq));
536 json_object_put(args);
538 subcall_request(sc, callback, cb_closure);
543 static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
547 struct afb_xreq *xreq = from_request(closure);
548 struct json_object *resu;
550 sc = subcall_alloc(xreq, api, verb, args);
553 resu = afb_msg_json_internal_error();
554 json_object_put(args);
556 rc = subcallsync(sc, &resu);
561 json_object_put(resu);
565 static void xreq_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
568 struct afb_xreq *xreq = from_request(closure);
570 if (!fmt || vasprintf(&p, fmt, args) < 0)
571 vverbose(level, file, line, func, fmt, args);
573 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.api, p);
578 static struct afb_stored_req *xreq_store_cb(struct afb_request *closure)
580 xreq_addref_cb(closure);
581 return (struct afb_stored_req*)closure;
584 static int xreq_has_permission_cb(struct afb_request *closure, const char *permission)
586 struct afb_xreq *xreq = from_request(closure);
587 return afb_auth_has_permission(xreq, permission);
590 static char *xreq_get_application_id_cb(struct afb_request *closure)
592 struct afb_xreq *xreq = from_request(closure);
593 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
596 static void *xreq_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
598 struct afb_xreq *xreq = from_request(closure);
599 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
602 static int xreq_get_uid_cb(struct afb_request *closure)
604 struct afb_xreq *xreq = from_request(closure);
605 return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
608 /******************************************************************************/
610 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
612 struct json_object *r = xreq_json_cb(closure);
613 struct afb_xreq *xreq = from_request(closure);
614 return afb_hook_xreq_json(xreq, r);
617 static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name)
619 struct afb_arg r = xreq_get_cb(closure, name);
620 struct afb_xreq *xreq = from_request(closure);
621 return afb_hook_xreq_get(xreq, name, r);
624 static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
626 struct afb_xreq *xreq = from_request(closure);
627 afb_hook_xreq_success(xreq, obj, info);
628 xreq_success_cb(closure, obj, info);
631 static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info)
633 struct afb_xreq *xreq = from_request(closure);
634 afb_hook_xreq_fail(xreq, status, info);
635 xreq_fail_cb(closure, status, info);
638 static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
640 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
643 static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
645 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
648 static void *xreq_hooked_context_get_cb(struct afb_request *closure)
650 void *r = xreq_context_get_cb(closure);
651 struct afb_xreq *xreq = from_request(closure);
652 return afb_hook_xreq_context_get(xreq, r);
655 static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
657 struct afb_xreq *xreq = from_request(closure);
658 afb_hook_xreq_context_set(xreq, value, free_value);
659 xreq_context_set_cb(closure, value, free_value);
662 static struct afb_request *xreq_hooked_addref_cb(struct afb_request *closure)
664 struct afb_xreq *xreq = from_request(closure);
665 afb_hook_xreq_addref(xreq);
666 return xreq_addref_cb(closure);
669 static void xreq_hooked_unref_cb(struct afb_request *closure)
671 struct afb_xreq *xreq = from_request(closure);
672 afb_hook_xreq_unref(xreq);
673 xreq_unref_cb(closure);
676 static void xreq_hooked_session_close_cb(struct afb_request *closure)
678 struct afb_xreq *xreq = from_request(closure);
679 afb_hook_xreq_session_close(xreq);
680 xreq_session_close_cb(closure);
683 static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level)
685 int r = xreq_session_set_LOA_cb(closure, level);
686 struct afb_xreq *xreq = from_request(closure);
687 return afb_hook_xreq_session_set_LOA(xreq, level, r);
690 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
691 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
693 return xreq_hooked_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
696 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
698 int r = xreq_subscribe_eventid_cb(closure, eventid);
699 struct afb_xreq *xreq = from_request(closure);
700 return afb_hook_xreq_subscribe(xreq, eventid, r);
703 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
704 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
706 return xreq_hooked_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
709 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
711 int r = xreq_unsubscribe_eventid_cb(closure, eventid);
712 struct afb_xreq *xreq = from_request(closure);
713 return afb_hook_xreq_unsubscribe(xreq, eventid, r);
716 static void xreq_hooked_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
718 struct afb_xreq *xreq = from_request(closure);
721 afb_hook_xreq_subcall(xreq, api, verb, args);
722 sc = subcall_alloc(xreq, api, verb, args);
725 callback(cb_closure, 1, afb_msg_json_internal_error());
726 json_object_put(args);
728 subcall_hooked(sc, callback, cb_closure);
732 static void xreq_hooked_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
734 struct afb_xreq *xreq = from_request(closure);
737 afb_hook_xreq_subcall_req(xreq, api, verb, args);
738 sc = subcall_alloc(xreq, api, verb, args);
741 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
742 json_object_put(args);
744 subcall_req_hooked(sc, callback, cb_closure);
748 static void xreq_hooked_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
750 struct afb_xreq *xreq = from_request(closure);
753 afb_hook_xreq_subcall(xreq, api, verb, args);
754 sc = subcall_alloc(xreq, api, verb, args);
757 callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq));
758 json_object_put(args);
760 subcall_request_hooked(sc, callback, cb_closure);
764 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
767 struct afb_xreq *xreq = from_request(closure);
768 afb_hook_xreq_subcallsync(xreq, api, verb, args);
769 r = xreq_subcallsync_cb(closure, api, verb, args, result);
770 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
773 static void xreq_hooked_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
775 struct afb_xreq *xreq = from_request(closure);
778 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
779 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
783 static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure)
785 struct afb_xreq *xreq = from_request(closure);
786 struct afb_stored_req *r = xreq_store_cb(closure);
787 afb_hook_xreq_store(xreq, r);
791 static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission)
793 struct afb_xreq *xreq = from_request(closure);
794 int r = xreq_has_permission_cb(closure, permission);
795 return afb_hook_xreq_has_permission(xreq, permission, r);
798 static char *xreq_hooked_get_application_id_cb(struct afb_request *closure)
800 struct afb_xreq *xreq = from_request(closure);
801 char *r = xreq_get_application_id_cb(closure);
802 return afb_hook_xreq_get_application_id(xreq, r);
805 static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
807 struct afb_xreq *xreq = from_request(closure);
808 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
809 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
812 static int xreq_hooked_get_uid_cb(struct afb_request *closure)
814 struct afb_xreq *xreq = from_request(closure);
815 int r = xreq_get_uid_cb(closure);
816 return afb_hook_xreq_get_uid(xreq, r);
819 /******************************************************************************/
821 const struct afb_request_itf xreq_itf = {
822 .json = xreq_json_cb,
824 .success = xreq_success_cb,
825 .fail = xreq_fail_cb,
826 .vsuccess = xreq_vsuccess_cb,
827 .vfail = xreq_vfail_cb,
828 .context_get = xreq_context_get_cb,
829 .context_set = xreq_context_set_cb,
830 .addref = xreq_addref_cb,
831 .unref = xreq_unref_cb,
832 .session_close = xreq_session_close_cb,
833 .session_set_LOA = xreq_session_set_LOA_cb,
834 .subscribe = xreq_subscribe_cb,
835 .unsubscribe = xreq_unsubscribe_cb,
836 .subcall = xreq_subcall_cb,
837 .subcallsync = xreq_subcallsync_cb,
838 .vverbose = xreq_vverbose_cb,
839 .store = xreq_store_cb,
840 .subcall_req = xreq_subcall_req_cb,
841 .has_permission = xreq_has_permission_cb,
842 .get_application_id = xreq_get_application_id_cb,
843 .context_make = xreq_context_make_cb,
844 .subscribe_eventid = xreq_subscribe_eventid_cb,
845 .unsubscribe_eventid = xreq_unsubscribe_eventid_cb,
846 .subcall_request = xreq_subcall_request_cb,
847 .get_uid = xreq_get_uid_cb,
850 const struct afb_request_itf xreq_hooked_itf = {
851 .json = xreq_hooked_json_cb,
852 .get = xreq_hooked_get_cb,
853 .success = xreq_hooked_success_cb,
854 .fail = xreq_hooked_fail_cb,
855 .vsuccess = xreq_hooked_vsuccess_cb,
856 .vfail = xreq_hooked_vfail_cb,
857 .context_get = xreq_hooked_context_get_cb,
858 .context_set = xreq_hooked_context_set_cb,
859 .addref = xreq_hooked_addref_cb,
860 .unref = xreq_hooked_unref_cb,
861 .session_close = xreq_hooked_session_close_cb,
862 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
863 .subscribe = xreq_hooked_subscribe_cb,
864 .unsubscribe = xreq_hooked_unsubscribe_cb,
865 .subcall = xreq_hooked_subcall_cb,
866 .subcallsync = xreq_hooked_subcallsync_cb,
867 .vverbose = xreq_hooked_vverbose_cb,
868 .store = xreq_hooked_store_cb,
869 .subcall_req = xreq_hooked_subcall_req_cb,
870 .has_permission = xreq_hooked_has_permission_cb,
871 .get_application_id = xreq_hooked_get_application_id_cb,
872 .context_make = xreq_hooked_context_make_cb,
873 .subscribe_eventid = xreq_hooked_subscribe_eventid_cb,
874 .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb,
875 .subcall_request = xreq_hooked_subcall_request_cb,
876 .get_uid = xreq_hooked_get_uid_cb,
879 /******************************************************************************/
881 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
883 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
885 afb_hook_xreq_unstore(xreq);
889 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
891 return afb_request_json(to_request(xreq));
894 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
896 afb_request_success(to_request(xreq), obj, info);
899 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
903 va_start(args, info);
904 afb_request_success_v(to_request(xreq), obj, info, args);
908 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
910 afb_request_fail(to_request(xreq), status, info);
913 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
917 va_start(args, info);
918 afb_request_fail_v(to_request(xreq), status, info, args);
923 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
925 struct json_object *obj = xreq_json_cb(to_request(xreq));
926 const char *result = json_object_to_json_string(obj);
928 *size = strlen(result);
932 void afb_xreq_addref(struct afb_xreq *xreq)
934 afb_request_addref(to_request(xreq));
937 void afb_xreq_unref(struct afb_xreq *xreq)
939 afb_request_unref(to_request(xreq));
942 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
944 xreq_subcall_request_cb(to_request(xreq), api, verb, args, callback, cb_closure);
947 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
949 afb_request_subcall(to_request(xreq), api, verb, args, callback, cb_closure);
952 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
954 return xreq_subcallsync_cb(to_request(xreq), api, verb, args, result);
957 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
959 return afb_request_subcall_sync(to_request(xreq), api, verb, args, result);
962 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
966 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
967 if (!afb_context_check(&xreq->context)) {
968 afb_context_close(&xreq->context);
969 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
975 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
976 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
977 if (!afb_context_check_loa(&xreq->context, loa)) {
978 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
984 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
985 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
986 if (afb_context_check_loa(&xreq->context, loa + 1)) {
987 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
993 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
994 afb_context_refresh(&xreq->context);
996 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
997 afb_context_change_loa(&xreq->context, 0);
998 afb_context_close(&xreq->context);
1004 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
1008 if (sessionflags != 0) {
1009 if (!afb_context_check(&xreq->context)) {
1010 afb_context_close(&xreq->context);
1011 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
1017 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
1018 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
1019 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
1024 if (auth && !afb_auth_check(xreq, auth)) {
1025 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
1030 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
1031 afb_context_refresh(&xreq->context);
1033 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
1034 afb_context_close(&xreq->context);
1040 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
1043 afb_xreq_fail_unknown_verb(xreq);
1045 if (!xreq_session_check_apply_v1(xreq, verb->session))
1046 verb->callback(to_req(xreq));
1049 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
1052 afb_xreq_fail_unknown_verb(xreq);
1054 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
1055 verb->callback(to_req(xreq));
1058 void afb_xreq_call_verb_vdyn(struct afb_xreq *xreq, const struct afb_api_dyn_verb *verb)
1061 afb_xreq_fail_unknown_verb(xreq);
1063 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
1064 verb->callback(to_request(xreq));
1067 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
1069 memset(xreq, 0, sizeof *xreq);
1070 xreq->request.itf = &xreq_hooked_itf; /* hook by default */
1072 xreq->queryitf = queryitf;
1075 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
1077 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1080 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
1082 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api);
1085 static void init_hooking(struct afb_xreq *xreq)
1087 afb_hook_init_xreq(xreq);
1088 if (xreq->hookflags)
1089 afb_hook_xreq_begin(xreq);
1091 xreq->request.itf = &xreq_itf; /* unhook the interface */
1095 * job callback for asynchronous and secured processing of the request.
1097 static void process_async(int signum, void *arg)
1099 struct afb_xreq *xreq = arg;
1100 const struct afb_api *api;
1103 /* emit the error (assumes that hooking is initialised) */
1104 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
1108 /* invoke api call method to process the reqiest */
1109 api = (const struct afb_api*)xreq->context.api_key;
1110 api->itf->call(api->closure, xreq);
1112 /* release the request */
1113 afb_xreq_unhooked_unref(xreq);
1117 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
1118 * The early failure occurs only in function 'afb_xreq_process' where normally,
1119 * the hooking is not initialised. So this "early" failure takes care to initialise
1120 * the hooking in first.
1122 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1130 va_start(args, info);
1131 afb_request_fail_v(to_request(xreq), status, info, args);
1136 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1137 * Errors are reported as request failures.
1139 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1141 const struct afb_api *api;
1142 struct afb_xreq *caller;
1144 /* lookup at the api */
1145 xreq->apiset = apiset;
1146 api = afb_apiset_lookup_started(apiset, xreq->request.api, 1);
1148 if (errno == ENOENT)
1149 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1151 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api);
1154 xreq->context.api_key = api;
1156 /* check self locking */
1158 caller = xreq->caller;
1160 if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1161 /* noconcurrency lock detected */
1162 ERROR("self-lock detected in call stack for API %s", xreq->request.api);
1163 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api);
1166 caller = caller->caller;
1170 /* queue the request job */
1171 afb_xreq_unhooked_addref(xreq);
1172 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1173 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1174 ERROR("can't process job with threads: %m");
1175 early_failure(xreq, "cancelled", "not able to create a job for the task");
1176 afb_xreq_unhooked_unref(xreq);
1179 afb_xreq_unhooked_unref(xreq);