Make addref for request return the pointer
[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 struct afb_request *xreq_addref_cb(struct afb_request *closure)
400 {
401         struct afb_xreq *xreq = from_request(closure);
402         afb_xreq_unhooked_addref(xreq);
403         return closure;
404 }
405
406 static void xreq_unref_cb(struct afb_request *closure)
407 {
408         struct afb_xreq *xreq = from_request(closure);
409         afb_xreq_unhooked_unref(xreq);
410 }
411
412 static void xreq_session_close_cb(struct afb_request *closure)
413 {
414         struct afb_xreq *xreq = from_request(closure);
415         afb_context_close(&xreq->context);
416 }
417
418 static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level)
419 {
420         struct afb_xreq *xreq = from_request(closure);
421         return afb_context_change_loa(&xreq->context, level);
422 }
423
424 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
425 static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event)
426 {
427         return xreq_subscribe_eventid_cb(closure, event.closure);
428 }
429
430 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
431 {
432         struct afb_xreq *xreq = from_request(closure);
433         return afb_xreq_subscribe(xreq, eventid);
434 }
435
436 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
437 {
438         if (xreq->listener)
439                 return afb_evt_add_watch(xreq->listener, eventid);
440         if (xreq->queryitf->subscribe)
441                 return xreq->queryitf->subscribe(xreq, eventid);
442         ERROR("no event listener, subscription impossible");
443         errno = EINVAL;
444         return -1;
445 }
446
447 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
448 static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
449 {
450         return xreq_unsubscribe_eventid_cb(closure, event.closure);
451 }
452
453 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
454 {
455         struct afb_xreq *xreq = from_request(closure);
456         return afb_xreq_unsubscribe(xreq, eventid);
457 }
458
459 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
460 {
461         if (xreq->listener)
462                 return afb_evt_remove_watch(xreq->listener, eventid);
463         if (xreq->queryitf->unsubscribe)
464                 return xreq->queryitf->unsubscribe(xreq, eventid);
465         ERROR("no event listener, unsubscription impossible");
466         errno = EINVAL;
467         return -1;
468 }
469
470 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)
471 {
472         struct afb_xreq *xreq = from_request(closure);
473         struct subcall *sc;
474
475         sc = subcall_alloc(xreq, api, verb, args);
476         if (sc == NULL) {
477                 if (callback)
478                         callback(cb_closure, 1, afb_msg_json_internal_error());
479                 json_object_put(args);
480         } else {
481                 subcall(sc, callback, cb_closure);
482         }
483 }
484
485 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)
486 {
487         struct afb_xreq *xreq = from_request(closure);
488         struct subcall *sc;
489
490         sc = subcall_alloc(xreq, api, verb, args);
491         if (sc == NULL) {
492                 if (callback)
493                         callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
494                 json_object_put(args);
495         } else {
496                 subcall_req(sc, callback, cb_closure);
497         }
498 }
499
500
501 static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
502 {
503         int rc;
504         struct subcall *sc;
505         struct afb_xreq *xreq = from_request(closure);
506         struct json_object *resu;
507
508         sc = subcall_alloc(xreq, api, verb, args);
509         if (!sc) {
510                 rc = -1;
511                 resu = afb_msg_json_internal_error();
512                 json_object_put(args);
513         } else {
514                 rc = subcallsync(sc, &resu);
515         }
516         if (result)
517                 *result = resu;
518         else
519                 json_object_put(resu);
520         return rc;
521 }
522
523 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)
524 {
525         char *p;
526         struct afb_xreq *xreq = from_request(closure);
527
528         if (!fmt || vasprintf(&p, fmt, args) < 0)
529                 vverbose(level, file, line, func, fmt, args);
530         else {
531                 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
532                 free(p);
533         }
534 }
535
536 static struct afb_stored_req *xreq_store_cb(struct afb_request *closure)
537 {
538         xreq_addref_cb(closure);
539         return (struct afb_stored_req*)closure;
540 }
541
542 static int xreq_has_permission_cb(struct afb_request *closure, const char *permission)
543 {
544         struct afb_xreq *xreq = from_request(closure);
545         return afb_auth_has_permission(xreq, permission);
546 }
547
548 static char *xreq_get_application_id_cb(struct afb_request *closure)
549 {
550         struct afb_xreq *xreq = from_request(closure);
551         return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
552 }
553
554 static void *xreq_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
555 {
556         struct afb_xreq *xreq = from_request(closure);
557         return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
558 }
559
560 /******************************************************************************/
561
562 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
563 {
564         struct json_object *r = xreq_json_cb(closure);
565         struct afb_xreq *xreq = from_request(closure);
566         return afb_hook_xreq_json(xreq, r);
567 }
568
569 static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name)
570 {
571         struct afb_arg r = xreq_get_cb(closure, name);
572         struct afb_xreq *xreq = from_request(closure);
573         return afb_hook_xreq_get(xreq, name, r);
574 }
575
576 static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
577 {
578         struct afb_xreq *xreq = from_request(closure);
579         afb_hook_xreq_success(xreq, obj, info);
580         xreq_success_cb(closure, obj, info);
581 }
582
583 static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info)
584 {
585         struct afb_xreq *xreq = from_request(closure);
586         afb_hook_xreq_fail(xreq, status, info);
587         xreq_fail_cb(closure, status, info);
588 }
589
590 static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
591 {
592         vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
593 }
594
595 static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
596 {
597         vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
598 }
599
600 static void *xreq_hooked_context_get_cb(struct afb_request *closure)
601 {
602         void *r = xreq_context_get_cb(closure);
603         struct afb_xreq *xreq = from_request(closure);
604         return afb_hook_xreq_context_get(xreq, r);
605 }
606
607 static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
608 {
609         struct afb_xreq *xreq = from_request(closure);
610         afb_hook_xreq_context_set(xreq, value, free_value);
611         xreq_context_set_cb(closure, value, free_value);
612 }
613
614 static struct afb_request *xreq_hooked_addref_cb(struct afb_request *closure)
615 {
616         struct afb_xreq *xreq = from_request(closure);
617         afb_hook_xreq_addref(xreq);
618         return xreq_addref_cb(closure);
619 }
620
621 static void xreq_hooked_unref_cb(struct afb_request *closure)
622 {
623         struct afb_xreq *xreq = from_request(closure);
624         afb_hook_xreq_unref(xreq);
625         xreq_unref_cb(closure);
626 }
627
628 static void xreq_hooked_session_close_cb(struct afb_request *closure)
629 {
630         struct afb_xreq *xreq = from_request(closure);
631         afb_hook_xreq_session_close(xreq);
632         xreq_session_close_cb(closure);
633 }
634
635 static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level)
636 {
637         int r = xreq_session_set_LOA_cb(closure, level);
638         struct afb_xreq *xreq = from_request(closure);
639         return afb_hook_xreq_session_set_LOA(xreq, level, r);
640 }
641
642 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
643 {
644         int r = xreq_subscribe_cb(closure, event);
645         struct afb_xreq *xreq = from_request(closure);
646         return afb_hook_xreq_subscribe(xreq, event.closure, r);
647 }
648
649 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
650 {
651         int r = xreq_unsubscribe_cb(closure, event);
652         struct afb_xreq *xreq = from_request(closure);
653         return afb_hook_xreq_unsubscribe(xreq, event.closure, r);
654 }
655
656 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)
657 {
658         struct afb_xreq *xreq = from_request(closure);
659         struct subcall *sc;
660
661         afb_hook_xreq_subcall(xreq, api, verb, args);
662         sc = subcall_alloc(xreq, api, verb, args);
663         if (sc == NULL) {
664                 if (callback)
665                         callback(cb_closure, 1, afb_msg_json_internal_error());
666                 json_object_put(args);
667         } else {
668                 subcall_hooked(sc, callback, cb_closure);
669         }
670 }
671
672 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)
673 {
674         struct afb_xreq *xreq = from_request(closure);
675         struct subcall *sc;
676
677         afb_hook_xreq_subcall_req(xreq, api, verb, args);
678         sc = subcall_alloc(xreq, api, verb, args);
679         if (sc == NULL) {
680                 if (callback)
681                         callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
682                 json_object_put(args);
683         } else {
684                 subcall_req_hooked(sc, callback, cb_closure);
685         }
686 }
687
688 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
689 {
690         int r;
691         struct afb_xreq *xreq = from_request(closure);
692         afb_hook_xreq_subcallsync(xreq, api, verb, args);
693         r = xreq_subcallsync_cb(closure, api, verb, args, result);
694         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
695 }
696
697 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)
698 {
699         struct afb_xreq *xreq = from_request(closure);
700         va_list ap;
701         va_copy(ap, args);
702         xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
703         afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
704         va_end(ap);
705 }
706
707 static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure)
708 {
709         struct afb_xreq *xreq = from_request(closure);
710         struct afb_stored_req *r = xreq_store_cb(closure);
711         afb_hook_xreq_store(xreq, r);
712         return r;
713 }
714
715 static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission)
716 {
717         struct afb_xreq *xreq = from_request(closure);
718         int r = xreq_has_permission_cb(closure, permission);
719         return afb_hook_xreq_has_permission(xreq, permission, r);
720 }
721
722 static char *xreq_hooked_get_application_id_cb(struct afb_request *closure)
723 {
724         struct afb_xreq *xreq = from_request(closure);
725         char *r = xreq_get_application_id_cb(closure);
726         return afb_hook_xreq_get_application_id(xreq, r);
727 }
728
729 static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
730 {
731         struct afb_xreq *xreq = from_request(closure);
732         void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
733         return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
734 }
735
736 /******************************************************************************/
737
738 const struct afb_request_itf xreq_itf = {
739         .json = xreq_json_cb,
740         .get = xreq_get_cb,
741         .success = xreq_success_cb,
742         .fail = xreq_fail_cb,
743         .vsuccess = xreq_vsuccess_cb,
744         .vfail = xreq_vfail_cb,
745         .context_get = xreq_context_get_cb,
746         .context_set = xreq_context_set_cb,
747         .addref = xreq_addref_cb,
748         .unref = xreq_unref_cb,
749         .session_close = xreq_session_close_cb,
750         .session_set_LOA = xreq_session_set_LOA_cb,
751         .subscribe = xreq_subscribe_cb,
752         .unsubscribe = xreq_unsubscribe_cb,
753         .subcall = xreq_subcall_cb,
754         .subcallsync = xreq_subcallsync_cb,
755         .vverbose = xreq_vverbose_cb,
756         .store = xreq_store_cb,
757         .subcall_req = xreq_subcall_req_cb,
758         .has_permission = xreq_has_permission_cb,
759         .get_application_id = xreq_get_application_id_cb,
760         .context_make = xreq_context_make_cb
761 };
762
763 const struct afb_request_itf xreq_hooked_itf = {
764         .json = xreq_hooked_json_cb,
765         .get = xreq_hooked_get_cb,
766         .success = xreq_hooked_success_cb,
767         .fail = xreq_hooked_fail_cb,
768         .vsuccess = xreq_hooked_vsuccess_cb,
769         .vfail = xreq_hooked_vfail_cb,
770         .context_get = xreq_hooked_context_get_cb,
771         .context_set = xreq_hooked_context_set_cb,
772         .addref = xreq_hooked_addref_cb,
773         .unref = xreq_hooked_unref_cb,
774         .session_close = xreq_hooked_session_close_cb,
775         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
776         .subscribe = xreq_hooked_subscribe_cb,
777         .unsubscribe = xreq_hooked_unsubscribe_cb,
778         .subcall = xreq_hooked_subcall_cb,
779         .subcallsync = xreq_hooked_subcallsync_cb,
780         .vverbose = xreq_hooked_vverbose_cb,
781         .store = xreq_hooked_store_cb,
782         .subcall_req = xreq_hooked_subcall_req_cb,
783         .has_permission = xreq_hooked_has_permission_cb,
784         .get_application_id = xreq_hooked_get_application_id_cb,
785         .context_make = xreq_hooked_context_make_cb
786 };
787
788 /******************************************************************************/
789
790 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
791 {
792         struct afb_xreq *xreq = (struct afb_xreq *)sreq;
793         if (xreq->hookflags)
794                 afb_hook_xreq_unstore(xreq);
795         return to_req(xreq);
796 }
797
798 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
799 {
800         return afb_request_json(to_request(xreq));
801 }
802
803 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
804 {
805         afb_request_success(to_request(xreq), obj, info);
806 }
807
808 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
809 {
810         va_list args;
811
812         va_start(args, info);
813         afb_request_success_v(to_request(xreq), obj, info, args);
814         va_end(args);
815 }
816
817 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
818 {
819         afb_request_fail(to_request(xreq), status, info);
820 }
821
822 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
823 {
824         va_list args;
825
826         va_start(args, info);
827         afb_request_fail_v(to_request(xreq), status, info, args);
828         va_end(args);
829
830 }
831
832 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
833 {
834         struct json_object *obj = xreq_json_cb(to_request(xreq));
835         const char *result = json_object_to_json_string(obj);
836         if (size != NULL)
837                 *size = strlen(result);
838         return result;
839 }
840
841 void afb_xreq_addref(struct afb_xreq *xreq)
842 {
843         afb_request_addref(to_request(xreq));
844 }
845
846 void afb_xreq_unref(struct afb_xreq *xreq)
847 {
848         afb_request_unref(to_request(xreq));
849 }
850
851 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)
852 {
853         xreq_subcall_cb(to_request(xreq), api, verb, args, callback, cb_closure);
854 }
855
856 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)
857 {
858         afb_request_subcall(to_request(xreq), api, verb, args, callback, cb_closure);
859 }
860
861 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
862 {
863         return xreq_subcallsync_cb(to_request(xreq), api, verb, args, result);
864 }
865
866 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
867 {
868         return afb_request_subcall_sync(to_request(xreq), api, verb, args, result);
869 }
870
871 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
872 {
873         int loa;
874
875         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
876                 if (!afb_context_check(&xreq->context)) {
877                         afb_context_close(&xreq->context);
878                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
879                         errno = EINVAL;
880                         return -1;
881                 }
882         }
883
884         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
885                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
886                 if (!afb_context_check_loa(&xreq->context, loa)) {
887                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
888                         errno = EPERM;
889                         return -1;
890                 }
891         }
892
893         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
894                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
895                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
896                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
897                         errno = EPERM;
898                         return -1;
899                 }
900         }
901
902         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
903                 afb_context_refresh(&xreq->context);
904         }
905         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
906                 afb_context_change_loa(&xreq->context, 0);
907                 afb_context_close(&xreq->context);
908         }
909
910         return 0;
911 }
912
913 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
914 {
915         int loa;
916
917         if (sessionflags != 0) {
918                 if (!afb_context_check(&xreq->context)) {
919                         afb_context_close(&xreq->context);
920                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
921                         errno = EINVAL;
922                         return -1;
923                 }
924         }
925
926         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
927         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
928                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
929                 errno = EPERM;
930                 return -1;
931         }
932
933         if (auth && !afb_auth_check(xreq, auth)) {
934                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
935                 errno = EPERM;
936                 return -1;
937         }
938
939         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
940                 afb_context_refresh(&xreq->context);
941         }
942         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
943                 afb_context_close(&xreq->context);
944         }
945
946         return 0;
947 }
948
949 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
950 {
951         if (!verb)
952                 afb_xreq_fail_unknown_verb(xreq);
953         else
954                 if (!xreq_session_check_apply_v1(xreq, verb->session))
955                         verb->callback(to_req(xreq));
956 }
957
958 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
959 {
960         if (!verb)
961                 afb_xreq_fail_unknown_verb(xreq);
962         else
963                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
964                         verb->callback(to_req(xreq));
965 }
966
967 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
968 {
969         memset(xreq, 0, sizeof *xreq);
970         xreq->request.itf = &xreq_hooked_itf; /* hook by default */
971         xreq->refcount = 1;
972         xreq->queryitf = queryitf;
973 }
974
975 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
976 {
977         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
978 }
979
980 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
981 {
982         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
983 }
984
985 static void init_hooking(struct afb_xreq *xreq)
986 {
987         afb_hook_init_xreq(xreq);
988         if (xreq->hookflags)
989                 afb_hook_xreq_begin(xreq);
990         else
991                 xreq->request.itf = &xreq_itf; /* unhook the interface */
992 }
993
994 /**
995  * job callback for asynchronous and secured processing of the request.
996  */
997 static void process_async(int signum, void *arg)
998 {
999         struct afb_xreq *xreq = arg;
1000         const struct afb_api *api;
1001
1002         if (signum != 0) {
1003                 /* emit the error (assumes that hooking is initialised) */
1004                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
1005         } else {
1006                 /* init hooking */
1007                 init_hooking(xreq);
1008                 /* invoke api call method to process the reqiest */
1009                 api = (const struct afb_api*)xreq->context.api_key;
1010                 api->itf->call(api->closure, xreq);
1011         }
1012         /* release the request */
1013         afb_xreq_unhooked_unref(xreq);
1014 }
1015
1016 /**
1017  * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
1018  * The early failure occurs only in function 'afb_xreq_process' where normally,
1019  * the hooking is not initialised. So this "early" failure takes care to initialise
1020  * the hooking in first.
1021  */
1022 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1023 {
1024         va_list args;
1025
1026         /* init hooking */
1027         init_hooking(xreq);
1028
1029         /* send error */
1030         va_start(args, info);
1031         afb_request_fail_v(to_request(xreq), status, info, args);
1032         va_end(args);
1033 }
1034
1035 /**
1036  * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1037  * Errors are reported as request failures.
1038  */
1039 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1040 {
1041         const struct afb_api *api;
1042         struct afb_xreq *caller;
1043
1044         /* lookup at the api */
1045         xreq->apiset = apiset;
1046         api = afb_apiset_lookup_started(apiset, xreq->api, 1);
1047         if (!api) {
1048                 if (errno == ENOENT)
1049                         early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
1050                 else
1051                         early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
1052                 goto end;
1053         }
1054         xreq->context.api_key = api;
1055
1056         /* check self locking */
1057         if (api->group) {
1058                 caller = xreq->caller;
1059                 while (caller) {
1060                         if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1061                                 /* noconcurrency lock detected */
1062                                 ERROR("self-lock detected in call stack for API %s", xreq->api);
1063                                 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api);
1064                                 goto end;
1065                         }
1066                         caller = caller->caller;
1067                 }
1068         }
1069
1070         /* queue the request job */
1071         afb_xreq_unhooked_addref(xreq);
1072         if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1073                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1074                 ERROR("can't process job with threads: %m");
1075                 early_failure(xreq, "cancelled", "not able to create a job for the task");
1076                 afb_xreq_unhooked_unref(xreq);
1077         }
1078 end:
1079         afb_xreq_unhooked_unref(xreq);
1080 }
1081