Implement afb_req_has_permission
[src/app-framework-binder.git] / src / afb-xreq.c
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
28 #include <afb/afb-binding-v2.h>
29
30 #include "afb-context.h"
31 #include "afb-xreq.h"
32 #include "afb-evt.h"
33 #include "afb-msg-json.h"
34 #include "afb-cred.h"
35 #include "afb-hook.h"
36 #include "afb-api.h"
37 #include "afb-apiset.h"
38 #include "afb-auth.h"
39 #include "jobs.h"
40 #include "verbose.h"
41
42 /******************************************************************************/
43
44 static inline void xreq_addref(struct afb_xreq *xreq)
45 {
46         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
47 }
48
49 static inline void xreq_unref(struct afb_xreq *xreq)
50 {
51         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
52                 if (!xreq->replied)
53                         afb_xreq_fail(xreq, "error", "no reply");
54                 if (xreq->hookflags)
55                         afb_hook_xreq_end(xreq);
56                 xreq->queryitf->unref(xreq);
57         }
58 }
59
60 /******************************************************************************/
61
62 extern const struct afb_req_itf xreq_itf;
63 extern const struct afb_req_itf xreq_hooked_itf;
64
65 static inline struct afb_req to_req(struct afb_xreq *xreq)
66 {
67         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
68 }
69
70 /******************************************************************************/
71
72 struct subcall
73 {
74         struct afb_xreq xreq;
75         struct afb_xreq *caller;
76         void (*callback)(void*, int, struct json_object*);
77         void *closure;
78         union {
79                 struct {
80                         struct jobloop *jobloop;
81                         struct json_object *result;
82                         int status;
83                 };
84                 struct {
85                         union {
86                                 void (*callback)(void*, int, struct json_object*);
87                                 void (*callback2)(void*, int, struct json_object*, struct afb_req);
88                         };
89                         void *closure;
90                 } hooked;
91         };
92 };
93
94 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
95 {
96         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
97
98         return afb_xreq_subscribe(subcall->caller, event);
99 }
100
101 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
102 {
103         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
104
105         return afb_xreq_unsubscribe(subcall->caller, event);
106 }
107
108 static void subcall_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
109 {
110         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
111
112         if (subcall->callback)
113                 subcall->callback(subcall->closure, status, obj);
114         json_object_put(obj);
115 }
116
117 static void subcall_destroy(struct afb_xreq *xreq)
118 {
119         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
120
121         json_object_put(subcall->xreq.json);
122         afb_cred_unref(subcall->xreq.cred);
123         xreq_unref(subcall->caller);
124         free(subcall);
125 }
126
127 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
128         .reply = subcall_reply,
129         .unref = subcall_destroy,
130         .subscribe = subcall_subscribe,
131         .unsubscribe = subcall_unsubscribe
132 };
133
134 static struct subcall *subcall_alloc(
135                 struct afb_xreq *caller,
136                 const char *api,
137                 const char *verb,
138                 struct json_object *args
139 )
140 {
141         struct subcall *subcall;
142         size_t lenapi, lenverb;
143         char *copy;
144
145         lenapi = 1 + strlen(api);
146         lenverb = 1 + strlen(verb);
147         subcall = malloc(lenapi + lenverb + sizeof *subcall);
148         if (!subcall)
149                 ERROR("out of memory");
150         else {
151                 copy = (char*)&subcall[1];
152                 memcpy(copy, api, lenapi);
153                 api = copy;
154                 copy = &copy[lenapi];
155                 memcpy(copy, verb, lenverb);
156                 verb = copy;
157
158                 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
159                 afb_context_subinit(&subcall->xreq.context, &caller->context);
160                 subcall->xreq.cred = afb_cred_addref(caller->cred);
161                 subcall->xreq.json = args;
162                 subcall->xreq.api = api;
163                 subcall->xreq.verb = verb;
164                 subcall->caller = caller;
165                 xreq_addref(caller);
166         }
167         return subcall;
168 }
169
170 static void subcall_process(struct subcall *subcall)
171 {
172         if (subcall->caller->queryitf->subcall) {
173                 subcall->caller->queryitf->subcall(
174                         subcall->caller, subcall->xreq.api, subcall->xreq.verb,
175                         subcall->xreq.json, subcall->callback, subcall->closure);
176                 xreq_unref(&subcall->xreq);
177         } else
178                 afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
179 }
180
181 static void subcall_sync_leave(struct subcall *subcall)
182 {
183         struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
184         if (jobloop)
185                 jobs_leave(jobloop);
186 }
187
188 static void subcall_sync_reply(void *closure, int status, struct json_object *obj)
189 {
190         struct subcall *subcall = closure;
191
192         subcall->status = status;
193         subcall->result = json_object_get(obj);
194         subcall_sync_leave(subcall);
195 }
196
197 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
198 {
199         struct subcall *subcall = closure;
200
201         if (!signum) {
202                 subcall->jobloop = jobloop;
203                 subcall_process(subcall);
204         } else {
205                 subcall->status = -1;
206                 subcall_sync_leave(subcall);
207         }
208 }
209
210 /******************************************************************************/
211
212 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
213 {
214         char *info;
215         if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
216                 info = NULL;
217         fun(first, second, info);
218         free(info);
219 }
220
221 /******************************************************************************/
222
223 static struct json_object *xreq_json_cb(void *closure)
224 {
225         struct afb_xreq *xreq = closure;
226         if (!xreq->json && xreq->queryitf->json)
227                 xreq->json = xreq->queryitf->json(xreq);
228         return xreq->json;
229 }
230
231 static struct afb_arg xreq_get_cb(void *closure, const char *name)
232 {
233         struct afb_xreq *xreq = closure;
234         struct afb_arg arg;
235         struct json_object *object, *value;
236
237         if (xreq->queryitf->get)
238                 arg = xreq->queryitf->get(xreq, name);
239         else {
240                 object = xreq_json_cb(closure);
241                 if (json_object_object_get_ex(object, name, &value)) {
242                         arg.name = name;
243                         arg.value = json_object_get_string(value);
244                 } else {
245                         arg.name = NULL;
246                         arg.value = NULL;
247                 }
248                 arg.path = NULL;
249         }
250         return arg;
251 }
252
253 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
254 {
255         struct afb_xreq *xreq = closure;
256
257         if (xreq->replied) {
258                 ERROR("reply called more than one time!!");
259                 json_object_put(obj);
260         } else {
261                 xreq->replied = 1;
262                 if (xreq->queryitf->success)
263                         xreq->queryitf->success(xreq, obj, info);
264                 else
265                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
266         }
267 }
268
269 static void xreq_fail_cb(void *closure, const char *status, const char *info)
270 {
271         struct afb_xreq *xreq = closure;
272
273         if (xreq->replied) {
274                 ERROR("reply called more than one time!!");
275         } else {
276                 xreq->replied = 1;
277                 if (xreq->queryitf->fail)
278                         xreq->queryitf->fail(xreq, status, info);
279                 else
280                         xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
281         }
282 }
283
284 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
285 {
286         vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
287 }
288
289 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
290 {
291         vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
292 }
293
294 static void *xreq_context_get_cb(void *closure)
295 {
296         struct afb_xreq *xreq = closure;
297         return afb_context_get(&xreq->context);
298 }
299
300 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
301 {
302         struct afb_xreq *xreq = closure;
303         afb_context_set(&xreq->context, value, free_value);
304 }
305
306 static void xreq_addref_cb(void *closure)
307 {
308         struct afb_xreq *xreq = closure;
309         xreq_addref(xreq);
310 }
311
312 static void xreq_unref_cb(void *closure)
313 {
314         struct afb_xreq *xreq = closure;
315         xreq_unref(xreq);
316 }
317
318 static void xreq_session_close_cb(void *closure)
319 {
320         struct afb_xreq *xreq = closure;
321         afb_context_close(&xreq->context);
322 }
323
324 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
325 {
326         struct afb_xreq *xreq = closure;
327         return afb_context_change_loa(&xreq->context, level);
328 }
329
330 static int xreq_subscribe_cb(void *closure, struct afb_event event)
331 {
332         struct afb_xreq *xreq = closure;
333         return afb_xreq_subscribe(xreq, event);
334 }
335
336 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
337 {
338         if (xreq->listener)
339                 return afb_evt_add_watch(xreq->listener, event);
340         if (xreq->queryitf->subscribe)
341                 return xreq->queryitf->subscribe(xreq, event);
342         ERROR("no event listener, subscription impossible");
343         errno = EINVAL;
344         return -1;
345 }
346
347 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
348 {
349         struct afb_xreq *xreq = closure;
350         return afb_xreq_unsubscribe(xreq, event);
351 }
352
353 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
354 {
355         if (xreq->listener)
356                 return afb_evt_remove_watch(xreq->listener, event);
357         if (xreq->queryitf->unsubscribe)
358                 return xreq->queryitf->unsubscribe(xreq, event);
359         ERROR("no event listener, unsubscription impossible");
360         errno = EINVAL;
361         return -1;
362 }
363
364 static void xreq_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
365 {
366         struct afb_xreq *xreq = closure;
367         struct subcall *subcall;
368
369         subcall = subcall_alloc(xreq, api, verb, args);
370         if (subcall == NULL) {
371                 if (callback)
372                         callback(cb_closure, 1, afb_msg_json_internal_error());
373                 json_object_put(args);
374         } else {
375                 subcall->callback = callback;
376                 subcall->closure = cb_closure;
377                 subcall_process(subcall);
378         }
379 }
380
381 static void xreq_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
382 {
383         struct subcall *subcall = closure;
384         subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
385 }
386
387 static void xreq_subcall_req_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
388 {
389         struct afb_xreq *xreq = closure;
390         struct subcall *subcall;
391
392         subcall = subcall_alloc(xreq, api, verb, args);
393         if (subcall == NULL) {
394                 if (callback)
395                         callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
396                 json_object_put(args);
397         } else {
398                 subcall->callback = xreq_subcall_req_reply_cb;
399                 subcall->closure = subcall;
400                 subcall->hooked.callback2 = callback;
401                 subcall->hooked.closure = cb_closure;
402                 subcall_process(subcall);
403         }
404 }
405
406
407 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
408 {
409         int rc;
410         struct subcall *subcall;
411         struct afb_xreq *xreq = closure;
412         struct json_object *resu;
413
414         subcall = subcall_alloc(xreq, api, verb, args);
415         if (!subcall) {
416                 rc = -1;
417                 resu = afb_msg_json_internal_error();
418                 json_object_put(args);
419         } else {
420                 subcall->callback = subcall_sync_reply;
421                 subcall->closure = subcall;
422                 subcall->jobloop = NULL;
423                 subcall->result = NULL;
424                 subcall->status = 0;
425                 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
426                 resu = subcall->result;
427                 if (rc < 0 || subcall->status < 0) {
428                         resu = resu ?: afb_msg_json_internal_error();
429                         rc = -1;
430                 }
431         }
432         if (result)
433                 *result = resu;
434         else
435                 json_object_put(resu);
436         return rc;
437 }
438
439 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
440 {
441         /* TODO: improves the implementation. example: on condition make a list of log messages that will be returned */
442         vverbose(level, file, line, func, fmt, args);
443 }
444
445 static struct afb_stored_req *xreq_store_cb(void *closure)
446 {
447         xreq_addref_cb(closure);
448         return closure;
449 }
450
451 static int xreq_has_permission_cb(void*closure, const char *permission)
452 {
453         struct afb_xreq *xreq = closure;
454         return afb_auth_has_permission(xreq, permission);
455 }
456
457 /******************************************************************************/
458
459 static struct json_object *xreq_hooked_json_cb(void *closure)
460 {
461         struct json_object *r = xreq_json_cb(closure);
462         struct afb_xreq *xreq = closure;
463         return afb_hook_xreq_json(xreq, r);
464 }
465
466 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
467 {
468         struct afb_arg r = xreq_get_cb(closure, name);
469         struct afb_xreq *xreq = closure;
470         return afb_hook_xreq_get(xreq, name, r);
471 }
472
473 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
474 {
475         struct afb_xreq *xreq = closure;
476         afb_hook_xreq_success(xreq, obj, info);
477         xreq_success_cb(closure, obj, info);
478 }
479
480 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
481 {
482         struct afb_xreq *xreq = closure;
483         afb_hook_xreq_fail(xreq, status, info);
484         xreq_fail_cb(closure, status, info);
485 }
486
487 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
488 {
489         vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
490 }
491
492 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
493 {
494         vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
495 }
496
497 static void *xreq_hooked_context_get_cb(void *closure)
498 {
499         void *r = xreq_context_get_cb(closure);
500         struct afb_xreq *xreq = closure;
501         return afb_hook_xreq_context_get(xreq, r);
502 }
503
504 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
505 {
506         struct afb_xreq *xreq = closure;
507         afb_hook_xreq_context_set(xreq, value, free_value);
508         xreq_context_set_cb(closure, value, free_value);
509 }
510
511 static void xreq_hooked_addref_cb(void *closure)
512 {
513         struct afb_xreq *xreq = closure;
514         afb_hook_xreq_addref(xreq);
515         xreq_addref_cb(closure);
516 }
517
518 static void xreq_hooked_unref_cb(void *closure)
519 {
520         struct afb_xreq *xreq = closure;
521         afb_hook_xreq_unref(xreq);
522         xreq_unref_cb(closure);
523 }
524
525 static void xreq_hooked_session_close_cb(void *closure)
526 {
527         struct afb_xreq *xreq = closure;
528         afb_hook_xreq_session_close(xreq);
529         xreq_session_close_cb(closure);
530 }
531
532 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
533 {
534         int r = xreq_session_set_LOA_cb(closure, level);
535         struct afb_xreq *xreq = closure;
536         return afb_hook_xreq_session_set_LOA(xreq, level, r);
537 }
538
539 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
540 {
541         int r = xreq_subscribe_cb(closure, event);
542         struct afb_xreq *xreq = closure;
543         return afb_hook_xreq_subscribe(xreq, event, r);
544 }
545
546 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
547 {
548         int r = xreq_unsubscribe_cb(closure, event);
549         struct afb_xreq *xreq = closure;
550         return afb_hook_xreq_unsubscribe(xreq, event, r);
551 }
552
553 static void xreq_hooked_subcall_reply_cb(void *closure, int status, struct json_object *result)
554 {
555         struct subcall *subcall = closure;
556
557         afb_hook_xreq_subcall_result(subcall->caller, status, result);
558         subcall->hooked.callback(subcall->hooked.closure, status, result);
559 }
560
561 static void xreq_hooked_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
562 {
563         struct afb_xreq *xreq = closure;
564         struct subcall *subcall;
565
566         afb_hook_xreq_subcall(xreq, api, verb, args);
567         subcall = subcall_alloc(xreq, api, verb, args);
568         if (subcall == NULL) {
569                 if (callback)
570                         callback(cb_closure, 1, afb_msg_json_internal_error());
571                 json_object_put(args);
572         } else {
573                 subcall->callback = xreq_hooked_subcall_reply_cb;
574                 subcall->closure = subcall;
575                 subcall->hooked.callback = callback;
576                 subcall->hooked.closure = cb_closure;
577                 subcall_process(subcall);
578         }
579 }
580
581 static void xreq_hooked_subcall_req_reply_cb(void *closure, int status, struct json_object *result)
582 {
583         struct subcall *subcall = closure;
584
585         afb_hook_xreq_subcall_req_result(subcall->caller, status, result);
586         subcall->hooked.callback2(subcall->hooked.closure, status, result, to_req(subcall->caller));
587 }
588
589 static void xreq_hooked_subcall_req_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
590 {
591         struct afb_xreq *xreq = closure;
592         struct subcall *subcall;
593
594         afb_hook_xreq_subcall_req(xreq, api, verb, args);
595         subcall = subcall_alloc(xreq, api, verb, args);
596         if (subcall == NULL) {
597                 if (callback)
598                         callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
599                 json_object_put(args);
600         } else {
601                 subcall->callback = xreq_hooked_subcall_req_reply_cb;
602                 subcall->closure = subcall;
603                 subcall->hooked.callback2 = callback;
604                 subcall->hooked.closure = cb_closure;
605                 subcall_process(subcall);
606         }
607 }
608
609 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
610 {
611         int r;
612         struct afb_xreq *xreq = closure;
613         afb_hook_xreq_subcallsync(xreq, api, verb, args);
614         r = xreq_subcallsync_cb(closure, api, verb, args, result);
615         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
616 }
617
618 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
619 {
620         struct afb_xreq *xreq = closure;
621         va_list ap;
622         va_copy(ap, args);
623         xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
624         afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
625         va_end(ap);
626 }
627
628 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
629 {
630         struct afb_xreq *xreq = closure;
631         struct afb_stored_req *r = xreq_store_cb(closure);
632         afb_hook_xreq_store(xreq, r);
633         return r;
634 }
635
636 static int xreq_hooked_has_permission_cb(void*closure, const char *permission)
637 {
638         struct afb_xreq *xreq = closure;
639         int r = xreq_has_permission_cb(closure, permission);
640         return afb_hook_xreq_has_permission(xreq, permission, r);
641 }
642
643 /******************************************************************************/
644
645 const struct afb_req_itf xreq_itf = {
646         .json = xreq_json_cb,
647         .get = xreq_get_cb,
648         .success = xreq_success_cb,
649         .fail = xreq_fail_cb,
650         .vsuccess = xreq_vsuccess_cb,
651         .vfail = xreq_vfail_cb,
652         .context_get = xreq_context_get_cb,
653         .context_set = xreq_context_set_cb,
654         .addref = xreq_addref_cb,
655         .unref = xreq_unref_cb,
656         .session_close = xreq_session_close_cb,
657         .session_set_LOA = xreq_session_set_LOA_cb,
658         .subscribe = xreq_subscribe_cb,
659         .unsubscribe = xreq_unsubscribe_cb,
660         .subcall = xreq_subcall_cb,
661         .subcallsync = xreq_subcallsync_cb,
662         .vverbose = xreq_vverbose_cb,
663         .store = xreq_store_cb,
664         .subcall_req = xreq_subcall_req_cb,
665         .has_permission = xreq_has_permission_cb
666 };
667
668 const struct afb_req_itf xreq_hooked_itf = {
669         .json = xreq_hooked_json_cb,
670         .get = xreq_hooked_get_cb,
671         .success = xreq_hooked_success_cb,
672         .fail = xreq_hooked_fail_cb,
673         .vsuccess = xreq_hooked_vsuccess_cb,
674         .vfail = xreq_hooked_vfail_cb,
675         .context_get = xreq_hooked_context_get_cb,
676         .context_set = xreq_hooked_context_set_cb,
677         .addref = xreq_hooked_addref_cb,
678         .unref = xreq_hooked_unref_cb,
679         .session_close = xreq_hooked_session_close_cb,
680         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
681         .subscribe = xreq_hooked_subscribe_cb,
682         .unsubscribe = xreq_hooked_unsubscribe_cb,
683         .subcall = xreq_hooked_subcall_cb,
684         .subcallsync = xreq_hooked_subcallsync_cb,
685         .vverbose = xreq_hooked_vverbose_cb,
686         .store = xreq_hooked_store_cb,
687         .subcall_req = xreq_hooked_subcall_req_cb,
688         .has_permission = xreq_hooked_has_permission_cb
689 };
690
691 /******************************************************************************/
692
693 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
694 {
695         struct afb_xreq *xreq = (struct afb_xreq *)sreq;
696         if (xreq->hookflags)
697                 afb_hook_xreq_unstore(xreq);
698         return to_req(xreq);
699 }
700
701 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
702 {
703         return afb_req_json(to_req(xreq));
704 }
705
706 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
707 {
708         afb_req_success(to_req(xreq), obj, info);
709 }
710
711 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
712 {
713         char *message;
714         va_list args;
715         va_start(args, info);
716         if (info == NULL || vasprintf(&message, info, args) < 0)
717                 message = NULL;
718         va_end(args);
719         afb_xreq_success(xreq, obj, message);
720         free(message);
721 }
722
723 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
724 {
725         afb_req_fail(to_req(xreq), status, info);
726 }
727
728 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
729 {
730         char *message;
731         va_list args;
732         va_start(args, info);
733         if (info == NULL || vasprintf(&message, info, args) < 0)
734                 message = NULL;
735         va_end(args);
736         afb_xreq_fail(xreq, status, message);
737         free(message);
738 }
739
740 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
741 {
742         struct json_object *obj = xreq_json_cb(xreq);
743         const char *result = json_object_to_json_string(obj);
744         if (size != NULL)
745                 *size = strlen(result);
746         return result;
747 }
748
749 void afb_xreq_addref(struct afb_xreq *xreq)
750 {
751         afb_req_addref(to_req(xreq));
752 }
753
754 void afb_xreq_unref(struct afb_xreq *xreq)
755 {
756         afb_req_unref(to_req(xreq));
757 }
758
759 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
760 {
761         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
762 }
763
764 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
765 {
766         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
767 }
768
769 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
770 {
771         return xreq_subcallsync_cb(xreq, api, verb, args, result);
772 }
773
774 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
775 {
776         return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
777 }
778
779 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
780 {
781         int loa;
782
783         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
784                 if (!afb_context_check(&xreq->context)) {
785                         afb_context_close(&xreq->context);
786                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
787                         errno = EINVAL;
788                         return -1;
789                 }
790         }
791
792         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
793                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
794                 if (!afb_context_check_loa(&xreq->context, loa)) {
795                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
796                         errno = EPERM;
797                         return -1;
798                 }
799         }
800
801         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
802                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
803                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
804                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
805                         errno = EPERM;
806                         return -1;
807                 }
808         }
809
810         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
811                 afb_context_refresh(&xreq->context);
812         }
813         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
814                 afb_context_change_loa(&xreq->context, 0);
815                 afb_context_close(&xreq->context);
816         }
817
818         return 0;
819 }
820
821 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
822 {
823         int loa;
824
825         if (sessionflags != 0) {
826                 if (!afb_context_check(&xreq->context)) {
827                         afb_context_close(&xreq->context);
828                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
829                         errno = EINVAL;
830                         return -1;
831                 }
832         }
833
834         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
835         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
836                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
837                 errno = EPERM;
838                 return -1;
839         }
840
841         if (auth && !afb_auth_check(xreq, auth)) {
842                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
843                 errno = EPERM;
844                 return -1;
845         }
846
847         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
848                 afb_context_refresh(&xreq->context);
849         }
850         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
851                 afb_context_close(&xreq->context);
852         }
853
854         return 0;
855 }
856
857 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
858 {
859         if (!verb)
860                 afb_xreq_fail_unknown_verb(xreq);
861         else
862                 if (!xreq_session_check_apply_v1(xreq, verb->session))
863                         verb->callback(to_req(xreq));
864 }
865
866 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
867 {
868         if (!verb)
869                 afb_xreq_fail_unknown_verb(xreq);
870         else
871                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
872                         verb->callback(to_req(xreq));
873 }
874
875 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
876 {
877         memset(xreq, 0, sizeof *xreq);
878         xreq->refcount = 1;
879         xreq->queryitf = queryitf;
880 }
881
882 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
883 {
884         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
885 }
886
887 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
888 {
889         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
890 }
891
892 static void process_sync(struct afb_xreq *xreq)
893 {
894         const struct afb_api *api;
895
896         /* init hooking */
897         afb_hook_init_xreq(xreq);
898         if (xreq->hookflags)
899                 afb_hook_xreq_begin(xreq);
900
901         /* search the api */
902         api = (const struct afb_api*)xreq->context.api_key;
903         if (api)
904                 api->itf->call(api->closure, xreq);
905         else {
906                 api = afb_apiset_lookup_started(xreq->apiset, xreq->api, 1);
907                 if (errno == ENOENT)
908                         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
909                 else
910                         afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
911         }
912 }
913
914 static void process_async(int signum, void *arg)
915 {
916         struct afb_xreq *xreq = arg;
917
918         if (signum != 0) {
919                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
920         } else {
921                 process_sync(xreq);
922         }
923         xreq_unref(xreq);
924 }
925
926 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
927 {
928         const struct afb_api *api;
929
930         xreq->apiset = apiset;
931         api = afb_apiset_lookup_started(apiset, xreq->api, 1);
932         xreq->context.api_key = (void*)api;
933
934         xreq_addref(xreq);
935         if (jobs_queue(api && api->noconcurrency ? (void*)api : NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
936                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
937                 ERROR("can't process job with threads: %m");
938                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
939                 xreq_unref(xreq);
940         }
941         xreq_unref(xreq);
942 }
943