1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "string-util.h"
10 #include "user-util.h"
13 int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry) {
20 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
22 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
28 if (acl_get_tag_type(i, &tag) < 0)
34 u = acl_get_qualifier(i);
52 #if 0 /// UNNEEDED by elogind
53 int calc_acl_mask_if_needed(acl_t *acl_p) {
60 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
62 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
65 if (acl_get_tag_type(i, &tag) < 0)
71 if (IN_SET(tag, ACL_USER, ACL_GROUP))
77 if (need && acl_calc_mask(acl_p) < 0)
83 int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
86 bool have_user_obj = false, have_group_obj = false, have_other = false;
88 _cleanup_(acl_freep) acl_t basic = NULL;
92 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
94 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
97 if (acl_get_tag_type(i, &tag) < 0)
100 if (tag == ACL_USER_OBJ)
101 have_user_obj = true;
102 else if (tag == ACL_GROUP_OBJ)
103 have_group_obj = true;
104 else if (tag == ACL_OTHER)
106 if (have_user_obj && have_group_obj && have_other)
116 basic = acl_from_mode(st.st_mode);
120 for (r = acl_get_entry(basic, ACL_FIRST_ENTRY, &i);
122 r = acl_get_entry(basic, ACL_NEXT_ENTRY, &i)) {
126 if (acl_get_tag_type(i, &tag) < 0)
129 if ((tag == ACL_USER_OBJ && have_user_obj) ||
130 (tag == ACL_GROUP_OBJ && have_group_obj) ||
131 (tag == ACL_OTHER && have_other))
134 r = acl_create_entry(acl_p, &dst);
138 r = acl_copy_entry(dst, i);
147 int acl_search_groups(const char *path, char ***ret_groups) {
148 _cleanup_strv_free_ char **g = NULL;
149 _cleanup_(acl_freep) acl_t acl = NULL;
156 acl = acl_get_file(path, ACL_TYPE_DEFAULT);
160 r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
162 _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
170 if (acl_get_tag_type(entry, &tag) < 0)
173 if (tag != ACL_GROUP)
176 gid = acl_get_qualifier(entry);
180 if (in_gid(*gid) > 0) {
190 name = gid_to_name(*gid);
194 r = strv_consume(&g, name);
200 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
204 *ret_groups = TAKE_PTR(g);
209 int parse_acl(const char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
210 _cleanup_free_ char **a = NULL, **d = NULL; /* strings are not freed */
211 _cleanup_strv_free_ char **split;
214 _cleanup_(acl_freep) acl_t a_acl = NULL, d_acl = NULL;
216 split = strv_split(text, ",");
220 STRV_FOREACH(entry, split) {
223 p = startswith(*entry, "default:");
225 p = startswith(*entry, "d:");
228 r = strv_push(&d, p);
230 r = strv_push(&a, *entry);
235 if (!strv_isempty(a)) {
236 _cleanup_free_ char *join;
238 join = strv_join(a, ",");
242 a_acl = acl_from_text(join);
247 r = calc_acl_mask_if_needed(&a_acl);
253 if (!strv_isempty(d)) {
254 _cleanup_free_ char *join;
256 join = strv_join(d, ",");
260 d_acl = acl_from_text(join);
265 r = calc_acl_mask_if_needed(&d_acl);
271 *acl_access = TAKE_PTR(a_acl);
272 *acl_default = TAKE_PTR(d_acl);
277 static int acl_entry_equal(acl_entry_t a, acl_entry_t b) {
278 acl_tag_t tag_a, tag_b;
280 if (acl_get_tag_type(a, &tag_a) < 0)
283 if (acl_get_tag_type(b, &tag_b) < 0)
294 /* can have only one of those */
297 _cleanup_(acl_free_uid_tpp) uid_t *uid_a = NULL, *uid_b = NULL;
299 uid_a = acl_get_qualifier(a);
303 uid_b = acl_get_qualifier(b);
307 return *uid_a == *uid_b;
310 _cleanup_(acl_free_gid_tpp) gid_t *gid_a = NULL, *gid_b = NULL;
312 gid_a = acl_get_qualifier(a);
316 gid_b = acl_get_qualifier(b);
320 return *gid_a == *gid_b;
323 assert_not_reached("Unknown acl tag type");
327 static int find_acl_entry(acl_t acl, acl_entry_t entry, acl_entry_t *out) {
331 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
333 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
335 r = acl_entry_equal(i, entry);
348 int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl) {
349 _cleanup_(acl_freep) acl_t old;
353 old = acl_get_file(path, type);
357 for (r = acl_get_entry(new, ACL_FIRST_ENTRY, &i);
359 r = acl_get_entry(new, ACL_NEXT_ENTRY, &i)) {
363 r = find_acl_entry(old, i, &j);
367 if (acl_create_entry(&old, &j) < 0)
370 if (acl_copy_entry(j, i) < 0)
376 *acl = TAKE_PTR(old);
381 int add_acls_for_user(int fd, uid_t uid) {
382 _cleanup_(acl_freep) acl_t acl = NULL;
384 acl_permset_t permset;
387 acl = acl_get_fd(fd);
391 r = acl_find_uid(acl, uid, &entry);
393 if (acl_create_entry(&acl, &entry) < 0 ||
394 acl_set_tag_type(entry, ACL_USER) < 0 ||
395 acl_set_qualifier(entry, &uid) < 0)
399 /* We do not recalculate the mask unconditionally here,
400 * so that the fchmod() mask above stays intact. */
401 if (acl_get_permset(entry, &permset) < 0 ||
402 acl_add_perm(permset, ACL_READ) < 0)
405 r = calc_acl_mask_if_needed(&acl);
409 return acl_set_fd(fd, acl);