Updated to fedora-glibc-20060511T1325
[glibc.git] / nis / nis_lookup.c
blob821b9bce73a080b4aaa4023022833f1a674417af
1 /* Copyright (C) 1997-1999, 2004, 2005, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <string.h>
21 #include <rpcsvc/nis.h>
22 #include "nis_xdr.h"
23 #include "nis_intern.h"
25 nis_result *
26 nis_lookup (const_nis_name name, const unsigned int flags)
28 nis_result *res = calloc (1, sizeof (nis_result));
29 struct ns_request req;
30 nis_name *names;
31 nis_error status;
32 int link_first_try = 0;
33 int count_links = 0; /* We will follow only 16 links in the deep */
34 int done = 0;
35 int name_nr = 0;
36 nis_name namebuf[2] = {NULL, NULL};
38 if (res == NULL)
39 return NULL;
41 if ((flags & EXPAND_NAME) && (name[strlen (name) - 1] != '.'))
43 names = nis_getnames (name);
44 if (names == NULL)
46 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
47 return res;
50 else
52 names = namebuf;
53 names[0] = (nis_name)name;
56 req.ns_name = names[0];
57 while (!done)
59 dir_binding bptr;
60 directory_obj *dir = NULL;
61 req.ns_object.ns_object_len = 0;
62 req.ns_object.ns_object_val = NULL;
64 status = __nisfind_server (req.ns_name, &dir);
65 if (status != NIS_SUCCESS)
67 NIS_RES_STATUS (res) = status;
68 goto out;
71 status = __nisbind_create (&bptr, dir->do_servers.do_servers_val,
72 dir->do_servers.do_servers_len, flags);
73 if (status != NIS_SUCCESS)
75 NIS_RES_STATUS (res) = status;
76 nis_free_directory (dir);
77 goto out;;
80 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
82 if (__nisbind_next (&bptr) != NIS_SUCCESS)
84 nis_free_directory (dir);
85 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
86 goto out;
92 static const struct timeval RPCTIMEOUT = {10, 0};
93 enum clnt_stat result;
95 again:
96 result = clnt_call (bptr.clnt, NIS_LOOKUP,
97 (xdrproc_t) _xdr_ns_request,
98 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
99 (caddr_t) res, RPCTIMEOUT);
101 if (result != RPC_SUCCESS)
102 status = NIS_RPCERROR;
103 else
105 status = NIS_SUCCESS;
107 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
109 if (__type_of(NIS_RES_OBJECT (res)) == NIS_LINK_OBJ
110 && (flags & FOLLOW_LINKS)) /* We are following links */
112 if (count_links)
113 free (req.ns_name);
114 /* if we hit the link limit, bail */
115 if (count_links > NIS_MAXLINKS)
117 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
118 break;
120 ++count_links;
121 req.ns_name =
122 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
123 if (req.ns_name == NULL)
125 nis_free_directory (dir);
126 res = NULL;
127 goto out;
130 /* The following is a non-obvious optimization. A
131 nis_freeresult call would call xdr_free as the
132 following code. But it also would unnecessarily
133 free the result structure. We avoid this here
134 along with the necessary tests. */
135 #if 1
136 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res);
137 memset (res, '\0', sizeof (*res));
138 #else
139 nis_freeresult (res);
140 res = calloc (1, sizeof (nis_result));
141 if (res == NULL)
143 __nisbind_destroy (&bptr);
144 return NULL;
146 #endif
148 link_first_try = 1; /* Try at first the old binding */
149 goto again;
152 else
153 if (NIS_RES_STATUS (res) == NIS_SYSTEMERROR
154 || NIS_RES_STATUS (res) == NIS_NOSUCHNAME
155 || NIS_RES_STATUS (res) == NIS_NOT_ME)
157 if (link_first_try)
159 __nisbind_destroy (&bptr);
160 nis_free_directory (dir);
161 /* Otherwise __nisfind_server will not do anything. */
162 dir = NULL;
164 if (__nisfind_server (req.ns_name, &dir)
165 != NIS_SUCCESS)
166 goto out;
168 if (__nisbind_create (&bptr,
169 dir->do_servers.do_servers_val,
170 dir->do_servers.do_servers_len,
171 flags) != NIS_SUCCESS)
173 nis_free_directory (dir);
174 goto out;
177 else
178 if (__nisbind_next (&bptr) != NIS_SUCCESS)
179 break; /* No more servers to search */
181 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
183 if (__nisbind_next (&bptr) != NIS_SUCCESS)
185 nis_free_directory (dir);
186 goto out;
189 goto again;
191 break;
193 link_first_try = 0; /* Set it back */
195 while ((flags & HARD_LOOKUP) && status == NIS_RPCERROR);
197 __nisbind_destroy (&bptr);
198 nis_free_directory (dir);
200 if (status != NIS_SUCCESS)
202 NIS_RES_STATUS (res) = status;
203 goto out;
206 switch (NIS_RES_STATUS (res))
208 case NIS_PARTIAL:
209 case NIS_SUCCESS:
210 case NIS_S_SUCCESS:
211 case NIS_LINKNAMEERROR: /* We follow to max links */
212 case NIS_UNAVAIL: /* NIS+ is not installed, or all servers are down */
213 ++done;
214 break;
215 default:
216 /* Try the next domainname if we don't follow a link */
217 if (count_links)
219 free (req.ns_name);
220 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
221 ++done;
222 break;
224 ++name_nr;
225 if (names[name_nr] == NULL)
227 ++done;
228 break;
230 req.ns_name = names[name_nr];
231 break;
235 out:
236 if (names != namebuf)
237 nis_freenames (names);
239 return res;
241 libnsl_hidden_def (nis_lookup)