Blame SOURCES/shadow-4.6-usermod-allow-all-group-types.patch

31f24e
From e481437ab9ebe9a8bf8fbaabe986d42b2f765991 Mon Sep 17 00:00:00 2001
31f24e
From: Iker Pedrosa <ipedrosa@redhat.com>
31f24e
Date: Tue, 3 Aug 2021 08:57:20 +0200
31f24e
Subject: [PATCH] usermod: allow all group types with -G option
31f24e
31f24e
The only way of removing a group from the supplementary list is to use
31f24e
-G option, and list all groups that the user is a member of except for
31f24e
the one that wants to be removed. The problem lies when there's a user
31f24e
that contains both local and remote groups, and the group to be removed
31f24e
is a local one. As we need to include the remote group with -G option
31f24e
the command will fail.
31f24e
31f24e
This reverts commit 140510de9de4771feb3af1d859c09604043a4c9b. This way,
31f24e
it would be possible to remove the remote groups from the supplementary
31f24e
list.
31f24e
31f24e
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1967641
31f24e
Resolves: https://github.com/shadow-maint/shadow/issues/338
31f24e
31f24e
Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
31f24e
---
31f24e
 src/usermod.c | 220 ++++++++++++++++++--------------------------------
31f24e
 1 file changed, 77 insertions(+), 143 deletions(-)
31f24e
31f24e
diff --git a/src/usermod.c b/src/usermod.c
31f24e
index 03bb9b9d..a0c03afa 100644
31f24e
--- a/src/usermod.c
31f24e
+++ b/src/usermod.c
31f24e
@@ -187,7 +187,6 @@ static bool sub_gid_locked = false;
31f24e
 static void date_to_str (/*@unique@*//*@out@*/char *buf, size_t maxsize,
31f24e
                          long int date);
31f24e
 static int get_groups (char *);
31f24e
-static struct group * get_local_group (char * grp_name);
31f24e
 static /*@noreturn@*/void usage (int status);
31f24e
 static void new_pwent (struct passwd *);
31f24e
 static void new_spent (struct spwd *);
31f24e
@@ -201,9 +200,7 @@ static void grp_update (void);
31f24e
 
31f24e
 static void process_flags (int, char **);
31f24e
 static void close_files (void);
31f24e
-static void close_group_files (void);
31f24e
 static void open_files (void);
31f24e
-static void open_group_files (void);
31f24e
 static void usr_update (void);
31f24e
 static void move_home (void);
31f24e
 static void update_lastlog (void);
31f24e
@@ -260,11 +257,6 @@ static int get_groups (char *list)
31f24e
 		return 0;
31f24e
 	}
31f24e
 
31f24e
-	/*
31f24e
-	 * Open the group files
31f24e
-	 */
31f24e
-	open_group_files ();
31f24e
-
31f24e
 	/*
31f24e
 	 * So long as there is some data to be converted, strip off each
31f24e
 	 * name and look it up. A mix of numerical and string values for
31f24e
@@ -284,7 +276,7 @@ static int get_groups (char *list)
31f24e
 		 * Names starting with digits are treated as numerical GID
31f24e
 		 * values, otherwise the string is looked up as is.
31f24e
 		 */
31f24e
-		grp = get_local_group (list);
31f24e
+		grp = prefix_getgr_nam_gid (list);
31f24e
 
31f24e
 		/*
31f24e
 		 * There must be a match, either by GID value or by
31f24e
@@ -334,8 +326,6 @@ static int get_groups (char *list)
31f24e
 		gr_free ((struct group *)grp);
31f24e
 	} while (NULL != list);
31f24e
 
31f24e
-	close_group_files ();
31f24e
-
31f24e
 	user_groups[ngroups] = (char *) 0;
31f24e
 
31f24e
 	/*
31f24e
@@ -348,44 +338,6 @@ static int get_groups (char *list)
31f24e
 	return 0;
31f24e
 }
31f24e
 
31f24e
-/*
31f24e
- * get_local_group - checks if a given group name exists locally
31f24e
- *
31f24e
- *	get_local_group() checks if a given group name exists locally.
31f24e
- *	If the name exists the group information is returned, otherwise NULL is
31f24e
- *	returned.
31f24e
- */
31f24e
-static struct group * get_local_group(char * grp_name)
31f24e
-{
31f24e
-	const struct group *grp;
31f24e
-	struct group *result_grp = NULL;
31f24e
-	long long int gid;
31f24e
-	char *endptr;
31f24e
-
31f24e
-	gid = strtoll (grp_name, &endptr, 10);
31f24e
-	if (   ('\0' != *grp_name)
31f24e
-		&& ('\0' == *endptr)
31f24e
-		&& (ERANGE != errno)
31f24e
-		&& (gid == (gid_t)gid)) {
31f24e
-		grp = gr_locate_gid ((gid_t) gid);
31f24e
-	}
31f24e
-	else {
31f24e
-		grp = gr_locate(grp_name);
31f24e
-	}
31f24e
-
31f24e
-	if (grp != NULL) {
31f24e
-		result_grp = __gr_dup (grp);
31f24e
-		if (NULL == result_grp) {
31f24e
-			fprintf (stderr,
31f24e
-					_("%s: Out of memory. Cannot find group '%s'.\n"),
31f24e
-					Prog, grp_name);
31f24e
-			fail_exit (E_GRP_UPDATE);
31f24e
-		}
31f24e
-	}
31f24e
-
31f24e
-	return result_grp;
31f24e
-}
31f24e
-
31f24e
 #ifdef ENABLE_SUBIDS
