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