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