file s_lrint.S was initially added on branch fedora-branch.
[glibc/history.git] / nis / nis_table.c
blobcb25be61e2443cacc04b7342cd3f99a5480b6312
1 /* Copyright (c) 1997-1999,2003,2004,2005,2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <string.h>
22 #include <rpcsvc/nis.h>
24 #include "nis_xdr.h"
25 #include "nis_intern.h"
26 #include "libnsl.h"
29 struct ib_request *
30 __create_ib_request (const_nis_name name, unsigned int flags)
32 struct ib_request *ibreq = calloc (1, sizeof (struct ib_request));
33 nis_attr *search_val = NULL;
34 size_t search_len = 0;
35 size_t size = 0;
37 if (ibreq == NULL)
38 return NULL;
40 ibreq->ibr_flags = flags;
42 char *cptr = strdupa (name);
44 /* Not of "[key=value,key=value,...],foo.." format? */
45 if (cptr[0] != '[')
47 ibreq->ibr_name = strdup (cptr);
48 if (ibreq->ibr_name == NULL)
50 free (ibreq);
51 return NULL;
53 return ibreq;
56 /* "[key=value,...],foo" format */
57 ibreq->ibr_name = strchr (cptr, ']');
58 if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
60 /* The object has not really been built yet so we use free. */
61 free (ibreq);
62 return NULL;
65 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
66 if (ibreq->ibr_name[-1] == ',')
67 ibreq->ibr_name[-1] = '\0';
68 else
69 ibreq->ibr_name[0] = '\0';
70 ibreq->ibr_name += 2;
71 ibreq->ibr_name = strdup (ibreq->ibr_name);
72 if (ibreq->ibr_name == NULL)
74 free_null:
75 while (search_len-- > 0)
77 free (search_val[search_len].zattr_ndx);
78 free (search_val[search_len].zattr_val.zattr_val_val);
80 free (search_val);
81 nis_free_request (ibreq);
82 return NULL;
85 ++cptr; /* Remove "[" */
87 while (cptr != NULL && cptr[0] != '\0')
89 char *key = cptr;
90 char *val = strchr (cptr, '=');
92 cptr = strchr (key, ',');
93 if (cptr != NULL)
94 *cptr++ = '\0';
96 if (__builtin_expect (val == NULL, 0))
98 nis_free_request (ibreq);
99 return NULL;
101 *val++ = '\0';
102 if (search_len + 1 >= size)
104 size += 1;
105 nis_attr *newp = realloc (search_val, size * sizeof (nis_attr));
106 if (newp == NULL)
107 goto free_null;
108 search_val = newp;
110 search_val[search_len].zattr_ndx = strdup (key);
111 if (search_val[search_len].zattr_ndx == NULL)
112 goto free_null;
114 search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1;
115 search_val[search_len].zattr_val.zattr_val_val = strdup (val);
116 if (search_val[search_len].zattr_val.zattr_val_val == NULL)
118 free (search_val[search_len].zattr_ndx);
119 goto free_null;
122 ++search_len;
125 ibreq->ibr_srch.ibr_srch_val = search_val;
126 ibreq->ibr_srch.ibr_srch_len = search_len;
128 return ibreq;
130 libnsl_hidden_def (__create_ib_request)
132 static const struct timeval RPCTIMEOUT = {10, 0};
134 static char *
135 get_tablepath (char *name, dir_binding *bptr)
137 enum clnt_stat result;
138 nis_result res;
139 struct ns_request req;
141 memset (&res, '\0', sizeof (res));
143 req.ns_name = name;
144 req.ns_object.ns_object_len = 0;
145 req.ns_object.ns_object_val = NULL;
147 result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request,
148 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
149 (caddr_t) &res, RPCTIMEOUT);
151 const char *cptr;
152 if (result == RPC_SUCCESS && NIS_RES_STATUS (&res) == NIS_SUCCESS
153 && __type_of (NIS_RES_OBJECT (&res)) == NIS_TABLE_OBJ)
154 cptr = NIS_RES_OBJECT (&res)->TA_data.ta_path;
155 else
156 cptr = "";
158 char *str = strdup (cptr);
160 if (result == RPC_SUCCESS)
161 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &res);
163 return str;
167 nis_error
168 __follow_path (char **tablepath, char **tableptr, struct ib_request *ibreq,
169 dir_binding *bptr)
171 if (*tablepath == NULL)
173 *tablepath = get_tablepath (ibreq->ibr_name, bptr);
174 if (*tablepath == NULL)
175 return NIS_NOMEMORY;
177 *tableptr = *tablepath;
179 if (*tableptr == NULL)
180 return NIS_NOTFOUND;
182 char *newname = strsep (tableptr, ":");
183 if (newname[0] == '\0')
184 return NIS_NOTFOUND;
186 newname = strdup (newname);
187 if (newname == NULL)
188 return NIS_NOMEMORY;
190 free (ibreq->ibr_name);
191 ibreq->ibr_name = newname;
193 return NIS_SUCCESS;
195 libnsl_hidden_def (__follow_path)
198 nis_result *
199 nis_list (const_nis_name name, unsigned int flags,
200 int (*callback) (const_nis_name name,
201 const nis_object *object,
202 const void *userdata),
203 const void *userdata)
205 nis_result *res = malloc (sizeof (nis_result));
206 ib_request *ibreq;
207 int status;
208 enum clnt_stat clnt_status;
209 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
210 int done = 0;
211 nis_name *names;
212 nis_name namebuf[2] = {NULL, NULL};
213 int name_nr = 0;
214 nis_cb *cb = NULL;
215 char *tableptr;
216 char *tablepath = NULL;
217 int first_try = 0; /* Do we try the old binding at first ? */
218 nis_result *allres = NULL;
220 if (res == NULL)
221 return NULL;
223 if (name == NULL)
225 status = NIS_BADNAME;
226 err_out:
227 nis_freeresult (allres);
228 memset (res, '\0', sizeof (nis_result));
229 NIS_RES_STATUS (res) = status;
230 return res;
233 ibreq = __create_ib_request (name, flags);
234 if (ibreq == NULL)
236 status = NIS_BADNAME;
237 goto err_out;
240 if ((flags & EXPAND_NAME)
241 && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
243 names = nis_getnames (ibreq->ibr_name);
244 free (ibreq->ibr_name);
245 ibreq->ibr_name = NULL;
246 if (names == NULL)
248 nis_free_request (ibreq);
249 status = NIS_BADNAME;
250 goto err_out;
252 ibreq->ibr_name = strdup (names[name_nr]);
253 if (ibreq->ibr_name == NULL)
255 nis_freenames (names);
256 nis_free_request (ibreq);
257 status = NIS_NOMEMORY;
258 goto err_out;
261 else
263 names = namebuf;
264 names[name_nr] = ibreq->ibr_name;
267 cb = NULL;
269 while (!done)
271 dir_binding bptr;
272 directory_obj *dir = NULL;
274 memset (res, '\0', sizeof (nis_result));
276 status = __nisfind_server (ibreq->ibr_name,
277 ibreq->ibr_srch.ibr_srch_val != NULL, &dir);
278 if (status != NIS_SUCCESS)
280 NIS_RES_STATUS (res) = status;
281 goto fail3;
284 status = __nisbind_create (&bptr, dir->do_servers.do_servers_val,
285 dir->do_servers.do_servers_len, flags);
286 if (__builtin_expect (status != NIS_SUCCESS, 0))
288 NIS_RES_STATUS (res) = status;
289 goto fail2;
292 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
293 if (__builtin_expect (__nisbind_next (&bptr) != NIS_SUCCESS, 0))
295 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
296 goto fail;
299 if (callback != NULL)
301 assert (cb == NULL);
302 cb = __nis_create_callback (callback, userdata, flags);
303 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
304 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
307 again:
308 clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
309 (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
310 (xdrproc_t) _xdr_nis_result,
311 (caddr_t) res, RPCTIMEOUT);
313 if (__builtin_expect (clnt_status != RPC_SUCCESS, 0))
314 NIS_RES_STATUS (res) = NIS_RPCERROR;
315 else
316 switch (NIS_RES_STATUS (res))
317 { /* start switch */
318 case NIS_PARTIAL:
319 case NIS_SUCCESS:
320 case NIS_S_SUCCESS:
321 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
322 && (flags & FOLLOW_LINKS)) /* We are following links. */
324 free (ibreq->ibr_name);
325 ibreq->ibr_name = NULL;
326 /* If we hit the link limit, bail. */
327 if (__builtin_expect (count_links > NIS_MAXLINKS, 0))
329 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
330 ++done;
331 break;
333 ++count_links;
334 ibreq->ibr_name =
335 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
336 if (ibreq->ibr_name == NULL)
338 NIS_RES_STATUS (res) = NIS_NOMEMORY;
339 fail:
340 __nisbind_destroy (&bptr);
341 fail2:
342 nis_free_directory (dir);
343 fail3:
344 free (tablepath);
345 if (cb)
347 __nis_destroy_callback (cb);
348 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
349 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
351 if (names != namebuf)
352 nis_freenames (names);
353 nis_free_request (ibreq);
354 nis_freeresult (allres);
355 return res;
357 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
358 if (ibreq->ibr_srch.ibr_srch_len == 0)
360 ibreq->ibr_srch.ibr_srch_len =
361 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
362 ibreq->ibr_srch.ibr_srch_val =
363 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
365 /* The following is a non-obvious optimization. A
366 nis_freeresult call would call xdr_free as the
367 following code. But it also would unnecessarily
368 free the result structure. We avoid this here
369 along with the necessary tests. */
370 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
371 memset (res, '\0', sizeof (*res));
372 first_try = 1; /* Try at first the old binding */
373 goto again;
375 else if ((flags & FOLLOW_PATH)
376 && NIS_RES_STATUS (res) == NIS_PARTIAL)
378 clnt_status = __follow_path (&tablepath, &tableptr, ibreq,
379 &bptr);
380 if (clnt_status != NIS_SUCCESS)
382 NIS_RES_STATUS (res) = clnt_status;
383 ++done;
385 else
387 /* The following is a non-obvious optimization. A
388 nis_freeresult call would call xdr_free as the
389 following code. But it also would unnecessarily
390 free the result structure. We avoid this here
391 along with the necessary tests. */
392 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
393 memset (res, '\0', sizeof (*res));
394 first_try = 1;
395 goto again;
398 else if ((flags & (FOLLOW_PATH | ALL_RESULTS))
399 == (FOLLOW_PATH | ALL_RESULTS))
401 if (allres == NULL)
403 allres = res;
404 res = malloc (sizeof (nis_result));
405 if (res == NULL)
407 res = allres;
408 allres = NULL;
409 NIS_RES_STATUS (res) = NIS_NOMEMORY;
410 goto fail;
412 NIS_RES_STATUS (res) = NIS_RES_STATUS (allres);
414 else
416 nis_object *objects_val
417 = realloc (NIS_RES_OBJECT (allres),
418 (NIS_RES_NUMOBJ (allres)
419 + NIS_RES_NUMOBJ (res))
420 * sizeof (nis_object));
421 if (objects_val == NULL)
423 NIS_RES_STATUS (res) = NIS_NOMEMORY;
424 goto fail;
426 NIS_RES_OBJECT (allres) = objects_val;
427 memcpy (NIS_RES_OBJECT (allres) + NIS_RES_NUMOBJ (allres),
428 NIS_RES_OBJECT (res),
429 NIS_RES_NUMOBJ (res) * sizeof (nis_object));
430 NIS_RES_NUMOBJ (allres) += NIS_RES_NUMOBJ (res);
431 NIS_RES_NUMOBJ (res) = 0;
432 free (NIS_RES_OBJECT (res));
433 NIS_RES_OBJECT (res) = NULL;
434 NIS_RES_STATUS (allres) = NIS_RES_STATUS (res);
435 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
437 clnt_status = __follow_path (&tablepath, &tableptr, ibreq,
438 &bptr);
439 if (clnt_status != NIS_SUCCESS)
441 /* Prepare for the nis_freeresult call. */
442 memset (res, '\0', sizeof (*res));
444 if (clnt_status == NIS_NOMEMORY)
445 NIS_RES_STATUS (allres) = clnt_status;
446 ++done;
449 else
450 ++done;
451 break;
452 case NIS_CBRESULTS:
453 if (cb != NULL)
455 __nis_do_callback (&bptr, &res->cookie, cb);
456 NIS_RES_STATUS (res) = cb->result;
458 if (!(flags & ALL_RESULTS))
459 ++done;
460 else
462 NIS_RES_STATUS (res)
463 = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
464 if (NIS_RES_STATUS (res) != NIS_SUCCESS)
465 ++done;
468 break;
469 case NIS_SYSTEMERROR:
470 case NIS_NOSUCHNAME:
471 case NIS_NOT_ME:
472 /* If we had first tried the old binding, do nothing, but
473 get a new binding */
474 if (!first_try)
476 if (__nisbind_next (&bptr) != NIS_SUCCESS)
478 ++done;
479 break; /* No more servers to search */
481 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
483 if (__nisbind_next (&bptr) != NIS_SUCCESS)
485 ++done;
486 break; /* No more servers to search */
489 goto again;
491 break;
492 default:
493 if (!first_try)
495 /* Try the next domainname if we don't follow a link. */
496 free (ibreq->ibr_name);
497 ibreq->ibr_name = NULL;
498 if (__builtin_expect (count_links, 0))
500 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
501 ++done;
502 break;
504 ++name_nr;
505 if (names[name_nr] == NULL)
507 ++done;
508 break;
510 ibreq->ibr_name = strdup (names[name_nr]);
511 if (ibreq->ibr_name == NULL)
513 NIS_RES_STATUS (res) = NIS_NOMEMORY;
514 goto fail;
516 first_try = 1; /* Try old binding at first */
517 goto again;
519 break;
521 first_try = 0;
523 if (cb)
525 __nis_destroy_callback (cb);
526 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
527 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
528 cb = NULL;
531 __nisbind_destroy (&bptr);
532 nis_free_directory (dir);
535 free (tablepath);
537 if (names != namebuf)
538 nis_freenames (names);
540 nis_free_request (ibreq);
542 if (allres)
544 nis_freeresult (res);
545 return allres;
548 return res;
550 libnsl_hidden_def (nis_list)
552 nis_result *
553 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
555 nis_result *res = calloc (1, sizeof (nis_result));
556 if (res == NULL)
557 return NULL;
559 if (name == NULL)
561 NIS_RES_STATUS (res) = NIS_BADNAME;
562 return res;
565 ib_request *ibreq = __create_ib_request (name, flags);
566 if (ibreq == NULL)
568 NIS_RES_STATUS (res) = NIS_BADNAME;
569 return res;
572 nis_object obj;
573 memcpy (&obj, obj2, sizeof (nis_object));
575 size_t namelen = strlen (name);
576 char buf1[namelen + 20];
577 char buf4[namelen + 20];
579 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
580 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
582 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
583 obj.zo_owner = nis_local_principal ();
585 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
586 obj.zo_group = nis_local_group ();
588 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
590 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
591 if (ibreq->ibr_obj.ibr_obj_val == NULL)
593 nis_free_request (ibreq);
594 NIS_RES_STATUS (res) = NIS_NOMEMORY;
595 return res;
597 ibreq->ibr_obj.ibr_obj_len = 1;
599 nis_error status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
600 (xdrproc_t) _xdr_ib_request,
601 (caddr_t) ibreq,
602 (xdrproc_t) _xdr_nis_result,
603 (caddr_t) res, 0, NULL);
604 if (__builtin_expect (status != NIS_SUCCESS, 0))
605 NIS_RES_STATUS (res) = status;
607 nis_free_request (ibreq);
609 return res;
612 nis_result *
613 nis_modify_entry (const_nis_name name, const nis_object *obj2,
614 unsigned int flags)
616 nis_object obj;
617 nis_result *res;
618 nis_error status;
619 ib_request *ibreq;
620 size_t namelen = strlen (name);
621 char buf1[namelen + 20];
622 char buf4[namelen + 20];
624 res = calloc (1, sizeof (nis_result));
625 if (res == NULL)
626 return NULL;
628 ibreq = __create_ib_request (name, flags);
629 if (ibreq == NULL)
631 NIS_RES_STATUS (res) = NIS_BADNAME;
632 return res;
635 memcpy (&obj, obj2, sizeof (nis_object));
637 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
638 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
640 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
641 obj.zo_owner = nis_local_principal ();
643 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
644 obj.zo_group = nis_local_group ();
646 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
648 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
649 if (ibreq->ibr_obj.ibr_obj_val == NULL)
651 nis_free_request (ibreq);
652 NIS_RES_STATUS (res) = NIS_NOMEMORY;
653 return res;
655 ibreq->ibr_obj.ibr_obj_len = 1;
657 status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
658 (xdrproc_t) _xdr_ib_request,
659 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
660 (caddr_t) res, 0, NULL);
661 if (__builtin_expect (status != NIS_SUCCESS, 0))
662 NIS_RES_STATUS (res) = status;
664 nis_free_request (ibreq);
666 return res;
669 nis_result *
670 nis_remove_entry (const_nis_name name, const nis_object *obj,
671 unsigned int flags)
673 nis_result *res;
674 ib_request *ibreq;
675 nis_error status;
677 res = calloc (1, sizeof (nis_result));
678 if (res == NULL)
679 return NULL;
681 if (name == NULL)
683 NIS_RES_STATUS (res) = NIS_BADNAME;
684 return res;
687 ibreq = __create_ib_request (name, flags);
688 if (ibreq == NULL)
690 NIS_RES_STATUS (res) = NIS_BADNAME;
691 return res;
694 if (obj != NULL)
696 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
697 if (ibreq->ibr_obj.ibr_obj_val == NULL)
699 nis_free_request (ibreq);
700 NIS_RES_STATUS (res) = NIS_NOMEMORY;
701 return res;
703 ibreq->ibr_obj.ibr_obj_len = 1;
706 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
707 (xdrproc_t) _xdr_ib_request,
708 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
709 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
710 NIS_RES_STATUS (res) = status;
712 nis_free_request (ibreq);
714 return res;
717 nis_result *
718 nis_first_entry (const_nis_name name)
720 nis_result *res;
721 ib_request *ibreq;
722 nis_error status;
724 res = calloc (1, sizeof (nis_result));
725 if (res == NULL)
726 return NULL;
728 if (name == NULL)
730 NIS_RES_STATUS (res) = NIS_BADNAME;
731 return res;
734 ibreq = __create_ib_request (name, 0);
735 if (ibreq == NULL)
737 NIS_RES_STATUS (res) = NIS_BADNAME;
738 return res;
741 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
742 (xdrproc_t) _xdr_ib_request,
743 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
744 (caddr_t) res, 0, NULL);
746 if (__builtin_expect (status != NIS_SUCCESS, 0))
747 NIS_RES_STATUS (res) = status;
749 nis_free_request (ibreq);
751 return res;
754 nis_result *
755 nis_next_entry (const_nis_name name, const netobj *cookie)
757 nis_result *res;
758 ib_request *ibreq;
759 nis_error status;
761 res = calloc (1, sizeof (nis_result));
762 if (res == NULL)
763 return NULL;
765 if (name == NULL)
767 NIS_RES_STATUS (res) = NIS_BADNAME;
768 return res;
771 ibreq = __create_ib_request (name, 0);
772 if (ibreq == NULL)
774 NIS_RES_STATUS (res) = NIS_BADNAME;
775 return res;
778 if (cookie != NULL)
780 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
781 ibreq->ibr_cookie.n_len = cookie->n_len;
784 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
785 (xdrproc_t) _xdr_ib_request,
786 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
787 (caddr_t) res, 0, NULL);
789 if (__builtin_expect (status != NIS_SUCCESS, 0))
790 NIS_RES_STATUS (res) = status;
792 if (cookie != NULL)
794 /* Don't give cookie free, it is not from us */
795 ibreq->ibr_cookie.n_bytes = NULL;
796 ibreq->ibr_cookie.n_len = 0;
799 nis_free_request (ibreq);
801 return res;