git: 1691bcc8b02c - releng/14.1 - pw: Rename some enums.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Thu, 09 May 2024 14:31:51 UTC
The branch releng/14.1 has been updated by des:

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

commit 1691bcc8b02c0d32dfde359f8a614a55bc98d6da
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-05-06 16:18:39 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-05-09 14:31:12 +0000

    pw: Rename some enums.
    
    Rename `M_PRINT` and `M_UPDATE` to `M_SHOW` and `M_MODIFY` to match the
    names of the commands they represent.  No functional change intended.
    
    MFC after:      3 days
    Reviewed by:    allanjude
    Differential Revision:  https://reviews.freebsd.org/D45096
    
    (cherry picked from commit a9ea647c29cf510a164947251de0d34f53f2bca0)
    
    pw: Don't silently ignore unparsed command line arguments.
    
    MFC after:      3 days
    Reviewed by:    allanjude
    Differential Revision:  https://reviews.freebsd.org/D45097
    
    (cherry picked from commit c86119328e6b2cfeb4f9319f6b154524d88caaf4)
    
    pw: Test home directory ownership and mode.
    
    MFC after:      3 days
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D45099
    
    (cherry picked from commit e12b7446bddcb91b869abef6048910cc15185a33)
    (cherry picked from commit 2f59ee8287548225e7c71db987574fe043ed1be4)
    
    Approved by:    re (cperciva)
---
 usr.sbin/pw/pw.c                     | 13 ++++++++--
 usr.sbin/pw/pw.h                     | 22 +++++++++--------
 usr.sbin/pw/pw_group.c               | 35 ++++++++++++++++++++------
 usr.sbin/pw/pw_user.c                | 48 ++++++++++++++++++++++++++++--------
 usr.sbin/pw/tests/pw_useradd_test.sh | 17 +++++++++++++
 5 files changed, 106 insertions(+), 29 deletions(-)

diff --git a/usr.sbin/pw/pw.c b/usr.sbin/pw/pw.c
index 063553dd084f..fc17f6dba022 100644
--- a/usr.sbin/pw/pw.c
+++ b/usr.sbin/pw/pw.c
@@ -101,13 +101,16 @@ static int (*cmdfunc[W_NUM][M_NUM])(int argc, char **argv, char *_name) = {
 
 struct pwconf conf;
 
+static int	mode = -1;
+static int	which = -1;
+
 static int	getindex(const char *words[], const char *word);
 static void	cmdhelp(int mode, int which);
 
 int
 main(int argc, char *argv[])
 {
-	int		mode = -1, which = -1, tmp;
+	int		tmp;
 	struct stat	st;
 	char		arg, *arg1;
 	bool		relocated, nis;
@@ -375,5 +378,11 @@ cmdhelp(int mode, int which)
 
 		fprintf(stderr, "%s", help[which][mode]);
 	}
-	exit(EXIT_FAILURE);
+	exit(EX_USAGE);
+}
+
+void
+usage(void)
+{
+	cmdhelp(mode, which);
 }
diff --git a/usr.sbin/pw/pw.h b/usr.sbin/pw/pw.h
index 5de333ce5e71..c3725693f91d 100644
--- a/usr.sbin/pw/pw.h
+++ b/usr.sbin/pw/pw.h
@@ -36,14 +36,14 @@
 
 enum _mode
 {
-        M_ADD,
-        M_DELETE,
-        M_UPDATE,
-        M_PRINT,
+	M_ADD,
+	M_DELETE,
+	M_MODIFY,
+	M_SHOW,
 	M_NEXT,
 	M_LOCK,
 	M_UNLOCK,
-        M_NUM
+	M_NUM
 };
 
 enum _passmode
@@ -56,13 +56,13 @@ enum _passmode
 
 enum _which
 {
-        W_USER,
-        W_GROUP,
-        W_NUM
+	W_USER,
+	W_GROUP,
+	W_NUM
 };
 
-#define	_DEF_DIRMODE	(S_IRWXU | S_IRWXG | S_IRWXO)
-#define	_PW_CONF	"pw.conf"
+#define _DEF_DIRMODE	(S_IRWXU | S_IRWXG | S_IRWXO)
+#define _PW_CONF	"pw.conf"
 #define _UC_MAXLINE	1024
 #define _UC_MAXSHELLS	32
 
@@ -114,3 +114,5 @@ uintmax_t strtounum(const char * __restrict, uintmax_t, uintmax_t,
     const char ** __restrict);
 
 bool grp_has_member(struct group *grp, const char *name);
+
+void usage(void);
diff --git a/usr.sbin/pw/pw_group.c b/usr.sbin/pw/pw_group.c
index 32dec769fb1a..1941c03aa2c5 100644
--- a/usr.sbin/pw/pw_group.c
+++ b/usr.sbin/pw/pw_group.c
@@ -273,9 +273,13 @@ pw_group_next(int argc, char **argv, char *arg1 __unused)
 			quiet = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (quiet)
 		freopen(_PATH_DEVNULL, "w", stderr);
@@ -332,9 +336,13 @@ pw_group_show(int argc, char **argv, char *arg1)
 			all = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (quiet)
 		freopen(_PATH_DEVNULL, "w", stderr);
@@ -391,9 +399,13 @@ pw_group_del(int argc, char **argv, char *arg1)
 			nis = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (quiet)
 		freopen(_PATH_DEVNULL, "w", stderr);
@@ -551,9 +563,13 @@ pw_group_add(int argc, char **argv, char *arg1)
 			nis = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (quiet)
 		freopen(_PATH_DEVNULL, "w", stderr);
