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