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
);
481 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
487 size_t namelen
= strlen (name
);
488 char buf1
[namelen
+ 20];
489 char buf4
[namelen
+ 20];
491 res
= calloc (1, sizeof (nis_result
));
497 NIS_RES_STATUS (res
) = NIS_BADNAME
;
501 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
503 NIS_RES_STATUS (res
) = NIS_BADNAME
;
507 memcpy (&obj
, obj2
, sizeof (nis_object
));
509 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
510 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
512 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
513 obj
.zo_owner
= nis_local_principal ();
515 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
516 obj
.zo_group
= nis_local_group ();
518 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
520 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
521 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
523 nis_free_request (ibreq
);
524 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
527 ibreq
->ibr_obj
.ibr_obj_len
= 1;
529 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
530 (xdrproc_t
) _xdr_ib_request
,
532 (xdrproc_t
) _xdr_nis_result
,
533 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
534 NIS_RES_STATUS (res
) = status
;
536 nis_free_request (ibreq
);
542 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
549 size_t namelen
= strlen (name
);
550 char buf1
[namelen
+ 20];
551 char buf4
[namelen
+ 20];
553 res
= calloc (1, sizeof (nis_result
));
557 if (( ibreq
=__create_ib_request (name
, flags
)) == NULL
)
559 NIS_RES_STATUS (res
) = NIS_BADNAME
;
563 memcpy (&obj
, obj2
, sizeof (nis_object
));
565 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
566 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
568 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
569 obj
.zo_owner
= nis_local_principal ();
571 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
572 obj
.zo_group
= nis_local_group ();
574 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
576 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
577 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
579 nis_free_request (ibreq
);
580 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
583 ibreq
->ibr_obj
.ibr_obj_len
= 1;
585 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
586 (xdrproc_t
) _xdr_ib_request
,
587 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
588 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
589 NIS_RES_STATUS (res
) = status
;
591 nis_free_request (ibreq
);
597 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
604 res
= calloc (1, sizeof (nis_result
));
610 NIS_RES_STATUS (res
) = NIS_BADNAME
;
614 if ((ibreq
=__create_ib_request (name
, flags
)) == NULL
)
616 NIS_RES_STATUS (res
) = NIS_BADNAME
;
622 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
623 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
625 nis_free_request (ibreq
);
626 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
629 ibreq
->ibr_obj
.ibr_obj_len
= 1;
632 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
633 (xdrproc_t
) _xdr_ib_request
,
634 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
635 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
636 NIS_RES_STATUS (res
) = status
;
638 nis_free_request (ibreq
);
644 nis_first_entry (const_nis_name name
)
650 res
= calloc (1, sizeof (nis_result
));
656 NIS_RES_STATUS (res
) = NIS_BADNAME
;
660 ibreq
= __create_ib_request (name
, 0);
663 NIS_RES_STATUS (res
) = NIS_BADNAME
;
667 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
668 (xdrproc_t
) _xdr_ib_request
,
669 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
670 (caddr_t
) res
, 0, NULL
);
672 if (status
!= NIS_SUCCESS
)
673 NIS_RES_STATUS (res
) = status
;
675 nis_free_request (ibreq
);
681 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
687 res
= calloc (1, sizeof (nis_result
));
693 NIS_RES_STATUS (res
) = NIS_BADNAME
;
697 ibreq
= __create_ib_request (name
, 0);
700 NIS_RES_STATUS (res
) = NIS_BADNAME
;
706 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
707 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
710 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
711 (xdrproc_t
) _xdr_ib_request
,
712 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
713 (caddr_t
) res
, 0, NULL
);
715 if (status
!= NIS_SUCCESS
)
716 NIS_RES_STATUS (res
) = status
;
720 /* Don't give cookie free, it is not from us */
721 ibreq
->ibr_cookie
.n_bytes
= NULL
;
722 ibreq
->ibr_cookie
.n_len
= 0;
725 nis_free_request (ibreq
);