xref: /freebsd/lib/libc/rpc/rpcb_clnt.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
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 			free(cptr->ac_uaddr);
183 			if (prevptr)
184 				prevptr->ac_next = cptr->ac_next;
185 			else
186 				front = cptr->ac_next;
187 			free(cptr);
188 			cachesize--;
189 			break;
190 		}
191 		prevptr = cptr;
192 	}
193 }
194 
195 static void
196 add_cache(const char *host, const char *netid, struct netbuf *taddr,
197     char *uaddr)
198 {
199 	struct address_cache  *ad_cache, *cptr, *prevptr;
200 
201 	ad_cache = (struct address_cache *)
202 			malloc(sizeof (struct address_cache));
203 	if (!ad_cache) {
204 		return;
205 	}
206 	ad_cache->ac_host = strdup(host);
207 	ad_cache->ac_netid = strdup(netid);
208 	ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
209 	ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
210 	if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
211 		(uaddr && !ad_cache->ac_uaddr)) {
212 		goto out;
213 	}
214 	ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
215 	ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
216 	if (ad_cache->ac_taddr->buf == NULL) {
217 out:
218 		free(ad_cache->ac_host);
219 		free(ad_cache->ac_netid);
220 		free(ad_cache->ac_uaddr);
221 		free(ad_cache->ac_taddr);
222 		free(ad_cache);
223 		return;
224 	}
225 	memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
226 #ifdef ND_DEBUG
227 	fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
228 #endif
229 
230 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
231 
232 	rwlock_wrlock(&rpcbaddr_cache_lock);
233 	if (cachesize < CACHESIZE) {
234 		ad_cache->ac_next = front;
235 		front = ad_cache;
236 		cachesize++;
237 	} else {
238 		/* Free the last entry */
239 		cptr = front;
240 		prevptr = NULL;
241 		while (cptr->ac_next) {
242 			prevptr = cptr;
243 			cptr = cptr->ac_next;
244 		}
245 
246 #ifdef ND_DEBUG
247 		fprintf(stderr, "Deleted from cache: %s : %s\n",
248 			cptr->ac_host, cptr->ac_netid);
249 #endif
250 		free(cptr->ac_host);
251 		free(cptr->ac_netid);
252 		free(cptr->ac_taddr->buf);
253 		free(cptr->ac_taddr);
254 		free(cptr->ac_uaddr);
255 
256 		if (prevptr) {
257 			prevptr->ac_next = NULL;
258 			ad_cache->ac_next = front;
259 			front = ad_cache;
260 		} else {
261 			front = ad_cache;
262 			ad_cache->ac_next = NULL;
263 		}
264 		free(cptr);
265 	}
266 	rwlock_unlock(&rpcbaddr_cache_lock);
267 }
268 
269 /*
270  * This routine will return a client handle that is connected to the
271  * rpcbind. If targaddr is non-NULL, the "universal address" of the
272  * host will be stored in *targaddr; the caller is responsible for
273  * freeing this string.
274  * On error, returns NULL and free's everything.
275  */
276 static CLIENT *
277 getclnthandle(const char *host, const struct netconfig *nconf, char **targaddr)
278 {
279 	CLIENT *client;
280 	struct netbuf *addr, taddr;
281 	struct netbuf addr_to_delete;
282 	struct __rpc_sockinfo si;
283 	struct addrinfo hints, *res, *tres;
284 	struct address_cache *ad_cache;
285 	char *tmpaddr;
286 
287 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  ad_cache */
288 
289 	/* Get the address of the rpcbind.  Check cache first */
290 	client = NULL;
291 	addr_to_delete.len = 0;
292 	rwlock_rdlock(&rpcbaddr_cache_lock);
293 	ad_cache = NULL;
294 	if (host != NULL)
295 		ad_cache = check_cache(host, nconf->nc_netid);
296 	if (ad_cache != NULL) {
297 		addr = ad_cache->ac_taddr;
298 		client = clnt_tli_create(RPC_ANYFD, nconf, addr,
299 		    (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
300 		if (client != NULL) {
301 			if (targaddr)
302 				*targaddr = strdup(ad_cache->ac_uaddr);
303 			rwlock_unlock(&rpcbaddr_cache_lock);
304 			return (client);
305 		}
306 		addr_to_delete.len = addr->len;
307 		addr_to_delete.buf = (char *)malloc(addr->len);
308 		if (addr_to_delete.buf == NULL) {
309 			addr_to_delete.len = 0;
310 		} else {
311 			memcpy(addr_to_delete.buf, addr->buf, addr->len);
312 		}
313 	}
314 	rwlock_unlock(&rpcbaddr_cache_lock);
315 	if (addr_to_delete.len != 0) {
316 		/*
317 		 * Assume this may be due to cache data being
318 		 *  outdated
319 		 */
320 		rwlock_wrlock(&rpcbaddr_cache_lock);
321 		delete_cache(&addr_to_delete);
322 		rwlock_unlock(&rpcbaddr_cache_lock);
323 		free(addr_to_delete.buf);
324 	}
325 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
326 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
327 		return NULL;
328 	}
329 
330 	memset(&hints, 0, sizeof hints);
331 	hints.ai_family = si.si_af;
332 	hints.ai_socktype = si.si_socktype;
333 	hints.ai_protocol = si.si_proto;
334 
335 #ifdef CLNT_DEBUG
336 	printf("trying netid %s family %d proto %d socktype %d\n",
337 	    nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
338 #endif
339 
340 	if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
341 		client = local_rpcb();
342 		if (! client) {
343 #ifdef ND_DEBUG
344 			clnt_pcreateerror("rpcbind clnt interface");
345 #endif
346 			return (NULL);
347 		} else {
348 			struct sockaddr_un sun;
349 			if (targaddr) {
350 			    *targaddr = malloc(sizeof(sun.sun_path));
351 			    if (*targaddr == NULL) {
352 				CLNT_DESTROY(client);
353 				return (NULL);
354 			    }
355 			    strncpy(*targaddr, _PATH_RPCBINDSOCK,
356 				sizeof(sun.sun_path));
357 			}
358 			return (client);
359 		}
360 	} else {
361 		if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
362 			rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
363 			return NULL;
364 		}
365 	}
366 
367 	for (tres = res; tres != NULL; tres = tres->ai_next) {
368 		taddr.buf = tres->ai_addr;
369 		taddr.len = taddr.maxlen = tres->ai_addrlen;
370 
371 #ifdef ND_DEBUG
372 		{
373 			char *ua;
374 
375 			ua = taddr2uaddr(nconf, &taddr);
376 			fprintf(stderr, "Got it [%s]\n", ua);
377 			free(ua);
378 		}
379 #endif
380 
381 #ifdef ND_DEBUG
382 		{
383 			int i;
384 
385 			fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
386 				taddr.len, taddr.maxlen);
387 			fprintf(stderr, "\tAddress is ");
388 			for (i = 0; i < taddr.len; i++)
389 				fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
390 			fprintf(stderr, "\n");
391 		}
392 #endif
393 		client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
394 		    (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
395 #ifdef ND_DEBUG
396 		if (! client) {
397 			clnt_pcreateerror("rpcbind clnt interface");
398 		}
399 #endif
400 
401 		if (client) {
402 			tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
403 			add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
404 			if (targaddr)
405 				*targaddr = tmpaddr;
406 			break;
407 		}
408 	}
409 	if (res)
410 		freeaddrinfo(res);
411 	return (client);
412 }
413 
414 /* XXX */
415 #define IN4_LOCALHOST_STRING	"127.0.0.1"
416 #define IN6_LOCALHOST_STRING	"::1"
417 
418 /*
419  * This routine will return a client handle that is connected to the local
420  * rpcbind. Returns NULL on error and free's everything.
421  */
422 static CLIENT *
423 local_rpcb(void)
424 {
425 	CLIENT *client;
426 	static struct netconfig *loopnconf;
427 	static char *hostname;
428 	int sock;
429 	size_t tsize;
430 	struct netbuf nbuf;
431 	struct sockaddr_un sun;
432 
433 	/*
434 	 * Try connecting to the local rpcbind through a local socket
435 	 * first. If this doesn't work, try all transports defined in
436 	 * the netconfig file.
437 	 */
438 	memset(&sun, 0, sizeof sun);
439 	sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
440 	if (sock < 0)
441 		goto try_nconf;
442 	sun.sun_family = AF_LOCAL;
443 	strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
444 	nbuf.len = sun.sun_len = SUN_LEN(&sun);
445 	nbuf.maxlen = sizeof (struct sockaddr_un);
446 	nbuf.buf = &sun;
447 
448 	tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
449 	client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
450 	    (rpcvers_t)RPCBVERS, tsize, tsize);
451 
452 	if (client != NULL) {
453 		/* Mark the socket to be closed in destructor */
454 		(void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
455 		return client;
456 	}
457 
458 	/* Nobody needs this socket anymore; free the descriptor. */
459 	_close(sock);
460 
461 try_nconf:
462 
463 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
464 	mutex_lock(&loopnconf_lock);
465 	if (loopnconf == NULL) {
466 		struct netconfig *nconf, *tmpnconf = NULL;
467 		void *nc_handle;
468 		int fd;
469 
470 		nc_handle = setnetconfig();
471 		if (nc_handle == NULL) {
472 			/* fails to open netconfig file */
473 			syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
474 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
475 			mutex_unlock(&loopnconf_lock);
476 			return (NULL);
477 		}
478 		while ((nconf = getnetconfig(nc_handle)) != NULL) {
479 #ifdef INET6
480 			if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
481 #else
482 			if ((
483 #endif
484 			     strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
485 			    (nconf->nc_semantics == NC_TPI_COTS ||
486 			     nconf->nc_semantics == NC_TPI_COTS_ORD)) {
487 				fd = __rpc_nconf2fd(nconf);
488 				/*
489 				 * Can't create a socket, assume that
490 				 * this family isn't configured in the kernel.
491 				 */
492 				if (fd < 0)
493 					continue;
494 				_close(fd);
495 				tmpnconf = nconf;
496 				if (!strcmp(nconf->nc_protofmly, NC_INET))
497 					hostname = IN4_LOCALHOST_STRING;
498 				else
499 					hostname = IN6_LOCALHOST_STRING;
500 			}
501 		}
502 		endnetconfig(nc_handle);
503 		if (tmpnconf == NULL) {
504 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
505 			mutex_unlock(&loopnconf_lock);
506 			return (NULL);
507 		}
508 		loopnconf = getnetconfigent(tmpnconf->nc_netid);
509 		/* loopnconf is never freed */
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 		 * Try UDP only - there are some portmappers out
743 		 * there that use UDP only.
744 		 */
745 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
746 			struct netconfig *newnconf;
747 
748 			if ((newnconf = getnetconfigent("udp")) == NULL) {
749 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
750 				return (NULL);
751 			}
752 			client = getclnthandle(host, newnconf, &parms.r_addr);
753 			freenetconfigent(newnconf);
754 		} else {
755 			client = getclnthandle(host, nconf, &parms.r_addr);
756 		}
757 		if (client == NULL)
758 			return (NULL);
759 
760 		/*
761 		 * Set version and retry timeout.
762 		 */
763 		CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
764 		CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
765 
766 		pmapparms.pm_prog = program;
767 		pmapparms.pm_vers = version;
768 		pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
769 					IPPROTO_UDP : IPPROTO_TCP;
770 		pmapparms.pm_port = 0;	/* not needed */
771 		clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
772 		    (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
773 		    (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
774 		    *tp);
775 		if (clnt_st != RPC_SUCCESS) {
776 			if ((clnt_st == RPC_PROGVERSMISMATCH) ||
777 				(clnt_st == RPC_PROGUNAVAIL))
778 				goto try_rpcbind; /* Try different versions */
779 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
780 			clnt_geterr(client, &rpc_createerr.cf_error);
781 			goto error;
782 		} else if (port == 0) {
783 			address = NULL;
784 			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
785 			goto error;
786 		}
787 		port = htons(port);
788 		CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
789 		if (((address = (struct netbuf *)
790 			malloc(sizeof (struct netbuf))) == NULL) ||
791 		    ((address->buf = (char *)
792 			malloc(remote.len)) == NULL)) {
793 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
794 			clnt_geterr(client, &rpc_createerr.cf_error);
795 			free(address);
796 			address = NULL;
797 			goto error;
798 		}
799 		memcpy(address->buf, remote.buf, remote.len);
800 		memcpy(&((char *)address->buf)[sizeof (short)],
801 				(char *)(void *)&port, sizeof (short));
802 		address->len = address->maxlen = remote.len;
803 		goto done;
804 	}
805 #endif				/* PORTMAP */
806 
807 try_rpcbind:
808 	/*
809 	 * Check if rpcbind is up.  This prevents needless delays when
810 	 * accessing applications such as the keyserver while booting
811 	 * disklessly.
812 	 */
813 	if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
814 		if (!__rpcbind_is_up()) {
815 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
816 			rpc_createerr.cf_error.re_errno = 0;
817 			goto error;
818 		}
819 		check_rpcbind = FALSE;
820 	}
821 
822 	/*
823 	 * Now we try version 4 and then 3.
824 	 * We also send the remote system the address we used to
825 	 * contact it in case it can help to connect back with us
826 	 */
827 	parms.r_prog = program;
828 	parms.r_vers = version;
829 	/*LINTED const castaway*/
830 	parms.r_owner = (char *) &nullstring[0];	/* not needed; */
831 							/* just for xdring */
832 	parms.r_netid = nconf->nc_netid; /* not really needed */
833 
834 	/*
835 	 * If a COTS transport is being used, try getting address via CLTS
836 	 * transport.  This works only with version 4.
837 	 */
838 	if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
839 			nconf->nc_semantics == NC_TPI_COTS) {
840 
841 		void *handle;
842 		struct netconfig *nconf_clts;
843 		rpcb_entry_list_ptr relp = NULL;
844 
845 		if (client == NULL) {
846 			/* This did not go through the above PORTMAP/TCP code */
847 			if ((handle = __rpc_setconf("datagram_v")) != NULL) {
848 				while ((nconf_clts = __rpc_getconf(handle))
849 					!= NULL) {
850 					if (strcmp(nconf_clts->nc_protofmly,
851 						nconf->nc_protofmly) != 0) {
852 						continue;
853 					}
854 					client = getclnthandle(host, nconf_clts,
855 							&parms.r_addr);
856 					break;
857 				}
858 				__rpc_endconf(handle);
859 			}
860 			if (client == NULL)
861 				goto regular_rpcbind;	/* Go the regular way */
862 		} else {
863 			/* This is a UDP PORTMAP handle.  Change to version 4 */
864 			vers = RPCBVERS4;
865 			CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
866 		}
867 		/*
868 		 * We also send the remote system the address we used to
869 		 * contact it in case it can help it connect back with us
870 		 */
871 		if (parms.r_addr == NULL) {
872 			/*LINTED const castaway*/
873 			parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
874 		}
875 
876 		CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
877 
878 		clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
879 		    (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
880 		    (xdrproc_t) xdr_rpcb_entry_list_ptr,
881 		    (char *)(void *)&relp, *tp);
882 		if (clnt_st == RPC_SUCCESS) {
883 			if ((address = got_entry(relp, nconf)) != NULL) {
884 				xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
885 				    (char *)(void *)&relp);
886 				CLNT_CONTROL(client, CLGET_SVC_ADDR,
887 					(char *)(void *)&servaddr);
888 				__rpc_fixup_addr(address, &servaddr);
889 				goto done;
890 			}
891 			/* Entry not found for this transport */
892 			xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
893 			    (char *)(void *)&relp);
894 			/*
895 			 * XXX: should have perhaps returned with error but
896 			 * since the remote machine might not always be able
897 			 * to send the address on all transports, we try the
898 			 * regular way with regular_rpcbind
899 			 */
900 			goto regular_rpcbind;
901 		} else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
902 			(clnt_st == RPC_PROGUNAVAIL)) {
903 			start_vers = RPCBVERS;	/* Try version 3 now */
904 			goto regular_rpcbind; /* Try different versions */
905 		} else {
906 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
907 			clnt_geterr(client, &rpc_createerr.cf_error);
908 			goto error;
909 		}
910 	}
911 
912 regular_rpcbind:
913 
914 	/* Now the same transport is to be used to get the address */
915 	if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
916 			(nconf->nc_semantics == NC_TPI_COTS))) {
917 		/* A CLTS type of client - destroy it */
918 		CLNT_DESTROY(client);
919 		client = NULL;
920 	}
921 
922 	if (client == NULL) {
923 		client = getclnthandle(host, nconf, &parms.r_addr);
924 		if (client == NULL) {
925 			goto error;
926 		}
927 	}
928 	if (parms.r_addr == NULL) {
929 		/*LINTED const castaway*/
930 		parms.r_addr = (char *) &nullstring[0];
931 	}
932 
933 	/* First try from start_vers and then version 3 (RPCBVERS) */
934 
935 	CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
936 	for (vers = start_vers;  vers >= RPCBVERS; vers--) {
937 		/* Set the version */
938 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
939 		clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
940 		    (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
941 		    (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
942 		if (clnt_st == RPC_SUCCESS) {
943 			if ((ua == NULL) || (ua[0] == 0)) {
944 				/* address unknown */
945 				rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
946 				goto error;
947 			}
948 			address = uaddr2taddr(nconf, ua);
949 #ifdef ND_DEBUG
950 			fprintf(stderr, "\tRemote address is [%s]\n", ua);
951 			if (!address)
952 				fprintf(stderr,
953 					"\tCouldn't resolve remote address!\n");
954 #endif
955 			xdr_free((xdrproc_t)xdr_wrapstring,
956 			    (char *)(void *)&ua);
957 
958 			if (! address) {
959 				/* We don't know about your universal address */
960 				rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
961 				goto error;
962 			}
963 			CLNT_CONTROL(client, CLGET_SVC_ADDR,
964 			    (char *)(void *)&servaddr);
965 			__rpc_fixup_addr(address, &servaddr);
966 			goto done;
967 		} else if (clnt_st == RPC_PROGVERSMISMATCH) {
968 			struct rpc_err rpcerr;
969 
970 			clnt_geterr(client, &rpcerr);
971 			if (rpcerr.re_vers.low > RPCBVERS4)
972 				goto error;  /* a new version, can't handle */
973 		} else if (clnt_st != RPC_PROGUNAVAIL) {
974 			/* Cant handle this error */
975 			rpc_createerr.cf_stat = clnt_st;
976 			clnt_geterr(client, &rpc_createerr.cf_error);
977 			goto error;
978 		}
979 	}
980 
981 error:
982 	if (client) {
983 		CLNT_DESTROY(client);
984 		client = NULL;
985 	}
986 done:
987 	if (nconf->nc_semantics != NC_TPI_CLTS) {
988 		/* This client is the connectionless one */
989 		if (client) {
990 			CLNT_DESTROY(client);
991 			client = NULL;
992 		}
993 	}
994 	if (clpp) {
995 		*clpp = client;
996 	} else if (client) {
997 		CLNT_DESTROY(client);
998 	}
999 	if (parms.r_addr != NULL && parms.r_addr != nullstring)
1000 		free(parms.r_addr);
1001 	return (address);
1002 }
1003 
1004 
1005 /*
1006  * Find the mapped address for program, version.
1007  * Calls the rpcbind service remotely to do the lookup.
1008  * Uses the transport specified in nconf.
1009  * Returns FALSE (0) if no map exists, else returns 1.
1010  *
1011  * Assuming that the address is all properly allocated
1012  */
1013 bool_t
1014 rpcb_getaddr(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf,
1015     struct netbuf *address, const char *host)
1016 {
1017 	struct netbuf *na;
1018 
1019 	if ((na = __rpcb_findaddr_timed(program, version,
1020 	    (struct netconfig *) nconf, (char *) host,
1021 	    (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1022 		return (FALSE);
1023 
1024 	if (na->len > address->maxlen) {
1025 		/* Too long address */
1026 		free(na->buf);
1027 		free(na);
1028 		rpc_createerr.cf_stat = RPC_FAILED;
1029 		return (FALSE);
1030 	}
1031 	memcpy(address->buf, na->buf, (size_t)na->len);
1032 	address->len = na->len;
1033 	free(na->buf);
1034 	free(na);
1035 	return (TRUE);
1036 }
1037 
1038 /*
1039  * Get a copy of the current maps.
1040  * Calls the rpcbind service remotely to get the maps.
1041  *
1042  * It returns only a list of the services
1043  * It returns NULL on failure.
1044  */
1045 rpcblist *
1046 rpcb_getmaps(const struct netconfig *nconf, const char *host)
1047 {
1048 	rpcblist_ptr head = NULL;
1049 	CLIENT *client;
1050 	enum clnt_stat clnt_st;
1051 	rpcvers_t vers = 0;
1052 
1053 	client = getclnthandle(host, nconf, NULL);
1054 	if (client == NULL) {
1055 		return (head);
1056 	}
1057 	clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1058 	    (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1059 	    (char *)(void *)&head, tottimeout);
1060 	if (clnt_st == RPC_SUCCESS)
1061 		goto done;
1062 
1063 	if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1064 	    (clnt_st != RPC_PROGUNAVAIL)) {
1065 		rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1066 		clnt_geterr(client, &rpc_createerr.cf_error);
1067 		goto done;
1068 	}
1069 
1070 	/* fall back to earlier version */
1071 	CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1072 	if (vers == RPCBVERS4) {
1073 		vers = RPCBVERS;
1074 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1075 		if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1076 		    (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1077 		    (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1078 			goto done;
1079 	}
1080 	rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1081 	clnt_geterr(client, &rpc_createerr.cf_error);
1082 
1083 done:
1084 	CLNT_DESTROY(client);
1085 	return (head);
1086 }
1087 
1088 /*
1089  * rpcbinder remote-call-service interface.
1090  * This routine is used to call the rpcbind remote call service
1091  * which will look up a service program in the address maps, and then
1092  * remotely call that routine with the given parameters. This allows
1093  * programs to do a lookup and call in one step.
1094  *
1095  * nconf    -Netconfig structure
1096  * host     - Remote host name
1097  * proc     - Remote proc identifiers
1098  * xdrargs, xdrres;  XDR routines
1099  * argsp, resp - Argument and Result
1100  * tout     - Timeout value for this call
1101  * addr_ptr - Preallocated netbuf address
1102  */
1103 enum clnt_stat
1104 rpcb_rmtcall(const struct netconfig *nconf, const char *host, rpcprog_t prog,
1105     rpcvers_t vers, rpcproc_t proc, xdrproc_t xdrargs, caddr_t argsp,
1106     xdrproc_t xdrres, caddr_t resp, struct timeval tout,
1107     const struct netbuf *addr_ptr)
1108 {
1109 	CLIENT *client;
1110 	enum clnt_stat stat;
1111 	struct r_rpcb_rmtcallargs a;
1112 	struct r_rpcb_rmtcallres r;
1113 	rpcvers_t rpcb_vers;
1114 
1115 	stat = 0;
1116 	client = getclnthandle(host, nconf, NULL);
1117 	if (client == NULL) {
1118 		return (RPC_FAILED);
1119 	}
1120 	/*LINTED const castaway*/
1121 	CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1122 	a.prog = prog;
1123 	a.vers = vers;
1124 	a.proc = proc;
1125 	a.args.args_val = argsp;
1126 	a.xdr_args = xdrargs;
1127 	r.addr = NULL;
1128 	r.results.results_val = resp;
1129 	r.xdr_res = xdrres;
1130 
1131 	for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1132 		CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1133 		stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1134 		    (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1135 		    (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1136 		if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1137 			struct netbuf *na;
1138 			/*LINTED const castaway*/
1139 			na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1140 			if (!na) {
1141 				stat = RPC_N2AXLATEFAILURE;
1142 				/*LINTED const castaway*/
1143 				((struct netbuf *) addr_ptr)->len = 0;
1144 				goto error;
1145 			}
1146 			if (na->len > addr_ptr->maxlen) {
1147 				/* Too long address */
1148 				stat = RPC_FAILED; /* XXX A better error no */
1149 				free(na->buf);
1150 				free(na);
1151 				/*LINTED const castaway*/
1152 				((struct netbuf *) addr_ptr)->len = 0;
1153 				goto error;
1154 			}
1155 			memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1156 			/*LINTED const castaway*/
1157 			((struct netbuf *)addr_ptr)->len = na->len;
1158 			free(na->buf);
1159 			free(na);
1160 			break;
1161 		} else if ((stat != RPC_PROGVERSMISMATCH) &&
1162 			    (stat != RPC_PROGUNAVAIL)) {
1163 			goto error;
1164 		}
1165 	}
1166 error:
1167 	CLNT_DESTROY(client);
1168 	if (r.addr)
1169 		xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1170 	return (stat);
1171 }
1172 
1173 /*
1174  * Gets the time on the remote host.
1175  * Returns 1 if succeeds else 0.
1176  */
1177 bool_t
1178 rpcb_gettime(const char *host, time_t *timep)
1179 {
1180 	CLIENT *client = NULL;
1181 	void *handle;
1182 	struct netconfig *nconf;
1183 	rpcvers_t vers;
1184 	enum clnt_stat st;
1185 
1186 
1187 	if ((host == NULL) || (host[0] == 0)) {
1188 		time(timep);
1189 		return (TRUE);
1190 	}
1191 
1192 	if ((handle = __rpc_setconf("netpath")) == NULL) {
1193 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1194 		return (FALSE);
1195 	}
1196 	rpc_createerr.cf_stat = RPC_SUCCESS;
1197 	while (client == NULL) {
1198 		if ((nconf = __rpc_getconf(handle)) == NULL) {
1199 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
1200 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1201 			break;
1202 		}
1203 		client = getclnthandle(host, nconf, NULL);
1204 		if (client)
1205 			break;
1206 	}
1207 	__rpc_endconf(handle);
1208 	if (client == (CLIENT *) NULL) {
1209 		return (FALSE);
1210 	}
1211 
1212 	st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1213 		(xdrproc_t) xdr_void, NULL,
1214 		(xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1215 
1216 	if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1217 		CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1218 		if (vers == RPCBVERS4) {
1219 			/* fall back to earlier version */
1220 			vers = RPCBVERS;
1221 			CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1222 			st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1223 				(xdrproc_t) xdr_void, NULL,
1224 				(xdrproc_t) xdr_int, (char *)(void *)timep,
1225 				tottimeout);
1226 		}
1227 	}
1228 	CLNT_DESTROY(client);
1229 	return (st == RPC_SUCCESS? TRUE: FALSE);
1230 }
1231 
1232 /*
1233  * Converts taddr to universal address.  This routine should never
1234  * really be called because local n2a libraries are always provided.
1235  */
1236 char *
1237 rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
1238 {
1239 	CLIENT *client;
1240 	char *uaddr = NULL;
1241 
1242 
1243 	/* parameter checking */
1244 	if (nconf == NULL) {
1245 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1246 		return (NULL);
1247 	}
1248 	if (taddr == NULL) {
1249 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1250 		return (NULL);
1251 	}
1252 	client = local_rpcb();
1253 	if (! client) {
1254 		return (NULL);
1255 	}
1256 
1257 	CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1258 	    (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1259 	    (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1260 	CLNT_DESTROY(client);
1261 	return (uaddr);
1262 }
1263 
1264 /*
1265  * Converts universal address to netbuf.  This routine should never
1266  * really be called because local n2a libraries are always provided.
1267  */
1268 struct netbuf *
1269 rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
1270 {
1271 	CLIENT *client;
1272 	struct netbuf *taddr;
1273 
1274 
1275 	/* parameter checking */
1276 	if (nconf == NULL) {
1277 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1278 		return (NULL);
1279 	}
1280 	if (uaddr == NULL) {
1281 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1282 		return (NULL);
1283 	}
1284 	client = local_rpcb();
1285 	if (! client) {
1286 		return (NULL);
1287 	}
1288 
1289 	taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1290 	if (taddr == NULL) {
1291 		CLNT_DESTROY(client);
1292 		return (NULL);
1293 	}
1294 	if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1295 	    (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1296 	    (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1297 	    tottimeout) != RPC_SUCCESS) {
1298 		free(taddr);
1299 		taddr = NULL;
1300 	}
1301 	CLNT_DESTROY(client);
1302 	return (taddr);
1303 }
1304