Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 23 Aug 2015 21:42:28 +0000 (UTC)
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r287084 - in stable/10/usr.sbin/pw: . tests
Message-ID:  <201508232142.t7NLgSXX033227@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: bapt
Date: Sun Aug 23 21:42:27 2015
New Revision: 287084
URL: https://svnweb.freebsd.org/changeset/base/287084

Log:
  MFC: r285133,r285136,r285137,r285156,r285157,r285158,r285256,r285318,r285395,
  r285396,r285398,r285401,r285403,r285405,r285406,r285408,r285409,r285411,
  r285412,r285413,r285415,r285418,r285430,r285433,r285434,r285442,r285948,
  r285984,r285985,r285989,r285996,r285997,r286045,r286047,r286066,r286150,
  r286151,r286152,r286154,r286155,r286156,r286157,r286173,r286196,r286197,
  r286198,r286199,r286200,r286201,r286202,r286203,r286204,r286210,r286211,
  r286217,r286218,r286258,r286259,r286341,r286775,r286982,r286986,r286991,
  r286993
  
  Validate most pw inputs.
  Rewrite the way parsing sub arguments is made to simplify code and improve
  maintenability
  Add -y (NIS) to userdel/usermod
  pw userdel -r <rootdir> now deletes directories in the rootdir
  Only parse pw.conf when needed
  Reject usermod and userdel if the user concerned is not on the user database
  supposed to be manipulated

Added:
  stable/10/usr.sbin/pw/pw_utils.c
     - copied, changed from r286196, head/usr.sbin/pw/pw_utils.c
  stable/10/usr.sbin/pw/strtounum.c
     - copied, changed from r285997, head/usr.sbin/pw/strtounum.c
  stable/10/usr.sbin/pw/tests/pw_groupadd.sh
     - copied, changed from r285985, head/usr.sbin/pw/tests/pw_groupadd.sh
Deleted:
  stable/10/usr.sbin/pw/fileupd.c
Modified:
  stable/10/usr.sbin/pw/Makefile
  stable/10/usr.sbin/pw/cpdir.c
  stable/10/usr.sbin/pw/grupd.c
  stable/10/usr.sbin/pw/psdate.c
  stable/10/usr.sbin/pw/pw.8
  stable/10/usr.sbin/pw/pw.c
  stable/10/usr.sbin/pw/pw.h
  stable/10/usr.sbin/pw/pw_conf.c
  stable/10/usr.sbin/pw/pw_group.c
  stable/10/usr.sbin/pw/pw_log.c
  stable/10/usr.sbin/pw/pw_nis.c
  stable/10/usr.sbin/pw/pw_user.c
  stable/10/usr.sbin/pw/pw_vpw.c
  stable/10/usr.sbin/pw/pwupd.c
  stable/10/usr.sbin/pw/pwupd.h
  stable/10/usr.sbin/pw/rm_r.c
  stable/10/usr.sbin/pw/tests/Makefile
  stable/10/usr.sbin/pw/tests/helper_functions.shin
  stable/10/usr.sbin/pw/tests/pw_groupdel.sh
  stable/10/usr.sbin/pw/tests/pw_groupmod.sh
  stable/10/usr.sbin/pw/tests/pw_useradd.sh
  stable/10/usr.sbin/pw/tests/pw_userdel.sh
  stable/10/usr.sbin/pw/tests/pw_usermod.sh
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/usr.sbin/pw/Makefile
==============================================================================
--- stable/10/usr.sbin/pw/Makefile	Sun Aug 23 21:10:20 2015	(r287083)
+++ stable/10/usr.sbin/pw/Makefile	Sun Aug 23 21:42:27 2015	(r287084)
@@ -5,8 +5,8 @@
 PROG=	pw
 MAN=	pw.conf.5 pw.8
 SRCS=	pw.c pw_conf.c pw_user.c pw_group.c pw_log.c pw_nis.c pw_vpw.c \
-	grupd.c pwupd.c fileupd.c psdate.c \
-	bitmap.c cpdir.c rm_r.c
+	grupd.c pwupd.c psdate.c bitmap.c cpdir.c rm_r.c strtounum.c \
+	pw_utils.c
 
 WARNS?=	3
 

Modified: stable/10/usr.sbin/pw/cpdir.c
==============================================================================
--- stable/10/usr.sbin/pw/cpdir.c	Sun Aug 23 21:10:20 2015	(r287083)
+++ stable/10/usr.sbin/pw/cpdir.c	Sun Aug 23 21:42:27 2015	(r287084)
@@ -29,103 +29,96 @@ static const char rcsid[] =
   "$FreeBSD$";
 #endif /* not lint */
 
