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