Skip to content

Commit

Permalink
Make it run on FreeBSD (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 authored Nov 19, 2024
2 parents fcb2b61 + 380bddd commit 9b2b25f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/su/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ fn authenticate(
user: &str,
login: bool,
) -> Result<PamContext<CLIConverser>, Error> {
let context = if login { "su-l" } else { "su" };
// FIXME make it configurable by the packager
let context = if login && cfg!(target_os = "linux") {
"su-l"
} else {
"su"
};
let use_stdin = true;
let mut pam = PamContext::builder_cli("su", use_stdin, false)
.target_user(user)
Expand Down
13 changes: 8 additions & 5 deletions src/sudo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ you are unsure how to do this then this software is not suited for you at this t
const VERSION: &str = std::env!("CARGO_PKG_VERSION");

pub(crate) fn candidate_sudoers_file() -> &'static Path {
let pb_rs: &'static Path = Path::new("/etc/sudoers-rs");
if pb_rs.exists() {
dev_info!("Running with /etc/sudoers-rs file");
let pb_rs = Path::new("/etc/sudoers-rs");
let file = if pb_rs.exists() {
pb_rs
} else if cfg!(target_os = "freebsd") {
// FIXME maybe make this configurable by the packager?
Path::new("/usr/local/etc/sudoers")
} else {
dev_info!("Running with /etc/sudoers file");
Path::new("/etc/sudoers")
}
};
dev_info!("Running with {} file", file.display());
file
}

#[derive(Default)]
Expand Down
7 changes: 6 additions & 1 deletion src/sudo/pam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ pub fn init_pam(
auth_user: &str,
requesting_user: &str,
) -> PamResult<PamContext<CLIConverser>> {
let service_name = if is_login_shell { "sudo-i" } else { "sudo" };
// FIXME make it configurable by the packager
let service_name = if is_login_shell && cfg!(target_os = "linux") {
"sudo-i"
} else {
"sudo"
};
let mut pam = PamContext::builder_cli("sudo", use_stdin, non_interactive)
.service_name(service_name)
.build()?;
Expand Down
4 changes: 4 additions & 0 deletions src/system/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use std::mem::zeroed;
use crate::common::Error;

pub fn kernel_check(major: u32, minor: u32) -> Result<(), Error> {
if cfg!(target_os = "freebsd") {
return Ok(());
}

let mut utsname: libc::utsname = unsafe { zeroed() };

if unsafe { libc::uname(&mut utsname) } != 0 {
Expand Down
1 change: 1 addition & 0 deletions test-framework/sudo-compliance-tests/src/sudo/pam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn sudo_uses_correct_service_file() -> Result<()> {
}

#[test]
#[cfg_attr(target_os = "freebsd", ignore = "FreeBSD doesn't use sudo-i PAM context")]
fn sudo_dash_i_uses_correct_service_file() -> Result<()> {
let env = Env("ALL ALL=(ALL:ALL) ALL")
.file("/etc/pam.d/sudo-i", "auth sufficient pam_permit.so")
Expand Down

0 comments on commit 9b2b25f

Please sign in to comment.