+ uh_ubus_send_response(cl, &buf);
+}
+
+static void uh_ubus_error(struct client *cl, int code, const char *message)
+{
+ blob_buf_init(&buf, 0);
+
+ blobmsg_add_u32(&buf, "code", code);
+ blobmsg_add_string(&buf, "message", message);
+ uh_ubus_send_response(cl, &buf);
+}
+
+static void uh_ubus_posix_error(struct client *cl, int err)
+{
+ uh_ubus_error(cl, -err, strerror(err));
+}
+
+static void uh_ubus_ubus_error(struct client *cl, int err)
+{
+ uh_ubus_error(cl, err, ubus_strerror(err));
+}
+
+static void uh_ubus_allowed_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+ struct blob_attr *tb[__SES_MAX];
+ bool *allow = (bool *)req->priv;
+
+ if (!msg)
+ return;
+
+ blobmsg_parse(ses_policy, __SES_MAX, tb, blob_data(msg), blob_len(msg));
+
+ if (tb[SES_ACCESS])
+ *allow = blobmsg_get_bool(tb[SES_ACCESS]);
+}
+
+static bool uh_ubus_allowed(const char *sid, const char *obj, const char *fun)
+{
+ uint32_t id;
+ bool allow = false;
+ static struct blob_buf req;
+
+ if (ubus_lookup_id(ctx, "session", &id))
+ return false;
+
+ blob_buf_init(&req, 0);
+ blobmsg_add_string(&req, "ubus_rpc_session", sid);
+ blobmsg_add_string(&req, "object", obj);
+ blobmsg_add_string(&req, "function", fun);
+
+ ubus_invoke(ctx, id, "access", req.head, uh_ubus_allowed_cb, &allow, conf.script_timeout * 500);
+
+ return allow;
+}
+
+/* GET requests handling */
+
+static void uh_ubus_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv);
+
+static void uh_ubus_handle_get_list(struct client *cl, const char *path)
+{
+ static struct blob_buf tmp;
+ struct list_data data = { .verbose = true, .add_object = !path, .buf = &tmp};
+ struct blob_attr *cur;
+ int rem;
+ int err;
+
+ blob_buf_init(&tmp, 0);
+
+ err = ubus_lookup(ctx, path, uh_ubus_list_cb, &data);
+ if (err) {
+ uh_ubus_send_header(cl, 500, "Ubus Protocol Error", "application/json");
+ uh_ubus_ubus_error(cl, err);
+ return;
+ }
+
+ blob_buf_init(&buf, 0);
+ blob_for_each_attr(cur, tmp.head, rem)
+ blobmsg_add_blob(&buf, cur);
+
+ uh_ubus_send_header(cl, 200, "OK", "application/json");
+ uh_ubus_send_response(cl, &buf);
+}
+
+static int uh_ubus_subscription_notification_cb(struct ubus_context *ctx,
+ struct ubus_object *obj,
+ struct ubus_request_data *req,
+ const char *method,
+ struct blob_attr *msg)
+{
+ struct ubus_subscriber *s;
+ struct dispatch_ubus *du;
+ struct client *cl;
+ char *json;
+
+ s = container_of(obj, struct ubus_subscriber, obj);
+ du = container_of(s, struct dispatch_ubus, sub);
+ cl = container_of(du, struct client, dispatch.ubus);
+
+ json = blobmsg_format_json(msg, true);
+ if (json) {
+ ops->chunk_printf(cl, "event: %s\ndata: %s\n\n", method, json);
+ free(json);
+ }
+
+ return 0;
+}
+
+static void uh_ubus_subscription_notification_remove_cb(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id)
+{
+ struct dispatch_ubus *du;
+ struct client *cl;
+
+ du = container_of(s, struct dispatch_ubus, sub);
+ cl = container_of(du, struct client, dispatch.ubus);
+
+ ubus_unregister_subscriber(ctx, &du->sub);
+
+ ops->request_done(cl);
+}
+
+static void uh_ubus_handle_get_subscribe(struct client *cl, const char *path)
+{
+ struct dispatch_ubus *du = &cl->dispatch.ubus;
+ const char *sid;
+ uint32_t id;
+ int err;
+
+ sid = uh_ubus_get_auth(cl->hdr.head);
+
+ if (!conf.ubus_noauth && !uh_ubus_allowed(sid, path, ":subscribe")) {
+ uh_ubus_send_header(cl, 200, "OK", "application/json");
+ uh_ubus_posix_error(cl, EACCES);
+ return;
+ }
+
+ du->sub.cb = uh_ubus_subscription_notification_cb;
+ du->sub.remove_cb = uh_ubus_subscription_notification_remove_cb;
+
+ uh_client_ref(cl);
+
+ err = ubus_register_subscriber(ctx, &du->sub);
+ if (err)
+ goto err_unref;
+
+ err = ubus_lookup_id(ctx, path, &id);
+ if (err)
+ goto err_unregister;
+
+ err = ubus_subscribe(ctx, &du->sub, id);
+ if (err)
+ goto err_unregister;
+
+ uh_ubus_send_header(cl, 200, "OK", "text/event-stream");
+
+ if (conf.events_retry)
+ ops->chunk_printf(cl, "retry: %d\n", conf.events_retry);
+
+ return;
+
+err_unregister:
+ ubus_unregister_subscriber(ctx, &du->sub);
+err_unref:
+ uh_client_unref(cl);
+ if (err) {
+ uh_ubus_send_header(cl, 200, "OK", "application/json");
+ uh_ubus_ubus_error(cl, err);
+ }
+}
+
+static void uh_ubus_handle_get(struct client *cl)
+{
+ struct dispatch_ubus *du = &cl->dispatch.ubus;
+ const char *url = du->url_path;
+
+ url += strlen(conf.ubus_prefix);
+
+ if (!strcmp(url, "/list") || !strncmp(url, "/list/", strlen("/list/"))) {
+ url += strlen("/list");
+
+ uh_ubus_handle_get_list(cl, *url ? url + 1 : NULL);
+ } else if (!strncmp(url, "/subscribe/", strlen("/subscribe/"))) {
+ url += strlen("/subscribe");
+
+ uh_ubus_handle_get_subscribe(cl, url + 1);
+ } else {
+ ops->http_header(cl, 404, "Not Found");
+ ustream_printf(cl->us, "\r\n");
+ ops->request_done(cl);
+ }