diff --git a/ly-core/src/interop.zig b/ly-core/src/interop.zig index 5709e8a..1ba8613 100644 --- a/ly-core/src/interop.zig +++ b/ly-core/src/interop.zig @@ -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; } diff --git a/src/main.zig b/src/main.zig index 1b9dc2f..3d8e0cb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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); }