Repository navigation
userdel: say when -r finds no mail spool or home #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split in 5586d94 into |
||
| 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:")); | ||
| } | ||
There was a problem hiding this comment.
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/loginlink is reported as not found and left behind.should we use
symlink_metadatahere?There was a problem hiding this comment.
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 -rfollows the link too. With a dangling/var/mail/LOGINlink it printsLOGIN mail spool (/var/mail/LOGIN) not found, exits 0 and leaves the link in place. A dangling home link getsLOGIN home directory (/home/LOGIN) not found, exit 0, and is also left in place. Soexists()already matches it, andsymlink_metadatawould remove a link that Ubuntu keeps. 1ae392c adds a comment saying this and tests for both cases. I checked that both tests fail withsymlink_metadata. When a link is removed it is never followed: the spool goes throughremove_file, which unlinks it, andsafe_remove_homerefuses a symlinked home.