From 6d2117e35bd9011d90fcfef5860a76ca2e3541df Mon Sep 17 00:00:00 2001 From: Pierre Warnier Date: Tue, 3 Nov 2026 11:20:27 +0100 Subject: [PATCH 1/3] userdel: say when -r finds no mail spool or home `userdel -r` was silent when the user's mail spool or home directory did not exist. Ubuntu 26.04's userdel prints `LOGIN mail spool (PATH) not found` and `LOGIN home directory (PATH) not found` on standard error, mail spool first, and still exits 0; an empty home field is reported as `()`. Do the same, handling the spool before the home as upstream does. Fixes #392 --- CHANGELOG.md | 4 ++ docs/man/userdel.8.md | 4 ++ src/uu/userdel/src/userdel.rs | 62 ++++++++++++++++++------ tests/by-util/test_userdel.rs | 88 +++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cb8a82e..1e5cd625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/man/userdel.8.md b/docs/man/userdel.8.md index f9f04120..7f495ab0 100644 --- a/docs/man/userdel.8.md +++ b/docs/man/userdel.8.md @@ -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. diff --git a/src/uu/userdel/src/userdel.rs b/src/uu/userdel/src/userdel.rs index e895965f..b8cf4718 100644 --- a/src/uu/userdel/src/userdel.rs +++ b/src/uu/userdel/src/userdel.rs @@ -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 { @@ -280,6 +279,43 @@ 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}")); + if mail.exists() { + 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 // --------------------------------------------------------------------------- @@ -290,7 +326,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. @@ -302,10 +338,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)", diff --git a/tests/by-util/test_userdel.rs b/tests/by-util/test_userdel.rs index ced9c507..b7bcf2cf 100644 --- a/tests/by-util/test_userdel.rs +++ b/tests/by-util/test_userdel.rs @@ -567,3 +567,91 @@ 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 { + 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() { + if crate::common::skip_unless_root() { + return; + } + + // Neither is there. + 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. + 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. + 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. + 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()] + ); +} From 0f4a6662d56823c1341147df9a21a9b393b94f4f Mon Sep 17 00:00:00 2001 From: Pierre Warnier Date: Wed, 4 Nov 2026 10:33:24 +0100 Subject: [PATCH 2/3] userdel: split the missing spool and home test by case One test per case, so a failure names the case: both missing, only the home missing, only the spool missing, and an empty home field. --- tests/by-util/test_userdel.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/tests/by-util/test_userdel.rs b/tests/by-util/test_userdel.rs index b7bcf2cf..8e91c9dc 100644 --- a/tests/by-util/test_userdel.rs +++ b/tests/by-util/test_userdel.rs @@ -584,7 +584,6 @@ fn test_remove_reports_a_missing_mail_spool_and_home() { return; } - // Neither is there. let dir = setup_root_dir(); let root = dir.path(); let prefix = root.to_str().expect("non-UTF-8 temp path"); @@ -605,8 +604,15 @@ fn test_remove_reports_a_missing_mail_spool_and_home() { ] ); 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; + } - // Only the home is missing: the spool goes without a word. let dir = setup_root_dir(); let root = dir.path(); let prefix = root.to_str().expect("non-UTF-8 temp path"); @@ -624,8 +630,15 @@ fn test_remove_reports_a_missing_mail_spool_and_home() { )] ); 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; + } - // Only the spool is missing: the home goes without a word. let dir = setup_root_dir(); let root = dir.path(); let prefix = root.to_str().expect("non-UTF-8 temp path"); @@ -642,8 +655,15 @@ fn test_remove_reports_a_missing_mail_spool_and_home() { )] ); 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; + } - // An empty home field is reported as an empty path, as upstream does. 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"); From 678d67991c9de1914918f23dbd58b3df9dad887a Mon Sep 17 00:00:00 2001 From: Pierre Warnier Date: Sun, 8 Nov 2026 08:16:38 +0100 Subject: [PATCH 3/3] userdel: pin -r on a dangling spool or home link Ubuntu 26.04's userdel -r follows the link: a dangling /var/mail/LOGIN or home link is reported as not found, with exit 0, and left in place. exists() follows it the same way; say so, and test both cases. --- src/uu/userdel/src/userdel.rs | 3 ++ tests/by-util/test_userdel.rs | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/uu/userdel/src/userdel.rs b/src/uu/userdel/src/userdel.rs index b8cf4718..eca8a4f2 100644 --- a/src/uu/userdel/src/userdel.rs +++ b/src/uu/userdel/src/userdel.rs @@ -296,6 +296,9 @@ fn remove_spool_and_home( 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() { let _ = std::fs::remove_file(&mail); } else { diff --git a/tests/by-util/test_userdel.rs b/tests/by-util/test_userdel.rs index 8e91c9dc..22f04e7e 100644 --- a/tests/by-util/test_userdel.rs +++ b/tests/by-util/test_userdel.rs @@ -675,3 +675,66 @@ fn test_remove_reports_an_empty_home_field() { ["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:")); +}