]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-uaccess.c
Merge pull request #7386 from keszybz/spdx
[thirdparty/systemd.git] / src / udev / udev-builtin-uaccess.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * manage device node user ACL
4 *
5 * Copyright 2010-2012 Kay Sievers <kay@vrfy.org>
6 * Copyright 2010 Lennart Poettering
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "sd-login.h"
27
28 #include "login-util.h"
29 #include "logind-acl.h"
30 #include "udev.h"
31 #include "util.h"
32
33 static int builtin_uaccess(struct udev_device *dev, int argc, char *argv[], bool test) {
34 int r;
35 const char *path = NULL, *seat;
36 bool changed_acl = false;
37 uid_t uid;
38
39 umask(0022);
40
41 /* don't muck around with ACLs when the system is not running systemd */
42 if (!logind_running())
43 return 0;
44
45 path = udev_device_get_devnode(dev);
46 seat = udev_device_get_property_value(dev, "ID_SEAT");
47 if (!seat)
48 seat = "seat0";
49
50 r = sd_seat_get_active(seat, NULL, &uid);
51 if (IN_SET(r, -ENXIO, -ENODATA)) {
52 /* No active session on this seat */
53 r = 0;
54 goto finish;
55 } else if (r < 0) {
56 log_error("Failed to determine active user on seat %s.", seat);
57 goto finish;
58 }
59
60 r = devnode_acl(path, true, false, 0, true, uid);
61 if (r < 0) {
62 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_ERR, r, "Failed to apply ACL on %s: %m", path);
63 goto finish;
64 }
65
66 changed_acl = true;
67 r = 0;
68
69 finish:
70 if (path && !changed_acl) {
71 int k;
72
73 /* Better be safe than sorry and reset ACL */
74 k = devnode_acl(path, true, false, 0, false, 0);
75 if (k < 0) {
76 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, k, "Failed to apply ACL on %s: %m", path);
77 if (r >= 0)
78 r = k;
79 }
80 }
81
82 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
83 }
84
85 const struct udev_builtin udev_builtin_uaccess = {
86 .name = "uaccess",
87 .cmd = builtin_uaccess,
88 .help = "Manage device node user ACL",
89 };