00db10
commit ced8f8933673f4efda1d666d26a1a949602035ed
00db10
Author: Stephen Gallagher <sgallagh@redhat.com>
00db10
Date:   Fri Apr 29 22:11:09 2016 -0400
00db10
00db10
    NSS: Implement group merging support.
00db10
00db10
commit 2413e73c32fc36470885ae548631e081d66f4201
00db10
Author: Zack Weinberg <zackw@panix.com>
00db10
Date:   Mon Jul 18 09:33:21 2016 -0300
00db10
00db10
    Don't install the internal header grp-merge.h
00db10
00db10
--- glibc-2.17-c758a686/grp/Makefile
00db10
+++ glibc-2.17-c758a686/grp/Makefile
00db10
@@ -23,7 +23,8 @@
00db10
 
00db10
 routines := fgetgrent initgroups setgroups \
00db10
 	    getgrent getgrgid getgrnam putgrent \
00db10
-	    getgrent_r getgrgid_r getgrnam_r fgetgrent_r
00db10
+	    getgrent_r getgrgid_r getgrnam_r fgetgrent_r \
00db10
+	    grp-merge
00db10
 
00db10
 include ../Makeconfig
00db10
 
00db10
--- glibc-2.17-c758a686/grp/Versions
00db10
+++ glibc-2.17-c758a686/grp/Versions
00db10
@@ -28,4 +28,7 @@
00db10
     # g*
00db10
     getgrouplist;
00db10
   }
00db10
+  GLIBC_PRIVATE {
00db10
+    __merge_grp; __copy_grp;
00db10
+  }
00db10
 }
00db10
--- glibc-2.17-c758a686/grp/getgrgid_r.c
00db10
+++ glibc-2.17-c758a686/grp/getgrgid_r.c
00db10
@@ -18,6 +18,7 @@
00db10
 
00db10
 #include <grp.h>
00db10
 
00db10
+#include <grp-merge.h>
00db10
 
00db10
 #define LOOKUP_TYPE	struct group
00db10
 #define FUNCTION_NAME	getgrgid
00db10
@@ -25,5 +26,7 @@
00db10
 #define ADD_PARAMS	gid_t gid
00db10
 #define ADD_VARIABLES	gid
00db10
 #define BUFLEN		NSS_BUFLEN_GROUP
00db10
+#define DEEPCOPY_FN	__copy_grp
00db10
+#define MERGE_FN	__merge_grp
00db10
 
00db10
 #include <nss/getXXbyYY_r.c>
00db10
--- glibc-2.17-c758a686/grp/getgrnam_r.c
00db10
+++ glibc-2.17-c758a686/grp/getgrnam_r.c
00db10
@@ -18,6 +18,7 @@
00db10
 
00db10
 #include <grp.h>
00db10
 
00db10
+#include <grp-merge.h>
00db10
 
00db10
 #define LOOKUP_TYPE	struct group
00db10
 #define FUNCTION_NAME	getgrnam
00db10
@@ -25,4 +26,7 @@
00db10
 #define ADD_PARAMS	const char *name
00db10
 #define ADD_VARIABLES	name
00db10
 
00db10
+#define DEEPCOPY_FN	__copy_grp
00db10
+#define MERGE_FN	__merge_grp
00db10
+
00db10
 #include <nss/getXXbyYY_r.c>
