ubusd_acl: event send access list support
[project/ubus.git] / ubusd_event.c
index 87213b3d7c2e36b620624adaaff28eab72d63011..712e7049946a88acba0207ce72ed0fc78653ec05 100644 (file)
@@ -1,3 +1,16 @@
+/*
+ * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
 #include <arpa/inet.h>
 #include "ubusd.h"
 
@@ -46,7 +59,7 @@ static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *m
        struct event_source *ev;
        struct ubus_object *obj;
        struct blob_attr *attr[EVREG_LAST];
-       char *pattern;
+       char *pattern, *name;
        uint32_t id;
        bool partial = false;
        int len;
@@ -75,6 +88,9 @@ static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *m
                len--;
        }
 
+       if (pattern[0] && ubusd_acl_check(cl, pattern, NULL, UBUS_ACL_LISTEN))
+               return UBUS_STATUS_PERMISSION_DENIED;
+
        ev = calloc(1, sizeof(*ev) + len + 1);
        if (!ev)
                return UBUS_STATUS_NO_DATA;
@@ -82,15 +98,14 @@ static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *m
        list_add(&ev->list, &obj->events);
        ev->obj = obj;
        ev->partial = partial;
-       ev->avl.key = (void *) (ev + 1);
-       strcpy(ev->avl.key, pattern);
+       name = (char *) (ev + 1);
+       strcpy(name, pattern);
+       ev->avl.key = name;
        avl_insert(&patterns, &ev->avl);
 
        return 0;
 }
 
-typedef struct ubus_msg_buf *(*event_fill_cb)(void *priv, const char *id);
-
 static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_client *cl,
                                 struct ubus_object *obj, const char *id,
                                 event_fill_cb fill_cb, void *cb_priv)
@@ -117,25 +132,19 @@ static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_client *c
        *objid_ptr = htonl(obj->id.id);
 
        (*ub)->hdr.seq = ++event_seq;
-       ubus_msg_send(obj->client, *ub, false);
-}
-
-bool strmatch_len(const char *s1, const char *s2, int *len)
-{
-       for (*len = 0; s1[*len] == s2[*len]; (*len)++)
-               if (!s1[*len])
-                       return true;
-
-       return false;
+       ubus_msg_send(obj->client, *ub);
 }
 
-static int ubusd_send_event(struct ubus_client *cl, const char *id,
-                           event_fill_cb fill_cb, void *cb_priv)
+int ubusd_send_event(struct ubus_client *cl, const char *id,
+                    event_fill_cb fill_cb, void *cb_priv)
 {
        struct ubus_msg_buf *ub = NULL;
        struct event_source *ev;
        int match_len = 0;
 
+       if (ubusd_acl_check(cl, id, NULL, UBUS_ACL_SEND))
+               return UBUS_STATUS_PERMISSION_DENIED;
+
        obj_event_seq++;
 
        /*
@@ -148,7 +157,7 @@ static int ubusd_send_event(struct ubus_client *cl, const char *id,
                int cur_match_len;
                bool full_match;
 
-               full_match = strmatch_len(id, key, &cur_match_len);
+               full_match = ubus_strmatch_len(id, key, &cur_match_len);
                if (cur_match_len < match_len)
                        break;
 
@@ -214,7 +223,7 @@ static int ubusd_forward_event(struct ubus_client *cl, struct blob_attr *msg)
        return ubusd_send_event(cl, id, ubusd_create_event_from_msg, data);
 }
 
-static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
+static int ubusd_event_recv(struct ubus_client *cl, struct ubus_msg_buf *ub, const char *method, struct blob_attr *msg)
 {
        if (!strcmp(method, "register"))
                return ubusd_alloc_event_pattern(cl, msg);
@@ -253,6 +262,7 @@ void ubusd_event_init(void)
 {
        ubus_init_string_tree(&patterns, true);
        event_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_EVENT);
-       event_obj->recv_msg = ubusd_event_recv;
+       if (event_obj != NULL)
+               event_obj->recv_msg = ubusd_event_recv;
 }