Back to index

glibc  2.9
nis_call.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 2001, 2004, 2005, 2006, 2007, 2008
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <fcntl.h>
00023 #include <string.h>
00024 #include <libintl.h>
00025 #include <rpc/rpc.h>
00026 #include <rpc/auth.h>
00027 #include <rpcsvc/nis.h>
00028 #include <sys/socket.h>
00029 #include <sys/stat.h>
00030 #include <unistd.h>
00031 #include <netinet/in.h>
00032 #include <arpa/inet.h>
00033 #include <bits/libc-lock.h>
00034 
00035 #include "nis_xdr.h"
00036 #include "nis_intern.h"
00037 #include <libnsl.h>
00038 
00039 static const struct timeval RPCTIMEOUT = {10, 0};
00040 static const struct timeval UDPTIMEOUT = {5, 0};
00041 
00042 extern u_short __pmap_getnisport (struct sockaddr_in *address, u_long program,
00043                               u_long version, u_int protocol);
00044 
00045 unsigned long int
00046 inetstr2int (const char *str)
00047 {
00048   size_t j = 0;
00049   for (size_t i = 0; str[i] != '\0'; ++i)
00050     if (str[i] == '.' && __builtin_expect (++j == 4, 0))
00051       {
00052        char buffer[i + 1];
00053        buffer[i] = '\0';
00054        return inet_addr (memcpy (buffer, str, i));
00055       }
00056 
00057   return inet_addr (str);
00058 }
00059 
00060 void
00061 __nisbind_destroy (dir_binding *bind)
00062 {
00063   if (bind->clnt != NULL)
00064     {
00065       if (bind->use_auth)
00066        auth_destroy (bind->clnt->cl_auth);
00067       clnt_destroy (bind->clnt);
00068     }
00069 }
00070 libnsl_hidden_def (__nisbind_destroy)
00071 
00072 nis_error
00073 __nisbind_next (dir_binding *bind)
00074 {
00075   if (bind->clnt != NULL)
00076     {
00077       if (bind->use_auth)
00078        auth_destroy (bind->clnt->cl_auth);
00079       clnt_destroy (bind->clnt);
00080       bind->clnt = NULL;
00081     }
00082 
00083   if (bind->trys >= bind->server_len)
00084     return NIS_FAIL;
00085 
00086   for (u_int j = bind->current_ep + 1;
00087        j < bind->server_val[bind->server_used].ep.ep_len; ++j)
00088     if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family,
00089               "inet") == 0)
00090       if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-')
00091        {
00092          bind->current_ep = j;
00093          return NIS_SUCCESS;
00094        }
00095 
00096   ++bind->trys;
00097   ++bind->server_used;
00098   if (bind->server_used >= bind->server_len)
00099     bind->server_used = 0;
00100 
00101   for (u_int j = 0; j < bind->server_val[bind->server_used].ep.ep_len; ++j)
00102     if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family,
00103               "inet") == 0)
00104       if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-')
00105        {
00106          bind->current_ep = j;
00107          return NIS_SUCCESS;
00108        }
00109 
00110   return NIS_FAIL;
00111 }
00112 libnsl_hidden_def (__nisbind_next)
00113 
00114 static struct ckey_cache_entry
00115 {
00116   struct in_addr inaddr;
00117   in_port_t port;
00118   unsigned int protocol;
00119   des_block ckey;
00120 } *ckey_cache;
00121 static size_t ckey_cache_size;
00122 static size_t ckey_cache_allocated;
00123 static pid_t ckey_cache_pid;
00124 static uid_t ckey_cache_euid;
00125 __libc_lock_define_initialized (static, ckey_cache_lock)
00126 
00127 static bool_t
00128 get_ckey (des_block *ckey, struct sockaddr_in *addr, unsigned int protocol)
00129 {
00130   size_t i;
00131   pid_t pid = getpid ();
00132   uid_t euid = geteuid ();
00133   bool_t ret = FALSE;
00134 
00135   __libc_lock_lock (ckey_cache_lock);
00136 
00137   if (ckey_cache_pid != pid || ckey_cache_euid != euid)
00138     {
00139       ckey_cache_size = 0;
00140       ckey_cache_pid = pid;
00141       ckey_cache_euid = euid;
00142     }
00143 
00144   for (i = 0; i < ckey_cache_size; ++i)
00145     if (ckey_cache[i].port == addr->sin_port
00146        && ckey_cache[i].protocol == protocol
00147        && memcmp (&ckey_cache[i].inaddr, &addr->sin_addr,
00148                  sizeof (addr->sin_addr)) == 0)
00149       {
00150        *ckey = ckey_cache[i].ckey;
00151        ret = TRUE;
00152        break;
00153       }
00154 
00155   if (!ret && key_gendes (ckey) >= 0)
00156     {
00157       ret = TRUE;
00158       /* Don't grow the cache indefinitely.  */
00159       if (ckey_cache_size == 256)
00160        ckey_cache_size = 0;
00161       if (ckey_cache_size == ckey_cache_allocated)
00162        {
00163          size_t size = ckey_cache_allocated ? ckey_cache_allocated * 2 : 16;
00164          struct ckey_cache_entry *new_cache
00165            = realloc (ckey_cache, size * sizeof (*ckey_cache));
00166          if (new_cache != NULL)
00167            {
00168              ckey_cache = new_cache;
00169              ckey_cache_allocated = size;
00170            }
00171        }
00172       ckey_cache[ckey_cache_size].inaddr = addr->sin_addr;
00173       ckey_cache[ckey_cache_size].port = addr->sin_port;
00174       ckey_cache[ckey_cache_size].protocol = protocol;
00175       ckey_cache[ckey_cache_size++].ckey = *ckey;
00176     }
00177 
00178   __libc_lock_unlock (ckey_cache_lock);
00179   return ret;
00180 }
00181 
00182 nis_error
00183 __nisbind_connect (dir_binding *dbp)
00184 {
00185   nis_server *serv;
00186   u_short port;
00187 
00188   if (dbp == NULL)
00189     return NIS_FAIL;
00190 
00191   serv = &dbp->server_val[dbp->server_used];
00192 
00193   memset (&dbp->addr, '\0', sizeof (dbp->addr));
00194   dbp->addr.sin_family = AF_INET;
00195 
00196   dbp->addr.sin_addr.s_addr =
00197     inetstr2int (serv->ep.ep_val[dbp->current_ep].uaddr);
00198 
00199   if (dbp->addr.sin_addr.s_addr == INADDR_NONE)
00200     return NIS_FAIL;
00201 
00202   /* Check, if the host is online and rpc.nisd is running. Much faster
00203      then the clnt*_create functions: */
00204   port = __pmap_getnisport (&dbp->addr, NIS_PROG, NIS_VERSION,
00205                          dbp->use_udp ? IPPROTO_UDP : IPPROTO_TCP);
00206   if (port == 0)
00207     return NIS_RPCERROR;
00208 
00209   dbp->addr.sin_port = htons (port);
00210   dbp->socket = RPC_ANYSOCK;
00211   if (dbp->use_udp)
00212     dbp->clnt = clntudp_create (&dbp->addr, NIS_PROG, NIS_VERSION,
00213                              UDPTIMEOUT, &dbp->socket);
00214   else
00215     dbp->clnt = clnttcp_create (&dbp->addr, NIS_PROG, NIS_VERSION,
00216                              &dbp->socket, 0, 0);
00217 
00218   if (dbp->clnt == NULL)
00219     return NIS_RPCERROR;
00220 
00221   clnt_control (dbp->clnt, CLSET_TIMEOUT, (caddr_t) &RPCTIMEOUT);
00222   /* If the program exists, close the socket */
00223   if (fcntl (dbp->socket, F_SETFD, 1) == -1)
00224     perror ("fcntl: F_SETFD");
00225 
00226   if (dbp->use_auth)
00227     {
00228       if (serv->key_type == NIS_PK_DH)
00229        {
00230          char netname[MAXNETNAMELEN + 1];
00231          char *p;
00232          des_block ckey;
00233 
00234          p = stpcpy (netname, "unix@");
00235          strncpy (p, serv->name, MAXNETNAMELEN - 5);
00236          netname[MAXNETNAMELEN] = '\0';
00237          dbp->clnt->cl_auth = NULL;
00238          if (get_ckey (&ckey, &dbp->addr,
00239                      dbp->use_udp ? IPPROTO_UDP : IPPROTO_TCP))
00240            dbp->clnt->cl_auth =
00241              authdes_pk_create (netname, &serv->pkey, 300, NULL, &ckey);
00242          if (!dbp->clnt->cl_auth)
00243            dbp->clnt->cl_auth = authunix_create_default ();
00244        }
00245       else
00246        dbp->clnt->cl_auth = authunix_create_default ();
00247     }
00248 
00249   return NIS_SUCCESS;
00250 }
00251 libnsl_hidden_def (__nisbind_connect)
00252 
00253 nis_error
00254 __nisbind_create (dir_binding *dbp, const nis_server *serv_val,
00255                 unsigned int serv_len, unsigned int server_used,
00256                 unsigned int current_ep, unsigned int flags)
00257 {
00258   dbp->clnt = NULL;
00259 
00260   dbp->server_len = serv_len;
00261   dbp->server_val = (nis_server *)serv_val;
00262 
00263   if (flags & USE_DGRAM)
00264     dbp->use_udp = TRUE;
00265   else
00266     dbp->use_udp = FALSE;
00267 
00268   if (flags & NO_AUTHINFO)
00269     dbp->use_auth = FALSE;
00270   else
00271     dbp->use_auth = TRUE;
00272 
00273   if (flags & MASTER_ONLY)
00274     dbp->master_only = TRUE;
00275   else
00276     dbp->master_only = FALSE;
00277 
00278   /* We try the first server */
00279   dbp->trys = 1;
00280 
00281   dbp->class = -1;
00282   if (server_used == ~0)
00283     {
00284       if (__nis_findfastest (dbp) < 1)
00285        return NIS_NAMEUNREACHABLE;
00286     }
00287   else
00288     {
00289       dbp->server_used = server_used;
00290       dbp->current_ep = current_ep;
00291     }
00292 
00293   return NIS_SUCCESS;
00294 }
00295 libnsl_hidden_def (__nisbind_create)
00296 
00297 /* __nisbind_connect (dbp) must be run before calling this function !
00298    So we could use the same binding twice */
00299 nis_error
00300 __do_niscall3 (dir_binding *dbp, u_long prog, xdrproc_t xargs, caddr_t req,
00301               xdrproc_t xres, caddr_t resp, unsigned int flags, nis_cb *cb)
00302 {
00303   enum clnt_stat result;
00304   nis_error retcode;
00305 
00306   if (dbp == NULL)
00307     return NIS_NAMEUNREACHABLE;
00308 
00309   do
00310     {
00311     again:
00312       result = clnt_call (dbp->clnt, prog, xargs, req, xres, resp, RPCTIMEOUT);
00313 
00314       if (result != RPC_SUCCESS)
00315        retcode = NIS_RPCERROR;
00316       else
00317        {
00318          switch (prog)
00319            {
00320            case NIS_IBLIST:
00321              if ((((nis_result *)resp)->status == NIS_CBRESULTS) &&
00322                 (cb != NULL))
00323               {
00324                 __nis_do_callback (dbp, &((nis_result *) resp)->cookie, cb);
00325                 break;
00326               }
00327              /* Yes, the missing break is correct. If we doesn't have to
00328                start a callback, look if we have to search another server */
00329            case NIS_LOOKUP:
00330            case NIS_ADD:
00331            case NIS_MODIFY:
00332            case NIS_REMOVE:
00333            case NIS_IBADD:
00334            case NIS_IBMODIFY:
00335            case NIS_IBREMOVE:
00336            case NIS_IBFIRST:
00337            case NIS_IBNEXT:
00338              if (((nis_result *)resp)->status == NIS_SYSTEMERROR
00339                 || ((nis_result *)resp)->status == NIS_NOSUCHNAME
00340                 || ((nis_result *)resp)->status == NIS_NOT_ME)
00341               {
00342               next_server:
00343                 if (__nisbind_next (dbp) == NIS_SUCCESS)
00344                   {
00345                     while (__nisbind_connect (dbp) != NIS_SUCCESS)
00346                      {
00347                        if (__nisbind_next (dbp) != NIS_SUCCESS)
00348                            return NIS_SUCCESS;
00349                      }
00350                   }
00351                 else
00352                   break; /* No more servers to search in */
00353                 goto again;
00354               }
00355              break;
00356            case NIS_FINDDIRECTORY:
00357              if (((fd_result *)resp)->status == NIS_SYSTEMERROR
00358                 || ((fd_result *)resp)->status == NIS_NOSUCHNAME
00359                 || ((fd_result *)resp)->status == NIS_NOT_ME)
00360               goto next_server;
00361              break;
00362            case NIS_DUMPLOG: /* log_result */
00363            case NIS_DUMP:
00364              if (((log_result *)resp)->lr_status == NIS_SYSTEMERROR
00365                 || ((log_result *)resp)->lr_status == NIS_NOSUCHNAME
00366                 || ((log_result *)resp)->lr_status == NIS_NOT_ME)
00367               goto next_server;
00368              break;
00369            default:
00370              break;
00371            }
00372          retcode = NIS_SUCCESS;
00373        }
00374     }
00375   while ((flags & HARD_LOOKUP) && retcode == NIS_RPCERROR);
00376 
00377   return retcode;
00378 }
00379 libnsl_hidden_def (__do_niscall3)
00380 
00381 
00382 nis_error
00383 __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
00384               xdrproc_t xargs, caddr_t req, xdrproc_t xres, caddr_t resp,
00385               unsigned int flags, nis_cb *cb)
00386 {
00387   dir_binding dbp;
00388   nis_error status;
00389 
00390   if (flags & MASTER_ONLY)
00391     server_len = 1;
00392 
00393   status = __nisbind_create (&dbp, server, server_len, ~0, ~0, flags);
00394   if (status != NIS_SUCCESS)
00395     return status;
00396 
00397   while (__nisbind_connect (&dbp) != NIS_SUCCESS)
00398     if (__nisbind_next (&dbp) != NIS_SUCCESS)
00399       return NIS_NAMEUNREACHABLE;
00400 
00401   status = __do_niscall3 (&dbp, prog, xargs, req, xres, resp, flags, cb);
00402 
00403   __nisbind_destroy (&dbp);
00404 
00405   return status;
00406 
00407 }
00408 
00409 static directory_obj *
00410 rec_dirsearch (const_nis_name name, directory_obj *dir, nis_error *status)
00411 {
00412   fd_result *fd_res;
00413   XDR xdrs;
00414 
00415   switch (nis_dir_cmp (name, dir->do_name))
00416     {
00417     case SAME_NAME:
00418       *status = NIS_SUCCESS;
00419       return dir;
00420     case NOT_SEQUENTIAL:
00421       /* NOT_SEQUENTIAL means, go one up and try it there ! */
00422     case HIGHER_NAME:
00423       { /* We need data from a parent domain */
00424        directory_obj *obj;
00425        const char *ndomain = __nis_domain_of (dir->do_name);
00426 
00427        /* The root server of our domain is a replica of the parent
00428           domain ! (Now I understand why a root server must be a
00429           replica of the parent domain) */
00430        fd_res = __nis_finddirectory (dir, ndomain);
00431        if (fd_res == NULL)
00432          {
00433            nis_free_directory (dir);
00434            *status = NIS_NOMEMORY;
00435            return NULL;
00436          }
00437        *status = fd_res->status;
00438        if (fd_res->status != NIS_SUCCESS)
00439          {
00440            /* Try the current directory obj, maybe it works */
00441            __free_fdresult (fd_res);
00442            return dir;
00443          }
00444        nis_free_directory (dir);
00445        obj = calloc (1, sizeof (directory_obj));
00446        if (obj == NULL)
00447          {
00448            __free_fdresult (fd_res);
00449            *status = NIS_NOMEMORY;
00450            return NULL;
00451          }
00452        xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val,
00453                      fd_res->dir_data.dir_data_len, XDR_DECODE);
00454        _xdr_directory_obj (&xdrs, obj);
00455        xdr_destroy (&xdrs);
00456        __free_fdresult (fd_res);
00457 
00458        /* We have found a NIS+ server serving ndomain, now
00459           let us search for "name" */
00460        return rec_dirsearch (name, obj, status);
00461       }
00462       break;
00463     case LOWER_NAME:
00464       {
00465        directory_obj *obj;
00466        size_t namelen = strlen (name);
00467        char leaf[namelen + 3];
00468        char domain[namelen + 3];
00469        const char *ndomain;
00470        char *cp;
00471 
00472        strcpy (domain, name);
00473 
00474        do
00475          {
00476            if (domain[0] == '\0')
00477              {
00478               nis_free_directory (dir);
00479               return NULL;
00480              }
00481            nis_leaf_of_r (domain, leaf, sizeof (leaf));
00482            ndomain = __nis_domain_of (domain);
00483            memmove (domain, ndomain, strlen (ndomain) + 1);
00484          }
00485        while (nis_dir_cmp (domain, dir->do_name) != SAME_NAME);
00486 
00487        cp = rawmemchr (leaf, '\0');
00488        *cp++ = '.';
00489        strcpy (cp, domain);
00490 
00491        fd_res = __nis_finddirectory (dir, leaf);
00492        if (fd_res == NULL)
00493          {
00494            nis_free_directory (dir);
00495            *status = NIS_NOMEMORY;
00496            return NULL;
00497          }
00498        *status = fd_res->status;
00499        if (fd_res->status != NIS_SUCCESS)
00500          {
00501            /* Try the current directory object, maybe it works */
00502            __free_fdresult (fd_res);
00503            return dir;
00504          }
00505        nis_free_directory (dir);
00506        obj = calloc (1, sizeof(directory_obj));
00507        if (obj == NULL)
00508          {
00509            __free_fdresult (fd_res);
00510            *status = NIS_NOMEMORY;
00511            return NULL;
00512          }
00513        xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val,
00514                      fd_res->dir_data.dir_data_len, XDR_DECODE);
00515        _xdr_directory_obj (&xdrs, obj);
00516        xdr_destroy (&xdrs);
00517        __free_fdresult (fd_res);
00518        /* We have found a NIS+ server serving ndomain, now
00519           let us search for "name" */
00520        return rec_dirsearch (name, obj, status);
00521       }
00522       break;
00523     case BAD_NAME:
00524       nis_free_directory (dir);
00525       *status = NIS_BADNAME;
00526       return NULL;
00527     }
00528   nis_free_directory (dir);
00529   *status = NIS_FAIL;
00530   return NULL;
00531 }
00532 
00533 /* We try to query the current server for the searched object,
00534    maybe he know about it ? */
00535 static directory_obj *
00536 first_shoot (const_nis_name name, directory_obj *dir)
00537 {
00538   directory_obj *obj = NULL;
00539   fd_result *fd_res;
00540   XDR xdrs;
00541 
00542   if (nis_dir_cmp (name, dir->do_name) == SAME_NAME)
00543     return dir;
00544 
00545   fd_res = __nis_finddirectory (dir, name);
00546   if (fd_res == NULL)
00547     return NULL;
00548   if (fd_res->status == NIS_SUCCESS
00549       && (obj = calloc (1, sizeof (directory_obj))) != NULL)
00550     {
00551       xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val,
00552                    fd_res->dir_data.dir_data_len, XDR_DECODE);
00553       _xdr_directory_obj (&xdrs, obj);
00554       xdr_destroy (&xdrs);
00555 
00556       if (strcmp (dir->do_name, obj->do_name) != 0)
00557        {
00558          nis_free_directory (obj);
00559          obj = NULL;
00560        }
00561     }
00562 
00563   __free_fdresult (fd_res);
00564 
00565   if (obj != NULL)
00566     nis_free_directory (dir);
00567 
00568   return obj;
00569 }
00570 
00571 static struct nis_server_cache
00572 {
00573   int search_parent;
00574   int uses;
00575   unsigned int size;
00576   unsigned int server_used;
00577   unsigned int current_ep;
00578   time_t expires;
00579   char name[];
00580 } *nis_server_cache[16];
00581 static time_t nis_cold_start_mtime;
00582 __libc_lock_define_initialized (static, nis_server_cache_lock)
00583 
00584 static directory_obj *
00585 nis_server_cache_search (const_nis_name name, int search_parent,
00586                       unsigned int *server_used, unsigned int *current_ep,
00587                       struct timeval *now)
00588 {
00589   directory_obj *ret = NULL;
00590   int i;
00591   char *addr;
00592   XDR xdrs;
00593   struct stat64 st;
00594 
00595   int saved_errno = errno;
00596   if (stat64 ("/var/nis/NIS_COLD_START", &st) < 0)
00597     st.st_mtime = nis_cold_start_mtime + 1;
00598   __set_errno (saved_errno);
00599 
00600   __libc_lock_lock (nis_server_cache_lock);
00601 
00602   for (i = 0; i < 16; ++i)
00603     if (nis_server_cache[i] == NULL)
00604       continue;
00605     else if (st.st_mtime != nis_cold_start_mtime
00606             || now->tv_sec > nis_server_cache[i]->expires)
00607       {
00608        free (nis_server_cache[i]);
00609        nis_server_cache[i] = NULL;
00610       }
00611     else if (nis_server_cache[i]->search_parent == search_parent
00612             && strcmp (nis_server_cache[i]->name, name) == 0)
00613       {
00614        ret = calloc (1, sizeof (directory_obj));
00615        if (ret == NULL)
00616          break;
00617 
00618        addr = rawmemchr (nis_server_cache[i]->name, '\0') + 8;
00619        addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7);
00620        xdrmem_create (&xdrs, addr, nis_server_cache[i]->size, XDR_DECODE);
00621        if (!_xdr_directory_obj (&xdrs, ret))
00622          {
00623            xdr_destroy (&xdrs);
00624            free (ret);
00625            ret = NULL;
00626            free (nis_server_cache[i]);
00627            nis_server_cache[i] = NULL;
00628            break;
00629          }
00630        xdr_destroy (&xdrs);
00631        *server_used = nis_server_cache[i]->server_used;
00632        *current_ep = nis_server_cache[i]->current_ep;
00633        break;
00634       }
00635 
00636   nis_cold_start_mtime = st.st_mtime;
00637 
00638   __libc_lock_unlock (nis_server_cache_lock);
00639   return ret;
00640 }
00641 
00642 static void
00643 nis_server_cache_add (const_nis_name name, int search_parent,
00644                     directory_obj *dir, unsigned int server_used,
00645                     unsigned int current_ep, struct timeval *now)
00646 {
00647   struct nis_server_cache **loc;
00648   struct nis_server_cache *new;
00649   struct nis_server_cache *old;
00650   int i;
00651   char *addr;
00652   unsigned int size;
00653   XDR xdrs;
00654 
00655   if (dir == NULL)
00656     return;
00657 
00658   size = xdr_sizeof ((xdrproc_t) _xdr_directory_obj, (char *) dir);
00659   new = calloc (1, sizeof (*new) + strlen (name) + 8 + size);
00660   if (new == NULL)
00661     return;
00662   new->search_parent = search_parent;
00663   new->uses = 1;
00664   new->expires = now->tv_sec + dir->do_ttl;
00665   new->size = size;
00666   new->server_used = server_used;
00667   new->current_ep = current_ep;
00668   addr = stpcpy (new->name, name) + 8;
00669   addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7);
00670 
00671   xdrmem_create(&xdrs, addr, size, XDR_ENCODE);
00672   if (!_xdr_directory_obj (&xdrs, dir))
00673     {
00674       xdr_destroy (&xdrs);
00675       free (new);
00676       return;
00677     }
00678   xdr_destroy (&xdrs);
00679 
00680   __libc_lock_lock (nis_server_cache_lock);
00681 
00682   /* Choose which entry should be evicted from the cache.  */
00683   loc = &nis_server_cache[0];
00684   if (*loc != NULL)
00685     for (i = 1; i < 16; ++i)
00686       if (nis_server_cache[i] == NULL)
00687        {
00688          loc = &nis_server_cache[i];
00689          break;
00690        }
00691       else if ((*loc)->uses > nis_server_cache[i]->uses
00692               || ((*loc)->uses == nis_server_cache[i]->uses
00693                  && (*loc)->expires > nis_server_cache[i]->expires))
00694        loc = &nis_server_cache[i];
00695   old = *loc;
00696   *loc = new;
00697 
00698   __libc_lock_unlock (nis_server_cache_lock);
00699   free (old);
00700 }
00701 
00702 nis_error
00703 __nisfind_server (const_nis_name name, int search_parent,
00704                 directory_obj **dir, dir_binding *dbp, unsigned int flags)
00705 {
00706   nis_error result = NIS_SUCCESS;
00707   nis_error status;
00708   directory_obj *obj;
00709   struct timeval now;
00710   unsigned int server_used = ~0;
00711   unsigned int current_ep = ~0;
00712 
00713   if (name == NULL)
00714     return NIS_BADNAME;
00715 
00716   if (*dir != NULL)
00717     return NIS_SUCCESS;
00718 
00719   (void) gettimeofday (&now, NULL);
00720 
00721   if ((flags & NO_CACHE) == 0)
00722     *dir = nis_server_cache_search (name, search_parent, &server_used,
00723                                 &current_ep, &now);
00724   if (*dir != NULL)
00725     {
00726       unsigned int server_len = (*dir)->do_servers.do_servers_len;
00727       if (flags & MASTER_ONLY)
00728        {
00729          server_len = 1;
00730          if (server_used != 0)
00731            {
00732              server_used = ~0;
00733              current_ep = ~0;
00734            }
00735        }
00736       result = __nisbind_create (dbp, (*dir)->do_servers.do_servers_val,
00737                              server_len, server_used, current_ep, flags);
00738       if (result != NIS_SUCCESS)
00739        {
00740          nis_free_directory (*dir);
00741          *dir = NULL;
00742        }
00743       return result;
00744     }
00745 
00746   int saved_errno = errno;
00747   *dir = readColdStartFile ();
00748   __set_errno (saved_errno);
00749   if (*dir == NULL)
00750     /* No /var/nis/NIS_COLD_START->no NIS+ installed.  */
00751     return NIS_UNAVAIL;
00752 
00753   /* Try at first, if servers in "dir" know our object */
00754   const char *search_name = name;
00755   if (search_parent)
00756     search_name = __nis_domain_of (name);
00757   obj = first_shoot (search_name, *dir);
00758   if (obj == NULL)
00759     {
00760       obj = rec_dirsearch (search_name, *dir, &status);
00761       if (obj == NULL)
00762        result = status;
00763     }
00764 
00765   if (result == NIS_SUCCESS)
00766     {
00767       unsigned int server_len = obj->do_servers.do_servers_len;
00768       if (flags & MASTER_ONLY)
00769        server_len = 1;
00770       result = __nisbind_create (dbp, obj->do_servers.do_servers_val,
00771                              server_len, ~0, ~0, flags);
00772       if (result == NIS_SUCCESS)
00773        {
00774          if ((flags & MASTER_ONLY) == 0
00775              || obj->do_servers.do_servers_len == 1)
00776            {
00777              server_used = dbp->server_used;
00778              current_ep = dbp->current_ep;
00779            }
00780          if ((flags & NO_CACHE) == 0)
00781            nis_server_cache_add (name, search_parent, obj,
00782                               server_used, current_ep, &now);
00783        }
00784       else
00785        {
00786          nis_free_directory (obj);
00787          obj = NULL;
00788        }
00789     }
00790 
00791   *dir = obj;
00792 
00793   return result;
00794 }
00795 
00796 
00797 nis_error
00798 __prepare_niscall (const_nis_name name, directory_obj **dirp,
00799                  dir_binding *bptrp, unsigned int flags)
00800 {
00801   nis_error retcode = __nisfind_server (name, 1, dirp, bptrp, flags);
00802   if (__builtin_expect (retcode != NIS_SUCCESS, 0))
00803     return retcode;
00804 
00805   do
00806     if (__nisbind_connect (bptrp) == NIS_SUCCESS)
00807       return NIS_SUCCESS;
00808   while (__nisbind_next (bptrp) == NIS_SUCCESS);
00809 
00810   __nisbind_destroy (bptrp);
00811   memset (bptrp, '\0', sizeof (*bptrp));
00812 
00813   retcode = NIS_NAMEUNREACHABLE;
00814   nis_free_directory (*dirp);
00815   *dirp = NULL;
00816 
00817   return retcode;
00818 }
00819 libnsl_hidden_def (__prepare_niscall)
00820 
00821 
00822 nis_error
00823 __do_niscall (const_nis_name name, u_long prog, xdrproc_t xargs,
00824              caddr_t req, xdrproc_t xres, caddr_t resp, unsigned int flags,
00825              nis_cb *cb)
00826 {
00827   dir_binding bptr;
00828   directory_obj *dir = NULL;
00829   int saved_errno = errno;
00830 
00831   nis_error retcode = __prepare_niscall (name, &dir, &bptr, flags);
00832   if (retcode == NIS_SUCCESS)
00833     {
00834       retcode = __do_niscall3 (&bptr, prog, xargs, req, xres, resp, flags, cb);
00835 
00836       __nisbind_destroy (&bptr);
00837 
00838       nis_free_directory (dir);
00839     }
00840 
00841   __set_errno (saved_errno);
00842 
00843   return retcode;
00844 }