.
[glibc/history.git] / nis / nis_call.c
blob6ae4ce5dbde3c8fc2180ba777ff77170d4956f7f
1 /* Copyright (C) 1997, 1998, 2001, 2004, 2005, 2006, 2007, 2008
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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
14 Lesser General Public License for more details.
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. */
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <libintl.h>
25 #include <rpc/rpc.h>
26 #include <rpc/auth.h>
27 #include <rpcsvc/nis.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <bits/libc-lock.h>
35 #include "nis_xdr.h"
36 #include "nis_intern.h"
37 #include <libnsl.h>
39 static const struct timeval RPCTIMEOUT = {10, 0};
40 static const struct timeval UDPTIMEOUT = {5, 0};
42 extern u_short __pmap_getnisport (struct sockaddr_in *address, u_long program,
43 u_long version, u_int protocol);
45 unsigned long int
46 inetstr2int (const char *str)
48 size_t j = 0;
49 for (size_t i = 0; str[i] != '\0'; ++i)
50 if (str[i] == '.' && __builtin_expect (++j == 4, 0))
52 char buffer[i + 1];
53 buffer[i] = '\0';
54 return inet_addr (memcpy (buffer, str, i));
57 return inet_addr (str);
60 void
61 __nisbind_destroy (dir_binding *bind)
63 if (bind->clnt != NULL)
65 if (bind->use_auth)
66 auth_destroy (bind->clnt->cl_auth);
67 clnt_destroy (bind->clnt);
70 libnsl_hidden_def (__nisbind_destroy)
72 nis_error
73 __nisbind_next (dir_binding *bind)
75 if (bind->clnt != NULL)
77 if (bind->use_auth)
78 auth_destroy (bind->clnt->cl_auth);
79 clnt_destroy (bind->clnt);
80 bind->clnt = NULL;
83 if (bind->trys >= bind->server_len)
84 return NIS_FAIL;
86 for (u_int j = bind->current_ep + 1;
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)
90 if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-')
92 bind->current_ep = j;
93 return NIS_SUCCESS;
96 ++bind->trys;
97 ++bind->server_used;
98 if (bind->server_used >= bind->server_len)
99 bind->server_used = 0;
101 for (u_int j = 0; j < bind->server_val[bind->server_used].ep.ep_len; ++j)
102 if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family,
103 "inet") == 0)
104 if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-')
106 bind->current_ep = j;
107 return NIS_SUCCESS;
110 return NIS_FAIL;
112 libnsl_hidden_def (__nisbind_next)
114 static struct ckey_cache_entry
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)
127 static bool_t
128 get_ckey (des_block *ckey, struct sockaddr_in *addr, unsigned int protocol)
130 size_t i;
131 pid_t pid = getpid ();
132 uid_t euid = geteuid ();
133 bool_t ret = FALSE;
135 __libc_lock_lock (ckey_cache_lock);
137 if (ckey_cache_pid != pid || ckey_cache_euid != euid)
139 ckey_cache_size = 0;
140 ckey_cache_pid = pid;
141 ckey_cache_euid = euid;
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)
150 *ckey = ckey_cache[i].ckey;
151 ret = TRUE;
152 break;
155 if (!ret && key_gendes (ckey) >= 0)
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)
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)
168 ckey_cache = new_cache;
169 ckey_cache_allocated = size;
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;
178 __libc_lock_unlock (ckey_cache_lock);
179 return ret;
182 nis_error
183 __nisbind_connect (dir_binding *dbp)
185 nis_server *serv;
186 u_short port;
188 if (dbp == NULL)
189 return NIS_FAIL;
191 serv = &dbp->server_val[dbp->server_used];
193 memset (&dbp->addr, '\0', sizeof (dbp->addr));
194 dbp->addr.sin_family = AF_INET;
196 dbp->addr.sin_addr.s_addr =
197 inetstr2int (serv->ep.ep_val[dbp->current_ep].uaddr);
199 if (dbp->addr.sin_addr.s_addr == INADDR_NONE)
200 return NIS_FAIL;
202 /* Check, if the host is online and rpc.nisd is running. Much faster
203 then the clnt*_create functions: */
204 port = __pmap_getnisport (&dbp->addr, NIS_PROG, NIS_VERSION,
205 dbp->use_udp ? IPPROTO_UDP : IPPROTO_TCP);
206 if (port == 0)
207 return NIS_RPCERROR;
209 dbp->addr.sin_port = htons (port);
210 dbp->socket = RPC_ANYSOCK;
211 if (dbp->use_udp)
212 dbp->clnt = clntudp_create (&dbp->addr, NIS_PROG, NIS_VERSION,
213 UDPTIMEOUT, &dbp->socket);
214 else
215 dbp->clnt = clnttcp_create (&dbp->addr, NIS_PROG, NIS_VERSION,
216 &dbp->socket, 0, 0);
218 if (dbp->clnt == NULL)
219 return NIS_RPCERROR;
221 clnt_control (dbp->clnt, CLSET_TIMEOUT, (caddr_t) &RPCTIMEOUT);
222 /* If the program exists, close the socket */
223 if (fcntl (dbp->socket, F_SETFD, 1) == -1)
224 perror ("fcntl: F_SETFD");
226 if (dbp->use_auth)
228 if (serv->key_type == NIS_PK_DH)
230 char netname[MAXNETNAMELEN + 1];
231 char *p;
232 des_block ckey;
234 p = stpcpy (netname, "unix@");
235 strncpy (p, serv->name, MAXNETNAMELEN - 5);
236 netname[MAXNETNAMELEN] = '\0';
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);
242 if (!dbp->clnt->cl_auth)
243 dbp->clnt->cl_auth = authunix_create_default ();
245 else
246 dbp->clnt->cl_auth = authunix_create_default ();
249 return NIS_SUCCESS;
251 libnsl_hidden_def (__nisbind_connect)
253 nis_error
254 __nisbind_create (dir_binding *dbp, const nis_server *serv_val,
255 unsigned int serv_len, unsigned int server_used,
256 unsigned int current_ep, unsigned int flags)
258 dbp->clnt = NULL;
260 dbp->server_len = serv_len;
261 dbp->server_val = (nis_server *)serv_val;
263 if (flags & USE_DGRAM)
264 dbp->use_udp = TRUE;
265 else
266 dbp->use_udp = FALSE;
268 if (flags & NO_AUTHINFO)
269 dbp->use_auth = FALSE;
270 else
271 dbp->use_auth = TRUE;
273 if (flags & MASTER_ONLY)
274 dbp->master_only = TRUE;
275 else
276 dbp->master_only = FALSE;
278 /* We try the first server */
279 dbp->trys = 1;
281 dbp->class = -1;
282 if (server_used == ~0)
284 if (__nis_findfastest (dbp) < 1)
285 return NIS_NAMEUNREACHABLE;
287 else
289 dbp->server_used = server_used;
290 dbp->current_ep = current_ep;
293 return NIS_SUCCESS;
295 libnsl_hidden_def (__nisbind_create)
297 /* __nisbind_connect (dbp) must be run before calling this function !
298 So we could use the same binding twice */
299 nis_error
300 __do_niscall3 (dir_binding *dbp, u_long prog, xdrproc_t xargs, caddr_t req,
301 xdrproc_t xres, caddr_t resp, unsigned int flags, nis_cb *cb)
303 enum clnt_stat result;
304 nis_error retcode;
306 if (dbp == NULL)
307 return NIS_NAMEUNREACHABLE;
311 again:
312 result = clnt_call (dbp->clnt, prog, xargs, req, xres, resp, RPCTIMEOUT);
314 if (result != RPC_SUCCESS)
315 retcode = NIS_RPCERROR;
316 else
318 switch (prog)
320 case NIS_IBLIST:
321 if ((((nis_result *)resp)->status == NIS_CBRESULTS) &&
322 (cb != NULL))
324 __nis_do_callback (dbp, &((nis_result *) resp)->cookie, cb);
325 break;
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 */
329 case NIS_LOOKUP:
330 case NIS_ADD:
331 case NIS_MODIFY:
332 case NIS_REMOVE:
333 case NIS_IBADD:
334 case NIS_IBMODIFY:
335 case NIS_IBREMOVE:
336 case NIS_IBFIRST:
337 case NIS_IBNEXT:
338 if (((nis_result *)resp)->status == NIS_SYSTEMERROR
339 || ((nis_result *)resp)->status == NIS_NOSUCHNAME
340 || ((nis_result *)resp)->status == NIS_NOT_ME)
342 next_server:
343 if (__nisbind_next (dbp) == NIS_SUCCESS)
345 while (__nisbind_connect (dbp) != NIS_SUCCESS)
347 if (__nisbind_next (dbp) != NIS_SUCCESS)
348 return NIS_SUCCESS;
351 else
352 break; /* No more servers to search in */
353 goto again;
355 break;
356 case NIS_FINDDIRECTORY:
357 if (((fd_result *)resp)->status == NIS_SYSTEMERROR
358 || ((fd_result *)resp)->status == NIS_NOSUCHNAME
359 || ((fd_result *)resp)->status == NIS_NOT_ME)
360 goto next_server;
361 break;
362 case NIS_DUMPLOG: /* log_result */
363 case NIS_DUMP:
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)
367 goto next_server;
368 break;
369 default:
370 break;
372 retcode = NIS_SUCCESS;
375 while ((flags & HARD_LOOKUP) && retcode == NIS_RPCERROR);
377 return retcode;
379 libnsl_hidden_def (__do_niscall3)
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,
385 unsigned int flags, nis_cb *cb)
387 dir_binding dbp;
388 nis_error status;
390 if (flags & MASTER_ONLY)
391 server_len = 1;
393 status = __nisbind_create (&dbp, server, server_len, ~0, ~0, flags);
394 if (status != NIS_SUCCESS)
395 return status;
397 while (__nisbind_connect (&dbp) != NIS_SUCCESS)
398 if (__nisbind_next (&dbp) != NIS_SUCCESS)
399 return NIS_NAMEUNREACHABLE;
401 status = __do_niscall3 (&dbp, prog, xargs, req, xres, resp, flags, cb);
403 __nisbind_destroy (&dbp);
405 return status;
409 static directory_obj *
410 rec_dirsearch (const_nis_name name, directory_obj *dir, nis_error *status)
412 fd_result *fd_res;
413 XDR xdrs;
415 switch (nis_dir_cmp (name, dir->do_name))
417 case SAME_NAME:
418 *status = NIS_SUCCESS;
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;
425 const char *ndomain = __nis_domain_of (dir->do_name);
427 /* The root server of our domain is a replica of the parent
428 domain ! (Now I understand why a root server must be a
429 replica of the parent domain) */
430 fd_res = __nis_finddirectory (dir, ndomain);
431 if (fd_res == NULL)
433 nis_free_directory (dir);
434 *status = NIS_NOMEMORY;
435 return NULL;
437 *status = fd_res->status;
438 if (fd_res->status != NIS_SUCCESS)
440 /* Try the current directory obj, maybe it works */
441 __free_fdresult (fd_res);
442 return dir;
444 nis_free_directory (dir);
445 obj = calloc (1, sizeof (directory_obj));
446 if (obj == NULL)
448 __free_fdresult (fd_res);
449 *status = NIS_NOMEMORY;
450 return NULL;
452 xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val,
453 fd_res->dir_data.dir_data_len, XDR_DECODE);
454 _xdr_directory_obj (&xdrs, obj);
455 xdr_destroy (&xdrs);
456 __free_fdresult (fd_res);
458 /* We have found a NIS+ server serving ndomain, now
459 let us search for "name" */
460 return rec_dirsearch (name, obj, status);
462 break;
463 case LOWER_NAME:
465 directory_obj *obj;
466 size_t namelen = strlen (name);
467 char leaf[namelen + 3];
468 char domain[namelen + 3];
469 const char *ndomain;
470 char *cp;
472 strcpy (domain, name);
476 if (domain[0] == '\0')
478 nis_free_directory (dir);
479 return NULL;
481 nis_leaf_of_r (domain, leaf, sizeof (leaf));
482 ndomain = __nis_domain_of (domain);
483 memmove (domain, ndomain, strlen (ndomain) + 1);
485 while (nis_dir_cmp (domain, dir->do_name) != SAME_NAME);
487 cp = rawmemchr (leaf, '\0');
488 *cp++ = '.';
489 strcpy (cp, domain);
491 fd_res = __nis_finddirectory (dir, leaf);
492 if (fd_res == NULL)
494 nis_free_directory (dir);
495 *status = NIS_NOMEMORY;
496 return NULL;
498 *status = fd_res->status;
499 if (fd_res->status != NIS_SUCCESS)
501 /* Try the current directory object, maybe it works */
502 __free_fdresult (fd_res);
503 return dir;
505 nis_free_directory (dir);
506 obj = calloc (1, sizeof(directory_obj));
507 if (obj == NULL)
509 __free_fdresult (fd_res);
510 *status = NIS_NOMEMORY;
511 return NULL;
513 xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val,
514 fd_res->dir_data.dir_data_len, XDR_DECODE);
515 _xdr_directory_obj (&xdrs, obj);
516 xdr_destroy (&xdrs);
517 __free_fdresult (fd_res);
518 /* We have found a NIS+ server serving ndomain, now
519 let us search for "name" */
520 return rec_dirsearch (name, obj, status);
522 break;
523 case BAD_NAME:
524 nis_free_directory (dir);
525 *status = NIS_BADNAME;
526 return NULL;
528 nis_free_directory (dir);
529 *status = NIS_FAIL;
530 return NULL;
533 /* We try to query the current server for the searched object,
534 maybe he know about it ? */
535 static directory_obj *
536 first_shoot (const_nis_name name, directory_obj *dir)
538 directory_obj *obj = NULL;
539 fd_result *fd_res;
540 XDR xdrs;
542 if (nis_dir_cmp (name, dir->do_name) == SAME_NAME)
543 return dir;
545 fd_res = __nis_finddirectory (dir, name);
546 if (fd_res == NULL)
547 return NULL;
548 if (fd_res->status == NIS_SUCCESS
549 && (obj = calloc (1, sizeof (directory_obj))) != NULL)
551 xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val,
552 fd_res->dir_data.dir_data_len, XDR_DECODE);
553 _xdr_directory_obj (&xdrs, obj);
554 xdr_destroy (&xdrs);
556 if (strcmp (dir->do_name, obj->do_name) != 0)
558 nis_free_directory (obj);
559 obj = NULL;
563 __free_fdresult (fd_res);
565 if (obj != NULL)
566 nis_free_directory (dir);
568 return obj;
571 static struct nis_server_cache
573 int search_parent;
574 int uses;
575 unsigned int size;
576 unsigned int server_used;
577 unsigned int current_ep;
578 time_t expires;
579 char name[];
580 } *nis_server_cache[16];
581 static time_t nis_cold_start_mtime;
582 __libc_lock_define_initialized (static, nis_server_cache_lock)
584 static directory_obj *
585 nis_server_cache_search (const_nis_name name, int search_parent,
586 unsigned int *server_used, unsigned int *current_ep,
587 struct timeval *now)
589 directory_obj *ret = NULL;
590 int i;
591 char *addr;
592 XDR xdrs;
593 struct stat64 st;
595 int saved_errno = errno;
596 if (stat64 ("/var/nis/NIS_COLD_START", &st) < 0)
597 st.st_mtime = nis_cold_start_mtime + 1;
598 __set_errno (saved_errno);
600 __libc_lock_lock (nis_server_cache_lock);
602 for (i = 0; i < 16; ++i)
603 if (nis_server_cache[i] == NULL)
604 continue;
605 else if (st.st_mtime != nis_cold_start_mtime
606 || now->tv_sec > nis_server_cache[i]->expires)
608 free (nis_server_cache[i]);
609 nis_server_cache[i] = NULL;
611 else if (nis_server_cache[i]->search_parent == search_parent
612 && strcmp (nis_server_cache[i]->name, name) == 0)
614 ret = calloc (1, sizeof (directory_obj));
615 if (ret == NULL)
616 break;
618 addr = rawmemchr (nis_server_cache[i]->name, '\0') + 8;
619 addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7);
620 xdrmem_create (&xdrs, addr, nis_server_cache[i]->size, XDR_DECODE);
621 if (!_xdr_directory_obj (&xdrs, ret))
623 xdr_destroy (&xdrs);
624 free (ret);
625 ret = NULL;
626 free (nis_server_cache[i]);
627 nis_server_cache[i] = NULL;
628 break;
630 xdr_destroy (&xdrs);
631 *server_used = nis_server_cache[i]->server_used;
632 *current_ep = nis_server_cache[i]->current_ep;
633 break;
636 nis_cold_start_mtime = st.st_mtime;
638 __libc_lock_unlock (nis_server_cache_lock);
639 return ret;
642 static void
643 nis_server_cache_add (const_nis_name name, int search_parent,
644 directory_obj *dir, unsigned int server_used,
645 unsigned int current_ep, struct timeval *now)
647 struct nis_server_cache **loc;
648 struct nis_server_cache *new;
649 struct nis_server_cache *old;
650 int i;
651 char *addr;
652 unsigned int size;
653 XDR xdrs;
655 if (dir == NULL)
656 return;
658 size = xdr_sizeof ((xdrproc_t) _xdr_directory_obj, (char *) dir);
659 new = calloc (1, sizeof (*new) + strlen (name) + 8 + size);
660 if (new == NULL)
661 return;
662 new->search_parent = search_parent;
663 new->uses = 1;
664 new->expires = now->tv_sec + dir->do_ttl;
665 new->size = size;
666 new->server_used = server_used;
667 new->current_ep = current_ep;
668 addr = stpcpy (new->name, name) + 8;
669 addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7);
671 xdrmem_create(&xdrs, addr, size, XDR_ENCODE);
672 if (!_xdr_directory_obj (&xdrs, dir))
674 xdr_destroy (&xdrs);
675 free (new);
676 return;
678 xdr_destroy (&xdrs);
680 __libc_lock_lock (nis_server_cache_lock);
682 /* Choose which entry should be evicted from the cache. */
683 loc = &nis_server_cache[0];
684 if (*loc != NULL)
685 for (i = 1; i < 16; ++i)
686 if (nis_server_cache[i] == NULL)
688 loc = &nis_server_cache[i];
689 break;
691 else if ((*loc)->uses > nis_server_cache[i]->uses
692 || ((*loc)->uses == nis_server_cache[i]->uses
693 && (*loc)->expires > nis_server_cache[i]->expires))
694 loc = &nis_server_cache[i];
695 old = *loc;
696 *loc = new;
698 __libc_lock_unlock (nis_server_cache_lock);
699 free (old);
702 nis_error
703 __nisfind_server (const_nis_name name, int search_parent,
704 directory_obj **dir, dir_binding *dbp, unsigned int flags)
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;
713 if (name == NULL)
714 return NIS_BADNAME;
716 if (*dir != NULL)
717 return NIS_SUCCESS;
719 (void) gettimeofday (&now, NULL);
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)
726 unsigned int server_len = (*dir)->do_servers.do_servers_len;
727 if (flags & MASTER_ONLY)
729 server_len = 1;
730 if (server_used != 0)
732 server_used = ~0;
733 current_ep = ~0;
736 result = __nisbind_create (dbp, (*dir)->do_servers.do_servers_val,
737 server_len, server_used, current_ep, flags);
738 if (result != NIS_SUCCESS)
740 nis_free_directory (*dir);
741 *dir = NULL;
743 return result;
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;
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)
760 obj = rec_dirsearch (search_name, *dir, &status);
761 if (obj == NULL)
762 result = status;
765 if (result == NIS_SUCCESS)
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)
774 if ((flags & MASTER_ONLY) == 0
775 || obj->do_servers.do_servers_len == 1)
777 server_used = dbp->server_used;
778 current_ep = dbp->current_ep;
780 if ((flags & NO_CACHE) == 0)
781 nis_server_cache_add (name, search_parent, obj,
782 server_used, current_ep, &now);
784 else
786 nis_free_directory (obj);
787 obj = NULL;
791 *dir = obj;
793 return result;
797 nis_error
798 __prepare_niscall (const_nis_name name, directory_obj **dirp,
799 dir_binding *bptrp, unsigned int flags)
801 nis_error retcode = __nisfind_server (name, 1, dirp, bptrp, flags);
802 if (__builtin_expect (retcode != NIS_SUCCESS, 0))
803 return retcode;
806 if (__nisbind_connect (bptrp) == NIS_SUCCESS)
807 return NIS_SUCCESS;
808 while (__nisbind_next (bptrp) == NIS_SUCCESS);
810 __nisbind_destroy (bptrp);
811 memset (bptrp, '\0', sizeof (*bptrp));
813 retcode = NIS_NAMEUNREACHABLE;
814 nis_free_directory (*dirp);
815 *dirp = NULL;
817 return retcode;
819 libnsl_hidden_def (__prepare_niscall)
822 nis_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)
827 dir_binding bptr;
828 directory_obj *dir = NULL;
829 int saved_errno = errno;
831 nis_error retcode = __prepare_niscall (name, &dir, &bptr, flags);
832 if (retcode == NIS_SUCCESS)
834 retcode = __do_niscall3 (&bptr, prog, xargs, req, xres, resp, flags, cb);
836 __nisbind_destroy (&bptr);
838 nis_free_directory (dir);
841 __set_errno (saved_errno);
843 return retcode;