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
, ',');
94 if (__builtin_expect (val
== NULL
, 0))
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 char *str
= strdup (cptr
);
157 if (result
== RPC_SUCCESS
)
158 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) &res
);
164 nis_list (const_nis_name name
, unsigned int flags
,
165 int (*callback
) (const_nis_name name
,
166 const nis_object
*object
,
167 const void *userdata
),
168 const void *userdata
)
170 nis_result
*res
= malloc (sizeof (nis_result
));
173 enum clnt_stat clnt_status
;
174 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
177 nis_name namebuf
[2] = {NULL
, NULL
};
181 char *tablepath
= NULL
;
182 int first_try
= 0; /* Do we try the old binding at first ? */
189 status
= NIS_BADNAME
;
191 memset (res
, '\0', sizeof (nis_result
));
192 NIS_RES_STATUS (res
) = status
;
196 ibreq
= create_ib_request (name
, flags
);
199 status
= NIS_BADNAME
;
203 if ((flags
& EXPAND_NAME
)
204 && ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
206 names
= nis_getnames (ibreq
->ibr_name
);
207 free (ibreq
->ibr_name
);
208 ibreq
->ibr_name
= NULL
;
211 nis_free_request (ibreq
);
212 status
= NIS_BADNAME
;
215 ibreq
->ibr_name
= strdup (names
[name_nr
]);
216 if (ibreq
->ibr_name
== NULL
)
218 nis_freenames (names
);
219 nis_free_request (ibreq
);
220 status
= NIS_NOMEMORY
;
227 names
[name_nr
] = ibreq
->ibr_name
;
235 directory_obj
*dir
= NULL
;
237 memset (res
, '\0', sizeof (nis_result
));
239 status
= __nisfind_server (ibreq
->ibr_name
, &dir
);
240 if (status
!= NIS_SUCCESS
)
242 NIS_RES_STATUS (res
) = status
;
246 status
= __nisbind_create (&bptr
, dir
->do_servers
.do_servers_val
,
247 dir
->do_servers
.do_servers_len
, flags
);
248 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
250 NIS_RES_STATUS (res
) = status
;
254 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
255 if (__builtin_expect (__nisbind_next (&bptr
) != NIS_SUCCESS
, 0))
257 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
261 if (callback
!= NULL
)
263 cb
= __nis_create_callback (callback
, userdata
, flags
);
264 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
265 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
269 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
270 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
271 (xdrproc_t
) _xdr_nis_result
,
272 (caddr_t
) res
, RPCTIMEOUT
);
274 if (__builtin_expect (clnt_status
!= RPC_SUCCESS
, 0))
275 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
277 switch (NIS_RES_STATUS (res
))
282 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
283 && (flags
& FOLLOW_LINKS
)) /* We are following links. */
285 free (ibreq
->ibr_name
);
286 ibreq
->ibr_name
= NULL
;
287 /* If we hit the link limit, bail. */
288 if (__builtin_expect (count_links
> NIS_MAXLINKS
, 0))
290 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
296 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
297 if (ibreq
->ibr_name
== NULL
)
299 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
301 __nisbind_destroy (&bptr
);
303 nis_free_directory (dir
);
308 __nis_destroy_callback (cb
);
309 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
310 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
312 if (names
!= namebuf
)
313 nis_freenames (names
);
314 nis_free_request (ibreq
);
317 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
318 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
320 ibreq
->ibr_srch
.ibr_srch_len
=
321 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
322 ibreq
->ibr_srch
.ibr_srch_val
=
323 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
325 /* The following is a non-obvious optimization. A
326 nis_freeresult call would call xdr_free as the
327 following code. But it also would unnecessarily
328 free the result structure. We avoid this here
329 along with the necessary tests. */
331 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *)res
);
332 memset (res
, '\0', sizeof (*res
));
334 nis_freeresult (res
);
335 res
= calloc (1, sizeof (nis_result
));
339 first_try
= 1; /* Try at first the old binding */
342 else if ((flags
& FOLLOW_PATH
)
343 && NIS_RES_STATUS (res
) == NIS_PARTIAL
)
345 if (tablepath
== NULL
)
347 tablepath
= get_tablepath (ibreq
->ibr_name
, &bptr
);
348 tableptr
= tablepath
;
350 if (tableptr
== NULL
)
355 free (ibreq
->ibr_name
);
356 ibreq
->ibr_name
= strsep (&tableptr
, ":");
357 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
359 ibreq
->ibr_name
= strdup ("");
360 if (ibreq
->ibr_name
== NULL
)
362 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
369 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
370 /* The following is a non-obvious optimization. A
371 nis_freeresult call would call xdr_free as the
372 following code. But it also would unnecessarily
373 free the result structure. We avoid this here
374 along with the necessary tests. */
376 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *)res
);
377 memset (res
, '\0', sizeof (*res
));
378 if (ibreq
->ibr_name
== NULL
)
380 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
384 nis_freeresult (res
);
385 res
= calloc (1, sizeof (nis_result
));
386 if (res
== NULL
|| ibreq
->ibr_name
== NULL
)
403 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
404 NIS_RES_STATUS (res
) = cb
->result
;
406 if (!(flags
& ALL_RESULTS
))
410 if (tablepath
== NULL
)
412 tablepath
= get_tablepath (ibreq
->ibr_name
, &bptr
);
413 tableptr
= tablepath
;
415 if (tableptr
== NULL
)
420 free (ibreq
->ibr_name
);
421 ibreq
->ibr_name
= strsep (&tableptr
, ":");
422 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
424 ibreq
->ibr_name
= strdup ("");
428 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
429 if (ibreq
->ibr_name
== NULL
)
431 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
437 case NIS_SYSTEMERROR
:
440 /* If we had first tried the old binding, do nothing, but
444 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
447 break; /* No more servers to search */
449 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
451 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
454 break; /* No more servers to search */
463 /* Try the next domainname if we don't follow a link. */
464 free (ibreq
->ibr_name
);
465 ibreq
->ibr_name
= NULL
;
466 if (__builtin_expect (count_links
, 0))
468 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
473 if (names
[name_nr
] == NULL
)
478 ibreq
->ibr_name
= strdup (names
[name_nr
]);
479 if (ibreq
->ibr_name
== NULL
)
481 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
484 first_try
= 1; /* Try old binding at first */
493 __nis_destroy_callback (cb
);
494 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
495 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
499 __nisbind_destroy (&bptr
);
500 nis_free_directory (dir
);
505 if (names
!= namebuf
)
506 nis_freenames (names
);
508 nis_free_request (ibreq
);
512 libnsl_hidden_def (nis_list
)
515 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
517 nis_result
*res
= calloc (1, sizeof (nis_result
));
523 NIS_RES_STATUS (res
) = NIS_BADNAME
;
527 ib_request
*ibreq
= create_ib_request (name
, flags
);
530 NIS_RES_STATUS (res
) = NIS_BADNAME
;
535 memcpy (&obj
, obj2
, sizeof (nis_object
));
537 size_t namelen
= strlen (name
);
538 char buf1
[namelen
+ 20];
539 char buf4
[namelen
+ 20];
541 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
542 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
544 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
545 obj
.zo_owner
= nis_local_principal ();
547 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
548 obj
.zo_group
= nis_local_group ();
550 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
552 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
553 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
555 nis_free_request (ibreq
);
556 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
559 ibreq
->ibr_obj
.ibr_obj_len
= 1;
561 nis_error status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
562 (xdrproc_t
) _xdr_ib_request
,
564 (xdrproc_t
) _xdr_nis_result
,
565 (caddr_t
) res
, 0, NULL
);
566 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
567 NIS_RES_STATUS (res
) = status
;
569 nis_free_request (ibreq
);
575 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
582 size_t namelen
= strlen (name
);
583 char buf1
[namelen
+ 20];
584 char buf4
[namelen
+ 20];
586 res
= calloc (1, sizeof (nis_result
));
590 ibreq
= create_ib_request (name
, flags
);
593 NIS_RES_STATUS (res
) = NIS_BADNAME
;
597 memcpy (&obj
, obj2
, sizeof (nis_object
));
599 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
600 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
602 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
603 obj
.zo_owner
= nis_local_principal ();
605 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
606 obj
.zo_group
= nis_local_group ();
608 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
610 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
611 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
613 nis_free_request (ibreq
);
614 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
617 ibreq
->ibr_obj
.ibr_obj_len
= 1;
619 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
620 (xdrproc_t
) _xdr_ib_request
,
621 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
622 (caddr_t
) res
, 0, NULL
);
623 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
624 NIS_RES_STATUS (res
) = status
;
626 nis_free_request (ibreq
);
632 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
639 res
= calloc (1, sizeof (nis_result
));
645 NIS_RES_STATUS (res
) = NIS_BADNAME
;
649 ibreq
= create_ib_request (name
, flags
);
652 NIS_RES_STATUS (res
) = NIS_BADNAME
;
658 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
659 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
661 nis_free_request (ibreq
);
662 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
665 ibreq
->ibr_obj
.ibr_obj_len
= 1;
668 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
669 (xdrproc_t
) _xdr_ib_request
,
670 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
671 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
672 NIS_RES_STATUS (res
) = status
;
674 nis_free_request (ibreq
);
680 nis_first_entry (const_nis_name name
)
686 res
= calloc (1, sizeof (nis_result
));
692 NIS_RES_STATUS (res
) = NIS_BADNAME
;
696 ibreq
= create_ib_request (name
, 0);
699 NIS_RES_STATUS (res
) = NIS_BADNAME
;
703 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
704 (xdrproc_t
) _xdr_ib_request
,
705 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
706 (caddr_t
) res
, 0, NULL
);
708 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
709 NIS_RES_STATUS (res
) = status
;
711 nis_free_request (ibreq
);
717 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
723 res
= calloc (1, sizeof (nis_result
));
729 NIS_RES_STATUS (res
) = NIS_BADNAME
;
733 ibreq
= create_ib_request (name
, 0);
736 NIS_RES_STATUS (res
) = NIS_BADNAME
;
742 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
743 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
746 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
747 (xdrproc_t
) _xdr_ib_request
,
748 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
749 (caddr_t
) res
, 0, NULL
);
751 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
752 NIS_RES_STATUS (res
) = status
;
756 /* Don't give cookie free, it is not from us */
757 ibreq
->ibr_cookie
.n_bytes
= NULL
;
758 ibreq
->ibr_cookie
.n_len
= 0;
761 nis_free_request (ibreq
);