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