Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / nis / nis_table.c
1 /* Copyright (c) 1997-2013 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <assert.h>
20 #include <string.h>
21 #include <rpcsvc/nis.h>
22
23 #include "nis_xdr.h"
24 #include "nis_intern.h"
25 #include "libnsl.h"
26
27
28 struct ib_request *
29 __create_ib_request (const_nis_name name, unsigned int flags)
30 {
31   struct ib_request *ibreq = calloc (1, sizeof (struct ib_request));
32   nis_attr *search_val = NULL;
33   size_t search_len = 0;
34   size_t size = 0;
35
36   if (ibreq == NULL)
37     return NULL;
38
39   ibreq->ibr_flags = flags;
40
41   char *cptr = strdupa (name);
42
43   /* Not of "[key=value,key=value,...],foo.." format? */
44   if (cptr[0] != '[')
45     {
46       ibreq->ibr_name = strdup (cptr);
47       if (ibreq->ibr_name == NULL)
48         {
49           free (ibreq);
50           return NULL;
51         }
52       return ibreq;
53     }
54
55   /* "[key=value,...],foo" format */
56   ibreq->ibr_name = strchr (cptr, ']');
57   if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
58     {
59       /* The object has not really been built yet so we use free.  */
60       free (ibreq);
61       return NULL;
62     }
63
64   /* Check if we have an entry of "[key=value,],bar".  If, remove the "," */
65   if (ibreq->ibr_name[-1] == ',')
66     ibreq->ibr_name[-1] = '\0';
67   else
68     ibreq->ibr_name[0] = '\0';
69   ibreq->ibr_name += 2;
70   ibreq->ibr_name = strdup (ibreq->ibr_name);
71   if (ibreq->ibr_name == NULL)
72     {
73     free_null:
74       while (search_len-- > 0)
75         {
76           free (search_val[search_len].zattr_ndx);
77           free (search_val[search_len].zattr_val.zattr_val_val);
78         }
79       free (search_val);
80       nis_free_request (ibreq);
81       return NULL;
82     }
83
84   ++cptr; /* Remove "[" */
85
86   while (cptr != NULL && cptr[0] != '\0')
87     {
88       char *key = cptr;
89       char *val = strchr (cptr, '=');
90
91       cptr = strchr (key, ',');
92       if (cptr != NULL)
93         *cptr++ = '\0';
94
95       if (__builtin_expect (val == NULL, 0))
96         {
97           nis_free_request (ibreq);
98           return NULL;
99         }
100       *val++ = '\0';
101       if (search_len + 1 >= size)
102         {
103           size += 1;
104           nis_attr *newp = realloc (search_val, size * sizeof (nis_attr));
105           if (newp == NULL)
106             goto free_null;
107           search_val = newp;
108         }
109       search_val[search_len].zattr_ndx = strdup (key);
110       if (search_val[search_len].zattr_ndx == NULL)
111         goto free_null;
112
113       search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1;
114       search_val[search_len].zattr_val.zattr_val_val = strdup (val);
115       if (search_val[search_len].zattr_val.zattr_val_val == NULL)
116         {
117           free (search_val[search_len].zattr_ndx);
118           goto free_null;
119         }
120
121       ++search_len;
122     }
123
124   ibreq->ibr_srch.ibr_srch_val = search_val;
125   ibreq->ibr_srch.ibr_srch_len = search_len;
126
127   return ibreq;
128 }
129 libnsl_hidden_def (__create_ib_request)
130
131 static const struct timeval RPCTIMEOUT = {10, 0};
132
133 static char *
134 get_tablepath (char *name, dir_binding *bptr)
135 {
136   enum clnt_stat result;
137   nis_result res;
138   struct ns_request req;
139
140   memset (&res, '\0', sizeof (res));
141
142   req.ns_name = name;
143   req.ns_object.ns_object_len = 0;
144   req.ns_object.ns_object_val = NULL;
145
146   result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request,
147                       (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
148                       (caddr_t) &res, RPCTIMEOUT);
149
150   const char *cptr;
151   if (result == RPC_SUCCESS && NIS_RES_STATUS (&res) == NIS_SUCCESS
152       && __type_of (NIS_RES_OBJECT (&res)) == NIS_TABLE_OBJ)
153     cptr = NIS_RES_OBJECT (&res)->TA_data.ta_path;
154   else
155     cptr = "";
156
157   char *str = strdup (cptr);
158
159   if (result == RPC_SUCCESS)
160     xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &res);
161
162   return str;
163 }
164
165
166 nis_error
167 __follow_path (char **tablepath, char **tableptr, struct ib_request *ibreq,
168                dir_binding *bptr)
169 {
170   if (*tablepath == NULL)
171     {
172       *tablepath = get_tablepath (ibreq->ibr_name, bptr);
173       if (*tablepath == NULL)
174         return NIS_NOMEMORY;
175
176       *tableptr = *tablepath;
177     }
178   if (*tableptr == NULL)
179     return NIS_NOTFOUND;
180
181   char *newname = strsep (tableptr, ":");
182   if (newname[0] == '\0')
183     return NIS_NOTFOUND;
184
185   newname = strdup (newname);
186   if (newname == NULL)
187     return NIS_NOMEMORY;
188
189   free (ibreq->ibr_name);
190   ibreq->ibr_name = newname;
191
192   return NIS_SUCCESS;
193 }
194 libnsl_hidden_def (__follow_path)
195
196
197 nis_result *
198 nis_list (const_nis_name name, unsigned int flags,
199           int (*callback) (const_nis_name name,
200                            const nis_object *object,
201                            const void *userdata),
202           const void *userdata)
203 {
204   nis_result *res = malloc (sizeof (nis_result));
205   ib_request *ibreq;
206   int status;
207   enum clnt_stat clnt_status;
208   int count_links = 0;          /* We will only follow NIS_MAXLINKS links! */
209   int done = 0;
210   nis_name *names;
211   nis_name namebuf[2] = {NULL, NULL};
212   int name_nr = 0;
213   nis_cb *cb = NULL;
214   char *tableptr;
215   char *tablepath = NULL;
216   int first_try = 0; /* Do we try the old binding at first ? */
217   nis_result *allres = NULL;
218
219   if (res == NULL)
220     return NULL;
221
222   if (name == NULL)
223     {
224       status = NIS_BADNAME;
225     err_out:
226       nis_freeresult (allres);
227       memset (res, '\0', sizeof (nis_result));
228       NIS_RES_STATUS (res) = status;
229       return res;
230     }
231
232   ibreq = __create_ib_request (name, flags);
233   if (ibreq == NULL)
234     {
235       status = NIS_BADNAME;
236       goto err_out;
237     }
238
239   if ((flags & EXPAND_NAME)
240       && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
241     {
242       names = nis_getnames (ibreq->ibr_name);
243       free (ibreq->ibr_name);
244       ibreq->ibr_name = NULL;
245       if (names == NULL)
246         {
247           nis_free_request (ibreq);
248           status = NIS_BADNAME;
249           goto err_out;
250         }
251       ibreq->ibr_name = strdup (names[name_nr]);
252       if (ibreq->ibr_name == NULL)
253         {
254           nis_freenames (names);
255           nis_free_request (ibreq);
256           status = NIS_NOMEMORY;
257           goto err_out;
258         }
259     }
260   else
261     {
262       names = namebuf;
263       names[name_nr] = ibreq->ibr_name;
264     }
265
266   cb = NULL;
267
268   while (!done)
269     {
270       dir_binding bptr;
271       directory_obj *dir = NULL;
272
273       memset (res, '\0', sizeof (nis_result));
274
275       status = __nisfind_server (ibreq->ibr_name,
276                                  ibreq->ibr_srch.ibr_srch_val != NULL,
277                                  &dir, &bptr, flags & ~MASTER_ONLY);
278       if (status != NIS_SUCCESS)
279         {
280           NIS_RES_STATUS (res) = status;
281           goto fail3;
282         }
283
284       while (__nisbind_connect (&bptr) != NIS_SUCCESS)
285         if (__builtin_expect (__nisbind_next (&bptr) != NIS_SUCCESS, 0))
286           {
287             NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
288             goto fail;
289           }
290
291       if (callback != NULL)
292         {
293           assert (cb == NULL);
294           cb = __nis_create_callback (callback, userdata, flags);
295           ibreq->ibr_cbhost.ibr_cbhost_len = 1;
296           ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
297         }
298
299     again:
300       clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
301                                (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
302                                (xdrproc_t) _xdr_nis_result,
303                                (caddr_t) res, RPCTIMEOUT);
304
305       if (__builtin_expect (clnt_status != RPC_SUCCESS, 0))
306         NIS_RES_STATUS (res) = NIS_RPCERROR;
307       else
308         switch (NIS_RES_STATUS (res))
309           { /* start switch */
310           case NIS_PARTIAL:
311           case NIS_SUCCESS:
312           case NIS_S_SUCCESS:
313             if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
314                 && (flags & FOLLOW_LINKS))      /* We are following links.  */
315               {
316                 free (ibreq->ibr_name);
317                 ibreq->ibr_name = NULL;
318                 /* If we hit the link limit, bail.  */
319                 if (__builtin_expect (count_links > NIS_MAXLINKS, 0))
320                   {
321                     NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
322                     ++done;
323                     break;
324                   }
325                 ++count_links;
326                 ibreq->ibr_name =
327                   strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
328                 if (ibreq->ibr_name == NULL)
329                   {
330                     NIS_RES_STATUS (res) = NIS_NOMEMORY;
331                   fail:
332                     __nisbind_destroy (&bptr);
333                     nis_free_directory (dir);
334                   fail3:
335                     free (tablepath);
336                     if (cb)
337                       {
338                         __nis_destroy_callback (cb);
339                         ibreq->ibr_cbhost.ibr_cbhost_len = 0;
340                         ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
341                       }
342                     if (names != namebuf)
343                       nis_freenames (names);
344                     nis_free_request (ibreq);
345                     nis_freeresult (allres);
346                     return res;
347                   }
348                 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
349                   if (ibreq->ibr_srch.ibr_srch_len == 0)
350                     {
351                       ibreq->ibr_srch.ibr_srch_len =
352                         NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
353                       ibreq->ibr_srch.ibr_srch_val =
354                         NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
355                     }
356                 /* The following is a non-obvious optimization.  A
357                    nis_freeresult call would call xdr_free as the
358                    following code.  But it also would unnecessarily
359                    free the result structure.  We avoid this here
360                    along with the necessary tests.  */
361                 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
362                 memset (res, '\0', sizeof (*res));
363                 first_try = 1; /* Try at first the old binding */
364                 goto again;
365               }
366             else if ((flags & FOLLOW_PATH)
367                      && NIS_RES_STATUS (res) == NIS_PARTIAL)
368               {
369                 enum nis_error err = __follow_path (&tablepath, &tableptr,
370                                                     ibreq, &bptr);
371                 if (err != NIS_SUCCESS)
372                   {
373                     if (err == NIS_NOMEMORY)
374                       NIS_RES_STATUS (res) = err;
375                     ++done;
376                   }
377                 else
378                   {
379                     /* The following is a non-obvious optimization.  A
380                        nis_freeresult call would call xdr_free as the
381                        following code.  But it also would unnecessarily
382                        free the result structure.  We avoid this here
383                        along with the necessary tests.  */
384                     xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
385                     memset (res, '\0', sizeof (*res));
386                     first_try = 1;
387                     goto again;
388                   }
389               }
390             else if ((flags & (FOLLOW_PATH | ALL_RESULTS))
391                      == (FOLLOW_PATH | ALL_RESULTS))
392               {
393                 if (allres == NULL)
394                   {
395                     allres = res;
396                     res = malloc (sizeof (nis_result));
397                     if (res == NULL)
398                       {
399                         res = allres;
400                         allres = NULL;
401                         NIS_RES_STATUS (res) = NIS_NOMEMORY;
402                         goto fail;
403                       }
404                     NIS_RES_STATUS (res) = NIS_RES_STATUS (allres);
405                   }
406                 else
407                   {
408                     nis_object *objects_val
409                       = realloc (NIS_RES_OBJECT (allres),
410                                  (NIS_RES_NUMOBJ (allres)
411                                   + NIS_RES_NUMOBJ (res))
412                                  * sizeof (nis_object));
413                     if (objects_val == NULL)
414                       {
415                         NIS_RES_STATUS (res) = NIS_NOMEMORY;
416                         goto fail;
417                       }
418                     NIS_RES_OBJECT (allres) = objects_val;
419                     memcpy (NIS_RES_OBJECT (allres) + NIS_RES_NUMOBJ (allres),
420                             NIS_RES_OBJECT (res),
421                             NIS_RES_NUMOBJ (res) * sizeof (nis_object));
422                     NIS_RES_NUMOBJ (allres) += NIS_RES_NUMOBJ (res);
423                     NIS_RES_NUMOBJ (res) = 0;
424                     free (NIS_RES_OBJECT (res));
425                     NIS_RES_OBJECT (res) = NULL;
426                     NIS_RES_STATUS (allres) = NIS_RES_STATUS (res);
427                     xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
428                   }
429                 enum nis_error err = __follow_path (&tablepath, &tableptr,
430                                                     ibreq, &bptr);
431                 if (err != NIS_SUCCESS)
432                   {
433                     /* Prepare for the nis_freeresult call.  */
434                     memset (res, '\0', sizeof (*res));
435
436                     if (err == NIS_NOMEMORY)
437                       NIS_RES_STATUS (allres) = err;
438                     ++done;
439                   }
440               }
441             else
442               ++done;
443             break;
444           case NIS_CBRESULTS:
445             if (cb != NULL)
446               {
447                 __nis_do_callback (&bptr, &res->cookie, cb);
448                 NIS_RES_STATUS (res) = cb->result;
449
450                 if (!(flags & ALL_RESULTS))
451                   ++done;
452                 else
453                   {
454                     enum nis_error err
455                       = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
456                     if (err != NIS_SUCCESS)
457                       {
458                         if (err == NIS_NOMEMORY)
459                           NIS_RES_STATUS (res) = err;
460                         ++done;
461                       }
462                   }
463               }
464             break;
465           case NIS_SYSTEMERROR:
466           case NIS_NOSUCHNAME:
467           case NIS_NOT_ME:
468             /* If we had first tried the old binding, do nothing, but
469                get a new binding */
470             if (!first_try)
471               {
472                 if (__nisbind_next (&bptr) != NIS_SUCCESS)
473                   {
474                     ++done;
475                     break; /* No more servers to search */
476                   }
477                 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
478                   {
479                     if (__nisbind_next (&bptr) != NIS_SUCCESS)
480                       {
481                         ++done;
482                         break; /* No more servers to search */
483                       }
484                   }
485                 goto again;
486               }
487             break;
488           default:
489             if (!first_try)
490               {
491                 /* Try the next domainname if we don't follow a link.  */
492                 free (ibreq->ibr_name);
493                 ibreq->ibr_name = NULL;
494                 if (__builtin_expect (count_links, 0))
495                   {
496                     NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
497                     ++done;
498                     break;
499                   }
500                 ++name_nr;
501                 if (names[name_nr] == NULL)
502                   {
503                     ++done;
504                     break;
505                   }
506                 ibreq->ibr_name = strdup (names[name_nr]);
507                 if (ibreq->ibr_name == NULL)
508                   {
509                     NIS_RES_STATUS (res) = NIS_NOMEMORY;
510                     goto fail;
511                   }
512                 first_try = 1; /* Try old binding at first */
513                 goto again;
514               }
515             break;
516           }
517       first_try = 0;
518
519       if (cb)
520         {
521           __nis_destroy_callback (cb);
522           ibreq->ibr_cbhost.ibr_cbhost_len = 0;
523           ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
524           cb = NULL;
525         }
526
527       __nisbind_destroy (&bptr);
528       nis_free_directory (dir);
529     }
530
531   free (tablepath);
532
533   if (names != namebuf)
534     nis_freenames (names);
535
536   nis_free_request (ibreq);
537
538   if (allres)
539     {
540       nis_freeresult (res);
541       return allres;
542     }
543
544   return res;
545 }
546 libnsl_hidden_def (nis_list)
547
548 nis_result *
549 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
550 {
551   nis_result *res = calloc (1, sizeof (nis_result));
552   if (res == NULL)
553     return NULL;
554
555   if (name == NULL)
556     {
557       NIS_RES_STATUS (res) = NIS_BADNAME;
558       return res;
559     }
560
561   ib_request *ibreq = __create_ib_request (name, flags);
562   if (ibreq == NULL)
563     {
564       NIS_RES_STATUS (res) = NIS_BADNAME;
565       return res;
566     }
567
568   nis_object obj;
569   memcpy (&obj, obj2, sizeof (nis_object));
570
571   size_t namelen = strlen (name);
572   char buf1[namelen + 20];
573   char buf4[namelen + 20];
574
575   if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
576     obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
577
578   if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
579     obj.zo_owner = nis_local_principal ();
580
581   if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
582     obj.zo_group = nis_local_group ();
583
584   obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
585
586   ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
587   if (ibreq->ibr_obj.ibr_obj_val == NULL)
588     {
589       nis_free_request (ibreq);
590       NIS_RES_STATUS (res) = NIS_NOMEMORY;
591       return res;
592     }
593   ibreq->ibr_obj.ibr_obj_len = 1;
594
595   nis_error status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
596                                    (xdrproc_t) _xdr_ib_request,
597                                    (caddr_t) ibreq,
598                                    (xdrproc_t) _xdr_nis_result,
599                                    (caddr_t) res, 0, NULL);
600   if (__builtin_expect (status != NIS_SUCCESS, 0))
601     NIS_RES_STATUS (res) = status;
602
603   nis_free_request (ibreq);
604
605   return res;
606 }
607
608 nis_result *
609 nis_modify_entry (const_nis_name name, const nis_object *obj2,
610                   unsigned int flags)
611 {
612   nis_object obj;
613   nis_result *res;
614   nis_error status;
615   ib_request *ibreq;
616   size_t namelen = strlen (name);
617   char buf1[namelen + 20];
618   char buf4[namelen + 20];
619
620   res = calloc (1, sizeof (nis_result));
621   if (res == NULL)
622     return NULL;
623
624   ibreq = __create_ib_request (name, flags);
625   if (ibreq == NULL)
626     {
627       NIS_RES_STATUS (res) = NIS_BADNAME;
628       return res;
629     }
630
631   memcpy (&obj, obj2, sizeof (nis_object));
632
633   if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
634     obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
635
636   if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
637     obj.zo_owner = nis_local_principal ();
638
639   if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
640     obj.zo_group = nis_local_group ();
641
642   obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
643
644   ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
645   if (ibreq->ibr_obj.ibr_obj_val == NULL)
646     {
647       nis_free_request (ibreq);
648       NIS_RES_STATUS (res) = NIS_NOMEMORY;
649       return res;
650     }
651   ibreq->ibr_obj.ibr_obj_len = 1;
652
653   status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
654                          (xdrproc_t) _xdr_ib_request,
655                          (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
656                          (caddr_t) res, 0, NULL);
657   if (__builtin_expect (status != NIS_SUCCESS, 0))
658     NIS_RES_STATUS (res) = status;
659
660   nis_free_request (ibreq);
661
662   return res;
663 }
664
665 nis_result *
666 nis_remove_entry (const_nis_name name, const nis_object *obj,
667                   unsigned int flags)
668 {
669   nis_result *res;
670   ib_request *ibreq;
671   nis_error status;
672
673   res = calloc (1, sizeof (nis_result));
674   if (res == NULL)
675     return NULL;
676
677   if (name == NULL)
678     {
679       NIS_RES_STATUS (res) = NIS_BADNAME;
680       return res;
681     }
682
683   ibreq = __create_ib_request (name, flags);
684   if (ibreq == NULL)
685     {
686       NIS_RES_STATUS (res) = NIS_BADNAME;
687       return res;
688     }
689
690   if (obj != NULL)
691     {
692       ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
693       if (ibreq->ibr_obj.ibr_obj_val == NULL)
694         {
695           nis_free_request (ibreq);
696           NIS_RES_STATUS (res) = NIS_NOMEMORY;
697           return res;
698         }
699       ibreq->ibr_obj.ibr_obj_len = 1;
700     }
701
702   if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
703                               (xdrproc_t) _xdr_ib_request,
704                               (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
705                               (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
706     NIS_RES_STATUS (res) = status;
707
708   nis_free_request (ibreq);
709
710   return res;
711 }
712
713 nis_result *
714 nis_first_entry (const_nis_name name)
715 {
716   nis_result *res;
717   ib_request *ibreq;
718   nis_error status;
719
720   res = calloc (1, sizeof (nis_result));
721   if (res == NULL)
722     return NULL;
723
724   if (name == NULL)
725     {
726       NIS_RES_STATUS (res) = NIS_BADNAME;
727       return res;
728     }
729
730   ibreq = __create_ib_request (name, 0);
731   if (ibreq == NULL)
732     {
733       NIS_RES_STATUS (res) = NIS_BADNAME;
734       return res;
735     }
736
737   status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
738                          (xdrproc_t) _xdr_ib_request,
739                          (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
740                          (caddr_t) res, 0, NULL);
741
742   if (__builtin_expect (status != NIS_SUCCESS, 0))
743     NIS_RES_STATUS (res) = status;
744
745   nis_free_request (ibreq);
746
747   return res;
748 }
749
750 nis_result *
751 nis_next_entry (const_nis_name name, const netobj *cookie)
752 {
753   nis_result *res;
754   ib_request *ibreq;
755   nis_error status;
756
757   res = calloc (1, sizeof (nis_result));
758   if (res == NULL)
759     return NULL;
760
761   if (name == NULL)
762     {
763       NIS_RES_STATUS (res) = NIS_BADNAME;
764       return res;
765     }
766
767   ibreq = __create_ib_request (name, 0);
768   if (ibreq == NULL)
769     {
770       NIS_RES_STATUS (res) = NIS_BADNAME;
771       return res;
772     }
773
774   if (cookie != NULL)
775     {
776       ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
777       ibreq->ibr_cookie.n_len = cookie->n_len;
778     }
779
780   status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
781                          (xdrproc_t) _xdr_ib_request,
782                          (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
783                          (caddr_t) res, 0, NULL);
784
785   if (__builtin_expect (status != NIS_SUCCESS, 0))
786     NIS_RES_STATUS (res) = status;
787
788   if (cookie != NULL)
789     {
790       /* Don't give cookie free, it is not from us */
791       ibreq->ibr_cookie.n_bytes = NULL;
792       ibreq->ibr_cookie.n_len = 0;
793     }
794
795   nis_free_request (ibreq);
796
797   return res;
798 }