]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/acl-util.c
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 int calc_acl_mask_if_needed(acl_t
*acl_p
) {
59 for (r
= acl_get_entry(*acl_p
, ACL_FIRST_ENTRY
, &i
);
61 r
= acl_get_entry(*acl_p
, ACL_NEXT_ENTRY
, &i
)) {
64 if (acl_get_tag_type(i
, &tag
) < 0)
70 if (IN_SET(tag
, ACL_USER
, ACL_GROUP
))
76 if (need
&& acl_calc_mask(acl_p
) < 0)
82 int add_base_acls_if_needed(acl_t
*acl_p
, const char *path
) {
85 bool have_user_obj
= false, have_group_obj
= false, have_other
= false;
87 _cleanup_(acl_freep
) acl_t basic
= NULL
;
91 for (r
= acl_get_entry(*acl_p
, ACL_FIRST_ENTRY
, &i
);
93 r
= acl_get_entry(*acl_p
, ACL_NEXT_ENTRY
, &i
)) {
96 if (acl_get_tag_type(i
, &tag
) < 0)
99 if (tag
== ACL_USER_OBJ
)
100 have_user_obj
= true;
101 else if (tag
== ACL_GROUP_OBJ
)
102 have_group_obj
= true;
103 else if (tag
== ACL_OTHER
)
105 if (have_user_obj
&& have_group_obj
&& have_other
)
115 basic
= acl_from_mode(st
.st_mode
);
119 for (r
= acl_get_entry(basic
, ACL_FIRST_ENTRY
, &i
);
121 r
= acl_get_entry(basic
, ACL_NEXT_ENTRY
, &i
)) {
125 if (acl_get_tag_type(i
, &tag
) < 0)
128 if ((tag
== ACL_USER_OBJ
&& have_user_obj
) ||
129 (tag
== ACL_GROUP_OBJ
&& have_group_obj
) ||
130 (tag
== ACL_OTHER
&& have_other
))
133 r
= acl_create_entry(acl_p
, &dst
);
137 r
= acl_copy_entry(dst
, i
);
146 int acl_search_groups(const char *path
, char ***ret_groups
) {
147 _cleanup_strv_free_
char **g
= NULL
;
148 _cleanup_(acl_freep
) acl_t acl
= NULL
;
155 acl
= acl_get_file(path
, ACL_TYPE_DEFAULT
);
159 r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
161 _cleanup_(acl_free_gid_tpp
) gid_t
*gid
= NULL
;
169 if (acl_get_tag_type(entry
, &tag
) < 0)
172 if (tag
!= ACL_GROUP
)
175 gid
= acl_get_qualifier(entry
);
179 if (in_gid(*gid
) > 0) {
189 name
= gid_to_name(*gid
);
193 r
= strv_consume(&g
, name
);
199 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
203 *ret_groups
= TAKE_PTR(g
);
208 int parse_acl(const char *text
, acl_t
*acl_access
, acl_t
*acl_default
, bool want_mask
) {
209 _cleanup_free_
char **a
= NULL
, **d
= NULL
; /* strings are not freed */
210 _cleanup_strv_free_
char **split
;
213 _cleanup_(acl_freep
) acl_t a_acl
= NULL
, d_acl
= NULL
;
215 split
= strv_split(text
, ",");
219 STRV_FOREACH(entry
, split
) {
222 p
= STARTSWITH_SET(*entry
, "default:", "d:");
224 r
= strv_push(&d
, p
);
226 r
= strv_push(&a
, *entry
);
231 if (!strv_isempty(a
)) {
232 _cleanup_free_
char *join
;
234 join
= strv_join(a
, ",");
238 a_acl
= acl_from_text(join
);
243 r
= calc_acl_mask_if_needed(&a_acl
);
249 if (!strv_isempty(d
)) {
250 _cleanup_free_
char *join
;
252 join
= strv_join(d
, ",");
256 d_acl
= acl_from_text(join
);
261 r
= calc_acl_mask_if_needed(&d_acl
);
267 *acl_access
= TAKE_PTR(a_acl
);
268 *acl_default
= TAKE_PTR(d_acl
);
273 static int acl_entry_equal(acl_entry_t a
, acl_entry_t b
) {
274 acl_tag_t tag_a
, tag_b
;
276 if (acl_get_tag_type(a
, &tag_a
) < 0)
279 if (acl_get_tag_type(b
, &tag_b
) < 0)
290 /* can have only one of those */
293 _cleanup_(acl_free_uid_tpp
) uid_t
*uid_a
= NULL
, *uid_b
= NULL
;
295 uid_a
= acl_get_qualifier(a
);
299 uid_b
= acl_get_qualifier(b
);
303 return *uid_a
== *uid_b
;
306 _cleanup_(acl_free_gid_tpp
) gid_t
*gid_a
= NULL
, *gid_b
= NULL
;
308 gid_a
= acl_get_qualifier(a
);
312 gid_b
= acl_get_qualifier(b
);
316 return *gid_a
== *gid_b
;
319 assert_not_reached("Unknown acl tag type");
323 static int find_acl_entry(acl_t acl
, acl_entry_t entry
, acl_entry_t
*out
) {
327 for (r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &i
);
329 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &i
)) {
331 r
= acl_entry_equal(i
, entry
);
344 int acls_for_file(const char *path
, acl_type_t type
, acl_t
new, acl_t
*acl
) {
345 _cleanup_(acl_freep
) acl_t old
;
349 old
= acl_get_file(path
, type
);
353 for (r
= acl_get_entry(new, ACL_FIRST_ENTRY
, &i
);
355 r
= acl_get_entry(new, ACL_NEXT_ENTRY
, &i
)) {
359 r
= find_acl_entry(old
, i
, &j
);
363 if (acl_create_entry(&old
, &j
) < 0)
366 if (acl_copy_entry(j
, i
) < 0)
372 *acl
= TAKE_PTR(old
);
377 int add_acls_for_user(int fd
, uid_t uid
) {
378 _cleanup_(acl_freep
) acl_t acl
= NULL
;
380 acl_permset_t permset
;
383 acl
= acl_get_fd(fd
);
387 r
= acl_find_uid(acl
, uid
, &entry
);
389 if (acl_create_entry(&acl
, &entry
) < 0 ||
390 acl_set_tag_type(entry
, ACL_USER
) < 0 ||
391 acl_set_qualifier(entry
, &uid
) < 0)
395 /* We do not recalculate the mask unconditionally here,
396 * so that the fchmod() mask above stays intact. */
397 if (acl_get_permset(entry
, &permset
) < 0 ||
398 acl_add_perm(permset
, ACL_READ
) < 0)
401 r
= calc_acl_mask_if_needed(&acl
);
405 return acl_set_fd(fd
, acl
);