Add the function afb_req_get_uid
[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 static int xreq_get_uid_cb(struct afb_request *closure)
603 {
604         struct afb_xreq *xreq = from_request(closure);
605         return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
606 }
607
608 /******************************************************************************/
609
610 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
611 {
612         struct json_object *r = xreq_json_cb(closure);
613         struct afb_xreq *xreq = from_request(closure);
614         return afb_hook_xreq_json(xreq, r);
615 }
616
617 static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name)
618 {
619         struct afb_arg r = xreq_get_cb(closure, name);
620         struct afb_xreq *xreq = from_request(closure);
621         return afb_hook_xreq_get(xreq, name, r);
622 }
623
624 static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
625 {
626         struct afb_xreq *xreq = from_request(closure);
627         afb_hook_xreq_success(xreq, obj, info);
628         xreq_success_cb(closure, obj, info);
629 }
630
631 static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info)
632 {
633         struct afb_xreq *xreq = from_request(closure);
634         afb_hook_xreq_fail(xreq, status, info);
635         xreq_fail_cb(closure, status, info);
636 }
637
638 static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
639 {
640         vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
641 }
642
643 static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
644 {
645         vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
646 }
647
648 static void *xreq_hooked_context_get_cb(struct afb_request *closure)
649 {
650         void *r = xreq_context_get_cb(closure);
651         struct afb_xreq *xreq = from_request(closure);
652         return afb_hook_xreq_context_get(xreq, r);
653 }
654
655 static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
656 {
657         struct afb_xreq *xreq = from_request(closure);
658         afb_hook_xreq_context_set(xreq, value, free_value);
659         xreq_context_set_cb(closure, value, free_value);
660 }
661
662 static struct afb_request *xreq_hooked_addref_cb(struct afb_request *closure)
663 {
664         struct afb_xreq *xreq = from_request(closure);
665         afb_hook_xreq_addref(xreq);
666         return xreq_addref_cb(closure);
667 }
668
669 static void xreq_hooked_unref_cb(struct afb_request *closure)
670 {
671         struct afb_xreq *xreq = from_request(closure);
672         afb_hook_xreq_unref(xreq);
673         xreq_unref_cb(closure);
674 }
675
676 static void xreq_hooked_session_close_cb(struct afb_request *closure)
677 {
678         struct afb_xreq *xreq = from_request(closure);
679         afb_hook_xreq_session_close(xreq);
680         xreq_session_close_cb(closure);
681 }
682
683 static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level)
684 {
685         int r = xreq_session_set_LOA_cb(closure, level);
686         struct afb_xreq *xreq = from_request(closure);
687         return afb_hook_xreq_session_set_LOA(xreq, level, r);
688 }
689
690 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
691 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
692 {
693         return xreq_hooked_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
694 }
695
696 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
697 {
698         int r = xreq_subscribe_eventid_cb(closure, eventid);
699         struct afb_xreq *xreq = from_request(closure);
700         return afb_hook_xreq_subscribe(xreq, eventid, r);
701 }
702
703 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
704 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
705 {
706         return xreq_hooked_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
707 }
708
709 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
710 {
711         int r = xreq_unsubscribe_eventid_cb(closure, eventid);
712         struct afb_xreq *xreq = from_request(closure);
713         return afb_hook_xreq_unsubscribe(xreq, eventid, r);
714 }
715
716 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)
717 {
718         struct afb_xreq *xreq = from_request(closure);
719         struct subcall *sc;
720
721         afb_hook_xreq_subcall(xreq, api, verb, args);
722         sc = subcall_alloc(xreq, api, verb, args);
723         if (sc == NULL) {
724                 if (callback)
725                         callback(cb_closure, 1, afb_msg_json_internal_error());
726                 json_object_put(args);
727         } else {
728                 subcall_hooked(sc, callback, cb_closure);
729         }
730 }
731
732 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)
733 {
734         struct afb_xreq *xreq = from_request(closure);
735         struct subcall *sc;
736
737         afb_hook_xreq_subcall_req(xreq, api, verb, args);
738         sc = subcall_alloc(xreq, api, verb, args);
739         if (sc == NULL) {
740                 if (callback)
741                         callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
742                 json_object_put(args);
743         } else {
744                 subcall_req_hooked(sc, callback, cb_closure);
745         }
746 }
747
748 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)
749 {
750         struct afb_xreq *xreq = from_request(closure);
751         struct subcall *sc;
752
753         afb_hook_xreq_subcall(xreq, api, verb, args);
754         sc = subcall_alloc(xreq, api, verb, args);
755         if (sc == NULL) {
756                 if (callback)
757                         callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq));
758                 json_object_put(args);
759         } else {
760                 subcall_request_hooked(sc, callback, cb_closure);
761         }
762 }
763
764 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
765 {
766         int r;
767         struct afb_xreq *xreq = from_request(closure);
768         afb_hook_xreq_subcallsync(xreq, api, verb, args);
769         r = xreq_subcallsync_cb(closure, api, verb, args, result);
770         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
771 }
772
773 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)
774 {
775         struct afb_xreq *xreq = from_request(closure);
776         va_list ap;
777         va_copy(ap, args);
778         xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
779         afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
780         va_end(ap);
781 }
782
783 static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure)
784 {
785         struct afb_xreq *xreq = from_request(closure);
786         struct afb_stored_req *r = xreq_store_cb(closure);
787         afb_hook_xreq_store(xreq, r);
788         return r;
789 }
790
791 static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission)
792 {
793         struct afb_xreq *xreq = from_request(closure);
794         int r = xreq_has_permission_cb(closure, permission);
795         return afb_hook_xreq_has_permission(xreq, permission, r);
796 }
797
798 static char *xreq_hooked_get_application_id_cb(struct afb_request *closure)
799 {
800         struct afb_xreq *xreq = from_request(closure);
801         char *r = xreq_get_application_id_cb(closure);
802         return afb_hook_xreq_get_application_id(xreq, r);
803 }
804
805 static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
806 {
807         struct afb_xreq *xreq = from_request(closure);
808         void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
809         return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
810 }
811
812 static int xreq_hooked_get_uid_cb(struct afb_request *closure)
813 {
814         struct afb_xreq *xreq = from_request(closure);
815         int r = xreq_get_uid_cb(closure);
816         return afb_hook_xreq_get_uid(xreq, r);
817 }
818
819 /******************************************************************************/
820
821 const struct afb_request_itf xreq_itf = {
822         .json = xreq_json_cb,
823         .get = xreq_get_cb,
824         .success = xreq_success_cb,
825         .fail = xreq_fail_cb,
826         .vsuccess = xreq_vsuccess_cb,
827         .vfail = xreq_vfail_cb,
828         .context_get = xreq_context_get_cb,
829         .context_set = xreq_context_set_cb,
830         .addref = xreq_addref_cb,
831         .unref = xreq_unref_cb,
832         .session_close = xreq_session_close_cb,
833         .session_set_LOA = xreq_session_set_LOA_cb,
834         .subscribe = xreq_subscribe_cb,
835         .unsubscribe = xreq_unsubscribe_cb,
836         .subcall = xreq_subcall_cb,
837         .subcallsync = xreq_subcallsync_cb,
838         .vverbose = xreq_vverbose_cb,
839         .store = xreq_store_cb,
840         .subcall_req = xreq_subcall_req_cb,
841         .has_permission = xreq_has_permission_cb,
842         .get_application_id = xreq_get_application_id_cb,
843         .context_make = xreq_context_make_cb,
844         .subscribe_eventid = xreq_subscribe_eventid_cb,
845         .unsubscribe_eventid = xreq_unsubscribe_eventid_cb,
846         .subcall_request = xreq_subcall_request_cb,
847         .get_uid = xreq_get_uid_cb,
848 };
849
850 const struct afb_request_itf xreq_hooked_itf = {
851         .json = xreq_hooked_json_cb,
852         .get = xreq_hooked_get_cb,
853         .success = xreq_hooked_success_cb,
854         .fail = xreq_hooked_fail_cb,
855         .vsuccess = xreq_hooked_vsuccess_cb,
856         .vfail = xreq_hooked_vfail_cb,
857         .context_get = xreq_hooked_context_get_cb,
858         .context_set = xreq_hooked_context_set_cb,
859         .addref = xreq_hooked_addref_cb,
860         .unref = xreq_hooked_unref_cb,
861         .session_close = xreq_hooked_session_close_cb,
862         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
863         .subscribe = xreq_hooked_subscribe_cb,
864         .unsubscribe = xreq_hooked_unsubscribe_cb,
865         .subcall = xreq_hooked_subcall_cb,
866         .subcallsync = xreq_hooked_subcallsync_cb,
867         .vverbose = xreq_hooked_vverbose_cb,
868         .store = xreq_hooked_store_cb,
869         .subcall_req = xreq_hooked_subcall_req_cb,
870         .has_permission = xreq_hooked_has_permission_cb,
871         .get_application_id = xreq_hooked_get_application_id_cb,
872         .context_make = xreq_hooked_context_make_cb,
873         .subscribe_eventid = xreq_hooked_subscribe_eventid_cb,
874         .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb,
875         .subcall_request = xreq_hooked_subcall_request_cb,
876         .get_uid = xreq_hooked_get_uid_cb,
877 };
878
879 /******************************************************************************/
880
881 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
882 {
883         struct afb_xreq *xreq = (struct afb_xreq *)sreq;
884         if (xreq->hookflags)
885                 afb_hook_xreq_unstore(xreq);
886         return to_req(xreq);
887 }
888
889 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
890 {
891         return afb_request_json(to_request(xreq));
892 }
893
894 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
895 {
896         afb_request_success(to_request(xreq), obj, info);
897 }
898
899 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
900 {
901         va_list args;
902
903         va_start(args, info);
904         afb_request_success_v(to_request(xreq), obj, info, args);
905         va_end(args);
906 }
907
908 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
909 {
910         afb_request_fail(to_request(xreq), status, info);
911 }
912
913 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
914 {
915         va_list args;
916
917         va_start(args, info);
918         afb_request_fail_v(to_request(xreq), status, info, args);
919         va_end(args);
920
921 }
922
923 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
924 {
925         struct json_object *obj = xreq_json_cb(to_request(xreq));
926         const char *result = json_object_to_json_string(obj);
927         if (size != NULL)
928                 *size = strlen(result);
929         return result;
930 }
931
932 void afb_xreq_addref(struct afb_xreq *xreq)
933 {
934         afb_request_addref(to_request(xreq));
935 }
936
937 void afb_xreq_unref(struct afb_xreq *xreq)
938 {
939         afb_request_unref(to_request(xreq));
940 }
941
942 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)
943 {
944         xreq_subcall_request_cb(to_request(xreq), api, verb, args, callback, cb_closure);
945 }
946
947 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)
948 {
949         afb_request_subcall(to_request(xreq), api, verb, args, callback, cb_closure);
950 }
951
952 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
953 {
954         return xreq_subcallsync_cb(to_request(xreq), api, verb, args, result);
955 }
956
957 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
958 {
959         return afb_request_subcall_sync(to_request(xreq), api, verb, args, result);
960 }
961
962 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
963 {
964         int loa;
965
966         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
967                 if (!afb_context_check(&xreq->context)) {
968                         afb_context_close(&xreq->context);
969                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
970                         errno = EINVAL;
971                         return -1;
972                 }
973         }
974
975         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
976                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
977                 if (!afb_context_check_loa(&xreq->context, loa)) {
978                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
979                         errno = EPERM;
980                         return -1;
981                 }
982         }
983
984         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
985                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
986                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
987                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
988                         errno = EPERM;
989                         return -1;
990                 }
991         }
992
993         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
994                 afb_context_refresh(&xreq->context);
995         }
996         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
997                 afb_context_change_loa(&xreq->context, 0);
998                 afb_context_close(&xreq->context);
999         }
1000
1001         return 0;
1002 }
1003
1004 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
1005 {
1006         int loa;
1007
1008         if (sessionflags != 0) {
1009                 if (!afb_context_check(&xreq->context)) {
1010                         afb_context_close(&xreq->context);
1011                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
1012                         errno = EINVAL;
1013                         return -1;
1014                 }
1015         }
1016
1017         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
1018         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
1019                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
1020                 errno = EPERM;
1021                 return -1;
1022         }
1023
1024         if (auth && !afb_auth_check(xreq, auth)) {
1025                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
1026                 errno = EPERM;
1027                 return -1;
1028         }
1029
1030         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
1031                 afb_context_refresh(&xreq->context);
1032         }
1033         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
1034                 afb_context_close(&xreq->context);
1035         }
1036
1037         return 0;
1038 }
1039
1040 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
1041 {
1042         if (!verb)
1043                 afb_xreq_fail_unknown_verb(xreq);
1044         else
1045                 if (!xreq_session_check_apply_v1(xreq, verb->session))
1046                         verb->callback(to_req(xreq));
1047 }
1048
1049 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
1050 {
1051         if (!verb)
1052                 afb_xreq_fail_unknown_verb(xreq);
1053         else
1054                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
1055                         verb->callback(to_req(xreq));
1056 }
1057
1058 void afb_xreq_call_verb_vdyn(struct afb_xreq *xreq, const struct afb_api_dyn_verb *verb)
1059 {
1060         if (!verb)
1061                 afb_xreq_fail_unknown_verb(xreq);
1062         else
1063                 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
1064                         verb->callback(to_request(xreq));
1065 }
1066
1067 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
1068 {
1069         memset(xreq, 0, sizeof *xreq);
1070         xreq->request.itf = &xreq_hooked_itf; /* hook by default */
1071         xreq->refcount = 1;
1072         xreq->queryitf = queryitf;
1073 }
1074
1075 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
1076 {
1077         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1078 }
1079
1080 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
1081 {
1082         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api);
1083 }
1084
1085 static void init_hooking(struct afb_xreq *xreq)
1086 {
1087         afb_hook_init_xreq(xreq);
1088         if (xreq->hookflags)
1089                 afb_hook_xreq_begin(xreq);
1090         else
1091                 xreq->request.itf = &xreq_itf; /* unhook the interface */
1092 }
1093
1094 /**
1095  * job callback for asynchronous and secured processing of the request.
1096  */
1097 static void process_async(int signum, void *arg)
1098 {
1099         struct afb_xreq *xreq = arg;
1100         const struct afb_api *api;
1101
1102         if (signum != 0) {
1103                 /* emit the error (assumes that hooking is initialised) */
1104                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
1105         } else {
1106                 /* init hooking */
1107                 init_hooking(xreq);
1108                 /* invoke api call method to process the reqiest */
1109                 api = (const struct afb_api*)xreq->context.api_key;
1110                 api->itf->call(api->closure, xreq);
1111         }
1112         /* release the request */
1113         afb_xreq_unhooked_unref(xreq);
1114 }
1115
1116 /**
1117  * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
1118  * The early failure occurs only in function 'afb_xreq_process' where normally,
1119  * the hooking is not initialised. So this "early" failure takes care to initialise
1120  * the hooking in first.
1121  */
1122 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1123 {
1124         va_list args;
1125
1126         /* init hooking */
1127         init_hooking(xreq);
1128
1129         /* send error */
1130         va_start(args, info);
1131         afb_request_fail_v(to_request(xreq), status, info, args);
1132         va_end(args);
1133 }
1134
1135 /**
1136  * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1137  * Errors are reported as request failures.
1138  */
1139 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1140 {
1141         const struct afb_api *api;
1142         struct afb_xreq *caller;
1143
1144         /* lookup at the api */
1145         xreq->apiset = apiset;
1146         api = afb_apiset_lookup_started(apiset, xreq->request.api, 1);
1147         if (!api) {
1148                 if (errno == ENOENT)
1149                         early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1150                 else
1151                         early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api);
1152                 goto end;
1153         }
1154         xreq->context.api_key = api;
1155
1156         /* check self locking */
1157         if (api->group) {
1158                 caller = xreq->caller;
1159                 while (caller) {
1160                         if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1161                                 /* noconcurrency lock detected */
1162                                 ERROR("self-lock detected in call stack for API %s", xreq->request.api);
1163                                 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api);
1164                                 goto end;
1165                         }
1166                         caller = caller->caller;
1167                 }
1168         }
1169
1170         /* queue the request job */
1171         afb_xreq_unhooked_addref(xreq);
1172         if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1173                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1174                 ERROR("can't process job with threads: %m");
1175                 early_failure(xreq, "cancelled", "not able to create a job for the task");
1176                 afb_xreq_unhooked_unref(xreq);
1177         }
1178 end:
1179         afb_xreq_unhooked_unref(xreq);
1180 }
1181