86b0d44828798f3a0b3ec0275a15df287c8a8565
[project/uhttpd.git] / ubus.c
1 /*
2 * uhttpd - Tiny single-threaded httpd
3 *
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 */
19
20
21 #include <libubox/blobmsg.h>
22 #include <libubox/blobmsg_json.h>
23 #include <libubox/avl.h>
24 #include <libubox/avl-cmp.h>
25 #include <fnmatch.h>
26 #include <stdio.h>
27 #include <poll.h>
28
29 #include "uhttpd.h"
30 #include "plugin.h"
31 #include "ubus.h"
32
33 static const struct uhttpd_ops *ops;
34 static struct config *_conf;
35 #define conf (*_conf)
36
37 static struct ubus_context *ctx;
38 static struct avl_tree sessions;
39 static struct blob_buf buf;
40
41 static const struct blobmsg_policy new_policy = {
42 .name = "timeout", .type = BLOBMSG_TYPE_INT32
43 };
44
45 static const struct blobmsg_policy sid_policy = {
46 .name = "sid", .type = BLOBMSG_TYPE_STRING
47 };
48
49 enum {
50 UH_UBUS_SS_SID,
51 UH_UBUS_SS_VALUES,
52 __UH_UBUS_SS_MAX,
53 };
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 },
57 };
58
59 enum {
60 UH_UBUS_SG_SID,
61 UH_UBUS_SG_KEYS,
62 __UH_UBUS_SG_MAX,
63 };
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 },
67 };
68
69 enum {
70 UH_UBUS_SA_SID,
71 UH_UBUS_SA_OBJECTS,
72 __UH_UBUS_SA_MAX,
73 };
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 },
77 };
78
79 /*
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
84 */
85 #define uh_foreach_matching_acl_prefix(_acl, _ses, _obj, _func) \
86 for (_acl = avl_find_le_element(&(_ses)->acls, _obj, _acl, avl); \
87 _acl; \
88 _acl = avl_is_first(&(ses)->acls, &(_acl)->avl) ? NULL : \
89 avl_prev_element((_acl), avl))
90
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))
96
97 static void
98 uh_ubus_random(char *dest)
99 {
100 unsigned char buf[16] = { 0 };
101 FILE *f;
102 int i;
103
104 f = fopen("/dev/urandom", "r");
105 if (!f)
106 return;
107
108 fread(buf, 1, sizeof(buf), f);
109 fclose(f);
110
111 for (i = 0; i < sizeof(buf); i++)
112 sprintf(dest + (i<<1), "%02x", buf[i]);
113 }
114
115 static void
116 uh_ubus_session_dump_data(struct uh_ubus_session *ses, struct blob_buf *b)
117 {
118 struct uh_ubus_session_data *d;
119
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));
123 }
124 }
125
126 static void
127 uh_ubus_session_dump_acls(struct uh_ubus_session *ses, struct blob_buf *b)
128 {
129 struct uh_ubus_session_acl *acl;
130 const char *lastobj = NULL;
131 void *c = NULL;
132
133 avl_for_each_element(&ses->acls, acl, avl) {
134 if (!lastobj || strcmp(acl->object, lastobj))
135 {
136 if (c) blobmsg_close_array(b, c);
137 c = blobmsg_open_array(b, acl->object);
138 }
139
140 blobmsg_add_string(b, NULL, acl->function);
141 lastobj = acl->object;
142 }
143
144 if (c) blobmsg_close_array(b, c);
145 }
146
147 static void
148 uh_ubus_session_dump(struct uh_ubus_session *ses,
149 struct ubus_context *ctx,
150 struct ubus_request_data *req)
151 {
152 void *c;
153 struct blob_buf b;
154
155 memset(&b, 0, sizeof(b));
156 blob_buf_init(&b, 0);
157
158 blobmsg_add_string(&b, "sid", ses->id);
159 blobmsg_add_u32(&b, "timeout", ses->timeout);
160
161 c = blobmsg_open_table(&b, "acls");
162 uh_ubus_session_dump_acls(ses, &b);
163 blobmsg_close_table(&b, c);
164
165 c = blobmsg_open_table(&b, "data");
166 uh_ubus_session_dump_data(ses, &b);
167 blobmsg_close_table(&b, c);
168
169 ubus_send_reply(ctx, req, b.head);
170 blob_buf_free(&b);
171 }
172
173 static void
174 uh_ubus_touch_session(struct uh_ubus_session *ses)
175 {
176 uloop_timeout_set(&ses->t, ses->timeout * 1000);
177 }
178
179 static void
180 uh_ubus_session_destroy(struct uh_ubus_session *ses)
181 {
182 struct uh_ubus_session_acl *acl, *nacl;
183 struct uh_ubus_session_data *data, *ndata;
184
185 uloop_timeout_cancel(&ses->t);
186 avl_remove_all_elements(&ses->acls, acl, avl, nacl)
187 free(acl);
188
189 avl_remove_all_elements(&ses->data, data, avl, ndata)
190 free(data);
191
192 avl_delete(&sessions, &ses->avl);
193 free(ses);
194 }
195
196 static void uh_ubus_session_timeout(struct uloop_timeout *t)
197 {
198 struct uh_ubus_session *ses;
199
200 ses = container_of(t, struct uh_ubus_session, t);
201 uh_ubus_session_destroy(ses);
202 }
203
204 static struct uh_ubus_session *
205 uh_ubus_session_create(int timeout)
206 {
207 struct uh_ubus_session *ses;
208
209 ses = calloc(1, sizeof(*ses));
210 if (!ses)
211 return NULL;
212
213 ses->timeout = timeout;
214 ses->avl.key = ses->id;
215 uh_ubus_random(ses->id);
216
217 avl_insert(&sessions, &ses->avl);
218 avl_init(&ses->acls, avl_strcmp, true, NULL);
219 avl_init(&ses->data, avl_strcmp, false, NULL);
220
221 ses->t.cb = uh_ubus_session_timeout;
222 uh_ubus_touch_session(ses);
223
224 return ses;
225 }
226
227 static struct uh_ubus_session *
228 uh_ubus_session_get(const char *id)
229 {
230 struct uh_ubus_session *ses;
231
232 ses = avl_find_element(&sessions, id, ses, avl);
233 if (!ses)
234 return NULL;
235
236 uh_ubus_touch_session(ses);
237 return ses;
238 }
239
240 static int
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)
244 {
245 struct uh_ubus_session *ses;
246 struct blob_attr *tb;
247 int timeout = conf.script_timeout;
248
249 blobmsg_parse(&new_policy, 1, &tb, blob_data(msg), blob_len(msg));
250 if (tb)
251 timeout = blobmsg_get_u32(tb);
252
253 ses = uh_ubus_session_create(timeout);
254 if (ses)
255 uh_ubus_session_dump(ses, ctx, req);
256
257 return 0;
258 }
259
260 static int
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)
264 {
265 struct uh_ubus_session *ses;
266 struct blob_attr *tb;
267
268 blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
269
270 if (!tb) {
271 avl_for_each_element(&sessions, ses, avl)
272 uh_ubus_session_dump(ses, ctx, req);
273 return 0;
274 }
275
276 ses = uh_ubus_session_get(blobmsg_data(tb));
277 if (!ses)
278 return UBUS_STATUS_NOT_FOUND;
279
280 uh_ubus_session_dump(ses, ctx, req);
281
282 return 0;
283 }
284
285 static int
286 uh_id_len(const char *str)
287 {
288 return strcspn(str, "*?[");
289 }
290
291 static int
292 uh_ubus_session_grant(struct uh_ubus_session *ses, struct ubus_context *ctx,
293 const char *object, const char *function)
294 {
295 struct uh_ubus_session_acl *acl;
296 char *new_obj, *new_func, *new_id;
297 int id_len;
298
299 if (!object || !function)
300 return UBUS_STATUS_INVALID_ARGUMENT;
301
302 uh_foreach_matching_acl_prefix(acl, ses, object, function) {
303 if (!strcmp(acl->object, object) &&
304 !strcmp(acl->function, function))
305 return 0;
306 }
307
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);
313
314 if (!acl)
315 return UBUS_STATUS_UNKNOWN_ERROR;
316
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);
321
322 return 0;
323 }
324
325 static int
326 uh_ubus_session_revoke(struct uh_ubus_session *ses, struct ubus_context *ctx,
327 const char *object, const char *function)
328 {
329 struct uh_ubus_session_acl *acl, *next;
330 int id_len;
331 char *id;
332
333 if (!object && !function) {
334 avl_remove_all_elements(&ses->acls, acl, avl, next)
335 free(acl);
336 return 0;
337 }
338
339 id_len = uh_id_len(object);
340 id = alloca(id_len + 1);
341 strncpy(id, object, id_len);
342 id[id_len] = 0;
343
344 acl = avl_find_element(&ses->acls, id, acl, avl);
345 while (acl) {
346 if (!avl_is_last(&ses->acls, &acl->avl))
347 next = avl_next_element(acl, avl);
348 else
349 next = NULL;
350
351 if (strcmp(id, acl->avl.key) != 0)
352 break;
353
354 if (!strcmp(acl->object, object) &&
355 !strcmp(acl->function, function)) {
356 avl_delete(&ses->acls, &acl->avl);
357 free(acl);
358 }
359 acl = next;
360 }
361
362 return 0;
363 }
364
365
366 static int
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)
370 {
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;
375 int rem1, rem2;
376
377 int (*cb)(struct uh_ubus_session *ses, struct ubus_context *ctx,
378 const char *object, const char *function);
379
380 blobmsg_parse(acl_policy, __UH_UBUS_SA_MAX, tb, blob_data(msg), blob_len(msg));
381
382 if (!tb[UH_UBUS_SA_SID])
383 return UBUS_STATUS_INVALID_ARGUMENT;
384
385 ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SA_SID]));
386 if (!ses)
387 return UBUS_STATUS_NOT_FOUND;
388
389 if (!strcmp(method, "grant"))
390 cb = uh_ubus_session_grant;
391 else
392 cb = uh_ubus_session_revoke;
393
394 if (!tb[UH_UBUS_SA_OBJECTS])
395 return cb(ses, ctx, NULL, NULL);
396
397 blobmsg_for_each_attr(attr, tb[UH_UBUS_SA_OBJECTS], rem1) {
398 if (blob_id(attr) != BLOBMSG_TYPE_ARRAY)
399 continue;
400
401 object = NULL;
402 function = NULL;
403
404 blobmsg_for_each_attr(sattr, attr, rem2) {
405 if (blob_id(sattr) != BLOBMSG_TYPE_STRING)
406 continue;
407
408 if (!object)
409 object = blobmsg_data(sattr);
410 else if (!function)
411 function = blobmsg_data(sattr);
412 else
413 break;
414 }
415
416 if (object && function)
417 cb(ses, ctx, object, function);
418 }
419
420 return 0;
421 }
422
423 static int
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)
427 {
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;
432 int rem;
433
434 blobmsg_parse(set_policy, __UH_UBUS_SS_MAX, tb, blob_data(msg), blob_len(msg));
435
436 if (!tb[UH_UBUS_SS_SID] || !tb[UH_UBUS_SS_VALUES])
437 return UBUS_STATUS_INVALID_ARGUMENT;
438
439 ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SS_SID]));
440 if (!ses)
441 return UBUS_STATUS_NOT_FOUND;
442
443 blobmsg_for_each_attr(attr, tb[UH_UBUS_SS_VALUES], rem) {
444 if (!blobmsg_name(attr)[0])
445 continue;
446
447 data = avl_find_element(&ses->data, blobmsg_name(attr), data, avl);
448 if (data) {
449 avl_delete(&ses->data, &data->avl);
450 free(data);
451 }
452
453 data = calloc(1, sizeof(*data) + blob_pad_len(attr));
454 if (!data)
455 break;
456
457 memcpy(data->attr, attr, blob_pad_len(attr));
458 data->avl.key = blobmsg_name(data->attr);
459 avl_insert(&ses->data, &data->avl);
460 }
461
462 return 0;
463 }
464
465 static int
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)
469 {
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;
474 struct blob_buf b;
475 void *c;
476 int rem;
477
478 blobmsg_parse(get_policy, __UH_UBUS_SG_MAX, tb, blob_data(msg), blob_len(msg));
479
480 if (!tb[UH_UBUS_SG_SID])
481 return UBUS_STATUS_INVALID_ARGUMENT;
482
483 ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SG_SID]));
484 if (!ses)
485 return UBUS_STATUS_NOT_FOUND;
486
487 memset(&b, 0, sizeof(b));
488 blob_buf_init(&b, 0);
489 c = blobmsg_open_table(&b, "values");
490
491 if (!tb[UH_UBUS_SG_KEYS]) {
492 uh_ubus_session_dump_data(ses, &b);
493 return 0;
494 }
495
496 blobmsg_for_each_attr(attr, tb[UH_UBUS_SG_KEYS], rem) {
497 if (blob_id(attr) != BLOBMSG_TYPE_STRING)
498 continue;
499
500 data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
501 if (!data)
502 continue;
503
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));
508 }
509
510 blobmsg_close_table(&b, c);
511 ubus_send_reply(ctx, req, b.head);
512 blob_buf_free(&b);
513
514 return 0;
515 }
516
517 static int
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)
521 {
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;
526 int rem;
527
528 blobmsg_parse(get_policy, __UH_UBUS_SG_MAX, tb, blob_data(msg), blob_len(msg));
529
530 if (!tb[UH_UBUS_SG_SID])
531 return UBUS_STATUS_INVALID_ARGUMENT;
532
533 ses = uh_ubus_session_get(blobmsg_data(tb[UH_UBUS_SG_SID]));
534 if (!ses)
535 return UBUS_STATUS_NOT_FOUND;
536
537 if (!tb[UH_UBUS_SG_KEYS]) {
538 avl_remove_all_elements(&ses->data, data, avl, ndata)
539 free(data);
540 return 0;
541 }
542
543 blobmsg_for_each_attr(attr, tb[UH_UBUS_SG_KEYS], rem) {
544 if (blob_id(attr) != BLOBMSG_TYPE_STRING)
545 continue;
546
547 data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
548 if (!data)
549 continue;
550
551 avl_delete(&ses->data, &data->avl);
552 free(data);
553 }
554
555 return 0;
556 }
557
558 static int
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)
562 {
563 struct uh_ubus_session *ses;
564 struct blob_attr *tb;
565
566 blobmsg_parse(&sid_policy, 1, &tb, blob_data(msg), blob_len(msg));
567
568 if (!tb)
569 return UBUS_STATUS_INVALID_ARGUMENT;
570
571 ses = uh_ubus_session_get(blobmsg_data(tb));
572 if (!ses)
573 return UBUS_STATUS_NOT_FOUND;
574
575 uh_ubus_session_destroy(ses);
576
577 return 0;
578 }
579
580 static char *split_str(char *str)
581 {
582 if (str)
583 str = strchr(str, '/');
584
585 while (str && *str == '/') {
586 *str = 0;
587 str++;
588 }
589 return str;
590 }
591
592 static bool
593 uh_ubus_request_parse_url(struct client *cl, char *url, char **sid, char **obj, char **fun)
594 {
595 url += strlen(conf.ubus_prefix);
596 while (url && *url == '/')
597 url++;
598
599 *sid = url;
600
601 url = split_str(url);
602 *obj = url;
603
604 url = split_str(url);
605 *fun = url;
606
607 return *sid && *obj && *fun;
608 }
609
610 static void
611 uh_ubus_request_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
612 {
613 struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
614 struct client *cl = container_of(du, struct client, dispatch.ubus);
615 char *str;
616
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;
621 }
622
623 str = blobmsg_format_json_indent(msg, true, 0);
624 ops->chunk_write(cl, str, strlen(str));
625 free(str);
626 }
627
628 static void
629 uh_ubus_request_cb(struct ubus_request *req, int ret)
630 {
631 struct dispatch_ubus *du = container_of(req, struct dispatch_ubus, req);
632 struct client *cl = container_of(du, struct client, dispatch.ubus);
633
634 if (!du->header_sent)
635 return ops->client_error(cl, 204, "No content", "Function did not return data");
636
637 ops->request_done(cl);
638 }
639
640 static void uh_ubus_close_fds(struct client *cl)
641 {
642 if (ctx->sock.fd < 0)
643 return;
644
645 close(ctx->sock.fd);
646 ctx->sock.fd = -1;
647 }
648
649 static void uh_ubus_request_free(struct client *cl)
650 {
651 struct dispatch_ubus *du = &cl->dispatch.ubus;
652
653 if (du->jsobj)
654 json_object_put(du->jsobj);
655
656 if (du->jstok)
657 json_tokener_free(du->jstok);
658
659 if (du->req_pending)
660 ubus_abort_request(ctx, &du->req);
661 }
662
663 static void uh_ubus_json_error(struct client *cl)
664 {
665 ops->client_error(cl, 400, "Bad Request", "Invalid JSON data");
666 }
667
668 static void uh_ubus_send_request(struct client *cl, json_object *obj)
669 {
670 struct dispatch *d = &cl->dispatch;
671 struct dispatch_ubus *du = &d->ubus;
672 int ret;
673
674 blob_buf_init(&buf, 0);
675
676 if (obj && !blobmsg_add_object(&buf, obj))
677 return uh_ubus_json_error(cl);
678
679 ret = ubus_invoke_async(ctx, du->obj, du->func, buf.head, &du->req);
680 if (ret)
681 return ops->client_error(cl, 500, "Internal Error",
682 "Error sending ubus request: %s", ubus_strerror(ret));
683
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);
687
688 du->req_pending = true;
689 }
690
691 static void uh_ubus_data_done(struct client *cl)
692 {
693 struct dispatch_ubus *du = &cl->dispatch.ubus;
694 struct json_object *obj = du->jsobj;
695
696 if (!obj || json_object_get_type(obj) != json_type_object)
697 return uh_ubus_json_error(cl);
698
699 uh_ubus_send_request(cl, obj);
700 }
701
702 static int uh_ubus_data_send(struct client *cl, const char *data, int len)
703 {
704 struct dispatch_ubus *du = &cl->dispatch.ubus;
705
706 if (du->jsobj) {
707 uh_ubus_json_error(cl);
708 return 0;
709 }
710
711 du->post_len += len;
712 if (du->post_len > UH_UBUS_MAX_POST_SIZE) {
713 ops->client_error(cl, 413, "Too Large", "Message too big");
714 return 0;
715 }
716
717 du->jsobj = json_tokener_parse_ex(du->jstok, data, len);
718 return len;
719 }
720
721 static void uh_ubus_defer_post(struct client *cl)
722 {
723 struct dispatch *d = &cl->dispatch;
724
725 d->ubus.jstok = json_tokener_new();
726 if (d->ubus.jstok)
727 return ops->client_error(cl, 500, "Internal Error", "Internal Error");
728
729 d->data_send = uh_ubus_data_send;
730 d->data_done = uh_ubus_data_done;
731 }
732
733 static void uh_ubus_handle_request(struct client *cl, char *url, struct path_info *pi)
734 {
735 struct uh_ubus_session_acl *acl;
736 struct uh_ubus_session *ses;
737 struct dispatch *d = &cl->dispatch;
738 char *sid, *obj, *fun;
739 bool access = false;
740
741 blob_buf_init(&buf, 0);
742
743 if (!uh_ubus_request_parse_url(cl, url, &sid, &obj, &fun))
744 return ops->client_error(cl, 400, "Bad Request", "Invalid Request");
745
746 ses = uh_ubus_session_get(sid);
747 if (!ses)
748 return ops->client_error(cl, 404, "Not Found", "No such session %s", sid);
749
750 uh_foreach_matching_acl(acl, ses, obj, fun) {
751 access = true;
752 break;
753 }
754
755 if (!access)
756 return ops->client_error(cl, 403, "Denied", "Access to object denied");
757
758 if (ubus_lookup_id(ctx, obj, &d->ubus.obj))
759 return ops->client_error(cl, 500, "Not Found", "No such object");
760
761 d->close_fds = uh_ubus_close_fds;
762 d->free = uh_ubus_request_free;
763 d->ubus.func = fun;
764
765 if (cl->request.method == UH_HTTP_MSG_POST)
766 uh_ubus_defer_post(cl);
767 else
768 uh_ubus_send_request(cl, NULL);
769 }
770
771 static bool
772 uh_ubus_check_url(const char *url)
773 {
774 return ops->path_match(conf.ubus_prefix, url);
775 }
776
777 static int
778 uh_ubus_init(void)
779 {
780 static struct dispatch_handler ubus_dispatch = {
781 .check_url = uh_ubus_check_url,
782 .handle_request = uh_ubus_handle_request,
783 };
784
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),
794 };
795
796 static struct ubus_object_type session_type =
797 UBUS_OBJECT_TYPE("uhttpd", session_methods);
798
799 static struct ubus_object obj = {
800 .name = "session",
801 .type = &session_type,
802 .methods = session_methods,
803 .n_methods = ARRAY_SIZE(session_methods),
804 };
805
806 int ret;
807
808 ctx = ubus_connect(conf.ubus_socket);
809 if (!ctx) {
810 fprintf(stderr, "Unable to connect to ubus socket\n");
811 exit(1);
812 }
813
814 ret = ubus_add_object(ctx, &obj);
815 if (ret) {
816 fprintf(stderr, "Unable to publish ubus object: %s\n",
817 ubus_strerror(ret));
818 exit(1);
819 }
820
821 avl_init(&sessions, avl_strcmp, false, NULL);
822 ops->dispatch_add(&ubus_dispatch);
823
824 uloop_done();
825 return 0;
826 }
827
828
829 static int uh_ubus_plugin_init(const struct uhttpd_ops *o, struct config *c)
830 {
831 ops = o;
832 _conf = c;
833 return uh_ubus_init();
834 }
835
836 static void uh_ubus_post_init(void)
837 {
838 ubus_add_uloop(ctx);
839 }
840
841 const struct uhttpd_plugin uhttpd_plugin = {
842 .init = uh_ubus_plugin_init,
843 .post_init = uh_ubus_post_init,
844 };