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@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 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/>. */
20 #include <rpcsvc/nis.h>
22 #include "nis_intern.h"
24 #include <shlib-compat.h>
28 nis_lookup (const_nis_name name
, const unsigned int flags
)
30 nis_result
*res
= calloc (1, sizeof (nis_result
));
31 struct ns_request req
;
34 int link_first_try
= 0;
35 int count_links
= 0; /* We will follow only 16 links in the deep */
38 nis_name namebuf
[2] = {NULL
, NULL
};
43 if ((flags
& EXPAND_NAME
) && (name
[strlen (name
) - 1] != '.'))
45 names
= nis_getnames (name
);
48 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
55 names
[0] = (nis_name
)name
;
58 req
.ns_name
= names
[0];
62 directory_obj
*dir
= NULL
;
63 req
.ns_object
.ns_object_len
= 0;
64 req
.ns_object
.ns_object_val
= NULL
;
66 status
= __prepare_niscall (req
.ns_name
, &dir
, &bptr
, flags
);
67 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
69 NIS_RES_STATUS (res
) = status
;
75 static const struct timeval RPCTIMEOUT
= {10, 0};
76 enum clnt_stat result
;
79 result
= clnt_call (bptr
.clnt
, NIS_LOOKUP
,
80 (xdrproc_t
) _xdr_ns_request
,
81 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
82 (caddr_t
) res
, RPCTIMEOUT
);
84 if (result
!= RPC_SUCCESS
)
85 status
= NIS_RPCERROR
;
90 if (NIS_RES_STATUS (res
) == NIS_SUCCESS
)
92 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
93 && (flags
& FOLLOW_LINKS
)) /* We are following links */
95 /* if we hit the link limit, bail */
96 if (count_links
> NIS_MAXLINKS
)
98 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
103 strdupa (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
105 /* The following is a non-obvious optimization. A
106 nis_freeresult call would call xdr_free as the
107 following code. But it also would unnecessarily
108 free the result structure. We avoid this here
109 along with the necessary tests. */
110 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
111 memset (res
, '\0', sizeof (*res
));
113 link_first_try
= 1; /* Try at first the old binding */
118 if (NIS_RES_STATUS (res
) == NIS_SYSTEMERROR
119 || NIS_RES_STATUS (res
) == NIS_NOSUCHNAME
120 || NIS_RES_STATUS (res
) == NIS_NOT_ME
)
124 __nisbind_destroy (&bptr
);
125 nis_free_directory (dir
);
126 /* Otherwise __nisfind_server will not do anything. */
129 if (__nisfind_server (req
.ns_name
, 1, &dir
, &bptr
,
130 flags
& ~MASTER_ONLY
)
135 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
137 /* No more servers to search. Try parent. */
138 const char *ndomain
= __nis_domain_of (req
.ns_name
);
139 req
.ns_name
= strdupa (ndomain
);
140 if (strcmp (req
.ns_name
, ".") == 0)
142 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
146 __nisbind_destroy (&bptr
);
147 nis_free_directory (dir
);
149 status
= __prepare_niscall (req
.ns_name
, &dir
,
151 if (__glibc_unlikely (status
!= NIS_SUCCESS
))
153 NIS_RES_STATUS (res
) = status
;
159 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
161 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
163 nis_free_directory (dir
);
171 link_first_try
= 0; /* Set it back */
173 while ((flags
& HARD_LOOKUP
) && status
== NIS_RPCERROR
);
175 __nisbind_destroy (&bptr
);
176 nis_free_directory (dir
);
178 if (status
!= NIS_SUCCESS
)
180 NIS_RES_STATUS (res
) = status
;
184 switch (NIS_RES_STATUS (res
))
189 case NIS_LINKNAMEERROR
: /* We follow to max links */
190 case NIS_UNAVAIL
: /* NIS+ is not installed, or all servers are down */
194 /* Try the next domainname if we don't follow a link */
198 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
203 if (names
[name_nr
] == NULL
)
208 req
.ns_name
= names
[name_nr
];
214 if (names
!= namebuf
)
215 nis_freenames (names
);
219 libnsl_hidden_nolink_def (nis_lookup
, GLIBC_2_1
)