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