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