Subject: -r option for groupadd
To: None <tech-userlevel@netbsd.org>
From: Julio M. Merino Vidal <jmmv84@gmail.com>
List: tech-userlevel
Date: 01/30/2005 14:04:46
--=-74wrp1bnEapKhSKc/z/k
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi all,

a long time ago, I filed bin/22791 requesting a -r option in the
groupadd command.  The reason is that I'd like to be able to add
some options in pkgsrc to restrict the installation scripts to always
create users and groups in a specific range (say, I want all pkgsrc
stuff between 500 and 600).

The PR has recently got a patch attached, created by URA Hiroshi.  It is
quite simple, but works fine.  I've attached a cleaned up version of it
(basically some stuff in the manual pages).

Any objections in adding this?

Thanks,

-- 
Julio M. Merino Vidal <jmmv84@gmail.com>
http://www.livejournal.com/users/jmmv/
The NetBSD Project - http://www.NetBSD.org/

--=-74wrp1bnEapKhSKc/z/k
Content-Disposition: attachment; filename=22791.patch
Content-Type: text/x-patch; name=22791.patch; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Index: group.8
===================================================================
RCS file: /cvsroot/src/usr.sbin/user/group.8,v
retrieving revision 1.10
diff -u -r1.10 group.8
--- group.8	25 Feb 2003 10:36:21 -0000	1.10
+++ group.8	30 Jan 2005 12:57:19 -0000
@@ -31,7 +31,7 @@
 .\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
 .\"
-.Dd November 30, 1999
+.Dd January 30, 2005
 .Dt GROUP 8
 .Os
 .Sh NAME
@@ -42,6 +42,7 @@
 .Cm add
 .Op Fl ov
 .Op Fl g Ar gid
+.Op Fl r Ar low Ns Li .. Ns Ar high
 .Ar group
 .Nm
 .Cm del
Index: groupadd.8
===================================================================
RCS file: /cvsroot/src/usr.sbin/user/groupadd.8,v
retrieving revision 1.9
diff -u -r1.9 groupadd.8
--- groupadd.8	14 Feb 2003 16:11:37 -0000	1.9
+++ groupadd.8	30 Jan 2005 12:57:19 -0000
@@ -31,7 +31,7 @@
 .\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
 .\"
-.Dd November 30, 1999
+.Dd January 30, 2005
 .Dt GROUPADD 8
 .Os
 .Sh NAME
@@ -41,6 +41,7 @@
 .Nm
 .Op Fl ov
 .Op Fl g Ar gid
+.Op Fl r Ar low Ns Li .. Ns Ar high
 .Ar group
 .Sh DESCRIPTION
 The
@@ -52,6 +53,10 @@
 gives the numeric group identifier to be used for the new group.
 .It Fl o
 allow the new group to have a gid which is already in use for another group.
+.It Fl r Ar low Ns Li .. Ns Ar high
+sets the low and high bounds of a gid range for new groups.
+A new group can only be created if there are gids which can be assigned
+inside the range.
 .It Fl v
 enables verbose mode - explain the commands as they are executed.
 .El
Index: user.c
===================================================================
RCS file: /cvsroot/src/usr.sbin/user/user.c,v
retrieving revision 1.76
diff -u -r1.76 user.c
--- user.c	2 Jul 2004 12:01:00 -0000	1.76
+++ user.c	30 Jan 2005 12:57:20 -0000
@@ -1521,8 +1521,8 @@
 		(void) fprintf(stderr, "usage: %s [-e] [-v] user\n", prog);
 #endif
 	} else if (strcmp(prog, "groupadd") == 0) {
-		(void) fprintf(stderr, "usage: %s [-g gid] [-o] [-v] group\n",
-		    prog);
+		(void) fprintf(stderr, "usage: %s [-g gid] [-o]"
+		    " [-r lowgid..highgid] [-v] group\n", prog);
 	} else if (strcmp(prog, "groupdel") == 0) {
 		(void) fprintf(stderr, "usage: %s [-v] group\n", prog);
 	} else if (strcmp(prog, "groupmod") == 0) {
@@ -1891,7 +1891,7 @@
 }
 
 #ifdef EXTENSIONS
-#define GROUP_ADD_OPT_EXTENSIONS	"v"
+#define GROUP_ADD_OPT_EXTENSIONS	"r:v"
 #else
 #define GROUP_ADD_OPT_EXTENSIONS	
 #endif
@@ -1903,9 +1903,13 @@
 	int	dupgid;
 	int	gid;
 	int	c;
+	int	lowgid;
+	int	highgid;
 
 	gid = -1;
 	dupgid = 0;
+	lowgid = LowGid;
+	highgid = HighGid;
 	while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
 		switch(c) {
 		case 'g':
@@ -1918,6 +1922,11 @@
 			dupgid = 1;
 			break;
 #ifdef EXTENSIONS
+		case 'r':
+			if (sscanf(optarg, "%d..%d", &lowgid, &highgid) != 2) {
+				errx(EXIT_FAILURE, "Bad range `%s`", optarg);
+			}
+			break;
 		case 'v':
 			verbose = 1;
 			break;
@@ -1933,7 +1942,7 @@
 		usermgmt_usage("groupadd");
 	}
 	checkeuid();
-	if (gid < 0 && !getnextgid(&gid, LowGid, HighGid)) {
+	if (gid < 0 && !getnextgid(&gid, lowgid, highgid)) {
 		err(EXIT_FAILURE, "can't add group: can't get next gid");
 	}
 	if (!dupgid && getgrgid((gid_t) gid) != NULL) {

--=-74wrp1bnEapKhSKc/z/k--