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