+#include <dirent.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <stdio.h>
 #include <string.h>
-#include <stdlib.h>
 #include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/param.h>
-#include <dirent.h>
 
 #include "pw.h"
 #include "pwupd.h"
 
 void
-copymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid)
+copymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
+    gid_t gid, int flags)
 {
-	char            src[MAXPATHLEN];
-	char            dst[MAXPATHLEN];
-	char            lnk[MAXPATHLEN];
-	int             len;
+	char		*p, lnk[MAXPATHLEN], copybuf[4096];
+	int		len, homefd, srcfd, destfd;
+	ssize_t		sz;
+	struct stat     st;
+	struct dirent  *e;
+	DIR		*d;
 
-	if (mkdir(dir, mode) != 0 && errno != EEXIST) {
+	if (*dir == '/')
+		dir++;
+
+	if (mkdirat(rootfd, dir, mode) != 0 && errno != EEXIST) {
 		warn("mkdir(%s)", dir);
-	} else {
-		int             infd, outfd;
-		struct stat     st;
-
-		static char     counter = 0;
-		static char    *copybuf = NULL;
-
-		++counter;
-		chown(dir, uid, gid);
-		if (skel != NULL && *skel != '\0') {
-			DIR            *d = opendir(skel);
-
-			if (d != NULL) {
-				struct dirent  *e;
-
-				while ((e = readdir(d)) != NULL) {
-					char           *p = e->d_name;
-
-					if (snprintf(src, sizeof(src), "%s/%s", skel, p) >= (int)sizeof(src))
-						warn("warning: pathname too long '%s/%s' (skel not copied)", skel, p);
-					else if (lstat(src, &st) == 0) {
-						if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
-							p += 3;
-						if (snprintf(dst, sizeof(dst), "%s/%s", dir, p) >= (int)sizeof(dst))
-							warn("warning: path too long '%s/%s' (skel file skipped)", dir, p);
-						else {
-						    if (S_ISDIR(st.st_mode)) {	/* Recurse for this */
-							if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0)
-								copymkdir(dst, src, st.st_mode & _DEF_DIRMODE, uid, gid);
-								chflags(dst, st.st_flags);	/* propagate flags */
-						    } else if (S_ISLNK(st.st_mode) && (len = readlink(src, lnk, sizeof(lnk) - 1)) != -1) {
-							lnk[len] = '\0';
-							symlink(lnk, dst);
-							lchown(dst, uid, gid);
-							/*
-							 * Note: don't propagate special attributes
-							 * but do propagate file flags
-							 */
-						    } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
-							if ((infd = open(src, O_RDONLY)) == -1) {
-								close(outfd);
-								remove(dst);
-							} else {
-								int             b;
-
-								/*
-								 * Allocate our copy buffer if we need to
-								 */
-								if (copybuf == NULL)
-									copybuf = malloc(4096);
-								while ((b = read(infd, copybuf, 4096)) > 0)
-									write(outfd, copybuf, b);
-								close(infd);
-								/*
-								 * Propagate special filesystem flags
-								 */
-								fchown(outfd, uid, gid);
-								fchflags(outfd, st.st_flags);
-								close(outfd);
-								chown(dst, uid, gid);
-							}
-						    }
-						}
-					}
-				}
-				closedir(d);
-			}
+		return;
+	}
+	fchownat(rootfd, dir, uid, gid, AT_SYMLINK_NOFOLLOW);
+	if (flags > 0)
+		chflagsat(rootfd, dir, flags, AT_SYMLINK_NOFOLLOW);
+
+	if (skelfd == -1)
+		return;
+
+	homefd = openat(rootfd, dir, O_DIRECTORY);
+	if ((d = fdopendir(skelfd)) == NULL) {
+		close(skelfd);
+		close(homefd);
+		return;
+	}
+
+	while ((e = readdir(d)) != NULL) {
+		if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
+			continue;
+
+		p = e->d_name;
+		if (fstatat(skelfd, p, &st, AT_SYMLINK_NOFOLLOW) == -1)
+			continue;
+
+		if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
+			p += 3;
+
+		if (S_ISDIR(st.st_mode)) {
+			copymkdir(homefd, p, openat(skelfd, e->d_name, O_DIRECTORY),
+			    st.st_mode & _DEF_DIRMODE, uid, gid, st.st_flags);
+			continue;
 		}
-		if (--counter == 0 && copybuf != NULL) {
-			free(copybuf);
-			copybuf = NULL;
+
+		if (S_ISLNK(st.st_mode) &&
+		    (len = readlinkat(skelfd, e->d_name, lnk, sizeof(lnk) -1))
+		    != -1) {
+			lnk[len] = '\0';
+			symlinkat(lnk, homefd, p);
+			fchownat(homefd, p, uid, gid, AT_SYMLINK_NOFOLLOW);
+			continue;
 		}
+
+		if (!S_ISREG(st.st_mode))
+			continue;
+
+		if ((srcfd = openat(skelfd, e->d_name, O_RDONLY)) == -1)
+			continue;
+		destfd = openat(homefd, p, O_RDWR | O_CREAT | O_EXCL,
+		    st.st_mode);
+		if (destfd == -1) {
+			close(srcfd);
+			continue;
+		}
+
+		while ((sz = read(srcfd, copybuf, sizeof(copybuf))) > 0)
+			write(destfd, copybuf, sz);
+
+		close(srcfd);
+		/*
+		 * Propagate special filesystem flags
+		 */
+		fchown(destfd, uid, gid);
+		fchflags(destfd, st.st_flags);
+		close(destfd);
 	}
+	closedir(d);
 }
-

Modified: stable/10/usr.sbin/pw/grupd.c
==============================================================================
--- stable/10/usr.sbin/pw/grupd.c	Sun Aug 23 21:10:20 2015	(r287083)
+++ stable/10/usr.sbin/pw/grupd.c	Sun Aug 23 21:42:27 2015	(r287084)
@@ -29,13 +29,11 @@ static const char rcsid[] =
   "$FreeBSD$";
 #endif /* not lint */
 
+#include <err.h>
 #include <grp.h>
 #include <libutil.h>
-#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-#include <sys/param.h>
 
 #include "pwupd.h"
 

Modified: stable/10/usr.sbin/pw/psdate.c
==============================================================================
--- stable/10/usr.sbin/pw/psdate.c	Sun Aug 23 21:10:20 2015	(r287083)
+++ stable/10/usr.sbin/pw/psdate.c	Sun Aug 23 21:42:27 2015	(r287084)
@@ -29,30 +29,16 @@ static const char rcsid[] =
   "$FreeBSD$";
 #endif /* not lint */
 
-#include <stdio.h>
+#include <ctype.h>
+#include <err.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
+#include <xlocale.h>
 
 #include "psdate.h"
 
 
 static int
-a2i(char const ** str)
-{
-	int             i = 0;
-	char const     *s = *str;
-
-	if (isdigit((unsigned char)*s)) {
-		i = atoi(s);
-		while (isdigit((unsigned char)*s))
-			++s;
-		*str = s;
-	}
-	return i;
-}
-
-static int
 numerics(char const * str)
 {
 	int             rc = isdigit((unsigned char)*str);
@@ -95,61 +81,73 @@ weekday(char const ** str)
 	return aindex(days, str, 3);
 }
 
-static int
-month(char const ** str)
-{
-	static char const *months[] =
-	{"jan", "feb", "mar", "apr", "may", "jun", "jul",
-	"aug", "sep", "oct", "nov", "dec", NULL};
-
-	return aindex(months, str, 3);
-}
-
 static void
-parse_time(char const * str, int *hour, int *min, int *sec)
+parse_datesub(char const * str, struct tm *t)
 {
-	*hour = a2i(&str);
-	if ((str = strchr(str, ':')) == NULL)
-		*min = *sec = 0;
-	else {
-		++str;
-		*min = a2i(&str);
-		*sec = ((str = strchr(str, ':')) == NULL) ? 0 : atoi(++str);
+	struct tm	 tm;
+	locale_t	 l;
+	int		 i;
+	char		*ret;
+	const char	*valid_formats[] = {
+		"%d-%b-%y",
+		"%d-%b-%Y",
+		"%d-%m-%y",
+		"%d-%m-%Y",
+		"%H:%M %d-%b-%y",
+		"%H:%M %d-%b-%Y",
+		"%H:%M %d-%m-%y",
+		"%H:%M %d-%m-%Y",
+		"%H:%M:%S %d-%b-%y",
+		"%H:%M:%S %d-%b-%Y",
+		"%H:%M:%S %d-%m-%y",
+		"%H:%M:%S %d-%m-%Y",
+		"%d-%b-%y %H:%M",
+		"%d-%b-%Y %H:%M",
+		"%d-%m-%y %H:%M",
+		"%d-%m-%Y %H:%M",
+		"%d-%b-%y %H:%M:%S",
+		"%d-%b-%Y %H:%M:%S",
+		"%d-%m-%y %H:%M:%S",
+		"%d-%m-%Y %H:%M:%S",
+		"%H:%M\t%d-%b-%y",
+		"%H:%M\t%d-%b-%Y",
+		"%H:%M\t%d-%m-%y",
+		"%H:%M\t%d-%m-%Y",
+		"%H:%M\t%S %d-%b-%y",
+		"%H:%M\t%S %d-%b-%Y",
+		"%H:%M\t%S %d-%m-%y",
+		"%H:%M\t%S %d-%m-%Y",
+		"%d-%b-%y\t%H:%M",
+		"%d-%b-%Y\t%H:%M",
+		"%d-%m-%y\t%H:%M",
+		"%d-%m-%Y\t%H:%M",
+		"%d-%b-%y\t%H:%M:%S",
+		"%d-%b-%Y\t%H:%M:%S",
+		"%d-%m-%y\t%H:%M:%S",
+		"%d-%m-%Y\t%H:%M:%S",
+		NULL,
+	};
+
+	l = newlocale(LC_ALL_MASK, "C", NULL);
+
+	memset(&tm, 0, sizeof(tm));
+	for (i=0; valid_formats[i] != NULL; i++) {
+		ret = strptime_l(str, valid_formats[i], &tm, l);
+		if (ret && *ret == '\0') {
+			t->tm_mday = tm.tm_mday;
+			t->tm_mon = tm.tm_mon;
+			t->tm_year = tm.tm_year;
+			t->tm_hour = tm.tm_hour;
+			t->tm_min = tm.tm_min;
+			t->tm_sec = tm.tm_sec;
+			freelocale(l);
+			return;
+		}
 	}
-}
-
-
-static void
-parse_datesub(char const * str, int *day, int *mon, int *year)
-{
-	int             i;
 
-	static char const nchrs[] = "0123456789 \t,/-.";
+	freelocale(l);
 
-	if ((i = month(&str)) != -1) {
-		*mon = i;
-		if ((i = a2i(&str)) != 0)
-			*day = i;
-	} else if ((i = a2i(&str)) != 0) {
-		*day = i;
-		while (*str && strchr(nchrs + 10, *str) != NULL)
-			++str;
-		if ((i = month(&str)) != -1)
-			*mon = i;
-		else if ((i = a2i(&str)) != 0)
-			*mon = i - 1;
-	} else
-		return;
-
-	while (*str && strchr(nchrs + 10, *str) != NULL)
-		++str;
-	if (isdigit((unsigned char)*str)) {
-		*year = atoi(str);
-		if (*year > 1900)
-			*year -= 1900;
-		else if (*year < 32)
-			*year += 100;
-	}
+	errx(EXIT_FAILURE, "Invalid date");
 }
 
 
@@ -256,39 +254,7 @@ parse_date(time_t dt, char const * str)
 			}
 		}
 
