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

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