00db10
--- glibc-2.17-c758a686/grp/grp-merge.c
00db10
+++ glibc-2.17-c758a686/grp/grp-merge.c
00db10
@@ -0,0 +1,186 @@
00db10
+/* Group merging implementation.
00db10
+   Copyright (C) 2016 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <errno.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+#include <grp.h>
00db10
+#include <grp-merge.h>
00db10
+
00db10
+#define BUFCHECK(size)			\
00db10
+  ({					\
00db10
+    do					\
00db10
+      {					\
00db10
+	if (c + (size) > buflen)	\
00db10
+          {				\
00db10
+	    free (members);		\
00db10
+	    return ERANGE;		\
00db10
+	  }				\
00db10
+      }					\
00db10
+    while (0);				\
00db10
+  })
00db10
+
00db10
+int
00db10
+internal_function
00db10
+__copy_grp (const struct group srcgrp, const size_t buflen,
00db10
+	    struct group *destgrp, char *destbuf, char **endptr)
00db10
+{
00db10
+  size_t i;
00db10
+  size_t c = 0;
00db10
+  size_t len;
00db10
+  size_t memcount;
00db10
+  char **members = NULL;
00db10
+
00db10
+  /* Copy the GID.  */
00db10
+  destgrp->gr_gid = srcgrp.gr_gid;
00db10
+
00db10
+  /* Copy the name.  */
00db10
+  len = strlen (srcgrp.gr_name) + 1;
00db10
+  BUFCHECK (len);
00db10
+  memcpy (&destbuf[c], srcgrp.gr_name, len);
00db10
+  destgrp->gr_name = &destbuf[c];
00db10
+  c += len;
00db10
+
00db10
+  /* Copy the password.  */
00db10
+  len = strlen (srcgrp.gr_passwd) + 1;
00db10
+  BUFCHECK (len);
00db10
+  memcpy (&destbuf[c], srcgrp.gr_passwd, len);
00db10
+  destgrp->gr_passwd = &destbuf[c];
00db10
+  c += len;
00db10
+
00db10
+  /* Count all of the members.  */
00db10
+  for (memcount = 0; srcgrp.gr_mem[memcount]; memcount++)
00db10
+    ;
00db10
+
00db10
+  /* Allocate a temporary holding area for the pointers to the member
00db10
+     contents, including space for a NULL-terminator.  */
00db10
+  members = malloc (sizeof (char *) * (memcount + 1));
00db10
+  if (members == NULL)
00db10
+    return ENOMEM;
00db10
+
00db10
+  /* Copy all of the group members to destbuf and add a pointer to each of
00db10
+     them into the 'members' array.  */
00db10
+  for (i = 0; srcgrp.gr_mem[i]; i++)
00db10
+    {
00db10
+      len = strlen (srcgrp.gr_mem[i]) + 1;
00db10
+      BUFCHECK (len);
00db10
+      memcpy (&destbuf[c], srcgrp.gr_mem[i], len);
00db10
+      members[i] = &destbuf[c];
00db10
+      c += len;
00db10
+    }
00db10
+  members[i] = NULL;
00db10
+
00db10
+  /* Copy the pointers from the members array into the buffer and assign them
00db10
+     to the gr_mem member of destgrp.  */
00db10
+  destgrp->gr_mem = (char **) &destbuf[c];
00db10
+  len = sizeof (char *) * (memcount + 1);
00db10
+  BUFCHECK (len);
00db10
+  memcpy (&destbuf[c], members, len);
00db10
+  c += len;
00db10
+  free (members);
00db10
+  members = NULL;
00db10
+
00db10
+  /* Save the count of members at the end.  */
00db10
+  BUFCHECK (sizeof (size_t));
00db10
+  memcpy (&destbuf[c], &memcount, sizeof (size_t));
00db10
+  c += sizeof (size_t);
00db10
+
00db10
+  if (endptr)
00db10
+    *endptr = destbuf + c;
00db10
+  return 0;
00db10
+}
00db10
+libc_hidden_def (__copy_grp)
00db10
+
00db10
+/* Check that the name, GID and passwd fields match, then
00db10
+   copy in the gr_mem array.  */
00db10
+int
00db10
+internal_function
00db10
+__merge_grp (struct group *savedgrp, char *savedbuf, char *savedend,
00db10
+	     size_t buflen, struct group *mergegrp, char *mergebuf)
00db10
+{
00db10
+  size_t c, i, len;
00db10
+  size_t savedmemcount;
00db10
+  size_t memcount;
00db10
+  size_t membersize;
00db10
+  char **members = NULL;
00db10
+
00db10
+  /* We only support merging members of groups with identical names and
00db10
+     GID values. If we hit this case, we need to overwrite the current
00db10
+     buffer with the saved one (which is functionally equivalent to
00db10
+     treating the new lookup as NSS_STATUS_NOTFOUND).  */
00db10
+  if (mergegrp->gr_gid != savedgrp->gr_gid
00db10
+      || strcmp (mergegrp->gr_name, savedgrp->gr_name))
00db10
+    return __copy_grp (*savedgrp, buflen, mergegrp, mergebuf, NULL);
00db10
+
00db10
+  /* Get the count of group members from the last sizeof (size_t) bytes in the
00db10
+     mergegrp buffer.  */
00db10
+  savedmemcount = (size_t) *(savedend - sizeof (size_t));
00db10
+
00db10
+  /* Get the count of new members to add.  */
00db10
+  for (memcount = 0; mergegrp->gr_mem[memcount]; memcount++)
00db10
+    ;
00db10
+
00db10
+  /* Create a temporary array to hold the pointers to the member values from
00db10
+     both the saved and merge groups.  */
00db10
+  membersize = savedmemcount + memcount + 1;
00db10
+  members = malloc (sizeof (char *) * membersize);
00db10
+  if (members == NULL)
00db10
+    return ENOMEM;
00db10
+
00db10
+  /* Copy in the existing member pointers from the saved group
00db10
+     Note: this is not NULL-terminated yet.  */
00db10
+  memcpy (members, savedgrp->gr_mem, sizeof (char *) * savedmemcount);
00db10
+
00db10
+  /* Back up into the savedbuf until we get back to the NULL-terminator of the
00db10
+     group member list. (This means walking back savedmemcount + 1 (char *) pointers
00db10
+     and the member count value.
00db10
+     The value of c is going to be the used length of the buffer backed up by
00db10
+     the member count and further backed up by the size of the pointers.  */
00db10
+  c = savedend - savedbuf
00db10
+      - sizeof (size_t)
00db10
+      - sizeof (char *) * (savedmemcount + 1);
00db10
+
00db10
+  /* Add all the new group members, overwriting the old NULL-terminator while
00db10
+     adding the new pointers to the temporary array.  */
00db10
+  for (i = 0; mergegrp->gr_mem[i]; i++)
00db10
+    {
00db10
+      len = strlen (mergegrp->gr_mem[i]) + 1;
00db10
+      BUFCHECK (len);
00db10
+      memcpy (&savedbuf[c], mergegrp->gr_mem[i], len);
00db10
+      members[savedmemcount + i] = &savedbuf[c];
00db10
+      c += len;
00db10
+    }
00db10
+  /* Add the NULL-terminator.  */
00db10
+  members[savedmemcount + memcount] = NULL;
00db10
+
00db10
+  /* Copy the member array back into the buffer after the member list and free
00db10
+     the member array.  */
00db10
+  savedgrp->gr_mem = (char **) &savedbuf[c];
00db10
+  len = sizeof (char *) * membersize;
00db10
+  BUFCHECK (len);
00db10
+  memcpy (&savedbuf[c], members, len);
00db10
+  c += len;
00db10
+
00db10
+  free (members);
00db10
+  members = NULL;
00db10
+
00db10
+  /* Finally, copy the results back into mergebuf, since that's the buffer
00db10
+     that we were provided by the caller.  */
00db10
+  return __copy_grp (*savedgrp, buflen, mergegrp, mergebuf, NULL);
00db10
+}
00db10
+libc_hidden_def (__merge_grp)
00db10
--- glibc-2.17-c758a686/grp/grp-merge.h
00db10
+++ glibc-2.17-c758a686/grp/grp-merge.h
00db10
@@ -0,0 +1,37 @@
00db10
+/* Group merging implementation.
00db10
+   Copyright (C) 2016 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#ifndef _GRP_MERGE_H
00db10
+#define _GRP_MERGE_H 1
00db10
+
00db10
+#include <grp.h>
00db10
+
00db10
+/* Duplicate a grp struct (and its members). When no longer needed, the
00db10
+   calling function must free(newbuf).  */
00db10
+int
00db10
+__copy_grp (const struct group srcgrp, const size_t buflen,
00db10
+	    struct group *destgrp, char *destbuf, char **endptr)
00db10
+	    internal_function;
00db10
+
00db10
+/* Merge the member lists of two grp structs together.  */
00db10
+int
00db10
+__merge_grp (struct group *savedgrp, char *savedbuf, char *savedend,
00db10
+	     size_t buflen, struct group *mergegrp, char *mergebuf)
00db10
+	     internal_function;
00db10
+
00db10
+#endif /* _GRP_MERGE_H */
00db10
--- glibc-2.17-c758a686/include/grp-merge.h
00db10
+++ glibc-2.17-c758a686/include/grp-merge.h
00db10
@@ -0,0 +1,7 @@
00db10
+#ifndef _GRP_MERGE_H
00db10
+#include <grp/grp-merge.h>
00db10
+
00db10
+libc_hidden_proto (__copy_grp)
00db10
+libc_hidden_proto (__merge_grp)
00db10
+
00db10
+#endif /* _GRP_MERGE_H */
00db10
--- glibc-2.17-c758a686/manual/nss.texi
00db10
+++ glibc-2.17-c758a686/manual/nss.texi
00db10
@@ -180,7 +180,7 @@
00db10
 
