X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=ubus.c;h=bc8c92cb9fdc16cbecaedf9a4a94a164793ba186;hb=d5710cb4e1ea104915023781da841973cdf37de3;hp=523027058371cc2c432d9a099160dc7fdf832b30;hpb=1eedd103be4919683f2d0b2165cefe82b73e4a5f;p=project%2Fuhttpd.git diff --git a/ubus.c b/ubus.c index 5230270..bc8c92c 100644 --- a/ubus.c +++ b/ubus.c @@ -1,23 +1,22 @@ /* * uhttpd - Tiny single-threaded httpd * - * Copyright (C) 2010-2012 Jo-Philipp Wich - * Copyright (C) 2012 Felix Fietkau + * Copyright (C) 2010-2013 Jo-Philipp Wich + * Copyright (C) 2013 Felix Fietkau * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ - #include #include #include @@ -27,7 +26,6 @@ #include "uhttpd.h" #include "plugin.h" -#include "ubus-session.h" static const struct uhttpd_ops *ops; static struct config *_conf; @@ -37,6 +35,7 @@ static struct ubus_context *ctx; static struct blob_buf buf; #define UH_UBUS_MAX_POST_SIZE 4096 +#define UH_UBUS_DEFAULT_SID "00000000000000000000000000000000" enum { RPC_JSONRPC, @@ -53,12 +52,22 @@ static const struct blobmsg_policy rpc_policy[__RPC_MAX] = { [RPC_ID] = { .name = "id", .type = BLOBMSG_TYPE_UNSPEC }, }; +enum { + SES_ACCESS, + __SES_MAX, +}; + +static const struct blobmsg_policy ses_policy[__SES_MAX] = { + [SES_ACCESS] = { .name = "access", .type = BLOBMSG_TYPE_BOOL }, +}; + struct rpc_data { struct blob_attr *id; const char *method; const char *object; const char *function; struct blob_attr *data; + struct blob_attr *params; }; enum rpc_error { @@ -207,7 +216,7 @@ static void uh_ubus_request_free(struct client *cl) struct dispatch_ubus *du = &cl->dispatch.ubus; blob_buf_free(&du->buf); - uloop_timeout_cancel(&cl->timeout); + uloop_timeout_cancel(&du->timeout); if (du->jsobj) json_object_put(du->jsobj); @@ -226,15 +235,21 @@ static void uh_ubus_single_error(struct client *cl, enum rpc_error type) ops->request_done(cl); } -static void uh_ubus_send_request(struct client *cl, json_object *obj) +static void uh_ubus_send_request(struct client *cl, json_object *obj, struct blob_attr *args) { struct dispatch *d = &cl->dispatch; struct dispatch_ubus *du = &d->ubus; - int ret; + struct blob_attr *cur; + static struct blob_buf req; + int ret, rem; + + blob_buf_init(&req, 0); + blobmsg_for_each_attr(cur, args, rem) + blobmsg_add_blob(&req, cur); blob_buf_init(&du->buf, 0); memset(&du->req, 0, sizeof(du->req)); - ret = ubus_invoke_async(ctx, du->obj, du->func, buf.head, &du->req); + ret = ubus_invoke_async(ctx, du->obj, du->func, req.head, &du->req); if (ret) return uh_ubus_json_error(cl, ERROR_INTERNAL); @@ -243,11 +258,88 @@ static void uh_ubus_send_request(struct client *cl, json_object *obj) ubus_complete_request_async(ctx, &du->req); du->timeout.cb = uh_ubus_timeout_cb; - uloop_timeout_set(&du->timeout, conf.script_timeout); + uloop_timeout_set(&du->timeout, conf.script_timeout * 1000); du->req_pending = true; } +static void uh_ubus_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv) +{ + struct blob_attr *sig, *attr; + int rem, rem2; + void *t, *o; + + if (!priv) { + blobmsg_add_string(&buf, NULL, obj->path); + return; + } + + if (!obj->signature) + return; + + o = blobmsg_open_table(&buf, obj->path); + blob_for_each_attr(sig, obj->signature, rem) { + t = blobmsg_open_table(&buf, blobmsg_name(sig)); + rem2 = blobmsg_data_len(sig); + __blob_for_each_attr(attr, blobmsg_data(sig), rem2) { + if (blob_id(attr) != BLOBMSG_TYPE_INT32) + continue; + + switch (blobmsg_get_u32(attr)) { + case BLOBMSG_TYPE_INT8: + blobmsg_add_string(&buf, blobmsg_name(attr), "boolean"); + break; + case BLOBMSG_TYPE_INT32: + blobmsg_add_string(&buf, blobmsg_name(attr), "number"); + break; + case BLOBMSG_TYPE_STRING: + blobmsg_add_string(&buf, blobmsg_name(attr), "string"); + break; + case BLOBMSG_TYPE_ARRAY: + blobmsg_add_string(&buf, blobmsg_name(attr), "array"); + break; + case BLOBMSG_TYPE_TABLE: + blobmsg_add_string(&buf, blobmsg_name(attr), "object"); + break; + default: + blobmsg_add_string(&buf, blobmsg_name(attr), "unknown"); + break; + } + } + blobmsg_close_table(&buf, t); + } + blobmsg_close_table(&buf, o); +} + +static void uh_ubus_send_list(struct client *cl, json_object *obj, struct blob_attr *params) +{ + struct blob_attr *cur, *dup; + void *r; + int rem; + + uh_ubus_init_response(cl); + + if (!params || blob_id(params) != BLOBMSG_TYPE_ARRAY) { + r = blobmsg_open_array(&buf, "result"); + ubus_lookup(ctx, NULL, uh_ubus_list_cb, NULL); + blobmsg_close_array(&buf, r); + } + else { + r = blobmsg_open_table(&buf, "result"); + dup = blob_memdup(params); + if (dup) + { + rem = blobmsg_data_len(dup); + __blob_for_each_attr(cur, blobmsg_data(dup), rem) + ubus_lookup(ctx, blobmsg_data(cur), uh_ubus_list_cb, blobmsg_data(cur)); + free(dup); + } + blobmsg_close_table(&buf, r); + } + + uh_ubus_send_response(cl); +} + static bool parse_json_rpc(struct rpc_data *d, struct blob_attr *data) { const struct blobmsg_policy data_policy[] = { @@ -274,17 +366,23 @@ static bool parse_json_rpc(struct rpc_data *d, struct blob_attr *data) cur = tb[RPC_PARAMS]; if (!cur) + return true; + + d->params = blob_memdup(cur); + if (!d->params) return false; blobmsg_parse_array(data_policy, ARRAY_SIZE(data_policy), tb2, - blobmsg_data(cur), blobmsg_data_len(cur)); + blobmsg_data(d->params), blobmsg_data_len(d->params)); - if (!tb2[0] || !tb2[1] || !tb2[2]) - return false; + if (tb2[0]) + d->object = blobmsg_data(tb2[0]); + + if (tb2[1]) + d->function = blobmsg_data(tb2[1]); - d->object = blobmsg_data(tb2[0]); - d->function = blobmsg_data(tb2[1]); d->data = tb2[2]; + return true; } @@ -303,10 +401,42 @@ static void uh_ubus_complete_batch(struct client *cl) ops->request_done(cl); } +static void uh_ubus_allowed_cb(struct ubus_request *req, int type, struct blob_attr *msg) +{ + struct blob_attr *tb[__SES_MAX]; + bool *allow = (bool *)req->priv; + + if (!msg) + return; + + blobmsg_parse(ses_policy, __SES_MAX, tb, blob_data(msg), blob_len(msg)); + + if (tb[SES_ACCESS]) + *allow = blobmsg_get_bool(tb[SES_ACCESS]); +} + +static bool uh_ubus_allowed(const char *sid, const char *obj, const char *fun) +{ + uint32_t id; + bool allow = false; + static struct blob_buf req; + + if (ubus_lookup_id(ctx, "session", &id)) + return false; + + blob_buf_init(&req, 0); + blobmsg_add_string(&req, "sid", sid); + blobmsg_add_string(&req, "object", obj); + blobmsg_add_string(&req, "function", fun); + + ubus_invoke(ctx, id, "access", req.head, uh_ubus_allowed_cb, &allow, conf.script_timeout * 500); + + return allow; +} + static void uh_ubus_handle_request_object(struct client *cl, struct json_object *obj) { struct dispatch_ubus *du = &cl->dispatch.ubus; - struct uh_ubus_session *ses; struct rpc_data data = {}; enum rpc_error err = ERROR_PARSE; @@ -321,33 +451,38 @@ static void uh_ubus_handle_request_object(struct client *cl, struct json_object if (!parse_json_rpc(&data, buf.head)) goto error; - if (strcmp(data.method, "call") != 0) { - err = ERROR_METHOD; - goto error; - } + if (!strcmp(data.method, "call")) { + if (!data.object || !data.function || !data.data) + goto error; - ses = uh_ubus_session_get(du->sid); - if (!ses) { - err = ERROR_SESSION; - goto error; - } + du->func = data.function; + if (ubus_lookup_id(ctx, data.object, &du->obj)) { + err = ERROR_OBJECT; + goto error; + } - if (!uh_ubus_session_acl_allowed(ses, data.object, data.function)) { - err = ERROR_ACCESS; - goto error; - } + if (!conf.ubus_noauth && !uh_ubus_allowed(du->sid, data.object, data.function)) { + err = ERROR_ACCESS; + goto error; + } - du->func = data.function; - if (ubus_lookup_id(ctx, data.object, &du->obj)) { - err = ERROR_OBJECT; + uh_ubus_send_request(cl, obj, data.data); + goto out; + } + else if (!strcmp(data.method, "list")) { + uh_ubus_send_list(cl, obj, data.params); + goto out; + } + else { + err = ERROR_METHOD; goto error; } - uh_ubus_send_request(cl, obj); - return; - error: uh_ubus_json_error(cl, err); +out: + if (data.params) + free(data.params); } static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout) @@ -376,9 +511,7 @@ static void uh_ubus_data_done(struct client *cl) return uh_ubus_handle_request_object(cl, obj); case json_type_array: uh_ubus_init_batch(cl); - if (json_object_array_length(obj) > 0) - return uh_ubus_next_batched_request(cl); - /* fall through */ + return uh_ubus_next_batched_request(cl); default: return uh_ubus_single_error(cl, ERROR_PARSE); } @@ -410,15 +543,21 @@ static void uh_ubus_handle_request(struct client *cl, char *url, struct path_inf blob_buf_init(&buf, 0); - url += strlen(conf.ubus_prefix); - while (*url == '/') - url++; + if (conf.ubus_noauth) { + sid = UH_UBUS_DEFAULT_SID; + } + else { + url += strlen(conf.ubus_prefix); + while (*url == '/') + url++; + + sep = strchr(url, '/'); + if (sep) + *sep = 0; - sep = strchr(url, '/'); - if (sep) - *sep = 0; + sid = url; + } - sid = url; if (strlen(sid) != 32 || cl->request.method != UH_HTTP_MSG_POST) return ops->client_error(cl, 400, "Bad Request", "Invalid Request"); @@ -452,10 +591,6 @@ uh_ubus_init(void) } ops->dispatch_add(&ubus_dispatch); - if (ubus_session_api_init(ctx)) { - fprintf(stderr, "Unable to initialize ubus session API\n"); - exit(1); - } uloop_done(); return 0;