b591384283ed0669e3b2f0234ec11c31a2c3f74f
[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 ubus_msg_buf *retmsg;
21 static int *retmsg_data;
22 static struct avl_tree clients;
23
24 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
25
26 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
27
28 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
29         [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
30         [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
31         [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
32         [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
33         [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
34         [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
35         [UBUS_ATTR_USER] = { .type = BLOB_ATTR_STRING },
36         [UBUS_ATTR_GROUP] = { .type = BLOB_ATTR_STRING },
37 };
38
39 struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
40 {
41         blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
42         return attrbuf;
43 }
44
45 static void ubus_msg_close_fd(struct ubus_msg_buf *ub)
46 {
47         if (ub->fd < 0)
48                 return;
49
50         close(ub->fd);
51         ub->fd = -1;
52 }
53
54 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
55 {
56         ub->hdr.version = 0;
57         ub->hdr.type = type;
58         ub->hdr.seq = seq;
59         ub->hdr.peer = peer;
60 }
61
62 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
63 {
64         return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
65 }
66
67 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
68 {
69         struct ubus_msg_buf *new;
70
71         new = ubus_msg_from_blob(shared);
72         if (!new)
73                 return NULL;
74
75         ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
76         return new;
77 }
78
79 void
80 ubus_proto_send_msg_from_blob(struct ubus_client *cl, struct ubus_msg_buf *ub,
81                         uint8_t type)
82 {
83         ub = ubus_reply_from_blob(ub, true);
84         if (!ub)
85                 return;
86
87         ub->hdr.type = type;
88         ubus_msg_send(cl, ub, true);
89 }
90
91 static bool ubusd_send_hello(struct ubus_client *cl)
92 {
93         struct ubus_msg_buf *ub;
94
95         blob_buf_init(&b, 0);
96         ub = ubus_msg_from_blob(true);
97         if (!ub)
98                 return false;
99
100         ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
101         ubus_msg_send(cl, ub, true);
102         return true;
103 }
104
105 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
106 {
107         ub->hdr.type = UBUS_MSG_DATA;
108         ubus_msg_send(cl, ub, false);
109         return 0;
110 }
111
112 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
113 {
114         struct ubus_object *obj;
115
116         if (!attr[UBUS_ATTR_OBJID])
117                 return UBUS_STATUS_INVALID_ARGUMENT;
118
119         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
120         if (!obj)
121                 return UBUS_STATUS_NOT_FOUND;
122
123         if (obj->client != cl)
124                 return UBUS_STATUS_PERMISSION_DENIED;
125
126         blob_buf_init(&b, 0);
127         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
128
129         /* check if we're removing the object type as well */
130         if (obj->type && obj->type->refcount == 1)
131                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
132
133         ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
134         ubusd_free_object(obj);
135
136         return 0;
137 }
138
139 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
140 {
141         struct ubus_object *obj;
142
143         obj = ubusd_create_object(cl, attr);
144         if (!obj)
145                 return UBUS_STATUS_INVALID_ARGUMENT;
146
147         blob_buf_init(&b, 0);
148         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
149         if (attr[UBUS_ATTR_SIGNATURE] && obj->type)
150                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
151
152         ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
153         return 0;
154 }
155
156 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
157 {
158         struct ubus_method *m;
159         int cnt = 0;
160         void *s;
161
162         if (!obj->type)
163                 return;
164
165         blob_buf_init(&b, 0);
166
167         blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
168         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
169         blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
170
171         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
172         list_for_each_entry(m, &obj->type->methods, list) {
173                 if (!ubusd_acl_check(cl, obj->path.key, blobmsg_name(m->data), UBUS_ACL_ACCESS)) {
174                         blobmsg_add_blob(&b, m->data);
175                         cnt++;
176                 }
177         }
178         blob_nest_end(&b, s);
179
180         if (cnt)
181                 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
182 }
183
184 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
185 {
186         struct ubus_object *obj;
187         char *objpath;
188         bool found = false;
189         int len;
190
191         if (!attr[UBUS_ATTR_OBJPATH]) {
192                 avl_for_each_element(&path, obj, path)
193                         ubusd_send_obj(cl, ub, obj);
194                 return 0;
195         }
196
197         objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
198         len = strlen(objpath);
199         if (objpath[len - 1] != '*') {
200                 obj = avl_find_element(&path, objpath, obj, path);
201                 if (!obj)
202                         return UBUS_STATUS_NOT_FOUND;
203
204                 ubusd_send_obj(cl, ub, obj);
205                 return 0;
206         }
207
208         objpath[--len] = 0;
209
210         obj = avl_find_ge_element(&path, objpath, obj, path);
211         if (!obj)
212                 return UBUS_STATUS_NOT_FOUND;
213
214         while (!strncmp(objpath, obj->path.key, len)) {
215                 found = true;
216                 ubusd_send_obj(cl, ub, obj);
217                 if (obj == avl_last_element(&path, obj, path))
218                         break;
219                 obj = avl_next_element(obj, path);
220         }
221
222         if (!found)
223                 return UBUS_STATUS_NOT_FOUND;
224
225         return 0;
226 }
227
228 static void
229 ubusd_forward_invoke(struct ubus_client *cl, struct ubus_object *obj,
230                      const char *method, struct ubus_msg_buf *ub,
231                      struct blob_attr *data)
232 {
233         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
234         blob_put_string(&b, UBUS_ATTR_METHOD, method);
235         if (cl->user)
236                 blob_put_string(&b, UBUS_ATTR_USER, cl->user);
237         if (cl->group)
238                 blob_put_string(&b, UBUS_ATTR_GROUP, cl->group);
239         if (data)
240                 blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
241
242         ubus_proto_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
243 }
244
245 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
246 {
247         struct ubus_object *obj = NULL;
248         struct ubus_id *id;
249         const char *method;
250
251         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
252                 return UBUS_STATUS_INVALID_ARGUMENT;
253
254         id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
255         if (!id)
256                 return UBUS_STATUS_NOT_FOUND;
257
258         obj = container_of(id, struct ubus_object, id);
259
260         method = blob_data(attr[UBUS_ATTR_METHOD]);
261
262         if (ubusd_acl_check(cl, obj->path.key, method, UBUS_ACL_ACCESS))
263                 return UBUS_STATUS_PERMISSION_DENIED;
264
265         if (!obj->client)
266                 return obj->recv_msg(cl, ub, method, attr[UBUS_ATTR_DATA]);
267
268         ub->hdr.peer = cl->id.id;
269         blob_buf_init(&b, 0);
270
271         ubusd_forward_invoke(cl, obj, method, ub, attr[UBUS_ATTR_DATA]);
272         ubus_msg_free(ub);
273
274         return -1;
275 }
276
277 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
278 {
279         struct ubus_object *obj = NULL;
280         struct ubus_subscription *s;
281         struct ubus_id *id;
282         const char *method;
283         bool no_reply = false;
284         void *c;
285
286         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
287                 return UBUS_STATUS_INVALID_ARGUMENT;
288
289         if (attr[UBUS_ATTR_NO_REPLY])
290                 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
291
292         id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
293         if (!id)
294                 return UBUS_STATUS_NOT_FOUND;
295
296         obj = container_of(id, struct ubus_object, id);
297         if (obj->client != cl)
298                 return UBUS_STATUS_PERMISSION_DENIED;
299
300         if (!no_reply) {
301                 blob_buf_init(&b, 0);
302                 blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
303                 c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
304                 list_for_each_entry(s, &obj->subscribers, list) {
305                         blob_put_int32(&b, 0, s->subscriber->id.id);
306                 }
307                 blob_nest_end(&b, c);
308                 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
309                 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
310         }
311
312         ub->hdr.peer = cl->id.id;
313         method = blob_data(attr[UBUS_ATTR_METHOD]);
314         list_for_each_entry(s, &obj->subscribers, list) {
315                 blob_buf_init(&b, 0);
316                 if (no_reply)
317                         blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
318                 ubusd_forward_invoke(cl, s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
319         }
320         ubus_msg_free(ub);
321
322         return -1;
323 }
324
325 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
326 {
327         struct ubus_id *clid;
328
329         clid = ubus_find_id(&clients, id);
330         if (!clid)
331                 return NULL;
332
333         return container_of(clid, struct ubus_client, id);
334 }
335
336 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
337 {
338         struct ubus_object *obj;
339
340         if (!attr[UBUS_ATTR_OBJID] ||
341             (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
342             (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
343                 goto error;
344
345         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
346         if (!obj)
347                 goto error;
348
349         if (cl != obj->client)
350                 goto error;
351
352         cl = ubusd_get_client_by_id(ub->hdr.peer);
353         if (!cl)
354                 goto error;
355
356         ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
357         ubus_msg_send(cl, ub, true);
358         return -1;
359
360 error:
361         ubus_msg_free(ub);
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
443         retmsg->hdr.seq = ub->hdr.seq;
444         retmsg->hdr.peer = ub->hdr.peer;
445
446         if (ub->hdr.type < __UBUS_MSG_LAST)
447                 cb = handlers[ub->hdr.type];
448
449         if (ub->hdr.type != UBUS_MSG_STATUS)
450                 ubus_msg_close_fd(ub);
451
452         if (cb)
453                 ret = cb(cl, ub, ubus_parse_msg(ub->data));
454         else
455                 ret = UBUS_STATUS_INVALID_COMMAND;
456
457         if (ret == -1)
458                 return;
459
460         ubus_msg_free(ub);
461
462         *retmsg_data = htonl(ret);
463         ubus_msg_send(cl, retmsg, false);
464 }
465
466 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
467 {
468         struct ubus_client *cl;
469
470         cl = calloc(1, sizeof(*cl));
471         if (!cl)
472                 return NULL;
473
474         if (ubusd_acl_init_client(cl, fd))
475                 goto free;
476
477         INIT_LIST_HEAD(&cl->objects);
478         cl->sock.fd = fd;
479         cl->sock.cb = cb;
480         cl->pending_msg_fd = -1;
481
482         if (!ubus_alloc_id(&clients, &cl->id, 0))
483                 goto free;
484
485         if (!ubusd_send_hello(cl))
486                 goto delete;
487
488         return cl;
489
490 delete:
491         ubus_free_id(&clients, &cl->id);
492 free:
493         free(cl);
494         return NULL;
495 }
496
497 void ubusd_proto_free_client(struct ubus_client *cl)
498 {
499         struct ubus_object *obj;
500
501         while (!list_empty(&cl->objects)) {
502                 obj = list_first_entry(&cl->objects, struct ubus_object, list);
503                 ubusd_free_object(obj);
504         }
505
506         ubusd_acl_free_client(cl);
507         ubus_free_id(&clients, &cl->id);
508 }
509
510 void ubus_notify_subscription(struct ubus_object *obj)
511 {
512         bool active = !list_empty(&obj->subscribers);
513         struct ubus_msg_buf *ub;
514
515         blob_buf_init(&b, 0);
516         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
517         blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
518
519         ub = ubus_msg_from_blob(false);
520         if (!ub)
521                 return;
522
523         ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
524         ubus_msg_send(obj->client, ub, true);
525 }
526
527 void ubus_notify_unsubscribe(struct ubus_subscription *s)
528 {
529         struct ubus_msg_buf *ub;
530
531         blob_buf_init(&b, 0);
532         blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
533         blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
534
535         ub = ubus_msg_from_blob(false);
536         if (ub != NULL) {
537                 ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
538                 ubus_msg_send(s->subscriber->client, ub, true);
539         }
540
541         ubus_unsubscribe(s);
542 }
543
544 static void __constructor ubusd_proto_init(void)
545 {
546         ubus_init_id_tree(&clients);
547
548         blob_buf_init(&b, 0);
549         blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
550
551         retmsg = ubus_msg_from_blob(false);
552         if (!retmsg)
553                 exit(1);
554
555         retmsg->hdr.type = UBUS_MSG_STATUS;
556         retmsg_data = blob_data(blob_data(retmsg->data));
557 }