xreq: export a function to get req
[src/app-framework-binder.git] / src / afb-xreq.c
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <json-c/json.h>
26 #include <afb/afb-binding-v1.h>
27 #include <afb/afb-binding-v2.h>
28 #include <afb/afb-request.h>
29
30 #include "afb-context.h"
31 #include "afb-xreq.h"
32 #include "afb-evt.h"
33 #include "afb-msg-json.h"
34 #include "afb-cred.h"
35 #include "afb-hook.h"
36 #include "afb-api.h"
37 #include "afb-api-dyn.h"
38 #include "afb-apiset.h"
39 #include "afb-auth.h"
40 #include "jobs.h"
41 #include "verbose.h"
42
43 /******************************************************************************/
44
45 static void xreq_finalize(struct afb_xreq *xreq)
46 {
47         if (!xreq->replied)
48                 afb_xreq_fail(xreq, "error", "no reply");
49         if (xreq->hookflags)
50                 afb_hook_xreq_end(xreq);
51         if (xreq->caller)
52                 afb_xreq_unhooked_unref(xreq->caller);
53         xreq->queryitf->unref(xreq);
54 }
55
56 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
57 {
58         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
59 }
60
61 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
62 {
63         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
64                 xreq_finalize(xreq);
65 }
66
67 /******************************************************************************/
68
69 struct subcall
70 {
71         struct afb_xreq xreq;
72
73         void (*completion)(struct subcall*, int, struct json_object*);
74
75         union {
76                 struct {
77                         struct jobloop *jobloop;
78                         struct json_object *result;
79                         int status;
80                 };
81                 struct {
82                         union {
83                                 void (*callback)(void*, int, struct json_object*);
84                                 void (*callback_req)(void*, int, struct json_object*, struct afb_req);
85                                 void (*callback_request)(void*, int, struct json_object*, struct afb_request*);
86                         };
87                         void *closure;
88                 };
89         };
90 };
91
92 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
93 {
94         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
95
96         return afb_xreq_subscribe(subcall->xreq.caller, eventid);
97 }
98
99 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
100 {
101         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
102
103         return afb_xreq_unsubscribe(subcall->xreq.caller, eventid);
104 }
105
106 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
107 {
108         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
109
110         subcall->completion(subcall, status, result);
111         json_object_put(result);
112         afb_xreq_unhooked_unref(&subcall->xreq);
113 }
114
115 static void subcall_destroy_cb(struct afb_xreq *xreq)
116 {
117         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
118
119         json_object_put(subcall->xreq.json);
120         afb_cred_unref(subcall->xreq.cred);
121         free(subcall);
122 }
123
124 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
125         .reply = subcall_reply_cb,
126         .unref = subcall_destroy_cb,
127         .subscribe = subcall_subscribe_cb,
128         .unsubscribe = subcall_unsubscribe_cb
129 };
130
131 static struct subcall *subcall_alloc(
132                 struct afb_xreq *caller,
133                 const char *api,
134                 const char *verb,
135                 struct json_object *args
136 )
137 {
138         struct subcall *subcall;
139         size_t lenapi, lenverb;
140         char *copy;
141
142         lenapi = 1 + strlen(api);
143         lenverb = 1 + strlen(verb);
144         subcall = malloc(lenapi + lenverb + sizeof *subcall);
145         if (!subcall)
146                 ERROR("out of memory");
147         else {
148                 copy = (char*)&subcall[1];
149                 memcpy(copy, api, lenapi);
150                 api = copy;
151                 copy = &copy[lenapi];
152                 memcpy(copy, verb, lenverb);
153                 verb = copy;
154
155                 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
156                 afb_context_subinit(&subcall->xreq.context, &caller->context);
157                 subcall->xreq.cred = afb_cred_addref(caller->cred);
158                 subcall->xreq.json = args;
159                 subcall->xreq.request.api = api;
160                 subcall->xreq.request.verb = verb;
161                 subcall->xreq.caller = caller;
162                 afb_xreq_unhooked_addref(caller);
163         }
164         return subcall;
165 }
166
167
168 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
169 {
170         subcall->callback(subcall->closure, status, result);
171 }
172
173 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
174 {
175         subcall->callback_req(subcall->closure, status, result, xreq_to_req(subcall->xreq.caller));
176 }
177
178 static void subcall_request_on_reply(struct subcall *subcall, int status, struct json_object *result)
179 {
180         subcall->callback_request(subcall->closure, status, result, xreq_to_request(subcall->xreq.caller));
181 }
182
183 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
184 {
185         afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
186         subcall_on_reply(subcall, status, result);
187 }
188
189 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
190 {
191         afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
192         subcall_req_on_reply(subcall, status, result);
193 }
194
195 static void subcall_request_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
196 {
197         afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
198         subcall_request_on_reply(subcall, status, result);
199 }
200
201 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
202 {
203         struct afb_xreq *xreq = closure;
204
205         if (xreq->replied) {
206                 ERROR("subcall replied more than one time!!");
207                 json_object_put(result);
208         } else {
209                 xreq->replied = 1;
210                 subcall_reply_cb(xreq, status, result);
211         }
212 }
213
214 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
215 {
216         subcall->completion = completion;
217         if (subcall->xreq.caller->queryitf->subcall) {
218                 subcall->xreq.caller->queryitf->subcall(
219                         subcall->xreq.caller, subcall->xreq.request.api, subcall->xreq.request.verb,
220                         subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
221         } else {
222                 afb_xreq_unhooked_addref(&subcall->xreq);
223                 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
224         }
225 }
226
227 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
228 {
229         subcall->callback = callback;
230         subcall->closure = cb_closure;
231         subcall_process(subcall, subcall_on_reply);
232 }
233
234 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
235 {
236         subcall->callback_req = callback;
237         subcall->closure = cb_closure;
238         subcall_process(subcall, subcall_req_on_reply);
239 }
240
241 static void subcall_request(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
242 {
243         subcall->callback_request = callback;
244         subcall->closure = cb_closure;
245         subcall_process(subcall, subcall_request_on_reply);
246 }
247
248 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
249 {
250         subcall->callback = callback;
251         subcall->closure = cb_closure;
252         subcall_process(subcall, subcall_hooked_on_reply);
253 }
254
255 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
256 {
257         subcall->callback_req = callback;
258         subcall->closure = cb_closure;
259         subcall_process(subcall, subcall_req_hooked_on_reply);
260 }
261
262 static void subcall_request_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
263 {
264         subcall->callback_request = callback;
265         subcall->closure = cb_closure;
266         subcall_process(subcall, subcall_request_hooked_on_reply);
267 }
268
269 static void subcall_sync_leave(struct subcall *subcall)
270 {
271         struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
272         if (jobloop)
273                 jobs_leave(jobloop);
274 }
275
276 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
277 {
278         subcall->status = status;
279         subcall->result = json_object_get(result);
280         subcall_sync_leave(subcall);
281 }
282
283 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
284 {
285         struct subcall *subcall = closure;
286
287         if (!signum) {
288                 subcall->jobloop = jobloop;
289                 subcall->result = NULL;
290                 subcall->status = 0;
291                 subcall_process(subcall, subcall_sync_reply);
292         } else {
293                 subcall->status = -1;
294                 subcall_sync_leave(subcall);
295         }
296 }
297
298 static int subcallsync(struct subcall *subcall, struct json_object **result)
299 {
300         int rc;
301
302         afb_xreq_unhooked_addref(&subcall->xreq);
303         rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
304         *result = subcall->result;
305         if (rc < 0 || subcall->status < 0) {
306                 *result = *result ?: afb_msg_json_internal_error();
307                 rc = -1;
308         }
309         afb_xreq_unhooked_unref(&subcall->xreq);
310         return rc;
311 }
312
313 /******************************************************************************/
314
315 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
316 {
317         char *info;
318         if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
319                 info = NULL;
320         fun(first, second, info);
321         free(info);
322 }
323
324 /******************************************************************************/
325
326 static struct json_object *xreq_json_cb(struct afb_request *closure)
327 {
328         struct afb_xreq *xreq = xreq_from_request(closure);
329         if (!xreq->json && xreq->queryitf->json)
330                 xreq->json = xreq->queryitf->json(xreq);
331         return xreq->json;
332 }
333
334 static struct afb_arg xreq_get_cb(struct afb_request *closure, const char *name)
335 {
336         struct afb_xreq *xreq = xreq_from_request(closure);
337         struct afb_arg arg;
338         struct json_object *object, *value;
339
340         if (xreq->queryitf->get)
341                 arg = xreq->queryitf->get(xreq, name);
342         else {
343                 object = xreq_json_cb(closure);
344                 if (json_object_object_get_ex(object, name, &value)) {
345                         arg.name = name;
346                         arg.value = json_object_get_string(value);
347                 } else {
348                         arg.name = NULL;
349                         arg.value = NULL;
350                 }
351                 arg.path = NULL;
352         }
353         return arg;
354 }
355
356 static void xreq_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
357 {
358         struct afb_xreq *xreq = xreq_from_request(closure);
359
360         if (xreq->replied) {
361                 ERROR("reply called more than one time!!");
362                 json_object_put(obj);
363         } else {
364                 xreq->replied = 1;
365                 if (xreq->queryitf->success)
366                         xreq->queryitf->success(xreq, obj, info);
367                 else
368                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
369         }
370 }
371
372 static void xreq_fail_cb(struct afb_request *closure, const char *status, const char *info)
373 {
374         struct afb_xreq *xreq = xreq_from_request(closure);
375
376         if (xreq->replied) {
377                 ERROR("reply called more than one time!!");
378         } else {
379                 xreq->replied = 1;
380                 if (xreq->queryitf->fail)
381                         xreq->queryitf->fail(xreq, status, info);
382                 else
383                         xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
384         }
385 }
386
387 static void xreq_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
388 {
389         vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
390 }
391
392 static void xreq_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
393 {
394         vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
395 }
396
397 static void *xreq_context_get_cb(struct afb_request *closure)
398 {
399         struct afb_xreq *xreq = xreq_from_request(closure);
400         return afb_context_get(&xreq->context);
401 }
402
403 static void xreq_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
404 {
405         struct afb_xreq *xreq = xreq_from_request(closure);
406         afb_context_set(&xreq->context, value, free_value);
407 }
408
409 static struct afb_request *xreq_addref_cb(struct afb_request *closure)
410 {
411         struct afb_xreq *xreq = xreq_from_request(closure);
412         afb_xreq_unhooked_addref(xreq);
413         return closure;
414 }
415
416 static void xreq_unref_cb(struct afb_request *closure)
417 {
418         struct afb_xreq *xreq = xreq_from_request(closure);
419         afb_xreq_unhooked_unref(xreq);
420 }
421
422 static void xreq_session_close_cb(struct afb_request *closure)
423 {
424         struct afb_xreq *xreq = xreq_from_request(closure);
425         afb_context_close(&xreq->context);
426 }
427
428 static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level)
429 {
430         struct afb_xreq *xreq = xreq_from_request(closure);
431         return afb_context_change_loa(&xreq->context, level);
432 }
433
434 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
435 static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event)
436 {
437         return xreq_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
438 }
439
440 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
441 {
442         struct afb_xreq *xreq = xreq_from_request(closure);
443         return afb_xreq_subscribe(xreq, eventid);
444 }
445
446 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
447 {
448         if (xreq->listener)
449                 return afb_evt_eventid_add_watch(xreq->listener, eventid);
450         if (xreq->queryitf->subscribe)
451                 return xreq->queryitf->subscribe(xreq, eventid);
452         ERROR("no event listener, subscription impossible");
453         errno = EINVAL;
454         return -1;
455 }
456
457 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
458 static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
459 {
460         return xreq_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
461 }
462
463 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
464 {
465         struct afb_xreq *xreq = xreq_from_request(closure);
466         return afb_xreq_unsubscribe(xreq, eventid);
467 }
468
469 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
470 {
471         if (xreq->listener)
472                 return afb_evt_eventid_remove_watch(xreq->listener, eventid);
473         if (xreq->queryitf->unsubscribe)
474                 return xreq->queryitf->unsubscribe(xreq, eventid);
475         ERROR("no event listener, unsubscription impossible");
476         errno = EINVAL;
477         return -1;
478 }
479
480 static void xreq_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
481 {
482         struct afb_xreq *xreq = xreq_from_request(closure);
483         struct subcall *sc;
484
485         sc = subcall_alloc(xreq, api, verb, args);
486         if (sc == NULL) {
487                 if (callback)
488                         callback(cb_closure, 1, afb_msg_json_internal_error());
489                 json_object_put(args);
490         } else {
491                 subcall(sc, callback, cb_closure);
492         }
493 }
494
495 static void xreq_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
496 {
497         struct afb_xreq *xreq = xreq_from_request(closure);
498         struct subcall *sc;
499
500         sc = subcall_alloc(xreq, api, verb, args);
501         if (sc == NULL) {
502                 if (callback)
503                         callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_req(xreq));
504                 json_object_put(args);
505         } else {
506                 subcall_req(sc, callback, cb_closure);
507         }
508 }
509
510 static void xreq_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
511 {
512         struct afb_xreq *xreq = xreq_from_request(closure);
513         struct subcall *sc;
514
515         sc = subcall_alloc(xreq, api, verb, args);
516         if (sc == NULL) {
517                 if (callback)
518                         callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_request(xreq));
519                 json_object_put(args);
520         } else {
521                 subcall_request(sc, callback, cb_closure);
522         }
523 }
524
525
526 static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
527 {
528         int rc;
529         struct subcall *sc;
530         struct afb_xreq *xreq = xreq_from_request(closure);
531         struct json_object *resu;
532
533         sc = subcall_alloc(xreq, api, verb, args);
534         if (!sc) {
535                 rc = -1;
536                 resu = afb_msg_json_internal_error();
537                 json_object_put(args);
538         } else {
539                 rc = subcallsync(sc, &resu);
540         }
541         if (result)
542                 *result = resu;
543         else
544                 json_object_put(resu);
545         return rc;
546 }
547
548 static void xreq_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
549 {
550         char *p;
551         struct afb_xreq *xreq = xreq_from_request(closure);
552
553         if (!fmt || vasprintf(&p, fmt, args) < 0)
554                 vverbose(level, file, line, func, fmt, args);
555         else {
556                 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.api, p);
557                 free(p);
558         }
559 }
560
561 static struct afb_stored_req *xreq_store_cb(struct afb_request *closure)
562 {
563         xreq_addref_cb(closure);
564         return (struct afb_stored_req*)closure;
565 }
566
567 static int xreq_has_permission_cb(struct afb_request *closure, const char *permission)
568 {
569         struct afb_xreq *xreq = xreq_from_request(closure);
570         return afb_auth_has_permission(xreq, permission);
571 }
572
573 static char *xreq_get_application_id_cb(struct afb_request *closure)
574 {
575         struct afb_xreq *xreq = xreq_from_request(closure);
576         return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
577 }
578
579 static void *xreq_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
580 {
581         struct afb_xreq *xreq = xreq_from_request(closure);
582         return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
583 }
584
585 static int xreq_get_uid_cb(struct afb_request *closure)
586 {
587         struct afb_xreq *xreq = xreq_from_request(closure);
588         return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
589 }
590
591 /******************************************************************************/
592
593 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
594 {
595         struct json_object *r = xreq_json_cb(closure);
596         struct afb_xreq *xreq = xreq_from_request(closure);
597         return afb_hook_xreq_json(xreq, r);
598 }
599
600 static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name)
601 {
602         struct afb_arg r = xreq_get_cb(closure, name);
603         struct afb_xreq *xreq = xreq_from_request(closure);
604         return afb_hook_xreq_get(xreq, name, r);
605 }
606
607 static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
608 {
609         struct afb_xreq *xreq = xreq_from_request(closure);
610         afb_hook_xreq_success(xreq, obj, info);
611         xreq_success_cb(closure, obj, info);
612 }
613
614 static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info)
615 {
616         struct afb_xreq *xreq = xreq_from_request(closure);
617         afb_hook_xreq_fail(xreq, status, info);
618         xreq_fail_cb(closure, status, info);
619 }
620
621 static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
622 {
623         vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
624 }
625
626 static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
627 {
628         vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
629 }
630
631 static void *xreq_hooked_context_get_cb(struct afb_request *closure)
632 {
633         void *r = xreq_context_get_cb(closure);
634         struct afb_xreq *xreq = xreq_from_request(closure);
635         return afb_hook_xreq_context_get(xreq, r);
636 }
637
638 static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
639 {
640         struct afb_xreq *xreq = xreq_from_request(closure);
641         afb_hook_xreq_context_set(xreq, value, free_value);
642         xreq_context_set_cb(closure, value, free_value);
643 }
644
645 static struct afb_request *xreq_hooked_addref_cb(struct afb_request *closure)
646 {
647         struct afb_xreq *xreq = xreq_from_request(closure);
648         afb_hook_xreq_addref(xreq);
649         return xreq_addref_cb(closure);
650 }
651
652 static void xreq_hooked_unref_cb(struct afb_request *closure)
653 {
654         struct afb_xreq *xreq = xreq_from_request(closure);
655         afb_hook_xreq_unref(xreq);
656         xreq_unref_cb(closure);
657 }
658
659 static void xreq_hooked_session_close_cb(struct afb_request *closure)
660 {
661         struct afb_xreq *xreq = xreq_from_request(closure);
662         afb_hook_xreq_session_close(xreq);
663         xreq_session_close_cb(closure);
664 }
665
666 static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level)
667 {
668         int r = xreq_session_set_LOA_cb(closure, level);
669         struct afb_xreq *xreq = xreq_from_request(closure);
670         return afb_hook_xreq_session_set_LOA(xreq, level, r);
671 }
672
673 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
674 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
675 {
676         return xreq_hooked_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
677 }
678
679 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
680 {
681         int r = xreq_subscribe_eventid_cb(closure, eventid);
682         struct afb_xreq *xreq = xreq_from_request(closure);
683         return afb_hook_xreq_subscribe(xreq, eventid, r);
684 }
685
686 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
687 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
688 {
689         return xreq_hooked_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
690 }
691
692 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
693 {
694         int r = xreq_unsubscribe_eventid_cb(closure, eventid);
695         struct afb_xreq *xreq = xreq_from_request(closure);
696         return afb_hook_xreq_unsubscribe(xreq, eventid, r);
697 }
698
699 static void xreq_hooked_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
700 {
701         struct afb_xreq *xreq = xreq_from_request(closure);
702         struct subcall *sc;
703
704         afb_hook_xreq_subcall(xreq, api, verb, args);
705         sc = subcall_alloc(xreq, api, verb, args);
706         if (sc == NULL) {
707                 if (callback)
708                         callback(cb_closure, 1, afb_msg_json_internal_error());
709                 json_object_put(args);
710         } else {
711                 subcall_hooked(sc, callback, cb_closure);
712         }
713 }
714
715 static void xreq_hooked_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
716 {
717         struct afb_xreq *xreq = xreq_from_request(closure);
718         struct subcall *sc;
719
720         afb_hook_xreq_subcall_req(xreq, api, verb, args);
721         sc = subcall_alloc(xreq, api, verb, args);
722         if (sc == NULL) {
723                 if (callback)
724                         callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_req(xreq));
725                 json_object_put(args);
726         } else {
727                 subcall_req_hooked(sc, callback, cb_closure);
728         }
729 }
730
731 static void xreq_hooked_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
732 {
733         struct afb_xreq *xreq = xreq_from_request(closure);
734         struct subcall *sc;
735
736         afb_hook_xreq_subcall(xreq, api, verb, args);
737         sc = subcall_alloc(xreq, api, verb, args);
738         if (sc == NULL) {
739                 if (callback)
740                         callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_request(xreq));
741                 json_object_put(args);
742         } else {
743                 subcall_request_hooked(sc, callback, cb_closure);
744         }
745 }
746
747 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
748 {
749         int r;
750         struct afb_xreq *xreq = xreq_from_request(closure);
751         afb_hook_xreq_subcallsync(xreq, api, verb, args);
752         r = xreq_subcallsync_cb(closure, api, verb, args, result);
753         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
754 }
755
756 static void xreq_hooked_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
757 {
758         struct afb_xreq *xreq = xreq_from_request(closure);
759         va_list ap;
760         va_copy(ap, args);
761         xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
762         afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
763         va_end(ap);
764 }
765
766 static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure)
767 {
768         struct afb_xreq *xreq = xreq_from_request(closure);
769         struct afb_stored_req *r = xreq_store_cb(closure);
770         afb_hook_xreq_store(xreq, r);
771         return r;
772 }
773
774 static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission)
775 {
776         struct afb_xreq *xreq = xreq_from_request(closure);
777         int r = xreq_has_permission_cb(closure, permission);
778         return afb_hook_xreq_has_permission(xreq, permission, r);
779 }
780
781 static char *xreq_hooked_get_application_id_cb(struct afb_request *closure)
782 {
783         struct afb_xreq *xreq = xreq_from_request(closure);
784         char *r = xreq_get_application_id_cb(closure);
785         return afb_hook_xreq_get_application_id(xreq, r);
786 }
787
788 static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
789 {
790         struct afb_xreq *xreq = xreq_from_request(closure);
791         void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
792         return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
793 }
794
795 static int xreq_hooked_get_uid_cb(struct afb_request *closure)
796 {
797         struct afb_xreq *xreq = xreq_from_request(closure);
798         int r = xreq_get_uid_cb(closure);
799         return afb_hook_xreq_get_uid(xreq, r);
800 }
801
802 /******************************************************************************/
803
804 const struct afb_request_itf xreq_itf = {
805         .json = xreq_json_cb,
806         .get = xreq_get_cb,
807         .success = xreq_success_cb,
808         .fail = xreq_fail_cb,
809         .vsuccess = xreq_vsuccess_cb,
810         .vfail = xreq_vfail_cb,
811         .context_get = xreq_context_get_cb,
812         .context_set = xreq_context_set_cb,
813         .addref = xreq_addref_cb,
814         .unref = xreq_unref_cb,
815         .session_close = xreq_session_close_cb,
816         .session_set_LOA = xreq_session_set_LOA_cb,
817         .subscribe = xreq_subscribe_cb,
818         .unsubscribe = xreq_unsubscribe_cb,
819         .subcall = xreq_subcall_cb,
820         .subcallsync = xreq_subcallsync_cb,
821         .vverbose = xreq_vverbose_cb,
822         .store = xreq_store_cb,
823         .subcall_req = xreq_subcall_req_cb,
824         .has_permission = xreq_has_permission_cb,
825         .get_application_id = xreq_get_application_id_cb,
826         .context_make = xreq_context_make_cb,
827         .subscribe_eventid = xreq_subscribe_eventid_cb,
828         .unsubscribe_eventid = xreq_unsubscribe_eventid_cb,
829         .subcall_request = xreq_subcall_request_cb,
830         .get_uid = xreq_get_uid_cb,
831 };
832
833 const struct afb_request_itf xreq_hooked_itf = {
834         .json = xreq_hooked_json_cb,
835         .get = xreq_hooked_get_cb,
836         .success = xreq_hooked_success_cb,
837         .fail = xreq_hooked_fail_cb,
838         .vsuccess = xreq_hooked_vsuccess_cb,
839         .vfail = xreq_hooked_vfail_cb,
840         .context_get = xreq_hooked_context_get_cb,
841         .context_set = xreq_hooked_context_set_cb,
842         .addref = xreq_hooked_addref_cb,
843         .unref = xreq_hooked_unref_cb,
844         .session_close = xreq_hooked_session_close_cb,
845         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
846         .subscribe = xreq_hooked_subscribe_cb,
847         .unsubscribe = xreq_hooked_unsubscribe_cb,
848         .subcall = xreq_hooked_subcall_cb,
849         .subcallsync = xreq_hooked_subcallsync_cb,
850         .vverbose = xreq_hooked_vverbose_cb,
851         .store = xreq_hooked_store_cb,
852         .subcall_req = xreq_hooked_subcall_req_cb,
853         .has_permission = xreq_hooked_has_permission_cb,
854         .get_application_id = xreq_hooked_get_application_id_cb,
855         .context_make = xreq_hooked_context_make_cb,
856         .subscribe_eventid = xreq_hooked_subscribe_eventid_cb,
857         .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb,
858         .subcall_request = xreq_hooked_subcall_request_cb,
859         .get_uid = xreq_hooked_get_uid_cb,
860 };
861
862 /******************************************************************************/
863
864 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
865 {
866         struct afb_xreq *xreq = (struct afb_xreq *)sreq;
867         if (xreq->hookflags)
868                 afb_hook_xreq_unstore(xreq);
869         return xreq_to_req(xreq);
870 }
871
872 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
873 {
874         return afb_request_json(xreq_to_request(xreq));
875 }
876
877 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
878 {
879         afb_request_success(xreq_to_request(xreq), obj, info);
880 }
881
882 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
883 {
884         va_list args;
885
886         va_start(args, info);
887         afb_request_success_v(xreq_to_request(xreq), obj, info, args);
888         va_end(args);
889 }
890
891 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
892 {
893         afb_request_fail(xreq_to_request(xreq), status, info);
894 }
895
896 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
897 {
898         va_list args;
899
900         va_start(args, info);
901         afb_request_fail_v(xreq_to_request(xreq), status, info, args);
902         va_end(args);
903
904 }
905
906 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
907 {
908         struct json_object *obj = xreq_json_cb(xreq_to_request(xreq));
909         const char *result = json_object_to_json_string(obj);
910         if (size != NULL)
911                 *size = strlen(result);
912         return result;
913 }
914
915 void afb_xreq_addref(struct afb_xreq *xreq)
916 {
917         afb_request_addref(xreq_to_request(xreq));
918 }
919
920 void afb_xreq_unref(struct afb_xreq *xreq)
921 {
922         afb_request_unref(xreq_to_request(xreq));
923 }
924
925 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
926 {
927         xreq_subcall_request_cb(xreq_to_request(xreq), api, verb, args, callback, cb_closure);
928 }
929
930 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
931 {
932         afb_request_subcall(xreq_to_request(xreq), api, verb, args, callback, cb_closure);
933 }
934
935 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
936 {
937         return xreq_subcallsync_cb(xreq_to_request(xreq), api, verb, args, result);
938 }
939
940 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
941 {
942         return afb_request_subcall_sync(xreq_to_request(xreq), api, verb, args, result);
943 }
944
945 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
946 {
947         int loa;
948
949         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
950                 if (!afb_context_check(&xreq->context)) {
951                         afb_context_close(&xreq->context);
952                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
953                         errno = EINVAL;
954                         return -1;
955                 }
956         }
957
958         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
959                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
960                 if (!afb_context_check_loa(&xreq->context, loa)) {
961                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
962                         errno = EPERM;
963                         return -1;
964                 }
965         }
966
967         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
968                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
969                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
970                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
971                         errno = EPERM;
972                         return -1;
973                 }
974         }
975
976         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
977                 afb_context_refresh(&xreq->context);
978         }
979         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
980                 afb_context_change_loa(&xreq->context, 0);
981                 afb_context_close(&xreq->context);
982         }
983
984         return 0;
985 }
986
987 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
988 {
989         int loa;
990
991         if (sessionflags != 0) {
992                 if (!afb_context_check(&xreq->context)) {
993                         afb_context_close(&xreq->context);
994                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
995                         errno = EINVAL;
996                         return -1;
997                 }
998         }
999
1000         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
1001         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
1002                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
1003                 errno = EPERM;
1004                 return -1;
1005         }
1006
1007         if (auth && !afb_auth_check(xreq, auth)) {
1008                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
1009                 errno = EPERM;
1010                 return -1;
1011         }
1012
1013         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
1014                 afb_context_refresh(&xreq->context);
1015         }
1016         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
1017                 afb_context_close(&xreq->context);
1018         }
1019
1020         return 0;
1021 }
1022
1023 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
1024 {
1025         if (!verb)
1026                 afb_xreq_fail_unknown_verb(xreq);
1027         else
1028                 if (!xreq_session_check_apply_v1(xreq, verb->session))
1029                         verb->callback(xreq_to_req(xreq));
1030 }
1031
1032 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
1033 {
1034         if (!verb)
1035                 afb_xreq_fail_unknown_verb(xreq);
1036         else
1037                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
1038                         verb->callback(xreq_to_req(xreq));
1039 }
1040
1041 void afb_xreq_call_verb_vdyn(struct afb_xreq *xreq, const struct afb_api_dyn_verb *verb)
1042 {
1043         if (!verb)
1044                 afb_xreq_fail_unknown_verb(xreq);
1045         else
1046                 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
1047                         verb->callback(xreq_to_request(xreq));
1048 }
1049
1050 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
1051 {
1052         memset(xreq, 0, sizeof *xreq);
1053         xreq->request.itf = &xreq_hooked_itf; /* hook by default */
1054         xreq->refcount = 1;
1055         xreq->queryitf = queryitf;
1056 }
1057
1058 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
1059 {
1060         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1061 }
1062
1063 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
1064 {
1065         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api);
1066 }
1067
1068 static void init_hooking(struct afb_xreq *xreq)
1069 {
1070         afb_hook_init_xreq(xreq);
1071         if (xreq->hookflags)
1072                 afb_hook_xreq_begin(xreq);
1073         else
1074                 xreq->request.itf = &xreq_itf; /* unhook the interface */
1075 }
1076
1077 /**
1078  * job callback for asynchronous and secured processing of the request.
1079  */
1080 static void process_async(int signum, void *arg)
1081 {
1082         struct afb_xreq *xreq = arg;
1083         const struct afb_api *api;
1084
1085         if (signum != 0) {
1086                 /* emit the error (assumes that hooking is initialised) */
1087                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
1088         } else {
1089                 /* init hooking */
1090                 init_hooking(xreq);
1091                 /* invoke api call method to process the reqiest */
1092                 api = (const struct afb_api*)xreq->context.api_key;
1093                 api->itf->call(api->closure, xreq);
1094         }
1095         /* release the request */
1096         afb_xreq_unhooked_unref(xreq);
1097 }
1098
1099 /**
1100  * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
1101  * The early failure occurs only in function 'afb_xreq_process' where normally,
1102  * the hooking is not initialised. So this "early" failure takes care to initialise
1103  * the hooking in first.
1104  */
1105 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1106 {
1107         va_list args;
1108
1109         /* init hooking */
1110         init_hooking(xreq);
1111
1112         /* send error */
1113         va_start(args, info);
1114         afb_request_fail_v(xreq_to_request(xreq), status, info, args);
1115         va_end(args);
1116 }
1117
1118 /**
1119  * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1120  * Errors are reported as request failures.
1121  */
1122 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1123 {
1124         const struct afb_api *api;
1125         struct afb_xreq *caller;
1126
1127         /* lookup at the api */
1128         xreq->apiset = apiset;
1129         api = afb_apiset_lookup_started(apiset, xreq->request.api, 1);
1130         if (!api) {
1131                 if (errno == ENOENT)
1132                         early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1133                 else
1134                         early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api);
1135                 goto end;
1136         }
1137         xreq->context.api_key = api;
1138
1139         /* check self locking */
1140         if (api->group) {
1141                 caller = xreq->caller;
1142                 while (caller) {
1143                         if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1144                                 /* noconcurrency lock detected */
1145                                 ERROR("self-lock detected in call stack for API %s", xreq->request.api);
1146                                 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api);
1147                                 goto end;
1148                         }
1149                         caller = caller->caller;
1150                 }
1151         }
1152
1153         /* queue the request job */
1154         afb_xreq_unhooked_addref(xreq);
1155         if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1156                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1157                 ERROR("can't process job with threads: %m");
1158                 early_failure(xreq, "cancelled", "not able to create a job for the task");
1159                 afb_xreq_unhooked_unref(xreq);
1160         }
1161 end:
1162         afb_xreq_unhooked_unref(xreq);
1163 }
1164