1 /* Copyright (c) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
20 #include <rpcsvc/nis.h>
21 #include <libc-diag.h>
22 #include <shlib-compat.h>
25 #include "nis_intern.h"
30 __create_ib_request (const_nis_name name
, unsigned int flags
)
32 struct ib_request
*ibreq
= calloc (1, sizeof (struct ib_request
));
33 nis_attr
*search_val
= NULL
;
34 size_t search_len
= 0;
40 ibreq
->ibr_flags
= flags
;
42 char *cptr
= strdupa (name
);
44 /* Not of "[key=value,key=value,...],foo.." format? */
47 ibreq
->ibr_name
= strdup (cptr
);
48 if (ibreq
->ibr_name
== NULL
)
56 /* "[key=value,...],foo" format */
57 ibreq
->ibr_name
= strchr (cptr
, ']');
58 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[1] != ',')
60 /* The object has not really been built yet so we use free. */
65 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
66 if (ibreq
->ibr_name
[-1] == ',')
67 ibreq
->ibr_name
[-1] = '\0';
69 ibreq
->ibr_name
[0] = '\0';
71 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
72 if (ibreq
->ibr_name
== NULL
)
75 while (search_len
-- > 0)
77 free (search_val
[search_len
].zattr_ndx
);
78 free (search_val
[search_len
].zattr_val
.zattr_val_val
);
81 nis_free_request (ibreq
);
85 ++cptr
; /* Remove "[" */
87 while (cptr
!= NULL
&& cptr
[0] != '\0')
90 char *val
= strchr (cptr
, '=');
92 cptr
= strchr (key
, ',');
96 if (__glibc_unlikely (val
== NULL
))
98 nis_free_request (ibreq
);
102 if (search_len
+ 1 >= size
)
105 nis_attr
*newp
= realloc (search_val
, size
* sizeof (nis_attr
));
110 search_val
[search_len
].zattr_ndx
= strdup (key
);
111 if (search_val
[search_len
].zattr_ndx
== NULL
)
114 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
115 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
116 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
118 free (search_val
[search_len
].zattr_ndx
);
125 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
126 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
130 libnsl_hidden_nolink_def (__create_ib_request
, GLIBC_PRIVATE
)
132 static const struct timeval RPCTIMEOUT
= {10, 0};
135 get_tablepath (char *name
, dir_binding
*bptr
)
137 enum clnt_stat result
;
139 struct ns_request req
;
141 memset (&res
, '\0', sizeof (res
));
144 req
.ns_object
.ns_object_len
= 0;
145 req
.ns_object
.ns_object_val
= NULL
;
147 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
148 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
149 (caddr_t
) &res
, RPCTIMEOUT
);
152 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (&res
) == NIS_SUCCESS
153 && __type_of (NIS_RES_OBJECT (&res
)) == NIS_TABLE_OBJ
)
154 cptr
= NIS_RES_OBJECT (&res
)->TA_data
.ta_path
;
158 char *str
= strdup (cptr
);
160 if (result
== RPC_SUCCESS
)
161 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) &res
);
168 __follow_path (char **tablepath
, char **tableptr
, struct ib_request
*ibreq
,
171 if (*tablepath
== NULL
)
173 *tablepath
= get_tablepath (ibreq
->ibr_name
, bptr
);
174 if (*tablepath
== NULL
)
177 *tableptr
= *tablepath
;
180 /* Since tableptr is only set here, and it's set when tablepath is NULL,
181 which it is initially defined as, we know it will always be set here. */
182 DIAG_PUSH_NEEDS_COMMENT
;
183 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
185 if (*tableptr
== NULL
)
188 char *newname
= strsep (tableptr
, ":");
189 if (newname
[0] == '\0')
192 DIAG_POP_NEEDS_COMMENT
;
194 newname
= strdup (newname
);
198 free (ibreq
->ibr_name
);
199 ibreq
->ibr_name
= newname
;
203 libnsl_hidden_nolink_def (__follow_path
, GLIBC_PRIVATE
)
207 nis_list (const_nis_name name
, unsigned int flags
,
208 int (*callback
) (const_nis_name name
,
209 const nis_object
*object
,
210 const void *userdata
),
211 const void *userdata
)
213 nis_result
*res
= malloc (sizeof (nis_result
));
216 enum clnt_stat clnt_status
;
217 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
220 nis_name namebuf
[2] = {NULL
, NULL
};
224 char *tablepath
= NULL
;
225 int first_try
= 0; /* Do we try the old binding at first ? */
226 nis_result
*allres
= NULL
;
233 status
= NIS_BADNAME
;
235 nis_freeresult (allres
);
236 memset (res
, '\0', sizeof (nis_result
));
237 NIS_RES_STATUS (res
) = status
;
241 ibreq
= __create_ib_request (name
, flags
);
244 status
= NIS_BADNAME
;
248 if ((flags
& EXPAND_NAME
)
249 && ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
251 names
= nis_getnames (ibreq
->ibr_name
);
252 free (ibreq
->ibr_name
);
253 ibreq
->ibr_name
= NULL
;
256 nis_free_request (ibreq
);
257 status
= NIS_BADNAME
;
260 ibreq
->ibr_name
= strdup (names
[name_nr
]);
261 if (ibreq
->ibr_name
== NULL
)
263 nis_freenames (names
);
264 nis_free_request (ibreq
);
265 status
= NIS_NOMEMORY
;
272 names
[name_nr
] = ibreq
->ibr_name
;
280 directory_obj
*dir
= NULL
;
282 memset (res
, '\0', sizeof (nis_result
));
284 status
= __nisfind_server (ibreq
->ibr_name
,
285 ibreq
->ibr_srch
.ibr_srch_val
!= NULL
,
286 &dir
, &bptr
, flags
& ~MASTER_ONLY
);
287 if (status
!= NIS_SUCCESS
)
289 NIS_RES_STATUS (res
) = status
;
293 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
294 if (__glibc_unlikely (__nisbind_next (&bptr
) != NIS_SUCCESS
))
296 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
300 if (callback
!= NULL
)
303 cb
= __nis_create_callback (callback
, userdata
, flags
);
304 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
305 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
309 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
310 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
311 (xdrproc_t
) _xdr_nis_result
,
312 (caddr_t
) res
, RPCTIMEOUT
);
314 if (__glibc_unlikely (clnt_status
!= RPC_SUCCESS
))
315 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
317 switch (NIS_RES_STATUS (res
))
322 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
323 && (flags
& FOLLOW_LINKS
)) /* We are following links. */
325 free (ibreq
->ibr_name
);
326 ibreq
->ibr_name
= NULL
;
327 /* If we hit the link limit, bail. */
328 if (__glibc_unlikely (count_links
> NIS_MAXLINKS
))
330 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
336 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
337 if (ibreq
->ibr_name
== NULL
)
339 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
341 __nisbind_destroy (&bptr
);
342 nis_free_directory (dir
);
347 __nis_destroy_callback (cb
);
348 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
349 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
351 if (names
!= namebuf
)
352 nis_freenames (names
);
353 nis_free_request (ibreq
);
354 nis_freeresult (allres
);
357 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
358 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
360 ibreq
->ibr_srch
.ibr_srch_len
=
361 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
362 ibreq
->ibr_srch
.ibr_srch_val
=
363 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
365 /* The following is a non-obvious optimization. A
366 nis_freeresult call would call xdr_free as the
367 following code. But it also would unnecessarily
368 free the result structure. We avoid this here
369 along with the necessary tests. */
370 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *)res
);
371 memset (res
, '\0', sizeof (*res
));
372 first_try
= 1; /* Try at first the old binding */
375 else if ((flags
& FOLLOW_PATH
)
376 && NIS_RES_STATUS (res
) == NIS_PARTIAL
)
378 enum nis_error err
= __follow_path (&tablepath
, &tableptr
,
380 if (err
!= NIS_SUCCESS
)
382 if (err
== NIS_NOMEMORY
)
383 NIS_RES_STATUS (res
) = err
;
388 /* The following is a non-obvious optimization. A
389 nis_freeresult call would call xdr_free as the
390 following code. But it also would unnecessarily
391 free the result structure. We avoid this here
392 along with the necessary tests. */
393 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
394 memset (res
, '\0', sizeof (*res
));
399 else if ((flags
& (FOLLOW_PATH
| ALL_RESULTS
))
400 == (FOLLOW_PATH
| ALL_RESULTS
))
405 res
= malloc (sizeof (nis_result
));
410 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
413 NIS_RES_STATUS (res
) = NIS_RES_STATUS (allres
);
417 nis_object
*objects_val
418 = realloc (NIS_RES_OBJECT (allres
),
419 (NIS_RES_NUMOBJ (allres
)
420 + NIS_RES_NUMOBJ (res
))
421 * sizeof (nis_object
));
422 if (objects_val
== NULL
)
424 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
427 NIS_RES_OBJECT (allres
) = objects_val
;
428 memcpy (NIS_RES_OBJECT (allres
) + NIS_RES_NUMOBJ (allres
),
429 NIS_RES_OBJECT (res
),
430 NIS_RES_NUMOBJ (res
) * sizeof (nis_object
));
431 NIS_RES_NUMOBJ (allres
) += NIS_RES_NUMOBJ (res
);
432 NIS_RES_NUMOBJ (res
) = 0;
433 free (NIS_RES_OBJECT (res
));
434 NIS_RES_OBJECT (res
) = NULL
;
435 NIS_RES_STATUS (allres
) = NIS_RES_STATUS (res
);
436 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
438 enum nis_error err
= __follow_path (&tablepath
, &tableptr
,
440 if (err
!= NIS_SUCCESS
)
442 /* Prepare for the nis_freeresult call. */
443 memset (res
, '\0', sizeof (*res
));
445 if (err
== NIS_NOMEMORY
)
446 NIS_RES_STATUS (allres
) = err
;
456 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
457 NIS_RES_STATUS (res
) = cb
->result
;
459 if (!(flags
& ALL_RESULTS
))
464 = __follow_path (&tablepath
, &tableptr
, ibreq
, &bptr
);
465 if (err
!= NIS_SUCCESS
)
467 if (err
== NIS_NOMEMORY
)
468 NIS_RES_STATUS (res
) = err
;
474 case NIS_SYSTEMERROR
:
477 /* If we had first tried the old binding, do nothing, but
481 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
484 break; /* No more servers to search */
486 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
488 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
491 break; /* No more servers to search */
500 /* Try the next domainname if we don't follow a link. */
501 free (ibreq
->ibr_name
);
502 ibreq
->ibr_name
= NULL
;
503 if (__glibc_unlikely (count_links
))
505 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
510 if (names
[name_nr
] == NULL
)
515 ibreq
->ibr_name
= strdup (names
[name_nr
]);
516 if (ibreq
->ibr_name
== NULL
)
518 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
521 first_try
= 1; /* Try old binding at first */
530 __nis_destroy_callback (cb
);
531 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
532 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
536 __nisbind_destroy (&bptr
);
537 nis_free_directory (dir
);
542 if (names
!= namebuf
)
543 nis_freenames (names
);
545 nis_free_request (ibreq
);
549 nis_freeresult (res
);
555 libnsl_hidden_nolink_def (nis_list
, GLIBC_2_1
)
558 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
560 nis_result
*res
= calloc (1, sizeof (nis_result
));
566 NIS_RES_STATUS (res
) = NIS_BADNAME
;
570 ib_request
*ibreq
= __create_ib_request (name
, flags
);
573 NIS_RES_STATUS (res
) = NIS_BADNAME
;
578 memcpy (&obj
, obj2
, sizeof (nis_object
));
580 size_t namelen
= strlen (name
);
581 char buf1
[namelen
+ 20];
582 char buf4
[namelen
+ 20];
584 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
585 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
587 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
588 obj
.zo_owner
= nis_local_principal ();
590 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
591 obj
.zo_group
= nis_local_group ();
593 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
595 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
596 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
598 nis_free_request (ibreq
);
599 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
602 ibreq
->ibr_obj
.ibr_obj_len
= 1;
604 nis_error status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
605 (xdrproc_t
) _xdr_ib_request
,
607 (xdrproc_t
) _xdr_nis_result
,
608 (caddr_t
) res
, 0, NULL
);
609 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
610 NIS_RES_STATUS (res
) = status
;
612 nis_free_request (ibreq
);
616 libnsl_hidden_nolink_def (nis_add_entry
, GLIBC_2_1
)
619 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
626 size_t namelen
= strlen (name
);
627 char buf1
[namelen
+ 20];
628 char buf4
[namelen
+ 20];
630 res
= calloc (1, sizeof (nis_result
));
634 ibreq
= __create_ib_request (name
, flags
);
637 NIS_RES_STATUS (res
) = NIS_BADNAME
;
641 memcpy (&obj
, obj2
, sizeof (nis_object
));
643 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
644 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
646 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
647 obj
.zo_owner
= nis_local_principal ();
649 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
650 obj
.zo_group
= nis_local_group ();
652 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
654 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
655 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
657 nis_free_request (ibreq
);
658 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
661 ibreq
->ibr_obj
.ibr_obj_len
= 1;
663 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
664 (xdrproc_t
) _xdr_ib_request
,
665 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
666 (caddr_t
) res
, 0, NULL
);
667 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
668 NIS_RES_STATUS (res
) = status
;
670 nis_free_request (ibreq
);
674 libnsl_hidden_nolink_def (nis_modify_entry
, GLIBC_2_1
)
677 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
684 res
= calloc (1, sizeof (nis_result
));
690 NIS_RES_STATUS (res
) = NIS_BADNAME
;
694 ibreq
= __create_ib_request (name
, flags
);
697 NIS_RES_STATUS (res
) = NIS_BADNAME
;
703 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
704 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
706 nis_free_request (ibreq
);
707 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
710 ibreq
->ibr_obj
.ibr_obj_len
= 1;
713 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
714 (xdrproc_t
) _xdr_ib_request
,
715 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
716 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
717 NIS_RES_STATUS (res
) = status
;
719 nis_free_request (ibreq
);
723 libnsl_hidden_nolink_def (nis_remove_entry
, GLIBC_2_1
)
726 nis_first_entry (const_nis_name name
)
732 res
= calloc (1, sizeof (nis_result
));
738 NIS_RES_STATUS (res
) = NIS_BADNAME
;
742 ibreq
= __create_ib_request (name
, 0);
745 NIS_RES_STATUS (res
) = NIS_BADNAME
;
749 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
750 (xdrproc_t
) _xdr_ib_request
,
751 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
752 (caddr_t
) res
, 0, NULL
);
754 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
755 NIS_RES_STATUS (res
) = status
;
757 nis_free_request (ibreq
);
761 libnsl_hidden_nolink_def (nis_first_entry
, GLIBC_2_1
)
764 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
770 res
= calloc (1, sizeof (nis_result
));
776 NIS_RES_STATUS (res
) = NIS_BADNAME
;
780 ibreq
= __create_ib_request (name
, 0);
783 NIS_RES_STATUS (res
) = NIS_BADNAME
;
789 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
790 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
793 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
794 (xdrproc_t
) _xdr_ib_request
,
795 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
796 (caddr_t
) res
, 0, NULL
);
798 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
799 NIS_RES_STATUS (res
) = status
;
803 /* Don't give cookie free, it is not from us */
804 ibreq
->ibr_cookie
.n_bytes
= NULL
;
805 ibreq
->ibr_cookie
.n_len
= 0;
808 nis_free_request (ibreq
);
812 libnsl_hidden_nolink_def (nis_next_entry
, GLIBC_2_1
)