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 return afb_evt_event_x2_add_watch(xreq->listener, event);
205 if (xreq->queryitf->subscribe)
206 return xreq->queryitf->subscribe(xreq, event);
207 ERROR("no event listener, subscription impossible");
212 static int xreq_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
214 struct afb_xreq *xreq = xreq_from_req_x2(closure);
215 return afb_xreq_unsubscribe(xreq, event);
218 static int xreq_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
220 return xreq_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
223 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
226 return afb_evt_event_x2_remove_watch(xreq->listener, event);
227 if (xreq->queryitf->unsubscribe)
228 return xreq->queryitf->unsubscribe(xreq, event);
229 ERROR("no event listener, unsubscription impossible");
234 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)
236 struct afb_xreq *xreq = xreq_from_req_x2(req);
237 return afb_calls_legacy_subcall_v1(xreq, api, verb, args, callback, closure);
240 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)
242 struct afb_xreq *xreq = xreq_from_req_x2(req);
243 return afb_calls_legacy_subcall_v2(xreq, api, verb, args, callback, closure);
246 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)
248 struct afb_xreq *xreq = xreq_from_req_x2(req);
249 return afb_calls_legacy_subcall_v3(xreq, api, verb, args, callback, closure);
253 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)
255 struct afb_xreq *xreq = xreq_from_req_x2(req);
256 return afb_calls_legacy_subcall_sync(xreq, api, verb, args, result);
259 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)
262 struct afb_xreq *xreq = xreq_from_req_x2(closure);
264 if (!fmt || vasprintf(&p, fmt, args) < 0)
265 vverbose(level, file, line, func, fmt, args);
267 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.called_api, p);
272 static struct afb_stored_req *xreq_legacy_store_cb(struct afb_req_x2 *closure)
274 xreq_addref_cb(closure);
275 return (struct afb_stored_req*)closure;
278 static int xreq_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
280 struct afb_xreq *xreq = xreq_from_req_x2(closure);
281 return afb_auth_has_permission(xreq, permission);
284 static char *xreq_get_application_id_cb(struct afb_req_x2 *closure)
286 struct afb_xreq *xreq = xreq_from_req_x2(closure);
287 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
290 static void *xreq_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
292 struct afb_xreq *xreq = xreq_from_req_x2(closure);
293 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
296 static int xreq_get_uid_cb(struct afb_req_x2 *closure)
298 struct afb_xreq *xreq = xreq_from_req_x2(closure);
299 return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
302 static struct json_object *xreq_get_client_info_cb(struct afb_req_x2 *closure)
304 struct afb_xreq *xreq = xreq_from_req_x2(closure);
305 struct json_object *r = json_object_new_object();
306 if (xreq->cred && xreq->cred->id) {
307 json_object_object_add(r, "uid", json_object_new_int(xreq->cred->uid));
308 json_object_object_add(r, "gid", json_object_new_int(xreq->cred->gid));
309 json_object_object_add(r, "pid", json_object_new_int(xreq->cred->pid));
310 json_object_object_add(r, "user", json_object_new_string(xreq->cred->user));
311 json_object_object_add(r, "label", json_object_new_string(xreq->cred->label));
312 json_object_object_add(r, "id", json_object_new_string(xreq->cred->id));
314 if (xreq->context.session) {
315 json_object_object_add(r, "uuid", json_object_new_string(afb_context_uuid(&xreq->context)));
316 json_object_object_add(r, "LOA", json_object_new_int(afb_context_get_loa(&xreq->context)));
321 static void xreq_subcall_cb(
322 struct afb_req_x2 *req,
325 struct json_object *args,
327 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
330 struct afb_xreq *xreq = xreq_from_req_x2(req);
331 afb_calls_subcall(xreq, api, verb, args, flags, callback, closure);
334 static int xreq_subcallsync_cb(
335 struct afb_req_x2 *req,
338 struct json_object *args,
340 struct json_object **object,
344 struct afb_xreq *xreq = xreq_from_req_x2(req);
345 return afb_calls_subcall_sync(xreq, api, verb, args, flags, object, error, info);
348 /******************************************************************************/
350 static struct json_object *xreq_hooked_json_cb(struct afb_req_x2 *closure)
352 struct json_object *r = xreq_json_cb(closure);
353 struct afb_xreq *xreq = xreq_from_req_x2(closure);
354 return afb_hook_xreq_json(xreq, r);
357 static struct afb_arg xreq_hooked_get_cb(struct afb_req_x2 *closure, const char *name)
359 struct afb_arg r = xreq_get_cb(closure, name);
360 struct afb_xreq *xreq = xreq_from_req_x2(closure);
361 return afb_hook_xreq_get(xreq, name, r);
364 static void xreq_hooked_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
366 struct afb_xreq *xreq = xreq_from_req_x2(closure);
367 afb_hook_xreq_reply(xreq, obj, error, info);
368 xreq_reply_cb(closure, obj, error, info);
371 static void xreq_hooked_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
374 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
376 xreq_hooked_reply_cb(closure, obj, error, info);
380 static void xreq_hooked_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
382 xreq_hooked_reply_cb(closure, obj, NULL, info);
385 static void xreq_hooked_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
387 xreq_hooked_reply_cb(closure, NULL, status, info);
390 static void xreq_hooked_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
392 xreq_hooked_vreply_cb(closure, obj, NULL, fmt, args);
395 static void xreq_hooked_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
397 xreq_hooked_vreply_cb(closure, NULL, status, fmt, args);
400 static void *xreq_hooked_legacy_context_get_cb(struct afb_req_x2 *closure)
402 void *r = xreq_legacy_context_get_cb(closure);
403 struct afb_xreq *xreq = xreq_from_req_x2(closure);
404 return afb_hook_xreq_legacy_context_get(xreq, r);
407 static void xreq_hooked_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
409 struct afb_xreq *xreq = xreq_from_req_x2(closure);
410 afb_hook_xreq_legacy_context_set(xreq, value, free_value);
411 xreq_legacy_context_set_cb(closure, value, free_value);
414 static struct afb_req_x2 *xreq_hooked_addref_cb(struct afb_req_x2 *closure)
416 struct afb_xreq *xreq = xreq_from_req_x2(closure);
417 afb_hook_xreq_addref(xreq);
418 return xreq_addref_cb(closure);
421 static void xreq_hooked_unref_cb(struct afb_req_x2 *closure)
423 struct afb_xreq *xreq = xreq_from_req_x2(closure);
424 afb_hook_xreq_unref(xreq);
425 xreq_unref_cb(closure);
428 static void xreq_hooked_session_close_cb(struct afb_req_x2 *closure)
430 struct afb_xreq *xreq = xreq_from_req_x2(closure);
431 afb_hook_xreq_session_close(xreq);
432 xreq_session_close_cb(closure);
435 static int xreq_hooked_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
437 int r = xreq_session_set_LOA_cb(closure, level);
438 struct afb_xreq *xreq = xreq_from_req_x2(closure);
439 return afb_hook_xreq_session_set_LOA(xreq, level, r);
442 static int xreq_hooked_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
444 int r = xreq_subscribe_event_x2_cb(closure, event);
445 struct afb_xreq *xreq = xreq_from_req_x2(closure);
446 return afb_hook_xreq_subscribe(xreq, event, r);
449 static int xreq_hooked_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
451 return xreq_hooked_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
454 static int xreq_hooked_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
456 int r = xreq_unsubscribe_event_x2_cb(closure, event);
457 struct afb_xreq *xreq = xreq_from_req_x2(closure);
458 return afb_hook_xreq_unsubscribe(xreq, event, r);
461 static int xreq_hooked_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
463 return xreq_hooked_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
466 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)
468 struct afb_xreq *xreq = xreq_from_req_x2(req);
469 afb_calls_legacy_hooked_subcall_v1(xreq, api, verb, args, callback, closure);
472 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)
474 struct afb_xreq *xreq = xreq_from_req_x2(req);
475 afb_calls_legacy_hooked_subcall_v2(xreq, api, verb, args, callback, closure);
478 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)
480 struct afb_xreq *xreq = xreq_from_req_x2(req);
481 afb_calls_legacy_hooked_subcall_v3(xreq, api, verb, args, callback, closure);
484 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)
486 struct afb_xreq *xreq = xreq_from_req_x2(req);
487 return afb_calls_legacy_hooked_subcall_sync(xreq, api, verb, args, result);
490 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)
492 struct afb_xreq *xreq = xreq_from_req_x2(closure);
495 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
496 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
500 static struct afb_stored_req *xreq_hooked_legacy_store_cb(struct afb_req_x2 *closure)
502 struct afb_xreq *xreq = xreq_from_req_x2(closure);
503 struct afb_stored_req *r = xreq_legacy_store_cb(closure);
504 afb_hook_xreq_legacy_store(xreq, r);
508 static int xreq_hooked_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
510 struct afb_xreq *xreq = xreq_from_req_x2(closure);
511 int r = xreq_has_permission_cb(closure, permission);
512 return afb_hook_xreq_has_permission(xreq, permission, r);
515 static char *xreq_hooked_get_application_id_cb(struct afb_req_x2 *closure)
517 struct afb_xreq *xreq = xreq_from_req_x2(closure);
518 char *r = xreq_get_application_id_cb(closure);
519 return afb_hook_xreq_get_application_id(xreq, r);
522 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)
524 struct afb_xreq *xreq = xreq_from_req_x2(closure);
525 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
526 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
529 static int xreq_hooked_get_uid_cb(struct afb_req_x2 *closure)
531 struct afb_xreq *xreq = xreq_from_req_x2(closure);
532 int r = xreq_get_uid_cb(closure);
533 return afb_hook_xreq_get_uid(xreq, r);
536 static struct json_object *xreq_hooked_get_client_info_cb(struct afb_req_x2 *closure)
538 struct afb_xreq *xreq = xreq_from_req_x2(closure);
539 struct json_object *r = xreq_get_client_info_cb(closure);
540 return afb_hook_xreq_get_client_info(xreq, r);
543 static void xreq_hooked_subcall_cb(
544 struct afb_req_x2 *req,
547 struct json_object *args,
549 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
552 struct afb_xreq *xreq = xreq_from_req_x2(req);
553 afb_calls_hooked_subcall(xreq, api, verb, args, flags, callback, closure);
556 static int xreq_hooked_subcallsync_cb(
557 struct afb_req_x2 *req,
560 struct json_object *args,
562 struct json_object **object,
566 struct afb_xreq *xreq = xreq_from_req_x2(req);
567 return afb_calls_hooked_subcall_sync(xreq, api, verb, args, flags, object, error, info);
570 /******************************************************************************/
572 const struct afb_req_x2_itf xreq_itf = {
573 .json = xreq_json_cb,
575 .legacy_success = xreq_legacy_success_cb,
576 .legacy_fail = xreq_legacy_fail_cb,
577 .legacy_vsuccess = xreq_legacy_vsuccess_cb,
578 .legacy_vfail = xreq_legacy_vfail_cb,
579 .legacy_context_get = xreq_legacy_context_get_cb,
580 .legacy_context_set = xreq_legacy_context_set_cb,
581 .addref = xreq_addref_cb,
582 .unref = xreq_unref_cb,
583 .session_close = xreq_session_close_cb,
584 .session_set_LOA = xreq_session_set_LOA_cb,
585 .legacy_subscribe_event_x1 = xreq_legacy_subscribe_event_x1_cb,
586 .legacy_unsubscribe_event_x1 = xreq_legacy_unsubscribe_event_x1_cb,
587 .legacy_subcall = xreq_legacy_subcall_cb,
588 .legacy_subcallsync = xreq_legacy_subcallsync_cb,
589 .vverbose = xreq_vverbose_cb,
590 .legacy_store_req = xreq_legacy_store_cb,
591 .legacy_subcall_req = xreq_legacy_subcall_req_cb,
592 .has_permission = xreq_has_permission_cb,
593 .get_application_id = xreq_get_application_id_cb,
594 .context_make = xreq_context_make_cb,
595 .subscribe_event_x2 = xreq_subscribe_event_x2_cb,
596 .unsubscribe_event_x2 = xreq_unsubscribe_event_x2_cb,
597 .legacy_subcall_request = xreq_legacy_subcall_request_cb,
598 .get_uid = xreq_get_uid_cb,
599 .reply = xreq_reply_cb,
600 .vreply = xreq_vreply_cb,
601 .get_client_info = xreq_get_client_info_cb,
602 .subcall = xreq_subcall_cb,
603 .subcallsync = xreq_subcallsync_cb,
606 const struct afb_req_x2_itf xreq_hooked_itf = {
607 .json = xreq_hooked_json_cb,
608 .get = xreq_hooked_get_cb,
609 .legacy_success = xreq_hooked_legacy_success_cb,
610 .legacy_fail = xreq_hooked_legacy_fail_cb,
611 .legacy_vsuccess = xreq_hooked_legacy_vsuccess_cb,
612 .legacy_vfail = xreq_hooked_legacy_vfail_cb,
613 .legacy_context_get = xreq_hooked_legacy_context_get_cb,
614 .legacy_context_set = xreq_hooked_legacy_context_set_cb,
615 .addref = xreq_hooked_addref_cb,
616 .unref = xreq_hooked_unref_cb,
617 .session_close = xreq_hooked_session_close_cb,
618 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
619 .legacy_subscribe_event_x1 = xreq_hooked_legacy_subscribe_event_x1_cb,
620 .legacy_unsubscribe_event_x1 = xreq_hooked_legacy_unsubscribe_event_x1_cb,
621 .legacy_subcall = xreq_hooked_legacy_subcall_cb,
622 .legacy_subcallsync = xreq_hooked_legacy_subcallsync_cb,
623 .vverbose = xreq_hooked_vverbose_cb,
624 .legacy_store_req = xreq_hooked_legacy_store_cb,
625 .legacy_subcall_req = xreq_hooked_legacy_subcall_req_cb,
626 .has_permission = xreq_hooked_has_permission_cb,
627 .get_application_id = xreq_hooked_get_application_id_cb,
628 .context_make = xreq_hooked_context_make_cb,
629 .subscribe_event_x2 = xreq_hooked_subscribe_event_x2_cb,
630 .unsubscribe_event_x2 = xreq_hooked_unsubscribe_event_x2_cb,
631 .legacy_subcall_request = xreq_hooked_legacy_subcall_request_cb,
632 .get_uid = xreq_hooked_get_uid_cb,
633 .reply = xreq_hooked_reply_cb,
634 .vreply = xreq_hooked_vreply_cb,
635 .get_client_info = xreq_hooked_get_client_info_cb,
636 .subcall = xreq_hooked_subcall_cb,
637 .subcallsync = xreq_hooked_subcallsync_cb,
640 /******************************************************************************/
642 struct afb_req_x1 afb_xreq_unstore(struct afb_stored_req *sreq)
644 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
646 afb_hook_xreq_legacy_unstore(xreq);
647 return xreq_to_req_x1(xreq);
650 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
652 return afb_req_x2_json(xreq_to_req_x2(xreq));
655 void afb_xreq_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
657 afb_req_x2_reply(xreq_to_req_x2(xreq), obj, error, info);
660 void afb_xreq_reply_f(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info, ...)
664 va_start(args, info);
665 afb_req_x2_reply_v(xreq_to_req_x2(xreq), obj, error, info, args);
669 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
671 struct json_object *obj = xreq_json_cb(xreq_to_req_x2(xreq));
672 const char *result = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE);
674 *size = strlen(result);
678 void afb_xreq_addref(struct afb_xreq *xreq)
680 afb_req_x2_addref(xreq_to_req_x2(xreq));
683 void afb_xreq_unref(struct afb_xreq *xreq)
685 afb_req_x2_unref(xreq_to_req_x2(xreq));
688 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)
690 xreq_legacy_subcall_request_cb(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
693 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)
695 afb_req_x2_subcall_legacy(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
698 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)
700 xreq_subcall_cb(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
703 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)
705 afb_req_x2_subcall(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
708 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)
710 return xreq_legacy_subcallsync_cb(xreq_to_req_x2(xreq), api, verb, args, result);
713 int afb_xreq_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
715 return afb_req_x2_subcall_sync_legacy(xreq_to_req_x2(xreq), api, verb, args, result);
718 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
722 if ((sessionflags & (AFB_SESSION_CLOSE_X1|AFB_SESSION_RENEW_X1|AFB_SESSION_CHECK_X1|AFB_SESSION_LOA_EQ_X1)) != 0) {
723 if (!afb_context_check(&xreq->context)) {
724 afb_context_close(&xreq->context);
725 afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
731 if ((sessionflags & AFB_SESSION_LOA_GE_X1) != 0) {
732 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
733 if (!afb_context_check_loa(&xreq->context, loa)) {
734 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
740 if ((sessionflags & AFB_SESSION_LOA_LE_X1) != 0) {
741 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
742 if (afb_context_check_loa(&xreq->context, loa + 1)) {
743 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
749 if ((sessionflags & AFB_SESSION_RENEW_X1) != 0) {
750 afb_context_refresh(&xreq->context);
752 if ((sessionflags & AFB_SESSION_CLOSE_X1) != 0) {
753 afb_context_change_loa(&xreq->context, 0);
754 afb_context_close(&xreq->context);
760 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
764 if (sessionflags != 0) {
765 if (!afb_context_check(&xreq->context)) {
766 afb_context_close(&xreq->context);
767 afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
773 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_X2);
774 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
775 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
780 if (auth && !afb_auth_check(xreq, auth)) {
781 afb_xreq_reply_f(xreq, NULL, "denied", "authorisation refused");
786 if ((sessionflags & AFB_SESSION_REFRESH_X2) != 0) {
787 afb_context_refresh(&xreq->context);
789 if ((sessionflags & AFB_SESSION_CLOSE_X2) != 0) {
790 afb_context_close(&xreq->context);
796 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
799 afb_xreq_reply_unknown_verb(xreq);
801 if (!xreq_session_check_apply_v1(xreq, verb->session))
802 verb->callback(xreq_to_req_x1(xreq));
805 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
808 afb_xreq_reply_unknown_verb(xreq);
810 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
811 verb->callback(xreq_to_req_x1(xreq));
814 void afb_xreq_call_verb_v3(struct afb_xreq *xreq, const struct afb_verb_v3 *verb)
817 afb_xreq_reply_unknown_verb(xreq);
819 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
820 verb->callback(xreq_to_req_x2(xreq));
823 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
825 memset(xreq, 0, sizeof *xreq);
826 xreq->request.itf = &xreq_itf; /* no hook by default */
828 xreq->queryitf = queryitf;
831 void afb_xreq_reply_unknown_api(struct afb_xreq *xreq)
833 afb_xreq_reply_f(xreq, NULL, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
836 void afb_xreq_reply_unknown_verb(struct afb_xreq *xreq)
838 afb_xreq_reply_f(xreq, NULL, "unknown-verb", "verb %s unknown within api %s", xreq->request.called_verb, xreq->request.called_api);
841 static void init_hooking(struct afb_xreq *xreq)
843 afb_hook_init_xreq(xreq);
844 if (xreq->hookflags) {
845 xreq->request.itf = &xreq_hooked_itf; /* unhook the interface */
846 afb_hook_xreq_begin(xreq);
851 * job callback for asynchronous and secured processing of the request.
853 static void process_async(int signum, void *arg)
855 struct afb_xreq *xreq = arg;
856 const struct afb_api_item *api;
859 /* emit the error (assumes that hooking is initialised) */
860 afb_xreq_reply_f(xreq, NULL, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
864 /* invoke api call method to process the request */
865 api = (const struct afb_api_item*)xreq->context.api_key;
866 api->itf->call(api->closure, xreq);
868 /* release the request */
869 afb_xreq_unhooked_unref(xreq);
873 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
874 * The early failure occurs only in function 'afb_xreq_process' where normally,
875 * the hooking is not initialised. So this "early" failure takes care to initialise
876 * the hooking in first.
878 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
886 va_start(args, info);
887 afb_req_x2_reply_v(xreq_to_req_x2(xreq), NULL, status, info, args);
892 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
893 * Errors are reported as request failures.
895 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
897 const struct afb_api_item *api;
898 struct afb_xreq *caller;
900 /* lookup at the api */
901 xreq->apiset = apiset;
902 api = afb_apiset_lookup_started(apiset, xreq->request.called_api, 1);
905 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
907 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.called_api);
910 xreq->context.api_key = api;
912 /* check self locking */
914 caller = xreq->caller;
916 if (((const struct afb_api_item*)caller->context.api_key)->group == api->group) {
917 /* noconcurrency lock detected */
918 ERROR("self-lock detected in call stack for API %s", xreq->request.called_api);
919 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.called_api);
922 caller = caller->caller;
926 /* queue the request job */
927 afb_xreq_unhooked_addref(xreq);
928 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
929 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
930 ERROR("can't process job with threads: %m");
931 early_failure(xreq, "cancelled", "not able to create a job for the task");
932 afb_xreq_unhooked_unref(xreq);
935 afb_xreq_unhooked_unref(xreq);
938 const char *xreq_on_behalf_cred_export(struct afb_xreq *xreq)
940 return xreq->caller ? afb_cred_export(xreq->cred) : NULL;