pw: remove mail file with unlinkat instead of building a path

This is consistent with how at jobs and crontabs are removed.

MFC After:	1 week
This commit is contained in:
Baptiste Daroussin committed 2026-09-04 16:28:47 +02:00
1 parent ba391775e8
commit 1ccf8075ea
2 files changed
+23 -4

No files matched your search

+7 -4
View File
@@ -837,7 +837,6 @@ pw_user_del(int argc, char **argv, char *arg1)
char *name = NULL;
char grname[MAXLOGNAME];
char *nispasswd = NULL;
char file[MAXPATHLEN];
char home[MAXPATHLEN];
const char *cfg = NULL;
struct stat st;
@@ -936,7 +935,6 @@ pw_user_del(int argc, char **argv, char *arg1)
* Save these for later, since contents of pwd may be
* invalidated by deletion
*/
snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
strlcpy(home, pwd->pw_dir, sizeof(home));
gr = GETGRGID(pwd->pw_gid);
if (gr != NULL)
@@ -987,8 +985,13 @@ pw_user_del(int argc, char **argv, char *arg1)
(uintmax_t)id);
/* Remove mail file */
if (PWALTDIR() != PWF_ALT)
unlinkat(conf.rootfd, file + 1, 0);
if (PWALTDIR() != PWF_ALT) {
int mfd = openat(conf.rootfd, &_PATH_MAILDIR[1], O_DIRECTORY | O_CLOEXEC);
if (mfd != -1) {
unlinkat(mfd, pwd->pw_name, 0);
close(mfd);
}
}
/* Remove at jobs */
if (PWALTDIR() != PWF_ALT && GETPWUID(id) == NULL)
+16
View File
@@ -127,6 +127,21 @@ delete_crontab_cleanup() {
rm -rf ${HOME}/var/cron/tabs
}
atf_test_case delete_mail cleanup
delete_mail_body() {
populate_root_etc_skel
mkdir -p ${HOME}/var/mail
atf_check -s exit:0 ${RPW} useradd foo
atf_check -s exit:0 ${RPW} userdel foo
[ ! -e ${HOME}/var/mail/foo ] || atf_fail "mail file not removed"
}
delete_mail_cleanup() {
rm -rf ${HOME}/var/mail
}
atf_init_test_cases() {
atf_add_test_case rmuser_seperate_group
atf_add_test_case user_do_not_try_to_delete_root_if_user_unknown
@@ -137,4 +152,5 @@ atf_init_test_cases() {
atf_add_test_case home_regular_dir
atf_add_test_case delete_at_jobs
atf_add_test_case delete_crontab
atf_add_test_case delete_mail
}