api-v3: First draft
[src/app-framework-binder.git] / src / afb-xreq.c
1 /*
2  * Copyright (C) 2017, 2018 "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 #include <stdarg.h>
25
26 #include <json-c/json.h>
27
28 #include <afb/afb-binding-v1.h>
29 #include <afb/afb-binding-v2.h>
30 #include <afb/afb-binding-v3.h>
31 #include <afb/afb-req-x2.h>
32
33 #include "afb-api.h"
34 #include "afb-apiset.h"
35 #include "afb-auth.h"
36 #include "afb-calls.h"
37 #include "afb-context.h"
38 #include "afb-evt.h"
39 #include "afb-cred.h"
40 #include "afb-hook.h"
41 #include "afb-msg-json.h"
42 #include "afb-xreq.h"
43
44 #include "jobs.h"
45 #include "verbose.h"
46
47 /******************************************************************************/
48
49 static void xreq_finalize(struct afb_xreq *xreq)
50 {
51         if (!xreq->replied)
52                 afb_xreq_reply(xreq, NULL, "error", "no reply");
53         if (xreq->hookflags)
54                 afb_hook_xreq_end(xreq);
55         if (xreq->caller)
56                 afb_xreq_unhooked_unref(xreq->caller);
57         xreq->queryitf->unref(xreq);
58 }
59
60 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
61 {
62         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
63 }
64
65 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
66 {
67         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
68                 xreq_finalize(xreq);
69 }
70
71 /******************************************************************************/
72
73 struct json_object *afb_xreq_unhooked_json(struct afb_xreq *xreq)
74 {
75         if (!xreq->json && xreq->queryitf->json)
76                 xreq->json = xreq->queryitf->json(xreq);
77         return xreq->json;
78 }
79
80 static struct json_object *xreq_json_cb(struct afb_req_x2 *closure)
81 {
82         struct afb_xreq *xreq = xreq_from_req_x2(closure);
83         return afb_xreq_unhooked_json(xreq);
84 }
85
86 static struct afb_arg xreq_get_cb(struct afb_req_x2 *closure, const char *name)
87 {
88         struct afb_xreq *xreq = xreq_from_req_x2(closure);
89         struct afb_arg arg;
90         struct json_object *object, *value;
91
92         if (xreq->queryitf->get)
93                 arg = xreq->queryitf->get(xreq, name);
94         else {
95                 object = xreq_json_cb(closure);
96                 if (json_object_object_get_ex(object, name, &value)) {
97                         arg.name = name;
98                         arg.value = json_object_get_string(value);
99                 } else {
100                         arg.name = NULL;
101                         arg.value = NULL;
102                 }
103                 arg.path = NULL;
104         }
105         return arg;
106 }
107
108 static void xreq_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
109 {
110         struct afb_xreq *xreq = xreq_from_req_x2(closure);
111
112         if (xreq->replied) {
113                 ERROR("reply called more than one time!!");
114                 json_object_put(obj);
115         } else {
116                 xreq->replied = 1;
117                 xreq->queryitf->reply(xreq, obj, error, info);
118         }
119 }
120
121 static void xreq_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
122 {
123         char *info;
124         if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
125                 info = NULL;
126         xreq_reply_cb(closure, obj, error, info);
127         free(info);
128 }
129
130 static void xreq_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
131 {
132         xreq_reply_cb(closure, obj, NULL, info);
133 }
134
135 static void xreq_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
136 {
137         xreq_reply_cb(closure, NULL, status, info);
138 }
139
140 static void xreq_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
141 {
142         xreq_vreply_cb(closure, obj, NULL, fmt, args);
143 }
144
145 static void xreq_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
146 {
147         xreq_vreply_cb(closure, NULL, status, fmt, args);
148 }
149
150 static void *xreq_legacy_context_get_cb(struct afb_req_x2 *closure)
151 {
152         struct afb_xreq *xreq = xreq_from_req_x2(closure);
153         return afb_context_get(&xreq->context);
154 }
155
156 static void xreq_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
157 {
158         struct afb_xreq *xreq = xreq_from_req_x2(closure);
159         afb_context_set(&xreq->context, value, free_value);
160 }
161
162 static struct afb_req_x2 *xreq_addref_cb(struct afb_req_x2 *closure)
163 {
164         struct afb_xreq *xreq = xreq_from_req_x2(closure);
165         afb_xreq_unhooked_addref(xreq);
166         return closure;
167 }
168
169 static void xreq_unref_cb(struct afb_req_x2 *closure)
170 {
171         struct afb_xreq *xreq = xreq_from_req_x2(closure);
172         afb_xreq_unhooked_unref(xreq);
173 }
174
175 static void xreq_session_close_cb(struct afb_req_x2 *closure)
176 {
177         struct afb_xreq *xreq = xreq_from_req_x2(closure);
178         afb_context_close(&xreq->context);
179 }
180
181 static int xreq_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
182 {
183         struct afb_xreq *xreq = xreq_from_req_x2(closure);
184         return afb_context_change_loa(&xreq->context, level);
185 }
186
187 static int xreq_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
188 {
189         struct afb_xreq *xreq = xreq_from_req_x2(closure);
190         return afb_xreq_subscribe(xreq, event);
191 }
192
193 static int xreq_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
194 {
195         return xreq_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
196 }
197
198 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
199 {
200         if (xreq->listener)
201                 return afb_evt_event_x2_add_watch(xreq->listener, event);
202         if (xreq->queryitf->subscribe)
203                 return xreq->queryitf->subscribe(xreq, event);
204         ERROR("no event listener, subscription impossible");
205         errno = EINVAL;
206         return -1;
207 }
208
209 static int xreq_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
210 {
211         struct afb_xreq *xreq = xreq_from_req_x2(closure);
212         return afb_xreq_unsubscribe(xreq, event);
213 }
214
215 static int xreq_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
216 {
217         return xreq_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
218 }
219
220 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
221 {
222         if (xreq->listener)
223                 return afb_evt_event_x2_remove_watch(xreq->listener, event);
224         if (xreq->queryitf->unsubscribe)
225                 return xreq->queryitf->unsubscribe(xreq, event);
226         ERROR("no event listener, unsubscription impossible");
227         errno = EINVAL;
228         return -1;
229 }
230
231 static void xreq_legacy_subcall_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
232 {
233         struct afb_xreq *xreq = xreq_from_req_x2(req);
234         return afb_calls_legacy_subcall_v1(xreq, api, verb, args, callback, closure);
235 }
236
237 static void xreq_legacy_subcall_req_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x1), void *closure)
238 {
239         struct afb_xreq *xreq = xreq_from_req_x2(req);
240         return afb_calls_legacy_subcall_v2(xreq, api, verb, args, callback, closure);
241 }
242
243 static void xreq_legacy_subcall_request_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2*), void *closure)
244 {
245         struct afb_xreq *xreq = xreq_from_req_x2(req);
246         return afb_calls_legacy_subcall_v3(xreq, api, verb, args, callback, closure);
247 }
248
249
250 static int xreq_legacy_subcallsync_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
251 {
252         struct afb_xreq *xreq = xreq_from_req_x2(req);
253         return afb_calls_legacy_subcall_sync(xreq, api, verb, args, result);
254 }
255
256 static void xreq_vverbose_cb(struct afb_req_x2 *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
257 {
258         char *p;
259         struct afb_xreq *xreq = xreq_from_req_x2(closure);
260
261         if (!fmt || vasprintf(&p, fmt, args) < 0)
262                 vverbose(level, file, line, func, fmt, args);
263         else {
264                 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.called_api, p);
265                 free(p);
266         }
267 }
268
269 static struct afb_stored_req *xreq_legacy_store_cb(struct afb_req_x2 *closure)
270 {
271         xreq_addref_cb(closure);
272         return (struct afb_stored_req*)closure;
273 }
274
275 static int xreq_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
276 {
277         struct afb_xreq *xreq = xreq_from_req_x2(closure);
278         return afb_auth_has_permission(xreq, permission);
279 }
280
281 static char *xreq_get_application_id_cb(struct afb_req_x2 *closure)
282 {
283         struct afb_xreq *xreq = xreq_from_req_x2(closure);
284         return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
285 }
286
287 static void *xreq_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
288 {
289         struct afb_xreq *xreq = xreq_from_req_x2(closure);
290         return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
291 }
292
293 static int xreq_get_uid_cb(struct afb_req_x2 *closure)
294 {
295         struct afb_xreq *xreq = xreq_from_req_x2(closure);
296         return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
297 }
298
299 static struct json_object *xreq_get_client_info_cb(struct afb_req_x2 *closure)
300 {
301         struct afb_xreq *xreq = xreq_from_req_x2(closure);
302         struct json_object *r = json_object_new_object();
303         if (xreq->cred && xreq->cred->id) {
304                 json_object_object_add(r, "uid", json_object_new_int(xreq->cred->uid));
305                 json_object_object_add(r, "gid", json_object_new_int(xreq->cred->gid));
306                 json_object_object_add(r, "pid", json_object_new_int(xreq->cred->pid));
307                 json_object_object_add(r, "user", json_object_new_string(xreq->cred->user));
308                 json_object_object_add(r, "label", json_object_new_string(xreq->cred->label));
309                 json_object_object_add(r, "id", json_object_new_string(xreq->cred->id));
310         }
311         if (xreq->context.session) {
312                 json_object_object_add(r, "uuid", json_object_new_string(afb_context_uuid(&xreq->context)));
313                 json_object_object_add(r, "LOA", json_object_new_int(afb_context_get_loa(&xreq->context)));
314         }
315         return r;
316 }
317
318 static void xreq_subcall_cb(
319                                 struct afb_req_x2 *req,
320                                 const char *api,
321                                 const char *verb,
322                                 struct json_object *args,
323                                 int flags,
324                                 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
325                                 void *closure)
326 {
327         struct afb_xreq *xreq = xreq_from_req_x2(req);
328         afb_calls_subcall(xreq, api, verb, args, flags, callback, closure);
329 }
330
331 static int xreq_subcallsync_cb(
332                                 struct afb_req_x2 *req,
333                                 const char *api,
334                                 const char *verb,
335                                 struct json_object *args,
336                                 int flags,
337                                 struct json_object **object,
338                                 char **error,
339                                 char **info)
340 {
341         struct afb_xreq *xreq = xreq_from_req_x2(req);
342         return afb_calls_subcall_sync(xreq, api, verb, args, flags, object, error, info);
343 }
344
345 /******************************************************************************/
346
347 static struct json_object *xreq_hooked_json_cb(struct afb_req_x2 *closure)
348 {
349         struct json_object *r = xreq_json_cb(closure);
350         struct afb_xreq *xreq = xreq_from_req_x2(closure);
351         return afb_hook_xreq_json(xreq, r);
352 }
353
354 static struct afb_arg xreq_hooked_get_cb(struct afb_req_x2 *closure, const char *name)
355 {
356         struct afb_arg r = xreq_get_cb(closure, name);
357         struct afb_xreq *xreq = xreq_from_req_x2(closure);
358         return afb_hook_xreq_get(xreq, name, r);
359 }
360
361 static void xreq_hooked_reply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *info)
362 {
363         struct afb_xreq *xreq = xreq_from_req_x2(closure);
364         afb_hook_xreq_reply(xreq, obj, error, info);
365         xreq_reply_cb(closure, obj, error, info);
366 }
367
368 static void xreq_hooked_vreply_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *error, const char *fmt, va_list args)
369 {
370         char *info;
371         if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
372                 info = NULL;
373         xreq_hooked_reply_cb(closure, obj, error, info);
374         free(info);
375 }
376
377 static void xreq_hooked_legacy_success_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *info)
378 {
379         xreq_hooked_reply_cb(closure, obj, NULL, info);
380 }
381
382 static void xreq_hooked_legacy_fail_cb(struct afb_req_x2 *closure, const char *status, const char *info)
383 {
384         xreq_hooked_reply_cb(closure, NULL, status, info);
385 }
386
387 static void xreq_hooked_legacy_vsuccess_cb(struct afb_req_x2 *closure, struct json_object *obj, const char *fmt, va_list args)
388 {
389         xreq_hooked_vreply_cb(closure, obj, NULL, fmt, args);
390 }
391
392 static void xreq_hooked_legacy_vfail_cb(struct afb_req_x2 *closure, const char *status, const char *fmt, va_list args)
393 {
394         xreq_hooked_vreply_cb(closure, NULL, status, fmt, args);
395 }
396
397 static void *xreq_hooked_legacy_context_get_cb(struct afb_req_x2 *closure)
398 {
399         void *r = xreq_legacy_context_get_cb(closure);
400         struct afb_xreq *xreq = xreq_from_req_x2(closure);
401         return afb_hook_xreq_legacy_context_get(xreq, r);
402 }
403
404 static void xreq_hooked_legacy_context_set_cb(struct afb_req_x2 *closure, void *value, void (*free_value)(void*))
405 {
406         struct afb_xreq *xreq = xreq_from_req_x2(closure);
407         afb_hook_xreq_legacy_context_set(xreq, value, free_value);
408         xreq_legacy_context_set_cb(closure, value, free_value);
409 }
410
411 static struct afb_req_x2 *xreq_hooked_addref_cb(struct afb_req_x2 *closure)
412 {
413         struct afb_xreq *xreq = xreq_from_req_x2(closure);
414         afb_hook_xreq_addref(xreq);
415         return xreq_addref_cb(closure);
416 }
417
418 static void xreq_hooked_unref_cb(struct afb_req_x2 *closure)
419 {
420         struct afb_xreq *xreq = xreq_from_req_x2(closure);
421         afb_hook_xreq_unref(xreq);
422         xreq_unref_cb(closure);
423 }
424
425 static void xreq_hooked_session_close_cb(struct afb_req_x2 *closure)
426 {
427         struct afb_xreq *xreq = xreq_from_req_x2(closure);
428         afb_hook_xreq_session_close(xreq);
429         xreq_session_close_cb(closure);
430 }
431
432 static int xreq_hooked_session_set_LOA_cb(struct afb_req_x2 *closure, unsigned level)
433 {
434         int r = xreq_session_set_LOA_cb(closure, level);
435         struct afb_xreq *xreq = xreq_from_req_x2(closure);
436         return afb_hook_xreq_session_set_LOA(xreq, level, r);
437 }
438
439 static int xreq_hooked_subscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
440 {
441         int r = xreq_subscribe_event_x2_cb(closure, event);
442         struct afb_xreq *xreq = xreq_from_req_x2(closure);
443         return afb_hook_xreq_subscribe(xreq, event, r);
444 }
445
446 static int xreq_hooked_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
447 {
448         return xreq_hooked_subscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
449 }
450
451 static int xreq_hooked_unsubscribe_event_x2_cb(struct afb_req_x2 *closure, struct afb_event_x2 *event)
452 {
453         int r = xreq_unsubscribe_event_x2_cb(closure, event);
454         struct afb_xreq *xreq = xreq_from_req_x2(closure);
455         return afb_hook_xreq_unsubscribe(xreq, event, r);
456 }
457
458 static int xreq_hooked_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struct afb_event_x1 event)
459 {
460         return xreq_hooked_unsubscribe_event_x2_cb(closure, afb_event_x1_to_event_x2(event));
461 }
462
463 static void xreq_hooked_legacy_subcall_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
464 {
465         struct afb_xreq *xreq = xreq_from_req_x2(req);
466         afb_calls_legacy_hooked_subcall_v1(xreq, api, verb, args, callback, closure);
467 }
468
469 static void xreq_hooked_legacy_subcall_req_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x1), void *closure)
470 {
471         struct afb_xreq *xreq = xreq_from_req_x2(req);
472         afb_calls_legacy_hooked_subcall_v2(xreq, api, verb, args, callback, closure);
473 }
474
475 static void xreq_hooked_legacy_subcall_request_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *closure)
476 {
477         struct afb_xreq *xreq = xreq_from_req_x2(req);
478         afb_calls_legacy_hooked_subcall_v3(xreq, api, verb, args, callback, closure);
479 }
480
481 static int xreq_hooked_legacy_subcallsync_cb(struct afb_req_x2 *req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
482 {
483         struct afb_xreq *xreq = xreq_from_req_x2(req);
484         return afb_calls_legacy_hooked_subcall_sync(xreq, api, verb, args, result);
485 }
486
487 static void xreq_hooked_vverbose_cb(struct afb_req_x2 *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
488 {
489         struct afb_xreq *xreq = xreq_from_req_x2(closure);
490         va_list ap;
491         va_copy(ap, args);
492         xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
493         afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
494         va_end(ap);
495 }
496
497 static struct afb_stored_req *xreq_hooked_legacy_store_cb(struct afb_req_x2 *closure)
498 {
499         struct afb_xreq *xreq = xreq_from_req_x2(closure);
500         struct afb_stored_req *r = xreq_legacy_store_cb(closure);
501         afb_hook_xreq_legacy_store(xreq, r);
502         return r;
503 }
504
505 static int xreq_hooked_has_permission_cb(struct afb_req_x2 *closure, const char *permission)
506 {
507         struct afb_xreq *xreq = xreq_from_req_x2(closure);
508         int r = xreq_has_permission_cb(closure, permission);
509         return afb_hook_xreq_has_permission(xreq, permission, r);
510 }
511
512 static char *xreq_hooked_get_application_id_cb(struct afb_req_x2 *closure)
513 {
514         struct afb_xreq *xreq = xreq_from_req_x2(closure);
515         char *r = xreq_get_application_id_cb(closure);
516         return afb_hook_xreq_get_application_id(xreq, r);
517 }
518
519 static void *xreq_hooked_context_make_cb(struct afb_req_x2 *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
520 {
521         struct afb_xreq *xreq = xreq_from_req_x2(closure);
522         void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
523         return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
524 }
525
526 static int xreq_hooked_get_uid_cb(struct afb_req_x2 *closure)
527 {
528         struct afb_xreq *xreq = xreq_from_req_x2(closure);
529         int r = xreq_get_uid_cb(closure);
530         return afb_hook_xreq_get_uid(xreq, r);
531 }
532
533 static struct json_object *xreq_hooked_get_client_info_cb(struct afb_req_x2 *closure)
534 {
535         struct afb_xreq *xreq = xreq_from_req_x2(closure);
536         struct json_object *r = xreq_get_client_info_cb(closure);
537         return afb_hook_xreq_get_client_info(xreq, r);
538 }
539
540 static void xreq_hooked_subcall_cb(
541                                 struct afb_req_x2 *req,
542                                 const char *api,
543                                 const char *verb,
544                                 struct json_object *args,
545                                 int flags,
546                                 void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
547                                 void *closure)
548 {
549         struct afb_xreq *xreq = xreq_from_req_x2(req);
550         afb_calls_hooked_subcall(xreq, api, verb, args, flags, callback, closure);
551 }
552
553 static int xreq_hooked_subcallsync_cb(
554                                 struct afb_req_x2 *req,
555                                 const char *api,
556                                 const char *verb,
557                                 struct json_object *args,
558                                 int flags,
559                                 struct json_object **object,
560                                 char **error,
561                                 char **info)
562 {
563         struct afb_xreq *xreq = xreq_from_req_x2(req);
564         return afb_calls_hooked_subcall_sync(xreq, api, verb, args, flags, object, error, info);
565 }
566
567 /******************************************************************************/
568
569 const struct afb_req_x2_itf xreq_itf = {
570         .json = xreq_json_cb,
571         .get = xreq_get_cb,
572         .legacy_success = xreq_legacy_success_cb,
573         .legacy_fail = xreq_legacy_fail_cb,
574         .legacy_vsuccess = xreq_legacy_vsuccess_cb,
575         .legacy_vfail = xreq_legacy_vfail_cb,
576         .legacy_context_get = xreq_legacy_context_get_cb,
577         .legacy_context_set = xreq_legacy_context_set_cb,
578         .addref = xreq_addref_cb,
579         .unref = xreq_unref_cb,
580         .session_close = xreq_session_close_cb,
581         .session_set_LOA = xreq_session_set_LOA_cb,
582         .legacy_subscribe_event_x1 = xreq_legacy_subscribe_event_x1_cb,
583         .legacy_unsubscribe_event_x1 = xreq_legacy_unsubscribe_event_x1_cb,
584         .legacy_subcall = xreq_legacy_subcall_cb,
585         .legacy_subcallsync = xreq_legacy_subcallsync_cb,
586         .vverbose = xreq_vverbose_cb,
587         .legacy_store_req = xreq_legacy_store_cb,
588         .legacy_subcall_req = xreq_legacy_subcall_req_cb,
589         .has_permission = xreq_has_permission_cb,
590         .get_application_id = xreq_get_application_id_cb,
591         .context_make = xreq_context_make_cb,
592         .subscribe_event_x2 = xreq_subscribe_event_x2_cb,
593         .unsubscribe_event_x2 = xreq_unsubscribe_event_x2_cb,
594         .legacy_subcall_request = xreq_legacy_subcall_request_cb,
595         .get_uid = xreq_get_uid_cb,
596         .reply = xreq_reply_cb,
597         .vreply = xreq_vreply_cb,
598         .get_client_info = xreq_get_client_info_cb,
599         .subcall = xreq_subcall_cb,
600         .subcallsync = xreq_subcallsync_cb,
601 };
602
603 const struct afb_req_x2_itf xreq_hooked_itf = {
604         .json = xreq_hooked_json_cb,
605         .get = xreq_hooked_get_cb,
606         .legacy_success = xreq_hooked_legacy_success_cb,
607         .legacy_fail = xreq_hooked_legacy_fail_cb,
608         .legacy_vsuccess = xreq_hooked_legacy_vsuccess_cb,
609         .legacy_vfail = xreq_hooked_legacy_vfail_cb,
610         .legacy_context_get = xreq_hooked_legacy_context_get_cb,
611         .legacy_context_set = xreq_hooked_legacy_context_set_cb,
612         .addref = xreq_hooked_addref_cb,
613         .unref = xreq_hooked_unref_cb,
614         .session_close = xreq_hooked_session_close_cb,
615         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
616         .legacy_subscribe_event_x1 = xreq_hooked_legacy_subscribe_event_x1_cb,
617         .legacy_unsubscribe_event_x1 = xreq_hooked_legacy_unsubscribe_event_x1_cb,
618         .legacy_subcall = xreq_hooked_legacy_subcall_cb,
619         .legacy_subcallsync = xreq_hooked_legacy_subcallsync_cb,
620         .vverbose = xreq_hooked_vverbose_cb,
621         .legacy_store_req = xreq_hooked_legacy_store_cb,
622         .legacy_subcall_req = xreq_hooked_legacy_subcall_req_cb,
623         .has_permission = xreq_hooked_has_permission_cb,
624         .get_application_id = xreq_hooked_get_application_id_cb,
625         .context_make = xreq_hooked_context_make_cb,
626         .subscribe_event_x2 = xreq_hooked_subscribe_event_x2_cb,
627         .unsubscribe_event_x2 = xreq_hooked_unsubscribe_event_x2_cb,
628         .legacy_subcall_request = xreq_hooked_legacy_subcall_request_cb,
629         .get_uid = xreq_hooked_get_uid_cb,
630         .reply = xreq_hooked_reply_cb,
631         .vreply = xreq_hooked_vreply_cb,
632         .get_client_info = xreq_hooked_get_client_info_cb,
633         .subcall = xreq_hooked_subcall_cb,
634         .subcallsync = xreq_hooked_subcallsync_cb,
635 };
636
637 /******************************************************************************/
638
639 struct afb_req_x1 afb_xreq_unstore(struct afb_stored_req *sreq)
640 {
641         struct afb_xreq *xreq = (struct afb_xreq *)sreq;
642         if (xreq->hookflags)
643                 afb_hook_xreq_legacy_unstore(xreq);
644         return xreq_to_req_x1(xreq);
645 }
646
647 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
648 {
649         return afb_req_x2_json(xreq_to_req_x2(xreq));
650 }
651
652 void afb_xreq_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
653 {
654         afb_req_x2_reply(xreq_to_req_x2(xreq), obj, error, info);
655 }
656
657 void afb_xreq_reply_f(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info, ...)
658 {
659         va_list args;
660
661         va_start(args, info);
662         afb_req_x2_reply_v(xreq_to_req_x2(xreq), obj, error, info, args);
663         va_end(args);
664 }
665
666 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
667 {
668         struct json_object *obj = xreq_json_cb(xreq_to_req_x2(xreq));
669         const char *result = json_object_to_json_string(obj);
670         if (size != NULL)
671                 *size = strlen(result);
672         return result;
673 }
674
675 void afb_xreq_addref(struct afb_xreq *xreq)
676 {
677         afb_req_x2_addref(xreq_to_req_x2(xreq));
678 }
679
680 void afb_xreq_unref(struct afb_xreq *xreq)
681 {
682         afb_req_x2_unref(xreq_to_req_x2(xreq));
683 }
684
685 void afb_xreq_unhooked_legacy_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *cb_closure)
686 {
687         xreq_legacy_subcall_request_cb(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
688 }
689
690 void afb_xreq_legacy_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *), void *cb_closure)
691 {
692         afb_req_x2_subcall_legacy(xreq_to_req_x2(xreq), api, verb, args, callback, cb_closure);
693 }
694
695 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags, void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_req_x2 *), void *closure)
696 {
697         xreq_subcall_cb(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
698 }
699
700 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, int flags, void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_req_x2 *), void *closure)
701 {
702         afb_req_x2_subcall(xreq_to_req_x2(xreq), api, verb, args, flags, callback, closure);
703 }
704
705 int afb_xreq_unhooked_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
706 {
707         return xreq_legacy_subcallsync_cb(xreq_to_req_x2(xreq), api, verb, args, result);
708 }
709
710 int afb_xreq_legacy_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
711 {
712         return afb_req_x2_subcall_sync_legacy(xreq_to_req_x2(xreq), api, verb, args, result);
713 }
714
715 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
716 {
717         int loa;
718
719         if ((sessionflags & (AFB_SESSION_CLOSE_X1|AFB_SESSION_RENEW_X1|AFB_SESSION_CHECK_X1|AFB_SESSION_LOA_EQ_X1)) != 0) {
720                 if (!afb_context_check(&xreq->context)) {
721                         afb_context_close(&xreq->context);
722                         afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
723                         errno = EINVAL;
724                         return -1;
725                 }
726         }
727
728         if ((sessionflags & AFB_SESSION_LOA_GE_X1) != 0) {
729                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
730                 if (!afb_context_check_loa(&xreq->context, loa)) {
731                         afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
732                         errno = EPERM;
733                         return -1;
734                 }
735         }
736
737         if ((sessionflags & AFB_SESSION_LOA_LE_X1) != 0) {
738                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1;
739                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
740                         afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
741                         errno = EPERM;
742                         return -1;
743                 }
744         }
745
746         if ((sessionflags & AFB_SESSION_RENEW_X1) != 0) {
747                 afb_context_refresh(&xreq->context);
748         }
749         if ((sessionflags & AFB_SESSION_CLOSE_X1) != 0) {
750                 afb_context_change_loa(&xreq->context, 0);
751                 afb_context_close(&xreq->context);
752         }
753
754         return 0;
755 }
756
757 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
758 {
759         int loa;
760
761         if (sessionflags != 0) {
762                 if (!afb_context_check(&xreq->context)) {
763                         afb_context_close(&xreq->context);
764                         afb_xreq_reply_f(xreq, NULL, "denied", "invalid token's identity");
765                         errno = EINVAL;
766                         return -1;
767                 }
768         }
769
770         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_X2);
771         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
772                 afb_xreq_reply_f(xreq, NULL, "denied", "invalid LOA");
773                 errno = EPERM;
774                 return -1;
775         }
776
777         if (auth && !afb_auth_check(xreq, auth)) {
778                 afb_xreq_reply_f(xreq, NULL, "denied", "authorisation refused");
779                 errno = EPERM;
780                 return -1;
781         }
782
783         if ((sessionflags & AFB_SESSION_REFRESH_X2) != 0) {
784                 afb_context_refresh(&xreq->context);
785         }
786         if ((sessionflags & AFB_SESSION_CLOSE_X2) != 0) {
787                 afb_context_close(&xreq->context);
788         }
789
790         return 0;
791 }
792
793 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
794 {
795         if (!verb)
796                 afb_xreq_reply_unknown_verb(xreq);
797         else
798                 if (!xreq_session_check_apply_v1(xreq, verb->session))
799                         verb->callback(xreq_to_req_x1(xreq));
800 }
801
802 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
803 {
804         if (!verb)
805                 afb_xreq_reply_unknown_verb(xreq);
806         else
807                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
808                         verb->callback(xreq_to_req_x1(xreq));
809 }
810
811 void afb_xreq_call_verb_v3(struct afb_xreq *xreq, const struct afb_verb_v3 *verb)
812 {
813         if (!verb)
814                 afb_xreq_reply_unknown_verb(xreq);
815         else
816                 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
817                         verb->callback(xreq_to_req_x2(xreq));
818 }
819
820 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
821 {
822         memset(xreq, 0, sizeof *xreq);
823         xreq->request.itf = &xreq_itf; /* no hook by default */
824         xreq->refcount = 1;
825         xreq->queryitf = queryitf;
826 }
827
828 void afb_xreq_reply_unknown_api(struct afb_xreq *xreq)
829 {
830         afb_xreq_reply_f(xreq, NULL, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
831 }
832
833 void afb_xreq_reply_unknown_verb(struct afb_xreq *xreq)
834 {
835         afb_xreq_reply_f(xreq, NULL, "unknown-verb", "verb %s unknown within api %s", xreq->request.called_verb, xreq->request.called_api);
836 }
837
838 static void init_hooking(struct afb_xreq *xreq)
839 {
840         afb_hook_init_xreq(xreq);
841         if (xreq->hookflags) {
842                 xreq->request.itf = &xreq_hooked_itf; /* unhook the interface */
843                 afb_hook_xreq_begin(xreq);
844         }
845 }
846
847 /**
848  * job callback for asynchronous and secured processing of the request.
849  */
850 static void process_async(int signum, void *arg)
851 {
852         struct afb_xreq *xreq = arg;
853         const struct afb_api_item *api;
854
855         if (signum != 0) {
856                 /* emit the error (assumes that hooking is initialised) */
857                 afb_xreq_reply_f(xreq, NULL, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
858         } else {
859                 /* init hooking */
860                 init_hooking(xreq);
861                 /* invoke api call method to process the request */
862                 api = (const struct afb_api_item*)xreq->context.api_key;
863                 api->itf->call(api->closure, xreq);
864         }
865         /* release the request */
866         afb_xreq_unhooked_unref(xreq);
867 }
868
869 /**
870  * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
871  * The early failure occurs only in function 'afb_xreq_process' where normally,
872  * the hooking is not initialised. So this "early" failure takes care to initialise
873  * the hooking in first.
874  */
875 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
876 {
877         va_list args;
878
879         /* init hooking */
880         init_hooking(xreq);
881
882         /* send error */
883         va_start(args, info);
884         afb_req_x2_reply_v(xreq_to_req_x2(xreq), NULL, status, info, args);
885         va_end(args);
886 }
887
888 /**
889  * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
890  * Errors are reported as request failures.
891  */
892 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
893 {
894         const struct afb_api_item *api;
895         struct afb_xreq *caller;
896
897         /* lookup at the api */
898         xreq->apiset = apiset;
899         api = afb_apiset_lookup_started(apiset, xreq->request.called_api, 1);
900         if (!api) {
901                 if (errno == ENOENT)
902                         early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.called_api, xreq->request.called_verb);
903                 else
904                         early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.called_api);
905                 goto end;
906         }
907         xreq->context.api_key = api;
908
909         /* check self locking */
910         if (api->group) {
911                 caller = xreq->caller;
912                 while (caller) {
913                         if (((const struct afb_api_item*)caller->context.api_key)->group == api->group) {
914                                 /* noconcurrency lock detected */
915                                 ERROR("self-lock detected in call stack for API %s", xreq->request.called_api);
916                                 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.called_api);
917                                 goto end;
918                         }
919                         caller = caller->caller;
920                 }
921         }
922
923         /* queue the request job */
924         afb_xreq_unhooked_addref(xreq);
925         if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
926                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
927                 ERROR("can't process job with threads: %m");
928                 early_failure(xreq, "cancelled", "not able to create a job for the task");
929                 afb_xreq_unhooked_unref(xreq);
930         }
931 end:
932         afb_xreq_unhooked_unref(xreq);
933 }
934
935 const char *xreq_on_behalf_cred_export(struct afb_xreq *xreq)
936 {
937         return xreq->caller ? afb_cred_export(xreq->cred) : NULL;
938 }
939