-		/*
-		 * See if there is a time hh:mm[:ss]
-		 */
-		if ((p = strchr(tmp, ':')) == NULL) {
-
-			/*
-			 * No time string involved
-			 */
-			T->tm_hour = T->tm_min = T->tm_sec = 0;
-			parse_datesub(tmp, &T->tm_mday, &T->tm_mon, &T->tm_year);
-		} else {
-			char            datestr[64], timestr[64];
-
-			/*
-			 * Let's chip off the time string
-			 */
-			if ((q = strpbrk(p, " \t")) != NULL) {	/* Time first? */
-				int             l = q - str;
-
-				strlcpy(timestr, str, l + 1);
-				strlcpy(datestr, q + 1, sizeof(datestr));
-				parse_time(timestr, &T->tm_hour, &T->tm_min, &T->tm_sec);
-				parse_datesub(datestr, &T->tm_mday, &T->tm_mon, &T->tm_year);
-			} else if ((q = strrchr(tmp, ' ')) != NULL) {	/* Time last */
-				int             l = q - tmp;
-
-				strlcpy(timestr, q + 1, sizeof(timestr));
-				strlcpy(datestr, tmp, l + 1);
-			} else	/* Bail out */
-				return dt;
-			parse_time(timestr, &T->tm_hour, &T->tm_min, &T->tm_sec);
-			parse_datesub(datestr, &T->tm_mday, &T->tm_mon, &T->tm_year);
-		}
+		parse_datesub(tmp, T);
 		dt = mktime(T);
 	}
 	return dt;

