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