Print this page
4729 __rpcb_findaddr_timed should try rpcbind protocol 4 first
Reviewed by: Marcel Telka <marcel@telka.sk>


   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 2006 Sun Microsystems, Inc.  All rights reserved.
  25  * Use is subject to license terms.
  26  */
  27 
  28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
  29 /* All Rights Reserved */
  30 /*
  31  * Portions of this source code were derived from Berkeley
  32  * 4.3 BSD under license from the Regents of the University of
  33  * California.
  34  */
  35 
  36 #pragma ident   "%Z%%M% %I%     %E% SMI"
  37 
  38 /*
  39  * interface to rpcbind rpc service.
  40  */
  41 
  42 #include "mt.h"
  43 #include "rpc_mt.h"
  44 #include <assert.h>
  45 #include <rpc/rpc.h>
  46 #include <rpc/rpcb_prot.h>
  47 #include <netconfig.h>
  48 #include <netdir.h>

  49 #include <rpc/nettype.h>
  50 #include <syslog.h>
  51 #ifdef PORTMAP
  52 #include <netinet/in.h>           /* FOR IPPROTO_TCP/UDP definitions */
  53 #include <rpc/pmap_prot.h>
  54 #endif
  55 #ifdef ND_DEBUG
  56 #include <stdio.h>
  57 #endif
  58 #include <sys/utsname.h>
  59 #include <errno.h>
  60 #include <stdlib.h>
  61 #include <string.h>
  62 #include <unistd.h>
  63 
  64 static struct timeval tottimeout = { 60, 0 };
  65 static const struct timeval rmttimeout = { 3, 0 };
  66 static struct timeval rpcbrmttime = { 15, 0 };
  67 
  68 extern bool_t xdr_wrapstring(XDR *, char **);
  69 
  70 static const char nullstring[] = "\000";
  71 
  72 extern CLIENT *_clnt_tli_create_timed(int, const struct netconfig *,
  73                         struct netbuf *, rpcprog_t, rpcvers_t, uint_t, uint_t,
  74                         const struct timeval *);
  75 
  76 static CLIENT *_getclnthandle_timed(char *, struct netconfig *, char **,
  77                         struct timeval *);


 153  */
 154 extern rwlock_t rpcbaddr_cache_lock;
 155 
 156 /*
 157  * The routines check_cache(), add_cache(), delete_cache() manage the
 158  * cache of rpcbind addresses for (host, netid).
 159  */
 160 
 161 static struct address_cache *
 162 check_cache(char *host, char *netid)
 163 {
 164         struct address_cache *cptr;
 165 
 166         /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
 167 
 168         assert(RW_READ_HELD(&rpcbaddr_cache_lock));
 169         for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
 170                 if ((strcmp(cptr->ac_host, host) == 0) &&
 171                     (strcmp(cptr->ac_netid, netid) == 0) &&
 172                         (time(NULL) <= cptr->ac_maxtime)) {
 173 #ifdef ND_DEBUG
 174                         fprintf(stderr, "Found cache entry for %s: %s\n",
 175                                 host, netid);
 176 #endif
 177                         return (cptr);
 178                 }
 179         }
 180         return (NULL);
 181 }
 182 
 183 static void
 184 delete_cache(struct netbuf *addr)
 185 {
 186         struct address_cache *cptr, *prevptr = NULL;
 187 
 188         /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
 189         assert(RW_WRITE_HELD(&rpcbaddr_cache_lock));
 190         for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
 191                 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
 192                         free(cptr->ac_host);
 193                         free(cptr->ac_netid);
 194                         free(cptr->ac_taddr->buf);
 195                         free(cptr->ac_taddr);
 196                         if (cptr->ac_uaddr)


 216         if (!ad_cache) {
 217                 goto memerr;
 218         }
 219         ad_cache->ac_maxtime = time(NULL) + CACHE_TTL;
 220         ad_cache->ac_host = strdup(host);
 221         ad_cache->ac_netid = strdup(netid);
 222         ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
 223         ad_cache->ac_taddr = malloc(sizeof (struct netbuf));
 224         if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
 225                 (uaddr && !ad_cache->ac_uaddr)) {
 226                 goto memerr1;
 227         }
 228 
 229         ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
 230         ad_cache->ac_taddr->buf = malloc(taddr->len);
 231         if (ad_cache->ac_taddr->buf == NULL) {
 232                 goto memerr1;
 233         }
 234 
 235         (void) memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
 236 #ifdef ND_DEBUG
 237         (void) fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
 238 #endif
 239 
 240 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
 241 
 242         (void) rw_wrlock(&rpcbaddr_cache_lock);
 243         if (cachesize < CACHESIZE) {
 244                 ad_cache->ac_next = front;
 245                 front = ad_cache;
 246                 cachesize++;
 247         } else {
 248                 /* Free the last entry */
 249                 cptr = front;
 250                 prevptr = NULL;
 251                 while (cptr->ac_next) {
 252                         prevptr = cptr;
 253                         cptr = cptr->ac_next;
 254                 }
 255 
 256 #ifdef ND_DEBUG
 257                 fprintf(stderr, "Deleted from cache: %s : %s\n",
 258                         cptr->ac_host, cptr->ac_netid);
 259 #endif
 260                 free(cptr->ac_host);
 261                 free(cptr->ac_netid);
 262                 free(cptr->ac_taddr->buf);
 263                 free(cptr->ac_taddr);
 264                 if (cptr->ac_uaddr)
 265                         free(cptr->ac_uaddr);
 266 
 267                 if (prevptr) {
 268                         prevptr->ac_next = NULL;
 269                         ad_cache->ac_next = front;
 270                         front = ad_cache;
 271                 } else {
 272                         front = ad_cache;
 273                         ad_cache->ac_next = NULL;
 274                 }
 275                 free(cptr);
 276         }
 277         (void) rw_unlock(&rpcbaddr_cache_lock);
 278         return;
 279 memerr1:


 364                 addr_to_delete.buf = malloc(addr->len);
 365                 if (addr_to_delete.buf == NULL) {
 366                         addr_to_delete.len = 0;
 367                 } else {
 368                         (void) memcpy(addr_to_delete.buf, addr->buf, addr->len);
 369                 }
 370         }
 371         (void) rw_unlock(&rpcbaddr_cache_lock);
 372         if (addr_to_delete.len != 0) {
 373                 /*
 374                  * Assume this may be due to cache data being
 375                  *  outdated
 376                  */
 377                 (void) rw_wrlock(&rpcbaddr_cache_lock);
 378                 delete_cache(&addr_to_delete);
 379                 (void) rw_unlock(&rpcbaddr_cache_lock);
 380                 free(addr_to_delete.buf);
 381         }
 382         rpcbind_hs.h_host = host;
 383         rpcbind_hs.h_serv = "rpcbind";
 384 #ifdef ND_DEBUG
 385         fprintf(stderr, "rpcbind client routines: diagnostics :\n");
 386         fprintf(stderr, "\tGetting address for (%s, %s, %s) ... \n",
 387                 rpcbind_hs.h_host, rpcbind_hs.h_serv, nconf->nc_netid);
 388 #endif
 389 
 390         if ((neterr = netdir_getbyname(nconf, &rpcbind_hs, &nas)) != 0) {
 391                 if (neterr == ND_NOHOST)
 392                         rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
 393                 else
 394                         rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
 395                 return (NULL);
 396         }
 397         /* XXX nas should perhaps be cached for better performance */
 398 
 399         for (j = 0; j < nas->n_cnt; j++) {
 400                 addr = &(nas->n_addrs[j]);
 401 #ifdef ND_DEBUG
 402 {
 403         int i;
 404         char *ua;
 405 
 406         ua = taddr2uaddr(nconf, &(nas->n_addrs[j]));
 407         fprintf(stderr, "Got it [%s]\n", ua);
 408         free(ua);
 409 
 410         fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
 411                 addr->len, addr->maxlen);
 412         fprintf(stderr, "\tAddress is ");
 413         for (i = 0; i < addr->len; i++)
 414                 fprintf(stderr, "%u.", addr->buf[i]);
 415         fprintf(stderr, "\n");
 416 }
 417 #endif
 418         client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr, RPCBPROG,
 419                                 RPCBVERS4, 0, 0, tp);
 420         if (client)
 421                 break;
 422         }
 423 #ifdef ND_DEBUG
 424         if (!client) {
 425                 clnt_pcreateerror("rpcbind clnt interface");
 426         }
 427 #endif
 428 
 429         if (client) {
 430                 tmpaddr = targaddr ? taddr2uaddr(nconf, addr) : NULL;
 431                 add_cache(host, nconf->nc_netid, addr, tmpaddr);
 432                 if (targaddr) {
 433                         *targaddr = tmpaddr;
 434                 }
 435         }
 436         netdir_free((char *)nas, ND_ADDRLIST);
 437         return (client);
 438 }
 439 
 440 /*
 441  * This routine will return a client handle that is connected to the local
 442  * rpcbind. Returns NULL on error and free's everything.
 443  */
 444 static CLIENT *
 445 local_rpcb(void)
 446 {
 447         static struct netconfig *loopnconf;
 448         static char *hostname;
 449         extern mutex_t loopnconf_lock;
 450 
 451 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
 452         (void) mutex_lock(&loopnconf_lock);
 453         if (loopnconf == NULL) {
 454                 struct utsname utsname;
 455                 struct netconfig *nconf, *tmpnconf = NULL;
 456                 void *nc_handle;
 457 
 458                 if (hostname == NULL) {
 459 #if defined(__i386) && !defined(__amd64)
 460                         if ((_nuname(&utsname) == -1) ||
 461 #else
 462                         if ((uname(&utsname) == -1) ||
 463 #endif
 464                             ((hostname = strdup(utsname.nodename)) == NULL)) {
 465                                 syslog(LOG_ERR, "local_rpcb : strdup failed.");
 466                                 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
 467                                 (void) mutex_unlock(&loopnconf_lock);
 468                                 return (NULL);
 469                         }
 470                 }
 471                 nc_handle = setnetconfig();
 472                 if (nc_handle == NULL) {
 473                         /* fails to open netconfig file */
 474                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 475                         (void) mutex_unlock(&loopnconf_lock);
 476                         return (NULL);
 477                 }
 478                 while (nconf = getnetconfig(nc_handle)) {
 479                         if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
 480                                 tmpnconf = nconf;
 481                                 if (nconf->nc_semantics == NC_TPI_CLTS)
 482                                         break;
 483                         }
 484                 }
 485                 if (tmpnconf == NULL) {
 486                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 487                         (void) mutex_unlock(&loopnconf_lock);
 488                         return (NULL);
 489                 }
 490                 loopnconf = getnetconfigent(tmpnconf->nc_netid);


 582         return (rslt);
 583 }
 584 
 585 /*
 586  * From the merged list, find the appropriate entry
 587  */
 588 static struct netbuf *
 589 got_entry(rpcb_entry_list_ptr relp, struct netconfig *nconf)
 590 {
 591         struct netbuf *na = NULL;
 592         rpcb_entry_list_ptr sp;
 593         rpcb_entry *rmap;
 594 
 595         for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
 596                 rmap = &sp->rpcb_entry_map;
 597                 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
 598                     (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
 599                     (nconf->nc_semantics == rmap->r_nc_semantics) &&
 600                     (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
 601                         na = uaddr2taddr(nconf, rmap->r_maddr);
 602 #ifdef ND_DEBUG
 603                         fprintf(stderr, "\tRemote address is [%s].\n",
 604                                 rmap->r_maddr);
 605                         if (!na)
 606                                 fprintf(stderr,
 607                                     "\tCouldn't resolve remote address!\n");
 608 #endif
 609                         break;
 610                 }
 611         }
 612         return (na);
 613 }
 614 
 615 /*
 616  * Quick check to see if rpcbind is up.  Tries to connect over
 617  * local transport.
 618  */
 619 bool_t
 620 __rpcbind_is_up(void)
 621 {
 622         struct utsname name;
 623         char uaddr[SYS_NMLN];
 624         struct netbuf *addr;
 625         int fd;
 626         struct t_call *sndcall;
 627         struct netconfig *netconf;
 628         bool_t res;
 629 
 630 #if defined(__i386) && !defined(__amd64)
 631         if (_nuname(&name) == -1)
 632 #else
 633         if (uname(&name) == -1)
 634 #endif
 635                 return (TRUE);
 636 
 637         if ((fd = t_open("/dev/ticotsord", O_RDWR, NULL)) == -1)
 638                 return (TRUE);
 639 
 640         if (t_bind(fd, NULL, NULL) == -1) {
 641                 (void) t_close(fd);
 642                 return (TRUE);
 643         }
 644 
 645         /* LINTED pointer cast */
 646         if ((sndcall = (struct t_call *)t_alloc(fd, T_CALL, 0)) == NULL) {
 647                 (void) t_close(fd);
 648                 return (TRUE);
 649         }
 650 
 651         uaddr[0] = '\0';
 652         (void) strcpy(uaddr, name.nodename);
 653         (void) strcat(uaddr, ".rpc");
 654         if ((netconf = getnetconfigent("ticotsord")) == NULL) {
 655                 (void) t_free((char *)sndcall, T_CALL);
 656                 (void) t_close(fd);
 657                 return (FALSE);
 658         }
 659         addr = uaddr2taddr(netconf, uaddr);
 660         freenetconfigent(netconf);
 661         if (addr == NULL || addr->buf == NULL) {
 662                 if (addr)
 663                         free(addr);
 664                 (void) t_free((char *)sndcall, T_CALL);
 665                 (void) t_close(fd);
 666                 return (FALSE);
 667         }
 668         sndcall->addr.maxlen = addr->maxlen;
 669         sndcall->addr.len = addr->len;
 670         sndcall->addr.buf = addr->buf;
 671 
 672         if (t_connect(fd, sndcall, NULL) == -1)
 673                 res = FALSE;
 674         else
 675                 res = TRUE;
 676 
 677         sndcall->addr.maxlen = sndcall->addr.len = 0;
 678         sndcall->addr.buf = NULL;
 679         (void) t_free((char *)sndcall, T_CALL);
 680         free(addr->buf);
 681         free(addr);
 682         (void) t_close(fd);
 683 
 684         return (res);
 685 }
 686 
 687 
 688 /*
 689  * An internal function which optimizes rpcb_getaddr function.  It also

 690  * returns the client handle that it uses to contact the remote rpcbind.

 691  *
 692  * The algorithm used: If the transports is TCP or UDP, it first tries
 693  * version 2 (portmap), 4 and then 3 (svr4).  This order should be
 694  * changed in the next OS release to 4, 2 and 3.  We are assuming that by
 695  * that time, version 4 would be available on many machines on the network.
 696  * With this algorithm, we get performance as well as a plan for
 697  * obsoleting version 2.
 698  *
 699  * For all other transports, the algorithm remains as 4 and then 3.
 700  *
 701  * XXX: Due to some problems with t_connect(), we do not reuse the same client
 702  * handle for COTS cases and hence in these cases we do not return the
 703  * client handle.  This code will change if t_connect() ever
 704  * starts working properly.  Also look under clnt_vc.c.
 705  */
 706 struct netbuf *
 707 __rpcb_findaddr_timed(rpcprog_t program, rpcvers_t version,
 708         struct netconfig *nconf, char *host, CLIENT **clpp, struct timeval *tp)
 709 {
 710         static bool_t check_rpcbind = TRUE;
 711         CLIENT *client = NULL;
 712         RPCB parms;
 713         enum clnt_stat clnt_st;
 714         char *ua = NULL;
 715         uint_t vers;
 716         struct netbuf *address = NULL;
 717         uint_t start_vers = RPCBVERS4;


 718 
 719         /* parameter checking */
 720         if (nconf == NULL) {
 721                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;




 722                 return (NULL);
 723         }
 724 
 725         parms.r_addr = NULL;
 726 
 727         /*
 728          * Use default total timeout if no timeout is specified.
 729          */
 730         if (tp == NULL)
 731                 tp = &tottimeout;
 732 
 733 #ifdef PORTMAP
 734         /* Try version 2 for TCP or UDP */
 735         if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
 736                 ushort_t port = 0;
 737                 struct netbuf remote;
 738                 uint_t pmapvers = 2;
 739                 struct pmap pmapparms;
 740 
 741                 /*
 742                  * Try UDP only - there are some portmappers out
 743                  * there that use UDP only.
 744                  */
 745                 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
 746                         struct netconfig *newnconf;
 747                         void *handle;
 748 
 749                         if ((handle = __rpc_setconf("udp")) == NULL) {
 750                                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 751                                 return (NULL);
 752                         }
 753 
 754                         /*
 755                          * The following to reinforce that you can
 756                          * only request for remote address through
 757                          * the same transport you are requesting.
 758                          * ie. requesting unversial address
 759                          * of IPv4 has to be carried through IPv4.
 760                          * Can't use IPv6 to send out the request.
 761                          * The mergeaddr in rpcbind can't handle
 762                          * this.
 763                          */
 764                         for (;;) {
 765                                 if ((newnconf = __rpc_getconf(handle))
 766                                                                     == NULL) {
 767                                         __rpc_endconf(handle);
 768                                         rpc_createerr.cf_stat =
 769                                             RPC_UNKNOWNPROTO;
 770                                         return (NULL);
 771                                 }
 772                                 /*
 773                                  * here check the protocol family to
 774                                  * be consistent with the request one
 775                                  */
 776                                 if (strcmp(newnconf->nc_protofmly,
 777                                     nconf->nc_protofmly) == NULL)
 778                                         break;
 779                         }
 780 
 781                         client = _getclnthandle_timed(host, newnconf,
 782                                         &parms.r_addr, tp);
 783                         __rpc_endconf(handle);
 784                 } else {
 785                         client = _getclnthandle_timed(host, nconf,
 786                                         &parms.r_addr, tp);
 787                 }
 788                 if (client == NULL)
 789                         return (NULL);
 790 
 791                 /*
 792                  * Set version and retry timeout.
 793                  */
 794                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
 795                 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
 796 
 797                 pmapparms.pm_prog = program;
 798                 pmapparms.pm_vers = version;
 799                 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
 800                                     IPPROTO_UDP : IPPROTO_TCP;
 801                 pmapparms.pm_port = 0;  /* not needed */
 802                 clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
 803                                     (xdrproc_t)xdr_pmap, (caddr_t)&pmapparms,
 804                                     (xdrproc_t)xdr_u_short, (caddr_t)&port,
 805                                     *tp);
 806                 if (clnt_st != RPC_SUCCESS) {
 807                         if ((clnt_st == RPC_PROGVERSMISMATCH) ||
 808                             (clnt_st == RPC_PROGUNAVAIL))
 809                                 goto try_rpcbind; /* Try different versions */
 810                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 811                         clnt_geterr(client, &rpc_createerr.cf_error);
 812                         goto error;
 813                 } else if (port == 0) {
 814                         address = NULL;
 815                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
 816                         goto error;
 817                 }
 818                 port = htons(port);
 819                 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
 820                 if (((address = malloc(sizeof (struct netbuf))) == NULL) ||
 821                     ((address->buf = malloc(remote.len)) == NULL)) {
 822                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
 823                         clnt_geterr(client, &rpc_createerr.cf_error);
 824                         if (address) {
 825                                 free(address);
 826                                 address = NULL;
 827                         }
 828                         goto error;
 829                 }
 830                 (void) memcpy(address->buf, remote.buf, remote.len);
 831                 (void) memcpy(&address->buf[sizeof (short)], &port,
 832                                                                 sizeof (short));
 833                 address->len = address->maxlen = remote.len;
 834                 goto done;
 835         }
 836 #endif
 837 
 838 try_rpcbind:
 839         /*
 840          * Check if rpcbind is up.  This prevents needless delays when
 841          * accessing applications such as the keyserver while booting
 842          * disklessly.
 843          */
 844         if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
 845                 if (!__rpcbind_is_up()) {
 846                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 847                         rpc_createerr.cf_error.re_errno = 0;
 848                         rpc_createerr.cf_error.re_terrno = 0;
 849                         goto error;
 850                 }
 851                 check_rpcbind = FALSE;
 852         }
 853 
 854         /*
 855          * Now we try version 4 and then 3.
 856          * We also send the remote system the address we used to
 857          * contact it in case it can help to connect back with us
 858          */
 859         parms.r_prog = program;
 860         parms.r_vers = version;
 861         parms.r_owner = (char *)&nullstring[0];     /* not needed; */
 862         /* just for xdring */
 863         parms.r_netid = nconf->nc_netid; /* not really needed */
 864 
 865         /*
 866          * If a COTS transport is being used, try getting address via CLTS
 867          * transport.  This works only with version 4.
 868          */
 869         if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
 870             nconf->nc_semantics == NC_TPI_COTS) {
 871                 void *handle;

 872                 struct netconfig *nconf_clts;
 873                 rpcb_entry_list_ptr relp = NULL;
 874 
 875                 if (client == NULL) {
 876                         /* This did not go through the above PORTMAP/TCP code */
 877                         if ((handle = __rpc_setconf("datagram_v")) != NULL) {
 878                                 while ((nconf_clts = __rpc_getconf(handle))
 879                                     != NULL) {
 880                                         if (strcmp(nconf_clts->nc_protofmly,
 881                                             nconf->nc_protofmly) != 0) {
 882                                                 continue;
 883                                         }
 884                                         client = _getclnthandle_timed(host,
 885                                                 nconf_clts, &parms.r_addr,
 886                                                 tp);



 887                                         break;
 888                                 }
 889                                 __rpc_endconf(handle);
 890                         }
 891                         if (client == NULL)
 892                                 goto regular_rpcbind;   /* Go the regular way */
 893                 } else {
 894                         /* This is a UDP PORTMAP handle.  Change to version 4 */






 895                         vers = RPCBVERS4;
 896                         CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
 897                 }
 898                 /*
 899                  * We also send the remote system the address we used to
 900                  * contact it in case it can help it connect back with us
 901                  */
 902                 if (parms.r_addr == NULL) {
 903                         parms.r_addr = strdup(""); /* for XDRing */
 904                         if (parms.r_addr == NULL) {
 905                                 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
 906                                         "strdup failed.");



 907                                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
 908                                 address = NULL;
 909                                 goto error;
 910                         }
 911                 }
 912 
 913                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);

 914 

 915                 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
 916                                     (xdrproc_t)xdr_rpcb, (char *)&parms,
 917                                     (xdrproc_t)xdr_rpcb_entry_list_ptr,
 918                                     (char *)&relp, *tp);
 919                 if (clnt_st == RPC_SUCCESS) {
 920                         if (address = got_entry(relp, nconf)) {

 921                                 xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
 922                                         (char *)&relp);


 923                                 goto done;
 924                         }
 925                         /* Entry not found for this transport */
 926                         xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
 927                                     (char *)&relp);
 928                         /*
 929                          * XXX: should have perhaps returned with error but

 930                          * since the remote machine might not always be able
 931                          * to send the address on all transports, we try the
 932                          * regular way with regular_rpcbind
 933                          */
 934                         goto regular_rpcbind;
 935                 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
 936                             (clnt_st == RPC_PROGUNAVAIL)) {
 937                         start_vers = RPCBVERS;  /* Try version 3 now */
 938                         goto regular_rpcbind; /* Try different versions */
 939                 } else {
 940                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 941                         clnt_geterr(client, &rpc_createerr.cf_error);














 942                         goto error;

 943                 }
 944         }
 945 
 946 regular_rpcbind:
 947 
 948         /* Now the same transport is to be used to get the address */
 949         if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
 950             (nconf->nc_semantics == NC_TPI_COTS))) {
 951                 /* A CLTS type of client - destroy it */



 952                 CLNT_DESTROY(client);
 953                 client = NULL;
 954                 free(parms.r_addr);
 955                 parms.r_addr = NULL;
 956         }

 957 





 958         if (client == NULL) {

 959                 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
 960                 if (client == NULL) {
 961                         address = NULL;
 962                         goto error;
 963                 }
 964         }

 965         if (parms.r_addr == NULL) {
 966                 parms.r_addr = strdup("");      /* for XDRing */
 967                 if (parms.r_addr == NULL) {
 968                         syslog(LOG_ERR, "__rpcb_findaddr_timed: "
 969                                 "strdup failed.");
 970                         address = NULL;


 971                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
 972                         goto error;
 973                 }
 974         }
 975 
 976         /* First try from start_vers and then version 3 (RPCBVERS) */
 977 
 978         CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
 979         for (vers = start_vers;  vers >= RPCBVERS; vers--) {
 980                 /* Set the version */
 981                 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);


 982                 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
 983                                     (xdrproc_t)xdr_rpcb, (char *)&parms,
 984                                     (xdrproc_t)xdr_wrapstring,
 985                                     (char *)&ua, *tp);
 986                 if (clnt_st == RPC_SUCCESS) {
 987                         if ((ua == NULL) || (ua[0] == NULL)) {
 988                                 if (ua != NULL)
 989                                         xdr_free(xdr_wrapstring, (char *)&ua);
 990 
 991                                 /* address unknown */














 992                                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
 993                                 goto error;
 994                         }
 995                         address = uaddr2taddr(nconf, ua);
 996 #ifdef ND_DEBUG
 997                         fprintf(stderr, "\tRemote address is [%s]\n", ua);
 998                         if (!address)
 999                                 fprintf(stderr,
1000                                         "\tCouldn't resolve remote address!\n");
1001 #endif
1002                         xdr_free((xdrproc_t)xdr_wrapstring, (char *)&ua);
1003 
1004                         if (!address) {
1005                                 /* We don't know about your universal address */
1006                                 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;






1007                                 goto error;















1008                         }
1009                         goto done;




























1010                 }
1011                 if (clnt_st == RPC_PROGVERSMISMATCH) {
1012                         struct rpc_err rpcerr;



1013 
1014                         clnt_geterr(client, &rpcerr);
1015                         if (rpcerr.re_vers.low > RPCBVERS4)
1016                                 goto error;  /* a new version, can't handle */
1017                 } else if (clnt_st != RPC_PROGUNAVAIL) {
1018                         /* Cant handle this error */















1019                         goto error;
1020                 }