31f24e
 struct ulong_range
31f24e
 {
31f24e
@@ -1523,7 +1475,50 @@ static void close_files (void)
31f24e
 	}
31f24e
 
31f24e
 	if (Gflg || lflg) {
31f24e
-		close_group_files ();
31f24e
+		if (gr_close () == 0) {
31f24e
+			fprintf (stderr,
31f24e
+			         _("%s: failure while writing changes to %s\n"),
31f24e
+			         Prog, gr_dbname ());
31f24e
+			SYSLOG ((LOG_ERR,
31f24e
+			         "failure while writing changes to %s",
31f24e
+			         gr_dbname ()));
31f24e
+			fail_exit (E_GRP_UPDATE);
31f24e
+		}
31f24e
+#ifdef SHADOWGRP
31f24e
+		if (is_shadow_grp) {
31f24e
+			if (sgr_close () == 0) {
31f24e
+				fprintf (stderr,
31f24e
+				         _("%s: failure while writing changes to %s\n"),
31f24e
+				         Prog, sgr_dbname ());
31f24e
+				SYSLOG ((LOG_ERR,
31f24e
+				         "failure while writing changes to %s",
31f24e
+				         sgr_dbname ()));
31f24e
+				fail_exit (E_GRP_UPDATE);
31f24e
+			}
31f24e
+		}
31f24e
+#endif
31f24e
+#ifdef SHADOWGRP
31f24e
+		if (is_shadow_grp) {
31f24e
+			if (sgr_unlock () == 0) {
31f24e
+				fprintf (stderr,
31f24e
+				         _("%s: failed to unlock %s\n"),
31f24e
+				         Prog, sgr_dbname ());
31f24e
+				SYSLOG ((LOG_ERR,
31f24e
+				         "failed to unlock %s",
31f24e
+				         sgr_dbname ()));
31f24e
+				/* continue */
31f24e
+			}
31f24e
+		}
31f24e
+#endif
31f24e
+		if (gr_unlock () == 0) {
31f24e
+			fprintf (stderr,
31f24e
+			         _("%s: failed to unlock %s\n"),
31f24e
+			         Prog, gr_dbname ());
31f24e
+			SYSLOG ((LOG_ERR,
31f24e
+			         "failed to unlock %s",
31f24e
+			         gr_dbname ()));
31f24e
+			/* continue */
31f24e
+		}
31f24e
 	}
31f24e
 
31f24e
 	if (is_shadow_pwd) {
31f24e
@@ -1592,60 +1587,6 @@ static void close_files (void)
31f24e
 #endif
31f24e
 }
31f24e
 
