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