]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/acl-util.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / shared / acl-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdbool.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7
8 #include "acl-util.h"
9 #include "alloc-util.h"
10 #include "string-util.h"
11 #include "strv.h"
12 #include "user-util.h"
13 #include "util.h"
14
15 int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *ret_entry) {
16 acl_entry_t i;
17 int r;
18
19 assert(acl);
20 assert(uid_is_valid(uid));
21 assert(ret_entry);
22
23 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
24 r > 0;
25 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
26
27 acl_tag_t tag;
28 uid_t *u;
29 bool b;
30
31 if (acl_get_tag_type(i, &tag) < 0)
32 return -errno;
33
34 if (tag != ACL_USER)
35 continue;
36
37 u = acl_get_qualifier(i);
38 if (!u)
39 return -errno;
40
41 b = *u == uid;
42 acl_free(u);
43
44 if (b) {
45 *ret_entry = i;
46 return 1;
47 }
48 }
49 if (r < 0)
50 return -errno;
51
52 *ret_entry = NULL;
53 return 0;
54 }
55
56 int calc_acl_mask_if_needed(acl_t *acl_p) {
57 acl_entry_t i;
58 int r;
59 bool need = false;
60
61 assert(acl_p);
62
63 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
64 r > 0;
65 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
66 acl_tag_t tag;
67
68 if (acl_get_tag_type(i, &tag) < 0)
69 return -errno;
70
71 if (tag == ACL_MASK)
72 return 0;
73
74 if (IN_SET(tag, ACL_USER, ACL_GROUP))
75 need = true;
76 }
77 if (r < 0)
78 return -errno;
79
80 if (need && acl_calc_mask(acl_p) < 0)
81 return -errno;
82
83 return need;
84 }
85
86 int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
87 acl_entry_t i;
88 int r;
89 bool have_user_obj = false, have_group_obj = false, have_other = false;
90 struct stat st;
91 _cleanup_(acl_freep) acl_t basic = NULL;
92
93 assert(acl_p);
94
95 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
96 r > 0;
97 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
98 acl_tag_t tag;
99
100 if (acl_get_tag_type(i, &tag) < 0)
101 return -errno;
102
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)
108 have_other = true;
109 if (have_user_obj && have_group_obj && have_other)
110 return 0;
111 }
112 if (r < 0)
113 return -errno;
114
115 r = stat(path, &st);
116 if (r < 0)
117 return -errno;
118
119 basic = acl_from_mode(st.st_mode);
120 if (!basic)
121 return -errno;
122
123 for (r = acl_get_entry(basic, ACL_FIRST_ENTRY, &i);
124 r > 0;
125 r = acl_get_entry(basic, ACL_NEXT_ENTRY, &i)) {
126 acl_tag_t tag;
127 acl_entry_t dst;
128
129 if (acl_get_tag_type(i, &tag) < 0)
130 return -errno;
131
132 if ((tag == ACL_USER_OBJ && have_user_obj) ||
133 (tag == ACL_GROUP_OBJ && have_group_obj) ||
134 (tag == ACL_OTHER && have_other))
135 continue;
136
137 r = acl_create_entry(acl_p, &dst);
138 if (r < 0)
139 return -errno;
140
141 r = acl_copy_entry(dst, i);
142 if (r < 0)
143 return -errno;
144 }
145 if (r < 0)
146 return -errno;
147 return 0;
148 }
149
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;
153 bool ret = false;
154 acl_entry_t entry;
155 int r;
156
157 assert(path);
158
159 acl = acl_get_file(path, ACL_TYPE_DEFAULT);
160 if (!acl)
161 return -errno;
162
163 r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
164 for (;;) {
165 _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
166 acl_tag_t tag;
167
168 if (r < 0)
169 return -errno;
170 if (r == 0)
171 break;
172
173 if (acl_get_tag_type(entry, &tag) < 0)
174 return -errno;
175
176 if (tag != ACL_GROUP)
177 goto next;
178
179 gid = acl_get_qualifier(entry);
180 if (!gid)
181 return -errno;
182
183 if (in_gid(*gid) > 0) {
184 if (!ret_groups)
185 return true;
186
187 ret = true;
188 }
189
190 if (ret_groups) {
191 char *name;
192
193 name = gid_to_name(*gid);
194 if (!name)
195 return -ENOMEM;
196
197 r = strv_consume(&g, name);
198 if (r < 0)
199 return r;
200 }
201
202 next:
203 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
204 }
205
206 if (ret_groups)
207 *ret_groups = TAKE_PTR(g);
208
209 return ret;
210 }
211
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;
215 char **entry;
216 int r = -EINVAL;
217 _cleanup_(acl_freep) acl_t a_acl = NULL, d_acl = NULL;
218
219 split = strv_split(text, ",");
220 if (!split)
221 return -ENOMEM;
222
223 STRV_FOREACH(entry, split) {
224 char *p;
225
226 p = STARTSWITH_SET(*entry, "default:", "d:");
227 if (p)
228 r = strv_push(&d, p);
229 else
230 r = strv_push(&a, *entry);
231 if (r < 0)
232 return r;
233 }
234
235 if (!strv_isempty(a)) {
236 _cleanup_free_ char *join;
237
238 join = strv_join(a, ",");
239 if (!join)
240 return -ENOMEM;
241
242 a_acl = acl_from_text(join);
243 if (!a_acl)
244 return -errno;
245
246 if (want_mask) {
247 r = calc_acl_mask_if_needed(&a_acl);
248 if (r < 0)
249 return r;
250 }
251 }
252
253 if (!strv_isempty(d)) {
254 _cleanup_free_ char *join;
255
256 join = strv_join(d, ",");
257 if (!join)
258 return -ENOMEM;
259
260 d_acl = acl_from_text(join);
261 if (!d_acl)
262 return -errno;
263
264 if (want_mask) {
265 r = calc_acl_mask_if_needed(&d_acl);
266 if (r < 0)
267 return r;
268 }
269 }
270
271 *acl_access = TAKE_PTR(a_acl);
272 *acl_default = TAKE_PTR(d_acl);
273
274 return 0;
275 }
276
277 static int acl_entry_equal(acl_entry_t a, acl_entry_t b) {
278 acl_tag_t tag_a, tag_b;
279
280 if (acl_get_tag_type(a, &tag_a) < 0)
281 return -errno;
282
283 if (acl_get_tag_type(b, &tag_b) < 0)
284 return -errno;
285
286 if (tag_a != tag_b)
287 return false;
288
289 switch (tag_a) {
290 case ACL_USER_OBJ:
291 case ACL_GROUP_OBJ:
292 case ACL_MASK:
293 case ACL_OTHER:
294 /* can have only one of those */
295 return true;
296 case ACL_USER: {
297 _cleanup_(acl_free_uid_tpp) uid_t *uid_a = NULL, *uid_b = NULL;
298
299 uid_a = acl_get_qualifier(a);
300 if (!uid_a)
301 return -errno;
302
303 uid_b = acl_get_qualifier(b);
304 if (!uid_b)
305 return -errno;
306
307 return *uid_a == *uid_b;
308 }
309 case ACL_GROUP: {
310 _cleanup_(acl_free_gid_tpp) gid_t *gid_a = NULL, *gid_b = NULL;
311
312 gid_a = acl_get_qualifier(a);
313 if (!gid_a)
314 return -errno;
315
316 gid_b = acl_get_qualifier(b);
317 if (!gid_b)
318 return -errno;
319
320 return *gid_a == *gid_b;
321 }
322 default:
323 assert_not_reached("Unknown acl tag type");
324 }
325 }
326
327 static int find_acl_entry(acl_t acl, acl_entry_t entry, acl_entry_t *out) {
328 acl_entry_t i;
329 int r;
330
331 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
332 r > 0;
333 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
334
335 r = acl_entry_equal(i, entry);
336 if (r < 0)
337 return r;
338 if (r > 0) {
339 *out = i;
340 return 1;
341 }
342 }
343 if (r < 0)
344 return -errno;
345 return 0;
346 }
347
348 int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl) {
349 _cleanup_(acl_freep) acl_t old;
350 acl_entry_t i;
351 int r;
352
353 old = acl_get_file(path, type);
354 if (!old)
355 return -errno;
356
357 for (r = acl_get_entry(new, ACL_FIRST_ENTRY, &i);
358 r > 0;
359 r = acl_get_entry(new, ACL_NEXT_ENTRY, &i)) {
360
361 acl_entry_t j;
362
363 r = find_acl_entry(old, i, &j);
364 if (r < 0)
365 return r;
366 if (r == 0)
367 if (acl_create_entry(&old, &j) < 0)
368 return -errno;
369
370 if (acl_copy_entry(j, i) < 0)
371 return -errno;
372 }
373 if (r < 0)
374 return -errno;
375
376 *acl = TAKE_PTR(old);
377
378 return 0;
379 }
380
381 /* POSIX says that ACL_{READ,WRITE,EXECUTE} don't have to be bitmasks. But that is a natural thing to do and
382 * all extant implementations do it. Let's make sure that we fail verbosely in the (imho unlikely) scenario
383 * that we get a new implementation that does not satisfy this. */
384 assert_cc(!(ACL_READ & ACL_WRITE));
385 assert_cc(!(ACL_WRITE & ACL_EXECUTE));
386 assert_cc(!(ACL_EXECUTE & ACL_READ));
387 assert_cc((unsigned) ACL_READ == ACL_READ);
388 assert_cc((unsigned) ACL_WRITE == ACL_WRITE);
389 assert_cc((unsigned) ACL_EXECUTE == ACL_EXECUTE);
390
391 int fd_add_uid_acl_permission(
392 int fd,
393 uid_t uid,
394 unsigned mask) {
395
396 _cleanup_(acl_freep) acl_t acl = NULL;
397 acl_permset_t permset;
398 acl_entry_t entry;
399 int r;
400
401 /* Adds an ACL entry for the specified file to allow the indicated access to the specified
402 * user. Operates purely incrementally. */
403
404 assert(fd >= 0);
405 assert(uid_is_valid(uid));
406
407 acl = acl_get_fd(fd);
408 if (!acl)
409 return -errno;
410
411 r = acl_find_uid(acl, uid, &entry);
412 if (r <= 0) {
413 if (acl_create_entry(&acl, &entry) < 0 ||
414 acl_set_tag_type(entry, ACL_USER) < 0 ||
415 acl_set_qualifier(entry, &uid) < 0)
416 return -errno;
417 }
418
419 if (acl_get_permset(entry, &permset) < 0)
420 return -errno;
421
422 if ((mask & ACL_READ) && acl_add_perm(permset, ACL_READ) < 0)
423 return -errno;
424 if ((mask & ACL_WRITE) && acl_add_perm(permset, ACL_WRITE) < 0)
425 return -errno;
426 if ((mask & ACL_EXECUTE) && acl_add_perm(permset, ACL_EXECUTE) < 0)
427 return -errno;
428
429 r = calc_acl_mask_if_needed(&acl);
430 if (r < 0)
431 return r;
432
433 if (acl_set_fd(fd, acl) < 0)
434 return -errno;
435
436 return 0;
437 }