Home | History | Annotate | Line # | Download | only in rpc
rpcb_clnt.c revision 1.24.6.2
      1 /*	$NetBSD: rpcb_clnt.c,v 1.24.6.2 2008/07/25 14:05:26 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.24.6.2 2008/07/25 14:05:26 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 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 = malloc(sizeof(*ad_cache));
    242 	if (!ad_cache) {
    243 		return;
    244 	}
    245 	ad_cache->ac_host = strdup(host);
    246 	ad_cache->ac_netid = strdup(netid);
    247 	ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
    248 	ad_cache->ac_taddr = malloc(sizeof(*ad_cache->ac_taddr));
    249 	if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
    250 		(uaddr && !ad_cache->ac_uaddr)) {
    251 		goto out;
    252 	}
    253 	ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
    254 	ad_cache->ac_taddr->buf = malloc(taddr->len);
    255 	if (ad_cache->ac_taddr->buf == NULL) {
    256 out:
    257 		if (ad_cache->ac_host)
    258 			free(ad_cache->ac_host);
    259 		if (ad_cache->ac_netid)
    260 			free(ad_cache->ac_netid);
    261 		if (ad_cache->ac_uaddr)
    262 			free(ad_cache->ac_uaddr);
    263 		if (ad_cache->ac_taddr)
    264 			free(ad_cache->ac_taddr);
    265 		free(ad_cache);
    266 		return;
    267 	}
    268 	memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
    269 #ifdef ND_DEBUG
    270 	fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
    271 #endif
    272 
    273 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
    274 
    275 	rwlock_wrlock(&rpcbaddr_cache_lock);
    276 	if (cachesize < CACHESIZE) {
    277 		ad_cache->ac_next = front;
    278 		front = ad_cache;
    279 		cachesize++;
    280 	} else {
    281 		/* Free the last entry */
    282 		cptr = front;
    283 		prevptr = NULL;
    284 		while (cptr->ac_next) {
    285 			prevptr = cptr;
    286 			cptr = cptr->ac_next;
    287 		}
    288 
    289 #ifdef ND_DEBUG
    290 		fprintf(stderr, "Deleted from cache: %s : %s\n",
    291 			cptr->ac_host, cptr->ac_netid);
    292 #endif
    293 		free(cptr->ac_host);
    294 		free(cptr->ac_netid);
    295 		free(cptr->ac_taddr->buf);
    296 		free(cptr->ac_taddr);
    297 		if (cptr->ac_uaddr)
    298 			free(cptr->ac_uaddr);
    299 
    300 		if (prevptr) {
    301 			prevptr->ac_next = NULL;
    302 			ad_cache->ac_next = front;
    303 			front = ad_cache;
    304 		} else {
    305 			front = ad_cache;
    306 			ad_cache->ac_next = NULL;
    307 		}
    308 		free(cptr);
    309 	}
    310 	rwlock_unlock(&rpcbaddr_cache_lock);
    311 }
    312 
    313 /*
    314  * This routine will return a client handle that is connected to the
    315  * rpcbind. Returns NULL on error and free's everything.
    316  */
    317 static CLIENT *
    318 getclnthandle(host, nconf, targaddr)
    319 	const char *host;
    320 	const struct netconfig *nconf;
    321 	char **targaddr;
    322 {
    323 	CLIENT *client;
    324 	struct netbuf *addr, taddr;
    325 	struct netbuf addr_to_delete;
    326 	struct __rpc_sockinfo si;
    327 	struct addrinfo hints, *res, *tres;
    328 	struct address_cache *ad_cache;
    329 	char *tmpaddr;
    330 
    331 	_DIAGASSERT(host != NULL);
    332 	_DIAGASSERT(nconf != NULL);
    333 	/* targaddr may be NULL */
    334 
    335 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  ad_cache */
    336 
    337 	/* Get the address of the rpcbind.  Check cache first */
    338 	client = NULL;
    339 	addr_to_delete.len = 0;
    340 	addr_to_delete.buf = NULL;
    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 = 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 = malloc(sizeof(struct netbuf))) == NULL) ||
    776 		    ((address->buf = malloc(remote.len)) == NULL)) {
    777 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    778 			clnt_geterr(client, &rpc_createerr.cf_error);
    779 			if (address) {
    780 				free(address);
    781 				address = NULL;
    782 			}
    783 			goto error;
    784 		}
    785 		memcpy(address->buf, remote.buf, remote.len);
    786 		memcpy(&((char *)address->buf)[sizeof (short)],
    787 				(char *)(void *)&port, sizeof (short));
    788 		address->len = address->maxlen = remote.len;
    789 		goto done;
    790 	}
    791 #endif
    792 
    793 try_rpcbind:
    794 	/*
    795 	 * Now we try version 4 and then 3.
    796 	 * We also send the remote system the address we used to
    797 	 * contact it in case it can help to connect back with us
    798 	 */
    799 	parms.r_prog = program;
    800 	parms.r_vers = version;
    801 	parms.r_owner = __UNCONST(&nullstring[0]);	/* not needed; */
    802 							/* just for xdring */
    803 	parms.r_netid = nconf->nc_netid; /* not really needed */
    804 
    805 	/*
    806 	 * If a COTS transport is being used, try getting address via CLTS
    807 	 * transport.  This works only with version 4.
    808 	 * NOTE: This is being done for all transports EXCEPT LOOPBACK
    809 	 * because with loopback the cost to go to a COTS is same as
    810 	 * the cost to go through CLTS, plus you get the advantage of
    811 	 * finding out immediately if the local rpcbind process is dead.
    812 	 */
    813 #if 1
    814 	if ((nconf->nc_semantics == NC_TPI_COTS_ORD ||
    815 			nconf->nc_semantics == NC_TPI_COTS) &&
    816 	    (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0))
    817 #else
    818 	if (client != NULL) {
    819 		CLNT_DESTROY(client);
    820 		client = NULL;
    821 	}
    822 	if (nconf->nc_semantics == NC_TPI_CLTS)
    823 #endif
    824 	{
    825 		void *handle;
    826 		struct netconfig *nconf_clts;
    827 		rpcb_entry_list_ptr relp = NULL;
    828 
    829 		if (client == NULL) {
    830 			/* This did not go through the above PORTMAP/TCP code */
    831 #if 1
    832 			if ((handle = __rpc_setconf("datagram_v")) != NULL)
    833 #else
    834 			if ((handle = __rpc_setconf("circuit_v")) != NULL)
    835 #endif
    836 			{
    837 				while ((nconf_clts = __rpc_getconf(handle))
    838 					!= NULL) {
    839 					if (strcmp(nconf_clts->nc_protofmly,
    840 						nconf->nc_protofmly) != 0) {
    841 						continue;
    842 					}
    843 					client = getclnthandle(host, nconf_clts,
    844 							&parms.r_addr);
    845 					break;
    846 				}
    847 				__rpc_endconf(handle);
    848 			}
    849 			if (client == NULL)
    850 				goto regular_rpcbind;	/* Go the regular way */
    851 		} else {
    852 			/* This is a UDP PORTMAP handle.  Change to version 4 */
    853 			vers = RPCBVERS4;
    854 			CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
    855 		}
    856 		/*
    857 		 * We also send the remote system the address we used to
    858 		 * contact it in case it can help it connect back with us
    859 		 */
    860 		if (parms.r_addr == NULL) {
    861 			/* for XDRing */
    862 			parms.r_addr = __UNCONST(&nullstring[0]);
    863 		}
    864 		clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
    865 		    (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
    866 		    (xdrproc_t) xdr_rpcb_entry_list_ptr,
    867 		    (char *)(void *)&relp, tottimeout);
    868 		if (clnt_st == RPC_SUCCESS) {
    869 			if ((address = got_entry(relp, nconf)) != NULL) {
    870 				xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
    871 				    (char *)(void *)&relp);
    872 				CLNT_CONTROL(client, CLGET_SVC_ADDR,
    873 					(char *)(void *)&servaddr);
    874 				__rpc_fixup_addr(address, &servaddr);
    875 				goto done;
    876 			}
    877 			/* Entry not found for this transport */
    878 			xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
    879 			    (char *)(void *)&relp);
    880 			/*
    881 			 * XXX: should have perhaps returned with error but
    882 			 * since the remote machine might not always be able
    883 			 * to send the address on all transports, we try the
    884 			 * regular way with regular_rpcbind
    885 			 */
    886 			goto regular_rpcbind;
    887 		} else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
    888 			(clnt_st == RPC_PROGUNAVAIL)) {
    889 			start_vers = RPCBVERS;	/* Try version 3 now */
    890 			goto regular_rpcbind; /* Try different versions */
    891 		} else {
    892 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
    893 			clnt_geterr(client, &rpc_createerr.cf_error);
    894 			goto error;
    895 		}
    896 	}
    897 
    898 regular_rpcbind:
    899 
    900 	/* Now the same transport is to be used to get the address */
    901 #if 1
    902 	if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
    903 			(nconf->nc_semantics == NC_TPI_COTS)))
    904 #else
    905 	if (client && nconf->nc_semantics == NC_TPI_CLTS)
    906 #endif
    907 	{
    908 		/* A CLTS type of client - destroy it */
    909 		CLNT_DESTROY(client);
    910 		client = NULL;
    911 	}
    912 
    913 	if (client == NULL) {
    914 		client = getclnthandle(host, nconf, &parms.r_addr);
    915 		if (client == NULL) {
    916 			goto error;
    917 		}
    918 	}
    919 	if (parms.r_addr == NULL)
    920 		parms.r_addr = __UNCONST(&nullstring[0]);
    921 
    922 	/* First try from start_vers and then version 3 (RPCBVERS) */
    923 	for (vers = start_vers;  vers >= RPCBVERS; vers--) {
    924 		/* Set the version */
    925 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
    926 		clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
    927 		    (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
    928 		    (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua,
    929 		    tottimeout);
    930 		if (clnt_st == RPC_SUCCESS) {
    931 			if ((ua == NULL) || (ua[0] == 0)) {
    932 				/* address unknown */
    933 				rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
    934 				goto error;
    935 			}
    936 			address = uaddr2taddr(nconf, ua);
    937 #ifdef ND_DEBUG
    938 			fprintf(stderr, "\tRemote address is [%s]\n", ua);
    939 			if (!address)
    940 				fprintf(stderr,
    941 					"\tCouldn't resolve remote address!\n");
    942 #endif
    943 			xdr_free((xdrproc_t)xdr_wrapstring,
    944 			    (char *)(void *)&ua);
    945 
    946 			if (! address) {
    947 				/* We don't know about your universal address */
    948 				rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
    949 				goto error;
    950 			}
    951 			CLNT_CONTROL(client, CLGET_SVC_ADDR,
    952 			    (char *)(void *)&servaddr);
    953 			__rpc_fixup_addr(address, &servaddr);
    954 			goto done;
    955 		} else if (clnt_st == RPC_PROGVERSMISMATCH) {
    956 			struct rpc_err rpcerr;
    957 
    958 			clnt_geterr(client, &rpcerr);
    959 			if (rpcerr.re_vers.low > RPCBVERS4)
    960 				goto error;  /* a new version, can't handle */
    961 		} else if (clnt_st != RPC_PROGUNAVAIL) {
    962 			/* Cant handle this error */
    963 			rpc_createerr.cf_stat = clnt_st;
    964 			clnt_geterr(client, &rpc_createerr.cf_error);
    965 			goto error;
    966 		}
    967 	}
    968 
    969 error:
    970 	if (client) {
    971 		CLNT_DESTROY(client);
    972 		client = NULL;
    973 	}
    974 done:
    975 	if (nconf->nc_semantics != NC_TPI_CLTS) {
    976 		/* This client is the connectionless one */
    977 		if (client) {
    978 			CLNT_DESTROY(client);
    979 			client = NULL;
    980 		}
    981 	}
    982 	if (clpp) {
    983 		*clpp = client;
    984 	} else if (client) {
    985 		CLNT_DESTROY(client);
    986 	}
    987 	return (address);
    988 }
    989 
    990 
    991 /*
    992  * Find the mapped address for program, version.
    993  * Calls the rpcbind service remotely to do the lookup.
    994  * Uses the transport specified in nconf.
    995  * Returns FALSE (0) if no map exists, else returns 1.
    996  *
    997  * Assuming that the address is all properly allocated
    998  */
    999 int
   1000 rpcb_getaddr(program, version, nconf, address, host)
   1001 	rpcprog_t program;
   1002 	rpcvers_t version;
   1003 	const struct netconfig *nconf;
   1004 	struct netbuf *address;
   1005 	const char *host;
   1006 {
   1007 	struct netbuf *na;
   1008 
   1009 	_DIAGASSERT(address != NULL);
   1010 
   1011 	if ((na = __rpcb_findaddr(program, version, nconf,
   1012 				host, NULL)) == NULL)
   1013 		return (FALSE);
   1014 
   1015 	if (na->len > address->maxlen) {
   1016 		/* Too long address */
   1017 		free(na->buf);
   1018 		free(na);
   1019 		rpc_createerr.cf_stat = RPC_FAILED;
   1020 		return (FALSE);
   1021 	}
   1022 	memcpy(address->buf, na->buf, (size_t)na->len);
   1023 	address->len = na->len;
   1024 	free(na->buf);
   1025 	free(na);
   1026 	return (TRUE);
   1027 }
   1028 
   1029 /*
   1030  * Get a copy of the current maps.
   1031  * Calls the rpcbind service remotely to get the maps.
   1032  *
   1033  * It returns only a list of the services
   1034  * It returns NULL on failure.
   1035  */
   1036 rpcblist *
   1037 rpcb_getmaps(nconf, host)
   1038 	const struct netconfig *nconf;
   1039 	const char *host;
   1040 {
   1041 	rpcblist_ptr head = NULL;
   1042 	CLIENT *client;
   1043 	enum clnt_stat clnt_st;
   1044 	rpcvers_t vers = 0;
   1045 
   1046 	client = getclnthandle(host, nconf, NULL);
   1047 	if (client == NULL) {
   1048 		return (head);
   1049 	}
   1050 	clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
   1051 	    (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
   1052 	    (char *)(void *)&head, tottimeout);
   1053 	if (clnt_st == RPC_SUCCESS)
   1054 		goto done;
   1055 
   1056 	if ((clnt_st != RPC_PROGVERSMISMATCH) &&
   1057 	    (clnt_st != RPC_PROGUNAVAIL)) {
   1058 		rpc_createerr.cf_stat = RPC_RPCBFAILURE;
   1059 		clnt_geterr(client, &rpc_createerr.cf_error);
   1060 		goto done;
   1061 	}
   1062 
   1063 	/* fall back to earlier version */
   1064 	CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
   1065 	if (vers == RPCBVERS4) {
   1066 		vers = RPCBVERS;
   1067 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
   1068 		if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
   1069 		    (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
   1070 		    (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
   1071 			goto done;
   1072 	}
   1073 	rpc_createerr.cf_stat = RPC_RPCBFAILURE;
   1074 	clnt_geterr(client, &rpc_createerr.cf_error);
   1075 
   1076 done:
   1077 	CLNT_DESTROY(client);
   1078 	return (head);
   1079 }
   1080 
   1081 /*
   1082  * rpcbinder remote-call-service interface.
   1083  * This routine is used to call the rpcbind remote call service
   1084  * which will look up a service program in the address maps, and then
   1085  * remotely call that routine with the given parameters. This allows
   1086  * programs to do a lookup and call in one step.
   1087 */
   1088 enum clnt_stat
   1089 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
   1090 		xdrres, resp, tout, addr_ptr)
   1091 	const struct netconfig *nconf;	/* Netconfig structure */
   1092 	const char *host;			/* Remote host name */
   1093 	rpcprog_t prog;
   1094 	rpcvers_t vers;
   1095 	rpcproc_t proc;			/* Remote proc identifiers */
   1096 	xdrproc_t xdrargs, xdrres;	/* XDR routines */
   1097 	const char *argsp;		/* Argument */
   1098 	caddr_t resp;			/* 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 	stat = RPC_FAILED;	/* XXXGCC -Wuninitialized [dreamcast] */
   1109 
   1110 	client = getclnthandle(host, nconf, NULL);
   1111 	if (client == NULL) {
   1112 		return (RPC_FAILED);
   1113 	}
   1114 	CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, __UNCONST(&rmttimeout));
   1115 	a.prog = prog;
   1116 	a.vers = vers;
   1117 	a.proc = proc;
   1118 	a.args.args_val = argsp;
   1119 	a.xdr_args = xdrargs;
   1120 	r.addr = NULL;
   1121 	r.results.results_val = resp;
   1122 	r.xdr_res = xdrres;
   1123 
   1124 	for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
   1125 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
   1126 		stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
   1127 		    (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
   1128 		    (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
   1129 		if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
   1130 			struct netbuf *na;
   1131 			na = uaddr2taddr(__UNCONST(nconf), r.addr);
   1132 			if (!na) {
   1133 				stat = RPC_N2AXLATEFAILURE;
   1134 				((struct netbuf *)__UNCONST(addr_ptr))->len = 0;
   1135 				goto error;
   1136 			}
   1137 			if (na->len > addr_ptr->maxlen) {
   1138 				/* Too long address */
   1139 				stat = RPC_FAILED; /* XXX A better error no */
   1140 				free(na->buf);
   1141 				free(na);
   1142 				((struct netbuf *)__UNCONST(addr_ptr))->len = 0;
   1143 				goto error;
   1144 			}
   1145 			memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
   1146 			((struct netbuf *)__UNCONST(addr_ptr))->len = na->len;
   1147 			free(na->buf);
   1148 			free(na);
   1149 			break;
   1150 		} else if ((stat != RPC_PROGVERSMISMATCH) &&
   1151 			    (stat != RPC_PROGUNAVAIL)) {
   1152 			goto error;
   1153 		}
   1154 	}
   1155 error:
   1156 	CLNT_DESTROY(client);
   1157 	if (r.addr)
   1158 		xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
   1159 	return (stat);
   1160 }
   1161 
   1162 /*
   1163  * Gets the time on the remote host.
   1164  * Returns 1 if succeeds else 0.
   1165  */
   1166 bool_t
   1167 rpcb_gettime(host, timep)
   1168 	const char *host;
   1169 	time_t *timep;
   1170 {
   1171 	CLIENT *client = NULL;
   1172 	void *handle;
   1173 	struct netconfig *nconf;
   1174 	rpcvers_t vers;
   1175 	enum clnt_stat st;
   1176 
   1177 
   1178 	if ((host == NULL) || (host[0] == 0)) {
   1179 		time(timep);
   1180 		return (TRUE);
   1181 	}
   1182 
   1183 	if ((handle = __rpc_setconf("netpath")) == NULL) {
   1184 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1185 		return (FALSE);
   1186 	}
   1187 	rpc_createerr.cf_stat = RPC_SUCCESS;
   1188 	while (client == NULL) {
   1189 		if ((nconf = __rpc_getconf(handle)) == NULL) {
   1190 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
   1191 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1192 			break;
   1193 		}
   1194 		client = getclnthandle(host, nconf, NULL);
   1195 		if (client)
   1196 			break;
   1197 	}
   1198 	__rpc_endconf(handle);
   1199 	if (client == NULL) {
   1200 		return (FALSE);
   1201 	}
   1202 
   1203 	st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
   1204 		(xdrproc_t) xdr_void, NULL,
   1205 		(xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
   1206 
   1207 	if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
   1208 		CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
   1209 		if (vers == RPCBVERS4) {
   1210 			/* fall back to earlier version */
   1211 			vers = RPCBVERS;
   1212 			CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
   1213 			st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
   1214 				(xdrproc_t) xdr_void, NULL,
   1215 				(xdrproc_t) xdr_int, (char *)(void *)timep,
   1216 				tottimeout);
   1217 		}
   1218 	}
   1219 	CLNT_DESTROY(client);
   1220 	return (st == RPC_SUCCESS? TRUE: FALSE);
   1221 }
   1222 
   1223 /*
   1224  * Converts taddr to universal address.  This routine should never
   1225  * really be called because local n2a libraries are always provided.
   1226  */
   1227 char *
   1228 rpcb_taddr2uaddr(nconf, taddr)
   1229 	struct netconfig *nconf;
   1230 	struct netbuf *taddr;
   1231 {
   1232 	CLIENT *client;
   1233 	char *uaddr = NULL;
   1234 
   1235 
   1236 	/* parameter checking */
   1237 	if (nconf == NULL) {
   1238 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1239 		return (NULL);
   1240 	}
   1241 	if (taddr == NULL) {
   1242 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
   1243 		return (NULL);
   1244 	}
   1245 	client = local_rpcb();
   1246 	if (! client) {
   1247 		return (NULL);
   1248 	}
   1249 
   1250 	CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
   1251 	    (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
   1252 	    (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
   1253 	CLNT_DESTROY(client);
   1254 	return (uaddr);
   1255 }
   1256 
   1257 /*
   1258  * Converts universal address to netbuf.  This routine should never
   1259  * really be called because local n2a libraries are always provided.
   1260  */
   1261 struct netbuf *
   1262 rpcb_uaddr2taddr(nconf, uaddr)
   1263 	struct netconfig *nconf;
   1264 	char *uaddr;
   1265 {
   1266 	CLIENT *client;
   1267 	struct netbuf *taddr;
   1268 
   1269 
   1270 	/* parameter checking */
   1271 	if (nconf == NULL) {
   1272 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1273 		return (NULL);
   1274 	}
   1275 	if (uaddr == NULL) {
   1276 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
   1277 		return (NULL);
   1278 	}
   1279 	client = local_rpcb();
   1280 	if (! client) {
   1281 		return (NULL);
   1282 	}
   1283 
   1284 	taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
   1285 	if (taddr == NULL) {
   1286 		CLNT_DESTROY(client);
   1287 		return (NULL);
   1288 	}
   1289 	if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
   1290 	    (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
   1291 	    (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
   1292 	    tottimeout) != RPC_SUCCESS) {
   1293 		free(taddr);
   1294 		taddr = NULL;
   1295 	}
   1296 	CLNT_DESTROY(client);
   1297 	return (taddr);
   1298 }
   1299