]>
Commit | Line | Data |
---|---|---|
697d37b1 JJ |
1 | /* Copyright (C) 1997, 1998, 2001, 2004, 2005, 2006, 2007 |
2 | Free Software Foundation, Inc. | |
e61abf83 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
e61abf83 UD |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
e61abf83 | 15 | |
41bdb6e2 AJ |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. */ | |
e61abf83 | 20 | |
36a8586d | 21 | #include <errno.h> |
2d7da676 | 22 | #include <fcntl.h> |
e61abf83 | 23 | #include <string.h> |
4360eafd | 24 | #include <libintl.h> |
e61abf83 UD |
25 | #include <rpc/rpc.h> |
26 | #include <rpc/auth.h> | |
27 | #include <rpcsvc/nis.h> | |
28 | #include <sys/socket.h> | |
697d37b1 JJ |
29 | #include <sys/stat.h> |
30 | #include <unistd.h> | |
e61abf83 UD |
31 | #include <netinet/in.h> |
32 | #include <arpa/inet.h> | |
697d37b1 | 33 | #include <bits/libc-lock.h> |
91eee4dd UD |
34 | |
35 | #include "nis_xdr.h" | |
e61abf83 | 36 | #include "nis_intern.h" |
2d571cd1 | 37 | #include <libnsl.h> |
e61abf83 | 38 | |
cd40e3a7 UD |
39 | static const struct timeval RPCTIMEOUT = {10, 0}; |
40 | static const struct timeval UDPTIMEOUT = {5, 0}; | |
2d7da676 | 41 | |
26a60f90 UD |
42 | extern u_short __pmap_getnisport (struct sockaddr_in *address, u_long program, |
43 | u_long version, u_int protocol); | |
44 | ||
bd1ebae0 | 45 | unsigned long int |
e61abf83 UD |
46 | inetstr2int (const char *str) |
47 | { | |
929b1c07 UD |
48 | size_t j = 0; |
49 | for (size_t i = 0; str[i] != '\0'; ++i) | |
bd1ebae0 | 50 | if (str[i] == '.' && __builtin_expect (++j == 4, 0)) |
e61abf83 | 51 | { |
929b1c07 UD |
52 | char buffer[i + 1]; |
53 | buffer[i] = '\0'; | |
54 | return inet_addr (memcpy (buffer, str, i)); | |
e61abf83 UD |
55 | } |
56 | ||
929b1c07 | 57 | return inet_addr (str); |
e61abf83 UD |
58 | } |
59 | ||
e852e889 UD |
60 | void |
61 | __nisbind_destroy (dir_binding *bind) | |
e61abf83 | 62 | { |
2d7da676 | 63 | if (bind->clnt != NULL) |
e61abf83 | 64 | { |
2d7da676 UD |
65 | if (bind->use_auth) |
66 | auth_destroy (bind->clnt->cl_auth); | |
67 | clnt_destroy (bind->clnt); | |
68 | } | |
2d7da676 | 69 | } |
7440c23e | 70 | libnsl_hidden_def (__nisbind_destroy) |
2d7da676 | 71 | |
e852e889 UD |
72 | nis_error |
73 | __nisbind_next (dir_binding *bind) | |
2d7da676 | 74 | { |
2d7da676 UD |
75 | if (bind->clnt != NULL) |
76 | { | |
77 | if (bind->use_auth) | |
78 | auth_destroy (bind->clnt->cl_auth); | |
79 | clnt_destroy (bind->clnt); | |
80 | bind->clnt = NULL; | |
81 | } | |
3996f34b UD |
82 | |
83 | if (bind->trys >= bind->server_len) | |
84 | return NIS_FAIL; | |
85 | ||
bd1ebae0 | 86 | for (u_int j = bind->current_ep + 1; |
3996f34b UD |
87 | j < bind->server_val[bind->server_used].ep.ep_len; ++j) |
88 | if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family, | |
89 | "inet") == 0) | |
e852e889 | 90 | if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-') |
3996f34b UD |
91 | { |
92 | bind->current_ep = j; | |
93 | return NIS_SUCCESS; | |
94 | } | |
95 | ||
96 | ++bind->trys; | |
97 | ++bind->server_used; | |
98 | if (bind->server_used >= bind->server_len) | |
99 | bind->server_used = 0; | |
100 | ||
bd1ebae0 | 101 | for (u_int j = 0; j < bind->server_val[bind->server_used].ep.ep_len; ++j) |
3996f34b UD |
102 | if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family, |
103 | "inet") == 0) | |
d6db1d53 | 104 | if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-') |
3996f34b UD |
105 | { |
106 | bind->current_ep = j; | |
107 | return NIS_SUCCESS; | |
108 | } | |
109 | ||
110 | return NIS_FAIL; | |
2d7da676 | 111 | } |
7440c23e | 112 | libnsl_hidden_def (__nisbind_next) |
2d7da676 | 113 | |
697d37b1 JJ |
114 | static struct ckey_cache_entry |
115 | { | |
116 | struct in_addr inaddr; | |
117 | in_port_t port; | |
118 | unsigned int protocol; | |
119 | des_block ckey; | |
120 | } *ckey_cache; | |
121 | static size_t ckey_cache_size; | |
122 | static size_t ckey_cache_allocated; | |
123 | static pid_t ckey_cache_pid; | |
124 | static uid_t ckey_cache_euid; | |
125 | __libc_lock_define_initialized (static, ckey_cache_lock) | |
126 | ||
127 | static bool_t | |
128 | get_ckey (des_block *ckey, struct sockaddr_in *addr, unsigned int protocol) | |
129 | { | |
130 | size_t i; | |
131 | pid_t pid = getpid (); | |
132 | uid_t euid = geteuid (); | |
133 | bool_t ret = FALSE; | |
134 | ||
135 | __libc_lock_lock (ckey_cache_lock); | |
136 | ||
137 | if (ckey_cache_pid != pid || ckey_cache_euid != euid) | |
138 | { | |
139 | ckey_cache_size = 0; | |
140 | ckey_cache_pid = pid; | |
141 | ckey_cache_euid = euid; | |
142 | } | |
143 | ||
144 | for (i = 0; i < ckey_cache_size; ++i) | |
145 | if (ckey_cache[i].port == addr->sin_port | |
146 | && ckey_cache[i].protocol == protocol | |
147 | && memcmp (&ckey_cache[i].inaddr, &addr->sin_addr, | |
148 | sizeof (addr->sin_addr)) == 0) | |
149 | { | |
150 | *ckey = ckey_cache[i].ckey; | |
151 | ret = TRUE; | |
152 | break; | |
153 | } | |
154 | ||
155 | if (!ret && key_gendes (ckey) >= 0) | |
156 | { | |
157 | ret = TRUE; | |
158 | /* Don't grow the cache indefinitely. */ | |
159 | if (ckey_cache_size == 256) | |
160 | ckey_cache_size = 0; | |
161 | if (ckey_cache_size == ckey_cache_allocated) | |
162 | { | |
163 | size_t size = ckey_cache_allocated ? ckey_cache_allocated * 2 : 16; | |
164 | struct ckey_cache_entry *new_cache | |
165 | = realloc (ckey_cache, size * sizeof (*ckey_cache)); | |
166 | if (new_cache != NULL) | |
167 | { | |
168 | ckey_cache = new_cache; | |
169 | ckey_cache_allocated = size; | |
170 | } | |
171 | } | |
172 | ckey_cache[ckey_cache_size].inaddr = addr->sin_addr; | |
173 | ckey_cache[ckey_cache_size].port = addr->sin_port; | |
174 | ckey_cache[ckey_cache_size].protocol = protocol; | |
175 | ckey_cache[ckey_cache_size++].ckey = *ckey; | |
176 | } | |
177 | ||
178 | __libc_lock_unlock (ckey_cache_lock); | |
179 | return ret; | |
180 | } | |
181 | ||
e852e889 UD |
182 | nis_error |
183 | __nisbind_connect (dir_binding *dbp) | |
2d7da676 | 184 | { |
2d7da676 | 185 | nis_server *serv; |
697d37b1 | 186 | u_short port; |
2d7da676 UD |
187 | |
188 | if (dbp == NULL) | |
189 | return NIS_FAIL; | |
190 | ||
191 | serv = &dbp->server_val[dbp->server_used]; | |
192 | ||
193 | memset (&dbp->addr, '\0', sizeof (dbp->addr)); | |
194 | dbp->addr.sin_family = AF_INET; | |
3996f34b UD |
195 | |
196 | dbp->addr.sin_addr.s_addr = | |
197 | inetstr2int (serv->ep.ep_val[dbp->current_ep].uaddr); | |
198 | ||
bd1ebae0 | 199 | if (dbp->addr.sin_addr.s_addr == INADDR_NONE) |
2d7da676 UD |
200 | return NIS_FAIL; |
201 | ||
26a60f90 UD |
202 | /* Check, if the host is online and rpc.nisd is running. Much faster |
203 | then the clnt*_create functions: */ | |
697d37b1 JJ |
204 | port = __pmap_getnisport (&dbp->addr, NIS_PROG, NIS_VERSION, |
205 | dbp->use_udp ? IPPROTO_UDP : IPPROTO_TCP); | |
206 | if (port == 0) | |
26a60f90 UD |
207 | return NIS_RPCERROR; |
208 | ||
697d37b1 | 209 | dbp->addr.sin_port = htons (port); |
2d7da676 UD |
210 | dbp->socket = RPC_ANYSOCK; |
211 | if (dbp->use_udp) | |
212 | dbp->clnt = clntudp_create (&dbp->addr, NIS_PROG, NIS_VERSION, | |
cc3fa755 | 213 | UDPTIMEOUT, &dbp->socket); |
2d7da676 UD |
214 | else |
215 | dbp->clnt = clnttcp_create (&dbp->addr, NIS_PROG, NIS_VERSION, | |
216 | &dbp->socket, 0, 0); | |
3996f34b | 217 | |
2d7da676 UD |
218 | if (dbp->clnt == NULL) |
219 | return NIS_RPCERROR; | |
3996f34b | 220 | |
cd40e3a7 | 221 | clnt_control (dbp->clnt, CLSET_TIMEOUT, (caddr_t) &RPCTIMEOUT); |
2d7da676 UD |
222 | /* If the program exists, close the socket */ |
223 | if (fcntl (dbp->socket, F_SETFD, 1) == -1) | |
a18f73be | 224 | perror ("fcntl: F_SETFD"); |
3996f34b | 225 | |
2d7da676 UD |
226 | if (dbp->use_auth) |
227 | { | |
e852e889 | 228 | if (serv->key_type == NIS_PK_DH) |
e61abf83 | 229 | { |
cd40e3a7 | 230 | char netname[MAXNETNAMELEN + 1]; |
2d7da676 | 231 | char *p; |
697d37b1 | 232 | des_block ckey; |
3996f34b | 233 | |
697d37b1 | 234 | p = stpcpy (netname, "unix@"); |
cd40e3a7 | 235 | strncpy (p, serv->name, MAXNETNAMELEN - 5); |
2d7da676 | 236 | netname[MAXNETNAMELEN] = '\0'; |
697d37b1 JJ |
237 | dbp->clnt->cl_auth = NULL; |
238 | if (get_ckey (&ckey, &dbp->addr, | |
239 | dbp->use_udp ? IPPROTO_UDP : IPPROTO_TCP)) | |
240 | dbp->clnt->cl_auth = | |
241 | authdes_pk_create (netname, &serv->pkey, 300, NULL, &ckey); | |
2d7da676 UD |
242 | if (!dbp->clnt->cl_auth) |
243 | dbp->clnt->cl_auth = authunix_create_default (); | |
e61abf83 | 244 | } |
2d7da676 | 245 | else |
2d7da676 | 246 | dbp->clnt->cl_auth = authunix_create_default (); |
2d7da676 | 247 | } |
3996f34b | 248 | |
2d7da676 UD |
249 | return NIS_SUCCESS; |
250 | } | |
7440c23e | 251 | libnsl_hidden_def (__nisbind_connect) |
26dee9c4 | 252 | |
e852e889 | 253 | nis_error |
a1129917 | 254 | __nisbind_create (dir_binding *dbp, const nis_server *serv_val, |
697d37b1 JJ |
255 | unsigned int serv_len, unsigned int server_used, |
256 | unsigned int current_ep, unsigned int flags) | |
2d7da676 | 257 | { |
d6db1d53 | 258 | dbp->clnt = NULL; |
3996f34b | 259 | |
2d7da676 | 260 | dbp->server_len = serv_len; |
d6db1d53 | 261 | dbp->server_val = (nis_server *)serv_val; |
3996f34b UD |
262 | |
263 | if (flags & USE_DGRAM) | |
264 | dbp->use_udp = TRUE; | |
265 | else | |
266 | dbp->use_udp = FALSE; | |
267 | ||
268 | if (flags & NO_AUTHINFO) | |
269 | dbp->use_auth = FALSE; | |
270 | else | |
271 | dbp->use_auth = TRUE; | |
272 | ||
273 | if (flags & MASTER_ONLY) | |
274 | dbp->master_only = TRUE; | |
275 | else | |
276 | dbp->master_only = FALSE; | |
277 | ||
26a60f90 | 278 | /* We try the first server */ |
3996f34b UD |
279 | dbp->trys = 1; |
280 | ||
ac9f45cf | 281 | dbp->class = -1; |
697d37b1 JJ |
282 | if (server_used == ~0) |
283 | { | |
284 | if (__nis_findfastest (dbp) < 1) | |
285 | return NIS_NAMEUNREACHABLE; | |
286 | } | |
287 | else | |
288 | { | |
289 | dbp->server_used = server_used; | |
290 | dbp->current_ep = current_ep; | |
291 | } | |
2d7da676 | 292 | |
d6db1d53 | 293 | return NIS_SUCCESS; |
e61abf83 | 294 | } |
7440c23e | 295 | libnsl_hidden_def (__nisbind_create) |
e61abf83 | 296 | |
e852e889 UD |
297 | /* __nisbind_connect (dbp) must be run before calling this function ! |
298 | So we could use the same binding twice */ | |
e61abf83 | 299 | nis_error |
e852e889 | 300 | __do_niscall3 (dir_binding *dbp, u_long prog, xdrproc_t xargs, caddr_t req, |
a1129917 | 301 | xdrproc_t xres, caddr_t resp, unsigned int flags, nis_cb *cb) |
e61abf83 | 302 | { |
2d7da676 UD |
303 | enum clnt_stat result; |
304 | nis_error retcode; | |
2d7da676 | 305 | |
e852e889 | 306 | if (dbp == NULL) |
a5a0310d | 307 | return NIS_NAMEUNREACHABLE; |
e61abf83 | 308 | |
2d7da676 | 309 | do |
43b0e40f | 310 | { |
2d7da676 | 311 | again: |
e852e889 | 312 | result = clnt_call (dbp->clnt, prog, xargs, req, xres, resp, RPCTIMEOUT); |
3996f34b | 313 | |
43b0e40f | 314 | if (result != RPC_SUCCESS) |
e852e889 | 315 | retcode = NIS_RPCERROR; |
43b0e40f | 316 | else |
2d7da676 UD |
317 | { |
318 | switch (prog) | |
319 | { | |
650425ce UD |
320 | case NIS_IBLIST: |
321 | if ((((nis_result *)resp)->status == NIS_CBRESULTS) && | |
322 | (cb != NULL)) | |
323 | { | |
cd40e3a7 | 324 | __nis_do_callback (dbp, &((nis_result *) resp)->cookie, cb); |
650425ce UD |
325 | break; |
326 | } | |
e852e889 UD |
327 | /* Yes, the missing break is correct. If we doesn't have to |
328 | start a callback, look if we have to search another server */ | |
2d7da676 UD |
329 | case NIS_LOOKUP: |
330 | case NIS_ADD: | |
331 | case NIS_MODIFY: | |
332 | case NIS_REMOVE: | |
2d7da676 UD |
333 | case NIS_IBADD: |
334 | case NIS_IBMODIFY: | |
335 | case NIS_IBREMOVE: | |
336 | case NIS_IBFIRST: | |
337 | case NIS_IBNEXT: | |
cd40e3a7 UD |
338 | if (((nis_result *)resp)->status == NIS_SYSTEMERROR |
339 | || ((nis_result *)resp)->status == NIS_NOSUCHNAME | |
340 | || ((nis_result *)resp)->status == NIS_NOT_ME) | |
3996f34b | 341 | { |
1d4f062a | 342 | next_server: |
e852e889 | 343 | if (__nisbind_next (dbp) == NIS_SUCCESS) |
650425ce | 344 | { |
e852e889 | 345 | while (__nisbind_connect (dbp) != NIS_SUCCESS) |
650425ce | 346 | { |
e852e889 | 347 | if (__nisbind_next (dbp) != NIS_SUCCESS) |
650425ce | 348 | return NIS_SUCCESS; |
650425ce UD |
349 | } |
350 | } | |
351 | else | |
352 | break; /* No more servers to search in */ | |
2d7da676 | 353 | goto again; |
3996f34b | 354 | } |
a5a0310d | 355 | break; |
2d7da676 | 356 | case NIS_FINDDIRECTORY: |
cd40e3a7 UD |
357 | if (((fd_result *)resp)->status == NIS_SYSTEMERROR |
358 | || ((fd_result *)resp)->status == NIS_NOSUCHNAME | |
359 | || ((fd_result *)resp)->status == NIS_NOT_ME) | |
1d4f062a | 360 | goto next_server; |
2d7da676 UD |
361 | break; |
362 | case NIS_DUMPLOG: /* log_result */ | |
363 | case NIS_DUMP: | |
cd40e3a7 UD |
364 | if (((log_result *)resp)->lr_status == NIS_SYSTEMERROR |
365 | || ((log_result *)resp)->lr_status == NIS_NOSUCHNAME | |
366 | || ((log_result *)resp)->lr_status == NIS_NOT_ME) | |
1d4f062a | 367 | goto next_server; |
2d7da676 | 368 | break; |
2d7da676 UD |
369 | default: |
370 | break; | |
371 | } | |
2d7da676 UD |
372 | retcode = NIS_SUCCESS; |
373 | } | |
43b0e40f | 374 | } |
2d7da676 | 375 | while ((flags & HARD_LOOKUP) && retcode == NIS_RPCERROR); |
3996f34b UD |
376 | |
377 | return retcode; | |
43b0e40f | 378 | } |
2d571cd1 UD |
379 | libnsl_hidden_def (__do_niscall3) |
380 | ||
43b0e40f | 381 | |
e852e889 UD |
382 | nis_error |
383 | __do_niscall2 (const nis_server *server, u_int server_len, u_long prog, | |
384 | xdrproc_t xargs, caddr_t req, xdrproc_t xres, caddr_t resp, | |
a1129917 | 385 | unsigned int flags, nis_cb *cb) |
e852e889 UD |
386 | { |
387 | dir_binding dbp; | |
388 | nis_error status; | |
389 | ||
390 | if (flags & MASTER_ONLY) | |
391 | server_len = 1; | |
392 | ||
697d37b1 | 393 | status = __nisbind_create (&dbp, server, server_len, ~0, ~0, flags); |
e852e889 UD |
394 | if (status != NIS_SUCCESS) |
395 | return status; | |
396 | ||
397 | while (__nisbind_connect (&dbp) != NIS_SUCCESS) | |
cd40e3a7 UD |
398 | if (__nisbind_next (&dbp) != NIS_SUCCESS) |
399 | return NIS_NAMEUNREACHABLE; | |
e852e889 UD |
400 | |
401 | status = __do_niscall3 (&dbp, prog, xargs, req, xres, resp, flags, cb); | |
402 | ||
403 | __nisbind_destroy (&dbp); | |
404 | ||
405 | return status; | |
406 | ||
407 | } | |
408 | ||
43b0e40f | 409 | static directory_obj * |
e852e889 | 410 | rec_dirsearch (const_nis_name name, directory_obj *dir, nis_error *status) |
43b0e40f | 411 | { |
2d7da676 UD |
412 | fd_result *fd_res; |
413 | XDR xdrs; | |
43b0e40f | 414 | |
26a60f90 | 415 | switch (nis_dir_cmp (name, dir->do_name)) |
43b0e40f UD |
416 | { |
417 | case SAME_NAME: | |
3996f34b | 418 | *status = NIS_SUCCESS; |
43b0e40f UD |
419 | return dir; |
420 | case NOT_SEQUENTIAL: | |
421 | /* NOT_SEQUENTIAL means, go one up and try it there ! */ | |
422 | case HIGHER_NAME: | |
423 | { /* We need data from a parent domain */ | |
424 | directory_obj *obj; | |
11bf311e UD |
425 | char ndomain[strlen (dir->do_name) + 3]; |
426 | ||
427 | nis_domain_of_r (dir->do_name, ndomain, sizeof (ndomain)); | |
43b0e40f UD |
428 | |
429 | /* The root server of our domain is a replica of the parent | |
430 | domain ! (Now I understand why a root server must be a | |
431 | replica of the parent domain) */ | |
2d7da676 | 432 | fd_res = __nis_finddirectory (dir, ndomain); |
5f1724bf UD |
433 | if (fd_res == NULL) |
434 | { | |
758b9d7e | 435 | nis_free_directory (dir); |
5f1724bf UD |
436 | *status = NIS_NOMEMORY; |
437 | return NULL; | |
438 | } | |
3996f34b | 439 | *status = fd_res->status; |
2d7da676 UD |
440 | if (fd_res->status != NIS_SUCCESS) |
441 | { | |
26a60f90 | 442 | /* Try the current directory obj, maybe it works */ |
af6f3906 | 443 | __free_fdresult (fd_res); |
26a60f90 | 444 | return dir; |
2d7da676 | 445 | } |
758b9d7e | 446 | nis_free_directory (dir); |
cd40e3a7 | 447 | obj = calloc (1, sizeof (directory_obj)); |
758b9d7e UD |
448 | if (obj == NULL) |
449 | { | |
450 | __free_fdresult (fd_res); | |
451 | *status = NIS_NOMEMORY; | |
452 | return NULL; | |
453 | } | |
cd40e3a7 UD |
454 | xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val, |
455 | fd_res->dir_data.dir_data_len, XDR_DECODE); | |
456 | _xdr_directory_obj (&xdrs, obj); | |
457 | xdr_destroy (&xdrs); | |
dfd2257a | 458 | __free_fdresult (fd_res); |
758b9d7e UD |
459 | |
460 | /* We have found a NIS+ server serving ndomain, now | |
461 | let us search for "name" */ | |
462 | return rec_dirsearch (name, obj, status); | |
43b0e40f | 463 | } |
758b9d7e | 464 | break; |
43b0e40f UD |
465 | case LOWER_NAME: |
466 | { | |
467 | directory_obj *obj; | |
a53bad16 | 468 | size_t namelen = strlen (name); |
cd40e3a7 UD |
469 | char leaf[namelen + 3]; |
470 | char domain[namelen + 3]; | |
11bf311e | 471 | char ndomain[namelen + 3]; |
714a562f | 472 | char *cp; |
26a60f90 UD |
473 | |
474 | strcpy (domain, name); | |
3996f34b | 475 | |
43b0e40f UD |
476 | do |
477 | { | |
a53bad16 | 478 | if (domain[0] == '\0') |
43b0e40f UD |
479 | { |
480 | nis_free_directory (dir); | |
481 | return NULL; | |
482 | } | |
483 | nis_leaf_of_r (domain, leaf, sizeof (leaf)); | |
11bf311e UD |
484 | nis_domain_of_r (domain, ndomain, sizeof (ndomain)); |
485 | strcpy (domain, ndomain); | |
43b0e40f UD |
486 | } |
487 | while (nis_dir_cmp (domain, dir->do_name) != SAME_NAME); | |
062e719b | 488 | |
616d9133 | 489 | cp = rawmemchr (leaf, '\0'); |
714a562f UD |
490 | *cp++ = '.'; |
491 | strcpy (cp, domain); | |
3996f34b | 492 | |
2d7da676 | 493 | fd_res = __nis_finddirectory (dir, leaf); |
5f1724bf UD |
494 | if (fd_res == NULL) |
495 | { | |
758b9d7e | 496 | nis_free_directory (dir); |
5f1724bf UD |
497 | *status = NIS_NOMEMORY; |
498 | return NULL; | |
499 | } | |
3996f34b | 500 | *status = fd_res->status; |
2d7da676 | 501 | if (fd_res->status != NIS_SUCCESS) |
43b0e40f | 502 | { |
26a60f90 | 503 | /* Try the current directory object, maybe it works */ |
af6f3906 | 504 | __free_fdresult (fd_res); |
26a60f90 | 505 | return dir; |
2d7da676 | 506 | } |
758b9d7e UD |
507 | nis_free_directory (dir); |
508 | obj = calloc (1, sizeof(directory_obj)); | |
509 | if (obj == NULL) | |
2d7da676 | 510 | { |
758b9d7e UD |
511 | __free_fdresult (fd_res); |
512 | *status = NIS_NOMEMORY; | |
513 | return NULL; | |
43b0e40f | 514 | } |
758b9d7e UD |
515 | xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val, |
516 | fd_res->dir_data.dir_data_len, XDR_DECODE); | |
517 | _xdr_directory_obj (&xdrs, obj); | |
518 | xdr_destroy (&xdrs); | |
519 | __free_fdresult (fd_res); | |
520 | /* We have found a NIS+ server serving ndomain, now | |
521 | let us search for "name" */ | |
522 | return rec_dirsearch (name, obj, status); | |
43b0e40f | 523 | } |
758b9d7e | 524 | break; |
43b0e40f UD |
525 | case BAD_NAME: |
526 | nis_free_directory (dir); | |
3996f34b | 527 | *status = NIS_BADNAME; |
43b0e40f UD |
528 | return NULL; |
529 | } | |
530 | nis_free_directory (dir); | |
3996f34b | 531 | *status = NIS_FAIL; |
43b0e40f UD |
532 | return NULL; |
533 | } | |
534 | ||
26a60f90 UD |
535 | /* We try to query the current server for the searched object, |
536 | maybe he know about it ? */ | |
537 | static directory_obj * | |
11bf311e | 538 | first_shoot (const_nis_name name, int search_parent_first, directory_obj *dir) |
26a60f90 | 539 | { |
7b53e1fb | 540 | directory_obj *obj = NULL; |
26a60f90 UD |
541 | fd_result *fd_res; |
542 | XDR xdrs; | |
11bf311e | 543 | char domain[strlen (name) + 3]; |
26a60f90 | 544 | |
11bf311e | 545 | #if 0 |
26a60f90 UD |
546 | if (nis_dir_cmp (name, dir->do_name) == SAME_NAME) |
547 | return dir; | |
11bf311e | 548 | #endif |
26a60f90 | 549 | |
11bf311e UD |
550 | const char *search_name = name; |
551 | if (search_parent_first) | |
552 | { | |
553 | nis_domain_of_r (name, domain, sizeof (domain)); | |
554 | search_name = domain; | |
555 | } | |
556 | ||
557 | if (nis_dir_cmp (search_name, dir->do_name) == SAME_NAME) | |
558 | return dir; | |
559 | ||
560 | fd_res = __nis_finddirectory (dir, search_name); | |
5f1724bf UD |
561 | if (fd_res == NULL) |
562 | return NULL; | |
cd40e3a7 UD |
563 | if (fd_res->status == NIS_SUCCESS |
564 | && (obj = calloc (1, sizeof (directory_obj))) != NULL) | |
26a60f90 | 565 | { |
bd1ebae0 UD |
566 | xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val, |
567 | fd_res->dir_data.dir_data_len, XDR_DECODE); | |
cd40e3a7 UD |
568 | _xdr_directory_obj (&xdrs, obj); |
569 | xdr_destroy (&xdrs); | |
bd1ebae0 UD |
570 | |
571 | if (strcmp (dir->do_name, obj->do_name) != 0) | |
572 | { | |
573 | nis_free_directory (obj); | |
574 | obj = NULL; | |
575 | } | |
26a60f90 | 576 | } |
cd40e3a7 | 577 | |
26a60f90 | 578 | __free_fdresult (fd_res); |
cd40e3a7 | 579 | |
26a60f90 | 580 | if (obj != NULL) |
cd40e3a7 UD |
581 | nis_free_directory (dir); |
582 | ||
583 | return obj; | |
26a60f90 UD |
584 | } |
585 | ||
697d37b1 JJ |
586 | static struct nis_server_cache |
587 | { | |
588 | int search_parent_first; | |
589 | int uses; | |
590 | unsigned int size; | |
591 | unsigned int server_used; | |
592 | unsigned int current_ep; | |
593 | time_t expires; | |
594 | char name[]; | |
595 | } *nis_server_cache[16]; | |
596 | static time_t nis_cold_start_mtime; | |
597 | __libc_lock_define_initialized (static, nis_server_cache_lock) | |
598 | ||
599 | static directory_obj * | |
600 | nis_server_cache_search (const_nis_name name, int search_parent_first, | |
601 | unsigned int *server_used, unsigned int *current_ep, | |
602 | struct timeval *now) | |
603 | { | |
604 | directory_obj *ret = NULL; | |
605 | int i; | |
606 | char *addr; | |
607 | XDR xdrs; | |
608 | struct stat64 st; | |
609 | ||
610 | if (stat64 ("/var/nis/NIS_COLD_START", &st) < 0) | |
611 | st.st_mtime = nis_cold_start_mtime + 1; | |
612 | ||
613 | __libc_lock_lock (nis_server_cache_lock); | |
614 | ||
615 | for (i = 0; i < 16; ++i) | |
616 | if (nis_server_cache[i] == NULL) | |
617 | continue; | |
618 | else if (st.st_mtime != nis_cold_start_mtime | |
619 | || now->tv_sec > nis_server_cache[i]->expires) | |
620 | { | |
621 | free (nis_server_cache[i]); | |
622 | nis_server_cache[i] = NULL; | |
623 | } | |
624 | else if (nis_server_cache[i]->search_parent_first == search_parent_first | |
625 | && strcmp (nis_server_cache[i]->name, name) == 0) | |
626 | { | |
627 | ret = calloc (1, sizeof (directory_obj)); | |
628 | if (ret == NULL) | |
629 | break; | |
630 | ||
631 | addr = rawmemchr (nis_server_cache[i]->name, '\0') + 8; | |
632 | addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7); | |
633 | xdrmem_create (&xdrs, addr, nis_server_cache[i]->size, XDR_DECODE); | |
634 | if (!_xdr_directory_obj (&xdrs, ret)) | |
635 | { | |
636 | xdr_destroy (&xdrs); | |
637 | free (ret); | |
638 | ret = NULL; | |
639 | free (nis_server_cache[i]); | |
640 | nis_server_cache[i] = NULL; | |
641 | break; | |
642 | } | |
643 | xdr_destroy (&xdrs); | |
644 | *server_used = nis_server_cache[i]->server_used; | |
645 | *current_ep = nis_server_cache[i]->current_ep; | |
646 | break; | |
647 | } | |
648 | ||
649 | nis_cold_start_mtime = st.st_mtime; | |
650 | ||
651 | __libc_lock_unlock (nis_server_cache_lock); | |
652 | return ret; | |
653 | } | |
654 | ||
655 | static void | |
656 | nis_server_cache_add (const_nis_name name, int search_parent_first, | |
657 | directory_obj *dir, unsigned int server_used, | |
658 | unsigned int current_ep, struct timeval *now) | |
659 | { | |
660 | struct nis_server_cache **loc; | |
661 | struct nis_server_cache *new; | |
662 | struct nis_server_cache *old; | |
663 | int i; | |
664 | char *addr; | |
665 | unsigned int size; | |
666 | XDR xdrs; | |
667 | ||
668 | if (dir == NULL) | |
669 | return; | |
670 | ||
671 | size = xdr_sizeof ((xdrproc_t) _xdr_directory_obj, (char *) dir); | |
672 | new = calloc (1, sizeof (*new) + strlen (name) + 8 + size); | |
673 | if (new == NULL) | |
674 | return; | |
675 | new->search_parent_first = search_parent_first; | |
676 | new->uses = 1; | |
677 | new->expires = now->tv_sec + dir->do_ttl; | |
678 | new->size = size; | |
679 | new->server_used = server_used; | |
680 | new->current_ep = current_ep; | |
681 | addr = stpcpy (new->name, name) + 8; | |
682 | addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7); | |
683 | ||
684 | xdrmem_create(&xdrs, addr, size, XDR_ENCODE); | |
685 | if (!_xdr_directory_obj (&xdrs, dir)) | |
686 | { | |
687 | xdr_destroy (&xdrs); | |
688 | free (new); | |
689 | return; | |
690 | } | |
691 | xdr_destroy (&xdrs); | |
692 | ||
693 | __libc_lock_lock (nis_server_cache_lock); | |
694 | ||
695 | /* Choose which entry should be evicted from the cache. */ | |
696 | loc = &nis_server_cache[0]; | |
697 | if (*loc != NULL) | |
698 | for (i = 1; i < 16; ++i) | |
699 | if (nis_server_cache[i] == NULL) | |
700 | { | |
701 | loc = &nis_server_cache[i]; | |
702 | break; | |
703 | } | |
704 | else if ((*loc)->uses > nis_server_cache[i]->uses | |
705 | || ((*loc)->uses == nis_server_cache[i]->uses | |
706 | && (*loc)->expires > nis_server_cache[i]->expires)) | |
707 | loc = &nis_server_cache[i]; | |
708 | old = *loc; | |
709 | *loc = new; | |
710 | ||
711 | __libc_lock_unlock (nis_server_cache_lock); | |
712 | free (old); | |
713 | } | |
714 | ||
43b0e40f | 715 | nis_error |
11bf311e | 716 | __nisfind_server (const_nis_name name, int search_parent_first, |
697d37b1 | 717 | directory_obj **dir, dir_binding *dbp, unsigned int flags) |
43b0e40f | 718 | { |
697d37b1 JJ |
719 | nis_error result = NIS_SUCCESS; |
720 | nis_error status; | |
721 | directory_obj *obj; | |
722 | struct timeval now; | |
723 | unsigned int server_used = ~0; | |
724 | unsigned int current_ep = ~0; | |
725 | ||
2d7da676 UD |
726 | if (name == NULL) |
727 | return NIS_BADNAME; | |
43b0e40f | 728 | |
697d37b1 JJ |
729 | if (*dir != NULL) |
730 | return NIS_SUCCESS; | |
3996f34b | 731 | |
697d37b1 JJ |
732 | (void) gettimeofday (&now, NULL); |
733 | ||
734 | if ((flags & NO_CACHE) == 0) | |
735 | *dir = nis_server_cache_search (name, search_parent_first, &server_used, | |
736 | ¤t_ep, &now); | |
737 | if (*dir != NULL) | |
0ecb606c | 738 | { |
697d37b1 JJ |
739 | unsigned int server_len = (*dir)->do_servers.do_servers_len; |
740 | if (flags & MASTER_ONLY) | |
741 | { | |
742 | server_len = 1; | |
743 | if (server_used != 0) | |
744 | { | |
745 | server_used = ~0; | |
746 | current_ep = ~0; | |
747 | } | |
748 | } | |
749 | result = __nisbind_create (dbp, (*dir)->do_servers.do_servers_val, | |
750 | server_len, server_used, current_ep, flags); | |
751 | if (result != NIS_SUCCESS) | |
752 | { | |
753 | nis_free_directory (*dir); | |
754 | *dir = NULL; | |
755 | } | |
756 | return result; | |
757 | } | |
0ecb606c | 758 | |
697d37b1 JJ |
759 | *dir = readColdStartFile (); |
760 | if (*dir == NULL) | |
761 | /* No /var/nis/NIS_COLD_START->no NIS+ installed. */ | |
762 | return NIS_UNAVAIL; | |
a334319f | 763 | |
697d37b1 JJ |
764 | /* Try at first, if servers in "dir" know our object */ |
765 | obj = first_shoot (name, search_parent_first, *dir); | |
766 | if (obj == NULL) | |
767 | { | |
768 | obj = rec_dirsearch (name, *dir, &status); | |
a334319f | 769 | if (obj == NULL) |
697d37b1 JJ |
770 | result = status; |
771 | } | |
772 | ||
773 | if (result == NIS_SUCCESS) | |
774 | { | |
775 | unsigned int server_len = (*dir)->do_servers.do_servers_len; | |
776 | if (flags & MASTER_ONLY) | |
777 | server_len = 1; | |
778 | result = __nisbind_create (dbp, (*dir)->do_servers.do_servers_val, | |
779 | server_len, ~0, ~0, flags); | |
780 | if (result == NIS_SUCCESS) | |
a788b6c2 | 781 | { |
697d37b1 JJ |
782 | if ((flags & MASTER_ONLY) == 0 |
783 | || (*dir)->do_servers.do_servers_len == 1) | |
784 | { | |
785 | server_used = dbp->server_used; | |
786 | current_ep = dbp->current_ep; | |
787 | } | |
788 | if ((flags & NO_CACHE) == 0) | |
789 | nis_server_cache_add (name, search_parent_first, obj, | |
790 | server_used, current_ep, &now); | |
791 | } | |
792 | else | |
793 | { | |
794 | nis_free_directory (obj); | |
795 | obj = NULL; | |
32c075e1 | 796 | } |
11bf311e | 797 | } |
32c075e1 | 798 | |
697d37b1 JJ |
799 | *dir = obj; |
800 | ||
5f1724bf | 801 | return result; |
0ecb606c | 802 | } |
0ecb606c | 803 | |
a17fa610 | 804 | |
e852e889 | 805 | nis_error |
a17fa610 UD |
806 | __prepare_niscall (const_nis_name name, directory_obj **dirp, |
807 | dir_binding *bptrp, unsigned int flags) | |
e852e889 | 808 | { |
697d37b1 | 809 | nis_error retcode = __nisfind_server (name, 1, dirp, bptrp, flags); |
a17fa610 UD |
810 | if (__builtin_expect (retcode != NIS_SUCCESS, 0)) |
811 | return retcode; | |
812 | ||
697d37b1 JJ |
813 | do |
814 | if (__nisbind_connect (bptrp) == NIS_SUCCESS) | |
815 | return NIS_SUCCESS; | |
816 | while (__nisbind_next (bptrp) == NIS_SUCCESS); | |
11bf311e | 817 | |
697d37b1 JJ |
818 | __nisbind_destroy (bptrp); |
819 | memset (bptrp, '\0', sizeof (*bptrp)); | |
a17fa610 | 820 | |
697d37b1 | 821 | retcode = NIS_NAMEUNREACHABLE; |
a17fa610 UD |
822 | nis_free_directory (*dirp); |
823 | *dirp = NULL; | |
824 | ||
825 | return retcode; | |
826 | } | |
2d571cd1 | 827 | libnsl_hidden_def (__prepare_niscall) |
a17fa610 UD |
828 | |
829 | ||
830 | nis_error | |
831 | __do_niscall (const_nis_name name, u_long prog, xdrproc_t xargs, | |
832 | caddr_t req, xdrproc_t xres, caddr_t resp, unsigned int flags, | |
833 | nis_cb *cb) | |
834 | { | |
835 | dir_binding bptr; | |
836 | directory_obj *dir = NULL; | |
837 | int saved_errno = errno; | |
838 | ||
839 | nis_error retcode = __prepare_niscall (name, &dir, &bptr, flags); | |
e852e889 UD |
840 | if (retcode == NIS_SUCCESS) |
841 | { | |
e852e889 | 842 | retcode = __do_niscall3 (&bptr, prog, xargs, req, xres, resp, flags, cb); |
3996f34b | 843 | |
e852e889 | 844 | __nisbind_destroy (&bptr); |
43b0e40f | 845 | |
a17fa610 UD |
846 | nis_free_directory (dir); |
847 | } | |
a334319f | 848 | |
a788b6c2 UD |
849 | __set_errno (saved_errno); |
850 | ||
2d7da676 | 851 | return retcode; |
e61abf83 | 852 | } |