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
, u_long 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
)
82 search_val
= malloc (size
* sizeof (nis_attr
));
84 search_val
= realloc (search_val
, size
* sizeof (nis_attr
));
85 if (search_val
== NULL
)
87 nis_free_request (ibreq
);
91 search_val
[search_len
].zattr_ndx
= strdup (key
);
92 if ((search_val
[search_len
].zattr_ndx
) == NULL
)
94 nis_free_request (ibreq
);
97 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
98 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
99 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
101 nis_free_request (ibreq
);
107 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
108 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
113 static struct timeval RPCTIMEOUT
= {10, 0};
116 __get_tablepath (char *name
, dir_binding
*bptr
)
118 enum clnt_stat result
;
119 nis_result
*res
= calloc (1, sizeof (nis_result
));
120 struct ns_request req
;
126 req
.ns_object
.ns_object_len
= 0;
127 req
.ns_object
.ns_object_val
= NULL
;
129 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
130 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
131 (caddr_t
) res
, RPCTIMEOUT
);
133 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (res
) == NIS_SUCCESS
&&
134 __type_of (NIS_RES_OBJECT (res
)) == NIS_TABLE_OBJ
)
136 char *cptr
= strdup (NIS_RES_OBJECT (res
)->TA_data
.ta_path
);
137 nis_freeresult (res
);
142 nis_freeresult (res
);
148 nis_list (const_nis_name name
, u_long flags
,
149 int (*callback
) (const_nis_name name
,
150 const nis_object
*object
,
151 const void *userdata
),
152 const void *userdata
)
154 nis_result
*res
= NULL
;
157 enum clnt_stat clnt_status
;
158 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
161 nis_name namebuf
[2] = {NULL
, NULL
};
164 char *tableptr
, *tablepath
= NULL
;
165 int have_tablepath
= 0;
166 int first_try
= 0; /* Do we try the old binding at first ? */
168 res
= calloc (1, sizeof (nis_result
));
174 NIS_RES_STATUS (res
) = NIS_BADNAME
;
178 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
180 NIS_RES_STATUS (res
) = NIS_BADNAME
;
184 if ((flags
& EXPAND_NAME
) &&
185 ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
187 names
= nis_getnames (ibreq
->ibr_name
);
188 free (ibreq
->ibr_name
);
189 ibreq
->ibr_name
= NULL
;
192 NIS_RES_STATUS (res
) = NIS_BADNAME
;
195 ibreq
->ibr_name
= strdup (names
[name_nr
]);
200 names
[name_nr
] = ibreq
->ibr_name
;
208 directory_obj
*dir
= NULL
;
210 memset (res
, '\0', sizeof (nis_result
));
212 status
= __nisfind_server (ibreq
->ibr_name
, &dir
);
213 if (status
!= NIS_SUCCESS
)
215 NIS_RES_STATUS (res
) = status
;
219 status
= __nisbind_create (&bptr
, dir
->do_servers
.do_servers_val
,
220 dir
->do_servers
.do_servers_len
, flags
);
221 if (status
!= NIS_SUCCESS
)
223 NIS_RES_STATUS (res
) = status
;
224 nis_free_directory (dir
);
228 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
229 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
231 __nisbind_destroy (&bptr
);
232 nis_free_directory (dir
);
233 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
237 if (callback
!= NULL
)
239 cb
= __nis_create_callback (callback
, userdata
, flags
);
240 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
241 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
245 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
246 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
247 (xdrproc_t
) _xdr_nis_result
,
248 (caddr_t
) res
, RPCTIMEOUT
);
250 if (clnt_status
!= RPC_SUCCESS
)
251 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
253 switch (NIS_RES_STATUS (res
))
258 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
&&
259 flags
& FOLLOW_LINKS
) /* We are following links. */
261 free (ibreq
->ibr_name
);
262 /* If we hit the link limit, bail. */
263 if (count_links
> NIS_MAXLINKS
)
265 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
271 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
272 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
273 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
275 ibreq
->ibr_srch
.ibr_srch_len
=
276 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
277 ibreq
->ibr_srch
.ibr_srch_val
=
278 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
280 nis_freeresult (res
);
281 res
= calloc (1, sizeof (nis_result
));
286 __nisbind_destroy (&bptr
);
287 nis_free_directory (dir
);
290 first_try
= 1; /* Try at first the old binding */
293 else if ((flags
& FOLLOW_PATH
) &&
294 NIS_RES_STATUS (res
) == NIS_PARTIAL
)
298 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
299 tableptr
= tablepath
;
302 if (tableptr
== NULL
)
307 free (ibreq
->ibr_name
);
308 ibreq
->ibr_name
= strsep (&tableptr
, ":");
309 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[0] == '\0')
311 ibreq
->ibr_name
= strdup ("");
316 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
317 nis_freeresult (res
);
318 res
= calloc (1, sizeof (nis_result
));
323 __nisbind_destroy (&bptr
);
324 nis_free_directory (dir
);
337 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
338 NIS_RES_STATUS (res
) = cb
->result
;
340 if (!(flags
& ALL_RESULTS
))
346 tablepath
= __get_tablepath (ibreq
->ibr_name
, &bptr
);
347 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 ("");
363 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
367 case NIS_SYSTEMERROR
:
370 /* If we had first tried the old binding, do nothing, but
374 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
377 break; /* No more servers to search */
379 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
381 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
384 break; /* No more servers to search */
393 /* Try the next domainname if we don't follow a link. */
396 free (ibreq
->ibr_name
);
397 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
402 if (names
[name_nr
] == NULL
)
407 ibreq
->ibr_name
= names
[name_nr
];
408 first_try
= 1; /* Try old binding at first */
417 __nis_destroy_callback (cb
);
418 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
419 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
422 __nisbind_destroy (&bptr
);
423 nis_free_directory (dir
);
426 if (names
!= namebuf
)
427 nis_freenames (names
);
429 nis_free_request (ibreq
);
435 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, u_long flags
)
441 size_t namelen
= strlen (name
);
442 char buf1
[namelen
+ 20];
443 char buf4
[namelen
+ 20];
445 res
= calloc (1, sizeof (nis_result
));
451 NIS_RES_STATUS (res
) = NIS_BADNAME
;
455 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
457 NIS_RES_STATUS (res
) = NIS_BADNAME
;
461 memcpy (&obj
, obj2
, sizeof (nis_object
));
463 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
464 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
466 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
467 obj
.zo_owner
= nis_local_principal ();
469 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
470 obj
.zo_group
= nis_local_group ();
472 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
474 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
475 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
477 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
480 ibreq
->ibr_obj
.ibr_obj_len
= 1;
482 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
483 (xdrproc_t
) _xdr_ib_request
,
485 (xdrproc_t
) _xdr_nis_result
,
486 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
487 NIS_RES_STATUS (res
) = status
;
489 nis_free_request (ibreq
);
495 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
, u_long flags
)
501 size_t namelen
= strlen (name
);
502 char buf1
[namelen
+ 20];
503 char buf4
[namelen
+ 20];
505 res
= calloc (1, sizeof (nis_result
));
509 if (( ibreq
=__create_ib_request (name
, flags
)) == NULL
)
511 NIS_RES_STATUS (res
) = NIS_BADNAME
;
515 memcpy (&obj
, obj2
, sizeof (nis_object
));
517 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
518 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
520 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
521 obj
.zo_owner
= nis_local_principal ();
523 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
524 obj
.zo_group
= nis_local_group ();
526 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
528 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
529 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
531 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
534 ibreq
->ibr_obj
.ibr_obj_len
= 1;
536 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
537 (xdrproc_t
) _xdr_ib_request
,
538 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
539 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
540 NIS_RES_STATUS (res
) = status
;
542 nis_free_request (ibreq
);
548 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
555 res
= calloc (1, sizeof (nis_result
));
561 NIS_RES_STATUS (res
) = NIS_BADNAME
;
565 if ((ibreq
=__create_ib_request (name
, flags
)) == NULL
)
567 NIS_RES_STATUS (res
) = NIS_BADNAME
;
573 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
574 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
576 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
579 ibreq
->ibr_obj
.ibr_obj_len
= 1;
582 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
583 (xdrproc_t
) _xdr_ib_request
,
584 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
585 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
586 NIS_RES_STATUS (res
) = status
;
588 nis_free_request (ibreq
);
594 nis_first_entry (const_nis_name name
)
600 res
= calloc (1, sizeof (nis_result
));
606 NIS_RES_STATUS (res
) = NIS_BADNAME
;
610 if ((ibreq
=__create_ib_request (name
, 0)) == NULL
)
612 NIS_RES_STATUS (res
) = NIS_BADNAME
;
616 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
617 (xdrproc_t
) _xdr_ib_request
,
618 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
619 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
620 NIS_RES_STATUS (res
) = status
;
622 nis_free_request (ibreq
);
628 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
634 res
= calloc (1, sizeof (nis_result
));
640 NIS_RES_STATUS (res
) = NIS_BADNAME
;
644 if (( ibreq
=__create_ib_request (name
, 0)) == NULL
)
646 NIS_RES_STATUS (res
) = NIS_BADNAME
;
652 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
653 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
656 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
657 (xdrproc_t
) _xdr_ib_request
,
658 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
659 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
660 NIS_RES_STATUS (res
) = status
;
664 /* Don't give cookie free, it is not from us */
665 ibreq
->ibr_cookie
.n_bytes
= NULL
;
666 ibreq
->ibr_cookie
.n_len
= 0;
669 nis_free_request (ibreq
);