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