Update.
[glibc.git] / nis / nis_table.c
blob2cb21d4fdafd9de155f1334ab1d157948804db6e
1 /* Copyright (c) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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 ibreq->ibr_flags = flags;
39 cptr = strcpy (buf, name);
41 /* Not of "[key=value,key=value,...],foo.." format? */
42 if (cptr[0] != '[')
44 ibreq->ibr_name = strdup (cptr);
45 return ibreq;
48 /* "[key=value,...],foo" format */
49 ibreq->ibr_name = strchr (cptr, ']');
50 if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',')
51 return NULL;
53 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
54 if (ibreq->ibr_name[-1] == ',')
55 ibreq->ibr_name[-1] = '\0';
56 else
57 ibreq->ibr_name[0] = '\0';
58 ibreq->ibr_name += 2;
59 ibreq->ibr_name = strdup (ibreq->ibr_name);
61 ++cptr; /* Remove "[" */
63 while (cptr != NULL && cptr[0] != '\0')
65 char *key = cptr;
66 char *val = strchr (cptr, '=');
68 cptr = strchr (key, ',');
69 if (cptr != NULL)
70 *cptr++ = '\0';
72 if (!val)
74 nis_free_request (ibreq);
75 return NULL;
77 *val++ = '\0';
78 if ((search_len + 1) >= size)
80 size += 1;
81 search_val = realloc (search_val, size * sizeof (nis_attr));
82 if (search_val == NULL)
84 nis_free_request (ibreq);
85 return NULL;
88 search_val[search_len].zattr_ndx = strdup (key);
89 if ((search_val[search_len].zattr_ndx) == NULL)
91 nis_free_request (ibreq);
92 return NULL;
94 search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1;
95 search_val[search_len].zattr_val.zattr_val_val = strdup (val);
96 if (search_val[search_len].zattr_val.zattr_val_val == NULL)
98 nis_free_request (ibreq);
99 return NULL;
101 ++search_len;
104 ibreq->ibr_srch.ibr_srch_val = search_val;
105 ibreq->ibr_srch.ibr_srch_len = search_len;
107 return ibreq;
110 static struct timeval RPCTIMEOUT = {10, 0};
112 static char *
113 __get_tablepath (char *name, dir_binding *bptr)
115 enum clnt_stat result;
116 nis_result *res = calloc (1, sizeof (nis_result));
117 struct ns_request req;
119 if (res == NULL)
120 return NULL;
122 req.ns_name = name;
123 req.ns_object.ns_object_len = 0;
124 req.ns_object.ns_object_val = NULL;
126 result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request,
127 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
128 (caddr_t) res, RPCTIMEOUT);
130 if (result == RPC_SUCCESS && NIS_RES_STATUS (res) == NIS_SUCCESS &&
131 __type_of (NIS_RES_OBJECT (res)) == NIS_TABLE_OBJ)
133 char *cptr = strdup (NIS_RES_OBJECT (res)->TA_data.ta_path);
134 nis_freeresult (res);
135 return cptr;
137 else
139 nis_freeresult (res);
140 return strdup ("");
144 nis_result *
145 nis_list (const_nis_name name, unsigned int flags,
146 int (*callback) (const_nis_name name,
147 const nis_object *object,
148 const void *userdata),
149 const void *userdata)
151 nis_result *res = NULL;
152 ib_request *ibreq;
153 int status;
154 enum clnt_stat clnt_status;
155 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
156 int done = 0;
157 nis_name *names;
158 nis_name namebuf[2] = {NULL, NULL};
159 int name_nr = 0;
160 nis_cb *cb = NULL;
161 char *tableptr, *tablepath = NULL;
162 int have_tablepath = 0;
163 int first_try = 0; /* Do we try the old binding at first ? */
165 res = calloc (1, sizeof (nis_result));
166 if (res == NULL)
167 return NULL;
169 if (name == NULL)
171 NIS_RES_STATUS (res) = NIS_BADNAME;
172 return res;
175 if ((ibreq = __create_ib_request (name, flags)) == NULL)
177 NIS_RES_STATUS (res) = NIS_BADNAME;
178 return res;
181 if ((flags & EXPAND_NAME) &&
182 ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.')
184 names = nis_getnames (ibreq->ibr_name);
185 free (ibreq->ibr_name);
186 ibreq->ibr_name = NULL;
187 if (names == NULL)
189 NIS_RES_STATUS (res) = NIS_BADNAME;
190 return res;
192 ibreq->ibr_name = strdup (names[name_nr]);
194 else
196 names = namebuf;
197 names[name_nr] = ibreq->ibr_name;
200 cb = NULL;
202 while (!done)
204 dir_binding bptr;
205 directory_obj *dir = NULL;
207 memset (res, '\0', sizeof (nis_result));
209 status = __nisfind_server (ibreq->ibr_name, &dir);
210 if (status != NIS_SUCCESS)
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_RES_STATUS (res) = status;
221 nis_free_directory (dir);
222 return res;
225 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
226 if (__nisbind_next (&bptr) != NIS_SUCCESS)
228 __nisbind_destroy (&bptr);
229 nis_free_directory (dir);
230 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
231 return res;
234 if (callback != NULL)
236 cb = __nis_create_callback (callback, userdata, flags);
237 ibreq->ibr_cbhost.ibr_cbhost_len = 1;
238 ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv;
241 again:
242 clnt_status = clnt_call (bptr.clnt, NIS_IBLIST,
243 (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq,
244 (xdrproc_t) _xdr_nis_result,
245 (caddr_t) res, RPCTIMEOUT);
247 if (clnt_status != RPC_SUCCESS)
248 NIS_RES_STATUS (res) = NIS_RPCERROR;
249 else
250 switch (NIS_RES_STATUS (res))
251 { /* start switch */
252 case NIS_PARTIAL:
253 case NIS_SUCCESS:
254 case NIS_S_SUCCESS:
255 if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ &&
256 flags & FOLLOW_LINKS) /* We are following links. */
258 free (ibreq->ibr_name);
259 /* If we hit the link limit, bail. */
260 if (count_links > NIS_MAXLINKS)
262 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
263 ++done;
264 break;
266 ++count_links;
267 ibreq->ibr_name =
268 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
269 if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len)
270 if (ibreq->ibr_srch.ibr_srch_len == 0)
272 ibreq->ibr_srch.ibr_srch_len =
273 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len;
274 ibreq->ibr_srch.ibr_srch_val =
275 NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val;
277 nis_freeresult (res);
278 res = calloc (1, sizeof (nis_result));
279 if (res == NULL)
281 if (have_tablepath)
282 free (tablepath);
283 __nisbind_destroy (&bptr);
284 nis_free_directory (dir);
285 return NULL;
287 first_try = 1; /* Try at first the old binding */
288 goto again;
290 else if ((flags & FOLLOW_PATH) &&
291 NIS_RES_STATUS (res) == NIS_PARTIAL)
293 if (!have_tablepath)
295 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
296 tableptr = tablepath;
297 have_tablepath = 1;
299 if (tableptr == NULL)
301 ++done;
302 break;
304 free (ibreq->ibr_name);
305 ibreq->ibr_name = strsep (&tableptr, ":");
306 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
308 ibreq->ibr_name = strdup ("");
309 ++done;
311 else
313 ibreq->ibr_name = strdup (ibreq->ibr_name);
314 nis_freeresult (res);
315 res = calloc (1, sizeof (nis_result));
316 if (res == NULL)
318 if (have_tablepath)
319 free (tablepath);
320 __nisbind_destroy (&bptr);
321 nis_free_directory (dir);
322 return NULL;
324 first_try = 1;
325 goto again;
328 else
329 ++done;
330 break;
331 case NIS_CBRESULTS:
332 if (cb != NULL)
334 __nis_do_callback (&bptr, &res->cookie, cb);
335 NIS_RES_STATUS (res) = cb->result;
337 if (!(flags & ALL_RESULTS))
338 ++done;
339 else
341 if (!have_tablepath)
343 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
344 tableptr = tablepath;
345 have_tablepath = 1;
347 if (tableptr == NULL)
349 ++done;
350 break;
352 free (ibreq->ibr_name);
353 ibreq->ibr_name = strsep (&tableptr, ":");
354 if (ibreq->ibr_name == NULL || ibreq->ibr_name[0] == '\0')
356 ibreq->ibr_name = strdup ("");
357 ++done;
359 else
360 ibreq->ibr_name = strdup (ibreq->ibr_name);
363 break;
364 case NIS_SYSTEMERROR:
365 case NIS_NOSUCHNAME:
366 case NIS_NOT_ME:
367 /* If we had first tried the old binding, do nothing, but
368 get a new binding */
369 if (!first_try)
371 if (__nisbind_next (&bptr) != NIS_SUCCESS)
373 ++done;
374 break; /* No more servers to search */
376 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
378 if (__nisbind_next (&bptr) != NIS_SUCCESS)
380 ++done;
381 break; /* No more servers to search */
384 goto again;
386 break;
387 default:
388 if (!first_try)
390 /* Try the next domainname if we don't follow a link. */
391 if (count_links)
393 free (ibreq->ibr_name);
394 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
395 ++done;
396 break;
398 ++name_nr;
399 if (names[name_nr] == NULL)
401 ++done;
402 break;
404 ibreq->ibr_name = names[name_nr];
405 first_try = 1; /* Try old binding at first */
406 goto again;
408 break;
410 first_try = 0;
412 if (cb)
414 __nis_destroy_callback (cb);
415 ibreq->ibr_cbhost.ibr_cbhost_len = 0;
416 ibreq->ibr_cbhost.ibr_cbhost_val = NULL;
419 __nisbind_destroy (&bptr);
420 nis_free_directory (dir);
423 if (names != namebuf)
424 nis_freenames (names);
426 nis_free_request (ibreq);
428 return res;
431 nis_result *
432 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
434 nis_object obj;
435 nis_result *res;
436 nis_error status;
437 ib_request *ibreq;
438 size_t namelen = strlen (name);
439 char buf1[namelen + 20];
440 char buf4[namelen + 20];
442 res = calloc (1, sizeof (nis_result));
443 if (res == NULL)
444 return NULL;
446 if (name == NULL)
448 NIS_RES_STATUS (res) = NIS_BADNAME;
449 return res;
452 if ((ibreq = __create_ib_request (name, flags)) == NULL)
454 NIS_RES_STATUS (res) = NIS_BADNAME;
455 return res;
458 memcpy (&obj, obj2, sizeof (nis_object));
460 if (obj.zo_name == NULL || strlen (obj.zo_name) == 0)
461 obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1));
463 if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0)
464 obj.zo_owner = nis_local_principal ();
466 if (obj.zo_group == NULL || strlen (obj.zo_group) == 0)
467 obj.zo_group = nis_local_group ();
469 obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4));
471 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL);
472 if (ibreq->ibr_obj.ibr_obj_val == NULL)
474 NIS_RES_STATUS (res) = NIS_NOMEMORY;
475 return res;
477 ibreq->ibr_obj.ibr_obj_len = 1;
479 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
480 (xdrproc_t) _xdr_ib_request,
481 (caddr_t) ibreq,
482 (xdrproc_t) _xdr_nis_result,
483 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
484 NIS_RES_STATUS (res) = status;
486 nis_free_request (ibreq);
488 return res;
491 nis_result *
492 nis_modify_entry (const_nis_name name, const nis_object *obj2,
493 unsigned int flags)
495 nis_object obj;
496 nis_result *res;
497 nis_error status;
498 ib_request *ibreq;
499 size_t namelen = strlen (name);
500 char buf1[namelen + 20];
501 char buf4[namelen + 20];
503 res = calloc (1, sizeof (nis_result));
504 if (res == NULL)
505 return NULL;
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_RES_STATUS (res) = NIS_NOMEMORY;
530 return res;
532 ibreq->ibr_obj.ibr_obj_len = 1;
534 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY,
535 (xdrproc_t) _xdr_ib_request,
536 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
537 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
538 NIS_RES_STATUS (res) = status;
540 nis_free_request (ibreq);
542 return res;
545 nis_result *
546 nis_remove_entry (const_nis_name name, const nis_object *obj,
547 unsigned int flags)
549 nis_result *res;
550 ib_request *ibreq;
551 nis_error status;
553 res = calloc (1, sizeof (nis_result));
554 if (res == NULL)
555 return NULL;
557 if (name == NULL)
559 NIS_RES_STATUS (res) = NIS_BADNAME;
560 return res;
563 if ((ibreq =__create_ib_request (name, flags)) == NULL)
565 NIS_RES_STATUS (res) = NIS_BADNAME;
566 return res;
569 if (obj != NULL)
571 ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL);
572 if (ibreq->ibr_obj.ibr_obj_val == NULL)
574 NIS_RES_STATUS (res) = NIS_NOMEMORY;
575 return res;
577 ibreq->ibr_obj.ibr_obj_len = 1;
580 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE,
581 (xdrproc_t) _xdr_ib_request,
582 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
583 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
584 NIS_RES_STATUS (res) = status;
586 nis_free_request (ibreq);
588 return res;
591 nis_result *
592 nis_first_entry (const_nis_name name)
594 nis_result *res;
595 ib_request *ibreq;
596 nis_error status;
598 res = calloc (1, sizeof (nis_result));
599 if (res == NULL)
600 return NULL;
602 if (name == NULL)
604 NIS_RES_STATUS (res) = NIS_BADNAME;
605 return res;
608 if ((ibreq =__create_ib_request (name, 0)) == NULL)
610 NIS_RES_STATUS (res) = NIS_BADNAME;
611 return res;
614 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST,
615 (xdrproc_t) _xdr_ib_request,
616 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
617 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
618 NIS_RES_STATUS (res) = status;
620 nis_free_request (ibreq);
622 return res;
625 nis_result *
626 nis_next_entry (const_nis_name name, const netobj *cookie)
628 nis_result *res;
629 ib_request *ibreq;
630 nis_error status;
632 res = calloc (1, sizeof (nis_result));
633 if (res == NULL)
634 return NULL;
636 if (name == NULL)
638 NIS_RES_STATUS (res) = NIS_BADNAME;
639 return res;
642 if (( ibreq =__create_ib_request (name, 0)) == NULL)
644 NIS_RES_STATUS (res) = NIS_BADNAME;
645 return res;
648 if (cookie != NULL)
650 ibreq->ibr_cookie.n_bytes = cookie->n_bytes;
651 ibreq->ibr_cookie.n_len = cookie->n_len;
654 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT,
655 (xdrproc_t) _xdr_ib_request,
656 (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result,
657 (caddr_t) res, 0, NULL)) != NIS_SUCCESS)
658 NIS_RES_STATUS (res) = status;
660 if (cookie != NULL)
662 /* Don't give cookie free, it is not from us */
663 ibreq->ibr_cookie.n_bytes = NULL;
664 ibreq->ibr_cookie.n_len = 0;
667 nis_free_request (ibreq);
669 return res;