00db10
 The case of the keywords is insignificant.  The @var{status}
00db10
 values are the results of a call to a lookup function of a specific
00db10
-service.  They mean
00db10
+service.  They mean:
00db10
 
00db10
 @ftable @samp
00db10
 @item success
00db10
@@ -204,6 +204,50 @@
00db10
 @end ftable
00db10
 
00db10
 @noindent
00db10
+The @var{action} values mean:
00db10
+
00db10
+@ftable @samp
00db10
+@item return
00db10
+
00db10
+If the status matches, stop the lookup process at this service
00db10
+specification.  If an entry is available, provide it to the application.
00db10
+If an error occurred, report it to the application.  In case of a prior
00db10
+@samp{merge} action, the data is combined with previous lookup results,
00db10
+as explained below.
00db10
+
00db10
+@item continue
00db10
+
00db10
+If the status matches, proceed with the lookup process at the next
00db10
+entry, discarding the result of the current lookup (and any merged
00db10
+data).  An exception is the @samp{initgroups} database and the
00db10
+@samp{success} status, where @samp{continue} acts like @code{merge}
00db10
+below.
00db10
+
00db10
+@item merge
00db10
+
00db10
+Proceed with the lookup process, retaining the current lookup result.
00db10
+This action is useful only with the @samp{success} status.  If a
00db10
+subsequent service lookup succeeds and has a matching @samp{return}
00db10
+specification, the results are merged, the lookup process ends, and the
00db10
+merged results are returned to the application.  If the following service
00db10
+has a matching @samp{merge} action, the lookup process continues,
00db10
+retaining the combined data from this and any previous lookups.
00db10
+
00db10
+After a @code{merge} action, errors from subsequent lookups are ignored,
00db10
+and the data gathered so far will be returned.
00db10
+
00db10
+The @samp{merge} only applies to the @samp{success} status.  It is
00db10
+currently implemented for the @samp{group} database and its group
00db10
+members field, @samp{gr_mem}.  If specified for other databases, it
00db10
+causes the lookup to fail (if the @var{status} matches).
00db10
+
00db10
+When processing @samp{merge} for @samp{group} membership, the group GID
00db10
+and name must be identical for both entries.  If only one or the other is
00db10
+a match, the behavior is undefined.
00db10
+
00db10
+@end ftable
00db10
+
00db10
+@noindent
00db10
 If we have a line like
