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