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..eca8a4f2 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,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() { + 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 +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. @@ -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)", diff --git a/tests/by-util/test_userdel.rs b/tests/by-util/test_userdel.rs index ced9c507..22f04e7e 100644 --- a/tests/by-util/test_userdel.rs +++ b/tests/by-util/test_userdel.rs @@ -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 { + 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; + } + + 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:")); +}