[Pkg-shadow-commits] r1757 - in upstream/trunk: . src

nekral-guest at alioth.debian.org nekral-guest at alioth.debian.org
Sun Feb 3 16:56:23 UTC 2008


Author: nekral-guest
Date: 2008-02-03 16:56:23 +0000 (Sun, 03 Feb 2008)
New Revision: 1757

Modified:
   upstream/trunk/ChangeLog
   upstream/trunk/src/groupadd.c
   upstream/trunk/src/useradd.c
Log:
Use the find_new_uid() and find_new_gid() from the library instead of the
local functions.


Modified: upstream/trunk/ChangeLog
===================================================================
--- upstream/trunk/ChangeLog	2008-02-03 16:55:37 UTC (rev 1756)
+++ upstream/trunk/ChangeLog	2008-02-03 16:56:23 UTC (rev 1757)
@@ -20,6 +20,8 @@
 	translatable strings.
 	* libmisc/Makefile.am: Add libmisc/find_new_ids.c to the sources
 	of the libmisc library.
+	* src/useradd.c, src/groupadd.c: Use the find_new_uid() and
+	find_new_gid() from the library instead of the local functions.
 
 2008-02-02  Nicolas François  <nicolas.francois at centraliens.net>
 

Modified: upstream/trunk/src/groupadd.c
===================================================================
--- upstream/trunk/src/groupadd.c	2008-02-03 16:55:37 UTC (rev 1756)
+++ upstream/trunk/src/groupadd.c	2008-02-03 16:56:23 UTC (rev 1757)
@@ -90,7 +90,6 @@
 static void new_sgent (struct sgrp *sgent);
 #endif
 static void grp_update (void);
-static void find_new_gid (void);
 static void check_new_name (void);
 static void close_files (void);
 static void open_files (void);
