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 #if 0 /// UNNEEDED by elogind
56 int calc_acl_mask_if_needed(acl_t *acl_p) {
63 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
65 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
68 if (acl_get_tag_type(i, &tag) < 0)
74 if (IN_SET(tag, ACL_USER, ACL_GROUP))
80 if (need && acl_calc_mask(acl_p) < 0)
86 int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
89 bool have_user_obj = false, have_group_obj = false, have_other = false;
91 _cleanup_(acl_freep) acl_t basic = NULL;
95 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
97 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
100 if (acl_get_tag_type(i, &tag) < 0)
103 if (tag == ACL_USER_OBJ)
104 have_user_obj = true;
105 else if (tag == ACL_GROUP_OBJ)
106 have_group_obj = true;
107 else if (tag == ACL_OTHER)
109 if (have_user_obj && have_group_obj && have_other)
119 basic = acl_from_mode(st.st_mode);
123 for (r = acl_get_entry(basic, ACL_FIRST_ENTRY, &i);
125 r = acl_get_entry(basic, ACL_NEXT_ENTRY, &i)) {
129 if (acl_get_tag_type(i, &tag) < 0)
132 if ((tag == ACL_USER_OBJ && have_user_obj) ||
133 (tag == ACL_GROUP_OBJ && have_group_obj) ||
134 (tag == ACL_OTHER && have_other))
137 r = acl_create_entry(acl_p, &dst);
141 r = acl_copy_entry(dst, i);
150 int acl_search_groups(const char *path, char ***ret_groups) {
151 _cleanup_strv_free_ char **g = NULL;
152 _cleanup_(acl_freep) acl_t acl = NULL;
159 acl = acl_get_file(path, ACL_TYPE_DEFAULT);
163 r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
165 _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
173 if (acl_get_tag_type(entry, &tag) < 0)
176 if (tag != ACL_GROUP)
179 gid = acl_get_qualifier(entry);
183 if (in_gid(*gid) > 0) {
193 name = gid_to_name(*gid);
197 r = strv_consume(&g, name);
203 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
207 *ret_groups = TAKE_PTR(g);
212 int parse_acl(const char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
213 _cleanup_free_ char **a = NULL, **d = NULL; /* strings are not freed */
214 _cleanup_strv_free_ char **split;
217 _cleanup_(acl_freep) acl_t a_acl = NULL, d_acl = NULL;
219 split = strv_split(text, ",");
223 STRV_FOREACH(entry, split) {
226 p = startswith(*entry, "default:");
228 p = startswith(*entry, "d:");
231 r = strv_push(&d, p);
233 r = strv_push(&a, *entry);
238 if (!strv_isempty(a)) {
239 _cleanup_free_ char *join;
241 join = strv_join(a, ",");
245 a_acl = acl_from_text(join);
250 r = calc_acl_mask_if_needed(&a_acl);
256 if (!strv_isempty(d)) {
257 _cleanup_free_ char *join;
259 join = strv_join(d, ",");
263 d_acl = acl_from_text(join);
268 r = calc_acl_mask_if_needed(&d_acl);
274 *acl_access = TAKE_PTR(a_acl);
275 *acl_default = TAKE_PTR(d_acl);
280 static int acl_entry_equal(acl_entry_t a, acl_entry_t b) {
281 acl_tag_t tag_a, tag_b;
283 if (acl_get_tag_type(a, &tag_a) < 0)
286 if (acl_get_tag_type(b, &tag_b) < 0)
297 /* can have only one of those */
300 _cleanup_(acl_free_uid_tpp) uid_t *uid_a = NULL, *uid_b = NULL;
302 uid_a = acl_get_qualifier(a);
306 uid_b = acl_get_qualifier(b);
310 return *uid_a == *uid_b;
313 _cleanup_(acl_free_gid_tpp) gid_t *gid_a = NULL, *gid_b = NULL;
315 gid_a = acl_get_qualifier(a);
319 gid_b = acl_get_qualifier(b);
323 return *gid_a == *gid_b;
326 assert_not_reached("Unknown acl tag type");
330 static int find_acl_entry(acl_t acl, acl_entry_t entry, acl_entry_t *out) {
334 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
336 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
338 r = acl_entry_equal(i, entry);
351 int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl) {
352 _cleanup_(acl_freep) acl_t old;
356 old = acl_get_file(path, type);
360 for (r = acl_get_entry(new, ACL_FIRST_ENTRY, &i);
362 r = acl_get_entry(new, ACL_NEXT_ENTRY, &i)) {
366 r = find_acl_entry(old, i, &j);
370 if (acl_create_entry(&old, &j) < 0)
373 if (acl_copy_entry(j, i) < 0)
379 *acl = TAKE_PTR(old);
384 int add_acls_for_user(int fd, uid_t uid) {
385 _cleanup_(acl_freep) acl_t acl = NULL;
387 acl_permset_t permset;
390 acl = acl_get_fd(fd);
394 r = acl_find_uid(acl, uid, &entry);
396 if (acl_create_entry(&acl, &entry) < 0 ||
397 acl_set_tag_type(entry, ACL_USER) < 0 ||
398 acl_set_qualifier(entry, &uid) < 0)
402 /* We do not recalculate the mask unconditionally here,
403 * so that the fchmod() mask above stays intact. */
404 if (acl_get_permset(entry, &permset) < 0 ||
405 acl_add_perm(permset, ACL_READ) < 0)
408 r = calc_acl_mask_if_needed(&acl);
412 return acl_set_fd(fd, acl);