Skip to content

Commit

Permalink
Overwrite vim instead of editor in tests on FreeBSD
Browse files Browse the repository at this point in the history
On FreeBSD visudo only tries to run /usr/bin/vi, not /usr/bin/editor.
This also changes sudo-rs to run /usr/bin/vi by default for visudo on
FreeBSD.
  • Loading branch information
bjorn3 committed Dec 10, 2024
1 parent ca2f0d2 commit cc06e93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/visudo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,18 @@ fn edit_sudoers_file(
}

fn editor_path_fallback() -> io::Result<PathBuf> {
let path = Path::new("/usr/bin/editor");
if can_execute(path) {
return Ok(path.to_owned());
let editors = if cfg!(target_os = "linux") {
&["/usr/bin/editor"][..]
} else if cfg!(target_os = "freebsd") {
&["/usr/bin/vi"]
} else {
unimplemented!("unknown default editor for target")
};
for &editor in editors {
let path = Path::new(editor);
if can_execute(path) {
return Ok(path.to_owned());
}
}

Err(io::Error::new(
Expand Down
3 changes: 3 additions & 0 deletions test-framework/sudo-compliance-tests/src/visudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ macro_rules! assert_snapshot {
}

const TMP_SUDOERS: &str = "/tmp/sudoers";
#[cfg(not(target_os = "freebsd"))]
const DEFAULT_EDITOR: &str = "/usr/bin/editor";
#[cfg(target_os = "freebsd")]
const DEFAULT_EDITOR: &str = "/usr/bin/vi";
const LOGS_PATH: &str = "/tmp/logs.txt";
const CHMOD_EXEC: &str = "100";
const EDITOR_DUMMY: &str = "#!/bin/sh
Expand Down

0 comments on commit cc06e93

Please sign in to comment.