autrh: Check if faillock file exists, not just dir

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-08-27 10:54:16 +02:00
parent 255af0f1d4
commit 809728e06a

View File

@@ -67,9 +67,12 @@ pub fn authenticate(
password: []const u8, password: []const u8,
maybe_new_password: ?[]const u8, maybe_new_password: ?[]const u8,
) !void { ) !void {
const lock_path = try std.fs.path.join(allocator, &.{ options.faillock_tally_dir, login });
defer allocator.free(lock_path);
var faillock_entries: usize = 0; var faillock_entries: usize = 0;
if (try dirExists(io, options.faillock_tally_dir)) { if (try fileExists(io, lock_path)) {
faillock_entries = try getFaillockEntries(allocator, io, login, options.faillock_tally_dir); faillock_entries = try getFaillockEntries(io, lock_path);
} }
var tty_buffer: [3]u8 = undefined; var tty_buffer: [3]u8 = undefined;
@@ -112,8 +115,8 @@ pub fn authenticate(
// Do the PAM routine // Do the PAM routine
try log_file.info(io, "auth/pam", "authenticating", .{}); try log_file.info(io, "auth/pam", "authenticating", .{});
status = interop.pam.pam_authenticate(handle, 0); status = interop.pam.pam_authenticate(handle, 0);
if (status == interop.pam.PAM_AUTH_ERR and try dirExists(io, options.faillock_tally_dir)) { if (status == interop.pam.PAM_AUTH_ERR and try fileExists(io, lock_path)) {
const new_faillock_entries = try getFaillockEntries(allocator, io, login, options.faillock_tally_dir); const new_faillock_entries = try getFaillockEntries(io, lock_path);
if (faillock_entries == new_faillock_entries) { if (faillock_entries == new_faillock_entries) {
return error.AccountLocked; return error.AccountLocked;
@@ -213,25 +216,17 @@ pub fn authenticate(
if (shared_err.readError()) |err| return err; if (shared_err.readError()) |err| return err;
} }
fn dirExists(io: std.Io, path: []const u8) !bool { fn fileExists(io: std.Io, path: []const u8) !bool {
var dir = std.Io.Dir.openDirAbsolute(io, path, .{}) catch |err| { var file = std.Io.Dir.openFileAbsolute(io, path, .{}) catch |err| {
if (err == error.FileNotFound) return false; if (err == error.FileNotFound) return false;
return err; return err;
}; };
defer dir.close(io); defer file.close(io);
return true; return true;
} }
fn getFaillockEntries( fn getFaillockEntries(io: std.Io, path: []const u8) !usize {
allocator: std.mem.Allocator,
io: std.Io,
username: []const u8,
tally_dir: []const u8,
) !usize {
const path = try std.fs.path.join(allocator, &.{ tally_dir, username });
defer allocator.free(path);
var file = try std.Io.Dir.openFileAbsolute(io, path, .{}); var file = try std.Io.Dir.openFileAbsolute(io, path, .{});
defer file.close(io); defer file.close(io);