Skip to content

Commit

Permalink
refactor conditional environment lines to be in the same style
Browse files Browse the repository at this point in the history
  • Loading branch information
squell committed Dec 17, 2024
1 parent b7f69f5 commit 62ac2bb
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/sudo/env/environment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{hash_map::Entry, HashMap, HashSet},
collections::{HashMap, HashSet},
ffi::{OsStr, OsString},
os::unix::prelude::OsStrExt,
};
Expand Down Expand Up @@ -63,19 +63,19 @@ fn add_extra_env(
);
environment.insert("SUDO_USER".into(), context.current_user.name.clone().into());
// target user
if let Entry::Vacant(entry) = environment.entry("MAIL".into()) {
entry.insert(format!("{PATH_MAILDIR}/{}", context.target_user.name).into());
}
environment
.entry("MAIL".into())
.or_insert_with(|| format!("{PATH_MAILDIR}/{}", context.target_user.name).into());
// The current SHELL variable should determine the shell to run when -s is passed, if none set use passwd entry
if let Entry::Vacant(entry) = environment.entry("SHELL".into()) {
entry.insert(context.target_user.shell.clone().into());
}
environment
.entry("SHELL".into())
.or_insert_with(|| context.target_user.shell.clone().into());
// HOME' Set to the home directory of the target user if -i or -H are specified, env_reset or always_set_home are
// set in sudoers, or when the -s option is specified and set_home is set in sudoers.
// Since we always want to do env_reset -> always set HOME
if let Entry::Vacant(entry) = environment.entry("HOME".into()) {
entry.insert(context.target_user.home.clone().into());
}
environment
.entry("HOME".into())
.or_insert_with(|| context.target_user.home.clone().into());

match (
environment.get(OsStr::new("LOGNAME")),
Expand Down Expand Up @@ -106,14 +106,13 @@ fn add_extra_env(
environment.insert("PATH".into(), secure_path.into());
}
// If the PATH and TERM variables are not preserved from the user's environment, they will be set to default value
if !environment.contains_key(OsStr::new("PATH")) {
// If the PATH variable is not set, it will be set to default value
environment.insert("PATH".into(), PATH_DEFAULT.into());
}
environment
.entry("PATH".into())
.or_insert_with(|| PATH_DEFAULT.into());
// If the TERM variable is not preserved from the user's environment, it will be set to default value
if !environment.contains_key(OsStr::new("TERM")) {
environment.insert("TERM".into(), "unknown".into());
}
environment
.entry("TERM".into())
.or_insert_with(|| "unknown".into());
// The SUDO_PS1 variable requires special treatment as the PS1 variable must be set in the
// target environment to the same value of SUDO_PS1 if the latter is set.
if let Some(sudo_ps1_value) = sudo_ps1 {
Expand Down

0 comments on commit 62ac2bb

Please sign in to comment.