1 /* Copyright (c) 1997-2013 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, see
17 <http://www.gnu.org/licenses/>. */
21 #include <rpcsvc/nis.h>
24 #include "nis_intern.h"
29 __create_ib_request (const_nis_name name
, unsigned int flags
)
31 struct ib_request
*ibreq
= calloc (1, sizeof (struct ib_request
));
32 nis_attr
*search_val
= NULL
;
33 size_t search_len
= 0;
39 ibreq
->ibr_flags
= flags
;
41 char *cptr
= strdupa (name
);
43 /* Not of "[key=value,key=value,...],foo.." format? */
46 ibreq
->ibr_name
= strdup (cptr
);
47 if (ibreq
->ibr_name
== NULL
)
55 /* "[key=value,...],foo" format */
56 ibreq
->ibr_name
= strchr (cptr
, ']');
57 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[1] != ',')
59 /* The object has not really been built yet so we use free. */
64 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
65 if (ibreq
->ibr_name
[-1] == ',')
66 ibreq
->ibr_name
[-1] = '\0';
68 ibreq
->ibr_name
[0] = '\0';
70 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
71 if (ibreq
->ibr_name
== NULL
)
74 while (search_len
-- > 0)
76 free (search_val
[search_len
].zattr_ndx
);
77 free (search_val
[search_len
].zattr_val
.zattr_val_val
);
80 nis_free_request (ibreq
);
84 ++cptr
; /* Remove "[" */
86 while (cptr
!= NULL
&& cptr
[0] != '\0')
89 char *val
= strchr (cptr
, '=');
91 cptr
= strchr (key
, ',');
95 if (__builtin_expect (val
== NULL
, 0))
97 nis_free_request (ibreq
);
101 if (search_len
+ 1 >= size
)
104 nis_attr
*newp
= realloc (search_val
, size
* sizeof (nis_attr
));
109 search_val
[search_len
].zattr_ndx
= strdup (key
);
110 if (search_val
[search_len
].zattr_ndx
== NULL
)
113 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
114 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
115 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
117 free (search_val
[search_len
].zattr_ndx
);
124 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
125 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
129 libnsl_hidden_def (__create_ib_request
)
131 static const struct timeval RPCTIMEOUT
= {10, 0};
134 get_tablepath (char *name
, dir_binding
*bptr
)
136 enum clnt_stat result
;
138 struct ns_request req
;
140 memset (&res
, '\0', sizeof (res
));
143 req
.ns_object
.ns_object_len
= 0;
144 req
.ns_object
.ns_object_val
= NULL
;
146 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
147 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
148 (caddr_t
) &res
, RPCTIMEOUT
);
151 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (&res
) == NIS_SUCCESS
152 && __type_of (NIS_RES_OBJECT (&res
)) == NIS_TABLE_OBJ
)
153 cptr
= NIS_RES_OBJECT (&res
)->TA_data
.ta_path
;
157 char *str
= strdup (cptr
);
159 if (result
== RPC_SUCCESS
)
160 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) &res
);
167 __follow_path (char **tablepath
, char **tableptr
, struct ib_request
*ibreq
,
170 if (*tablepath
== NULL
)
172 *tablepath
= get_tablepath (ibreq
->ibr_name
, bptr
);
173 if (*tablepath
== NULL
)
176 *tableptr
= *tablepath
;
178 if (*tableptr
== NULL
)
181 char *newname
= strsep (tableptr
, ":");
182 if (newname
[0] == '\0')
185 newname
= strdup (newname
);
189 free (ibreq
->ibr_name
);
190 ibreq
->ibr_name
= newname
;
194 libnsl_hidden_def (__follow_path
)
198 nis_list (const_nis_name name
, unsigned int flags
,
199 int (*callback
) (const_nis_name name
,
200 const nis_object
*object
,
201 const void *userdata
),
202 const void *userdata
)
204 nis_result
*res
= malloc (sizeof (nis_result
));
207 enum clnt_stat clnt_status
;
208 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
211 nis_name namebuf
[2] = {NULL
, NULL
};
215 char *tablepath
= NULL
;
216 int first_try
= 0; /* Do we try the old binding at first ? */
217 nis_result
*allres
= NULL
;
224 status
= NIS_BADNAME
;
226 nis_freeresult (allres
);
227 memset (res
, '\0', sizeof (nis_result
));
228 NIS_RES_STATUS (res
) = status
;
232 ibreq
= __create_ib_request (name
, flags
);
235 status
= NIS_BADNAME
;
239 if ((flags
& EXPAND_NAME
)
240 && ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
242 names
= nis_getnames (ibreq
->ibr_name
);
243 free (ibreq
->ibr_name
);
244 ibreq
->ibr_name
= NULL
;
247 nis_free_request (ibreq
);
248 status
= NIS_BADNAME
;
251 ibreq
->ibr_name
= strdup (names
[name_nr
]);
252 if (ibreq
->ibr_name
== NULL
)
254 nis_freenames (names
);
255 nis_free_request (ibreq
);
256 status
= NIS_NOMEMORY
;
263 names
[name_nr
] = ibreq
->ibr_name
;
271 directory_obj
*dir
= NULL
;
273 memset (res
, '\0', sizeof (nis_result
));
275 status
= __nisfind_server (ibreq
->ibr_name
,
276 ibreq
->ibr_srch
.ibr_srch_val
!= NULL
,
277 &dir
, &bptr
, flags
& ~MASTER_ONLY
);
278 if (status
!= NIS_SUCCESS
)
280 NIS_RES_STATUS (res
) = status
;
284 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
285 if (__builtin_expect (__nisbind_next (&bptr
) != NIS_SUCCESS
, 0))
287 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
291 if (callback
!= NULL
)
294 cb
= __nis_create_callback (callback
, userdata
, flags
);
295 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
296 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
300 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
301 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
302 (xdrproc_t
) _xdr_nis_result
,
303 (caddr_t
) res
, RPCTIMEOUT
);
305 if (__builtin_expect (clnt_status
!= RPC_SUCCESS
, 0))
306 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
308 switch (NIS_RES_STATUS (res
))
313 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
314 && (flags
& FOLLOW_LINKS
)) /* We are following links. */
316 free (ibreq
->ibr_name
);
317 ibreq
->ibr_name
= NULL
;
318 /* If we hit the link limit, bail. */
319 if (__builtin_expect (count_links
> NIS_MAXLINKS
, 0))
321 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
327 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
328 if (ibreq
->ibr_name
== NULL
)
330 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
332 __nisbind_destroy (&bptr
);
333 nis_free_directory (dir
);
338 __nis_destroy_callback (cb
);
339 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
340 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
342 if (names
!= namebuf
)
343 nis_freenames (names
);
344 nis_free_request (ibreq
);
345 nis_freeresult (allres
);
348 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
349 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
351 ibreq
->ibr_srch
.ibr_srch_len
=
352 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
353 ibreq
->ibr_srch
.ibr_srch_val
=
354 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
356 /* The following is a non-obvious optimization. A
357 nis_freeresult call would call xdr_free as the
358 following code. But it also would unnecessarily
359 free the result structure. We avoid this here
360 along with the necessary tests. */
361 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *)res
);
362 memset (res
, '\0', sizeof (*res
));
363 first_try
= 1; /* Try at first the old binding */
366 else if ((flags
& FOLLOW_PATH
)
367 && NIS_RES_STATUS (res
) == NIS_PARTIAL
)
369 enum nis_error err
= __follow_path (&tablepath
, &tableptr
,
371 if (err
!= NIS_SUCCESS
)
373 if (err
== NIS_NOMEMORY
)
374 NIS_RES_STATUS (res
) = err
;
379 /* The following is a non-obvious optimization. A
380 nis_freeresult call would call xdr_free as the
381 following code. But it also would unnecessarily
382 free the result structure. We avoid this here
383 along with the necessary tests. */
384 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
385 memset (res
, '\0', sizeof (*res
));
390 else if ((flags
& (FOLLOW_PATH
| ALL_RESULTS
))
391 == (FOLLOW_PATH
| ALL_RESULTS
))
396 res
= malloc (sizeof (nis_result
));
401 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
404 NIS_RES_STATUS (res
) = NIS_RES_STATUS (allres
);
408 nis_object
*objects_val
409 = realloc (NIS_RES_OBJECT (allres
),
410 (NIS_RES_NUMOBJ (allres
)
411 + NIS_RES_NUMOBJ (res
))
412 * sizeof (nis_object
));
413 if (objects_val
== NULL
)
415 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
418 NIS_RES_OBJECT (allres
) = objects_val
;
419 memcpy (NIS_RES_OBJECT (allres
) + NIS_RES_NUMOBJ (allres
),
420 NIS_RES_OBJECT (res
),
421 NIS_RES_NUMOBJ (res
) * sizeof (nis_object
));
422 NIS_RES_NUMOBJ (allres
) += NIS_RES_NUMOBJ (res
);
423 NIS_RES_NUMOBJ (res
) = 0;
424 free (NIS_RES_OBJECT (res
));
425 NIS_RES_OBJECT (res
) = NULL
;
426 NIS_RES_STATUS (allres
) = NIS_RES_STATUS (res
);
427 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
429 enum nis_error err
= __follow_path (&tablepath
, &tableptr
,
431 if (err
!= NIS_SUCCESS
)
433 /* Prepare for the nis_freeresult call. */
434 memset (res
, '\0', sizeof (*res
));
436 if (err
== NIS_NOMEMORY
)
437 NIS_RES_STATUS (allres
) = err
;
447 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
448 NIS_RES_STATUS (res
) = cb
->result
;
450 if (!(flags
& ALL_RESULTS
))
455 = __follow_path (&tablepath
, &tableptr
, ibreq
, &bptr
);
456 if (err
!= NIS_SUCCESS
)
458 if (err
== NIS_NOMEMORY
)
459 NIS_RES_STATUS (res
) = err
;
465 case NIS_SYSTEMERROR
:
468 /* If we had first tried the old binding, do nothing, but
472 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
475 break; /* No more servers to search */
477 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
479 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
482 break; /* No more servers to search */
491 /* Try the next domainname if we don't follow a link. */
492 free (ibreq
->ibr_name
);
493 ibreq
->ibr_name
= NULL
;
494 if (__builtin_expect (count_links
, 0))
496 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
501 if (names
[name_nr
] == NULL
)
506 ibreq
->ibr_name
= strdup (names
[name_nr
]);
507 if (ibreq
->ibr_name
== NULL
)
509 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
512 first_try
= 1; /* Try old binding at first */
521 __nis_destroy_callback (cb
);
522 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
523 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
527 __nisbind_destroy (&bptr
);
528 nis_free_directory (dir
);
533 if (names
!= namebuf
)
534 nis_freenames (names
);
536 nis_free_request (ibreq
);
540 nis_freeresult (res
);
546 libnsl_hidden_def (nis_list
)
549 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
551 nis_result
*res
= calloc (1, sizeof (nis_result
));
557 NIS_RES_STATUS (res
) = NIS_BADNAME
;
561 ib_request
*ibreq
= __create_ib_request (name
, flags
);
564 NIS_RES_STATUS (res
) = NIS_BADNAME
;
569 memcpy (&obj
, obj2
, sizeof (nis_object
));
571 size_t namelen
= strlen (name
);
572 char buf1
[namelen
+ 20];
573 char buf4
[namelen
+ 20];
575 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
576 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
578 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
579 obj
.zo_owner
= nis_local_principal ();
581 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
582 obj
.zo_group
= nis_local_group ();
584 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
586 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
587 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
589 nis_free_request (ibreq
);
590 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
593 ibreq
->ibr_obj
.ibr_obj_len
= 1;
595 nis_error status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
596 (xdrproc_t
) _xdr_ib_request
,
598 (xdrproc_t
) _xdr_nis_result
,
599 (caddr_t
) res
, 0, NULL
);
600 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
601 NIS_RES_STATUS (res
) = status
;
603 nis_free_request (ibreq
);
609 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
616 size_t namelen
= strlen (name
);
617 char buf1
[namelen
+ 20];
618 char buf4
[namelen
+ 20];
620 res
= calloc (1, sizeof (nis_result
));
624 ibreq
= __create_ib_request (name
, flags
);
627 NIS_RES_STATUS (res
) = NIS_BADNAME
;
631 memcpy (&obj
, obj2
, sizeof (nis_object
));
633 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
634 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
636 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
637 obj
.zo_owner
= nis_local_principal ();
639 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
640 obj
.zo_group
= nis_local_group ();
642 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
644 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
645 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
647 nis_free_request (ibreq
);
648 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
651 ibreq
->ibr_obj
.ibr_obj_len
= 1;
653 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
654 (xdrproc_t
) _xdr_ib_request
,
655 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
656 (caddr_t
) res
, 0, NULL
);
657 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
658 NIS_RES_STATUS (res
) = status
;
660 nis_free_request (ibreq
);
666 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
673 res
= calloc (1, sizeof (nis_result
));
679 NIS_RES_STATUS (res
) = NIS_BADNAME
;
683 ibreq
= __create_ib_request (name
, flags
);
686 NIS_RES_STATUS (res
) = NIS_BADNAME
;
692 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
693 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
695 nis_free_request (ibreq
);
696 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
699 ibreq
->ibr_obj
.ibr_obj_len
= 1;
702 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
703 (xdrproc_t
) _xdr_ib_request
,
704 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
705 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
706 NIS_RES_STATUS (res
) = status
;
708 nis_free_request (ibreq
);
714 nis_first_entry (const_nis_name name
)
720 res
= calloc (1, sizeof (nis_result
));
726 NIS_RES_STATUS (res
) = NIS_BADNAME
;
730 ibreq
= __create_ib_request (name
, 0);
733 NIS_RES_STATUS (res
) = NIS_BADNAME
;
737 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
738 (xdrproc_t
) _xdr_ib_request
,
739 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
740 (caddr_t
) res
, 0, NULL
);
742 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
743 NIS_RES_STATUS (res
) = status
;
745 nis_free_request (ibreq
);
751 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
757 res
= calloc (1, sizeof (nis_result
));
763 NIS_RES_STATUS (res
) = NIS_BADNAME
;
767 ibreq
= __create_ib_request (name
, 0);
770 NIS_RES_STATUS (res
) = NIS_BADNAME
;
776 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
777 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
780 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
781 (xdrproc_t
) _xdr_ib_request
,
782 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
783 (caddr_t
) res
, 0, NULL
);
785 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
786 NIS_RES_STATUS (res
) = status
;
790 /* Don't give cookie free, it is not from us */
791 ibreq
->ibr_cookie
.n_bytes
= NULL
;
792 ibreq
->ibr_cookie
.n_len
= 0;
795 nis_free_request (ibreq
);