Re: [patch] document useradd behavior when run without the -g option

Mike Frysinger <[email protected]> Fri, 7 Apr 2006 22:20:44 -0400
Newsgroups gmane.linux.pld.shadow.general
Organization wh0rd.org
Message-ID <[email protected]>
On Friday 07 April 2006 20:27, Mike Frysinger wrote:
> a user submitted a bug that useradd behaved "incorrectly" when run with the
> -G option but not the -g option

actually, after going through the code, it seems that the user is correct in 
that useradd is behaving wrongly :)

the current behavior of useradd ignores the GROUP setting 
in /etc/defaults/useradd when the -g option is not provided.  the code also 
never actually sets the variable 'nflg' so the net result is a group is 
usually added for the user.

the attached patch actually utilizes the 'nflg' variable as intended 
(according to the comments for the decl) ... it adds a new -n option to 
useradd so that the admin can choose to enable the usergroup feature and 
useradd now defaults the nflg setting to whatever the admin has 
USERGROUPS_ENAB set in login.defs file.  finally, it fixes the logic for when 
a group needs to be added automatically by useradd
-mike
shadow-fix-useradd-usergroups.patch (text/x-diff, 2.5 KB)
2006-04-07  Mike Frysinger  <[email protected]>

	* src/useradd.c: Actually utilize the nflg by adding an -n option for the
	user and by setting to USERGROUPS_ENAB from login.defs.  Also fix the logic
	for when the user group actually needs to be added.

--- src/useradd.c
+++ src/useradd.c
@@ -114,7 +114,7 @@ static int do_grp_update = 0;	/* group f
 static char *Prog;
 
 static int
- bflg = 0,			/* new default root of home directory */
+    bflg = 0,			/* new default root of home directory */
     cflg = 0,			/* comment (GECOS) field for new account */
     dflg = 0,			/* home directory for new account */
     Dflg = 0,			/* set/show new user default values */
@@ -253,6 +253,11 @@ static void get_defaults (void)
 	const struct group *grp;
 
 	/*
+	 * Pull relevant settings from login.defs first.
+	 */
+	nflg = getdef_bool ("USERGROUPS_ENAB");
+
+	/*
 	 * Open the defaults file for reading.
 	 */
 
@@ -628,6 +633,8 @@ static void usage (void)
 			   "  -K, --key KEY=VALUE		overrides /etc/login.defs defaults\n"
 			   "  -m, --create-home		create home directory for the new user\n"
 			   "				account\n"
+			   "  -n, --user-group		create a new group with the same name as the\n"
+			   "				new user\n"
 			   "  -o, --non-unique		allow create user with duplicate\n"
 			   "				(non-unique) UID\n"
 			   "  -p, --password PASSWORD	use encrypted password for the new user\n"
@@ -1009,6 +1016,7 @@ static void process_flags (int argc, cha
 			{"skel", required_argument, NULL, 'k'},
 			{"key", required_argument, NULL, 'K'},
 			{"create-home", no_argument, NULL, 'm'},
+			{"user-group", no_argument, NULL, 'n'},
 			{"non-unique", no_argument, NULL, 'o'},
 			{"password", required_argument, NULL, 'p'},
 			{"shell", required_argument, NULL, 's'},
@@ -1016,7 +1024,7 @@ static void process_flags (int argc, cha
 			{NULL, 0, NULL, '\0'}
 		};
 		while ((c =
-			getopt_long (argc, argv, "b:c:d:De:f:g:G:k:K:mMop:s:u:",
+			getopt_long (argc, argv, "b:c:d:De:f:g:G:k:K:mMnop:s:u:",
 				     long_options, NULL)) != -1) {
 			switch (c) {
 			case 'b':
@@ -1156,6 +1164,9 @@ static void process_flags (int argc, cha
 			case 'm':
 				mflg++;
 				break;
+			case 'n':
+				nflg++;
+				break;
 			case 'o':
 				oflg++;
 				break;
@@ -1767,7 +1778,7 @@ int main (int argc, char **argv)
 
 	/* 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)) {
+	if (nflg && !gflg) {
 		find_new_gid ();
 		grp_add ();
 	}