1021         }
1022 
1023         if ((address == NULL) || (address->len == 0)) {










1024                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;






















1025                 clnt_geterr(client, &rpc_createerr.cf_error);








1026         }




































1027 
1028 error:
1029         if (client) {


1030                 CLNT_DESTROY(client);
1031                 client = NULL;
1032         }

1033 done:
1034         if (nconf->nc_semantics != NC_TPI_CLTS) {
1035                 /* This client is the connectionless one */
1036                 if (client) {
1037                         CLNT_DESTROY(client);
1038                         client = NULL;
1039                 }
1040         }
1041         if (clpp) {
1042                 *clpp = client;
1043         } else if (client) {
1044                 CLNT_DESTROY(client);
1045         }
1046         if (parms.r_addr)
1047                 free(parms.r_addr);
1048         return (address);
1049 }
1050 
1051 
1052 /*
1053  * Find the mapped address for program, version.
1054  * Calls the rpcbind service remotely to do the lookup.
1055  * Uses the transport specified in nconf.
1056  * Returns FALSE (0) if no map exists, else returns 1.
1057  *
1058  * Assuming that the address is all properly allocated
1059  */
1060 int
1061 rpcb_getaddr(const rpcprog_t program, const rpcvers_t version,
1062         const struct netconfig *nconf, struct netbuf *address, const char *host)
1063 {
1064         struct netbuf *na;
1065 
1066         if ((na = __rpcb_findaddr_timed(program, version,




   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 <netdb.h>
  49 #include <rpc/nettype.h>
  50 #include <syslog.h>
  51 #ifdef PORTMAP
  52 #include <netinet/in.h>           /* FOR IPPROTO_TCP/UDP definitions */
  53 #include <rpc/pmap_prot.h>
  54 #endif



  55 #include <sys/utsname.h>
  56 #include <errno.h>
  57 #include <stdlib.h>
  58 #include <string.h>
  59 #include <unistd.h>
  60 
  61 static struct timeval tottimeout = { 60, 0 };
  62 static const struct timeval rmttimeout = { 3, 0 };
  63 static struct timeval rpcbrmttime = { 15, 0 };
  64 
  65 extern bool_t xdr_wrapstring(XDR *, char **);
  66 
  67 static const char nullstring[] = "\000";
  68 
  69 extern CLIENT *_clnt_tli_create_timed(int, const struct netconfig *,
  70                         struct netbuf *, rpcprog_t, rpcvers_t, uint_t, uint_t,
  71                         const struct timeval *);
  72 
  73 static CLIENT *_getclnthandle_timed(char *, struct netconfig *, char **,
  74                         struct timeval *);


 150  */
 151 extern rwlock_t rpcbaddr_cache_lock;
 152 
 153 /*
 154  * The routines check_cache(), add_cache(), delete_cache() manage the
 155  * cache of rpcbind addresses for (host, netid).
 156  */
 157 
 158 static struct address_cache *
 159 check_cache(char *host, char *netid)
 160 {
 161         struct address_cache *cptr;
 162 
 163         /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
 164 
 165         assert(RW_READ_HELD(&rpcbaddr_cache_lock));
 166         for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
 167                 if ((strcmp(cptr->ac_host, host) == 0) &&
 168                     (strcmp(cptr->ac_netid, netid) == 0) &&
 169                     (time(NULL) <= cptr->ac_maxtime)) {




 170                         return (cptr);
 171                 }
 172         }
 173         return (NULL);
 174 }
 175 
 176 static void
 177 delete_cache(struct netbuf *addr)
 178 {
 179         struct address_cache *cptr, *prevptr = NULL;
 180 
 181         /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
 182         assert(RW_WRITE_HELD(&rpcbaddr_cache_lock));
 183         for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
 184                 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
 185                         free(cptr->ac_host);
 186                         free(cptr->ac_netid);
 187                         free(cptr->ac_taddr->buf);
 188                         free(cptr->ac_taddr);
 189                         if (cptr->ac_uaddr)


 209         if (!ad_cache) {
 210                 goto memerr;
 211         }
 212         ad_cache->ac_maxtime = time(NULL) + CACHE_TTL;
 213         ad_cache->ac_host = strdup(host);
 214         ad_cache->ac_netid = strdup(netid);
 215         ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
 216         ad_cache->ac_taddr = malloc(sizeof (struct netbuf));
 217         if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
 218             (uaddr && !ad_cache->ac_uaddr)) {
 219                 goto memerr1;
 220         }
 221 
 222         ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
 223         ad_cache->ac_taddr->buf = malloc(taddr->len);
 224         if (ad_cache->ac_taddr->buf == NULL) {
 225                 goto memerr1;
 226         }
 227 
 228         (void) memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);



 229 
 230 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
 231 
 232         (void) rw_wrlock(&rpcbaddr_cache_lock);
 233         if (cachesize < CACHESIZE) {
 234                 ad_cache->ac_next = front;
 235                 front = ad_cache;
 236                 cachesize++;
 237         } else {
 238                 /* Free the last entry */
 239                 cptr = front;
 240                 prevptr = NULL;
 241                 while (cptr->ac_next) {
 242                         prevptr = cptr;
 243                         cptr = cptr->ac_next;
 244                 }
 245 




 246                 free(cptr->ac_host);
 247                 free(cptr->ac_netid);
 248                 free(cptr->ac_taddr->buf);
 249                 free(cptr->ac_taddr);
 250                 if (cptr->ac_uaddr)
 251                         free(cptr->ac_uaddr);
 252 
 253                 if (prevptr) {
 254                         prevptr->ac_next = NULL;
 255                         ad_cache->ac_next = front;
 256                         front = ad_cache;
 257                 } else {
 258                         front = ad_cache;
 259                         ad_cache->ac_next = NULL;
 260                 }
 261                 free(cptr);
 262         }
 263         (void) rw_unlock(&rpcbaddr_cache_lock);
 264         return;
 265 memerr1:


 350                 addr_to_delete.buf = malloc(addr->len);
 351                 if (addr_to_delete.buf == NULL) {
 352                         addr_to_delete.len = 0;
 353                 } else {
 354                         (void) memcpy(addr_to_delete.buf, addr->buf, addr->len);
 355                 }
 356         }
 357         (void) rw_unlock(&rpcbaddr_cache_lock);
 358         if (addr_to_delete.len != 0) {
 359                 /*
 360                  * Assume this may be due to cache data being
 361                  *  outdated
 362                  */
 363                 (void) rw_wrlock(&rpcbaddr_cache_lock);
 364                 delete_cache(&addr_to_delete);
 365                 (void) rw_unlock(&rpcbaddr_cache_lock);
 366                 free(addr_to_delete.buf);
 367         }
 368         rpcbind_hs.h_host = host;
 369         rpcbind_hs.h_serv = "rpcbind";





 370 
 371         if ((neterr = netdir_getbyname(nconf, &rpcbind_hs, &nas)) != 0) {
 372                 if (neterr == ND_NOHOST)
 373                         rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
 374                 else
 375                         rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
 376                 return (NULL);
 377         }
 378         /* XXX nas should perhaps be cached for better performance */
 379 
 380         for (j = 0; j < nas->n_cnt; j++) {
 381                 addr = &(nas->n_addrs[j]);

















 382         client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr, RPCBPROG,
 383             RPCBVERS4, 0, 0, tp);
 384         if (client)
 385                 break;
 386         }





 387 
 388         if (client) {
 389                 tmpaddr = targaddr ? taddr2uaddr(nconf, addr) : NULL;
 390                 add_cache(host, nconf->nc_netid, addr, tmpaddr);
 391                 if (targaddr) {
 392                         *targaddr = tmpaddr;
 393                 }
 394         }
 395         netdir_free((char *)nas, ND_ADDRLIST);
 396         return (client);
 397 }
 398 
 399 /*
 400  * This routine will return a client handle that is connected to the local
 401  * rpcbind. Returns NULL on error.
 402  */
 403 static CLIENT *
 404 local_rpcb(void)
 405 {
 406         static struct netconfig *loopnconf;
 407         static char hostname[MAXHOSTNAMELEN + 1];
 408         extern mutex_t loopnconf_lock;
 409 
 410 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
 411         (void) mutex_lock(&loopnconf_lock);
 412         if (loopnconf == NULL) {

 413                 struct netconfig *nconf, *tmpnconf = NULL;
 414                 void *nc_handle;
 415 
 416                 if ((hostname[0] == '\0') && (gethostname(hostname,
 417                     sizeof (hostname)) < 0)) {
 418                         syslog(LOG_ERR, "local_rpcb: gethostname failed.");





 419                         rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
 420                         (void) mutex_unlock(&loopnconf_lock);
 421                         return (NULL);
 422                 }

 423                 nc_handle = setnetconfig();
 424                 if (nc_handle == NULL) {
 425                         /* fails to open netconfig file */
 426                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 427                         (void) mutex_unlock(&loopnconf_lock);
 428                         return (NULL);
 429                 }
 430                 while (nconf = getnetconfig(nc_handle)) {
 431                         if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
 432                                 tmpnconf = nconf;
 433                                 if (nconf->nc_semantics == NC_TPI_CLTS)
 434                                         break;
 435                         }
 436                 }
 437                 if (tmpnconf == NULL) {
 438                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 439                         (void) mutex_unlock(&loopnconf_lock);
 440                         return (NULL);
 441                 }
 442                 loopnconf = getnetconfigent(tmpnconf->nc_netid);


 534         return (rslt);
 535 }
 536 
 537 /*
 538  * From the merged list, find the appropriate entry
 539  */
 540 static struct netbuf *
 541 got_entry(rpcb_entry_list_ptr relp, struct netconfig *nconf)
 542 {
 543         struct netbuf *na = NULL;
 544         rpcb_entry_list_ptr sp;
 545         rpcb_entry *rmap;
 546 
 547         for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
 548                 rmap = &sp->rpcb_entry_map;
 549                 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
 550                     (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
 551                     (nconf->nc_semantics == rmap->r_nc_semantics) &&
 552                     (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
 553                         na = uaddr2taddr(nconf, rmap->r_maddr);







 554                         break;
 555                 }
 556         }
 557         return (na);
 558 }
 559 
 560 /*
 561  * Quick check to see if rpcbind is up.  Tries to connect over
 562  * local transport.
 563  */
 564 bool_t
 565 __rpcbind_is_up(void)
 566 {
 567         char hostname[MAXHOSTNAMELEN + 1];
 568         char uaddr[SYS_NMLN];
 569         struct netbuf *addr;
 570         int fd;
 571         struct t_call *sndcall;
 572         struct netconfig *netconf;
 573         bool_t res;
 574 
 575         if (gethostname(hostname, sizeof (hostname)) < 0)




 576                 return (TRUE);
 577 
 578         if ((fd = t_open("/dev/ticotsord", O_RDWR, NULL)) == -1)
 579                 return (TRUE);
 580 
 581         if (t_bind(fd, NULL, NULL) == -1) {
 582                 (void) t_close(fd);
 583                 return (TRUE);
 584         }
 585 
 586         /* LINTED pointer cast */
 587         if ((sndcall = (struct t_call *)t_alloc(fd, T_CALL, 0)) == NULL) {
 588                 (void) t_close(fd);
 589                 return (TRUE);
 590         }
 591 
 592         uaddr[0] = '\0';
 593         (void) strlcpy(uaddr, hostname, sizeof (uaddr) - 5);
 594         (void) strcat(uaddr, ".rpc");
 595         if ((netconf = getnetconfigent("ticotsord")) == NULL) {
 596                 (void) t_free((char *)sndcall, T_CALL);
 597                 (void) t_close(fd);
 598                 return (FALSE);
 599         }
 600         addr = uaddr2taddr(netconf, uaddr);
 601         freenetconfigent(netconf);
 602         if (addr == NULL || addr->buf == NULL) {
 603                 if (addr)
 604                         free(addr);
 605                 (void) t_free((char *)sndcall, T_CALL);
 606                 (void) t_close(fd);
 607                 return (FALSE);
 608         }
 609         sndcall->addr.maxlen = addr->maxlen;
 610         sndcall->addr.len = addr->len;
 611         sndcall->addr.buf = addr->buf;
 612 
 613         if (t_connect(fd, sndcall, NULL) == -1)
 614                 res = FALSE;
 615         else
 616                 res = TRUE;
 617 
 618         sndcall->addr.maxlen = sndcall->addr.len = 0;
 619         sndcall->addr.buf = NULL;
 620         (void) t_free((char *)sndcall, T_CALL);
 621         free(addr->buf);
 622         free(addr);
 623         (void) t_close(fd);
 624 
 625         return (res);
 626 }
 627 
 628 
 629 /*
 630  * An internal function which optimizes rpcb_getaddr function.  It returns
 631  * the universal address of the remote service or NULL.  It also optionally
 632  * returns the client handle that it uses to contact the remote rpcbind.
 633  * The caller will re-purpose the client handle to contact the remote service.
 634  *
 635  * The algorithm used: First try version 4.  Then try version 3 (svr4).
 636  * Finally, if the transport is TCP or UDP, try version 2 (portmap).
 637  * Version 4 is now available with all current systems on the network.

 638  * With this algorithm, we get performance as well as a plan for
 639  * obsoleting version 2.
 640  *


 641  * XXX: Due to some problems with t_connect(), we do not reuse the same client
 642  * handle for COTS cases and hence in these cases we do not return the
 643  * client handle.  This code will change if t_connect() ever
 644  * starts working properly.  Also look under clnt_vc.c.
 645  */
 646 struct netbuf *
 647 __rpcb_findaddr_timed(rpcprog_t program, rpcvers_t version,
 648         struct netconfig *nconf, char *host, CLIENT **clpp, struct timeval *tp)
 649 {
 650         static bool_t check_rpcbind = TRUE;
 651         CLIENT *client = NULL;
 652         RPCB parms;
 653         enum clnt_stat clnt_st;
 654         char *ua = NULL;
 655         uint_t vers;
 656         struct netbuf *address = NULL;
 657         void *handle;
 658         rpcb_entry_list_ptr relp = NULL;
 659         bool_t tmp_client = FALSE;
 660 
 661         /* parameter checking */
 662         if (nconf == NULL) {
 663                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 664                 /*
 665                  * Setting rpc_createerr.cf_stat is sufficient.
 666                  * No details in rpc_createerr.cf_error needed.
 667                  */
 668                 return (NULL);
 669         }
 670 
 671         parms.r_addr = NULL;
 672 
 673         /*
 674          * Use default total timeout if no timeout is specified.
 675          */
 676         if (tp == NULL)
 677                 tp = &tottimeout;
 678 








 679         /*


































































































 680          * Check if rpcbind is up.  This prevents needless delays when
 681          * accessing applications such as the keyserver while booting
 682          * disklessly.
 683          */
 684         if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
 685                 if (!__rpcbind_is_up()) {
 686                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 687                         rpc_createerr.cf_error.re_errno = 0;
 688                         rpc_createerr.cf_error.re_terrno = 0;
 689                         goto error;
 690                 }
 691                 check_rpcbind = FALSE;
 692         }
 693 
 694         /*
 695          * First try version 4.


 696          */
 697         parms.r_prog = program;
 698         parms.r_vers = version;
 699         parms.r_owner = (char *)&nullstring[0];     /* not needed; */
 700         /* just for xdring */
 701         parms.r_netid = nconf->nc_netid; /* not really needed */
 702 
 703         /*
 704          * If a COTS transport is being used, try getting address via CLTS
 705          * transport.  This works only with version 4.
 706          */
 707         if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
 708             nconf->nc_semantics == NC_TPI_COTS) {
 709                 tmp_client = TRUE;
 710                 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
 711                         struct netconfig *nconf_clts;

 712 
 713                         while ((nconf_clts = __rpc_getconf(handle)) != NULL) {




 714                                 if (strcmp(nconf_clts->nc_protofmly,
 715                                     nconf->nc_protofmly) != 0) {
 716                                         continue;
 717                                 }
 718                                 /*
 719                                  * Sets rpc_createerr.cf_error members
 720                                  * on failure
 721                                  */
 722                                 client = _getclnthandle_timed(host, nconf_clts,
 723                                     &parms.r_addr, tp);
 724                                 break;
 725                         }
 726                         __rpc_endconf(handle);
 727                 }


 728         } else {
 729                 /* Sets rpc_createerr.cf_error members on failure */
 730                 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
 731         }
 732 
 733         if (client != NULL) {
 734 
 735                 /* Set rpcbind version 4 */
 736                 vers = RPCBVERS4;
 737                 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
 738 
 739                 /*
 740                  * We also send the remote system the address we used to
 741                  * contact it in case it can help it connect back with us
 742                  */
 743                 if (parms.r_addr == NULL) {
 744                         parms.r_addr = strdup(""); /* for XDRing */
 745                         if (parms.r_addr == NULL) {
 746                                 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
 747                                     "strdup failed.");
 748                                 /* Construct a system error */
 749                                 rpc_createerr.cf_error.re_errno = errno;
 750                                 rpc_createerr.cf_error.re_terrno = 0;
 751                                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;

 752                                 goto error;
 753                         }
 754                 }
 755 
 756                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
 757                     (char *)&rpcbrmttime);
 758 
 759                 /* Sets error structure members in client handle */
 760                 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
 761                     (xdrproc_t)xdr_rpcb, (char *)&parms,
 762                     (xdrproc_t)xdr_rpcb_entry_list_ptr, (char *)&relp, *tp);
 763 
 764                 switch (clnt_st) {
 765                 case RPC_SUCCESS: /* Call succeeded */
 766                         address = got_entry(relp, nconf);
 767                         xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
 768                             (char *)&relp);
 769                         if (address != NULL) {
 770                                 /* Program number and version number matched */
 771                                 goto done;
 772                         }
 773                         /* Program and version not found for this transport */


 774                         /*
 775                          * XXX: should have returned with RPC_PROGUNAVAIL
 776                          * or perhaps RPC_PROGNOTREGISTERED error but
 777                          * since the remote machine might not always be able
 778                          * to send the address on all transports, we try the
 779                          * regular way with version 3, then 2
 780                          */
 781                         /* Try the next version */
 782                         break;
 783                 case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */




 784                         clnt_geterr(client, &rpc_createerr.cf_error);
 785                         if (rpc_createerr.cf_error.re_vers.low > vers) {
 786                                 rpc_createerr.cf_stat = clnt_st;
 787                                 goto error;  /* a new version, can't handle */
 788                         }
 789                         /* Try the next version */
 790                         break;
 791                 case RPC_PROCUNAVAIL: /* Procedure unavailable */
 792                 case RPC_PROGUNAVAIL: /* Program not available */
 793                 case RPC_TIMEDOUT: /* Call timed out */
 794                         /* Try the next version */
 795                         break;
 796                 default:
 797                         clnt_geterr(client, &rpc_createerr.cf_error);
 798                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 799                         goto error;
 800                         break;
 801                 }

 802 
 803         } else {
 804 
 805                 /* No client */
 806                 tmp_client = FALSE;
 807 
 808         } /* End of version 4 */
 809 
 810         /* Destroy a temporary client */
 811         if (client != NULL && tmp_client) {
 812                 CLNT_DESTROY(client);
 813                 client = NULL;
 814                 free(parms.r_addr);
 815                 parms.r_addr = NULL;
 816         }
 817         tmp_client = FALSE;
 818 
 819         /*
 820          * Try version 3
 821          */
 822 
 823         /* Now the same transport is to be used to get the address */
 824         if (client == NULL) {
 825                 /* Sets rpc_createerr.cf_error members on failure */
 826                 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);



 827         }
 828         address = NULL;
 829         if (client != NULL) {
 830                 if (parms.r_addr == NULL) {
 831                         parms.r_addr = strdup("");      /* for XDRing */
 832                         if (parms.r_addr == NULL) {
 833                                 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
 834                                     "strdup failed.");
 835                                 /* Construct a system error */
 836                                 rpc_createerr.cf_error.re_errno = errno;
 837                                 rpc_createerr.cf_error.re_terrno = 0;
 838                                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
 839                                 goto error;
 840                         }
 841                 }
 842 
 843                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
 844                     (char *)&rpcbrmttime);
 845                 vers = RPCBVERS; /* Set the version */


 846                 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
 847 
 848                 /* Sets error structure members in client handle */
 849                 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
 850                     (xdrproc_t)xdr_rpcb, (char *)&parms,
 851                     (xdrproc_t)xdr_wrapstring, (char *)&ua, *tp);





 852 
 853                 switch (clnt_st) {
 854                 case RPC_SUCCESS: /* Call succeeded */
 855                         if (ua != NULL) {
 856                                 if (ua[0] != '\0') {
 857                                         address = uaddr2taddr(nconf, ua);
 858                                 }
 859                                 xdr_free((xdrproc_t)xdr_wrapstring,
 860                                     (char *)&ua);
 861 
 862                                 if (address != NULL) {
 863                                         goto done;
 864                                 }
 865                                 /* NULL universal address */
 866                                 /* But client call was successful */
 867                                 clnt_geterr(client, &rpc_createerr.cf_error);
 868                                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
 869                                 goto error;
 870                         }
 871 #ifndef PORTMAP
 872                         clnt_geterr(client, &rpc_createerr.cf_error);
 873                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 874                         goto error;


 875 #endif
 876                         /* Try the next version */
 877                         break;
 878                 case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
 879                         clnt_geterr(client, &rpc_createerr.cf_error);
 880 #ifdef PORTMAP
 881                         if (rpc_createerr.cf_error.re_vers.low > vers) {
 882                                 rpc_createerr.cf_stat = clnt_st;
 883                                 goto error;  /* a new version, can't handle */
 884                         }
 885 #else
 886                         rpc_createerr.cf_stat = clnt_st;
 887                         goto error;
 888 #endif
 889                         /* Try the next version */
 890                         break;
 891 #ifdef PORTMAP
 892                 case RPC_PROCUNAVAIL: /* Procedure unavailable */
 893                 case RPC_PROGUNAVAIL: /* Program not available */
 894                 case RPC_TIMEDOUT: /* Call timed out */
 895                         /* Try the next version */
 896                         break;
 897 #endif
 898                 default:
 899                         clnt_geterr(client, &rpc_createerr.cf_error);
 900                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
 901                         goto error;
 902                         break;
 903                 }
 904         } /* End of version 3 */
 905 #ifndef PORTMAP
 906         /* cf_error members set by creation failure */
 907         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
 908 #endif
 909         /*
 910          * Try version 2
 911          */
 912 
 913 #ifdef PORTMAP
 914         /* Try version 2 for TCP or UDP */
 915         if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
 916                 ushort_t port = 0;
 917                 struct netbuf remote;
 918                 uint_t pmapvers = 2;
 919                 struct pmap pmapparms;
 920 
 921                 /*
 922                  * Try UDP only - there are some portmappers out
 923                  * there that use UDP only.
 924                  */
 925                 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
 926                         struct netconfig *newnconf;
 927 
 928                         if (client != NULL) {
 929                                 CLNT_DESTROY(client);
 930                                 client = NULL;
 931                                 free(parms.r_addr);
 932                                 parms.r_addr = NULL;
 933                         }
 934                         if ((handle = __rpc_setconf("udp")) == NULL) {
 935                                 /* Construct an unknown protocol error */
 936                                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 937                                 goto error;
 938                         }
 939 
 940                         /*
 941                          * The following to reinforce that you can
 942                          * only request for remote address through
 943                          * the same transport you are requesting.
 944                          * ie. requesting unversial address
 945                          * of IPv4 has to be carried through IPv4.
 946                          * Can't use IPv6 to send out the request.
 947                          * The mergeaddr in rpcbind can't handle
 948                          * this.
 949                          */
 950                         for (;;) {
 951                                 if ((newnconf = __rpc_getconf(handle))
 952                                     == NULL) {
 953                                         __rpc_endconf(handle);
 954                                         /*
 955                                          * Construct an unknown protocol
 956                                          * error
 957                                          */
 958                                         rpc_createerr.cf_stat =
 959                                             RPC_UNKNOWNPROTO;
 960                                         goto error;
 961                                 }
 962                                 /*
 963                                  * here check the protocol family to
 964                                  * be consistent with the request one
 965                                  */
 966                                 if (strcmp(newnconf->nc_protofmly,
 967                                     nconf->nc_protofmly) == 0)
 968                                         break;
 969                         }
 970 
 971                         /* Sets rpc_createerr.cf_error members on failure */
 972                         client = _getclnthandle_timed(host, newnconf,
 973                             &parms.r_addr, tp);
 974                         __rpc_endconf(handle);
 975                         tmp_client = TRUE;
 976                 }
 977                 if (client == NULL) {
 978                         /*
 979                          * rpc_createerr. cf_error members were set by
 980                          * creation failure
 981                          */
 982                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
 983                         tmp_client = FALSE;
 984                         goto error;
 985                 }
 986 
 987                 /*
 988                  * Set version and retry timeout.
 989                  */
 990                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
 991                 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
 992 
 993                 pmapparms.pm_prog = program;
 994                 pmapparms.pm_vers = version;
 995                 pmapparms.pm_prot = (strcmp(nconf->nc_proto, NC_TCP) != 0) ?
 996                     IPPROTO_UDP : IPPROTO_TCP;
 997                 pmapparms.pm_port = 0;  /* not needed */
 998 
 999                 /* Sets error structure members in client handle */
