4c1ef1c
diff -up shadow-4.1.5.1/src/newgrp.c.ingroup shadow-4.1.5.1/src/newgrp.c
4c1ef1c
--- shadow-4.1.5.1/src/newgrp.c.ingroup	2014-08-29 13:31:38.000000000 +0200
4c1ef1c
+++ shadow-4.1.5.1/src/newgrp.c	2014-08-29 14:04:57.183849650 +0200
4c1ef1c
@@ -83,15 +83,29 @@ static void usage (void)
4c1ef1c
 	}
4c1ef1c
 }
4c1ef1c
 
4c1ef1c
+static bool ingroup(const char *name, struct group *gr)
4c1ef1c
+{
4c1ef1c
+	char **look;
4c1ef1c
+	bool notfound = true;
4c1ef1c
+
4c1ef1c
+	look = gr->gr_mem;
4c1ef1c
+	while (*look && notfound)
4c1ef1c
+		notfound = strcmp (*look++, name);
4c1ef1c
+
4c1ef1c
+	return !notfound;
4c1ef1c
+}
4c1ef1c
+
4c1ef1c
 /*
4c1ef1c
- * find_matching_group - search all groups of a given group id for
4c1ef1c
+ * find_matching_group - search all groups of a gr's group id for
4c1ef1c
  *                       membership of a given username
4c1ef1c
+ *                       but check gr itself first
4c1ef1c
  */
4c1ef1c
-static /*@null@*/struct group *find_matching_group (const char *name, gid_t gid)
4c1ef1c
+static /*@null@*/struct group *find_matching_group (const char *name, struct group *gr)
4c1ef1c
 {
4c1ef1c
-	struct group *gr;
4c1ef1c
-	char **look;
4c1ef1c
-	bool notfound = true;
4c1ef1c
+	gid_t gid = gr->gr_gid;
4c1ef1c
+
4c1ef1c
+	if (ingroup(name, gr))
4c1ef1c
+		return gr;
4c1ef1c
 
4c1ef1c
 	setgrent ();
4c1ef1c
 	while ((gr = getgrent ()) != NULL) {
4c1ef1c
@@ -103,14 +117,8 @@ static /*@null@*/struct group *find_matc
4c1ef1c
 		 * A group with matching GID was found.
4c1ef1c
 		 * Test for membership of 'name'.
4c1ef1c
 		 */
4c1ef1c
-		look = gr->gr_mem;
4c1ef1c
-		while ((NULL != *look) && notfound) {
4c1ef1c
-			notfound = (strcmp (*look, name) != 0);
4c1ef1c
-			look++;
4c1ef1c
-		}
4c1ef1c
-		if (!notfound) {
4c1ef1c
+		if (ingroup(name, gr))
4c1ef1c
 			break;
4c1ef1c
-		}
4c1ef1c
 	}
4c1ef1c
 	endgrent ();
4c1ef1c
 	return gr;
4c1ef1c
@@ -616,7 +624,7 @@ int main (int argc, char **argv)
4c1ef1c
 	 * groups of the same GID like the requested group for
4c1ef1c
 	 * membership of the current user.
4c1ef1c
 	 */
4c1ef1c
-	grp = find_matching_group (name, grp->gr_gid);
4c1ef1c
+	grp = find_matching_group (name, grp);
4c1ef1c
 	if (NULL == grp) {
4c1ef1c
 		/*
4c1ef1c
 		 * No matching group found. As we already know that