1 /* Copyright (c) 1997,1998,1999,2003,2004,2005 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? */
44 return (ibreq
->ibr_name
= strdup (cptr
)) == NULL
? NULL
: ibreq
;
46 /* "[key=value,...],foo" format */
47 ibreq
->ibr_name
= strchr (cptr
, ']');
48 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[1] != ',')
50 /* The object has not really been built yet so we use free. */
55 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
56 if (ibreq
->ibr_name
[-1] == ',')
57 ibreq
->ibr_name
[-1] = '\0';
59 ibreq
->ibr_name
[0] = '\0';
61 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
62 if (ibreq
->ibr_name
== NULL
)
65 while (search_len
-- > 0)
67 free (search_val
[search_len
].zattr_ndx
);
68 free (search_val
[search_len
].zattr_val
.zattr_val_val
);
71 nis_free_request (ibreq
);
75 ++cptr
; /* Remove "[" */
77 while (cptr
!= NULL
&& cptr
[0] != '\0')
80 char *val
= strchr (cptr
, '=');
82 cptr
= strchr (key
, ',');
88 nis_free_request (ibreq
);
92 if ((search_len
+ 1) >= size
)
95 nis_attr
*newp
= realloc (search_val
, size
* sizeof (nis_attr
));
100 search_val
[search_len
].zattr_ndx
= strdup (key
);
101 if ((search_val
[search_len
].zattr_ndx
) == NULL
)
104 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
105 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
106 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
108 free (search_val
[search_len
].zattr_ndx
);
115 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
116 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
121 static const struct timeval RPCTIMEOUT
= {10, 0};
124 __get_tablepath (char *name
, dir_binding
*bptr
)
126 enum clnt_stat result
;
128 struct ns_request req
;
130 memset (&res
, '\0', sizeof (res
));
133 req
.ns_object
.ns_object_len
= 0;
134 req
.ns_object
.ns_object_val
= NULL
;
136 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
137 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
138 (caddr_t
) &res
, RPCTIMEOUT
);
141 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (&res
) == NIS_SUCCESS
142 && __type_of (NIS_RES_OBJECT (&res
)) == NIS_TABLE_OBJ
)
143 cptr
= NIS_RES_OBJECT (&res
)->TA_data
.ta_path
;
147 return strdup (cptr
);
151 nis_list (const_nis_name name
, unsigned int flags
,
152 int (*callback
) (const_nis_name name
,
153 const nis_object
*object
,
154 const void *userdata
),
155 const void *userdata
)
157 nis_result
*res
= calloc (1, sizeof (nis_result
));
160 enum clnt_stat clnt_status
;
161 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
164 nis_name namebuf
[2] = {NULL
, NULL
};
167 char *tableptr
, *tablepath
= NULL
;
168 int have_tablepath
= 0;
169 int first_try
= 0; /* Do we try the old binding at first ? */
176 NIS_RES_STATUS (res
) = NIS_BADNAME
;
180 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
182 NIS_RES_STATUS (res
) = NIS_BADNAME
;
186 if ((flags
& EXPAND_NAME
)
187 && ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
189 names
= nis_getnames (ibreq
->ibr_name
);
190 free (ibreq
->ibr_name
);
191 ibreq
->ibr_name
= NULL
;
194 nis_free_request (ibreq
);
195 NIS_RES_STATUS (res
) = NIS_BADNAME
;
198 ibreq
->ibr_name
= strdup (names
[name_nr
]);
199 if (ibreq
->ibr_name
== NULL
)
201 nis_free_request (ibreq
);
202 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
209 names
[name_nr
] = ibreq
->ibr_name
;
217 directory_obj
*dir
= NULL
;
219 memset (res
, '\0', sizeof (nis_result
));
221 status
= __nisfind_server (ibreq
->ibr_name
, &dir
);
222 if (status
!= NIS_SUCCESS
)
224 nis_free_request (ibreq
);
225 NIS_RES_STATUS (res
) = status
;
229 status
= __nisbind_create (&bptr
, dir
->do_servers
.do_servers_val
,
230 dir
->do_servers
.do_servers_len
, flags
);
231 if (status
!= NIS_SUCCESS
)
233 nis_free_request (ibreq
);
234 NIS_RES_STATUS (res
) = status
;
235 nis_free_directory (dir
);
239 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
240 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
242 __nisbind_destroy (&bptr
);
243 nis_free_directory (dir
);
244 nis_free_request (ibreq
);
245 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
249 if (callback
!= NULL
)
251 cb
= __nis_create_callback (callback
, userdata
, flags
);
252 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
253 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
257 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
258 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
259 (xdrproc_t
) _xdr_nis_result
,
260 (caddr_t
) res
, RPCTIMEOUT
);
262 if (clnt_status
!= RPC_SUCCESS
)
263 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
265 switch (NIS_RES_STATUS (res
))
270 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
&&
271 flags
& FOLLOW_LINKS
) /* We are following links. */
273 free (ibreq
->ibr_name
);
274 ibreq
->ibr_name
= NULL
;
275 /* If we hit the link limit, bail. */
276 if (count_links
> NIS_MAXLINKS
)
278 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
284 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
285 if (ibreq
->ibr_name
== NULL
)
287 nis_free_request (ibreq
);
288 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
291 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
292 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
294 ibreq
->ibr_srch
.ibr_srch_len
=
295 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
296 ibreq
->ibr_srch
.ibr_srch_val
=
297 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
299 nis_freeresult (res
);
300 res
= calloc (1, sizeof (nis_result
));
305 __nisbind_destroy (&bptr
);
306 nis_free_directory (dir
);
309 first_try
= 1; /* Try at first the old binding */
312 else if ((flags
& FOLLOW_PATH
) &&
313 NIS_RES_STATUS (res
) == NIS_PARTIAL
)
317 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
318 tableptr
= tablepath
;
321 if (tableptr
== NULL
)
326 free (ibreq
->ibr_name
);
327 ibreq
->ibr_name
= strsep (&tableptr
, ":");
328 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
330 ibreq
->ibr_name
= strdup ("");
331 if (ibreq
->ibr_name
== NULL
)
333 nis_free_request (ibreq
);
334 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
341 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
342 nis_freeresult (res
);
343 res
= calloc (1, sizeof (nis_result
));
344 if (res
== NULL
|| ibreq
->ibr_name
== NULL
)
346 free (ibreq
->ibr_name
);
348 nis_free_request (ibreq
);
351 __nisbind_destroy (&bptr
);
352 nis_free_directory (dir
);
365 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
366 NIS_RES_STATUS (res
) = cb
->result
;
368 if (!(flags
& ALL_RESULTS
))
374 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
375 tableptr
= tablepath
;
378 if (tableptr
== NULL
)
383 free (ibreq
->ibr_name
);
384 ibreq
->ibr_name
= strsep (&tableptr
, ":");
385 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
387 ibreq
->ibr_name
= strdup ("");
391 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
392 if (ibreq
->ibr_name
== NULL
)
394 nis_free_request (ibreq
);
395 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
401 case NIS_SYSTEMERROR
:
404 /* If we had first tried the old binding, do nothing, but
408 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
411 break; /* No more servers to search */
413 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
415 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
418 break; /* No more servers to search */
427 /* Try the next domainname if we don't follow a link. */
428 free (ibreq
->ibr_name
);
429 ibreq
->ibr_name
= NULL
;
432 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
437 if (names
[name_nr
] == NULL
)
442 ibreq
->ibr_name
= strdup (names
[name_nr
]);
443 if (ibreq
->ibr_name
== NULL
)
445 nis_free_request (ibreq
);
446 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
449 first_try
= 1; /* Try old binding at first */
458 __nis_destroy_callback (cb
);
459 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
460 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
463 __nisbind_destroy (&bptr
);
464 nis_free_directory (dir
);
467 if (names
!= namebuf
)
468 nis_freenames (names
);
470 nis_free_request (ibreq
);
474 libnsl_hidden_def (nis_list
)
477 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
483 size_t namelen
= strlen (name
);
484 char buf1
[namelen
+ 20];
485 char buf4
[namelen
+ 20];
487 res
= calloc (1, sizeof (nis_result
));
493 NIS_RES_STATUS (res
) = NIS_BADNAME
;
497 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
499 NIS_RES_STATUS (res
) = NIS_BADNAME
;
503 memcpy (&obj
, obj2
, sizeof (nis_object
));
505 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
506 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
508 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
509 obj
.zo_owner
= nis_local_principal ();
511 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
512 obj
.zo_group
= nis_local_group ();
514 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
516 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
517 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
519 nis_free_request (ibreq
);
520 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
523 ibreq
->ibr_obj
.ibr_obj_len
= 1;
525 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
526 (xdrproc_t
) _xdr_ib_request
,
528 (xdrproc_t
) _xdr_nis_result
,
529 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
530 NIS_RES_STATUS (res
) = status
;
532 nis_free_request (ibreq
);
538 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
545 size_t namelen
= strlen (name
);
546 char buf1
[namelen
+ 20];
547 char buf4
[namelen
+ 20];
549 res
= calloc (1, sizeof (nis_result
));
553 if (( ibreq
=__create_ib_request (name
, flags
)) == NULL
)
555 NIS_RES_STATUS (res
) = NIS_BADNAME
;
559 memcpy (&obj
, obj2
, sizeof (nis_object
));
561 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
562 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
564 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
565 obj
.zo_owner
= nis_local_principal ();
567 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
568 obj
.zo_group
= nis_local_group ();
570 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
572 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
573 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
575 nis_free_request (ibreq
);
576 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
579 ibreq
->ibr_obj
.ibr_obj_len
= 1;
581 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
582 (xdrproc_t
) _xdr_ib_request
,
583 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
584 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
585 NIS_RES_STATUS (res
) = status
;
587 nis_free_request (ibreq
);
593 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
600 res
= calloc (1, sizeof (nis_result
));
606 NIS_RES_STATUS (res
) = NIS_BADNAME
;
610 if ((ibreq
=__create_ib_request (name
, flags
)) == NULL
)
612 NIS_RES_STATUS (res
) = NIS_BADNAME
;
618 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
619 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
621 nis_free_request (ibreq
);
622 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
625 ibreq
->ibr_obj
.ibr_obj_len
= 1;
628 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
629 (xdrproc_t
) _xdr_ib_request
,
630 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
631 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
632 NIS_RES_STATUS (res
) = status
;
634 nis_free_request (ibreq
);
640 nis_first_entry (const_nis_name name
)
646 res
= calloc (1, sizeof (nis_result
));
652 NIS_RES_STATUS (res
) = NIS_BADNAME
;
656 ibreq
= __create_ib_request (name
, 0);
659 NIS_RES_STATUS (res
) = NIS_BADNAME
;
663 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
664 (xdrproc_t
) _xdr_ib_request
,
665 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
666 (caddr_t
) res
, 0, NULL
);
668 if (status
!= NIS_SUCCESS
)
669 NIS_RES_STATUS (res
) = status
;
671 nis_free_request (ibreq
);
677 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
683 res
= calloc (1, sizeof (nis_result
));
689 NIS_RES_STATUS (res
) = NIS_BADNAME
;
693 ibreq
= __create_ib_request (name
, 0);
696 NIS_RES_STATUS (res
) = NIS_BADNAME
;
702 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
703 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
706 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
707 (xdrproc_t
) _xdr_ib_request
,
708 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
709 (caddr_t
) res
, 0, NULL
);
711 if (status
!= NIS_SUCCESS
)
712 NIS_RES_STATUS (res
) = status
;
716 /* Don't give cookie free, it is not from us */
717 ibreq
->ibr_cookie
.n_bytes
= NULL
;
718 ibreq
->ibr_cookie
.n_len
= 0;
721 nis_free_request (ibreq
);