Conditionally import login_cap.h with pwd.h

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion 2024-08-05 15:16:58 +02:00
parent 2c428f5537
commit 5e85618730
No known key found for this signature in database
GPG Key ID: 3E85EB44F610AD7F
2 changed files with 9 additions and 17 deletions

View File

@ -66,12 +66,12 @@ pub fn authenticate(config: Config, current_environment: Session.Environment, lo
if (status != interop.pam.PAM_SUCCESS) return pamDiagnose(status); if (status != interop.pam.PAM_SUCCESS) return pamDiagnose(status);
defer status = interop.pam.pam_close_session(handle, 0); defer status = interop.pam.pam_close_session(handle, 0);
var pwd: *interop.passwd = undefined; var pwd: *interop.pwd.passwd = undefined;
{ {
defer interop.endpwent(); defer interop.pwd.endpwent();
// Get password structure from username // Get password structure from username
pwd = interop.getpwnam(login) orelse return error.GetPasswordNameFailed; pwd = interop.pwd.getpwnam(login) orelse return error.GetPasswordNameFailed;
} }
// Set user shell if it hasn't already been set // Set user shell if it hasn't already been set
@ -122,7 +122,7 @@ pub fn authenticate(config: Config, current_environment: Session.Environment, lo
fn startSession( fn startSession(
config: Config, config: Config,
pwd: *interop.passwd, pwd: *interop.pwd.passwd,
handle: ?*interop.pam.pam_handle, handle: ?*interop.pam.pam_handle,
current_environment: Session.Environment, current_environment: Session.Environment,
) !void { ) !void {
@ -132,7 +132,7 @@ fn startSession(
if (status != 0) return error.GroupInitializationFailed; if (status != 0) return error.GroupInitializationFailed;
// FreeBSD sets the GID and UID with setusercontext() // FreeBSD sets the GID and UID with setusercontext()
const result = interop.logincap.setusercontext(null, pwd, pwd.pw_uid, interop.logincap.LOGIN_SETALL); const result = interop.pwd.setusercontext(null, pwd, pwd.pw_uid, interop.pwd.LOGIN_SETALL);
if (result != 0) return error.SetUserUidFailed; if (result != 0) return error.SetUserUidFailed;
} else { } else {
const status = interop.grp.initgroups(pwd.pw_name, pwd.pw_gid); const status = interop.grp.initgroups(pwd.pw_name, pwd.pw_gid);
@ -166,7 +166,7 @@ fn startSession(
} }
} }
fn initEnv(pwd: *interop.passwd, path_env: ?[:0]const u8) !void { fn initEnv(pwd: *interop.pwd.passwd, path_env: ?[:0]const u8) !void {
_ = interop.stdlib.setenv("HOME", pwd.pw_dir, 1); _ = interop.stdlib.setenv("HOME", pwd.pw_dir, 1);
_ = interop.stdlib.setenv("PWD", pwd.pw_dir, 1); _ = interop.stdlib.setenv("PWD", pwd.pw_dir, 1);
_ = interop.stdlib.setenv("SHELL", pwd.pw_shell, 1); _ = interop.stdlib.setenv("SHELL", pwd.pw_shell, 1);

View File

@ -35,18 +35,15 @@ pub const stdlib = @cImport({
pub const pwd = @cImport({ pub const pwd = @cImport({
@cInclude("pwd.h"); @cInclude("pwd.h");
// We include a FreeBSD-specific header here since login_cap.h references
// the passwd struct directly, so we can't import it separately'
if (builtin.os.tag == .freebsd) @cInclude("login_cap.h");
}); });
pub const grp = @cImport({ pub const grp = @cImport({
@cInclude("grp.h"); @cInclude("grp.h");
}); });
// FreeBSD-specific headers (except for pwd.h)
pub const logincap = @cImport({
@cInclude("pwd.h");
@cInclude("login_cap.h");
});
// BSD-specific headers // BSD-specific headers
pub const kbio = @cImport({ pub const kbio = @cImport({
@cInclude("sys/kbio.h"); @cInclude("sys/kbio.h");
@ -61,11 +58,6 @@ pub const vt = @cImport({
@cInclude("sys/vt.h"); @cInclude("sys/vt.h");
}); });
// On FreeBSD, login_cap.h references the passwd struct directly, so we must use logincap.passwd instead
pub const passwd = if (builtin.os.tag == .freebsd) logincap.passwd else pwd.passwd;
pub const endpwent = if (builtin.os.tag == .freebsd) logincap.endpwent else pwd.endpwent;
pub const getpwnam = if (builtin.os.tag == .freebsd) logincap.getpwnam else pwd.getpwnam;
// Used for getting & setting the lock state // Used for getting & setting the lock state
const LedState = if (builtin.os.tag.isBSD()) c_int else c_char; const LedState = if (builtin.os.tag.isBSD()) c_int else c_char;
const get_led_state = if (builtin.os.tag.isBSD()) kbio.KDGETLED else kd.KDGKBLED; const get_led_state = if (builtin.os.tag.isBSD()) kbio.KDGETLED else kd.KDGKBLED;