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