* nis/nis_table.c (nis_list): Avoid clearing res twice before
[glibc.git] / nis / nis_table.c
blob49a630312a7e1565f7abfc0b5e035d51f0d3e55d
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 <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] != '[')
45 ibreq->ibr_name = strdup (cptr);
46 if (ibreq->ibr_name == NULL)
48 free (ibreq);
49 return NULL;
51 return ibreq;
54 /* "[key=value,...],foo" format */
55 ibreq->ibr_name = strchr (cptr, ']');
56 if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
58 /* The object has not really been built yet so we use free. */
59 free (ibreq);
60 return NULL;
63 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
64 if (ibreq->ibr_name[-1] == ',')
65 ibreq->ibr_name[-1] = '\0';
66 else
67 ibreq->ibr_name[0] = '\0';
68 ibreq->ibr_name += 2;
69 ibreq->ibr_name = strdup (ibreq->ibr_name);
70 if (ibreq->ibr_name == NULL)
72 free_null:
73 while (search_len-- > 0)
75 free (search_val[search_len].zattr_ndx);
76 free (search_val[search_len].zattr_val.zattr_val_val);
78 free (search_val);
79 nis_free_request (ibreq);
80 return NULL;
83 ++cptr; /* Remove "[" */
85 while (cptr != NULL && cptr[0] != '\0')
87 char *key = cptr;
88 char *val = strchr (cptr, '=');
90 cptr = strchr (key, ',');
91 if (cptr != NULL)
92 *cptr++ = '\0';
94 if (!val)
96 nis_free_request (ibreq);
97 return NULL;
99 *val++ = '\0';
100 if ((search_len + 1) >= size)
102 size += 1;
103 nis_attr *newp = realloc (search_val, size * sizeof (nis_attr));
104 if (newp == NULL)
105 goto free_null;
106 search_val = newp;
108 search_val[search_len].zattr_ndx = strdup (key);
109 if ((search_val[search_len].zattr_ndx) == NULL)
110 goto free_null;
112 search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1;
113 search_val[search_len].zattr_val.zattr_val_val = strdup (val);
114 if (search_val[search_len].zattr_val.zattr_val_val == NULL)
116 free (search_val[search_len].zattr_ndx);
117 goto free_null;
120 ++search_len;
123 ibreq->ibr_srch.ibr_srch_val = search_val;
124 ibreq->ibr_srch.ibr_srch_len = search_len;
126 return ibreq;
129 static const struct timeval RPCTIMEOUT = {10, 0};
131 static char *
132 get_tablepath (char *name, dir_binding *bptr)
134 enum clnt_stat result;
135 nis_result res;
136 struct ns_request req;
138 memset (&res, '\0', sizeof (res));
140 req.ns_name = name;
141 req.ns_object.ns_object_len = 0;
142 req.ns_object.ns_object_val = NULL;
144 result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request,
145 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
146 (caddr_t) &res, RPCTIMEOUT);
148 const char *cptr;
149 if (result == RPC_SUCCESS && NIS_RES_STATUS (&res) == NIS_SUCCESS
150 && __type_of (NIS_RES_OBJECT (&res)) == NIS_TABLE_OBJ)
151 cptr = NIS_RES_OBJECT (&res)->TA_data.ta_path;
152 else
153 cptr = "";
155 char *str = strdup (cptr);
157 if (result == RPC_SUCCESS)
158 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &res);
160 return str;
163 nis_result *
164 nis_list (const_nis_name name, unsigned int flags,
165 int (*callback) (const_nis_name name,
166 const nis_object *object,
167 const void *userdata),
168 const void *userdata)
170 nis_result *res = malloc (sizeof (nis_result));
171 ib_request *ibreq;
172 int status;
173 enum clnt_stat clnt_status;
174 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
175 int done = 0;
176 nis_name *names;
177 nis_name namebuf[2] = {NULL, NULL};
178 int name_nr = 0;
179 nis_cb *cb = NULL;
180 char *tableptr;
181 char *tablepath = NULL;
182 int first_try = 0; /* Do we try the old binding at first ? */
183 int errcode;
185 if (res == NULL)
186 return NULL;
188 if (name == NULL)
190 errcode = NIS_BADNAME;
191 err_out:
192 memset (res, '\0', sizeof (nis_result));
193 NIS_RES_STATUS (res) = NIS_BADNAME;
194 return res;
197 if ((ibreq = __create_ib_request (name, flags)) == NULL)
199 errcode = NIS_BADNAME;
200 goto err_out;
203 if ((flags & EXPAND_NAME)
204 && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
206 names = nis_getnames (ibreq->ibr_name);
207 free (ibreq->ibr_name);
208 ibreq->ibr_name = NULL;
209 if (names == NULL)
211 nis_free_request (ibreq);
212 errcode = NIS_BADNAME;
213 goto err_out;
215 ibreq->ibr_name = strdup (names[name_nr]);
216 if (ibreq->ibr_name == NULL)
218 nis_freenames (names);
219 nis_free_request (ibreq);
220 errcode = NIS_NOMEMORY;
221 goto err_out;
224 else
226 names = namebuf;
227 names[name_nr] = ibreq->ibr_name;
230 cb = NULL;
232 while (!done)
234 dir_binding bptr;
235 directory_obj *dir = NULL;
237 memset (res, '\0', sizeof (nis_result));
239 status = __nisfind_server (ibreq->ibr_name, &dir);
240 if (status != NIS_SUCCESS)
242 NIS_RES_STATUS (res) = status;
243 goto fail3;
246 status = __nisbind_create (&bptr, dir->do_servers.do_servers_val,
247 dir->do_servers.do_servers_len, flags);
248 if (status != NIS_SUCCESS)
250 NIS_RES_STATUS (res) = status;
251 goto fail2;
254 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
255 if (__nisbind_next (&bptr) != NIS_SUCCESS)
257 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
258 goto fail;
261 if (callback != NULL)
263 cb = __nis_create_callback (callback, userdata, flags);
264 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
265 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
268 again:
269 clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
270 (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
271 (xdrproc_t) _xdr_nis_result,
272 (caddr_t) res, RPCTIMEOUT);
274 if (clnt_status != RPC_SUCCESS)
275 NIS_RES_STATUS (res) = NIS_RPCERROR;
276 else
277 switch (NIS_RES_STATUS (res))
278 { /* start switch */
279 case NIS_PARTIAL:
280 case NIS_SUCCESS:
281 case NIS_S_SUCCESS:
282 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
283 && (flags & FOLLOW_LINKS)) /* We are following links. */
285 free (ibreq->ibr_name);
286 ibreq->ibr_name = NULL;
287 /* If we hit the link limit, bail. */
288 if (count_links > NIS_MAXLINKS)
290 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
291 ++done;
292 break;
294 ++count_links;
295 ibreq->ibr_name =
296 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
297 if (ibreq->ibr_name == NULL)
299 NIS_RES_STATUS (res) = NIS_NOMEMORY;
300 fail:
301 __nisbind_destroy (&bptr);
302 fail2:
303 nis_free_directory (dir);
304 fail3:
305 free (tablepath);
306 if (cb)
308 __nis_destroy_callback (cb);
309 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
310 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
312 if (names != namebuf)
313 nis_freenames (names);
314 nis_free_request (ibreq);
315 return res;
317 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
318 if (ibreq->ibr_srch.ibr_srch_len == 0)
320 ibreq->ibr_srch.ibr_srch_len =
321 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
322 ibreq->ibr_srch.ibr_srch_val =
323 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
325 /* The following is a non-obvious optimization. A
326 nis_freeresult call would call xdr_free as the
327 following code. But it also would unnecessarily
328 free the result structure. We avoid this here
329 along with the necessary tests. */
330 #if 1
331 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
332 memset (res, '\0', sizeof (*res));
333 #else
334 nis_freeresult (res);
335 res = calloc (1, sizeof (nis_result));
336 if (res == NULL)
337 goto fail;
338 #endif
339 first_try = 1; /* Try at first the old binding */
340 goto again;
342 else if ((flags & FOLLOW_PATH)
343 && NIS_RES_STATUS (res) == NIS_PARTIAL)
345 if (tablepath == NULL)
347 tablepath = get_tablepath (ibreq->ibr_name, &bptr);
348 tableptr = tablepath;
350 if (tableptr == NULL)
352 ++done;
353 break;
355 free (ibreq->ibr_name);
356 ibreq->ibr_name = strsep (&tableptr, ":");
357 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
359 ibreq->ibr_name = strdup ("");
360 if (ibreq->ibr_name == NULL)
362 NIS_RES_STATUS (res) = NIS_NOMEMORY;
363 goto fail;
365 ++done;
367 else
369 ibreq->ibr_name = strdup (ibreq->ibr_name);
370 /* The following is a non-obvious optimization. A
371 nis_freeresult call would call xdr_free as the
372 following code. But it also would unnecessarily
373 free the result structure. We avoid this here
374 along with the necessary tests. */
375 #if 1
376 xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res);
377 memset (res, '\0', sizeof (*res));
378 if (ibreq->ibr_name == NULL)
380 NIS_RES_STATUS (res) = NIS_NOMEMORY;
381 goto fail;
383 #else
384 nis_freeresult (res);
385 res = calloc (1, sizeof (nis_result));
386 if (res == NULL || ibreq->ibr_name == NULL)
388 free (res);
389 res = NULL;
390 goto fail;
392 #endif
393 first_try = 1;
394 goto again;
397 else
398 ++done;
399 break;
400 case NIS_CBRESULTS:
401 if (cb != NULL)
403 __nis_do_callback (&bptr, &res->cookie, cb);
404 NIS_RES_STATUS (res) = cb->result;
406 if (!(flags & ALL_RESULTS))
407 ++done;
408 else
410 if (tablepath == NULL)
412 tablepath = get_tablepath (ibreq->ibr_name, &bptr);
413 tableptr = tablepath;
415 if (tableptr == NULL)
417 ++done;
418 break;
420 free (ibreq->ibr_name);
421 ibreq->ibr_name = strsep (&tableptr, ":");
422 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
424 ibreq->ibr_name = strdup ("");
425 ++done;
427 else
428 ibreq->ibr_name = strdup (ibreq->ibr_name);
429 if (ibreq->ibr_name == NULL)
431 NIS_RES_STATUS (res) = NIS_NOMEMORY;
432 goto fail;
436 break;
437 case NIS_SYSTEMERROR:
438 case NIS_NOSUCHNAME:
439 case NIS_NOT_ME:
440 /* If we had first tried the old binding, do nothing, but
441 get a new binding */
442 if (!first_try)
444 if (__nisbind_next (&bptr) != NIS_SUCCESS)
446 ++done;
447 break; /* No more servers to search */
449 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
451 if (__nisbind_next (&bptr) != NIS_SUCCESS)
453 ++done;
454 break; /* No more servers to search */
457 goto again;
459 break;
460 default:
461 if (!first_try)
463 /* Try the next domainname if we don't follow a link. */
464 free (ibreq->ibr_name);
465 ibreq->ibr_name = NULL;
466 if (count_links)
468 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
469 ++done;
470 break;
472 ++name_nr;
473 if (names[name_nr] == NULL)
475 ++done;
476 break;
478 ibreq->ibr_name = strdup (names[name_nr]);
479 if (ibreq->ibr_name == NULL)
481 NIS_RES_STATUS (res) = NIS_NOMEMORY;
482 goto fail;
484 first_try = 1; /* Try old binding at first */
485 goto again;
487 break;
489 first_try = 0;
491 if (cb)
493 __nis_destroy_callback (cb);
494 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
495 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
496 cb = NULL;
499 __nisbind_destroy (&bptr);
500 nis_free_directory (dir);
503 free (tablepath);
505 if (names != namebuf)
506 nis_freenames (names);
508 nis_free_request (ibreq);
510 return res;
512 libnsl_hidden_def (nis_list)
514 nis_result *
515 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
517 nis_result *res = calloc (1, sizeof (nis_result));
518 if (res == NULL)
519 return NULL;
521 if (name == NULL)
523 NIS_RES_STATUS (res) = NIS_BADNAME;
524 return res;
527 size_t namelen = strlen (name);
528 char buf1[namelen + 20];
529 char buf4[namelen + 20];
531 ib_request *ibreq = __create_ib_request (name, flags);
532 if (ibreq == NULL)
534 NIS_RES_STATUS (res) = NIS_BADNAME;
535 return res;
538 nis_object obj;
539 memcpy (&obj, obj2, sizeof (nis_object));
541 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
542 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
544 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
545 obj.zo_owner = nis_local_principal ();
547 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
548 obj.zo_group = nis_local_group ();
550 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
552 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
553 if (ibreq->ibr_obj.ibr_obj_val == NULL)
555 nis_free_request (ibreq);
556 NIS_RES_STATUS (res) = NIS_NOMEMORY;
557 return res;
559 ibreq->ibr_obj.ibr_obj_len = 1;
561 nis_error status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
562 (xdrproc_t) _xdr_ib_request,
563 (caddr_t) ibreq,
564 (xdrproc_t) _xdr_nis_result,
565 (caddr_t) res, 0, NULL);
566 if (status != NIS_SUCCESS)
567 NIS_RES_STATUS (res) = status;
569 nis_free_request (ibreq);
571 return res;
574 nis_result *
575 nis_modify_entry (const_nis_name name, const nis_object *obj2,
576 unsigned int flags)
578 nis_object obj;
579 nis_result *res;
580 nis_error status;
581 ib_request *ibreq;
582 size_t namelen = strlen (name);
583 char buf1[namelen + 20];
584 char buf4[namelen + 20];
586 res = calloc (1, sizeof (nis_result));
587 if (res == NULL)
588 return NULL;
590 if (( ibreq =__create_ib_request (name, flags)) == NULL)
592 NIS_RES_STATUS (res) = NIS_BADNAME;
593 return res;
596 memcpy (&obj, obj2, sizeof (nis_object));
598 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
599 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
601 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
602 obj.zo_owner = nis_local_principal ();
604 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
605 obj.zo_group = nis_local_group ();
607 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
609 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
610 if (ibreq->ibr_obj.ibr_obj_val == NULL)
612 nis_free_request (ibreq);
613 NIS_RES_STATUS (res) = NIS_NOMEMORY;
614 return res;
616 ibreq->ibr_obj.ibr_obj_len = 1;
618 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
619 (xdrproc_t) _xdr_ib_request,
620 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
621 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
622 NIS_RES_STATUS (res) = status;
624 nis_free_request (ibreq);
626 return res;
629 nis_result *
630 nis_remove_entry (const_nis_name name, const nis_object *obj,
631 unsigned int flags)
633 nis_result *res;
634 ib_request *ibreq;
635 nis_error status;
637 res = calloc (1, sizeof (nis_result));
638 if (res == NULL)
639 return NULL;
641 if (name == NULL)
643 NIS_RES_STATUS (res) = NIS_BADNAME;
644 return res;
647 if ((ibreq =__create_ib_request (name, flags)) == NULL)
649 NIS_RES_STATUS (res) = NIS_BADNAME;
650 return res;
653 if (obj != NULL)
655 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
656 if (ibreq->ibr_obj.ibr_obj_val == NULL)
658 nis_free_request (ibreq);
659 NIS_RES_STATUS (res) = NIS_NOMEMORY;
660 return res;
662 ibreq->ibr_obj.ibr_obj_len = 1;
665 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
666 (xdrproc_t) _xdr_ib_request,
667 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
668 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
669 NIS_RES_STATUS (res) = status;
671 nis_free_request (ibreq);
673 return res;
676 nis_result *
677 nis_first_entry (const_nis_name name)
679 nis_result *res;
680 ib_request *ibreq;
681 nis_error status;
683 res = calloc (1, sizeof (nis_result));
684 if (res == NULL)
685 return NULL;
687 if (name == NULL)
689 NIS_RES_STATUS (res) = NIS_BADNAME;
690 return res;
693 ibreq = __create_ib_request (name, 0);
694 if (ibreq == NULL)
696 NIS_RES_STATUS (res) = NIS_BADNAME;
697 return res;
700 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
701 (xdrproc_t) _xdr_ib_request,
702 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
703 (caddr_t) res, 0, NULL);
705 if (status != NIS_SUCCESS)
706 NIS_RES_STATUS (res) = status;
708 nis_free_request (ibreq);
710 return res;
713 nis_result *
714 nis_next_entry (const_nis_name name, const netobj *cookie)
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 if (cookie != NULL)
739 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
740 ibreq->ibr_cookie.n_len = cookie->n_len;
743 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
744 (xdrproc_t) _xdr_ib_request,
745 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
746 (caddr_t) res, 0, NULL);
748 if (status != NIS_SUCCESS)
749 NIS_RES_STATUS (res) = status;
751 if (cookie != NULL)
753 /* Don't give cookie free, it is not from us */
754 ibreq->ibr_cookie.n_bytes = NULL;
755 ibreq->ibr_cookie.n_len = 0;
758 nis_free_request (ibreq);
760 return res;