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