1 /* Copyright (c) 1997, 1998, 1999, 2003 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
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
;
33 size_t search_len
= 0;
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
]);
192 if (ibreq
->ibr_name
== NULL
)
194 nis_free_request (ibreq
);
195 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
202 names
[name_nr
] = ibreq
->ibr_name
;
210 directory_obj
*dir
= NULL
;
212 memset (res
, '\0', sizeof (nis_result
));
214 status
= __nisfind_server (ibreq
->ibr_name
, &dir
);
215 if (status
!= NIS_SUCCESS
)
217 nis_free_request (ibreq
);
218 NIS_RES_STATUS (res
) = status
;
222 status
= __nisbind_create (&bptr
, dir
->do_servers
.do_servers_val
,
223 dir
->do_servers
.do_servers_len
, flags
);
224 if (status
!= NIS_SUCCESS
)
226 nis_free_request (ibreq
);
227 NIS_RES_STATUS (res
) = status
;
228 nis_free_directory (dir
);
232 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
233 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
235 __nisbind_destroy (&bptr
);
236 nis_free_directory (dir
);
237 nis_free_request (ibreq
);
238 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
242 if (callback
!= NULL
)
244 cb
= __nis_create_callback (callback
, userdata
, flags
);
245 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
246 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
250 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
251 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
252 (xdrproc_t
) _xdr_nis_result
,
253 (caddr_t
) res
, RPCTIMEOUT
);
255 if (clnt_status
!= RPC_SUCCESS
)
256 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
258 switch (NIS_RES_STATUS (res
))
263 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
&&
264 flags
& FOLLOW_LINKS
) /* We are following links. */
266 free (ibreq
->ibr_name
);
267 ibreq
->ibr_name
= NULL
;
268 /* If we hit the link limit, bail. */
269 if (count_links
> NIS_MAXLINKS
)
271 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
277 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
278 if (ibreq
->ibr_name
== NULL
)
280 nis_free_request (ibreq
);
281 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
284 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
285 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
287 ibreq
->ibr_srch
.ibr_srch_len
=
288 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
289 ibreq
->ibr_srch
.ibr_srch_val
=
290 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
292 nis_freeresult (res
);
293 res
= calloc (1, sizeof (nis_result
));
298 __nisbind_destroy (&bptr
);
299 nis_free_directory (dir
);
302 first_try
= 1; /* Try at first the old binding */
305 else if ((flags
& FOLLOW_PATH
) &&
306 NIS_RES_STATUS (res
) == NIS_PARTIAL
)
310 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
311 tableptr
= tablepath
;
314 if (tableptr
== NULL
)
319 free (ibreq
->ibr_name
);
320 ibreq
->ibr_name
= strsep (&tableptr
, ":");
321 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
323 ibreq
->ibr_name
= strdup ("");
324 if (ibreq
->ibr_name
== NULL
)
326 nis_free_request (ibreq
);
327 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
334 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
335 nis_freeresult (res
);
336 res
= calloc (1, sizeof (nis_result
));
337 if (res
== NULL
|| ibreq
->ibr_name
== NULL
)
340 nis_free_request (ibreq
);
343 __nisbind_destroy (&bptr
);
344 nis_free_directory (dir
);
357 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
358 NIS_RES_STATUS (res
) = cb
->result
;
360 if (!(flags
& ALL_RESULTS
))
366 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
367 tableptr
= tablepath
;
370 if (tableptr
== NULL
)
375 free (ibreq
->ibr_name
);
376 ibreq
->ibr_name
= strsep (&tableptr
, ":");
377 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
379 ibreq
->ibr_name
= strdup ("");
383 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
384 if (ibreq
->ibr_name
== NULL
)
386 nis_free_request (ibreq
);
387 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
393 case NIS_SYSTEMERROR
:
396 /* If we had first tried the old binding, do nothing, but
400 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
403 break; /* No more servers to search */
405 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
407 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
410 break; /* No more servers to search */
419 /* Try the next domainname if we don't follow a link. */
420 free (ibreq
->ibr_name
);
421 ibreq
->ibr_name
= NULL
;
424 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
429 if (names
[name_nr
] == NULL
)
434 ibreq
->ibr_name
= strdup (names
[name_nr
]);
435 if (ibreq
->ibr_name
== NULL
)
437 nis_free_request (ibreq
);
438 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
441 first_try
= 1; /* Try old binding at first */
450 __nis_destroy_callback (cb
);
451 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
452 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
455 __nisbind_destroy (&bptr
);
456 nis_free_directory (dir
);
459 if (names
!= namebuf
)
460 nis_freenames (names
);
462 nis_free_request (ibreq
);
468 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
474 size_t namelen
= strlen (name
);
475 char buf1
[namelen
+ 20];
476 char buf4
[namelen
+ 20];
478 res
= calloc (1, sizeof (nis_result
));
484 NIS_RES_STATUS (res
) = NIS_BADNAME
;
488 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
490 NIS_RES_STATUS (res
) = NIS_BADNAME
;
494 memcpy (&obj
, obj2
, sizeof (nis_object
));
496 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
497 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
499 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
500 obj
.zo_owner
= nis_local_principal ();
502 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
503 obj
.zo_group
= nis_local_group ();
505 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
507 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
508 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
510 nis_free_request (ibreq
);
511 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
514 ibreq
->ibr_obj
.ibr_obj_len
= 1;
516 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
517 (xdrproc_t
) _xdr_ib_request
,
519 (xdrproc_t
) _xdr_nis_result
,
520 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
521 NIS_RES_STATUS (res
) = status
;
523 nis_free_request (ibreq
);
529 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
536 size_t namelen
= strlen (name
);
537 char buf1
[namelen
+ 20];
538 char buf4
[namelen
+ 20];
540 res
= calloc (1, sizeof (nis_result
));
544 if (( ibreq
=__create_ib_request (name
, flags
)) == NULL
)
546 NIS_RES_STATUS (res
) = NIS_BADNAME
;
550 memcpy (&obj
, obj2
, sizeof (nis_object
));
552 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
553 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
555 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
556 obj
.zo_owner
= nis_local_principal ();
558 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
559 obj
.zo_group
= nis_local_group ();
561 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
563 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
564 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
566 nis_free_request (ibreq
);
567 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
570 ibreq
->ibr_obj
.ibr_obj_len
= 1;
572 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
573 (xdrproc_t
) _xdr_ib_request
,
574 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
575 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
576 NIS_RES_STATUS (res
) = status
;
578 nis_free_request (ibreq
);
584 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
591 res
= calloc (1, sizeof (nis_result
));
597 NIS_RES_STATUS (res
) = NIS_BADNAME
;
601 if ((ibreq
=__create_ib_request (name
, flags
)) == NULL
)
603 NIS_RES_STATUS (res
) = NIS_BADNAME
;
609 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
610 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
612 nis_free_request (ibreq
);
613 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
616 ibreq
->ibr_obj
.ibr_obj_len
= 1;
619 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
620 (xdrproc_t
) _xdr_ib_request
,
621 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
622 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
623 NIS_RES_STATUS (res
) = status
;
625 nis_free_request (ibreq
);
631 nis_first_entry (const_nis_name name
)
637 res
= calloc (1, sizeof (nis_result
));
643 NIS_RES_STATUS (res
) = NIS_BADNAME
;
647 ibreq
= __create_ib_request (name
, 0);
650 NIS_RES_STATUS (res
) = NIS_BADNAME
;
654 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
655 (xdrproc_t
) _xdr_ib_request
,
656 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
657 (caddr_t
) res
, 0, NULL
);
659 if (status
!= NIS_SUCCESS
)
660 NIS_RES_STATUS (res
) = status
;
662 nis_free_request (ibreq
);
668 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
674 res
= calloc (1, sizeof (nis_result
));
680 NIS_RES_STATUS (res
) = NIS_BADNAME
;
684 ibreq
= __create_ib_request (name
, 0);
687 NIS_RES_STATUS (res
) = NIS_BADNAME
;
693 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
694 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
697 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
698 (xdrproc_t
) _xdr_ib_request
,
699 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
700 (caddr_t
) res
, 0, NULL
);
702 if (status
!= NIS_SUCCESS
)
703 NIS_RES_STATUS (res
) = status
;
707 /* Don't give cookie free, it is not from us */
708 ibreq
->ibr_cookie
.n_bytes
= NULL
;
709 ibreq
->ibr_cookie
.n_len
= 0;
712 nis_free_request (ibreq
);