]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/acl-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "string-util.h"
12 #include "user-util.h"
15 int acl_find_uid(acl_t acl
, uid_t uid
, acl_entry_t
*entry
) {
22 for (r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &i
);
24 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &i
)) {
30 if (acl_get_tag_type(i
, &tag
) < 0)
36 u
= acl_get_qualifier(i
);
54 int calc_acl_mask_if_needed(acl_t
*acl_p
) {
61 for (r
= acl_get_entry(*acl_p
, ACL_FIRST_ENTRY
, &i
);
63 r
= acl_get_entry(*acl_p
, ACL_NEXT_ENTRY
, &i
)) {
66 if (acl_get_tag_type(i
, &tag
) < 0)
72 if (IN_SET(tag
, ACL_USER
, ACL_GROUP
))
78 if (need
&& acl_calc_mask(acl_p
) < 0)
84 int add_base_acls_if_needed(acl_t
*acl_p
, const char *path
) {
87 bool have_user_obj
= false, have_group_obj
= false, have_other
= false;
89 _cleanup_(acl_freep
) acl_t basic
= NULL
;
93 for (r
= acl_get_entry(*acl_p
, ACL_FIRST_ENTRY
, &i
);
95 r
= acl_get_entry(*acl_p
, ACL_NEXT_ENTRY
, &i
)) {
98 if (acl_get_tag_type(i
, &tag
) < 0)
101 if (tag
== ACL_USER_OBJ
)
102 have_user_obj
= true;
103 else if (tag
== ACL_GROUP_OBJ
)
104 have_group_obj
= true;
105 else if (tag
== ACL_OTHER
)
107 if (have_user_obj
&& have_group_obj
&& have_other
)
117 basic
= acl_from_mode(st
.st_mode
);
121 for (r
= acl_get_entry(basic
, ACL_FIRST_ENTRY
, &i
);
123 r
= acl_get_entry(basic
, ACL_NEXT_ENTRY
, &i
)) {
127 if (acl_get_tag_type(i
, &tag
) < 0)
130 if ((tag
== ACL_USER_OBJ
&& have_user_obj
) ||
131 (tag
== ACL_GROUP_OBJ
&& have_group_obj
) ||
132 (tag
== ACL_OTHER
&& have_other
))
135 r
= acl_create_entry(acl_p
, &dst
);
139 r
= acl_copy_entry(dst
, i
);
148 int acl_search_groups(const char *path
, char ***ret_groups
) {
149 _cleanup_strv_free_
char **g
= NULL
;
150 _cleanup_(acl_freep
) acl_t acl
= NULL
;
157 acl
= acl_get_file(path
, ACL_TYPE_DEFAULT
);
161 r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
163 _cleanup_(acl_free_gid_tpp
) gid_t
*gid
= NULL
;
171 if (acl_get_tag_type(entry
, &tag
) < 0)
174 if (tag
!= ACL_GROUP
)
177 gid
= acl_get_qualifier(entry
);
181 if (in_gid(*gid
) > 0) {
191 name
= gid_to_name(*gid
);
195 r
= strv_consume(&g
, name
);
201 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
205 *ret_groups
= TAKE_PTR(g
);
210 int parse_acl(const char *text
, acl_t
*acl_access
, acl_t
*acl_default
, bool want_mask
) {
211 _cleanup_free_
char **a
= NULL
, **d
= NULL
; /* strings are not freed */
212 _cleanup_strv_free_
char **split
;
215 _cleanup_(acl_freep
) acl_t a_acl
= NULL
, d_acl
= NULL
;
217 split
= strv_split(text
, ",");
221 STRV_FOREACH(entry
, split
) {
224 p
= STARTSWITH_SET(*entry
, "default:", "d:");
226 r
= strv_push(&d
, p
);
228 r
= strv_push(&a
, *entry
);
233 if (!strv_isempty(a
)) {
234 _cleanup_free_
char *join
;
236 join
= strv_join(a
, ",");
240 a_acl
= acl_from_text(join
);
245 r
= calc_acl_mask_if_needed(&a_acl
);
251 if (!strv_isempty(d
)) {
252 _cleanup_free_
char *join
;
254 join
= strv_join(d
, ",");
258 d_acl
= acl_from_text(join
);
263 r
= calc_acl_mask_if_needed(&d_acl
);
269 *acl_access
= TAKE_PTR(a_acl
);
270 *acl_default
= TAKE_PTR(d_acl
);
275 static int acl_entry_equal(acl_entry_t a
, acl_entry_t b
) {
276 acl_tag_t tag_a
, tag_b
;
278 if (acl_get_tag_type(a
, &tag_a
) < 0)
281 if (acl_get_tag_type(b
, &tag_b
) < 0)
292 /* can have only one of those */
295 _cleanup_(acl_free_uid_tpp
) uid_t
*uid_a
= NULL
, *uid_b
= NULL
;
297 uid_a
= acl_get_qualifier(a
);
301 uid_b
= acl_get_qualifier(b
);
305 return *uid_a
== *uid_b
;
308 _cleanup_(acl_free_gid_tpp
) gid_t
*gid_a
= NULL
, *gid_b
= NULL
;
310 gid_a
= acl_get_qualifier(a
);
314 gid_b
= acl_get_qualifier(b
);
318 return *gid_a
== *gid_b
;
321 assert_not_reached("Unknown acl tag type");
325 static int find_acl_entry(acl_t acl
, acl_entry_t entry
, acl_entry_t
*out
) {
329 for (r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &i
);
331 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &i
)) {
333 r
= acl_entry_equal(i
, entry
);
346 int acls_for_file(const char *path
, acl_type_t type
, acl_t
new, acl_t
*acl
) {
347 _cleanup_(acl_freep
) acl_t old
;
351 old
= acl_get_file(path
, type
);
355 for (r
= acl_get_entry(new, ACL_FIRST_ENTRY
, &i
);
357 r
= acl_get_entry(new, ACL_NEXT_ENTRY
, &i
)) {
361 r
= find_acl_entry(old
, i
, &j
);
365 if (acl_create_entry(&old
, &j
) < 0)
368 if (acl_copy_entry(j
, i
) < 0)
374 *acl
= TAKE_PTR(old
);
379 int add_acls_for_user(int fd
, uid_t uid
) {
380 _cleanup_(acl_freep
) acl_t acl
= NULL
;
382 acl_permset_t permset
;
385 acl
= acl_get_fd(fd
);
389 r
= acl_find_uid(acl
, uid
, &entry
);
391 if (acl_create_entry(&acl
, &entry
) < 0 ||
392 acl_set_tag_type(entry
, ACL_USER
) < 0 ||
393 acl_set_qualifier(entry
, &uid
) < 0)
397 /* We do not recalculate the mask unconditionally here,
398 * so that the fchmod() mask above stays intact. */
399 if (acl_get_permset(entry
, &permset
) < 0 ||
400 acl_add_perm(permset
, ACL_READ
) < 0)
403 r
= calc_acl_mask_if_needed(&acl
);
407 return acl_set_fd(fd
, acl
);