Modified: stable/10/usr.sbin/pw/pw.8
==============================================================================
--- stable/10/usr.sbin/pw/pw.8	Sun Aug 23 21:10:20 2015	(r287083)
+++ stable/10/usr.sbin/pw/pw.8	Sun Aug 23 21:42:27 2015	(r287084)
@@ -35,11 +35,9 @@
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar useradd
-.Op name|uid
+.Oo Fl n Oc name Oo Fl u Ar uid Oc
 .Op Fl C Ar config
 .Op Fl q
-.Op Fl n Ar name
-.Op Fl u Ar uid
 .Op Fl c Ar comment
 .Op Fl d Ar dir
 .Op Fl e Ar date
@@ -61,7 +59,6 @@
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar useradd
-.Op name|uid
 .Fl D
 .Op Fl C Ar config
 .Op Fl q
@@ -81,27 +78,23 @@
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar userdel
-.Op name|uid
-.Op Fl n Ar name
-.Op Fl u Ar uid
+.Oo Fl n Oc name|uid | Fl u Ar uid
 .Op Fl r
 .Op Fl Y
 .Nm
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar usermod
-.Op name|uid
+.Oo Fl n Oc name|uid Oo Fl u Ar newuid Oc | Fl u Ar uid
 .Op Fl C Ar config
 .Op Fl q
