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

Mike Frysinger <[email protected]> Sat, 10 Jun 2006 18:24:23 -0400
Newsgroups gmane.linux.pld.shadow.general
Organization wh0rd.org
Message-ID <[email protected]>
ok, sorry for the delay

attached is a patch that combines my and Johannes' work

this new one has a bit more logic surrounding the nflg variable:
 -1 -> default to yes from USERGROUPS_ENAB in login.defs
 0 -> disabled
 1 -> enabled via cmdline

the reason for this is to handle the case where USERGROUPS_ENAB is enabled by 
default and the admin runs `useradd -g <gid> <user>` ... if we just set nflg 
to USERGROUPS_ENAB, we'd trigger the mutual exclusion error message 
with -n/-g
-mike
signature.asc (application/pgp-signature, 827 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iQIVAwUARItGmEFjO5/oN/WBAQLeWw/8Cvj1SSSEA0qSL3AjDol7lZ3Vr7rd1TGv
DI6L+sS4px13tq59y+x77TW/DXQjnNjmlTTKsSfDaOdy2sA9+yDmac2n12v4Bmjz
WPrMuIrR54PUqXcEtQhjtXe2mY++BL9t5xliUD5yqwFQVlBVcILtUwe2R7nO0Jce
ddjrPyFNe/yVazTPbdETgVLxbZYKHLzJOwLlnx8YQgoGjs5PkxJI4EdP+J2/UkJG
dgGr44f3qIKukeydznDBeSZelyUD5w4ugONZOl2XwMVt/3zOd974MX0J76/2s7Us
TL2z8549Mi42aC7O3j7TaOlr7mSupvNK5jYDAJRaJpT7LpB+acOzifVotOuyDa3o
yQCtWrFqw6a0+WbgrzcsWw7wHDPI8il1fRfYpz656EAxN0aFGsrVJ3THkku3jLJT
lhWtK6H6ksKv1WkrZyc1b8qhxH0VF4PBNikgHRuQWxzC26wHpoGEpKojkLvPhU/X
iOWNTZJsVEp100GAg5F1CNy9efVlAIKKamOozt+kgWswl3KIsW7RyDumgOhGMPFR
FCVEW3oGfnq1aAJBqVmNuiat9vPMAVh/JYNo+5Y0Xzq3j+1g5HyaVRhr8AIUqhbB
MBxvA2vBZWnlBCKwd2XvFRNyeUtpJR/R9wuldEfRMeIktB54chhWPV9o3Q7GUb5D
lIq7YDB84T4=
=k1gP
-----END PGP SIGNATURE-----
shadow-fix-useradd-usergroups.patch (text/x-diff, 3.9 KB)
Index: src/useradd.c
===================================================================
RCS file: /cvsroot/shadow/src/useradd.c,v
retrieving revision 1.96
diff -u -p -r1.96 useradd.c
--- src/useradd.c	30 May 2006 18:28:45 -0000	1.96
+++ src/useradd.c	10 Jun 2006 22:13:32 -0000
@@ -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,12 @@ static void get_defaults (void)
 	const struct group *grp;
 
 	/*
+	 * Pull relevant settings from login.defs first.
+	 */
+	if (getdef_bool ("USERGROUPS_ENAB"))
+		nflg = -1;
+
+	/*
 	 * Open the defaults file for reading.
 	 */
 
@@ -628,6 +634,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 +1017,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 +1025,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 +1165,9 @@ static void process_flags (int argc, cha
 			case 'm':
 				mflg++;
 				break;
+			case 'n':
+				nflg = 1;
+				break;
 			case 'o':
 				oflg++;
 				break;
@@ -1203,6 +1215,16 @@ static void process_flags (int argc, cha
 		usage ();
 
 	/*
+	 * Using --gid and --user-group doesn't make sense.
+	 */
+	if (nflg == -1 && gflg)
+		nflg = 0;
+	if (nflg && gflg) {
+		fprintf (stderr, _("%s: options -g and -n conflict\n"), Prog);
+		exit (E_BAD_ARG);
+	}
+
+	/*
 	 * Either -D or username is required. Defaults can be set with -D
 	 * for the -b, -e, -f, -g, -s options only.
 	 */
@@ -1725,7 +1747,7 @@ int main (int argc, char **argv)
 	 * to that group, use useradd -g username username.
 	 * --bero
 	 */
-	if (!gflg) {
+	if (nflg) {
 		if (getgrnam (user_name)) {
 			fprintf (stderr,
 				 _
@@ -1759,7 +1781,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) {
 		find_new_gid ();
 		grp_add ();
 	}
Index: man/useradd.8.xml
===================================================================
RCS file: /cvsroot/shadow/man/useradd.8.xml,v
retrieving revision 1.34
diff -u -p -r1.34 useradd.8.xml
--- man/useradd.8.xml	20 May 2006 12:11:38 -0000	1.34
+++ man/useradd.8.xml	10 Jun 2006 22:17:13 -0000
@@ -204,6 +204,19 @@
       </varlistentry>
       <varlistentry>
 	<term>
+	  <option>-n</option>, <option>--user-group</option>
+	</term>
+	<listitem>
+	  <para>
+	    Create a new group with the same name as the new user.
+	  </para>
+	  <para>
+	    See the <replaceable>USERGROUPS_ENAB</replaceable> login.defs option for more details.
+	  </para>
+	</listitem>
+      </varlistentry>
+      <varlistentry>
+	<term>
 	  <option>-o</option>, <option>--non-unique</option>
 	</term>
 	<listitem>