1 /* Copyright (c) 1997-1999, 2003, 2004, 2005, 2006, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <rpcsvc/nis.h>
26 #include "nis_intern.h"
31 __create_ib_request (const_nis_name name
, unsigned int flags
)
33 struct ib_request
*ibreq
= calloc (1, sizeof (struct ib_request
));
34 nis_attr
*search_val
= NULL
;
35 size_t search_len
= 0;
41 ibreq
->ibr_flags
= flags
;
43 char *cptr
= strdupa (name
);
45 /* Not of "[key=value,key=value,...],foo.." format? */
48 ibreq
->ibr_name
= strdup (cptr
);
49 if (ibreq
->ibr_name
== NULL
)
57 /* "[key=value,...],foo" format */
58 ibreq
->ibr_name
= strchr (cptr
, ']');
59 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[1] != ',')
61 /* The object has not really been built yet so we use free. */
66 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
67 if (ibreq
->ibr_name
[-1] == ',')
68 ibreq
->ibr_name
[-1] = '\0';
70 ibreq
->ibr_name
[0] = '\0';
72 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
73 if (ibreq
->ibr_name
== NULL
)
76 while (search_len
-- > 0)
78 free (search_val
[search_len
].zattr_ndx
);
79 free (search_val
[search_len
].zattr_val
.zattr_val_val
);
82 nis_free_request (ibreq
);
86 ++cptr
; /* Remove "[" */
88 while (cptr
!= NULL
&& cptr
[0] != '\0')
91 char *val
= strchr (cptr
, '=');
93 cptr
= strchr (key
, ',');
97 if (__builtin_expect (val
== NULL
, 0))
99 nis_free_request (ibreq
);
103 if (search_len
+ 1 >= size
)
106 nis_attr
*newp
= realloc (search_val
, size
* sizeof (nis_attr
));
111 search_val
[search_len
].zattr_ndx
= strdup (key
);
112 if (search_val
[search_len
].zattr_ndx
== NULL
)
115 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
116 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
117 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
119 free (search_val
[search_len
].zattr_ndx
);
126 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
127 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
131 libnsl_hidden_def (__create_ib_request
)
133 static const struct timeval RPCTIMEOUT
= {10, 0};
136 get_tablepath (char *name
, dir_binding
*bptr
)
138 enum clnt_stat result
;
140 struct ns_request req
;
142 memset (&res
, '\0', sizeof (res
));
145 req
.ns_object
.ns_object_len
= 0;
146 req
.ns_object
.ns_object_val
= NULL
;
148 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
149 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
150 (caddr_t
) &res
, RPCTIMEOUT
);
153 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (&res
) == NIS_SUCCESS
154 && __type_of (NIS_RES_OBJECT (&res
)) == NIS_TABLE_OBJ
)
155 cptr
= NIS_RES_OBJECT (&res
)->TA_data
.ta_path
;
159 char *str
= strdup (cptr
);
161 if (result
== RPC_SUCCESS
)
162 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) &res
);
169 __follow_path (char **tablepath
, char **tableptr
, struct ib_request
*ibreq
,
172 if (*tablepath
== NULL
)
174 *tablepath
= get_tablepath (ibreq
->ibr_name
, bptr
);
175 if (*tablepath
== NULL
)
178 *tableptr
= *tablepath
;
180 if (*tableptr
== NULL
)
183 char *newname
= strsep (tableptr
, ":");
184 if (newname
[0] == '\0')
187 newname
= strdup (newname
);
191 free (ibreq
->ibr_name
);
192 ibreq
->ibr_name
= newname
;
196 libnsl_hidden_def (__follow_path
)
200 nis_list (const_nis_name name
, unsigned int flags
,
201 int (*callback
) (const_nis_name name
,
202 const nis_object
*object
,
203 const void *userdata
),
204 const void *userdata
)
206 nis_result
*res
= malloc (sizeof (nis_result
));
209 enum clnt_stat clnt_status
;
210 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
213 nis_name namebuf
[2] = {NULL
, NULL
};
217 char *tablepath
= NULL
;
218 int first_try
= 0; /* Do we try the old binding at first ? */
219 nis_result
*allres
= NULL
;
226 status
= NIS_BADNAME
;
228 nis_freeresult (allres
);
229 memset (res
, '\0', sizeof (nis_result
));
230 NIS_RES_STATUS (res
) = status
;
234 ibreq
= __create_ib_request (name
, flags
);
237 status
= NIS_BADNAME
;
241 if ((flags
& EXPAND_NAME
)
242 && ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
244 names
= nis_getnames (ibreq
->ibr_name
);
245 free (ibreq
->ibr_name
);
246 ibreq
->ibr_name
= NULL
;
249 nis_free_request (ibreq
);
250 status
= NIS_BADNAME
;
253 ibreq
->ibr_name
= strdup (names
[name_nr
]);
254 if (ibreq
->ibr_name
== NULL
)
256 nis_freenames (names
);
257 nis_free_request (ibreq
);
258 status
= NIS_NOMEMORY
;
265 names
[name_nr
] = ibreq
->ibr_name
;
273 directory_obj
*dir
= NULL
;
275 memset (res
, '\0', sizeof (nis_result
));
277 status
= __nisfind_server (ibreq
->ibr_name
,
278 ibreq
->ibr_srch
.ibr_srch_val
!= NULL
,
279 &dir
, &bptr
, flags
& ~MASTER_ONLY
);
280 if (status
!= NIS_SUCCESS
)
282 NIS_RES_STATUS (res
) = status
;
286 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
287 if (__builtin_expect (__nisbind_next (&bptr
) != NIS_SUCCESS
, 0))
289 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
293 if (callback
!= NULL
)
296 cb
= __nis_create_callback (callback
, userdata
, flags
);
297 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
298 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
302 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
303 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
304 (xdrproc_t
) _xdr_nis_result
,
305 (caddr_t
) res
, RPCTIMEOUT
);
307 if (__builtin_expect (clnt_status
!= RPC_SUCCESS
, 0))
308 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
310 switch (NIS_RES_STATUS (res
))
315 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
316 && (flags
& FOLLOW_LINKS
)) /* We are following links. */
318 free (ibreq
->ibr_name
);
319 ibreq
->ibr_name
= NULL
;
320 /* If we hit the link limit, bail. */
321 if (__builtin_expect (count_links
> NIS_MAXLINKS
, 0))
323 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
329 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
330 if (ibreq
->ibr_name
== NULL
)
332 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
334 __nisbind_destroy (&bptr
);
335 nis_free_directory (dir
);
340 __nis_destroy_callback (cb
);
341 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
342 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
344 if (names
!= namebuf
)
345 nis_freenames (names
);
346 nis_free_request (ibreq
);
347 nis_freeresult (allres
);
350 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
351 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
353 ibreq
->ibr_srch
.ibr_srch_len
=
354 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
355 ibreq
->ibr_srch
.ibr_srch_val
=
356 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
358 /* The following is a non-obvious optimization. A
359 nis_freeresult call would call xdr_free as the
360 following code. But it also would unnecessarily
361 free the result structure. We avoid this here
362 along with the necessary tests. */
363 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *)res
);
364 memset (res
, '\0', sizeof (*res
));
365 first_try
= 1; /* Try at first the old binding */
368 else if ((flags
& FOLLOW_PATH
)
369 && NIS_RES_STATUS (res
) == NIS_PARTIAL
)
371 clnt_status
= __follow_path (&tablepath
, &tableptr
, ibreq
,
373 if (clnt_status
!= NIS_SUCCESS
)
375 if (clnt_status
== NIS_NOMEMORY
)
376 NIS_RES_STATUS (res
) = clnt_status
;
381 /* The following is a non-obvious optimization. A
382 nis_freeresult call would call xdr_free as the
383 following code. But it also would unnecessarily
384 free the result structure. We avoid this here
385 along with the necessary tests. */
386 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
387 memset (res
, '\0', sizeof (*res
));
392 else if ((flags
& (FOLLOW_PATH
| ALL_RESULTS
))
393 == (FOLLOW_PATH
| ALL_RESULTS
))
398 res
= malloc (sizeof (nis_result
));
403 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
406 NIS_RES_STATUS (res
) = NIS_RES_STATUS (allres
);
410 nis_object
*objects_val
411 = realloc (NIS_RES_OBJECT (allres
),
412 (NIS_RES_NUMOBJ (allres
)
413 + NIS_RES_NUMOBJ (res
))
414 * sizeof (nis_object
));
415 if (objects_val
== NULL
)
417 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
420 NIS_RES_OBJECT (allres
) = objects_val
;
421 memcpy (NIS_RES_OBJECT (allres
) + NIS_RES_NUMOBJ (allres
),
422 NIS_RES_OBJECT (res
),
423 NIS_RES_NUMOBJ (res
) * sizeof (nis_object
));
424 NIS_RES_NUMOBJ (allres
) += NIS_RES_NUMOBJ (res
);
425 NIS_RES_NUMOBJ (res
) = 0;
426 free (NIS_RES_OBJECT (res
));
427 NIS_RES_OBJECT (res
) = NULL
;
428 NIS_RES_STATUS (allres
) = NIS_RES_STATUS (res
);
429 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
431 clnt_status
= __follow_path (&tablepath
, &tableptr
, ibreq
,
433 if (clnt_status
!= NIS_SUCCESS
)
435 /* Prepare for the nis_freeresult call. */
436 memset (res
, '\0', sizeof (*res
));
438 if (clnt_status
== NIS_NOMEMORY
)
439 NIS_RES_STATUS (allres
) = clnt_status
;
449 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
450 NIS_RES_STATUS (res
) = cb
->result
;
452 if (!(flags
& ALL_RESULTS
))
457 = __follow_path (&tablepath
, &tableptr
, ibreq
, &bptr
);
458 if (clnt_status
!= NIS_SUCCESS
)
460 if (clnt_status
== NIS_NOMEMORY
)
461 NIS_RES_STATUS (res
) = clnt_status
;
467 case NIS_SYSTEMERROR
:
470 /* If we had first tried the old binding, do nothing, but
474 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
477 break; /* No more servers to search */
479 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
481 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
484 break; /* No more servers to search */
493 /* Try the next domainname if we don't follow a link. */
494 free (ibreq
->ibr_name
);
495 ibreq
->ibr_name
= NULL
;
496 if (__builtin_expect (count_links
, 0))
498 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
503 if (names
[name_nr
] == NULL
)
508 ibreq
->ibr_name
= strdup (names
[name_nr
]);
509 if (ibreq
->ibr_name
== NULL
)
511 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
514 first_try
= 1; /* Try old binding at first */
523 __nis_destroy_callback (cb
);
524 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
525 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
529 __nisbind_destroy (&bptr
);
530 nis_free_directory (dir
);
535 if (names
!= namebuf
)
536 nis_freenames (names
);
538 nis_free_request (ibreq
);
542 nis_freeresult (res
);
548 libnsl_hidden_def (nis_list
)
551 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
553 nis_result
*res
= calloc (1, sizeof (nis_result
));
559 NIS_RES_STATUS (res
) = NIS_BADNAME
;
563 ib_request
*ibreq
= __create_ib_request (name
, flags
);
566 NIS_RES_STATUS (res
) = NIS_BADNAME
;
571 memcpy (&obj
, obj2
, sizeof (nis_object
));
573 size_t namelen
= strlen (name
);
574 char buf1
[namelen
+ 20];
575 char buf4
[namelen
+ 20];
577 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
578 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
580 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
581 obj
.zo_owner
= nis_local_principal ();
583 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
584 obj
.zo_group
= nis_local_group ();
586 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
588 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
589 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
591 nis_free_request (ibreq
);
592 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
595 ibreq
->ibr_obj
.ibr_obj_len
= 1;
597 nis_error status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
598 (xdrproc_t
) _xdr_ib_request
,
600 (xdrproc_t
) _xdr_nis_result
,
601 (caddr_t
) res
, 0, NULL
);
602 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
603 NIS_RES_STATUS (res
) = status
;
605 nis_free_request (ibreq
);
611 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
618 size_t namelen
= strlen (name
);
619 char buf1
[namelen
+ 20];
620 char buf4
[namelen
+ 20];
622 res
= calloc (1, sizeof (nis_result
));
626 ibreq
= __create_ib_request (name
, flags
);
629 NIS_RES_STATUS (res
) = NIS_BADNAME
;
633 memcpy (&obj
, obj2
, sizeof (nis_object
));
635 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
636 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
638 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
639 obj
.zo_owner
= nis_local_principal ();
641 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
642 obj
.zo_group
= nis_local_group ();
644 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
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;
655 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
656 (xdrproc_t
) _xdr_ib_request
,
657 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
658 (caddr_t
) res
, 0, NULL
);
659 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
660 NIS_RES_STATUS (res
) = status
;
662 nis_free_request (ibreq
);
668 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
675 res
= calloc (1, sizeof (nis_result
));
681 NIS_RES_STATUS (res
) = NIS_BADNAME
;
685 ibreq
= __create_ib_request (name
, flags
);
688 NIS_RES_STATUS (res
) = NIS_BADNAME
;
694 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
695 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
697 nis_free_request (ibreq
);
698 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
701 ibreq
->ibr_obj
.ibr_obj_len
= 1;
704 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
705 (xdrproc_t
) _xdr_ib_request
,
706 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
707 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
708 NIS_RES_STATUS (res
) = status
;
710 nis_free_request (ibreq
);
716 nis_first_entry (const_nis_name name
)
722 res
= calloc (1, sizeof (nis_result
));
728 NIS_RES_STATUS (res
) = NIS_BADNAME
;
732 ibreq
= __create_ib_request (name
, 0);
735 NIS_RES_STATUS (res
) = NIS_BADNAME
;
739 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
740 (xdrproc_t
) _xdr_ib_request
,
741 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
742 (caddr_t
) res
, 0, NULL
);
744 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
745 NIS_RES_STATUS (res
) = status
;
747 nis_free_request (ibreq
);
753 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
759 res
= calloc (1, sizeof (nis_result
));
765 NIS_RES_STATUS (res
) = NIS_BADNAME
;
769 ibreq
= __create_ib_request (name
, 0);
772 NIS_RES_STATUS (res
) = NIS_BADNAME
;
778 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
779 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
782 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
783 (xdrproc_t
) _xdr_ib_request
,
784 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
785 (caddr_t
) res
, 0, NULL
);
787 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
788 NIS_RES_STATUS (res
) = status
;
792 /* Don't give cookie free, it is not from us */
793 ibreq
->ibr_cookie
.n_bytes
= NULL
;
794 ibreq
->ibr_cookie
.n_len
= 0;
797 nis_free_request (ibreq
);