-.Op Fl n Ar name
-.Op Fl u Ar uid
 .Op Fl c Ar comment
 .Op Fl d Ar dir
 .Op Fl e Ar date
 .Op Fl p Ar date
 .Op Fl g Ar group
 .Op Fl G Ar grouplist
-.Op Fl l Ar name
+.Op Fl l Ar newname
 .Op Fl m
 .Op Fl M Ar mode
 .Op Fl k Ar dir
@@ -116,9 +109,7 @@
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar usershow
-.Op name|uid
-.Op Fl n Ar name
-.Op Fl u Ar uid
+.Oo Fl n Oc name|uid | Fl u Ar uid
 .Op Fl F
 .Op Fl P
 .Op Fl 7
@@ -133,11 +124,9 @@
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar groupadd
-.Op group|gid
+.Oo Fl n Oc name Oo Fl g Ar gid Oc
 .Op Fl C Ar config
 .Op Fl q
-.Op Fl n Ar group
-.Op Fl g Ar gid
 .Op Fl M Ar members
 .Op Fl o
 .Op Fl h Ar fd | Fl H Ar fd
@@ -148,20 +137,16 @@
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar groupdel
-.Op group|gid
-.Op Fl n Ar name
-.Op Fl g Ar gid
+.Oo Fl n Oc name|gid | Fl g Ar gid
 .Op Fl Y
 .Nm
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar groupmod
-.Op group|gid
+.Oo Fl n Oc name|gid Oo Fl g Ar newgid Oc | Fl g Ar gid
 .Op Fl C Ar config
 .Op Fl q
-.Op Fl n Ar name
-.Op Fl g Ar gid
-.Op Fl l Ar name
+.Op Fl l Ar newname
 .Op Fl M Ar members
 .Op Fl m Ar newmembers
 .Op Fl d Ar oldmembers
@@ -173,9 +158,7 @@
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar groupshow
-.Op group|gid
-.Op Fl n Ar name
-.Op Fl g Ar gid
+.Oo Fl n Oc name|gid | Fl g Ar gid
 .Op Fl F
 .Op Fl P
 .Op Fl a
@@ -189,14 +172,14 @@
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar lock
-.Op name|uid
+.Oo Fl n Oc name|uid | Fl u Ar uid
 .Op Fl C Ar config
 .Op Fl q
 .Nm
 .Op Fl R Ar rootdir
 .Op Fl V Ar etcdir
 .Ar unlock
-.Op name|uid
+.Oo Fl n Oc name|uid | Fl u Ar uid
 .Op Fl C Ar config
 .Op Fl q
 .Sh DESCRIPTION
@@ -250,8 +233,9 @@ all mean the same thing.)
 This flexibility is useful for interactive scripts calling
 .Nm
 for user and group database manipulation.
-Following these keywords, you may optionally specify the user or group name or numeric
-id as an alternative to using the
+Following these keywords,
+the user or group name or numeric id may be optionally specified as an
+alternative to using the
 .Fl n Ar name ,
 .Fl u Ar uid ,
 .Fl g Ar gid
@@ -266,12 +250,13 @@ will operate.
 Any paths specified will be relative to
 .Va rootdir .
 .It Fl V Ar etcdir
-This flag sets an alternate location for the password, group and configuration files,
-and may be used to maintain a user/group database in an alternate location.
+Set an alternate location for the password, group, and configuration files.
+Can be used to maintain a user/group database in an alternate location.
 If this switch is specified, the system
 .Pa /etc/pw.conf
-will not be sourced for default configuration data, but the file pw.conf in the
-specified directory will be used instead (or none, if it does not exist).
+will not be sourced for default configuration data,
+but the file pw.conf in the specified directory will be used instead
+.Pq or none, if it does not exist .
 The
 .Fl C
 flag may be used to override this behaviour.
@@ -294,7 +279,8 @@ configuration file.
 .It Fl q
 Use of this option causes
 .Nm
