2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <libubox/blobmsg.h>
22 #include <libubox/blobmsg_json.h>
23 #include <libubox/avl.h>
24 #include <libubox/avl-cmp.h>
33 static const struct uhttpd_ops *ops;
34 static struct config *_conf;
37 static struct ubus_context *ctx;
38 static struct avl_tree sessions;
39 static struct blob_buf buf;
41 static const struct blobmsg_policy new_policy = {
42 .name = "timeout", .type = BLOBMSG_TYPE_INT32
45 static const struct blobmsg_policy sid_policy = {
46 .name = "sid", .type = BLOBMSG_TYPE_STRING
54 static const struct blobmsg_policy set_policy[__UH_UBUS_SS_MAX] = {
55 [UH_UBUS_SS_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
56 [UH_UBUS_SS_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
64 static const struct blobmsg_policy get_policy[__UH_UBUS_SG_MAX] = {
65 [UH_UBUS_SG_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
66 [UH_UBUS_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
74 static const struct blobmsg_policy acl_policy[__UH_UBUS_SA_MAX] = {
75 [UH_UBUS_SA_SID] = { .name = "sid", .type = BLOBMSG_TYPE_STRING },
76 [UH_UBUS_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY },
80 * Keys in the AVL tree contain all pattern characters up to the first wildcard.
81 * To look up entries, start with the last entry that has a key less than or
82 * equal to the method name, then work backwards as long as the AVL key still
83 * matches its counterpart in the object name
85 #define uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func) \
86 for (_acl = avl_find_le_element(&(_ses)->acls, _obj, _acl, avl); \
88 _acl = avl_is_first(&(ses)->acls, &(_acl)->avl) ? NULL : \
89 avl_prev_element((_acl), avl))
91 #define uh_foreach_matching_acl(_acl, _ses, _obj, _func) \
92 uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func) \
93 if (!strncmp((_acl)->object, _obj, (_acl)->sort_len &&) \
94 !fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) && \
95 !fnmatch((_acl)->function, (_func), FNM_NOESCAPE))
98 uh_ubus_random(char *dest)
100 unsigned char buf[16] = { 0 };
104 f = fopen("/dev/urandom", "r");
108 fread(buf, 1, sizeof(buf), f);
111 for (i = 0; i < sizeof(buf); i++)
112 sprintf(dest + (i<<1), "%02x", buf[i]);
116 uh_ubus_session_dump_data(struct uh_ubus_session *ses, struct blob_buf *b)
118 struct uh_ubus_session_data *d;
120 avl_for_each_element(&ses->data, d, avl) {
121 blobmsg_add_field(b, blobmsg_type(d->attr), blobmsg_name(d->attr),
122 blobmsg_data(d->attr), blobmsg_data_len(d->attr));
127 uh_ubus_session_dump_acls(struct uh_ubus_session *ses, struct blob_buf *b)
129 struct uh_ubus_session_acl *acl;
130 const char *lastobj = NULL;
133 avl_for_each_element(&ses->acls, acl, avl) {
134 if (!lastobj || strcmp(acl->object, lastobj))
136 if (c) blobmsg_close_array(b, c);
137 c = blobmsg_open_array(b, acl->object);
140 blobmsg_add_string(b, NULL, acl->function);
141 lastobj = acl->object;
144 if (c) blobmsg_close_array(b, c);
148 uh_ubus_session_dump(struct uh_ubus_session *ses,
149 struct ubus_context *ctx,
150 struct ubus_request_data *req)
155 memset(&b, 0, sizeof(b));
156 blob_buf_init(&b, 0);
158 blobmsg_add_string(&b, "sid", ses->id);
159 blobmsg_add_u32(&b, "timeout", ses->timeout);
161 c = blobmsg_open_table(&b, "acls");
162 uh_ubus_session_dump_acls(ses, &b);
163 blobmsg_close_table(&b, c);
165 c = blobmsg_open_table(&b, "data");
166 uh_ubus_session_dump_data(ses, &b);
167 blobmsg_close_table(&b, c);
169 ubus_send_reply(ctx, req, b.head);
174 uh_ubus_touch_session(struct uh_ubus_session *ses)
176 uloop_timeout_set(&ses->t, ses->timeout * 1000);
180 uh_ubus_session_destroy(struct uh_ubus_session *ses)
182 struct uh_ubus_session_acl *acl, *nacl;
183 struct uh_ubus_session_data *data, *ndata;
185 uloop_timeout_cancel(&ses->t);
186 avl_remove_all_elements(&ses->acls, acl, avl, nacl)
189 avl_remove_all_elements(&ses->data, data, avl, ndata)
192 avl_delete(&sessions, &ses->avl);
196 static void uh_ubus_session_timeout(struct uloop_timeout *t)
198 struct uh_ubus_session *ses;
200 ses = container_of(t, struct uh_ubus_session, t);
201 uh_ubus_session_destroy(ses);
204 static struct uh_ubus_session *
205 uh_ubus_session_create(int timeout)
207 struct uh_ubus_session *ses;
209 ses = calloc(1, sizeof(*ses));
213 ses->timeout = timeout;
214 ses->avl.key = ses->id;
215 uh_ubus_random(ses->id);
217 avl_insert(&sessions, &ses->avl);
218 avl_init(&ses->acls, avl_strcmp, true, NULL);
219 avl_init(&ses->data, avl_strcmp, false, NULL);
221 ses->t.cb = uh_ubus_session_timeout;
222 uh_ubus_touch_session(ses);
227 static struct uh_ubus_session *
228 uh_ubus_session_get(const char *id)
230 struct uh_ubus_session *ses;
232 ses = avl_find_element(&sessions, id, ses, avl);
236 uh_ubus_touch_session(ses);
241 uh_ubus_handle_create(struct ubus_context *ctx, struct ubus_object *obj,
242 struct ubus_request_data *req, const char *method,
243 struct blob_attr *msg)
245 struct uh_ubus_session *ses;
246 struct blob_attr *tb;
247 int timeout = conf.script_timeout;
249 blobmsg_parse(&new_policy, 1, &tb, blob_data(msg), blob_len(msg));
251 timeout = blobmsg_get_u32(tb);
253 ses = uh_ubus_session_create(timeout);
255 uh_ubus_session_dump(ses, ctx, req);
261 uh_ubus_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
262 struct ubus_request_data *req, const char *method,
263 struct blob_attr *msg)
265 struct uh_ubus_session *ses;
266 struct blob_attr *tb;
268 blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
271 avl_for_each_element(&sessions, ses, avl)
272 uh_ubus_session_dump(ses, ctx, req);
276 ses = uh_ubus_session_get(blobmsg_data(tb));
278 return UBUS_STATUS_NOT_FOUND;
280 uh_ubus_session_dump(ses, ctx, req);
286 uh_id_len(const char *str)
288 return strcspn(str, "*?[");
292 uh_ubus_session_grant(struct uh_ubus_session *ses, struct ubus_context *ctx,
293 const char *object, const char *function)
295 struct uh_ubus_session_acl *acl;
296 char *new_obj, *new_func, *new_id;
299 if (!object || !function)
300 return UBUS_STATUS_INVALID_ARGUMENT;
302 uh_foreach_matching_acl_prefix(acl, ses, object, function) {
303 if (!strcmp(acl->object, object) &&
304 !strcmp(acl->function, function))
308 id_len = uh_id_len(object);
309 acl = calloc_a(sizeof(*acl),
310 &new_obj, strlen(object) + 1,
311 &new_func, strlen(function) + 1,
312 &new_id, id_len + 1);
315 return UBUS_STATUS_UNKNOWN_ERROR;
317 acl->object = strcpy(new_obj, object);
318 acl->function = strcpy(new_func, function);
319 acl->avl.key = strncpy(new_id, object, id_len);
320 avl_insert(&ses->acls, &acl->avl);
326 uh_ubus_session_revoke(struct uh_ubus_session *ses, struct ubus_context *ctx,
327 const char *object, const char *function)
329 struct uh_ubus_session_acl *acl, *next;
333 if (!object && !function) {
334 avl_remove_all_elements(&ses->acls, acl, avl, next)
339 id_len = uh_id_len(object);
340 id = alloca(id_len + 1);
341 strncpy(id, object, id_len);
344 acl = avl_find_element(&ses->acls, id, acl, avl);
346 if (!avl_is_last(&ses->acls, &acl->avl))
347 next = avl_next_element(acl, avl);
351 if (strcmp(id, acl->avl.key) != 0)
354 if (!strcmp(acl->object, object) &&
355 !strcmp(acl->function, function)) {
356 avl_delete(&ses->acls, &acl->avl);
367 uh_ubus_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
368 struct ubus_request_data *req, const char *method,
369 struct blob_attr *msg)
371 struct uh_ubus_session *ses;
372 struct blob_attr *tb[__UH_UBUS_SA_MAX];
373 struct blob_attr *attr, *sattr;
374 const char *object, *function;
377 int (*cb)(struct uh_ubus_session *ses, struct ubus_context *ctx,
378 const char *object, const char *function);
380 blobmsg_parse(acl_policy, __UH_UBUS_SA_MAX, tb, blob_data(msg), blob_len(msg));
382 if (!tb[UH_UBUS_SA_SID])
383 return UBUS_STATUS_INVALID_ARGUMENT;
385 ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SA_SID]));
387 return UBUS_STATUS_NOT_FOUND;
389 if (!strcmp(method, "grant"))
390 cb = uh_ubus_session_grant;
392 cb = uh_ubus_session_revoke;
394 if (!tb[UH_UBUS_SA_OBJECTS])
395 return cb(ses, ctx, NULL, NULL);
397 blobmsg_for_each_attr(attr, tb[UH_UBUS_SA_OBJECTS], rem1) {
398 if (blob_id(attr) != BLOBMSG_TYPE_ARRAY)
404 blobmsg_for_each_attr(sattr, attr, rem2) {
405 if (blob_id(sattr) != BLOBMSG_TYPE_STRING)
409 object = blobmsg_data(sattr);
411 function = blobmsg_data(sattr);
416 if (object && function)
417 cb(ses, ctx, object, function);
424 uh_ubus_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
425 struct ubus_request_data *req, const char *method,
426 struct blob_attr *msg)
428 struct uh_ubus_session *ses;
429 struct uh_ubus_session_data *data;
430 struct blob_attr *tb[__UH_UBUS_SA_MAX];
431 struct blob_attr *attr;
434 blobmsg_parse(set_policy, __UH_UBUS_SS_MAX, tb, blob_data(msg), blob_len(msg));
436 if (!tb[UH_UBUS_SS_SID] || !tb[UH_UBUS_SS_VALUES])
437 return UBUS_STATUS_INVALID_ARGUMENT;
439 ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SS_SID]));
441 return UBUS_STATUS_NOT_FOUND;
443 blobmsg_for_each_attr(attr, tb[UH_UBUS_SS_VALUES], rem) {
444 if (!blobmsg_name(attr)[0])
447 data = avl_find_element(&ses->data, blobmsg_name(attr), data, avl);
449 avl_delete(&ses->data, &data->avl);
453 data = calloc(1, sizeof(*data) + blob_pad_len(attr));
457 memcpy(data->attr, attr, blob_pad_len(attr));
458 data->avl.key = blobmsg_name(data->attr);
459 avl_insert(&ses->data, &data->avl);
466 uh_ubus_handle_get(struct ubus_context *ctx, struct ubus_object *obj,
467 struct ubus_request_data *req, const char *method,
468 struct blob_attr *msg)
470 struct uh_ubus_session *ses;
471 struct uh_ubus_session_data *data;
472 struct blob_attr *tb[__UH_UBUS_SA_MAX];
473 struct blob_attr *attr;
478 blobmsg_parse(get_policy, __UH_UBUS_SG_MAX, tb, blob_data(msg), blob_len(msg));
480 if (!tb[UH_UBUS_SG_SID])
481 return UBUS_STATUS_INVALID_ARGUMENT;
483 ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SG_SID]));
485 return UBUS_STATUS_NOT_FOUND;
487 memset(&b, 0, sizeof(b));
488 blob_buf_init(&b, 0);
489 c = blobmsg_open_table(&b, "values");
491 if (!tb[UH_UBUS_SG_KEYS]) {
492 uh_ubus_session_dump_data(ses, &b);
496 blobmsg_for_each_attr(attr, tb[UH_UBUS_SG_KEYS], rem) {
497 if (blob_id(attr) != BLOBMSG_TYPE_STRING)
500 data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
504 blobmsg_add_field(&b, blobmsg_type(data->attr),
505 blobmsg_name(data->attr),
506 blobmsg_data(data->attr),
507 blobmsg_data_len(data->attr));
510 blobmsg_close_table(&b, c);
511 ubus_send_reply(ctx, req, b.head);
518 uh_ubus_handle_unset(struct ubus_context *ctx, struct ubus_object *obj,
519 struct ubus_request_data *req, const char *method,
520 struct blob_attr *msg)
522 struct uh_ubus_session *ses;
523 struct uh_ubus_session_data *data, *ndata;
524 struct blob_attr *tb[__UH_UBUS_SA_MAX];
525 struct blob_attr *attr;
528 blobmsg_parse(get_policy, __UH_UBUS_SG_MAX, tb, blob_data(msg), blob_len(msg));
530 if (!tb[UH_UBUS_SG_SID])
531 return UBUS_STATUS_INVALID_ARGUMENT;
533 ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SG_SID]));
535 return UBUS_STATUS_NOT_FOUND;
537 if (!tb[UH_UBUS_SG_KEYS]) {
538 avl_remove_all_elements(&ses->data, data, avl, ndata)
543 blobmsg_for_each_attr(attr, tb[UH_UBUS_SG_KEYS], rem) {
544 if (blob_id(attr) != BLOBMSG_TYPE_STRING)
547 data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
551 avl_delete(&ses->data, &data->avl);
559 uh_ubus_handle_destroy(struct ubus_context *ctx, struct ubus_object *obj,
560 struct ubus_request_data *req, const char *method,
561 struct blob_attr *msg)
563 struct uh_ubus_session *ses;
564 struct blob_attr *tb;
566 blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
569 return UBUS_STATUS_INVALID_ARGUMENT;
571 ses = uh_ubus_session_get(blobmsg_data(tb));
573 return UBUS_STATUS_NOT_FOUND;
575 uh_ubus_session_destroy(ses);
580 static char *split_str(char *str)
583 str = strchr(str, '/');
585 while (str && *str == '/') {
593 uh_ubus_request_parse_url(struct client *cl, char *url, char **sid, char **obj, char **fun)
595 url += strlen(conf.ubus_prefix);
596 while (url && *url == '/')
601 url = split_str(url);
604 url = split_str(url);
607 return *sid && *obj && *fun;
611 uh_ubus_request_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
613 struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
614 struct client *cl = container_of(du, struct client, dispatch.ubus);
617 if (!du->header_sent) {
618 ops->http_header(cl, 200, "OK");
619 ustream_printf(cl->us, "Content-Type: application/json\r\n\r\n");
620 du->header_sent = true;
623 str = blobmsg_format_json_indent(msg, true, 0);
624 ops->chunk_write(cl, str, strlen(str));
629 uh_ubus_request_cb(struct ubus_request *req, int ret)
631 struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
632 struct client *cl = container_of(du, struct client, dispatch.ubus);
634 if (!du->header_sent)
635 return ops->client_error(cl, 204, "No content", "Function did not return data");
637 ops->request_done(cl);
640 static void uh_ubus_close_fds(struct client *cl)
642 if (ctx->sock.fd < 0)
649 static void uh_ubus_request_free(struct client *cl)
651 struct dispatch_ubus *du = &cl->dispatch.ubus;
654 json_object_put(du->jsobj);
657 json_tokener_free(du->jstok);
660 ubus_abort_request(ctx, &du->req);
663 static void uh_ubus_json_error(struct client *cl)
665 ops->client_error(cl, 400, "Bad Request", "Invalid JSON data");
668 static void uh_ubus_send_request(struct client *cl, json_object *obj)
670 struct dispatch *d = &cl->dispatch;
671 struct dispatch_ubus *du = &d->ubus;
674 blob_buf_init(&buf, 0);
676 if (obj && !blobmsg_add_object(&buf, obj))
677 return uh_ubus_json_error(cl);
679 ret = ubus_invoke_async(ctx, du->obj, du->func, buf.head, &du->req);
681 return ops->client_error(cl, 500, "Internal Error",
682 "Error sending ubus request: %s", ubus_strerror(ret));
684 du->req.data_cb = uh_ubus_request_data_cb;
685 du->req.complete_cb = uh_ubus_request_cb;
686 ubus_complete_request_async(ctx, &du->req);
688 du->req_pending = true;
691 static void uh_ubus_data_done(struct client *cl)
693 struct dispatch_ubus *du = &cl->dispatch.ubus;
694 struct json_object *obj = du->jsobj;
696 if (!obj || json_object_get_type(obj) != json_type_object)
697 return uh_ubus_json_error(cl);
699 uh_ubus_send_request(cl, obj);
702 static int uh_ubus_data_send(struct client *cl, const char *data, int len)
704 struct dispatch_ubus *du = &cl->dispatch.ubus;
707 uh_ubus_json_error(cl);
712 if (du->post_len > UH_UBUS_MAX_POST_SIZE) {
713 ops->client_error(cl, 413, "Too Large", "Message too big");
717 du->jsobj = json_tokener_parse_ex(du->jstok, data, len);
721 static void uh_ubus_defer_post(struct client *cl)
723 struct dispatch *d = &cl->dispatch;
725 d->ubus.jstok = json_tokener_new();
727 return ops->client_error(cl, 500, "Internal Error", "Internal Error");
729 d->data_send = uh_ubus_data_send;
730 d->data_done = uh_ubus_data_done;
733 static void uh_ubus_handle_request(struct client *cl, char *url, struct path_info *pi)
735 struct uh_ubus_session_acl *acl;
736 struct uh_ubus_session *ses;
737 struct dispatch *d = &cl->dispatch;
738 char *sid, *obj, *fun;
741 blob_buf_init(&buf, 0);
743 if (!uh_ubus_request_parse_url(cl, url, &sid, &obj, &fun))
744 return ops->client_error(cl, 400, "Bad Request", "Invalid Request");
746 ses = uh_ubus_session_get(sid);
748 return ops->client_error(cl, 404, "Not Found", "No such session %s", sid);
750 uh_foreach_matching_acl(acl, ses, obj, fun) {
756 return ops->client_error(cl, 403, "Denied", "Access to object denied");
758 if (ubus_lookup_id(ctx, obj, &d->ubus.obj))
759 return ops->client_error(cl, 500, "Not Found", "No such object");
761 d->close_fds = uh_ubus_close_fds;
762 d->free = uh_ubus_request_free;
765 if (cl->request.method == UH_HTTP_MSG_POST)
766 uh_ubus_defer_post(cl);
768 uh_ubus_send_request(cl, NULL);
772 uh_ubus_check_url(const char *url)
774 return ops->path_match(conf.ubus_prefix, url);
780 static struct dispatch_handler ubus_dispatch = {
781 .check_url = uh_ubus_check_url,
782 .handle_request = uh_ubus_handle_request,
785 static const struct ubus_method session_methods[] = {
786 UBUS_METHOD("create", uh_ubus_handle_create, &new_policy),
787 UBUS_METHOD("list", uh_ubus_handle_list, &sid_policy),
788 UBUS_METHOD("grant", uh_ubus_handle_acl, acl_policy),
789 UBUS_METHOD("revoke", uh_ubus_handle_acl, acl_policy),
790 UBUS_METHOD("set", uh_ubus_handle_set, set_policy),
791 UBUS_METHOD("get", uh_ubus_handle_get, get_policy),
792 UBUS_METHOD("unset", uh_ubus_handle_unset, get_policy),
793 UBUS_METHOD("destroy", uh_ubus_handle_destroy, &sid_policy),
796 static struct ubus_object_type session_type =
797 UBUS_OBJECT_TYPE("uhttpd", session_methods);
799 static struct ubus_object obj = {
801 .type = &session_type,
802 .methods = session_methods,
803 .n_methods = ARRAY_SIZE(session_methods),
808 ctx = ubus_connect(conf.ubus_socket);
810 fprintf(stderr, "Unable to connect to ubus socket\n");
814 ret = ubus_add_object(ctx, &obj);
816 fprintf(stderr, "Unable to publish ubus object: %s\n",
821 avl_init(&sessions, avl_strcmp, false, NULL);
822 ops->dispatch_add(&ubus_dispatch);
829 static int uh_ubus_plugin_init(const struct uhttpd_ops *o, struct config *c)
833 return uh_ubus_init();
836 static void uh_ubus_post_init(void)
841 const struct uhttpd_plugin uhttpd_plugin = {
842 .init = uh_ubus_plugin_init,
843 .post_init = uh_ubus_post_init,