afb-stub-ws: Always validate clients of stub-ws
[src/app-framework-binder.git] / src / afb-stub-ws.c
1 /*
2  * Copyright (C) 2015, 2016, 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 #define NO_PLUGIN_VERBOSE_MACRO
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <endian.h>
29 #include <netdb.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include <pthread.h>
34
35 #include <json-c/json.h>
36
37 #include <afb/afb-event.h>
38
39 #include "afb-common.h"
40
41 #include "afb-session.h"
42 #include "afb-cred.h"
43 #include "afb-api.h"
44 #include "afb-apiset.h"
45 #include "afb-proto-ws.h"
46 #include "afb-stub-ws.h"
47 #include "afb-context.h"
48 #include "afb-evt.h"
49 #include "afb-xreq.h"
50 #include "verbose.h"
51 #include "jobs.h"
52
53 struct afb_stub_ws;
54
55
56 /******************* handling subcalls *****************************/
57
58 /**
59  * Structure on server side for recording pending
60  * subcalls.
61  */
62 struct server_subcall
63 {
64         struct server_subcall *next;    /**< next subcall for the client */
65         uint32_t subcallid;             /**< the subcallid */
66         void (*callback)(void*, int, struct json_object*); /**< callback on completion */
67         void *closure;                  /**< closure of the callback */
68 };
69
70 /**
71  * Structure for sending back replies on client side
72  */
73 struct client_subcall
74 {
75         struct afb_stub_ws *stubws;     /**< stub descriptor */
76         uint32_t subcallid;             /**< subcallid for the reply */
77 };
78
79 /*
80  * structure for recording calls on client side
81  */
82 struct client_call {
83         struct client_call *next;       /* the next call */
84         struct afb_stub_ws *stubws;     /* the stub_ws */
85         struct afb_xreq *xreq;          /* the request handle */
86         uint32_t msgid;                 /* the message identifier */
87 };
88
89 /*
90  * structure for a ws request
91  */
92 struct server_req {
93         struct afb_xreq xreq;           /* the xreq */
94         struct afb_stub_ws *stubws;     /* the client of the request */
95         struct afb_proto_ws_call *call; /* the incoming call */
96 };
97
98 /*
99  * structure for recording events on client side
100  */
101 struct client_event
102 {
103         struct client_event *next;
104         struct afb_eventid *eventid;
105         int id;
106         int refcount;
107 };
108
109 /*
110  * structure for recording describe requests
111  */
112 struct client_describe
113 {
114         struct afb_stub_ws *stubws;
115         struct jobloop *jobloop;
116         struct json_object *result;
117 };
118
119 /*
120  * structure for jobs of describing
121  */
122 struct server_describe
123 {
124         struct afb_stub_ws *stubws;
125         struct afb_proto_ws_describe *describe;
126 };
127
128 /******************* stub description for client or servers ******************/
129
130 struct afb_stub_ws
131 {
132         /* count of references */
133         int refcount;
134
135         /* resource control */
136         pthread_mutex_t mutex;
137
138         /* protocol */
139         struct afb_proto_ws *proto;
140
141         /* listener for events (server side) */
142         struct afb_evt_listener *listener;
143
144         /* event replica (client side) */
145         struct client_event *events;
146
147         /* credentials (server side) */
148         struct afb_cred *cred;
149
150         /* apiset */
151         struct afb_apiset *apiset;
152
153         /* on hangup callback */
154         void (*on_hangup)(struct afb_stub_ws *);
155
156         /* the api name */
157         char apiname[1];
158 };
159
160 /******************* ws request part for server *****************/
161
162 /* decrement the reference count of the request and free/release it on falling to null */
163 static void server_req_destroy_cb(struct afb_xreq *xreq)
164 {
165         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
166
167         afb_context_disconnect(&wreq->xreq.context);
168         afb_cred_unref(wreq->xreq.cred);
169         json_object_put(wreq->xreq.json);
170         afb_proto_ws_call_unref(wreq->call);
171         afb_stub_ws_unref(wreq->stubws);
172         free(wreq);
173 }
174
175 static void server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
176 {
177         int rc;
178         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
179
180         rc = afb_proto_ws_call_success(wreq->call, obj, info);
181         if (rc < 0)
182                 ERROR("error while sending success");
183         json_object_put(obj);
184 }
185
186 static void server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
187 {
188         int rc;
189         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
190
191         rc = afb_proto_ws_call_fail(wreq->call, status, info);
192         if (rc < 0)
193                 ERROR("error while sending fail");
194 }
195
196 static void server_req_subcall_cb(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
197 {
198         int rc;
199         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
200
201         rc = afb_proto_ws_call_subcall(wreq->call, api, verb, args, callback, cb_closure);
202         if (rc < 0)
203                 ERROR("error while sending subcall");
204 }
205
206 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *event)
207 {
208         int rc;
209         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
210
211         rc = afb_evt_eventid_add_watch(wreq->stubws->listener, event);
212         if (rc >= 0)
213                 rc = afb_proto_ws_call_subscribe(wreq->call,  afb_evt_eventid_fullname(event), afb_evt_eventid_id(event));
214         if (rc < 0)
215                 ERROR("error while subscribing event");
216         return rc;
217 }
218
219 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *event)
220 {
221         int rc, rc2;
222         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
223
224         rc = afb_proto_ws_call_unsubscribe(wreq->call,  afb_evt_eventid_fullname(event), afb_evt_eventid_id(event));
225         rc2 = afb_evt_eventid_remove_watch(wreq->stubws->listener, event);
226         if (rc >= 0 && rc2 < 0)
227                 rc = rc2;
228         if (rc < 0)
229                 ERROR("error while unsubscribing event");
230         return rc;
231 }
232
233 static const struct afb_xreq_query_itf server_req_xreq_itf = {
234         .success = server_req_success_cb,
235         .fail = server_req_fail_cb,
236         .unref = server_req_destroy_cb,
237         .subcall = server_req_subcall_cb,
238         .subscribe = server_req_subscribe_cb,
239         .unsubscribe = server_req_unsubscribe_cb
240 };
241
242 /******************* client part **********************************/
243
244 /* search the event */
245 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
246 {
247         struct client_event *ev;
248
249         ev = stubws->events;
250         while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_eventid_fullname(ev->eventid), name)))
251                 ev = ev->next;
252
253         return ev;
254 }
255
256 /* on call, propagate it to the ws service */
257 static void client_call_cb(void * closure, struct afb_xreq *xreq)
258 {
259         struct afb_stub_ws *stubws = closure;
260
261         afb_proto_ws_client_call(stubws->proto, xreq->request.verb, afb_xreq_json(xreq), afb_session_uuid(xreq->context.session), xreq);
262         afb_xreq_unhooked_addref(xreq);
263 }
264
265 static void client_on_description_cb(void *closure, struct json_object *data)
266 {
267         struct client_describe *desc = closure;
268
269         desc->result = data;
270         jobs_leave(desc->jobloop);
271 }
272
273 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
274 {
275         struct client_describe *desc = closure;
276
277         if (signum)
278                 jobs_leave(jobloop);
279         else {
280                 desc->jobloop = jobloop;
281                 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
282         }
283 }
284
285 /* get the description */
286 static struct json_object *client_describe_cb(void * closure)
287 {
288         struct client_describe desc;
289
290         /* synchronous job: send the request and wait its result */
291         desc.stubws = closure;
292         desc.result = NULL;
293         jobs_enter(NULL, 0, client_send_describe_cb, &desc);
294         return desc.result;
295 }
296
297 /******************* server part: manage events **********************************/
298
299 static void server_event_add(void *closure, const char *event, int eventid)
300 {
301         struct afb_stub_ws *stubws = closure;
302
303         afb_proto_ws_server_event_create(stubws->proto, event, eventid);
304 }
305
306 static void server_event_remove(void *closure, const char *event, int eventid)
307 {
308         struct afb_stub_ws *stubws = closure;
309
310         afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
311 }
312
313 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
314 {
315         struct afb_stub_ws *stubws = closure;
316
317         afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
318         json_object_put(object);
319 }
320
321 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
322 {
323         struct afb_stub_ws *stubws = closure;
324
325         afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
326         json_object_put(object);
327 }
328
329 /*****************************************************/
330
331 static void on_reply_success(void *closure, void *request, struct json_object *result, const char *info)
332 {
333         struct afb_xreq *xreq = request;
334
335         afb_xreq_success(xreq, result, *info ? info : NULL);
336         afb_xreq_unhooked_unref(xreq);
337 }
338
339 static void on_reply_fail(void *closure, void *request, const char *status, const char *info)
340 {
341         struct afb_xreq *xreq = request;
342
343         afb_xreq_fail(xreq, status, *info ? info : NULL);
344         afb_xreq_unhooked_unref(xreq);
345 }
346
347 static void on_event_create(void *closure, const char *event_name, int event_id)
348 {
349         struct afb_stub_ws *stubws = closure;
350         struct client_event *ev;
351
352         /* check conflicts */
353         ev = client_event_search(stubws, event_id, event_name);
354         if (ev != NULL) {
355                 ev->refcount++;
356                 return;
357         }
358
359         /* no conflict, try to add it */
360         ev = malloc(sizeof *ev);
361         if (ev != NULL) {
362                 ev->eventid = afb_evt_eventid_create(event_name);
363                 if (ev->eventid != NULL) {
364                         ev->refcount = 1;
365                         ev->id = event_id;
366                         ev->next = stubws->events;
367                         stubws->events = ev;
368                         return;
369                 }
370                 free(ev);
371         }
372         ERROR("can't create event %s, out of memory", event_name);
373 }
374
375 static void on_event_remove(void *closure, const char *event_name, int event_id)
376 {
377         struct afb_stub_ws *stubws = closure;
378         struct client_event *ev, **prv;
379
380         /* check conflicts */
381         ev = client_event_search(stubws, event_id, event_name);
382         if (ev == NULL)
383                 return;
384
385         /* decrease the reference count */
386         if (--ev->refcount)
387                 return;
388
389         /* unlinks the event */
390         prv = &stubws->events;
391         while (*prv != ev)
392                 prv = &(*prv)->next;
393         *prv = ev->next;
394
395         /* destroys the event */
396         afb_evt_eventid_unref(ev->eventid);
397         free(ev);
398 }
399
400 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
401 {
402         struct afb_stub_ws *stubws = closure;
403         struct afb_xreq *xreq = request;
404         struct client_event *ev;
405
406         /* check conflicts */
407         ev = client_event_search(stubws, event_id, event_name);
408         if (ev == NULL)
409                 return;
410
411         if (afb_xreq_subscribe(xreq, ev->eventid) < 0)
412                 ERROR("can't subscribe: %m");
413 }
414
415 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
416 {
417         struct afb_stub_ws *stubws = closure;
418         struct afb_xreq *xreq = request;
419         struct client_event *ev;
420
421         /* check conflicts */
422         ev = client_event_search(stubws, event_id, event_name);
423         if (ev == NULL)
424                 return;
425
426         if (afb_xreq_unsubscribe(xreq, ev->eventid) < 0)
427                 ERROR("can't unsubscribe: %m");
428 }
429
430 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
431 {
432         struct afb_stub_ws *stubws = closure;
433         struct client_event *ev;
434
435         /* check conflicts */
436         ev = client_event_search(stubws, event_id, event_name);
437         if (ev)
438                 afb_evt_eventid_push(ev->eventid, data);
439         else
440                 ERROR("unreadable push event");
441 }
442
443 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
444 {
445         afb_evt_broadcast(event_name, data);
446 }
447
448 static void client_subcall_reply_cb(void *closure, int status, json_object *object, struct afb_request *request)
449 {
450         struct afb_proto_ws_subcall *subcall = closure;
451         afb_proto_ws_subcall_reply(subcall, status, object);
452 }
453
454 static void on_subcall(void *closure, struct afb_proto_ws_subcall *subcall, void *request, const char *api, const char *verb, struct json_object *args)
455 {
456         struct afb_xreq *xreq = request;
457
458         afb_xreq_subcall(xreq, api, verb, args, client_subcall_reply_cb, subcall);
459 }
460
461 /*****************************************************/
462
463 static void on_call(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, const char *sessionid)
464 {
465         struct afb_stub_ws *stubws = closure;
466         struct server_req *wreq;
467
468         afb_stub_ws_addref(stubws);
469
470         /* create the request */
471         wreq = malloc(sizeof *wreq);
472         if (wreq == NULL)
473                 goto out_of_memory;
474
475         afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
476         wreq->stubws = stubws;
477         wreq->call = call;
478
479         /* init the context */
480         if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
481                 goto unconnected;
482         wreq->xreq.context.validated = 1;
483
484         /* makes the call */
485         wreq->xreq.cred = afb_cred_addref(stubws->cred);
486         wreq->xreq.request.api = stubws->apiname;
487         wreq->xreq.request.verb = verb;
488         wreq->xreq.json = args;
489         afb_xreq_process(&wreq->xreq, stubws->apiset);
490         return;
491
492 unconnected:
493         free(wreq);
494 out_of_memory:
495         json_object_put(args);
496         afb_stub_ws_unref(stubws);
497         afb_proto_ws_call_fail(call, "internal-error", NULL);
498         afb_proto_ws_call_unref(call);
499 }
500
501 static void server_describe_sjob(int signum, void *closure)
502 {
503         struct json_object *obj;
504         struct server_describe *desc = closure;
505
506         /* get the description if possible */
507         obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
508
509         /* send it */
510         afb_proto_ws_describe_put(desc->describe, obj);
511         json_object_put(obj);
512         afb_stub_ws_unref(desc->stubws);
513 }
514
515 static void server_describe_job(int signum, void *closure)
516 {
517         server_describe_sjob(signum, closure);
518         free(closure);
519 }
520
521 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
522 {
523         struct server_describe *desc, sdesc;
524         struct afb_stub_ws *stubws = closure;
525
526         /* allocate (if possible) and init */
527         desc = malloc(sizeof *desc);
528         if (desc == NULL)
529                 desc = &sdesc;
530         desc->stubws = stubws;
531         desc->describe = describe;
532         afb_stub_ws_addref(stubws);
533
534         /* process */
535         if (desc == &sdesc)
536                 jobs_call(NULL, 0, server_describe_sjob, desc);
537         else {
538                 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
539                         jobs_call(NULL, 0, server_describe_job, desc);
540         }
541 }
542
543 /*****************************************************/
544
545 static const struct afb_proto_ws_client_itf client_itf =
546 {
547         .on_reply_success = on_reply_success,
548         .on_reply_fail = on_reply_fail,
549         .on_event_create = on_event_create,
550         .on_event_remove = on_event_remove,
551         .on_event_subscribe = on_event_subscribe,
552         .on_event_unsubscribe = on_event_unsubscribe,
553         .on_event_push = on_event_push,
554         .on_event_broadcast = on_event_broadcast,
555         .on_subcall = on_subcall
556 };
557
558 static const struct afb_proto_ws_server_itf server_itf =
559 {
560         .on_call = on_call,
561         .on_describe = on_describe
562 };
563
564 static struct afb_api_itf ws_api_itf = {
565         .call = client_call_cb,
566         .describe = client_describe_cb
567 };
568
569 /* the interface for events pushing */
570 static const struct afb_evt_itf server_evt_itf = {
571         .broadcast = server_event_broadcast,
572         .push = server_event_push,
573         .add = server_event_add,
574         .remove = server_event_remove
575 };
576
577 /*****************************************************/
578
579 static void drop_all_events(struct afb_stub_ws *stubws)
580 {
581         struct client_event *ev, *nxt;
582
583         ev = stubws->events;
584         stubws->events = NULL;
585
586         while (ev) {
587                 nxt = ev->next;
588                 afb_evt_eventid_unref(ev->eventid);
589                 free(ev);
590                 ev = nxt;
591         }
592 }
593
594 /* callback when receiving a hangup */
595 static void on_hangup(void *closure)
596 {
597         struct afb_stub_ws *stubws = closure;
598
599         if (stubws->on_hangup)
600                 stubws->on_hangup(stubws);
601 }
602
603 /*****************************************************/
604
605 static struct afb_stub_ws *afb_stub_ws_create(int fd, const char *apiname, struct afb_apiset *apiset, int client)
606 {
607         struct afb_stub_ws *stubws;
608
609         stubws = calloc(1, sizeof *stubws + strlen(apiname));
610         if (stubws == NULL)
611                 errno = ENOMEM;
612         else {
613                 if (client)
614                         stubws->proto = afb_proto_ws_create_client(afb_common_get_event_loop(), fd, &client_itf, stubws);
615                 else
616                         stubws->proto = afb_proto_ws_create_server(afb_common_get_event_loop(), fd, &server_itf, stubws);
617                 if (stubws->proto != NULL) {
618                         strcpy(stubws->apiname, apiname);
619                         stubws->apiset = afb_apiset_addref(apiset);
620                         stubws->refcount = 1;
621                         afb_proto_ws_on_hangup(stubws->proto, on_hangup);
622                         return stubws;
623                 }
624                 free(stubws);
625         }
626         return NULL;
627 }
628
629 struct afb_stub_ws *afb_stub_ws_create_client(int fd, const char *apiname, struct afb_apiset *apiset)
630 {
631         return afb_stub_ws_create(fd, apiname, apiset, 1);
632 }
633
634 struct afb_stub_ws *afb_stub_ws_create_server(int fd, const char *apiname, struct afb_apiset *apiset)
635 {
636         struct afb_stub_ws *stubws;
637
638         stubws = afb_stub_ws_create(fd, apiname, apiset, 0);
639         if (stubws) {
640                 stubws->cred = afb_cred_create_for_socket(fd);
641                 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
642                 if (stubws->listener != NULL)
643                         return stubws;
644                 afb_stub_ws_unref(stubws);
645         }
646         return NULL;
647 }
648
649 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
650 {
651         if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
652                 drop_all_events(stubws);
653                 afb_evt_listener_unref(stubws->listener);
654                 afb_proto_ws_unref(stubws->proto);
655                 afb_cred_unref(stubws->cred);
656                 afb_apiset_unref(stubws->apiset);
657                 free(stubws);
658         }
659 }
660
661 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
662 {
663         __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
664 }
665
666 void afb_stub_ws_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
667 {
668         stubws->on_hangup = on_hangup;
669 }
670
671 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
672 {
673         return stubws->apiname;
674 }
675
676 struct afb_api afb_stub_ws_client_api(struct afb_stub_ws *stubws)
677 {
678         struct afb_api api;
679
680         assert(!stubws->listener); /* check client */
681         api.closure = stubws;
682         api.itf = &ws_api_itf;
683         api.group = NULL;
684         return api;
685 }
686
687 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
688 {
689         return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));
690 }
691