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