chiark / gitweb /
af3b4a4d2dce3cf984cc23271b077a8c5ea155fc
[elogind.git] / src / login / logind-acl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2011 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <string.h>
10
11 #include "acl-util.h"
12 #include "alloc-util.h"
13 #include "dirent-util.h"
14 #include "escape.h"
15 #include "fd-util.h"
16 #include "format-util.h"
17 #include "logind-acl.h"
18 #include "set.h"
19 #include "string-util.h"
20 #include "udev-util.h"
21 #include "util.h"
22
23 static int flush_acl(acl_t acl) {
24         acl_entry_t i;
25         int found;
26         bool changed = false;
27
28         assert(acl);
29
30         for (found = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
31              found > 0;
32              found = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
33
34                 acl_tag_t tag;
35
36                 if (acl_get_tag_type(i, &tag) < 0)
37                         return -errno;
38
39                 if (tag != ACL_USER)
40                         continue;
41
42                 if (acl_delete_entry(acl, i) < 0)
43                         return -errno;
44
45                 changed = true;
46         }
47
48         if (found < 0)
49                 return -errno;
50
51         return changed;
52 }
53
54 int devnode_acl(const char *path,
55                 bool flush,
56                 bool del, uid_t old_uid,
57                 bool add, uid_t new_uid) {
58
59         acl_t acl;
60         int r = 0;
61         bool changed = false;
62
63         assert(path);
64
65         acl = acl_get_file(path, ACL_TYPE_ACCESS);
66         if (!acl)
67                 return -errno;
68
69         if (flush) {
70
71                 r = flush_acl(acl);
72                 if (r < 0)
73                         goto finish;
74                 if (r > 0)
75                         changed = true;
76
77         } else if (del && old_uid > 0) {
78                 acl_entry_t entry;
79
80                 r = acl_find_uid(acl, old_uid, &entry);
81                 if (r < 0)
82                         goto finish;
83
84                 if (r > 0) {
85                         if (acl_delete_entry(acl, entry) < 0) {
86                                 r = -errno;
87                                 goto finish;
88                         }
89
90                         changed = true;
91                 }
92         }
93
94         if (add && new_uid > 0) {
95                 acl_entry_t entry;
96                 acl_permset_t permset;
97                 int rd, wt;
98
99                 r = acl_find_uid(acl, new_uid, &entry);
100                 if (r < 0)
101                         goto finish;
102
103                 if (r == 0) {
104                         if (acl_create_entry(&acl, &entry) < 0) {
105                                 r = -errno;
106                                 goto finish;
107                         }
108
109                         if (acl_set_tag_type(entry, ACL_USER) < 0 ||
110                             acl_set_qualifier(entry, &new_uid) < 0) {
111                                 r = -errno;
112                                 goto finish;
113                         }
114                 }
115
116                 if (acl_get_permset(entry, &permset) < 0) {
117                         r = -errno;
118                         goto finish;
119                 }
120
121                 rd = acl_get_perm(permset, ACL_READ);
122                 if (rd < 0) {
123                         r = -errno;
124                         goto finish;
125                 }
126
127                 wt = acl_get_perm(permset, ACL_WRITE);
128                 if (wt < 0) {
129                         r = -errno;
130                         goto finish;
131                 }
132
133                 if (!rd || !wt) {
134
135                         if (acl_add_perm(permset, ACL_READ|ACL_WRITE) < 0) {
136                                 r = -errno;
137                                 goto finish;
138                         }
139
140                         changed = true;
141                 }
142         }
143
144         if (!changed)
145                 goto finish;
146
147         if (acl_calc_mask(&acl) < 0) {
148                 r = -errno;
149                 goto finish;
150         }
151
152         if (acl_set_file(path, ACL_TYPE_ACCESS, acl) < 0) {
153                 r = -errno;
154                 goto finish;
155         }
156
157         r = 0;
158
159 finish:
160         acl_free(acl);
161
162         return r;
163 }
164
165 int devnode_acl_all(struct udev *udev,
166                     const char *seat,
167                     bool flush,
168                     bool del, uid_t old_uid,
169                     bool add, uid_t new_uid) {
170
171         _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
172         struct udev_list_entry *item = NULL, *first = NULL;
173         _cleanup_set_free_free_ Set *nodes = NULL;
174         _cleanup_closedir_ DIR *dir = NULL;
175         struct dirent *dent;
176         Iterator i;
177         char *n;
178         int r;
179
180         assert(udev);
181
182         nodes = set_new(&path_hash_ops);
183         if (!nodes)
184                 return -ENOMEM;
185
186         e = udev_enumerate_new(udev);
187         if (!e)
188                 return -ENOMEM;
189
190         if (isempty(seat))
191                 seat = "seat0";
192
193         /* We can only match by one tag in libudev. We choose
194          * "uaccess" for that. If we could match for two tags here we
195          * could add the seat name as second match tag, but this would
196          * be hardly optimizable in libudev, and hence checking the
197          * second tag manually in our loop is a good solution. */
198         r = udev_enumerate_add_match_tag(e, "uaccess");
199         if (r < 0)
200                 return r;
201
202         r = udev_enumerate_add_match_is_initialized(e);
203         if (r < 0)
204                 return r;
205
206         r = udev_enumerate_scan_devices(e);
207         if (r < 0)
208                 return r;
209
210         first = udev_enumerate_get_list_entry(e);
211         udev_list_entry_foreach(item, first) {
212                 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
213                 const char *node, *sn;
214
215                 d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
216                 if (!d)
217                         return -ENOMEM;
218
219                 sn = udev_device_get_property_value(d, "ID_SEAT");
220                 if (isempty(sn))
221                         sn = "seat0";
222
223                 if (!streq(seat, sn))
224                         continue;
225
226                 node = udev_device_get_devnode(d);
227                 /* In case people mistag devices with nodes, we need to ignore this */
228                 if (!node)
229                         continue;
230
231                 n = strdup(node);
232                 if (!n)
233                         return -ENOMEM;
234
235                 log_debug("Found udev node %s for seat %s", n, seat);
236                 r = set_consume(nodes, n);
237                 if (r < 0)
238                         return r;
239         }
240
241         /* udev exports "dead" device nodes to allow module on-demand loading,
242          * these devices are not known to the kernel at this moment */
243         dir = opendir("/run/udev/static_node-tags/uaccess");
244         if (dir) {
245                 FOREACH_DIRENT(dent, dir, return -errno) {
246                         _cleanup_free_ char *unescaped_devname = NULL;
247
248                         if (cunescape(dent->d_name, UNESCAPE_RELAX, &unescaped_devname) < 0)
249                                 return -ENOMEM;
250
251                         n = strappend("/dev/", unescaped_devname);
252                         if (!n)
253                                 return -ENOMEM;
254
255                         log_debug("Found static node %s for seat %s", n, seat);
256                         r = set_consume(nodes, n);
257                         if (r == -EEXIST)
258                                 continue;
259                         if (r < 0)
260                                 return r;
261                 }
262         }
263
264         r = 0;
265         SET_FOREACH(n, nodes, i) {
266                 int k;
267
268                 log_debug("Changing ACLs at %s for seat %s (uid "UID_FMT"→"UID_FMT"%s%s)",
269                           n, seat, old_uid, new_uid,
270                           del ? " del" : "", add ? " add" : "");
271
272                 k = devnode_acl(n, flush, del, old_uid, add, new_uid);
273                 if (k == -ENOENT)
274                         log_debug("Device %s disappeared while setting ACLs", n);
275                 else if (k < 0 && r == 0)
276                         r = k;
277         }
278
279         return r;
280 }