00db10
commit 9a3c6a6ff602c88d7155139a7d7d0000b7b7e946
00db10
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
00db10
Date:   Thu Jan 2 10:05:27 2014 +0530
00db10
00db10
    Fix return code from getent netgroup when the netgroup is not found (bz #16366)
00db10
00db10
diff -pruN glibc-2.17-c758a686/nscd/netgroupcache.c glibc-2.17-c758a686/nscd/netgroupcache.c
00db10
--- glibc-2.17-c758a686/nscd/netgroupcache.c	2013-12-03 20:41:12.000000000 -0500
00db10
+++ glibc-2.17-c758a686/nscd/netgroupcache.c	2013-12-19 08:36:52.253000000 -0500
00db10
@@ -65,6 +65,55 @@ struct dataset
00db10
   char strdata[0];
00db10
 };
00db10
 
00db10
+/* Sends a notfound message and prepares a notfound dataset to write to the
00db10
+   cache.  Returns true if there was enough memory to allocate the dataset and
00db10
+   returns the dataset in DATASETP, total bytes to write in TOTALP and the
00db10
+   timeout in TIMEOUTP.  KEY_COPY is set to point to the copy of the key in the
00db10
+   dataset. */
00db10
+static bool
00db10
+do_notfound (struct database_dyn *db, int fd, request_header *req,
00db10
+	       const char *key, struct dataset **datasetp, ssize_t *totalp,
00db10
+	       time_t *timeoutp, char **key_copy)
00db10
+{
00db10
+  struct dataset *dataset;
00db10
+  ssize_t total;
00db10
+  time_t timeout;
00db10
+  bool cacheable = false;
00db10
+
00db10
+  total = sizeof (notfound);
00db10
+  timeout = time (NULL) + db->negtimeout;
00db10
+
00db10
+  if (fd != -1)
00db10
+    TEMP_FAILURE_RETRY (send (fd, &notfound, total, MSG_NOSIGNAL));
00db10
+
00db10
+  dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len, 1);
00db10
+  /* If we cannot permanently store the result, so be it.  */
00db10
+  if (dataset != NULL)
00db10
+    {
00db10
+      dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
00db10
+      dataset->head.recsize = total;
00db10
+      dataset->head.notfound = true;
00db10
+      dataset->head.nreloads = 0;
00db10
+      dataset->head.usable = true;
00db10
+
00db10
+      /* Compute the timeout time.  */
00db10
+      timeout = dataset->head.timeout = time (NULL) + db->negtimeout;
00db10
+      dataset->head.ttl = db->negtimeout;
00db10
+
00db10
+      /* This is the reply.  */
00db10
+      memcpy (&dataset->resp, &notfound, total);
00db10
+
00db10
+      /* Copy the key data.  */
00db10
+      memcpy (dataset->strdata, key, req->key_len);
00db10
+      *key_copy = dataset->strdata;
00db10
+
00db10
+      cacheable = true;
00db10
+    }
00db10
+  *timeoutp = timeout;
00db10
+  *totalp = total;
00db10
+  *datasetp = dataset;
00db10
+  return cacheable;
00db10
+}
00db10
 
00db10
 static time_t
00db10
 addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
00db10
@@ -84,6 +133,7 @@ addgetnetgrentX (struct database_dyn *db
00db10
   struct dataset *dataset;
00db10
   bool cacheable = false;
00db10
   ssize_t total;
00db10
+  bool found = false;
00db10
 
00db10
   char *key_copy = NULL;
00db10
   struct __netgrent data;
00db10
@@ -103,35 +153,8 @@ addgetnetgrentX (struct database_dyn *db
00db10
       && __nss_database_lookup ("netgroup", NULL, NULL, &netgroup_database))
00db10
     {
00db10
       /* No such service.  */
00db10
-      total = sizeof (notfound);
00db10
-      timeout = time (NULL) + db->negtimeout;
00db10
-
00db10
-      if (fd != -1)
00db10
-	TEMP_FAILURE_RETRY (send (fd, &notfound, total, MSG_NOSIGNAL));
00db10
-
00db10
-      dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len, 1);
00db10
-      /* If we cannot permanently store the result, so be it.  */
00db10
-      if (dataset != NULL)
00db10
-	{
00db10
-	  dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
00db10
-	  dataset->head.recsize = total;
00db10
-	  dataset->head.notfound = true;
00db10
-	  dataset->head.nreloads = 0;
00db10
-	  dataset->head.usable = true;
00db10
-
00db10
-	  /* Compute the timeout time.  */
00db10
-	  timeout = dataset->head.timeout = time (NULL) + db->negtimeout;
00db10
-	  dataset->head.ttl = db->negtimeout;
00db10
-
00db10
-	  /* This is the reply.  */
00db10
-	  memcpy (&dataset->resp, &notfound, total);
00db10
-
00db10
-	  /* Copy the key data.  */
00db10
-	  memcpy (dataset->strdata, key, req->key_len);
00db10
-
00db10
-	  cacheable = true;
00db10
-	}
00db10
-
00db10
+      cacheable = do_notfound (db, fd, req, key, &dataset, &total, &timeout,
00db10
+			       &key_copy);
00db10
       goto writeout;
00db10
     }
00db10
 
00db10
@@ -167,6 +190,7 @@ addgetnetgrentX (struct database_dyn *db
00db10
 
00db10
 	  if (status == NSS_STATUS_SUCCESS)
00db10
 	    {
00db10
+	      found = true;
00db10
 	      union
00db10
 	      {
00db10
 		enum nss_status (*f) (struct __netgrent *, char *, size_t,
00db10
@@ -325,6 +349,15 @@ addgetnetgrentX (struct database_dyn *db
00db10
 	}
00db10
     }
00db10
 
00db10
+  /* No results.  Return a failure and write out a notfound record in the
00db10
+     cache.  */
00db10
+  if (!found)
00db10
+    {
00db10
+      cacheable = do_notfound (db, fd, req, key, &dataset, &total, &timeout,
00db10
+			       &key_copy);
00db10
+      goto writeout;
00db10
+    }
00db10
+
00db10
   total = buffilled;
00db10
 
00db10
   /* Fill in the dataset.  */