2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
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.
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.
20 #include <libubox/blobmsg.h>
21 #include <libubox/blobmsg_json.h>
22 #include <libubox/avl.h>
23 #include <libubox/avl-cmp.h>
29 #include "ubus-session.h"
31 static const struct uhttpd_ops
*ops
;
32 static struct config
*_conf
;
35 static struct ubus_context
*ctx
;
36 static struct blob_buf buf
;
38 #define UH_UBUS_MAX_POST_SIZE 4096
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
},
60 struct blob_attr
*data
;
79 } json_errors
[__ERROR_MAX
] = {
80 [ERROR_PARSE
] = { -32700, "Parse error" },
81 [ERROR_REQUEST
] = { -32600, "Invalid request" },
82 [ERROR_METHOD
] = { -32601, "Method not found" },
83 [ERROR_PARAMS
] = { -32602, "Invalid parameters" },
84 [ERROR_INTERNAL
] = { -32603, "Internal error" },
85 [ERROR_OBJECT
] = { -32000, "Object not found" },
86 [ERROR_SESSION
] = { -32001, "Session not found" },
87 [ERROR_ACCESS
] = { -32002, "Access denied" },
88 [ERROR_TIMEOUT
] = { -32003, "ubus request timed out" },
91 static void __uh_ubus_next_batched_request(struct uloop_timeout
*timeout
);
93 static void uh_ubus_next_batched_request(struct client
*cl
)
95 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
97 du
->timeout
.cb
= __uh_ubus_next_batched_request
;
98 uloop_timeout_set(&du
->timeout
, 1);
101 static void uh_ubus_send_header(struct client
*cl
)
103 ops
->http_header(cl
, 200, "OK");
104 ustream_printf(cl
->us
, "Content-Type: application/json\r\n\r\n");
107 static void uh_ubus_send_response(struct client
*cl
)
109 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
110 const char *sep
= "";
113 if (du
->array
&& du
->array_idx
> 1)
116 str
= blobmsg_format_json_indent(buf
.head
, true, du
->array
);
117 ops
->chunk_printf(cl
, "%s%s", sep
, str
);
120 du
->jsobj_cur
= NULL
;
122 uh_ubus_next_batched_request(cl
);
124 ops
->chunk_printf(cl
, "\n");
125 return ops
->request_done(cl
);
129 static void uh_ubus_init_response(struct client
*cl
)
131 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
132 struct json_object
*obj
= du
->jsobj_cur
;
134 blob_buf_init(&buf
, 0);
135 blobmsg_add_string(&buf
, "jsonrpc", "2.0");
138 obj
= json_object_object_get(obj
, "id");
141 blobmsg_add_json_element(&buf
, "id", obj
);
143 blobmsg_add_field(&buf
, BLOBMSG_TYPE_UNSPEC
, "id", NULL
, 0);
146 static void uh_ubus_json_error(struct client
*cl
, enum rpc_error type
)
150 uh_ubus_init_response(cl
);
151 c
= blobmsg_open_table(&buf
, "error");
152 blobmsg_add_u32(&buf
, "code", json_errors
[type
].code
);
153 blobmsg_add_string(&buf
, "message", json_errors
[type
].msg
);
154 blobmsg_close_table(&buf
, c
);
155 uh_ubus_send_response(cl
);
159 uh_ubus_request_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
161 struct dispatch_ubus
*du
= container_of(req
, struct dispatch_ubus
, req
);
163 blobmsg_add_field(&du
->buf
, BLOBMSG_TYPE_TABLE
, "", blob_data(msg
), blob_len(msg
));
167 uh_ubus_request_cb(struct ubus_request
*req
, int ret
)
169 struct dispatch_ubus
*du
= container_of(req
, struct dispatch_ubus
, req
);
170 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
171 struct blob_attr
*cur
;
175 uloop_timeout_cancel(&du
->timeout
);
176 uh_ubus_init_response(cl
);
177 r
= blobmsg_open_array(&buf
, "result");
178 blobmsg_add_u32(&buf
, "", ret
);
179 blob_for_each_attr(cur
, du
->buf
.head
, rem
)
180 blobmsg_add_blob(&buf
, cur
);
181 blobmsg_close_array(&buf
, r
);
182 uh_ubus_send_response(cl
);
186 uh_ubus_timeout_cb(struct uloop_timeout
*timeout
)
188 struct dispatch_ubus
*du
= container_of(timeout
, struct dispatch_ubus
, timeout
);
189 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
191 ubus_abort_request(ctx
, &du
->req
);
192 uh_ubus_json_error(cl
, ERROR_TIMEOUT
);
195 static void uh_ubus_close_fds(struct client
*cl
)
197 if (ctx
->sock
.fd
< 0)
204 static void uh_ubus_request_free(struct client
*cl
)
206 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
208 blob_buf_free(&du
->buf
);
209 uloop_timeout_cancel(&cl
->timeout
);
212 json_object_put(du
->jsobj
);
215 json_tokener_free(du
->jstok
);
218 ubus_abort_request(ctx
, &du
->req
);
221 static void uh_ubus_single_error(struct client
*cl
, enum rpc_error type
)
223 uh_ubus_send_header(cl
);
224 uh_ubus_json_error(cl
, type
);
225 ops
->request_done(cl
);
228 static void uh_ubus_send_request(struct client
*cl
, json_object
*obj
)
230 struct dispatch
*d
= &cl
->dispatch
;
231 struct dispatch_ubus
*du
= &d
->ubus
;
234 blob_buf_init(&du
->buf
, 0);
235 memset(&du
->req
, 0, sizeof(du
->req
));
236 ret
= ubus_invoke_async(ctx
, du
->obj
, du
->func
, buf
.head
, &du
->req
);
238 return uh_ubus_json_error(cl
, ERROR_INTERNAL
);
240 du
->req
.data_cb
= uh_ubus_request_data_cb
;
241 du
->req
.complete_cb
= uh_ubus_request_cb
;
242 ubus_complete_request_async(ctx
, &du
->req
);
244 du
->timeout
.cb
= uh_ubus_timeout_cb
;
245 uloop_timeout_set(&du
->timeout
, conf
.script_timeout
);
247 du
->req_pending
= true;
250 static bool parse_json_rpc(struct rpc_data
*d
, struct blob_attr
*data
)
252 const struct blobmsg_policy data_policy
[] = {
253 { .type
= BLOBMSG_TYPE_STRING
},
254 { .type
= BLOBMSG_TYPE_STRING
},
255 { .type
= BLOBMSG_TYPE_TABLE
},
257 struct blob_attr
*tb
[__RPC_MAX
];
258 struct blob_attr
*tb2
[3];
259 struct blob_attr
*cur
;
261 blobmsg_parse(rpc_policy
, __RPC_MAX
, tb
, blob_data(data
), blob_len(data
));
263 cur
= tb
[RPC_JSONRPC
];
264 if (!cur
|| strcmp(blobmsg_data(cur
), "2.0") != 0)
267 cur
= tb
[RPC_METHOD
];
272 d
->method
= blobmsg_data(cur
);
274 cur
= tb
[RPC_PARAMS
];
278 blobmsg_parse_array(data_policy
, ARRAY_SIZE(data_policy
), tb2
,
279 blobmsg_data(cur
), blobmsg_data_len(cur
));
281 if (!tb2
[0] || !tb2
[1] || !tb2
[2])
284 d
->object
= blobmsg_data(tb2
[0]);
285 d
->function
= blobmsg_data(tb2
[1]);
290 static void uh_ubus_init_batch(struct client
*cl
)
292 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
295 uh_ubus_send_header(cl
);
296 ops
->chunk_printf(cl
, "[\n\t");
299 static void uh_ubus_complete_batch(struct client
*cl
)
301 ops
->chunk_printf(cl
, "\n]\n");
302 ops
->request_done(cl
);
305 static void uh_ubus_handle_request_object(struct client
*cl
, struct json_object
*obj
)
307 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
308 struct uh_ubus_session
*ses
;
309 struct rpc_data data
= {};
310 enum rpc_error err
= ERROR_PARSE
;
312 if (json_object_get_type(obj
) != json_type_object
)
316 blob_buf_init(&buf
, 0);
317 if (!blobmsg_add_object(&buf
, obj
))
320 if (!parse_json_rpc(&data
, buf
.head
))
323 if (strcmp(data
.method
, "call") != 0) {
328 ses
= uh_ubus_session_get(du
->sid
);
334 if (!uh_ubus_session_acl_allowed(ses
, data
.object
, data
.function
)) {
339 du
->func
= data
.function
;
340 if (ubus_lookup_id(ctx
, data
.object
, &du
->obj
)) {
345 uh_ubus_send_request(cl
, obj
);
349 uh_ubus_json_error(cl
, err
);
352 static void __uh_ubus_next_batched_request(struct uloop_timeout
*timeout
)
354 struct dispatch_ubus
*du
= container_of(timeout
, struct dispatch_ubus
, timeout
);
355 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
356 struct json_object
*obj
= du
->jsobj
;
359 len
= json_object_array_length(obj
);
360 if (du
->array_idx
>= len
)
361 return uh_ubus_complete_batch(cl
);
363 obj
= json_object_array_get_idx(obj
, du
->array_idx
++);
364 uh_ubus_handle_request_object(cl
, obj
);
367 static void uh_ubus_data_done(struct client
*cl
)
369 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
370 struct json_object
*obj
= du
->jsobj
;
372 switch (obj
? json_object_get_type(obj
) : json_type_null
) {
373 case json_type_object
:
374 uh_ubus_send_header(cl
);
375 return uh_ubus_handle_request_object(cl
, obj
);
376 case json_type_array
:
377 uh_ubus_init_batch(cl
);
378 if (json_object_array_length(obj
) > 0)
379 return uh_ubus_next_batched_request(cl
);
382 return uh_ubus_single_error(cl
, ERROR_PARSE
);
386 static int uh_ubus_data_send(struct client
*cl
, const char *data
, int len
)
388 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
390 if (du
->jsobj
|| !du
->jstok
)
394 if (du
->post_len
> UH_UBUS_MAX_POST_SIZE
)
397 du
->jsobj
= json_tokener_parse_ex(du
->jstok
, data
, len
);
401 uh_ubus_single_error(cl
, ERROR_PARSE
);
405 static void uh_ubus_handle_request(struct client
*cl
, char *url
, struct path_info
*pi
)
407 struct dispatch
*d
= &cl
->dispatch
;
410 blob_buf_init(&buf
, 0);
412 url
+= strlen(conf
.ubus_prefix
);
416 sep
= strchr(url
, '/');
421 if (strlen(sid
) != 32 ||
422 cl
->request
.method
!= UH_HTTP_MSG_POST
)
423 return ops
->client_error(cl
, 400, "Bad Request", "Invalid Request");
425 d
->close_fds
= uh_ubus_close_fds
;
426 d
->free
= uh_ubus_request_free
;
427 d
->data_send
= uh_ubus_data_send
;
428 d
->data_done
= uh_ubus_data_done
;
429 d
->ubus
.jstok
= json_tokener_new();
434 uh_ubus_check_url(const char *url
)
436 return ops
->path_match(conf
.ubus_prefix
, url
);
442 static struct dispatch_handler ubus_dispatch
= {
443 .check_url
= uh_ubus_check_url
,
444 .handle_request
= uh_ubus_handle_request
,
447 ctx
= ubus_connect(conf
.ubus_socket
);
449 fprintf(stderr
, "Unable to connect to ubus socket\n");
453 ops
->dispatch_add(&ubus_dispatch
);
454 if (ubus_session_api_init(ctx
)) {
455 fprintf(stderr
, "Unable to initialize ubus session API\n");
464 static int uh_ubus_plugin_init(const struct uhttpd_ops
*o
, struct config
*c
)
468 return uh_ubus_init();
471 static void uh_ubus_post_init(void)
476 const struct uhttpd_plugin uhttpd_plugin
= {
477 .init
= uh_ubus_plugin_init
,
478 .post_init
= uh_ubus_post_init
,