ubus.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. #include <libubox/blobmsg.h>
  20. #include <libubox/blobmsg_json.h>
  21. #include <libubox/avl.h>
  22. #include <libubox/avl-cmp.h>
  23. #include <stdio.h>
  24. #include <poll.h>
  25. #include "uhttpd.h"
  26. #include "plugin.h"
  27. static const struct uhttpd_ops *ops;
  28. static struct config *_conf;
  29. #define conf (*_conf)
  30. static struct ubus_context *ctx;
  31. static struct blob_buf buf;
  32. #define UH_UBUS_MAX_POST_SIZE 65536
  33. #define UH_UBUS_DEFAULT_SID "00000000000000000000000000000000"
  34. enum {
  35. RPC_JSONRPC,
  36. RPC_METHOD,
  37. RPC_PARAMS,
  38. RPC_ID,
  39. __RPC_MAX,
  40. };
  41. static const struct blobmsg_policy rpc_policy[__RPC_MAX] = {
  42. [RPC_JSONRPC] = { .name = "jsonrpc", .type = BLOBMSG_TYPE_STRING },
  43. [RPC_METHOD] = { .name = "method", .type = BLOBMSG_TYPE_STRING },
  44. [RPC_PARAMS] = { .name = "params", .type = BLOBMSG_TYPE_UNSPEC },
  45. [RPC_ID] = { .name = "id", .type = BLOBMSG_TYPE_UNSPEC },
  46. };
  47. enum {
  48. SES_ACCESS,
  49. __SES_MAX,
  50. };
  51. static const struct blobmsg_policy ses_policy[__SES_MAX] = {
  52. [SES_ACCESS] = { .name = "access", .type = BLOBMSG_TYPE_BOOL },
  53. };
  54. struct rpc_data {
  55. struct blob_attr *id;
  56. const char *sid;
  57. const char *method;
  58. const char *object;
  59. const char *function;
  60. struct blob_attr *data;
  61. struct blob_attr *params;
  62. };
  63. struct list_data {
  64. bool verbose;
  65. bool add_object;
  66. struct blob_buf *buf;
  67. };
  68. enum rpc_error {
  69. ERROR_PARSE,
  70. ERROR_REQUEST,
  71. ERROR_METHOD,
  72. ERROR_PARAMS,
  73. ERROR_INTERNAL,
  74. ERROR_OBJECT,
  75. ERROR_SESSION,
  76. ERROR_ACCESS,
  77. ERROR_TIMEOUT,
  78. __ERROR_MAX
  79. };
  80. static const struct {
  81. int code;
  82. const char *msg;
  83. } json_errors[__ERROR_MAX] = {
  84. [ERROR_PARSE] = { -32700, "Parse error" },
  85. [ERROR_REQUEST] = { -32600, "Invalid request" },
  86. [ERROR_METHOD] = { -32601, "Method not found" },
  87. [ERROR_PARAMS] = { -32602, "Invalid parameters" },
  88. [ERROR_INTERNAL] = { -32603, "Internal error" },
  89. [ERROR_OBJECT] = { -32000, "Object not found" },
  90. [ERROR_SESSION] = { -32001, "Session not found" },
  91. [ERROR_ACCESS] = { -32002, "Access denied" },
  92. [ERROR_TIMEOUT] = { -32003, "ubus request timed out" },
  93. };
  94. enum cors_hdr {
  95. HDR_ORIGIN,
  96. HDR_ACCESS_CONTROL_REQUEST_METHOD,
  97. HDR_ACCESS_CONTROL_REQUEST_HEADERS,
  98. __HDR_MAX
  99. };
  100. enum ubus_hdr {
  101. HDR_AUTHORIZATION,
  102. __HDR_UBUS_MAX
  103. };
  104. static const char *uh_ubus_get_auth(const struct blob_attr *attr)
  105. {
  106. static const struct blobmsg_policy hdr_policy[__HDR_UBUS_MAX] = {
  107. [HDR_AUTHORIZATION] = { "authorization", BLOBMSG_TYPE_STRING },
  108. };
  109. struct blob_attr *tb[__HDR_UBUS_MAX];
  110. blobmsg_parse(hdr_policy, __HDR_UBUS_MAX, tb, blob_data(attr), blob_len(attr));
  111. if (tb[HDR_AUTHORIZATION]) {
  112. const char *tmp = blobmsg_get_string(tb[HDR_AUTHORIZATION]);
  113. if (!strncasecmp(tmp, "Bearer ", 7))
  114. return tmp + 7;
  115. }
  116. return UH_UBUS_DEFAULT_SID;
  117. }
  118. static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout);
  119. static void uh_ubus_next_batched_request(struct client *cl)
  120. {
  121. struct dispatch_ubus *du = &cl->dispatch.ubus;
  122. du->timeout.cb = __uh_ubus_next_batched_request;
  123. uloop_timeout_set(&du->timeout, 1);
  124. }
  125. static void uh_ubus_add_cors_headers(struct client *cl)
  126. {
  127. struct blob_attr *tb[__HDR_MAX];
  128. static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
  129. [HDR_ORIGIN] = { "origin", BLOBMSG_TYPE_STRING },
  130. [HDR_ACCESS_CONTROL_REQUEST_METHOD] = { "access-control-request-method", BLOBMSG_TYPE_STRING },
  131. [HDR_ACCESS_CONTROL_REQUEST_HEADERS] = { "access-control-request-headers", BLOBMSG_TYPE_STRING },
  132. };
  133. blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(cl->hdr.head), blob_len(cl->hdr.head));
  134. if (!tb[HDR_ORIGIN])
  135. return;
  136. if (tb[HDR_ACCESS_CONTROL_REQUEST_METHOD])
  137. {
  138. char *hdr = (char *) blobmsg_data(tb[HDR_ACCESS_CONTROL_REQUEST_METHOD]);
  139. if (strcmp(hdr, "GET") && strcmp(hdr, "POST") && strcmp(hdr, "OPTIONS"))
  140. return;
  141. }
  142. ustream_printf(cl->us, "Access-Control-Allow-Origin: %s\r\n",
  143. blobmsg_get_string(tb[HDR_ORIGIN]));
  144. if (tb[HDR_ACCESS_CONTROL_REQUEST_HEADERS])
  145. ustream_printf(cl->us, "Access-Control-Allow-Headers: %s\r\n",
  146. blobmsg_get_string(tb[HDR_ACCESS_CONTROL_REQUEST_HEADERS]));
  147. ustream_printf(cl->us, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
  148. ustream_printf(cl->us, "Access-Control-Allow-Credentials: true\r\n");
  149. }
  150. static void uh_ubus_send_header(struct client *cl, int code, const char *summary, const char *content_type)
  151. {
  152. ops->http_header(cl, code, summary);
  153. if (conf.ubus_cors)
  154. uh_ubus_add_cors_headers(cl);
  155. ustream_printf(cl->us, "Content-Type: %s\r\n", content_type);
  156. if (cl->request.method == UH_HTTP_MSG_OPTIONS)
  157. ustream_printf(cl->us, "Content-Length: 0\r\n");
  158. ustream_printf(cl->us, "\r\n");
  159. }
  160. static void uh_ubus_send_response(struct client *cl, struct blob_buf *buf)
  161. {
  162. struct dispatch_ubus *du = &cl->dispatch.ubus;
  163. const char *sep = "";
  164. char *str;
  165. if (du->array && du->array_idx > 1)
  166. sep = ",";
  167. str = blobmsg_format_json(buf->head, true);
  168. ops->chunk_printf(cl, "%s%s", sep, str);
  169. free(str);
  170. du->jsobj_cur = NULL;
  171. if (du->array)
  172. uh_ubus_next_batched_request(cl);
  173. else
  174. return ops->request_done(cl);
  175. }
  176. static void uh_ubus_init_json_rpc_response(struct client *cl, struct blob_buf *buf)
  177. {
  178. struct dispatch_ubus *du = &cl->dispatch.ubus;
  179. struct json_object *obj = du->jsobj_cur, *obj2 = NULL;
  180. blobmsg_add_string(buf, "jsonrpc", "2.0");
  181. if (obj)
  182. json_object_object_get_ex(obj, "id", &obj2);
  183. if (obj2)
  184. blobmsg_add_json_element(buf, "id", obj2);
  185. else
  186. blobmsg_add_field(buf, BLOBMSG_TYPE_UNSPEC, "id", NULL, 0);
  187. }
  188. static void uh_ubus_json_rpc_error(struct client *cl, enum rpc_error type)
  189. {
  190. void *c;
  191. blob_buf_init(&buf, 0);
  192. uh_ubus_init_json_rpc_response(cl, &buf);
  193. c = blobmsg_open_table(&buf, "error");
  194. blobmsg_add_u32(&buf, "code", json_errors[type].code);
  195. blobmsg_add_string(&buf, "message", json_errors[type].msg);
  196. blobmsg_close_table(&buf, c);
  197. uh_ubus_send_response(cl, &buf);
  198. }
  199. static void uh_ubus_error(struct client *cl, int code, const char *message)
  200. {
  201. blob_buf_init(&buf, 0);
  202. blobmsg_add_u32(&buf, "code", code);
  203. blobmsg_add_string(&buf, "message", message);
  204. uh_ubus_send_response(cl, &buf);
  205. }
  206. static void uh_ubus_posix_error(struct client *cl, int err)
  207. {
  208. uh_ubus_error(cl, -err, strerror(err));
  209. }
  210. static void uh_ubus_ubus_error(struct client *cl, int err)
  211. {
  212. uh_ubus_error(cl, err, ubus_strerror(err));
  213. }
  214. static void uh_ubus_allowed_cb(struct ubus_request *req, int type, struct blob_attr *msg)
  215. {
  216. struct blob_attr *tb[__SES_MAX];
  217. bool *allow = (bool *)req->priv;
  218. if (!msg)
  219. return;
  220. blobmsg_parse(ses_policy, __SES_MAX, tb, blob_data(msg), blob_len(msg));
  221. if (tb[SES_ACCESS])
  222. *allow = blobmsg_get_bool(tb[SES_ACCESS]);
  223. }
  224. static bool uh_ubus_allowed(const char *sid, const char *obj, const char *fun)
  225. {
  226. uint32_t id;
  227. bool allow = false;
  228. static struct blob_buf req;
  229. if (ubus_lookup_id(ctx, "session", &id))
  230. return false;
  231. blob_buf_init(&req, 0);
  232. blobmsg_add_string(&req, "ubus_rpc_session", sid);
  233. blobmsg_add_string(&req, "object", obj);
  234. blobmsg_add_string(&req, "function", fun);
  235. ubus_invoke(ctx, id, "access", req.head, uh_ubus_allowed_cb, &allow, conf.script_timeout * 500);
  236. return allow;
  237. }
  238. /* GET requests handling */
  239. static void uh_ubus_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv);
  240. static void uh_ubus_handle_get_list(struct client *cl, const char *path)
  241. {
  242. static struct blob_buf tmp;
  243. struct list_data data = { .verbose = true, .add_object = !path, .buf = &tmp};
  244. struct blob_attr *cur;
  245. int rem;
  246. int err;
  247. blob_buf_init(&tmp, 0);
  248. err = ubus_lookup(ctx, path, uh_ubus_list_cb, &data);
  249. if (err) {
  250. uh_ubus_send_header(cl, 500, "Ubus Protocol Error", "application/json");
  251. uh_ubus_ubus_error(cl, err);
  252. return;
  253. }
  254. blob_buf_init(&buf, 0);
  255. blob_for_each_attr(cur, tmp.head, rem)
  256. blobmsg_add_blob(&buf, cur);
  257. uh_ubus_send_header(cl, 200, "OK", "application/json");
  258. uh_ubus_send_response(cl, &buf);
  259. }
  260. static int uh_ubus_subscription_notification_cb(struct ubus_context *ctx,
  261. struct ubus_object *obj,
  262. struct ubus_request_data *req,
  263. const char *method,
  264. struct blob_attr *msg)
  265. {
  266. struct ubus_subscriber *s;
  267. struct dispatch_ubus *du;
  268. struct client *cl;
  269. char *json;
  270. s = container_of(obj, struct ubus_subscriber, obj);
  271. du = container_of(s, struct dispatch_ubus, sub);
  272. cl = container_of(du, struct client, dispatch.ubus);
  273. json = blobmsg_format_json(msg, true);
  274. if (json) {
  275. ops->chunk_printf(cl, "event: %s\ndata: %s\n\n", method, json);
  276. free(json);
  277. }
  278. return 0;
  279. }
  280. static void uh_ubus_subscription_notification_remove_cb(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id)
  281. {
  282. struct dispatch_ubus *du;
  283. struct client *cl;
  284. du = container_of(s, struct dispatch_ubus, sub);
  285. cl = container_of(du, struct client, dispatch.ubus);
  286. ops->request_done(cl);
  287. }
  288. static void uh_ubus_handle_get_subscribe(struct client *cl, const char *path)
  289. {
  290. struct dispatch_ubus *du = &cl->dispatch.ubus;
  291. const char *sid;
  292. uint32_t id;
  293. int err;
  294. sid = uh_ubus_get_auth(cl->hdr.head);
  295. if (!conf.ubus_noauth && !uh_ubus_allowed(sid, path, ":subscribe")) {
  296. uh_ubus_send_header(cl, 200, "OK", "application/json");
  297. uh_ubus_posix_error(cl, EACCES);
  298. return;
  299. }
  300. du->sub.cb = uh_ubus_subscription_notification_cb;
  301. du->sub.remove_cb = uh_ubus_subscription_notification_remove_cb;
  302. uh_client_ref(cl);
  303. err = ubus_register_subscriber(ctx, &du->sub);
  304. if (err)
  305. goto err_unref;
  306. err = ubus_lookup_id(ctx, path, &id);
  307. if (err)
  308. goto err_unregister;
  309. err = ubus_subscribe(ctx, &du->sub, id);
  310. if (err)
  311. goto err_unregister;
  312. uh_ubus_send_header(cl, 200, "OK", "text/event-stream");
  313. if (conf.events_retry)
  314. ops->chunk_printf(cl, "retry: %d\n", conf.events_retry);
  315. return;
  316. err_unregister:
  317. ubus_unregister_subscriber(ctx, &du->sub);
  318. err_unref:
  319. uh_client_unref(cl);
  320. if (err) {
  321. uh_ubus_send_header(cl, 200, "OK", "application/json");
  322. uh_ubus_ubus_error(cl, err);
  323. }
  324. }
  325. static void uh_ubus_handle_get(struct client *cl)
  326. {
  327. struct dispatch_ubus *du = &cl->dispatch.ubus;
  328. const char *url = du->url_path;
  329. url += strlen(conf.ubus_prefix);
  330. if (!strcmp(url, "/list") || !strncmp(url, "/list/", strlen("/list/"))) {
  331. url += strlen("/list");
  332. uh_ubus_handle_get_list(cl, *url ? url + 1 : NULL);
  333. } else if (!strncmp(url, "/subscribe/", strlen("/subscribe/"))) {
  334. url += strlen("/subscribe");
  335. uh_ubus_handle_get_subscribe(cl, url + 1);
  336. } else {
  337. ops->http_header(cl, 404, "Not Found");
  338. ustream_printf(cl->us, "\r\n");
  339. ops->request_done(cl);
  340. }
  341. }
  342. /* POST requests handling */
  343. static void
  344. uh_ubus_request_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
  345. {
  346. struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
  347. struct blob_attr *cur;
  348. int len;
  349. blob_for_each_attr(cur, msg, len)
  350. blobmsg_add_blob(&du->buf, cur);
  351. }
  352. static void
  353. uh_ubus_request_cb(struct ubus_request *req, int ret)
  354. {
  355. struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
  356. struct client *cl = container_of(du, struct client, dispatch.ubus);
  357. struct blob_attr *cur;
  358. void *r;
  359. int rem;
  360. blob_buf_init(&buf, 0);
  361. uloop_timeout_cancel(&du->timeout);
  362. /* Legacy format always uses "result" array - even for errors and empty
  363. * results. */
  364. if (du->legacy) {
  365. void *c;
  366. uh_ubus_init_json_rpc_response(cl, &buf);
  367. r = blobmsg_open_array(&buf, "result");
  368. blobmsg_add_u32(&buf, "", ret);
  369. if (blob_len(du->buf.head)) {
  370. c = blobmsg_open_table(&buf, NULL);
  371. blob_for_each_attr(cur, du->buf.head, rem)
  372. blobmsg_add_blob(&buf, cur);
  373. blobmsg_close_table(&buf, c);
  374. }
  375. blobmsg_close_array(&buf, r);
  376. uh_ubus_send_response(cl, &buf);
  377. return;
  378. }
  379. if (ret) {
  380. void *c;
  381. uh_ubus_init_json_rpc_response(cl, &buf);
  382. c = blobmsg_open_table(&buf, "error");
  383. blobmsg_add_u32(&buf, "code", ret);
  384. blobmsg_add_string(&buf, "message", ubus_strerror(ret));
  385. blobmsg_close_table(&buf, c);
  386. uh_ubus_send_response(cl, &buf);
  387. } else {
  388. uh_ubus_init_json_rpc_response(cl, &buf);
  389. if (blob_len(du->buf.head)) {
  390. r = blobmsg_open_table(&buf, "result");
  391. blob_for_each_attr(cur, du->buf.head, rem)
  392. blobmsg_add_blob(&buf, cur);
  393. blobmsg_close_table(&buf, r);
  394. } else {
  395. blobmsg_add_field(&buf, BLOBMSG_TYPE_UNSPEC, "result", NULL, 0);
  396. }
  397. uh_ubus_send_response(cl, &buf);
  398. }
  399. }
  400. static void
  401. uh_ubus_timeout_cb(struct uloop_timeout *timeout)
  402. {
  403. struct dispatch_ubus *du = container_of(timeout, struct dispatch_ubus, timeout);
  404. struct client *cl = container_of(du, struct client, dispatch.ubus);
  405. ubus_abort_request(ctx, &du->req);
  406. uh_ubus_json_rpc_error(cl, ERROR_TIMEOUT);
  407. }
  408. static void uh_ubus_close_fds(struct client *cl)
  409. {
  410. if (ctx->sock.fd < 0)
  411. return;
  412. close(ctx->sock.fd);
  413. ctx->sock.fd = -1;
  414. }
  415. static void uh_ubus_request_free(struct client *cl)
  416. {
  417. struct dispatch_ubus *du = &cl->dispatch.ubus;
  418. blob_buf_free(&du->buf);
  419. uloop_timeout_cancel(&du->timeout);
  420. if (du->jsobj)
  421. json_object_put(du->jsobj);
  422. if (du->jstok)
  423. json_tokener_free(du->jstok);
  424. if (du->req_pending)
  425. ubus_abort_request(ctx, &du->req);
  426. free(du->url_path);
  427. du->url_path = NULL;
  428. }
  429. static void uh_ubus_single_error(struct client *cl, enum rpc_error type)
  430. {
  431. uh_ubus_send_header(cl, 200, "OK", "application/json");
  432. uh_ubus_json_rpc_error(cl, type);
  433. ops->request_done(cl);
  434. }
  435. static void uh_ubus_send_request(struct client *cl, const char *sid, struct blob_attr *args)
  436. {
  437. struct dispatch *d = &cl->dispatch;
  438. struct dispatch_ubus *du = &d->ubus;
  439. struct blob_attr *cur;
  440. static struct blob_buf req;
  441. int ret, rem;
  442. blob_buf_init(&req, 0);
  443. blobmsg_for_each_attr(cur, args, rem) {
  444. if (!strcmp(blobmsg_name(cur), "ubus_rpc_session"))
  445. return uh_ubus_json_rpc_error(cl, ERROR_PARAMS);
  446. blobmsg_add_blob(&req, cur);
  447. }
  448. blobmsg_add_string(&req, "ubus_rpc_session", sid);
  449. blob_buf_init(&du->buf, 0);
  450. memset(&du->req, 0, sizeof(du->req));
  451. ret = ubus_invoke_async(ctx, du->obj, du->func, req.head, &du->req);
  452. if (ret)
  453. return uh_ubus_json_rpc_error(cl, ERROR_INTERNAL);
  454. du->req.data_cb = uh_ubus_request_data_cb;
  455. du->req.complete_cb = uh_ubus_request_cb;
  456. ubus_complete_request_async(ctx, &du->req);
  457. du->timeout.cb = uh_ubus_timeout_cb;
  458. uloop_timeout_set(&du->timeout, conf.script_timeout * 1000);
  459. du->req_pending = true;
  460. }
  461. static void uh_ubus_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
  462. {
  463. struct blob_attr *sig, *attr;
  464. struct list_data *data = priv;
  465. int rem, rem2;
  466. void *t, *o;
  467. if (!data->verbose) {
  468. blobmsg_add_string(data->buf, NULL, obj->path);
  469. return;
  470. }
  471. if (!obj->signature)
  472. return;
  473. if (data->add_object)
  474. o = blobmsg_open_table(data->buf, obj->path);
  475. blob_for_each_attr(sig, obj->signature, rem) {
  476. t = blobmsg_open_table(data->buf, blobmsg_name(sig));
  477. rem2 = blobmsg_data_len(sig);
  478. __blob_for_each_attr(attr, blobmsg_data(sig), rem2) {
  479. if (blob_id(attr) != BLOBMSG_TYPE_INT32)
  480. continue;
  481. switch (blobmsg_get_u32(attr)) {
  482. case BLOBMSG_TYPE_INT8:
  483. blobmsg_add_string(data->buf, blobmsg_name(attr), "boolean");
  484. break;
  485. case BLOBMSG_TYPE_INT32:
  486. blobmsg_add_string(data->buf, blobmsg_name(attr), "number");
  487. break;
  488. case BLOBMSG_TYPE_STRING:
  489. blobmsg_add_string(data->buf, blobmsg_name(attr), "string");
  490. break;
  491. case BLOBMSG_TYPE_ARRAY:
  492. blobmsg_add_string(data->buf, blobmsg_name(attr), "array");
  493. break;
  494. case BLOBMSG_TYPE_TABLE:
  495. blobmsg_add_string(data->buf, blobmsg_name(attr), "object");
  496. break;
  497. default:
  498. blobmsg_add_string(data->buf, blobmsg_name(attr), "unknown");
  499. break;
  500. }
  501. }
  502. blobmsg_close_table(data->buf, t);
  503. }
  504. if (data->add_object)
  505. blobmsg_close_table(data->buf, o);
  506. }
  507. static void uh_ubus_send_list(struct client *cl, struct blob_attr *params)
  508. {
  509. struct blob_attr *cur, *dup;
  510. struct list_data data = { .buf = &cl->dispatch.ubus.buf, .verbose = false, .add_object = true };
  511. void *r;
  512. int rem;
  513. blob_buf_init(data.buf, 0);
  514. uh_client_ref(cl);
  515. if (!params || blob_id(params) != BLOBMSG_TYPE_ARRAY) {
  516. r = blobmsg_open_array(data.buf, "result");
  517. ubus_lookup(ctx, NULL, uh_ubus_list_cb, &data);
  518. blobmsg_close_array(data.buf, r);
  519. }
  520. else {
  521. r = blobmsg_open_table(data.buf, "result");
  522. dup = blob_memdup(params);
  523. if (dup)
  524. {
  525. rem = blobmsg_data_len(dup);
  526. data.verbose = true;
  527. __blob_for_each_attr(cur, blobmsg_data(dup), rem)
  528. ubus_lookup(ctx, blobmsg_data(cur), uh_ubus_list_cb, &data);
  529. free(dup);
  530. }
  531. blobmsg_close_table(data.buf, r);
  532. }
  533. uh_client_unref(cl);
  534. blob_buf_init(&buf, 0);
  535. uh_ubus_init_json_rpc_response(cl, &buf);
  536. blobmsg_add_blob(&buf, blob_data(data.buf->head));
  537. uh_ubus_send_response(cl, &buf);
  538. }
  539. static bool parse_json_rpc(struct rpc_data *d, struct blob_attr *data)
  540. {
  541. struct blob_attr *tb[__RPC_MAX];
  542. struct blob_attr *cur;
  543. blobmsg_parse(rpc_policy, __RPC_MAX, tb, blob_data(data), blob_len(data));
  544. cur = tb[RPC_JSONRPC];
  545. if (!cur || strcmp(blobmsg_data(cur), "2.0") != 0)
  546. return false;
  547. cur = tb[RPC_METHOD];
  548. if (!cur)
  549. return false;
  550. d->id = tb[RPC_ID];
  551. d->method = blobmsg_data(cur);
  552. cur = tb[RPC_PARAMS];
  553. if (!cur)
  554. return true;
  555. d->params = blob_memdup(cur);
  556. if (!d->params)
  557. return false;
  558. return true;
  559. }
  560. static void parse_call_params(struct rpc_data *d)
  561. {
  562. const struct blobmsg_policy data_policy[] = {
  563. { .type = BLOBMSG_TYPE_STRING },
  564. { .type = BLOBMSG_TYPE_STRING },
  565. { .type = BLOBMSG_TYPE_STRING },
  566. { .type = BLOBMSG_TYPE_TABLE },
  567. };
  568. struct blob_attr *tb[4];
  569. if (!d->params || blobmsg_type(d->params) != BLOBMSG_TYPE_ARRAY)
  570. return;
  571. blobmsg_parse_array(data_policy, ARRAY_SIZE(data_policy), tb,
  572. blobmsg_data(d->params), blobmsg_data_len(d->params));
  573. if (tb[0])
  574. d->sid = blobmsg_data(tb[0]);
  575. if (conf.ubus_noauth && (!d->sid || !*d->sid))
  576. d->sid = UH_UBUS_DEFAULT_SID;
  577. if (tb[1])
  578. d->object = blobmsg_data(tb[1]);
  579. if (tb[2])
  580. d->function = blobmsg_data(tb[2]);
  581. d->data = tb[3];
  582. }
  583. static void uh_ubus_init_batch(struct client *cl)
  584. {
  585. struct dispatch_ubus *du = &cl->dispatch.ubus;
  586. du->array = true;
  587. uh_ubus_send_header(cl, 200, "OK", "application/json");
  588. ops->chunk_printf(cl, "[");
  589. }
  590. static void uh_ubus_complete_batch(struct client *cl)
  591. {
  592. ops->chunk_printf(cl, "]");
  593. ops->request_done(cl);
  594. }
  595. static void uh_ubus_handle_request_object(struct client *cl, struct json_object *obj)
  596. {
  597. struct dispatch_ubus *du = &cl->dispatch.ubus;
  598. struct rpc_data data = {};
  599. enum rpc_error err = ERROR_PARSE;
  600. static struct blob_buf req;
  601. uh_client_ref(cl);
  602. if (json_object_get_type(obj) != json_type_object)
  603. goto error;
  604. du->jsobj_cur = obj;
  605. blob_buf_init(&req, 0);
  606. if (!blobmsg_add_object(&req, obj))
  607. goto error;
  608. if (!parse_json_rpc(&data, req.head))
  609. goto error;
  610. if (!strcmp(data.method, "call")) {
  611. parse_call_params(&data);
  612. if (!data.sid || !data.object || !data.function || !data.data)
  613. goto error;
  614. du->func = data.function;
  615. if (ubus_lookup_id(ctx, data.object, &du->obj)) {
  616. err = ERROR_OBJECT;
  617. goto error;
  618. }
  619. if (!conf.ubus_noauth && !uh_ubus_allowed(data.sid, data.object, data.function)) {
  620. err = ERROR_ACCESS;
  621. goto error;
  622. }
  623. uh_ubus_send_request(cl, data.sid, data.data);
  624. goto out;
  625. }
  626. else if (!strcmp(data.method, "list")) {
  627. uh_ubus_send_list(cl, data.params);
  628. goto out;
  629. }
  630. else {
  631. err = ERROR_METHOD;
  632. goto error;
  633. }
  634. error:
  635. uh_ubus_json_rpc_error(cl, err);
  636. out:
  637. if (data.params)
  638. free(data.params);
  639. uh_client_unref(cl);
  640. }
  641. static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout)
  642. {
  643. struct dispatch_ubus *du = container_of(timeout, struct dispatch_ubus, timeout);
  644. struct client *cl = container_of(du, struct client, dispatch.ubus);
  645. struct json_object *obj = du->jsobj;
  646. int len;
  647. len = json_object_array_length(obj);
  648. if (du->array_idx >= len)
  649. return uh_ubus_complete_batch(cl);
  650. obj = json_object_array_get_idx(obj, du->array_idx++);
  651. uh_ubus_handle_request_object(cl, obj);
  652. }
  653. static void uh_ubus_data_done(struct client *cl)
  654. {
  655. struct dispatch_ubus *du = &cl->dispatch.ubus;
  656. struct json_object *obj = du->jsobj;
  657. switch (obj ? json_object_get_type(obj) : json_type_null) {
  658. case json_type_object:
  659. uh_ubus_send_header(cl, 200, "OK", "application/json");
  660. return uh_ubus_handle_request_object(cl, obj);
  661. case json_type_array:
  662. uh_ubus_init_batch(cl);
  663. return uh_ubus_next_batched_request(cl);
  664. default:
  665. return uh_ubus_single_error(cl, ERROR_PARSE);
  666. }
  667. }
  668. static void uh_ubus_call(struct client *cl, const char *path, const char *sid)
  669. {
  670. struct dispatch_ubus *du = &cl->dispatch.ubus;
  671. struct json_object *obj = du->jsobj;
  672. struct rpc_data data = {};
  673. enum rpc_error err = ERROR_PARSE;
  674. static struct blob_buf req;
  675. uh_client_ref(cl);
  676. if (!obj || json_object_get_type(obj) != json_type_object)
  677. goto error;
  678. uh_ubus_send_header(cl, 200, "OK", "application/json");
  679. du->jsobj_cur = obj;
  680. blob_buf_init(&req, 0);
  681. if (!blobmsg_add_object(&req, obj))
  682. goto error;
  683. if (!parse_json_rpc(&data, req.head))
  684. goto error;
  685. du->func = data.method;
  686. if (ubus_lookup_id(ctx, path, &du->obj)) {
  687. err = ERROR_OBJECT;
  688. goto error;
  689. }
  690. if (!conf.ubus_noauth && !uh_ubus_allowed(sid, path, data.method)) {
  691. err = ERROR_ACCESS;
  692. goto error;
  693. }
  694. uh_ubus_send_request(cl, sid, data.params);
  695. goto out;
  696. error:
  697. uh_ubus_json_rpc_error(cl, err);
  698. out:
  699. if (data.params)
  700. free(data.params);
  701. uh_client_unref(cl);
  702. }
  703. static void uh_ubus_handle_post(struct client *cl)
  704. {
  705. struct dispatch_ubus *du = &cl->dispatch.ubus;
  706. const char *url = du->url_path;
  707. const char *auth;
  708. /* Treat both: /foo AND /foo/ as legacy requests. */
  709. if (ops->path_match(conf.ubus_prefix, url) && strlen(url) - strlen(conf.ubus_prefix) <= 1) {
  710. du->legacy = true;
  711. uh_ubus_data_done(cl);
  712. return;
  713. }
  714. auth = uh_ubus_get_auth(cl->hdr.head);
  715. url += strlen(conf.ubus_prefix);
  716. if (!strncmp(url, "/call/", strlen("/call/"))) {
  717. url += strlen("/call/");
  718. uh_ubus_call(cl, url, auth);
  719. } else {
  720. ops->http_header(cl, 404, "Not Found");
  721. ustream_printf(cl->us, "\r\n");
  722. ops->request_done(cl);
  723. }
  724. }
  725. static int uh_ubus_data_send(struct client *cl, const char *data, int len)
  726. {
  727. struct dispatch_ubus *du = &cl->dispatch.ubus;
  728. if (du->jsobj || !du->jstok)
  729. goto error;
  730. du->post_len += len;
  731. if (du->post_len > UH_UBUS_MAX_POST_SIZE)
  732. goto error;
  733. du->jsobj = json_tokener_parse_ex(du->jstok, data, len);
  734. return len;
  735. error:
  736. uh_ubus_single_error(cl, ERROR_PARSE);
  737. return 0;
  738. }
  739. static void uh_ubus_handle_request(struct client *cl, char *url, struct path_info *pi)
  740. {
  741. struct dispatch *d = &cl->dispatch;
  742. struct dispatch_ubus *du = &d->ubus;
  743. char *chr;
  744. du->url_path = strdup(url);
  745. if (!du->url_path) {
  746. ops->client_error(cl, 500, "Internal Server Error", "Failed to allocate resources");
  747. return;
  748. }
  749. chr = strchr(du->url_path, '?');
  750. if (chr)
  751. chr[0] = '\0';
  752. du->legacy = false;
  753. switch (cl->request.method)
  754. {
  755. case UH_HTTP_MSG_GET:
  756. uh_ubus_handle_get(cl);
  757. break;
  758. case UH_HTTP_MSG_POST:
  759. d->data_send = uh_ubus_data_send;
  760. d->data_done = uh_ubus_handle_post;
  761. d->close_fds = uh_ubus_close_fds;
  762. d->free = uh_ubus_request_free;
  763. du->jstok = json_tokener_new();
  764. return;
  765. case UH_HTTP_MSG_OPTIONS:
  766. uh_ubus_send_header(cl, 200, "OK", "application/json");
  767. ops->request_done(cl);
  768. break;
  769. default:
  770. ops->client_error(cl, 400, "Bad Request", "Invalid Request");
  771. }
  772. free(du->url_path);
  773. du->url_path = NULL;
  774. }
  775. static bool
  776. uh_ubus_check_url(const char *url)
  777. {
  778. return ops->path_match(conf.ubus_prefix, url);
  779. }
  780. static int
  781. uh_ubus_init(void)
  782. {
  783. static struct dispatch_handler ubus_dispatch = {
  784. .check_url = uh_ubus_check_url,
  785. .handle_request = uh_ubus_handle_request,
  786. };
  787. ctx = ubus_connect(conf.ubus_socket);
  788. if (!ctx) {
  789. fprintf(stderr, "Unable to connect to ubus socket\n");
  790. exit(1);
  791. }
  792. ops->dispatch_add(&ubus_dispatch);
  793. uloop_done();
  794. return 0;
  795. }
  796. static int uh_ubus_plugin_init(const struct uhttpd_ops *o, struct config *c)
  797. {
  798. ops = o;
  799. _conf = c;
  800. return uh_ubus_init();
  801. }
  802. static void uh_ubus_post_init(void)
  803. {
  804. ubus_add_uloop(ctx);
  805. }
  806. struct uhttpd_plugin uhttpd_plugin = {
  807. .init = uh_ubus_plugin_init,
  808. .post_init = uh_ubus_post_init,
  809. };