Reactivate hooking of requests
[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 NO_BINDING_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 "verbose.h"
35
36
37 static struct json_object *xreq_json_cb(void *closure)
38 {
39         struct afb_xreq *xreq = closure;
40         return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq->query));
41 }
42
43 static struct afb_arg xreq_get_cb(void *closure, const char *name)
44 {
45         struct afb_xreq *xreq = closure;
46         if (xreq->queryitf->get)
47                 return xreq->queryitf->get(xreq->query, name);
48         else
49                 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
50 }
51
52 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
53 {
54         struct afb_xreq *xreq = closure;
55         afb_xreq_success(xreq, obj, info);
56 }
57
58 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
59 {
60         if (xreq->replied) {
61                 ERROR("reply called more than one time!!");
62                 json_object_put(obj);
63         } else {
64                 xreq->replied = 1;
65                 if (xreq->queryitf->success)
66                         xreq->queryitf->success(xreq->query, obj, info);
67                 else
68                         xreq->queryitf->reply(xreq->query, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
69         }
70 }
71
72 static void xreq_fail_cb(void *closure, const char *status, const char *info)
73 {
74         struct afb_xreq *xreq = closure;
75         afb_xreq_fail(xreq, status, info);
76 }
77
78 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
79 {
80         if (xreq->replied) {
81                 ERROR("reply called more than one time!!");
82         } else {
83                 xreq->replied = 1;
84                 if (xreq->queryitf->fail)
85                         xreq->queryitf->fail(xreq->query, status, info);
86                 else
87                         xreq->queryitf->reply(xreq->query, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
88         }
89 }
90
91 static const char *xreq_raw_cb(void *closure, size_t *size)
92 {
93         struct afb_xreq *xreq = closure;
94         return afb_xreq_raw(xreq, size);
95 }
96
97 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
98 {
99         const char *result = json_object_to_json_string(xreq_json_cb(xreq));
100         if (size != NULL)
101                 *size = strlen(result);
102         return result;
103 }
104
105 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
106 {
107         struct json_object *obj = json_tokener_parse(buffer);
108         if (!obj == !buffer)
109                 xreq_success_cb(closure, obj, "fake send");
110         else
111                 xreq_fail_cb(closure, "fake-send-failed", "fake send");
112 }
113
114 static void *xreq_context_get_cb(void *closure)
115 {
116         struct afb_xreq *xreq = closure;
117         return afb_context_get(&xreq->context);
118 }
119
120 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
121 {
122         struct afb_xreq *xreq = closure;
123         afb_context_set(&xreq->context, value, free_value);
124 }
125
126 static void xreq_addref_cb(void *closure)
127 {
128         struct afb_xreq *xreq = closure;
129         afb_xreq_addref(xreq);
130 }
131
132 void afb_xreq_addref(struct afb_xreq *xreq)
133 {
134         xreq->refcount++;
135 }
136
137 static void xreq_unref_cb(void *closure)
138 {
139         struct afb_xreq *xreq = closure;
140         afb_xreq_unref(xreq);
141 }
142
143 void afb_xreq_unref(struct afb_xreq *xreq)
144 {
145         if (!--xreq->refcount) {
146                 xreq->queryitf->unref(xreq->query);
147         }
148 }
149
150 static void xreq_session_close_cb(void *closure)
151 {
152         struct afb_xreq *xreq = closure;
153         afb_context_close(&xreq->context);
154 }
155
156 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
157 {
158         struct afb_xreq *xreq = closure;
159         return afb_context_change_loa(&xreq->context, level);
160 }
161
162 static int xreq_subscribe_cb(void *closure, struct afb_event event)
163 {
164         struct afb_xreq *xreq = closure;
165         return afb_xreq_subscribe(xreq, event);
166 }
167
168 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
169 {
170         if (xreq->listener)
171                 return afb_evt_add_watch(xreq->listener, event);
172         if (xreq->queryitf->subscribe)
173                 return xreq->queryitf->subscribe(xreq->query, event);
174         ERROR("no event listener, subscription impossible");
175         errno = EINVAL;
176         return -1;
177 }
178
179 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
180 {
181         struct afb_xreq *xreq = closure;
182         return afb_xreq_unsubscribe(xreq, event);
183 }
184
185 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
186 {
187         if (xreq->listener)
188                 return afb_evt_remove_watch(xreq->listener, event);
189         if (xreq->queryitf->unsubscribe)
190                 return xreq->queryitf->unsubscribe(xreq->query, event);
191         ERROR("no event listener, unsubscription impossible");
192         errno = EINVAL;
193         return -1;
194 }
195
196 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)
197 {
198         struct afb_xreq *xreq = closure;
199
200         afb_xreq_subcall(xreq, api, verb, args, callback, cb_closure);
201 }
202
203 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)
204 {
205         if (xreq->queryitf->subcall)
206                 xreq->queryitf->subcall(xreq->query, api, verb, args, callback, cb_closure);
207         else
208                 afb_subcall(xreq, api, verb, args, callback, cb_closure);
209 }
210
211 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
212 {
213         struct afb_xreq *xreq = closure;
214         return afb_subcall_sync(xreq, api, verb, args, result);
215 }
216
217 static struct json_object *xreq_hooked_json_cb(void *closure)
218 {
219         struct json_object *r = xreq_json_cb(closure);
220         struct afb_xreq *xreq = closure;
221         return afb_hook_xreq_json(xreq, r);
222 }
223
224 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
225 {
226         struct afb_arg r = xreq_get_cb(closure, name);
227         struct afb_xreq *xreq = closure;
228         return afb_hook_xreq_get(xreq, name, r);
229 }
230
231 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
232 {
233         struct afb_xreq *xreq = closure;
234         afb_hook_xreq_success(xreq, obj, info);
235         xreq_success_cb(closure, obj, info);
236 }
237
238 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
239 {
240         struct afb_xreq *xreq = closure;
241         afb_hook_xreq_fail(xreq, status, info);
242         xreq_fail_cb(closure, status, info);
243 }
244
245 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
246 {
247         size_t s;
248         const char *r = xreq_raw_cb(closure, size ? : &s);
249         struct afb_xreq *xreq = closure;
250         return afb_hook_xreq_raw(xreq, r, *(size ? : &s));
251 }
252
253 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
254 {
255         struct afb_xreq *xreq = closure;
256         afb_hook_xreq_send(xreq, buffer, size);
257         xreq_send_cb(closure, buffer, size);
258 }
259
260 static void *xreq_hooked_context_get_cb(void *closure)
261 {
262         void *r = xreq_context_get_cb(closure);
263         struct afb_xreq *xreq = closure;
264         return afb_hook_xreq_context_get(xreq, r);
265 }
266
267 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
268 {
269         struct afb_xreq *xreq = closure;
270         afb_hook_xreq_context_set(xreq, value, free_value);
271         xreq_context_set_cb(closure, value, free_value);
272 }
273
274 static void xreq_hooked_addref_cb(void *closure)
275 {
276         struct afb_xreq *xreq = closure;
277         afb_hook_xreq_addref(xreq);
278         xreq_addref_cb(closure);
279 }
280 /*
281 static void xreq_hooked_unref_cb(void *closure)
282 {
283         TODO
284 }
285 */
286 static void xreq_hooked_session_close_cb(void *closure)
287 {
288         struct afb_xreq *xreq = closure;
289         afb_hook_xreq_session_close(xreq);
290         xreq_session_close_cb(closure);
291 }
292
293 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
294 {
295         int r = xreq_session_set_LOA_cb(closure, level);
296         struct afb_xreq *xreq = closure;
297         return afb_hook_xreq_session_set_LOA(xreq, level, r);
298 }
299
300 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
301 {
302         int r = xreq_subscribe_cb(closure, event);
303         struct afb_xreq *xreq = closure;
304         return afb_hook_xreq_subscribe(xreq, event, r);
305 }
306
307 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
308 {
309         int r = xreq_unsubscribe_cb(closure, event);
310         struct afb_xreq *xreq = closure;
311         return afb_hook_xreq_unsubscribe(xreq, event, r);
312 }
313
314 /*
315 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)
316 {
317         struct afb_xreq *xreq = closure;
318
319         afb_xreq_subcall(xreq, api, verb, args, callback, cb_closure);
320 }
321
322 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
323 {
324         struct afb_xreq *xreq = closure;
325         return afb_subcall_sync(xreq, api, verb, args, result);
326 }
327 */
328 const struct afb_req_itf xreq_itf = {
329         .json = xreq_json_cb,
330         .get = xreq_get_cb,
331         .success = xreq_success_cb,
332         .fail = xreq_fail_cb,
333         .raw = xreq_raw_cb,
334         .send = xreq_send_cb,
335         .context_get = xreq_context_get_cb,
336         .context_set = xreq_context_set_cb,
337         .addref = xreq_addref_cb,
338         .unref = xreq_unref_cb,
339         .session_close = xreq_session_close_cb,
340         .session_set_LOA = xreq_session_set_LOA_cb,
341         .subscribe = xreq_subscribe_cb,
342         .unsubscribe = xreq_unsubscribe_cb,
343         .subcall = xreq_subcall_cb,
344         .subcallsync = xreq_subcallsync_cb
345 };
346
347 const struct afb_req_itf xreq_hooked_itf = {
348         .json = xreq_hooked_json_cb,
349         .get = xreq_hooked_get_cb,
350         .success = xreq_hooked_success_cb,
351         .fail = xreq_hooked_fail_cb,
352         .raw = xreq_hooked_raw_cb,
353         .send = xreq_hooked_send_cb,
354         .context_get = xreq_hooked_context_get_cb,
355         .context_set = xreq_hooked_context_set_cb,
356         .addref = xreq_hooked_addref_cb,
357 .unref = xreq_unref_cb,
358         .session_close = xreq_hooked_session_close_cb,
359         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
360         .subscribe = xreq_hooked_subscribe_cb,
361         .unsubscribe = xreq_hooked_unsubscribe_cb,
362 .subcall = xreq_subcall_cb,
363 .subcallsync = xreq_subcallsync_cb
364 };
365
366 static inline struct afb_req to_req(struct afb_xreq *xreq)
367 {
368         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
369 }
370
371 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
372 {
373         char *message;
374         va_list args;
375         va_start(args, info);
376         if (info == NULL || vasprintf(&message, info, args) < 0)
377                 message = NULL;
378         va_end(args);
379         afb_xreq_success(xreq, obj, message);
380         free(message);
381 }
382
383 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
384 {
385         char *message;
386         va_list args;
387         va_start(args, info);
388         if (info == NULL || vasprintf(&message, info, args) < 0)
389                 message = NULL;
390         va_end(args);
391         afb_xreq_fail(xreq, status, message);
392         free(message);
393 }
394
395 static int xcheck(struct afb_xreq *xreq, int sessionflags)
396 {
397         if ((sessionflags & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
398                 if (!afb_context_check(&xreq->context)) {
399                         afb_context_close(&xreq->context);
400                         afb_xreq_fail_f(xreq, "failed", "invalid token's identity");
401                         return 0;
402                 }
403         }
404
405         if ((sessionflags & AFB_SESSION_CREATE) != 0) {
406                 if (afb_context_check_loa(&xreq->context, 1)) {
407                         afb_xreq_fail_f(xreq, "failed", "invalid creation state");
408                         return 0;
409                 }
410                 afb_context_change_loa(&xreq->context, 1);
411                 afb_context_refresh(&xreq->context);
412         }
413
414         if ((sessionflags & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
415                 afb_context_refresh(&xreq->context);
416
417         if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
418                 afb_context_change_loa(&xreq->context, 0);
419                 afb_context_close(&xreq->context);
420         }
421
422         if ((sessionflags & AFB_SESSION_LOA_GE) != 0) {
423                 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
424                 if (!afb_context_check_loa(&xreq->context, loa)) {
425                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
426                         return 0;
427                 }
428         }
429
430         if ((sessionflags & AFB_SESSION_LOA_LE) != 0) {
431                 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
432                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
433                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
434                         return 0;
435                 }
436         }
437         return 1;
438 }
439
440 void afb_xreq_call(struct afb_xreq *xreq, int sessionflags, void (*method)(struct afb_req req))
441 {
442         if (xcheck(xreq, sessionflags))
443                 method(to_req(xreq));
444 }
445
446 void afb_xreq_begin(struct afb_xreq *xreq)
447 {
448         afb_hook_init_xreq(xreq);
449         if (xreq->hookflags)
450                 afb_hook_xreq_begin(xreq);
451 }
452
453