]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/acl-util.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / shared / acl-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f4b47811 2
f4b47811
LP
3#include <errno.h>
4#include <stdbool.h>
5
b1d4f8e1 6#include "acl-util.h"
cf0fbc49 7#include "alloc-util.h"
07630cea 8#include "string-util.h"
478c8269 9#include "strv.h"
b1d4f8e1 10#include "user-util.h"
07630cea 11#include "util.h"
f4b47811
LP
12
13int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry) {
14 acl_entry_t i;
dd4105b0 15 int r;
f4b47811
LP
16
17 assert(acl);
18 assert(entry);
19
dd4105b0
ZJS
20 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
21 r > 0;
22 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
f4b47811
LP
23
24 acl_tag_t tag;
25 uid_t *u;
26 bool b;
27
28 if (acl_get_tag_type(i, &tag) < 0)
29 return -errno;
30
31 if (tag != ACL_USER)
32 continue;
33
34 u = acl_get_qualifier(i);
35 if (!u)
36 return -errno;
37
38 b = *u == uid;
39 acl_free(u);
40
41 if (b) {
42 *entry = i;
43 return 1;
44 }
45 }
dd4105b0 46 if (r < 0)
f4b47811
LP
47 return -errno;
48
49 return 0;
50}
478c8269 51
23ad4dd8
JAS
52int calc_acl_mask_if_needed(acl_t *acl_p) {
53 acl_entry_t i;
dd4105b0 54 int r;
6debb398 55 bool need = false;
23ad4dd8
JAS
56
57 assert(acl_p);
58
dd4105b0
ZJS
59 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
60 r > 0;
61 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
23ad4dd8
JAS
62 acl_tag_t tag;
63
64 if (acl_get_tag_type(i, &tag) < 0)
65 return -errno;
66
67 if (tag == ACL_MASK)
68 return 0;
e346512c 69
6debb398
ZJS
70 if (IN_SET(tag, ACL_USER, ACL_GROUP))
71 need = true;
23ad4dd8 72 }
dd4105b0 73 if (r < 0)
23ad4dd8
JAS
74 return -errno;
75
6debb398
ZJS
76 if (need && acl_calc_mask(acl_p) < 0)
77 return -errno;
78
79 return need;
dd4105b0
ZJS
80}
81
82int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
83 acl_entry_t i;
84 int r;
85 bool have_user_obj = false, have_group_obj = false, have_other = false;
86 struct stat st;
87 _cleanup_(acl_freep) acl_t basic = NULL;
88
89 assert(acl_p);
90
91 for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
92 r > 0;
93 r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
94 acl_tag_t tag;
95
96 if (acl_get_tag_type(i, &tag) < 0)
97 return -errno;
98
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)
104 have_other = true;
105 if (have_user_obj && have_group_obj && have_other)
106 return 0;
107 }
108 if (r < 0)
109 return -errno;
23ad4dd8 110
dd4105b0
ZJS
111 r = stat(path, &st);
112 if (r < 0)
113 return -errno;
114
115 basic = acl_from_mode(st.st_mode);
116 if (!basic)
117 return -errno;
118
119 for (r = acl_get_entry(basic, ACL_FIRST_ENTRY, &i);
120 r > 0;
121 r = acl_get_entry(basic, ACL_NEXT_ENTRY, &i)) {
122 acl_tag_t tag;
123 acl_entry_t dst;
124
125 if (acl_get_tag_type(i, &tag) < 0)
126 return -errno;
127
128 if ((tag == ACL_USER_OBJ && have_user_obj) ||
129 (tag == ACL_GROUP_OBJ && have_group_obj) ||
130 (tag == ACL_OTHER && have_other))
131 continue;
132
133 r = acl_create_entry(acl_p, &dst);
134 if (r < 0)
135 return -errno;
136
137 r = acl_copy_entry(dst, i);
138 if (r < 0)
139 return -errno;
140 }
141 if (r < 0)
142 return -errno;
23ad4dd8
JAS
143 return 0;
144}
145
e346512c
LP
146int acl_search_groups(const char *path, char ***ret_groups) {
147 _cleanup_strv_free_ char **g = NULL;
29d87223 148 _cleanup_(acl_freep) acl_t acl = NULL;
e346512c
LP
149 bool ret = false;
150 acl_entry_t entry;
151 int r;
478c8269
ZJS
152
153 assert(path);
478c8269
ZJS
154
155 acl = acl_get_file(path, ACL_TYPE_DEFAULT);
e346512c
LP
156 if (!acl)
157 return -errno;
478c8269 158
e346512c
LP
159 r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
160 for (;;) {
161 _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
162 acl_tag_t tag;
478c8269 163
e346512c
LP
164 if (r < 0)
165 return -errno;
166 if (r == 0)
167 break;
478c8269 168
e346512c
LP
169 if (acl_get_tag_type(entry, &tag) < 0)
170 return -errno;
171
172 if (tag != ACL_GROUP)
173 goto next;
174
175 gid = acl_get_qualifier(entry);
176 if (!gid)
177 return -errno;
478c8269 178
e346512c
LP
179 if (in_gid(*gid) > 0) {
180 if (!ret_groups)
181 return true;
182
183 ret = true;
184 }
185
186 if (ret_groups) {
187 char *name;
478c8269
ZJS
188
189 name = gid_to_name(*gid);
e346512c
LP
190 if (!name)
191 return -ENOMEM;
192
193 r = strv_consume(&g, name);
194 if (r < 0)
195 return r;
478c8269
ZJS
196 }
197
e346512c
LP
198 next:
199 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
478c8269
ZJS
200 }
201
1cc6c93a
YW
202 if (ret_groups)
203 *ret_groups = TAKE_PTR(g);
e346512c
LP
204
205 return ret;
478c8269 206}
f8eeeaf9 207
e738c945 208int parse_acl(const char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
848f0178 209 _cleanup_free_ char **a = NULL, **d = NULL; /* strings are not freed */
f8eeeaf9
ZJS
210 _cleanup_strv_free_ char **split;
211 char **entry;
212 int r = -EINVAL;
213 _cleanup_(acl_freep) acl_t a_acl = NULL, d_acl = NULL;
214
215 split = strv_split(text, ",");
216 if (!split)
e738c945 217 return -ENOMEM;
f8eeeaf9
ZJS
218
219 STRV_FOREACH(entry, split) {
220 char *p;
221
222 p = startswith(*entry, "default:");
223 if (!p)
224 p = startswith(*entry, "d:");
225
226 if (p)
227 r = strv_push(&d, p);
228 else
229 r = strv_push(&a, *entry);
e738c945
LP
230 if (r < 0)
231 return r;
f8eeeaf9 232 }
f8eeeaf9
ZJS
233
234 if (!strv_isempty(a)) {
235 _cleanup_free_ char *join;
236
237 join = strv_join(a, ",");
238 if (!join)
239 return -ENOMEM;
240
241 a_acl = acl_from_text(join);
242 if (!a_acl)
e738c945 243 return -errno;
f8eeeaf9 244
50d9e46d
ZJS
245 if (want_mask) {
246 r = calc_acl_mask_if_needed(&a_acl);
247 if (r < 0)
248 return r;
249 }
f8eeeaf9
ZJS
250 }
251
252 if (!strv_isempty(d)) {
253 _cleanup_free_ char *join;
254
255 join = strv_join(d, ",");
256 if (!join)
257 return -ENOMEM;
258
259 d_acl = acl_from_text(join);
260 if (!d_acl)
e738c945 261 return -errno;
f8eeeaf9 262
50d9e46d
ZJS
263 if (want_mask) {
264 r = calc_acl_mask_if_needed(&d_acl);
265 if (r < 0)
266 return r;
267 }
f8eeeaf9
ZJS
268 }
269
1cc6c93a
YW
270 *acl_access = TAKE_PTR(a_acl);
271 *acl_default = TAKE_PTR(d_acl);
e738c945 272
f8eeeaf9
ZJS
273 return 0;
274}
50d9e46d 275
1c73f3bc
ZJS
276static int acl_entry_equal(acl_entry_t a, acl_entry_t b) {
277 acl_tag_t tag_a, tag_b;
278
279 if (acl_get_tag_type(a, &tag_a) < 0)
280 return -errno;
281
282 if (acl_get_tag_type(b, &tag_b) < 0)
283 return -errno;
284
285 if (tag_a != tag_b)
286 return false;
287
288 switch (tag_a) {
289 case ACL_USER_OBJ:
290 case ACL_GROUP_OBJ:
291 case ACL_MASK:
292 case ACL_OTHER:
293 /* can have only one of those */
294 return true;
295 case ACL_USER: {
76dcbc49 296 _cleanup_(acl_free_uid_tpp) uid_t *uid_a = NULL, *uid_b = NULL;
1c73f3bc
ZJS
297
298 uid_a = acl_get_qualifier(a);
299 if (!uid_a)
300 return -errno;
301
302 uid_b = acl_get_qualifier(b);
303 if (!uid_b)
304 return -errno;
305
306 return *uid_a == *uid_b;
307 }
308 case ACL_GROUP: {
76dcbc49 309 _cleanup_(acl_free_gid_tpp) gid_t *gid_a = NULL, *gid_b = NULL;
1c73f3bc
ZJS
310
311 gid_a = acl_get_qualifier(a);
312 if (!gid_a)
313 return -errno;
314
315 gid_b = acl_get_qualifier(b);
316 if (!gid_b)
317 return -errno;
318
319 return *gid_a == *gid_b;
320 }
321 default:
322 assert_not_reached("Unknown acl tag type");
323 }
324}
325
326static int find_acl_entry(acl_t acl, acl_entry_t entry, acl_entry_t *out) {
327 acl_entry_t i;
328 int r;
329
330 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
331 r > 0;
332 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
333
334 r = acl_entry_equal(i, entry);
335 if (r < 0)
336 return r;
337 if (r > 0) {
338 *out = i;
339 return 1;
340 }
341 }
342 if (r < 0)
343 return -errno;
344 return 0;
345}
346
50d9e46d
ZJS
347int acls_for_file(const char *path, acl_type_t type, acl_t new, acl_t *acl) {
348 _cleanup_(acl_freep) acl_t old;
349 acl_entry_t i;
dd4105b0 350 int r;
50d9e46d
ZJS
351
352 old = acl_get_file(path, type);
353 if (!old)
354 return -errno;
355
dd4105b0
ZJS
356 for (r = acl_get_entry(new, ACL_FIRST_ENTRY, &i);
357 r > 0;
358 r = acl_get_entry(new, ACL_NEXT_ENTRY, &i)) {
50d9e46d
ZJS
359
360 acl_entry_t j;
361
1c73f3bc
ZJS
362 r = find_acl_entry(old, i, &j);
363 if (r < 0)
364 return r;
365 if (r == 0)
366 if (acl_create_entry(&old, &j) < 0)
367 return -errno;
50d9e46d
ZJS
368
369 if (acl_copy_entry(j, i) < 0)
370 return -errno;
371 }
50d9e46d 372 if (r < 0)
dd4105b0 373 return -errno;
50d9e46d 374
1cc6c93a
YW
375 *acl = TAKE_PTR(old);
376
50d9e46d
ZJS
377 return 0;
378}
5c3bde3f
ZJS
379
380int add_acls_for_user(int fd, uid_t uid) {
381 _cleanup_(acl_freep) acl_t acl = NULL;
382 acl_entry_t entry;
383 acl_permset_t permset;
384 int r;
385
386 acl = acl_get_fd(fd);
387 if (!acl)
388 return -errno;
389
390 r = acl_find_uid(acl, uid, &entry);
391 if (r <= 0) {
392 if (acl_create_entry(&acl, &entry) < 0 ||
393 acl_set_tag_type(entry, ACL_USER) < 0 ||
394 acl_set_qualifier(entry, &uid) < 0)
395 return -errno;
396 }
397
398 /* We do not recalculate the mask unconditionally here,
399 * so that the fchmod() mask above stays intact. */
400 if (acl_get_permset(entry, &permset) < 0 ||
401 acl_add_perm(permset, ACL_READ) < 0)
402 return -errno;
403
404 r = calc_acl_mask_if_needed(&acl);
405 if (r < 0)
406 return r;
407
408 return acl_set_fd(fd, acl);
409}