]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/acl-util.c
7f6bfde0de18e2a560ff0630aa6fce90e3b7ae92
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011,2013 Lennart Poettering
10 #include "alloc-util.h"
11 #include "string-util.h"
13 #include "user-util.h"
16 int acl_find_uid(acl_t acl
, uid_t uid
, acl_entry_t
*entry
) {
23 for (r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &i
);
25 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &i
)) {
31 if (acl_get_tag_type(i
, &tag
) < 0)
37 u
= acl_get_qualifier(i
);
55 int calc_acl_mask_if_needed(acl_t
*acl_p
) {
62 for (r
= acl_get_entry(*acl_p
, ACL_FIRST_ENTRY
, &i
);
64 r
= acl_get_entry(*acl_p
, ACL_NEXT_ENTRY
, &i
)) {
67 if (acl_get_tag_type(i
, &tag
) < 0)
73 if (IN_SET(tag
, ACL_USER
, ACL_GROUP
))
79 if (need
&& acl_calc_mask(acl_p
) < 0)
85 int add_base_acls_if_needed(acl_t
*acl_p
, const char *path
) {
88 bool have_user_obj
= false, have_group_obj
= false, have_other
= false;
90 _cleanup_(acl_freep
) acl_t basic
= NULL
;
94 for (r
= acl_get_entry(*acl_p
, ACL_FIRST_ENTRY
, &i
);
96 r
= acl_get_entry(*acl_p
, ACL_NEXT_ENTRY
, &i
)) {
99 if (acl_get_tag_type(i
, &tag
) < 0)
102 if (tag
== ACL_USER_OBJ
)
103 have_user_obj
= true;
104 else if (tag
== ACL_GROUP_OBJ
)
105 have_group_obj
= true;
106 else if (tag
== ACL_OTHER
)
108 if (have_user_obj
&& have_group_obj
&& have_other
)
118 basic
= acl_from_mode(st
.st_mode
);
122 for (r
= acl_get_entry(basic
, ACL_FIRST_ENTRY
, &i
);
124 r
= acl_get_entry(basic
, ACL_NEXT_ENTRY
, &i
)) {
128 if (acl_get_tag_type(i
, &tag
) < 0)
131 if ((tag
== ACL_USER_OBJ
&& have_user_obj
) ||
132 (tag
== ACL_GROUP_OBJ
&& have_group_obj
) ||
133 (tag
== ACL_OTHER
&& have_other
))
136 r
= acl_create_entry(acl_p
, &dst
);
140 r
= acl_copy_entry(dst
, i
);
149 int acl_search_groups(const char *path
, char ***ret_groups
) {
150 _cleanup_strv_free_
char **g
= NULL
;
151 _cleanup_(acl_freep
) acl_t acl
= NULL
;
158 acl
= acl_get_file(path
, ACL_TYPE_DEFAULT
);
162 r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
164 _cleanup_(acl_free_gid_tpp
) gid_t
*gid
= NULL
;
172 if (acl_get_tag_type(entry
, &tag
) < 0)
175 if (tag
!= ACL_GROUP
)
178 gid
= acl_get_qualifier(entry
);
182 if (in_gid(*gid
) > 0) {
192 name
= gid_to_name(*gid
);
196 r
= strv_consume(&g
, name
);
202 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
206 *ret_groups
= TAKE_PTR(g
);
211 int parse_acl(const char *text
, acl_t
*acl_access
, acl_t
*acl_default
, bool want_mask
) {
212 _cleanup_free_
char **a
= NULL
, **d
= NULL
; /* strings are not freed */
213 _cleanup_strv_free_
char **split
;
216 _cleanup_(acl_freep
) acl_t a_acl
= NULL
, d_acl
= NULL
;
218 split
= strv_split(text
, ",");
222 STRV_FOREACH(entry
, split
) {
225 p
= startswith(*entry
, "default:");
227 p
= startswith(*entry
, "d:");
230 r
= strv_push(&d
, p
);
232 r
= strv_push(&a
, *entry
);
237 if (!strv_isempty(a
)) {
238 _cleanup_free_
char *join
;
240 join
= strv_join(a
, ",");
244 a_acl
= acl_from_text(join
);
249 r
= calc_acl_mask_if_needed(&a_acl
);
255 if (!strv_isempty(d
)) {
256 _cleanup_free_
char *join
;
258 join
= strv_join(d
, ",");
262 d_acl
= acl_from_text(join
);
267 r
= calc_acl_mask_if_needed(&d_acl
);
273 *acl_access
= TAKE_PTR(a_acl
);
274 *acl_default
= TAKE_PTR(d_acl
);
279 static int acl_entry_equal(acl_entry_t a
, acl_entry_t b
) {
280 acl_tag_t tag_a
, tag_b
;
282 if (acl_get_tag_type(a
, &tag_a
) < 0)
285 if (acl_get_tag_type(b
, &tag_b
) < 0)
296 /* can have only one of those */
299 _cleanup_(acl_free_uid_tpp
) uid_t
*uid_a
= NULL
, *uid_b
= NULL
;
301 uid_a
= acl_get_qualifier(a
);
305 uid_b
= acl_get_qualifier(b
);
309 return *uid_a
== *uid_b
;
312 _cleanup_(acl_free_gid_tpp
) gid_t
*gid_a
= NULL
, *gid_b
= NULL
;
314 gid_a
= acl_get_qualifier(a
);
318 gid_b
= acl_get_qualifier(b
);
322 return *gid_a
== *gid_b
;
325 assert_not_reached("Unknown acl tag type");
329 static int find_acl_entry(acl_t acl
, acl_entry_t entry
, acl_entry_t
*out
) {
333 for (r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &i
);
335 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &i
)) {
337 r
= acl_entry_equal(i
, entry
);
350 int acls_for_file(const char *path
, acl_type_t type
, acl_t
new, acl_t
*acl
) {
351 _cleanup_(acl_freep
) acl_t old
;
355 old
= acl_get_file(path
, type
);
359 for (r
= acl_get_entry(new, ACL_FIRST_ENTRY
, &i
);
361 r
= acl_get_entry(new, ACL_NEXT_ENTRY
, &i
)) {
365 r
= find_acl_entry(old
, i
, &j
);
369 if (acl_create_entry(&old
, &j
) < 0)
372 if (acl_copy_entry(j
, i
) < 0)
378 *acl
= TAKE_PTR(old
);
383 int add_acls_for_user(int fd
, uid_t uid
) {
384 _cleanup_(acl_freep
) acl_t acl
= NULL
;
386 acl_permset_t permset
;
389 acl
= acl_get_fd(fd
);
393 r
= acl_find_uid(acl
, uid
, &entry
);
395 if (acl_create_entry(&acl
, &entry
) < 0 ||
396 acl_set_tag_type(entry
, ACL_USER
) < 0 ||
397 acl_set_qualifier(entry
, &uid
) < 0)
401 /* We do not recalculate the mask unconditionally here,
402 * so that the fchmod() mask above stays intact. */
403 if (acl_get_permset(entry
, &permset
) < 0 ||
404 acl_add_perm(permset
, ACL_READ
) < 0)
407 r
= calc_acl_mask_if_needed(&acl
);
411 return acl_set_fd(fd
, acl
);