-to suppress error messages, which may be useful in interactive environments where it
+to suppress error messages,
+which may be useful in interactive environments where it
 is preferable to interpret status codes returned by
 .Nm
 rather than messing up a carefully formatted display.
@@ -338,27 +324,40 @@ and
 .Ar usermod
 commands:
 .Bl -tag -width "-G grouplist"
-.It Fl n Ar name
+.It Oo Fl n Oc Ar name
+Required unless
+.Fl u Ar uid
+is given.
 Specify the user/account name.
+In the case of
+.Ar usermod
+can be a uid.
 .It Fl u Ar uid
+Required if
+.Ar name
+is not given.
 Specify the user/account numeric id.
-.Pp
-Usually, you only need to provide one or the other of these options, as the account
-name will imply the uid, or vice versa.
-However, there are times when you need to provide both.
+In the case of
+.Ar usermod
+if paired with
+.Ar name ,
+changes the numeric id of the named user/account.
+.Pp
+Usually, only one of these options is required,
+as the account name will imply the uid, or vice versa.
+However, there are times when both are needed.
 For example, when changing the uid of an existing user with
 .Ar usermod ,
-or overriding the default uid when creating a new account.
-If you wish
-.Nm
-to automatically allocate the uid to a new user with
+or overriding the default uid when creating a new account with
+.Ar useradd .
+To automatically allocate the uid to a new user with
 .Ar useradd ,
-then you should
+then do
 .Em not
 use the
 .Fl u
 option.
-You may also provide either the account or userid immediately after the
+Either the account or userid can also be provided immediately after the
 .Ar useradd ,
 .Ar userdel ,
 .Ar usermod
@@ -372,21 +371,23 @@ options.
 .El
 .Bl -tag -width "-G grouplist"
 .It Fl c Ar comment
-This field sets the contents of the passwd GECOS field, which normally contains up
-to four comma-separated fields containing the user's full name, office or location,
+This field sets the contents of the passwd GECOS field,
+which normally contains up to four comma-separated fields containing the
+user's full name, office or location,
 and work and home phone numbers.
 These sub-fields are used by convention only, however, and are optional.
-If this field is to contain spaces, you need to quote the comment itself with double
-quotes
+If this field is to contain spaces,
+the comment must be enclosed in double quotes
 .Ql \&" .
-Avoid using commas in this field as these are used as sub-field separators, and the
-colon
+Avoid using commas in this field as these are used as sub-field separators,
+and the colon
 .Ql \&:
 character also cannot be used as this is the field separator for the passwd
 file itself.
 .It Fl d Ar dir
 This option sets the account's home directory.
-Normally, you will only use this if the home directory is to be different from the
+Normally,
+this is only used if the home directory is to be different from the
 default determined from
 .Pa /etc/pw.conf
 - normally
@@ -396,13 +397,15 @@ with the account name as a subdirectory.
 Set the account's expiration date.
 Format of the date is either a UNIX time in decimal, or a date in
 .Ql dd-mmm-yy[yy]
-format, where dd is the day, mmm is the month, either in numeric or alphabetic format
+format, where dd is the day,
+mmm is the month, either in numeric or alphabetic format
 ('Jan', 'Feb', etc) and year is either a two or four digit year.
 This option also accepts a relative date in the form
 .Ql \&+n[mhdwoy]
 where
 .Ql \&n
-is a decimal, octal (leading 0) or hexadecimal (leading 0x) digit followed by the
+is a decimal,
+octal (leading 0) or hexadecimal (leading 0x) digit followed by the
 number of Minutes, Hours, Days, Weeks, Months or Years from the current date at
 which the expiration date is to be set.
 .It Fl p Ar date
@@ -442,8 +445,8 @@ This option instructs
 to attempt to create the user's home directory.
 While primarily useful when adding a new account with
 .Ar useradd ,
-this may also be of use when moving an existing user's home directory elsewhere on
-the file system.
+this may also be of use when moving an existing user's home directory elsewhere
+on the file system.
 The new home directory is populated with the contents of the
 .Ar skeleton
 directory, which typically contains a set of shell configuration files that the
@@ -461,7 +464,8 @@ existing configuration files in the user
 .Em not
 overwritten from the skeleton files.
 .Pp
-When a user's home directory is created, it will by default be a subdirectory of the
+When a user's home directory is created,
+it will by default be a subdirectory of the
 .Ar basehome
 directory as specified by the
 .Fl b
@@ -599,10 +603,13 @@ The default value for this is
 but it may be set elsewhere as desired.
 .It Fl e Ar days
 Set the default account expiration period in days.
