* nscd/mem.c (mempool_alloc): Round array size to 16 bytes
[glibc.git] / nis / nis_table.c
bloba92a4b64ce541042c4700013a7acdda2f83ea77c
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 ? */
219 if (res == NULL)
220 return NULL;
222 if (name == NULL)
224 status = NIS_BADNAME;
225 err_out:
226 memset (res, '\0', sizeof (nis_result));
227 NIS_RES_STATUS (res) = status;
228 return res;
231 ibreq = __create_ib_request (name, flags);
232 if (ibreq == NULL)
234 status = NIS_BADNAME;
235 goto err_out;
238 if ((flags & EXPAND_NAME)
239 && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
241 names = nis_getnames (ibreq->ibr_name);
242 free (ibreq->ibr_name);
243 ibreq->ibr_name = NULL;
244 if (names == NULL)
246 nis_free_request (ibreq);
247 status = NIS_BADNAME;
248 goto err_out;
250 ibreq->ibr_name = strdup (names[name_nr]);
251 if (ibreq->ibr_name == NULL)
253 nis_freenames (names);
254 nis_free_request (ibreq);
255 status = NIS_NOMEMORY;
256 goto err_out;
259 else
261 names = namebuf;
262 names[name_nr] = ibreq->ibr_name;
265 cb = NULL;
267 while (!done)
269 dir_binding bptr;
270 directory_obj *dir = NULL;
272 memset (res, '\0', sizeof (nis_result));
274 status = __nisfind_server (ibreq->ibr_name,
275 ibreq->ibr_srch.ibr_srch_val != NULL, &dir);
276 if (status != NIS_SUCCESS)
278 NIS_RES_STATUS (res) = status;
279 goto fail3;
282 status = __nisbind_create (&bptr, dir->do_servers.do_servers_val,
283 dir->do_servers.do_servers_len, flags);
284 if (__builtin_expect (status != NIS_SUCCESS, 0))
286 NIS_RES_STATUS (res) = status;
287 goto fail2;
290 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
291 if (__builtin_expect (__nisbind_next (&bptr) != NIS_SUCCESS, 0))
293 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
294 goto fail;
297 if (callback != NULL)
299 assert (cb == NULL);
300 cb = __nis_create_callback (callback, userdata, flags);
301 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
302 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
305 again:
306 clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
307 (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
308 (xdrproc_t) _xdr_nis_result,
309 (caddr_t) res, RPCTIMEOUT);
311 if (__builtin_expect (clnt_status != RPC_SUCCESS, 0))
312 NIS_RES_STATUS (res) = NIS_RPCERROR;
313 else
314 switch (NIS_RES_STATUS (res))
315 { /* start switch */
316 case NIS_PARTIAL:
317 case NIS_SUCCESS:
318 case NIS_S_SUCCESS:
319 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
320 && (flags & FOLLOW_LINKS)) /* We are following links. */
322 free (ibreq->ibr_name);
323 ibreq->ibr_name = NULL;
324 /* If we hit the link limit, bail. */
325 if (__builtin_expect (count_links > NIS_MAXLINKS, 0))
327 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
328 ++done;
329 break;
331 ++count_links;
332 ibreq->ibr_name =
333 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
334 if (ibreq->ibr_name == NULL)
336 NIS_RES_STATUS (res) = NIS_NOMEMORY;
337 fail:
338 __nisbind_destroy (&bptr);
339 fail2:
340 nis_free_directory (dir);
341 fail3:
342 free (tablepath);
343 if (cb)
345 __nis_destroy_callback (cb);
346 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
347 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
349 if (names != namebuf)
350 nis_freenames (names);
351 nis_free_request (ibreq);
352 return res;
354 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
355 if (ibreq->ibr_srch.ibr_srch_len == 0)
357 ibreq->ibr_srch.ibr_srch_len =
358 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
359 ibreq->ibr_srch.ibr_srch_val =
360 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
362 /* The following is a non-obvious optimization. A
363 nis_freeresult call would call xdr_free as the
364 following code. But it also would unnecessarily
365 free the result structure. We avoid this here
366 along with the necessary tests. */
367 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
368 memset (res, '\0', sizeof (*res));
369 first_try = 1; /* Try at first the old binding */
370 goto again;
372 else if ((flags & FOLLOW_PATH)
373 && NIS_RES_STATUS (res) == NIS_PARTIAL)
375 clnt_status = __follow_path (&tablepath, &tableptr, ibreq,
376 &bptr);
377 if (clnt_status != NIS_SUCCESS)
379 NIS_RES_STATUS (res) = clnt_status;
380 ++done;
382 else
384 /* The following is a non-obvious optimization. A
385 nis_freeresult call would call xdr_free as the
386 following code. But it also would unnecessarily
387 free the result structure. We avoid this here
388 along with the necessary tests. */
389 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
390 memset (res, '\0', sizeof (*res));
391 first_try = 1;
392 goto again;
395 else
396 ++done;
397 break;
398 case NIS_CBRESULTS:
399 if (cb != NULL)
401 __nis_do_callback (&bptr, &res->cookie, cb);
402 NIS_RES_STATUS (res) = cb->result;
404 if (!(flags & ALL_RESULTS))
405 ++done;
406 else
408 NIS_RES_STATUS (res)
409 = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
410 if (NIS_RES_STATUS (res) != NIS_SUCCESS)
411 ++done;
414 break;
415 case NIS_SYSTEMERROR:
416 case NIS_NOSUCHNAME:
417 case NIS_NOT_ME:
418 /* If we had first tried the old binding, do nothing, but
419 get a new binding */
420 if (!first_try)
422 if (__nisbind_next (&bptr) != NIS_SUCCESS)
424 ++done;
425 break; /* No more servers to search */
427 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
429 if (__nisbind_next (&bptr) != NIS_SUCCESS)
431 ++done;
432 break; /* No more servers to search */
435 goto again;
437 break;
438 default:
439 if (!first_try)
441 /* Try the next domainname if we don't follow a link. */
442 free (ibreq->ibr_name);
443 ibreq->ibr_name = NULL;
444 if (__builtin_expect (count_links, 0))
446 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
447 ++done;
448 break;
450 ++name_nr;
451 if (names[name_nr] == NULL)
453 ++done;
454 break;
456 ibreq->ibr_name = strdup (names[name_nr]);
457 if (ibreq->ibr_name == NULL)
459 NIS_RES_STATUS (res) = NIS_NOMEMORY;
460 goto fail;
462 first_try = 1; /* Try old binding at first */
463 goto again;
465 break;
467 first_try = 0;
469 if (cb)
471 __nis_destroy_callback (cb);
472 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
473 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
474 cb = NULL;
477 __nisbind_destroy (&bptr);
478 nis_free_directory (dir);
481 free (tablepath);
483 if (names != namebuf)
484 nis_freenames (names);
486 nis_free_request (ibreq);
488 return res;
490 libnsl_hidden_def (nis_list)
492 nis_result *
493 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
495 nis_result *res = calloc (1, sizeof (nis_result));
496 if (res == NULL)
497 return NULL;
499 if (name == NULL)
501 NIS_RES_STATUS (res) = NIS_BADNAME;
502 return res;
505 ib_request *ibreq = __create_ib_request (name, flags);
506 if (ibreq == NULL)
508 NIS_RES_STATUS (res) = NIS_BADNAME;
509 return res;
512 nis_object obj;
513 memcpy (&obj, obj2, sizeof (nis_object));
515 size_t namelen = strlen (name);
516 char buf1[namelen + 20];
517 char buf4[namelen + 20];
519 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
520 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
522 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
523 obj.zo_owner = nis_local_principal ();
525 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
526 obj.zo_group = nis_local_group ();
528 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
530 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
531 if (ibreq->ibr_obj.ibr_obj_val == NULL)
533 nis_free_request (ibreq);
534 NIS_RES_STATUS (res) = NIS_NOMEMORY;
535 return res;
537 ibreq->ibr_obj.ibr_obj_len = 1;
539 nis_error status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
540 (xdrproc_t) _xdr_ib_request,
541 (caddr_t) ibreq,
542 (xdrproc_t) _xdr_nis_result,
543 (caddr_t) res, 0, NULL);
544 if (__builtin_expect (status != NIS_SUCCESS, 0))
545 NIS_RES_STATUS (res) = status;
547 nis_free_request (ibreq);
549 return res;
552 nis_result *
553 nis_modify_entry (const_nis_name name, const nis_object *obj2,
554 unsigned int flags)
556 nis_object obj;
557 nis_result *res;
558 nis_error status;
559 ib_request *ibreq;
560 size_t namelen = strlen (name);
561 char buf1[namelen + 20];
562 char buf4[namelen + 20];
564 res = calloc (1, sizeof (nis_result));
565 if (res == NULL)
566 return NULL;
568 ibreq = __create_ib_request (name, flags);
569 if (ibreq == NULL)
571 NIS_RES_STATUS (res) = NIS_BADNAME;
572 return res;
575 memcpy (&obj, obj2, sizeof (nis_object));
577 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
578 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
580 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
581 obj.zo_owner = nis_local_principal ();
583 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
584 obj.zo_group = nis_local_group ();
586 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
588 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
589 if (ibreq->ibr_obj.ibr_obj_val == NULL)
591 nis_free_request (ibreq);
592 NIS_RES_STATUS (res) = NIS_NOMEMORY;
593 return res;
595 ibreq->ibr_obj.ibr_obj_len = 1;
597 status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
598 (xdrproc_t) _xdr_ib_request,
599 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
600 (caddr_t) res, 0, NULL);
601 if (__builtin_expect (status != NIS_SUCCESS, 0))
602 NIS_RES_STATUS (res) = status;
604 nis_free_request (ibreq);
606 return res;
609 nis_result *
610 nis_remove_entry (const_nis_name name, const nis_object *obj,
611 unsigned int flags)
613 nis_result *res;
614 ib_request *ibreq;
615 nis_error status;
617 res = calloc (1, sizeof (nis_result));
618 if (res == NULL)
619 return NULL;
621 if (name == NULL)
623 NIS_RES_STATUS (res) = NIS_BADNAME;
624 return res;
627 ibreq = __create_ib_request (name, flags);
628 if (ibreq == NULL)
630 NIS_RES_STATUS (res) = NIS_BADNAME;
631 return res;
634 if (obj != NULL)
636 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
637 if (ibreq->ibr_obj.ibr_obj_val == NULL)
639 nis_free_request (ibreq);
640 NIS_RES_STATUS (res) = NIS_NOMEMORY;
641 return res;
643 ibreq->ibr_obj.ibr_obj_len = 1;
646 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
647 (xdrproc_t) _xdr_ib_request,
648 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
649 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
650 NIS_RES_STATUS (res) = status;
652 nis_free_request (ibreq);
654 return res;
657 nis_result *
658 nis_first_entry (const_nis_name name)
660 nis_result *res;
661 ib_request *ibreq;
662 nis_error status;
664 res = calloc (1, sizeof (nis_result));
665 if (res == NULL)
666 return NULL;
668 if (name == NULL)
670 NIS_RES_STATUS (res) = NIS_BADNAME;
671 return res;
674 ibreq = __create_ib_request (name, 0);
675 if (ibreq == NULL)
677 NIS_RES_STATUS (res) = NIS_BADNAME;
678 return res;
681 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
682 (xdrproc_t) _xdr_ib_request,
683 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
684 (caddr_t) res, 0, NULL);
686 if (__builtin_expect (status != NIS_SUCCESS, 0))
687 NIS_RES_STATUS (res) = status;
689 nis_free_request (ibreq);
691 return res;
694 nis_result *
695 nis_next_entry (const_nis_name name, const netobj *cookie)
697 nis_result *res;
698 ib_request *ibreq;
699 nis_error status;
701 res = calloc (1, sizeof (nis_result));
702 if (res == NULL)
703 return NULL;
705 if (name == NULL)
707 NIS_RES_STATUS (res) = NIS_BADNAME;
708 return res;
711 ibreq = __create_ib_request (name, 0);
712 if (ibreq == NULL)
714 NIS_RES_STATUS (res) = NIS_BADNAME;
715 return res;
718 if (cookie != NULL)
720 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
721 ibreq->ibr_cookie.n_len = cookie->n_len;
724 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
725 (xdrproc_t) _xdr_ib_request,
726 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
727 (caddr_t) res, 0, NULL);
729 if (__builtin_expect (status != NIS_SUCCESS, 0))
730 NIS_RES_STATUS (res) = status;
732 if (cookie != NULL)
734 /* Don't give cookie free, it is not from us */
735 ibreq->ibr_cookie.n_bytes = NULL;
736 ibreq->ibr_cookie.n_len = 0;
739 nis_free_request (ibreq);
741 return res;