00db10
 
00db10
 @smallexample
00db10
--- glibc-2.17-c758a686/nscd/getgrgid_r.c
00db10
+++ glibc-2.17-c758a686/nscd/getgrgid_r.c
00db10
@@ -17,6 +17,7 @@
00db10
 
00db10
 #include <grp.h>
00db10
 
00db10
+#include <grp-merge.h>
00db10
 
00db10
 #define LOOKUP_TYPE	struct group
00db10
 #define FUNCTION_NAME	getgrgid
00db10
@@ -25,6 +26,9 @@
00db10
 #define ADD_VARIABLES	gid
00db10
 #define BUFLEN		NSS_BUFLEN_GROUP
00db10
 
00db10
+#define DEEPCOPY_FN	__copy_grp
00db10
+#define MERGE_FN	__merge_grp
00db10
+
00db10
 /* We are nscd, so we don't want to be talking to ourselves.  */
00db10
 #undef	USE_NSCD
00db10
 
00db10
--- glibc-2.17-c758a686/nscd/getgrnam_r.c
00db10
+++ glibc-2.17-c758a686/nscd/getgrnam_r.c
00db10
@@ -17,6 +17,7 @@
00db10
 
00db10
 #include <grp.h>
00db10
 
00db10
+#include <grp-merge.h>
00db10
 
00db10
 #define LOOKUP_TYPE	struct group
00db10
 #define FUNCTION_NAME	getgrnam
00db10
@@ -24,6 +25,9 @@
00db10
 #define ADD_PARAMS	const char *name
00db10
 #define ADD_VARIABLES	name
00db10
 
00db10
+#define DEEPCOPY_FN	__copy_grp
00db10
+#define MERGE_FN	__merge_grp
00db10
+
00db10
 /* We are nscd, so we don't want to be talking to ourselves.  */
00db10
 #undef	USE_NSCD
00db10
 
00db10
--- glibc-2.17-c758a686/nss/getXXbyYY_r.c
00db10
+++ glibc-2.17-c758a686/nss/getXXbyYY_r.c
00db10
@@ -131,6 +131,52 @@
00db10
 # define AF_VAL AF_INET
