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>
28 #include <afb/afb-binding-v1.h>
29 #include <afb/afb-binding-v2.h>
30 #include <afb/afb-binding-v3.h>
31 #include <afb/afb-req-x2.h>
34 #include "afb-apiset.h"
36 #include "afb-calls.h"
37 #include "afb-context.h"
41 #include "afb-msg-json.h"
47 /******************************************************************************/
49 static void xreq_finalize(struct afb_xreq *xreq)
52 afb_xreq_reply(xreq, NULL, "error", "no reply");
54 afb_hook_xreq_end(xreq);
56 afb_xreq_unhooked_unref(xreq->caller);
57 xreq->queryitf->unref(xreq);
60 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
62 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
65 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
67 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
71 /******************************************************************************/
73 struct json_object *afb_xreq_unhooked_json(struct afb_xreq *xreq)
75 if (!xreq->json && xreq->queryitf->json)
76 xreq->json = xreq->queryitf->json(xreq);
80 static struct json_object *xreq_json_cb(struct afb_req_x2 *closure)
82 struct afb_xreq *xreq = xreq_from_req_x2(closure);
83 return afb_xreq_unhooked_json(xreq);
86 static struct afb_arg xreq_get_cb(struct afb_req_x2 *closure, const char *name)
88 struct afb_xreq *xreq = xreq_from_req_x2(closure);
90 struct json_object *object, *value;
92 if (xreq->queryitf->get)
93 arg = xreq->queryitf->get(xreq, name);
95 object = xreq_json_cb(closure);
96 if (json_object_object_get_ex(object, name, &value)) {
98 arg.value = json_object_get_string(value);
108 static void xreq_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
110 struct afb_xreq *xreq = xreq_from_req_x2(closure);
113 ERROR("reply called more than one time!!");
114 json_object_put(obj);
117 xreq->queryitf->reply(xreq, obj, error, info);
121 static void xreq_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
124 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
126 xreq_reply_cb(closure, obj, error, info);
130 static void xreq_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
132 xreq_reply_cb(closure, obj, NULL, info);
135 static void xreq_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
137 xreq_reply_cb(closure, NULL, status, info);
140 static void xreq_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
142 xreq_vreply_cb(closure, obj, NULL, fmt, args);
145 static void xreq_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
147 xreq_vreply_cb(closure, NULL, status, fmt, args);
150 static void *xreq_legacy_context_get_cb(struct afb_req_x2 *closure)
152 struct afb_xreq *xreq = xreq_from_req_x2(closure);
153 return afb_context_get(&xreq->context);
156 static void xreq_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
158 struct afb_xreq *xreq = xreq_from_req_x2(closure);
159 afb_context_set(&xreq->context, value, free_value);
162 static struct afb_req_x2 *xreq_addref_cb(struct afb_req_x2 *closure)
164 struct afb_xreq *xreq = xreq_from_req_x2(closure);
165 afb_xreq_unhooked_addref(xreq);
169 static void xreq_unref_cb(struct afb_req_x2 *closure)
171 struct afb_xreq *xreq = xreq_from_req_x2(closure);
172 afb_xreq_unhooked_unref(xreq);
175 static void xreq_session_close_cb(struct afb_req_x2 *closure)
177 struct afb_xreq *xreq = xreq_from_req_x2(closure);
178 afb_context_close(&xreq->context);
181 static int xreq_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
183 struct afb_xreq *xreq = xreq_from_req_x2(closure);
184 return afb_context_change_loa(&xreq->context, level);
187 static int xreq_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
189 struct afb_xreq *xreq = xreq_from_req_x2(closure);
190 return afb_xreq_subscribe(xreq, event);
193 static int xreq_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
195 return xreq_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
198 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
201 return afb_evt_event_x2_add_watch(xreq->listener, event);
202 if (xreq->queryitf->subscribe)
203 return xreq->queryitf->subscribe(xreq, event);
204 ERROR("no event listener, subscription impossible");
209 static int xreq_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
211 struct afb_xreq *xreq = xreq_from_req_x2(closure);
212 return afb_xreq_unsubscribe(xreq, event);
215 static int xreq_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
217 return xreq_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
220 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
223 return afb_evt_event_x2_remove_watch(xreq->listener, event);
224 if (xreq->queryitf->unsubscribe)
225 return xreq->queryitf->unsubscribe(xreq, event);
226 ERROR("no event listener, unsubscription impossible");
231 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)
233 struct afb_xreq *xreq = xreq_from_req_x2(req);
234 return afb_calls_legacy_subcall_v1(xreq, api, verb, args, callback, closure);
237 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)
239 struct afb_xreq *xreq = xreq_from_req_x2(req);
240 return afb_calls_legacy_subcall_v2(xreq, api, verb, args, callback, closure);
243 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)
245 struct afb_xreq *xreq = xreq_from_req_x2(req);
246 return afb_calls_legacy_subcall_v3(xreq, api, verb, args, callback, closure);
250 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)
252 struct afb_xreq *xreq = xreq_from_req_x2(req);
253 return afb_calls_legacy_subcall_sync(xreq, api, verb, args, result);
256 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)
259 struct afb_xreq *xreq = xreq_from_req_x2(closure);
261 if (!fmt || vasprintf(&p, fmt, args) < 0)
262 vverbose(level, file, line, func, fmt, args);
264 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.called_api, p);
269 static struct afb_stored_req *xreq_legacy_store_cb(struct afb_req_x2 *closure)
271 xreq_addref_cb(closure);
272 return (struct afb_stored_req*)closure;
275 static int xreq_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
277 struct afb_xreq *xreq = xreq_from_req_x2(closure);
278 return afb_auth_has_permission(xreq, permission);
281 static char *xreq_get_application_id_cb(struct afb_req_x2 *closure)
283 struct afb_xreq *xreq = xreq_from_req_x2(closure);
284 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
287 static void *xreq_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
289 struct afb_xreq *xreq = xreq_from_req_x2(closure);
290 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
293 static int xreq_get_uid_cb(struct afb_req_x2 *closure)
295 struct afb_xreq *xreq = xreq_from_req_x2(closure);
296 return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
299 static struct json_object *xreq_get_client_info_cb(struct afb_req_x2 *closure)
301 struct afb_xreq *xreq = xreq_from_req_x2(closure);
302 struct json_object *r = json_object_new_object();
303 if (xreq->cred && xreq->cred->id) {
304 json_object_object_add(r, "uid", json_object_new_int(xreq->cred->uid));
305 json_object_object_add(r, "gid", json_object_new_int(xreq->cred->gid));
306 json_object_object_add(r, "pid", json_object_new_int(xreq->cred->pid));
307 json_object_object_add(r, "user", json_object_new_string(xreq->cred->user));
308 json_object_object_add(r, "label", json_object_new_string(xreq->cred->label));
309 json_object_object_add(r, "id", json_object_new_string(xreq->cred->id));
311 if (xreq->context.session) {
312 json_object_object_add(r, "uuid", json_object_new_string(afb_context_uuid(&xreq->context)));
313 json_object_object_add(r, "LOA", json_object_new_int(afb_context_get_loa(&xreq->context)));
318 static void xreq_subcall_cb(
319 struct afb_req_x2 *req,
322 struct json_object *args,
324 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
327 struct afb_xreq *xreq = xreq_from_req_x2(req);
328 afb_calls_subcall(xreq, api, verb, args, flags, callback, closure);
331 static int xreq_subcallsync_cb(
332 struct afb_req_x2 *req,
335 struct json_object *args,
337 struct json_object **object,
341 struct afb_xreq *xreq = xreq_from_req_x2(req);
342 return afb_calls_subcall_sync(xreq, api, verb, args, flags, object, error, info);
345 /******************************************************************************/
347 static struct json_object *xreq_hooked_json_cb(struct afb_req_x2 *closure)
349 struct json_object *r = xreq_json_cb(closure);
350 struct afb_xreq *xreq = xreq_from_req_x2(closure);
351 return afb_hook_xreq_json(xreq, r);
354 static struct afb_arg xreq_hooked_get_cb(struct afb_req_x2 *closure, const char *name)
356 struct afb_arg r = xreq_get_cb(closure, name);
357 struct afb_xreq *xreq = xreq_from_req_x2(closure);
358 return afb_hook_xreq_get(xreq, name, r);
361 static void xreq_hooked_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
363 struct afb_xreq *xreq = xreq_from_req_x2(closure);
364 afb_hook_xreq_reply(xreq, obj, error, info);
365 xreq_reply_cb(closure, obj, error, info);
368 static void xreq_hooked_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
371 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
373 xreq_hooked_reply_cb(closure, obj, error, info);
377 static void xreq_hooked_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
379 xreq_hooked_reply_cb(closure, obj, NULL, info);
382 static void xreq_hooked_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
384 xreq_hooked_reply_cb(closure, NULL, status, info);
387 static void xreq_hooked_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
389 xreq_hooked_vreply_cb(closure, obj, NULL, fmt, args);
392 static void xreq_hooked_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
394 xreq_hooked_vreply_cb(closure, NULL, status, fmt, args);
397 static void *xreq_hooked_legacy_context_get_cb(struct afb_req_x2 *closure)
399 void *r = xreq_legacy_context_get_cb(closure);
400 struct afb_xreq *xreq = xreq_from_req_x2(closure);
401 return afb_hook_xreq_legacy_context_get(xreq, r);
404 static void xreq_hooked_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
406 struct afb_xreq *xreq = xreq_from_req_x2(closure);
407 afb_hook_xreq_legacy_context_set(xreq, value, free_value);
408 xreq_legacy_context_set_cb(closure, value, free_value);
411 static struct afb_req_x2 *xreq_hooked_addref_cb(struct afb_req_x2 *closure)
413 struct afb_xreq *xreq = xreq_from_req_x2(closure);
414 afb_hook_xreq_addref(xreq);
415 return xreq_addref_cb(closure);
418 static void xreq_hooked_unref_cb(struct afb_req_x2 *closure)
420 struct afb_xreq *xreq = xreq_from_req_x2(closure);
421 afb_hook_xreq_unref(xreq);
422 xreq_unref_cb(closure);
425 static void xreq_hooked_session_close_cb(struct afb_req_x2 *closure)
427 struct afb_xreq *xreq = xreq_from_req_x2(closure);
428 afb_hook_xreq_session_close(xreq);
429 xreq_session_close_cb(closure);
432 static int xreq_hooked_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
434 int r = xreq_session_set_LOA_cb(closure, level);
435 struct afb_xreq *xreq = xreq_from_req_x2(closure);
436 return afb_hook_xreq_session_set_LOA(xreq, level, r);
439 static int xreq_hooked_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
441 int r = xreq_subscribe_event_x2_cb(closure, event);
442 struct afb_xreq *xreq = xreq_from_req_x2(closure);
443 return afb_hook_xreq_subscribe(xreq, event, r);
446 static int xreq_hooked_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
448 return xreq_hooked_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
451 static int xreq_hooked_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
453 int r = xreq_unsubscribe_event_x2_cb(closure, event);
454 struct afb_xreq *xreq = xreq_from_req_x2(closure);
455 return afb_hook_xreq_unsubscribe(xreq, event, r);
458 static int xreq_hooked_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
460 return xreq_hooked_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
463 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)
465 struct afb_xreq *xreq = xreq_from_req_x2(req);
466 afb_calls_legacy_hooked_subcall_v1(xreq, api, verb, args, callback, closure);
469 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)
471 struct afb_xreq *xreq = xreq_from_req_x2(req);
472 afb_calls_legacy_hooked_subcall_v2(xreq, api, verb, args, callback, closure);
475 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)
477 struct afb_xreq *xreq = xreq_from_req_x2(req);
478 afb_calls_legacy_hooked_subcall_v3(xreq, api, verb, args, callback, closure);
481 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)
483 struct afb_xreq *xreq = xreq_from_req_x2(req);
484 return afb_calls_legacy_hooked_subcall_sync(xreq, api, verb, args, result);
487 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)
489 struct afb_xreq *xreq = xreq_from_req_x2(closure);
492 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
493 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
497 static struct afb_stored_req *xreq_hooked_legacy_store_cb(struct afb_req_x2 *closure)
499 struct afb_xreq *xreq = xreq_from_req_x2(closure);
500 struct afb_stored_req *r = xreq_legacy_store_cb(closure);
501 afb_hook_xreq_legacy_store(xreq, r);
505 static int xreq_hooked_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
507 struct afb_xreq *xreq = xreq_from_req_x2(closure);
508 int r = xreq_has_permission_cb(closure, permission);
509 return afb_hook_xreq_has_permission(xreq, permission, r);
512 static char *xreq_hooked_get_application_id_cb(struct afb_req_x2 *closure)
514 struct afb_xreq *xreq = xreq_from_req_x2(closure);
515 char *r = xreq_get_application_id_cb(closure);
516 return afb_hook_xreq_get_application_id(xreq, r);
519 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)
521 struct afb_xreq *xreq = xreq_from_req_x2(closure);
522 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
523 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
526 static int xreq_hooked_get_uid_cb(struct afb_req_x2 *closure)
528 struct afb_xreq *xreq = xreq_from_req_x2(closure);
529 int r = xreq_get_uid_cb(closure);
530 return afb_hook_xreq_get_uid(xreq, r);
533 static struct json_object *xreq_hooked_get_client_info_cb(struct afb_req_x2 *closure)
535 struct afb_xreq *xreq = xreq_from_req_x2(closure);
536 struct json_object *r = xreq_get_client_info_cb(closure);
537 return afb_hook_xreq_get_client_info(xreq, r);
540 static void xreq_hooked_subcall_cb(
541 struct afb_req_x2 *req,
544 struct json_object *args,
546 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
549 struct afb_xreq *xreq = xreq_from_req_x2(req);
550 afb_calls_hooked_subcall(xreq, api, verb, args, flags, callback, closure);
553 static int xreq_hooked_subcallsync_cb(
554 struct afb_req_x2 *req,
557 struct json_object *args,
559 struct json_object **object,
563 struct afb_xreq *xreq = xreq_from_req_x2(req);
564 return afb_calls_hooked_subcall_sync(xreq, api, verb, args, flags, object, error, info);
567 /******************************************************************************/
569 const struct afb_req_x2_itf xreq_itf = {
570 .json = xreq_json_cb,
572 .legacy_success = xreq_legacy_success_cb,
573 .legacy_fail = xreq_legacy_fail_cb,
574 .legacy_vsuccess = xreq_legacy_vsuccess_cb,
575 .legacy_vfail = xreq_legacy_vfail_cb,
576 .legacy_context_get = xreq_legacy_context_get_cb,
577 .legacy_context_set = xreq_legacy_context_set_cb,
578 .addref = xreq_addref_cb,
579 .unref = xreq_unref_cb,
580 .session_close = xreq_session_close_cb,
581 .session_set_LOA = xreq_session_set_LOA_cb,
582 .legacy_subscribe_event_x1 = xreq_legacy_subscribe_event_x1_cb,
583 .legacy_unsubscribe_event_x1 = xreq_legacy_unsubscribe_event_x1_cb,
584 .legacy_subcall = xreq_legacy_subcall_cb,
585 .legacy_subcallsync = xreq_legacy_subcallsync_cb,
586 .vverbose = xreq_vverbose_cb,
587 .legacy_store_req = xreq_legacy_store_cb,
588 .legacy_subcall_req = xreq_legacy_subcall_req_cb,
589 .has_permission = xreq_has_permission_cb,
590 .get_application_id = xreq_get_application_id_cb,
591 .context_make = xreq_context_make_cb,
592 .subscribe_event_x2 = xreq_subscribe_event_x2_cb,
593 .unsubscribe_event_x2 = xreq_unsubscribe_event_x2_cb,
594 .legacy_subcall_request = xreq_legacy_subcall_request_cb,
595 .get_uid = xreq_get_uid_cb,
596 .reply = xreq_reply_cb,
597 .vreply = xreq_vreply_cb,
598 .get_client_info = xreq_get_client_info_cb,
599 .subcall = xreq_subcall_cb,
600 .subcallsync = xreq_subcallsync_cb,
603 const struct afb_req_x2_itf xreq_hooked_itf = {
604 .json = xreq_hooked_json_cb,
605 .get = xreq_hooked_get_cb,
606 .legacy_success = xreq_hooked_legacy_success_cb,
607 .legacy_fail = xreq_hooked_legacy_fail_cb,
608 .legacy_vsuccess = xreq_hooked_legacy_vsuccess_cb,
609 .legacy_vfail = xreq_hooked_legacy_vfail_cb,
610 .legacy_context_get = xreq_hooked_legacy_context_get_cb,
611 .legacy_context_set = xreq_hooked_legacy_context_set_cb,
612 .addref = xreq_hooked_addref_cb,
613 .unref = xreq_hooked_unref_cb,
614 .session_close = xreq_hooked_session_close_cb,
615 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
616 .legacy_subscribe_event_x1 = xreq_hooked_legacy_subscribe_event_x1_cb,
617 .legacy_unsubscribe_event_x1 = xreq_hooked_legacy_unsubscribe_event_x1_cb,
618 .legacy_subcall = xreq_hooked_legacy_subcall_cb,
619 .legacy_subcallsync = xreq_hooked_legacy_subcallsync_cb,
620 .vverbose = xreq_hooked_vverbose_cb,
621 .legacy_store_req = xreq_hooked_legacy_store_cb,
622 .legacy_subcall_req = xreq_hooked_legacy_subcall_req_cb,
623 .has_permission = xreq_hooked_has_permission_cb,
624 .get_application_id = xreq_hooked_get_application_id_cb,
625 .context_make = xreq_hooked_context_make_cb,
626 .subscribe_event_x2 = xreq_hooked_subscribe_event_x2_cb,
627 .unsubscribe_event_x2 = xreq_hooked_unsubscribe_event_x2_cb,
628 .legacy_subcall_request = xreq_hooked_legacy_subcall_request_cb,
629 .get_uid = xreq_hooked_get_uid_cb,
630 .reply = xreq_hooked_reply_cb,
631 .vreply = xreq_hooked_vreply_cb,
632 .get_client_info = xreq_hooked_get_client_info_cb,
633 .subcall = xreq_hooked_subcall_cb,
634 .subcallsync = xreq_hooked_subcallsync_cb,
637 /******************************************************************************/
639 struct afb_req_x1 afb_xreq_unstore(struct afb_stored_req *sreq)
641 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
643 afb_hook_xreq_legacy_unstore(xreq);
644 return xreq_to_req_x1(xreq);
647 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
649 return afb_req_x2_json(xreq_to_req_x2(xreq));
652 void afb_xreq_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
654 afb_req_x2_reply(xreq_to_req_x2(xreq), obj, error, info);
657 void afb_xreq_reply_f(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info, ...)
661 va_start(args, info);
662 afb_req_x2_reply_v(xreq_to_req_x2(xreq), obj, error, info, args);
666 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
668 struct json_object *obj = xreq_json_cb(xreq_to_req_x2(xreq));
669 const char *result = json_object_to_json_string(obj);
671 *size = strlen(result);
675 void afb_xreq_addref(struct afb_xreq *xreq)
677 afb_req_x2_addref(xreq_to_req_x2(xreq));
680 void afb_xreq_unref(struct afb_xreq *xreq)
682 afb_req_x2_unref(xreq_to_req_x2(xreq));
685 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)
687 xreq_legacy_subcall_request_cb(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
690 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)
692 afb_req_x2_subcall_legacy(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
695 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)
697 xreq_subcall_cb(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
700 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)
702 afb_req_x2_subcall(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
705 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)
707 return xreq_legacy_subcallsync_cb(xreq_to_req_x2(xreq), api, verb, args, result);
710 int afb_xreq_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
712 return afb_req_x2_subcall_sync_legacy(xreq_to_req_x2(xreq), api, verb, args, result);
715 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
719 if ((sessionflags & (AFB_SESSION_CLOSE_X1|AFB_SESSION_RENEW_X1|AFB_SESSION_CHECK_X1|AFB_SESSION_LOA_EQ_X1)) != 0) {
720 if (!afb_context_check(&xreq->context)) {
721 afb_context_close(&xreq->context);
722 afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
728 if ((sessionflags & AFB_SESSION_LOA_GE_X1) != 0) {
729 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
730 if (!afb_context_check_loa(&xreq->context, loa)) {
731 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
737 if ((sessionflags & AFB_SESSION_LOA_LE_X1) != 0) {
738 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
739 if (afb_context_check_loa(&xreq->context, loa + 1)) {
740 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
746 if ((sessionflags & AFB_SESSION_RENEW_X1) != 0) {
747 afb_context_refresh(&xreq->context);
749 if ((sessionflags & AFB_SESSION_CLOSE_X1) != 0) {
750 afb_context_change_loa(&xreq->context, 0);
751 afb_context_close(&xreq->context);
757 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
761 if (sessionflags != 0) {
762 if (!afb_context_check(&xreq->context)) {
763 afb_context_close(&xreq->context);
764 afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
770 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_X2);
771 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
772 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
777 if (auth && !afb_auth_check(xreq, auth)) {
778 afb_xreq_reply_f(xreq, NULL, "denied", "authorisation refused");
783 if ((sessionflags & AFB_SESSION_REFRESH_X2) != 0) {
784 afb_context_refresh(&xreq->context);
786 if ((sessionflags & AFB_SESSION_CLOSE_X2) != 0) {
787 afb_context_close(&xreq->context);
793 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
796 afb_xreq_reply_unknown_verb(xreq);
798 if (!xreq_session_check_apply_v1(xreq, verb->session))
799 verb->callback(xreq_to_req_x1(xreq));
802 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
805 afb_xreq_reply_unknown_verb(xreq);
807 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
808 verb->callback(xreq_to_req_x1(xreq));
811 void afb_xreq_call_verb_v3(struct afb_xreq *xreq, const struct afb_verb_v3 *verb)
814 afb_xreq_reply_unknown_verb(xreq);
816 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
817 verb->callback(xreq_to_req_x2(xreq));
820 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
822 memset(xreq, 0, sizeof *xreq);
823 xreq->request.itf = &xreq_itf; /* no hook by default */
825 xreq->queryitf = queryitf;
828 void afb_xreq_reply_unknown_api(struct afb_xreq *xreq)
830 afb_xreq_reply_f(xreq, NULL, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
833 void afb_xreq_reply_unknown_verb(struct afb_xreq *xreq)
835 afb_xreq_reply_f(xreq, NULL, "unknown-verb", "verb %s unknown within api %s", xreq->request.called_verb, xreq->request.called_api);
838 static void init_hooking(struct afb_xreq *xreq)
840 afb_hook_init_xreq(xreq);
841 if (xreq->hookflags) {
842 xreq->request.itf = &xreq_hooked_itf; /* unhook the interface */
843 afb_hook_xreq_begin(xreq);
848 * job callback for asynchronous and secured processing of the request.
850 static void process_async(int signum, void *arg)
852 struct afb_xreq *xreq = arg;
853 const struct afb_api_item *api;
856 /* emit the error (assumes that hooking is initialised) */
857 afb_xreq_reply_f(xreq, NULL, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
861 /* invoke api call method to process the request */
862 api = (const struct afb_api_item*)xreq->context.api_key;
863 api->itf->call(api->closure, xreq);
865 /* release the request */
866 afb_xreq_unhooked_unref(xreq);
870 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
871 * The early failure occurs only in function 'afb_xreq_process' where normally,
872 * the hooking is not initialised. So this "early" failure takes care to initialise
873 * the hooking in first.
875 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
883 va_start(args, info);
884 afb_req_x2_reply_v(xreq_to_req_x2(xreq), NULL, status, info, args);
889 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
890 * Errors are reported as request failures.
892 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
894 const struct afb_api_item *api;
895 struct afb_xreq *caller;
897 /* lookup at the api */
898 xreq->apiset = apiset;
899 api = afb_apiset_lookup_started(apiset, xreq->request.called_api, 1);
902 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
904 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.called_api);
907 xreq->context.api_key = api;
909 /* check self locking */
911 caller = xreq->caller;
913 if (((const struct afb_api_item*)caller->context.api_key)->group == api->group) {
914 /* noconcurrency lock detected */
915 ERROR("self-lock detected in call stack for API %s", xreq->request.called_api);
916 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.called_api);
919 caller = caller->caller;
923 /* queue the request job */
924 afb_xreq_unhooked_addref(xreq);
925 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
926 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
927 ERROR("can't process job with threads: %m");
928 early_failure(xreq, "cancelled", "not able to create a job for the task");
929 afb_xreq_unhooked_unref(xreq);
932 afb_xreq_unhooked_unref(xreq);
935 const char *xreq_on_behalf_cred_export(struct afb_xreq *xreq)
937 return xreq->caller ? afb_cred_export(xreq->cred) : NULL;