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