Skip to content

Commit

Permalink
remove build_from_context: path information is handled in build_from_…
Browse files Browse the repository at this point in the history
…options
  • Loading branch information
squell committed Jan 21, 2025
1 parent c2cadff commit 231cf3f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
17 changes: 14 additions & 3 deletions src/common/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub enum LaunchType {
impl Context {
pub fn build_from_options(
sudo_options: OptionsForContext,
path: String,
secure_path: Option<&str>,
) -> Result<Context, Error> {
let hostname = Hostname::resolve();
let current_user = CurrentUser::resolve()?;
Expand All @@ -76,7 +76,18 @@ impl Context {
// FIXME `Default` is being used as `Option::None`
Default::default()
}
_ => CommandAndArguments::build_from_args(shell, sudo_options.positional_args, &path),
_ => {
let system_path;

let path = if let Some(path) = secure_path {
path
} else {
system_path = std::env::var("PATH").unwrap_or_default();
system_path.as_ref()
};

CommandAndArguments::build_from_args(shell, sudo_options.positional_args, path)
}
};

Ok(Context {
Expand Down Expand Up @@ -117,7 +128,7 @@ mod tests {
.unwrap();
let path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
let (ctx_opts, _pipe_opts) = options.into();
let context = Context::build_from_options(ctx_opts, path.to_string()).unwrap();
let context = Context::build_from_options(ctx_opts, Some(path)).unwrap();

let mut target_environment = HashMap::new();
target_environment.insert("SUDO_USER".to_string(), context.current_user.name.clone());
Expand Down
17 changes: 4 additions & 13 deletions src/sudo/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::process::exit;

use super::cli::{SudoRunOptions, SudoValidateOptions};
use super::diagnostic;
use crate::common::context::OptionsForContext;
use crate::common::resolve::{AuthUser, CurrentUser};
use crate::common::{Context, Error};
use crate::exec::{ExecOutput, ExitReason};
Expand Down Expand Up @@ -75,7 +74,7 @@ impl<Auth: AuthPlugin> Pipeline<Auth> {
)
}

let mut context = build_context(ctx_opts, &policy)?;
let mut context = Context::build_from_options(ctx_opts, policy.secure_path())?;

let policy = judge(policy, &context)?;

Expand Down Expand Up @@ -138,10 +137,10 @@ impl<Auth: AuthPlugin> Pipeline<Auth> {
}

pub fn run_validate(mut self, cmd_opts: SudoValidateOptions) -> Result<(), Error> {
let pre = read_sudoers()?;
let mut context = build_context(cmd_opts.into(), &pre)?;
let policy = read_sudoers()?;
let mut context = Context::build_from_options(cmd_opts.into(), policy.secure_path())?;

match pre.validate_authorization() {
match policy.validate_authorization() {
Authorization::Forbidden => {
return Err(Error::Authorization(context.current_user.name.to_string()));
}
Expand Down Expand Up @@ -233,14 +232,6 @@ impl<Auth: AuthPlugin> Pipeline<Auth> {
}
}

fn build_context(cmd_opts: OptionsForContext, pre: &Sudoers) -> Result<Context, Error> {
let secure_path: String = pre
.secure_path()
.map(|s| s.to_owned())
.unwrap_or_else(|| std::env::var("PATH").unwrap_or_default());
Context::build_from_options(cmd_opts, secure_path)
}

/// This should determine what the authentication status for the given record
/// match limit and origin/target user from the context is.
fn determine_auth_status(
Expand Down
3 changes: 2 additions & 1 deletion src/sudo/pipeline/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ impl<Auth: super::AuthPlugin> Pipeline<Auth> {
let original_command = cmd_opts.positional_args.first().cloned();

let sudoers = super::read_sudoers()?;
let mut context = super::build_context(cmd_opts.into(), &sudoers)?;

let mut context = Context::build_from_options(cmd_opts.into(), sudoers.secure_path())?;

if original_command.is_some() && !context.command.resolved {
return Err(Error::CommandNotFound(context.command.command));
Expand Down

0 comments on commit 231cf3f

Please sign in to comment.