2 * Copyright (C) 2017-2019 "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");
58 afb_hook_xreq_end(xreq);
61 afb_xreq_unhooked_unref(xreq->caller);
62 xreq->queryitf->unref(xreq);
65 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
67 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
70 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
72 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
76 /******************************************************************************/
78 struct json_object *afb_xreq_unhooked_json(struct afb_xreq *xreq)
80 if (!xreq->json && xreq->queryitf->json)
81 xreq->json = xreq->queryitf->json(xreq);
85 static struct json_object *xreq_json_cb(struct afb_req_x2 *closure)
87 struct afb_xreq *xreq = xreq_from_req_x2(closure);
88 return afb_xreq_unhooked_json(xreq);
91 static struct afb_arg xreq_get_cb(struct afb_req_x2 *closure, const char *name)
93 struct afb_xreq *xreq = xreq_from_req_x2(closure);
95 struct json_object *object, *value;
97 if (xreq->queryitf->get)
98 arg = xreq->queryitf->get(xreq, name);
100 object = xreq_json_cb(closure);
101 if (json_object_object_get_ex(object, name, &value)) {
103 arg.value = json_object_get_string(value);
113 static void xreq_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
115 struct afb_xreq *xreq = xreq_from_req_x2(closure);
118 ERROR("reply called more than one time!!");
119 json_object_put(obj);
122 xreq->queryitf->reply(xreq, obj, error, info);
126 static void xreq_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
129 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
131 xreq_reply_cb(closure, obj, error, info);
135 static void xreq_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
137 xreq_reply_cb(closure, obj, NULL, info);
140 static void xreq_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
142 xreq_reply_cb(closure, NULL, status, info);
145 static void xreq_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
147 xreq_vreply_cb(closure, obj, NULL, fmt, args);
150 static void xreq_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
152 xreq_vreply_cb(closure, NULL, status, fmt, args);
155 static void *xreq_legacy_context_get_cb(struct afb_req_x2 *closure)
157 struct afb_xreq *xreq = xreq_from_req_x2(closure);
158 return afb_context_get(&xreq->context);
161 static void xreq_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
163 struct afb_xreq *xreq = xreq_from_req_x2(closure);
164 afb_context_set(&xreq->context, value, free_value);
167 static struct afb_req_x2 *xreq_addref_cb(struct afb_req_x2 *closure)
169 struct afb_xreq *xreq = xreq_from_req_x2(closure);
170 afb_xreq_unhooked_addref(xreq);
174 static void xreq_unref_cb(struct afb_req_x2 *closure)
176 struct afb_xreq *xreq = xreq_from_req_x2(closure);
177 afb_xreq_unhooked_unref(xreq);
180 static void xreq_session_close_cb(struct afb_req_x2 *closure)
182 struct afb_xreq *xreq = xreq_from_req_x2(closure);
183 afb_context_close(&xreq->context);
186 static int xreq_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
188 struct afb_xreq *xreq = xreq_from_req_x2(closure);
189 return afb_context_change_loa(&xreq->context, level);
192 static int xreq_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
194 struct afb_xreq *xreq = xreq_from_req_x2(closure);
195 return afb_xreq_subscribe(xreq, event);
198 static int xreq_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
200 return xreq_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
203 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
206 ERROR("request replied, subscription impossible");
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");
234 if (xreq->queryitf->unsubscribe)
235 return xreq->queryitf->unsubscribe(xreq, event);
236 ERROR("no event listener, unsubscription impossible");
242 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)
244 struct afb_xreq *xreq = xreq_from_req_x2(req);
245 return afb_calls_legacy_subcall_v1(xreq, api, verb, args, callback, closure);
248 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)
250 struct afb_xreq *xreq = xreq_from_req_x2(req);
251 return afb_calls_legacy_subcall_v2(xreq, api, verb, args, callback, closure);
254 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)
256 struct afb_xreq *xreq = xreq_from_req_x2(req);
257 return afb_calls_legacy_subcall_v3(xreq, api, verb, args, callback, closure);
261 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)
263 struct afb_xreq *xreq = xreq_from_req_x2(req);
264 return afb_calls_legacy_subcall_sync(xreq, api, verb, args, result);
267 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)
270 struct afb_xreq *xreq = xreq_from_req_x2(closure);
272 if (!fmt || vasprintf(&p, fmt, args) < 0)
273 vverbose(level, file, line, func, fmt, args);
275 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.called_api, p);
280 static struct afb_stored_req *xreq_legacy_store_cb(struct afb_req_x2 *closure)
282 xreq_addref_cb(closure);
283 return (struct afb_stored_req*)closure;
286 static int xreq_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
288 struct afb_xreq *xreq = xreq_from_req_x2(closure);
289 return afb_auth_has_permission(xreq, permission);
292 static char *xreq_get_application_id_cb(struct afb_req_x2 *closure)
294 struct afb_xreq *xreq = xreq_from_req_x2(closure);
295 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
298 static void *xreq_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
300 struct afb_xreq *xreq = xreq_from_req_x2(closure);
301 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
304 static int xreq_get_uid_cb(struct afb_req_x2 *closure)
306 struct afb_xreq *xreq = xreq_from_req_x2(closure);
307 return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
310 static struct json_object *xreq_get_client_info_cb(struct afb_req_x2 *closure)
312 struct afb_xreq *xreq = xreq_from_req_x2(closure);
313 struct json_object *r = json_object_new_object();
314 if (xreq->cred && xreq->cred->id) {
315 json_object_object_add(r, "uid", json_object_new_int(xreq->cred->uid));
316 json_object_object_add(r, "gid", json_object_new_int(xreq->cred->gid));
317 json_object_object_add(r, "pid", json_object_new_int(xreq->cred->pid));
318 json_object_object_add(r, "user", json_object_new_string(xreq->cred->user));
319 json_object_object_add(r, "label", json_object_new_string(xreq->cred->label));
320 json_object_object_add(r, "id", json_object_new_string(xreq->cred->id));
322 if (xreq->context.session) {
323 json_object_object_add(r, "uuid", json_object_new_string(afb_context_uuid(&xreq->context)));
324 json_object_object_add(r, "LOA", json_object_new_int(afb_context_get_loa(&xreq->context)));
329 static void xreq_subcall_cb(
330 struct afb_req_x2 *req,
333 struct json_object *args,
335 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
338 struct afb_xreq *xreq = xreq_from_req_x2(req);
339 afb_calls_subcall(xreq, api, verb, args, flags, callback, closure);
342 static int xreq_subcallsync_cb(
343 struct afb_req_x2 *req,
346 struct json_object *args,
348 struct json_object **object,
352 struct afb_xreq *xreq = xreq_from_req_x2(req);
353 return afb_calls_subcall_sync(xreq, api, verb, args, flags, object, error, info);
356 /******************************************************************************/
358 const struct afb_req_x2_itf xreq_itf = {
359 .json = xreq_json_cb,
361 .legacy_success = xreq_legacy_success_cb,
362 .legacy_fail = xreq_legacy_fail_cb,
363 .legacy_vsuccess = xreq_legacy_vsuccess_cb,
364 .legacy_vfail = xreq_legacy_vfail_cb,
365 .legacy_context_get = xreq_legacy_context_get_cb,
366 .legacy_context_set = xreq_legacy_context_set_cb,
367 .addref = xreq_addref_cb,
368 .unref = xreq_unref_cb,
369 .session_close = xreq_session_close_cb,
370 .session_set_LOA = xreq_session_set_LOA_cb,
371 .legacy_subscribe_event_x1 = xreq_legacy_subscribe_event_x1_cb,
372 .legacy_unsubscribe_event_x1 = xreq_legacy_unsubscribe_event_x1_cb,
373 .legacy_subcall = xreq_legacy_subcall_cb,
374 .legacy_subcallsync = xreq_legacy_subcallsync_cb,
375 .vverbose = xreq_vverbose_cb,
376 .legacy_store_req = xreq_legacy_store_cb,
377 .legacy_subcall_req = xreq_legacy_subcall_req_cb,
378 .has_permission = xreq_has_permission_cb,
379 .get_application_id = xreq_get_application_id_cb,
380 .context_make = xreq_context_make_cb,
381 .subscribe_event_x2 = xreq_subscribe_event_x2_cb,
382 .unsubscribe_event_x2 = xreq_unsubscribe_event_x2_cb,
383 .legacy_subcall_request = xreq_legacy_subcall_request_cb,
384 .get_uid = xreq_get_uid_cb,
385 .reply = xreq_reply_cb,
386 .vreply = xreq_vreply_cb,
387 .get_client_info = xreq_get_client_info_cb,
388 .subcall = xreq_subcall_cb,
389 .subcallsync = xreq_subcallsync_cb,
391 /******************************************************************************/
394 static struct json_object *xreq_hooked_json_cb(struct afb_req_x2 *closure)
396 struct json_object *r = xreq_json_cb(closure);
397 struct afb_xreq *xreq = xreq_from_req_x2(closure);
398 return afb_hook_xreq_json(xreq, r);
401 static struct afb_arg xreq_hooked_get_cb(struct afb_req_x2 *closure, const char *name)
403 struct afb_arg r = xreq_get_cb(closure, name);
404 struct afb_xreq *xreq = xreq_from_req_x2(closure);
405 return afb_hook_xreq_get(xreq, name, r);
408 static void xreq_hooked_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
410 struct afb_xreq *xreq = xreq_from_req_x2(closure);
411 afb_hook_xreq_reply(xreq, obj, error, info);
412 xreq_reply_cb(closure, obj, error, info);
415 static void xreq_hooked_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
418 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
420 xreq_hooked_reply_cb(closure, obj, error, info);
424 static void xreq_hooked_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
426 xreq_hooked_reply_cb(closure, obj, NULL, info);
429 static void xreq_hooked_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
431 xreq_hooked_reply_cb(closure, NULL, status, info);
434 static void xreq_hooked_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
436 xreq_hooked_vreply_cb(closure, obj, NULL, fmt, args);
439 static void xreq_hooked_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
441 xreq_hooked_vreply_cb(closure, NULL, status, fmt, args);
444 static void *xreq_hooked_legacy_context_get_cb(struct afb_req_x2 *closure)
446 void *r = xreq_legacy_context_get_cb(closure);
447 struct afb_xreq *xreq = xreq_from_req_x2(closure);
448 return afb_hook_xreq_legacy_context_get(xreq, r);
451 static void xreq_hooked_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
453 struct afb_xreq *xreq = xreq_from_req_x2(closure);
454 afb_hook_xreq_legacy_context_set(xreq, value, free_value);
455 xreq_legacy_context_set_cb(closure, value, free_value);
458 static struct afb_req_x2 *xreq_hooked_addref_cb(struct afb_req_x2 *closure)
460 struct afb_xreq *xreq = xreq_from_req_x2(closure);
461 afb_hook_xreq_addref(xreq);
462 return xreq_addref_cb(closure);
465 static void xreq_hooked_unref_cb(struct afb_req_x2 *closure)
467 struct afb_xreq *xreq = xreq_from_req_x2(closure);
468 afb_hook_xreq_unref(xreq);
469 xreq_unref_cb(closure);
472 static void xreq_hooked_session_close_cb(struct afb_req_x2 *closure)
474 struct afb_xreq *xreq = xreq_from_req_x2(closure);
475 afb_hook_xreq_session_close(xreq);
476 xreq_session_close_cb(closure);
479 static int xreq_hooked_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
481 int r = xreq_session_set_LOA_cb(closure, level);
482 struct afb_xreq *xreq = xreq_from_req_x2(closure);
483 return afb_hook_xreq_session_set_LOA(xreq, level, r);
486 static int xreq_hooked_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
488 int r = xreq_subscribe_event_x2_cb(closure, event);
489 struct afb_xreq *xreq = xreq_from_req_x2(closure);
490 return afb_hook_xreq_subscribe(xreq, event, r);
493 static int xreq_hooked_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
495 return xreq_hooked_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
498 static int xreq_hooked_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
500 int r = xreq_unsubscribe_event_x2_cb(closure, event);
501 struct afb_xreq *xreq = xreq_from_req_x2(closure);
502 return afb_hook_xreq_unsubscribe(xreq, event, r);
505 static int xreq_hooked_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
507 return xreq_hooked_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
510 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)
512 struct afb_xreq *xreq = xreq_from_req_x2(req);
513 afb_calls_legacy_hooked_subcall_v1(xreq, api, verb, args, callback, closure);
516 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)
518 struct afb_xreq *xreq = xreq_from_req_x2(req);
519 afb_calls_legacy_hooked_subcall_v2(xreq, api, verb, args, callback, closure);
522 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)
524 struct afb_xreq *xreq = xreq_from_req_x2(req);
525 afb_calls_legacy_hooked_subcall_v3(xreq, api, verb, args, callback, closure);
528 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)
530 struct afb_xreq *xreq = xreq_from_req_x2(req);
531 return afb_calls_legacy_hooked_subcall_sync(xreq, api, verb, args, result);
534 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)
536 struct afb_xreq *xreq = xreq_from_req_x2(closure);
539 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
540 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
544 static struct afb_stored_req *xreq_hooked_legacy_store_cb(struct afb_req_x2 *closure)
546 struct afb_xreq *xreq = xreq_from_req_x2(closure);
547 struct afb_stored_req *r = xreq_legacy_store_cb(closure);
548 afb_hook_xreq_legacy_store(xreq, r);
552 static int xreq_hooked_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
554 struct afb_xreq *xreq = xreq_from_req_x2(closure);
555 int r = xreq_has_permission_cb(closure, permission);
556 return afb_hook_xreq_has_permission(xreq, permission, r);
559 static char *xreq_hooked_get_application_id_cb(struct afb_req_x2 *closure)
561 struct afb_xreq *xreq = xreq_from_req_x2(closure);
562 char *r = xreq_get_application_id_cb(closure);
563 return afb_hook_xreq_get_application_id(xreq, r);
566 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)
568 struct afb_xreq *xreq = xreq_from_req_x2(closure);
569 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
570 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
573 static int xreq_hooked_get_uid_cb(struct afb_req_x2 *closure)
575 struct afb_xreq *xreq = xreq_from_req_x2(closure);
576 int r = xreq_get_uid_cb(closure);
577 return afb_hook_xreq_get_uid(xreq, r);
580 static struct json_object *xreq_hooked_get_client_info_cb(struct afb_req_x2 *closure)
582 struct afb_xreq *xreq = xreq_from_req_x2(closure);
583 struct json_object *r = xreq_get_client_info_cb(closure);
584 return afb_hook_xreq_get_client_info(xreq, r);
587 static void xreq_hooked_subcall_cb(
588 struct afb_req_x2 *req,
591 struct json_object *args,
593 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
596 struct afb_xreq *xreq = xreq_from_req_x2(req);
597 afb_calls_hooked_subcall(xreq, api, verb, args, flags, callback, closure);
600 static int xreq_hooked_subcallsync_cb(
601 struct afb_req_x2 *req,
604 struct json_object *args,
606 struct json_object **object,
610 struct afb_xreq *xreq = xreq_from_req_x2(req);
611 return afb_calls_hooked_subcall_sync(xreq, api, verb, args, flags, object, error, info);
614 /******************************************************************************/
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,
651 /******************************************************************************/
653 struct afb_req_x1 afb_xreq_unstore(struct afb_stored_req *sreq)
655 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
658 afb_hook_xreq_legacy_unstore(xreq);
660 return xreq_to_req_x1(xreq);
663 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
665 return afb_req_x2_json(xreq_to_req_x2(xreq));
668 void afb_xreq_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
670 afb_req_x2_reply(xreq_to_req_x2(xreq), obj, error, info);
673 void afb_xreq_reply_f(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info, ...)
677 va_start(args, info);
678 afb_req_x2_reply_v(xreq_to_req_x2(xreq), obj, error, info, args);
682 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
684 struct json_object *obj = xreq_json_cb(xreq_to_req_x2(xreq));
685 const char *result = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE);
687 *size = strlen(result);
691 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)
693 xreq_legacy_subcall_request_cb(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
696 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)
698 xreq_subcall_cb(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
701 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)
703 return xreq_legacy_subcallsync_cb(xreq_to_req_x2(xreq), api, verb, args, result);
706 void afb_xreq_addref(struct afb_xreq *xreq)
708 afb_req_x2_addref(xreq_to_req_x2(xreq));
711 void afb_xreq_unref(struct afb_xreq *xreq)
713 afb_req_x2_unref(xreq_to_req_x2(xreq));
716 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)
718 afb_req_x2_subcall_legacy(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
721 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)
723 afb_req_x2_subcall(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
726 int afb_xreq_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
728 return afb_req_x2_subcall_sync_legacy(xreq_to_req_x2(xreq), api, verb, args, result);
731 #if WITH_LEGACY_BINDING_V1
732 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
736 if ((sessionflags & (AFB_SESSION_CLOSE_X1|AFB_SESSION_RENEW_X1|AFB_SESSION_CHECK_X1|AFB_SESSION_LOA_EQ_X1)) != 0) {
737 if (!afb_context_check(&xreq->context)) {
738 afb_context_close(&xreq->context);
739 afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
745 if ((sessionflags & AFB_SESSION_LOA_GE_X1) != 0) {
746 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
747 if (!afb_context_check_loa(&xreq->context, loa)) {
748 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
754 if ((sessionflags & AFB_SESSION_LOA_LE_X1) != 0) {
755 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
756 if (afb_context_check_loa(&xreq->context, loa + 1)) {
757 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
763 if ((sessionflags & AFB_SESSION_RENEW_X1) != 0) {
764 afb_context_refresh(&xreq->context);
766 if ((sessionflags & AFB_SESSION_CLOSE_X1) != 0) {
767 afb_context_change_loa(&xreq->context, 0);
768 afb_context_close(&xreq->context);
775 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
779 if (sessionflags != 0) {
780 if (!afb_context_check(&xreq->context)) {
781 afb_context_close(&xreq->context);
782 afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
788 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_X2);
789 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
790 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
795 if (auth && !afb_auth_check(xreq, auth)) {
796 afb_xreq_reply_f(xreq, NULL, "denied", "authorisation refused");
801 if ((sessionflags & AFB_SESSION_REFRESH_X2) != 0) {
802 afb_context_refresh(&xreq->context);
804 if ((sessionflags & AFB_SESSION_CLOSE_X2) != 0) {
805 afb_context_close(&xreq->context);
811 #if WITH_LEGACY_BINDING_V1
812 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
815 afb_xreq_reply_unknown_verb(xreq);
817 if (!xreq_session_check_apply_v1(xreq, verb->session))
818 verb->callback(xreq_to_req_x1(xreq));
822 #if WITH_LEGACY_BINDING_V2
823 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
826 afb_xreq_reply_unknown_verb(xreq);
828 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
829 verb->callback(xreq_to_req_x1(xreq));
833 void afb_xreq_call_verb_v3(struct afb_xreq *xreq, const struct afb_verb_v3 *verb)
836 afb_xreq_reply_unknown_verb(xreq);
838 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
839 verb->callback(xreq_to_req_x2(xreq));
842 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
844 memset(xreq, 0, sizeof *xreq);
845 xreq->request.itf = &xreq_itf; /* no hook by default */
847 xreq->queryitf = queryitf;
850 void afb_xreq_reply_unknown_api(struct afb_xreq *xreq)
852 afb_xreq_reply_f(xreq, NULL, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
855 void afb_xreq_reply_unknown_verb(struct afb_xreq *xreq)
857 afb_xreq_reply_f(xreq, NULL, "unknown-verb", "verb %s unknown within api %s", xreq->request.called_verb, xreq->request.called_api);
861 static void init_hooking(struct afb_xreq *xreq)
863 afb_hook_init_xreq(xreq);
864 if (xreq->hookflags) {
865 xreq->request.itf = &xreq_hooked_itf; /* unhook the interface */
866 afb_hook_xreq_begin(xreq);
872 * job callback for asynchronous and secured processing of the request.
874 static void process_async(int signum, void *arg)
876 struct afb_xreq *xreq = arg;
877 const struct afb_api_item *api;
880 /* emit the error (assumes that hooking is initialised) */
881 afb_xreq_reply_f(xreq, NULL, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
887 /* invoke api call method to process the request */
888 api = (const struct afb_api_item*)xreq->context.api_key;
889 api->itf->call(api->closure, xreq);
891 /* release the request */
892 afb_xreq_unhooked_unref(xreq);
896 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
897 * The early failure occurs only in function 'afb_xreq_process' where normally,
898 * the hooking is not initialised. So this "early" failure takes care to initialise
899 * the hooking in first.
901 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
911 va_start(args, info);
912 afb_req_x2_reply_v(xreq_to_req_x2(xreq), NULL, status, info, args);
917 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
918 * Errors are reported as request failures.
920 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
922 const struct afb_api_item *api;
923 struct afb_xreq *caller;
925 /* lookup at the api */
926 xreq->apiset = apiset;
927 api = afb_apiset_lookup_started(apiset, xreq->request.called_api, 1);
930 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
932 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.called_api);
935 xreq->context.api_key = api;
937 /* check self locking */
939 caller = xreq->caller;
941 if (((const struct afb_api_item*)caller->context.api_key)->group == api->group) {
942 /* noconcurrency lock detected */
943 ERROR("self-lock detected in call stack for API %s", xreq->request.called_api);
944 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.called_api);
947 caller = caller->caller;
951 /* queue the request job */
952 afb_xreq_unhooked_addref(xreq);
953 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
954 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
955 ERROR("can't process job with threads: %m");
956 early_failure(xreq, "cancelled", "not able to create a job for the task");
957 afb_xreq_unhooked_unref(xreq);
960 afb_xreq_unhooked_unref(xreq);
963 const char *xreq_on_behalf_cred_export(struct afb_xreq *xreq)
965 return xreq->caller ? afb_cred_export(xreq->cred) : NULL;