remove obseleted validation of key
[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 };
36
37 static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
38 {
39 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
40 return attrbuf;
41 }
42
43 static void ubus_msg_close_fd(struct ubus_msg_buf *ub)
44 {
45 if (ub->fd < 0)
46 return;
47
48 close(ub->fd);
49 ub->fd = -1;
50 }
51
52 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
53 {
54 ub->hdr.version = 0;
55 ub->hdr.type = type;
56 ub->hdr.seq = seq;
57 ub->hdr.peer = peer;
58 }
59
60 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
61 {
62 return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
63 }
64
65 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
66 {
67 struct ubus_msg_buf *new;
68
69 new = ubus_msg_from_blob(shared);
70 if (!new)
71 return NULL;
72
73 ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
74 return new;
75 }
76
77 static void
78 ubus_send_msg_from_blob(struct ubus_client *cl, struct ubus_msg_buf *ub,
79 uint8_t type)
80 {
81 ub = ubus_reply_from_blob(ub, true);
82 if (!ub)
83 return;
84
85 ub->hdr.type = type;
86 ubus_msg_send(cl, ub, true);
87 }
88
89 static bool ubusd_send_hello(struct ubus_client *cl)
90 {
91 struct ubus_msg_buf *ub;
92
93 blob_buf_init(&b, 0);
94 ub = ubus_msg_from_blob(true);
95 if (!ub)
96 return false;
97
98 ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
99 ubus_msg_send(cl, ub, true);
100 return true;
101 }
102
103 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
104 {
105 ub->hdr.type = UBUS_MSG_DATA;
106 ubus_msg_send(cl, ub, false);
107 return 0;
108 }
109
110 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
111 {
112 struct ubus_object *obj;
113
114 if (!attr[UBUS_ATTR_OBJID])
115 return UBUS_STATUS_INVALID_ARGUMENT;
116
117 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
118 if (!obj)
119 return UBUS_STATUS_NOT_FOUND;
120
121 if (obj->client != cl)
122 return UBUS_STATUS_PERMISSION_DENIED;
123
124 blob_buf_init(&b, 0);
125 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
126
127 /* check if we're removing the object type as well */
128 if (obj->type && obj->type->refcount == 1)
129 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
130
131 ubusd_free_object(obj);
132 ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
133
134 return 0;
135 }
136
137 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
138 {
139 struct ubus_object *obj;
140
141 obj = ubusd_create_object(cl, attr);
142 if (!obj)
143 return UBUS_STATUS_INVALID_ARGUMENT;
144
145 blob_buf_init(&b, 0);
146 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
147 if (attr[UBUS_ATTR_SIGNATURE])
148 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
149
150 ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
151 return 0;
152 }
153
154 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
155 {
156 struct ubus_method *m;
157 void *s;
158
159 blob_buf_init(&b, 0);
160
161 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
162 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
163 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
164
165 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
166 list_for_each_entry(m, &obj->type->methods, list)
167 blobmsg_add_blob(&b, m->data);
168 blob_nest_end(&b, s);
169
170 ubus_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
171 }
172
173 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
174 {
175 struct ubus_object *obj;
176 char *objpath;
177 bool found = false;
178 int len;
179
180 if (!attr[UBUS_ATTR_OBJPATH]) {
181 avl_for_each_element(&path, obj, path)
182 ubusd_send_obj(cl, ub, obj);
183 return 0;
184 }
185
186 objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
187 len = strlen(objpath);
188 if (objpath[len - 1] != '*') {
189 obj = avl_find_element(&path, objpath, obj, path);
190 if (!obj)
191 return UBUS_STATUS_NOT_FOUND;
192
193 ubusd_send_obj(cl, ub, obj);
194 return 0;
195 }
196
197 objpath[--len] = 0;
198
199 obj = avl_find_ge_element(&path, objpath, obj, path);
200 if (!obj)
201 return UBUS_STATUS_NOT_FOUND;
202
203 while (!strncmp(objpath, obj->path.key, len)) {
204 found = true;
205 ubusd_send_obj(cl, ub, obj);
206 if (obj == avl_last_element(&path, obj, path))
207 break;
208 obj = avl_next_element(obj, path);
209 }
210
211 if (!found)
212 return UBUS_STATUS_NOT_FOUND;
213
214 return 0;
215 }
216
217 static void
218 ubusd_forward_invoke(struct ubus_object *obj, const char *method,
219 struct ubus_msg_buf *ub, struct blob_attr *data)
220 {
221 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
222 blob_put_string(&b, UBUS_ATTR_METHOD, method);
223 if (data)
224 blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
225
226 ubus_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
227 }
228
229 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
230 {
231 struct ubus_object *obj = NULL;
232 struct ubus_id *id;
233 const char *method;
234
235 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
236 return UBUS_STATUS_INVALID_ARGUMENT;
237
238 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
239 if (!id)
240 return UBUS_STATUS_NOT_FOUND;
241
242 obj = container_of(id, struct ubus_object, id);
243
244 method = blob_data(attr[UBUS_ATTR_METHOD]);
245
246 if (!obj->client)
247 return obj->recv_msg(cl, method, attr[UBUS_ATTR_DATA]);
248
249 ub->hdr.peer = cl->id.id;
250 blob_buf_init(&b, 0);
251 ubusd_forward_invoke(obj, method, ub, attr[UBUS_ATTR_DATA]);
252 ubus_msg_free(ub);
253
254 return -1;
255 }
256
257 static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
258 {
259 struct ubus_object *obj = NULL;
260 struct ubus_subscription *s;
261 struct ubus_id *id;
262 const char *method;
263 bool no_reply = false;
264 void *c;
265
266 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
267 return UBUS_STATUS_INVALID_ARGUMENT;
268
269 if (attr[UBUS_ATTR_NO_REPLY])
270 no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
271
272 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
273 if (!id)
274 return UBUS_STATUS_NOT_FOUND;
275
276 obj = container_of(id, struct ubus_object, id);
277 if (obj->client != cl)
278 return UBUS_STATUS_PERMISSION_DENIED;
279
280 if (!no_reply) {
281 blob_buf_init(&b, 0);
282 blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
283 c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
284 list_for_each_entry(s, &obj->subscribers, list) {
285 blob_put_int32(&b, 0, s->subscriber->id.id);
286 }
287 blob_nest_end(&b, c);
288 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
289 ubus_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
290 }
291
292 ub->hdr.peer = cl->id.id;
293 method = blob_data(attr[UBUS_ATTR_METHOD]);
294 list_for_each_entry(s, &obj->subscribers, list) {
295 blob_buf_init(&b, 0);
296 if (no_reply)
297 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
298 ubusd_forward_invoke(s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
299 }
300 ubus_msg_free(ub);
301
302 return -1;
303 }
304
305 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
306 {
307 struct ubus_id *clid;
308
309 clid = ubus_find_id(&clients, id);
310 if (!clid)
311 return NULL;
312
313 return container_of(clid, struct ubus_client, id);
314 }
315
316 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
317 {
318 struct ubus_object *obj;
319
320 if (!attr[UBUS_ATTR_OBJID] ||
321 (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
322 (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
323 goto error;
324
325 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
326 if (!obj)
327 goto error;
328
329 if (cl != obj->client)
330 goto error;
331
332 cl = ubusd_get_client_by_id(ub->hdr.peer);
333 if (!cl)
334 goto error;
335
336 ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
337 ubus_msg_send(cl, ub, true);
338 return -1;
339
340 error:
341 ubus_msg_free(ub);
342 return -1;
343 }
344
345 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
346 {
347 struct ubus_object *obj, *target;
348
349 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
350 return UBUS_STATUS_INVALID_ARGUMENT;
351
352 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
353 if (!obj)
354 return UBUS_STATUS_NOT_FOUND;
355
356 if (cl != obj->client)
357 return UBUS_STATUS_INVALID_ARGUMENT;
358
359 target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
360 if (!target)
361 return UBUS_STATUS_NOT_FOUND;
362
363 if (cl == target->client)
364 return UBUS_STATUS_INVALID_ARGUMENT;
365
366 ubus_subscribe(obj, target);
367 return 0;
368 }
369
370 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
371 {
372 struct ubus_object *obj;
373 struct ubus_subscription *s;
374 uint32_t id;
375
376 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
377 return UBUS_STATUS_INVALID_ARGUMENT;
378
379 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
380 if (!obj)
381 return UBUS_STATUS_NOT_FOUND;
382
383 if (cl != obj->client)
384 return UBUS_STATUS_INVALID_ARGUMENT;
385
386 id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
387 list_for_each_entry(s, &obj->target_list, target_list) {
388 if (s->target->id.id != id)
389 continue;
390
391 ubus_unsubscribe(s);
392 return 0;
393 }
394
395 return UBUS_STATUS_NOT_FOUND;
396 }
397
398 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
399 [UBUS_MSG_PING] = ubusd_send_pong,
400 [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
401 [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
402 [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
403 [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
404 [UBUS_MSG_STATUS] = ubusd_handle_response,
405 [UBUS_MSG_DATA] = ubusd_handle_response,
406 [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
407 [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
408 [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
409 };
410
411 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
412 {
413 ubus_cmd_cb cb = NULL;
414 int ret;
415
416 retmsg->hdr.seq = ub->hdr.seq;
417 retmsg->hdr.peer = ub->hdr.peer;
418
419 if (ub->hdr.type < __UBUS_MSG_LAST)
420 cb = handlers[ub->hdr.type];
421
422 if (ub->hdr.type != UBUS_MSG_STATUS)
423 ubus_msg_close_fd(ub);
424
425 if (cb)
426 ret = cb(cl, ub, ubus_parse_msg(ub->data));
427 else
428 ret = UBUS_STATUS_INVALID_COMMAND;
429
430 if (ret == -1)
431 return;
432
433 ubus_msg_free(ub);
434
435 *retmsg_data = htonl(ret);
436 ubus_msg_send(cl, retmsg, false);
437 }
438
439 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
440 {
441 struct ubus_client *cl;
442
443 cl = calloc(1, sizeof(*cl));
444 if (!cl)
445 return NULL;
446
447 INIT_LIST_HEAD(&cl->objects);
448 cl->sock.fd = fd;
449 cl->sock.cb = cb;
450 cl->pending_msg_fd = -1;
451
452 if (!ubus_alloc_id(&clients, &cl->id, 0))
453 goto free;
454
455 if (!ubusd_send_hello(cl))
456 goto delete;
457
458 return cl;
459
460 delete:
461 ubus_free_id(&clients, &cl->id);
462 free:
463 free(cl);
464 return NULL;
465 }
466
467 void ubusd_proto_free_client(struct ubus_client *cl)
468 {
469 struct ubus_object *obj;
470
471 while (!list_empty(&cl->objects)) {
472 obj = list_first_entry(&cl->objects, struct ubus_object, list);
473 ubusd_free_object(obj);
474 }
475
476 ubus_free_id(&clients, &cl->id);
477 }
478
479 void ubus_notify_subscription(struct ubus_object *obj)
480 {
481 bool active = !list_empty(&obj->subscribers);
482 struct ubus_msg_buf *ub;
483
484 blob_buf_init(&b, 0);
485 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
486 blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
487
488 ub = ubus_msg_from_blob(false);
489 if (!ub)
490 return;
491
492 ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
493 ubus_msg_send(obj->client, ub, true);
494 }
495
496 void ubus_notify_unsubscribe(struct ubus_subscription *s)
497 {
498 struct ubus_msg_buf *ub;
499
500 blob_buf_init(&b, 0);
501 blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
502 blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
503
504 ub = ubus_msg_from_blob(false);
505 if (ub != NULL) {
506 ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
507 ubus_msg_send(s->subscriber->client, ub, true);
508 }
509
510 ubus_unsubscribe(s);
511 }
512
513 static void __constructor ubusd_proto_init(void)
514 {
515 ubus_init_id_tree(&clients);
516
517 blob_buf_init(&b, 0);
518 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
519
520 retmsg = ubus_msg_from_blob(false);
521 if (!retmsg)
522 exit(1);
523
524 retmsg->hdr.type = UBUS_MSG_STATUS;
525 retmsg_data = blob_data(blob_data(retmsg->data));
526 }