Cleanup legacy internal functions
[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 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <json-c/json.h>
26 #include <afb/afb-binding.h>
27
28 #include "afb-context.h"
29 #include "afb-xreq.h"
30 #include "afb-evt.h"
31 #include "afb-msg-json.h"
32 #include "afb-subcall.h"
33 #include "afb-hook.h"
34 #include "afb-api.h"
35 #include "afb-apiset.h"
36 #include "afb-auth.h"
37 #include "jobs.h"
38 #include "verbose.h"
39
40 /******************************************************************************/
41
42 static struct json_object *xreq_json_cb(void *closure)
43 {
44         struct afb_xreq *xreq = closure;
45         return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq));
46 }
47
48 static struct afb_arg xreq_get_cb(void *closure, const char *name)
49 {
50         struct afb_xreq *xreq = closure;
51         if (xreq->queryitf->get)
52                 return xreq->queryitf->get(xreq, name);
53         else
54                 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
55 }
56
57 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
58 {
59         struct afb_xreq *xreq = closure;
60
61         if (xreq->replied) {
62                 ERROR("reply called more than one time!!");
63                 json_object_put(obj);
64         } else {
65                 xreq->replied = 1;
66                 if (xreq->queryitf->success)
67                         xreq->queryitf->success(xreq, obj, info);
68                 else
69                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
70         }
71 }
72
73 static void xreq_fail_cb(void *closure, const char *status, const char *info)
74 {
75         struct afb_xreq *xreq = closure;
76
77         if (xreq->replied) {
78                 ERROR("reply called more than one time!!");
79         } else {
80                 xreq->replied = 1;
81                 if (xreq->queryitf->fail)
82                         xreq->queryitf->fail(xreq, status, info);
83                 else
84                         xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
85         }
86 }
87
88 static void *xreq_context_get_cb(void *closure)
89 {
90         struct afb_xreq *xreq = closure;
91         return afb_context_get(&xreq->context);
92 }
93
94 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
95 {
96         struct afb_xreq *xreq = closure;
97         afb_context_set(&xreq->context, value, free_value);
98 }
99
100 static void xreq_addref_cb(void *closure)
101 {
102         struct afb_xreq *xreq = closure;
103         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
104 }
105
106 static void xreq_unref_cb(void *closure)
107 {
108         struct afb_xreq *xreq = closure;
109         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
110                 xreq->queryitf->unref(xreq);
111         }
112 }
113
114 static void xreq_session_close_cb(void *closure)
115 {
116         struct afb_xreq *xreq = closure;
117         afb_context_close(&xreq->context);
118 }
119
120 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
121 {
122         struct afb_xreq *xreq = closure;
123         return afb_context_change_loa(&xreq->context, level);
124 }
125
126 static int xreq_subscribe_cb(void *closure, struct afb_event event)
127 {
128         struct afb_xreq *xreq = closure;
129         return afb_xreq_subscribe(xreq, event);
130 }
131
132 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
133 {
134         if (xreq->listener)
135                 return afb_evt_add_watch(xreq->listener, event);
136         if (xreq->queryitf->subscribe)
137                 return xreq->queryitf->subscribe(xreq, event);
138         ERROR("no event listener, subscription impossible");
139         errno = EINVAL;
140         return -1;
141 }
142
143 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
144 {
145         struct afb_xreq *xreq = closure;
146         return afb_xreq_unsubscribe(xreq, event);
147 }
148
149 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
150 {
151         if (xreq->listener)
152                 return afb_evt_remove_watch(xreq->listener, event);
153         if (xreq->queryitf->unsubscribe)
154                 return xreq->queryitf->unsubscribe(xreq, event);
155         ERROR("no event listener, unsubscription impossible");
156         errno = EINVAL;
157         return -1;
158 }
159
160 static void xreq_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
161 {
162         struct afb_xreq *xreq = closure;
163
164         if (xreq->queryitf->subcall) {
165                 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
166                 json_object_put(args);
167         } else {
168                 afb_subcall(xreq, api, verb, args, callback, cb_closure);
169         }
170 }
171
172 struct xreq_sync
173 {
174         struct afb_xreq *caller;
175         const char *api;
176         const char *verb;
177         struct json_object *args;
178         struct jobloop *jobloop;
179         struct json_object *result;
180         int iserror;
181 };
182
183 static void xreq_sync_leave(struct xreq_sync *sync)
184 {
185         struct jobloop *jobloop = sync->jobloop;
186         if (jobloop) {
187                 sync->jobloop = NULL;
188                 jobs_leave(jobloop);
189         }
190 }
191
192 static void xreq_sync_reply(void *closure, int iserror, struct json_object *obj)
193 {
194         struct xreq_sync *sync = closure;
195
196         sync->iserror = iserror;
197         sync->result = json_object_get(obj);
198         xreq_sync_leave(sync);
199 }
200
201 static void xreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
202 {
203         struct xreq_sync *sync = closure;
204
205         if (!signum) {
206                 sync->jobloop = jobloop;
207                 xreq_subcall_cb(sync->caller, sync->api, sync->verb, json_object_get(sync->args), xreq_sync_reply, sync);
208         } else {
209                 sync->iserror = 1;
210                 xreq_sync_leave(sync);
211         }
212 }
213
214 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
215 {
216         int rc;
217         struct xreq_sync sync;
218         struct afb_xreq *xreq = closure;
219
220         sync.caller = xreq;
221         sync.api = api;
222         sync.verb = verb;
223         sync.args = args;
224         sync.jobloop = NULL;
225         sync.result = NULL;
226         sync.iserror = 1;
227
228         rc = jobs_enter(NULL, 0, xreq_sync_enter, &sync);
229         json_object_put(args);
230         if (rc < 0 || sync.iserror) {
231                 *result = sync.result ? : afb_msg_json_internal_error();
232                 return 0;
233         }
234         *result = sync.result;
235         return 1;
236 }
237
238 /******************************************************************************/
239
240 static struct json_object *xreq_hooked_json_cb(void *closure)
241 {
242         struct json_object *r = xreq_json_cb(closure);
243         struct afb_xreq *xreq = closure;
244         return afb_hook_xreq_json(xreq, r);
245 }
246
247 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
248 {
249         struct afb_arg r = xreq_get_cb(closure, name);
250         struct afb_xreq *xreq = closure;
251         return afb_hook_xreq_get(xreq, name, r);
252 }
253
254 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
255 {
256         struct afb_xreq *xreq = closure;
257         afb_hook_xreq_success(xreq, obj, info);
258         xreq_success_cb(closure, obj, info);
259 }
260
261 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
262 {
263         struct afb_xreq *xreq = closure;
264         afb_hook_xreq_fail(xreq, status, info);
265         xreq_fail_cb(closure, status, info);
266 }
267
268 static void *xreq_hooked_context_get_cb(void *closure)
269 {
270         void *r = xreq_context_get_cb(closure);
271         struct afb_xreq *xreq = closure;
272         return afb_hook_xreq_context_get(xreq, r);
273 }
274
275 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
276 {
277         struct afb_xreq *xreq = closure;
278         afb_hook_xreq_context_set(xreq, value, free_value);
279         xreq_context_set_cb(closure, value, free_value);
280 }
281
282 static void xreq_hooked_addref_cb(void *closure)
283 {
284         struct afb_xreq *xreq = closure;
285         afb_hook_xreq_addref(xreq);
286         xreq_addref_cb(closure);
287 }
288
289 static void xreq_hooked_unref_cb(void *closure)
290 {
291         struct afb_xreq *xreq = closure;
292         afb_hook_xreq_unref(xreq);
293         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
294                 afb_hook_xreq_end(xreq);
295                 xreq->queryitf->unref(xreq);
296         }
297 }
298
299 static void xreq_hooked_session_close_cb(void *closure)
300 {
301         struct afb_xreq *xreq = closure;
302         afb_hook_xreq_session_close(xreq);
303         xreq_session_close_cb(closure);
304 }
305
306 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
307 {
308         int r = xreq_session_set_LOA_cb(closure, level);
309         struct afb_xreq *xreq = closure;
310         return afb_hook_xreq_session_set_LOA(xreq, level, r);
311 }
312
313 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
314 {
315         int r = xreq_subscribe_cb(closure, event);
316         struct afb_xreq *xreq = closure;
317         return afb_hook_xreq_subscribe(xreq, event, r);
318 }
319
320 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
321 {
322         int r = xreq_unsubscribe_cb(closure, event);
323         struct afb_xreq *xreq = closure;
324         return afb_hook_xreq_unsubscribe(xreq, event, r);
325 }
326
327 struct reply
328 {
329         struct afb_xreq *xreq;
330         void (*callback)(void*, int, struct json_object*);
331         void *closure;
332 };
333
334 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
335 {
336         struct reply *reply = closure;
337         
338         afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
339         reply->callback(reply->closure, iserror, result);
340         free(reply);
341 }
342
343 static void xreq_hooked_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
344 {
345         struct reply *reply = malloc(sizeof *reply);
346         struct afb_xreq *xreq = closure;
347         afb_hook_xreq_subcall(xreq, api, verb, args);
348         if (reply) {
349                 reply->xreq = xreq;
350                 reply->callback = callback;
351                 reply->closure = cb_closure;
352                 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
353         } else {
354                 ERROR("out of memory");
355                 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
356         }
357 }
358
359 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
360 {
361         int r;
362         struct afb_xreq *xreq = closure;
363         afb_hook_xreq_subcallsync(xreq, api, verb, args);
364         r = xreq_subcallsync_cb(closure, api, verb, args, result);
365         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
366 }
367
368 /******************************************************************************/
369
370 const struct afb_req_itf xreq_itf = {
371         .json = xreq_json_cb,
372         .get = xreq_get_cb,
373         .success = xreq_success_cb,
374         .fail = xreq_fail_cb,
375         .context_get = xreq_context_get_cb,
376         .context_set = xreq_context_set_cb,
377         .addref = xreq_addref_cb,
378         .unref = xreq_unref_cb,
379         .session_close = xreq_session_close_cb,
380         .session_set_LOA = xreq_session_set_LOA_cb,
381         .subscribe = xreq_subscribe_cb,
382         .unsubscribe = xreq_unsubscribe_cb,
383         .subcall = xreq_subcall_cb,
384         .subcallsync = xreq_subcallsync_cb
385 };
386
387 const struct afb_req_itf xreq_hooked_itf = {
388         .json = xreq_hooked_json_cb,
389         .get = xreq_hooked_get_cb,
390         .success = xreq_hooked_success_cb,
391         .fail = xreq_hooked_fail_cb,
392         .context_get = xreq_hooked_context_get_cb,
393         .context_set = xreq_hooked_context_set_cb,
394         .addref = xreq_hooked_addref_cb,
395         .unref = xreq_hooked_unref_cb,
396         .session_close = xreq_hooked_session_close_cb,
397         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
398         .subscribe = xreq_hooked_subscribe_cb,
399         .unsubscribe = xreq_hooked_unsubscribe_cb,
400         .subcall = xreq_hooked_subcall_cb,
401         .subcallsync = xreq_hooked_subcallsync_cb
402 };
403
404 static inline struct afb_req to_req(struct afb_xreq *xreq)
405 {
406         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
407 }
408
409 /******************************************************************************/
410
411 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
412 {
413         return afb_req_json(to_req(xreq));
414 }
415
416 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
417 {
418         afb_req_success(to_req(xreq), obj, info);
419 }
420
421 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
422 {
423         char *message;
424         va_list args;
425         va_start(args, info);
426         if (info == NULL || vasprintf(&message, info, args) < 0)
427                 message = NULL;
428         va_end(args);
429         afb_xreq_success(xreq, obj, message);
430         free(message);
431 }
432
433 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
434 {
435         afb_req_fail(to_req(xreq), status, info);
436 }
437
438 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
439 {
440         char *message;
441         va_list args;
442         va_start(args, info);
443         if (info == NULL || vasprintf(&message, info, args) < 0)
444                 message = NULL;
445         va_end(args);
446         afb_xreq_fail(xreq, status, message);
447         free(message);
448 }
449
450 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
451 {
452         struct json_object *obj = xreq_json_cb(xreq);
453         const char *result = json_object_to_json_string(obj);
454         if (size != NULL)
455                 *size = strlen(result);
456         return result;
457 }
458
459 void afb_xreq_addref(struct afb_xreq *xreq)
460 {
461         afb_req_addref(to_req(xreq));
462 }
463
464 void afb_xreq_unref(struct afb_xreq *xreq)
465 {
466         afb_req_unref(to_req(xreq));
467 }
468
469 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*), void *cb_closure)
470 {
471         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
472 }
473
474 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*), void *cb_closure)
475 {
476         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
477 }
478
479 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
480 {
481         return xreq_subcallsync_cb(xreq, api, verb, args, result);
482 }
483
484 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
485 {
486         return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
487 }
488
489 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
490 {
491         int loa;
492
493         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
494                 if (!afb_context_check(&xreq->context)) {
495                         afb_context_close(&xreq->context);
496                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
497                         errno = EINVAL;
498                         return -1;
499                 }
500         }
501
502         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
503                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
504                 if (!afb_context_check_loa(&xreq->context, loa)) {
505                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
506                         errno = EPERM;
507                         return -1;
508                 }
509         }
510
511         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
512                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
513                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
514                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
515                         errno = EPERM;
516                         return -1;
517                 }
518         }
519
520         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
521                 afb_context_refresh(&xreq->context);
522         }
523         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
524                 afb_context_change_loa(&xreq->context, 0);
525                 afb_context_close(&xreq->context);
526         }
527
528         return 0;
529 }
530
531 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
532 {
533         int loa;
534
535         if (sessionflags != 0) {
536                 if (!afb_context_check(&xreq->context)) {
537                         afb_context_close(&xreq->context);
538                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
539                         errno = EINVAL;
540                         return -1;
541                 }
542         }
543
544         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
545         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
546                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
547                 errno = EPERM;
548                 return -1;
549         }
550
551         if (auth && !afb_auth_check(auth, xreq)) {
552                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
553                 errno = EPERM;
554                 return -1;
555         }
556
557         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
558                 afb_context_refresh(&xreq->context);
559         }
560         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
561                 afb_context_close(&xreq->context);
562         }
563
564         return 0;
565 }
566
567 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
568 {
569         if (!verb)
570                 afb_xreq_fail_unknown_verb(xreq);
571         else
572                 if (!xreq_session_check_apply_v1(xreq, verb->session))
573                         verb->callback(to_req(xreq));
574 }
575
576 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
577 {
578         if (!verb)
579                 afb_xreq_fail_unknown_verb(xreq);
580         else
581                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
582                         verb->callback(to_req(xreq));
583 }
584
585 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
586 {
587         memset(xreq, 0, sizeof *xreq);
588         xreq->refcount = 1;
589         xreq->queryitf = queryitf;
590 }
591
592 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
593 {
594         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
595 }
596
597 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
598 {
599         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
600 }
601
602 static void process_sync(struct afb_xreq *xreq)
603 {
604         struct afb_api api;
605
606         /* init hooking */
607         afb_hook_init_xreq(xreq);
608         if (xreq->hookflags)
609                 afb_hook_xreq_begin(xreq);
610
611         /* search the api */
612         if (afb_apiset_get(xreq->apiset, xreq->api, &api) < 0) {
613                 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
614         } else {
615                 xreq->context.api_key = api.closure;
616                 api.itf->call(api.closure, xreq);
617         }
618 }
619
620 static void process_async(int signum, void *arg)
621 {
622         struct afb_xreq *xreq = arg;
623
624         if (signum != 0) {
625                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
626         } else {
627                 process_sync(xreq);
628         }
629         afb_xreq_unref(xreq);
630 }
631
632 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
633 {
634         xreq->apiset = apiset;
635
636         afb_xreq_addref(xreq);
637         if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
638                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
639                 ERROR("can't process job with threads: %m");
640                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
641                 afb_xreq_unref(xreq);
642         }
643         afb_xreq_unref(xreq);
644 }
645