00db10
 #endif
00db10
 
00db10
+
00db10
+/* Set defaults for merge functions that haven't been defined.  */
00db10
+#ifndef DEEPCOPY_FN
00db10
+static inline int
00db10
+__copy_einval (LOOKUP_TYPE a,
00db10
+	       const size_t b,
00db10
+	       LOOKUP_TYPE *c,
00db10
+	       char *d,
00db10
+	       char **e)
00db10
+{
00db10
+  return EINVAL;
00db10
+}
00db10
+# define DEEPCOPY_FN __copy_einval
00db10
+#endif
00db10
+
00db10
+#ifndef MERGE_FN
00db10
+static inline int
00db10
+__merge_einval (LOOKUP_TYPE *a,
00db10
+		char *b,
00db10
+		char *c,
00db10
+		size_t d,
00db10
+		LOOKUP_TYPE *e,
00db10
+		char *f)
00db10
+{
00db10
+  return EINVAL;
00db10
+}
00db10
+# define MERGE_FN __merge_einval
00db10
+#endif
00db10
+
00db10
+#define CHECK_MERGE(err, status)		\
00db10
+  ({						\
00db10
+    do						\
00db10
+      {						\
00db10
+	if (err)				\
00db10
+	  {					\
00db10
+	    __set_errno (err);			\
00db10
+	    if (err == ERANGE)			\
00db10
+	      status = NSS_STATUS_TRYAGAIN;	\
00db10
+	    else				\
00db10
+	      status = NSS_STATUS_UNAVAIL;	\
00db10
+	    break;				\
00db10
+	  }					\
00db10
+      }						\
00db10
+    while (0);					\
00db10
+  })
00db10
+
00db10
 /* Type of the lookup function we need here.  */
00db10
 typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
00db10
 					    size_t, int * H_ERRNO_PARM
00db10
@@ -152,13 +198,16 @@
00db10
   static service_user *startp;
00db10
   static lookup_function start_fct;
00db10
   service_user *nip;
00db10
+  int do_merge = 0;
00db10
+  LOOKUP_TYPE mergegrp;
00db10
+  char *mergebuf = NULL;
00db10
+  char *endptr = NULL;
00db10
   union
00db10
   {
00db10
     lookup_function l;
00db10
     void *ptr;
00db10
   } fct;
00db10
-
00db10
-  int no_more;
00db10
+  int no_more, err;
00db10
   enum nss_status status = NSS_STATUS_UNAVAIL;
00db10
 #ifdef USE_NSCD
00db10
   int nscd_status;
00db10
@@ -278,9 +327,66 @@
00db10
 	  && errno == ERANGE)
00db10
 	break;
00db10
 
00db10
+      if (do_merge)
00db10
+	{
00db10
+
00db10
+	  if (status == NSS_STATUS_SUCCESS)
00db10
+	    {
00db10
+	      /* The previous loop saved a buffer for merging.
00db10
+		 Perform the merge now.  */
00db10
+	      err = MERGE_FN (&mergegrp, mergebuf, endptr, buflen, resbuf,
00db10
+			      buffer);
00db10
+	      CHECK_MERGE (err,status);
00db10
+	      do_merge = 0;
00db10
+	    }
00db10
+	  else
00db10
+	    {
00db10
+	      /* If the result wasn't SUCCESS, copy the saved buffer back
00db10
+	         into the result buffer and set the status back to
00db10
+	         NSS_STATUS_SUCCESS to match the previous pass through the
00db10
+	         loop.
00db10
+	          * If the next action is CONTINUE, it will overwrite the value
00db10
+	            currently in the buffer and return the new value.
00db10
+	          * If the next action is RETURN, we'll return the previously-
00db10
+	            acquired values.
00db10
+	          * If the next action is MERGE, then it will be added to the
00db10
+	            buffer saved from the previous source.  */
00db10
+	      err = DEEPCOPY_FN (mergegrp, buflen, resbuf, buffer, NULL);
00db10
+	      CHECK_MERGE (err, status);
00db10
+	      status = NSS_STATUS_SUCCESS;
00db10
+	    }
00db10
+	}
00db10
+
00db10
+      /* If we were are configured to merge this value with the next one,
00db10
+         save the current value of the group struct.  */
00db10
+      if (nss_next_action (nip, status) == NSS_ACTION_MERGE
00db10
+	  && status == NSS_STATUS_SUCCESS)
00db10
+	{
00db10
+	  /* Copy the current values into a buffer to be merged with the next
00db10
+	     set of retrieved values.  */
00db10
+	  if (mergebuf == NULL)
00db10
+	    {
00db10
+	      /* Only allocate once and reuse it for as many merges as we need
00db10
+	         to perform.  */
00db10
+	      mergebuf = malloc (buflen);
00db10
+	      if (mergebuf == NULL)
00db10
+		{
00db10
+		  __set_errno (ENOMEM);
00db10
+		  status = NSS_STATUS_UNAVAIL;
00db10
+		  break;
00db10
+		}
00db10
+	    }
00db10
+
00db10
+	  err = DEEPCOPY_FN (*resbuf, buflen, &mergegrp, mergebuf, &endptr);
00db10
+	  CHECK_MERGE (err, status);
00db10
+	  do_merge = 1;
00db10
+	}
00db10
+
00db10
       no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING,
