ubus: drop unused "obj" arguments
[project/uhttpd.git] / ubus.c
1 /*
2 * uhttpd - Tiny single-threaded httpd
3 *
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <libubox/blobmsg.h>
21 #include <libubox/blobmsg_json.h>
22 #include <libubox/avl.h>
23 #include <libubox/avl-cmp.h>
24 #include <stdio.h>
25 #include <poll.h>
26
27 #include "uhttpd.h"
28 #include "plugin.h"
29
30 static const struct uhttpd_ops *ops;
31 static struct config *_conf;
32 #define conf (*_conf)
33
34 static struct ubus_context *ctx;
35 static struct blob_buf buf;
36
37 #define UH_UBUS_MAX_POST_SIZE 65536
38 #define UH_UBUS_DEFAULT_SID "00000000000000000000000000000000"
39
40 enum {
41 RPC_JSONRPC,
42 RPC_METHOD,
43 RPC_PARAMS,
44 RPC_ID,
45 __RPC_MAX,
46 };
47
48 static const struct blobmsg_policy rpc_policy[__RPC_MAX] = {
49 [RPC_JSONRPC] = { .name = "jsonrpc", .type = BLOBMSG_TYPE_STRING },
50 [RPC_METHOD] = { .name = "method", .type = BLOBMSG_TYPE_STRING },
51 [RPC_PARAMS] = { .name = "params", .type = BLOBMSG_TYPE_ARRAY },
52 [RPC_ID] = { .name = "id", .type = BLOBMSG_TYPE_UNSPEC },
53 };
54
55 enum {
56 SES_ACCESS,
57 __SES_MAX,
58 };
59
60 static const struct blobmsg_policy ses_policy[__SES_MAX] = {
61 [SES_ACCESS] = { .name = "access", .type = BLOBMSG_TYPE_BOOL },
62 };
63
64 struct rpc_data {
65 struct blob_attr *id;
66 const char *sid;
67 const char *method;
68 const char *object;
69 const char *function;
70 struct blob_attr *data;
71 struct blob_attr *params;
72 };
73
74 struct list_data {
75 bool verbose;
76 struct blob_buf *buf;
77 };
78
79 enum rpc_error {
80 ERROR_PARSE,
81 ERROR_REQUEST,
82 ERROR_METHOD,
83 ERROR_PARAMS,
84 ERROR_INTERNAL,
85 ERROR_OBJECT,
86 ERROR_SESSION,
87 ERROR_ACCESS,
88 ERROR_TIMEOUT,
89 __ERROR_MAX
90 };
91
92 static const struct {
93 int code;
94 const char *msg;
95 } json_errors[__ERROR_MAX] = {
96 [ERROR_PARSE] = { -32700, "Parse error" },
97 [ERROR_REQUEST] = { -32600, "Invalid request" },
98 [ERROR_METHOD] = { -32601, "Method not found" },
99 [ERROR_PARAMS] = { -32602, "Invalid parameters" },
100 [ERROR_INTERNAL] = { -32603, "Internal error" },
101 [ERROR_OBJECT] = { -32000, "Object not found" },
102 [ERROR_SESSION] = { -32001, "Session not found" },
103 [ERROR_ACCESS] = { -32002, "Access denied" },
104 [ERROR_TIMEOUT] = { -32003, "ubus request timed out" },
105 };
106
107 enum cors_hdr {
108 HDR_ORIGIN,
109 HDR_ACCESS_CONTROL_REQUEST_METHOD,
110 HDR_ACCESS_CONTROL_REQUEST_HEADERS,
111 __HDR_MAX
112 };
113
114 static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout);
115
116 static void uh_ubus_next_batched_request(struct client *cl)
117 {
118 struct dispatch_ubus *du = &cl->dispatch.ubus;
119
120 du->timeout.cb = __uh_ubus_next_batched_request;
121 uloop_timeout_set(&du->timeout, 1);
122 }
123
124 static void uh_ubus_add_cors_headers(struct client *cl)
125 {
126 struct blob_attr *tb[__HDR_MAX];
127 static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
128 [HDR_ORIGIN] = { "origin", BLOBMSG_TYPE_STRING },
129 [HDR_ACCESS_CONTROL_REQUEST_METHOD] = { "access-control-request-method", BLOBMSG_TYPE_STRING },
130 [HDR_ACCESS_CONTROL_REQUEST_HEADERS] = { "access-control-request-headers", BLOBMSG_TYPE_STRING },
131 };
132
133 blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(cl->hdr.head), blob_len(cl->hdr.head));
134
135 if (!tb[HDR_ORIGIN])
136 return;
137
138 if (tb[HDR_ACCESS_CONTROL_REQUEST_METHOD])
139 {
140 char *hdr = (char *) blobmsg_data(tb[HDR_ACCESS_CONTROL_REQUEST_METHOD]);
141
142 if (strcmp(hdr, "POST") && strcmp(hdr, "OPTIONS"))
143 return;
144 }
145
146 ustream_printf(cl->us, "Access-Control-Allow-Origin: %s\r\n",
147 blobmsg_get_string(tb[HDR_ORIGIN]));
148
149 if (tb[HDR_ACCESS_CONTROL_REQUEST_HEADERS])
150 ustream_printf(cl->us, "Access-Control-Allow-Headers: %s\r\n",
151 blobmsg_get_string(tb[HDR_ACCESS_CONTROL_REQUEST_HEADERS]));
152
153 ustream_printf(cl->us, "Access-Control-Allow-Methods: POST, OPTIONS\r\n");
154 ustream_printf(cl->us, "Access-Control-Allow-Credentials: true\r\n");
155 }
156
157 static void uh_ubus_send_header(struct client *cl)
158 {
159 ops->http_header(cl, 200, "OK");
160
161 if (conf.ubus_cors)
162 uh_ubus_add_cors_headers(cl);
163
164 ustream_printf(cl->us, "Content-Type: application/json\r\n");
165
166 if (cl->request.method == UH_HTTP_MSG_OPTIONS)
167 ustream_printf(cl->us, "Content-Length: 0\r\n");
168
169 ustream_printf(cl->us, "\r\n");
170 }
171
172 static void uh_ubus_send_response(struct client *cl)
173 {
174 struct dispatch_ubus *du = &cl->dispatch.ubus;
175 const char *sep = "";
176 char *str;
177
178 if (du->array && du->array_idx > 1)
179 sep = ",";
180
181 str = blobmsg_format_json(buf.head, true);
182 ops->chunk_printf(cl, "%s%s", sep, str);
183 free(str);
184
185 du->jsobj_cur = NULL;
186 if (du->array)
187 uh_ubus_next_batched_request(cl);
188 else
189 return ops->request_done(cl);
190 }
191
192 static void uh_ubus_init_response(struct client *cl)
193 {
194 struct dispatch_ubus *du = &cl->dispatch.ubus;
195 struct json_object *obj = du->jsobj_cur, *obj2 = NULL;
196
197 blob_buf_init(&buf, 0);
198 blobmsg_add_string(&buf, "jsonrpc", "2.0");
199
200 if (obj)
201 json_object_object_get_ex(obj, "id", &obj2);
202
203 if (obj2)
204 blobmsg_add_json_element(&buf, "id", obj2);
205 else
206 blobmsg_add_field(&buf, BLOBMSG_TYPE_UNSPEC, "id", NULL, 0);
207 }
208
209 static void uh_ubus_json_error(struct client *cl, enum rpc_error type)
210 {
211 void *c;
212
213 uh_ubus_init_response(cl);
214 c = blobmsg_open_table(&buf, "error");
215 blobmsg_add_u32(&buf, "code", json_errors[type].code);
216 blobmsg_add_string(&buf, "message", json_errors[type].msg);
217 blobmsg_close_table(&buf, c);
218 uh_ubus_send_response(cl);
219 }
220
221 static void
222 uh_ubus_request_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
223 {
224 struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
225
226 blobmsg_add_field(&du->buf, BLOBMSG_TYPE_TABLE, "", blob_data(msg), blob_len(msg));
227 }
228
229 static void
230 uh_ubus_request_cb(struct ubus_request *req, int ret)
231 {
232 struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
233 struct client *cl = container_of(du, struct client, dispatch.ubus);
234 struct blob_attr *cur;
235 void *r;
236 int rem;
237
238 uloop_timeout_cancel(&du->timeout);
239 uh_ubus_init_response(cl);
240 r = blobmsg_open_array(&buf, "result");
241 blobmsg_add_u32(&buf, "", ret);
242 blob_for_each_attr(cur, du->buf.head, rem)
243 blobmsg_add_blob(&buf, cur);
244 blobmsg_close_array(&buf, r);
245 uh_ubus_send_response(cl);
246 }
247
248 static void
249 uh_ubus_timeout_cb(struct uloop_timeout *timeout)
250 {
251 struct dispatch_ubus *du = container_of(timeout, struct dispatch_ubus, timeout);
252 struct client *cl = container_of(du, struct client, dispatch.ubus);
253
254 ubus_abort_request(ctx, &du->req);
255 uh_ubus_json_error(cl, ERROR_TIMEOUT);
256 }
257
258 static void uh_ubus_close_fds(struct client *cl)
259 {
260 if (ctx->sock.fd < 0)
261 return;
262
263 close(ctx->sock.fd);
264 ctx->sock.fd = -1;
265 }
266
267 static void uh_ubus_request_free(struct client *cl)
268 {
269 struct dispatch_ubus *du = &cl->dispatch.ubus;
270
271 blob_buf_free(&du->buf);
272 uloop_timeout_cancel(&du->timeout);
273
274 if (du->jsobj)
275 json_object_put(du->jsobj);
276
277 if (du->jstok)
278 json_tokener_free(du->jstok);
279
280 if (du->req_pending)
281 ubus_abort_request(ctx, &du->req);
282 }
283
284 static void uh_ubus_single_error(struct client *cl, enum rpc_error type)
285 {
286 uh_ubus_send_header(cl);
287 uh_ubus_json_error(cl, type);
288 ops->request_done(cl);
289 }
290
291 static void uh_ubus_send_request(struct client *cl, const char *sid, struct blob_attr *args)
292 {
293 struct dispatch *d = &cl->dispatch;
294 struct dispatch_ubus *du = &d->ubus;
295 struct blob_attr *cur;
296 static struct blob_buf req;
297 int ret, rem;
298
299 blob_buf_init(&req, 0);
300 blobmsg_for_each_attr(cur, args, rem) {
301 if (!strcmp(blobmsg_name(cur), "ubus_rpc_session"))
302 return uh_ubus_json_error(cl, ERROR_PARAMS);
303 blobmsg_add_blob(&req, cur);
304 }
305
306 blobmsg_add_string(&req, "ubus_rpc_session", sid);
307
308 blob_buf_init(&du->buf, 0);
309 memset(&du->req, 0, sizeof(du->req));
310 ret = ubus_invoke_async(ctx, du->obj, du->func, req.head, &du->req);
311 if (ret)
312 return uh_ubus_json_error(cl, ERROR_INTERNAL);
313
314 du->req.data_cb = uh_ubus_request_data_cb;
315 du->req.complete_cb = uh_ubus_request_cb;
316 ubus_complete_request_async(ctx, &du->req);
317
318 du->timeout.cb = uh_ubus_timeout_cb;
319 uloop_timeout_set(&du->timeout, conf.script_timeout * 1000);
320
321 du->req_pending = true;
322 }
323
324 static void uh_ubus_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
325 {
326 struct blob_attr *sig, *attr;
327 struct list_data *data = priv;
328 int rem, rem2;
329 void *t, *o;
330
331 if (!data->verbose) {
332 blobmsg_add_string(data->buf, NULL, obj->path);
333 return;
334 }
335
336 if (!obj->signature)
337 return;
338
339 o = blobmsg_open_table(data->buf, obj->path);
340 blob_for_each_attr(sig, obj->signature, rem) {
341 t = blobmsg_open_table(data->buf, blobmsg_name(sig));
342 rem2 = blobmsg_data_len(sig);
343 __blob_for_each_attr(attr, blobmsg_data(sig), rem2) {
344 if (blob_id(attr) != BLOBMSG_TYPE_INT32)
345 continue;
346
347 switch (blobmsg_get_u32(attr)) {
348 case BLOBMSG_TYPE_INT8:
349 blobmsg_add_string(data->buf, blobmsg_name(attr), "boolean");
350 break;
351 case BLOBMSG_TYPE_INT32:
352 blobmsg_add_string(data->buf, blobmsg_name(attr), "number");
353 break;
354 case BLOBMSG_TYPE_STRING:
355 blobmsg_add_string(data->buf, blobmsg_name(attr), "string");
356 break;
357 case BLOBMSG_TYPE_ARRAY:
358 blobmsg_add_string(data->buf, blobmsg_name(attr), "array");
359 break;
360 case BLOBMSG_TYPE_TABLE:
361 blobmsg_add_string(data->buf, blobmsg_name(attr), "object");
362 break;
363 default:
364 blobmsg_add_string(data->buf, blobmsg_name(attr), "unknown");
365 break;
366 }
367 }
368 blobmsg_close_table(data->buf, t);
369 }
370 blobmsg_close_table(data->buf, o);
371 }
372
373 static void uh_ubus_send_list(struct client *cl, struct blob_attr *params)
374 {
375 struct blob_attr *cur, *dup;
376 struct list_data data = { .buf = &cl->dispatch.ubus.buf, .verbose = false };
377 void *r;
378 int rem;
379
380 blob_buf_init(data.buf, 0);
381
382 uh_client_ref(cl);
383
384 if (!params || blob_id(params) != BLOBMSG_TYPE_ARRAY) {
385 r = blobmsg_open_array(data.buf, "result");
386 ubus_lookup(ctx, NULL, uh_ubus_list_cb, &data);
387 blobmsg_close_array(data.buf, r);
388 }
389 else {
390 r = blobmsg_open_table(data.buf, "result");
391 dup = blob_memdup(params);
392 if (dup)
393 {
394 rem = blobmsg_data_len(dup);
395 data.verbose = true;
396 __blob_for_each_attr(cur, blobmsg_data(dup), rem)
397 ubus_lookup(ctx, blobmsg_data(cur), uh_ubus_list_cb, &data);
398 free(dup);
399 }
400 blobmsg_close_table(data.buf, r);
401 }
402
403 uh_client_unref(cl);
404
405 uh_ubus_init_response(cl);
406 blobmsg_add_blob(&buf, blob_data(data.buf->head));
407 uh_ubus_send_response(cl);
408 }
409
410 static bool parse_json_rpc(struct rpc_data *d, struct blob_attr *data)
411 {
412 struct blob_attr *tb[__RPC_MAX];
413 struct blob_attr *cur;
414
415 blobmsg_parse(rpc_policy, __RPC_MAX, tb, blob_data(data), blob_len(data));
416
417 cur = tb[RPC_JSONRPC];
418 if (!cur || strcmp(blobmsg_data(cur), "2.0") != 0)
419 return false;
420
421 cur = tb[RPC_METHOD];
422 if (!cur)
423 return false;
424
425 d->id = tb[RPC_ID];
426 d->method = blobmsg_data(cur);
427
428 cur = tb[RPC_PARAMS];
429 if (!cur)
430 return true;
431
432 d->params = blob_memdup(cur);
433 if (!d->params)
434 return false;
435
436 return true;
437 }
438
439 static void parse_call_params(struct rpc_data *d)
440 {
441 const struct blobmsg_policy data_policy[] = {
442 { .type = BLOBMSG_TYPE_STRING },
443 { .type = BLOBMSG_TYPE_STRING },
444 { .type = BLOBMSG_TYPE_STRING },
445 { .type = BLOBMSG_TYPE_TABLE },
446 };
447 struct blob_attr *tb[4];
448
449 blobmsg_parse_array(data_policy, ARRAY_SIZE(data_policy), tb,
450 blobmsg_data(d->params), blobmsg_data_len(d->params));
451
452 if (tb[0])
453 d->sid = blobmsg_data(tb[0]);
454
455 if (conf.ubus_noauth && (!d->sid || !*d->sid))
456 d->sid = UH_UBUS_DEFAULT_SID;
457
458 if (tb[1])
459 d->object = blobmsg_data(tb[1]);
460
461 if (tb[2])
462 d->function = blobmsg_data(tb[2]);
463
464 d->data = tb[3];
465 }
466
467 static void uh_ubus_init_batch(struct client *cl)
468 {
469 struct dispatch_ubus *du = &cl->dispatch.ubus;
470
471 du->array = true;
472 uh_ubus_send_header(cl);
473 ops->chunk_printf(cl, "[");
474 }
475
476 static void uh_ubus_complete_batch(struct client *cl)
477 {
478 ops->chunk_printf(cl, "]");
479 ops->request_done(cl);
480 }
481
482 static void uh_ubus_allowed_cb(struct ubus_request *req, int type, struct blob_attr *msg)
483 {
484 struct blob_attr *tb[__SES_MAX];
485 bool *allow = (bool *)req->priv;
486
487 if (!msg)
488 return;
489
490 blobmsg_parse(ses_policy, __SES_MAX, tb, blob_data(msg), blob_len(msg));
491
492 if (tb[SES_ACCESS])
493 *allow = blobmsg_get_bool(tb[SES_ACCESS]);
494 }
495
496 static bool uh_ubus_allowed(const char *sid, const char *obj, const char *fun)
497 {
498 uint32_t id;
499 bool allow = false;
500 static struct blob_buf req;
501
502 if (ubus_lookup_id(ctx, "session", &id))
503 return false;
504
505 blob_buf_init(&req, 0);
506 blobmsg_add_string(&req, "ubus_rpc_session", sid);
507 blobmsg_add_string(&req, "object", obj);
508 blobmsg_add_string(&req, "function", fun);
509
510 ubus_invoke(ctx, id, "access", req.head, uh_ubus_allowed_cb, &allow, conf.script_timeout * 500);
511
512 return allow;
513 }
514
515 static void uh_ubus_handle_request_object(struct client *cl, struct json_object *obj)
516 {
517 struct dispatch_ubus *du = &cl->dispatch.ubus;
518 struct rpc_data data = {};
519 enum rpc_error err = ERROR_PARSE;
520
521 uh_client_ref(cl);
522
523 if (json_object_get_type(obj) != json_type_object)
524 goto error;
525
526 du->jsobj_cur = obj;
527 blob_buf_init(&buf, 0);
528 if (!blobmsg_add_object(&buf, obj))
529 goto error;
530
531 if (!parse_json_rpc(&data, buf.head))
532 goto error;
533
534 if (!strcmp(data.method, "call")) {
535 parse_call_params(&data);
536
537 if (!data.sid || !data.object || !data.function || !data.data)
538 goto error;
539
540 du->func = data.function;
541 if (ubus_lookup_id(ctx, data.object, &du->obj)) {
542 err = ERROR_OBJECT;
543 goto error;
544 }
545
546 if (!conf.ubus_noauth && !uh_ubus_allowed(data.sid, data.object, data.function)) {
547 err = ERROR_ACCESS;
548 goto error;
549 }
550
551 uh_ubus_send_request(cl, data.sid, data.data);
552 goto out;
553 }
554 else if (!strcmp(data.method, "list")) {
555 uh_ubus_send_list(cl, data.params);
556 goto out;
557 }
558 else {
559 err = ERROR_METHOD;
560 goto error;
561 }
562
563 error:
564 uh_ubus_json_error(cl, err);
565 out:
566 if (data.params)
567 free(data.params);
568
569 uh_client_unref(cl);
570 }
571
572 static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout)
573 {
574 struct dispatch_ubus *du = container_of(timeout, struct dispatch_ubus, timeout);
575 struct client *cl = container_of(du, struct client, dispatch.ubus);
576 struct json_object *obj = du->jsobj;
577 int len;
578
579 len = json_object_array_length(obj);
580 if (du->array_idx >= len)
581 return uh_ubus_complete_batch(cl);
582
583 obj = json_object_array_get_idx(obj, du->array_idx++);
584 uh_ubus_handle_request_object(cl, obj);
585 }
586
587 static void uh_ubus_data_done(struct client *cl)
588 {
589 struct dispatch_ubus *du = &cl->dispatch.ubus;
590 struct json_object *obj = du->jsobj;
591
592 switch (obj ? json_object_get_type(obj) : json_type_null) {
593 case json_type_object:
594 uh_ubus_send_header(cl);
595 return uh_ubus_handle_request_object(cl, obj);
596 case json_type_array:
597 uh_ubus_init_batch(cl);
598 return uh_ubus_next_batched_request(cl);
599 default:
600 return uh_ubus_single_error(cl, ERROR_PARSE);
601 }
602 }
603
604 static int uh_ubus_data_send(struct client *cl, const char *data, int len)
605 {
606 struct dispatch_ubus *du = &cl->dispatch.ubus;
607
608 if (du->jsobj || !du->jstok)
609 goto error;
610
611 du->post_len += len;
612 if (du->post_len > UH_UBUS_MAX_POST_SIZE)
613 goto error;
614
615 du->jsobj = json_tokener_parse_ex(du->jstok, data, len);
616 return len;
617
618 error:
619 uh_ubus_single_error(cl, ERROR_PARSE);
620 return 0;
621 }
622
623 static void uh_ubus_handle_request(struct client *cl, char *url, struct path_info *pi)
624 {
625 struct dispatch *d = &cl->dispatch;
626
627 blob_buf_init(&buf, 0);
628
629 switch (cl->request.method)
630 {
631 case UH_HTTP_MSG_POST:
632 d->data_send = uh_ubus_data_send;
633 d->data_done = uh_ubus_data_done;
634 d->close_fds = uh_ubus_close_fds;
635 d->free = uh_ubus_request_free;
636 d->ubus.jstok = json_tokener_new();
637 break;
638
639 case UH_HTTP_MSG_OPTIONS:
640 uh_ubus_send_header(cl);
641 ops->request_done(cl);
642 break;
643
644 default:
645 ops->client_error(cl, 400, "Bad Request", "Invalid Request");
646 }
647 }
648
649 static bool
650 uh_ubus_check_url(const char *url)
651 {
652 return ops->path_match(conf.ubus_prefix, url);
653 }
654
655 static int
656 uh_ubus_init(void)
657 {
658 static struct dispatch_handler ubus_dispatch = {
659 .check_url = uh_ubus_check_url,
660 .handle_request = uh_ubus_handle_request,
661 };
662
663 ctx = ubus_connect(conf.ubus_socket);
664 if (!ctx) {
665 fprintf(stderr, "Unable to connect to ubus socket\n");
666 exit(1);
667 }
668
669 ops->dispatch_add(&ubus_dispatch);
670
671 uloop_done();
672 return 0;
673 }
674
675
676 static int uh_ubus_plugin_init(const struct uhttpd_ops *o, struct config *c)
677 {
678 ops = o;
679 _conf = c;
680 return uh_ubus_init();
681 }
682
683 static void uh_ubus_post_init(void)
684 {
685 ubus_add_uloop(ctx);
686 }
687
688 struct uhttpd_plugin uhttpd_plugin = {
689 .init = uh_ubus_plugin_init,
690 .post_init = uh_ubus_post_init,
691 };