@@ -208,55 +207,6 @@
 }
 
 /*
- * find_new_gid - find the next available GID
- *
- *	find_new_gid() locates the next highest unused GID in the group
- *	file.
- */
-static void find_new_gid (void)
-{
-	const struct group *grp;
-	gid_t gid_min, gid_max;
-
-	/*
-	 * It doesn't make sense to use find_new_gid(),
-	 * if a GID is specified via "-g" option.
-	 */
-	assert (!gflg);
-
-	gid_min = getdef_unum ("GID_MIN", 1000);
-	gid_max = getdef_unum ("GID_MAX", 60000);
-
-	/*
-	 * Start with the lowest GID.
-	 */
-	group_id = gid_min;
-
-	/*
-	 * Search the entire group file, looking for the largest unused
-	 * value.
-	 */
-	setgrent ();
-	while ((grp = getgrent ())) {
-		if ((grp->gr_gid >= group_id) && (grp->gr_gid <= gid_max)) {
-			group_id = grp->gr_gid + 1;
-		}
-	}
-	if (group_id == (gid_max + 1)) {
-		for (group_id = gid_min; group_id < gid_max; group_id++) {
-			/* local, no need for xgetgrgid */
-			if (!getgrgid (group_id)) {
-				break;
-			}
-		}
-		if (group_id == gid_max) {
-			fprintf (stderr, _("%s: can't get unique GID\n"), Prog);
-			fail_exit (E_GID_IN_USE);
-		}
-	}
-}
-
-/*
  * check_new_name - check the new name for validity
  *
  *	check_new_name() insures that the new name doesn't contain any
@@ -606,7 +556,10 @@
 	open_files ();
 
 	if (!gflg) {
-		find_new_gid ();
+		if (find_new_gid (0, &group_id, NULL) < 0) {
+			fprintf (stderr, _("%s: can't create group\n"), Prog);
+			fail_exit (E_GID_IN_USE);
+		}
 	}
 
 	grp_update ();

Modified: upstream/trunk/src/useradd.c
===================================================================
--- upstream/trunk/src/useradd.c	2008-02-03 16:55:37 UTC (rev 1756)
+++ upstream/trunk/src/useradd.c	2008-02-03 16:56:23 UTC (rev 1757)
@@ -170,8 +170,6 @@
 static long scale_age (long);
 static void new_spent (struct spwd *);
 static void grp_update (void);
-static void find_new_uid (void);
-static void find_new_gid (void);
 
 static void process_flags (int argc, char **argv);
 static void close_files (void);
@@ -812,105 +810,6 @@
 }
 
 /*
- * find_new_uid - find the next available UID
- *
- *	find_new_uid() locates the next highest unused UID in the password
- *	file.
- */
-static void find_new_uid (void)
-{
-	const struct passwd *pwd;
-	uid_t uid_min, uid_max;
-
-	/*
-	 * It doesn't make sense to use find_new_uid(),
-	 * if an UID is specified via "-u" option.
-	 */
-	assert (!uflg);
-
-	uid_min = getdef_unum ("UID_MIN", 1000);
-	uid_max = getdef_unum ("UID_MAX", 60000);
-
-	user_id = uid_min;
-
-	/*
-	 * Search the entire password file,
-	 * looking for the largest unused value.
-	 */
-	setpwent ();
-	while ((pwd = getpwent ()) != NULL) {
-		if ((pwd->pw_uid >= user_id) && (pwd->pw_uid <= uid_max)) {
-			user_id = pwd->pw_uid + 1;
-		}
-	}
-
-	/*
-	 * If a user with UID equal to UID_MAX exists, the above algorithm
-	 * will give us UID_MAX+1 even if not unique. Search for the first
-	 * free UID starting with UID_MIN (it's O(n*n) but can be avoided
-	 * by not having users with UID equal to UID_MAX).  --marekm
-	 */
-	if (user_id == uid_max + 1) {
-		for (user_id = uid_min; user_id < uid_max; user_id++) {
-			/* local, no need for xgetpwuid */
-			if (!getpwuid (user_id))
-				break;
-		}
-		if (user_id == uid_max) {
-			fprintf (stderr, _("%s: can't get unique UID (no more available UIDs)\n"), Prog);
-			fail_exit (E_UID_IN_USE);
-		}
-	}
-}
-
-/*
- * find_new_gid - find the next available GID
- *
- *	find_new_gid() locates the next highest unused GID in the group
- *	file
- */
-static void find_new_gid (void)
-{
-	const struct group *grp;
-	gid_t gid_min, gid_max;
-
-	/*
-	 * It doesn't make sense to use find_new_gid(),
-	 * if a group is specified via "-g" option.
-	 */
-	assert (!gflg);
-
-	gid_min = getdef_num ("GID_MIN", 500);
-	gid_max = getdef_num ("GID_MAX", 60000);
-
-	user_gid = gid_min;
-
-	/*
-	 * Search the entire group file,
-	 * looking for the largest unused value.
-	 */
-	setgrent ();
-	while ((grp = getgrent ())) {
-		if ((grp->gr_gid >= user_gid) && (grp->gr_gid <= gid_max)) {
-			user_gid = grp->gr_gid + 1;
-		}
-	}
-	if (user_gid == gid_max + 1) {
-		for (user_gid = gid_min; user_gid < gid_max; user_gid++) {
-			/* local, no need for xgetgrgid */
-			if (!getgrgid (user_gid))
-				break;
-		}
-		if (user_gid == gid_max) {
-			fprintf (stderr,
-				 _("%s: can't get unique GID (no more available GIDs)\n"),
-				 Prog);
-			fail_exit (4);
-		}
-	}
-}
-
-/*
  * process_flags - perform command line argument setting
  *
  *	process_flags() interprets the command line arguments and sets
@@ -1699,9 +1598,12 @@
 		/* first, seek for a valid uid to use for this user.
 		 * We do this because later we can use the uid we found as
 		 * gid too ... --gafton */
-		if (!uflg)
-			find_new_uid ();
-		else {
+		if (!uflg) {
+			if (find_new_uid (0, &user_id, NULL) < 0) {
+				fprintf (stderr, _("%s: can't create user\n"), Prog);
+				fail_exit (E_UID_IN_USE);
+			}
+		} else {
 			if (getpwuid (user_id) != NULL) {
 				fprintf (stderr, _("%s: UID %u is not unique\n"), Prog, (unsigned int) user_id);
 #ifdef WITH_AUDIT
@@ -1715,7 +1617,12 @@
 	/* do we have to add a group for that user? This is why we need to
 	 * open the group files in the open_files() function  --gafton */
 	if (!(nflg || gflg)) {
-		find_new_gid ();
+		if (find_new_gid (0, &user_gid, &user_id) < 0) {
+			fprintf (stderr,
+				 _("%s: can't create group\n"),
+				 Prog);
+			fail_exit (4);
+		}
 		grp_add ();
 	}
 




More information about the Pkg-shadow-commits mailing list