ubusd: move global retmsg per client
[project/ubus.git] / ubusd_proto.c
1 /*
2  * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <arpa/inet.h>
15 #include <unistd.h>
16
17 #include "ubusd.h"
18
19 struct blob_buf b;
20 static struct avl_tree clients;
21
22 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
23
24 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
25
26 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
27         [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
28         [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
29         [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
30         [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
31         [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
32         [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
33         [UBUS_ATTR_USER] = { .type = BLOB_ATTR_STRING },
34         [UBUS_ATTR_GROUP] = { .type = BLOB_ATTR_STRING },
35 };
36
37 struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
38 {
39         blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
40         return attrbuf;
41 }
42
43 static void ubus_msg_close_fd(struct ubus_msg_buf *ub)
44 {
45         if (ub->fd < 0)
46                 return;
47
48         close(ub->fd);
49         ub->fd = -1;
50 }
51
52 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
53 {
54         ub->hdr.version = 0;
55         ub->hdr.type = type;
56         ub->hdr.seq = seq;
57         ub->hdr.peer = peer;
58 }
59
60 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
61 {
62         return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
63 }
64
65 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
66 {
67         struct ubus_msg_buf *new;
68
69         new = ubus_msg_from_blob(shared);
70         if (!new)
71                 return NULL;
72
73         ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
74         return new;
75 }
76
77 void
78 ubus_proto_send_msg_from_blob(struct ubus_client *cl, struct ubus_msg_buf *ub,
79                         uint8_t type)
80 {
81         /* keep the fd to be passed if it is UBUS_MSG_INVOKE */
82         int fd = ub->fd;
83         ub = ubus_reply_from_blob(ub, true);
84         if (!ub)
85                 return;
86
87         ub->hdr.type = type;
88         ub->fd = fd;
89
90         ubus_msg_send(cl, ub);
91         ubus_msg_free(ub);
92 }
93
94 static bool ubusd_send_hello(struct ubus_client *cl)
95 {
96         struct ubus_msg_buf *ub;
97
98         blob_buf_init(&b, 0);
99         ub = ubus_msg_from_blob(true);
100         if (!ub)
101                 return false;
102
103         ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
104         ubus_msg_send(cl, ub);
105         ubus_msg_free(ub);
106         return true;
107 }
108
109 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
110 {
111         ub->hdr.type = UBUS_MSG_DATA;
112         ubus_msg_send(cl, ub);
113         return 0;
114 }
115
116 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
117 {
118         struct ubus_object *obj;
119
120         if (!attr[UBUS_ATTR_OBJID])
121                 return UBUS_STATUS_INVALID_ARGUMENT;
122
123         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
124         if (!obj)
125                 return UBUS_STATUS_NOT_FOUND;
126
127         if (obj->client != cl)
128                 return UBUS_STATUS_PERMISSION_DENIED;
129
130         blob_buf_init(&b, 0);
131         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
132
133         /* check if we're removing the object type as well */
134         if (obj->type && obj->type->refcount == 1)
135                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
136
137         ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
138         ubusd_free_object(obj);
139
140         return 0;
141 }
142
143 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
144 {
145         struct ubus_object *obj;
146
147         obj = ubusd_create_object(cl, attr);
148         if (!obj)
149                 return UBUS_STATUS_INVALID_ARGUMENT;
150
151         blob_buf_init(&b, 0);
152         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
153         if (attr[UBUS_ATTR_SIGNATURE] && obj->type)
154                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
155
156         ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
157         return 0;
158 }
159
160 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
161 {
162         struct ubus_method *m;
163         int all_cnt = 0, cnt = 0;
164         void *s;
165
166         if (!obj->type)
167                 return;
168
169         blob_buf_init(&b, 0);
170
171         blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
172         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
173         blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
174
175         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
176         list_for_each_entry(m, &obj->type->methods, list) {
177                 all_cnt++;
178                 if (!ubusd_acl_check(cl, obj->path.key, blobmsg_name(m->data), UBUS_ACL_ACCESS)) {
179                         blobmsg_add_blob(&b, m->data);
180                         cnt++;
181                 }
182         }
183         blob_nest_end(&b, s);
184
185         if (cnt || !all_cnt)
186                 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
187 }
188
189 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
190 {
191         struct ubus_object *obj;
192         char *objpath;
193         bool found = false;
194         int len;
195
196         if (!attr[UBUS_ATTR_OBJPATH]) {
197                 avl_for_each_element(&path, obj, path)
198                         ubusd_send_obj(cl, ub, obj);
199                 return 0;
200         }
201
202         objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
203         len = strlen(objpath);
204         if (objpath[len - 1] != '*') {
205                 obj = avl_find_element(&path, objpath, obj, path);
206                 if (!obj)
207                         return UBUS_STATUS_NOT_FOUND;
208
209                 ubusd_send_obj(cl, ub, obj);
210                 return 0;
211         }
212
213         objpath[--len] = 0;
214
215         obj = avl_find_ge_element(&path, objpath, obj, path);
216         if (!obj)
217                 return UBUS_STATUS_NOT_FOUND;
218
219         while (!strncmp(objpath, obj->path.key, len)) {
220                 found = true;
221                 ubusd_send_obj(cl, ub, obj);
222                 if (obj == avl_last_element(&path, obj, path))
223                         break;
224                 obj = avl_next_element(obj, path);
225         }
226
227         if (!found)
228                 return UBUS_STATUS_NOT_FOUND;
229
230         return 0;
231 }
232
233 static void
234 ubusd_forward_invoke(struct ubus_client *cl, struct ubus_object *obj,
235                      const char *method, struct ubus_msg_buf *ub,
236                      struct blob_attr *data)
237 {
238         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
239         blob_put_string(&b, UBUS_ATTR_METHOD, method);
240         if (cl->user)
241                 blob_put_string(&b, UBUS_ATTR_USER, cl->user);
242         if (cl->group)
243                 blob_put_string(&b, UBUS_ATTR_GROUP, cl->group);
244         if (data)
245                 blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
246
247         ubus_proto_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
248 }
249
250 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
251 {
252         struct ubus_object *obj = NULL;
253         struct ubus_id *id;
254         const char *method;
255
256         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
257                 return UBUS_STATUS_INVALID_ARGUMENT;
258
259         id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
260         if (!id)
261                 return UBUS_STATUS_NOT_FOUND;
262
263         obj = container_of(id, struct ubus_object, id);
264
265         method = blob_data(attr[UBUS_ATTR_METHOD]);
266
267         if (ubusd_acl_check(cl, obj->path.key, method, UBUS_ACL_ACCESS))
268                 return UBUS_STATUS_PERMISSION_DENIED;
269
270         if (!obj->client)
271                 return obj->recv_msg(cl, ub, method, attr[UBUS_ATTR_DATA]);
272
273         ub->hdr.peer = cl->id.id;
274         blob_buf_init(&b, 0);
275
276         ubusd_forward_invoke(cl, obj, method, ub, attr[UBUS_ATTR_DATA]);
277
278         return -1;
279 }
280
281 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
282 {
283         struct ubus_object *obj = NULL;
284         struct ubus_subscription *s;
285         struct ubus_id *id;
286         const char *method;
287         bool no_reply = false;
288         void *c;
289
290         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
291                 return UBUS_STATUS_INVALID_ARGUMENT;
292
293         if (attr[UBUS_ATTR_NO_REPLY])
294                 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
295
296         id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
297         if (!id)
298                 return UBUS_STATUS_NOT_FOUND;
299
300         obj = container_of(id, struct ubus_object, id);
301         if (obj->client != cl)
302                 return UBUS_STATUS_PERMISSION_DENIED;
303
304         if (!no_reply) {
305                 blob_buf_init(&b, 0);
306                 blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
307                 c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
308                 list_for_each_entry(s, &obj->subscribers, list) {
309                         blob_put_int32(&b, 0, s->subscriber->id.id);
310                 }
311                 blob_nest_end(&b, c);
312                 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
313                 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
314         }
315
316         ub->hdr.peer = cl->id.id;
317         method = blob_data(attr[UBUS_ATTR_METHOD]);
318         list_for_each_entry(s, &obj->subscribers, list) {
319                 blob_buf_init(&b, 0);
320                 if (no_reply)
321                         blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
322                 ubusd_forward_invoke(cl, s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
323         }
324
325         return -1;
326 }
327
328 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
329 {
330         struct ubus_id *clid;
331
332         clid = ubus_find_id(&clients, id);
333         if (!clid)
334                 return NULL;
335
336         return container_of(clid, struct ubus_client, id);
337 }
338
339 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
340 {
341         struct ubus_object *obj;
342
343         if (!attr[UBUS_ATTR_OBJID] ||
344             (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
345             (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
346                 goto out;
347
348         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
349         if (!obj)
350                 goto out;
351
352         if (cl != obj->client)
353                 goto out;
354
355         cl = ubusd_get_client_by_id(ub->hdr.peer);
356         if (!cl)
357                 goto out;
358
359         ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
360         ubus_msg_send(cl, ub);
361 out:
362         return -1;
363 }
364
365 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
366 {
367         struct ubus_object *obj, *target;
368
369         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
370                 return UBUS_STATUS_INVALID_ARGUMENT;
371
372         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
373         if (!obj)
374                 return UBUS_STATUS_NOT_FOUND;
375
376         if (cl != obj->client)
377                 return UBUS_STATUS_INVALID_ARGUMENT;
378
379         target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
380         if (!target || !target->client)
381                 return UBUS_STATUS_NOT_FOUND;
382
383         if (cl == target->client)
384                 return UBUS_STATUS_INVALID_ARGUMENT;
385
386         if (!target->path.key) {
387                 if (strcmp(target->client->user, cl->user) && strcmp(target->client->group, cl->group))
388                         return UBUS_STATUS_NOT_FOUND;
389         } else if (ubusd_acl_check(cl, target->path.key, NULL, UBUS_ACL_SUBSCRIBE)) {
390                 return UBUS_STATUS_NOT_FOUND;
391         }
392
393         ubus_subscribe(obj, target);
394         return 0;
395 }
396
397 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
398 {
399         struct ubus_object *obj;
400         struct ubus_subscription *s;
401         uint32_t id;
402
403         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
404                 return UBUS_STATUS_INVALID_ARGUMENT;
405
406         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
407         if (!obj)
408                 return UBUS_STATUS_NOT_FOUND;
409
410         if (cl != obj->client)
411                 return UBUS_STATUS_INVALID_ARGUMENT;
412
413         id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
414         list_for_each_entry(s, &obj->target_list, target_list) {
415                 if (s->target->id.id != id)
416                         continue;
417
418                 ubus_unsubscribe(s);
419                 return 0;
420         }
421
422         return UBUS_STATUS_NOT_FOUND;
423 }
424
425 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
426         [UBUS_MSG_PING] = ubusd_send_pong,
427         [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
428         [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
429         [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
430         [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
431         [UBUS_MSG_STATUS] = ubusd_handle_response,
432         [UBUS_MSG_DATA] = ubusd_handle_response,
433         [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
434         [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
435         [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
436 };
437
438 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
439 {
440         ubus_cmd_cb cb = NULL;
441         int ret;
442         struct ubus_msg_buf *retmsg = cl->retmsg;
443         int *retmsg_data = blob_data(blob_data(retmsg->data));
444
445         retmsg->hdr.seq = ub->hdr.seq;
446         retmsg->hdr.peer = ub->hdr.peer;
447
448         if (ub->hdr.type < __UBUS_MSG_LAST)
449                 cb = handlers[ub->hdr.type];
450
451         if (ub->hdr.type != UBUS_MSG_STATUS && ub->hdr.type != UBUS_MSG_INVOKE)
452                 ubus_msg_close_fd(ub);
453
454         /* Note: no callback should free the `ub` buffer
455                  that's always done right after the callback finishes */
456         if (cb)
457                 ret = cb(cl, ub, ubus_parse_msg(ub->data));
458         else
459                 ret = UBUS_STATUS_INVALID_COMMAND;
460
461         ubus_msg_free(ub);
462
463         if (ret == -1)
464                 return;
465
466         *retmsg_data = htonl(ret);
467         ubus_msg_send(cl, retmsg);
468 }
469
470 static int ubusd_proto_init_retmsg(struct ubus_client *cl)
471 {
472         struct blob_buf *b = &cl->b;
473
474         blob_buf_init(&cl->b, 0);
475         blob_put_int32(&cl->b, UBUS_ATTR_STATUS, 0);
476
477         /* we make the 'retmsg' buffer shared with the blob_buf b, to reduce mem duplication */
478         cl->retmsg = ubus_msg_new(b->head, blob_raw_len(b->head), true);
479         if (!cl->retmsg)
480                 return -1;
481
482         cl->retmsg->hdr.type = UBUS_MSG_STATUS;
483         return 0;
484 }
485
486 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
487 {
488         struct ubus_client *cl;
489
490         cl = calloc(1, sizeof(*cl));
491         if (!cl)
492                 return NULL;
493
494         if (ubusd_acl_init_client(cl, fd))
495                 goto free;
496
497         INIT_LIST_HEAD(&cl->objects);
498         cl->sock.fd = fd;
499         cl->sock.cb = cb;
500         cl->pending_msg_fd = -1;
501
502         if (!ubus_alloc_id(&clients, &cl->id, 0))
503                 goto free;
504
505         if (ubusd_proto_init_retmsg(cl))
506                 goto free;
507
508         if (!ubusd_send_hello(cl))
509                 goto delete;
510
511         return cl;
512
513 delete:
514         ubus_free_id(&clients, &cl->id);
515 free:
516         free(cl);
517         return NULL;
518 }
519
520 void ubusd_proto_free_client(struct ubus_client *cl)
521 {
522         struct ubus_object *obj;
523
524         while (!list_empty(&cl->objects)) {
525                 obj = list_first_entry(&cl->objects, struct ubus_object, list);
526                 ubusd_free_object(obj);
527         }
528         ubus_msg_free(cl->retmsg);
529         blob_buf_free(&cl->b);
530
531         ubusd_acl_free_client(cl);
532         ubus_free_id(&clients, &cl->id);
533 }
534
535 void ubus_notify_subscription(struct ubus_object *obj)
536 {
537         bool active = !list_empty(&obj->subscribers);
538         struct ubus_msg_buf *ub;
539
540         blob_buf_init(&b, 0);
541         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
542         blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
543
544         ub = ubus_msg_from_blob(false);
545         if (!ub)
546                 return;
547
548         ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
549         ubus_msg_send(obj->client, ub);
550         ubus_msg_free(ub);
551 }
552
553 void ubus_notify_unsubscribe(struct ubus_subscription *s)
554 {
555         struct ubus_msg_buf *ub;
556
557         blob_buf_init(&b, 0);
558         blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
559         blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
560
561         ub = ubus_msg_from_blob(false);
562         if (ub != NULL) {
563                 ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
564                 ubus_msg_send(s->subscriber->client, ub);
565                 ubus_msg_free(ub);
566         }
567
568         ubus_unsubscribe(s);
569 }
570
571 static void __constructor ubusd_proto_init(void)
572 {
573         ubus_init_id_tree(&clients);
574 }