Skip to content

Commit 8c5080f

Browse files
bjorn3squell
authored andcommitted
Don't set the MAIL env var
On Linux pretty much every program that used to read it no longer exists in distros as far as we can tell. And the few that still do, already support getting the default value that sudo-rs would have set anyway. In addition neither a regular gui session nor openssh sets this env var. We will still respect setting the env var if it is in env_keep, env_check or set by PAM through for example pam_mail like on the default PAM config of Debian for su and login.
1 parent de00427 commit 8c5080f

File tree

7 files changed

+18
-32
lines changed

7 files changed

+18
-32
lines changed

build.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ fn main() {
1414
])
1515
.expect("no zoneinfo database");
1616

17-
let path_maildir: &str =
18-
get_first_path(&["/var/mail", "/var/spool/mail", "/usr/spool/mail"]).unwrap_or("/var/mail");
19-
2017
// TODO: use _PATH_STDPATH and _PATH_DEFPATH_ROOT from paths.h
2118
println!("cargo:rustc-env=SUDO_PATH_DEFAULT=/usr/bin:/bin:/usr/sbin:/sbin");
2219
println!(
@@ -26,7 +23,6 @@ fn main() {
2623
"cargo:rustc-env=SU_PATH_DEFAULT=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
2724
);
2825

29-
println!("cargo:rustc-env=PATH_MAILDIR={path_maildir}");
3026
println!("cargo:rustc-env=PATH_ZONEINFO={path_zoneinfo}");
3127
println!("cargo:rerun-if-changed=build.rs");
3228

docs/man/sudoers.5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ By default, `sudo-rs` logs both successful and unsuccessful attempts (as well as
2727
Since environment variables can influence program behavior, `sudo-rs` restricts which variables from the user's environment are inherited by the command to be run.
2828

2929
In `sudo-rs`, the *env_reset* flag cannot be disabled. This causes commands to be executed with a new, minimal environment.
30-
The `HOME`, `MAIL`, `SHELL`, `LOGNAME` and `USER` environment variables are initialized based on the target user and the `SUDO_*` variables are set based on the invoking user. Additional variables, such as `DISPLAY`, `PATH` and `TERM`, are preserved from the invoking user's environment if permitted by the *env_check* or *env_keep* options. A few environment variables are treated specially. If the `PATH` and `TERM` variables are not preserved from the user's environment, they will be set to default values. The `LOGNAME` and `USER` are handled as a single entity. If one of them is preserved (or removed) from the user's environment, the other will be as well.
30+
The `HOME`, `SHELL`, `LOGNAME` and `USER` environment variables are initialized based on the target user and the `SUDO_*` variables are set based on the invoking user. Additional variables, such as `DISPLAY`, `PATH` and `TERM`, are preserved from the invoking user's environment if permitted by the *env_check* or *env_keep* options. A few environment variables are treated specially. If the `PATH` and `TERM` variables are not preserved from the user's environment, they will be set to default values. The `LOGNAME` and `USER` are handled as a single entity. If one of them is preserved (or removed) from the user's environment, the other will be as well.
3131
If `LOGNAME` and `USER` are to be preserved but only one of them is present in the user's environment, the other will be set to the same value. This avoids an inconsistent environment where one of the variables describing the user name is set to the invoking user and one is set to the target user.
3232
Environment variables with a value beginning with `()` are removed, as they may be interpreted as functions by the bash shell.
3333

src/su/context.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use super::cli::SuRunOptions;
1919
const VALID_LOGIN_SHELLS_LIST: &str = "/etc/shells";
2020
const FALLBACK_LOGIN_SHELL: &str = "/bin/sh";
2121

22-
const PATH_MAILDIR: &str = env!("PATH_MAILDIR");
2322
const PATH_DEFAULT: &str = env!("SU_PATH_DEFAULT");
2423
const PATH_DEFAULT_ROOT: &str = env!("SU_PATH_DEFAULT_ROOT");
2524

@@ -182,10 +181,6 @@ impl SuContext {
182181
// extend environment with fixed variables
183182
environment.insert("HOME".into(), user.home.clone().into());
184183
environment.insert("SHELL".into(), command.clone().into());
185-
environment.insert(
186-
"MAIL".into(),
187-
format!("{PATH_MAILDIR}/{}", user.name).into(),
188-
);
189184

190185
if !is_target_root || options.login {
191186
environment.insert("USER".into(), options.user.clone().into());

src/sudo/env/environment.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::system::PATH_MAX;
1010

1111
use super::wildcard_match::wildcard_match;
1212

13-
const PATH_MAILDIR: &str = env!("PATH_MAILDIR");
1413
const PATH_ZONEINFO: &str = env!("PATH_ZONEINFO");
1514
const PATH_DEFAULT: &str = env!("SUDO_PATH_DEFAULT");
1615

@@ -64,9 +63,6 @@ fn add_extra_env(
6463
environment.insert("SUDO_USER".into(), context.current_user.name.clone().into());
6564
environment.insert("SUDO_HOME".into(), context.current_user.home.clone().into());
6665
// target user
67-
environment
68-
.entry("MAIL".into())
69-
.or_insert_with(|| format!("{PATH_MAILDIR}/{}", context.target_user.name).into());
7066
// The current SHELL variable should determine the shell to run when -s is passed, if none set use passwd entry
7167
environment
7268
.entry("SHELL".into())
@@ -199,7 +195,7 @@ fn should_keep(key: &OsStr, value: &OsStr, cfg: &Restrictions) -> bool {
199195
/// see <https://github.com/sudo-project/sudo/blob/main/plugins/sudoers/env.c> for the original implementation
200196
/// see <https://www.sudo.ws/docs/man/sudoers.man/#Command_environment> for the original documentation
201197
///
202-
/// The HOME, MAIL, SHELL, LOGNAME and USER environment variables are initialized based on the target user
198+
/// The HOME, SHELL, LOGNAME and USER environment variables are initialized based on the target user
203199
/// and the SUDO_* variables are set based on the invoking user.
204200
///
205201
/// Additional variables, such as DISPLAY, PATH and TERM, are preserved from the invoking user's

src/sudo/env/tests.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const TESTS: &str = "
2929
LANGUAGE=en_US.UTF-8
3030
LC_ALL=en_US.UTF-8
3131
LS_COLORS=cd=40;33;01:*.jpg=01;35:*.mp3=00;36:
32-
MAIL=/var/mail/root
3332
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
3433
SHELL=/bin/bash
3534
SUDO_COMMAND=/usr/bin/env
@@ -47,7 +46,6 @@ const TESTS: &str = "
4746
LANGUAGE=en_US.UTF-8
4847
LC_ALL=en_US.UTF-8
4948
LS_COLORS=cd=40;33;01:*.jpg=01;35:*.mp3=00;36:
50-
MAIL=/var/mail/test
5149
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
5250
SHELL=/bin/sh
5351
SUDO_COMMAND=/usr/bin/env

test-framework/sudo-compliance-tests/src/sudo/env_reset.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ fn some_vars_are_set() {
3030
let mut sudo_env = helpers::parse_env_output(&stdout);
3131

3232
// # man sudo
33-
// "Set to the mail spool of the target user"
34-
assert_eq!(Some("/var/mail/root"), sudo_env.remove("MAIL"));
35-
33+
if sudo_test::is_original_sudo() {
34+
assert_eq!(Some("/var/mail/root"), sudo_env.remove("MAIL"));
35+
}
3636
// "Set to the home directory of the target user"
3737
assert_eq!(Some("/root"), sudo_env.remove("HOME"));
3838

@@ -60,7 +60,7 @@ fn some_vars_are_set() {
6060
assert_eq!(Some("root"), sudo_env.remove("USER"));
6161

6262
// # man sudoers
63-
// "The HOME, MAIL, SHELL, LOGNAME and USER environment variables are initialized based on the target user"
63+
// "The HOME, SHELL, LOGNAME and USER environment variables are initialized based on the target user"
6464
assert_eq!(Some("/bin/sh"), sudo_env.remove("SHELL"));
6565

6666
// "If the PATH and TERM variables are not preserved from the user's environment, they will be set to default values."
@@ -128,15 +128,17 @@ fn user_dependent_vars() {
128128
.stdout();
129129
let mut sudo_env = helpers::parse_env_output(&stdout);
130130

131-
// "The HOME, MAIL, SHELL, LOGNAME and USER environment variables are initialized based on the target user"
131+
// "The HOME, SHELL, LOGNAME and USER environment variables are initialized based on the target user"
132132
assert_eq!(
133133
Some(format!("/home/{USERNAME}")).as_deref(),
134134
sudo_env.remove("HOME")
135135
);
136-
assert_eq!(
137-
Some(format!("/var/mail/{USERNAME}")).as_deref(),
138-
sudo_env.remove("MAIL")
139-
);
136+
if sudo_test::is_original_sudo() {
137+
assert_eq!(
138+
Some(format!("/var/mail/{USERNAME}")).as_deref(),
139+
sudo_env.remove("MAIL")
140+
);
141+
}
140142
assert_eq!(Some(shell_path), sudo_env.remove("SHELL"));
141143
assert_eq!(Some(USERNAME), sudo_env.remove("LOGNAME"));
142144
assert_eq!(Some(USERNAME), sudo_env.remove("USER"));
@@ -202,7 +204,9 @@ fn some_vars_are_preserved() {
202204

203205
// not preserved
204206
assert_eq!(Some("/root"), sudo_env.remove("HOME"));
205-
assert_eq!(Some("/var/mail/root"), sudo_env.remove("MAIL"));
207+
if sudo_test::is_original_sudo() {
208+
assert_eq!(Some("/var/mail/root"), sudo_env.remove("MAIL"));
209+
}
206210
assert_eq!(Some("/bin/sh"), sudo_env.remove("SHELL"));
207211
assert_eq!(Some("root"), sudo_env.remove("LOGNAME"));
208212
assert_eq!(Some("root"), sudo_env.remove("USER"));

test-framework/sudo-compliance-tests/src/sudo/sudoers/env.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,24 +277,22 @@ fn plus_equal_repeated(env_list: EnvList) {
277277
}
278278

279279
// see 'environment' section in `man sudo`
280-
// the variables HOME, LOGNAME, MAIL and USER are set by sudo with a value that depends on the
280+
// the variables HOME, LOGNAME and USER are set by sudo with a value that depends on the
281281
// target user *unless* they appear in the env_keep list
282282
fn vars_with_target_user_specific_values(env_list: EnvList) {
283283
let home = "my-home";
284284
let logname = "my-logname";
285-
let mail = "my-mail";
286285
let user = "my-user";
287286

288287
let env = Env([
289288
SUDOERS_ALL_ALL_NOPASSWD,
290-
&format!("Defaults {env_list} = \"HOME LOGNAME MAIL USER\""),
289+
&format!("Defaults {env_list} = \"HOME LOGNAME USER\""),
291290
])
292291
.build();
293292

294293
let stdout = Command::new("env")
295294
.arg(format!("HOME={home}"))
296295
.arg(format!("LOGNAME={logname}"))
297-
.arg(format!("MAIL={mail}"))
298296
.arg(format!("USER={user}"))
299297
.args(["sudo", "env"])
300298
.output(&env)
@@ -303,7 +301,6 @@ fn vars_with_target_user_specific_values(env_list: EnvList) {
303301

304302
assert_eq!(Some(home), sudo_env.get("HOME").copied());
305303
assert_eq!(Some(logname), sudo_env.get("LOGNAME").copied());
306-
assert_eq!(Some(mail), sudo_env.get("MAIL").copied());
307304
assert_eq!(Some(user), sudo_env.get("USER").copied());
308305
}
309306

0 commit comments

Comments
 (0)