31f24e
-/*
31f24e
- * close_group_files - close all of the files that were opened
31f24e
- *
31f24e
- *	close_group_files() closes all of the files that were opened related
31f24e
- *  with groups. This causes any modified entries to be written out.
31f24e
- */
31f24e
-static void close_group_files (void)
31f24e
-{
31f24e
-	if (gr_close () == 0) {
31f24e
-		fprintf (stderr,
31f24e
-					_("%s: failure while writing changes to %s\n"),
31f24e
-					Prog, gr_dbname ());
31f24e
-		SYSLOG ((LOG_ERR,
31f24e
-					"failure while writing changes to %s",
31f24e
-					gr_dbname ()));
31f24e
-		fail_exit (E_GRP_UPDATE);
31f24e
-	}
31f24e
-#ifdef SHADOWGRP
31f24e
-	if (is_shadow_grp) {
31f24e
-		if (sgr_close () == 0) {
31f24e
-			fprintf (stderr,
31f24e
-						_("%s: failure while writing changes to %s\n"),
31f24e
-						Prog, sgr_dbname ());
31f24e
-			SYSLOG ((LOG_ERR,
31f24e
-						"failure while writing changes to %s",
31f24e
-						sgr_dbname ()));
31f24e
-			fail_exit (E_GRP_UPDATE);
31f24e
-		}
31f24e
-	}
31f24e
-#endif
31f24e
-#ifdef SHADOWGRP
31f24e
-	if (is_shadow_grp) {
31f24e
-		if (sgr_unlock () == 0) {
31f24e
-			fprintf (stderr,
31f24e
-						_("%s: failed to unlock %s\n"),
31f24e
-						Prog, sgr_dbname ());
31f24e
-			SYSLOG ((LOG_ERR,
31f24e
-						"failed to unlock %s",
31f24e
-						sgr_dbname ()));
31f24e
-			/* continue */
31f24e
-		}
31f24e
-	}
31f24e
-#endif
31f24e
-	if (gr_unlock () == 0) {
31f24e
-		fprintf (stderr,
31f24e
-					_("%s: failed to unlock %s\n"),
31f24e
-					Prog, gr_dbname ());
31f24e
-		SYSLOG ((LOG_ERR,
31f24e
-					"failed to unlock %s",
31f24e
-					gr_dbname ()));
31f24e
-		/* continue */
31f24e
-	}
31f24e
-}
31f24e
-
31f24e
 /*
31f24e
  * open_files - lock and open the password files
31f24e
  *
31f24e
@@ -1681,7 +1622,38 @@ static void open_files (void)
31f24e
 	}
31f24e
 
31f24e
 	if (Gflg || lflg) {
31f24e
-		open_group_files ();
31f24e
+		/*
31f24e
+		 * Lock and open the group file. This will load all of the
31f24e
+		 * group entries.
31f24e
+		 */
31f24e
+		if (gr_lock () == 0) {
31f24e
+			fprintf (stderr,
31f24e
+			         _("%s: cannot lock %s; try again later.\n"),
31f24e
+			         Prog, gr_dbname ());
31f24e
+			fail_exit (E_GRP_UPDATE);
31f24e
+		}
31f24e
+		gr_locked = true;
31f24e
+		if (gr_open (O_CREAT | O_RDWR) == 0) {
31f24e
+			fprintf (stderr,
31f24e
+			         _("%s: cannot open %s\n"),
31f24e
+			         Prog, gr_dbname ());
31f24e
+			fail_exit (E_GRP_UPDATE);
31f24e
+		}
31f24e
+#ifdef SHADOWGRP
31f24e
+		if (is_shadow_grp && (sgr_lock () == 0)) {
31f24e
+			fprintf (stderr,
31f24e
+			         _("%s: cannot lock %s; try again later.\n"),
31f24e
+			         Prog, sgr_dbname ());
31f24e
+			fail_exit (E_GRP_UPDATE);
31f24e
+		}
31f24e
+		sgr_locked = true;
31f24e
+		if (is_shadow_grp && (sgr_open (O_CREAT | O_RDWR) == 0)) {
31f24e
+			fprintf (stderr,
31f24e
+			         _("%s: cannot open %s\n"),
31f24e
+			         Prog, sgr_dbname ());
31f24e
+			fail_exit (E_GRP_UPDATE);
31f24e
+		}
31f24e
+#endif
31f24e
 	}
31f24e
 #ifdef ENABLE_SUBIDS
31f24e
 	if (vflg || Vflg) {
31f24e
@@ -1717,44 +1689,6 @@ static void open_files (void)
31f24e
 #endif				/* ENABLE_SUBIDS */
31f24e
 }
31f24e
 
31f24e
-/*
31f24e
- * open_group_files - lock and open the group files
31f24e
- *
31f24e
- *	open_group_files() loads all of the group entries.
31f24e
- */
31f24e
-static void open_group_files (void)
31f24e
-{
31f24e
-	if (gr_lock () == 0) {
31f24e
-		fprintf (stderr,
31f24e
-					_("%s: cannot lock %s; try again later.\n"),
31f24e
-					Prog, gr_dbname ());
31f24e
-		fail_exit (E_GRP_UPDATE);
31f24e
-	}
31f24e
-	gr_locked = true;
31f24e
-	if (gr_open (O_CREAT | O_RDWR) == 0) {
31f24e
-		fprintf (stderr,
31f24e
-					_("%s: cannot open %s\n"),
31f24e
-					Prog, gr_dbname ());
31f24e
-		fail_exit (E_GRP_UPDATE);
31f24e
-	}
31f24e
-
31f24e
-#ifdef SHADOWGRP
31f24e
-	if (is_shadow_grp && (sgr_lock () == 0)) {
31f24e
-		fprintf (stderr,
31f24e
-					_("%s: cannot lock %s; try again later.\n"),
31f24e
-					Prog, sgr_dbname ());
31f24e
-		fail_exit (E_GRP_UPDATE);
31f24e
-	}
31f24e
-	sgr_locked = true;
31f24e
-	if (is_shadow_grp && (sgr_open (O_CREAT | O_RDWR) == 0)) {
31f24e
-		fprintf (stderr,
31f24e
-					_("%s: cannot open %s\n"),
31f24e
-					Prog, sgr_dbname ());
31f24e
-		fail_exit (E_GRP_UPDATE);
31f24e
-	}
31f24e
-#endif
31f24e
-}
31f24e
-
31f24e
 /*
31f24e
  * usr_update - create the user entries
31f24e
  *
31f24e
-- 
31f24e
2.31.1
31f24e