| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include "sd-login.h" |
| 4 |
|
| 5 |
#include "acl-util.h" |
| 6 |
#include "alloc-util.h" |
| 7 |
#include "device-util.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "login-util.h" |
| 11 |
#include "set.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "strv.h" |
| 14 |
#include "udev-builtin.h" |
| 15 |
#include "user-util.h" |
| 16 |
|
| 17 |
static int builtin_uaccess(UdevEvent *event, int argc, char *argv[]) { |
| 18 |
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); |
| 19 |
_cleanup_strv_free_ char **sessions = NULL; |
| 20 |
_cleanup_set_free_ Set *uids = NULL; |
| 21 |
uid_t uid; |
| 22 |
int r = 0, k; |
| 23 |
|
| 24 |
if (event->event_mode != EVENT_UDEV_WORKER) { |
| 25 |
log_device_debug(dev, "Running in test mode, skipping execution of 'uaccess' builtin command."); |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
umask(0022); |
| 30 |
|
| 31 |
/* don't muck around with ACLs when the system is not running systemd */ |
| 32 |
if (!logind_running()) |
| 33 |
return 0; |
| 34 |
|
| 35 |
_cleanup_close_ int fd = sd_device_open(dev, O_CLOEXEC|O_PATH); |
| 36 |
if (fd < 0) { |
| 37 |
bool ignore = ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(fd); |
| 38 |
log_device_full_errno(dev, ignore ? LOG_DEBUG : LOG_WARNING, fd, |
| 39 |
"Failed to open device node%s: %m", |
| 40 |
ignore ? ", ignoring" : ""); |
| 41 |
return ignore ? 0 : fd; |
| 42 |
} |
| 43 |
|
| 44 |
r = sd_device_has_tag(dev, "uaccess"); |
| 45 |
if (r < 0) |
| 46 |
return log_device_error_errno(dev, r, "Failed to query uaccess tag: %m"); |
| 47 |
|
| 48 |
if (r > 0) { |
| 49 |
const char *seat; |
| 50 |
r = device_get_seat(dev, &seat); |
| 51 |
if (r < 0) |
| 52 |
return log_device_error_errno(dev, r, "Failed to get seat: %m"); |
| 53 |
|
| 54 |
r = sd_seat_get_active(seat, /* ret_session= */ NULL, &uid); |
| 55 |
if (IN_SET(r, -ENXIO, -ENODATA)) |
| 56 |
/* No active session on this seat */ |
| 57 |
r = 0; |
| 58 |
else if (r < 0) |
| 59 |
log_device_error_errno(dev, r, "Failed to determine active user on seat %s, ignoring: %m", seat); |
| 60 |
else { |
| 61 |
if (set_ensure_put(&uids, NULL, UID_TO_PTR(uid)) < 0) |
| 62 |
return log_oom(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
bool has_xaccess = false; |
| 67 |
FOREACH_DEVICE_CURRENT_TAG(dev, tag) |
| 68 |
if (startswith(tag, "xaccess-")) { |
| 69 |
has_xaccess = true; |
| 70 |
break; |
| 71 |
} |
| 72 |
|
| 73 |
if (has_xaccess) { |
| 74 |
r = sd_get_sessions(&sessions); |
| 75 |
if (r < 0) |
| 76 |
return log_device_error_errno(dev, r, "Failed to list sessions: %m"); |
| 77 |
|
| 78 |
STRV_FOREACH(s, sessions) { |
| 79 |
_cleanup_free_ char *state = NULL; |
| 80 |
r = sd_session_get_state(*s, &state); |
| 81 |
if (r < 0) { |
| 82 |
log_device_debug_errno(dev, r, "Failed to query state for session %s, ignoring: %m", *s); |
| 83 |
continue; |
| 84 |
} |
| 85 |
if (streq(state, "closing")) |
| 86 |
continue; |
| 87 |
|
| 88 |
r = sd_session_get_uid(*s, &uid); |
| 89 |
if (r < 0) { |
| 90 |
log_device_debug_errno(dev, r, "Failed to query uid for session %s, ignoring: %m", *s); |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
_cleanup_strv_free_ char **extra_devices = NULL; |
| 95 |
r = sd_session_get_extra_device_access(*s, &extra_devices); |
| 96 |
if (r < 0) { |
| 97 |
log_device_debug_errno(dev, r, "Failed to query extra device access for session %s, ignoring: %m", *s); |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
bool match = false; |
| 102 |
STRV_FOREACH(id, extra_devices) { |
| 103 |
_cleanup_free_ char *tag = strjoin("xaccess-", *id); |
| 104 |
if (!tag) |
| 105 |
return log_oom(); |
| 106 |
|
| 107 |
r = sd_device_has_current_tag(dev, tag); |
| 108 |
if (r < 0) |
| 109 |
return log_device_error_errno(dev, r, "Failed to query %s tag: %m", tag); |
| 110 |
if (r > 0) { |
| 111 |
match = true; |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
if (!match) |
| 116 |
continue; |
| 117 |
|
| 118 |
if (set_ensure_put(&uids, NULL, UID_TO_PTR(uid)) < 0) |
| 119 |
return log_oom(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
r = devnode_acl(fd, uids); |
| 124 |
if (r < 0) { |
| 125 |
log_device_full_errno(dev, r == -ENOENT ? LOG_DEBUG : LOG_ERR, r, "Failed to apply ACL: %m"); |
| 126 |
goto reset; |
| 127 |
} |
| 128 |
|
| 129 |
return 0; |
| 130 |
|
| 131 |
reset: |
| 132 |
/* Better be safe than sorry and reset ACL */ |
| 133 |
k = devnode_acl(fd, /* uids= */ NULL); |
| 134 |
if (k < 0) |
| 135 |
RET_GATHER(r, log_device_full_errno(dev, k == -ENOENT ? LOG_DEBUG : LOG_ERR, k, "Failed to flush ACLs: %m")); |
| 136 |
|
| 137 |
return r; |
| 138 |
} |
| 139 |
|
| 140 |
const UdevBuiltin udev_builtin_uaccess = { |
| 141 |
.name = "uaccess", |
| 142 |
.cmd = builtin_uaccess, |
| 143 |
.help = "Manage device node user ACL", |
| 144 |
}; |