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