1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 
  23 /*
  24  * Copyright 2014 Gary Mills
  25  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
  30 /* All Rights Reserved */
  31 /*
  32  * Portions of this source code were derived from Berkeley
  33  * 4.3 BSD under license from the Regents of the University of
  34  * California.
  35  */
  36 
  37 /*
  38  * interface to rpcbind rpc service.
  39  */
  40 
  41 #include "mt.h"
  42 #include "rpc_mt.h"
  43 #include <assert.h>
  44 #include <rpc/rpc.h>
  45 #include <rpc/rpcb_prot.h>
  46 #include <netconfig.h>
  47 #include <netdir.h>
  48 #include <rpc/nettype.h>
  49 #include <syslog.h>
  50 #ifdef PORTMAP
  51 #include <netinet/in.h>           /* FOR IPPROTO_TCP/UDP definitions */
  52 #include <rpc/pmap_prot.h>
  53 #endif
  54 #include <sys/utsname.h>
  55 #include <errno.h>
  56 #include <stdlib.h>
  57 #include <string.h>
  58 #include <unistd.h>
  59 
  60 static struct timeval tottimeout = { 60, 0 };
  61 static const struct timeval rmttimeout = { 3, 0 };
  62 static struct timeval rpcbrmttime = { 15, 0 };
  63 
  64 extern bool_t xdr_wrapstring(XDR *, char **);
  65 
  66 static const char nullstring[] = "\000";
  67 
  68 extern CLIENT *_clnt_tli_create_timed(int, const struct netconfig *,
  69                         struct netbuf *, rpcprog_t, rpcvers_t, uint_t, uint_t,
  70                         const struct timeval *);
  71 
  72 static CLIENT *_getclnthandle_timed(char *, struct netconfig *, char **,
  73                         struct timeval *);
  74 
  75 
  76 /*
  77  * The life time of a cached entry should not exceed 5 minutes
  78  * since automountd attempts an unmount every 5 minutes.
  79  * It is arbitrarily set a little lower (3 min = 180 sec)
  80  * to reduce the time during which an entry is stale.
  81  */
  82 #define CACHE_TTL 180
  83 #define CACHESIZE 6
  84 
  85 struct address_cache {
  86         char *ac_host;
  87         char *ac_netid;
  88         char *ac_uaddr;
  89         struct netbuf *ac_taddr;
  90         struct address_cache *ac_next;
  91         time_t ac_maxtime;
  92 };
  93 
  94 static struct address_cache *front;
  95 static int cachesize;
  96 
  97 extern int lowvers;
  98 extern int authdes_cachesz;
  99 /*
 100  * This routine adjusts the timeout used for calls to the remote rpcbind.
 101  * Also, this routine can be used to set the use of portmapper version 2
 102  * only when doing rpc_broadcasts
 103  * These are private routines that may not be provided in future releases.
 104  */
 105 bool_t
 106 __rpc_control(int request, void *info)
 107 {
 108         switch (request) {
 109         case CLCR_GET_RPCB_TIMEOUT:
 110                 *(struct timeval *)info = tottimeout;
 111                 break;
 112         case CLCR_SET_RPCB_TIMEOUT:
 113                 tottimeout = *(struct timeval *)info;
 114                 break;
 115         case CLCR_GET_LOWVERS:
 116                 *(int *)info = lowvers;
 117                 break;
 118         case CLCR_SET_LOWVERS:
 119                 lowvers = *(int *)info;
 120                 break;
 121         case CLCR_GET_RPCB_RMTTIME:
 122                 *(struct timeval *)info = rpcbrmttime;
 123                 break;
 124         case CLCR_SET_RPCB_RMTTIME:
 125                 rpcbrmttime = *(struct timeval *)info;
 126                 break;
 127         case CLCR_GET_CRED_CACHE_SZ:
 128                 *(int *)info = authdes_cachesz;
 129                 break;
 130         case CLCR_SET_CRED_CACHE_SZ:
 131                 authdes_cachesz = *(int *)info;
 132                 break;
 133         default:
 134                 return (FALSE);
 135         }
 136         return (TRUE);
 137 }
 138 
 139 /*
 140  *      It might seem that a reader/writer lock would be more reasonable here.
 141  *      However because getclnthandle(), the only user of the cache functions,
 142  *      may do a delete_cache() operation if a check_cache() fails to return an
 143  *      address useful to clnt_tli_create(), we may as well use a mutex.
 144  */
 145 /*
 146  * As it turns out, if the cache lock is *not* a reader/writer lock, we will
 147  * block all clnt_create's if we are trying to connect to a host that's down,
 148  * since the lock will be held all during that time.
 149  */
 150 extern rwlock_t rpcbaddr_cache_lock;
 151 
 152 /*
 153  * The routines check_cache(), add_cache(), delete_cache() manage the
 154  * cache of rpcbind addresses for (host, netid).
 155  */
 156 
 157 static struct address_cache *
 158 check_cache(char *host, char *netid)
 159 {
 160         struct address_cache *cptr;
 161 
 162         /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
 163 
 164         assert(RW_READ_HELD(&rpcbaddr_cache_lock));
 165         for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
 166                 if ((strcmp(cptr->ac_host, host) == 0) &&
 167                     (strcmp(cptr->ac_netid, netid) == 0) &&
 168                     (time(NULL) <= cptr->ac_maxtime)) {
 169                         return (cptr);
 170                 }
 171         }
 172         return (NULL);
 173 }
 174 
 175 static void
 176 delete_cache(struct netbuf *addr)
 177 {
 178         struct address_cache *cptr, *prevptr = NULL;
 179 
 180         /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
 181         assert(RW_WRITE_HELD(&rpcbaddr_cache_lock));
 182         for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
 183                 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
 184                         free(cptr->ac_host);
 185                         free(cptr->ac_netid);
 186                         free(cptr->ac_taddr->buf);
 187                         free(cptr->ac_taddr);
 188                         if (cptr->ac_uaddr)
 189                                 free(cptr->ac_uaddr);
 190                         if (prevptr)
 191                                 prevptr->ac_next = cptr->ac_next;
 192                         else
 193                                 front = cptr->ac_next;
 194                         free(cptr);
 195                         cachesize--;
 196                         break;
 197                 }
 198                 prevptr = cptr;
 199         }
 200 }
 201 
 202 static void
 203 add_cache(char *host, char *netid, struct netbuf *taddr, char *uaddr)
 204 {
 205         struct address_cache  *ad_cache, *cptr, *prevptr;
 206 
 207         ad_cache = malloc(sizeof (struct address_cache));
 208         if (!ad_cache) {
 209                 goto memerr;
 210         }
 211         ad_cache->ac_maxtime = time(NULL) + CACHE_TTL;
 212         ad_cache->ac_host = strdup(host);
 213         ad_cache->ac_netid = strdup(netid);
 214         ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
 215         ad_cache->ac_taddr = malloc(sizeof (struct netbuf));
 216         if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
 217             (uaddr && !ad_cache->ac_uaddr)) {
 218                 goto memerr1;
 219         }
 220 
 221         ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
 222         ad_cache->ac_taddr->buf = malloc(taddr->len);
 223         if (ad_cache->ac_taddr->buf == NULL) {
 224                 goto memerr1;
 225         }
 226 
 227         (void) memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
 228 
 229 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
 230 
 231         (void) rw_wrlock(&rpcbaddr_cache_lock);
 232         if (cachesize < CACHESIZE) {
 233                 ad_cache->ac_next = front;
 234                 front = ad_cache;
 235                 cachesize++;
 236         } else {
 237                 /* Free the last entry */
 238                 cptr = front;
 239                 prevptr = NULL;
 240                 while (cptr->ac_next) {
 241                         prevptr = cptr;
 242                         cptr = cptr->ac_next;
 243                 }
 244 
 245                 free(cptr->ac_host);
 246                 free(cptr->ac_netid);
 247                 free(cptr->ac_taddr->buf);
 248                 free(cptr->ac_taddr);
 249                 if (cptr->ac_uaddr)
 250                         free(cptr->ac_uaddr);
 251 
 252                 if (prevptr) {
 253                         prevptr->ac_next = NULL;
 254                         ad_cache->ac_next = front;
 255                         front = ad_cache;
 256                 } else {
 257                         front = ad_cache;
 258                         ad_cache->ac_next = NULL;
 259                 }
 260                 free(cptr);
 261         }
 262         (void) rw_unlock(&rpcbaddr_cache_lock);
 263         return;
 264 memerr1:
 265         if (ad_cache->ac_host)
 266                 free(ad_cache->ac_host);
 267         if (ad_cache->ac_netid)
 268                 free(ad_cache->ac_netid);
 269         if (ad_cache->ac_uaddr)
 270                 free(ad_cache->ac_uaddr);
 271         if (ad_cache->ac_taddr)
 272                 free(ad_cache->ac_taddr);
 273         free(ad_cache);
 274 memerr:
 275         syslog(LOG_ERR, "add_cache : out of memory.");
 276 }
 277 
 278 /*
 279  * This routine will return a client handle that is connected to the
 280  * rpcbind. Returns NULL on error and free's everything.
 281  */
 282 static CLIENT *
 283 getclnthandle(char *host, struct netconfig *nconf, char **targaddr)
 284 {
 285         return (_getclnthandle_timed(host, nconf, targaddr, NULL));
 286 }
 287 
 288 /*
 289  * Same as getclnthandle() except it takes an extra timeout argument.
 290  * This is for bug 4049792: clnt_create_timed does not timeout.
 291  *
 292  * If tp is NULL, use default timeout to get a client handle.
 293  */
 294 static CLIENT *
 295 _getclnthandle_timed(char *host, struct netconfig *nconf, char **targaddr,
 296                                                         struct timeval *tp)
 297 {
 298         CLIENT *client = NULL;
 299         struct netbuf *addr;
 300         struct netbuf addr_to_delete;
 301         struct nd_addrlist *nas;
 302         struct nd_hostserv rpcbind_hs;
 303         struct address_cache *ad_cache;
 304         char *tmpaddr;
 305         int neterr;
 306         int j;
 307 
 308 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  ad_cache */
 309 
 310         /* Get the address of the rpcbind.  Check cache first */
 311         addr_to_delete.len = 0;
 312         (void) rw_rdlock(&rpcbaddr_cache_lock);
 313         ad_cache = check_cache(host, nconf->nc_netid);
 314         if (ad_cache != NULL) {
 315                 addr = ad_cache->ac_taddr;
 316                 client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr,
 317                     RPCBPROG, RPCBVERS4, 0, 0, tp);
 318                 if (client != NULL) {
 319                         if (targaddr) {
 320                                 /*
 321                                  * case where a client handle is created
 322                                  * without a targaddr and the handle is
 323                                  * requested with a targaddr
 324                                  */
 325                                 if (ad_cache->ac_uaddr != NULL) {
 326                                         *targaddr = strdup(ad_cache->ac_uaddr);
 327                                         if (*targaddr == NULL) {
 328                                                 syslog(LOG_ERR,
 329                                                 "_getclnthandle_timed: strdup "
 330                                                 "failed.");
 331                                                 rpc_createerr.cf_stat =
 332                                                     RPC_SYSTEMERROR;
 333                                                 (void) rw_unlock(
 334                                                     &rpcbaddr_cache_lock);
 335                                                 return (NULL);
 336                                         }
 337                                 } else {
 338                                         *targaddr = NULL;
 339                                 }
 340                         }
 341                         (void) rw_unlock(&rpcbaddr_cache_lock);
 342                         return (client);
 343                 }
 344                 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
 345                         (void) rw_unlock(&rpcbaddr_cache_lock);
 346                         return (NULL);
 347                 }
 348                 addr_to_delete.len = addr->len;
 349                 addr_to_delete.buf = malloc(addr->len);
 350                 if (addr_to_delete.buf == NULL) {
 351                         addr_to_delete.len = 0;
 352                 } else {
 353                         (void) memcpy(addr_to_delete.buf, addr->buf, addr->len);
 354                 }
 355         }
 356         (void) rw_unlock(&rpcbaddr_cache_lock);
 357         if (addr_to_delete.len != 0) {
 358                 /*
 359                  * Assume this may be due to cache data being
 360                  *  outdated
 361                  */
 362                 (void) rw_wrlock(&rpcbaddr_cache_lock);
 363                 delete_cache(&addr_to_delete);
 364                 (void) rw_unlock(&rpcbaddr_cache_lock);
 365                 free(addr_to_delete.buf);
 366         }
 367         rpcbind_hs.h_host = host;
 368         rpcbind_hs.h_serv = "rpcbind";
 369 
 370         if ((neterr = netdir_getbyname(nconf, &rpcbind_hs, &nas)) != 0) {
 371                 if (neterr == ND_NOHOST)
 372                         rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
 373                 else
 374                         rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
 375                 return (NULL);
 376         }
 377         /* XXX nas should perhaps be cached for better performance */
 378 
 379         for (j = 0; j < nas->n_cnt; j++) {
 380                 addr = &(nas->n_addrs[j]);
 381         client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr, RPCBPROG,
 382             RPCBVERS4, 0, 0, tp);
 383         if (client)
 384                 break;
 385         }
 386 
 387         if (client) {
 388                 tmpaddr = targaddr ? taddr2uaddr(nconf, addr) : NULL;
 389                 add_cache(host, nconf->nc_netid, addr, tmpaddr);
 390                 if (targaddr) {
 391                         *targaddr = tmpaddr;
 392                 }
 393         }
 394         netdir_free((char *)nas, ND_ADDRLIST);
 395         return (client);
 396 }
 397 
 398 /*
 399  * This routine will return a client handle that is connected to the local
 400  * rpcbind. Returns NULL on error.
 401  */
 402 static CLIENT *
 403 local_rpcb(void)
 404 {
 405         static struct netconfig *loopnconf;
 406         static char *hostname;
 407         extern mutex_t loopnconf_lock;
 408 
 409 /* VARIABLES PROTECTED BY loopnconf_lock: hostname loopnconf */
 410         (void) mutex_lock(&loopnconf_lock);
 411         if (loopnconf == NULL) {
 412                 struct utsname utsname;
 413                 struct netconfig *nconf, *tmpnconf = NULL;
 414                 void *nc_handle;
 415 
 416                 if (hostname == NULL) {
 417 #if defined(__i386) && !defined(__amd64)
 418                         if ((_nuname(&utsname) == -1) ||
 419                             ((hostname = strdup(utsname.nodename)) == NULL)) {
 420 #else
 421                         if ((uname(&utsname) == -1) ||
 422                             ((hostname = strdup(utsname.nodename)) == NULL)) {
 423 #endif
 424                                 syslog(LOG_ERR, "local_rpcb : strdup failed.");
 425                                 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
 426                                 (void) mutex_unlock(&loopnconf_lock);
 427                                 return (NULL);
 428                         }
 429                         /* hostname is never freed */
 430                 }
 431                 nc_handle = setnetconfig();
 432                 if (nc_handle == NULL) {
 433                         /* fails to open netconfig file */
 434                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 435                         (void) mutex_unlock(&loopnconf_lock);
 436                         return (NULL);
 437                 }
 438                 while (nconf = getnetconfig(nc_handle)) {
 439                         if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
 440                                 tmpnconf = nconf;
 441                                 if (nconf->nc_semantics == NC_TPI_CLTS)
 442                                         break;
 443                         }
 444                 }
 445                 if (tmpnconf == NULL) {
 446                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 447                         (void) mutex_unlock(&loopnconf_lock);
 448                         return (NULL);
 449                 }
 450                 loopnconf = getnetconfigent(tmpnconf->nc_netid);
 451                 /* loopnconf is never freed */
 452                 (void) endnetconfig(nc_handle);
 453         }
 454         (void) mutex_unlock(&loopnconf_lock);
 455         return (getclnthandle(hostname, loopnconf, NULL));
 456 }
 457 
 458 /*
 459  * Set a mapping between program, version and address.
 460  * Calls the rpcbind service to do the mapping.
 461  */
 462 bool_t
 463 rpcb_set(const rpcprog_t program, const rpcvers_t version,
 464                 const struct netconfig *nconf, const struct netbuf *address)
 465 {
 466         CLIENT *client;
 467         bool_t rslt = FALSE;
 468         RPCB parms;
 469         char uidbuf[32];
 470 
 471         /* parameter checking */
 472         if (nconf == NULL) {
 473                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 474                 return (FALSE);
 475         }
 476         if (address == NULL) {
 477                 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
 478                 return (FALSE);
 479         }
 480         client = local_rpcb();
 481         if (!client)
 482                 return (FALSE);
 483 
 484         parms.r_addr = taddr2uaddr((struct netconfig *)nconf,
 485             (struct netbuf *)address); /* convert to universal */
 486         if (!parms.r_addr) {
 487                 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
 488                 return (FALSE); /* no universal address */
 489         }
 490         parms.r_prog = program;
 491         parms.r_vers = version;
 492         parms.r_netid = nconf->nc_netid;
 493         /*
 494          * Though uid is not being used directly, we still send it for
 495          * completeness.  For non-unix platforms, perhaps some other
 496          * string or an empty string can be sent.
 497          */
 498         (void) sprintf(uidbuf, "%d", (int)geteuid());
 499         parms.r_owner = uidbuf;
 500 
 501         CLNT_CALL(client, RPCBPROC_SET, (xdrproc_t)xdr_rpcb, (char *)&parms,
 502             (xdrproc_t)xdr_bool, (char *)&rslt, tottimeout);
 503 
 504         CLNT_DESTROY(client);
 505         free(parms.r_addr);
 506         return (rslt);
 507 }
 508 
 509 /*
 510  * Remove the mapping between program, version and netbuf address.
 511  * Calls the rpcbind service to do the un-mapping.
 512  * If netbuf is NULL, unset for all the transports, otherwise unset
 513  * only for the given transport.
 514  */
 515 bool_t
 516 rpcb_unset(const rpcprog_t program, const rpcvers_t version,
 517                                                 const struct netconfig *nconf)
 518 {
 519         CLIENT *client;
 520         bool_t rslt = FALSE;
 521         RPCB parms;
 522         char uidbuf[32];
 523 
 524         client = local_rpcb();
 525         if (!client)
 526                 return (FALSE);
 527 
 528         parms.r_prog = program;
 529         parms.r_vers = version;
 530         if (nconf)
 531                 parms.r_netid = nconf->nc_netid;
 532         else
 533                 parms.r_netid = (char *)&nullstring[0]; /* unsets  all */
 534         parms.r_addr = (char *)&nullstring[0];
 535         (void) sprintf(uidbuf, "%d", (int)geteuid());
 536         parms.r_owner = uidbuf;
 537 
 538         CLNT_CALL(client, RPCBPROC_UNSET, (xdrproc_t)xdr_rpcb, (char *)&parms,
 539             (xdrproc_t)xdr_bool, (char *)&rslt, tottimeout);
 540 
 541         CLNT_DESTROY(client);
 542         return (rslt);
 543 }
 544 
 545 /*
 546  * From the merged list, find the appropriate entry
 547  */
 548 static struct netbuf *
 549 got_entry(rpcb_entry_list_ptr relp, struct netconfig *nconf)
 550 {
 551         struct netbuf *na = NULL;
 552         rpcb_entry_list_ptr sp;
 553         rpcb_entry *rmap;
 554 
 555         for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
 556                 rmap = &sp->rpcb_entry_map;
 557                 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
 558                     (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
 559                     (nconf->nc_semantics == rmap->r_nc_semantics) &&
 560                     (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
 561                         na = uaddr2taddr(nconf, rmap->r_maddr);
 562                         break;
 563                 }
 564         }
 565         return (na);
 566 }
 567 
 568 /*
 569  * Quick check to see if rpcbind is up.  Tries to connect over
 570  * local transport.
 571  */
 572 bool_t
 573 __rpcbind_is_up(void)
 574 {
 575         struct utsname name;
 576         char uaddr[SYS_NMLN];
 577         struct netbuf *addr;
 578         int fd;
 579         struct t_call *sndcall;
 580         struct netconfig *netconf;
 581         bool_t res;
 582 
 583 #if defined(__i386) && !defined(__amd64)
 584         if (_nuname(&name) == -1)
 585 #else
 586         if (uname(&name) == -1)
 587 #endif
 588                 return (TRUE);
 589 
 590         if ((fd = t_open("/dev/ticotsord", O_RDWR, NULL)) == -1)
 591                 return (TRUE);
 592 
 593         if (t_bind(fd, NULL, NULL) == -1) {
 594                 (void) t_close(fd);
 595                 return (TRUE);
 596         }
 597 
 598         /* LINTED pointer cast */
 599         if ((sndcall = (struct t_call *)t_alloc(fd, T_CALL, 0)) == NULL) {
 600                 (void) t_close(fd);
 601                 return (TRUE);
 602         }
 603 
 604         uaddr[0] = '\0';
 605         (void) strcpy(uaddr, name.nodename);
 606         (void) strcat(uaddr, ".rpc");
 607         if ((netconf = getnetconfigent("ticotsord")) == NULL) {
 608                 (void) t_free((char *)sndcall, T_CALL);
 609                 (void) t_close(fd);
 610                 return (FALSE);
 611         }
 612         addr = uaddr2taddr(netconf, uaddr);
 613         freenetconfigent(netconf);
 614         if (addr == NULL || addr->buf == NULL) {
 615                 if (addr)
 616                         free(addr);
 617                 (void) t_free((char *)sndcall, T_CALL);
 618                 (void) t_close(fd);
 619                 return (FALSE);
 620         }
 621         sndcall->addr.maxlen = addr->maxlen;
 622         sndcall->addr.len = addr->len;
 623         sndcall->addr.buf = addr->buf;
 624 
 625         if (t_connect(fd, sndcall, NULL) == -1)
 626                 res = FALSE;
 627         else
 628                 res = TRUE;
 629 
 630         sndcall->addr.maxlen = sndcall->addr.len = 0;
 631         sndcall->addr.buf = NULL;
 632         (void) t_free((char *)sndcall, T_CALL);
 633         free(addr->buf);
 634         free(addr);
 635         (void) t_close(fd);
 636 
 637         return (res);
 638 }
 639 
 640 
 641 /*
 642  * An internal function which optimizes rpcb_getaddr function.  It returns
 643  * the universal address of the remote service or NULL.  It also optionally
 644  * returns the client handle that it uses to contact the remote rpcbind.
 645  * The caller will re-purpose the client to contact the remote service.
 646  *
 647  * The algorithm used: First try version 4.  Then try version 3 (svr4).
 648  * Finally, if the transport is TCP or UDP, try version 2 (portmap).
 649  * Version 4 is now available with all current systems on the network.
 650  * With this algorithm, we get performance as well as a plan for
 651  * obsoleting version 2.
 652  *
 653  * XXX: Due to some problems with t_connect(), we do not reuse the same client
 654  * handle for COTS cases and hence in these cases we do not return the
 655  * client handle.  This code will change if t_connect() ever
 656  * starts working properly.  Also look under clnt_vc.c.
 657  */
 658 struct netbuf *
 659 __rpcb_findaddr_timed(rpcprog_t program, rpcvers_t version,
 660         struct netconfig *nconf, char *host, CLIENT **clpp, struct timeval *tp)
 661 {
 662         static bool_t check_rpcbind = TRUE;
 663         CLIENT *client = NULL;
 664         RPCB parms;
 665         enum clnt_stat clnt_st;
 666         char *ua = NULL;
 667         uint_t vers;
 668         struct netbuf *address = NULL;
 669         void *handle;
 670         rpcb_entry_list_ptr relp = NULL;
 671         bool_t tmp_client = FALSE;
 672 
 673         /* parameter checking */
 674         if (nconf == NULL) {
 675                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 676                 return (NULL);
 677         }
 678 
 679         parms.r_addr = NULL;
 680 
 681         /*
 682          * Use default total timeout if no timeout is specified.
 683          */
 684         if (tp == NULL)
 685                 tp = &tottimeout;
 686 
 687         /*
 688          * Check if rpcbind is up.  This prevents needless delays when
 689          * accessing applications such as the keyserver while booting
 690          * disklessly.
 691          */
 692         if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
 693                 if (!__rpcbind_is_up()) {
 694                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 695                         rpc_createerr.cf_error.re_errno = 0;
 696                         rpc_createerr.cf_error.re_terrno = 0;
 697                         goto error;
 698                 }
 699                 check_rpcbind = FALSE;
 700         }
 701 
 702         /*
 703          * First try version 4.
 704          */
 705         parms.r_prog = program;
 706         parms.r_vers = version;
 707         parms.r_owner = (char *)&nullstring[0];     /* not needed; */
 708         /* just for xdring */
 709         parms.r_netid = nconf->nc_netid; /* not really needed */
 710 
 711         /*
 712          * If a COTS transport is being used, try getting address via CLTS
 713          * transport.  This works only with version 4.
 714          */
 715         if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
 716             nconf->nc_semantics == NC_TPI_COTS) {
 717                 tmp_client = TRUE;
 718                 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
 719                         struct netconfig *nconf_clts;
 720 
 721                         while ((nconf_clts = __rpc_getconf(handle)) != NULL) {
 722                                 if (strcmp(nconf_clts->nc_protofmly,
 723                                     nconf->nc_protofmly) != 0) {
 724                                         continue;
 725                                 }
 726                                 client = _getclnthandle_timed(host, nconf_clts,
 727                                     &parms.r_addr, tp);
 728                                 break;
 729                         }
 730                         __rpc_endconf(handle);
 731                 }
 732         } else {
 733                 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
 734                 /* Sets cf_error members on failure */
 735         }
 736 
 737         if (client != NULL) {
 738 
 739                 /* Set rpcbind version 4 */
 740                 vers = RPCBVERS4;
 741                 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
 742 
 743                 /*
 744                  * We also send the remote system the address we used to
 745                  * contact it in case it can help it connect back with us
 746                  */
 747                 if (parms.r_addr == NULL) {
 748                         parms.r_addr = strdup(""); /* for XDRing */
 749                         if (parms.r_addr == NULL) {
 750                                 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
 751                                     "strdup failed.");
 752                                 /* cf_error is still zeroed */
 753                                 rpc_createerr.cf_error.re_errno = errno;
 754                                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
 755                                 goto error;
 756                         }
 757                 }
 758 
 759                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
 760                     (char *)&rpcbrmttime);
 761 
 762                 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
 763                     (xdrproc_t)xdr_rpcb, (char *)&parms,
 764                     (xdrproc_t)xdr_rpcb_entry_list_ptr, (char *)&relp, *tp);
 765                 /* Sets error structure members in client handle */
 766                 switch (clnt_st) {
 767                 case RPC_SUCCESS: /* Call succeeded */
 768                         address = got_entry(relp, nconf);
 769                         xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
 770                             (char *)&relp);
 771                         if (address != NULL) {
 772                                 /* Program number and version number matched */
 773                                 goto done;
 774                         }
 775                         /* Program and version not found for this transport */
 776                         /*
 777                          * XXX: should have returned with RPC_PROGUNAVAIL
 778                          * or perhaps RPC_PROGNOTREGISTERED error but
 779                          * since the remote machine might not always be able
 780                          * to send the address on all transports, we try the
 781                          * regular way with version 3, then 2
 782                          */
 783                         /* Try the next version */
 784                         break;
 785                 case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
 786                         clnt_geterr(client, &rpc_createerr.cf_error);
 787                         if (rpc_createerr.cf_error.re_vers.low > vers) {
 788                                 rpc_createerr.cf_stat = clnt_st;
 789                                 goto error;  /* a new version, can't handle */
 790                         }
 791                         /* Try the next version */
 792                         break;
 793                 case RPC_PROCUNAVAIL: /* Procedure unavailable */
 794                 case RPC_PROGUNAVAIL: /* Program not available */
 795                 case RPC_TIMEDOUT: /* Call timed out */
 796                         /* Try the next version */
 797                         break;
 798                 default:
 799                         clnt_geterr(client, &rpc_createerr.cf_error);
 800                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 801                         goto error;
 802                         break;
 803                 }
 804 
 805         } else {
 806 
 807                 /* No client */
 808                 tmp_client = FALSE;
 809 
 810         } /* End of version 4 */
 811 
 812         /* Destroy a temporary client */
 813         if (client != NULL && tmp_client) {
 814                 CLNT_DESTROY(client);
 815                 client = NULL;
 816                 free(parms.r_addr);
 817                 parms.r_addr = NULL;
 818         }
 819         tmp_client = FALSE;
 820 
 821         /*
 822          * Try version 3
 823          */
 824 
 825         /* Now the same transport is to be used to get the address */
 826         if (client == NULL) {
 827                 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
 828                 /* Sets cf_error members on failure */
 829         }
 830         address = NULL;
 831         if (client != NULL) {
 832                 if (parms.r_addr == NULL) {
 833                         parms.r_addr = strdup("");      /* for XDRing */
 834                         if (parms.r_addr == NULL) {
 835                                 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
 836                                     "strdup failed.");
 837                                 /* cf_error is still zeroed */
 838                                 rpc_createerr.cf_error.re_errno = errno;
 839                                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
 840                                 goto error;
 841                         }
 842                 }
 843 
 844                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
 845                     (char *)&rpcbrmttime);
 846                 vers = RPCBVERS; /* Set the version */
 847                 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
 848                 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
 849                     (xdrproc_t)xdr_rpcb, (char *)&parms,
 850                     (xdrproc_t)xdr_wrapstring, (char *)&ua, *tp);
 851                 /* Sets error structure members in client handle */
 852                 switch (clnt_st) {
 853                 case RPC_SUCCESS: /* Call succeeded */
 854                         if (ua != NULL) {
 855                                 if (ua[0] != '\0') {
 856                                         address = uaddr2taddr(nconf, ua);
 857                                 }
 858                                 xdr_free((xdrproc_t)xdr_wrapstring,
 859                                     (char *)&ua);
 860 
 861                                 if (address != NULL) {
 862                                         goto done;
 863                                 }
 864                                 /* We don't know about your universal addr */
 865                                 /* cf_error is still zeroed */
 866                                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
 867                                 goto error;
 868                         }
 869 #ifndef PORTMAP
 870                         clnt_geterr(client, &rpc_createerr.cf_error);
 871                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 872                         goto error;
 873 #endif
 874                         /* Try the next version */
 875                         break;
 876                 case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
 877                         clnt_geterr(client, &rpc_createerr.cf_error);
 878 #ifdef PORTMAP
 879                         if (rpc_createerr.cf_error.re_vers.low > vers) {
 880                                 rpc_createerr.cf_stat = clnt_st;
 881                                 goto error;  /* a new version, can't handle */
 882                         }
 883 #else
 884                         rpc_createerr.cf_stat = clnt_st;
 885                         goto error;
 886 #endif
 887                         /* Try the next version */
 888                         break;
 889 #ifdef PORTMAP
 890                 case RPC_PROCUNAVAIL: /* Procedure unavailable */
 891                 case RPC_PROGUNAVAIL: /* Program not available */
 892                 case RPC_TIMEDOUT: /* Call timed out */
 893                         /* Try the next version */
 894                         break;
 895 #endif
 896                 default:
 897                         clnt_geterr(client, &rpc_createerr.cf_error);
 898                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 899                         goto error;
 900                         break;
 901                 }
 902         } /* End of version 3 */
 903 #ifndef PORTMAP
 904         /* cf_error members set by creation failure */
 905         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
 906 #endif
 907         /*
 908          * Try version 2
 909          */
 910 
 911 #ifdef PORTMAP
 912         /* Try version 2 for TCP or UDP */
 913         if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
 914                 ushort_t port = 0;
 915                 struct netbuf remote;
 916                 uint_t pmapvers = 2;
 917                 struct pmap pmapparms;
 918 
 919                 /*
 920                  * Try UDP only - there are some portmappers out
 921                  * there that use UDP only.
 922                  */
 923                 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
 924                         struct netconfig *newnconf;
 925 
 926                         if (client != NULL) {
 927                                 CLNT_DESTROY(client);
 928                                 client = NULL;
 929                                 free(parms.r_addr);
 930                                 parms.r_addr = NULL;
 931                         }
 932                         if ((handle = __rpc_setconf("udp")) == NULL) {
 933                                 /* cf_error is still zeroed */
 934                                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 935                                 goto error;
 936                         }
 937 
 938                         /*
 939                          * The following to reinforce that you can
 940                          * only request for remote address through
 941                          * the same transport you are requesting.
 942                          * ie. requesting unversial address
 943                          * of IPv4 has to be carried through IPv4.
 944                          * Can't use IPv6 to send out the request.
 945                          * The mergeaddr in rpcbind can't handle
 946                          * this.
 947                          */
 948                         for (;;) {
 949                                 if ((newnconf = __rpc_getconf(handle))
 950                                     == NULL) {
 951                                         __rpc_endconf(handle);
 952                                         /* cf_error is still zeroed */
 953                                         rpc_createerr.cf_stat =
 954                                             RPC_UNKNOWNPROTO;
 955                                         goto error;
 956                                 }
 957                                 /*
 958                                  * here check the protocol family to
 959                                  * be consistent with the request one
 960                                  */
 961                                 if (strcmp(newnconf->nc_protofmly,
 962                                     nconf->nc_protofmly) == 0)
 963                                         break;
 964                         }
 965 
 966                         client = _getclnthandle_timed(host, newnconf,
 967                             &parms.r_addr, tp);
 968                         /* Sets cf_error members on failure */
 969                         __rpc_endconf(handle);
 970                         tmp_client = TRUE;
 971                 }
 972                 if (client == NULL) {
 973                         /* cf_error members set by creation failure */
 974                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
 975                         tmp_client = FALSE;
 976                         goto error;
 977                 }
 978 
 979                 /*
 980                  * Set version and retry timeout.
 981                  */
 982                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
 983                 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
 984 
 985                 pmapparms.pm_prog = program;
 986                 pmapparms.pm_vers = version;
 987                 pmapparms.pm_prot = (strcmp(nconf->nc_proto, NC_TCP) != 0) ?
 988                     IPPROTO_UDP : IPPROTO_TCP;
 989                 pmapparms.pm_port = 0;  /* not needed */
 990                 clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
 991                     (xdrproc_t)xdr_pmap, (caddr_t)&pmapparms,
 992                     (xdrproc_t)xdr_u_short, (caddr_t)&port, *tp);
 993                 /* Sets error structure members in client handle */
 994                 if (clnt_st != RPC_SUCCESS) {
 995                         clnt_geterr(client, &rpc_createerr.cf_error);
 996                         rpc_createerr.cf_stat = RPC_RPCBFAILURE;
 997                         goto error;
 998                 } else if (port == 0) {
 999                         /* cf_error is still zeroed */
1000                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1001                         goto error;
1002                 }
1003                 port = htons(port);
1004                 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
1005                 if (((address = malloc(sizeof (struct netbuf))) == NULL) ||
1006                     ((address->buf = malloc(remote.len)) == NULL)) {
1007                         /* cf_error is still zeroed */
1008                         rpc_createerr.cf_error.re_errno = errno;
1009                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1010                         free(address);
1011                         address = NULL;
1012                         goto error;
1013                 }
1014                 (void) memcpy(address->buf, remote.buf, remote.len);
1015                 (void) memcpy(&address->buf[sizeof (short)], &port,
1016                     sizeof (short));
1017                 address->len = address->maxlen = remote.len;
1018                 goto done;
1019         } else {
1020                 /* Not NC_INET */
1021                 if (client != NULL && clnt_st != RPC_SUCCESS) {
1022                         clnt_geterr(client, &rpc_createerr.cf_error);
1023                         rpc_createerr.cf_stat = clnt_st;
1024                 } else {
1025                         /* No client handle */
1026                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1027                 }
1028         }
1029 #endif
1030 
1031 error:
1032         /* Return NULL address and NULL client */
1033         address = NULL;
1034         if (client != NULL) {
1035                 CLNT_DESTROY(client);
1036                 client = NULL;
1037         }
1038 
1039 done:
1040         /* Return an address and optional client */
1041         if (client != NULL && tmp_client) {
1042                 /* This client is the temporary one */
1043                 CLNT_DESTROY(client);
1044                 client = NULL;
1045         }
1046         if (clpp != NULL) {
1047                 *clpp = client;
1048         } else if (client != NULL) {
1049                 CLNT_DESTROY(client);
1050         }
1051         free(parms.r_addr);
1052         return (address);
1053 }
1054 
1055 
1056 /*
1057  * Find the mapped address for program, version.
1058  * Calls the rpcbind service remotely to do the lookup.
1059  * Uses the transport specified in nconf.
1060  * Returns FALSE (0) if no map exists, else returns 1.
1061  *
1062  * Assuming that the address is all properly allocated
1063  */
1064 int
1065 rpcb_getaddr(const rpcprog_t program, const rpcvers_t version,
1066         const struct netconfig *nconf, struct netbuf *address, const char *host)
1067 {
1068         struct netbuf *na;
1069 
1070         if ((na = __rpcb_findaddr_timed(program, version,
1071             (struct netconfig *)nconf, (char *)host, NULL, NULL)) == NULL)
1072                 return (FALSE);
1073 
1074         if (na->len > address->maxlen) {
1075                 /* Too long address */
1076                 netdir_free((char *)na, ND_ADDR);
1077                 rpc_createerr.cf_stat = RPC_FAILED;
1078                 return (FALSE);
1079         }
1080         (void) memcpy(address->buf, na->buf, (int)na->len);
1081         address->len = na->len;
1082         netdir_free((char *)na, ND_ADDR);
1083         return (TRUE);
1084 }
1085 
1086 /*
1087  * Get a copy of the current maps.
1088  * Calls the rpcbind service remotely to get the maps.
1089  *
1090  * It returns only a list of the services
1091  * It returns NULL on failure.
1092  */
1093 rpcblist *
1094 rpcb_getmaps(const struct netconfig *nconf, const char *host)
1095 {
1096         rpcblist_ptr head = NULL;
1097         CLIENT *client;
1098         enum clnt_stat clnt_st;
1099         int vers = 0;
1100 
1101         client = getclnthandle((char *)host,
1102             (struct netconfig *)nconf, NULL);
1103         if (client == NULL)
1104                 return (NULL);
1105 
1106         clnt_st = CLNT_CALL(client, RPCBPROC_DUMP,
1107             (xdrproc_t)xdr_void, NULL,
1108             (xdrproc_t)xdr_rpcblist_ptr,
1109             (char *)&head, tottimeout);
1110         if (clnt_st == RPC_SUCCESS)
1111                 goto done;
1112 
1113         if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1114             (clnt_st != RPC_PROGUNAVAIL)) {
1115                 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1116                 clnt_geterr(client, &rpc_createerr.cf_error);
1117                 goto done;
1118         }
1119 
1120         /* fall back to earlier version */
1121         CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
1122         if (vers == RPCBVERS4) {
1123                 vers = RPCBVERS;
1124                 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
1125                 if (CLNT_CALL(client, RPCBPROC_DUMP,
1126                     (xdrproc_t)xdr_void,
1127                     NULL, (xdrproc_t)xdr_rpcblist_ptr,
1128                     (char *)&head, tottimeout) == RPC_SUCCESS)
1129                                 goto done;
1130         }
1131         rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1132         clnt_geterr(client, &rpc_createerr.cf_error);
1133 
1134 done:
1135         CLNT_DESTROY(client);
1136         return (head);
1137 }
1138 
1139 /*
1140  * rpcbinder remote-call-service interface.
1141  * This routine is used to call the rpcbind remote call service
1142  * which will look up a service program in the address maps, and then
1143  * remotely call that routine with the given parameters. This allows
1144  * programs to do a lookup and call in one step.
1145  */
1146 enum clnt_stat
1147 rpcb_rmtcall(const struct netconfig *nconf, const char *host,
1148         const rpcprog_t prog, const rpcvers_t vers, const rpcproc_t proc,
1149         const xdrproc_t xdrargs, const caddr_t argsp, const xdrproc_t xdrres,
1150         const caddr_t resp, const struct timeval tout, struct netbuf *addr_ptr)
1151 {
1152         CLIENT *client;
1153         enum clnt_stat stat;
1154         struct r_rpcb_rmtcallargs a;
1155         struct r_rpcb_rmtcallres r;
1156         int rpcb_vers;
1157 
1158         client = getclnthandle((char *)host, (struct netconfig *)nconf, NULL);
1159         if (client == NULL)
1160                 return (RPC_FAILED);
1161         CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rmttimeout);
1162         a.prog = prog;
1163         a.vers = vers;
1164         a.proc = proc;
1165         a.args.args_val = argsp;
1166         a.xdr_args = xdrargs;
1167         r.addr = NULL;
1168         r.results.results_val = resp;
1169         r.xdr_res = xdrres;
1170 
1171         for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1172                 CLNT_CONTROL(client, CLSET_VERS, (char *)&rpcb_vers);
1173                 stat = CLNT_CALL(client, RPCBPROC_CALLIT,
1174                     (xdrproc_t)xdr_rpcb_rmtcallargs, (char *)&a,
1175                     (xdrproc_t)xdr_rpcb_rmtcallres, (char *)&r, tout);
1176                 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1177                         struct netbuf *na;
1178 
1179                         na = uaddr2taddr((struct netconfig *)nconf, r.addr);
1180                         if (!na) {
1181                                 stat = RPC_N2AXLATEFAILURE;
1182                                 ((struct netbuf *)addr_ptr)->len = 0;
1183                                 goto error;
1184                         }
1185                         if (na->len > addr_ptr->maxlen) {
1186                                 /* Too long address */
1187                                 stat = RPC_FAILED; /* XXX A better error no */
1188                                 netdir_free((char *)na, ND_ADDR);
1189                                 ((struct netbuf *)addr_ptr)->len = 0;
1190                                 goto error;
1191                         }
1192                         (void) memcpy(addr_ptr->buf, na->buf, (int)na->len);
1193                         ((struct netbuf *)addr_ptr)->len = na->len;
1194                         netdir_free((char *)na, ND_ADDR);
1195                         break;
1196                 }
1197                 if ((stat != RPC_PROGVERSMISMATCH) &&
1198                     (stat != RPC_PROGUNAVAIL))
1199                         goto error;
1200         }
1201 error:
1202         CLNT_DESTROY(client);
1203         if (r.addr)
1204                 xdr_free((xdrproc_t)xdr_wrapstring, (char *)&r.addr);
1205         return (stat);
1206 }
1207 
1208 /*
1209  * Gets the time on the remote host.
1210  * Returns 1 if succeeds else 0.
1211  */
1212 bool_t
1213 rpcb_gettime(const char *host, time_t *timep)
1214 {
1215         CLIENT *client = NULL;
1216         void *handle;
1217         struct netconfig *nconf;
1218         int vers;
1219         enum clnt_stat st;
1220 
1221         if ((host == NULL) || (host[0] == NULL)) {
1222                 (void) time(timep);
1223                 return (TRUE);
1224         }
1225 
1226         if ((handle = __rpc_setconf("netpath")) == NULL) {
1227                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1228                 return (FALSE);
1229         }
1230         rpc_createerr.cf_stat = RPC_SUCCESS;
1231         while (client == NULL) {
1232                 if ((nconf = __rpc_getconf(handle)) == NULL) {
1233                         if (rpc_createerr.cf_stat == RPC_SUCCESS)
1234                                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1235                         break;
1236                 }
1237                 client = getclnthandle((char *)host, nconf, NULL);
1238                 if (client)
1239                         break;
1240         }
1241         __rpc_endconf(handle);
1242         if (client == NULL)
1243                 return (FALSE);
1244 
1245         st = CLNT_CALL(client, RPCBPROC_GETTIME,
1246             (xdrproc_t)xdr_void, NULL,
1247             (xdrproc_t)xdr_time_t, (char *)timep, tottimeout);
1248 
1249         if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1250                 CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
1251                 if (vers == RPCBVERS4) {
1252                         /* fall back to earlier version */
1253                         vers = RPCBVERS;
1254                         CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
1255                         st = CLNT_CALL(client, RPCBPROC_GETTIME,
1256                             (xdrproc_t)xdr_void, NULL,
1257                             (xdrproc_t)xdr_time_t, (char *)timep,
1258                             tottimeout);
1259                 }
1260         }
1261         CLNT_DESTROY(client);
1262         return (st == RPC_SUCCESS? TRUE : FALSE);
1263 }
1264 
1265 /*
1266  * Converts taddr to universal address.  This routine should never
1267  * really be called because local n2a libraries are always provided.
1268  */
1269 char *
1270 rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
1271 {
1272         CLIENT *client;
1273         char *uaddr = NULL;
1274 
1275         /* parameter checking */
1276         if (nconf == NULL) {
1277                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1278                 return (NULL);
1279         }
1280         if (taddr == NULL) {
1281                 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1282                 return (NULL);
1283         }
1284         client = local_rpcb();
1285         if (!client)
1286                 return (NULL);
1287 
1288         CLNT_CALL(client, RPCBPROC_TADDR2UADDR, (xdrproc_t)xdr_netbuf,
1289             (char *)taddr, (xdrproc_t)xdr_wrapstring, (char *)&uaddr,
1290             tottimeout);
1291         CLNT_DESTROY(client);
1292         return (uaddr);
1293 }
1294 
1295 /*
1296  * Converts universal address to netbuf.  This routine should never
1297  * really be called because local n2a libraries are always provided.
1298  */
1299 struct netbuf *
1300 rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
1301 {
1302         CLIENT *client;
1303         struct netbuf *taddr;
1304 
1305         /* parameter checking */
1306         if (nconf == NULL) {
1307                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1308                 return (NULL);
1309         }
1310         if (uaddr == NULL) {
1311                 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1312                 return (NULL);
1313         }
1314         client = local_rpcb();
1315         if (!client)
1316                 return (NULL);
1317 
1318         taddr = calloc(1, sizeof (struct netbuf));
1319         if (taddr == NULL) {
1320                 CLNT_DESTROY(client);
1321                 return (NULL);
1322         }
1323 
1324         if (CLNT_CALL(client, RPCBPROC_UADDR2TADDR, (xdrproc_t)xdr_wrapstring,
1325             (char *)&uaddr, (xdrproc_t)xdr_netbuf, (char *)taddr,
1326             tottimeout) != RPC_SUCCESS) {
1327                 free(taddr);
1328                 taddr = NULL;
1329         }
1330         CLNT_DESTROY(client);
1331         return (taddr);
1332 }