2005-02-07 Steven Munroe <sjmunroe@us.ibm.com>
[glibc/history.git] / nis / nis_table.c
blob13acdfdf503db7a5577505cc464691fda8eb5f5e
1 /* Copyright (c) 1997, 1998, 1999, 2003, 2004 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 (ib_request));
31 char buf[strlen (name) + 1];
32 nis_attr *search_val = NULL;
33 size_t search_len = 0;
34 char *cptr;
35 size_t size = 0;
37 if (ibreq == NULL)
38 return NULL;
40 ibreq->ibr_flags = flags;
42 cptr = strcpy (buf, name);
44 /* Not of "[key=value,key=value,...],foo.." format? */
45 if (cptr[0] != '[')
46 return (ibreq->ibr_name = strdup (cptr)) == NULL ? NULL : ibreq;
48 /* "[key=value,...],foo" format */
49 ibreq->ibr_name = strchr (cptr, ']');
50 if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
52 ibreq->ibr_name = NULL; /* Or the xdr_* functions will dump */
53 nis_free_request (ibreq);
54 return NULL;
57 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
58 if (ibreq->ibr_name[-1] == ',')
59 ibreq->ibr_name[-1] = '\0';
60 else
61 ibreq->ibr_name[0] = '\0';
62 ibreq->ibr_name += 2;
63 ibreq->ibr_name = strdup (ibreq->ibr_name);
64 if (ibreq->ibr_name == NULL)
66 free_null:
67 while (search_len-- > 0)
69 free (search_val[search_len].zattr_ndx);
70 free (search_val[search_len].zattr_val.zattr_val_val);
72 free (search_val);
73 nis_free_request (ibreq);
74 return NULL;
77 ++cptr; /* Remove "[" */
79 while (cptr != NULL && cptr[0] != '\0')
81 char *key = cptr;
82 char *val = strchr (cptr, '=');
84 cptr = strchr (key, ',');
85 if (cptr != NULL)
86 *cptr++ = '\0';
88 if (!val)
90 nis_free_request (ibreq);
91 return NULL;
93 *val++ = '\0';
94 if ((search_len + 1) >= size)
96 size += 1;
97 search_val = realloc (search_val, size * sizeof (nis_attr));
98 if (search_val == NULL)
99 goto free_null;
101 search_val[search_len].zattr_ndx = strdup (key);
102 if ((search_val[search_len].zattr_ndx) == NULL)
103 goto free_null;
105 search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1;
106 search_val[search_len].zattr_val.zattr_val_val = strdup (val);
107 if (search_val[search_len].zattr_val.zattr_val_val == NULL)
109 free (search_val[search_len].zattr_ndx);
110 goto free_null;
113 ++search_len;
116 ibreq->ibr_srch.ibr_srch_val = search_val;
117 ibreq->ibr_srch.ibr_srch_len = search_len;
119 return ibreq;
122 static struct timeval RPCTIMEOUT = {10, 0};
124 static char *
125 __get_tablepath (char *name, dir_binding *bptr)
127 enum clnt_stat result;
128 nis_result *res = calloc (1, sizeof (nis_result));
129 struct ns_request req;
131 if (res == NULL)
132 return NULL;
134 req.ns_name = name;
135 req.ns_object.ns_object_len = 0;
136 req.ns_object.ns_object_val = NULL;
138 result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request,
139 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
140 (caddr_t) res, RPCTIMEOUT);
142 if (result == RPC_SUCCESS && NIS_RES_STATUS (res) == NIS_SUCCESS &&
143 __type_of (NIS_RES_OBJECT (res)) == NIS_TABLE_OBJ)
145 char *cptr = strdup (NIS_RES_OBJECT (res)->TA_data.ta_path);
146 nis_freeresult (res);
147 return cptr;
149 else
151 nis_freeresult (res);
152 return strdup ("");
156 nis_result *
157 nis_list (const_nis_name name, unsigned int flags,
158 int (*callback) (const_nis_name name,
159 const nis_object *object,
160 const void *userdata),
161 const void *userdata)
163 nis_result *res = calloc (1, sizeof (nis_result));
164 ib_request *ibreq;
165 int status;
166 enum clnt_stat clnt_status;
167 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
168 int done = 0;
169 nis_name *names;
170 nis_name namebuf[2] = {NULL, NULL};
171 int name_nr = 0;
172 nis_cb *cb = NULL;
173 char *tableptr, *tablepath = NULL;
174 int have_tablepath = 0;
175 int first_try = 0; /* Do we try the old binding at first ? */
177 if (res == NULL)
178 return NULL;
180 if (name == NULL)
182 NIS_RES_STATUS (res) = NIS_BADNAME;
183 return res;
186 if ((ibreq = __create_ib_request (name, flags)) == NULL)
188 NIS_RES_STATUS (res) = NIS_BADNAME;
189 return res;
192 if ((flags & EXPAND_NAME)
193 && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
195 names = nis_getnames (ibreq->ibr_name);
196 free (ibreq->ibr_name);
197 ibreq->ibr_name = NULL;
198 if (names == NULL)
200 nis_free_request (ibreq);
201 NIS_RES_STATUS (res) = NIS_BADNAME;
202 return res;
204 ibreq->ibr_name = strdup (names[name_nr]);
205 if (ibreq->ibr_name == NULL)
207 nis_free_request (ibreq);
208 NIS_RES_STATUS (res) = NIS_NOMEMORY;
209 return res;
212 else
214 names = namebuf;
215 names[name_nr] = ibreq->ibr_name;
218 cb = NULL;
220 while (!done)
222 dir_binding bptr;
223 directory_obj *dir = NULL;
225 memset (res, '\0', sizeof (nis_result));
227 status = __nisfind_server (ibreq->ibr_name, &dir);
228 if (status != NIS_SUCCESS)
230 nis_free_request (ibreq);
231 NIS_RES_STATUS (res) = status;
232 return res;
235 status = __nisbind_create (&bptr, dir->do_servers.do_servers_val,
236 dir->do_servers.do_servers_len, flags);
237 if (status != NIS_SUCCESS)
239 nis_free_request (ibreq);
240 NIS_RES_STATUS (res) = status;
241 nis_free_directory (dir);
242 return res;
245 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
246 if (__nisbind_next (&bptr) != NIS_SUCCESS)
248 __nisbind_destroy (&bptr);
249 nis_free_directory (dir);
250 nis_free_request (ibreq);
251 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
252 return res;
255 if (callback != NULL)
257 cb = __nis_create_callback (callback, userdata, flags);
258 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
259 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
262 again:
263 clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
264 (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
265 (xdrproc_t) _xdr_nis_result,
266 (caddr_t) res, RPCTIMEOUT);
268 if (clnt_status != RPC_SUCCESS)
269 NIS_RES_STATUS (res) = NIS_RPCERROR;
270 else
271 switch (NIS_RES_STATUS (res))
272 { /* start switch */
273 case NIS_PARTIAL:
274 case NIS_SUCCESS:
275 case NIS_S_SUCCESS:
276 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ &&
277 flags & FOLLOW_LINKS) /* We are following links. */
279 free (ibreq->ibr_name);
280 ibreq->ibr_name = NULL;
281 /* If we hit the link limit, bail. */
282 if (count_links > NIS_MAXLINKS)
284 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
285 ++done;
286 break;
288 ++count_links;
289 ibreq->ibr_name =
290 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
291 if (ibreq->ibr_name == NULL)
293 nis_free_request (ibreq);
294 NIS_RES_STATUS (res) = NIS_NOMEMORY;
295 return res;
297 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
298 if (ibreq->ibr_srch.ibr_srch_len == 0)
300 ibreq->ibr_srch.ibr_srch_len =
301 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
302 ibreq->ibr_srch.ibr_srch_val =
303 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
305 nis_freeresult (res);
306 res = calloc (1, sizeof (nis_result));
307 if (res == NULL)
309 if (have_tablepath)
310 free (tablepath);
311 __nisbind_destroy (&bptr);
312 nis_free_directory (dir);
313 return NULL;
315 first_try = 1; /* Try at first the old binding */
316 goto again;
318 else if ((flags & FOLLOW_PATH) &&
319 NIS_RES_STATUS (res) == NIS_PARTIAL)
321 if (!have_tablepath)
323 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
324 tableptr = tablepath;
325 have_tablepath = 1;
327 if (tableptr == NULL)
329 ++done;
330 break;
332 free (ibreq->ibr_name);
333 ibreq->ibr_name = strsep (&tableptr, ":");
334 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
336 ibreq->ibr_name = strdup ("");
337 if (ibreq->ibr_name == NULL)
339 nis_free_request (ibreq);
340 NIS_RES_STATUS (res) = NIS_NOMEMORY;
341 return res;
343 ++done;
345 else
347 ibreq->ibr_name = strdup (ibreq->ibr_name);
348 nis_freeresult (res);
349 res = calloc (1, sizeof (nis_result));
350 if (res == NULL || ibreq->ibr_name == NULL)
352 free (res);
353 nis_free_request (ibreq);
354 if (have_tablepath)
355 free (tablepath);
356 __nisbind_destroy (&bptr);
357 nis_free_directory (dir);
358 return NULL;
360 first_try = 1;
361 goto again;
364 else
365 ++done;
366 break;
367 case NIS_CBRESULTS:
368 if (cb != NULL)
370 __nis_do_callback (&bptr, &res->cookie, cb);
371 NIS_RES_STATUS (res) = cb->result;
373 if (!(flags & ALL_RESULTS))
374 ++done;
375 else
377 if (!have_tablepath)
379 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
380 tableptr = tablepath;
381 have_tablepath = 1;
383 if (tableptr == NULL)
385 ++done;
386 break;
388 free (ibreq->ibr_name);
389 ibreq->ibr_name = strsep (&tableptr, ":");
390 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
392 ibreq->ibr_name = strdup ("");
393 ++done;
395 else
396 ibreq->ibr_name = strdup (ibreq->ibr_name);
397 if (ibreq->ibr_name == NULL)
399 nis_free_request (ibreq);
400 NIS_RES_STATUS (res) = NIS_NOMEMORY;
401 return res;
405 break;
406 case NIS_SYSTEMERROR:
407 case NIS_NOSUCHNAME:
408 case NIS_NOT_ME:
409 /* If we had first tried the old binding, do nothing, but
410 get a new binding */
411 if (!first_try)
413 if (__nisbind_next (&bptr) != NIS_SUCCESS)
415 ++done;
416 break; /* No more servers to search */
418 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
420 if (__nisbind_next (&bptr) != NIS_SUCCESS)
422 ++done;
423 break; /* No more servers to search */
426 goto again;
428 break;
429 default:
430 if (!first_try)
432 /* Try the next domainname if we don't follow a link. */
433 free (ibreq->ibr_name);
434 ibreq->ibr_name = NULL;
435 if (count_links)
437 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
438 ++done;
439 break;
441 ++name_nr;
442 if (names[name_nr] == NULL)
444 ++done;
445 break;
447 ibreq->ibr_name = strdup (names[name_nr]);
448 if (ibreq->ibr_name == NULL)
450 nis_free_request (ibreq);
451 NIS_RES_STATUS (res) = NIS_NOMEMORY;
452 return res;
454 first_try = 1; /* Try old binding at first */
455 goto again;
457 break;
459 first_try = 0;
461 if (cb)
463 __nis_destroy_callback (cb);
464 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
465 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
468 __nisbind_destroy (&bptr);
469 nis_free_directory (dir);
472 if (names != namebuf)
473 nis_freenames (names);
475 nis_free_request (ibreq);
477 return res;
479 libnsl_hidden_def (nis_list)
481 nis_result *
482 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
484 nis_object obj;
485 nis_result *res;
486 nis_error status;
487 ib_request *ibreq;
488 size_t namelen = strlen (name);
489 char buf1[namelen + 20];
490 char buf4[namelen + 20];
492 res = calloc (1, sizeof (nis_result));
493 if (res == NULL)
494 return NULL;
496 if (name == NULL)
498 NIS_RES_STATUS (res) = NIS_BADNAME;
499 return res;
502 if ((ibreq = __create_ib_request (name, flags)) == NULL)
504 NIS_RES_STATUS (res) = NIS_BADNAME;
505 return res;
508 memcpy (&obj, obj2, sizeof (nis_object));
510 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
511 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
513 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
514 obj.zo_owner = nis_local_principal ();
516 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
517 obj.zo_group = nis_local_group ();
519 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
521 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
522 if (ibreq->ibr_obj.ibr_obj_val == NULL)
524 nis_free_request (ibreq);
525 NIS_RES_STATUS (res) = NIS_NOMEMORY;
526 return res;
528 ibreq->ibr_obj.ibr_obj_len = 1;
530 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
531 (xdrproc_t) _xdr_ib_request,
532 (caddr_t) ibreq,
533 (xdrproc_t) _xdr_nis_result,
534 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
535 NIS_RES_STATUS (res) = status;
537 nis_free_request (ibreq);
539 return res;
542 nis_result *
543 nis_modify_entry (const_nis_name name, const nis_object *obj2,
544 unsigned int flags)
546 nis_object obj;
547 nis_result *res;
548 nis_error status;
549 ib_request *ibreq;
550 size_t namelen = strlen (name);
551 char buf1[namelen + 20];
552 char buf4[namelen + 20];
554 res = calloc (1, sizeof (nis_result));
555 if (res == NULL)
556 return NULL;
558 if (( ibreq =__create_ib_request (name, flags)) == NULL)
560 NIS_RES_STATUS (res) = NIS_BADNAME;
561 return res;
564 memcpy (&obj, obj2, sizeof (nis_object));
566 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
567 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
569 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
570 obj.zo_owner = nis_local_principal ();
572 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
573 obj.zo_group = nis_local_group ();
575 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
577 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
578 if (ibreq->ibr_obj.ibr_obj_val == NULL)
580 nis_free_request (ibreq);
581 NIS_RES_STATUS (res) = NIS_NOMEMORY;
582 return res;
584 ibreq->ibr_obj.ibr_obj_len = 1;
586 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
587 (xdrproc_t) _xdr_ib_request,
588 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
589 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
590 NIS_RES_STATUS (res) = status;
592 nis_free_request (ibreq);
594 return res;
597 nis_result *
598 nis_remove_entry (const_nis_name name, const nis_object *obj,
599 unsigned int flags)
601 nis_result *res;
602 ib_request *ibreq;
603 nis_error status;
605 res = calloc (1, sizeof (nis_result));
606 if (res == NULL)
607 return NULL;
609 if (name == NULL)
611 NIS_RES_STATUS (res) = NIS_BADNAME;
612 return res;
615 if ((ibreq =__create_ib_request (name, flags)) == NULL)
617 NIS_RES_STATUS (res) = NIS_BADNAME;
618 return res;
621 if (obj != NULL)
623 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
624 if (ibreq->ibr_obj.ibr_obj_val == NULL)
626 nis_free_request (ibreq);
627 NIS_RES_STATUS (res) = NIS_NOMEMORY;
628 return res;
630 ibreq->ibr_obj.ibr_obj_len = 1;
633 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
634 (xdrproc_t) _xdr_ib_request,
635 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
636 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
637 NIS_RES_STATUS (res) = status;
639 nis_free_request (ibreq);
641 return res;
644 nis_result *
645 nis_first_entry (const_nis_name name)
647 nis_result *res;
648 ib_request *ibreq;
649 nis_error status;
651 res = calloc (1, sizeof (nis_result));
652 if (res == NULL)
653 return NULL;
655 if (name == NULL)
657 NIS_RES_STATUS (res) = NIS_BADNAME;
658 return res;
661 ibreq = __create_ib_request (name, 0);
662 if (ibreq == NULL)
664 NIS_RES_STATUS (res) = NIS_BADNAME;
665 return res;
668 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
669 (xdrproc_t) _xdr_ib_request,
670 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
671 (caddr_t) res, 0, NULL);
673 if (status != NIS_SUCCESS)
674 NIS_RES_STATUS (res) = status;
676 nis_free_request (ibreq);
678 return res;
681 nis_result *
682 nis_next_entry (const_nis_name name, const netobj *cookie)
684 nis_result *res;
685 ib_request *ibreq;
686 nis_error status;
688 res = calloc (1, sizeof (nis_result));
689 if (res == NULL)
690 return NULL;
692 if (name == NULL)
694 NIS_RES_STATUS (res) = NIS_BADNAME;
695 return res;
698 ibreq = __create_ib_request (name, 0);
699 if (ibreq == NULL)
701 NIS_RES_STATUS (res) = NIS_BADNAME;
702 return res;
705 if (cookie != NULL)
707 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
708 ibreq->ibr_cookie.n_len = cookie->n_len;
711 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
712 (xdrproc_t) _xdr_ib_request,
713 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
714 (caddr_t) res, 0, NULL);
716 if (status != NIS_SUCCESS)
717 NIS_RES_STATUS (res) = status;
719 if (cookie != NULL)
721 /* Don't give cookie free, it is not from us */
722 ibreq->ibr_cookie.n_bytes = NULL;
723 ibreq->ibr_cookie.n_len = 0;
726 nis_free_request (ibreq);
728 return res;