]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_table.c
Remove duplicate version of pmap_getport from NIS code.
[thirdparty/glibc.git] / nis / nis_table.c
CommitLineData
697d37b1
JJ
1/* Copyright (c) 1997-1999, 2003, 2004, 2005, 2006, 2007
2 Free Software Foundation, Inc.
e61abf83 3 This file is part of the GNU C Library.
3d8fa13a 4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
e61abf83
UD
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
e61abf83
UD
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
41bdb6e2 14 Lesser General Public License for more details.
e61abf83 15
41bdb6e2
AJ
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. */
e61abf83 20
a17fa610 21#include <assert.h>
e61abf83
UD
22#include <string.h>
23#include <rpcsvc/nis.h>
91eee4dd
UD
24
25#include "nis_xdr.h"
e61abf83 26#include "nis_intern.h"
a17fa610 27#include "libnsl.h"
e61abf83 28
d6db1d53 29
a17fa610
UD
30struct ib_request *
31__create_ib_request (const_nis_name name, unsigned int flags)
e61abf83 32{
3218d55b 33 struct ib_request *ibreq = calloc (1, sizeof (struct ib_request));
d6db1d53 34 nis_attr *search_val = NULL;
8112cc70 35 size_t search_len = 0;
d6db1d53 36 size_t size = 0;
e61abf83 37
3d8fa13a
UD
38 if (ibreq == NULL)
39 return NULL;
40
d6db1d53 41 ibreq->ibr_flags = flags;
e61abf83 42
3218d55b 43 char *cptr = strdupa (name);
e61abf83
UD
44
45 /* Not of "[key=value,key=value,...],foo.." format? */
46 if (cptr[0] != '[')
0292b0dd
UD
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 }
e61abf83 56
d6db1d53
UD
57 /* "[key=value,...],foo" format */
58 ibreq->ibr_name = strchr (cptr, ']');
59 if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
3d8fa13a 60 {
3218d55b
UD
61 /* The object has not really been built yet so we use free. */
62 free (ibreq);
3d8fa13a
UD
63 return NULL;
64 }
e61abf83 65
9be31a51 66 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
d6db1d53
UD
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);
32abdb71 73 if (ibreq->ibr_name == NULL)
9be31a51
UD
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 }
e61abf83 85
d6db1d53 86 ++cptr; /* Remove "[" */
e61abf83 87
d6db1d53 88 while (cptr != NULL && cptr[0] != '\0')
e61abf83 89 {
d6db1d53
UD
90 char *key = cptr;
91 char *val = strchr (cptr, '=');
92
93 cptr = strchr (key, ',');
94 if (cptr != NULL)
95 *cptr++ = '\0';
e61abf83 96
fff04b32 97 if (__builtin_expect (val == NULL, 0))
e61abf83 98 {
d6db1d53
UD
99 nis_free_request (ibreq);
100 return NULL;
e61abf83 101 }
d6db1d53 102 *val++ = '\0';
fff04b32 103 if (search_len + 1 >= size)
d6db1d53
UD
104 {
105 size += 1;
458901c6
UD
106 nis_attr *newp = realloc (search_val, size * sizeof (nis_attr));
107 if (newp == NULL)
9be31a51 108 goto free_null;
458901c6 109 search_val = newp;
e61abf83 110 }
d6db1d53 111 search_val[search_len].zattr_ndx = strdup (key);
fff04b32 112 if (search_val[search_len].zattr_ndx == NULL)
9be31a51 113 goto free_null;
32abdb71 114
d6db1d53
UD
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)
9be31a51
UD
118 {
119 free (search_val[search_len].zattr_ndx);
120 goto free_null;
121 }
32abdb71 122
d6db1d53 123 ++search_len;
e61abf83 124 }
e61abf83 125
d6db1d53
UD
126 ibreq->ibr_srch.ibr_srch_val = search_val;
127 ibreq->ibr_srch.ibr_srch_len = search_len;
e61abf83
UD
128
129 return ibreq;
130}
a17fa610 131libnsl_hidden_def (__create_ib_request)
e61abf83 132
3218d55b 133static const struct timeval RPCTIMEOUT = {10, 0};
e852e889
UD
134
135static char *
3e4370cf 136get_tablepath (char *name, dir_binding *bptr)
e852e889
UD
137{
138 enum clnt_stat result;
3218d55b 139 nis_result res;
e852e889
UD
140 struct ns_request req;
141
3218d55b 142 memset (&res, '\0', sizeof (res));
e852e889
UD
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,
3218d55b 150 (caddr_t) &res, RPCTIMEOUT);
e852e889 151
d00002ed 152 const char *cptr;
3218d55b
UD
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;
e852e889 156 else
3218d55b
UD
157 cptr = "";
158
3e4370cf
UD
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;
e852e889
UD
165}
166
a17fa610
UD
167
168nis_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}
196libnsl_hidden_def (__follow_path)
197
198
e61abf83 199nis_result *
a1129917 200nis_list (const_nis_name name, unsigned int flags,
c131718c 201 int (*callback) (const_nis_name name,
e61abf83
UD
202 const nis_object *object,
203 const void *userdata),
204 const void *userdata)
205{
f2d5cf50 206 nis_result *res = malloc (sizeof (nis_result));
d6db1d53 207 ib_request *ibreq;
2d7da676 208 int status;
e852e889 209 enum clnt_stat clnt_status;
650425ce 210 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
2d7da676
UD
211 int done = 0;
212 nis_name *names;
213 nis_name namebuf[2] = {NULL, NULL};
214 int name_nr = 0;
650425ce 215 nis_cb *cb = NULL;
07683f84
UD
216 char *tableptr;
217 char *tablepath = NULL;
e852e889 218 int first_try = 0; /* Do we try the old binding at first ? */
68361572 219 nis_result *allres = NULL;
e61abf83 220
91eee4dd
UD
221 if (res == NULL)
222 return NULL;
e61abf83 223
d6db1d53
UD
224 if (name == NULL)
225 {
fff04b32 226 status = NIS_BADNAME;
f2d5cf50 227 err_out:
68361572 228 nis_freeresult (allres);
f2d5cf50 229 memset (res, '\0', sizeof (nis_result));
fff04b32 230 NIS_RES_STATUS (res) = status;
d6db1d53
UD
231 return res;
232 }
233
a17fa610 234 ibreq = __create_ib_request (name, flags);
fff04b32 235 if (ibreq == NULL)
e61abf83 236 {
fff04b32 237 status = NIS_BADNAME;
f2d5cf50 238 goto err_out;
e61abf83
UD
239 }
240
901956a5
UD
241 if ((flags & EXPAND_NAME)
242 && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
e61abf83 243 {
dfd2257a
UD
244 names = nis_getnames (ibreq->ibr_name);
245 free (ibreq->ibr_name);
246 ibreq->ibr_name = NULL;
2d7da676 247 if (names == NULL)
e61abf83 248 {
3d8fa13a 249 nis_free_request (ibreq);
fff04b32 250 status = NIS_BADNAME;
f2d5cf50 251 goto err_out;
e61abf83 252 }
dfd2257a 253 ibreq->ibr_name = strdup (names[name_nr]);
901956a5
UD
254 if (ibreq->ibr_name == NULL)
255 {
07683f84 256 nis_freenames (names);
901956a5 257 nis_free_request (ibreq);
fff04b32 258 status = NIS_NOMEMORY;
f2d5cf50 259 goto err_out;
901956a5 260 }
2d7da676
UD
261 }
262 else
650425ce
UD
263 {
264 names = namebuf;
dfd2257a 265 names[name_nr] = ibreq->ibr_name;
650425ce 266 }
e61abf83 267
650425ce
UD
268 cb = NULL;
269
e852e889 270 while (!done)
2d7da676 271 {
e852e889
UD
272 dir_binding bptr;
273 directory_obj *dir = NULL;
650425ce 274
e852e889 275 memset (res, '\0', sizeof (nis_result));
650425ce 276
bd1ebae0 277 status = __nisfind_server (ibreq->ibr_name,
697d37b1
JJ
278 ibreq->ibr_srch.ibr_srch_val != NULL,
279 &dir, &bptr, flags & ~MASTER_ONLY);
e852e889 280 if (status != NIS_SUCCESS)
07683f84 281 {
e852e889 282 NIS_RES_STATUS (res) = status;
07683f84
UD
283 goto fail3;
284 }
a334319f 285
e852e889 286 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
fff04b32 287 if (__builtin_expect (__nisbind_next (&bptr) != NIS_SUCCESS, 0))
e852e889 288 {
e852e889 289 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
07683f84 290 goto fail;
e852e889 291 }
6f0ee462 292
650425ce
UD
293 if (callback != NULL)
294 {
a17fa610 295 assert (cb == NULL);
650425ce 296 cb = __nis_create_callback (callback, userdata, flags);
dfd2257a
UD
297 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
298 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
e852e889 299 }
650425ce 300
e852e889
UD
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
fff04b32 307 if (__builtin_expect (clnt_status != RPC_SUCCESS, 0))
e852e889
UD
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:
707c7499
UD
315 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
316 && (flags & FOLLOW_LINKS)) /* We are following links. */
e852e889
UD
317 {
318 free (ibreq->ibr_name);
3d8fa13a 319 ibreq->ibr_name = NULL;
e852e889 320 /* If we hit the link limit, bail. */
fff04b32 321 if (__builtin_expect (count_links > NIS_MAXLINKS, 0))
e852e889
UD
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);
901956a5
UD
330 if (ibreq->ibr_name == NULL)
331 {
a334319f 332 NIS_RES_STATUS (res) = NIS_NOMEMORY;
07683f84
UD
333 fail:
334 __nisbind_destroy (&bptr);
07683f84
UD
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);
68361572 347 nis_freeresult (allres);
901956a5
UD
348 return res;
349 }
e852e889
UD
350 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
351 if (ibreq->ibr_srch.ibr_srch_len == 0)
650425ce 352 {
e852e889
UD
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;
650425ce 357 }
56a5719e
UD
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. */
56a5719e
UD
363 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
364 memset (res, '\0', sizeof (*res));
e852e889
UD
365 first_try = 1; /* Try at first the old binding */
366 goto again;
367 }
707c7499
UD
368 else if ((flags & FOLLOW_PATH)
369 && NIS_RES_STATUS (res) == NIS_PARTIAL)
e852e889 370 {
a17fa610
UD
371 clnt_status = __follow_path (&tablepath, &tableptr, ibreq,
372 &bptr);
373 if (clnt_status != NIS_SUCCESS)
e852e889 374 {
0008163a
UD
375 if (clnt_status == NIS_NOMEMORY)
376 NIS_RES_STATUS (res) = clnt_status;
a334319f 377 ++done;
e852e889
UD
378 }
379 else
380 {
707c7499
UD
381 /* The following is a non-obvious optimization. A
382 nis_freeresult call would call xdr_free as the
383 following code. But it also would unnecessarily
384 free the result structure. We avoid this here
385 along with the necessary tests. */
a17fa610 386 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
707c7499 387 memset (res, '\0', sizeof (*res));
a334319f
UD
388 first_try = 1;
389 goto again;
e852e889
UD
390 }
391 }
68361572
UD
392 else if ((flags & (FOLLOW_PATH | ALL_RESULTS))
393 == (FOLLOW_PATH | ALL_RESULTS))
394 {
395 if (allres == NULL)
396 {
397 allres = res;
398 res = malloc (sizeof (nis_result));
399 if (res == NULL)
400 {
401 res = allres;
402 allres = NULL;
403 NIS_RES_STATUS (res) = NIS_NOMEMORY;
404 goto fail;
405 }
406 NIS_RES_STATUS (res) = NIS_RES_STATUS (allres);
407 }
408 else
409 {
410 nis_object *objects_val
411 = realloc (NIS_RES_OBJECT (allres),
412 (NIS_RES_NUMOBJ (allres)
413 + NIS_RES_NUMOBJ (res))
414 * sizeof (nis_object));
415 if (objects_val == NULL)
416 {
417 NIS_RES_STATUS (res) = NIS_NOMEMORY;
418 goto fail;
419 }
420 NIS_RES_OBJECT (allres) = objects_val;
421 memcpy (NIS_RES_OBJECT (allres) + NIS_RES_NUMOBJ (allres),
422 NIS_RES_OBJECT (res),
423 NIS_RES_NUMOBJ (res) * sizeof (nis_object));
424 NIS_RES_NUMOBJ (allres) += NIS_RES_NUMOBJ (res);
425 NIS_RES_NUMOBJ (res) = 0;
426 free (NIS_RES_OBJECT (res));
427 NIS_RES_OBJECT (res) = NULL;
428 NIS_RES_STATUS (allres) = NIS_RES_STATUS (res);
429 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
430 }
431 clnt_status = __follow_path (&tablepath, &tableptr, ibreq,
432 &bptr);
433 if (clnt_status != NIS_SUCCESS)
434 {
4df92d57
UD
435 /* Prepare for the nis_freeresult call. */
436 memset (res, '\0', sizeof (*res));
437
438 if (clnt_status == NIS_NOMEMORY)
439 NIS_RES_STATUS (allres) = clnt_status;
68361572
UD
440 ++done;
441 }
442 }
e852e889 443 else
2d7da676 444 ++done;
e852e889
UD
445 break;
446 case NIS_CBRESULTS:
447 if (cb != NULL)
448 {
449 __nis_do_callback (&bptr, &res->cookie, cb);
450 NIS_RES_STATUS (res) = cb->result;
451
452 if (!(flags & ALL_RESULTS))
650425ce 453 ++done;
e852e889
UD
454 else
455 {
0008163a 456 clnt_status
a17fa610 457 = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
0008163a
UD
458 if (clnt_status != NIS_SUCCESS)
459 {
460 if (clnt_status == NIS_NOMEMORY)
461 NIS_RES_STATUS (res) = clnt_status;
462 ++done;
463 }
e852e889
UD
464 }
465 }
466 break;
467 case NIS_SYSTEMERROR:
468 case NIS_NOSUCHNAME:
469 case NIS_NOT_ME:
470 /* If we had first tried the old binding, do nothing, but
471 get a new binding */
472 if (!first_try)
473 {
474 if (__nisbind_next (&bptr) != NIS_SUCCESS)
475 {
476 ++done;
477 break; /* No more servers to search */
478 }
479 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
480 {
481 if (__nisbind_next (&bptr) != NIS_SUCCESS)
482 {
483 ++done;
484 break; /* No more servers to search */
485 }
486 }
487 goto again;
488 }
489 break;
490 default:
491 if (!first_try)
492 {
493 /* Try the next domainname if we don't follow a link. */
901956a5
UD
494 free (ibreq->ibr_name);
495 ibreq->ibr_name = NULL;
fff04b32 496 if (__builtin_expect (count_links, 0))
e852e889 497 {
e852e889
UD
498 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
499 ++done;
500 break;
501 }
502 ++name_nr;
503 if (names[name_nr] == NULL)
504 {
505 ++done;
506 break;
507 }
901956a5
UD
508 ibreq->ibr_name = strdup (names[name_nr]);
509 if (ibreq->ibr_name == NULL)
510 {
901956a5 511 NIS_RES_STATUS (res) = NIS_NOMEMORY;
07683f84 512 goto fail;
901956a5 513 }
e852e889
UD
514 first_try = 1; /* Try old binding at first */
515 goto again;
516 }
517 break;
518 }
519 first_try = 0;
520
521 if (cb)
522 {
523 __nis_destroy_callback (cb);
524 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
525 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
07683f84 526 cb = NULL;
e61abf83 527 }
e852e889
UD
528
529 __nisbind_destroy (&bptr);
530 nis_free_directory (dir);
531 }
e61abf83 532
07683f84
UD
533 free (tablepath);
534
2d7da676
UD
535 if (names != namebuf)
536 nis_freenames (names);
537
dfd2257a 538 nis_free_request (ibreq);
e61abf83 539
68361572
UD
540 if (allres)
541 {
542 nis_freeresult (res);
543 return allres;
544 }
545
e61abf83
UD
546 return res;
547}
7440c23e 548libnsl_hidden_def (nis_list)
e61abf83
UD
549
550nis_result *
a1129917 551nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
e61abf83 552{
0292b0dd 553 nis_result *res = calloc (1, sizeof (nis_result));
91eee4dd
UD
554 if (res == NULL)
555 return NULL;
556
d6db1d53 557 if (name == NULL)
91eee4dd 558 {
d6db1d53 559 NIS_RES_STATUS (res) = NIS_BADNAME;
91eee4dd
UD
560 return res;
561 }
e61abf83 562
a17fa610 563 ib_request *ibreq = __create_ib_request (name, flags);
0292b0dd 564 if (ibreq == NULL)
e61abf83 565 {
91eee4dd 566 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
567 return res;
568 }
569
0292b0dd 570 nis_object obj;
91eee4dd 571 memcpy (&obj, obj2, sizeof (nis_object));
e61abf83 572
fff04b32
UD
573 size_t namelen = strlen (name);
574 char buf1[namelen + 20];
575 char buf4[namelen + 20];
576
91eee4dd
UD
577 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
578 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
ce37fa88 579
91eee4dd
UD
580 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
581 obj.zo_owner = nis_local_principal ();
ce37fa88 582
91eee4dd
UD
583 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
584 obj.zo_group = nis_local_group ();
ce37fa88 585
91eee4dd
UD
586 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
587
588 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
589 if (ibreq->ibr_obj.ibr_obj_val == NULL)
590 {
3d8fa13a 591 nis_free_request (ibreq);
91eee4dd
UD
592 NIS_RES_STATUS (res) = NIS_NOMEMORY;
593 return res;
594 }
595 ibreq->ibr_obj.ibr_obj_len = 1;
ce37fa88 596
0292b0dd
UD
597 nis_error status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
598 (xdrproc_t) _xdr_ib_request,
599 (caddr_t) ibreq,
600 (xdrproc_t) _xdr_nis_result,
601 (caddr_t) res, 0, NULL);
fff04b32 602 if (__builtin_expect (status != NIS_SUCCESS, 0))
91eee4dd 603 NIS_RES_STATUS (res) = status;
ce37fa88 604
dfd2257a 605 nis_free_request (ibreq);
e61abf83
UD
606
607 return res;
608}
609
610nis_result *
a1129917
UD
611nis_modify_entry (const_nis_name name, const nis_object *obj2,
612 unsigned int flags)
e61abf83 613{
91eee4dd 614 nis_object obj;
e61abf83 615 nis_result *res;
e61abf83 616 nis_error status;
d6db1d53
UD
617 ib_request *ibreq;
618 size_t namelen = strlen (name);
619 char buf1[namelen + 20];
620 char buf4[namelen + 20];
e61abf83
UD
621
622 res = calloc (1, sizeof (nis_result));
d6db1d53
UD
623 if (res == NULL)
624 return NULL;
e61abf83 625
a17fa610 626 ibreq = __create_ib_request (name, flags);
fff04b32 627 if (ibreq == NULL)
e61abf83 628 {
91eee4dd 629 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
630 return res;
631 }
632
91eee4dd 633 memcpy (&obj, obj2, sizeof (nis_object));
e61abf83 634
91eee4dd
UD
635 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
636 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
ce37fa88 637
91eee4dd
UD
638 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
639 obj.zo_owner = nis_local_principal ();
ce37fa88 640
91eee4dd
UD
641 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
642 obj.zo_group = nis_local_group ();
ce37fa88 643
91eee4dd
UD
644 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
645
646 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
647 if (ibreq->ibr_obj.ibr_obj_val == NULL)
648 {
3d8fa13a 649 nis_free_request (ibreq);
91eee4dd
UD
650 NIS_RES_STATUS (res) = NIS_NOMEMORY;
651 return res;
652 }
653 ibreq->ibr_obj.ibr_obj_len = 1;
ce37fa88 654
fff04b32
UD
655 status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
656 (xdrproc_t) _xdr_ib_request,
657 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
658 (caddr_t) res, 0, NULL);
659 if (__builtin_expect (status != NIS_SUCCESS, 0))
91eee4dd 660 NIS_RES_STATUS (res) = status;
ce37fa88 661
dfd2257a 662 nis_free_request (ibreq);
e61abf83
UD
663
664 return res;
665}
666
667nis_result *
c131718c 668nis_remove_entry (const_nis_name name, const nis_object *obj,
a1129917 669 unsigned int flags)
e61abf83
UD
670{
671 nis_result *res;
d6db1d53 672 ib_request *ibreq;
e61abf83
UD
673 nis_error status;
674
675 res = calloc (1, sizeof (nis_result));
91eee4dd
UD
676 if (res == NULL)
677 return NULL;
678
d6db1d53 679 if (name == NULL)
91eee4dd 680 {
d6db1d53 681 NIS_RES_STATUS (res) = NIS_BADNAME;
91eee4dd
UD
682 return res;
683 }
e61abf83 684
a17fa610 685 ibreq = __create_ib_request (name, flags);
fff04b32 686 if (ibreq == NULL)
e61abf83 687 {
91eee4dd 688 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
689 return res;
690 }
691
e61abf83
UD
692 if (obj != NULL)
693 {
dfd2257a 694 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
91eee4dd
UD
695 if (ibreq->ibr_obj.ibr_obj_val == NULL)
696 {
3d8fa13a 697 nis_free_request (ibreq);
91eee4dd
UD
698 NIS_RES_STATUS (res) = NIS_NOMEMORY;
699 return res;
700 }
dfd2257a 701 ibreq->ibr_obj.ibr_obj_len = 1;
e61abf83
UD
702 }
703
dfd2257a 704 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
91eee4dd
UD
705 (xdrproc_t) _xdr_ib_request,
706 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
650425ce 707 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
91eee4dd 708 NIS_RES_STATUS (res) = status;
e61abf83 709
dfd2257a 710 nis_free_request (ibreq);
e61abf83
UD
711
712 return res;
713}
714
715nis_result *
c131718c 716nis_first_entry (const_nis_name name)
e61abf83
UD
717{
718 nis_result *res;
d6db1d53 719 ib_request *ibreq;
e61abf83
UD
720 nis_error status;
721
722 res = calloc (1, sizeof (nis_result));
91eee4dd
UD
723 if (res == NULL)
724 return NULL;
725
d6db1d53 726 if (name == NULL)
91eee4dd 727 {
d6db1d53 728 NIS_RES_STATUS (res) = NIS_BADNAME;
91eee4dd
UD
729 return res;
730 }
e61abf83 731
a17fa610 732 ibreq = __create_ib_request (name, 0);
32abdb71 733 if (ibreq == NULL)
e61abf83 734 {
91eee4dd 735 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
736 return res;
737 }
738
32abdb71
UD
739 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
740 (xdrproc_t) _xdr_ib_request,
741 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
742 (caddr_t) res, 0, NULL);
743
fff04b32 744 if (__builtin_expect (status != NIS_SUCCESS, 0))
91eee4dd 745 NIS_RES_STATUS (res) = status;
e61abf83 746
dfd2257a 747 nis_free_request (ibreq);
e61abf83
UD
748
749 return res;
750}
751
752nis_result *
c131718c 753nis_next_entry (const_nis_name name, const netobj *cookie)
e61abf83
UD
754{
755 nis_result *res;
d6db1d53 756 ib_request *ibreq;
e61abf83
UD
757 nis_error status;
758
759 res = calloc (1, sizeof (nis_result));
91eee4dd
UD
760 if (res == NULL)
761 return NULL;
762
d6db1d53 763 if (name == NULL)
91eee4dd 764 {
d6db1d53 765 NIS_RES_STATUS (res) = NIS_BADNAME;
91eee4dd
UD
766 return res;
767 }
e61abf83 768
a17fa610 769 ibreq = __create_ib_request (name, 0);
32abdb71 770 if (ibreq == NULL)
e61abf83 771 {
91eee4dd 772 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
773 return res;
774 }
775
776 if (cookie != NULL)
777 {
d6db1d53 778 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
dfd2257a 779 ibreq->ibr_cookie.n_len = cookie->n_len;
e61abf83
UD
780 }
781
32abdb71
UD
782 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
783 (xdrproc_t) _xdr_ib_request,
784 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
785 (caddr_t) res, 0, NULL);
786
fff04b32 787 if (__builtin_expect (status != NIS_SUCCESS, 0))
91eee4dd 788 NIS_RES_STATUS (res) = status;
e61abf83 789
d6db1d53
UD
790 if (cookie != NULL)
791 {
792 /* Don't give cookie free, it is not from us */
793 ibreq->ibr_cookie.n_bytes = NULL;
794 ibreq->ibr_cookie.n_len = 0;
795 }
796
dfd2257a 797 nis_free_request (ibreq);
e61abf83
UD
798
799 return res;
800}