git: bbb2d2ce4220 - main - pw: do not move /home/$user to /usr/home

From: Mike Karels <karels_at_FreeBSD.org>
Date: Tue, 23 May 2023 12:24:14 UTC
The branch main has been updated by karels:

URL: https://cgit.FreeBSD.org/src/commit/?id=bbb2d2ce422070869bd93a0771251e654c07b13d

commit bbb2d2ce422070869bd93a0771251e654c07b13d
Author:     Mike Karels <karels@FreeBSD.org>
AuthorDate: 2023-05-23 12:17:42 +0000
Commit:     Mike Karels <karels@FreeBSD.org>
CommitDate: 2023-05-23 12:17:42 +0000

    pw: do not move /home/$user to /usr/home
    
    When adding a user, pw will create the path to the home directory
    if needed.  However, if creating a path with just one component,
    i.e.  that appears to be in the root directory, pw would create the
    directory in /usr, and create a symlink from the root directory.
    Most commonly, this meant that the default of /home/$user would turn
    into /usr/home/$user.  This was added in a self-described kludge 26
    years ago.  It made (some) sense when root was generally a small
    partition, with most of the space in /usr.  However, the default is
    now one large partition.  /home really doesn't belong under /usr,
    and anyone who wants to use /usr/home can specify it explicitly.
    Remove the kludge to move /home under /usr and create the symlink,
    and just use the specified path.  Note that this operation was
    done only on the first invocation for a path, and this happened most
    commonly when adding a user during the install.
    
    Modify the test that checked for the creation of the symlink to
    verify that the symlink is *not* made, but rather a directory.
    Add a test that intermediate directories are still created.
    
    Reviewed by:    rgrimes, bapt
    Differential Revision:  https://reviews.freebsd.org/D40085
---
 usr.sbin/pw/pw_user.c                | 20 ++------------------
 usr.sbin/pw/tests/pw_useradd_test.sh | 21 +++++++++++++++------
 2 files changed, 17 insertions(+), 24 deletions(-)

diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c
index c8e8de1fc46a..bea248c802ed 100644
--- a/usr.sbin/pw/pw_user.c
+++ b/usr.sbin/pw/pw_user.c
@@ -114,36 +114,20 @@ mkdir_home_parents(int dfd, const char *dir)
 	}
 	tmp[0] = '\0';
 
-	/*
-	 * This is a kludge especially for Joerg :)
-	 * If the home directory would be created in the root partition, then
-	 * we really create it under /usr which is likely to have more space.
-	 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
-	 */
-	if (strchr(dirs, '/') == NULL) {
-		asprintf(&tmp, "usr/%s", dirs);
-		if (tmp == NULL)
-			errx(EX_UNAVAILABLE, "out of memory");
-		if (mkdirat(dfd, tmp, _DEF_DIRMODE) != -1 || errno == EEXIST) {
-			fchownat(dfd, tmp, 0, 0, 0);
-			symlinkat(tmp, dfd, dirs);
-		}
-		free(tmp);
-	}
 	tmp = dirs;
 	if (fstatat(dfd, dirs, &st, 0) == -1) {
 		while ((tmp = strchr(tmp + 1, '/')) != NULL) {
 			*tmp = '\0';
 			if (fstatat(dfd, dirs, &st, 0) == -1) {
 				if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1)
-					err(EX_OSFILE,  "'%s' (root home parent) is not a directory", dirs);
+					err(EX_OSFILE,  "'%s' (home parent) is not a directory", dirs);
 			}
 			*tmp = '/';
 		}
 	}
 	if (fstatat(dfd, dirs, &st, 0) == -1) {
 		if (mkdirat(dfd, dirs, _DEF_DIRMODE) == -1)
-			err(EX_OSFILE,  "'%s' (root home parent) is not a directory", dirs);
+			err(EX_OSFILE,  "'%s' (home parent) is not a directory", dirs);
 		fchownat(dfd, dirs, 0, 0, 0);
 	}
 
diff --git a/usr.sbin/pw/tests/pw_useradd_test.sh b/usr.sbin/pw/tests/pw_useradd_test.sh
index a50f018ab99d..e9d0a3aa20af 100755
--- a/usr.sbin/pw/tests/pw_useradd_test.sh
+++ b/usr.sbin/pw/tests/pw_useradd_test.sh
@@ -295,15 +295,23 @@ user_add_R_body() {
 	[ ! -d ${HOME}/home/bar ] || atf_fail "Directory not removed"
 }
 
-atf_test_case user_add_R_symlink
-user_add_R_symlink_body() {
+atf_test_case user_add_R_no_symlink
+user_add_R_no_symlink_body() {
 	populate_root_etc_skel
 
 	mkdir ${HOME}/usr
 	atf_check -s exit:0 ${RPW} useradd foo -m
-	test -d ${HOME}/usr/home || atf_fail "Home parent directory not created"
-	test -h ${HOME}/home || atf_fail "/home directory is not a symlink"
-	atf_check -s exit:0 -o inline:"usr/home\n" readlink ${HOME}/home
+	[ ! -d ${HOME}/usr/home ] || atf_fail "/usr/home created"
+	test -d ${HOME}/home || atf_fail "/home directory not created"
+}
+
+atf_test_case user_add_R_intermed
+user_add_R_intermed_body() {
+	populate_root_etc_skel
+
+	atf_check -s exit:0 ${RPW} useradd foo -m -d /a/b/c/foo
+	test -d ${HOME}/a/b/c || atf_fail "intermediate directories not created"
+	test -d ${HOME}/a/b/c/foo || atf_fail "user directory not created"
 }
 
 atf_test_case user_add_skel
@@ -479,7 +487,8 @@ atf_init_test_cases() {
 	atf_add_test_case user_add_invalid_group_entry
 	atf_add_test_case user_add_password_from_h
 	atf_add_test_case user_add_R
-	atf_add_test_case user_add_R_symlink
+	atf_add_test_case user_add_R_no_symlink
+	atf_add_test_case user_add_R_intermed
 	atf_add_test_case user_add_skel
 	atf_add_test_case user_add_uid0
 	atf_add_test_case user_add_uid_too_large