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