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 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. */
21 #include <rpcsvc/nis.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;
40 ibreq->ibr_flags = flags;
42 cptr = strcpy (buf, name);
44 /* Not of "[key=value,key=value,...],foo.." format? */
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);
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';
61 ibreq->ibr_name[0] = '\0';
63 ibreq->ibr_name = strdup (ibreq->ibr_name);
64 if (ibreq->ibr_name == NULL)
67 ++cptr; /* Remove "[" */
69 while (cptr != NULL && cptr[0] != '\0')
72 char *val = strchr (cptr, '=');
74 cptr = strchr (key, ',');
80 nis_free_request (ibreq);
84 if ((search_len + 1) >= size)
87 search_val = realloc (search_val, size * sizeof (nis_attr));
88 if (search_val == NULL)
91 search_val[search_len].zattr_ndx = strdup (key);
92 if ((search_val[search_len].zattr_ndx) == 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)
103 ibreq->ibr_srch.ibr_srch_val = search_val;
104 ibreq->ibr_srch.ibr_srch_len = search_len;
109 static struct timeval RPCTIMEOUT = {10, 0};
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;
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);
138 nis_freeresult (res);
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));
153 enum clnt_stat clnt_status;
154 int count_links = 0; /* We will only follow NIS_MAXLINKS links! */
157 nis_name namebuf[2] = {NULL, NULL};
160 char *tableptr, *tablepath = NULL;
161 int have_tablepath = 0;
162 int first_try = 0; /* Do we try the old binding at first ? */
169 NIS_RES_STATUS (res) = NIS_BADNAME;
173 if ((ibreq = __create_ib_request (name, flags)) == NULL)
175 NIS_RES_STATUS (res) = NIS_BADNAME;
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;
187 nis_free_request (ibreq);
188 NIS_RES_STATUS (res) = NIS_BADNAME;
191 ibreq->ibr_name = strdup (names[name_nr]);
196 names[name_nr] = ibreq->ibr_name;
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;
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);
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;
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;
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;
252 switch (NIS_RES_STATUS (res))
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;
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));
286 __nisbind_destroy (&bptr);
287 nis_free_directory (dir);
290 first_try = 1; /* Try at first the old binding */
293 else if ((flags & FOLLOW_PATH) &&
294 NIS_RES_STATUS (res) == NIS_PARTIAL)
298 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
299 tableptr = tablepath;
302 if (tableptr == NULL)
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 ("");
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)
323 nis_free_request (ibreq);
326 __nisbind_destroy (&bptr);
327 nis_free_directory (dir);
340 __nis_do_callback (&bptr, &res->cookie, cb);
341 NIS_RES_STATUS (res) = cb->result;
343 if (!(flags & ALL_RESULTS))
349 tablepath = __get_tablepath (ibreq->ibr_name, &bptr);
350 tableptr = tablepath;
353 if (tableptr == NULL)
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 ("");
366 ibreq->ibr_name = strdup (ibreq->ibr_name);
370 case NIS_SYSTEMERROR:
373 /* If we had first tried the old binding, do nothing, but
377 if (__nisbind_next (&bptr) != NIS_SUCCESS)
380 break; /* No more servers to search */
382 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
384 if (__nisbind_next (&bptr) != NIS_SUCCESS)
387 break; /* No more servers to search */
396 /* Try the next domainname if we don't follow a link. */
399 free (ibreq->ibr_name);
400 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
405 if (names[name_nr] == NULL)
410 ibreq->ibr_name = names[name_nr];
411 first_try = 1; /* Try old binding at first */
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);
438 nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags)
444 size_t namelen = strlen (name);
445 char buf1[namelen + 20];
446 char buf4[namelen + 20];
448 res = calloc (1, sizeof (nis_result));
454 NIS_RES_STATUS (res) = NIS_BADNAME;
458 if ((ibreq = __create_ib_request (name, flags)) == NULL)
460 NIS_RES_STATUS (res) = NIS_BADNAME;
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;
484 ibreq->ibr_obj.ibr_obj_len = 1;
486 if ((status = __do_niscall (ibreq->ibr_name, NIS_IBADD,
487 (xdrproc_t) _xdr_ib_request,
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);
499 nis_modify_entry (const_nis_name name, const nis_object *obj2,
506 size_t namelen = strlen (name);
507 char buf1[namelen + 20];
508 char buf4[namelen + 20];
510 res = calloc (1, sizeof (nis_result));
514 if (( ibreq =__create_ib_request (name, flags)) == NULL)
516 NIS_RES_STATUS (res) = NIS_BADNAME;
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;
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);
554 nis_remove_entry (const_nis_name name, const nis_object *obj,
561 res = calloc (1, sizeof (nis_result));
567 NIS_RES_STATUS (res) = NIS_BADNAME;
571 if ((ibreq =__create_ib_request (name, flags)) == NULL)
573 NIS_RES_STATUS (res) = NIS_BADNAME;
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;
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);
601 nis_first_entry (const_nis_name name)
607 res = calloc (1, sizeof (nis_result));
613 NIS_RES_STATUS (res) = NIS_BADNAME;
617 ibreq = __create_ib_request (name, 0);
620 NIS_RES_STATUS (res) = NIS_BADNAME;
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);
638 nis_next_entry (const_nis_name name, const netobj *cookie)
644 res = calloc (1, sizeof (nis_result));
650 NIS_RES_STATUS (res) = NIS_BADNAME;
654 ibreq = __create_ib_request (name, 0);
657 NIS_RES_STATUS (res) = NIS_BADNAME;
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;
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);