CMakeLists.txt: bump minimum cmake version
[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 /* keep the fd to be passed if it is UBUS_MSG_INVOKE */
84 int fd = ub->fd;
85 ub = ubus_reply_from_blob(ub, true);
86 if (!ub)
87 return;
88
89 ub->hdr.type = type;
90 ub->fd = fd;
91
92 ubus_msg_send(cl, ub, true);
93 }
94
95 static bool ubusd_send_hello(struct ubus_client *cl)
96 {
97 struct ubus_msg_buf *ub;
98
99 blob_buf_init(&b, 0);
100 ub = ubus_msg_from_blob(true);
101 if (!ub)
102 return false;
103
104 ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
105 ubus_msg_send(cl, ub, true);
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, false);
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 ubus_msg_free(ub);
278
279 return -1;
280 }
281
282 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
283 {
284 struct ubus_object *obj = NULL;
285 struct ubus_subscription *s;
286 struct ubus_id *id;
287 const char *method;
288 bool no_reply = false;
289 void *c;
290
291 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
292 return UBUS_STATUS_INVALID_ARGUMENT;
293
294 if (attr[UBUS_ATTR_NO_REPLY])
295 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
296
297 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
298 if (!id)
299 return UBUS_STATUS_NOT_FOUND;
300
301 obj = container_of(id, struct ubus_object, id);
302 if (obj->client != cl)
303 return UBUS_STATUS_PERMISSION_DENIED;
304
305 if (!no_reply) {
306 blob_buf_init(&b, 0);
307 blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
308 c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
309 list_for_each_entry(s, &obj->subscribers, list) {
310 blob_put_int32(&b, 0, s->subscriber->id.id);
311 }
312 blob_nest_end(&b, c);
313 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
314 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
315 }
316
317 ub->hdr.peer = cl->id.id;
318 method = blob_data(attr[UBUS_ATTR_METHOD]);
319 list_for_each_entry(s, &obj->subscribers, list) {
320 blob_buf_init(&b, 0);
321 if (no_reply)
322 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
323 ubusd_forward_invoke(cl, s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
324 }
325 ubus_msg_free(ub);
326
327 return -1;
328 }
329
330 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
331 {
332 struct ubus_id *clid;
333
334 clid = ubus_find_id(&clients, id);
335 if (!clid)
336 return NULL;
337
338 return container_of(clid, struct ubus_client, id);
339 }
340
341 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
342 {
343 struct ubus_object *obj;
344
345 if (!attr[UBUS_ATTR_OBJID] ||
346 (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
347 (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
348 goto error;
349
350 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
351 if (!obj)
352 goto error;
353
354 if (cl != obj->client)
355 goto error;
356
357 cl = ubusd_get_client_by_id(ub->hdr.peer);
358 if (!cl)
359 goto error;
360
361 ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
362 ubus_msg_send(cl, ub, true);
363 return -1;
364
365 error:
366 ubus_msg_free(ub);
367 return -1;
368 }
369
370 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
371 {
372 struct ubus_object *obj, *target;
373
374 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
375 return UBUS_STATUS_INVALID_ARGUMENT;
376
377 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
378 if (!obj)
379 return UBUS_STATUS_NOT_FOUND;
380
381 if (cl != obj->client)
382 return UBUS_STATUS_INVALID_ARGUMENT;
383
384 target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
385 if (!target || !target->client)
386 return UBUS_STATUS_NOT_FOUND;
387
388 if (cl == target->client)
389 return UBUS_STATUS_INVALID_ARGUMENT;
390
391 if (!target->path.key) {
392 if (strcmp(target->client->user, cl->user) && strcmp(target->client->group, cl->group))
393 return UBUS_STATUS_NOT_FOUND;
394 } else if (ubusd_acl_check(cl, target->path.key, NULL, UBUS_ACL_SUBSCRIBE)) {
395 return UBUS_STATUS_NOT_FOUND;
396 }
397
398 ubus_subscribe(obj, target);
399 return 0;
400 }
401
402 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
403 {
404 struct ubus_object *obj;
405 struct ubus_subscription *s;
406 uint32_t id;
407
408 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
409 return UBUS_STATUS_INVALID_ARGUMENT;
410
411 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
412 if (!obj)
413 return UBUS_STATUS_NOT_FOUND;
414
415 if (cl != obj->client)
416 return UBUS_STATUS_INVALID_ARGUMENT;
417
418 id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
419 list_for_each_entry(s, &obj->target_list, target_list) {
420 if (s->target->id.id != id)
421 continue;
422
423 ubus_unsubscribe(s);
424 return 0;
425 }
426
427 return UBUS_STATUS_NOT_FOUND;
428 }
429
430 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
431 [UBUS_MSG_PING] = ubusd_send_pong,
432 [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
433 [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
434 [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
435 [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
436 [UBUS_MSG_STATUS] = ubusd_handle_response,
437 [UBUS_MSG_DATA] = ubusd_handle_response,
438 [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
439 [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
440 [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
441 };
442
443 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
444 {
445 ubus_cmd_cb cb = NULL;
446 int ret;
447
448 retmsg->hdr.seq = ub->hdr.seq;
449 retmsg->hdr.peer = ub->hdr.peer;
450
451 if (ub->hdr.type < __UBUS_MSG_LAST)
452 cb = handlers[ub->hdr.type];
453
454 if (ub->hdr.type != UBUS_MSG_STATUS && ub->hdr.type != UBUS_MSG_INVOKE)
455 ubus_msg_close_fd(ub);
456
457 if (cb)
458 ret = cb(cl, ub, ubus_parse_msg(ub->data));
459 else
460 ret = UBUS_STATUS_INVALID_COMMAND;
461
462 if (ret == -1)
463 return;
464
465 ubus_msg_free(ub);
466
467 *retmsg_data = htonl(ret);
468 ubus_msg_send(cl, retmsg, false);
469 }
470
471 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
472 {
473 struct ubus_client *cl;
474
475 cl = calloc(1, sizeof(*cl));
476 if (!cl)
477 return NULL;
478
479 if (ubusd_acl_init_client(cl, fd))
480 goto free;
481
482 INIT_LIST_HEAD(&cl->objects);
483 cl->sock.fd = fd;
484 cl->sock.cb = cb;
485 cl->pending_msg_fd = -1;
486
487 if (!ubus_alloc_id(&clients, &cl->id, 0))
488 goto free;
489
490 if (!ubusd_send_hello(cl))
491 goto delete;
492
493 return cl;
494
495 delete:
496 ubus_free_id(&clients, &cl->id);
497 free:
498 free(cl);
499 return NULL;
500 }
501
502 void ubusd_proto_free_client(struct ubus_client *cl)
503 {
504 struct ubus_object *obj;
505
506 while (!list_empty(&cl->objects)) {
507 obj = list_first_entry(&cl->objects, struct ubus_object, list);
508 ubusd_free_object(obj);
509 }
510
511 ubusd_acl_free_client(cl);
512 ubus_free_id(&clients, &cl->id);
513 }
514
515 void ubus_notify_subscription(struct ubus_object *obj)
516 {
517 bool active = !list_empty(&obj->subscribers);
518 struct ubus_msg_buf *ub;
519
520 blob_buf_init(&b, 0);
521 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
522 blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
523
524 ub = ubus_msg_from_blob(false);
525 if (!ub)
526 return;
527
528 ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
529 ubus_msg_send(obj->client, ub, true);
530 }
531
532 void ubus_notify_unsubscribe(struct ubus_subscription *s)
533 {
534 struct ubus_msg_buf *ub;
535
536 blob_buf_init(&b, 0);
537 blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
538 blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
539
540 ub = ubus_msg_from_blob(false);
541 if (ub != NULL) {
542 ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
543 ubus_msg_send(s->subscriber->client, ub, true);
544 }
545
546 ubus_unsubscribe(s);
547 }
548
549 static void __constructor ubusd_proto_init(void)
550 {
551 ubus_init_id_tree(&clients);
552
553 blob_buf_init(&b, 0);
554 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
555
556 retmsg = ubus_msg_from_blob(false);
557 if (!retmsg)
558 exit(1);
559
560 retmsg->hdr.type = UBUS_MSG_STATUS;
561 retmsg_data = blob_data(blob_data(retmsg->data));
562 }