2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <sys/socket.h>
17 #include <sys/types.h>
26 #include <libubox/vlist.h>
27 #include <libubox/blobmsg_json.h>
28 #include <libubox/avl-cmp.h>
40 struct ubusd_acl_obj
{
42 struct list_head list
;
49 struct blob_attr
*methods
;
50 struct blob_attr
*tags
;
51 struct blob_attr
*priv
;
58 struct ubusd_acl_file
{
59 struct vlist_node avl
;
64 struct blob_attr
*blob
;
70 const char *ubusd_acl_dir
= "/usr/share/acl.d";
71 static struct blob_buf bbuf
;
72 static struct avl_tree ubusd_acls
;
73 static int ubusd_acl_seq
;
74 static struct ubus_object
*acl_obj
;
77 ubusd_acl_match_cred(struct ubus_client
*cl
, struct ubusd_acl_obj
*obj
)
79 if (obj
->user
&& !strcmp(cl
->user
, obj
->user
))
82 if (obj
->group
&& !strcmp(cl
->group
, obj
->group
))
89 ubusd_acl_check(struct ubus_client
*cl
, const char *obj
,
90 const char *method
, enum ubusd_acl_type type
)
92 struct ubusd_acl_obj
*acl
;
95 if (!cl
|| !cl
->uid
|| !obj
)
99 * Since this tree is sorted alphabetically, we can only expect
100 * to find matching entries as long as the number of matching
101 * characters between the access list string and the object path
102 * is monotonically increasing.
104 avl_for_each_element(&ubusd_acls
, acl
, avl
) {
105 const char *key
= acl
->avl
.key
;
109 full_match
= ubus_strmatch_len(obj
, key
, &cur_match_len
);
110 if (cur_match_len
< match_len
)
113 match_len
= cur_match_len
;
119 if (match_len
!= (int) strlen(key
))
123 if (ubusd_acl_match_cred(cl
, acl
))
127 case UBUS_ACL_PUBLISH
:
132 case UBUS_ACL_SUBSCRIBE
:
137 case UBUS_ACL_LISTEN
:
147 case UBUS_ACL_ACCESS
:
149 struct blob_attr
*cur
;
153 blobmsg_for_each_attr(cur
, acl
->methods
, rem
)
154 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
) {
155 cur_method
= blobmsg_get_string(cur
);
157 if (!strcmp(method
, cur_method
) || !strcmp("*", cur_method
))
169 ubusd_acl_init_client(struct ubus_client
*cl
, int fd
)
176 unsigned int len
= sizeof(struct ucred
);
178 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &cred
, &len
) == -1)
181 memset(&cred
, 0, sizeof(cred
));
184 pwd
= getpwuid(cred
.uid
);
188 group
= getgrgid(cred
.gid
);
195 cl
->group
= strdup(group
->gr_name
);
196 cl
->user
= strdup(pwd
->pw_name
);
202 ubusd_acl_free_client(struct ubus_client
*cl
)
209 ubusd_acl_file_free(struct ubusd_acl_file
*file
)
211 struct ubusd_acl_obj
*p
, *q
;
213 list_for_each_entry_safe(p
, q
, &file
->acl
, list
) {
214 avl_delete(&ubusd_acls
, &p
->avl
);
229 static const struct blobmsg_policy acl_obj_policy
[__ACL_ACCESS_MAX
] = {
230 [ACL_ACCESS_METHODS
] = { .name
= "methods", .type
= BLOBMSG_TYPE_ARRAY
},
231 [ACL_ACCESS_TAGS
] = { .name
= "tags", .type
= BLOBMSG_TYPE_ARRAY
},
232 [ACL_ACCESS_PRIV
] = { .name
= "acl", .type
= BLOBMSG_TYPE_TABLE
},
235 static struct ubusd_acl_obj
*
236 ubusd_acl_alloc_obj(struct ubusd_acl_file
*file
, const char *obj
)
238 struct ubusd_acl_obj
*o
;
239 int len
= strlen(obj
);
241 bool partial
= false;
243 if (obj
[len
- 1] == '*') {
248 o
= calloc_a(sizeof(*o
), &k
, len
+ 1);
249 o
->partial
= partial
;
250 o
->user
= file
->user
;
251 o
->group
= file
->group
;
252 o
->avl
.key
= memcpy(k
, obj
, len
);
254 list_add(&o
->list
, &file
->acl
);
255 avl_insert(&ubusd_acls
, &o
->avl
);
261 ubusd_acl_add_access(struct ubusd_acl_file
*file
, struct blob_attr
*obj
)
263 struct blob_attr
*tb
[__ACL_ACCESS_MAX
];
264 struct ubusd_acl_obj
*o
;
266 blobmsg_parse(acl_obj_policy
, __ACL_ACCESS_MAX
, tb
, blobmsg_data(obj
),
267 blobmsg_data_len(obj
));
269 if (!tb
[ACL_ACCESS_METHODS
] && !tb
[ACL_ACCESS_TAGS
] && !tb
[ACL_ACCESS_PRIV
])
272 o
= ubusd_acl_alloc_obj(file
, blobmsg_name(obj
));
274 o
->methods
= tb
[ACL_ACCESS_METHODS
];
275 o
->tags
= tb
[ACL_ACCESS_TAGS
];
276 o
->priv
= tb
[ACL_ACCESS_PRIV
];
278 if (file
->user
|| file
->group
)
283 ubusd_acl_add_subscribe(struct ubusd_acl_file
*file
, const char *obj
)
285 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
291 ubusd_acl_add_publish(struct ubusd_acl_file
*file
, const char *obj
)
293 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
298 static void ubusd_acl_add_listen(struct ubusd_acl_file
*file
, const char *obj
)
300 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
305 static void ubusd_acl_add_send(struct ubusd_acl_file
*file
, const char *obj
)
307 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
324 static const struct blobmsg_policy acl_policy
[__ACL_MAX
] = {
325 [ACL_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
326 [ACL_GROUP
] = { .name
= "group", .type
= BLOBMSG_TYPE_STRING
},
327 [ACL_ACCESS
] = { .name
= "access", .type
= BLOBMSG_TYPE_TABLE
},
328 [ACL_PUBLISH
] = { .name
= "publish", .type
= BLOBMSG_TYPE_ARRAY
},
329 [ACL_SUBSCRIBE
] = { .name
= "subscribe", .type
= BLOBMSG_TYPE_ARRAY
},
330 [ACL_INHERIT
] = { .name
= "inherit", .type
= BLOBMSG_TYPE_ARRAY
},
331 [ACL_LISTEN
] = { .name
= "listen", .type
= BLOBMSG_TYPE_ARRAY
},
332 [ACL_SEND
] = { .name
= "send", .type
= BLOBMSG_TYPE_ARRAY
},
336 ubusd_acl_file_add(struct ubusd_acl_file
*file
)
338 struct blob_attr
*tb
[__ACL_MAX
], *cur
;
341 blobmsg_parse(acl_policy
, __ACL_MAX
, tb
, blob_data(file
->blob
),
342 blob_len(file
->blob
));
345 file
->user
= blobmsg_get_string(tb
[ACL_USER
]);
346 else if (tb
[ACL_GROUP
])
347 file
->group
= blobmsg_get_string(tb
[ACL_GROUP
]);
352 blobmsg_for_each_attr(cur
, tb
[ACL_ACCESS
], rem
)
353 ubusd_acl_add_access(file
, cur
);
355 if (tb
[ACL_SUBSCRIBE
])
356 blobmsg_for_each_attr(cur
, tb
[ACL_SUBSCRIBE
], rem
)
357 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
358 ubusd_acl_add_subscribe(file
, blobmsg_get_string(cur
));
361 blobmsg_for_each_attr(cur
, tb
[ACL_PUBLISH
], rem
)
362 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
363 ubusd_acl_add_publish(file
, blobmsg_get_string(cur
));
366 blobmsg_for_each_attr(cur
, tb
[ACL_LISTEN
], rem
)
367 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
368 ubusd_acl_add_listen(file
, blobmsg_get_string(cur
));
371 blobmsg_for_each_attr(cur
, tb
[ACL_SEND
], rem
)
372 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
373 ubusd_acl_add_send(file
, blobmsg_get_string(cur
));
377 ubusd_acl_update_cb(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
378 struct vlist_node
*node_old
)
380 struct ubusd_acl_file
*file
;
383 file
= container_of(node_old
, struct ubusd_acl_file
, avl
);
384 ubusd_acl_file_free(file
);
388 file
= container_of(node_new
, struct ubusd_acl_file
, avl
);
389 ubusd_acl_file_add(file
);
393 static struct ubus_msg_buf
*
394 ubusd_create_sequence_event_msg(void *priv
, const char *id
)
398 blob_buf_init(&b
, 0);
399 blob_put_int32(&b
, UBUS_ATTR_OBJID
, 0);
400 blob_put_string(&b
, UBUS_ATTR_METHOD
, id
);
401 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
402 blobmsg_add_u32(&b
, "sequence", ubusd_acl_seq
);
403 blob_nest_end(&b
, s
);
405 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), true);
408 static VLIST_TREE(ubusd_acl_files
, avl_strcmp
, ubusd_acl_update_cb
, false, false);
411 ubusd_acl_load_file(const char *filename
)
413 struct ubusd_acl_file
*file
;
416 blob_buf_init(&bbuf
, 0);
417 if (!blobmsg_add_json_from_file(&bbuf
, filename
)) {
418 syslog(LOG_ERR
, "failed to parse %s\n", filename
);
422 file
= calloc_a(sizeof(*file
), &blob
, blob_raw_len(bbuf
.head
));
428 memcpy(blob
, bbuf
.head
, blob_raw_len(bbuf
.head
));
429 INIT_LIST_HEAD(&file
->acl
);
431 vlist_add(&ubusd_acl_files
, &file
->avl
, filename
);
432 syslog(LOG_INFO
, "loading %s\n", filename
);
443 const char *suffix
= "/*.json";
444 char *path
= alloca(strlen(ubusd_acl_dir
) + strlen(suffix
) + 1);
446 sprintf(path
, "%s%s", ubusd_acl_dir
, suffix
);
447 if (glob(path
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
))
450 vlist_update(&ubusd_acl_files
);
451 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
452 if (stat(gl
.gl_pathv
[j
], &st
) || !S_ISREG(st
.st_mode
))
455 if (st
.st_uid
|| st
.st_gid
) {
456 syslog(LOG_ERR
, "%s has wrong owner\n", gl
.gl_pathv
[j
]);
459 if (st
.st_mode
& (S_IWOTH
| S_IWGRP
| S_IXOTH
)) {
460 syslog(LOG_ERR
, "%s has wrong permissions\n", gl
.gl_pathv
[j
]);
463 ubusd_acl_load_file(gl
.gl_pathv
[j
]);
467 vlist_flush(&ubusd_acl_files
);
469 ubusd_send_event(NULL
, "ubus.acl.sequence", ubusd_create_sequence_event_msg
, NULL
);
473 ubusd_reply_add(struct ubus_object
*obj
)
475 struct ubusd_acl_obj
*acl
;
482 * Since this tree is sorted alphabetically, we can only expect
483 * to find matching entries as long as the number of matching
484 * characters between the access list string and the object path
485 * is monotonically increasing.
487 avl_for_each_element(&ubusd_acls
, acl
, avl
) {
488 const char *key
= acl
->avl
.key
;
496 full_match
= ubus_strmatch_len(obj
->path
.key
, key
, &cur_match_len
);
497 if (cur_match_len
< match_len
)
500 match_len
= cur_match_len
;
506 if (match_len
!= (int) strlen(key
))
510 c
= blobmsg_open_table(&b
, NULL
);
511 blobmsg_add_string(&b
, "obj", obj
->path
.key
);
513 blobmsg_add_string(&b
, "user", acl
->user
);
515 blobmsg_add_string(&b
, "group", acl
->group
);
517 blobmsg_add_field(&b
, blobmsg_type(acl
->priv
), "acl",
518 blobmsg_data(acl
->priv
), blobmsg_data_len(acl
->priv
));
520 blobmsg_close_table(&b
, c
);
524 static int ubusd_reply_query(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
, struct blob_attr
*msg
)
526 struct ubus_object
*obj
;
529 if (!attr
[UBUS_ATTR_OBJID
])
530 return UBUS_STATUS_INVALID_ARGUMENT
;
532 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
534 return UBUS_STATUS_NOT_FOUND
;
536 blob_buf_init(&b
, 0);
537 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
538 d
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
540 blobmsg_add_u32(&b
, "seq", ubusd_acl_seq
);
541 a
= blobmsg_open_array(&b
, "acl");
542 list_for_each_entry(obj
, &cl
->objects
, list
)
543 ubusd_reply_add(obj
);
544 blobmsg_close_table(&b
, a
);
546 blob_nest_end(&b
, d
);
548 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
553 static int ubusd_acl_recv(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, const char *method
, struct blob_attr
*msg
)
555 if (!strcmp(method
, "query"))
556 return ubusd_reply_query(cl
, ub
, ubus_parse_msg(ub
->data
, blob_raw_len(ub
->data
)), msg
);
558 return UBUS_STATUS_INVALID_COMMAND
;
561 void ubusd_acl_init(void)
563 ubus_init_string_tree(&ubusd_acls
, true);
564 acl_obj
= ubusd_create_object_internal(NULL
, UBUS_SYSTEM_OBJECT_ACL
);
565 acl_obj
->recv_msg
= ubusd_acl_recv
;