00db10
 			     REENTRANT2_NAME_STRING, &fct.ptr, status, 0);
00db10
     }
00db10
+  free (mergebuf);
00db10
+  mergebuf = NULL;
00db10
 
00db10
 #ifdef HANDLE_DIGITS_DOTS
00db10
 done:
00db10
--- glibc-2.17-c758a686/nss/getnssent_r.c
00db10
+++ glibc-2.17-c758a686/nss/getnssent_r.c
00db10
@@ -79,7 +79,18 @@
00db10
       else
00db10
 	status = DL_CALL_FCT (fct.f, (0));
00db10
 
00db10
-      no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, status, 0);
00db10
+
00db10
+      /* This is a special-case.  When [SUCCESS=merge] is in play,
00db10
+         _nss_next2() will skip to the next database.  Due to the
00db10
+         implementation of that function, we can't know whether we're
00db10
+         in an enumeration or an individual lookup, which behaves
00db10
+         differently with regards to merging.  We'll treat SUCCESS as
00db10
+         an indication to start the enumeration at this database. */
00db10
+      if (nss_next_action (*nip, status) == NSS_ACTION_MERGE)
00db10
+	no_more = 1;
00db10
+      else
00db10
+	no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, status, 0);
00db10
+
00db10
       if (is_last_nip)
00db10
 	*last_nip = *nip;
00db10
     }
00db10
@@ -175,8 +186,18 @@
00db10
 
00db10
       do
00db10
 	{
00db10
-	  no_more = __nss_next2 (nip, getent_func_name, NULL, &fct.ptr,
00db10
-				 status, 0);
00db10
+        /* This is a special-case.  When [SUCCESS=merge] is in play,
00db10
+           _nss_next2() will skip to the next database.  Due to the
00db10
+           implementation of that function, we can't know whether we're
00db10
+           in an enumeration or an individual lookup, which behaves
00db10
+           differently with regards to merging.  We'll treat SUCCESS as
00db10
+           an indication to return the results here. */
00db10
+	  if (status == NSS_STATUS_SUCCESS
00db10
+	      && nss_next_action (*nip, status) == NSS_ACTION_MERGE)
00db10
+	    no_more = 1;
00db10
+	  else
00db10
+	    no_more = __nss_next2 (nip, getent_func_name, NULL, &fct.ptr,
00db10
+				   status, 0);
00db10
 
00db10
 	  if (is_last_nip)
00db10
 	    *last_nip = *nip;
00db10
--- glibc-2.17-c758a686/nss/nsswitch.c
00db10
+++ glibc-2.17-c758a686/nss/nsswitch.c
00db10
@@ -712,6 +712,9 @@
00db10
 	      else if (line - name == 8
00db10
 		       && __strncasecmp (name, "CONTINUE", 8) == 0)
00db10
 		action = NSS_ACTION_CONTINUE;
00db10
+	      else if (line - name == 5
00db10
+		       && __strncasecmp (name, "MERGE", 5) == 0)
00db10
+		action = NSS_ACTION_MERGE;
00db10
 	      else
00db10
 		goto finish;
00db10
 
00db10
--- glibc-2.17-c758a686/nss/nsswitch.h
00db10
+++ glibc-2.17-c758a686/nss/nsswitch.h
00db10
@@ -32,7 +32,8 @@
00db10
 typedef enum
00db10
 {
00db10
   NSS_ACTION_CONTINUE,
00db10
-  NSS_ACTION_RETURN
00db10
+  NSS_ACTION_RETURN,
00db10
+  NSS_ACTION_MERGE
00db10
 } lookup_actions;
00db10
 
00db10