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