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