Better systemd-homed user detection (fixes #913)

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-01-26 20:36:44 +01:00
parent a4076b83da
commit 5bfa1670cc
2 changed files with 21 additions and 17 deletions

View File

@@ -79,9 +79,6 @@ fn PlatformStruct() type {
pub const vt_activate = vt.VT_ACTIVATE;
pub const vt_waitactive = vt.VT_WAITACTIVE;
const SYSTEMD_HOMED_UID_MIN = 60001;
const SYSTEMD_HOMED_UID_MAX = 60513;
pub fn setUserContextImpl(username: [*:0]const u8, entry: UsernameEntry) !void {
const status = grp.initgroups(username, @intCast(entry.gid));
if (status != 0) return error.GroupInitializationFailed;
@@ -185,19 +182,6 @@ fn PlatformStruct() type {
if (!nameFound) return error.UidNameNotFound;
// This code assumes the OS has a login.defs file with UID_MIN
// and UID_MAX values defined in it, which should be the case
// for most systemd-based Linux distributions out there.
// This should be a good enough safeguard for now, as there's
// no reliable (and clean) way to check for systemd support
if (uid_range.uid_min > SYSTEMD_HOMED_UID_MIN) {
uid_range.uid_min = SYSTEMD_HOMED_UID_MIN;
}
if (uid_range.uid_max < SYSTEMD_HOMED_UID_MAX) {
uid_range.uid_max = SYSTEMD_HOMED_UID_MAX;
}
return uid_range;
}

View File

@@ -1321,13 +1321,33 @@ fn getAllUsernames(allocator: std.mem.Allocator, login_defs_path: []const u8, ui
};
};
// There's no reliable (and clean) way to check for systemd support, so
// let's just define a range and check if a user is within it
const SYSTEMD_HOMED_UID_MIN = 60001;
const SYSTEMD_HOMED_UID_MAX = 60513;
const homed_uid_range = UidRange{
.uid_min = SYSTEMD_HOMED_UID_MIN,
.uid_max = SYSTEMD_HOMED_UID_MAX,
};
var usernames: StringList = .empty;
var maybe_entry = interop.getNextUsernameEntry();
while (maybe_entry) |entry| {
// We check if the UID is equal to 0 because we always want to add root
// as a username (even if you can't log into it)
if (entry.uid >= uid_range.uid_min and entry.uid <= uid_range.uid_max or entry.uid == 0 and entry.username != null) {
const is_within_range =
entry.uid >= uid_range.uid_min and
entry.uid <= uid_range.uid_max;
const is_within_homed_range =
builtin.os.tag == .linux and
entry.uid >= homed_uid_range.uid_min and
entry.uid <= homed_uid_range.uid_max;
const is_root =
entry.uid == 0 and
entry.username != null;
if (is_within_range or is_within_homed_range or is_root) {
const username = try allocator.dupe(u8, entry.username.?);
try usernames.append(allocator, username);
}