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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
not a usage error. `newusers` refuses to create a group over 32 characters, as
upstream does (#391)

- `userdel -r` says `LOGIN mail spool (PATH) not found` and `LOGIN home directory
(PATH) not found` on standard error when either is missing, mail spool first, as
upstream does, with exit 0. It said nothing (#392)

- `chfn` writes the "other" GECOS sub-field, and the comma before it, only when it is
not empty, as upstream does. `chfn -f NewName` on an empty GECOS wrote
`NewName,,,,` instead of `NewName,,,`, so every change left a trailing empty field
Expand Down
4 changes: 4 additions & 0 deletions docs/man/userdel.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ is also removed from membership lists in /etc/group and /etc/gshadow.
the removal is committed, as upstream prints it. On a kernel older than 6.10,
which does not tell the subvolume's ID, the line is not printed.

A mail spool or home that is not there is not an error: *LOGIN mail spool
(PATH) not found* and *LOGIN home directory (PATH) not found* are printed on
standard error, in that order, and the exit status is unchanged.

**-R**, **--root** *CHROOT_DIR*
: Apply changes in the *CHROOT_DIR* directory.

Expand Down
65 changes: 50 additions & 15 deletions src/uu/userdel/src/userdel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// Restore signals before potentially long-running home removal.
drop(signals);

// 7. Optionally remove the home directory and mail spool.
// 7. Optionally remove the mail spool and home directory.
if remove_home {
if !saved_home.is_empty() {
let home = root.resolve(saved_home);
safe_remove_home(&home, saved_uid, force, home_shared_by_other)?;
}

let mail = root.resolve(&format!("/var/mail/{login}"));
if mail.exists() {
let _ = std::fs::remove_file(&mail);
}
remove_spool_and_home(
&root,
login,
saved_home,
saved_uid,
force,
home_shared_by_other,
)?;
}

if selinux_user {
Expand Down Expand Up @@ -280,6 +279,46 @@ pub fn uu_app() -> Command {
)
}

// ---------------------------------------------------------------------------
// Mail spool and home removal
// ---------------------------------------------------------------------------

/// Remove the mail spool and the home of the account being deleted, for `-r`.
///
/// Either one missing is said on stderr and is not a failure, spool first, as
/// upstream does.
fn remove_spool_and_home(
root: &SysRoot,
login: &str,
home: &str,
uid: u32,
force: bool,
shared_by_other: bool,
) -> Result<(), UserdelError> {
let mail = root.resolve(&format!("/var/mail/{login}"));
// `exists()` follows a symlink on purpose, here and for the home below:
// Ubuntu 26.04's `userdel -r` reports a dangling link as not found, exit
// 0, and leaves it in place.
if mail.exists() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exists() follows symlinks: a dangling /var/mail/login link is reported as not found and left behind.
should we use symlink_metadata here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked Ubuntu 26.04 (passwd 1:4.17.4-2ubuntu3). userdel -r follows the link too. With a dangling /var/mail/LOGIN link it prints LOGIN mail spool (/var/mail/LOGIN) not found, exits 0 and leaves the link in place. A dangling home link gets LOGIN home directory (/home/LOGIN) not found, exit 0, and is also left in place. So exists() already matches it, and symlink_metadata would remove a link that Ubuntu keeps. 1ae392c adds a comment saying this and tests for both cases. I checked that both tests fail with symlink_metadata. When a link is removed it is never followed: the spool goes through remove_file, which unlinks it, and safe_remove_home refuses a symlinked home.

let _ = std::fs::remove_file(&mail);
} else {
uucore::show_error!("{login} mail spool ({}) not found", mail.display());
}

if home.is_empty() {
// Nothing to remove; upstream names the empty path all the same.
uucore::show_error!("{login} home directory () not found");
return Ok(());
}
let home = root.resolve(home);
if home.exists() {
safe_remove_home(&home, uid, force, shared_by_other)
} else {
uucore::show_error!("{login} home directory ({}) not found", home.display());
Ok(())
}
}

// ---------------------------------------------------------------------------
// Safe home directory removal
// ---------------------------------------------------------------------------
Expand All @@ -290,7 +329,7 @@ const PROTECTED_DIRS: &[&str] = &[
"/proc", "/root", "/run", "/sbin", "/srv", "/sys", "/tmp", "/usr", "/var",
];

/// Safely remove a home directory with multiple safeguards:
/// Safely remove a home directory that exists, with multiple safeguards:
/// - Refuse a home shared with another account, or not owned by the user,
/// unless `force` (userdel(8) ties both to `-f`).
/// - Refuse to remove protected system directories.
Expand All @@ -302,10 +341,6 @@ fn safe_remove_home(
force: bool,
shared_by_other: bool,
) -> Result<(), UserdelError> {
if !home.exists() {
return Ok(());
}

if shared_by_other && !force {
return Err(UserdelError::CantRemoveHome(format!(
"not removing '{}': it is used by another user (use -f to force)",
Expand Down
171 changes: 171 additions & 0 deletions tests/by-util/test_userdel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,174 @@ fn test_last_private_group_in_gshadow_goes_with_the_file() {
);
assert!(!etc.join("gshadow").exists());
}

/// The messages of a run, without the program name each line starts with.
fn messages(out: &crate::common::Output) -> Vec<String> {
out.stderr
.lines()
.map(|l| l.split_once(": ").map_or(l, |(_, rest)| rest).to_string())
.collect()
}

/// `userdel -r` says so when the mail spool or the home is not there, mail
/// spool first, on standard error and with exit 0, as Ubuntu 26.04's does.
#[test]
fn test_remove_reports_a_missing_mail_spool_and_home() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please split this into 4 tests? easier to see which case fails

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split in 5586d94 into test_remove_reports_a_missing_mail_spool_and_home, test_remove_reports_a_missing_home, test_remove_reports_a_missing_mail_spool and test_remove_reports_an_empty_home_field.

if crate::common::skip_unless_root() {
return;
}

let dir = setup_root_dir();
let root = dir.path();
let prefix = root.to_str().expect("non-UTF-8 temp path");
let out = crate::common::run("userdel", &["-P", prefix, "-r", "testuser"]);
out.assert_code(0);
assert!(out.stdout.is_empty(), "stdout: {:?}", out.stdout);
assert_eq!(
messages(&out),
[
format!(
"testuser mail spool ({}) not found",
root.join("var/mail/testuser").display()
),
format!(
"testuser home directory ({}) not found",
root.join("home/testuser").display()
),
]
);
assert!(!read_passwd(&dir).contains("testuser:"));
}

/// Only the home is missing: the spool goes without a word.
#[test]
fn test_remove_reports_a_missing_home() {
if crate::common::skip_unless_root() {
return;
}

let dir = setup_root_dir();
let root = dir.path();
let prefix = root.to_str().expect("non-UTF-8 temp path");
let spool = root.join("var/mail/testuser");
std::fs::create_dir_all(root.join("var/mail")).expect("mail dir");
std::fs::write(&spool, "").expect("spool");
std::os::unix::fs::chown(&spool, Some(1000), Some(1000)).expect("chown spool");
let out = crate::common::run("userdel", &["-P", prefix, "-r", "testuser"]);
out.assert_code(0);
assert_eq!(
messages(&out),
[format!(
"testuser home directory ({}) not found",
root.join("home/testuser").display()
)]
);
assert!(!spool.exists(), "the mail spool should have been removed");
}

/// Only the spool is missing: the home goes without a word.
#[test]
fn test_remove_reports_a_missing_mail_spool() {
if crate::common::skip_unless_root() {
return;
}

let dir = setup_root_dir();
let root = dir.path();
let prefix = root.to_str().expect("non-UTF-8 temp path");
let home = root.join("home/testuser");
std::fs::create_dir_all(&home).expect("home");
std::os::unix::fs::chown(&home, Some(1000), Some(1000)).expect("chown home");
let out = crate::common::run("userdel", &["-P", prefix, "-r", "testuser"]);
out.assert_code(0);
assert_eq!(
messages(&out),
[format!(
"testuser mail spool ({}) not found",
root.join("var/mail/testuser").display()
)]
);
assert!(!home.exists(), "the home should have been removed");
}

/// An empty home field is reported as an empty path, as upstream does.
#[test]
fn test_remove_reports_an_empty_home_field() {
if crate::common::skip_unless_root() {
return;
}

let dir = setup_root_dir();
let passwd = read_passwd(&dir).replace("/home/testuser", "");
std::fs::write(dir.path().join("etc/passwd"), passwd).expect("rewrite passwd");
let prefix = dir.path().to_str().expect("non-UTF-8 temp path");
let out = crate::common::run("userdel", &["-P", prefix, "-r", "testuser"]);
out.assert_code(0);
assert_eq!(
messages(&out)[1..],
["testuser home directory () not found".to_string()]
);
}

/// A mail spool that is a dangling symlink counts as missing and is left in
/// place, as Ubuntu 26.04's `userdel -r` does.
#[test]
fn test_remove_leaves_a_dangling_mail_spool_link() {
if crate::common::skip_unless_root() {
return;
}

let dir = setup_root_dir();
let root = dir.path();
let prefix = root.to_str().expect("non-UTF-8 temp path");
let home = root.join("home/testuser");
std::fs::create_dir_all(&home).expect("home");
std::os::unix::fs::chown(&home, Some(1000), Some(1000)).expect("chown home");
let spool = root.join("var/mail/testuser");
std::fs::create_dir_all(root.join("var/mail")).expect("mail dir");
std::os::unix::fs::symlink(root.join("nowhere"), &spool).expect("spool link");
let out = crate::common::run("userdel", &["-P", prefix, "-r", "testuser"]);
out.assert_code(0);
assert_eq!(
messages(&out),
[format!(
"testuser mail spool ({}) not found",
spool.display()
)]
);
let meta = std::fs::symlink_metadata(&spool).expect("the link should be left in place");
assert!(meta.file_type().is_symlink());
assert!(!home.exists(), "the home should have been removed");
assert!(!read_passwd(&dir).contains("testuser:"));
}

/// A home that is a dangling symlink counts as missing and is left in place,
/// as Ubuntu 26.04's `userdel -r` does.
#[test]
fn test_remove_leaves_a_dangling_home_link() {
if crate::common::skip_unless_root() {
return;
}

let dir = setup_root_dir();
let root = dir.path();
let prefix = root.to_str().expect("non-UTF-8 temp path");
let home = root.join("home/testuser");
std::fs::create_dir_all(root.join("home")).expect("home parent");
std::os::unix::fs::symlink(root.join("nowhere"), &home).expect("home link");
let out = crate::common::run("userdel", &["-P", prefix, "-r", "testuser"]);
out.assert_code(0);
assert_eq!(
messages(&out),
[
format!(
"testuser mail spool ({}) not found",
root.join("var/mail/testuser").display()
),
format!("testuser home directory ({}) not found", home.display()),
]
);
let meta = std::fs::symlink_metadata(&home).expect("the link should be left in place");
assert!(meta.file_type().is_symlink());
assert!(!read_passwd(&dir).contains("testuser:"));
}