ubusd: simplify/fix avl loop in ubusd_reply_add()
[project/ubus.git] / ubusd_acl.c
1 /*
2 * Copyright (C) 2015 John Crispin <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 #define _GNU_SOURCE
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18
19 #include <syslog.h>
20 #include <unistd.h>
21 #include <glob.h>
22 #include <grp.h>
23 #include <pwd.h>
24
25 #include <libubox/vlist.h>
26 #include <libubox/blobmsg_json.h>
27 #include <libubox/avl-cmp.h>
28
29 #include "ubusd.h"
30
31 #ifndef SO_PEERCRED
32 struct ucred {
33 int pid;
34 int uid;
35 int gid;
36 };
37 #endif
38
39 struct ubusd_acl_obj {
40 struct avl_node avl;
41 struct list_head list;
42
43 const char *user;
44 const char *group;
45
46 struct blob_attr *methods;
47 struct blob_attr *tags;
48 struct blob_attr *priv;
49 bool subscribe;
50 bool publish;
51 };
52
53 struct ubusd_acl_file {
54 struct vlist_node avl;
55
56 const char *user;
57 const char *group;
58
59 struct blob_attr *blob;
60 struct list_head acl;
61
62 int ok;
63 };
64
65 const char *ubusd_acl_dir = "/usr/share/acl.d";
66 static struct blob_buf bbuf;
67 static struct avl_tree ubusd_acls;
68 static int ubusd_acl_seq;
69 static struct ubus_object *acl_obj;
70
71 static int
72 ubusd_acl_match_path(const void *k1, const void *k2, void *ptr)
73 {
74 const char *name = k1;
75 const char *match = k2;
76 char *wildcard = strstr(match, "\t");
77
78 if (wildcard)
79 return strncmp(name, match, wildcard - match);
80
81 return strcmp(name, match);
82 }
83
84 static int
85 ubusd_acl_match_cred(struct ubus_client *cl, struct ubusd_acl_obj *obj)
86 {
87 if (obj->user && !strcmp(cl->user, obj->user))
88 return 0;
89
90 if (obj->group && !strcmp(cl->group, obj->group))
91 return 0;
92
93 return -1;
94 }
95
96 int
97 ubusd_acl_check(struct ubus_client *cl, const char *obj,
98 const char *method, enum ubusd_acl_type type)
99 {
100 struct ubusd_acl_obj *acl;
101 struct blob_attr *cur;
102 int rem;
103
104 if (!cl->gid && !cl->uid)
105 return 0;
106
107 acl = avl_find_ge_element(&ubusd_acls, obj, acl, avl);
108 avl_for_element_to_last(&ubusd_acls, acl, acl, avl) {
109 int diff = ubusd_acl_match_path(obj, acl->avl.key, NULL);
110
111 if (diff)
112 break;
113
114 if (ubusd_acl_match_cred(cl, acl))
115 continue;
116
117 switch (type) {
118 case UBUS_ACL_PUBLISH:
119 if (acl->publish)
120 return 0;
121 break;
122
123 case UBUS_ACL_SUBSCRIBE:
124 if (acl->subscribe)
125 return 0;
126 break;
127
128 case UBUS_ACL_ACCESS:
129 if (acl->methods)
130 blobmsg_for_each_attr(cur, acl->methods, rem)
131 if (blobmsg_type(cur) == BLOBMSG_TYPE_STRING)
132 if (!ubusd_acl_match_path(method, blobmsg_get_string(cur), NULL))
133 return 0;
134 break;
135 }
136 }
137
138 return -1;
139 }
140
141 int
142 ubusd_acl_init_client(struct ubus_client *cl, int fd)
143 {
144 struct ucred cred;
145 struct passwd *pwd;
146 struct group *group;
147
148 #ifdef SO_PEERCRED
149 unsigned int len = sizeof(struct ucred);
150
151 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1)
152 return -1;
153 #else
154 memset(&cred, 0, sizeof(cred));
155 #endif
156
157 pwd = getpwuid(cred.uid);
158 if (!pwd)
159 return -1;
160
161 group = getgrgid(cred.gid);
162 if (!group)
163 return -1;
164
165 cl->uid = cred.uid;
166 cl->gid = cred.gid;
167
168 cl->group = strdup(group->gr_name);
169 cl->user = strdup(pwd->pw_name);
170
171 return 0;
172 }
173
174 static void
175 ubusd_acl_file_free(struct ubusd_acl_file *file)
176 {
177 struct ubusd_acl_obj *p, *q;
178
179 list_for_each_entry_safe(p, q, &file->acl, list) {
180 avl_delete(&ubusd_acls, &p->avl);
181 list_del(&p->list);
182 free(p);
183 }
184
185 free(file);
186 }
187
188 enum {
189 ACL_ACCESS_METHODS,
190 ACL_ACCESS_TAGS,
191 ACL_ACCESS_PRIV,
192 __ACL_ACCESS_MAX
193 };
194
195 static const struct blobmsg_policy acl_obj_policy[__ACL_ACCESS_MAX] = {
196 [ACL_ACCESS_METHODS] = { .name = "methods", .type = BLOBMSG_TYPE_ARRAY },
197 [ACL_ACCESS_TAGS] = { .name = "tags", .type = BLOBMSG_TYPE_ARRAY },
198 [ACL_ACCESS_PRIV] = { .name = "acl", .type = BLOBMSG_TYPE_TABLE },
199 };
200
201 static struct ubusd_acl_obj*
202 ubusd_acl_alloc_obj(struct ubusd_acl_file *file, const char *obj)
203 {
204 struct ubusd_acl_obj *o;
205 char *k;
206
207 o = calloc_a(sizeof(*o), &k, strlen(obj) + 1);
208 o->user = file->user;
209 o->group = file->group;
210 o->avl.key = k;
211 strcpy(k, obj);
212
213 while (*k) {
214 if (*k == '*')
215 *k = '\t';
216 k++;
217 }
218
219 list_add(&o->list, &file->acl);
220 avl_insert(&ubusd_acls, &o->avl);
221
222 return o;
223 }
224
225 static void
226 ubusd_acl_add_access(struct ubusd_acl_file *file, struct blob_attr *obj)
227 {
228 struct blob_attr *tb[__ACL_ACCESS_MAX];
229 struct ubusd_acl_obj *o;
230
231 blobmsg_parse(acl_obj_policy, __ACL_ACCESS_MAX, tb, blobmsg_data(obj),
232 blobmsg_data_len(obj));
233
234 if (!tb[ACL_ACCESS_METHODS] && !tb[ACL_ACCESS_TAGS] && !tb[ACL_ACCESS_PRIV])
235 return;
236
237 o = ubusd_acl_alloc_obj(file, blobmsg_name(obj));
238
239 o->methods = tb[ACL_ACCESS_METHODS];
240 o->tags = tb[ACL_ACCESS_TAGS];
241 o->priv = tb[ACL_ACCESS_PRIV];
242
243 if (file->user || file->group)
244 file->ok = 1;
245 }
246
247 static void
248 ubusd_acl_add_subscribe(struct ubusd_acl_file *file, const char *obj)
249 {
250 struct ubusd_acl_obj *o = ubusd_acl_alloc_obj(file, obj);
251
252 o->subscribe = true;
253 }
254
255 static void
256 ubusd_acl_add_publish(struct ubusd_acl_file *file, const char *obj)
257 {
258 struct ubusd_acl_obj *o = ubusd_acl_alloc_obj(file, obj);
259
260 o->publish = true;
261 }
262
263 enum {
264 ACL_USER,
265 ACL_GROUP,
266 ACL_ACCESS,
267 ACL_PUBLISH,
268 ACL_SUBSCRIBE,
269 ACL_INHERIT,
270 __ACL_MAX
271 };
272
273 static const struct blobmsg_policy acl_policy[__ACL_MAX] = {
274 [ACL_USER] = { .name = "user", .type = BLOBMSG_TYPE_STRING },
275 [ACL_GROUP] = { .name = "group", .type = BLOBMSG_TYPE_STRING },
276 [ACL_ACCESS] = { .name = "access", .type = BLOBMSG_TYPE_TABLE },
277 [ACL_PUBLISH] = { .name = "publish", .type = BLOBMSG_TYPE_ARRAY },
278 [ACL_SUBSCRIBE] = { .name = "subscribe", .type = BLOBMSG_TYPE_ARRAY },
279 [ACL_INHERIT] = { .name = "inherit", .type = BLOBMSG_TYPE_ARRAY },
280 };
281
282 static void
283 ubusd_acl_file_add(struct ubusd_acl_file *file)
284 {
285 struct blob_attr *tb[__ACL_MAX], *cur;
286 int rem;
287
288 blobmsg_parse(acl_policy, __ACL_MAX, tb, blob_data(file->blob),
289 blob_len(file->blob));
290
291 if (tb[ACL_USER])
292 file->user = blobmsg_get_string(tb[ACL_USER]);
293 else if (tb[ACL_GROUP])
294 file->group = blobmsg_get_string(tb[ACL_GROUP]);
295 else
296 return;
297
298 if (tb[ACL_ACCESS])
299 blobmsg_for_each_attr(cur, tb[ACL_ACCESS], rem)
300 ubusd_acl_add_access(file, cur);
301
302 if (tb[ACL_SUBSCRIBE])
303 blobmsg_for_each_attr(cur, tb[ACL_SUBSCRIBE], rem)
304 if (blobmsg_type(cur) == BLOBMSG_TYPE_STRING)
305 ubusd_acl_add_subscribe(file, blobmsg_get_string(cur));
306
307 if (tb[ACL_PUBLISH])
308 blobmsg_for_each_attr(cur, tb[ACL_PUBLISH], rem)
309 if (blobmsg_type(cur) == BLOBMSG_TYPE_STRING)
310 ubusd_acl_add_publish(file, blobmsg_get_string(cur));
311 }
312
313 static void
314 ubusd_acl_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
315 struct vlist_node *node_old)
316 {
317 struct ubusd_acl_file *file;
318
319 if (node_old) {
320 file = container_of(node_old, struct ubusd_acl_file, avl);
321 ubusd_acl_file_free(file);
322 }
323
324 if (node_new) {
325 file = container_of(node_new, struct ubusd_acl_file, avl);
326 ubusd_acl_file_add(file);
327 }
328 }
329
330 static struct ubus_msg_buf *
331 ubusd_create_sequence_event_msg(void *priv, const char *id)
332 {
333 void *s;
334
335 blob_buf_init(&b, 0);
336 blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
337 blob_put_string(&b, UBUS_ATTR_METHOD, id);
338 s = blob_nest_start(&b, UBUS_ATTR_DATA);
339 blobmsg_add_u32(&b, "sequence", ubusd_acl_seq);
340 blob_nest_end(&b, s);
341
342 return ubus_msg_new(b.head, blob_raw_len(b.head), true);
343 }
344
345 static VLIST_TREE(ubusd_acl_files, avl_strcmp, ubusd_acl_update_cb, false, false);
346
347 static int
348 ubusd_acl_load_file(const char *filename)
349 {
350 struct ubusd_acl_file *file;
351 void *blob;
352
353 blob_buf_init(&bbuf, 0);
354 if (!blobmsg_add_json_from_file(&bbuf, filename)) {
355 syslog(LOG_ERR, "failed to parse %s\n", filename);
356 return -1;
357 }
358
359 file = calloc_a(sizeof(*file), &blob, blob_raw_len(bbuf.head));
360 if (!file)
361 return -1;
362
363 file->blob = blob;
364
365 memcpy(blob, bbuf.head, blob_raw_len(bbuf.head));
366 INIT_LIST_HEAD(&file->acl);
367
368 vlist_add(&ubusd_acl_files, &file->avl, filename);
369 syslog(LOG_INFO, "loading %s\n", filename);
370
371 return 0;
372 }
373
374 void
375 ubusd_acl_load(void)
376 {
377 struct stat st;
378 glob_t gl;
379 int j;
380 const char *suffix = "/*.json";
381 char *path = alloca(strlen(ubusd_acl_dir) + strlen(suffix) + 1);
382
383 sprintf(path, "%s%s", ubusd_acl_dir, suffix);
384 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
385 return;
386
387 vlist_update(&ubusd_acl_files);
388 for (j = 0; j < gl.gl_pathc; j++) {
389 if (stat(gl.gl_pathv[j], &st) || !S_ISREG(st.st_mode))
390 continue;
391
392 if (st.st_uid || st.st_gid) {
393 syslog(LOG_ERR, "%s has wrong owner\n", gl.gl_pathv[j]);
394 continue;
395 }
396 if (st.st_mode & (S_IWOTH | S_IWGRP | S_IXOTH)) {
397 syslog(LOG_ERR, "%s has wrong permissions\n", gl.gl_pathv[j]);
398 continue;
399 }
400 ubusd_acl_load_file(gl.gl_pathv[j]);
401 }
402
403 globfree(&gl);
404 vlist_flush(&ubusd_acl_files);
405 ubusd_acl_seq++;
406 ubusd_send_event(NULL, "ubus.acl.sequence", ubusd_create_sequence_event_msg, NULL);
407 }
408
409 static void
410 ubusd_reply_add(struct ubus_object *obj)
411 {
412 struct ubusd_acl_obj *acl;
413
414 if (!obj->path.key)
415 return;
416
417 acl = avl_find_ge_element(&ubusd_acls, obj->path.key, acl, avl);
418 avl_for_element_to_last(&ubusd_acls, acl, acl, avl) {
419 void *c;
420
421 if (!acl->priv)
422 continue;
423
424 if (!ubusd_acl_match_path(obj->path.key, acl->avl.key, NULL))
425 continue;
426
427 c = blobmsg_open_table(&b, NULL);
428 blobmsg_add_string(&b, "obj", obj->path.key);
429 if (acl->user)
430 blobmsg_add_string(&b, "user", acl->user);
431 if (acl->group)
432 blobmsg_add_string(&b, "group", acl->group);
433
434 blobmsg_add_field(&b, blobmsg_type(acl->priv), "acl",
435 blobmsg_data(acl->priv), blobmsg_data_len(acl->priv));
436
437 blobmsg_close_table(&b, c);
438 }
439 }
440 static int ubusd_reply_query(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr, struct blob_attr *msg)
441 {
442 struct ubus_object *obj;
443 void *d, *a;
444
445 if (!attr[UBUS_ATTR_OBJID])
446 return UBUS_STATUS_INVALID_ARGUMENT;
447
448 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
449 if (!obj)
450 return UBUS_STATUS_NOT_FOUND;
451
452 blob_buf_init(&b, 0);
453 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
454 d = blob_nest_start(&b, UBUS_ATTR_DATA);
455
456 blobmsg_add_u32(&b, "seq", ubusd_acl_seq);
457 a = blobmsg_open_array(&b, "acl");
458 list_for_each_entry(obj, &cl->objects, list)
459 ubusd_reply_add(obj);
460 blobmsg_close_table(&b, a);
461
462 blob_nest_end(&b, d);
463
464 ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
465
466 return 0;
467 }
468
469 static int ubusd_acl_recv(struct ubus_client *cl, struct ubus_msg_buf *ub, const char *method, struct blob_attr *msg)
470 {
471 if (!strcmp(method, "query"))
472 return ubusd_reply_query(cl, ub, ubus_parse_msg(ub->data), msg);
473
474 return UBUS_STATUS_INVALID_COMMAND;
475 }
476
477 void ubusd_acl_init(void)
478 {
479 avl_init(&ubusd_acls, ubusd_acl_match_path, true, NULL);
480 acl_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_ACL);
481 acl_obj->recv_msg = ubusd_acl_recv;
482 }