(timeout_handler): Rewrite ts initialization for C++.
[glibc.git] / nis / nis_table.c
blob2ef28ac59b769fca0acd3db6d2bf4b3b61d69352
1 /* Copyright (c) 1997,1998,1999,2003,2004,2005 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 <string.h>
21 #include <rpcsvc/nis.h>
23 #include "nis_xdr.h"
24 #include "nis_intern.h"
27 static struct ib_request *
28 __create_ib_request (const_nis_name name, unsigned int flags)
30 struct ib_request *ibreq = calloc (1, sizeof (struct ib_request));
31 nis_attr *search_val = NULL;
32 size_t search_len = 0;
33 size_t size = 0;
35 if (ibreq == NULL)
36 return NULL;
38 ibreq->ibr_flags = flags;
40 char *cptr = strdupa (name);
42 /* Not of "[key=value,key=value,...],foo.." format? */
43 if (cptr[0] != '[')
44 return (ibreq->ibr_name = strdup (cptr)) == NULL ? NULL : ibreq;
46 /* "[key=value,...],foo" format */
47 ibreq->ibr_name = strchr (cptr, ']');
48 if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
50 /* The object has not really been built yet so we use free. */
51 free (ibreq);
52 return NULL;
55 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
56 if (ibreq->ibr_name[-1] == ',')
57 ibreq->ibr_name[-1] = '\0';
58 else
59 ibreq->ibr_name[0] = '\0';
60 ibreq->ibr_name += 2;
61 ibreq->ibr_name = strdup (ibreq->ibr_name);
62 if (ibreq->ibr_name == NULL)
64 free_null:
65 while (search_len-- > 0)
67 free (search_val[search_len].zattr_ndx);
68 free (search_val[search_len].zattr_val.zattr_val_val);
70 free (search_val);
71 nis_free_request (ibreq);
72 return NULL;
75 ++cptr; /* Remove "[" */
77 while (cptr != NULL && cptr[0] != '\0')
79 char *key = cptr;
80 char *val = strchr (cptr, '=');
82 cptr = strchr (key, ',');
83 if (cptr != NULL)
84 *cptr++ = '\0';
86 if (!val)
88 nis_free_request (ibreq);
89 return NULL;
91 *val++ = '\0';
92 if ((search_len + 1) >= size)
94 size += 1;
95 nis_attr *newp = realloc (search_val, size * sizeof (nis_attr));
96 if (newp == NULL)
97 goto free_null;
98 search_val = newp;
100 search_val[search_len].zattr_ndx = strdup (key);
101 if ((search_val[search_len].zattr_ndx) == NULL)
102 goto free_null;
104 search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1;
105 search_val[search_len].zattr_val.zattr_val_val = strdup (val);
106 if (search_val[search_len].zattr_val.zattr_val_val == NULL)
108 free (search_val[search_len].zattr_ndx);
109 goto free_null;
112 ++search_len;
115 ibreq->ibr_srch.ibr_srch_val = search_val;
116 ibreq->ibr_srch.ibr_srch_len = search_len;
118 return ibreq;
121 static const struct timeval RPCTIMEOUT = {10, 0};
123 static char *
124 __get_tablepath (char *name, dir_binding *bptr)
126 enum clnt_stat result;
127 nis_result res;
128 struct ns_request req;
130 memset (&res, '\0', sizeof (res));
132 req.ns_name = name;
133 req.ns_object.ns_object_len = 0;
134 req.ns_object.ns_object_val = NULL;
136 result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request,
137 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
138 (caddr_t) &res, RPCTIMEOUT);
140 const char *cptr;
141 if (result == RPC_SUCCESS && NIS_RES_STATUS (&res) == NIS_SUCCESS
142 && __type_of (NIS_RES_OBJECT (&res)) == NIS_TABLE_OBJ)
143 cptr = NIS_RES_OBJECT (&res)->TA_data.ta_path;
144 else
145 cptr = "";
147 return strdup (cptr);
150 nis_result *
151 nis_list (const_nis_name name, unsigned int flags,
152 int (*callback) (const_nis_name name,
153 const nis_object *object,
154 const void *userdata),
155 const void *userdata)
157 nis_result *res = calloc (1, sizeof (nis_result));
158 ib_request *ibreq;
159 int status;
160 enum clnt_stat clnt_status;
161 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
162 int done = 0;
163 nis_name *names;
164 nis_name namebuf[2] = {NULL, NULL};
165 int name_nr = 0;
166 nis_cb *cb = NULL;
167 char *tableptr;
168 char *tablepath = NULL;
169 int first_try = 0; /* Do we try the old binding at first ? */
171 if (res == NULL)
172 return NULL;
174 if (name == NULL)
176 NIS_RES_STATUS (res) = NIS_BADNAME;
177 return res;
180 if ((ibreq = __create_ib_request (name, flags)) == NULL)
182 NIS_RES_STATUS (res) = NIS_BADNAME;
183 return res;
186 if ((flags & EXPAND_NAME)
187 && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
189 names = nis_getnames (ibreq->ibr_name);
190 free (ibreq->ibr_name);
191 ibreq->ibr_name = NULL;
192 if (names == NULL)
194 nis_free_request (ibreq);
195 NIS_RES_STATUS (res) = NIS_BADNAME;
196 return res;
198 ibreq->ibr_name = strdup (names[name_nr]);
199 if (ibreq->ibr_name == NULL)
201 nis_freenames (names);
202 nis_free_request (ibreq);
203 NIS_RES_STATUS (res) = NIS_NOMEMORY;
204 return res;
207 else
209 names = namebuf;
210 names[name_nr] = ibreq->ibr_name;
213 cb = NULL;
215 while (!done)
217 dir_binding bptr;
218 directory_obj *dir = NULL;
220 memset (res, '\0', sizeof (nis_result));
222 status = __nisfind_server (ibreq->ibr_name, &dir);
223 if (status != NIS_SUCCESS)
225 NIS_RES_STATUS (res) = status;
226 goto fail3;
229 status = __nisbind_create (&bptr, dir->do_servers.do_servers_val,
230 dir->do_servers.do_servers_len, flags);
231 if (status != NIS_SUCCESS)
233 NIS_RES_STATUS (res) = status;
234 goto fail2;
237 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
238 if (__nisbind_next (&bptr) != NIS_SUCCESS)
240 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
241 goto fail;
244 if (callback != NULL)
246 cb = __nis_create_callback (callback, userdata, flags);
247 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
248 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
251 again:
252 clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
253 (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
254 (xdrproc_t) _xdr_nis_result,
255 (caddr_t) res, RPCTIMEOUT);
257 if (clnt_status != RPC_SUCCESS)
258 NIS_RES_STATUS (res) = NIS_RPCERROR;
259 else
260 switch (NIS_RES_STATUS (res))
261 { /* start switch */
262 case NIS_PARTIAL:
263 case NIS_SUCCESS:
264 case NIS_S_SUCCESS:
265 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
266 && (flags & FOLLOW_LINKS)) /* We are following links. */
268 free (ibreq->ibr_name);
269 ibreq->ibr_name = NULL;
270 /* If we hit the link limit, bail. */
271 if (count_links > NIS_MAXLINKS)
273 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
274 ++done;
275 break;
277 ++count_links;
278 ibreq->ibr_name =
279 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
280 if (ibreq->ibr_name == NULL)
282 NIS_RES_STATUS (res) = NIS_NOMEMORY;
283 fail:
284 __nisbind_destroy (&bptr);
285 fail2:
286 nis_free_directory (dir);
287 fail3:
288 free (tablepath);
289 if (cb)
291 __nis_destroy_callback (cb);
292 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
293 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
295 if (names != namebuf)
296 nis_freenames (names);
297 nis_free_request (ibreq);
298 return res;
300 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
301 if (ibreq->ibr_srch.ibr_srch_len == 0)
303 ibreq->ibr_srch.ibr_srch_len =
304 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
305 ibreq->ibr_srch.ibr_srch_val =
306 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
308 /* The following is a non-obvious optimization. A
309 nis_freeresult call would call xdr_free as the
310 following code. But it also would unnecessarily
311 free the result structure. We avoid this here
312 along with the necessary tests. */
313 #if 1
314 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
315 memset (res, '\0', sizeof (*res));
316 #else
317 nis_freeresult (res);
318 res = calloc (1, sizeof (nis_result));
319 if (res == NULL)
320 goto fail;
321 #endif
322 first_try = 1; /* Try at first the old binding */
323 goto again;
325 else if ((flags & FOLLOW_PATH)
326 && NIS_RES_STATUS (res) == NIS_PARTIAL)
328 if (tablepath == NULL)
330 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
331 tableptr = tablepath;
333 if (tableptr == NULL)
335 ++done;
336 break;
338 free (ibreq->ibr_name);
339 ibreq->ibr_name = strsep (&tableptr, ":");
340 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
342 ibreq->ibr_name = strdup ("");
343 if (ibreq->ibr_name == NULL)
345 NIS_RES_STATUS (res) = NIS_NOMEMORY;
346 goto fail;
348 ++done;
350 else
352 ibreq->ibr_name = strdup (ibreq->ibr_name);
353 /* The following is a non-obvious optimization. A
354 nis_freeresult call would call xdr_free as the
355 following code. But it also would unnecessarily
356 free the result structure. We avoid this here
357 along with the necessary tests. */
358 #if 1
359 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
360 memset (res, '\0', sizeof (*res));
361 if (ibreq->ibr_name == NULL)
363 NIS_RES_STATUS (res) = NIS_NOMEMORY;
364 goto fail;
366 #else
367 nis_freeresult (res);
368 res = calloc (1, sizeof (nis_result));
369 if (res == NULL || ibreq->ibr_name == NULL)
371 free (res);
372 res = NULL;
373 goto fail;
375 #endif
376 first_try = 1;
377 goto again;
380 else
381 ++done;
382 break;
383 case NIS_CBRESULTS:
384 if (cb != NULL)
386 __nis_do_callback (&bptr, &res->cookie, cb);
387 NIS_RES_STATUS (res) = cb->result;
389 if (!(flags & ALL_RESULTS))
390 ++done;
391 else
393 if (tablepath == NULL)
395 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
396 tableptr = tablepath;
398 if (tableptr == NULL)
400 ++done;
401 break;
403 free (ibreq->ibr_name);
404 ibreq->ibr_name = strsep (&tableptr, ":");
405 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
407 ibreq->ibr_name = strdup ("");
408 ++done;
410 else
411 ibreq->ibr_name = strdup (ibreq->ibr_name);
412 if (ibreq->ibr_name == NULL)
414 NIS_RES_STATUS (res) = NIS_NOMEMORY;
415 goto fail;
419 break;
420 case NIS_SYSTEMERROR:
421 case NIS_NOSUCHNAME:
422 case NIS_NOT_ME:
423 /* If we had first tried the old binding, do nothing, but
424 get a new binding */
425 if (!first_try)
427 if (__nisbind_next (&bptr) != NIS_SUCCESS)
429 ++done;
430 break; /* No more servers to search */
432 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
434 if (__nisbind_next (&bptr) != NIS_SUCCESS)
436 ++done;
437 break; /* No more servers to search */
440 goto again;
442 break;
443 default:
444 if (!first_try)
446 /* Try the next domainname if we don't follow a link. */
447 free (ibreq->ibr_name);
448 ibreq->ibr_name = NULL;
449 if (count_links)
451 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
452 ++done;
453 break;
455 ++name_nr;
456 if (names[name_nr] == NULL)
458 ++done;
459 break;
461 ibreq->ibr_name = strdup (names[name_nr]);
462 if (ibreq->ibr_name == NULL)
464 NIS_RES_STATUS (res) = NIS_NOMEMORY;
465 goto fail;
467 first_try = 1; /* Try old binding at first */
468 goto again;
470 break;
472 first_try = 0;
474 if (cb)
476 __nis_destroy_callback (cb);
477 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
478 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
479 cb = NULL;
482 __nisbind_destroy (&bptr);
483 nis_free_directory (dir);
486 free (tablepath);
488 if (names != namebuf)
489 nis_freenames (names);
491 nis_free_request (ibreq);
493 return res;
495 libnsl_hidden_def (nis_list)
497 nis_result *
498 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
500 nis_object obj;
501 nis_result *res;
502 nis_error status;
503 ib_request *ibreq;
504 size_t namelen = strlen (name);
505 char buf1[namelen + 20];
506 char buf4[namelen + 20];
508 res = calloc (1, sizeof (nis_result));
509 if (res == NULL)
510 return NULL;
512 if (name == NULL)
514 NIS_RES_STATUS (res) = NIS_BADNAME;
515 return res;
518 if ((ibreq = __create_ib_request (name, flags)) == NULL)
520 NIS_RES_STATUS (res) = NIS_BADNAME;
521 return res;
524 memcpy (&obj, obj2, sizeof (nis_object));
526 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
527 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
529 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
530 obj.zo_owner = nis_local_principal ();
532 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
533 obj.zo_group = nis_local_group ();
535 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
537 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
538 if (ibreq->ibr_obj.ibr_obj_val == NULL)
540 nis_free_request (ibreq);
541 NIS_RES_STATUS (res) = NIS_NOMEMORY;
542 return res;
544 ibreq->ibr_obj.ibr_obj_len = 1;
546 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
547 (xdrproc_t) _xdr_ib_request,
548 (caddr_t) ibreq,
549 (xdrproc_t) _xdr_nis_result,
550 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
551 NIS_RES_STATUS (res) = status;
553 nis_free_request (ibreq);
555 return res;
558 nis_result *
559 nis_modify_entry (const_nis_name name, const nis_object *obj2,
560 unsigned int flags)
562 nis_object obj;
563 nis_result *res;
564 nis_error status;
565 ib_request *ibreq;
566 size_t namelen = strlen (name);
567 char buf1[namelen + 20];
568 char buf4[namelen + 20];
570 res = calloc (1, sizeof (nis_result));
571 if (res == NULL)
572 return NULL;
574 if (( ibreq =__create_ib_request (name, flags)) == NULL)
576 NIS_RES_STATUS (res) = NIS_BADNAME;
577 return res;
580 memcpy (&obj, obj2, sizeof (nis_object));
582 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
583 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
585 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
586 obj.zo_owner = nis_local_principal ();
588 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
589 obj.zo_group = nis_local_group ();
591 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
593 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
594 if (ibreq->ibr_obj.ibr_obj_val == NULL)
596 nis_free_request (ibreq);
597 NIS_RES_STATUS (res) = NIS_NOMEMORY;
598 return res;
600 ibreq->ibr_obj.ibr_obj_len = 1;
602 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
603 (xdrproc_t) _xdr_ib_request,
604 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
605 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
606 NIS_RES_STATUS (res) = status;
608 nis_free_request (ibreq);
610 return res;
613 nis_result *
614 nis_remove_entry (const_nis_name name, const nis_object *obj,
615 unsigned int flags)
617 nis_result *res;
618 ib_request *ibreq;
619 nis_error status;
621 res = calloc (1, sizeof (nis_result));
622 if (res == NULL)
623 return NULL;
625 if (name == NULL)
627 NIS_RES_STATUS (res) = NIS_BADNAME;
628 return res;
631 if ((ibreq =__create_ib_request (name, flags)) == NULL)
633 NIS_RES_STATUS (res) = NIS_BADNAME;
634 return res;
637 if (obj != NULL)
639 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
640 if (ibreq->ibr_obj.ibr_obj_val == NULL)
642 nis_free_request (ibreq);
643 NIS_RES_STATUS (res) = NIS_NOMEMORY;
644 return res;
646 ibreq->ibr_obj.ibr_obj_len = 1;
649 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
650 (xdrproc_t) _xdr_ib_request,
651 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
652 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
653 NIS_RES_STATUS (res) = status;
655 nis_free_request (ibreq);
657 return res;
660 nis_result *
661 nis_first_entry (const_nis_name name)
663 nis_result *res;
664 ib_request *ibreq;
665 nis_error status;
667 res = calloc (1, sizeof (nis_result));
668 if (res == NULL)
669 return NULL;
671 if (name == NULL)
673 NIS_RES_STATUS (res) = NIS_BADNAME;
674 return res;
677 ibreq = __create_ib_request (name, 0);
678 if (ibreq == NULL)
680 NIS_RES_STATUS (res) = NIS_BADNAME;
681 return res;
684 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
685 (xdrproc_t) _xdr_ib_request,
686 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
687 (caddr_t) res, 0, NULL);
689 if (status != NIS_SUCCESS)
690 NIS_RES_STATUS (res) = status;
692 nis_free_request (ibreq);
694 return res;
697 nis_result *
698 nis_next_entry (const_nis_name name, const netobj *cookie)
700 nis_result *res;
701 ib_request *ibreq;
702 nis_error status;
704 res = calloc (1, sizeof (nis_result));
705 if (res == NULL)
706 return NULL;
708 if (name == NULL)
710 NIS_RES_STATUS (res) = NIS_BADNAME;
711 return res;
714 ibreq = __create_ib_request (name, 0);
715 if (ibreq == NULL)
717 NIS_RES_STATUS (res) = NIS_BADNAME;
718 return res;
721 if (cookie != NULL)
723 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
724 ibreq->ibr_cookie.n_len = cookie->n_len;
727 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
728 (xdrproc_t) _xdr_ib_request,
729 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
730 (caddr_t) res, 0, NULL);
732 if (status != NIS_SUCCESS)
733 NIS_RES_STATUS (res) = status;
735 if (cookie != NULL)
737 /* Don't give cookie free, it is not from us */
738 ibreq->ibr_cookie.n_bytes = NULL;
739 ibreq->ibr_cookie.n_len = 0;
742 nis_free_request (ibreq);
744 return res;