@@ -645,9 +661,14 @@ pw_group_mod(int argc, char **argv, char *arg1)
 			nis = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
+
 	if (quiet)
 		freopen(_PATH_DEVNULL, "w", stderr);
 	cnf = get_userconfig(cfg);
@@ -697,11 +718,11 @@ pw_group_mod(int argc, char **argv, char *arg1)
 	if ((grp = GETGRNAM(name)) == NULL)
 		errx(EX_SOFTWARE, "group disappeared during update");
 
-	pw_log(cnf, M_UPDATE, W_GROUP, "%s(%ju)", grp->gr_name,
+	pw_log(cnf, M_MODIFY, W_GROUP, "%s(%ju)", grp->gr_name,
 	    (uintmax_t)grp->gr_gid);
 
 	if (nis && nis_update() == 0)
-		pw_log(cnf, M_UPDATE, W_GROUP, "NIS maps updated");
+		pw_log(cnf, M_MODIFY, W_GROUP, "NIS maps updated");
 
 	return (EXIT_SUCCESS);
 }
diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c
index 6875d931a1d2..89354b249935 100644
--- a/usr.sbin/pw/pw_user.c
+++ b/usr.sbin/pw/pw_user.c
@@ -146,7 +146,7 @@ create_and_populate_homedir(struct userconf *cnf, struct passwd *pwd,
 
 	copymkdir(conf.rootfd, pwd->pw_dir, skelfd, homemode, pwd->pw_uid,
 	    pwd->pw_gid, 0);
-	pw_log(cnf, update ? M_UPDATE : M_ADD, W_USER, "%s(%ju) home %s made",
+	pw_log(cnf, update ? M_MODIFY : M_ADD, W_USER, "%s(%ju) home %s made",
 	    pwd->pw_name, (uintmax_t)pwd->pw_uid, pwd->pw_dir);
 }
 
@@ -708,9 +708,13 @@ pw_user_next(int argc, char **argv, char *name __unused)
 			quiet = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (quiet)
 		freopen(_PATH_DEVNULL, "w", stderr);
@@ -772,9 +776,13 @@ pw_user_show(int argc, char **argv, char *arg1)
 			v7 = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (quiet)
 		freopen(_PATH_DEVNULL, "w", stderr);
@@ -855,9 +863,13 @@ pw_user_del(int argc, char **argv, char *arg1)
 			nis = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (quiet)
 		freopen(_PATH_DEVNULL, "w", stderr);
@@ -1003,9 +1015,13 @@ pw_user_lock(int argc, char **argv, char *arg1)
 			/* compatibility */
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	return (pw_userlock(arg1, M_LOCK));
 }
@@ -1022,9 +1038,13 @@ pw_user_unlock(int argc, char **argv, char *arg1)
 			/* compatibility */
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	return (pw_userlock(arg1, M_UNLOCK));
 }
@@ -1291,9 +1311,13 @@ pw_user_add(int argc, char **argv, char *arg1)
 			nis = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (geteuid() != 0 && ! dryrun)
 		errx(EX_NOPERM, "you must be root");
@@ -1604,9 +1628,13 @@ pw_user_mod(int argc, char **argv, char *arg1)
 			nis = true;
 			break;
 		default:
-			exit(EX_USAGE);
+			usage();
 		}
 	}
+	argc -= optind;
+	argv += optind;
+	if (argc > 0)
+		usage();
 
 	if (geteuid() != 0 && ! dryrun)
 		errx(EX_NOPERM, "you must be root");
@@ -1787,7 +1815,7 @@ pw_user_mod(int argc, char **argv, char *arg1)
 	if (pwd == NULL)
 		errx(EX_NOUSER, "user '%s' disappeared during update", name);
 	grp = GETGRGID(pwd->pw_gid);
-	pw_log(cnf, M_UPDATE, W_USER, "%s(%ju):%s(%ju):%s:%s:%s",
+	pw_log(cnf, M_MODIFY, W_USER, "%s(%ju):%s(%ju):%s:%s:%s",
 	    pwd->pw_name, (uintmax_t)pwd->pw_uid,
 	    grp ? grp->gr_name : "unknown",
 	    (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1),
@@ -1808,7 +1836,7 @@ pw_user_mod(int argc, char **argv, char *arg1)
 	}
 
 	if (nis && nis_update() == 0)
-		pw_log(cnf, M_UPDATE, W_USER, "NIS maps updated");
+		pw_log(cnf, M_MODIFY, W_USER, "NIS maps updated");
 
 	return (EXIT_SUCCESS);
 }
diff --git a/usr.sbin/pw/tests/pw_useradd_test.sh b/usr.sbin/pw/tests/pw_useradd_test.sh
index 3b495482eb05..b4efa42bada7 100755
--- a/usr.sbin/pw/tests/pw_useradd_test.sh
+++ b/usr.sbin/pw/tests/pw_useradd_test.sh
@@ -313,6 +313,22 @@ user_add_R_intermed_body() {
 	test -d ${HOME}/a/b/c/foo || atf_fail "user directory not created"
 }
 
+atf_test_case user_add_dir
+user_add_dir_body() {
+	populate_root_etc_skel
+
+	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 save:ugid \
+	      awk -F: '$1 == "foo" { print $3, $4 }' \
+	      $HOME/etc/master.passwd
+	atf_check -o file:ugid \
+	    stat -f '%u %g' ${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
@@ -511,6 +527,7 @@ atf_init_test_cases() {
 	atf_add_test_case user_add_R
 	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_skel
 	atf_add_test_case user_add_uid0
 	atf_add_test_case user_add_uid_too_large