1 /* Copyright (c) 1997-2018 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>
22 #include <libc-diag.h>
23 #include <shlib-compat.h>
26 #include "nis_intern.h"
31 __create_ib_request (const_nis_name name
, unsigned int flags
)
33 struct ib_request
*ibreq
= calloc (1, sizeof (struct ib_request
));
34 nis_attr
*search_val
= NULL
;
35 size_t search_len
= 0;
41 ibreq
->ibr_flags
= flags
;
43 char *cptr
= strdupa (name
);
45 /* Not of "[key=value,key=value,...],foo.." format? */
48 ibreq
->ibr_name
= strdup (cptr
);
49 if (ibreq
->ibr_name
== NULL
)
57 /* "[key=value,...],foo" format */
58 ibreq
->ibr_name
= strchr (cptr
, ']');
59 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[1] != ',')
61 /* The object has not really been built yet so we use free. */
66 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
67 if (ibreq
->ibr_name
[-1] == ',')
68 ibreq
->ibr_name
[-1] = '\0';
70 ibreq
->ibr_name
[0] = '\0';
72 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
73 if (ibreq
->ibr_name
== NULL
)
76 while (search_len
-- > 0)
78 free (search_val
[search_len
].zattr_ndx
);
79 free (search_val
[search_len
].zattr_val
.zattr_val_val
);
82 nis_free_request (ibreq
);
86 ++cptr
; /* Remove "[" */
88 while (cptr
!= NULL
&& cptr
[0] != '\0')
91 char *val
= strchr (cptr
, '=');
93 cptr
= strchr (key
, ',');
97 if (__glibc_unlikely (val
== NULL
))
99 nis_free_request (ibreq
);
103 if (search_len
+ 1 >= size
)
106 nis_attr
*newp
= realloc (search_val
, size
* sizeof (nis_attr
));
111 search_val
[search_len
].zattr_ndx
= strdup (key
);
112 if (search_val
[search_len
].zattr_ndx
== NULL
)
115 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
116 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
117 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
119 free (search_val
[search_len
].zattr_ndx
);
126 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
127 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
131 libnsl_hidden_nolink_def (__create_ib_request
, GLIBC_PRIVATE
)
133 static const struct timeval RPCTIMEOUT
= {10, 0};
136 get_tablepath (char *name
, dir_binding
*bptr
)
138 enum clnt_stat result
;
140 struct ns_request req
;
142 memset (&res
, '\0', sizeof (res
));
145 req
.ns_object
.ns_object_len
= 0;
146 req
.ns_object
.ns_object_val
= NULL
;
148 result
= clnt_call (bptr
->clnt
, NIS_LOOKUP
, (xdrproc_t
) _xdr_ns_request
,
149 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
150 (caddr_t
) &res
, RPCTIMEOUT
);
153 if (result
== RPC_SUCCESS
&& NIS_RES_STATUS (&res
) == NIS_SUCCESS
154 && __type_of (NIS_RES_OBJECT (&res
)) == NIS_TABLE_OBJ
)
155 cptr
= NIS_RES_OBJECT (&res
)->TA_data
.ta_path
;
159 char *str
= strdup (cptr
);
161 if (result
== RPC_SUCCESS
)
162 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) &res
);
169 __follow_path (char **tablepath
, char **tableptr
, struct ib_request
*ibreq
,
172 if (*tablepath
== NULL
)
174 *tablepath
= get_tablepath (ibreq
->ibr_name
, bptr
);
175 if (*tablepath
== NULL
)
178 *tableptr
= *tablepath
;
181 /* Since tableptr is only set here, and it's set when tablepath is NULL,
182 which it is initially defined as, we know it will always be set here. */
183 DIAG_PUSH_NEEDS_COMMENT
;
184 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
186 if (*tableptr
== NULL
)
189 char *newname
= strsep (tableptr
, ":");
190 if (newname
[0] == '\0')
193 DIAG_POP_NEEDS_COMMENT
;
195 newname
= strdup (newname
);
199 free (ibreq
->ibr_name
);
200 ibreq
->ibr_name
= newname
;
204 libnsl_hidden_nolink_def (__follow_path
, GLIBC_PRIVATE
)
208 nis_list (const_nis_name name
, unsigned int flags
,
209 int (*callback
) (const_nis_name name
,
210 const nis_object
*object
,
211 const void *userdata
),
212 const void *userdata
)
214 nis_result
*res
= malloc (sizeof (nis_result
));
217 enum clnt_stat clnt_status
;
218 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
221 nis_name namebuf
[2] = {NULL
, NULL
};
225 char *tablepath
= NULL
;
226 int first_try
= 0; /* Do we try the old binding at first ? */
227 nis_result
*allres
= NULL
;
234 status
= NIS_BADNAME
;
236 nis_freeresult (allres
);
237 memset (res
, '\0', sizeof (nis_result
));
238 NIS_RES_STATUS (res
) = status
;
242 ibreq
= __create_ib_request (name
, flags
);
245 status
= NIS_BADNAME
;
249 if ((flags
& EXPAND_NAME
)
250 && ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
252 names
= nis_getnames (ibreq
->ibr_name
);
253 free (ibreq
->ibr_name
);
254 ibreq
->ibr_name
= NULL
;
257 nis_free_request (ibreq
);
258 status
= NIS_BADNAME
;
261 ibreq
->ibr_name
= strdup (names
[name_nr
]);
262 if (ibreq
->ibr_name
== NULL
)
264 nis_freenames (names
);
265 nis_free_request (ibreq
);
266 status
= NIS_NOMEMORY
;
273 names
[name_nr
] = ibreq
->ibr_name
;
281 directory_obj
*dir
= NULL
;
283 memset (res
, '\0', sizeof (nis_result
));
285 status
= __nisfind_server (ibreq
->ibr_name
,
286 ibreq
->ibr_srch
.ibr_srch_val
!= NULL
,
287 &dir
, &bptr
, flags
& ~MASTER_ONLY
);
288 if (status
!= NIS_SUCCESS
)
290 NIS_RES_STATUS (res
) = status
;
294 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
295 if (__glibc_unlikely (__nisbind_next (&bptr
) != NIS_SUCCESS
))
297 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
301 if (callback
!= NULL
)
304 cb
= __nis_create_callback (callback
, userdata
, flags
);
305 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
306 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
310 clnt_status
= clnt_call (bptr
.clnt
, NIS_IBLIST
,
311 (xdrproc_t
) _xdr_ib_request
, (caddr_t
) ibreq
,
312 (xdrproc_t
) _xdr_nis_result
,
313 (caddr_t
) res
, RPCTIMEOUT
);
315 if (__glibc_unlikely (clnt_status
!= RPC_SUCCESS
))
316 NIS_RES_STATUS (res
) = NIS_RPCERROR
;
318 switch (NIS_RES_STATUS (res
))
323 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
324 && (flags
& FOLLOW_LINKS
)) /* We are following links. */
326 free (ibreq
->ibr_name
);
327 ibreq
->ibr_name
= NULL
;
328 /* If we hit the link limit, bail. */
329 if (__glibc_unlikely (count_links
> NIS_MAXLINKS
))
331 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
337 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
338 if (ibreq
->ibr_name
== NULL
)
340 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
342 __nisbind_destroy (&bptr
);
343 nis_free_directory (dir
);
348 __nis_destroy_callback (cb
);
349 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
350 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
352 if (names
!= namebuf
)
353 nis_freenames (names
);
354 nis_free_request (ibreq
);
355 nis_freeresult (allres
);
358 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
359 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
361 ibreq
->ibr_srch
.ibr_srch_len
=
362 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
363 ibreq
->ibr_srch
.ibr_srch_val
=
364 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
366 /* The following is a non-obvious optimization. A
367 nis_freeresult call would call xdr_free as the
368 following code. But it also would unnecessarily
369 free the result structure. We avoid this here
370 along with the necessary tests. */
371 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *)res
);
372 memset (res
, '\0', sizeof (*res
));
373 first_try
= 1; /* Try at first the old binding */
376 else if ((flags
& FOLLOW_PATH
)
377 && NIS_RES_STATUS (res
) == NIS_PARTIAL
)
379 enum nis_error err
= __follow_path (&tablepath
, &tableptr
,
381 if (err
!= NIS_SUCCESS
)
383 if (err
== NIS_NOMEMORY
)
384 NIS_RES_STATUS (res
) = err
;
389 /* The following is a non-obvious optimization. A
390 nis_freeresult call would call xdr_free as the
391 following code. But it also would unnecessarily
392 free the result structure. We avoid this here
393 along with the necessary tests. */
394 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
395 memset (res
, '\0', sizeof (*res
));
400 else if ((flags
& (FOLLOW_PATH
| ALL_RESULTS
))
401 == (FOLLOW_PATH
| ALL_RESULTS
))
406 res
= malloc (sizeof (nis_result
));
411 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
414 NIS_RES_STATUS (res
) = NIS_RES_STATUS (allres
);
418 nis_object
*objects_val
419 = realloc (NIS_RES_OBJECT (allres
),
420 (NIS_RES_NUMOBJ (allres
)
421 + NIS_RES_NUMOBJ (res
))
422 * sizeof (nis_object
));
423 if (objects_val
== NULL
)
425 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
428 NIS_RES_OBJECT (allres
) = objects_val
;
429 memcpy (NIS_RES_OBJECT (allres
) + NIS_RES_NUMOBJ (allres
),
430 NIS_RES_OBJECT (res
),
431 NIS_RES_NUMOBJ (res
) * sizeof (nis_object
));
432 NIS_RES_NUMOBJ (allres
) += NIS_RES_NUMOBJ (res
);
433 NIS_RES_NUMOBJ (res
) = 0;
434 free (NIS_RES_OBJECT (res
));
435 NIS_RES_OBJECT (res
) = NULL
;
436 NIS_RES_STATUS (allres
) = NIS_RES_STATUS (res
);
437 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
439 enum nis_error err
= __follow_path (&tablepath
, &tableptr
,
441 if (err
!= NIS_SUCCESS
)
443 /* Prepare for the nis_freeresult call. */
444 memset (res
, '\0', sizeof (*res
));
446 if (err
== NIS_NOMEMORY
)
447 NIS_RES_STATUS (allres
) = err
;
457 __nis_do_callback (&bptr
, &res
->cookie
, cb
);
458 NIS_RES_STATUS (res
) = cb
->result
;
460 if (!(flags
& ALL_RESULTS
))
465 = __follow_path (&tablepath
, &tableptr
, ibreq
, &bptr
);
466 if (err
!= NIS_SUCCESS
)
468 if (err
== NIS_NOMEMORY
)
469 NIS_RES_STATUS (res
) = err
;
475 case NIS_SYSTEMERROR
:
478 /* If we had first tried the old binding, do nothing, but
482 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
485 break; /* No more servers to search */
487 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
489 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
492 break; /* No more servers to search */
501 /* Try the next domainname if we don't follow a link. */
502 free (ibreq
->ibr_name
);
503 ibreq
->ibr_name
= NULL
;
504 if (__glibc_unlikely (count_links
))
506 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
511 if (names
[name_nr
] == NULL
)
516 ibreq
->ibr_name
= strdup (names
[name_nr
]);
517 if (ibreq
->ibr_name
== NULL
)
519 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
522 first_try
= 1; /* Try old binding at first */
531 __nis_destroy_callback (cb
);
532 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
533 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
537 __nisbind_destroy (&bptr
);
538 nis_free_directory (dir
);
543 if (names
!= namebuf
)
544 nis_freenames (names
);
546 nis_free_request (ibreq
);
550 nis_freeresult (res
);
556 libnsl_hidden_nolink_def (nis_list
, GLIBC_2_1
)
559 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, unsigned int flags
)
561 nis_result
*res
= calloc (1, sizeof (nis_result
));
567 NIS_RES_STATUS (res
) = NIS_BADNAME
;
571 ib_request
*ibreq
= __create_ib_request (name
, flags
);
574 NIS_RES_STATUS (res
) = NIS_BADNAME
;
579 memcpy (&obj
, obj2
, sizeof (nis_object
));
581 size_t namelen
= strlen (name
);
582 char buf1
[namelen
+ 20];
583 char buf4
[namelen
+ 20];
585 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
586 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
588 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
589 obj
.zo_owner
= nis_local_principal ();
591 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
592 obj
.zo_group
= nis_local_group ();
594 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
596 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
597 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
599 nis_free_request (ibreq
);
600 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
603 ibreq
->ibr_obj
.ibr_obj_len
= 1;
605 nis_error status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
606 (xdrproc_t
) _xdr_ib_request
,
608 (xdrproc_t
) _xdr_nis_result
,
609 (caddr_t
) res
, 0, NULL
);
610 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
611 NIS_RES_STATUS (res
) = status
;
613 nis_free_request (ibreq
);
617 libnsl_hidden_nolink_def (nis_add_entry
, GLIBC_2_1
)
620 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
,
627 size_t namelen
= strlen (name
);
628 char buf1
[namelen
+ 20];
629 char buf4
[namelen
+ 20];
631 res
= calloc (1, sizeof (nis_result
));
635 ibreq
= __create_ib_request (name
, flags
);
638 NIS_RES_STATUS (res
) = NIS_BADNAME
;
642 memcpy (&obj
, obj2
, sizeof (nis_object
));
644 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
645 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
647 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
648 obj
.zo_owner
= nis_local_principal ();
650 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
651 obj
.zo_group
= nis_local_group ();
653 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
655 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
656 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
658 nis_free_request (ibreq
);
659 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
662 ibreq
->ibr_obj
.ibr_obj_len
= 1;
664 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
665 (xdrproc_t
) _xdr_ib_request
,
666 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
667 (caddr_t
) res
, 0, NULL
);
668 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
669 NIS_RES_STATUS (res
) = status
;
671 nis_free_request (ibreq
);
675 libnsl_hidden_nolink_def (nis_modify_entry
, GLIBC_2_1
)
678 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
685 res
= calloc (1, sizeof (nis_result
));
691 NIS_RES_STATUS (res
) = NIS_BADNAME
;
695 ibreq
= __create_ib_request (name
, flags
);
698 NIS_RES_STATUS (res
) = NIS_BADNAME
;
704 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
705 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
707 nis_free_request (ibreq
);
708 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
711 ibreq
->ibr_obj
.ibr_obj_len
= 1;
714 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
715 (xdrproc_t
) _xdr_ib_request
,
716 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
717 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
718 NIS_RES_STATUS (res
) = status
;
720 nis_free_request (ibreq
);
724 libnsl_hidden_nolink_def (nis_remove_entry
, GLIBC_2_1
)
727 nis_first_entry (const_nis_name name
)
733 res
= calloc (1, sizeof (nis_result
));
739 NIS_RES_STATUS (res
) = NIS_BADNAME
;
743 ibreq
= __create_ib_request (name
, 0);
746 NIS_RES_STATUS (res
) = NIS_BADNAME
;
750 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
751 (xdrproc_t
) _xdr_ib_request
,
752 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
753 (caddr_t
) res
, 0, NULL
);
755 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
756 NIS_RES_STATUS (res
) = status
;
758 nis_free_request (ibreq
);
762 libnsl_hidden_nolink_def (nis_first_entry
, GLIBC_2_1
)
765 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
771 res
= calloc (1, sizeof (nis_result
));
777 NIS_RES_STATUS (res
) = NIS_BADNAME
;
781 ibreq
= __create_ib_request (name
, 0);
784 NIS_RES_STATUS (res
) = NIS_BADNAME
;
790 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
791 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
794 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
795 (xdrproc_t
) _xdr_ib_request
,
796 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
797 (caddr_t
) res
, 0, NULL
);
799 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
800 NIS_RES_STATUS (res
) = status
;
804 /* Don't give cookie free, it is not from us */
805 ibreq
->ibr_cookie
.n_bytes
= NULL
;
806 ibreq
->ibr_cookie
.n_len
= 0;
809 nis_free_request (ibreq
);
813 libnsl_hidden_nolink_def (nis_next_entry
, GLIBC_2_1
)