Define NOT_IN_libc when compiling benchmark programs
[glibc.git] / nis / nis_table.c
blob0ca40dbc6e7c30c30be0d806ccaa360511e03c61
1 /* Copyright (c) 1997-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <string.h>
21 #include <rpcsvc/nis.h>
23 #include "nis_xdr.h"
24 #include "nis_intern.h"
25 #include "libnsl.h"
28 struct ib_request *
29 __create_ib_request (const_nis_name name, unsigned int flags)
31 struct ib_request *ibreq = calloc (1, sizeof (struct ib_request));
32 nis_attr *search_val = NULL;
33 size_t search_len = 0;
34 size_t size = 0;
36 if (ibreq == NULL)
37 return NULL;
39 ibreq->ibr_flags = flags;
41 char *cptr = strdupa (name);
43 /* Not of "[key=value,key=value,...],foo.." format? */
44 if (cptr[0] != '[')
46 ibreq->ibr_name = strdup (cptr);
47 if (ibreq->ibr_name == NULL)
49 free (ibreq);
50 return NULL;
52 return ibreq;
55 /* "[key=value,...],foo" format */
56 ibreq->ibr_name = strchr (cptr, ']');
57 if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
59 /* The object has not really been built yet so we use free. */
60 free (ibreq);
61 return NULL;
64 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
65 if (ibreq->ibr_name[-1] == ',')
66 ibreq->ibr_name[-1] = '\0';
67 else
68 ibreq->ibr_name[0] = '\0';
69 ibreq->ibr_name += 2;
70 ibreq->ibr_name = strdup (ibreq->ibr_name);
71 if (ibreq->ibr_name == NULL)
73 free_null:
74 while (search_len-- > 0)
76 free (search_val[search_len].zattr_ndx);
77 free (search_val[search_len].zattr_val.zattr_val_val);
79 free (search_val);
80 nis_free_request (ibreq);
81 return NULL;
84 ++cptr; /* Remove "[" */
86 while (cptr != NULL && cptr[0] != '\0')
88 char *key = cptr;
89 char *val = strchr (cptr, '=');
91 cptr = strchr (key, ',');
92 if (cptr != NULL)
93 *cptr++ = '\0';
95 if (__builtin_expect (val == NULL, 0))
97 nis_free_request (ibreq);
98 return NULL;
100 *val++ = '\0';
101 if (search_len + 1 >= size)
103 size += 1;
104 nis_attr *newp = realloc (search_val, size * sizeof (nis_attr));
105 if (newp == NULL)
106 goto free_null;
107 search_val = newp;
109 search_val[search_len].zattr_ndx = strdup (key);
110 if (search_val[search_len].zattr_ndx == NULL)
111 goto free_null;
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)
117 free (search_val[search_len].zattr_ndx);
118 goto free_null;
121 ++search_len;
124 ibreq->ibr_srch.ibr_srch_val = search_val;
125 ibreq->ibr_srch.ibr_srch_len = search_len;
127 return ibreq;
129 libnsl_hidden_def (__create_ib_request)
131 static const struct timeval RPCTIMEOUT = {10, 0};
133 static char *
134 get_tablepath (char *name, dir_binding *bptr)
136 enum clnt_stat result;
137 nis_result res;
138 struct ns_request req;
140 memset (&res, '\0', sizeof (res));
142 req.ns_name = name;
143 req.ns_object.ns_object_len = 0;
144 req.ns_object.ns_object_val = NULL;
146 result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request,
147 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
148 (caddr_t) &res, RPCTIMEOUT);
150 const char *cptr;
151 if (result == RPC_SUCCESS && NIS_RES_STATUS (&res) == NIS_SUCCESS
152 && __type_of (NIS_RES_OBJECT (&res)) == NIS_TABLE_OBJ)
153 cptr = NIS_RES_OBJECT (&res)->TA_data.ta_path;
154 else
155 cptr = "";
157 char *str = strdup (cptr);
159 if (result == RPC_SUCCESS)
160 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &res);
162 return str;
166 nis_error
167 __follow_path (char **tablepath, char **tableptr, struct ib_request *ibreq,
168 dir_binding *bptr)
170 if (*tablepath == NULL)
172 *tablepath = get_tablepath (ibreq->ibr_name, bptr);
173 if (*tablepath == NULL)
174 return NIS_NOMEMORY;
176 *tableptr = *tablepath;
178 if (*tableptr == NULL)
179 return NIS_NOTFOUND;
181 char *newname = strsep (tableptr, ":");
182 if (newname[0] == '\0')
183 return NIS_NOTFOUND;
185 newname = strdup (newname);
186 if (newname == NULL)
187 return NIS_NOMEMORY;
189 free (ibreq->ibr_name);
190 ibreq->ibr_name = newname;
192 return NIS_SUCCESS;
194 libnsl_hidden_def (__follow_path)
197 nis_result *
198 nis_list (const_nis_name name, unsigned int flags,
199 int (*callback) (const_nis_name name,
200 const nis_object *object,
201 const void *userdata),
202 const void *userdata)
204 nis_result *res = malloc (sizeof (nis_result));
205 ib_request *ibreq;
206 int status;
207 enum clnt_stat clnt_status;
208 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
209 int done = 0;
210 nis_name *names;
211 nis_name namebuf[2] = {NULL, NULL};
212 int name_nr = 0;
213 nis_cb *cb = NULL;
214 char *tableptr;
215 char *tablepath = NULL;
216 int first_try = 0; /* Do we try the old binding at first ? */
217 nis_result *allres = NULL;
219 if (res == NULL)
220 return NULL;
222 if (name == NULL)
224 status = NIS_BADNAME;
225 err_out:
226 nis_freeresult (allres);
227 memset (res, '\0', sizeof (nis_result));
228 NIS_RES_STATUS (res) = status;
229 return res;
232 ibreq = __create_ib_request (name, flags);
233 if (ibreq == NULL)
235 status = NIS_BADNAME;
236 goto err_out;
239 if ((flags & EXPAND_NAME)
240 && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
242 names = nis_getnames (ibreq->ibr_name);
243 free (ibreq->ibr_name);
244 ibreq->ibr_name = NULL;
245 if (names == NULL)
247 nis_free_request (ibreq);
248 status = NIS_BADNAME;
249 goto err_out;
251 ibreq->ibr_name = strdup (names[name_nr]);
252 if (ibreq->ibr_name == NULL)
254 nis_freenames (names);
255 nis_free_request (ibreq);
256 status = NIS_NOMEMORY;
257 goto err_out;
260 else
262 names = namebuf;
263 names[name_nr] = ibreq->ibr_name;
266 cb = NULL;
268 while (!done)
270 dir_binding bptr;
271 directory_obj *dir = NULL;
273 memset (res, '\0', sizeof (nis_result));
275 status = __nisfind_server (ibreq->ibr_name,
276 ibreq->ibr_srch.ibr_srch_val != NULL,
277 &dir, &bptr, flags & ~MASTER_ONLY);
278 if (status != NIS_SUCCESS)
280 NIS_RES_STATUS (res) = status;
281 goto fail3;
284 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
285 if (__builtin_expect (__nisbind_next (&bptr) != NIS_SUCCESS, 0))
287 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
288 goto fail;
291 if (callback != NULL)
293 assert (cb == NULL);
294 cb = __nis_create_callback (callback, userdata, flags);
295 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
296 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
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);
305 if (__builtin_expect (clnt_status != RPC_SUCCESS, 0))
306 NIS_RES_STATUS (res) = NIS_RPCERROR;
307 else
308 switch (NIS_RES_STATUS (res))
309 { /* start switch */
310 case NIS_PARTIAL:
311 case NIS_SUCCESS:
312 case NIS_S_SUCCESS:
313 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
314 && (flags & FOLLOW_LINKS)) /* We are following links. */
316 free (ibreq->ibr_name);
317 ibreq->ibr_name = NULL;
318 /* If we hit the link limit, bail. */
319 if (__builtin_expect (count_links > NIS_MAXLINKS, 0))
321 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
322 ++done;
323 break;
325 ++count_links;
326 ibreq->ibr_name =
327 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
328 if (ibreq->ibr_name == NULL)
330 NIS_RES_STATUS (res) = NIS_NOMEMORY;
331 fail:
332 __nisbind_destroy (&bptr);
333 nis_free_directory (dir);
334 fail3:
335 free (tablepath);
336 if (cb)
338 __nis_destroy_callback (cb);
339 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
340 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
342 if (names != namebuf)
343 nis_freenames (names);
344 nis_free_request (ibreq);
345 nis_freeresult (allres);
346 return res;
348 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
349 if (ibreq->ibr_srch.ibr_srch_len == 0)
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;
356 /* The following is a non-obvious optimization. A
357 nis_freeresult call would call xdr_free as the
358 following code. But it also would unnecessarily
359 free the result structure. We avoid this here
360 along with the necessary tests. */
361 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
362 memset (res, '\0', sizeof (*res));
363 first_try = 1; /* Try at first the old binding */
364 goto again;
366 else if ((flags & FOLLOW_PATH)
367 && NIS_RES_STATUS (res) == NIS_PARTIAL)
369 enum nis_error err = __follow_path (&tablepath, &tableptr,
370 ibreq, &bptr);
371 if (err != NIS_SUCCESS)
373 if (err == NIS_NOMEMORY)
374 NIS_RES_STATUS (res) = err;
375 ++done;
377 else
379 /* The following is a non-obvious optimization. A
380 nis_freeresult call would call xdr_free as the
381 following code. But it also would unnecessarily
382 free the result structure. We avoid this here
383 along with the necessary tests. */
384 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
385 memset (res, '\0', sizeof (*res));
386 first_try = 1;
387 goto again;
390 else if ((flags & (FOLLOW_PATH | ALL_RESULTS))
391 == (FOLLOW_PATH | ALL_RESULTS))
393 if (allres == NULL)
395 allres = res;
396 res = malloc (sizeof (nis_result));
397 if (res == NULL)
399 res = allres;
400 allres = NULL;
401 NIS_RES_STATUS (res) = NIS_NOMEMORY;
402 goto fail;
404 NIS_RES_STATUS (res) = NIS_RES_STATUS (allres);
406 else
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)
415 NIS_RES_STATUS (res) = NIS_NOMEMORY;
416 goto fail;
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);
429 enum nis_error err = __follow_path (&tablepath, &tableptr,
430 ibreq, &bptr);
431 if (err != NIS_SUCCESS)
433 /* Prepare for the nis_freeresult call. */
434 memset (res, '\0', sizeof (*res));
436 if (err == NIS_NOMEMORY)
437 NIS_RES_STATUS (allres) = err;
438 ++done;
441 else
442 ++done;
443 break;
444 case NIS_CBRESULTS:
445 if (cb != NULL)
447 __nis_do_callback (&bptr, &res->cookie, cb);
448 NIS_RES_STATUS (res) = cb->result;
450 if (!(flags & ALL_RESULTS))
451 ++done;
452 else
454 enum nis_error err
455 = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
456 if (err != NIS_SUCCESS)
458 if (err == NIS_NOMEMORY)
459 NIS_RES_STATUS (res) = err;
460 ++done;
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)
472 if (__nisbind_next (&bptr) != NIS_SUCCESS)
474 ++done;
475 break; /* No more servers to search */
477 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
479 if (__nisbind_next (&bptr) != NIS_SUCCESS)
481 ++done;
482 break; /* No more servers to search */
485 goto again;
487 break;
488 default:
489 if (!first_try)
491 /* Try the next domainname if we don't follow a link. */
492 free (ibreq->ibr_name);
493 ibreq->ibr_name = NULL;
494 if (__builtin_expect (count_links, 0))
496 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
497 ++done;
498 break;
500 ++name_nr;
501 if (names[name_nr] == NULL)
503 ++done;
504 break;
506 ibreq->ibr_name = strdup (names[name_nr]);
507 if (ibreq->ibr_name == NULL)
509 NIS_RES_STATUS (res) = NIS_NOMEMORY;
510 goto fail;
512 first_try = 1; /* Try old binding at first */
513 goto again;
515 break;
517 first_try = 0;
519 if (cb)
521 __nis_destroy_callback (cb);
522 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
523 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
524 cb = NULL;
527 __nisbind_destroy (&bptr);
528 nis_free_directory (dir);
531 free (tablepath);
533 if (names != namebuf)
534 nis_freenames (names);
536 nis_free_request (ibreq);
538 if (allres)
540 nis_freeresult (res);
541 return allres;
544 return res;
546 libnsl_hidden_def (nis_list)
548 nis_result *
549 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
551 nis_result *res = calloc (1, sizeof (nis_result));
552 if (res == NULL)
553 return NULL;
555 if (name == NULL)
557 NIS_RES_STATUS (res) = NIS_BADNAME;
558 return res;
561 ib_request *ibreq = __create_ib_request (name, flags);
562 if (ibreq == NULL)
564 NIS_RES_STATUS (res) = NIS_BADNAME;
565 return res;
568 nis_object obj;
569 memcpy (&obj, obj2, sizeof (nis_object));
571 size_t namelen = strlen (name);
572 char buf1[namelen + 20];
573 char buf4[namelen + 20];
575 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
576 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
578 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
579 obj.zo_owner = nis_local_principal ();
581 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
582 obj.zo_group = nis_local_group ();
584 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
586 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
587 if (ibreq->ibr_obj.ibr_obj_val == NULL)
589 nis_free_request (ibreq);
590 NIS_RES_STATUS (res) = NIS_NOMEMORY;
591 return res;
593 ibreq->ibr_obj.ibr_obj_len = 1;
595 nis_error status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
596 (xdrproc_t) _xdr_ib_request,
597 (caddr_t) ibreq,
598 (xdrproc_t) _xdr_nis_result,
599 (caddr_t) res, 0, NULL);
600 if (__builtin_expect (status != NIS_SUCCESS, 0))
601 NIS_RES_STATUS (res) = status;
603 nis_free_request (ibreq);
605 return res;
608 nis_result *
609 nis_modify_entry (const_nis_name name, const nis_object *obj2,
610 unsigned int flags)
612 nis_object obj;
613 nis_result *res;
614 nis_error status;
615 ib_request *ibreq;
616 size_t namelen = strlen (name);
617 char buf1[namelen + 20];
618 char buf4[namelen + 20];
620 res = calloc (1, sizeof (nis_result));
621 if (res == NULL)
622 return NULL;
624 ibreq = __create_ib_request (name, flags);
625 if (ibreq == NULL)
627 NIS_RES_STATUS (res) = NIS_BADNAME;
628 return res;
631 memcpy (&obj, obj2, sizeof (nis_object));
633 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
634 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
636 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
637 obj.zo_owner = nis_local_principal ();
639 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
640 obj.zo_group = nis_local_group ();
642 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
644 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
645 if (ibreq->ibr_obj.ibr_obj_val == NULL)
647 nis_free_request (ibreq);
648 NIS_RES_STATUS (res) = NIS_NOMEMORY;
649 return res;
651 ibreq->ibr_obj.ibr_obj_len = 1;
653 status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
654 (xdrproc_t) _xdr_ib_request,
655 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
656 (caddr_t) res, 0, NULL);
657 if (__builtin_expect (status != NIS_SUCCESS, 0))
658 NIS_RES_STATUS (res) = status;
660 nis_free_request (ibreq);
662 return res;
665 nis_result *
666 nis_remove_entry (const_nis_name name, const nis_object *obj,
667 unsigned int flags)
669 nis_result *res;
670 ib_request *ibreq;
671 nis_error status;
673 res = calloc (1, sizeof (nis_result));
674 if (res == NULL)
675 return NULL;
677 if (name == NULL)
679 NIS_RES_STATUS (res) = NIS_BADNAME;
680 return res;
683 ibreq = __create_ib_request (name, flags);
684 if (ibreq == NULL)
686 NIS_RES_STATUS (res) = NIS_BADNAME;
687 return res;
690 if (obj != NULL)
692 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
693 if (ibreq->ibr_obj.ibr_obj_val == NULL)
695 nis_free_request (ibreq);
696 NIS_RES_STATUS (res) = NIS_NOMEMORY;
697 return res;
699 ibreq->ibr_obj.ibr_obj_len = 1;
702 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
703 (xdrproc_t) _xdr_ib_request,
704 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
705 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
706 NIS_RES_STATUS (res) = status;
708 nis_free_request (ibreq);
710 return res;
713 nis_result *
714 nis_first_entry (const_nis_name name)
716 nis_result *res;
717 ib_request *ibreq;
718 nis_error status;
720 res = calloc (1, sizeof (nis_result));
721 if (res == NULL)
722 return NULL;
724 if (name == NULL)
726 NIS_RES_STATUS (res) = NIS_BADNAME;
727 return res;
730 ibreq = __create_ib_request (name, 0);
731 if (ibreq == NULL)
733 NIS_RES_STATUS (res) = NIS_BADNAME;
734 return res;
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);
742 if (__builtin_expect (status != NIS_SUCCESS, 0))
743 NIS_RES_STATUS (res) = status;
745 nis_free_request (ibreq);
747 return res;
750 nis_result *
751 nis_next_entry (const_nis_name name, const netobj *cookie)
753 nis_result *res;
754 ib_request *ibreq;
755 nis_error status;
757 res = calloc (1, sizeof (nis_result));
758 if (res == NULL)
759 return NULL;
761 if (name == NULL)
763 NIS_RES_STATUS (res) = NIS_BADNAME;
764 return res;
767 ibreq = __create_ib_request (name, 0);
768 if (ibreq == NULL)
770 NIS_RES_STATUS (res) = NIS_BADNAME;
771 return res;
774 if (cookie != NULL)
776 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
777 ibreq->ibr_cookie.n_len = cookie->n_len;
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);
785 if (__builtin_expect (status != NIS_SUCCESS, 0))
786 NIS_RES_STATUS (res) = status;
788 if (cookie != NULL)
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;
795 nis_free_request (ibreq);
797 return res;