Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 17 Dec 1996 02:24:34 +0100
From:      Wolfram Schneider <wosch@cs.tu-berlin.de>
To:        current@freebsd.org
Cc:        wpaul@frebsd.org.cs.tu-berlin.de
Subject:   group(5) limits
Message-ID:  <199612170124.CAA02231@campa.panke.de>

next in thread | raw e-mail | index | archive | help

The current limit is 200 members per group or maximum 1024 character per
line. I changed getgrent(3) to use dynamic allocated buffers
instead static buffers. No member or line length limit anymore -
now 500 members or 5000 members are possible.

For security group lines longer than 256K will be ignored. 
256K should be enough for ~50000 users.

--- 1.1	1996/12/13 23:57:21
+++ getgrent.c	1996/12/17 00:55:23
@@ -55,10 +55,19 @@
 static int _nextypgroup(struct group *);
 #endif
 
-#define	MAXGRP		200
-static char *members[MAXGRP];
-#define	MAXLINELENGTH	1024
-static char line[MAXLINELENGTH];
+
+#define	MAXGRP		64
+#define	MAXLINELENGTH	256
+
+static char **members; 		/* list of group members */
+static int maxgrp;              /* current length of **mebers */
+static char *line;		/* temp buffer for group line */
+static int maxlinelength;       /* current length of *line */
+
+/* < 0 disable check for maximum line length */
+/* 256K is enough for 64,000 uids */
+#define MAXLINELENGTHLIMIT	(256*1024)
+
 
 struct group *
 getgrent()
@@ -176,6 +185,20 @@
 		rewind(_gr_fp);
 	}
 #endif
+
+	if (maxlinelength == 0) {
+		if ((line = (char *)malloc(sizeof(char) * 
+					   MAXLINELENGTH)) == NULL)
+			return(0);
+		maxlinelength += MAXLINELENGTH;
+	}
+	if (maxgrp == 0) {
+		if ((members = (char **)malloc(sizeof(char **) * 
+					       MAXGRP)) == NULL)
+			return(0);
+		maxgrp += MAXGRP;
+	}
+
 	return 1;
 }
 
@@ -207,6 +230,9 @@
 	if (_gr_fp) {
 		(void)fclose(_gr_fp);
 		_gr_fp = NULL;
+		free(line);
+		free(members);
+		maxlinelength = maxgrp = 0;
 	}
 }
 
@@ -217,24 +243,53 @@
 {
 	register char *cp, **m;
 	char *bp;
+
+
 #ifdef YP
 	int _ypfound;
-#endif;
+#endif
 	for (;;) {
 #ifdef YP
 		_ypfound = 0;
 #endif
-		if (!fgets(line, sizeof(line), _gr_fp))
+		if (fgets(line, maxlinelength, _gr_fp) == NULL)
 			return(0);
-		bp = line;
-		/* skip lines that are too big */
+
 		if (!index(line, '\n')) {
-			int ch;
+			do {
+				if (feof(_gr_fp))
+					return(0);
+			
+				/* don't allocate infinite memory */
+				if (MAXLINELENGTHLIMIT > 0 && 
+				    maxlinelength >= MAXLINELENGTHLIMIT)
+					return(0);
 
-			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
-				;
-			continue;
+				if ((line = (char *)realloc(line, 
+				     sizeof(char) * 
+				     (maxlinelength + MAXLINELENGTH))) == NULL)
+					return(0);
+			
+				if (fgets(line + maxlinelength - 1, 
+					  MAXLINELENGTH + 1, _gr_fp) == NULL)
+					return(0);
+
+				maxlinelength += MAXLINELENGTH;
+			} while (!index(line + maxlinelength - 
+				       MAXLINELENGTH - 1, '\n'));
 		}
+
+#if 1
+		/* 
+		 * Ignore comments. A comment is a line which start 
+		 * with character `#'. 
+		 */
+		if (*line == '#') 
+			continue;
+#endif
+
+		bp = line;
+
 		if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL)
 			break;
 #ifdef YP
@@ -290,9 +345,11 @@
 			break;
 #endif
 		if (!(cp = strsep(&bp, ":\n")))
+#ifdef YP
 			if (_ypfound)
 				return(1);
 			else
+#endif /* YP */
 				continue;
 #ifdef YP
 		/*
@@ -318,9 +375,16 @@
 		bp = cp;
 		cp = NULL;
 #endif
-		for (m = _gr_group.gr_mem = members;; bp++) {
-			if (m == &members[MAXGRP - 1])
-				break;
+		for (m = members; ; bp++) {
+			if (m == (members + maxgrp - 1)) {
+				if ((members = (char **)
+				     realloc(members, 
+					     sizeof(char **) * 
+					     (maxgrp + MAXGRP))) == NULL)
+					return(0);
+				m = members + maxgrp - 1;
+				maxgrp += MAXGRP;
+			}
 			if (*bp == ',') {
 				if (cp) {
 					*bp = '\0';
@@ -331,11 +395,13 @@
 				if (cp) {
 					*bp = '\0';
 					*m++ = cp;
-			}
+				}
 				break;
 			} else if (cp == NULL)
 				cp = bp;
+			
 		}
+		_gr_group.gr_mem = members;
 		*m = NULL;
 		return(1);
 	}
@@ -368,9 +434,13 @@
 	if ((s = result) == NULL) return 0;
 	cp = 0;
 
-	for (m = _gr_group.gr_mem = members; /**/; s++) {
-		if (m == &members[MAXGRP - 1]) {
-			break;
+	for (m = members; ; s++) {
+		if (m == members + maxgrp - 1) {
+			if ((members = (char **)realloc(members, 
+			     sizeof(char **) * (maxgrp + MAXGRP))) == NULL)
+				return(0);
+			m = members + maxgrp - 1;
+			maxgrp += MAXGRP;
 		}
 		if (*s == ',') {
 			if (cp) {
@@ -388,6 +458,7 @@
 			cp = s;
 		}
 	}
+	_gr_group.gr_mem = members;
 	*m = NULL;
 
 	return 1;



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