Update.
[glibc.git] / nis / nis_table.c
blob746444c311765ccf06b0db023e2a7c8aa3566d9a
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;
480 nis_result *
481 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
483 nis_object obj;
484 nis_result *res;
485 nis_error status;
486 ib_request *ibreq;
487 size_t namelen = strlen (name);
488 char buf1[namelen + 20];
489 char buf4[namelen + 20];
491 res = calloc (1, sizeof (nis_result));
492 if (res == NULL)
493 return NULL;
495 if (name == NULL)
497 NIS_RES_STATUS (res) = NIS_BADNAME;
498 return res;
501 if ((ibreq = __create_ib_request (name, flags)) == NULL)
503 NIS_RES_STATUS (res) = NIS_BADNAME;
504 return res;
507 memcpy (&obj, obj2, sizeof (nis_object));
509 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
510 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
512 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
513 obj.zo_owner = nis_local_principal ();
515 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
516 obj.zo_group = nis_local_group ();
518 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
520 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
521 if (ibreq->ibr_obj.ibr_obj_val == NULL)
523 nis_free_request (ibreq);
524 NIS_RES_STATUS (res) = NIS_NOMEMORY;
525 return res;
527 ibreq->ibr_obj.ibr_obj_len = 1;
529 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
530 (xdrproc_t) _xdr_ib_request,
531 (caddr_t) ibreq,
532 (xdrproc_t) _xdr_nis_result,
533 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
534 NIS_RES_STATUS (res) = status;
536 nis_free_request (ibreq);
538 return res;
541 nis_result *
542 nis_modify_entry (const_nis_name name, const nis_object *obj2,
543 unsigned int flags)
545 nis_object obj;
546 nis_result *res;
547 nis_error status;
548 ib_request *ibreq;
549 size_t namelen = strlen (name);
550 char buf1[namelen + 20];
551 char buf4[namelen + 20];
553 res = calloc (1, sizeof (nis_result));
554 if (res == NULL)
555 return NULL;
557 if (( ibreq =__create_ib_request (name, flags)) == NULL)
559 NIS_RES_STATUS (res) = NIS_BADNAME;
560 return res;
563 memcpy (&obj, obj2, sizeof (nis_object));
565 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
566 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
568 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
569 obj.zo_owner = nis_local_principal ();
571 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
572 obj.zo_group = nis_local_group ();
574 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
576 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
577 if (ibreq->ibr_obj.ibr_obj_val == NULL)
579 nis_free_request (ibreq);
580 NIS_RES_STATUS (res) = NIS_NOMEMORY;
581 return res;
583 ibreq->ibr_obj.ibr_obj_len = 1;
585 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
586 (xdrproc_t) _xdr_ib_request,
587 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
588 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
589 NIS_RES_STATUS (res) = status;
591 nis_free_request (ibreq);
593 return res;
596 nis_result *
597 nis_remove_entry (const_nis_name name, const nis_object *obj,
598 unsigned int flags)
600 nis_result *res;
601 ib_request *ibreq;
602 nis_error status;
604 res = calloc (1, sizeof (nis_result));
605 if (res == NULL)
606 return NULL;
608 if (name == NULL)
610 NIS_RES_STATUS (res) = NIS_BADNAME;
611 return res;
614 if ((ibreq =__create_ib_request (name, flags)) == NULL)
616 NIS_RES_STATUS (res) = NIS_BADNAME;
617 return res;
620 if (obj != NULL)
622 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
623 if (ibreq->ibr_obj.ibr_obj_val == NULL)
625 nis_free_request (ibreq);
626 NIS_RES_STATUS (res) = NIS_NOMEMORY;
627 return res;
629 ibreq->ibr_obj.ibr_obj_len = 1;
632 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
633 (xdrproc_t) _xdr_ib_request,
634 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
635 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
636 NIS_RES_STATUS (res) = status;
638 nis_free_request (ibreq);
640 return res;
643 nis_result *
644 nis_first_entry (const_nis_name name)
646 nis_result *res;
647 ib_request *ibreq;
648 nis_error status;
650 res = calloc (1, sizeof (nis_result));
651 if (res == NULL)
652 return NULL;
654 if (name == NULL)
656 NIS_RES_STATUS (res) = NIS_BADNAME;
657 return res;
660 ibreq = __create_ib_request (name, 0);
661 if (ibreq == NULL)
663 NIS_RES_STATUS (res) = NIS_BADNAME;
664 return res;
667 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
668 (xdrproc_t) _xdr_ib_request,
669 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
670 (caddr_t) res, 0, NULL);
672 if (status != NIS_SUCCESS)
673 NIS_RES_STATUS (res) = status;
675 nis_free_request (ibreq);
677 return res;
680 nis_result *
681 nis_next_entry (const_nis_name name, const netobj *cookie)
683 nis_result *res;
684 ib_request *ibreq;
685 nis_error status;
687 res = calloc (1, sizeof (nis_result));
688 if (res == NULL)
689 return NULL;
691 if (name == NULL)
693 NIS_RES_STATUS (res) = NIS_BADNAME;
694 return res;
697 ibreq = __create_ib_request (name, 0);
698 if (ibreq == NULL)
700 NIS_RES_STATUS (res) = NIS_BADNAME;
701 return res;
704 if (cookie != NULL)
706 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
707 ibreq->ibr_cookie.n_len = cookie->n_len;
710 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
711 (xdrproc_t) _xdr_ib_request,
712 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
713 (caddr_t) res, 0, NULL);
715 if (status != NIS_SUCCESS)
716 NIS_RES_STATUS (res) = status;
718 if (cookie != NULL)
720 /* Don't give cookie free, it is not from us */
721 ibreq->ibr_cookie.n_bytes = NULL;
722 ibreq->ibr_cookie.n_len = 0;
725 nis_free_request (ibreq);
727 return res;