]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nis_lookup.c
Update.
[thirdparty/glibc.git] / nis / nis_lookup.c
1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1997.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #include <string.h>
21 #include <rpcsvc/nis.h>
22 #include "nis_xdr.h"
23 #include "nis_intern.h"
24
25 nis_result *
26 nis_lookup (const_nis_name name, const unsigned int flags)
27 {
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};
37
38 if (res == NULL)
39 return NULL;
40
41 if ((flags & EXPAND_NAME) && (name[strlen (name) - 1] != '.'))
42 {
43 names = nis_getnames (name);
44 if (names == NULL)
45 {
46 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
47 return res;
48 }
49 }
50 else
51 {
52 names = namebuf;
53 names[0] = (nis_name)name;
54 }
55
56 req.ns_name = names[0];
57 while (!done)
58 {
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;
63
64 status = __nisfind_server (req.ns_name, &dir);
65 if (status != NIS_SUCCESS)
66 {
67 NIS_RES_STATUS (res) = status;
68 return res;
69 }
70
71 status = __nisbind_create (&bptr, dir->do_servers.do_servers_val,
72 dir->do_servers.do_servers_len, flags);
73 if (status != NIS_SUCCESS)
74 {
75 NIS_RES_STATUS (res) = status;
76 nis_free_directory (dir);
77 return res;
78 }
79
80 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
81 {
82 if (__nisbind_next (&bptr) != NIS_SUCCESS)
83 {
84 __nisbind_destroy (&bptr);
85 nis_free_directory (dir);
86 NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE;
87 return res;
88 }
89 }
90
91 do
92 {
93 static struct timeval RPCTIMEOUT = {10, 0};
94 enum clnt_stat result;
95
96 again:
97 result = clnt_call (bptr.clnt, NIS_LOOKUP,
98 (xdrproc_t) _xdr_ns_request,
99 (caddr_t) &req, (xdrproc_t) _xdr_nis_result,
100 (caddr_t) res, RPCTIMEOUT);
101
102 if (result != RPC_SUCCESS)
103 status = NIS_RPCERROR;
104 else
105 {
106 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
107 {
108 if (__type_of(NIS_RES_OBJECT (res)) == NIS_LINK_OBJ &&
109 flags & FOLLOW_LINKS) /* We are following links */
110 {
111 if (count_links)
112 free (req.ns_name);
113 /* if we hit the link limit, bail */
114 if (count_links > NIS_MAXLINKS)
115 {
116 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
117 break;
118 }
119 ++count_links;
120 req.ns_name =
121 strdup (NIS_RES_OBJECT (res)->LI_data.li_name);
122 nis_freeresult (res);
123 res = calloc (1, sizeof (nis_result));
124 if (res == NULL)
125 {
126 __nisbind_destroy (&bptr);
127 nis_free_directory (dir);
128 return NULL;
129 }
130 link_first_try = 1; /* Try at first the old binding */
131 goto again;
132 }
133 }
134 else
135 if ((NIS_RES_STATUS (res) == NIS_SYSTEMERROR) ||
136 (NIS_RES_STATUS (res) == NIS_NOSUCHNAME) ||
137 (NIS_RES_STATUS (res) == NIS_NOT_ME))
138 {
139 if (link_first_try)
140 {
141 __nisbind_destroy (&bptr);
142 nis_free_directory (dir);
143
144 if (__nisfind_server (req.ns_name, &dir) != NIS_SUCCESS)
145 return res;
146
147 if (__nisbind_create (&bptr,
148 dir->do_servers.do_servers_val,
149 dir->do_servers.do_servers_len,
150 flags) != NIS_SUCCESS)
151 {
152 nis_free_directory (dir);
153 return res;
154 }
155 }
156 else
157 if (__nisbind_next (&bptr) != NIS_SUCCESS)
158 break; /* No more servers to search */
159
160 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
161 {
162 if (__nisbind_next (&bptr) != NIS_SUCCESS)
163 {
164 __nisbind_destroy (&bptr);
165 nis_free_directory (dir);
166 return res;
167 }
168 }
169 goto again;
170 }
171 break;
172 }
173 link_first_try = 0; /* Set it back */
174 status= NIS_SUCCESS;
175 }
176 while ((flags & HARD_LOOKUP) && status == NIS_RPCERROR);
177
178 __nisbind_destroy (&bptr);
179 nis_free_directory (dir);
180
181 if (status != NIS_SUCCESS)
182 {
183 NIS_RES_STATUS (res) = status;
184 return res;
185 }
186
187 switch (NIS_RES_STATUS (res))
188 {
189 case NIS_PARTIAL:
190 case NIS_SUCCESS:
191 case NIS_S_SUCCESS:
192 case NIS_LINKNAMEERROR: /* We follow to max links */
193 case NIS_UNAVAIL: /* NIS+ is not installed, or all servers are down */
194 ++done;
195 break;
196 default:
197 /* Try the next domainname if we don't follow a link */
198 if (count_links)
199 {
200 free (req.ns_name);
201 NIS_RES_STATUS (res) = NIS_LINKNAMEERROR;
202 ++done;
203 break;
204 }
205 ++name_nr;
206 if (names[name_nr] == NULL)
207 {
208 ++done;
209 break;
210 }
211 req.ns_name = names[name_nr];
212 break;
213 }
214 }
215
216 if (names != namebuf)
217 nis_freenames (names);
218
219 return res;
220 }