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