ubusd: rename goto label from `error` to `out`
[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);
93 ubus_msg_free(ub);
94 }
95
96 static bool ubusd_send_hello(struct ubus_client *cl)
97 {
98 struct ubus_msg_buf *ub;
99
100 blob_buf_init(&b, 0);
101 ub = ubus_msg_from_blob(true);
102 if (!ub)
103 return false;
104
105 ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
106 ubus_msg_send(cl, ub);
107 ubus_msg_free(ub);
108 return true;
109 }
110
111 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
112 {
113 ub->hdr.type = UBUS_MSG_DATA;
114 ubus_msg_send(cl, ub);
115 return 0;
116 }
117
118 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
119 {
120 struct ubus_object *obj;
121
122 if (!attr[UBUS_ATTR_OBJID])
123 return UBUS_STATUS_INVALID_ARGUMENT;
124
125 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
126 if (!obj)
127 return UBUS_STATUS_NOT_FOUND;
128
129 if (obj->client != cl)
130 return UBUS_STATUS_PERMISSION_DENIED;
131
132 blob_buf_init(&b, 0);
133 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
134
135 /* check if we're removing the object type as well */
136 if (obj->type && obj->type->refcount == 1)
137 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
138
139 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
140 ubusd_free_object(obj);
141
142 return 0;
143 }
144
145 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
146 {
147 struct ubus_object *obj;
148
149 obj = ubusd_create_object(cl, attr);
150 if (!obj)
151 return UBUS_STATUS_INVALID_ARGUMENT;
152
153 blob_buf_init(&b, 0);
154 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
155 if (attr[UBUS_ATTR_SIGNATURE] && obj->type)
156 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
157
158 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
159 return 0;
160 }
161
162 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
163 {
164 struct ubus_method *m;
165 int all_cnt = 0, cnt = 0;
166 void *s;
167
168 if (!obj->type)
169 return;
170
171 blob_buf_init(&b, 0);
172
173 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
174 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
175 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
176
177 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
178 list_for_each_entry(m, &obj->type->methods, list) {
179 all_cnt++;
180 if (!ubusd_acl_check(cl, obj->path.key, blobmsg_name(m->data), UBUS_ACL_ACCESS)) {
181 blobmsg_add_blob(&b, m->data);
182 cnt++;
183 }
184 }
185 blob_nest_end(&b, s);
186
187 if (cnt || !all_cnt)
188 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
189 }
190
191 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
192 {
193 struct ubus_object *obj;
194 char *objpath;
195 bool found = false;
196 int len;
197
198 if (!attr[UBUS_ATTR_OBJPATH]) {
199 avl_for_each_element(&path, obj, path)
200 ubusd_send_obj(cl, ub, obj);
201 return 0;
202 }
203
204 objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
205 len = strlen(objpath);
206 if (objpath[len - 1] != '*') {
207 obj = avl_find_element(&path, objpath, obj, path);
208 if (!obj)
209 return UBUS_STATUS_NOT_FOUND;
210
211 ubusd_send_obj(cl, ub, obj);
212 return 0;
213 }
214
215 objpath[--len] = 0;
216
217 obj = avl_find_ge_element(&path, objpath, obj, path);
218 if (!obj)
219 return UBUS_STATUS_NOT_FOUND;
220
221 while (!strncmp(objpath, obj->path.key, len)) {
222 found = true;
223 ubusd_send_obj(cl, ub, obj);
224 if (obj == avl_last_element(&path, obj, path))
225 break;
226 obj = avl_next_element(obj, path);
227 }
228
229 if (!found)
230 return UBUS_STATUS_NOT_FOUND;
231
232 return 0;
233 }
234
235 static void
236 ubusd_forward_invoke(struct ubus_client *cl, struct ubus_object *obj,
237 const char *method, struct ubus_msg_buf *ub,
238 struct blob_attr *data)
239 {
240 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
241 blob_put_string(&b, UBUS_ATTR_METHOD, method);
242 if (cl->user)
243 blob_put_string(&b, UBUS_ATTR_USER, cl->user);
244 if (cl->group)
245 blob_put_string(&b, UBUS_ATTR_GROUP, cl->group);
246 if (data)
247 blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
248
249 ubus_proto_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
250 }
251
252 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
253 {
254 struct ubus_object *obj = NULL;
255 struct ubus_id *id;
256 const char *method;
257
258 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
259 return UBUS_STATUS_INVALID_ARGUMENT;
260
261 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
262 if (!id)
263 return UBUS_STATUS_NOT_FOUND;
264
265 obj = container_of(id, struct ubus_object, id);
266
267 method = blob_data(attr[UBUS_ATTR_METHOD]);
268
269 if (ubusd_acl_check(cl, obj->path.key, method, UBUS_ACL_ACCESS))
270 return UBUS_STATUS_PERMISSION_DENIED;
271
272 if (!obj->client)
273 return obj->recv_msg(cl, ub, method, attr[UBUS_ATTR_DATA]);
274
275 ub->hdr.peer = cl->id.id;
276 blob_buf_init(&b, 0);
277
278 ubusd_forward_invoke(cl, obj, method, ub, attr[UBUS_ATTR_DATA]);
279
280 return -1;
281 }
282
283 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
284 {
285 struct ubus_object *obj = NULL;
286 struct ubus_subscription *s;
287 struct ubus_id *id;
288 const char *method;
289 bool no_reply = false;
290 void *c;
291
292 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
293 return UBUS_STATUS_INVALID_ARGUMENT;
294
295 if (attr[UBUS_ATTR_NO_REPLY])
296 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
297
298 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
299 if (!id)
300 return UBUS_STATUS_NOT_FOUND;
301
302 obj = container_of(id, struct ubus_object, id);
303 if (obj->client != cl)
304 return UBUS_STATUS_PERMISSION_DENIED;
305
306 if (!no_reply) {
307 blob_buf_init(&b, 0);
308 blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
309 c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
310 list_for_each_entry(s, &obj->subscribers, list) {
311 blob_put_int32(&b, 0, s->subscriber->id.id);
312 }
313 blob_nest_end(&b, c);
314 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
315 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
316 }
317
318 ub->hdr.peer = cl->id.id;
319 method = blob_data(attr[UBUS_ATTR_METHOD]);
320 list_for_each_entry(s, &obj->subscribers, list) {
321 blob_buf_init(&b, 0);
322 if (no_reply)
323 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
324 ubusd_forward_invoke(cl, s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
325 }
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 out;
349
350 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
351 if (!obj)
352 goto out;
353
354 if (cl != obj->client)
355 goto out;
356
357 cl = ubusd_get_client_by_id(ub->hdr.peer);
358 if (!cl)
359 goto out;
360
361 ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
362 ubus_msg_send(cl, ub);
363 out:
364 return -1;
365 }
366
367 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
368 {
369 struct ubus_object *obj, *target;
370
371 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
372 return UBUS_STATUS_INVALID_ARGUMENT;
373
374 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
375 if (!obj)
376 return UBUS_STATUS_NOT_FOUND;
377
378 if (cl != obj->client)
379 return UBUS_STATUS_INVALID_ARGUMENT;
380
381 target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
382 if (!target || !target->client)
383 return UBUS_STATUS_NOT_FOUND;
384
385 if (cl == target->client)
386 return UBUS_STATUS_INVALID_ARGUMENT;
387
388 if (!target->path.key) {
389 if (strcmp(target->client->user, cl->user) && strcmp(target->client->group, cl->group))
390 return UBUS_STATUS_NOT_FOUND;
391 } else if (ubusd_acl_check(cl, target->path.key, NULL, UBUS_ACL_SUBSCRIBE)) {
392 return UBUS_STATUS_NOT_FOUND;
393 }
394
395 ubus_subscribe(obj, target);
396 return 0;
397 }
398
399 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
400 {
401 struct ubus_object *obj;
402 struct ubus_subscription *s;
403 uint32_t id;
404
405 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
406 return UBUS_STATUS_INVALID_ARGUMENT;
407
408 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
409 if (!obj)
410 return UBUS_STATUS_NOT_FOUND;
411
412 if (cl != obj->client)
413 return UBUS_STATUS_INVALID_ARGUMENT;
414
415 id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
416 list_for_each_entry(s, &obj->target_list, target_list) {
417 if (s->target->id.id != id)
418 continue;
419
420 ubus_unsubscribe(s);
421 return 0;
422 }
423
424 return UBUS_STATUS_NOT_FOUND;
425 }
426
427 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
428 [UBUS_MSG_PING] = ubusd_send_pong,
429 [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
430 [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
431 [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
432 [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
433 [UBUS_MSG_STATUS] = ubusd_handle_response,
434 [UBUS_MSG_DATA] = ubusd_handle_response,
435 [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
436 [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
437 [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
438 };
439
440 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
441 {
442 ubus_cmd_cb cb = NULL;
443 int ret;
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 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
471 {
472 struct ubus_client *cl;
473
474 cl = calloc(1, sizeof(*cl));
475 if (!cl)
476 return NULL;
477
478 if (ubusd_acl_init_client(cl, fd))
479 goto free;
480
481 INIT_LIST_HEAD(&cl->objects);
482 cl->sock.fd = fd;
483 cl->sock.cb = cb;
484 cl->pending_msg_fd = -1;
485
486 if (!ubus_alloc_id(&clients, &cl->id, 0))
487 goto free;
488
489 if (!ubusd_send_hello(cl))
490 goto delete;
491
492 return cl;
493
494 delete:
495 ubus_free_id(&clients, &cl->id);
496 free:
497 free(cl);
498 return NULL;
499 }
500
501 void ubusd_proto_free_client(struct ubus_client *cl)
502 {
503 struct ubus_object *obj;
504
505 while (!list_empty(&cl->objects)) {
506 obj = list_first_entry(&cl->objects, struct ubus_object, list);
507 ubusd_free_object(obj);
508 }
509
510 ubusd_acl_free_client(cl);
511 ubus_free_id(&clients, &cl->id);
512 }
513
514 void ubus_notify_subscription(struct ubus_object *obj)
515 {
516 bool active = !list_empty(&obj->subscribers);
517 struct ubus_msg_buf *ub;
518
519 blob_buf_init(&b, 0);
520 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
521 blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
522
523 ub = ubus_msg_from_blob(false);
524 if (!ub)
525 return;
526
527 ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
528 ubus_msg_send(obj->client, ub);
529 ubus_msg_free(ub);
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);
544 ubus_msg_free(ub);
545 }
546
547 ubus_unsubscribe(s);
548 }
549
550 static void __constructor ubusd_proto_init(void)
551 {
552 ubus_init_id_tree(&clients);
553
554 blob_buf_init(&b, 0);
555 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
556
557 retmsg = ubus_msg_from_blob(false);
558 if (!retmsg)
559 exit(1);
560
561 retmsg->hdr.type = UBUS_MSG_STATUS;
562 retmsg_data = blob_data(blob_data(retmsg->data));
563 }