1000                 clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
1001                     (xdrproc_t)xdr_pmap, (caddr_t)&pmapparms,
1002                     (xdrproc_t)xdr_u_short, (caddr_t)&port, *tp);
1003 
1004                 if (clnt_st != RPC_SUCCESS) {
1005                         clnt_geterr(client, &rpc_createerr.cf_error);
1006                         rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1007                         goto error;
1008                 } else if (port == 0) {
1009                         /* Will be NULL universal address */
1010                         /* But client call was successful */
1011                         clnt_geterr(client, &rpc_createerr.cf_error);
1012                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1013                         goto error;
1014                 }
1015                 port = htons(port);
1016                 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
1017                 if (((address = malloc(sizeof (struct netbuf))) == NULL) ||
1018                     ((address->buf = malloc(remote.len)) == NULL)) {
1019                         /* Construct a system error */
1020                         rpc_createerr.cf_error.re_errno = errno;
1021                         rpc_createerr.cf_error.re_terrno = 0;
1022                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1023                         free(address);
1024                         address = NULL;
1025                         goto error;
1026                 }
1027                 (void) memcpy(address->buf, remote.buf, remote.len);
1028                 (void) memcpy(&address->buf[sizeof (short)], &port,
1029                     sizeof (short));
1030                 address->len = address->maxlen = remote.len;
1031                 goto done;
1032         } else {
1033                 /*
1034                  * This is not NC_INET.
1035                  * Always an error for version 2.
1036                  */
1037                 if (client != NULL && clnt_st != RPC_SUCCESS) {
1038                         /* There is a client that failed */
1039                         clnt_geterr(client, &rpc_createerr.cf_error);
1040                         rpc_createerr.cf_stat = clnt_st;
1041                 } else {
1042                         /* Something else */
1043                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1044                         /*
1045                          * Setting rpc_createerr.cf_stat is sufficient.
1046                          * No details in rpc_createerr.cf_error needed.
1047                          */
1048                 }
1049         }
1050 #endif
1051 
1052 error:
1053         /* Return NULL address and NULL client */
1054         address = NULL;
1055         if (client != NULL) {
1056                 CLNT_DESTROY(client);
1057                 client = NULL;
1058         }
1059 
1060 done:
1061         /* Return an address and optional client */
1062         if (client != NULL && tmp_client) {
1063                 /* This client is the temporary one */
1064                 CLNT_DESTROY(client);
1065                 client = NULL;
1066         }
1067         if (clpp != NULL) {

1068                 *clpp = client;
1069         } else if (client != NULL) {
1070                 CLNT_DESTROY(client);
1071         }

1072         free(parms.r_addr);
1073         return (address);
1074 }
1075 
1076 
1077 /*
1078  * Find the mapped address for program, version.
1079  * Calls the rpcbind service remotely to do the lookup.
1080  * Uses the transport specified in nconf.
1081  * Returns FALSE (0) if no map exists, else returns 1.
1082  *
1083  * Assuming that the address is all properly allocated
1084  */
1085 int
1086 rpcb_getaddr(const rpcprog_t program, const rpcvers_t version,
1087         const struct netconfig *nconf, struct netbuf *address, const char *host)
1088 {
1089         struct netbuf *na;
1090 
1091         if ((na = __rpcb_findaddr_timed(program, version,