]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_table.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / nis / nis_table.c
CommitLineData
d4697bc9 1/* Copyright (c) 1997-2014 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 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
e61abf83 18
a17fa610 19#include <assert.h>
e61abf83
UD
20#include <string.h>
21#include <rpcsvc/nis.h>
91eee4dd
UD
22
23#include "nis_xdr.h"
e61abf83 24#include "nis_intern.h"
a17fa610 25#include "libnsl.h"
e61abf83 26
d6db1d53 27
a17fa610
UD
28struct ib_request *
29__create_ib_request (const_nis_name name, unsigned int flags)
e61abf83 30{
3218d55b 31 struct ib_request *ibreq = calloc (1, sizeof (struct ib_request));
d6db1d53 32 nis_attr *search_val = NULL;
8112cc70 33 size_t search_len = 0;
d6db1d53 34 size_t size = 0;
e61abf83 35
3d8fa13a
UD
36 if (ibreq == NULL)
37 return NULL;
38
d6db1d53 39 ibreq->ibr_flags = flags;
e61abf83 40
3218d55b 41 char *cptr = strdupa (name);
e61abf83
UD
42
43 /* Not of "[key=value,key=value,...],foo.." format? */
44 if (cptr[0] != '[')
0292b0dd
UD
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 }
e61abf83 54
d6db1d53
UD
55 /* "[key=value,...],foo" format */
56 ibreq->ibr_name = strchr (cptr, ']');
57 if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
3d8fa13a 58 {
3218d55b
UD
59 /* The object has not really been built yet so we use free. */
60 free (ibreq);
3d8fa13a
UD
61 return NULL;
62 }
e61abf83 63
9be31a51 64 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
d6db1d53
UD
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);
32abdb71 71 if (ibreq->ibr_name == NULL)
9be31a51
UD
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 }
e61abf83 83
d6db1d53 84 ++cptr; /* Remove "[" */
e61abf83 85
d6db1d53 86 while (cptr != NULL && cptr[0] != '\0')
e61abf83 87 {
d6db1d53
UD
88 char *key = cptr;
89 char *val = strchr (cptr, '=');
90
91 cptr = strchr (key, ',');
92 if (cptr != NULL)
93 *cptr++ = '\0';
e61abf83 94
fff04b32 95 if (__builtin_expect (val == NULL, 0))
e61abf83 96 {
d6db1d53
UD
97 nis_free_request (ibreq);
98 return NULL;
e61abf83 99 }
d6db1d53 100 *val++ = '\0';
fff04b32 101 if (search_len + 1 >= size)
ab26144e
UD
102 {
103 size += 1;
458901c6
UD
104 nis_attr *newp = realloc (search_val, size * sizeof (nis_attr));
105 if (newp == NULL)
9be31a51 106 goto free_null;
458901c6 107 search_val = newp;
e61abf83 108 }
d6db1d53 109 search_val[search_len].zattr_ndx = strdup (key);
fff04b32 110 if (search_val[search_len].zattr_ndx == NULL)
9be31a51 111 goto free_null;
32abdb71 112
d6db1d53
UD
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)
9be31a51
UD
116 {
117 free (search_val[search_len].zattr_ndx);
118 goto free_null;
119 }
32abdb71 120
d6db1d53 121 ++search_len;
e61abf83 122 }
e61abf83 123
d6db1d53
UD
124 ibreq->ibr_srch.ibr_srch_val = search_val;
125 ibreq->ibr_srch.ibr_srch_len = search_len;
e61abf83
UD
126
127 return ibreq;
128}
a17fa610 129libnsl_hidden_def (__create_ib_request)
e61abf83 130
3218d55b 131static const struct timeval RPCTIMEOUT = {10, 0};
e852e889
UD
132
133static char *
3e4370cf 134get_tablepath (char *name, dir_binding *bptr)
e852e889
UD
135{
136 enum clnt_stat result;
3218d55b 137 nis_result res;
e852e889
UD
138 struct ns_request req;
139
3218d55b 140 memset (&res, '\0', sizeof (res));
e852e889
UD
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,
3218d55b 148 (caddr_t) &res, RPCTIMEOUT);
e852e889 149
d00002ed 150 const char *cptr;
3218d55b
UD
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;
e852e889 154 else
3218d55b
UD
155 cptr = "";
156
3e4370cf
UD
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;
e852e889
UD
163}
164
a17fa610
UD
165
166nis_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}
194libnsl_hidden_def (__follow_path)
195
196
e61abf83 197nis_result *
a1129917 198nis_list (const_nis_name name, unsigned int flags,
c131718c 199 int (*callback) (const_nis_name name,
e61abf83
UD
200 const nis_object *object,
201 const void *userdata),
202 const void *userdata)
203{
f2d5cf50 204 nis_result *res = malloc (sizeof (nis_result));
d6db1d53 205 ib_request *ibreq;
2d7da676 206 int status;
e852e889 207 enum clnt_stat clnt_status;
650425ce 208 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
2d7da676
UD
209 int done = 0;
210 nis_name *names;
211 nis_name namebuf[2] = {NULL, NULL};
212 int name_nr = 0;
650425ce 213 nis_cb *cb = NULL;
07683f84
UD
214 char *tableptr;
215 char *tablepath = NULL;
e852e889 216 int first_try = 0; /* Do we try the old binding at first ? */
68361572 217 nis_result *allres = NULL;
e61abf83 218
91eee4dd
UD
219 if (res == NULL)
220 return NULL;
e61abf83 221
d6db1d53
UD
222 if (name == NULL)
223 {
fff04b32 224 status = NIS_BADNAME;
f2d5cf50 225 err_out:
68361572 226 nis_freeresult (allres);
f2d5cf50 227 memset (res, '\0', sizeof (nis_result));
fff04b32 228 NIS_RES_STATUS (res) = status;
d6db1d53
UD
229 return res;
230 }
231
a17fa610 232 ibreq = __create_ib_request (name, flags);
fff04b32 233 if (ibreq == NULL)
e61abf83 234 {
fff04b32 235 status = NIS_BADNAME;
f2d5cf50 236 goto err_out;
e61abf83
UD
237 }
238
901956a5
UD
239 if ((flags & EXPAND_NAME)
240 && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
e61abf83 241 {
dfd2257a
UD
242 names = nis_getnames (ibreq->ibr_name);
243 free (ibreq->ibr_name);
244 ibreq->ibr_name = NULL;
2d7da676 245 if (names == NULL)
e61abf83 246 {
3d8fa13a 247 nis_free_request (ibreq);
fff04b32 248 status = NIS_BADNAME;
f2d5cf50 249 goto err_out;
e61abf83 250 }
dfd2257a 251 ibreq->ibr_name = strdup (names[name_nr]);
901956a5
UD
252 if (ibreq->ibr_name == NULL)
253 {
07683f84 254 nis_freenames (names);
901956a5 255 nis_free_request (ibreq);
fff04b32 256 status = NIS_NOMEMORY;
f2d5cf50 257 goto err_out;
901956a5 258 }
2d7da676
UD
259 }
260 else
650425ce
UD
261 {
262 names = namebuf;
dfd2257a 263 names[name_nr] = ibreq->ibr_name;
650425ce 264 }
e61abf83 265
650425ce
UD
266 cb = NULL;
267
e852e889 268 while (!done)
2d7da676 269 {
e852e889
UD
270 dir_binding bptr;
271 directory_obj *dir = NULL;
650425ce 272
e852e889 273 memset (res, '\0', sizeof (nis_result));
650425ce 274
bd1ebae0 275 status = __nisfind_server (ibreq->ibr_name,
697d37b1
JJ
276 ibreq->ibr_srch.ibr_srch_val != NULL,
277 &dir, &bptr, flags & ~MASTER_ONLY);
e852e889 278 if (status != NIS_SUCCESS)
07683f84 279 {
ab26144e
UD
280 NIS_RES_STATUS (res) = status;
281 goto fail3;
07683f84 282 }
a334319f 283
e852e889 284 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
fff04b32 285 if (__builtin_expect (__nisbind_next (&bptr) != NIS_SUCCESS, 0))
e852e889 286 {
e852e889 287 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
07683f84 288 goto fail;
e852e889 289 }
6f0ee462 290
650425ce
UD
291 if (callback != NULL)
292 {
a17fa610 293 assert (cb == NULL);
650425ce 294 cb = __nis_create_callback (callback, userdata, flags);
dfd2257a
UD
295 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
296 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
e852e889 297 }
650425ce 298
e852e889
UD
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
fff04b32 305 if (__builtin_expect (clnt_status != RPC_SUCCESS, 0))
e852e889
UD
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:
707c7499
UD
313 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
314 && (flags & FOLLOW_LINKS)) /* We are following links. */
e852e889
UD
315 {
316 free (ibreq->ibr_name);
3d8fa13a 317 ibreq->ibr_name = NULL;
e852e889 318 /* If we hit the link limit, bail. */
fff04b32 319 if (__builtin_expect (count_links > NIS_MAXLINKS, 0))
e852e889
UD
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);
901956a5
UD
328 if (ibreq->ibr_name == NULL)
329 {
a334319f 330 NIS_RES_STATUS (res) = NIS_NOMEMORY;
07683f84
UD
331 fail:
332 __nisbind_destroy (&bptr);
07683f84
UD
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);
68361572 345 nis_freeresult (allres);
901956a5
UD
346 return res;
347 }
e852e889
UD
348 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
349 if (ibreq->ibr_srch.ibr_srch_len == 0)
650425ce 350 {
e852e889
UD
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;
650425ce 355 }
56a5719e
UD
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. */
56a5719e
UD
361 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
362 memset (res, '\0', sizeof (*res));
e852e889
UD
363 first_try = 1; /* Try at first the old binding */
364 goto again;
365 }
707c7499
UD
366 else if ((flags & FOLLOW_PATH)
367 && NIS_RES_STATUS (res) == NIS_PARTIAL)
e852e889 368 {
ab26144e
UD
369 enum nis_error err = __follow_path (&tablepath, &tableptr,
370 ibreq, &bptr);
371 if (err != NIS_SUCCESS)
e852e889 372 {
ab26144e
UD
373 if (err == NIS_NOMEMORY)
374 NIS_RES_STATUS (res) = err;
a334319f 375 ++done;
e852e889
UD
376 }
377 else
378 {
707c7499
UD
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. */
a17fa610 384 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
707c7499 385 memset (res, '\0', sizeof (*res));
a334319f
UD
386 first_try = 1;
387 goto again;
e852e889
UD
388 }
389 }
68361572
UD
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 }
ab26144e
UD
429 enum nis_error err = __follow_path (&tablepath, &tableptr,
430 ibreq, &bptr);
431 if (err != NIS_SUCCESS)
68361572 432 {
4df92d57
UD
433 /* Prepare for the nis_freeresult call. */
434 memset (res, '\0', sizeof (*res));
435
ab26144e
UD
436 if (err == NIS_NOMEMORY)
437 NIS_RES_STATUS (allres) = err;
68361572
UD
438 ++done;
439 }
440 }
e852e889 441 else
2d7da676 442 ++done;
e852e889
UD
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))
650425ce 451 ++done;
e852e889
UD
452 else
453 {
ab26144e 454 enum nis_error err
a17fa610 455 = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
ab26144e 456 if (err != NIS_SUCCESS)
0008163a 457 {
ab26144e
UD
458 if (err == NIS_NOMEMORY)
459 NIS_RES_STATUS (res) = err;
0008163a
UD
460 ++done;
461 }
e852e889
UD
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. */
901956a5
UD
492 free (ibreq->ibr_name);
493 ibreq->ibr_name = NULL;
fff04b32 494 if (__builtin_expect (count_links, 0))
e852e889 495 {
e852e889
UD
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 }
901956a5
UD
506 ibreq->ibr_name = strdup (names[name_nr]);
507 if (ibreq->ibr_name == NULL)
508 {
901956a5 509 NIS_RES_STATUS (res) = NIS_NOMEMORY;
07683f84 510 goto fail;
901956a5 511 }
e852e889
UD
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;
07683f84 524 cb = NULL;
e61abf83 525 }
e852e889
UD
526
527 __nisbind_destroy (&bptr);
528 nis_free_directory (dir);
529 }
e61abf83 530
07683f84
UD
531 free (tablepath);
532
2d7da676
UD
533 if (names != namebuf)
534 nis_freenames (names);
535
dfd2257a 536 nis_free_request (ibreq);
e61abf83 537
68361572
UD
538 if (allres)
539 {
540 nis_freeresult (res);
541 return allres;
542 }
543
e61abf83
UD
544 return res;
545}
7440c23e 546libnsl_hidden_def (nis_list)
e61abf83
UD
547
548nis_result *
a1129917 549nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
e61abf83 550{
0292b0dd 551 nis_result *res = calloc (1, sizeof (nis_result));
91eee4dd
UD
552 if (res == NULL)
553 return NULL;
554
d6db1d53 555 if (name == NULL)
91eee4dd 556 {
d6db1d53 557 NIS_RES_STATUS (res) = NIS_BADNAME;
91eee4dd
UD
558 return res;
559 }
e61abf83 560
a17fa610 561 ib_request *ibreq = __create_ib_request (name, flags);
0292b0dd 562 if (ibreq == NULL)
e61abf83 563 {
91eee4dd 564 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
565 return res;
566 }
567
0292b0dd 568 nis_object obj;
91eee4dd 569 memcpy (&obj, obj2, sizeof (nis_object));
e61abf83 570
fff04b32
UD
571 size_t namelen = strlen (name);
572 char buf1[namelen + 20];
573 char buf4[namelen + 20];
574
91eee4dd
UD
575 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
576 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
ce37fa88 577
91eee4dd
UD
578 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
579 obj.zo_owner = nis_local_principal ();
ce37fa88 580
91eee4dd
UD
581 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
582 obj.zo_group = nis_local_group ();
ce37fa88 583
91eee4dd
UD
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 {
3d8fa13a 589 nis_free_request (ibreq);
91eee4dd
UD
590 NIS_RES_STATUS (res) = NIS_NOMEMORY;
591 return res;
592 }
593 ibreq->ibr_obj.ibr_obj_len = 1;
ce37fa88 594
0292b0dd
UD
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);
fff04b32 600 if (__builtin_expect (status != NIS_SUCCESS, 0))
91eee4dd 601 NIS_RES_STATUS (res) = status;
ce37fa88 602
dfd2257a 603 nis_free_request (ibreq);
e61abf83
UD
604
605 return res;
606}
607
608nis_result *
a1129917
UD
609nis_modify_entry (const_nis_name name, const nis_object *obj2,
610 unsigned int flags)
e61abf83 611{
91eee4dd 612 nis_object obj;
e61abf83 613 nis_result *res;
e61abf83 614 nis_error status;
d6db1d53
UD
615 ib_request *ibreq;
616 size_t namelen = strlen (name);
617 char buf1[namelen + 20];
618 char buf4[namelen + 20];
e61abf83
UD
619
620 res = calloc (1, sizeof (nis_result));
d6db1d53
UD
621 if (res == NULL)
622 return NULL;
e61abf83 623
a17fa610 624 ibreq = __create_ib_request (name, flags);
fff04b32 625 if (ibreq == NULL)
e61abf83 626 {
91eee4dd 627 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
628 return res;
629 }
630
91eee4dd 631 memcpy (&obj, obj2, sizeof (nis_object));
e61abf83 632
91eee4dd
UD
633 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
634 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
ce37fa88 635
91eee4dd
UD
636 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
637 obj.zo_owner = nis_local_principal ();
ce37fa88 638
91eee4dd
UD
639 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
640 obj.zo_group = nis_local_group ();
ce37fa88 641
91eee4dd
UD
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 {
3d8fa13a 647 nis_free_request (ibreq);
91eee4dd
UD
648 NIS_RES_STATUS (res) = NIS_NOMEMORY;
649 return res;
650 }
651 ibreq->ibr_obj.ibr_obj_len = 1;
ce37fa88 652
fff04b32
UD
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))
91eee4dd 658 NIS_RES_STATUS (res) = status;
ce37fa88 659
dfd2257a 660 nis_free_request (ibreq);
e61abf83
UD
661
662 return res;
663}
664
665nis_result *
c131718c 666nis_remove_entry (const_nis_name name, const nis_object *obj,
a1129917 667 unsigned int flags)
e61abf83
UD
668{
669 nis_result *res;
d6db1d53 670 ib_request *ibreq;
e61abf83
UD
671 nis_error status;
672
673 res = calloc (1, sizeof (nis_result));
91eee4dd
UD
674 if (res == NULL)
675 return NULL;
676
d6db1d53 677 if (name == NULL)
91eee4dd 678 {
d6db1d53 679 NIS_RES_STATUS (res) = NIS_BADNAME;
91eee4dd
UD
680 return res;
681 }
e61abf83 682
a17fa610 683 ibreq = __create_ib_request (name, flags);
fff04b32 684 if (ibreq == NULL)
e61abf83 685 {
91eee4dd 686 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
687 return res;
688 }
689
e61abf83
UD
690 if (obj != NULL)
691 {
dfd2257a 692 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
91eee4dd
UD
693 if (ibreq->ibr_obj.ibr_obj_val == NULL)
694 {
3d8fa13a 695 nis_free_request (ibreq);
91eee4dd
UD
696 NIS_RES_STATUS (res) = NIS_NOMEMORY;
697 return res;
698 }
dfd2257a 699 ibreq->ibr_obj.ibr_obj_len = 1;
e61abf83
UD
700 }
701
dfd2257a 702 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
91eee4dd
UD
703 (xdrproc_t) _xdr_ib_request,
704 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
650425ce 705 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
91eee4dd 706 NIS_RES_STATUS (res) = status;
e61abf83 707
dfd2257a 708 nis_free_request (ibreq);
e61abf83
UD
709
710 return res;
711}
712
713nis_result *
c131718c 714nis_first_entry (const_nis_name name)
e61abf83
UD
715{
716 nis_result *res;
d6db1d53 717 ib_request *ibreq;
e61abf83
UD
718 nis_error status;
719
720 res = calloc (1, sizeof (nis_result));
91eee4dd
UD
721 if (res == NULL)
722 return NULL;
723
d6db1d53 724 if (name == NULL)
91eee4dd 725 {
d6db1d53 726 NIS_RES_STATUS (res) = NIS_BADNAME;
91eee4dd
UD
727 return res;
728 }
e61abf83 729
a17fa610 730 ibreq = __create_ib_request (name, 0);
32abdb71 731 if (ibreq == NULL)
e61abf83 732 {
91eee4dd 733 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
734 return res;
735 }
736
32abdb71
UD
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
fff04b32 742 if (__builtin_expect (status != NIS_SUCCESS, 0))
91eee4dd 743 NIS_RES_STATUS (res) = status;
e61abf83 744
dfd2257a 745 nis_free_request (ibreq);
e61abf83
UD
746
747 return res;
748}
749
750nis_result *
c131718c 751nis_next_entry (const_nis_name name, const netobj *cookie)
e61abf83
UD
752{
753 nis_result *res;
d6db1d53 754 ib_request *ibreq;
e61abf83
UD
755 nis_error status;
756
757 res = calloc (1, sizeof (nis_result));
91eee4dd
UD
758 if (res == NULL)
759 return NULL;
760
d6db1d53 761 if (name == NULL)
91eee4dd 762 {
d6db1d53 763 NIS_RES_STATUS (res) = NIS_BADNAME;
91eee4dd
UD
764 return res;
765 }
e61abf83 766
a17fa610 767 ibreq = __create_ib_request (name, 0);
32abdb71 768 if (ibreq == NULL)
e61abf83 769 {
91eee4dd 770 NIS_RES_STATUS (res) = NIS_BADNAME;
e61abf83
UD
771 return res;
772 }
773
774 if (cookie != NULL)
775 {
d6db1d53 776 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
dfd2257a 777 ibreq->ibr_cookie.n_len = cookie->n_len;
e61abf83
UD
778 }
779
32abdb71
UD
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
fff04b32 785 if (__builtin_expect (status != NIS_SUCCESS, 0))
91eee4dd 786 NIS_RES_STATUS (res) = status;
e61abf83 787
d6db1d53
UD
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
dfd2257a 795 nis_free_request (ibreq);
e61abf83
UD
796
797 return res;
798}