2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
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
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.
14 #include <arpa/inet.h>
20 static struct ubus_msg_buf *retmsg;
21 static int *retmsg_data;
22 static struct avl_tree clients;
24 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
26 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
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 },
39 struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
41 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
45 static void ubus_msg_close_fd(struct ubus_msg_buf *ub)
54 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
62 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
64 return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
67 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
69 struct ubus_msg_buf *new;
71 new = ubus_msg_from_blob(shared);
75 ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
80 ubus_proto_send_msg_from_blob(struct ubus_client *cl, struct ubus_msg_buf *ub,
83 ub = ubus_reply_from_blob(ub, true);
88 ubus_msg_send(cl, ub, true);
91 static bool ubusd_send_hello(struct ubus_client *cl)
93 struct ubus_msg_buf *ub;
96 ub = ubus_msg_from_blob(true);
100 ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
101 ubus_msg_send(cl, ub, true);
105 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
107 ub->hdr.type = UBUS_MSG_DATA;
108 ubus_msg_send(cl, ub, false);
112 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
114 struct ubus_object *obj;
116 if (!attr[UBUS_ATTR_OBJID])
117 return UBUS_STATUS_INVALID_ARGUMENT;
119 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
121 return UBUS_STATUS_NOT_FOUND;
123 if (obj->client != cl)
124 return UBUS_STATUS_PERMISSION_DENIED;
126 blob_buf_init(&b, 0);
127 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
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);
133 ubusd_free_object(obj);
134 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
139 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
141 struct ubus_object *obj;
143 obj = ubusd_create_object(cl, attr);
145 return UBUS_STATUS_INVALID_ARGUMENT;
147 blob_buf_init(&b, 0);
148 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
149 if (attr[UBUS_ATTR_SIGNATURE])
150 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
152 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
156 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
158 struct ubus_method *m;
162 blob_buf_init(&b, 0);
164 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
165 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
166 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
168 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
169 list_for_each_entry(m, &obj->type->methods, list) {
170 if (!ubusd_acl_check(cl, obj->path.key, blobmsg_name(m->data), UBUS_ACL_ACCESS)) {
171 blobmsg_add_blob(&b, m->data);
175 blob_nest_end(&b, s);
178 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
181 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
183 struct ubus_object *obj;
188 if (!attr[UBUS_ATTR_OBJPATH]) {
189 avl_for_each_element(&path, obj, path)
190 ubusd_send_obj(cl, ub, obj);
194 objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
195 len = strlen(objpath);
196 if (objpath[len - 1] != '*') {
197 obj = avl_find_element(&path, objpath, obj, path);
199 return UBUS_STATUS_NOT_FOUND;
201 ubusd_send_obj(cl, ub, obj);
207 obj = avl_find_ge_element(&path, objpath, obj, path);
209 return UBUS_STATUS_NOT_FOUND;
211 while (!strncmp(objpath, obj->path.key, len)) {
213 ubusd_send_obj(cl, ub, obj);
214 if (obj == avl_last_element(&path, obj, path))
216 obj = avl_next_element(obj, path);
220 return UBUS_STATUS_NOT_FOUND;
226 ubusd_forward_invoke(struct ubus_client *cl, struct ubus_object *obj,
227 const char *method, struct ubus_msg_buf *ub,
228 struct blob_attr *data)
230 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
231 blob_put_string(&b, UBUS_ATTR_METHOD, method);
233 blob_put_string(&b, UBUS_ATTR_USER, cl->user);
235 blob_put_string(&b, UBUS_ATTR_GROUP, cl->group);
237 blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
239 ubus_proto_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
242 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
244 struct ubus_object *obj = NULL;
248 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
249 return UBUS_STATUS_INVALID_ARGUMENT;
251 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
253 return UBUS_STATUS_NOT_FOUND;
255 obj = container_of(id, struct ubus_object, id);
257 method = blob_data(attr[UBUS_ATTR_METHOD]);
259 if (ubusd_acl_check(cl, obj->path.key, method, UBUS_ACL_ACCESS))
260 return UBUS_STATUS_NOT_FOUND;
263 return obj->recv_msg(cl, ub, method, attr[UBUS_ATTR_DATA]);
265 ub->hdr.peer = cl->id.id;
266 blob_buf_init(&b, 0);
268 ubusd_forward_invoke(cl, obj, method, ub, attr[UBUS_ATTR_DATA]);
274 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
276 struct ubus_object *obj = NULL;
277 struct ubus_subscription *s;
280 bool no_reply = false;
283 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
284 return UBUS_STATUS_INVALID_ARGUMENT;
286 if (attr[UBUS_ATTR_NO_REPLY])
287 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
289 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
291 return UBUS_STATUS_NOT_FOUND;
293 obj = container_of(id, struct ubus_object, id);
294 if (obj->client != cl)
295 return UBUS_STATUS_PERMISSION_DENIED;
298 blob_buf_init(&b, 0);
299 blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
300 c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
301 list_for_each_entry(s, &obj->subscribers, list) {
302 blob_put_int32(&b, 0, s->subscriber->id.id);
304 blob_nest_end(&b, c);
305 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
306 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
309 ub->hdr.peer = cl->id.id;
310 method = blob_data(attr[UBUS_ATTR_METHOD]);
311 list_for_each_entry(s, &obj->subscribers, list) {
312 blob_buf_init(&b, 0);
314 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
315 ubusd_forward_invoke(cl, s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
322 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
324 struct ubus_id *clid;
326 clid = ubus_find_id(&clients, id);
330 return container_of(clid, struct ubus_client, id);
333 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
335 struct ubus_object *obj;
337 if (!attr[UBUS_ATTR_OBJID] ||
338 (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
339 (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
342 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
346 if (cl != obj->client)
349 cl = ubusd_get_client_by_id(ub->hdr.peer);
353 ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
354 ubus_msg_send(cl, ub, true);
362 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
364 struct ubus_object *obj, *target;
366 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
367 return UBUS_STATUS_INVALID_ARGUMENT;
369 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
371 return UBUS_STATUS_NOT_FOUND;
373 if (cl != obj->client)
374 return UBUS_STATUS_INVALID_ARGUMENT;
376 target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
378 return UBUS_STATUS_NOT_FOUND;
380 if (cl == target->client)
381 return UBUS_STATUS_INVALID_ARGUMENT;
383 if (!target->path.key) {
384 if (strcmp(target->client->user, cl->user) && strcmp(target->client->group, cl->group))
385 return UBUS_STATUS_NOT_FOUND;
386 } else if (ubusd_acl_check(cl, target->path.key, NULL, UBUS_ACL_SUBSCRIBE)) {
387 return UBUS_STATUS_NOT_FOUND;
390 ubus_subscribe(obj, target);
394 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
396 struct ubus_object *obj;
397 struct ubus_subscription *s;
400 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
401 return UBUS_STATUS_INVALID_ARGUMENT;
403 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
405 return UBUS_STATUS_NOT_FOUND;
407 if (cl != obj->client)
408 return UBUS_STATUS_INVALID_ARGUMENT;
410 id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
411 list_for_each_entry(s, &obj->target_list, target_list) {
412 if (s->target->id.id != id)
419 return UBUS_STATUS_NOT_FOUND;
422 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
423 [UBUS_MSG_PING] = ubusd_send_pong,
424 [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
425 [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
426 [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
427 [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
428 [UBUS_MSG_STATUS] = ubusd_handle_response,
429 [UBUS_MSG_DATA] = ubusd_handle_response,
430 [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
431 [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
432 [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
435 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
437 ubus_cmd_cb cb = NULL;
440 retmsg->hdr.seq = ub->hdr.seq;
441 retmsg->hdr.peer = ub->hdr.peer;
443 if (ub->hdr.type < __UBUS_MSG_LAST)
444 cb = handlers[ub->hdr.type];
446 if (ub->hdr.type != UBUS_MSG_STATUS)
447 ubus_msg_close_fd(ub);
450 ret = cb(cl, ub, ubus_parse_msg(ub->data));
452 ret = UBUS_STATUS_INVALID_COMMAND;
459 *retmsg_data = htonl(ret);
460 ubus_msg_send(cl, retmsg, false);
463 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
465 struct ubus_client *cl;
467 cl = calloc(1, sizeof(*cl));
471 if (ubusd_acl_init_client(cl, fd))
474 INIT_LIST_HEAD(&cl->objects);
477 cl->pending_msg_fd = -1;
479 if (!ubus_alloc_id(&clients, &cl->id, 0))
482 if (!ubusd_send_hello(cl))
488 ubus_free_id(&clients, &cl->id);
494 void ubusd_proto_free_client(struct ubus_client *cl)
496 struct ubus_object *obj;
498 while (!list_empty(&cl->objects)) {
499 obj = list_first_entry(&cl->objects, struct ubus_object, list);
500 ubusd_free_object(obj);
503 ubus_free_id(&clients, &cl->id);
506 void ubus_notify_subscription(struct ubus_object *obj)
508 bool active = !list_empty(&obj->subscribers);
509 struct ubus_msg_buf *ub;
511 blob_buf_init(&b, 0);
512 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
513 blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
515 ub = ubus_msg_from_blob(false);
519 ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
520 ubus_msg_send(obj->client, ub, true);
523 void ubus_notify_unsubscribe(struct ubus_subscription *s)
525 struct ubus_msg_buf *ub;
527 blob_buf_init(&b, 0);
528 blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
529 blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
531 ub = ubus_msg_from_blob(false);
533 ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
534 ubus_msg_send(s->subscriber->client, ub, true);
540 static void __constructor ubusd_proto_init(void)
542 ubus_init_id_tree(&clients);
544 blob_buf_init(&b, 0);
545 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
547 retmsg = ubus_msg_from_blob(false);
551 retmsg->hdr.type = UBUS_MSG_STATUS;
552 retmsg_data = blob_data(blob_data(retmsg->data));