* nis/nis_table.c (nis_list): Optimize freeing and reallocation of
[glibc.git] / nis / nis_table.c
blob773380acac20d20a043768377c6460d4bd9da1a4
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, *tablepath = NULL;
168 int have_tablepath = 0;
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_free_request (ibreq);
202 NIS_RES_STATUS (res) = NIS_NOMEMORY;
203 return res;
206 else
208 names = namebuf;
209 names[name_nr] = ibreq->ibr_name;
212 cb = NULL;
214 while (!done)
216 dir_binding bptr;
217 directory_obj *dir = NULL;
219 memset (res, '\0', sizeof (nis_result));
221 status = __nisfind_server (ibreq->ibr_name, &dir);
222 if (status != NIS_SUCCESS)
224 nis_free_request (ibreq);
225 NIS_RES_STATUS (res) = status;
226 return res;
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_free_request (ibreq);
234 NIS_RES_STATUS (res) = status;
235 nis_free_directory (dir);
236 return res;
239 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
240 if (__nisbind_next (&bptr) != NIS_SUCCESS)
242 __nisbind_destroy (&bptr);
243 nis_free_directory (dir);
244 nis_free_request (ibreq);
245 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
246 return res;
249 if (callback != NULL)
251 cb = __nis_create_callback (callback, userdata, flags);
252 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
253 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
256 again:
257 clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
258 (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
259 (xdrproc_t) _xdr_nis_result,
260 (caddr_t) res, RPCTIMEOUT);
262 if (clnt_status != RPC_SUCCESS)
263 NIS_RES_STATUS (res) = NIS_RPCERROR;
264 else
265 switch (NIS_RES_STATUS (res))
266 { /* start switch */
267 case NIS_PARTIAL:
268 case NIS_SUCCESS:
269 case NIS_S_SUCCESS:
270 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ &&
271 flags & FOLLOW_LINKS) /* We are following links. */
273 free (ibreq->ibr_name);
274 ibreq->ibr_name = NULL;
275 /* If we hit the link limit, bail. */
276 if (count_links > NIS_MAXLINKS)
278 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
279 ++done;
280 break;
282 ++count_links;
283 ibreq->ibr_name =
284 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
285 if (ibreq->ibr_name == NULL)
287 nis_free_request (ibreq);
288 NIS_RES_STATUS (res) = NIS_NOMEMORY;
289 return res;
291 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
292 if (ibreq->ibr_srch.ibr_srch_len == 0)
294 ibreq->ibr_srch.ibr_srch_len =
295 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
296 ibreq->ibr_srch.ibr_srch_val =
297 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
299 /* The following is a non-obvious optimization. A
300 nis_freeresult call would call xdr_free as the
301 following code. But it also would unnecessarily
302 free the result structure. We avoid this here
303 along with the necessary tests. */
304 #if 1
305 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
306 memset (res, '\0', sizeof (*res));
307 #else
308 nis_freeresult (res);
309 res = calloc (1, sizeof (nis_result));
310 if (res == NULL)
312 if (have_tablepath)
313 free (tablepath);
314 __nisbind_destroy (&bptr);
315 nis_free_directory (dir);
316 return NULL;
318 #endif
319 first_try = 1; /* Try at first the old binding */
320 goto again;
322 else if ((flags & FOLLOW_PATH) &&
323 NIS_RES_STATUS (res) == NIS_PARTIAL)
325 if (!have_tablepath)
327 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
328 tableptr = tablepath;
329 have_tablepath = 1;
331 if (tableptr == NULL)
333 ++done;
334 break;
336 free (ibreq->ibr_name);
337 ibreq->ibr_name = strsep (&tableptr, ":");
338 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
340 ibreq->ibr_name = strdup ("");
341 if (ibreq->ibr_name == NULL)
343 nis_free_request (ibreq);
344 NIS_RES_STATUS (res) = NIS_NOMEMORY;
345 return res;
347 ++done;
349 else
351 ibreq->ibr_name = strdup (ibreq->ibr_name);
352 nis_freeresult (res);
353 res = calloc (1, sizeof (nis_result));
354 if (res == NULL || ibreq->ibr_name == NULL)
356 free (ibreq->ibr_name);
357 free (res);
358 nis_free_request (ibreq);
359 if (have_tablepath)
360 free (tablepath);
361 __nisbind_destroy (&bptr);
362 nis_free_directory (dir);
363 return NULL;
365 first_try = 1;
366 goto again;
369 else
370 ++done;
371 break;
372 case NIS_CBRESULTS:
373 if (cb != NULL)
375 __nis_do_callback (&bptr, &res->cookie, cb);
376 NIS_RES_STATUS (res) = cb->result;
378 if (!(flags & ALL_RESULTS))
379 ++done;
380 else
382 if (!have_tablepath)
384 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
385 tableptr = tablepath;
386 have_tablepath = 1;
388 if (tableptr == NULL)
390 ++done;
391 break;
393 free (ibreq->ibr_name);
394 ibreq->ibr_name = strsep (&tableptr, ":");
395 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
397 ibreq->ibr_name = strdup ("");
398 ++done;
400 else
401 ibreq->ibr_name = strdup (ibreq->ibr_name);
402 if (ibreq->ibr_name == NULL)
404 nis_free_request (ibreq);
405 NIS_RES_STATUS (res) = NIS_NOMEMORY;
406 return res;
410 break;
411 case NIS_SYSTEMERROR:
412 case NIS_NOSUCHNAME:
413 case NIS_NOT_ME:
414 /* If we had first tried the old binding, do nothing, but
415 get a new binding */
416 if (!first_try)
418 if (__nisbind_next (&bptr) != NIS_SUCCESS)
420 ++done;
421 break; /* No more servers to search */
423 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
425 if (__nisbind_next (&bptr) != NIS_SUCCESS)
427 ++done;
428 break; /* No more servers to search */
431 goto again;
433 break;
434 default:
435 if (!first_try)
437 /* Try the next domainname if we don't follow a link. */
438 free (ibreq->ibr_name);
439 ibreq->ibr_name = NULL;
440 if (count_links)
442 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
443 ++done;
444 break;
446 ++name_nr;
447 if (names[name_nr] == NULL)
449 ++done;
450 break;
452 ibreq->ibr_name = strdup (names[name_nr]);
453 if (ibreq->ibr_name == NULL)
455 nis_free_request (ibreq);
456 NIS_RES_STATUS (res) = NIS_NOMEMORY;
457 return res;
459 first_try = 1; /* Try old binding at first */
460 goto again;
462 break;
464 first_try = 0;
466 if (cb)
468 __nis_destroy_callback (cb);
469 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
470 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
473 __nisbind_destroy (&bptr);
474 nis_free_directory (dir);
477 if (names != namebuf)
478 nis_freenames (names);
480 nis_free_request (ibreq);
482 return res;
484 libnsl_hidden_def (nis_list)
486 nis_result *
487 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
489 nis_object obj;
490 nis_result *res;
491 nis_error status;
492 ib_request *ibreq;
493 size_t namelen = strlen (name);
494 char buf1[namelen + 20];
495 char buf4[namelen + 20];
497 res = calloc (1, sizeof (nis_result));
498 if (res == NULL)
499 return NULL;
501 if (name == NULL)
503 NIS_RES_STATUS (res) = NIS_BADNAME;
504 return res;
507 if ((ibreq = __create_ib_request (name, flags)) == NULL)
509 NIS_RES_STATUS (res) = NIS_BADNAME;
510 return res;
513 memcpy (&obj, obj2, sizeof (nis_object));
515 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
516 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
518 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
519 obj.zo_owner = nis_local_principal ();
521 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
522 obj.zo_group = nis_local_group ();
524 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
526 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
527 if (ibreq->ibr_obj.ibr_obj_val == NULL)
529 nis_free_request (ibreq);
530 NIS_RES_STATUS (res) = NIS_NOMEMORY;
531 return res;
533 ibreq->ibr_obj.ibr_obj_len = 1;
535 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
536 (xdrproc_t) _xdr_ib_request,
537 (caddr_t) ibreq,
538 (xdrproc_t) _xdr_nis_result,
539 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
540 NIS_RES_STATUS (res) = status;
542 nis_free_request (ibreq);
544 return res;
547 nis_result *
548 nis_modify_entry (const_nis_name name, const nis_object *obj2,
549 unsigned int flags)
551 nis_object obj;
552 nis_result *res;
553 nis_error status;
554 ib_request *ibreq;
555 size_t namelen = strlen (name);
556 char buf1[namelen + 20];
557 char buf4[namelen + 20];
559 res = calloc (1, sizeof (nis_result));
560 if (res == NULL)
561 return NULL;
563 if (( ibreq =__create_ib_request (name, flags)) == NULL)
565 NIS_RES_STATUS (res) = NIS_BADNAME;
566 return res;
569 memcpy (&obj, obj2, sizeof (nis_object));
571 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
572 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
574 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
575 obj.zo_owner = nis_local_principal ();
577 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
578 obj.zo_group = nis_local_group ();
580 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
582 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
583 if (ibreq->ibr_obj.ibr_obj_val == NULL)
585 nis_free_request (ibreq);
586 NIS_RES_STATUS (res) = NIS_NOMEMORY;
587 return res;
589 ibreq->ibr_obj.ibr_obj_len = 1;
591 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
592 (xdrproc_t) _xdr_ib_request,
593 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
594 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
595 NIS_RES_STATUS (res) = status;
597 nis_free_request (ibreq);
599 return res;
602 nis_result *
603 nis_remove_entry (const_nis_name name, const nis_object *obj,
604 unsigned int flags)
606 nis_result *res;
607 ib_request *ibreq;
608 nis_error status;
610 res = calloc (1, sizeof (nis_result));
611 if (res == NULL)
612 return NULL;
614 if (name == NULL)
616 NIS_RES_STATUS (res) = NIS_BADNAME;
617 return res;
620 if ((ibreq =__create_ib_request (name, flags)) == NULL)
622 NIS_RES_STATUS (res) = NIS_BADNAME;
623 return res;
626 if (obj != NULL)
628 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
629 if (ibreq->ibr_obj.ibr_obj_val == NULL)
631 nis_free_request (ibreq);
632 NIS_RES_STATUS (res) = NIS_NOMEMORY;
633 return res;
635 ibreq->ibr_obj.ibr_obj_len = 1;
638 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
639 (xdrproc_t) _xdr_ib_request,
640 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
641 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
642 NIS_RES_STATUS (res) = status;
644 nis_free_request (ibreq);
646 return res;
649 nis_result *
650 nis_first_entry (const_nis_name name)
652 nis_result *res;
653 ib_request *ibreq;
654 nis_error status;
656 res = calloc (1, sizeof (nis_result));
657 if (res == NULL)
658 return NULL;
660 if (name == NULL)
662 NIS_RES_STATUS (res) = NIS_BADNAME;
663 return res;
666 ibreq = __create_ib_request (name, 0);
667 if (ibreq == NULL)
669 NIS_RES_STATUS (res) = NIS_BADNAME;
670 return res;
673 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
674 (xdrproc_t) _xdr_ib_request,
675 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
676 (caddr_t) res, 0, NULL);
678 if (status != NIS_SUCCESS)
679 NIS_RES_STATUS (res) = status;
681 nis_free_request (ibreq);
683 return res;
686 nis_result *
687 nis_next_entry (const_nis_name name, const netobj *cookie)
689 nis_result *res;
690 ib_request *ibreq;
691 nis_error status;
693 res = calloc (1, sizeof (nis_result));
694 if (res == NULL)
695 return NULL;
697 if (name == NULL)
699 NIS_RES_STATUS (res) = NIS_BADNAME;
700 return res;
703 ibreq = __create_ib_request (name, 0);
704 if (ibreq == NULL)
706 NIS_RES_STATUS (res) = NIS_BADNAME;
707 return res;
710 if (cookie != NULL)
712 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
713 ibreq->ibr_cookie.n_len = cookie->n_len;
716 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
717 (xdrproc_t) _xdr_ib_request,
718 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
719 (caddr_t) res, 0, NULL);
721 if (status != NIS_SUCCESS)
722 NIS_RES_STATUS (res) = status;
724 if (cookie != NULL)
726 /* Don't give cookie free, it is not from us */
727 ibreq->ibr_cookie.n_bytes = NULL;
728 ibreq->ibr_cookie.n_len = 0;
731 nis_free_request (ibreq);
733 return res;