Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/sudo/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::os::unix::ffi::OsStrExt;
use std::{borrow::Cow, mem};

use crate::common::{SudoPath, SudoString};
use crate::log::user_warn;

pub mod help;
pub mod help_edit;
Expand Down Expand Up @@ -649,8 +650,8 @@ impl SudoOptions {
options.bell = true;
}
"-E" | "--preserve-env" => {
eprintln_ignore_io_error!(
"warning: preserving the entire environment is not supported, `{flag}` is ignored"
user_warn!(
"preserving the entire environment is not supported, `{flag}` is ignored"
)
}
"-e" | "--edit" if !invoked_as_sudoedit => {
Expand Down Expand Up @@ -740,7 +741,7 @@ impl SudoOptions {
&& !is_dir
&& (cmd.ends_with("sudoedit") || cmd.ends_with("sudoedit-rs"))
{
eprintln_ignore_io_error!("sudoedit doesn't need to be run via sudo");
user_warn!("sudoedit doesn't need to be run via sudo");
options.edit = true;
rest.remove(0);
}
Expand Down
4 changes: 2 additions & 2 deletions src/sudo/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ macro_rules! diagnostic {
if let Some(range) = $pos {
$crate::sudo::diagnostic::cited_error(&format!($str), range, $path);
} else {
eprintln_ignore_io_error!("sudo-rs: {}", format!($str));
eprintln_ignore_io_error!("sudo: {}", format!($str));
}
};
($str:expr) => {{
eprintln_ignore_io_error!("sudo-rs: {}", format!($str));
eprintln_ignore_io_error!("sudo: {}", format!($str));
}};
}

Expand Down
28 changes: 14 additions & 14 deletions src/sudo/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{io, process};

use crate::common::SudoPath;
use crate::exec::ExitReason;
use crate::log::user_info;
use crate::log::{user_error, user_info};
use crate::system::file::{create_temporary_dir, FileLock};
use crate::system::wait::{Wait, WaitError, WaitOptions};
use crate::system::{fork, ForkResult};
Expand Down Expand Up @@ -157,8 +157,8 @@ struct TempDirDropGuard(PathBuf);
impl Drop for TempDirDropGuard {
fn drop(&mut self) {
if let Err(e) = std::fs::remove_dir_all(&self.0) {
eprintln_ignore_io_error!(
"Failed to remove temporary directory {}: {e}",
user_error!(
"failed to remove temporary directory {}: {e}",
self.0.display(),
);
};
Expand All @@ -169,7 +169,7 @@ fn handle_child(editor: &Path, file: Vec<ChildFileInfo<'_>>) -> ! {
match handle_child_inner(editor, file) {
Ok(()) => process::exit(0),
Err(err) => {
eprintln_ignore_io_error!("{err}");
user_error!("{err}");
process::exit(1);
}
}
Expand All @@ -184,15 +184,15 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
}

let tempdir = TempDirDropGuard(
create_temporary_dir().map_err(|e| format!("Failed to create temporary directory: {e}"))?,
create_temporary_dir().map_err(|e| format!("failed to create temporary directory: {e}"))?,
);

for (i, file) in files.iter_mut().enumerate() {
// Create temp file
let dir = tempdir.0.join(format!("{i}"));
std::fs::create_dir(&dir).map_err(|e| {
format!(
"Failed to create temporary directory {}: {e}",
"failed to create temporary directory {}: {e}",
dir.display(),
)
})?;
Expand All @@ -205,15 +205,15 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
.open(&tempfile_path)
.map_err(|e| {
format!(
"Failed to create temporary file {}: {e}",
"failed to create temporary file {}: {e}",
tempfile_path.display(),
)
})?;

// Write to temp file
tempfile.write_all(&file.old_data).map_err(|e| {
format!(
"Failed to write to temporary file {}: {e}",
"failed to write to temporary file {}: {e}",
tempfile_path.display(),
)
})?;
Expand All @@ -229,7 +229,7 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
.map(|file| file.tempfile_path.as_ref().expect("filled in above")),
)
.status()
.map_err(|e| format!("Failed to run editor {}: {e}", editor.display()))?;
.map_err(|e| format!("failed to run editor {}: {e}", editor.display()))?;

if !status.success() {
drop(tempdir);
Expand All @@ -246,15 +246,15 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
// Read from temp file
let new_data = std::fs::read(tempfile_path).map_err(|e| {
format!(
"Failed to read from temporary file {}: {e}",
"failed to read from temporary file {}: {e}",
tempfile_path.display(),
)
})?;

// FIXME preserve temporary file if the original couldn't be written to
std::fs::remove_file(tempfile_path).map_err(|e| {
format!(
"Failed to remove temporary file {}: {e}",
"failed to remove temporary file {}: {e}",
tempfile_path.display(),
)
})?;
Expand All @@ -271,11 +271,11 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
) {
Ok(b'y') => {}
_ => {
eprintln_ignore_io_error!("Not overwriting {}", file.path.display());
user_info!("not overwriting {}", file.path.display());

// Parent ignores write when new data matches old data
write_stream(&mut file.new_data_tx, &file.old_data)
.map_err(|e| format!("Failed to write data to parent: {e}"))?;
.map_err(|e| format!("failed to write data to parent: {e}"))?;

continue;
}
Expand All @@ -284,7 +284,7 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul

// Write to socket
write_stream(&mut file.new_data_tx, &new_data)
.map_err(|e| format!("Failed to write data to parent: {e}"))?;
.map_err(|e| format!("failed to write data to parent: {e}"))?;
}

process::exit(0);
Expand Down
4 changes: 2 additions & 2 deletions src/sudo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ fn sudo_process() -> Result<(), Error> {
match SudoAction::from_env() {
Ok(action) => match action {
SudoAction::Help(_) => {
eprintln_ignore_io_error!("{}", long_help());
println_ignore_io_error!("{}", long_help());
std::process::exit(0);
}
SudoAction::Version(_) => {
eprintln_ignore_io_error!("sudo-rs {VERSION}");
println_ignore_io_error!("sudo-rs {VERSION}");
std::process::exit(0);
}
SudoAction::RemoveTimestamp(_) => {
Expand Down
4 changes: 2 additions & 2 deletions src/sudoers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ impl Parse for MetaOrTag {
// this is less fatal
"LOG_INPUT" | "NOLOG_INPUT" | "LOG_OUTPUT" | "NOLOG_OUTPUT" | "MAIL" | "NOMAIL"
| "FOLLOW" => {
eprintln_ignore_io_error!(
"warning: {} tags are ignored by sudo-rs",
crate::log::user_warn!(
"{} tags in the sudoers policy are ignored by sudo-rs",
keyword.as_str()
);
switch(|_| {})?
Expand Down
2 changes: 1 addition & 1 deletion test-framework/e2e-tests/src/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn no_permissions_should_not_violate_io_safety() {

assert_eq!(
stderr,
"sudo-rs: cannot execute '/usr/bin/foo': Permission denied (os error 13)"
"sudo: cannot execute '/usr/bin/foo': Permission denied (os error 13)"
);
}

Expand Down
19 changes: 19 additions & 0 deletions test-framework/sudo-compliance-tests/src/sudo/flag_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,22 @@ fn does_not_panic_on_io_errors() -> Result<()> {

Ok(())
}

#[test]
fn prints_on_stdout() -> Result<()> {
let env = Env("").build();

let output = Command::new("sudo").args(["--help"]).output(&env);

let output = output.stdout();
assert_starts_with!(
output,
if sudo_test::is_original_sudo() {
"sudo - execute a command as another user"
} else {
"sudo - run commands as another user"
}
);

Ok(())
}
4 changes: 2 additions & 2 deletions test-framework/sudo-compliance-tests/src/sudo/flag_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ fn resolves_command_in_invoking_users_path_fail() {
let diagnostic = if sudo_test::is_original_sudo() {
"sudo: true: command not found"
} else {
"sudo-rs: 'true': command not found"
"sudo: 'true': command not found"
};
assert_eq!(output.stderr(), diagnostic);
}
Expand Down Expand Up @@ -453,7 +453,7 @@ fn relative_path_does_not_exist() {
let diagnostic = if sudo_test::is_original_sudo() {
format!("sudo: {prog_rel_path}: command not found")
} else {
format!("sudo-rs: '{prog_rel_path}': command not found")
format!("sudo: '{prog_rel_path}': command not found")
};
assert_contains!(output.stderr(), diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn other_user_does_not_exist() {
let diagnostic = if sudo_test::is_original_sudo() {
format!("sudo: unknown user {USERNAME}")
} else {
format!("sudo-rs: user '{USERNAME}' not found")
format!("sudo: user '{USERNAME}' not found")
};
assert_contains!(output.stderr(), diagnostic);
}
Expand Down
19 changes: 19 additions & 0 deletions test-framework/sudo-compliance-tests/src/sudo/flag_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ fn does_not_panic_on_io_errors() -> Result<()> {

Ok(())
}

#[test]
fn prints_on_stdout() -> Result<()> {
let env = Env("").build();

let output = Command::new("sudo").args(["--version"]).output(&env);

let output = output.stdout();
assert_starts_with!(
output,
if sudo_test::is_original_sudo() {
"Sudo version"
} else {
"sudo-rs"
}
);

Ok(())
}
2 changes: 1 addition & 1 deletion test-framework/sudo-compliance-tests/src/sudo/nopasswd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn v_flag_without_pwd_fails_if_nopasswd_is_not_set_for_all_users_entries() {
} else {
assert_contains!(
stderr,
"[sudo: authenticate] Password: \nsudo: Authentication failed, try again.\n[sudo: authenticate] Password: \nsudo: Authentication failed, try again.\n[sudo: authenticate] Password: \nsudo-rs: Maximum 3 incorrect authentication attempts"
"[sudo: authenticate] Password: \nsudo: Authentication failed, try again.\n[sudo: authenticate] Password: \nsudo: Authentication failed, try again.\n[sudo: authenticate] Password: \nsudo: Maximum 3 incorrect authentication attempts"
);
}
}
2 changes: 1 addition & 1 deletion test-framework/sudo-compliance-tests/src/sudoedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ rm $1",
if sudo_test::is_original_sudo() {
assert_contains!(stderr, format!("sudoedit: {ETC_SUDOERS} left unmodified"));
} else {
assert_contains!(stderr, format!("Failed to read from temporary file"));
assert_contains!(stderr, format!("sudo: failed to read from temporary file"));
}
}

Expand Down
Loading