Fix user detection if /etc/login.defs does not contain both UID_MIN and UID_MAX (#1014)

## What are the changes about?

I modified getUserIdRange(), so it checks if either of UID_MIN and UID_MAX is set in /etc/login.defs. If any, but not both, is not set, it uses fallback value. I don't know zig build system, so I added ugly fallback values passthrough.

## What existing issue does this resolve?

#1013

## Pre-requisites

- [X] I have tested & confirmed the changes work locally
- [X] I have read and fully adhere to the rules set in the contributing guidelines found in `CONTRIBUTING.md`

Reviewed-on: https://codeberg.org/fairyglade/ly/pulls/1014
Reviewed-by: AnErrupTion <anerruption+codeberg@disroot.org>
This commit is contained in:
Дмитрий 2026-06-12 12:09:37 +02:00 committed by AnErrupTion
parent a07ca88c74
commit 008d413295
4 changed files with 30 additions and 4 deletions

View File

@ -75,6 +75,8 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
.enable_x11_support = enable_x11_support,
.fallback_uid_min = fallback_uid_min,
.fallback_uid_max = fallback_uid_max
});
exe.root_module.addImport("ly-ui", ly_ui.module("ly-ui"));

View File

@ -11,6 +11,13 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
const fallback_uid_min = b.option(std.posix.uid_t, "fallback_uid_min", "Set the fallback minimum UID (default is 1000). This value gets embedded into the binary").?;
const fallback_uid_max = b.option(std.posix.uid_t, "fallback_uid_max", "Set the fallback maximum UID (default is 60000). This value gets embedded into the binary").?;
const build_options = b.addOptions();
build_options.addOption(std.posix.uid_t, "fallback_uid_min", fallback_uid_min);
build_options.addOption(std.posix.uid_t, "fallback_uid_max", fallback_uid_max);
mod.addOptions("build_options", build_options);
const zigini = b.dependency("zigini", .{ .target = target, .optimize = optimize });
mod.addImport("zigini", zigini.module("zigini"));

View File

@ -1,5 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const build_options = @import("build_options");
const UidRange = @import("UidRange.zig");
const pwd = @import("pwd");
const stdlib = @import("stdlib");
@ -145,21 +146,32 @@ fn PlatformStruct() type {
var iterator = std.mem.splitScalar(u8, login_defs_buffer, '\n');
var uid_range = UidRange{};
var nameFound = false;
var uid_min_Found = false;
var uid_max_Found = false;
while (iterator.next()) |line| {
const trimmed_line = std.mem.trim(u8, line, " \n\r\t");
if (std.mem.startsWith(u8, trimmed_line, "UID_MIN")) {
uid_range.uid_min = try parseValue(std.posix.uid_t, "UID_MIN", trimmed_line);
nameFound = true;
uid_min_Found = true;
} else if (std.mem.startsWith(u8, trimmed_line, "UID_MAX")) {
uid_range.uid_max = try parseValue(std.posix.uid_t, "UID_MAX", trimmed_line);
nameFound = true;
uid_max_Found = true;
}
}
if (!nameFound) return error.UidNameNotFound;
if (!(uid_min_Found or uid_max_Found)) {
return error.UidNameNotFound;
}
if (!uid_min_Found) {
uid_range.uid_min = build_options.fallback_uid_min;
}
if (!uid_max_Found) {
uid_range.uid_max = build_options.fallback_uid_max;
}
return uid_range;
}

View File

@ -11,10 +11,15 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
const fallback_uid_min = b.option(std.posix.uid_t, "fallback_uid_min", "Set the fallback minimum UID (default is 1000). This value gets embedded into the binary");
const fallback_uid_max = b.option(std.posix.uid_t, "fallback_uid_max", "Set the fallback maximum UID (default is 60000). This value gets embedded into the binary");
const ly_core = b.dependency("ly_core", .{
.target = target,
.optimize = optimize,
.enable_x11_support = enable_x11_support,
.fallback_uid_min = fallback_uid_min,
.fallback_uid_max = fallback_uid_max
});
mod.addImport("ly-core", ly_core.module("ly-core"));