git: e3bd4b9288cc - stable/13 - pw: Rename some enums.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Thu, 09 May 2024 13:16:53 UTC
The branch stable/13 has been updated by des:

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

commit e3bd4b9288cc05ac4b9e6bf7574d4d2f10ae02a2
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 11:05:35 +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)
---
 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 ac04dc2fa89b..ebe80977fa0e 100644
--- a/usr.sbin/pw/pw_user.c
+++ b/usr.sbin/pw/pw_user.c
@@ -163,7 +163,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);
 }
 
@@ -756,9 +756,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);
@@ -820,9 +824,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);
@@ -903,9 +911,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);
@@ -1055,9 +1067,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));
 }
@@ -1074,9 +1090,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));
 }
@@ -1343,9 +1363,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");
@@ -1656,9 +1680,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");
@@ -1839,7 +1867,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),
@@ -1860,7 +1888,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 b191d51104f7..93a192c59409 100755
--- a/usr.sbin/pw/tests/pw_useradd_test.sh
+++ b/usr.sbin/pw/tests/pw_useradd_test.sh
@@ -305,6 +305,22 @@ user_add_R_symlink_body() {
 	atf_check -s exit:0 -o inline:"usr/home\n" readlink ${HOME}/home
 }
 
+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
@@ -479,6 +495,7 @@ atf_init_test_cases() {
 	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_dir
 	atf_add_test_case user_add_skel
 	atf_add_test_case user_add_uid0
 	atf_add_test_case user_add_uid_too_large