1 /* Copyright (c) 1997, 1998, 1999, 2003, 2004 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 while (search_len
-- > 0)
69 free (search_val
[search_len
].zattr_ndx
);
70 free (search_val
[search_len
].zattr_val
.zattr_val_val
);
73 nis_free_request (ibreq
);
77 ++cptr
; /* Remove "[" */
79 while (cptr
!= NULL
&& cptr
[0] != '\0')
82 char *val
= strchr (cptr
, '=');
84 cptr
= strchr (key
, ',');
90 nis_free_request (ibreq
);
94 if ((search_len
+ 1) >= size
)
97 search_val
= realloc (search_val
, size
* sizeof (nis_attr
));
98 if (search_val
== NULL
)
101 search_val
[search_len
].zattr_ndx
= strdup (key
);
102 if ((search_val
[search_len
].zattr_ndx
) == NULL
)
105 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
106 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
107 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
109 free (search_val
[search_len
].zattr_ndx
);
116 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
117 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
122 static struct timeval RPCTIMEOUT
= {10, 0};
125 __get_tablepath (char *name
, dir_binding
*bptr
)
127 enum clnt_stat result
;
128 nis_result
*res
= calloc (1, sizeof (nis_result
));
129 struct ns_request req
;
135 req
.ns_object
.ns_object_len
= 0;
136 req
.ns_object
.ns_object_val
= NULL
;
138 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
139 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
140 (caddr_t
) res
, RPCTIMEOUT
);
142 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (res
) == NIS_SUCCESS
&&
143 __type_of (NIS_RES_OBJECT (res
)) == NIS_TABLE_OBJ
)
145 char *cptr
= strdup (NIS_RES_OBJECT (res
)->TA_data
.ta_path
);
146 nis_freeresult (res
);
151 nis_freeresult (res
);
157 nis_list (const_nis_name name
, unsigned int flags
,
158 int (*callback
) (const_nis_name name
,
159 const nis_object
*object
,
160 const void *userdata
),
161 const void *userdata
)
163 nis_result
*res
= calloc (1, sizeof (nis_result
));
166 enum clnt_stat clnt_status
;
167 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
170 nis_name namebuf
[2] = {NULL
, NULL
};
173 char *tableptr
, *tablepath
= NULL
;
174 int have_tablepath
= 0;
175 int first_try
= 0; /* Do we try the old binding at first ? */
182 NIS_RES_STATUS (res
) = NIS_BADNAME
;
186 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
188 NIS_RES_STATUS (res
) = NIS_BADNAME
;
192 if ((flags
& EXPAND_NAME
)
193 && ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
195 names
= nis_getnames (ibreq
->ibr_name
);
196 free (ibreq
->ibr_name
);
197 ibreq
->ibr_name
= NULL
;
200 nis_free_request (ibreq
);
201 NIS_RES_STATUS (res
) = NIS_BADNAME
;
204 ibreq
->ibr_name
= strdup (names
[name_nr
]);
205 if (ibreq
->ibr_name
== NULL
)
207 nis_free_request (ibreq
);
208 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
215 names
[name_nr
] = ibreq
->ibr_name
;
223 directory_obj
*dir
= NULL
;
225 memset (res
, '\0', sizeof (nis_result
));
227 status
= __nisfind_server (ibreq
->ibr_name
, &dir
);
228 if (status
!= NIS_SUCCESS
)
230 nis_free_request (ibreq
);
231 NIS_RES_STATUS (res
) = status
;
235 status
= __nisbind_create (&bptr
, dir
->do_servers
.do_servers_val
,
236 dir
->do_servers
.do_servers_len
, flags
);
237 if (status
!= NIS_SUCCESS
)
239 nis_free_request (ibreq
);
240 NIS_RES_STATUS (res
) = status
;
241 nis_free_directory (dir
);
245 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
246 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
248 __nisbind_destroy (&bptr
);
249 nis_free_directory (dir
);
250 nis_free_request (ibreq
);
251 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
255 if (callback
!= NULL
)
257 cb
= __nis_create_callback (callback
, userdata
, flags
);
258 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
259 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
263 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
264 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
265 (xdrproc_t
) _xdr_nis_result
,
266 (caddr_t
) res
, RPCTIMEOUT
);
268 if (clnt_status
!= RPC_SUCCESS
)
269 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
271 switch (NIS_RES_STATUS (res
))
276 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
&&
277 flags
& FOLLOW_LINKS
) /* We are following links. */
279 free (ibreq
->ibr_name
);
280 ibreq
->ibr_name
= NULL
;
281 /* If we hit the link limit, bail. */
282 if (count_links
> NIS_MAXLINKS
)
284 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
290 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
291 if (ibreq
->ibr_name
== NULL
)
293 nis_free_request (ibreq
);
294 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
297 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
298 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
300 ibreq
->ibr_srch
.ibr_srch_len
=
301 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
302 ibreq
->ibr_srch
.ibr_srch_val
=
303 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
305 nis_freeresult (res
);
306 res
= calloc (1, sizeof (nis_result
));
311 __nisbind_destroy (&bptr
);
312 nis_free_directory (dir
);
315 first_try
= 1; /* Try at first the old binding */
318 else if ((flags
& FOLLOW_PATH
) &&
319 NIS_RES_STATUS (res
) == NIS_PARTIAL
)
323 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
324 tableptr
= tablepath
;
327 if (tableptr
== NULL
)
332 free (ibreq
->ibr_name
);
333 ibreq
->ibr_name
= strsep (&tableptr
, ":");
334 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
336 ibreq
->ibr_name
= strdup ("");
337 if (ibreq
->ibr_name
== NULL
)
339 nis_free_request (ibreq
);
340 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
347 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
348 nis_freeresult (res
);
349 res
= calloc (1, sizeof (nis_result
));
350 if (res
== NULL
|| ibreq
->ibr_name
== NULL
)
353 nis_free_request (ibreq
);
356 __nisbind_destroy (&bptr
);
357 nis_free_directory (dir
);
370 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
371 NIS_RES_STATUS (res
) = cb
->result
;
373 if (!(flags
& ALL_RESULTS
))
379 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
380 tableptr
= tablepath
;
383 if (tableptr
== NULL
)
388 free (ibreq
->ibr_name
);
389 ibreq
->ibr_name
= strsep (&tableptr
, ":");
390 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
392 ibreq
->ibr_name
= strdup ("");
396 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
397 if (ibreq
->ibr_name
== NULL
)
399 nis_free_request (ibreq
);
400 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
406 case NIS_SYSTEMERROR
:
409 /* If we had first tried the old binding, do nothing, but
413 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
416 break; /* No more servers to search */
418 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
420 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
423 break; /* No more servers to search */
432 /* Try the next domainname if we don't follow a link. */
433 free (ibreq
->ibr_name
);
434 ibreq
->ibr_name
= NULL
;
437 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
442 if (names
[name_nr
] == NULL
)
447 ibreq
->ibr_name
= strdup (names
[name_nr
]);
448 if (ibreq
->ibr_name
== NULL
)
450 nis_free_request (ibreq
);
451 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
454 first_try
= 1; /* Try old binding at first */
463 __nis_destroy_callback (cb
);
464 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
465 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
468 __nisbind_destroy (&bptr
);
469 nis_free_directory (dir
);
472 if (names
!= namebuf
)
473 nis_freenames (names
);
475 nis_free_request (ibreq
);
479 libnsl_hidden_def (nis_list
)
482 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
488 size_t namelen
= strlen (name
);
489 char buf1
[namelen
+ 20];
490 char buf4
[namelen
+ 20];
492 res
= calloc (1, sizeof (nis_result
));
498 NIS_RES_STATUS (res
) = NIS_BADNAME
;
502 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
504 NIS_RES_STATUS (res
) = NIS_BADNAME
;
508 memcpy (&obj
, obj2
, sizeof (nis_object
));
510 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
511 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
513 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
514 obj
.zo_owner
= nis_local_principal ();
516 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
517 obj
.zo_group
= nis_local_group ();
519 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
521 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
522 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
524 nis_free_request (ibreq
);
525 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
528 ibreq
->ibr_obj
.ibr_obj_len
= 1;
530 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
531 (xdrproc_t
) _xdr_ib_request
,
533 (xdrproc_t
) _xdr_nis_result
,
534 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
535 NIS_RES_STATUS (res
) = status
;
537 nis_free_request (ibreq
);
543 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
550 size_t namelen
= strlen (name
);
551 char buf1
[namelen
+ 20];
552 char buf4
[namelen
+ 20];
554 res
= calloc (1, sizeof (nis_result
));
558 if (( ibreq
=__create_ib_request (name
, flags
)) == NULL
)
560 NIS_RES_STATUS (res
) = NIS_BADNAME
;
564 memcpy (&obj
, obj2
, sizeof (nis_object
));
566 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
567 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
569 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
570 obj
.zo_owner
= nis_local_principal ();
572 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
573 obj
.zo_group
= nis_local_group ();
575 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
577 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
578 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
580 nis_free_request (ibreq
);
581 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
584 ibreq
->ibr_obj
.ibr_obj_len
= 1;
586 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
587 (xdrproc_t
) _xdr_ib_request
,
588 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
589 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
590 NIS_RES_STATUS (res
) = status
;
592 nis_free_request (ibreq
);
598 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
605 res
= calloc (1, sizeof (nis_result
));
611 NIS_RES_STATUS (res
) = NIS_BADNAME
;
615 if ((ibreq
=__create_ib_request (name
, flags
)) == NULL
)
617 NIS_RES_STATUS (res
) = NIS_BADNAME
;
623 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
624 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
626 nis_free_request (ibreq
);
627 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
630 ibreq
->ibr_obj
.ibr_obj_len
= 1;
633 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
634 (xdrproc_t
) _xdr_ib_request
,
635 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
636 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
637 NIS_RES_STATUS (res
) = status
;
639 nis_free_request (ibreq
);
645 nis_first_entry (const_nis_name name
)
651 res
= calloc (1, sizeof (nis_result
));
657 NIS_RES_STATUS (res
) = NIS_BADNAME
;
661 ibreq
= __create_ib_request (name
, 0);
664 NIS_RES_STATUS (res
) = NIS_BADNAME
;
668 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
669 (xdrproc_t
) _xdr_ib_request
,
670 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
671 (caddr_t
) res
, 0, NULL
);
673 if (status
!= NIS_SUCCESS
)
674 NIS_RES_STATUS (res
) = status
;
676 nis_free_request (ibreq
);
682 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
688 res
= calloc (1, sizeof (nis_result
));
694 NIS_RES_STATUS (res
) = NIS_BADNAME
;
698 ibreq
= __create_ib_request (name
, 0);
701 NIS_RES_STATUS (res
) = NIS_BADNAME
;
707 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
708 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
711 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
712 (xdrproc_t
) _xdr_ib_request
,
713 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
714 (caddr_t
) res
, 0, NULL
);
716 if (status
!= NIS_SUCCESS
)
717 NIS_RES_STATUS (res
) = status
;
721 /* Don't give cookie free, it is not from us */
722 ibreq
->ibr_cookie
.n_bytes
= NULL
;
723 ibreq
->ibr_cookie
.n_len
= 0;
726 nis_free_request (ibreq
);