Ubus
OpenWrt system message/RPC bus.
libubus-acl.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 John Cripin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <unistd.h>
15 
16 #include <libubox/blob.h>
17 #include <libubox/blobmsg.h>
18 
19 #include "libubus.h"
20 #include <libubox/avl-cmp.h>
21 
22 static struct ubus_event_handler acl_event;
23 static struct ubus_request acl_req;
24 static struct blob_attr *acl_blob;
25 
26 static int acl_cmp(const void *k1, const void *k2, void *ptr)
27 {
28  const struct ubus_acl_key *key1 = k1;
29  const struct ubus_acl_key *key2 = k2;
30  int ret = 0;
31 
32  if (key1->user && key2->user)
33  ret = strcmp(key1->user, key2->user);
34  if (ret)
35  return ret;
36 
37  if (key1->group && key2->group)
38  ret = strcmp(key1->group, key2->group);
39  if (ret)
40  return ret;
41 
42  return strcmp(key1->object, key2->object);
43 }
44 
46 
47 enum {
53 };
54 
55 static const struct blobmsg_policy acl_obj_policy[__ACL_OBJ_MAX] = {
56  [ACL_OBJ_OBJECT] = { .name = "obj", .type = BLOBMSG_TYPE_STRING },
57  [ACL_OBJ_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
58  [ACL_OBJ_GROUP] = { .name = "group", .type = BLOBMSG_TYPE_STRING },
59  [ACL_OBJ_ACL] = { .name = "acl", .type = BLOBMSG_TYPE_TABLE },
60 };
61 
62 static void
63 acl_add(struct blob_attr *obj)
64 {
65  struct blob_attr *tb[__ACL_OBJ_MAX];
66  struct acl_object *acl;
67 
68  blobmsg_parse(acl_obj_policy, __ACL_OBJ_MAX, tb, blobmsg_data(obj),
69  blobmsg_data_len(obj));
70 
71  if (!tb[ACL_OBJ_OBJECT] || !tb[ACL_OBJ_ACL])
72  return;
73 
74  if (!tb[ACL_OBJ_USER] && !tb[ACL_OBJ_GROUP])
75  return;
76 
77  acl = calloc(1, sizeof(*acl));
78  if (!acl)
79  return;
80 
81  acl->avl.key = &acl->key;
82  acl->key.object = blobmsg_get_string(tb[ACL_OBJ_OBJECT]);
83  acl->key.user = blobmsg_get_string(tb[ACL_OBJ_USER]);
84  acl->key.group = blobmsg_get_string(tb[ACL_OBJ_GROUP]);
85  acl->acl = tb[ACL_OBJ_ACL];
86  avl_insert(&acl_objects, &acl->avl);
87 }
88 
89 enum {
93 };
94 
95 static const struct blobmsg_policy acl_policy[__ACL_POLICY_MAX] = {
96  [ACL_POLICY_SEQ] = { .name = "seq", .type = BLOBMSG_TYPE_INT32 },
97  [ACL_POLICY_ACL] = { .name = "acl", .type = BLOBMSG_TYPE_ARRAY },
98 };
99 
100 static void acl_recv_cb(struct ubus_request *req,
101  int type, struct blob_attr *msg)
102 {
103  struct blob_attr *tb[__ACL_POLICY_MAX];
104  struct blob_attr *cur;
105  size_t rem;
106 
107  if (acl_blob) {
108  struct acl_object *p, *q;
109 
110  avl_for_each_element_safe(&acl_objects, p, avl, q) {
111  avl_delete(&acl_objects, &p->avl);
112  free(p);
113  }
114  free(acl_blob);
115  }
116  acl_blob = blob_memdup(msg);
117  blobmsg_parse(acl_policy, __ACL_POLICY_MAX, tb, blobmsg_data(msg),
118  blobmsg_data_len(msg));
119 
120  if (!tb[ACL_POLICY_SEQ] && !tb[ACL_POLICY_ACL])
121  return;
122 
123  blobmsg_for_each_attr(cur, tb[ACL_POLICY_ACL], rem)
124  acl_add(cur);
125 }
126 
127 static void acl_query(struct ubus_context *ctx)
128 {
132 }
133 
134 static void acl_subscribe_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
135  const char *type, struct blob_attr *msg)
136 {
137  if (strcmp(type, "ubus.acl.sequence"))
138  return;
139  acl_query(ctx);
140 }
141 
143 {
144  int ret;
145 
147 
148  ret = ubus_register_event_handler(ctx, &acl_event, "ubus.acl.sequence");
149  if (!ret)
150  acl_query(ctx);
151 
152  return ret;
153 }
static struct ubus_context * ctx
Definition: client.c:22
static void acl_add(struct blob_attr *obj)
Definition: libubus-acl.c:63
static struct ubus_event_handler acl_event
Definition: libubus-acl.c:22
static const struct blobmsg_policy acl_obj_policy[__ACL_OBJ_MAX]
Definition: libubus-acl.c:55
static const struct blobmsg_policy acl_policy[__ACL_POLICY_MAX]
Definition: libubus-acl.c:95
@ __ACL_OBJ_MAX
Definition: libubus-acl.c:52
@ ACL_OBJ_ACL
Definition: libubus-acl.c:51
@ ACL_OBJ_USER
Definition: libubus-acl.c:49
@ ACL_OBJ_GROUP
Definition: libubus-acl.c:50
@ ACL_OBJ_OBJECT
Definition: libubus-acl.c:48
@ __ACL_POLICY_MAX
Definition: libubus-acl.c:92
@ ACL_POLICY_ACL
Definition: libubus-acl.c:91
@ ACL_POLICY_SEQ
Definition: libubus-acl.c:90
int ubus_register_acl(struct ubus_context *ctx)
Definition: libubus-acl.c:142
static void acl_subscribe_cb(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg)
Definition: libubus-acl.c:134
static struct blob_attr * acl_blob
Definition: libubus-acl.c:24
static void acl_recv_cb(struct ubus_request *req, int type, struct blob_attr *msg)
Definition: libubus-acl.c:100
static void acl_query(struct ubus_context *ctx)
Definition: libubus-acl.c:127
static struct ubus_request acl_req
Definition: libubus-acl.c:23
AVL_TREE(acl_objects, acl_cmp, true, NULL)
static int acl_cmp(const void *k1, const void *k2, void *ptr)
Definition: libubus-acl.c:26
void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
Definition: libubus-req.c:90
int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *pattern)
Definition: libubus.c:225
struct avl_tree acl_objects
static int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, struct ubus_request *req)
Definition: libubus.h:365
struct blob_attr * acl
Definition: libubus.h:337
struct avl_node avl
Definition: libubus.h:336
const char * group
Definition: libubus.h:190
const char * object
Definition: libubus.h:191
const char * user
Definition: libubus.h:189
ubus_event_handler_t cb
Definition: libubus.h:154
ubus_data_handler_t data_cb
Definition: libubus.h:221
uint8_t type
Definition: ubusmsg.h:1
#define UBUS_SYSTEM_OBJECT_ACL
Definition: ubusmsg.h:25