d2feed9aa3ae027b648586ade4d5803d23b51960
[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 all_cnt = 0, 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 all_cnt++;
174 if (!ubusd_acl_check(cl, obj->path.key, blobmsg_name(m->data), UBUS_ACL_ACCESS)) {
175 blobmsg_add_blob(&b, m->data);
176 cnt++;
177 }
178 }
179 blob_nest_end(&b, s);
180
181 if (cnt || !all_cnt)
182 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
183 }
184
185 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
186 {
187 struct ubus_object *obj;
188 char *objpath;
189 bool found = false;
190 int len;
191
192 if (!attr[UBUS_ATTR_OBJPATH]) {
193 avl_for_each_element(&path, obj, path)
194 ubusd_send_obj(cl, ub, obj);
195 return 0;
196 }
197
198 objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
199 len = strlen(objpath);
200 if (objpath[len - 1] != '*') {
201 obj = avl_find_element(&path, objpath, obj, path);
202 if (!obj)
203 return UBUS_STATUS_NOT_FOUND;
204
205 ubusd_send_obj(cl, ub, obj);
206 return 0;
207 }
208
209 objpath[--len] = 0;
210
211 obj = avl_find_ge_element(&path, objpath, obj, path);
212 if (!obj)
213 return UBUS_STATUS_NOT_FOUND;
214
215 while (!strncmp(objpath, obj->path.key, len)) {
216 found = true;
217 ubusd_send_obj(cl, ub, obj);
218 if (obj == avl_last_element(&path, obj, path))
219 break;
220 obj = avl_next_element(obj, path);
221 }
222
223 if (!found)
224 return UBUS_STATUS_NOT_FOUND;
225
226 return 0;
227 }
228
229 static void
230 ubusd_forward_invoke(struct ubus_client *cl, struct ubus_object *obj,
231 const char *method, struct ubus_msg_buf *ub,
232 struct blob_attr *data)
233 {
234 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
235 blob_put_string(&b, UBUS_ATTR_METHOD, method);
236 if (cl->user)
237 blob_put_string(&b, UBUS_ATTR_USER, cl->user);
238 if (cl->group)
239 blob_put_string(&b, UBUS_ATTR_GROUP, cl->group);
240 if (data)
241 blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
242
243 ubus_proto_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
244 }
245
246 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
247 {
248 struct ubus_object *obj = NULL;
249 struct ubus_id *id;
250 const char *method;
251
252 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
253 return UBUS_STATUS_INVALID_ARGUMENT;
254
255 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
256 if (!id)
257 return UBUS_STATUS_NOT_FOUND;
258
259 obj = container_of(id, struct ubus_object, id);
260
261 method = blob_data(attr[UBUS_ATTR_METHOD]);
262
263 if (ubusd_acl_check(cl, obj->path.key, method, UBUS_ACL_ACCESS))
264 return UBUS_STATUS_PERMISSION_DENIED;
265
266 if (!obj->client)
267 return obj->recv_msg(cl, ub, method, attr[UBUS_ATTR_DATA]);
268
269 ub->hdr.peer = cl->id.id;
270 blob_buf_init(&b, 0);
271
272 ubusd_forward_invoke(cl, obj, method, ub, attr[UBUS_ATTR_DATA]);
273 ubus_msg_free(ub);
274
275 return -1;
276 }
277
278 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
279 {
280 struct ubus_object *obj = NULL;
281 struct ubus_subscription *s;
282 struct ubus_id *id;
283 const char *method;
284 bool no_reply = false;
285 void *c;
286
287 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
288 return UBUS_STATUS_INVALID_ARGUMENT;
289
290 if (attr[UBUS_ATTR_NO_REPLY])
291 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
292
293 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
294 if (!id)
295 return UBUS_STATUS_NOT_FOUND;
296
297 obj = container_of(id, struct ubus_object, id);
298 if (obj->client != cl)
299 return UBUS_STATUS_PERMISSION_DENIED;
300
301 if (!no_reply) {
302 blob_buf_init(&b, 0);
303 blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
304 c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
305 list_for_each_entry(s, &obj->subscribers, list) {
306 blob_put_int32(&b, 0, s->subscriber->id.id);
307 }
308 blob_nest_end(&b, c);
309 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
310 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
311 }
312
313 ub->hdr.peer = cl->id.id;
314 method = blob_data(attr[UBUS_ATTR_METHOD]);
315 list_for_each_entry(s, &obj->subscribers, list) {
316 blob_buf_init(&b, 0);
317 if (no_reply)
318 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
319 ubusd_forward_invoke(cl, s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
320 }
321 ubus_msg_free(ub);
322
323 return -1;
324 }
325
326 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
327 {
328 struct ubus_id *clid;
329
330 clid = ubus_find_id(&clients, id);
331 if (!clid)
332 return NULL;
333
334 return container_of(clid, struct ubus_client, id);
335 }
336
337 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
338 {
339 struct ubus_object *obj;
340
341 if (!attr[UBUS_ATTR_OBJID] ||
342 (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
343 (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
344 goto error;
345
346 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
347 if (!obj)
348 goto error;
349
350 if (cl != obj->client)
351 goto error;
352
353 cl = ubusd_get_client_by_id(ub->hdr.peer);
354 if (!cl)
355 goto error;
356
357 ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
358 ubus_msg_send(cl, ub, true);
359 return -1;
360
361 error:
362 ubus_msg_free(ub);
363 return -1;
364 }
365
366 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
367 {
368 struct ubus_object *obj, *target;
369
370 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
371 return UBUS_STATUS_INVALID_ARGUMENT;
372
373 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
374 if (!obj)
375 return UBUS_STATUS_NOT_FOUND;
376
377 if (cl != obj->client)
378 return UBUS_STATUS_INVALID_ARGUMENT;
379
380 target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
381 if (!target || !target->client)
382 return UBUS_STATUS_NOT_FOUND;
383
384 if (cl == target->client)
385 return UBUS_STATUS_INVALID_ARGUMENT;
386
387 if (!target->path.key) {
388 if (strcmp(target->client->user, cl->user) && strcmp(target->client->group, cl->group))
389 return UBUS_STATUS_NOT_FOUND;
390 } else if (ubusd_acl_check(cl, target->path.key, NULL, UBUS_ACL_SUBSCRIBE)) {
391 return UBUS_STATUS_NOT_FOUND;
392 }
393
394 ubus_subscribe(obj, target);
395 return 0;
396 }
397
398 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
399 {
400 struct ubus_object *obj;
401 struct ubus_subscription *s;
402 uint32_t id;
403
404 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
405 return UBUS_STATUS_INVALID_ARGUMENT;
406
407 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
408 if (!obj)
409 return UBUS_STATUS_NOT_FOUND;
410
411 if (cl != obj->client)
412 return UBUS_STATUS_INVALID_ARGUMENT;
413
414 id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
415 list_for_each_entry(s, &obj->target_list, target_list) {
416 if (s->target->id.id != id)
417 continue;
418
419 ubus_unsubscribe(s);
420 return 0;
421 }
422
423 return UBUS_STATUS_NOT_FOUND;
424 }
425
426 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
427 [UBUS_MSG_PING] = ubusd_send_pong,
428 [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
429 [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
430 [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
431 [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
432 [UBUS_MSG_STATUS] = ubusd_handle_response,
433 [UBUS_MSG_DATA] = ubusd_handle_response,
434 [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
435 [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
436 [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
437 };
438
439 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
440 {
441 ubus_cmd_cb cb = NULL;
442 int ret;
443
444 retmsg->hdr.seq = ub->hdr.seq;
445 retmsg->hdr.peer = ub->hdr.peer;
446
447 if (ub->hdr.type < __UBUS_MSG_LAST)
448 cb = handlers[ub->hdr.type];
449
450 if (ub->hdr.type != UBUS_MSG_STATUS)
451 ubus_msg_close_fd(ub);
452
453 if (cb)
454 ret = cb(cl, ub, ubus_parse_msg(ub->data));
455 else
456 ret = UBUS_STATUS_INVALID_COMMAND;
457
458 if (ret == -1)
459 return;
460
461 ubus_msg_free(ub);
462
463 *retmsg_data = htonl(ret);
464 ubus_msg_send(cl, retmsg, false);
465 }
466
467 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
468 {
469 struct ubus_client *cl;
470
471 cl = calloc(1, sizeof(*cl));
472 if (!cl)
473 return NULL;
474
475 if (ubusd_acl_init_client(cl, fd))
476 goto free;
477
478 INIT_LIST_HEAD(&cl->objects);
479 cl->sock.fd = fd;
480 cl->sock.cb = cb;
481 cl->pending_msg_fd = -1;
482
483 if (!ubus_alloc_id(&clients, &cl->id, 0))
484 goto free;
485
486 if (!ubusd_send_hello(cl))
487 goto delete;
488
489 return cl;
490
491 delete:
492 ubus_free_id(&clients, &cl->id);
493 free:
494 free(cl);
495 return NULL;
496 }
497
498 void ubusd_proto_free_client(struct ubus_client *cl)
499 {
500 struct ubus_object *obj;
501
502 while (!list_empty(&cl->objects)) {
503 obj = list_first_entry(&cl->objects, struct ubus_object, list);
504 ubusd_free_object(obj);
505 }
506
507 ubusd_acl_free_client(cl);
508 ubus_free_id(&clients, &cl->id);
509 }
510
511 void ubus_notify_subscription(struct ubus_object *obj)
512 {
513 bool active = !list_empty(&obj->subscribers);
514 struct ubus_msg_buf *ub;
515
516 blob_buf_init(&b, 0);
517 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
518 blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
519
520 ub = ubus_msg_from_blob(false);
521 if (!ub)
522 return;
523
524 ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
525 ubus_msg_send(obj->client, ub, true);
526 }
527
528 void ubus_notify_unsubscribe(struct ubus_subscription *s)
529 {
530 struct ubus_msg_buf *ub;
531
532 blob_buf_init(&b, 0);
533 blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
534 blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
535
536 ub = ubus_msg_from_blob(false);
537 if (ub != NULL) {
538 ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
539 ubus_msg_send(s->subscriber->client, ub, true);
540 }
541
542 ubus_unsubscribe(s);
543 }
544
545 static void __constructor ubusd_proto_init(void)
546 {
547 ubus_init_id_tree(&clients);
548
549 blob_buf_init(&b, 0);
550 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
551
552 retmsg = ubus_msg_from_blob(false);
553 if (!retmsg)
554 exit(1);
555
556 retmsg->hdr.type = UBUS_MSG_STATUS;
557 retmsg_data = blob_data(blob_data(retmsg->data));
558 }