1 /* Copyright (c) 1997-1999,2003,2004,2005,2006 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 (struct ib_request
));
31 nis_attr
*search_val
= NULL
;
32 size_t search_len
= 0;
38 ibreq
->ibr_flags
= flags
;
40 char *cptr
= strdupa (name
);
42 /* Not of "[key=value,key=value,...],foo.." format? */
45 ibreq
->ibr_name
= strdup (cptr
);
46 if (ibreq
->ibr_name
== NULL
)
54 /* "[key=value,...],foo" format */
55 ibreq
->ibr_name
= strchr (cptr
, ']');
56 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[1] != ',')
58 /* The object has not really been built yet so we use free. */
63 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
64 if (ibreq
->ibr_name
[-1] == ',')
65 ibreq
->ibr_name
[-1] = '\0';
67 ibreq
->ibr_name
[0] = '\0';
69 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
70 if (ibreq
->ibr_name
== NULL
)
73 while (search_len
-- > 0)
75 free (search_val
[search_len
].zattr_ndx
);
76 free (search_val
[search_len
].zattr_val
.zattr_val_val
);
79 nis_free_request (ibreq
);
83 ++cptr
; /* Remove "[" */
85 while (cptr
!= NULL
&& cptr
[0] != '\0')
88 char *val
= strchr (cptr
, '=');
90 cptr
= strchr (key
, ',');
96 nis_free_request (ibreq
);
100 if ((search_len
+ 1) >= size
)
103 nis_attr
*newp
= realloc (search_val
, size
* sizeof (nis_attr
));
108 search_val
[search_len
].zattr_ndx
= strdup (key
);
109 if ((search_val
[search_len
].zattr_ndx
) == NULL
)
112 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
113 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
114 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
116 free (search_val
[search_len
].zattr_ndx
);
123 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
124 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
129 static const struct timeval RPCTIMEOUT
= {10, 0};
132 __get_tablepath (char *name
, dir_binding
*bptr
)
134 enum clnt_stat result
;
136 struct ns_request req
;
138 memset (&res
, '\0', sizeof (res
));
141 req
.ns_object
.ns_object_len
= 0;
142 req
.ns_object
.ns_object_val
= NULL
;
144 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
145 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
146 (caddr_t
) &res
, RPCTIMEOUT
);
149 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (&res
) == NIS_SUCCESS
150 && __type_of (NIS_RES_OBJECT (&res
)) == NIS_TABLE_OBJ
)
151 cptr
= NIS_RES_OBJECT (&res
)->TA_data
.ta_path
;
155 return strdup (cptr
);
159 nis_list (const_nis_name name
, unsigned int flags
,
160 int (*callback
) (const_nis_name name
,
161 const nis_object
*object
,
162 const void *userdata
),
163 const void *userdata
)
165 nis_result
*res
= calloc (1, sizeof (nis_result
));
168 enum clnt_stat clnt_status
;
169 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
172 nis_name namebuf
[2] = {NULL
, NULL
};
176 char *tablepath
= NULL
;
177 int first_try
= 0; /* Do we try the old binding at first ? */
184 NIS_RES_STATUS (res
) = NIS_BADNAME
;
188 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
190 NIS_RES_STATUS (res
) = NIS_BADNAME
;
194 if ((flags
& EXPAND_NAME
)
195 && ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
197 names
= nis_getnames (ibreq
->ibr_name
);
198 free (ibreq
->ibr_name
);
199 ibreq
->ibr_name
= NULL
;
202 nis_free_request (ibreq
);
203 NIS_RES_STATUS (res
) = NIS_BADNAME
;
206 ibreq
->ibr_name
= strdup (names
[name_nr
]);
207 if (ibreq
->ibr_name
== NULL
)
209 nis_freenames (names
);
210 nis_free_request (ibreq
);
211 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
218 names
[name_nr
] = ibreq
->ibr_name
;
226 directory_obj
*dir
= NULL
;
228 memset (res
, '\0', sizeof (nis_result
));
230 status
= __nisfind_server (ibreq
->ibr_name
, &dir
);
231 if (status
!= NIS_SUCCESS
)
233 NIS_RES_STATUS (res
) = status
;
237 status
= __nisbind_create (&bptr
, dir
->do_servers
.do_servers_val
,
238 dir
->do_servers
.do_servers_len
, flags
);
239 if (status
!= NIS_SUCCESS
)
241 NIS_RES_STATUS (res
) = status
;
245 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
246 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
248 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
252 if (callback
!= NULL
)
254 cb
= __nis_create_callback (callback
, userdata
, flags
);
255 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
256 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
260 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
261 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
262 (xdrproc_t
) _xdr_nis_result
,
263 (caddr_t
) res
, RPCTIMEOUT
);
265 if (clnt_status
!= RPC_SUCCESS
)
266 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
268 switch (NIS_RES_STATUS (res
))
273 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
274 && (flags
& FOLLOW_LINKS
)) /* We are following links. */
276 free (ibreq
->ibr_name
);
277 ibreq
->ibr_name
= NULL
;
278 /* If we hit the link limit, bail. */
279 if (count_links
> NIS_MAXLINKS
)
281 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
287 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
288 if (ibreq
->ibr_name
== NULL
)
290 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
292 __nisbind_destroy (&bptr
);
294 nis_free_directory (dir
);
299 __nis_destroy_callback (cb
);
300 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
301 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
303 if (names
!= namebuf
)
304 nis_freenames (names
);
305 nis_free_request (ibreq
);
308 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
309 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
311 ibreq
->ibr_srch
.ibr_srch_len
=
312 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
313 ibreq
->ibr_srch
.ibr_srch_val
=
314 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
316 /* The following is a non-obvious optimization. A
317 nis_freeresult call would call xdr_free as the
318 following code. But it also would unnecessarily
319 free the result structure. We avoid this here
320 along with the necessary tests. */
322 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *)res
);
323 memset (res
, '\0', sizeof (*res
));
325 nis_freeresult (res
);
326 res
= calloc (1, sizeof (nis_result
));
330 first_try
= 1; /* Try at first the old binding */
333 else if ((flags
& FOLLOW_PATH
)
334 && NIS_RES_STATUS (res
) == NIS_PARTIAL
)
336 if (tablepath
== NULL
)
338 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
339 tableptr
= tablepath
;
341 if (tableptr
== NULL
)
346 free (ibreq
->ibr_name
);
347 ibreq
->ibr_name
= strsep (&tableptr
, ":");
348 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
350 ibreq
->ibr_name
= strdup ("");
351 if (ibreq
->ibr_name
== NULL
)
353 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
360 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
361 /* The following is a non-obvious optimization. A
362 nis_freeresult call would call xdr_free as the
363 following code. But it also would unnecessarily
364 free the result structure. We avoid this here
365 along with the necessary tests. */
367 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *)res
);
368 memset (res
, '\0', sizeof (*res
));
369 if (ibreq
->ibr_name
== NULL
)
371 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
375 nis_freeresult (res
);
376 res
= calloc (1, sizeof (nis_result
));
377 if (res
== NULL
|| ibreq
->ibr_name
== NULL
)
394 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
395 NIS_RES_STATUS (res
) = cb
->result
;
397 if (!(flags
& ALL_RESULTS
))
401 if (tablepath
== NULL
)
403 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
404 tableptr
= tablepath
;
406 if (tableptr
== NULL
)
411 free (ibreq
->ibr_name
);
412 ibreq
->ibr_name
= strsep (&tableptr
, ":");
413 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
415 ibreq
->ibr_name
= strdup ("");
419 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
420 if (ibreq
->ibr_name
== NULL
)
422 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
428 case NIS_SYSTEMERROR
:
431 /* If we had first tried the old binding, do nothing, but
435 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
438 break; /* No more servers to search */
440 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
442 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
445 break; /* No more servers to search */
454 /* Try the next domainname if we don't follow a link. */
455 free (ibreq
->ibr_name
);
456 ibreq
->ibr_name
= NULL
;
459 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
464 if (names
[name_nr
] == NULL
)
469 ibreq
->ibr_name
= strdup (names
[name_nr
]);
470 if (ibreq
->ibr_name
== NULL
)
472 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
475 first_try
= 1; /* Try old binding at first */
484 __nis_destroy_callback (cb
);
485 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
486 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
490 __nisbind_destroy (&bptr
);
491 nis_free_directory (dir
);
496 if (names
!= namebuf
)
497 nis_freenames (names
);
499 nis_free_request (ibreq
);
503 libnsl_hidden_def (nis_list
)
506 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
508 nis_result
*res
= calloc (1, sizeof (nis_result
));
514 NIS_RES_STATUS (res
) = NIS_BADNAME
;
518 size_t namelen
= strlen (name
);
519 char buf1
[namelen
+ 20];
520 char buf4
[namelen
+ 20];
522 ib_request
*ibreq
= __create_ib_request (name
, flags
);
525 NIS_RES_STATUS (res
) = NIS_BADNAME
;
530 memcpy (&obj
, obj2
, sizeof (nis_object
));
532 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
533 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
535 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
536 obj
.zo_owner
= nis_local_principal ();
538 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
539 obj
.zo_group
= nis_local_group ();
541 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
543 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
544 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
546 nis_free_request (ibreq
);
547 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
550 ibreq
->ibr_obj
.ibr_obj_len
= 1;
552 nis_error status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
553 (xdrproc_t
) _xdr_ib_request
,
555 (xdrproc_t
) _xdr_nis_result
,
556 (caddr_t
) res
, 0, NULL
);
557 if (status
!= NIS_SUCCESS
)
558 NIS_RES_STATUS (res
) = status
;
560 nis_free_request (ibreq
);
566 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
573 size_t namelen
= strlen (name
);
574 char buf1
[namelen
+ 20];
575 char buf4
[namelen
+ 20];
577 res
= calloc (1, sizeof (nis_result
));
581 if (( ibreq
=__create_ib_request (name
, flags
)) == NULL
)
583 NIS_RES_STATUS (res
) = NIS_BADNAME
;
587 memcpy (&obj
, obj2
, sizeof (nis_object
));
589 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
590 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
592 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
593 obj
.zo_owner
= nis_local_principal ();
595 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
596 obj
.zo_group
= nis_local_group ();
598 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
600 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
601 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
603 nis_free_request (ibreq
);
604 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
607 ibreq
->ibr_obj
.ibr_obj_len
= 1;
609 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
610 (xdrproc_t
) _xdr_ib_request
,
611 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
612 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
613 NIS_RES_STATUS (res
) = status
;
615 nis_free_request (ibreq
);
621 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
628 res
= calloc (1, sizeof (nis_result
));
634 NIS_RES_STATUS (res
) = NIS_BADNAME
;
638 if ((ibreq
=__create_ib_request (name
, flags
)) == NULL
)
640 NIS_RES_STATUS (res
) = NIS_BADNAME
;
646 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
647 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
649 nis_free_request (ibreq
);
650 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
653 ibreq
->ibr_obj
.ibr_obj_len
= 1;
656 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
657 (xdrproc_t
) _xdr_ib_request
,
658 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
659 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
660 NIS_RES_STATUS (res
) = status
;
662 nis_free_request (ibreq
);
668 nis_first_entry (const_nis_name name
)
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
;
691 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
692 (xdrproc_t
) _xdr_ib_request
,
693 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
694 (caddr_t
) res
, 0, NULL
);
696 if (status
!= NIS_SUCCESS
)
697 NIS_RES_STATUS (res
) = status
;
699 nis_free_request (ibreq
);
705 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
711 res
= calloc (1, sizeof (nis_result
));
717 NIS_RES_STATUS (res
) = NIS_BADNAME
;
721 ibreq
= __create_ib_request (name
, 0);
724 NIS_RES_STATUS (res
) = NIS_BADNAME
;
730 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
731 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
734 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
735 (xdrproc_t
) _xdr_ib_request
,
736 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
737 (caddr_t
) res
, 0, NULL
);
739 if (status
!= NIS_SUCCESS
)
740 NIS_RES_STATUS (res
) = status
;
744 /* Don't give cookie free, it is not from us */
745 ibreq
->ibr_cookie
.n_bytes
= NULL
;
746 ibreq
->ibr_cookie
.n_len
= 0;
749 nis_free_request (ibreq
);