]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/acl-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011,2013 Lennart Poettering
12 #include "alloc-util.h"
13 #include "string-util.h"
15 #include "user-util.h"
18 int acl_find_uid(acl_t acl
, uid_t uid
, acl_entry_t
*entry
) {
25 for (r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &i
);
27 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &i
)) {
33 if (acl_get_tag_type(i
, &tag
) < 0)
39 u
= acl_get_qualifier(i
);
57 int calc_acl_mask_if_needed(acl_t
*acl_p
) {
64 for (r
= acl_get_entry(*acl_p
, ACL_FIRST_ENTRY
, &i
);
66 r
= acl_get_entry(*acl_p
, ACL_NEXT_ENTRY
, &i
)) {
69 if (acl_get_tag_type(i
, &tag
) < 0)
75 if (IN_SET(tag
, ACL_USER
, ACL_GROUP
))
81 if (need
&& acl_calc_mask(acl_p
) < 0)
87 int add_base_acls_if_needed(acl_t
*acl_p
, const char *path
) {
90 bool have_user_obj
= false, have_group_obj
= false, have_other
= false;
92 _cleanup_(acl_freep
) acl_t basic
= NULL
;
96 for (r
= acl_get_entry(*acl_p
, ACL_FIRST_ENTRY
, &i
);
98 r
= acl_get_entry(*acl_p
, ACL_NEXT_ENTRY
, &i
)) {
101 if (acl_get_tag_type(i
, &tag
) < 0)
104 if (tag
== ACL_USER_OBJ
)
105 have_user_obj
= true;
106 else if (tag
== ACL_GROUP_OBJ
)
107 have_group_obj
= true;
108 else if (tag
== ACL_OTHER
)
110 if (have_user_obj
&& have_group_obj
&& have_other
)
120 basic
= acl_from_mode(st
.st_mode
);
124 for (r
= acl_get_entry(basic
, ACL_FIRST_ENTRY
, &i
);
126 r
= acl_get_entry(basic
, ACL_NEXT_ENTRY
, &i
)) {
130 if (acl_get_tag_type(i
, &tag
) < 0)
133 if ((tag
== ACL_USER_OBJ
&& have_user_obj
) ||
134 (tag
== ACL_GROUP_OBJ
&& have_group_obj
) ||
135 (tag
== ACL_OTHER
&& have_other
))
138 r
= acl_create_entry(acl_p
, &dst
);
142 r
= acl_copy_entry(dst
, i
);
151 int acl_search_groups(const char *path
, char ***ret_groups
) {
152 _cleanup_strv_free_
char **g
= NULL
;
153 _cleanup_(acl_freep
) acl_t acl
= NULL
;
160 acl
= acl_get_file(path
, ACL_TYPE_DEFAULT
);
164 r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
166 _cleanup_(acl_free_gid_tpp
) gid_t
*gid
= NULL
;
174 if (acl_get_tag_type(entry
, &tag
) < 0)
177 if (tag
!= ACL_GROUP
)
180 gid
= acl_get_qualifier(entry
);
184 if (in_gid(*gid
) > 0) {
194 name
= gid_to_name(*gid
);
198 r
= strv_consume(&g
, name
);
204 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
208 *ret_groups
= TAKE_PTR(g
);
213 int parse_acl(const char *text
, acl_t
*acl_access
, acl_t
*acl_default
, bool want_mask
) {
214 _cleanup_free_
char **a
= NULL
, **d
= NULL
; /* strings are not freed */
215 _cleanup_strv_free_
char **split
;
218 _cleanup_(acl_freep
) acl_t a_acl
= NULL
, d_acl
= NULL
;
220 split
= strv_split(text
, ",");
224 STRV_FOREACH(entry
, split
) {
227 p
= startswith(*entry
, "default:");
229 p
= startswith(*entry
, "d:");
232 r
= strv_push(&d
, p
);
234 r
= strv_push(&a
, *entry
);
239 if (!strv_isempty(a
)) {
240 _cleanup_free_
char *join
;
242 join
= strv_join(a
, ",");
246 a_acl
= acl_from_text(join
);
251 r
= calc_acl_mask_if_needed(&a_acl
);
257 if (!strv_isempty(d
)) {
258 _cleanup_free_
char *join
;
260 join
= strv_join(d
, ",");
264 d_acl
= acl_from_text(join
);
269 r
= calc_acl_mask_if_needed(&d_acl
);
275 *acl_access
= TAKE_PTR(a_acl
);
276 *acl_default
= TAKE_PTR(d_acl
);
281 static int acl_entry_equal(acl_entry_t a
, acl_entry_t b
) {
282 acl_tag_t tag_a
, tag_b
;
284 if (acl_get_tag_type(a
, &tag_a
) < 0)
287 if (acl_get_tag_type(b
, &tag_b
) < 0)
298 /* can have only one of those */
301 _cleanup_(acl_free_uid_tpp
) uid_t
*uid_a
= NULL
, *uid_b
= NULL
;
303 uid_a
= acl_get_qualifier(a
);
307 uid_b
= acl_get_qualifier(b
);
311 return *uid_a
== *uid_b
;
314 _cleanup_(acl_free_gid_tpp
) gid_t
*gid_a
= NULL
, *gid_b
= NULL
;
316 gid_a
= acl_get_qualifier(a
);
320 gid_b
= acl_get_qualifier(b
);
324 return *gid_a
== *gid_b
;
327 assert_not_reached("Unknown acl tag type");
331 static int find_acl_entry(acl_t acl
, acl_entry_t entry
, acl_entry_t
*out
) {
335 for (r
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &i
);
337 r
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &i
)) {
339 r
= acl_entry_equal(i
, entry
);
352 int acls_for_file(const char *path
, acl_type_t type
, acl_t
new, acl_t
*acl
) {
353 _cleanup_(acl_freep
) acl_t old
;
357 old
= acl_get_file(path
, type
);
361 for (r
= acl_get_entry(new, ACL_FIRST_ENTRY
, &i
);
363 r
= acl_get_entry(new, ACL_NEXT_ENTRY
, &i
)) {
367 r
= find_acl_entry(old
, i
, &j
);
371 if (acl_create_entry(&old
, &j
) < 0)
374 if (acl_copy_entry(j
, i
) < 0)
380 *acl
= TAKE_PTR(old
);
385 int add_acls_for_user(int fd
, uid_t uid
) {
386 _cleanup_(acl_freep
) acl_t acl
= NULL
;
388 acl_permset_t permset
;
391 acl
= acl_get_fd(fd
);
395 r
= acl_find_uid(acl
, uid
, &entry
);
397 if (acl_create_entry(&acl
, &entry
) < 0 ||
398 acl_set_tag_type(entry
, ACL_USER
) < 0 ||
399 acl_set_qualifier(entry
, &uid
) < 0)
403 /* We do not recalculate the mask unconditionally here,
404 * so that the fchmod() mask above stays intact. */
405 if (acl_get_permset(entry
, &permset
) < 0 ||
406 acl_add_perm(permset
, ACL_READ
) < 0)
409 r
= calc_acl_mask_if_needed(&acl
);
413 return acl_set_fd(fd
, acl
);