2 * Copyright (C) 2017, 2018 "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.
26 #include <json-c/json.h>
27 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
28 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
31 #include <afb/afb-binding-v1.h>
32 #include <afb/afb-binding-v2.h>
33 #include <afb/afb-binding-v3.h>
34 #include <afb/afb-req-x2.h>
37 #include "afb-apiset.h"
39 #include "afb-calls.h"
40 #include "afb-context.h"
44 #include "afb-msg-json.h"
50 /******************************************************************************/
52 static void xreq_finalize(struct afb_xreq *xreq)
55 afb_xreq_reply(xreq, NULL, "error", "no reply");
57 afb_hook_xreq_end(xreq);
59 afb_xreq_unhooked_unref(xreq->caller);
60 xreq->queryitf->unref(xreq);
63 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
65 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
68 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
70 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
74 /******************************************************************************/
76 struct json_object *afb_xreq_unhooked_json(struct afb_xreq *xreq)
78 if (!xreq->json && xreq->queryitf->json)
79 xreq->json = xreq->queryitf->json(xreq);
83 static struct json_object *xreq_json_cb(struct afb_req_x2 *closure)
85 struct afb_xreq *xreq = xreq_from_req_x2(closure);
86 return afb_xreq_unhooked_json(xreq);
89 static struct afb_arg xreq_get_cb(struct afb_req_x2 *closure, const char *name)
91 struct afb_xreq *xreq = xreq_from_req_x2(closure);
93 struct json_object *object, *value;
95 if (xreq->queryitf->get)
96 arg = xreq->queryitf->get(xreq, name);
98 object = xreq_json_cb(closure);
99 if (json_object_object_get_ex(object, name, &value)) {
101 arg.value = json_object_get_string(value);
111 static void xreq_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
113 struct afb_xreq *xreq = xreq_from_req_x2(closure);
116 ERROR("reply called more than one time!!");
117 json_object_put(obj);
120 xreq->queryitf->reply(xreq, obj, error, info);
124 static void xreq_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
127 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
129 xreq_reply_cb(closure, obj, error, info);
133 static void xreq_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
135 xreq_reply_cb(closure, obj, NULL, info);
138 static void xreq_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
140 xreq_reply_cb(closure, NULL, status, info);
143 static void xreq_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
145 xreq_vreply_cb(closure, obj, NULL, fmt, args);
148 static void xreq_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
150 xreq_vreply_cb(closure, NULL, status, fmt, args);
153 static void *xreq_legacy_context_get_cb(struct afb_req_x2 *closure)
155 struct afb_xreq *xreq = xreq_from_req_x2(closure);
156 return afb_context_get(&xreq->context);
159 static void xreq_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
161 struct afb_xreq *xreq = xreq_from_req_x2(closure);
162 afb_context_set(&xreq->context, value, free_value);
165 static struct afb_req_x2 *xreq_addref_cb(struct afb_req_x2 *closure)
167 struct afb_xreq *xreq = xreq_from_req_x2(closure);
168 afb_xreq_unhooked_addref(xreq);
172 static void xreq_unref_cb(struct afb_req_x2 *closure)
174 struct afb_xreq *xreq = xreq_from_req_x2(closure);
175 afb_xreq_unhooked_unref(xreq);
178 static void xreq_session_close_cb(struct afb_req_x2 *closure)
180 struct afb_xreq *xreq = xreq_from_req_x2(closure);
181 afb_context_close(&xreq->context);
184 static int xreq_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
186 struct afb_xreq *xreq = xreq_from_req_x2(closure);
187 return afb_context_change_loa(&xreq->context, level);
190 static int xreq_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
192 struct afb_xreq *xreq = xreq_from_req_x2(closure);
193 return afb_xreq_subscribe(xreq, event);
196 static int xreq_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
198 return xreq_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
201 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
204 ERROR("request replied, subscription impossible");
208 return afb_evt_event_x2_add_watch(xreq->listener, event);
209 if (xreq->queryitf->subscribe)
210 return xreq->queryitf->subscribe(xreq, event);
211 ERROR("no event listener, subscription impossible");
217 static int xreq_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
219 struct afb_xreq *xreq = xreq_from_req_x2(closure);
220 return afb_xreq_unsubscribe(xreq, event);
223 static int xreq_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
225 return xreq_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
228 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
231 ERROR("request replied, unsubscription impossible");
235 return afb_evt_event_x2_remove_watch(xreq->listener, event);
236 if (xreq->queryitf->unsubscribe)
237 return xreq->queryitf->unsubscribe(xreq, event);
238 ERROR("no event listener, unsubscription impossible");
244 static void xreq_legacy_subcall_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
246 struct afb_xreq *xreq = xreq_from_req_x2(req);
247 return afb_calls_legacy_subcall_v1(xreq, api, verb, args, callback, closure);
250 static void xreq_legacy_subcall_req_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x1), void *closure)
252 struct afb_xreq *xreq = xreq_from_req_x2(req);
253 return afb_calls_legacy_subcall_v2(xreq, api, verb, args, callback, closure);
256 static void xreq_legacy_subcall_request_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2*), void *closure)
258 struct afb_xreq *xreq = xreq_from_req_x2(req);
259 return afb_calls_legacy_subcall_v3(xreq, api, verb, args, callback, closure);
263 static int xreq_legacy_subcallsync_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
265 struct afb_xreq *xreq = xreq_from_req_x2(req);
266 return afb_calls_legacy_subcall_sync(xreq, api, verb, args, result);
269 static void xreq_vverbose_cb(struct afb_req_x2 *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
272 struct afb_xreq *xreq = xreq_from_req_x2(closure);
274 if (!fmt || vasprintf(&p, fmt, args) < 0)
275 vverbose(level, file, line, func, fmt, args);
277 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.called_api, p);
282 static struct afb_stored_req *xreq_legacy_store_cb(struct afb_req_x2 *closure)
284 xreq_addref_cb(closure);
285 return (struct afb_stored_req*)closure;
288 static int xreq_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
290 struct afb_xreq *xreq = xreq_from_req_x2(closure);
291 return afb_auth_has_permission(xreq, permission);
294 static char *xreq_get_application_id_cb(struct afb_req_x2 *closure)
296 struct afb_xreq *xreq = xreq_from_req_x2(closure);
297 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
300 static void *xreq_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
302 struct afb_xreq *xreq = xreq_from_req_x2(closure);
303 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
306 static int xreq_get_uid_cb(struct afb_req_x2 *closure)
308 struct afb_xreq *xreq = xreq_from_req_x2(closure);
309 return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
312 static struct json_object *xreq_get_client_info_cb(struct afb_req_x2 *closure)
314 struct afb_xreq *xreq = xreq_from_req_x2(closure);
315 struct json_object *r = json_object_new_object();
316 if (xreq->cred && xreq->cred->id) {
317 json_object_object_add(r, "uid", json_object_new_int(xreq->cred->uid));
318 json_object_object_add(r, "gid", json_object_new_int(xreq->cred->gid));
319 json_object_object_add(r, "pid", json_object_new_int(xreq->cred->pid));
320 json_object_object_add(r, "user", json_object_new_string(xreq->cred->user));
321 json_object_object_add(r, "label", json_object_new_string(xreq->cred->label));
322 json_object_object_add(r, "id", json_object_new_string(xreq->cred->id));
324 if (xreq->context.session) {
325 json_object_object_add(r, "uuid", json_object_new_string(afb_context_uuid(&xreq->context)));
326 json_object_object_add(r, "LOA", json_object_new_int(afb_context_get_loa(&xreq->context)));
331 static void xreq_subcall_cb(
332 struct afb_req_x2 *req,
335 struct json_object *args,
337 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
340 struct afb_xreq *xreq = xreq_from_req_x2(req);
341 afb_calls_subcall(xreq, api, verb, args, flags, callback, closure);
344 static int xreq_subcallsync_cb(
345 struct afb_req_x2 *req,
348 struct json_object *args,
350 struct json_object **object,
354 struct afb_xreq *xreq = xreq_from_req_x2(req);
355 return afb_calls_subcall_sync(xreq, api, verb, args, flags, object, error, info);
358 /******************************************************************************/
360 static struct json_object *xreq_hooked_json_cb(struct afb_req_x2 *closure)
362 struct json_object *r = xreq_json_cb(closure);
363 struct afb_xreq *xreq = xreq_from_req_x2(closure);
364 return afb_hook_xreq_json(xreq, r);
367 static struct afb_arg xreq_hooked_get_cb(struct afb_req_x2 *closure, const char *name)
369 struct afb_arg r = xreq_get_cb(closure, name);
370 struct afb_xreq *xreq = xreq_from_req_x2(closure);
371 return afb_hook_xreq_get(xreq, name, r);
374 static void xreq_hooked_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
376 struct afb_xreq *xreq = xreq_from_req_x2(closure);
377 afb_hook_xreq_reply(xreq, obj, error, info);
378 xreq_reply_cb(closure, obj, error, info);
381 static void xreq_hooked_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
384 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
386 xreq_hooked_reply_cb(closure, obj, error, info);
390 static void xreq_hooked_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
392 xreq_hooked_reply_cb(closure, obj, NULL, info);
395 static void xreq_hooked_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
397 xreq_hooked_reply_cb(closure, NULL, status, info);
400 static void xreq_hooked_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
402 xreq_hooked_vreply_cb(closure, obj, NULL, fmt, args);
405 static void xreq_hooked_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
407 xreq_hooked_vreply_cb(closure, NULL, status, fmt, args);
410 static void *xreq_hooked_legacy_context_get_cb(struct afb_req_x2 *closure)
412 void *r = xreq_legacy_context_get_cb(closure);
413 struct afb_xreq *xreq = xreq_from_req_x2(closure);
414 return afb_hook_xreq_legacy_context_get(xreq, r);
417 static void xreq_hooked_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
419 struct afb_xreq *xreq = xreq_from_req_x2(closure);
420 afb_hook_xreq_legacy_context_set(xreq, value, free_value);
421 xreq_legacy_context_set_cb(closure, value, free_value);
424 static struct afb_req_x2 *xreq_hooked_addref_cb(struct afb_req_x2 *closure)
426 struct afb_xreq *xreq = xreq_from_req_x2(closure);
427 afb_hook_xreq_addref(xreq);
428 return xreq_addref_cb(closure);
431 static void xreq_hooked_unref_cb(struct afb_req_x2 *closure)
433 struct afb_xreq *xreq = xreq_from_req_x2(closure);
434 afb_hook_xreq_unref(xreq);
435 xreq_unref_cb(closure);
438 static void xreq_hooked_session_close_cb(struct afb_req_x2 *closure)
440 struct afb_xreq *xreq = xreq_from_req_x2(closure);
441 afb_hook_xreq_session_close(xreq);
442 xreq_session_close_cb(closure);
445 static int xreq_hooked_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
447 int r = xreq_session_set_LOA_cb(closure, level);
448 struct afb_xreq *xreq = xreq_from_req_x2(closure);
449 return afb_hook_xreq_session_set_LOA(xreq, level, r);
452 static int xreq_hooked_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
454 int r = xreq_subscribe_event_x2_cb(closure, event);
455 struct afb_xreq *xreq = xreq_from_req_x2(closure);
456 return afb_hook_xreq_subscribe(xreq, event, r);
459 static int xreq_hooked_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
461 return xreq_hooked_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
464 static int xreq_hooked_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
466 int r = xreq_unsubscribe_event_x2_cb(closure, event);
467 struct afb_xreq *xreq = xreq_from_req_x2(closure);
468 return afb_hook_xreq_unsubscribe(xreq, event, r);
471 static int xreq_hooked_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
473 return xreq_hooked_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
476 static void xreq_hooked_legacy_subcall_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
478 struct afb_xreq *xreq = xreq_from_req_x2(req);
479 afb_calls_legacy_hooked_subcall_v1(xreq, api, verb, args, callback, closure);
482 static void xreq_hooked_legacy_subcall_req_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x1), void *closure)
484 struct afb_xreq *xreq = xreq_from_req_x2(req);
485 afb_calls_legacy_hooked_subcall_v2(xreq, api, verb, args, callback, closure);
488 static void xreq_hooked_legacy_subcall_request_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *closure)
490 struct afb_xreq *xreq = xreq_from_req_x2(req);
491 afb_calls_legacy_hooked_subcall_v3(xreq, api, verb, args, callback, closure);
494 static int xreq_hooked_legacy_subcallsync_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
496 struct afb_xreq *xreq = xreq_from_req_x2(req);
497 return afb_calls_legacy_hooked_subcall_sync(xreq, api, verb, args, result);
500 static void xreq_hooked_vverbose_cb(struct afb_req_x2 *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
502 struct afb_xreq *xreq = xreq_from_req_x2(closure);
505 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
506 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
510 static struct afb_stored_req *xreq_hooked_legacy_store_cb(struct afb_req_x2 *closure)
512 struct afb_xreq *xreq = xreq_from_req_x2(closure);
513 struct afb_stored_req *r = xreq_legacy_store_cb(closure);
514 afb_hook_xreq_legacy_store(xreq, r);
518 static int xreq_hooked_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
520 struct afb_xreq *xreq = xreq_from_req_x2(closure);
521 int r = xreq_has_permission_cb(closure, permission);
522 return afb_hook_xreq_has_permission(xreq, permission, r);
525 static char *xreq_hooked_get_application_id_cb(struct afb_req_x2 *closure)
527 struct afb_xreq *xreq = xreq_from_req_x2(closure);
528 char *r = xreq_get_application_id_cb(closure);
529 return afb_hook_xreq_get_application_id(xreq, r);
532 static void *xreq_hooked_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
534 struct afb_xreq *xreq = xreq_from_req_x2(closure);
535 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
536 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
539 static int xreq_hooked_get_uid_cb(struct afb_req_x2 *closure)
541 struct afb_xreq *xreq = xreq_from_req_x2(closure);
542 int r = xreq_get_uid_cb(closure);
543 return afb_hook_xreq_get_uid(xreq, r);
546 static struct json_object *xreq_hooked_get_client_info_cb(struct afb_req_x2 *closure)
548 struct afb_xreq *xreq = xreq_from_req_x2(closure);
549 struct json_object *r = xreq_get_client_info_cb(closure);
550 return afb_hook_xreq_get_client_info(xreq, r);
553 static void xreq_hooked_subcall_cb(
554 struct afb_req_x2 *req,
557 struct json_object *args,
559 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
562 struct afb_xreq *xreq = xreq_from_req_x2(req);
563 afb_calls_hooked_subcall(xreq, api, verb, args, flags, callback, closure);
566 static int xreq_hooked_subcallsync_cb(
567 struct afb_req_x2 *req,
570 struct json_object *args,
572 struct json_object **object,
576 struct afb_xreq *xreq = xreq_from_req_x2(req);
577 return afb_calls_hooked_subcall_sync(xreq, api, verb, args, flags, object, error, info);
580 /******************************************************************************/
582 const struct afb_req_x2_itf xreq_itf = {
583 .json = xreq_json_cb,
585 .legacy_success = xreq_legacy_success_cb,
586 .legacy_fail = xreq_legacy_fail_cb,
587 .legacy_vsuccess = xreq_legacy_vsuccess_cb,
588 .legacy_vfail = xreq_legacy_vfail_cb,
589 .legacy_context_get = xreq_legacy_context_get_cb,
590 .legacy_context_set = xreq_legacy_context_set_cb,
591 .addref = xreq_addref_cb,
592 .unref = xreq_unref_cb,
593 .session_close = xreq_session_close_cb,
594 .session_set_LOA = xreq_session_set_LOA_cb,
595 .legacy_subscribe_event_x1 = xreq_legacy_subscribe_event_x1_cb,
596 .legacy_unsubscribe_event_x1 = xreq_legacy_unsubscribe_event_x1_cb,
597 .legacy_subcall = xreq_legacy_subcall_cb,
598 .legacy_subcallsync = xreq_legacy_subcallsync_cb,
599 .vverbose = xreq_vverbose_cb,
600 .legacy_store_req = xreq_legacy_store_cb,
601 .legacy_subcall_req = xreq_legacy_subcall_req_cb,
602 .has_permission = xreq_has_permission_cb,
603 .get_application_id = xreq_get_application_id_cb,
604 .context_make = xreq_context_make_cb,
605 .subscribe_event_x2 = xreq_subscribe_event_x2_cb,
606 .unsubscribe_event_x2 = xreq_unsubscribe_event_x2_cb,
607 .legacy_subcall_request = xreq_legacy_subcall_request_cb,
608 .get_uid = xreq_get_uid_cb,
609 .reply = xreq_reply_cb,
610 .vreply = xreq_vreply_cb,
611 .get_client_info = xreq_get_client_info_cb,
612 .subcall = xreq_subcall_cb,
613 .subcallsync = xreq_subcallsync_cb,
616 const struct afb_req_x2_itf xreq_hooked_itf = {
617 .json = xreq_hooked_json_cb,
618 .get = xreq_hooked_get_cb,
619 .legacy_success = xreq_hooked_legacy_success_cb,
620 .legacy_fail = xreq_hooked_legacy_fail_cb,
621 .legacy_vsuccess = xreq_hooked_legacy_vsuccess_cb,
622 .legacy_vfail = xreq_hooked_legacy_vfail_cb,
623 .legacy_context_get = xreq_hooked_legacy_context_get_cb,
624 .legacy_context_set = xreq_hooked_legacy_context_set_cb,
625 .addref = xreq_hooked_addref_cb,
626 .unref = xreq_hooked_unref_cb,
627 .session_close = xreq_hooked_session_close_cb,
628 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
629 .legacy_subscribe_event_x1 = xreq_hooked_legacy_subscribe_event_x1_cb,
630 .legacy_unsubscribe_event_x1 = xreq_hooked_legacy_unsubscribe_event_x1_cb,
631 .legacy_subcall = xreq_hooked_legacy_subcall_cb,
632 .legacy_subcallsync = xreq_hooked_legacy_subcallsync_cb,
633 .vverbose = xreq_hooked_vverbose_cb,
634 .legacy_store_req = xreq_hooked_legacy_store_cb,
635 .legacy_subcall_req = xreq_hooked_legacy_subcall_req_cb,
636 .has_permission = xreq_hooked_has_permission_cb,
637 .get_application_id = xreq_hooked_get_application_id_cb,
638 .context_make = xreq_hooked_context_make_cb,
639 .subscribe_event_x2 = xreq_hooked_subscribe_event_x2_cb,
640 .unsubscribe_event_x2 = xreq_hooked_unsubscribe_event_x2_cb,
641 .legacy_subcall_request = xreq_hooked_legacy_subcall_request_cb,
642 .get_uid = xreq_hooked_get_uid_cb,
643 .reply = xreq_hooked_reply_cb,
644 .vreply = xreq_hooked_vreply_cb,
645 .get_client_info = xreq_hooked_get_client_info_cb,
646 .subcall = xreq_hooked_subcall_cb,
647 .subcallsync = xreq_hooked_subcallsync_cb,
650 /******************************************************************************/
652 struct afb_req_x1 afb_xreq_unstore(struct afb_stored_req *sreq)
654 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
656 afb_hook_xreq_legacy_unstore(xreq);
657 return xreq_to_req_x1(xreq);
660 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
662 return afb_req_x2_json(xreq_to_req_x2(xreq));
665 void afb_xreq_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
667 afb_req_x2_reply(xreq_to_req_x2(xreq), obj, error, info);
670 void afb_xreq_reply_f(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info, ...)
674 va_start(args, info);
675 afb_req_x2_reply_v(xreq_to_req_x2(xreq), obj, error, info, args);
679 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
681 struct json_object *obj = xreq_json_cb(xreq_to_req_x2(xreq));
682 const char *result = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE);
684 *size = strlen(result);
688 void afb_xreq_addref(struct afb_xreq *xreq)
690 afb_req_x2_addref(xreq_to_req_x2(xreq));
693 void afb_xreq_unref(struct afb_xreq *xreq)
695 afb_req_x2_unref(xreq_to_req_x2(xreq));
698 void afb_xreq_unhooked_legacy_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *cb_closure)
700 xreq_legacy_subcall_request_cb(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
703 void afb_xreq_legacy_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *cb_closure)
705 afb_req_x2_subcall_legacy(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
708 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags, void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_req_x2 *), void *closure)
710 xreq_subcall_cb(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
713 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags, void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_req_x2 *), void *closure)
715 afb_req_x2_subcall(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
718 int afb_xreq_unhooked_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
720 return xreq_legacy_subcallsync_cb(xreq_to_req_x2(xreq), api, verb, args, result);
723 int afb_xreq_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
725 return afb_req_x2_subcall_sync_legacy(xreq_to_req_x2(xreq), api, verb, args, result);
728 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
732 if ((sessionflags & (AFB_SESSION_CLOSE_X1|AFB_SESSION_RENEW_X1|AFB_SESSION_CHECK_X1|AFB_SESSION_LOA_EQ_X1)) != 0) {
733 if (!afb_context_check(&xreq->context)) {
734 afb_context_close(&xreq->context);
735 afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
741 if ((sessionflags & AFB_SESSION_LOA_GE_X1) != 0) {
742 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
743 if (!afb_context_check_loa(&xreq->context, loa)) {
744 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
750 if ((sessionflags & AFB_SESSION_LOA_LE_X1) != 0) {
751 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
752 if (afb_context_check_loa(&xreq->context, loa + 1)) {
753 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
759 if ((sessionflags & AFB_SESSION_RENEW_X1) != 0) {
760 afb_context_refresh(&xreq->context);
762 if ((sessionflags & AFB_SESSION_CLOSE_X1) != 0) {
763 afb_context_change_loa(&xreq->context, 0);
764 afb_context_close(&xreq->context);
770 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
774 if (sessionflags != 0) {
775 if (!afb_context_check(&xreq->context)) {
776 afb_context_close(&xreq->context);
777 afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
783 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_X2);
784 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
785 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
790 if (auth && !afb_auth_check(xreq, auth)) {
791 afb_xreq_reply_f(xreq, NULL, "denied", "authorisation refused");
796 if ((sessionflags & AFB_SESSION_REFRESH_X2) != 0) {
797 afb_context_refresh(&xreq->context);
799 if ((sessionflags & AFB_SESSION_CLOSE_X2) != 0) {
800 afb_context_close(&xreq->context);
806 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
809 afb_xreq_reply_unknown_verb(xreq);
811 if (!xreq_session_check_apply_v1(xreq, verb->session))
812 verb->callback(xreq_to_req_x1(xreq));
815 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
818 afb_xreq_reply_unknown_verb(xreq);
820 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
821 verb->callback(xreq_to_req_x1(xreq));
824 void afb_xreq_call_verb_v3(struct afb_xreq *xreq, const struct afb_verb_v3 *verb)
827 afb_xreq_reply_unknown_verb(xreq);
829 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
830 verb->callback(xreq_to_req_x2(xreq));
833 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
835 memset(xreq, 0, sizeof *xreq);
836 xreq->request.itf = &xreq_itf; /* no hook by default */
838 xreq->queryitf = queryitf;
841 void afb_xreq_reply_unknown_api(struct afb_xreq *xreq)
843 afb_xreq_reply_f(xreq, NULL, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
846 void afb_xreq_reply_unknown_verb(struct afb_xreq *xreq)
848 afb_xreq_reply_f(xreq, NULL, "unknown-verb", "verb %s unknown within api %s", xreq->request.called_verb, xreq->request.called_api);
851 static void init_hooking(struct afb_xreq *xreq)
853 afb_hook_init_xreq(xreq);
854 if (xreq->hookflags) {
855 xreq->request.itf = &xreq_hooked_itf; /* unhook the interface */
856 afb_hook_xreq_begin(xreq);
861 * job callback for asynchronous and secured processing of the request.
863 static void process_async(int signum, void *arg)
865 struct afb_xreq *xreq = arg;
866 const struct afb_api_item *api;
869 /* emit the error (assumes that hooking is initialised) */
870 afb_xreq_reply_f(xreq, NULL, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
874 /* invoke api call method to process the request */
875 api = (const struct afb_api_item*)xreq->context.api_key;
876 api->itf->call(api->closure, xreq);
878 /* release the request */
879 afb_xreq_unhooked_unref(xreq);
883 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
884 * The early failure occurs only in function 'afb_xreq_process' where normally,
885 * the hooking is not initialised. So this "early" failure takes care to initialise
886 * the hooking in first.
888 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
896 va_start(args, info);
897 afb_req_x2_reply_v(xreq_to_req_x2(xreq), NULL, status, info, args);
902 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
903 * Errors are reported as request failures.
905 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
907 const struct afb_api_item *api;
908 struct afb_xreq *caller;
910 /* lookup at the api */
911 xreq->apiset = apiset;
912 api = afb_apiset_lookup_started(apiset, xreq->request.called_api, 1);
915 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
917 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.called_api);
920 xreq->context.api_key = api;
922 /* check self locking */
924 caller = xreq->caller;
926 if (((const struct afb_api_item*)caller->context.api_key)->group == api->group) {
927 /* noconcurrency lock detected */
928 ERROR("self-lock detected in call stack for API %s", xreq->request.called_api);
929 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.called_api);
932 caller = caller->caller;
936 /* queue the request job */
937 afb_xreq_unhooked_addref(xreq);
938 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
939 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
940 ERROR("can't process job with threads: %m");
941 early_failure(xreq, "cancelled", "not able to create a job for the task");
942 afb_xreq_unhooked_unref(xreq);
945 afb_xreq_unhooked_unref(xreq);
948 const char *xreq_on_behalf_cred_export(struct afb_xreq *xreq)
950 return xreq->caller ? afb_cred_export(xreq->cred) : NULL;