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