1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
12 #include "alloc-util.h"
13 #include "dirent-util.h"
16 #include "format-util.h"
17 #include "logind-acl.h"
19 #include "string-util.h"
20 #include "udev-util.h"
23 static int flush_acl(acl_t acl) {
30 for (found = acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
32 found = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
36 if (acl_get_tag_type(i, &tag) < 0)
42 if (acl_delete_entry(acl, i) < 0)
54 int devnode_acl(const char *path,
56 bool del, uid_t old_uid,
57 bool add, uid_t new_uid) {
65 acl = acl_get_file(path, ACL_TYPE_ACCESS);
77 } else if (del && old_uid > 0) {
80 r = acl_find_uid(acl, old_uid, &entry);
85 if (acl_delete_entry(acl, entry) < 0) {
94 if (add && new_uid > 0) {
96 acl_permset_t permset;
99 r = acl_find_uid(acl, new_uid, &entry);
104 if (acl_create_entry(&acl, &entry) < 0) {
109 if (acl_set_tag_type(entry, ACL_USER) < 0 ||
110 acl_set_qualifier(entry, &new_uid) < 0) {
116 if (acl_get_permset(entry, &permset) < 0) {
121 rd = acl_get_perm(permset, ACL_READ);
127 wt = acl_get_perm(permset, ACL_WRITE);
135 if (acl_add_perm(permset, ACL_READ|ACL_WRITE) < 0) {
147 if (acl_calc_mask(&acl) < 0) {
152 if (acl_set_file(path, ACL_TYPE_ACCESS, acl) < 0) {
165 int devnode_acl_all(struct udev *udev,
168 bool del, uid_t old_uid,
169 bool add, uid_t new_uid) {
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;
182 nodes = set_new(&path_hash_ops);
186 e = udev_enumerate_new(udev);
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");
202 r = udev_enumerate_add_match_is_initialized(e);
206 r = udev_enumerate_scan_devices(e);
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;
215 d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
219 sn = udev_device_get_property_value(d, "ID_SEAT");
223 if (!streq(seat, sn))
226 node = udev_device_get_devnode(d);
227 /* In case people mistag devices with nodes, we need to ignore this */
235 log_debug("Found udev node %s for seat %s", n, seat);
236 r = set_consume(nodes, n);
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");
245 FOREACH_DIRENT(dent, dir, return -errno) {
246 _cleanup_free_ char *unescaped_devname = NULL;
248 if (cunescape(dent->d_name, UNESCAPE_RELAX, &unescaped_devname) < 0)
251 n = strappend("/dev/", unescaped_devname);
255 log_debug("Found static node %s for seat %s", n, seat);
256 r = set_consume(nodes, n);
265 SET_FOREACH(n, nodes, i) {
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" : "");
272 k = devnode_acl(n, flush, del, old_uid, add, new_uid);
274 log_debug("Device %s disappeared while setting ACLs", n);
275 else if (k < 0 && r == 0)