-Unlike use without
-.Fl D ,
-the argument must be numeric, which specifies the number of days after creation when
-the account is to expire.
+When
+.Fl D
+is used, the
+.Ar days
+argument is interpreted differently.
+It must be numeric and represents the number of days after creation
+that the account expires.
 A value of 0 suppresses automatic calculation of the expiry date.
 .It Fl p Ar days
 Set the default password expiration period in days.
@@ -615,8 +622,8 @@ with the same name as their login name.
 If a group is supplied, either its name or uid may be given as an argument.
 .It Fl G Ar grouplist
 Set the default groups in which new users are granted membership.
-This is a separate set of groups from the primary group, and you should avoid
-nominating the same group as both primary and extra groups.
+This is a separate set of groups from the primary group.
+Avoid nominating the same group as both primary and extra groups.
 In other words, these extra groups determine membership in groups
 .Em other than
 the primary group.
@@ -630,7 +637,8 @@ This option sets the default login class
 .It Fl k Ar dir
 Set the default
 .Em skeleton
-directory, from which prototype shell and other initialization files are copied when
+directory,
+from which prototype shell and other initialization files are copied when
 .Nm
 creates a user's home directory.
 See description of
@@ -640,22 +648,24 @@ for naming conventions of these files.
 .Fl u Ar min , Ns Ar max ,
 .Fl i Ar min , Ns Ar max
 .Xc
-These options set the minimum and maximum user and group ids allocated for new accounts
-and groups created by
+Set the minimum and maximum user and group ids allocated for new
+accounts and groups created by
 .Nm .
 The default values for each is 1000 minimum and 32000 maximum.
 .Ar min
 and
 .Ar max
-are both numbers, where max must be greater than min, and both must be between 0
-and 32767.
-In general, user and group ids less than 100 are reserved for use by the system,
-and numbers greater than 32000 may also be reserved for special purposes (used by
-some system daemons).
+are both numbers, where max must be greater than min,
+and both must be between 0 and 32767.
+In general,
+user and group ids less than 100 are reserved for use by the system,
+and numbers greater than 32000 may also be reserved for special purposes
+.Pq used by some system daemons .
 .It Fl w Ar method
 The
 .Fl w
-option sets the default method used to set passwords for newly created user accounts.
+option selects the default method used to set passwords for newly created user
+accounts.
 .Ar method
 is one of:
 .Pp
@@ -676,9 +686,11 @@ or
 .Ql \&no
 methods are the most secure; in the former case,
 .Nm
-generates a password and prints it to stdout, which is suitable where you issue
-users with passwords to access their accounts rather than having the user nominate
-their own (possibly poorly chosen) password.
+generates a password and prints it to stdout,
+which is suitable when users are issued passwords rather than being allowed
+to select their own
+.Pq possibly poorly chosen
+password.
 The
 .Ql \&no
 method requires that the superuser use
@@ -699,7 +711,7 @@ servers.
 .Pp
 The
 .Ar userdel
-command has only three valid options.
+command has three distinct options.
 The
 .Fl n Ar name
 and
@@ -714,7 +726,8 @@ to remove the user's home directory and 
 The
 .Nm
 utility errs on the side of caution when removing files from the system.
-Firstly, it will not do so if the uid of the account being removed is also used by
+Firstly,
+it will not do so if the uid of the account being removed is also used by
 another account on the system, and the 'home' directory in the password file is
 a valid path that commences with the character
 .Ql \&/ .
@@ -725,20 +738,20 @@ will be removed.
 If any additional cleanup work is required, this is left to the administrator.
 .El
 .Pp
-Mail spool files and crontabs are always removed when an account is deleted as these
-are unconditionally attached to the user name.
+Mail spool files and crontabs are always removed when an account is deleted as
+these are unconditionally attached to the user name.
 Jobs queued for processing by
 .Ar at
-are also removed if the user's uid is unique and not also used by another account on the
-system.
+are also removed if the user's uid is unique and not also used by another
+account on the system.
 .Pp
 The
 .Ar usermod
 command adds one additional option:
 .Bl -tag -width "-G grouplist"
-.It Fl l Ar name
+.It Fl l Ar newname
 This option allows changing of an existing account name to
-.Ql \&name .
+.Ql \&newname .
 The new name must not already exist, and any attempt to duplicate an
 existing account name will be rejected.
 .El
