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