chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / shared / acl-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdbool.h>
5
6 #include "acl-util.h"
7 #include "alloc-util.h"
8 #include "string-util.h"
9 #include "strv.h"
10 #include "user-util.h"
11 #include "util.h"
12
13 int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry) {
14         acl_entry_t i;
15         int r;
16
17         assert(acl);
18         assert(entry);
19
20         for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
21              r > 0;
22              r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
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         }
46         if (r < 0)
47                 return -errno;
48
49         return 0;
50 }
51
52 #if 0 /// UNNEEDED by elogind
53 int calc_acl_mask_if_needed(acl_t *acl_p) {
54         acl_entry_t i;
55         int r;
56         bool need = false;
57
58         assert(acl_p);
59
60         for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
61              r > 0;
62              r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
63                 acl_tag_t tag;
64
65                 if (acl_get_tag_type(i, &tag) < 0)
66                         return -errno;
67
68                 if (tag == ACL_MASK)
69                         return 0;
70
71                 if (IN_SET(tag, ACL_USER, ACL_GROUP))
72                         need = true;
73         }
74         if (r < 0)
75                 return -errno;
76
77         if (need && acl_calc_mask(acl_p) < 0)
78                 return -errno;
79
80         return need;
81 }
82
83 int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
84         acl_entry_t i;
85         int r;
86         bool have_user_obj = false, have_group_obj = false, have_other = false;
87         struct stat st;
88         _cleanup_(acl_freep) acl_t basic = NULL;
89
90         assert(acl_p);
91
92         for (r = acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
93              r > 0;
94              r = acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
95                 acl_tag_t tag;
96
97                 if (acl_get_tag_type(i, &tag) < 0)
98                         return -errno;
99
100                 if (tag == ACL_USER_OBJ)
101                         have_user_obj = true;
102                 else if (tag == ACL_GROUP_OBJ)
103                         have_group_obj = true;
104                 else if (tag == ACL_OTHER)
105                         have_other = true;
106                 if (have_user_obj && have_group_obj && have_other)
107                         return 0;
108         }
109         if (r < 0)
110                 return -errno;
111
112         r = stat(path, &st);
113         if (r < 0)
114                 return -errno;
115
116         basic = acl_from_mode(st.st_mode);
117         if (!basic)
118                 return -errno;
119
120         for (r = acl_get_entry(basic, ACL_FIRST_ENTRY, &i);
121              r > 0;
122              r = acl_get_entry(basic, ACL_NEXT_ENTRY, &i)) {
123                 acl_tag_t tag;
124                 acl_entry_t dst;
125
126                 if (acl_get_tag_type(i, &tag) < 0)
127                         return -errno;
128
129                 if ((tag == ACL_USER_OBJ && have_user_obj) ||
130                     (tag == ACL_GROUP_OBJ && have_group_obj) ||
131                     (tag == ACL_OTHER && have_other))
132                         continue;
133
134                 r = acl_create_entry(acl_p, &dst);
135                 if (r < 0)
136                         return -errno;
137
138                 r = acl_copy_entry(dst, i);
139                 if (r < 0)
140                         return -errno;
141         }
142         if (r < 0)
143                 return -errno;
144         return 0;
145 }
146
147 int acl_search_groups(const char *path, char ***ret_groups) {
148         _cleanup_strv_free_ char **g = NULL;
149         _cleanup_(acl_freep) acl_t acl = NULL;
150         bool ret = false;
151         acl_entry_t entry;
152         int r;
153
154         assert(path);
155
156         acl = acl_get_file(path, ACL_TYPE_DEFAULT);
157         if (!acl)
158                 return -errno;
159
160         r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
161         for (;;) {
162                 _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
163                 acl_tag_t tag;
164
165                 if (r < 0)
166                         return -errno;
167                 if (r == 0)
168                         break;
169
170                 if (acl_get_tag_type(entry, &tag) < 0)
171                         return -errno;
172
173                 if (tag != ACL_GROUP)
174                         goto next;
175
176                 gid = acl_get_qualifier(entry);
177                 if (!gid)
178                         return -errno;
179
180                 if (in_gid(*gid) > 0) {
181                         if (!ret_groups)
182                                 return true;
183
184                         ret = true;
185                 }
186
187                 if (ret_groups) {
188                         char *name;
189
190                         name = gid_to_name(*gid);
191                         if (!name)
192                                 return -ENOMEM;
193
194                         r = strv_consume(&g, name);
195                         if (r < 0)
196                                 return r;
197                 }
198
199         next:
200                 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
201         }
202
203         if (ret_groups)
204                 *ret_groups = TAKE_PTR(g);
205
206         return ret;
207 }
208
209 int parse_acl(const char *text, acl_t *acl_access, acl_t *acl_default, bool want_mask) {
210         _cleanup_free_ char **a = NULL, **d = NULL; /* strings are not freed */
211         _cleanup_strv_free_ char **split;
212         char **entry;
213         int r = -EINVAL;
214         _cleanup_(acl_freep) acl_t a_acl = NULL, d_acl = NULL;
215
216         split = strv_split(text, ",");
217         if (!split)
218                 return -ENOMEM;
219
220         STRV_FOREACH(entry, split) {
221                 char *p;
222
223                 p = startswith(*entry, "default:");
224                 if (!p)
225                         p = startswith(*entry, "d:");
226
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 int add_acls_for_user(int fd, uid_t uid) {
382         _cleanup_(acl_freep) acl_t acl = NULL;
383         acl_entry_t entry;
384         acl_permset_t permset;
385         int r;
386
387         acl = acl_get_fd(fd);
388         if (!acl)
389                 return -errno;
390
391         r = acl_find_uid(acl, uid, &entry);
392         if (r <= 0) {
393                 if (acl_create_entry(&acl, &entry) < 0 ||
394                     acl_set_tag_type(entry, ACL_USER) < 0 ||
395                     acl_set_qualifier(entry, &uid) < 0)
396                         return -errno;
397         }
398
399         /* We do not recalculate the mask unconditionally here,
400          * so that the fchmod() mask above stays intact. */
401         if (acl_get_permset(entry, &permset) < 0 ||
402             acl_add_perm(permset, ACL_READ) < 0)
403                 return -errno;
404
405         r = calc_acl_mask_if_needed(&acl);
406         if (r < 0)
407                 return r;
408
409         return acl_set_fd(fd, acl);
410 }
411 #endif // 0