@@ -782,10 +795,24 @@ options (explained at the start of the p
 with the group manipulation commands.
 Other common options to all group-related commands are:
 .Bl -tag -width "-m newmembers"
-.It Fl n Ar name
+.It Oo Fl n Oc Ar name
+Required unless
+.Fl g Ar gid
+is given.
 Specify the group name.
+In the case of
+.Ar groupmod
+can be a gid.
 .It Fl g Ar gid
+Required if
+.Ar name
+is not given.
 Specify the group numeric id.
+In the case of
+.Ar groupmod
+if paired with
+.Ar name ,
+changes the numeric id of the named group.
 .Pp
 As with the account name and id fields, you will usually only need
 to supply one of these, as the group name implies the uid and vice
@@ -822,18 +849,19 @@ silently eliminated.
 also has a
 .Fl o
 option that allows allocation of an existing group id to a new group.
-The default action is to reject an attempt to add a group, and this option overrides
-the check for duplicate group ids.
+The default action is to reject an attempt to add a group,
+and this option overrides the check for duplicate group ids.
 There is rarely any need to duplicate a group id.
 .Pp
 The
 .Ar groupmod
 command adds one additional option:
 .Bl -tag -width "-m newmembers"
-.It Fl l Ar name
+.It Fl l Ar newname
 This option allows changing of an existing group name to
-.Ql \&name .
-The new name must not already exist, and any attempt to duplicate an existing group
+.Ql \&newname .
+The new name must not already exist,
+and any attempt to duplicate an existing group
 name will be rejected.
 .El
 .Pp

Modified: stable/10/usr.sbin/pw/pw.c
==============================================================================
--- stable/10/usr.sbin/pw/pw.c	Sun Aug 23 21:10:20 2015	(r287083)
+++ stable/10/usr.sbin/pw/pw.c	Sun Aug 23 21:42:27 2015	(r287084)
@@ -32,14 +32,12 @@ static const char rcsid[] =
 #include <err.h>
 #include <fcntl.h>
 #include <locale.h>
-#include <paths.h>
-#include <stdbool.h>
-#include <sys/wait.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
 #include "pw.h"
 
-#if !defined(_PATH_YP)
-#define	_PATH_YP	"/var/yp/"
-#endif
 const char     *Modes[] = {
   "add", "del", "mod", "show", "next",
   NULL};
@@ -85,71 +83,53 @@ struct pwf VPWF =
 	vgetgrnam,
 };
 
-struct pwconf conf;
-
-static struct cargs arglist;
+static int (*cmdfunc[W_NUM][M_NUM])(int argc, char **argv, char *_name) = {
+	{ /* user */
+		pw_user_add,
+		pw_user_del,
+		pw_user_mod,
+		pw_user_show,
+		pw_user_next,
+		pw_user_lock,
+		pw_user_unlock,
+	},
+	{ /* group */
+		pw_group_add,
+		pw_group_del,
+		pw_group_mod,
+		pw_group_show,
+		pw_group_next,
+	}
+};
 
-static int      getindex(const char *words[], const char *word);
-static void     cmdhelp(int mode, int which);
+struct pwconf conf;
 
+static int	getindex(const char *words[], const char *word);
+static void	cmdhelp(int mode, int which);
 
 int
 main(int argc, char *argv[])
 {
-	int             ch;
-	int             mode = -1;
-	int             which = -1;
-	long		id = -1;
-	char		*config = NULL;
+	int		mode = -1, which = -1, tmp;
 	struct stat	st;
-	const char	*errstr;
-	char		arg, *name;
+	char		arg, *arg1;
 	bool		relocated, nis;
 
-	static const char *opts[W_NUM][M_NUM] =
-	{
-		{ /* user */
-			"R:V:C:qn:u:c:d:e:p:g:G:mM:k:s:oL:i:w:h:H:Db:NPy:Y",
-			"R:V:C:qn:u:rY",
-			"R:V:C:qn:u:c:d:e:p:g:G:mM:l:k:s:w:L:h:H:FNPY",
-			"R:V:C:qn:u:FPa7",
-			"R:V:C:q",
-			"R:V:C:q",
-			"R:V:C:q"
-		},
-		{ /* grp  */
-			"R:V:C:qn:g:h:H:M:opNPY",
-			"R:V:C:qn:g:Y",
-			"R:V:C:qn:d:g:l:h:H:FM:m:NPY",
-			"R:V:C:qn:g:FPa",
-			"R:V:C:q"
-		 }
-	};
-
-	static int      (*funcs[W_NUM]) (int _mode, char *_name, long _id,
-	    struct cargs * _args) =
-	{			/* Request handlers */
-		pw_user,
-		pw_group
-	};
-
-	name = NULL;
+	arg1 = NULL;
 	relocated = nis = false;
 	memset(&conf, 0, sizeof(conf));
+	strlcpy(conf.rootdir, "/", sizeof(conf.rootdir));
 	strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath));
 	conf.checkduplicate = true;
+	conf.fd = -1;
 
-	LIST_INIT(&arglist);
-
-	(void)setlocale(LC_ALL, "");
+	setlocale(LC_ALL, "");
 
 	/*

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201508232142.t7NLgSXX033227>