about summary refs log tree commit diff
path: root/nis
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>1998-06-26 22:47:20 +0000
committerUlrich Drepper <drepper@redhat.com>1998-06-26 22:47:20 +0000
commitd6db1d53b378a5b1bfdbad56978acce5f6e46409 (patch)
treebc66616539cf94514b191bad6a9b60d4202650e4 /nis
parent026d50118f29514a92df816fd768c84bccf7e5dc (diff)
downloadglibc-d6db1d53b378a5b1bfdbad56978acce5f6e46409.tar.gz
glibc-d6db1d53b378a5b1bfdbad56978acce5f6e46409.tar.xz
glibc-d6db1d53b378a5b1bfdbad56978acce5f6e46409.zip
Update.
1998-06-26  Thorsten Kukuk  <kukuk@vt.uni-paderborn.de>

	* nis/nis_call.c: Don't copy server structure.
	* nis/nis_intern.h: Remove unused entries.
	* nis/nis_table.c: Rewrite __create_ib_request.
Diffstat (limited to 'nis')
-rw-r--r--nis/nis_call.c133
-rw-r--r--nis/nis_intern.h2
-rw-r--r--nis/nis_table.c263
3 files changed, 143 insertions, 255 deletions
diff --git a/nis/nis_call.c b/nis/nis_call.c
index a2bbdb50c0..94144d50ea 100644
--- a/nis/nis_call.c
+++ b/nis/nis_call.c
@@ -69,8 +69,6 @@ __bind_destroy (dir_binding *bind)
 	auth_destroy (bind->clnt->cl_auth);
       clnt_destroy (bind->clnt);
     }
-  free (bind->server_val);
-  free (bind);
 }
 
 static nis_error
@@ -108,8 +106,7 @@ __bind_next (dir_binding *bind)
   for (j = 0; j < bind->server_val[bind->server_used].ep.ep_len; ++j)
     if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family,
 		"inet") == 0)
-      if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].proto,
-		  "-") == 0)
+      if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-')
 	{
 	  bind->current_ep = j;
 	  return NIS_SUCCESS;
@@ -121,9 +118,7 @@ __bind_next (dir_binding *bind)
 static nis_error
 __bind_connect (dir_binding *dbp)
 {
-  struct sockaddr_in check;
   nis_server *serv;
-  int checklen;
 
   if (dbp == NULL)
     return NIS_FAIL;
@@ -182,38 +177,17 @@ __bind_connect (dir_binding *dbp)
       dbp->use_auth = TRUE;
     }
 
-  /* Get port for sanity checks later */
-  checklen = sizeof (struct sockaddr_in);
-  memset (&check, 0, checklen);
-  if (dbp->use_udp)
-    bind (dbp->socket, (struct sockaddr *)&check, checklen);
-  check.sin_family = AF_INET;
-  if (!getsockname (dbp->socket, (struct sockaddr *)&check, &checklen))
-    dbp->port = check.sin_port;
-
-  dbp->create = time (NULL);
-
   return NIS_SUCCESS;
 }
 
