X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuhttpd.git;a=blobdiff_plain;f=ubus.c;h=da1ab606762871222768ac768812660cb54bf831;hp=3a21fc7ca64edafd890fcc66fcc6e9e8405e0ea1;hb=cd66639800ee2882a0867ec54868502eb9b893d8;hpb=2787be7a7d916e6368ab3492f7a942cb2a4464b3 diff --git a/ubus.c b/ubus.c index 3a21fc7..da1ab60 100644 --- a/ubus.c +++ b/ubus.c @@ -35,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, @@ -62,10 +63,17 @@ static const struct blobmsg_policy ses_policy[__SES_MAX] = { struct rpc_data { struct blob_attr *id; + const char *sid; const char *method; const char *object; const char *function; struct blob_attr *data; + struct blob_attr *params; +}; + +struct list_data { + bool verbose; + struct blob_buf *buf; }; enum rpc_error { @@ -214,7 +222,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); @@ -233,7 +241,7 @@ 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, struct blob_attr *args) +static void uh_ubus_send_request(struct client *cl, json_object *obj, const char *sid, struct blob_attr *args) { struct dispatch *d = &cl->dispatch; struct dispatch_ubus *du = &d->ubus; @@ -242,8 +250,13 @@ static void uh_ubus_send_request(struct client *cl, json_object *obj, struct blo int ret, rem; blob_buf_init(&req, 0); - blobmsg_for_each_attr(cur, args, rem) + blobmsg_for_each_attr(cur, args, rem) { + if (!strcmp(blobmsg_name(cur), "ubus_rpc_session")) + return uh_ubus_json_error(cl, ERROR_PARAMS); blobmsg_add_blob(&req, cur); + } + + blobmsg_add_string(&req, "ubus_rpc_session", sid); blob_buf_init(&du->buf, 0); memset(&du->req, 0, sizeof(du->req)); @@ -256,20 +269,107 @@ static void uh_ubus_send_request(struct client *cl, json_object *obj, struct blo 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; + struct list_data *data = priv; + int rem, rem2; + void *t, *o; + + if (!data->verbose) { + blobmsg_add_string(data->buf, NULL, obj->path); + return; + } + + if (!obj->signature) + return; + + o = blobmsg_open_table(data->buf, obj->path); + blob_for_each_attr(sig, obj->signature, rem) { + t = blobmsg_open_table(data->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(data->buf, blobmsg_name(attr), "boolean"); + break; + case BLOBMSG_TYPE_INT32: + blobmsg_add_string(data->buf, blobmsg_name(attr), "number"); + break; + case BLOBMSG_TYPE_STRING: + blobmsg_add_string(data->buf, blobmsg_name(attr), "string"); + break; + case BLOBMSG_TYPE_ARRAY: + blobmsg_add_string(data->buf, blobmsg_name(attr), "array"); + break; + case BLOBMSG_TYPE_TABLE: + blobmsg_add_string(data->buf, blobmsg_name(attr), "object"); + break; + default: + blobmsg_add_string(data->buf, blobmsg_name(attr), "unknown"); + break; + } + } + blobmsg_close_table(data->buf, t); + } + blobmsg_close_table(data->buf, o); +} + +static void uh_ubus_send_list(struct client *cl, json_object *obj, struct blob_attr *params) +{ + struct blob_attr *cur, *dup; + struct list_data data = { .buf = &cl->dispatch.ubus.buf, .verbose = false }; + void *r; + int rem; + + blob_buf_init(data.buf, 0); + + uh_client_ref(cl); + + if (!params || blob_id(params) != BLOBMSG_TYPE_ARRAY) { + r = blobmsg_open_array(data.buf, "result"); + ubus_lookup(ctx, NULL, uh_ubus_list_cb, &data); + blobmsg_close_array(data.buf, r); + } + else { + r = blobmsg_open_table(data.buf, "result"); + dup = blob_memdup(params); + if (dup) + { + rem = blobmsg_data_len(dup); + data.verbose = true; + __blob_for_each_attr(cur, blobmsg_data(dup), rem) + ubus_lookup(ctx, blobmsg_data(cur), uh_ubus_list_cb, &data); + free(dup); + } + blobmsg_close_table(data.buf, r); + } + + uh_client_unref(cl); + + uh_ubus_init_response(cl); + blobmsg_add_blob(&buf, blob_data(data.buf->head)); + uh_ubus_send_response(cl); +} + static bool parse_json_rpc(struct rpc_data *d, struct blob_attr *data) { const struct blobmsg_policy data_policy[] = { { .type = BLOBMSG_TYPE_STRING }, { .type = BLOBMSG_TYPE_STRING }, + { .type = BLOBMSG_TYPE_STRING }, { .type = BLOBMSG_TYPE_TABLE }, }; struct blob_attr *tb[__RPC_MAX]; - struct blob_attr *tb2[3]; + struct blob_attr *tb2[4]; struct blob_attr *cur; blobmsg_parse(rpc_policy, __RPC_MAX, tb, blob_data(data), blob_len(data)); @@ -287,17 +387,29 @@ 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->sid = blobmsg_data(tb2[0]); + + if (conf.ubus_noauth && (!d->sid || !*d->sid)) + d->sid = UH_UBUS_DEFAULT_SID; + + if (tb2[1]) + d->object = blobmsg_data(tb2[1]); + + if (tb2[2]) + d->function = blobmsg_data(tb2[2]); + + d->data = tb2[3]; - d->object = blobmsg_data(tb2[0]); - d->function = blobmsg_data(tb2[1]); - d->data = tb2[2]; return true; } @@ -340,11 +452,11 @@ static bool uh_ubus_allowed(const char *sid, const char *obj, const char *fun) return false; blob_buf_init(&req, 0); - blobmsg_add_string(&req, "sid", sid); + blobmsg_add_string(&req, "ubus_rpc_session", 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, 250); + ubus_invoke(ctx, id, "access", req.head, uh_ubus_allowed_cb, &allow, conf.script_timeout * 500); return allow; } @@ -355,6 +467,8 @@ static void uh_ubus_handle_request_object(struct client *cl, struct json_object struct rpc_data data = {}; enum rpc_error err = ERROR_PARSE; + uh_client_ref(cl); + if (json_object_get_type(obj) != json_type_object) goto error; @@ -366,27 +480,40 @@ 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.sid || !data.object || !data.function || !data.data) + goto error; - du->func = data.function; - if (ubus_lookup_id(ctx, data.object, &du->obj)) { - err = ERROR_OBJECT; - goto error; - } + du->func = data.function; + if (ubus_lookup_id(ctx, data.object, &du->obj)) { + err = ERROR_OBJECT; + goto error; + } - if (!conf.ubus_noauth && !uh_ubus_allowed(du->sid, data.object, data.function)) { - err = ERROR_ACCESS; + if (!conf.ubus_noauth && !uh_ubus_allowed(data.sid, data.object, data.function)) { + err = ERROR_ACCESS; + goto error; + } + + uh_ubus_send_request(cl, obj, data.sid, 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, data.data); - return; - error: uh_ubus_json_error(cl, err); +out: + if (data.params) + free(data.params); + + uh_client_unref(cl); } static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout) @@ -415,9 +542,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); } @@ -445,21 +570,10 @@ error: static void uh_ubus_handle_request(struct client *cl, char *url, struct path_info *pi) { struct dispatch *d = &cl->dispatch; - char *sid, *sep; blob_buf_init(&buf, 0); - url += strlen(conf.ubus_prefix); - while (*url == '/') - url++; - - sep = strchr(url, '/'); - if (sep) - *sep = 0; - - sid = url; - if (strlen(sid) != 32 || - cl->request.method != UH_HTTP_MSG_POST) + if (cl->request.method != UH_HTTP_MSG_POST) return ops->client_error(cl, 400, "Bad Request", "Invalid Request"); d->close_fds = uh_ubus_close_fds; @@ -467,7 +581,6 @@ static void uh_ubus_handle_request(struct client *cl, char *url, struct path_inf d->data_send = uh_ubus_data_send; d->data_done = uh_ubus_data_done; d->ubus.jstok = json_tokener_new(); - d->ubus.sid = sid; } static bool