1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011,2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry) {
36 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
38 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
44 if (acl_get_tag_type(i, &tag) < 0)
50 u = acl_get_qualifier(i);
68 /// UNNEEDED by elogind
70 int calc_acl_mask_if_needed(acl_t *acl_p) {
76 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
78 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
81 if (acl_get_tag_type(i, &tag) < 0)
87 if (IN_SET(tag, ACL_USER, ACL_GROUP)) {
88 if (acl_calc_mask(acl_p) < 0)
100 int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
103 bool have_user_obj = false, have_group_obj = false, have_other = false;
105 _cleanup_(acl_freep) acl_t basic = NULL;
109 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
111 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
114 if (acl_get_tag_type(i, &tag) < 0)
117 if (tag == ACL_USER_OBJ)
118 have_user_obj = true;
119 else if (tag == ACL_GROUP_OBJ)
120 have_group_obj = true;
121 else if (tag == ACL_OTHER)
123 if (have_user_obj && have_group_obj && have_other)
133 basic = acl_from_mode(st.st_mode);
137 for (r = acl_get_entry(basic, ACL_FIRST_ENTRY, &i);
139 r = acl_get_entry(basic, ACL_NEXT_ENTRY, &i)) {
143 if (acl_get_tag_type(i, &tag) < 0)
146 if ((tag == ACL_USER_OBJ && have_user_obj) ||
147 (tag == ACL_GROUP_OBJ && have_group_obj) ||
148 (tag == ACL_OTHER && have_other))
151 r = acl_create_entry(acl_p, &dst);
155 r = acl_copy_entry(dst, i);
164 int acl_search_groups(const char *path, char ***ret_groups) {
165 _cleanup_strv_free_ char **g = NULL;
166 _cleanup_(acl_free) acl_t acl = NULL;
173 acl = acl_get_file(path, ACL_TYPE_DEFAULT);
177 r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
179 _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
187 if (acl_get_tag_type(entry, &tag) < 0)
190 if (tag != ACL_GROUP)
193 gid = acl_get_qualifier(entry);
197 if (in_gid(*gid) > 0) {
207 name = gid_to_name(*gid);
211 r = strv_consume(&g, name);
217 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
228 int parse_acl(const char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
229 _cleanup_free_ char **a = NULL, **d = NULL; /* strings are not be freed */
230 _cleanup_strv_free_ char **split;
233 _cleanup_(acl_freep) acl_t a_acl = NULL, d_acl = NULL;
235 split = strv_split(text, ",");
239 STRV_FOREACH(entry, split) {
242 p = startswith(*entry, "default:");
244 p = startswith(*entry, "d:");
247 r = strv_push(&d, p);
249 r = strv_push(&a, *entry);
254 if (!strv_isempty(a)) {
255 _cleanup_free_ char *join;
257 join = strv_join(a, ",");
261 a_acl = acl_from_text(join);
266 r = calc_acl_mask_if_needed(&a_acl);
272 if (!strv_isempty(d)) {
273 _cleanup_free_ char *join;
275 join = strv_join(d, ",");
279 d_acl = acl_from_text(join);
284 r = calc_acl_mask_if_needed(&d_acl);
291 *acl_default = d_acl;
292 a_acl = d_acl = NULL;
297 static int acl_entry_equal(acl_entry_t a, acl_entry_t b) {
298 acl_tag_t tag_a, tag_b;
300 if (acl_get_tag_type(a, &tag_a) < 0)
303 if (acl_get_tag_type(b, &tag_b) < 0)
314 /* can have only one of those */
317 _cleanup_(acl_free_uid_tpp) uid_t *uid_a = NULL, *uid_b = NULL;
319 uid_a = acl_get_qualifier(a);
323 uid_b = acl_get_qualifier(b);
327 return *uid_a == *uid_b;
330 _cleanup_(acl_free_gid_tpp) gid_t *gid_a = NULL, *gid_b = NULL;
332 gid_a = acl_get_qualifier(a);
336 gid_b = acl_get_qualifier(b);
340 return *gid_a == *gid_b;
343 assert_not_reached("Unknown acl tag type");
347 static int find_acl_entry(acl_t acl, acl_entry_t entry, acl_entry_t *out) {
351 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
353 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
355 r = acl_entry_equal(i, entry);
368 int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl) {
369 _cleanup_(acl_freep) acl_t old;
373 old = acl_get_file(path, type);
377 for (r = acl_get_entry(new, ACL_FIRST_ENTRY, &i);
379 r = acl_get_entry(new, ACL_NEXT_ENTRY, &i)) {
383 r = find_acl_entry(old, i, &j);
387 if (acl_create_entry(&old, &j) < 0)
390 if (acl_copy_entry(j, i) < 0)