Home | History | Annotate | Line # | Download | only in rpc
rpcb_clnt.c revision 1.28
      1 /*	$NetBSD: rpcb_clnt.c,v 1.28 2013/03/05 19:55:22 christos Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 /*
     32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     33  */
     34 
     35 /* #ident	"@(#)rpcb_clnt.c	1.27	94/04/24 SMI" */
     36 
     37 #include <sys/cdefs.h>
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 #if 0
     40 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
     41 #else
     42 __RCSID("$NetBSD: rpcb_clnt.c,v 1.28 2013/03/05 19:55:22 christos Exp $");
     43 #endif
     44 #endif
     45 
     46 /*
     47  * rpcb_clnt.c
     48  * interface to rpcbind rpc service.
     49  *
     50  * Copyright (C) 1988, Sun Microsystems, Inc.
     51  */
     52 
     53 #include "namespace.h"
     54 #include "reentrant.h"
     55 #include <sys/types.h>
     56 #include <sys/socket.h>
     57 #include <sys/un.h>
     58 #include <sys/utsname.h>
     59 #include <rpc/rpc.h>
     60 #include <rpc/rpcb_prot.h>
     61 #include <rpc/nettype.h>
     62 #include <netconfig.h>
     63 #ifdef PORTMAP
     64 #include <netinet/in.h>		/* FOR IPPROTO_TCP/UDP definitions */
     65 #include <rpc/pmap_prot.h>
     66 #endif
     67 #include <assert.h>
     68 #include <errno.h>
     69 #include <netdb.h>
     70 #include <stdio.h>
     71 #include <stdlib.h>
     72 #include <string.h>
     73 #include <syslog.h>
     74 #include <unistd.h>
     75 
     76 #include "svc_fdset.h"
     77 #include "rpc_internal.h"
     78 
     79 #ifdef __weak_alias
     80 __weak_alias(rpcb_set,_rpcb_set)
     81 __weak_alias(rpcb_unset,_rpcb_unset)
     82 __weak_alias(rpcb_getmaps,_rpcb_getmaps)
     83 __weak_alias(rpcb_taddr2uaddr,_rpcb_taddr2uaddr)
     84 __weak_alias(rpcb_uaddr2taddr,_rpcb_uaddr2taddr)
     85 #endif
     86 
     87 static struct timeval tottimeout = { 60, 0 };
     88 static const struct timeval rmttimeout = { 3, 0 };
     89 
     90 static const char nullstring[] = "\000";
     91 
     92 #define	CACHESIZE 6
     93 
     94 struct address_cache {
     95 	char *ac_host;
     96 	char *ac_netid;
     97 	char *ac_uaddr;
     98 	struct netbuf *ac_taddr;
     99 	struct address_cache *ac_next;
    100 };
    101 
    102 static struct address_cache *front;
    103 static int cachesize;
    104 
    105 #define	CLCR_GET_RPCB_TIMEOUT	1
    106 #define	CLCR_SET_RPCB_TIMEOUT	2
    107 
    108 
    109 extern int __rpc_lowvers;
    110 
    111 static struct address_cache *check_cache(const char *, const char *);
    112 static void delete_cache(struct netbuf *);
    113 static void add_cache(const char *, const char *, struct netbuf *, char *);
    114 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
    115 static CLIENT *local_rpcb(void);
    116 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
    117 
    118 /*
    119  * This routine adjusts the timeout used for calls to the remote rpcbind.
    120  * Also, this routine can be used to set the use of portmapper version 2
    121  * only when doing rpc_broadcasts
    122  * These are private routines that may not be provided in future releases.
    123  */
    124 bool_t
    125 __rpc_control(int request, void *info)
    126 {
    127 
    128 	_DIAGASSERT(info != NULL);
    129 
    130 	switch (request) {
    131 	case CLCR_GET_RPCB_TIMEOUT:
    132 		*(struct timeval *)info = tottimeout;
    133 		break;
    134 	case CLCR_SET_RPCB_TIMEOUT:
    135 		tottimeout = *(struct timeval *)info;
    136 		break;
    137 	case CLCR_SET_LOWVERS:
    138 		__rpc_lowvers = *(int *)info;
    139 		break;
    140 	case CLCR_GET_LOWVERS:
    141 		*(int *)info = __rpc_lowvers;
    142 		break;
    143 	default:
    144 		return (FALSE);
    145 	}
    146 	return (TRUE);
    147 }
    148 
    149 /*
    150  *	It might seem that a reader/writer lock would be more reasonable here.
    151  *	However because getclnthandle(), the only user of the cache functions,
    152  *	may do a delete_cache() operation if a check_cache() fails to return an
    153  *	address useful to clnt_tli_create(), we may as well use a mutex.
    154  */
    155 /*
    156  * As it turns out, if the cache lock is *not* a reader/writer lock, we will
    157  * block all clnt_create's if we are trying to connect to a host that's down,
    158  * since the lock will be held all during that time.
    159  */
    160 #ifdef _REENTRANT
    161 extern rwlock_t	rpcbaddr_cache_lock;
    162 #endif
    163 
    164 /*
    165  * The routines check_cache(), add_cache(), delete_cache() manage the
    166  * cache of rpcbind addresses for (host, netid).
    167  */
    168 
    169 static struct address_cache *
    170 check_cache(const char *host, const char *netid)
    171 {
    172 	struct address_cache *cptr;
    173 
    174 	_DIAGASSERT(host != NULL);
    175 	_DIAGASSERT(netid != NULL);
    176 
    177 	/* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
    178 
    179 	for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
    180 		if (!strcmp(cptr->ac_host, host) &&
    181 		    !strcmp(cptr->ac_netid, netid)) {
    182 #ifdef ND_DEBUG
    183 			fprintf(stderr, "Found cache entry for %s: %s\n",
    184 				host, netid);
    185 #endif
    186 			return (cptr);
    187 		}
    188 	}
    189 	return NULL;
    190 }
    191 
    192 static void
    193 delete_cache(struct netbuf *addr)
    194 {
    195 	struct address_cache *cptr, *prevptr = NULL;
    196 
    197 	_DIAGASSERT(addr != NULL);
    198 
    199 	/* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
    200 	for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
    201 		if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
    202 			free(cptr->ac_host);
    203 			free(cptr->ac_netid);
    204 			free(cptr->ac_taddr->buf);
    205 			free(cptr->ac_taddr);
    206 			if (cptr->ac_uaddr)
    207 				free(cptr->ac_uaddr);
    208 			if (prevptr)
    209 				prevptr->ac_next = cptr->ac_next;
    210 			else
    211 				front = cptr->ac_next;
    212 			free(cptr);
    213 			cachesize--;
    214 			break;
    215 		}
    216 		prevptr = cptr;
    217 	}
    218 }
    219 
    220 static void
    221 add_cache(const char *host, const char *netid, struct netbuf *taddr,
    222 	char *uaddr)
    223 {
    224 	struct address_cache  *ad_cache, *cptr, *prevptr;
    225 
    226 	_DIAGASSERT(host != NULL);
    227 	_DIAGASSERT(netid != NULL);
    228 	/* uaddr may be NULL */
    229 	/* taddr may be NULL ??? */
    230 
    231 	ad_cache = malloc(sizeof(*ad_cache));
    232 	if (!ad_cache) {
    233 		return;
    234 	}
    235 	ad_cache->ac_host = strdup(host);
    236 	ad_cache->ac_netid = strdup(netid);
    237 	ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
    238 	ad_cache->ac_taddr = malloc(sizeof(*ad_cache->ac_taddr));
    239 	if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
    240 		(uaddr && !ad_cache->ac_uaddr)) {
    241 		goto out;
    242 	}
    243 	ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
    244 	ad_cache->ac_taddr->buf = malloc(taddr->len);
    245 	if (ad_cache->ac_taddr->buf == NULL) {
    246 out:
    247 		if (ad_cache->ac_host)
    248 			free(ad_cache->ac_host);
    249 		if (ad_cache->ac_netid)
    250 			free(ad_cache->ac_netid);
    251 		if (ad_cache->ac_uaddr)
    252 			free(ad_cache->ac_uaddr);
    253 		if (ad_cache->ac_taddr)
    254 			free(ad_cache->ac_taddr);
    255 		free(ad_cache);
    256 		return;
    257 	}
    258 	memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
    259 #ifdef ND_DEBUG
    260 	fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
    261 #endif
    262 
    263 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
    264 
    265 	rwlock_wrlock(&rpcbaddr_cache_lock);
    266 	if (cachesize < CACHESIZE) {
    267 		ad_cache->ac_next = front;
    268 		front = ad_cache;
    269 		cachesize++;
    270 	} else {
    271 		/* Free the last entry */
    272 		cptr = front;
    273 		prevptr = NULL;
    274 		while (cptr->ac_next) {
    275 			prevptr = cptr;
    276 			cptr = cptr->ac_next;
    277 		}
    278 
    279 #ifdef ND_DEBUG
    280 		fprintf(stderr, "Deleted from cache: %s : %s\n",
    281 			cptr->ac_host, cptr->ac_netid);
    282 #endif
    283 		free(cptr->ac_host);
    284 		free(cptr->ac_netid);
    285 		free(cptr->ac_taddr->buf);
    286 		free(cptr->ac_taddr);
    287 		if (cptr->ac_uaddr)
    288 			free(cptr->ac_uaddr);
    289 
    290 		if (prevptr) {
    291 			prevptr->ac_next = NULL;
    292 			ad_cache->ac_next = front;
    293 			front = ad_cache;
    294 		} else {
    295 			front = ad_cache;
    296 			ad_cache->ac_next = NULL;
    297 		}
    298 		free(cptr);
    299 	}
    300 	rwlock_unlock(&rpcbaddr_cache_lock);
    301 }
    302 
    303 /*
    304  * This routine will return a client handle that is connected to the
    305  * rpcbind. Returns NULL on error and free's everything.
    306  */
    307 static CLIENT *
    308 getclnthandle(const char *host, const struct netconfig *nconf, char **targaddr)
    309 {
    310 	CLIENT *client;
    311 	struct netbuf *addr, taddr;
    312 	struct netbuf addr_to_delete;
    313 	struct __rpc_sockinfo si;
    314 	struct addrinfo hints, *res, *tres;
    315 	struct address_cache *ad_cache;
    316 	char *tmpaddr;
    317 
    318 	_DIAGASSERT(host != NULL);
    319 	_DIAGASSERT(nconf != NULL);
    320 	/* targaddr may be NULL */
    321 
    322 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  ad_cache */
    323 
    324 	/* Get the address of the rpcbind.  Check cache first */
    325 	client = NULL;
    326 	addr_to_delete.len = 0;
    327 	addr_to_delete.buf = NULL;
    328 	rwlock_rdlock(&rpcbaddr_cache_lock);
    329 	ad_cache = check_cache(host, nconf->nc_netid);
    330 	if (ad_cache != NULL) {
    331 		addr = ad_cache->ac_taddr;
    332 		client = clnt_tli_create(RPC_ANYFD, nconf, addr,
    333 		    (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
    334 		if (client != NULL) {
    335 			if (targaddr)
    336 				*targaddr = ad_cache->ac_uaddr;
    337 			rwlock_unlock(&rpcbaddr_cache_lock);
    338 			return (client);
    339 		}
    340 		addr_to_delete.len = addr->len;
    341 		addr_to_delete.buf = malloc(addr->len);
    342 		if (addr_to_delete.buf == NULL) {
    343 			addr_to_delete.len = 0;
    344 		} else {
    345 			memcpy(addr_to_delete.buf, addr->buf, addr->len);
    346 		}
    347 	}
    348 	rwlock_unlock(&rpcbaddr_cache_lock);
    349 	if (addr_to_delete.len != 0) {
    350 		/*
    351 		 * Assume this may be due to cache data being
    352 		 *  outdated
    353 		 */
    354 		rwlock_wrlock(&rpcbaddr_cache_lock);
    355 		delete_cache(&addr_to_delete);
    356 		rwlock_unlock(&rpcbaddr_cache_lock);
    357 		free(addr_to_delete.buf);
    358 	}
    359 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
    360 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    361 		return NULL;
    362 	}
    363 
    364 	memset(&hints, 0, sizeof hints);
    365 	hints.ai_family = si.si_af;
    366 	hints.ai_socktype = si.si_socktype;
    367 	hints.ai_protocol = si.si_proto;
    368 
    369 #ifdef CLNT_DEBUG
    370 	printf("trying netid %s family %d proto %d socktype %d\n",
    371 	    nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
    372 #endif
    373 
    374 	if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
    375 		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
    376 		return NULL;
    377 	}
    378 
    379 	for (tres = res; tres != NULL; tres = tres->ai_next) {
    380 		taddr.buf = tres->ai_addr;
    381 		taddr.len = taddr.maxlen = tres->ai_addrlen;
    382 
    383 #ifdef ND_DEBUG
    384 		{
    385 			char *ua;
    386 
    387 			ua = taddr2uaddr(nconf, &taddr);
    388 			fprintf(stderr, "Got it [%s]\n", ua);
    389 			free(ua);
    390 		}
    391 #endif
    392 
    393 #ifdef ND_DEBUG
    394 		{
    395 			int i;
    396 
    397 			fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
    398 				taddr.len, taddr.maxlen);
    399 			fprintf(stderr, "\tAddress is ");
    400 			for (i = 0; i < taddr.len; i++)
    401 				fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
    402 			fprintf(stderr, "\n");
    403 		}
    404 #endif
    405 		client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
    406 		    (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
    407 #ifdef ND_DEBUG
    408 		if (! client) {
    409 			clnt_pcreateerror("rpcbind clnt interface");
    410 		}
    411 #endif
    412 
    413 		if (client) {
    414 			tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
    415 			add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
    416 			if (targaddr)
    417 				*targaddr = tmpaddr;
    418 			break;
    419 		}
    420 	}
    421 	freeaddrinfo(res);
    422 	return (client);
    423 }
    424 
    425 /* XXX */
    426 #define IN4_LOCALHOST_STRING	"127.0.0.1"
    427 #define IN6_LOCALHOST_STRING	"::1"
    428 
    429 /*
    430  * This routine will return a client handle that is connected to the local
    431  * rpcbind. Returns NULL on error and free's everything.
    432  */
    433 static CLIENT *
    434 local_rpcb(void)
    435 {
    436 	CLIENT *client;
    437 	static struct netconfig *loopnconf;
    438 	static const char *hostname;
    439 #ifdef _REENTRANT
    440 	extern mutex_t loopnconf_lock;
    441 #endif
    442 	int sock;
    443 	size_t tsize;
    444 	struct netbuf nbuf;
    445 	struct sockaddr_un sun;
    446 
    447 	/*
    448 	 * Try connecting to the local rpcbind through a local socket
    449 	 * first. If this doesn't work, try all transports defined in
    450 	 * the netconfig file.
    451 	 */
    452 	memset(&sun, 0, sizeof sun);
    453 	sock = socket(AF_LOCAL, SOCK_STREAM, 0);
    454 	if (sock < 0)
    455 		goto try_nconf;
    456 	sun.sun_family = AF_LOCAL;
    457 	strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
    458 	tsize = SUN_LEN(&sun);
    459 	_DIAGASSERT(__type_fit(uint8_t, tsize));
    460 	nbuf.len = sun.sun_len = (uint8_t)tsize;
    461 	nbuf.maxlen = sizeof (struct sockaddr_un);
    462 	nbuf.buf = &sun;
    463 
    464 	tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
    465 	_DIAGASSERT(__type_fit(u_int, tsize));
    466 	client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
    467 	    (rpcvers_t)RPCBVERS, (u_int)tsize, (u_int)tsize);
    468 
    469 	if (client != NULL) {
    470 		/* XXX - mark the socket to be closed in destructor */
    471 		(void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
    472 		return client;
    473 	}
    474 
    475 	/* XXX - nobody needs this socket anymore, free the descriptor */
    476 	close(sock);
    477 
    478 try_nconf:
    479 
    480 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
    481 	mutex_lock(&loopnconf_lock);
    482 	if (loopnconf == NULL) {
    483 		struct netconfig *nconf, *tmpnconf = NULL;
    484 		void *nc_handle;
    485 		int fd;
    486 
    487 		nc_handle = setnetconfig();
    488 		if (nc_handle == NULL) {
    489 			/* fails to open netconfig file */
    490 			syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
    491 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    492 			mutex_unlock(&loopnconf_lock);
    493 			return (NULL);
    494 		}
    495 		while ((nconf = getnetconfig(nc_handle)) != NULL) {
    496 #ifdef INET6
    497 			if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
    498 #else
    499 			if ((
    500 #endif
    501 			     strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
    502 			    (nconf->nc_semantics == NC_TPI_COTS ||
    503 			     nconf->nc_semantics == NC_TPI_COTS_ORD)) {
    504 				fd = __rpc_nconf2fd(nconf);
    505 				/*
    506 				 * Can't create a socket, assume that
    507 				 * this family isn't configured in the kernel.
    508 				 */
    509 				if (fd < 0)
    510 					continue;
    511 				close(fd);
    512 				tmpnconf = nconf;
    513 				if (!strcmp(nconf->nc_protofmly, NC_INET))
    514 					hostname = IN4_LOCALHOST_STRING;
    515 				else
    516 					hostname = IN6_LOCALHOST_STRING;
    517 			}
    518 		}
    519 		if (tmpnconf == NULL) {
    520 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    521 			mutex_unlock(&loopnconf_lock);
    522 			return (NULL);
    523 		}
    524 		loopnconf = getnetconfigent(tmpnconf->nc_netid);
    525 		/* loopnconf is never freed */
    526 		endnetconfig(nc_handle);
    527 	}
    528 	mutex_unlock(&loopnconf_lock);
    529 	client = getclnthandle(hostname, loopnconf, NULL);
    530 	return (client);
    531 }
    532 
    533 /*
    534  * Set a mapping between program, version and address.
    535  * Calls the rpcbind service to do the mapping.
    536  */
    537 bool_t
    538 rpcb_set(rpcprog_t program, rpcvers_t version,
    539 	const struct netconfig *nconf,	/* Network structure of transport */
    540 	const struct netbuf *address)	/* Services netconfig address */
    541 {
    542 	CLIENT *client;
    543 	bool_t rslt = FALSE;
    544 	RPCB parms;
    545 	char uidbuf[32];
    546 
    547 	/* parameter checking */
    548 	if (nconf == NULL) {
    549 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    550 		return (FALSE);
    551 	}
    552 	if (address == NULL) {
    553 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
    554 		return (FALSE);
    555 	}
    556 	client = local_rpcb();
    557 	if (! client) {
    558 		return (FALSE);
    559 	}
    560 
    561 	/* convert to universal */
    562 	parms.r_addr = taddr2uaddr(__UNCONST(nconf), __UNCONST(address));
    563 	if (!parms.r_addr) {
    564 		CLNT_DESTROY(client);
    565 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
    566 		return (FALSE); /* no universal address */
    567 	}
    568 	parms.r_prog = program;
    569 	parms.r_vers = version;
    570 	parms.r_netid = nconf->nc_netid;
    571 	/*
    572 	 * Though uid is not being used directly, we still send it for
    573 	 * completeness.  For non-unix platforms, perhaps some other
    574 	 * string or an empty string can be sent.
    575 	 */
    576 	(void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
    577 	parms.r_owner = uidbuf;
    578 
    579 	CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
    580 	    (char *)(void *)&parms, (xdrproc_t) xdr_bool,
    581 	    (char *)(void *)&rslt, tottimeout);
    582 
    583 	CLNT_DESTROY(client);
    584 	free(parms.r_addr);
    585 	return (rslt);
    586 }
    587 
    588 /*
    589  * Remove the mapping between program, version and netbuf address.
    590  * Calls the rpcbind service to do the un-mapping.
    591  * If netbuf is NULL, unset for all the transports, otherwise unset
    592  * only for the given transport.
    593  */
    594 bool_t
    595 rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
    596 {
    597 	CLIENT *client;
    598 	bool_t rslt = FALSE;
    599 	RPCB parms;
    600 	char uidbuf[32];
    601 
    602 	client = local_rpcb();
    603 	if (! client) {
    604 		return (FALSE);
    605 	}
    606 
    607 	parms.r_prog = program;
    608 	parms.r_vers = version;
    609 	if (nconf)
    610 		parms.r_netid = nconf->nc_netid;
    611 	else {
    612 		parms.r_netid = __UNCONST(&nullstring[0]); /* unsets  all */
    613 	}
    614 	parms.r_addr = __UNCONST(&nullstring[0]);
    615 	(void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
    616 	parms.r_owner = uidbuf;
    617 
    618 	CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
    619 	    (char *)(void *)&parms, (xdrproc_t) xdr_bool,
    620 	    (char *)(void *)&rslt, tottimeout);
    621 
    622 	CLNT_DESTROY(client);
    623 	return (rslt);
    624 }
    625 
    626 /*
    627  * From the merged list, find the appropriate entry
    628  */
    629 static struct netbuf *
    630 got_entry(rpcb_entry_list_ptr relp, const struct netconfig *nconf)
    631 {
    632 	struct netbuf *na = NULL;
    633 	rpcb_entry_list_ptr sp;
    634 	rpcb_entry *rmap;
    635 
    636 	_DIAGASSERT(nconf != NULL);
    637 
    638 	for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
    639 		rmap = &sp->rpcb_entry_map;
    640 		if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
    641 		    (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
    642 		    (nconf->nc_semantics == rmap->r_nc_semantics) &&
    643 		    (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
    644 			na = uaddr2taddr(nconf, rmap->r_maddr);
    645 #ifdef ND_DEBUG
    646 			fprintf(stderr, "\tRemote address is [%s].\n",
    647 				rmap->r_maddr);
    648 			if (!na)
    649 				fprintf(stderr,
    650 				    "\tCouldn't resolve remote address!\n");
    651 #endif
    652 			break;
    653 		}
    654 	}
    655 	return (na);
    656 }
    657 
    658 /*
    659  * An internal function which optimizes rpcb_getaddr function.  It also
    660  * returns the client handle that it uses to contact the remote rpcbind.
    661  *
    662  * The algorithm used: If the transports is TCP or UDP, it first tries
    663  * version 2 (portmap), 4 and then 3 (svr4).  This order should be
    664  * changed in the next OS release to 4, 2 and 3.  We are assuming that by
    665  * that time, version 4 would be available on many machines on the network.
    666  * With this algorithm, we get performance as well as a plan for
    667  * obsoleting version 2.
    668  *
    669  * For all other transports, the algorithm remains as 4 and then 3.
    670  *
    671  * XXX: Due to some problems with t_connect(), we do not reuse the same client
    672  * handle for COTS cases and hence in these cases we do not return the
    673  * client handle.  This code will change if t_connect() ever
    674  * starts working properly.  Also look under clnt_vc.c.
    675  */
    676 struct netbuf *
    677 __rpcb_findaddr(rpcprog_t program, rpcvers_t version,
    678 	const struct netconfig *nconf, const char *host, CLIENT **clpp)
    679 {
    680 	CLIENT *client = NULL;
    681 	RPCB parms;
    682 	enum clnt_stat clnt_st;
    683 	char *ua = NULL;
    684 	rpcvers_t vers;
    685 	struct netbuf *address = NULL;
    686 	rpcvers_t start_vers = RPCBVERS4;
    687 	struct netbuf servaddr;
    688 
    689 	/* nconf is handled below */
    690 	_DIAGASSERT(host != NULL);
    691 	/* clpp may be NULL */
    692 
    693 	/* parameter checking */
    694 	if (nconf == NULL) {
    695 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    696 		return (NULL);
    697 	}
    698 
    699 	parms.r_addr = NULL;
    700 
    701 #ifdef PORTMAP
    702 	/* Try version 2 for TCP or UDP */
    703 	if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
    704 		u_short port = 0;
    705 		struct netbuf remote;
    706 		rpcvers_t pmapvers = 2;
    707 		struct pmap pmapparms;
    708 
    709 		/*
    710 		 * Try UDP only - there are some portmappers out
    711 		 * there that use UDP only.
    712 		 */
    713 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
    714 			struct netconfig *newnconf;
    715 
    716 			if ((newnconf = getnetconfigent("udp")) == NULL) {
    717 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    718 				return (NULL);
    719 			}
    720 			client = getclnthandle(host, newnconf, &parms.r_addr);
    721 			freenetconfigent(newnconf);
    722 		} else {
    723 			client = getclnthandle(host, nconf, &parms.r_addr);
    724 		}
    725 		if (client == NULL) {
    726 			return (NULL);
    727 		}
    728 
    729 		/* Set the version */
    730 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&pmapvers);
    731 		pmapparms.pm_prog = program;
    732 		pmapparms.pm_vers = version;
    733 		pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
    734 					IPPROTO_UDP : IPPROTO_TCP;
    735 		pmapparms.pm_port = 0;	/* not needed */
    736 		clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
    737 		    (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
    738 		    (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
    739 		    tottimeout);
    740 		if (clnt_st != RPC_SUCCESS) {
    741 			if ((clnt_st == RPC_PROGVERSMISMATCH) ||
    742 				(clnt_st == RPC_PROGUNAVAIL))
    743 				goto try_rpcbind; /* Try different versions */
    744 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
    745 			clnt_geterr(client, &rpc_createerr.cf_error);
    746 			goto error;
    747 		} else if (port == 0) {
    748 			address = NULL;
    749 			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
    750 			goto error;
    751 		}
    752 		port = htons(port);
    753 		CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&remote);
    754 		if (((address = malloc(sizeof(struct netbuf))) == NULL) ||
    755 		    ((address->buf = malloc(remote.len)) == NULL)) {
    756 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    757 			clnt_geterr(client, &rpc_createerr.cf_error);
    758 			if (address) {
    759 				free(address);
    760 				address = NULL;
    761 			}
    762 			goto error;
    763 		}
    764 		memcpy(address->buf, remote.buf, remote.len);
    765 		memcpy(&((char *)address->buf)[sizeof (short)],
    766 				(char *)(void *)&port, sizeof (short));
    767 		address->len = address->maxlen = remote.len;
    768 		goto done;
    769 	}
    770 #endif
    771 
    772 try_rpcbind:
    773 	/*
    774 	 * Now we try version 4 and then 3.
    775 	 * We also send the remote system the address we used to
    776 	 * contact it in case it can help to connect back with us
    777 	 */
    778 	parms.r_prog = program;
    779 	parms.r_vers = version;
    780 	parms.r_owner = __UNCONST(&nullstring[0]);	/* not needed; */
    781 							/* just for xdring */
    782 	parms.r_netid = nconf->nc_netid; /* not really needed */
    783 
    784 	/*
    785 	 * If a COTS transport is being used, try getting address via CLTS
    786 	 * transport.  This works only with version 4.
    787 	 * NOTE: This is being done for all transports EXCEPT LOOPBACK
    788 	 * because with loopback the cost to go to a COTS is same as
    789 	 * the cost to go through CLTS, plus you get the advantage of
    790 	 * finding out immediately if the local rpcbind process is dead.
    791 	 */
    792 #if 1
    793 	if ((nconf->nc_semantics == NC_TPI_COTS_ORD ||
    794 			nconf->nc_semantics == NC_TPI_COTS) &&
    795 	    (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0))
    796 #else
    797 	if (client != NULL) {
    798 		CLNT_DESTROY(client);
    799 		client = NULL;
    800 	}
    801 	if (nconf->nc_semantics == NC_TPI_CLTS)
    802 #endif
    803 	{
    804 		void *handle;
    805 		struct netconfig *nconf_clts;
    806 		rpcb_entry_list_ptr relp = NULL;
    807 
    808 		if (client == NULL) {
    809 			/* This did not go through the above PORTMAP/TCP code */
    810 #if 1
    811 			if ((handle = __rpc_setconf("datagram_v")) != NULL)
    812 #else
    813 			if ((handle = __rpc_setconf("circuit_v")) != NULL)
    814 #endif
    815 			{
    816 				while ((nconf_clts = __rpc_getconf(handle))
    817 					!= NULL) {
    818 					if (strcmp(nconf_clts->nc_protofmly,
    819 						nconf->nc_protofmly) != 0) {
    820 						continue;
    821 					}
    822 					client = getclnthandle(host, nconf_clts,
    823 							&parms.r_addr);
    824 					break;
    825 				}
    826 				__rpc_endconf(handle);
    827 			}
    828 			if (client == NULL)
    829 				goto regular_rpcbind;	/* Go the regular way */
    830 		} else {
    831 			/* This is a UDP PORTMAP handle.  Change to version 4 */
    832 			vers = RPCBVERS4;
    833 			CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
    834 		}
    835 		/*
    836 		 * We also send the remote system the address we used to
    837 		 * contact it in case it can help it connect back with us
    838 		 */
    839 		if (parms.r_addr == NULL) {
    840 			/* for XDRing */
    841 			parms.r_addr = __UNCONST(&nullstring[0]);
    842 		}
    843 		clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
    844 		    (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
    845 		    (xdrproc_t) xdr_rpcb_entry_list_ptr,
    846 		    (char *)(void *)&relp, tottimeout);
    847 		if (clnt_st == RPC_SUCCESS) {
    848 			if ((address = got_entry(relp, nconf)) != NULL) {
    849 				xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
    850 				    (char *)(void *)&relp);
    851 				CLNT_CONTROL(client, CLGET_SVC_ADDR,
    852 					(char *)(void *)&servaddr);
    853 				__rpc_fixup_addr(address, &servaddr);
    854 				goto done;
    855 			}
    856 			/* Entry not found for this transport */
    857 			xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
    858 			    (char *)(void *)&relp);
    859 			/*
    860 			 * XXX: should have perhaps returned with error but
    861 			 * since the remote machine might not always be able
    862 			 * to send the address on all transports, we try the
    863 			 * regular way with regular_rpcbind
    864 			 */
    865 			goto regular_rpcbind;
    866 		} else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
    867 			(clnt_st == RPC_PROGUNAVAIL)) {
    868 			start_vers = RPCBVERS;	/* Try version 3 now */
    869 			goto regular_rpcbind; /* Try different versions */
    870 		} else {
    871 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
    872 			clnt_geterr(client, &rpc_createerr.cf_error);
    873 			goto error;
    874 		}
    875 	}
    876 
    877 regular_rpcbind:
    878 
    879 	/* Now the same transport is to be used to get the address */
    880 #if 1
    881 	if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
    882 			(nconf->nc_semantics == NC_TPI_COTS)))
    883 #else
    884 	if (client && nconf->nc_semantics == NC_TPI_CLTS)
    885 #endif
    886 	{
    887 		/* A CLTS type of client - destroy it */
    888 		CLNT_DESTROY(client);
    889 		client = NULL;
    890 	}
    891 
    892 	if (client == NULL) {
    893 		client = getclnthandle(host, nconf, &parms.r_addr);
    894 		if (client == NULL) {
    895 			goto error;
    896 		}
    897 	}
    898 	if (parms.r_addr == NULL)
    899 		parms.r_addr = __UNCONST(&nullstring[0]);
    900 
    901 	/* First try from start_vers and then version 3 (RPCBVERS) */
    902 	for (vers = start_vers;  vers >= RPCBVERS; vers--) {
    903 		/* Set the version */
    904 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
    905 		clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
    906 		    (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
    907 		    (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua,
    908 		    tottimeout);
    909 		if (clnt_st == RPC_SUCCESS) {
    910 			if ((ua == NULL) || (ua[0] == 0)) {
    911 				/* address unknown */
    912 				rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
    913 				goto error;
    914 			}
    915 			address = uaddr2taddr(nconf, ua);
    916 #ifdef ND_DEBUG
    917 			fprintf(stderr, "\tRemote address is [%s]\n", ua);
    918 			if (!address)
    919 				fprintf(stderr,
    920 					"\tCouldn't resolve remote address!\n");
    921 #endif
    922 			xdr_free((xdrproc_t)xdr_wrapstring,
    923 			    (char *)(void *)&ua);
    924 
    925 			if (! address) {
    926 				/* We don't know about your universal address */
    927 				rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
    928 				goto error;
    929 			}
    930 			CLNT_CONTROL(client, CLGET_SVC_ADDR,
    931 			    (char *)(void *)&servaddr);
    932 			__rpc_fixup_addr(address, &servaddr);
    933 			goto done;
    934 		} else if (clnt_st == RPC_PROGVERSMISMATCH) {
    935 			struct rpc_err rpcerr;
    936 
    937 			clnt_geterr(client, &rpcerr);
    938 			if (rpcerr.re_vers.low > RPCBVERS4)
    939 				goto error;  /* a new version, can't handle */
    940 		} else if (clnt_st != RPC_PROGUNAVAIL) {
    941 			/* Cant handle this error */
    942 			rpc_createerr.cf_stat = clnt_st;
    943 			clnt_geterr(client, &rpc_createerr.cf_error);
    944 			goto error;
    945 		}
    946 	}
    947 
    948 error:
    949 	if (client) {
    950 		CLNT_DESTROY(client);
    951 		client = NULL;
    952 	}
    953 done:
    954 	if (nconf->nc_semantics != NC_TPI_CLTS) {
    955 		/* This client is the connectionless one */
    956 		if (client) {
    957 			CLNT_DESTROY(client);
    958 			client = NULL;
    959 		}
    960 	}
    961 	if (clpp) {
    962 		*clpp = client;
    963 	} else if (client) {
    964 		CLNT_DESTROY(client);
    965 	}
    966 	return (address);
    967 }
    968 
    969 
    970 /*
    971  * Find the mapped address for program, version.
    972  * Calls the rpcbind service remotely to do the lookup.
    973  * Uses the transport specified in nconf.
    974  * Returns FALSE (0) if no map exists, else returns 1.
    975  *
    976  * Assuming that the address is all properly allocated
    977  */
    978 int
    979 rpcb_getaddr(rpcprog_t program, rpcvers_t version,
    980 	const struct netconfig *nconf, struct netbuf *address,
    981 	const char *host)
    982 {
    983 	struct netbuf *na;
    984 
    985 	_DIAGASSERT(address != NULL);
    986 
    987 	if ((na = __rpcb_findaddr(program, version, nconf,
    988 				host, NULL)) == NULL)
    989 		return (FALSE);
    990 
    991 	if (na->len > address->maxlen) {
    992 		/* Too long address */
    993 		free(na->buf);
    994 		free(na);
    995 		rpc_createerr.cf_stat = RPC_FAILED;
    996 		return (FALSE);
    997 	}
    998 	memcpy(address->buf, na->buf, (size_t)na->len);
    999 	address->len = na->len;
   1000 	free(na->buf);
   1001 	free(na);
   1002 	return (TRUE);
   1003 }
   1004 
   1005 /*
   1006  * Get a copy of the current maps.
   1007  * Calls the rpcbind service remotely to get the maps.
   1008  *
   1009  * It returns only a list of the services
   1010  * It returns NULL on failure.
   1011  */
   1012 rpcblist *
   1013 rpcb_getmaps(const struct netconfig *nconf, const char *host)
   1014 {
   1015 	rpcblist_ptr head = NULL;
   1016 	CLIENT *client;
   1017 	enum clnt_stat clnt_st;
   1018 	rpcvers_t vers = 0;
   1019 
   1020 	client = getclnthandle(host, nconf, NULL);
   1021 	if (client == NULL) {
   1022 		return (head);
   1023 	}
   1024 	clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
   1025 	    (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
   1026 	    (char *)(void *)&head, tottimeout);
   1027 	if (clnt_st == RPC_SUCCESS)
   1028 		goto done;
   1029 
   1030 	if ((clnt_st != RPC_PROGVERSMISMATCH) &&
   1031 	    (clnt_st != RPC_PROGUNAVAIL)) {
   1032 		rpc_createerr.cf_stat = RPC_RPCBFAILURE;
   1033 		clnt_geterr(client, &rpc_createerr.cf_error);
   1034 		goto done;
   1035 	}
   1036 
   1037 	/* fall back to earlier version */
   1038 	CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
   1039 	if (vers == RPCBVERS4) {
   1040 		vers = RPCBVERS;
   1041 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
   1042 		if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
   1043 		    (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
   1044 		    (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
   1045 			goto done;
   1046 	}
   1047 	rpc_createerr.cf_stat = RPC_RPCBFAILURE;
   1048 	clnt_geterr(client, &rpc_createerr.cf_error);
   1049 
   1050 done:
   1051 	CLNT_DESTROY(client);
   1052 	return (head);
   1053 }
   1054 
   1055 /*
   1056  * rpcbinder remote-call-service interface.
   1057  * This routine is used to call the rpcbind remote call service
   1058  * which will look up a service program in the address maps, and then
   1059  * remotely call that routine with the given parameters. This allows
   1060  * programs to do a lookup and call in one step.
   1061 */
   1062 enum clnt_stat
   1063 rpcb_rmtcall(
   1064 	const struct netconfig *nconf,	/* Netconfig structure */
   1065 	const char *host,		/* Remote host name */
   1066 	rpcprog_t prog,
   1067 	rpcvers_t vers,
   1068 	rpcproc_t proc,			/* Remote proc identifiers */
   1069 	xdrproc_t xdrargs,
   1070 	const char *argsp,		/* Argument */
   1071 	xdrproc_t xdrres,		/* XDR routines */
   1072 	caddr_t resp,			/* Result */
   1073 	struct timeval tout,		/* Timeout value for this call */
   1074 	const struct netbuf *addr_ptr)	/* Preallocated netbuf address */
   1075 {
   1076 	CLIENT *client;
   1077 	enum clnt_stat stat;
   1078 	struct r_rpcb_rmtcallargs a;
   1079 	struct r_rpcb_rmtcallres r;
   1080 	rpcvers_t rpcb_vers;
   1081 
   1082 	stat = RPC_FAILED;	/* XXXGCC -Wuninitialized [dreamcast] */
   1083 
   1084 	client = getclnthandle(host, nconf, NULL);
   1085 	if (client == NULL) {
   1086 		return (RPC_FAILED);
   1087 	}
   1088 	CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, __UNCONST(&rmttimeout));
   1089 	a.prog = prog;
   1090 	a.vers = vers;
   1091 	a.proc = proc;
   1092 	a.args.args_val = argsp;
   1093 	a.xdr_args = xdrargs;
   1094 	r.addr = NULL;
   1095 	r.results.results_val = resp;
   1096 	r.xdr_res = xdrres;
   1097 
   1098 	for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
   1099 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
   1100 		stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
   1101 		    (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
   1102 		    (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
   1103 		if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
   1104 			struct netbuf *na;
   1105 			na = uaddr2taddr(__UNCONST(nconf), r.addr);
   1106 			if (!na) {
   1107 				stat = RPC_N2AXLATEFAILURE;
   1108 				((struct netbuf *)__UNCONST(addr_ptr))->len = 0;
   1109 				goto error;
   1110 			}
   1111 			if (na->len > addr_ptr->maxlen) {
   1112 				/* Too long address */
   1113 				stat = RPC_FAILED; /* XXX A better error no */
   1114 				free(na->buf);
   1115 				free(na);
   1116 				((struct netbuf *)__UNCONST(addr_ptr))->len = 0;
   1117 				goto error;
   1118 			}
   1119 			memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
   1120 			((struct netbuf *)__UNCONST(addr_ptr))->len = na->len;
   1121 			free(na->buf);
   1122 			free(na);
   1123 			break;
   1124 		} else if ((stat != RPC_PROGVERSMISMATCH) &&
   1125 			    (stat != RPC_PROGUNAVAIL)) {
   1126 			goto error;
   1127 		}
   1128 	}
   1129 error:
   1130 	CLNT_DESTROY(client);
   1131 	if (r.addr)
   1132 		xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
   1133 	return (stat);
   1134 }
   1135 
   1136 /*
   1137  * Gets the time on the remote host.
   1138  * Returns 1 if succeeds else 0.
   1139  */
   1140 bool_t
   1141 rpcb_gettime(const char *host, time_t *timep)
   1142 {
   1143 	CLIENT *client = NULL;
   1144 	void *handle;
   1145 	struct netconfig *nconf;
   1146 	rpcvers_t vers;
   1147 	enum clnt_stat st;
   1148 
   1149 
   1150 	if ((host == NULL) || (host[0] == 0)) {
   1151 		time(timep);
   1152 		return (TRUE);
   1153 	}
   1154 
   1155 	if ((handle = __rpc_setconf("netpath")) == NULL) {
   1156 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1157 		return (FALSE);
   1158 	}
   1159 	rpc_createerr.cf_stat = RPC_SUCCESS;
   1160 	while (client == NULL) {
   1161 		if ((nconf = __rpc_getconf(handle)) == NULL) {
   1162 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
   1163 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1164 			break;
   1165 		}
   1166 		client = getclnthandle(host, nconf, NULL);
   1167 		if (client)
   1168 			break;
   1169 	}
   1170 	__rpc_endconf(handle);
   1171 	if (client == NULL) {
   1172 		return (FALSE);
   1173 	}
   1174 
   1175 	st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
   1176 		(xdrproc_t) xdr_void, NULL,
   1177 		(xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
   1178 
   1179 	if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
   1180 		CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
   1181 		if (vers == RPCBVERS4) {
   1182 			/* fall back to earlier version */
   1183 			vers = RPCBVERS;
   1184 			CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
   1185 			st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
   1186 				(xdrproc_t) xdr_void, NULL,
   1187 				(xdrproc_t) xdr_int, (char *)(void *)timep,
   1188 				tottimeout);
   1189 		}
   1190 	}
   1191 	CLNT_DESTROY(client);
   1192 	return (st == RPC_SUCCESS? TRUE: FALSE);
   1193 }
   1194 
   1195 /*
   1196  * Converts taddr to universal address.  This routine should never
   1197  * really be called because local n2a libraries are always provided.
   1198  */
   1199 char *
   1200 rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
   1201 {
   1202 	CLIENT *client;
   1203 	char *uaddr = NULL;
   1204 
   1205 	/* parameter checking */
   1206 	if (nconf == NULL) {
   1207 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1208 		return (NULL);
   1209 	}
   1210 	if (taddr == NULL) {
   1211 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
   1212 		return (NULL);
   1213 	}
   1214 	client = local_rpcb();
   1215 	if (! client) {
   1216 		return (NULL);
   1217 	}
   1218 
   1219 	CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
   1220 	    (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
   1221 	    (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
   1222 	CLNT_DESTROY(client);
   1223 	return (uaddr);
   1224 }
   1225 
   1226 /*
   1227  * Converts universal address to netbuf.  This routine should never
   1228  * really be called because local n2a libraries are always provided.
   1229  */
   1230 struct netbuf *
   1231 rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
   1232 {
   1233 	CLIENT *client;
   1234 	struct netbuf *taddr;
   1235 
   1236 
   1237 	/* parameter checking */
   1238 	if (nconf == NULL) {
   1239 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1240 		return (NULL);
   1241 	}
   1242 	if (uaddr == NULL) {
   1243 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
   1244 		return (NULL);
   1245 	}
   1246 	client = local_rpcb();
   1247 	if (! client) {
   1248 		return (NULL);
   1249 	}
   1250 
   1251 	taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
   1252 	if (taddr == NULL) {
   1253 		CLNT_DESTROY(client);
   1254 		return (NULL);
   1255 	}
   1256 	if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
   1257 	    (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
   1258 	    (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
   1259 	    tottimeout) != RPC_SUCCESS) {
   1260 		free(taddr);
   1261 		taddr = NULL;
   1262 	}
   1263 	CLNT_DESTROY(client);
   1264 	return (taddr);
   1265 }
   1266