-static dir_binding *
-__bind_create (const nis_server *serv_val, u_int serv_len, u_long flags,
-	       cache2_info *cinfo)
+static nis_error
+__bind_create (dir_binding *dbp, const nis_server *serv_val, u_int serv_len,
+	       u_long flags, cache2_info *cinfo)
 {
-  dir_binding *dbp;
-  u_int i;
-
-  dbp = calloc (1, sizeof (dir_binding));
-  if (dbp == NULL)
-    return NULL;
+  dbp->clnt = NULL;
 
   dbp->server_len = serv_len;
-  dbp->server_val = calloc (1, sizeof (nis_server) * serv_len);
-  if (dbp->server_val == NULL)
-    {
-      free (dbp);
-      return NULL;
-    }
+  dbp->server_val = (nis_server *)serv_val;
 
   if (flags & USE_DGRAM)
     dbp->use_udp = TRUE;
@@ -233,54 +207,6 @@ __bind_create (const nis_server *serv_val, u_int serv_len, u_long flags,
   /* We try the first server */
   dbp->trys = 1;
 
-  for (i = 0; i < serv_len; ++i)
-    {
-      if (serv_val[i].name != NULL)
-	dbp->server_val[i].name = strdup (serv_val[i].name);
-
-      dbp->server_val[i].ep.ep_len = serv_val[i].ep.ep_len;
-      if (dbp->server_val[i].ep.ep_len > 0)
-	{
-	  unsigned long j;
-
-	  dbp->server_val[i].ep.ep_val =
-	    malloc (serv_val[i].ep.ep_len * sizeof (endpoint));
-	  for (j = 0; j < dbp->server_val[i].ep.ep_len; ++j)
-	    {
-	      if (serv_val[i].ep.ep_val[j].uaddr)
-		dbp->server_val[i].ep.ep_val[j].uaddr =
-		  strdup (serv_val[i].ep.ep_val[j].uaddr);
-	      else
-		dbp->server_val[i].ep.ep_val[j].uaddr = NULL;
-	      if (serv_val[i].ep.ep_val[j].family)
-		dbp->server_val[i].ep.ep_val[j].family =
-		  strdup (serv_val[i].ep.ep_val[j].family);
-	      else
-		dbp->server_val[i].ep.ep_val[j].family = NULL;
-	      if (serv_val[i].ep.ep_val[j].proto)
-		dbp->server_val[i].ep.ep_val[j].proto =
-		  strdup (serv_val[i].ep.ep_val[j].proto);
-	      else
-		dbp->server_val[i].ep.ep_val[j].proto = NULL;
-	    }
-	}
-      else
-	dbp->server_val[i].ep.ep_val = NULL;
-      dbp->server_val[i].key_type = serv_val[i].key_type;
-      dbp->server_val[i].pkey.n_len = serv_val[i].pkey.n_len;
-      if (serv_val[i].pkey.n_len > 0)
-	{
-	  dbp->server_val[i].pkey.n_bytes =
-	    malloc (serv_val[i].pkey.n_len);
-	  if (dbp->server_val[i].pkey.n_bytes == NULL)
-	    return NULL;
-	  memcpy (dbp->server_val[i].pkey.n_bytes, serv_val[i].pkey.n_bytes,
-		  serv_val[i].pkey.n_len);
-	}
-      else
-	dbp->server_val[i].pkey.n_bytes = NULL;
-    }
-
   dbp->class = -1;
   if (cinfo != NULL && cinfo->server_used >= 0)
     {
@@ -291,10 +217,10 @@ __bind_create (const nis_server *serv_val, u_int serv_len, u_long flags,
   else if (__nis_findfastest (dbp) < 1)
     {
       __bind_destroy (dbp);
-      return NULL;
+      return NIS_NAMEUNREACHABLE;
     }
 
-  return dbp;
+  return NIS_SUCCESS;
 }
 
 nis_error
@@ -304,19 +230,18 @@ __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
 {
   enum clnt_stat result;
   nis_error retcode;
-  dir_binding *dbp;
+  dir_binding dbp;
 
   if (flags & MASTER_ONLY)
     server_len = 1;
 
-  dbp = __bind_create (server, server_len, flags, cinfo);
-  if (dbp == NULL)
+  if (__bind_create (&dbp, server, server_len, flags, cinfo) != NIS_SUCCESS)
     return NIS_NAMEUNREACHABLE;
-  while (__bind_connect (dbp) != NIS_SUCCESS)
+  while (__bind_connect (&dbp) != NIS_SUCCESS)
     {
-      if (__bind_next (dbp) != NIS_SUCCESS)
+      if (__bind_next (&dbp) != NIS_SUCCESS)
 	{
-	  __bind_destroy (dbp);
+	  __bind_destroy (&dbp);
 	  return NIS_NAMEUNREACHABLE;
 	}
     }
@@ -324,11 +249,11 @@ __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
   do
     {
     again:
-      result = clnt_call (dbp->clnt, prog, xargs, req, xres, resp, RPCTIMEOUT);
+      result = clnt_call (dbp.clnt, prog, xargs, req, xres, resp, RPCTIMEOUT);
 
       if (result != RPC_SUCCESS)
 	{
-	  __bind_destroy (dbp);
+	  __bind_destroy (&dbp);
 	  retcode = NIS_RPCERROR;
 	}
       else
@@ -339,7 +264,7 @@ __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
 	      if ((((nis_result *)resp)->status == NIS_CBRESULTS) &&
 		  (cb != NULL))
 		{
-		  __nis_do_callback(dbp, &((nis_result *)resp)->cookie, cb);
+		  __nis_do_callback(&dbp, &((nis_result *)resp)->cookie, cb);
 		  break;
 		}
 	      /* Yes, this is correct. If we doesn't have to start
@@ -357,13 +282,13 @@ __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
 		  (((nis_result *)resp)->status == NIS_NOSUCHNAME) ||
 		  (((nis_result *)resp)->status == NIS_NOT_ME))
 		{
-		  if (__bind_next (dbp) == NIS_SUCCESS)
+		  if (__bind_next (&dbp) == NIS_SUCCESS)
 		    {
-		      while (__bind_connect (dbp) != NIS_SUCCESS)
+		      while (__bind_connect (&dbp) != NIS_SUCCESS)
 			{
-			  if (__bind_next (dbp) != NIS_SUCCESS)
+			  if (__bind_next (&dbp) != NIS_SUCCESS)
 			    {
-			      __bind_destroy (dbp);
+			      __bind_destroy (&dbp);
 			      return NIS_SUCCESS;
 			    }
 			}
@@ -378,13 +303,13 @@ __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
 		  (((fd_result *)resp)->status == NIS_NOSUCHNAME) ||
 		  (((fd_result *)resp)->status == NIS_NOT_ME))
 		{
-		  if (__bind_next (dbp) == NIS_SUCCESS)
+		  if (__bind_next (&dbp) == NIS_SUCCESS)
 		    {
-		      while (__bind_connect (dbp) != NIS_SUCCESS)
+		      while (__bind_connect (&dbp) != NIS_SUCCESS)
 			{
-			  if (__bind_next (dbp) != NIS_SUCCESS)
+			  if (__bind_next (&dbp) != NIS_SUCCESS)
 			    {
-			      __bind_destroy (dbp);
+			      __bind_destroy (&dbp);
 			      return NIS_SUCCESS;
 			    }
 			}
@@ -400,13 +325,13 @@ __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
 		  (((log_result *)resp)->lr_status == NIS_NOSUCHNAME) ||
 		  (((log_result *)resp)->lr_status == NIS_NOT_ME))
 		{
-		  if (__bind_next (dbp) == NIS_SUCCESS)
+		  if (__bind_next (&dbp) == NIS_SUCCESS)
 		    {
-		      while (__bind_connect (dbp) != NIS_SUCCESS)
+		      while (__bind_connect (&dbp) != NIS_SUCCESS)
 			{
-			  if (__bind_next (dbp) != NIS_SUCCESS)
+			  if (__bind_next (&dbp) != NIS_SUCCESS)
 			    {
-			      __bind_destroy (dbp);
+			      __bind_destroy (&dbp);
 			      return NIS_SUCCESS;
 			    }
 			}
@@ -419,7 +344,7 @@ __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
 	    default:
 	      break;
 	    }
-	  __bind_destroy (dbp);
+	  __bind_destroy (&dbp);
 	  retcode = NIS_SUCCESS;
 	}
     }
diff --git a/nis/nis_intern.h b/nis/nis_intern.h
index 311555d3b9..bea4e272ce 100644
--- a/nis/nis_intern.h
+++ b/nis/nis_intern.h
@@ -36,10 +36,8 @@ struct dir_binding
   bool_t master_only;            /* Is only binded to the master */
   bool_t use_auth;               /* Do we use AUTH ? */
   bool_t use_udp;                /* Do we use UDP ? */
-  time_t create;                 /* Binding creation time */
   struct sockaddr_in addr;       /* Server's IP address */
   int socket;                    /* Server's local socket */
-  unsigned short port;           /* Local port */
 };
 typedef struct dir_binding dir_binding;
 
diff --git a/nis/nis_table.c b/nis/nis_table.c
index 70d2b80fbc..ed4b3740dd 100644
--- a/nis/nis_table.c
+++ b/nis/nis_table.c
@@ -23,138 +23,89 @@
 #include "nis_xdr.h"
 #include "nis_intern.h"
 
-static void
-splitname (const_nis_name name, nis_name *ibr_name, int *srch_len,
-	   nis_attr **srch_val)
+
+static struct ib_request *
+__create_ib_request (const_nis_name name, u_long flags)
 {
-  char *cptr, *key, *val, *next;
-  int size;
+  struct ib_request *ibreq = calloc (1, sizeof (ib_request));
+  char buf[strlen (name) + 1];
+  nis_attr *search_val = NULL;
+  int search_len = 0;
+  char *cptr;
+  size_t size = 0;
 
-  if (name == NULL)
-    return;
+  ibreq->ibr_flags = flags;
 
-  cptr = strdup (name);
-  if (srch_len)
-    *srch_len = 0;
-  if (srch_val)
-    *srch_val = NULL;
-  size = 0;
+  cptr = strcpy (buf, name);
 
   /* Not of "[key=value,key=value,...],foo.." format? */
   if (cptr[0] != '[')
     {
-      *ibr_name = cptr;
-      return;
+      ibreq->ibr_name = strdup (cptr);
+      return ibreq;
     }
 
-  *ibr_name = strchr (cptr, ']');
-  if (*ibr_name == NULL || (*ibr_name)[1] != ',')
-    {
-      free (cptr);
-      *ibr_name = NULL;
-      return;
-    }
+  /* "[key=value,...],foo" format */
+  ibreq->ibr_name = strchr (cptr, ']');
+  if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
+    return NULL;
 
-  *ibr_name[0] = '\0';
-  *ibr_name += 2;
-  *ibr_name = strdup (*ibr_name);
+  /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
+  if (ibreq->ibr_name[-1] == ',')
+    ibreq->ibr_name[-1] = '\0';
+  else
+    ibreq->ibr_name[0] = '\0';
+  ibreq->ibr_name += 2;
+  ibreq->ibr_name = strdup (ibreq->ibr_name);
 
-  if (srch_len == NULL || srch_val == NULL)
-    {
-      free (cptr);
-      return;
-    }
+  ++cptr; /* Remove "[" */
 
-  key = (cptr) + 1;
-  do
+  while (cptr != NULL && cptr[0] != '\0')
     {
-      next = strchr (key, ',');
-      if (next)
-	{
-	  next[0] = '\0';
-	  ++next;
-	}
+      char *key = cptr;
+      char *val = strchr (cptr, '=');
+
+      cptr = strchr (key, ',');
+      if (cptr != NULL)
+	*cptr++ = '\0';
 
-      val = strchr (key, '=');
       if (!val)
 	{
-	  free (cptr);
-	  *srch_val = malloc (sizeof (nis_attr));
-	  if (*srch_val == NULL)
-	    {
-	      free (cptr);
-	      free (*ibr_name);
-	      *ibr_name = NULL;
-	      return;
-	    }
-	  (*srch_val)[0].zattr_val.zattr_val_len = 0;
-	  (*srch_val)[0].zattr_val.zattr_val_val = NULL;
-	  return;
+	  nis_free_request (ibreq);
+	  return NULL;
 	}
-
-      val[0] = '\0';
-      ++val;
-
-      if ((*srch_len) + 1 >= size)
-	{
-	  size += 10;
-	  if (size == 10)
-	    *srch_val = malloc (size * sizeof (char *));
-	  else
-	    *srch_val = realloc (val, size * sizeof (char *));
-	  if (*srch_val == NULL)
+      *val++ = '\0';
+      if ((search_len + 1) >= size)
+        {
+          size += 1;
+          if (size == 1)
+            search_val = malloc (size * sizeof (nis_attr));
+          else
+            search_val = realloc (search_val, size * sizeof (nis_attr));
+	  if (search_val == NULL)
 	    {
-	      free (cptr);
-	      free (*ibr_name);
-	      *ibr_name = NULL;
-	      return;
+	      nis_free_request (ibreq);
+	      return NULL;
 	    }
 	}
-
-      (*srch_val)[*srch_len].zattr_ndx = strdup (key);
-      if (((*srch_val)[*srch_len].zattr_ndx) == NULL)
-	{
-	  free (cptr);
-	  free (*ibr_name);
-	  *ibr_name = NULL;
-	  return;
-	}
-      (*srch_val)[*srch_len].zattr_val.zattr_val_len = strlen (val) + 1;
-      (*srch_val)[*srch_len].zattr_val.zattr_val_val = strdup (val);
-      if ((*srch_val)[*srch_len].zattr_val.zattr_val_val == NULL)
-	{
-	  free (cptr);
-	  free (*ibr_name);
-	  *ibr_name = NULL;
-	  return;
-	}
-      ++(*srch_len);
-
-      key = next;
-
+      search_val[search_len].zattr_ndx = strdup (key);
+      if ((search_val[search_len].zattr_ndx) == NULL)
+        {
+	  nis_free_request (ibreq);
+	  return NULL;
+        }
+      search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1;
+      search_val[search_len].zattr_val.zattr_val_val = strdup (val);
+      if (search_val[search_len].zattr_val.zattr_val_val == NULL)
+        {
+	  nis_free_request (ibreq);
+          return NULL;
+        }
+      ++search_len;
     }
-  while (next);
-
-  free (cptr);
-}
 
-static struct ib_request *
-__create_ib_request (const_nis_name name, struct ib_request *ibreq,
-		     u_long flags)
-{
-  splitname (name, &ibreq->ibr_name, &ibreq->ibr_srch.ibr_srch_len,
-	     &ibreq->ibr_srch.ibr_srch_val);
-  if (ibreq->ibr_name == NULL)
-    return NULL;
-
-  ibreq->ibr_flags = flags;
-  ibreq->ibr_obj.ibr_obj_len = 0;
-  ibreq->ibr_obj.ibr_obj_val = NULL;
-  ibreq->ibr_cbhost.ibr_cbhost_len = 0;
-  ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
-  ibreq->ibr_bufsize = 0;
-  ibreq->ibr_cookie.n_len = 0;
-  ibreq->ibr_cookie.n_bytes = NULL;
+  ibreq->ibr_srch.ibr_srch_val = search_val;
+  ibreq->ibr_srch.ibr_srch_len = search_len;
 
   return ibreq;
 }
@@ -167,7 +118,7 @@ nis_list (const_nis_name name, u_long flags,
 	  const void *userdata)
 {
   nis_result *res = NULL;
-  ib_request *ibreq = calloc (1, sizeof (ib_request));
+  ib_request *ibreq;
   int status;
   int count_links = 0;		/* We will only follow NIS_MAXLINKS links! */
   int done = 0;
@@ -180,13 +131,20 @@ nis_list (const_nis_name name, u_long flags,
   if (res == NULL)
     return NULL;
 
-  if (__create_ib_request (name, ibreq, flags) == NULL)
+  if (name == NULL)
+    {
+      NIS_RES_STATUS (res) = NIS_BADNAME;
+      return res;
+    }
+
+  if ((ibreq = __create_ib_request (name, flags)) == NULL)
     {
       NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
     }
 
-  if (flags & EXPAND_NAME)
+  if ((flags & EXPAND_NAME) &&
+      ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
     {
       names = nis_getnames (ibreq->ibr_name);
       free (ibreq->ibr_name);
@@ -210,23 +168,23 @@ nis_list (const_nis_name name, u_long flags,
     {
       nis_result *lres;
       u_long newflags = flags & ~FOLLOW_PATH & ~ALL_RESULTS;
-      char table_path[NIS_MAXPATH + 1];
+      char table_path[NIS_MAXPATH + 3];
       char *ntable, *p;
       u_long done = 0, failures = 0;
 
       while (names[name_nr] != NULL && !done)
 	{
-	  lres = nis_lookup (names[name_nr], newflags);
+	  lres = nis_lookup (names[name_nr], newflags | NO_AUTHINFO);
 	  if (lres == NULL || NIS_RES_STATUS (lres) != NIS_SUCCESS)
 	    {
 	      NIS_RES_STATUS (res) = NIS_RES_STATUS (lres);
 	      nis_freeresult (lres);
 	      ++name_nr;
 	      continue;
-	      }
+	    }
 
 	  /* nis_lookup handles FOLLOW_LINKS,
-	     so we must have a table object.  */
+	     so we must have a table object.*/
 	  if (__type_of (NIS_RES_OBJECT (lres)) != NIS_TABLE_OBJ)
 	    {
 	      nis_freeresult (lres);
@@ -235,8 +193,11 @@ nis_list (const_nis_name name, u_long flags,
 	    }
 
 	  /* Save the path, discard everything else.  */
-	  snprintf (table_path, NIS_MAXPATH, "%s:%s", names[name_nr],
-		    NIS_RES_OBJECT (lres)->TA_data.ta_path);
+	  p = __stpncpy (table_path, names[name_nr], NIS_MAXPATH);
+	  *p++ = ':';
+	  p = __stpncpy (p, NIS_RES_OBJECT (lres)->TA_data.ta_path,
+			 NIS_MAXPATH - (p - table_path));
+	  *p = '\0';
 	  nis_freeresult (lres);
 	  free (res);
 	  res = NULL;
@@ -397,21 +358,22 @@ nis_add_entry (const_nis_name name, const nis_object *obj2, u_long flags)
   nis_object obj;
   nis_result *res;
   nis_error status;
-  ib_request *ibreq = calloc (1, sizeof (ib_request));
-  char buf1[strlen (name) + 20];
-  char buf4[strlen (name) + 20];
+  ib_request *ibreq;
+  size_t namelen = strlen (name);
+  char buf1[namelen + 20];
+  char buf4[namelen + 20];
 
   res = calloc (1, sizeof (nis_result));
   if (res == NULL)
     return NULL;
 
-  if (ibreq == NULL)
+  if (name == NULL)
     {
-      NIS_RES_STATUS (res) = NIS_NOMEMORY;
+      NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
     }
 
-  if (__create_ib_request (name, ibreq, flags) == NULL)
+  if ((ibreq = __create_ib_request (name, flags)) == NULL)
     {
       NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
@@ -456,13 +418,16 @@ nis_modify_entry (const_nis_name name, const nis_object *obj2, u_long flags)
   nis_object obj;
   nis_result *res;
   nis_error status;
-  ib_request *ibreq = calloc (1, sizeof (ib_request));
-  char buf1[strlen (name) + 20];
-  char buf4[strlen (name) + 20];
+  ib_request *ibreq;
+  size_t namelen = strlen (name);
+  char buf1[namelen + 20];
+  char buf4[namelen + 20];
 
   res = calloc (1, sizeof (nis_result));
+  if (res == NULL)
+    return NULL;
 
-  if (__create_ib_request (name, ibreq, flags) == NULL)
+  if (( ibreq =__create_ib_request (name, flags)) == NULL)
     {
       NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
@@ -505,20 +470,20 @@ nis_remove_entry (const_nis_name name, const nis_object *obj,
 		  u_long flags)
 {
   nis_result *res;
-  ib_request *ibreq = calloc (1, sizeof (ib_request));
+  ib_request *ibreq;
   nis_error status;
 
   res = calloc (1, sizeof (nis_result));
   if (res == NULL)
     return NULL;
 
-  if (ibreq == NULL)
+  if (name == NULL)
     {
-      NIS_RES_STATUS (res) = NIS_NOMEMORY;
+      NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
     }
 
-  if (__create_ib_request (name, ibreq, flags) == NULL)
+  if ((ibreq =__create_ib_request (name, flags)) == NULL)
     {
       NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
@@ -550,20 +515,20 @@ nis_result *
 nis_first_entry (const_nis_name name)
 {
   nis_result *res;
-  ib_request *ibreq = calloc (1, sizeof (ib_request));
+  ib_request *ibreq;
   nis_error status;
 
   res = calloc (1, sizeof (nis_result));
   if (res == NULL)
     return NULL;
 
-  if (ibreq == NULL)
+  if (name == NULL)
     {
-      NIS_RES_STATUS (res) = NIS_NOMEMORY;
+      NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
     }
 
-  if (__create_ib_request (name, ibreq, 0) == NULL)
+  if ((ibreq =__create_ib_request (name, 0)) == NULL)
     {
       NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
@@ -584,20 +549,20 @@ nis_result *
 nis_next_entry (const_nis_name name, const netobj *cookie)
 {
   nis_result *res;
-  ib_request *ibreq = calloc (1, sizeof (ib_request));
+  ib_request *ibreq;
   nis_error status;
 
   res = calloc (1, sizeof (nis_result));
   if (res == NULL)
     return NULL;
 
-  if (ibreq == NULL)
+  if (name == NULL)
     {
-      NIS_RES_STATUS (res) = NIS_NOMEMORY;
+      NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
     }
 
-  if (__create_ib_request (name, ibreq, 0) == NULL)
+  if (( ibreq =__create_ib_request (name, 0)) == NULL)
     {
       NIS_RES_STATUS (res) = NIS_BADNAME;
       return res;
@@ -605,14 +570,7 @@ nis_next_entry (const_nis_name name, const netobj *cookie)
 
   if (cookie != NULL)
     {
-      ibreq->ibr_cookie.n_bytes = malloc (cookie->n_len);
-      if (ibreq->ibr_cookie.n_bytes == NULL)
-	{
-	  NIS_RES_STATUS (res) = NIS_NOMEMORY;
-	  nis_free_request (ibreq);
-	  return res;
-	}
-      memcpy (ibreq->ibr_cookie.n_bytes, cookie->n_bytes, cookie->n_len);
+      ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
       ibreq->ibr_cookie.n_len = cookie->n_len;
     }
 
@@ -622,6 +580,13 @@ nis_next_entry (const_nis_name name, const netobj *cookie)
 			      (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
     NIS_RES_STATUS (res) = status;
 
+  if (cookie != NULL)
+    {
+      /* Don't give cookie free, it is not from us */
+      ibreq->ibr_cookie.n_bytes = NULL;
+      ibreq->ibr_cookie.n_len = 0;
+    }
+
   nis_free_request (ibreq);
 
   return res;