Skip to content

Commit

Permalink
auth: fix getting username on FreeBSD
Browse files Browse the repository at this point in the history
The current code accidentally used the linux getuid() syscall directly.
Instead import the relevant libc headers to ensure maximum portability
  • Loading branch information
ifreund committed Feb 6, 2023
1 parent 75b8dbe commit 562ec1e
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/auth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const log = std.log;
const mem = std.mem;
const os = std.os;

const c = @cImport({
@cInclude("unistd.h"); // getuid()
@cInclude("pwd.h"); // getpwuid()
});

const pam = @import("pam.zig");

const PasswordBuffer = @import("PasswordBuffer.zig");
Expand Down Expand Up @@ -61,7 +66,7 @@ pub fn run(conn: Connection) noreturn {
var pamh: *pam.Handle = undefined;

{
const pw = getpwuid(os.linux.getuid()) orelse {
const pw = @as(?*c.struct_passwd, c.getpwuid(c.getuid())) orelse {
log.err("failed to get name of current user", .{});
os.exit(1);
};
Expand Down Expand Up @@ -171,19 +176,3 @@ fn converse(

return .success;
}

// TODO: upstream these to the zig standard library
pub const passwd = extern struct {
pw_name: [*:0]const u8,
pw_passwd: [*:0]const u8,
pw_uid: os.uid_t,
pw_gid: os.gid_t,
pw_change: os.time_t,
pw_class: [*:0]const u8,
pw_gecos: [*:0]const u8,
pw_dir: [*:0]const u8,
pw_shell: [*:0]const u8,
pw_expire: os.time_t,
};

pub extern fn getpwuid(uid: os.uid_t) ?*passwd;

0 comments on commit 562ec1e

Please sign in to comment.