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 /* keep the fd to be passed if it is UBUS_MSG_INVOKE */
85 ub
= ubus_reply_from_blob(ub
, true);
92 ubus_msg_send(cl
, ub
, true);
95 static bool ubusd_send_hello(struct ubus_client
*cl
)
97 struct ubus_msg_buf
*ub
;
100 ub
= ubus_msg_from_blob(true);
104 ubus_msg_init(ub
, UBUS_MSG_HELLO
, 0, cl
->id
.id
);
105 ubus_msg_send(cl
, ub
, true);
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
, false);
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
]);
282 static int ubusd_handle_notify(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
284 struct ubus_object
*obj
= NULL
;
285 struct ubus_subscription
*s
;
288 bool no_reply
= false;
291 if (!attr
[UBUS_ATTR_METHOD
] || !attr
[UBUS_ATTR_OBJID
])
292 return UBUS_STATUS_INVALID_ARGUMENT
;
294 if (attr
[UBUS_ATTR_NO_REPLY
])
295 no_reply
= blob_get_int8(attr
[UBUS_ATTR_NO_REPLY
]);
297 id
= ubus_find_id(&objects
, blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
299 return UBUS_STATUS_NOT_FOUND
;
301 obj
= container_of(id
, struct ubus_object
, id
);
302 if (obj
->client
!= cl
)
303 return UBUS_STATUS_PERMISSION_DENIED
;
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
);
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
);
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);
322 blob_put_int8(&b
, UBUS_ATTR_NO_REPLY
, 1);
323 ubusd_forward_invoke(cl
, s
->subscriber
, method
, ub
, attr
[UBUS_ATTR_DATA
]);
330 static struct ubus_client
*ubusd_get_client_by_id(uint32_t id
)
332 struct ubus_id
*clid
;
334 clid
= ubus_find_id(&clients
, id
);
338 return container_of(clid
, struct ubus_client
, id
);
341 static int ubusd_handle_response(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
343 struct ubus_object
*obj
;
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
]))
350 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
354 if (cl
!= obj
->client
)
357 cl
= ubusd_get_client_by_id(ub
->hdr
.peer
);
361 ub
->hdr
.peer
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
362 ubus_msg_send(cl
, ub
, true);
370 static int ubusd_handle_add_watch(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
372 struct ubus_object
*obj
, *target
;
374 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_TARGET
])
375 return UBUS_STATUS_INVALID_ARGUMENT
;
377 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
379 return UBUS_STATUS_NOT_FOUND
;
381 if (cl
!= obj
->client
)
382 return UBUS_STATUS_INVALID_ARGUMENT
;
384 target
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_TARGET
]));
385 if (!target
|| !target
->client
)
386 return UBUS_STATUS_NOT_FOUND
;
388 if (cl
== target
->client
)
389 return UBUS_STATUS_INVALID_ARGUMENT
;
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
;
398 ubus_subscribe(obj
, target
);
402 static int ubusd_handle_remove_watch(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
404 struct ubus_object
*obj
;
405 struct ubus_subscription
*s
;
408 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_TARGET
])
409 return UBUS_STATUS_INVALID_ARGUMENT
;
411 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
413 return UBUS_STATUS_NOT_FOUND
;
415 if (cl
!= obj
->client
)
416 return UBUS_STATUS_INVALID_ARGUMENT
;
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
)
427 return UBUS_STATUS_NOT_FOUND
;
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
,
443 void ubusd_proto_receive_message(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
445 ubus_cmd_cb cb
= NULL
;
448 retmsg
->hdr
.seq
= ub
->hdr
.seq
;
449 retmsg
->hdr
.peer
= ub
->hdr
.peer
;
451 if (ub
->hdr
.type
< __UBUS_MSG_LAST
)
452 cb
= handlers
[ub
->hdr
.type
];
454 if (ub
->hdr
.type
!= UBUS_MSG_STATUS
&& ub
->hdr
.type
!= UBUS_MSG_INVOKE
)
455 ubus_msg_close_fd(ub
);
458 ret
= cb(cl
, ub
, ubus_parse_msg(ub
->data
));
460 ret
= UBUS_STATUS_INVALID_COMMAND
;
467 *retmsg_data
= htonl(ret
);
468 ubus_msg_send(cl
, retmsg
, false);
471 struct ubus_client
*ubusd_proto_new_client(int fd
, uloop_fd_handler cb
)
473 struct ubus_client
*cl
;
475 cl
= calloc(1, sizeof(*cl
));
479 if (ubusd_acl_init_client(cl
, fd
))
482 INIT_LIST_HEAD(&cl
->objects
);
485 cl
->pending_msg_fd
= -1;
487 if (!ubus_alloc_id(&clients
, &cl
->id
, 0))
490 if (!ubusd_send_hello(cl
))
496 ubus_free_id(&clients
, &cl
->id
);
502 void ubusd_proto_free_client(struct ubus_client
*cl
)
504 struct ubus_object
*obj
;
506 while (!list_empty(&cl
->objects
)) {
507 obj
= list_first_entry(&cl
->objects
, struct ubus_object
, list
);
508 ubusd_free_object(obj
);
511 ubusd_acl_free_client(cl
);
512 ubus_free_id(&clients
, &cl
->id
);
515 void ubus_notify_subscription(struct ubus_object
*obj
)
517 bool active
= !list_empty(&obj
->subscribers
);
518 struct ubus_msg_buf
*ub
;
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
);
524 ub
= ubus_msg_from_blob(false);
528 ubus_msg_init(ub
, UBUS_MSG_NOTIFY
, ++obj
->invoke_seq
, 0);
529 ubus_msg_send(obj
->client
, ub
, true);
532 void ubus_notify_unsubscribe(struct ubus_subscription
*s
)
534 struct ubus_msg_buf
*ub
;
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
);
540 ub
= ubus_msg_from_blob(false);
542 ubus_msg_init(ub
, UBUS_MSG_UNSUBSCRIBE
, ++s
->subscriber
->invoke_seq
, 0);
543 ubus_msg_send(s
->subscriber
->client
, ub
, true);
549 static void __constructor
ubusd_proto_init(void)
551 ubus_init_id_tree(&clients
);
553 blob_buf_init(&b
, 0);
554 blob_put_int32(&b
, UBUS_ATTR_STATUS
, 0);
556 retmsg
= ubus_msg_from_blob(false);
560 retmsg
->hdr
.type
= UBUS_MSG_STATUS
;
561 retmsg_data
= blob_data(blob_data(retmsg
->data
));