1 /* Copyright (c) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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
;
37 ibreq
->ibr_flags
= flags
;
39 cptr
= strcpy (buf
, name
);
41 /* Not of "[key=value,key=value,...],foo.." format? */
44 ibreq
->ibr_name
= strdup (cptr
);
48 /* "[key=value,...],foo" format */
49 ibreq
->ibr_name
= strchr (cptr
, ']');
50 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[1] != ',')
53 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
54 if (ibreq
->ibr_name
[-1] == ',')
55 ibreq
->ibr_name
[-1] = '\0';
57 ibreq
->ibr_name
[0] = '\0';
59 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
61 ++cptr
; /* Remove "[" */
63 while (cptr
!= NULL
&& cptr
[0] != '\0')
66 char *val
= strchr (cptr
, '=');
68 cptr
= strchr (key
, ',');
74 nis_free_request (ibreq
);
78 if ((search_len
+ 1) >= size
)
81 search_val
= realloc (search_val
, size
* sizeof (nis_attr
));
82 if (search_val
== NULL
)
84 nis_free_request (ibreq
);
88 search_val
[search_len
].zattr_ndx
= strdup (key
);
89 if ((search_val
[search_len
].zattr_ndx
) == NULL
)
91 nis_free_request (ibreq
);
94 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
95 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
96 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
98 nis_free_request (ibreq
);
104 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
105 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
110 static struct timeval RPCTIMEOUT
= {10, 0};
113 __get_tablepath (char *name
, dir_binding
*bptr
)
115 enum clnt_stat result
;
116 nis_result
*res
= calloc (1, sizeof (nis_result
));
117 struct ns_request req
;
123 req
.ns_object
.ns_object_len
= 0;
124 req
.ns_object
.ns_object_val
= NULL
;
126 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
127 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
128 (caddr_t
) res
, RPCTIMEOUT
);
130 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (res
) == NIS_SUCCESS
&&
131 __type_of (NIS_RES_OBJECT (res
)) == NIS_TABLE_OBJ
)
133 char *cptr
= strdup (NIS_RES_OBJECT (res
)->TA_data
.ta_path
);
134 nis_freeresult (res
);
139 nis_freeresult (res
);
145 nis_list (const_nis_name name
, unsigned int flags
,
146 int (*callback
) (const_nis_name name
,
147 const nis_object
*object
,
148 const void *userdata
),
149 const void *userdata
)
151 nis_result
*res
= NULL
;
154 enum clnt_stat clnt_status
;
155 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
158 nis_name namebuf
[2] = {NULL
, NULL
};
161 char *tableptr
, *tablepath
= NULL
;
162 int have_tablepath
= 0;
163 int first_try
= 0; /* Do we try the old binding at first ? */
165 res
= calloc (1, sizeof (nis_result
));
171 NIS_RES_STATUS (res
) = NIS_BADNAME
;
175 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
177 NIS_RES_STATUS (res
) = NIS_BADNAME
;
181 if ((flags
& EXPAND_NAME
) &&
182 ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
184 names
= nis_getnames (ibreq
->ibr_name
);
185 free (ibreq
->ibr_name
);
186 ibreq
->ibr_name
= NULL
;
189 NIS_RES_STATUS (res
) = NIS_BADNAME
;
192 ibreq
->ibr_name
= strdup (names
[name_nr
]);
197 names
[name_nr
] = ibreq
->ibr_name
;
205 directory_obj
*dir
= NULL
;
207 memset (res
, '\0', sizeof (nis_result
));
209 status
= __nisfind_server (ibreq
->ibr_name
, &dir
);
210 if (status
!= NIS_SUCCESS
)
212 NIS_RES_STATUS (res
) = status
;
216 status
= __nisbind_create (&bptr
, dir
->do_servers
.do_servers_val
,
217 dir
->do_servers
.do_servers_len
, flags
);
218 if (status
!= NIS_SUCCESS
)
220 NIS_RES_STATUS (res
) = status
;
221 nis_free_directory (dir
);
225 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
226 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
228 __nisbind_destroy (&bptr
);
229 nis_free_directory (dir
);
230 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
234 if (callback
!= NULL
)
236 cb
= __nis_create_callback (callback
, userdata
, flags
);
237 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
238 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
242 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
243 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
244 (xdrproc_t
) _xdr_nis_result
,
245 (caddr_t
) res
, RPCTIMEOUT
);
247 if (clnt_status
!= RPC_SUCCESS
)
248 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
250 switch (NIS_RES_STATUS (res
))
255 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
&&
256 flags
& FOLLOW_LINKS
) /* We are following links. */
258 free (ibreq
->ibr_name
);
259 /* If we hit the link limit, bail. */
260 if (count_links
> NIS_MAXLINKS
)
262 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
268 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
269 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
270 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
272 ibreq
->ibr_srch
.ibr_srch_len
=
273 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
274 ibreq
->ibr_srch
.ibr_srch_val
=
275 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
277 nis_freeresult (res
);
278 res
= calloc (1, sizeof (nis_result
));
283 __nisbind_destroy (&bptr
);
284 nis_free_directory (dir
);
287 first_try
= 1; /* Try at first the old binding */
290 else if ((flags
& FOLLOW_PATH
) &&
291 NIS_RES_STATUS (res
) == NIS_PARTIAL
)
295 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
296 tableptr
= tablepath
;
299 if (tableptr
== NULL
)
304 free (ibreq
->ibr_name
);
305 ibreq
->ibr_name
= strsep (&tableptr
, ":");
306 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
308 ibreq
->ibr_name
= strdup ("");
313 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
314 nis_freeresult (res
);
315 res
= calloc (1, sizeof (nis_result
));
320 __nisbind_destroy (&bptr
);
321 nis_free_directory (dir
);
334 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
335 NIS_RES_STATUS (res
) = cb
->result
;
337 if (!(flags
& ALL_RESULTS
))
343 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
344 tableptr
= tablepath
;
347 if (tableptr
== NULL
)
352 free (ibreq
->ibr_name
);
353 ibreq
->ibr_name
= strsep (&tableptr
, ":");
354 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
356 ibreq
->ibr_name
= strdup ("");
360 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
364 case NIS_SYSTEMERROR
:
367 /* If we had first tried the old binding, do nothing, but
371 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
374 break; /* No more servers to search */
376 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
378 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
381 break; /* No more servers to search */
390 /* Try the next domainname if we don't follow a link. */
393 free (ibreq
->ibr_name
);
394 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
399 if (names
[name_nr
] == NULL
)
404 ibreq
->ibr_name
= names
[name_nr
];
405 first_try
= 1; /* Try old binding at first */
414 __nis_destroy_callback (cb
);
415 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
416 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
419 __nisbind_destroy (&bptr
);
420 nis_free_directory (dir
);
423 if (names
!= namebuf
)
424 nis_freenames (names
);
426 nis_free_request (ibreq
);
432 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
438 size_t namelen
= strlen (name
);
439 char buf1
[namelen
+ 20];
440 char buf4
[namelen
+ 20];
442 res
= calloc (1, sizeof (nis_result
));
448 NIS_RES_STATUS (res
) = NIS_BADNAME
;
452 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
454 NIS_RES_STATUS (res
) = NIS_BADNAME
;
458 memcpy (&obj
, obj2
, sizeof (nis_object
));
460 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
461 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
463 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
464 obj
.zo_owner
= nis_local_principal ();
466 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
467 obj
.zo_group
= nis_local_group ();
469 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
471 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
472 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
474 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
477 ibreq
->ibr_obj
.ibr_obj_len
= 1;
479 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
480 (xdrproc_t
) _xdr_ib_request
,
482 (xdrproc_t
) _xdr_nis_result
,
483 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
484 NIS_RES_STATUS (res
) = status
;
486 nis_free_request (ibreq
);
492 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
499 size_t namelen
= strlen (name
);
500 char buf1
[namelen
+ 20];
501 char buf4
[namelen
+ 20];
503 res
= calloc (1, sizeof (nis_result
));
507 if (( ibreq
=__create_ib_request (name
, flags
)) == NULL
)
509 NIS_RES_STATUS (res
) = NIS_BADNAME
;
513 memcpy (&obj
, obj2
, sizeof (nis_object
));
515 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
516 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
518 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
519 obj
.zo_owner
= nis_local_principal ();
521 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
522 obj
.zo_group
= nis_local_group ();
524 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
526 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
527 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
529 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
532 ibreq
->ibr_obj
.ibr_obj_len
= 1;
534 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
535 (xdrproc_t
) _xdr_ib_request
,
536 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
537 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
538 NIS_RES_STATUS (res
) = status
;
540 nis_free_request (ibreq
);
546 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
553 res
= calloc (1, sizeof (nis_result
));
559 NIS_RES_STATUS (res
) = NIS_BADNAME
;
563 if ((ibreq
=__create_ib_request (name
, flags
)) == NULL
)
565 NIS_RES_STATUS (res
) = NIS_BADNAME
;
571 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
572 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
574 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
577 ibreq
->ibr_obj
.ibr_obj_len
= 1;
580 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
581 (xdrproc_t
) _xdr_ib_request
,
582 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
583 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
584 NIS_RES_STATUS (res
) = status
;
586 nis_free_request (ibreq
);
592 nis_first_entry (const_nis_name name
)
598 res
= calloc (1, sizeof (nis_result
));
604 NIS_RES_STATUS (res
) = NIS_BADNAME
;
608 if ((ibreq
=__create_ib_request (name
, 0)) == NULL
)
610 NIS_RES_STATUS (res
) = NIS_BADNAME
;
614 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
615 (xdrproc_t
) _xdr_ib_request
,
616 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
617 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
618 NIS_RES_STATUS (res
) = status
;
620 nis_free_request (ibreq
);
626 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
632 res
= calloc (1, sizeof (nis_result
));
638 NIS_RES_STATUS (res
) = NIS_BADNAME
;
642 if (( ibreq
=__create_ib_request (name
, 0)) == NULL
)
644 NIS_RES_STATUS (res
) = NIS_BADNAME
;
650 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
651 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
654 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
655 (xdrproc_t
) _xdr_ib_request
,
656 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
657 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
658 NIS_RES_STATUS (res
) = status
;
662 /* Don't give cookie free, it is not from us */
663 ibreq
->ibr_cookie
.n_bytes
= NULL
;
664 ibreq
->ibr_cookie
.n_len
= 0;
667 nis_free_request (ibreq
);