diff --git a/usr.sbin/adduser/adduser.8 b/usr.sbin/adduser/adduser.8 --- a/usr.sbin/adduser/adduser.8 +++ b/usr.sbin/adduser/adduser.8 @@ -24,7 +24,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd April 11, 2024 +.Dd August 27, 2024 .Dt ADDUSER 8 .Os .Sh NAME @@ -243,7 +243,9 @@ script. .It Fl M Ar mode Create the home directory with permissions set to -.Ar mode . +.Ar mode , +modified by the current +.Xr umask 2 . .It Fl N Do not read the default configuration file. .It Fl q diff --git a/usr.sbin/pw/cpdir.c b/usr.sbin/pw/cpdir.c --- a/usr.sbin/pw/cpdir.c +++ b/usr.sbin/pw/cpdir.c @@ -49,9 +49,19 @@ if (*dir == '/') dir++; - if (mkdirat(rootfd, dir, mode) != 0 && errno != EEXIST) { - warn("mkdir(%s)", dir); - return; + if (mkdirat(rootfd, dir, mode) != 0) { + mode_t pumask; + + if (errno != EEXIST) { + warn("mkdir(%s)", dir); + return; + } + + pumask = umask(0); + umask(pumask); + + (void)fchmodat(rootfd, dir, mode & ~pumask, + AT_SYMLINK_NOFOLLOW); } fchownat(rootfd, dir, uid, gid, AT_SYMLINK_NOFOLLOW); if (flags > 0) diff --git a/usr.sbin/pw/tests/pw_useradd_test.sh b/usr.sbin/pw/tests/pw_useradd_test.sh --- a/usr.sbin/pw/tests/pw_useradd_test.sh +++ b/usr.sbin/pw/tests/pw_useradd_test.sh @@ -329,6 +329,21 @@ stat -f '%p' ${HOME}/home/foo } +atf_test_case user_add_existing_dir +user_add_existing_dir_body() { + populate_root_etc_skel + + mkdir -p -m 0777 ${HOME}/home/foo + atf_check -o inline:"40777\n" \ + stat -f '%p' ${HOME}/home/foo + + atf_check -s exit:0 ${RPW} useradd foo -M 0705 -m + atf_check grep -q '^foo:' $HOME/etc/master.passwd + atf_check test -d ${HOME}/home/foo + atf_check -o inline:"40705\n" \ + stat -f '%p' ${HOME}/home/foo +} + atf_test_case user_add_skel user_add_skel_body() { populate_root_etc_skel @@ -528,6 +543,7 @@ atf_add_test_case user_add_R_no_symlink atf_add_test_case user_add_R_intermed atf_add_test_case user_add_dir + atf_add_test_case user_add_existing_dir atf_add_test_case user_add_skel atf_add_test_case user_add_uid0 atf_add_test_case user_add_uid_too_large