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