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 avl_tree clients
;
22 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
24 typedef int (*ubus_cmd_cb
)(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
);
26 static const struct blob_attr_info ubus_policy
[UBUS_ATTR_MAX
] = {
27 [UBUS_ATTR_SIGNATURE
] = { .type
= BLOB_ATTR_NESTED
},
28 [UBUS_ATTR_OBJTYPE
] = { .type
= BLOB_ATTR_INT32
},
29 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
30 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
31 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
32 [UBUS_ATTR_METHOD
] = { .type
= BLOB_ATTR_STRING
},
33 [UBUS_ATTR_USER
] = { .type
= BLOB_ATTR_STRING
},
34 [UBUS_ATTR_GROUP
] = { .type
= BLOB_ATTR_STRING
},
37 struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
39 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
43 static void ubus_msg_close_fd(struct ubus_msg_buf
*ub
)
52 static void ubus_msg_init(struct ubus_msg_buf
*ub
, uint8_t type
, uint16_t seq
, uint32_t peer
)
60 static struct ubus_msg_buf
*ubus_msg_from_blob(bool shared
)
62 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), shared
);
65 static struct ubus_msg_buf
*ubus_reply_from_blob(struct ubus_msg_buf
*ub
, bool shared
)
67 struct ubus_msg_buf
*new;
69 new = ubus_msg_from_blob(shared
);
73 ubus_msg_init(new, UBUS_MSG_DATA
, ub
->hdr
.seq
, ub
->hdr
.peer
);
78 ubus_proto_send_msg_from_blob(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
,
81 /* keep the fd to be passed if it is UBUS_MSG_INVOKE */
83 ub
= ubus_reply_from_blob(ub
, true);
90 ubus_msg_send(cl
, ub
);
94 static bool ubusd_send_hello(struct ubus_client
*cl
)
96 struct ubus_msg_buf
*ub
;
99 ub
= ubus_msg_from_blob(true);
103 ubus_msg_init(ub
, UBUS_MSG_HELLO
, 0, cl
->id
.id
);
104 ubus_msg_send(cl
, ub
);
109 static int ubusd_send_pong(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
111 ub
->hdr
.type
= UBUS_MSG_DATA
;
112 ubus_msg_send(cl
, ub
);
116 static int ubusd_handle_remove_object(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
118 struct ubus_object
*obj
;
120 if (!attr
[UBUS_ATTR_OBJID
])
121 return UBUS_STATUS_INVALID_ARGUMENT
;
123 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
125 return UBUS_STATUS_NOT_FOUND
;
127 if (obj
->client
!= cl
)
128 return UBUS_STATUS_PERMISSION_DENIED
;
130 blob_buf_init(&b
, 0);
131 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
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
);
137 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
138 ubusd_free_object(obj
);
143 static int ubusd_handle_add_object(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
145 struct ubus_object
*obj
;
147 obj
= ubusd_create_object(cl
, attr
);
149 return UBUS_STATUS_INVALID_ARGUMENT
;
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
);
156 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
160 static void ubusd_send_obj(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct ubus_object
*obj
)
162 struct ubus_method
*m
;
163 int all_cnt
= 0, cnt
= 0;
169 blob_buf_init(&b
, 0);
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
);
175 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
176 list_for_each_entry(m
, &obj
->type
->methods
, list
) {
178 if (!ubusd_acl_check(cl
, obj
->path
.key
, blobmsg_name(m
->data
), UBUS_ACL_ACCESS
)) {
179 blobmsg_add_blob(&b
, m
->data
);
183 blob_nest_end(&b
, s
);
186 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
189 static int ubusd_handle_lookup(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
191 struct ubus_object
*obj
;
196 if (!attr
[UBUS_ATTR_OBJPATH
]) {
197 avl_for_each_element(&path
, obj
, path
)
198 ubusd_send_obj(cl
, ub
, obj
);
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
);
207 return UBUS_STATUS_NOT_FOUND
;
209 ubusd_send_obj(cl
, ub
, obj
);
215 obj
= avl_find_ge_element(&path
, objpath
, obj
, path
);
217 return UBUS_STATUS_NOT_FOUND
;
219 while (!strncmp(objpath
, obj
->path
.key
, len
)) {
221 ubusd_send_obj(cl
, ub
, obj
);
222 if (obj
== avl_last_element(&path
, obj
, path
))
224 obj
= avl_next_element(obj
, path
);
228 return UBUS_STATUS_NOT_FOUND
;
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
)
238 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
239 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
241 blob_put_string(&b
, UBUS_ATTR_USER
, cl
->user
);
243 blob_put_string(&b
, UBUS_ATTR_GROUP
, cl
->group
);
245 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(data
), blob_len(data
));
247 ubus_proto_send_msg_from_blob(obj
->client
, ub
, UBUS_MSG_INVOKE
);
250 static int ubusd_handle_invoke(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
252 struct ubus_object
*obj
= NULL
;
256 if (!attr
[UBUS_ATTR_METHOD
] || !attr
[UBUS_ATTR_OBJID
])
257 return UBUS_STATUS_INVALID_ARGUMENT
;
259 id
= ubus_find_id(&objects
, blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
261 return UBUS_STATUS_NOT_FOUND
;
263 obj
= container_of(id
, struct ubus_object
, id
);
265 method
= blob_data(attr
[UBUS_ATTR_METHOD
]);
267 if (ubusd_acl_check(cl
, obj
->path
.key
, method
, UBUS_ACL_ACCESS
))
268 return UBUS_STATUS_PERMISSION_DENIED
;
271 return obj
->recv_msg(cl
, ub
, method
, attr
[UBUS_ATTR_DATA
]);
273 ub
->hdr
.peer
= cl
->id
.id
;
274 blob_buf_init(&b
, 0);
276 ubusd_forward_invoke(cl
, obj
, method
, ub
, attr
[UBUS_ATTR_DATA
]);
281 static int ubusd_handle_notify(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
283 struct ubus_object
*obj
= NULL
;
284 struct ubus_subscription
*s
;
287 bool no_reply
= false;
290 if (!attr
[UBUS_ATTR_METHOD
] || !attr
[UBUS_ATTR_OBJID
])
291 return UBUS_STATUS_INVALID_ARGUMENT
;
293 if (attr
[UBUS_ATTR_NO_REPLY
])
294 no_reply
= blob_get_int8(attr
[UBUS_ATTR_NO_REPLY
]);
296 id
= ubus_find_id(&objects
, blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
298 return UBUS_STATUS_NOT_FOUND
;
300 obj
= container_of(id
, struct ubus_object
, id
);
301 if (obj
->client
!= cl
)
302 return UBUS_STATUS_PERMISSION_DENIED
;
305 blob_buf_init(&b
, 0);
306 blob_put_int32(&b
, UBUS_ATTR_OBJID
, id
->id
);
307 c
= blob_nest_start(&b
, UBUS_ATTR_SUBSCRIBERS
);
308 list_for_each_entry(s
, &obj
->subscribers
, list
) {
309 blob_put_int32(&b
, 0, s
->subscriber
->id
.id
);
311 blob_nest_end(&b
, c
);
312 blob_put_int32(&b
, UBUS_ATTR_STATUS
, 0);
313 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_STATUS
);
316 ub
->hdr
.peer
= cl
->id
.id
;
317 method
= blob_data(attr
[UBUS_ATTR_METHOD
]);
318 list_for_each_entry(s
, &obj
->subscribers
, list
) {
319 blob_buf_init(&b
, 0);
321 blob_put_int8(&b
, UBUS_ATTR_NO_REPLY
, 1);
322 ubusd_forward_invoke(cl
, s
->subscriber
, method
, ub
, attr
[UBUS_ATTR_DATA
]);
328 static struct ubus_client
*ubusd_get_client_by_id(uint32_t id
)
330 struct ubus_id
*clid
;
332 clid
= ubus_find_id(&clients
, id
);
336 return container_of(clid
, struct ubus_client
, id
);
339 static int ubusd_handle_response(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
341 struct ubus_object
*obj
;
343 if (!attr
[UBUS_ATTR_OBJID
] ||
344 (ub
->hdr
.type
== UBUS_MSG_STATUS
&& !attr
[UBUS_ATTR_STATUS
]) ||
345 (ub
->hdr
.type
== UBUS_MSG_DATA
&& !attr
[UBUS_ATTR_DATA
]))
348 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
352 if (cl
!= obj
->client
)
355 cl
= ubusd_get_client_by_id(ub
->hdr
.peer
);
359 ub
->hdr
.peer
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
360 ubus_msg_send(cl
, ub
);
365 static int ubusd_handle_add_watch(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
367 struct ubus_object
*obj
, *target
;
369 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_TARGET
])
370 return UBUS_STATUS_INVALID_ARGUMENT
;
372 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
374 return UBUS_STATUS_NOT_FOUND
;
376 if (cl
!= obj
->client
)
377 return UBUS_STATUS_INVALID_ARGUMENT
;
379 target
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_TARGET
]));
380 if (!target
|| !target
->client
)
381 return UBUS_STATUS_NOT_FOUND
;
383 if (cl
== target
->client
)
384 return UBUS_STATUS_INVALID_ARGUMENT
;
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
;
393 ubus_subscribe(obj
, target
);
397 static int ubusd_handle_remove_watch(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
399 struct ubus_object
*obj
;
400 struct ubus_subscription
*s
;
403 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_TARGET
])
404 return UBUS_STATUS_INVALID_ARGUMENT
;
406 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
408 return UBUS_STATUS_NOT_FOUND
;
410 if (cl
!= obj
->client
)
411 return UBUS_STATUS_INVALID_ARGUMENT
;
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
)
422 return UBUS_STATUS_NOT_FOUND
;
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
,
438 void ubusd_proto_receive_message(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
440 ubus_cmd_cb cb
= NULL
;
442 struct ubus_msg_buf
*retmsg
= cl
->retmsg
;
443 int *retmsg_data
= blob_data(blob_data(retmsg
->data
));
445 retmsg
->hdr
.seq
= ub
->hdr
.seq
;
446 retmsg
->hdr
.peer
= ub
->hdr
.peer
;
448 if (ub
->hdr
.type
< __UBUS_MSG_LAST
)
449 cb
= handlers
[ub
->hdr
.type
];
451 if (ub
->hdr
.type
!= UBUS_MSG_STATUS
&& ub
->hdr
.type
!= UBUS_MSG_INVOKE
)
452 ubus_msg_close_fd(ub
);
454 /* Note: no callback should free the `ub` buffer
455 that's always done right after the callback finishes */
457 ret
= cb(cl
, ub
, ubus_parse_msg(ub
->data
));
459 ret
= UBUS_STATUS_INVALID_COMMAND
;
466 *retmsg_data
= htonl(ret
);
467 ubus_msg_send(cl
, retmsg
);
470 static int ubusd_proto_init_retmsg(struct ubus_client
*cl
)
472 struct blob_buf
*b
= &cl
->b
;
474 blob_buf_init(&cl
->b
, 0);
475 blob_put_int32(&cl
->b
, UBUS_ATTR_STATUS
, 0);
477 /* we make the 'retmsg' buffer shared with the blob_buf b, to reduce mem duplication */
478 cl
->retmsg
= ubus_msg_new(b
->head
, blob_raw_len(b
->head
), true);
482 cl
->retmsg
->hdr
.type
= UBUS_MSG_STATUS
;
486 struct ubus_client
*ubusd_proto_new_client(int fd
, uloop_fd_handler cb
)
488 struct ubus_client
*cl
;
490 cl
= calloc(1, sizeof(*cl
));
494 if (ubusd_acl_init_client(cl
, fd
))
497 INIT_LIST_HEAD(&cl
->objects
);
500 cl
->pending_msg_fd
= -1;
502 if (!ubus_alloc_id(&clients
, &cl
->id
, 0))
505 if (ubusd_proto_init_retmsg(cl
))
508 if (!ubusd_send_hello(cl
))
514 ubus_free_id(&clients
, &cl
->id
);
520 void ubusd_proto_free_client(struct ubus_client
*cl
)
522 struct ubus_object
*obj
;
524 while (!list_empty(&cl
->objects
)) {
525 obj
= list_first_entry(&cl
->objects
, struct ubus_object
, list
);
526 ubusd_free_object(obj
);
528 ubus_msg_free(cl
->retmsg
);
529 blob_buf_free(&cl
->b
);
531 ubusd_acl_free_client(cl
);
532 ubus_free_id(&clients
, &cl
->id
);
535 void ubus_notify_subscription(struct ubus_object
*obj
)
537 bool active
= !list_empty(&obj
->subscribers
);
538 struct ubus_msg_buf
*ub
;
540 blob_buf_init(&b
, 0);
541 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
542 blob_put_int8(&b
, UBUS_ATTR_ACTIVE
, active
);
544 ub
= ubus_msg_from_blob(false);
548 ubus_msg_init(ub
, UBUS_MSG_NOTIFY
, ++obj
->invoke_seq
, 0);
549 ubus_msg_send(obj
->client
, ub
);
553 void ubus_notify_unsubscribe(struct ubus_subscription
*s
)
555 struct ubus_msg_buf
*ub
;
557 blob_buf_init(&b
, 0);
558 blob_put_int32(&b
, UBUS_ATTR_OBJID
, s
->subscriber
->id
.id
);
559 blob_put_int32(&b
, UBUS_ATTR_TARGET
, s
->target
->id
.id
);
561 ub
= ubus_msg_from_blob(false);
563 ubus_msg_init(ub
, UBUS_MSG_UNSUBSCRIBE
, ++s
->subscriber
->invoke_seq
, 0);
564 ubus_msg_send(s
->subscriber
->client
, ub
);
571 static void __constructor
ubusd_proto_init(void)
573 ubus_init_id_tree(&clients
);