Fix redefinition problem.
[glibc.git] / nis / nis_table.c
blobe4d2f3020604673b41a2851cb1615e4317f28d06
1 /* Copyright (c) 1997, 1998, 1999 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 int 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)
65 return NULL;
67 ++cptr; /* Remove "[" */
69 while (cptr != NULL && cptr[0] != '\0')
71 char *key = cptr;
72 char *val = strchr (cptr, '=');
74 cptr = strchr (key, ',');
75 if (cptr != NULL)
76 *cptr++ = '\0';
78 if (!val)
80 nis_free_request (ibreq);
81 return NULL;
83 *val++ = '\0';
84 if ((search_len + 1) >= size)
86 size += 1;
87 search_val = realloc (search_val, size * sizeof (nis_attr));
88 if (search_val == NULL)
89 return NULL;
91 search_val[search_len].zattr_ndx = strdup (key);
92 if ((search_val[search_len].zattr_ndx) == NULL)
93 return NULL;
95 search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1;
96 search_val[search_len].zattr_val.zattr_val_val = strdup (val);
97 if (search_val[search_len].zattr_val.zattr_val_val == NULL)
98 return NULL;
100 ++search_len;
103 ibreq->ibr_srch.ibr_srch_val = search_val;
104 ibreq->ibr_srch.ibr_srch_len = search_len;
106 return ibreq;
109 static struct timeval RPCTIMEOUT = {10, 0};
111 static char *
112 __get_tablepath (char *name, dir_binding *bptr)
114 enum clnt_stat result;
115 nis_result *res = calloc (1, sizeof (nis_result));
116 struct ns_request req;
118 if (res == NULL)
119 return NULL;
121 req.ns_name = name;
122 req.ns_object.ns_object_len = 0;
123 req.ns_object.ns_object_val = NULL;
125 result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request,
126 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
127 (caddr_t) res, RPCTIMEOUT);
129 if (result == RPC_SUCCESS && NIS_RES_STATUS (res) == NIS_SUCCESS &&
130 __type_of (NIS_RES_OBJECT (res)) == NIS_TABLE_OBJ)
132 char *cptr = strdup (NIS_RES_OBJECT (res)->TA_data.ta_path);
133 nis_freeresult (res);
134 return cptr;
136 else
138 nis_freeresult (res);
139 return strdup ("");
143 nis_result *
144 nis_list (const_nis_name name, unsigned int flags,
145 int (*callback) (const_nis_name name,
146 const nis_object *object,
147 const void *userdata),
148 const void *userdata)
150 nis_result *res = calloc (1, sizeof (nis_result));
151 ib_request *ibreq;
152 int status;
153 enum clnt_stat clnt_status;
154 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
155 int done = 0;
156 nis_name *names;
157 nis_name namebuf[2] = {NULL, NULL};
158 int name_nr = 0;
159 nis_cb *cb = NULL;
160 char *tableptr, *tablepath = NULL;
161 int have_tablepath = 0;
162 int first_try = 0; /* Do we try the old binding at first ? */
164 if (res == NULL)
165 return NULL;
167 if (name == NULL)
169 NIS_RES_STATUS (res) = NIS_BADNAME;
170 return res;
173 if ((ibreq = __create_ib_request (name, flags)) == NULL)
175 NIS_RES_STATUS (res) = NIS_BADNAME;
176 return res;
179 if ((flags & EXPAND_NAME) &&
180 ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
182 names = nis_getnames (ibreq->ibr_name);
183 free (ibreq->ibr_name);
184 ibreq->ibr_name = NULL;
185 if (names == NULL)
187 nis_free_request (ibreq);
188 NIS_RES_STATUS (res) = NIS_BADNAME;
189 return res;
191 ibreq->ibr_name = strdup (names[name_nr]);
193 else
195 names = namebuf;
196 names[name_nr] = ibreq->ibr_name;
199 cb = NULL;
201 while (!done)
203 dir_binding bptr;
204 directory_obj *dir = NULL;
206 memset (res, '\0', sizeof (nis_result));
208 status = __nisfind_server (ibreq->ibr_name, &dir);
209 if (status != NIS_SUCCESS)
211 nis_free_request (ibreq);
212 NIS_RES_STATUS (res) = status;
213 return res;
216 status = __nisbind_create (&bptr, dir->do_servers.do_servers_val,
217 dir->do_servers.do_servers_len, flags);
218 if (status != NIS_SUCCESS)
220 nis_free_request (ibreq);
221 NIS_RES_STATUS (res) = status;
222 nis_free_directory (dir);
223 return res;
226 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
227 if (__nisbind_next (&bptr) != NIS_SUCCESS)
229 __nisbind_destroy (&bptr);
230 nis_free_directory (dir);
231 nis_free_request (ibreq);
232 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
233 return res;
236 if (callback != NULL)
238 cb = __nis_create_callback (callback, userdata, flags);
239 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
240 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
243 again:
244 clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
245 (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
246 (xdrproc_t) _xdr_nis_result,
247 (caddr_t) res, RPCTIMEOUT);
249 if (clnt_status != RPC_SUCCESS)
250 NIS_RES_STATUS (res) = NIS_RPCERROR;
251 else
252 switch (NIS_RES_STATUS (res))
253 { /* start switch */
254 case NIS_PARTIAL:
255 case NIS_SUCCESS:
256 case NIS_S_SUCCESS:
257 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ &&
258 flags & FOLLOW_LINKS) /* We are following links. */
260 free (ibreq->ibr_name);
261 ibreq->ibr_name = NULL;
262 /* If we hit the link limit, bail. */
263 if (count_links > NIS_MAXLINKS)
265 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
266 ++done;
267 break;
269 ++count_links;
270 ibreq->ibr_name =
271 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
272 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
273 if (ibreq->ibr_srch.ibr_srch_len == 0)
275 ibreq->ibr_srch.ibr_srch_len =
276 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
277 ibreq->ibr_srch.ibr_srch_val =
278 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
280 nis_freeresult (res);
281 res = calloc (1, sizeof (nis_result));
282 if (res == NULL)
284 if (have_tablepath)
285 free (tablepath);
286 __nisbind_destroy (&bptr);
287 nis_free_directory (dir);
288 return NULL;
290 first_try = 1; /* Try at first the old binding */
291 goto again;
293 else if ((flags & FOLLOW_PATH) &&
294 NIS_RES_STATUS (res) == NIS_PARTIAL)
296 if (!have_tablepath)
298 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
299 tableptr = tablepath;
300 have_tablepath = 1;
302 if (tableptr == NULL)
304 ++done;
305 break;
307 free (ibreq->ibr_name);
308 ibreq->ibr_name = strsep (&tableptr, ":");
309 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
311 ibreq->ibr_name = strdup ("");
312 ++done;
314 else
316 ibreq->ibr_name = strdup (ibreq->ibr_name);
317 nis_freeresult (res);
318 res = calloc (1, sizeof (nis_result));
319 if (res == NULL || ibreq->ibr_name == NULL)
321 if (res)
322 free (res);
323 nis_free_request (ibreq);
324 if (have_tablepath)
325 free (tablepath);
326 __nisbind_destroy (&bptr);
327 nis_free_directory (dir);
328 return NULL;
330 first_try = 1;
331 goto again;
334 else
335 ++done;
336 break;
337 case NIS_CBRESULTS:
338 if (cb != NULL)
340 __nis_do_callback (&bptr, &res->cookie, cb);
341 NIS_RES_STATUS (res) = cb->result;
343 if (!(flags & ALL_RESULTS))
344 ++done;
345 else
347 if (!have_tablepath)
349 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
350 tableptr = tablepath;
351 have_tablepath = 1;
353 if (tableptr == NULL)
355 ++done;
356 break;
358 free (ibreq->ibr_name);
359 ibreq->ibr_name = strsep (&tableptr, ":");
360 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
362 ibreq->ibr_name = strdup ("");
363 ++done;
365 else
366 ibreq->ibr_name = strdup (ibreq->ibr_name);
369 break;
370 case NIS_SYSTEMERROR:
371 case NIS_NOSUCHNAME:
372 case NIS_NOT_ME:
373 /* If we had first tried the old binding, do nothing, but
374 get a new binding */
375 if (!first_try)
377 if (__nisbind_next (&bptr) != NIS_SUCCESS)
379 ++done;
380 break; /* No more servers to search */
382 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
384 if (__nisbind_next (&bptr) != NIS_SUCCESS)
386 ++done;
387 break; /* No more servers to search */
390 goto again;
392 break;
393 default:
394 if (!first_try)
396 /* Try the next domainname if we don't follow a link. */
397 if (count_links)
399 free (ibreq->ibr_name);
400 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
401 ++done;
402 break;
404 ++name_nr;
405 if (names[name_nr] == NULL)
407 ++done;
408 break;
410 ibreq->ibr_name = names[name_nr];
411 first_try = 1; /* Try old binding at first */
412 goto again;
414 break;
416 first_try = 0;
418 if (cb)
420 __nis_destroy_callback (cb);
421 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
422 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
425 __nisbind_destroy (&bptr);
426 nis_free_directory (dir);
429 if (names != namebuf)
430 nis_freenames (names);
432 nis_free_request (ibreq);
434 return res;
437 nis_result *
438 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
440 nis_object obj;
441 nis_result *res;
442 nis_error status;
443 ib_request *ibreq;
444 size_t namelen = strlen (name);
445 char buf1[namelen + 20];
446 char buf4[namelen + 20];
448 res = calloc (1, sizeof (nis_result));
449 if (res == NULL)
450 return NULL;
452 if (name == NULL)
454 NIS_RES_STATUS (res) = NIS_BADNAME;
455 return res;
458 if ((ibreq = __create_ib_request (name, flags)) == NULL)
460 NIS_RES_STATUS (res) = NIS_BADNAME;
461 return res;
464 memcpy (&obj, obj2, sizeof (nis_object));
466 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
467 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
469 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
470 obj.zo_owner = nis_local_principal ();
472 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
473 obj.zo_group = nis_local_group ();
475 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
477 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
478 if (ibreq->ibr_obj.ibr_obj_val == NULL)
480 nis_free_request (ibreq);
481 NIS_RES_STATUS (res) = NIS_NOMEMORY;
482 return res;
484 ibreq->ibr_obj.ibr_obj_len = 1;
486 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
487 (xdrproc_t) _xdr_ib_request,
488 (caddr_t) ibreq,
489 (xdrproc_t) _xdr_nis_result,
490 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
491 NIS_RES_STATUS (res) = status;
493 nis_free_request (ibreq);
495 return res;
498 nis_result *
499 nis_modify_entry (const_nis_name name, const nis_object *obj2,
500 unsigned int flags)
502 nis_object obj;
503 nis_result *res;
504 nis_error status;
505 ib_request *ibreq;
506 size_t namelen = strlen (name);
507 char buf1[namelen + 20];
508 char buf4[namelen + 20];
510 res = calloc (1, sizeof (nis_result));
511 if (res == NULL)
512 return NULL;
514 if (( ibreq =__create_ib_request (name, flags)) == NULL)
516 NIS_RES_STATUS (res) = NIS_BADNAME;
517 return res;
520 memcpy (&obj, obj2, sizeof (nis_object));
522 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
523 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
525 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
526 obj.zo_owner = nis_local_principal ();
528 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
529 obj.zo_group = nis_local_group ();
531 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
533 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
534 if (ibreq->ibr_obj.ibr_obj_val == NULL)
536 nis_free_request (ibreq);
537 NIS_RES_STATUS (res) = NIS_NOMEMORY;
538 return res;
540 ibreq->ibr_obj.ibr_obj_len = 1;
542 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
543 (xdrproc_t) _xdr_ib_request,
544 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
545 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
546 NIS_RES_STATUS (res) = status;
548 nis_free_request (ibreq);
550 return res;
553 nis_result *
554 nis_remove_entry (const_nis_name name, const nis_object *obj,
555 unsigned int flags)
557 nis_result *res;
558 ib_request *ibreq;
559 nis_error status;
561 res = calloc (1, sizeof (nis_result));
562 if (res == NULL)
563 return NULL;
565 if (name == NULL)
567 NIS_RES_STATUS (res) = NIS_BADNAME;
568 return res;
571 if ((ibreq =__create_ib_request (name, flags)) == NULL)
573 NIS_RES_STATUS (res) = NIS_BADNAME;
574 return res;
577 if (obj != NULL)
579 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
580 if (ibreq->ibr_obj.ibr_obj_val == NULL)
582 nis_free_request (ibreq);
583 NIS_RES_STATUS (res) = NIS_NOMEMORY;
584 return res;
586 ibreq->ibr_obj.ibr_obj_len = 1;
589 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
590 (xdrproc_t) _xdr_ib_request,
591 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
592 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
593 NIS_RES_STATUS (res) = status;
595 nis_free_request (ibreq);
597 return res;
600 nis_result *
601 nis_first_entry (const_nis_name name)
603 nis_result *res;
604 ib_request *ibreq;
605 nis_error status;
607 res = calloc (1, sizeof (nis_result));
608 if (res == NULL)
609 return NULL;
611 if (name == NULL)
613 NIS_RES_STATUS (res) = NIS_BADNAME;
614 return res;
617 ibreq = __create_ib_request (name, 0);
618 if (ibreq == NULL)
620 NIS_RES_STATUS (res) = NIS_BADNAME;
621 return res;
624 status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
625 (xdrproc_t) _xdr_ib_request,
626 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
627 (caddr_t) res, 0, NULL);
629 if (status != NIS_SUCCESS)
630 NIS_RES_STATUS (res) = status;
632 nis_free_request (ibreq);
634 return res;
637 nis_result *
638 nis_next_entry (const_nis_name name, const netobj *cookie)
640 nis_result *res;
641 ib_request *ibreq;
642 nis_error status;
644 res = calloc (1, sizeof (nis_result));
645 if (res == NULL)
646 return NULL;
648 if (name == NULL)
650 NIS_RES_STATUS (res) = NIS_BADNAME;
651 return res;
654 ibreq = __create_ib_request (name, 0);
655 if (ibreq == NULL)
657 NIS_RES_STATUS (res) = NIS_BADNAME;
658 return res;
661 if (cookie != NULL)
663 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
664 ibreq->ibr_cookie.n_len = cookie->n_len;
667 status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
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 if (cookie != NULL)
677 /* Don't give cookie free, it is not from us */
678 ibreq->ibr_cookie.n_bytes = NULL;
679 ibreq->ibr_cookie.n_len = 0;
682 nis_free_request (ibreq);
684 return res;