libubus: limit stack depth for incoming invoke requests
[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 wildcard = false;
167         bool found = false;
168         int len;
169
170         if (!attr[UBUS_ATTR_OBJPATH]) {
171                 avl_for_each_element(&path, obj, path)
172                         ubusd_send_obj(cl, ub, obj);
173                 return 0;
174         }
175
176         objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
177         len = strlen(objpath);
178         if (objpath[len - 1] != '*') {
179                 obj = avl_find_element(&path, objpath, obj, path);
180                 if (!obj)
181                         return UBUS_STATUS_NOT_FOUND;
182
183                 ubusd_send_obj(cl, ub, obj);
184                 return 0;
185         }
186
187         objpath[--len] = 0;
188         wildcard = true;
189
190         obj = avl_find_ge_element(&path, objpath, obj, path);
191         if (!obj)
192                 return UBUS_STATUS_NOT_FOUND;
193
194         while (!strncmp(objpath, obj->path.key, len)) {
195                 found = true;
196                 ubusd_send_obj(cl, ub, obj);
197                 if (obj == avl_last_element(&path, obj, path))
198                         break;
199                 obj = avl_next_element(obj, path);
200         }
201
202         if (!found)
203                 return UBUS_STATUS_NOT_FOUND;
204
205         return 0;
206 }
207
208 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
209 {
210         struct ubus_msg_buf *ub_new;
211         struct ubus_object *obj = NULL;
212         struct ubus_id *id;
213         const char *method;
214
215         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
216                 return UBUS_STATUS_INVALID_ARGUMENT;
217
218         id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
219         if (!id)
220                 return UBUS_STATUS_NOT_FOUND;
221
222         obj = container_of(id, struct ubus_object, id);
223
224         method = blob_data(attr[UBUS_ATTR_METHOD]);
225
226         if (!obj->client)
227                 return obj->recv_msg(cl, method, attr[UBUS_ATTR_DATA]);
228
229         blob_buf_init(&b, 0);
230         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
231         blob_put_string(&b, UBUS_ATTR_METHOD, method);
232         if (attr[UBUS_ATTR_DATA])
233                 blob_put(&b, UBUS_ATTR_DATA, blob_data(attr[UBUS_ATTR_DATA]),
234                          blob_len(attr[UBUS_ATTR_DATA]));
235
236         ub_new = ubus_reply_from_blob(ub, true);
237         ubus_msg_free(ub);
238         ub = ub_new;
239
240         if (!ub)
241                 return UBUS_STATUS_NO_DATA;
242
243         ub->hdr.type = UBUS_MSG_INVOKE;
244         ub->hdr.peer = cl->id.id;
245         ubus_msg_send(obj->client, ub, true);
246
247         return -1;
248 }
249
250 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
251 {
252         struct ubus_id *clid;
253
254         clid = ubus_find_id(&clients, id);
255         if (!clid)
256                 return NULL;
257
258         return container_of(clid, struct ubus_client, id);
259 }
260
261 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
262 {
263         struct ubus_object *obj;
264
265         if (!attr[UBUS_ATTR_OBJID] ||
266             (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
267             (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
268                 goto error;
269
270         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
271         if (!obj)
272                 goto error;
273
274         if (cl != obj->client)
275                 goto error;
276
277         cl = ubusd_get_client_by_id(ub->hdr.peer);
278         if (!cl)
279                 goto error;
280
281         ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
282         ubus_msg_send(cl, ub, true);
283         return -1;
284
285 error:
286         ubus_msg_free(ub);
287         return -1;
288 }
289
290 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
291         [UBUS_MSG_PING] = ubusd_send_pong,
292         [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
293         [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
294         [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
295         [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
296         [UBUS_MSG_STATUS] = ubusd_handle_response,
297         [UBUS_MSG_DATA] = ubusd_handle_response,
298 };
299
300 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
301 {
302         ubus_cmd_cb cb = NULL;
303         int ret;
304
305         retmsg->hdr.seq = ub->hdr.seq;
306         retmsg->hdr.peer = ub->hdr.peer;
307
308         if (ub->hdr.type < __UBUS_MSG_LAST)
309                 cb = handlers[ub->hdr.type];
310
311         if (cb)
312                 ret = cb(cl, ub, ubus_parse_msg(ub->data));
313         else
314                 ret = UBUS_STATUS_INVALID_COMMAND;
315
316         if (ret == -1)
317                 return;
318
319         ubus_msg_free(ub);
320
321         *retmsg_data = htonl(ret);
322         ubus_msg_send(cl, retmsg, false);
323 }
324
325 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
326 {
327         struct ubus_client *cl;
328
329         cl = calloc(1, sizeof(*cl));
330         if (!cl)
331                 return NULL;
332
333         INIT_LIST_HEAD(&cl->objects);
334         cl->sock.fd = fd;
335         cl->sock.cb = cb;
336
337         if (!ubus_alloc_id(&clients, &cl->id, 0))
338                 goto free;
339
340         if (!ubusd_send_hello(cl))
341                 goto delete;
342
343         return cl;
344
345 delete:
346         ubus_free_id(&clients, &cl->id);
347 free:
348         free(cl);
349         return NULL;
350 }
351
352 void ubusd_proto_free_client(struct ubus_client *cl)
353 {
354         struct ubus_object *obj;
355
356         while (!list_empty(&cl->objects)) {
357                 obj = list_first_entry(&cl->objects, struct ubus_object, list);
358                 ubusd_free_object(obj);
359         }
360
361         ubus_free_id(&clients, &cl->id);
362 }
363
364 static void __init ubusd_proto_init(void)
365 {
366         ubus_init_id_tree(&clients);
367
368         blob_buf_init(&b, 0);
369         blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
370
371         retmsg = ubus_msg_from_blob(false);
372         if (!retmsg)
373                 exit(1);
374
375         retmsg->hdr.type = UBUS_MSG_STATUS;
376         retmsg_data = blob_data(blob_data(retmsg->data));
377 }