xref: /titanic_51/usr/src/lib/libnsl/rpc/rpcb_clnt.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28 /*
29  * Portions of this source code were derived from Berkeley
30  * 4.3 BSD under license from the Regents of the University of
31  * California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 /*
37  * rpcb_clnt.c
38  * interface to rpcbind rpc service.
39  *
40  */
41 
42 #include "mt.h"
43 #include "rpc_mt.h"
44 #include <assert.h>
45 #include <rpc/rpc.h>
46 #include <rpc/trace.h>
47 #include <rpc/rpcb_prot.h>
48 #include <netconfig.h>
49 #include <netdir.h>
50 #include <rpc/nettype.h>
51 #include <syslog.h>
52 #ifdef PORTMAP
53 #include <netinet/in.h>		/* FOR IPPROTO_TCP/UDP definitions */
54 #include <rpc/pmap_prot.h>
55 #endif
56 #ifdef ND_DEBUG
57 #include <stdio.h>
58 #endif
59 #include <sys/utsname.h>
60 #include <errno.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 
65 #if !(defined(__i386) && !defined(__amd64))
66 extern int _uname();
67 #endif
68 
69 static struct timeval tottimeout = { 60, 0 };
70 static const struct timeval rmttimeout = { 3, 0 };
71 static struct timeval rpcbrmttime = { 15, 0 };
72 
73 #ifdef __STDC__
74 bool_t xdr_wrapstring(XDR *, char **);
75 #else
76 extern  bool_t xdr_wrapstring();
77 #endif
78 
79 static const char nullstring[] = "\000";
80 
81 extern CLIENT *_clnt_tli_create_timed(int, const struct netconfig *,
82 			struct netbuf *, rpcprog_t, rpcvers_t, uint_t, uint_t,
83 			const struct timeval *);
84 
85 static CLIENT *_getclnthandle_timed(char *, struct netconfig *, char **,
86 			struct timeval *);
87 
88 
89 /*
90  * The life time of a cached entry should not exceed 5 minutes
91  * since automountd attempts an unmount every 5 minutes.
92  * It is arbitrarily set a little lower (3 min = 180 sec)
93  * to reduce the time during which an entry is stale.
94  */
95 #define	CACHE_TTL 180
96 #define	CACHESIZE 6
97 
98 struct address_cache {
99 	char *ac_host;
100 	char *ac_netid;
101 	char *ac_uaddr;
102 	struct netbuf *ac_taddr;
103 	struct address_cache *ac_next;
104 	time_t ac_maxtime;
105 };
106 
107 static struct address_cache *front;
108 static int cachesize;
109 
110 extern int lowvers;
111 extern int authdes_cachesz;
112 /*
113  * This routine adjusts the timeout used for calls to the remote rpcbind.
114  * Also, this routine can be used to set the use of portmapper version 2
115  * only when doing rpc_broadcasts
116  * These are private routines that may not be provided in future releases.
117  */
118 bool_t
119 __rpc_control(request, info)
120 	int	request;
121 	void	*info;
122 {
123 	switch (request) {
124 	case CLCR_GET_RPCB_TIMEOUT:
125 		*(struct timeval *)info = tottimeout;
126 		break;
127 	case CLCR_SET_RPCB_TIMEOUT:
128 		tottimeout = *(struct timeval *)info;
129 		break;
130 	case CLCR_GET_LOWVERS:
131 		*(int *)info = lowvers;
132 		break;
133 	case CLCR_SET_LOWVERS:
134 		lowvers = *(int *)info;
135 		break;
136 	case CLCR_GET_RPCB_RMTTIME:
137 		*(struct timeval *)info = rpcbrmttime;
138 		break;
139 	case CLCR_SET_RPCB_RMTTIME:
140 		rpcbrmttime = *(struct timeval *)info;
141 		break;
142 	case CLCR_GET_CRED_CACHE_SZ:
143 		*(int *)info = authdes_cachesz;
144 		break;
145 	case CLCR_SET_CRED_CACHE_SZ:
146 		authdes_cachesz = *(int *)info;
147 		break;
148 	default:
149 		return (FALSE);
150 	}
151 	return (TRUE);
152 }
153 
154 /*
155  *	It might seem that a reader/writer lock would be more reasonable here.
156  *	However because getclnthandle(), the only user of the cache functions,
157  *	may do a delete_cache() operation if a check_cache() fails to return an
158  *	address useful to clnt_tli_create(), we may as well use a mutex.
159  */
160 /*
161  * As it turns out, if the cache lock is *not* a reader/writer lock, we will
162  * block all clnt_create's if we are trying to connect to a host that's down,
163  * since the lock will be held all during that time.
164  */
165 extern rwlock_t	rpcbaddr_cache_lock;
166 
167 /*
168  * The routines check_cache(), add_cache(), delete_cache() manage the
169  * cache of rpcbind addresses for (host, netid).
170  */
171 
172 static struct address_cache *
173 check_cache(host, netid)
174 	char *host, *netid;
175 {
176 	struct address_cache *cptr;
177 
178 	/* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
179 
180 	trace1(TR_check_cache, 0);
181 	assert(RW_READ_HELD(&rpcbaddr_cache_lock));
182 	for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
183 		if ((strcmp(cptr->ac_host, host) == 0) &&
184 		    (strcmp(cptr->ac_netid, netid) == 0) &&
185 			(time(NULL) <= cptr->ac_maxtime)) {
186 #ifdef ND_DEBUG
187 			fprintf(stderr, "Found cache entry for %s: %s\n",
188 				host, netid);
189 #endif
190 			trace1(TR_check_cache, 1);
191 			return (cptr);
192 		}
193 	}
194 	trace1(TR_check_cache, 1);
195 	return ((struct address_cache *)NULL);
196 }
197 
198 static void
199 delete_cache(addr)
200 	struct netbuf *addr;
201 {
202 	struct address_cache *cptr, *prevptr = NULL;
203 
204 	/* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
205 	trace1(TR_delete_cache, 0);
206 	assert(RW_WRITE_HELD(&rpcbaddr_cache_lock));
207 	for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
208 		if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
209 			free(cptr->ac_host);
210 			free(cptr->ac_netid);
211 			free(cptr->ac_taddr->buf);
212 			free(cptr->ac_taddr);
213 			if (cptr->ac_uaddr)
214 				free(cptr->ac_uaddr);
215 			if (prevptr)
216 				prevptr->ac_next = cptr->ac_next;
217 			else
218 				front = cptr->ac_next;
219 			free(cptr);
220 			cachesize--;
221 			break;
222 		}
223 		prevptr = cptr;
224 	}
225 	trace1(TR_delete_cache, 1);
226 }
227 
228 static void
229 add_cache(host, netid, taddr, uaddr)
230 	char *host, *netid, *uaddr;
231 	struct netbuf *taddr;
232 {
233 	struct address_cache  *ad_cache, *cptr, *prevptr;
234 
235 	trace1(TR_add_cache, 0);
236 	ad_cache = (struct address_cache *)
237 			malloc(sizeof (struct address_cache));
238 	if (!ad_cache) {
239 		goto memerr;
240 	}
241 	ad_cache->ac_maxtime = time(NULL) + CACHE_TTL;
242 	ad_cache->ac_host = strdup(host);
243 	ad_cache->ac_netid = strdup(netid);
244 	ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
245 	ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
246 	if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
247 		(uaddr && !ad_cache->ac_uaddr)) {
248 		goto memerr1;
249 	}
250 
251 	ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
252 	ad_cache->ac_taddr->buf = (char *)malloc(taddr->len);
253 	if (ad_cache->ac_taddr->buf == NULL) {
254 		goto memerr1;
255 	}
256 
257 	memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
258 #ifdef ND_DEBUG
259 	fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
260 #endif
261 
262 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
263 
264 	rw_wrlock(&rpcbaddr_cache_lock);
265 	if (cachesize < CACHESIZE) {
266 		ad_cache->ac_next = front;
267 		front = ad_cache;
268 		cachesize++;
269 	} else {
270 		/* Free the last entry */
271 		cptr = front;
272 		prevptr = NULL;
273 		while (cptr->ac_next) {
274 			prevptr = cptr;
275 			cptr = cptr->ac_next;
276 		}
277 
278 #ifdef ND_DEBUG
279 		fprintf(stderr, "Deleted from cache: %s : %s\n",
280 			cptr->ac_host, cptr->ac_netid);
281 #endif
282 		free(cptr->ac_host);
283 		free(cptr->ac_netid);
284 		free(cptr->ac_taddr->buf);
285 		free(cptr->ac_taddr);
286 		if (cptr->ac_uaddr)
287 			free(cptr->ac_uaddr);
288 
289 		if (prevptr) {
290 			prevptr->ac_next = NULL;
291 			ad_cache->ac_next = front;
292 			front = ad_cache;
293 		} else {
294 			front = ad_cache;
295 			ad_cache->ac_next = NULL;
296 		}
297 		free(cptr);
298 	}
299 	rw_unlock(&rpcbaddr_cache_lock);
300 	trace1(TR_add_cache, 1);
301 	return;
302 memerr1:
303 	if (ad_cache->ac_host)
304 		free(ad_cache->ac_host);
305 	if (ad_cache->ac_netid)
306 		free(ad_cache->ac_netid);
307 	if (ad_cache->ac_uaddr)
308 		free(ad_cache->ac_uaddr);
309 	if (ad_cache->ac_taddr)
310 		free(ad_cache->ac_taddr);
311 	free(ad_cache);
312 memerr:
313 	syslog(LOG_ERR, "add_cache : out of memory.");
314 }
315 
316 /*
317  * This routine will return a client handle that is connected to the
318  * rpcbind. Returns NULL on error and free's everything.
319  */
320 static CLIENT *
321 getclnthandle(host, nconf, targaddr)
322 	char *host;
323 	struct netconfig *nconf;
324 	char **targaddr;
325 {
326 	return (_getclnthandle_timed(host, nconf, targaddr, NULL));
327 }
328 
329 /*
330  * Same as getclnthandle() except it takes an extra timeout argument.
331  * This is for bug 4049792: clnt_create_timed does not timeout.
332  *
333  * If tp is NULL, use default timeout to get a client handle.
334  */
335 static CLIENT *
336 _getclnthandle_timed(host, nconf, targaddr, tp)
337 	char *host;
338 	struct netconfig *nconf;
339 	char **targaddr;
340 	struct timeval *tp;
341 {
342 	CLIENT *client = NULL;
343 	struct netbuf *addr;
344 	struct netbuf addr_to_delete;
345 	struct nd_addrlist *nas;
346 	struct nd_hostserv rpcbind_hs;
347 	struct address_cache *ad_cache;
348 	char *tmpaddr;
349 	int neterr;
350 	int j;
351 
352 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  ad_cache */
353 
354 	trace1(TR_getclnthandle_timed, 0);
355 	/* Get the address of the rpcbind.  Check cache first */
356 	addr_to_delete.len = 0;
357 	rw_rdlock(&rpcbaddr_cache_lock);
358 	ad_cache = check_cache(host, nconf->nc_netid);
359 	if (ad_cache != NULL) {
360 		addr = ad_cache->ac_taddr;
361 		client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr,
362 				RPCBPROG, RPCBVERS4, 0, 0, tp);
363 		if (client != NULL) {
364 			if (targaddr) {
365 				/*
366 				 * case where a client handle is created
367 				 * without a targaddr and the handle is
368 				 * requested with a targaddr
369 				 */
370 				if (ad_cache->ac_uaddr != NULL) {
371 					*targaddr = strdup(ad_cache->ac_uaddr);
372 					if (*targaddr == NULL) {
373 						syslog(LOG_ERR,
374 						"_getclnthandle_timed: strdup "
375 						"failed.");
376 						rpc_createerr.cf_stat =
377 							RPC_SYSTEMERROR;
378 						rw_unlock(&rpcbaddr_cache_lock);
379 						trace1(TR_getclnthandle_timed,
380 							1);
381 						return ((CLIENT *)NULL);
382 					}
383 				}
384 				else
385 					*targaddr = NULL;
386 			}
387 			rw_unlock(&rpcbaddr_cache_lock);
388 			trace1(TR_getclnthandle_timed, 1);
389 			return (client);
390 		} else {
391 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
392 				rw_unlock(&rpcbaddr_cache_lock);
393 				trace1(TR_getclnthandle_timed, 1);
394 				return ((CLIENT *)NULL);
395 			}
396 		}
397 		addr_to_delete.len = addr->len;
398 		addr_to_delete.buf = (char *)malloc(addr->len);
399 		if (addr_to_delete.buf == NULL) {
400 			addr_to_delete.len = 0;
401 		} else {
402 			memcpy(addr_to_delete.buf, addr->buf, addr->len);
403 		}
404 	}
405 	rw_unlock(&rpcbaddr_cache_lock);
406 	if (addr_to_delete.len != 0) {
407 		/*
408 		 * Assume this may be due to cache data being
409 		 *  outdated
410 		 */
411 		rw_wrlock(&rpcbaddr_cache_lock);
412 		delete_cache(&addr_to_delete);
413 		rw_unlock(&rpcbaddr_cache_lock);
414 		free(addr_to_delete.buf);
415 	}
416 	rpcbind_hs.h_host = host;
417 	rpcbind_hs.h_serv = "rpcbind";
418 #ifdef ND_DEBUG
419 	fprintf(stderr, "rpcbind client routines: diagnostics :\n");
420 	fprintf(stderr, "\tGetting address for (%s, %s, %s) ... \n",
421 		rpcbind_hs.h_host, rpcbind_hs.h_serv, nconf->nc_netid);
422 #endif
423 
424 	if ((neterr = netdir_getbyname(nconf, &rpcbind_hs, &nas)) != 0) {
425 		if (neterr == ND_NOHOST)
426 			rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
427 		else
428 			rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
429 		trace1(TR_getclnthandle_timed, 1);
430 		return ((CLIENT *)NULL);
431 	}
432 	/* XXX nas should perhaps be cached for better performance */
433 
434 	for (j = 0; j < nas->n_cnt; j++) {
435 		addr = &(nas->n_addrs[j]);
436 #ifdef ND_DEBUG
437 {
438 	int i;
439 	char *ua;
440 
441 	ua = taddr2uaddr(nconf, &(nas->n_addrs[j]));
442 	fprintf(stderr, "Got it [%s]\n", ua);
443 	free(ua);
444 
445 	fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
446 		addr->len, addr->maxlen);
447 	fprintf(stderr, "\tAddress is ");
448 	for (i = 0; i < addr->len; i++)
449 		fprintf(stderr, "%u.", addr->buf[i]);
450 	fprintf(stderr, "\n");
451 }
452 #endif
453 	client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr, RPCBPROG,
454 				RPCBVERS4, 0, 0, tp);
455 	if (client)
456 		break;
457 	}
458 #ifdef ND_DEBUG
459 	if (! client) {
460 		clnt_pcreateerror("rpcbind clnt interface");
461 	}
462 #endif
463 
464 	if (client) {
465 		tmpaddr = targaddr ? taddr2uaddr(nconf, addr) : NULL;
466 		add_cache(host, nconf->nc_netid, addr, tmpaddr);
467 		if (targaddr) {
468 			*targaddr = tmpaddr;
469 		}
470 	}
471 	netdir_free((char *)nas, ND_ADDRLIST);
472 	trace1(TR_getclnthandle_timed, 1);
473 	return (client);
474 }
475 
476 /*
477  * This routine will return a client handle that is connected to the local
478  * rpcbind. Returns NULL on error and free's everything.
479  */
480 static CLIENT *
481 local_rpcb()
482 {
483 	CLIENT *client;
484 	static struct netconfig *loopnconf;
485 	static char *hostname;
486 	extern mutex_t loopnconf_lock;
487 
488 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
489 	trace1(TR_local_rpcb, 0);
490 	mutex_lock(&loopnconf_lock);
491 	if (loopnconf == NULL) {
492 		struct utsname utsname;
493 		struct netconfig *nconf, *tmpnconf = NULL;
494 		void *nc_handle;
495 
496 		if (hostname == (char *)NULL) {
497 #if defined(__i386) && !defined(__amd64)
498 			if ((_nuname(&utsname) == -1) ||
499 #else
500 			if ((_uname(&utsname) == -1) ||
501 #endif
502 			    ((hostname = strdup(utsname.nodename)) == NULL)) {
503 				syslog(LOG_ERR, "local_rpcb : strdup failed.");
504 				rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
505 				mutex_unlock(&loopnconf_lock);
506 				trace1(TR_local_rpcb, 1);
507 				return ((CLIENT *) NULL);
508 			}
509 		}
510 		nc_handle = setnetconfig();
511 		if (nc_handle == NULL) {
512 			/* fails to open netconfig file */
513 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
514 			mutex_unlock(&loopnconf_lock);
515 			trace1(TR_local_rpcb, 1);
516 			return (NULL);
517 		}
518 		while (nconf = getnetconfig(nc_handle)) {
519 			if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
520 				tmpnconf = nconf;
521 				if (nconf->nc_semantics == NC_TPI_CLTS)
522 					break;
523 			}
524 		}
525 		if (tmpnconf == NULL) {
526 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
527 			mutex_unlock(&loopnconf_lock);
528 			trace1(TR_local_rpcb, 1);
529 			return (NULL);
530 		}
531 		loopnconf = getnetconfigent(tmpnconf->nc_netid);
532 		/* loopnconf is never freed */
533 		endnetconfig(nc_handle);
534 	}
535 	mutex_unlock(&loopnconf_lock);
536 	client = getclnthandle(hostname, loopnconf, (char **)NULL);
537 	trace1(TR_local_rpcb, 1);
538 	return (client);
539 }
540 
541 /*
542  * Set a mapping between program, version and address.
543  * Calls the rpcbind service to do the mapping.
544  */
545 bool_t
546 rpcb_set(program, version, nconf, address)
547 	rpcprog_t program;
548 	rpcvers_t version;
549 	const struct netconfig *nconf;	/* Network structure of transport */
550 	const struct netbuf *address;		/* Services netconfig address */
551 {
552 	CLIENT *client;
553 	bool_t rslt = FALSE;
554 	RPCB parms;
555 	char uidbuf[32];
556 
557 	trace3(TR_rpcb_set, 0, program, version);
558 	/* parameter checking */
559 	if (nconf == (struct netconfig *)NULL) {
560 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
561 		trace3(TR_rpcb_set, 1, program, version);
562 		return (FALSE);
563 	}
564 	if (address == NULL) {
565 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
566 		trace3(TR_rpcb_set, 1, program, version);
567 		return (FALSE);
568 	}
569 	client = local_rpcb();
570 	if (! client) {
571 		trace3(TR_rpcb_set, 1, program, version);
572 		return (FALSE);
573 	}
574 
575 	parms.r_addr = taddr2uaddr((struct netconfig *)nconf, (struct netbuf
576 	*) address); /* convert to universal */
577 	if (!parms.r_addr) {
578 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
579 		trace3(TR_rpcb_set, 1, program, version);
580 		return (FALSE); /* no universal address */
581 	}
582 	parms.r_prog = program;
583 	parms.r_vers = version;
584 	parms.r_netid = nconf->nc_netid;
585 	/*
586 	 * Though uid is not being used directly, we still send it for
587 	 * completeness.  For non-unix platforms, perhaps some other
588 	 * string or an empty string can be sent.
589 	 */
590 	(void) sprintf(uidbuf, "%d", geteuid());
591 	parms.r_owner = uidbuf;
592 
593 	CLNT_CALL(client, RPCBPROC_SET, (xdrproc_t)xdr_rpcb, (char *)&parms,
594 			(xdrproc_t)xdr_bool, (char *)&rslt, tottimeout);
595 
596 	CLNT_DESTROY(client);
597 	free(parms.r_addr);
598 	trace3(TR_rpcb_set, 1, program, version);
599 	return (rslt);
600 }
601 
602 /*
603  * Remove the mapping between program, version and netbuf address.
604  * Calls the rpcbind service to do the un-mapping.
605  * If netbuf is NULL, unset for all the transports, otherwise unset
606  * only for the given transport.
607  */
608 bool_t
609 rpcb_unset(program, version, nconf)
610 	rpcprog_t program;
611 	rpcvers_t version;
612 	const struct netconfig *nconf;
613 {
614 	CLIENT *client;
615 	bool_t rslt = FALSE;
616 	RPCB parms;
617 	char uidbuf[32];
618 
619 	trace3(TR_rpcb_unset, 0, program, version);
620 	client = local_rpcb();
621 	if (! client) {
622 		trace3(TR_rpcb_unset, 1, program, version);
623 		return (FALSE);
624 	}
625 
626 	parms.r_prog = program;
627 	parms.r_vers = version;
628 	if (nconf)
629 		parms.r_netid = nconf->nc_netid;
630 	else
631 		parms.r_netid = (char *)&nullstring[0]; /* unsets  all */
632 	parms.r_addr = (char *)&nullstring[0];
633 	(void) sprintf(uidbuf, "%d", geteuid());
634 	parms.r_owner = uidbuf;
635 
636 	CLNT_CALL(client, RPCBPROC_UNSET, (xdrproc_t)xdr_rpcb, (char *)&parms,
637 			(xdrproc_t)xdr_bool, (char *)&rslt, tottimeout);
638 
639 	CLNT_DESTROY(client);
640 	trace3(TR_rpcb_unset, 1, program, version);
641 	return (rslt);
642 }
643 
644 /*
645  * From the merged list, find the appropriate entry
646  */
647 static struct netbuf *
648 got_entry(relp, nconf)
649 	rpcb_entry_list_ptr relp;
650 	struct netconfig *nconf;
651 {
652 	struct netbuf *na = NULL;
653 	rpcb_entry_list_ptr sp;
654 	rpcb_entry *rmap;
655 
656 	trace1(TR_got_entry, 0);
657 	for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
658 		rmap = &sp->rpcb_entry_map;
659 		if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
660 		    (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
661 		    (nconf->nc_semantics == rmap->r_nc_semantics) &&
662 		    (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
663 			na = uaddr2taddr(nconf, rmap->r_maddr);
664 #ifdef ND_DEBUG
665 			fprintf(stderr, "\tRemote address is [%s].\n",
666 				rmap->r_maddr);
667 			if (!na)
668 				fprintf(stderr,
669 				    "\tCouldn't resolve remote address!\n");
670 #endif
671 			break;
672 		}
673 	}
674 	trace1(TR_got_entry, 1);
675 	return (na);
676 }
677 
678 /*
679  * Quick check to see if rpcbind is up.  Tries to connect over
680  * local transport.
681  */
682 bool_t
683 __rpcbind_is_up()
684 {
685 	struct utsname name;
686 	char uaddr[SYS_NMLN];
687 	struct netbuf *addr;
688 	int fd;
689 	struct t_call *sndcall;
690 	struct netconfig *netconf;
691 	bool_t res;
692 
693 #if defined(__i386) && !defined(__amd64)
694 	if (_nuname(&name) == -1)
695 #else
696 	if (_uname(&name) == -1)
697 #endif
698 		return (TRUE);
699 
700 	if ((fd = t_open("/dev/ticotsord", O_RDWR, NULL)) == -1)
701 		return (TRUE);
702 
703 	if (t_bind(fd, NULL, NULL) == -1) {
704 		t_close(fd);
705 		return (TRUE);
706 	}
707 
708 	if ((sndcall = (struct t_call *)t_alloc(fd, T_CALL, 0)) == NULL) {
709 		t_close(fd);
710 		return (TRUE);
711 	}
712 
713 	uaddr[0] = '\0';
714 	strcpy(uaddr, name.nodename);
715 	strcat(uaddr, ".rpc");
716 	if ((netconf = getnetconfigent("ticotsord")) == NULL) {
717 		t_free((char *)sndcall, T_CALL);
718 		t_close(fd);
719 		return (FALSE);
720 	}
721 	addr = uaddr2taddr(netconf, uaddr);
722 	freenetconfigent(netconf);
723 	if (addr == NULL || addr->buf == NULL) {
724 		if (addr)
725 			free((char *)addr);
726 		t_free((char *)sndcall, T_CALL);
727 		t_close(fd);
728 		return (FALSE);
729 	}
730 	sndcall->addr.maxlen = addr->maxlen;
731 	sndcall->addr.len = addr->len;
732 	sndcall->addr.buf = addr->buf;
733 
734 	if (t_connect(fd, sndcall, NULL) == -1)
735 		res = FALSE;
736 	else
737 		res = TRUE;
738 
739 	sndcall->addr.maxlen = sndcall->addr.len = 0;
740 	sndcall->addr.buf = NULL;
741 	t_free((char *)sndcall, T_CALL);
742 	free((char *)addr->buf);
743 	free((char *)addr);
744 	t_close(fd);
745 
746 	return (res);
747 }
748 
749 
750 /*
751  * An internal function which optimizes rpcb_getaddr function.  It also
752  * returns the client handle that it uses to contact the remote rpcbind.
753  *
754  * The algorithm used: If the transports is TCP or UDP, it first tries
755  * version 2 (portmap), 4 and then 3 (svr4).  This order should be
756  * changed in the next OS release to 4, 2 and 3.  We are assuming that by
757  * that time, version 4 would be available on many machines on the network.
758  * With this algorithm, we get performance as well as a plan for
759  * obsoleting version 2.
760  *
761  * For all other transports, the algorithm remains as 4 and then 3.
762  *
763  * XXX: Due to some problems with t_connect(), we do not reuse the same client
764  * handle for COTS cases and hence in these cases we do not return the
765  * client handle.  This code will change if t_connect() ever
766  * starts working properly.  Also look under clnt_vc.c.
767  */
768 struct netbuf *
769 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
770 	rpcprog_t program;
771 	rpcvers_t version;
772 	struct netconfig *nconf;
773 	char *host;
774 	CLIENT **clpp;
775 	struct timeval *tp;
776 {
777 	static bool_t check_rpcbind = TRUE;
778 	CLIENT *client = NULL;
779 	RPCB parms;
780 	enum clnt_stat clnt_st;
781 	char *ua = NULL;
782 	uint_t vers;
783 	struct netbuf *address = NULL;
784 	uint_t start_vers = RPCBVERS4;
785 
786 	trace3(TR_rpcb_findaddr, 0, program, version);
787 	/* parameter checking */
788 	if (nconf == (struct netconfig *)NULL) {
789 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
790 		trace3(TR_rpcb_findaddr, 1, program, version);
791 		return (NULL);
792 	}
793 
794 	parms.r_addr = NULL;
795 
796 	/*
797 	 * Use default total timeout if no timeout is specified.
798 	 */
799 	if (tp == NULL)
800 		tp = &tottimeout;
801 
802 #ifdef PORTMAP
803 	/* Try version 2 for TCP or UDP */
804 	if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
805 		ushort_t port = 0;
806 		struct netbuf remote;
807 		uint_t pmapvers = 2;
808 		struct pmap pmapparms;
809 
810 		/*
811 		 * Try UDP only - there are some portmappers out
812 		 * there that use UDP only.
813 		 */
814 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
815 			struct netconfig *newnconf;
816 			void *handle;
817 
818 			if ((handle = __rpc_setconf("udp")) == NULL) {
819 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
820 				trace3(TR_rpcb_findaddr, 1, program, version);
821 				return (NULL);
822 			}
823 
824 			/*
825 			 * The following to reinforce that you can
826 			 * only request for remote address through
827 			 * the same transport you are requesting.
828 			 * ie. requesting unversial address
829 			 * of IPv4 has to be carried through IPv4.
830 			 * Can't use IPv6 to send out the request.
831 			 * The mergeaddr in rpcbind can't handle
832 			 * this.
833 			 */
834 			while (1) {
835 				if ((newnconf = __rpc_getconf(handle))
836 				    == NULL) {
837 					__rpc_endconf(handle);
838 					rpc_createerr.cf_stat =\
839 					    RPC_UNKNOWNPROTO;
840 					trace3(TR_rpcb_findaddr,
841 					    1, program, version);
842 					return (NULL);
843 				}
844 				/*
845 				 * here check the protocol family to
846 				 * be consistent with the request one
847 				 */
848 				if (strcmp(newnconf->nc_protofmly,
849 				    nconf->nc_protofmly) == NULL)
850 					break;
851 
852 			}
853 
854 			client = _getclnthandle_timed(host, newnconf,
855 					&parms.r_addr, tp);
856 			__rpc_endconf(handle);
857 		} else {
858 			client = _getclnthandle_timed(host, nconf,
859 					&parms.r_addr, tp);
860 		}
861 		if (client == (CLIENT *)NULL) {
862 			trace3(TR_rpcb_findaddr, 1, program, version);
863 			return (NULL);
864 		}
865 
866 		/*
867 		 * Set version and retry timeout.
868 		 */
869 		CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
870 		CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
871 
872 		pmapparms.pm_prog = program;
873 		pmapparms.pm_vers = version;
874 		pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
875 				    IPPROTO_UDP : IPPROTO_TCP;
876 		pmapparms.pm_port = 0;	/* not needed */
877 		clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
878 				    (xdrproc_t)xdr_pmap, (caddr_t)&pmapparms,
879 				    (xdrproc_t)xdr_u_short, (caddr_t)&port,
880 				    *tp);
881 		if (clnt_st != RPC_SUCCESS) {
882 			if ((clnt_st == RPC_PROGVERSMISMATCH) ||
883 			    (clnt_st == RPC_PROGUNAVAIL))
884 				goto try_rpcbind; /* Try different versions */
885 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
886 			clnt_geterr(client, &rpc_createerr.cf_error);
887 			goto error;
888 		} else if (port == 0) {
889 			address = NULL;
890 			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
891 			goto error;
892 		}
893 		port = htons(port);
894 		CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
895 		if (((address = (struct netbuf *)
896 		    malloc(sizeof (struct netbuf))) == NULL) ||
897 		    ((address->buf = (char *)
898 		    malloc(remote.len)) == NULL)) {
899 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
900 			clnt_geterr(client, &rpc_createerr.cf_error);
901 			if (address) {
902 				free(address);
903 				address = NULL;
904 			}
905 			goto error;
906 		}
907 		memcpy(address->buf, remote.buf, remote.len);
908 		memcpy((char *)&address->buf[sizeof (short)],
909 		    (char *)&port, sizeof (short));
910 		address->len = address->maxlen = remote.len;
911 		goto done;
912 	}
913 #endif
914 
915 try_rpcbind:
916 	/*
917 	 * Check if rpcbind is up.  This prevents needless delays when
918 	 * accessing applications such as the keyserver while booting
919 	 * disklessly.
920 	 */
921 	if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
922 		if (!__rpcbind_is_up()) {
923 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
924 			rpc_createerr.cf_error.re_errno = 0;
925 			rpc_createerr.cf_error.re_terrno = 0;
926 			goto error;
927 		}
928 		check_rpcbind = FALSE;
929 	}
930 
931 	/*
932 	 * Now we try version 4 and then 3.
933 	 * We also send the remote system the address we used to
934 	 * contact it in case it can help to connect back with us
935 	 */
936 	parms.r_prog = program;
937 	parms.r_vers = version;
938 	parms.r_owner = (char *)&nullstring[0];	/* not needed; */
939 	/* just for xdring */
940 	parms.r_netid = nconf->nc_netid; /* not really needed */
941 
942 	/*
943 	 * If a COTS transport is being used, try getting address via CLTS
944 	 * transport.  This works only with version 4.
945 	 */
946 	if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
947 	    nconf->nc_semantics == NC_TPI_COTS) {
948 		void *handle;
949 		struct netconfig *nconf_clts;
950 		rpcb_entry_list_ptr relp = NULL;
951 
952 		if (client == NULL) {
953 			/* This did not go through the above PORTMAP/TCP code */
954 			if ((handle = __rpc_setconf("datagram_v")) != NULL) {
955 				while ((nconf_clts = __rpc_getconf(handle))
956 				    != NULL) {
957 					if (strcmp(nconf_clts->nc_protofmly,
958 					    nconf->nc_protofmly) != 0) {
959 						continue;
960 					}
961 					client = _getclnthandle_timed(host,
962 						nconf_clts, &parms.r_addr,
963 						tp);
964 					break;
965 				}
966 				__rpc_endconf(handle);
967 			}
968 			if (client == (CLIENT *)NULL)
969 				goto regular_rpcbind;	/* Go the regular way */
970 		} else {
971 			/* This is a UDP PORTMAP handle.  Change to version 4 */
972 			vers = RPCBVERS4;
973 			CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
974 		}
975 		/*
976 		 * We also send the remote system the address we used to
977 		 * contact it in case it can help it connect back with us
978 		 */
979 		if (parms.r_addr == NULL) {
980 			parms.r_addr = strdup(""); /* for XDRing */
981 			if (parms.r_addr == NULL) {
982 				syslog(LOG_ERR, "__rpcb_findaddr_timed: "
983 					"strdup failed.");
984 				rpc_createerr.cf_stat = RPC_SYSTEMERROR;
985 				address = NULL;
986 				goto error;
987 			}
988 		}
989 
990 		CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
991 
992 		clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
993 				    (xdrproc_t)xdr_rpcb, (char *)&parms,
994 				    (xdrproc_t)xdr_rpcb_entry_list_ptr,
995 				    (char *)&relp, *tp);
996 		if (clnt_st == RPC_SUCCESS) {
997 			if (address = got_entry(relp, nconf)) {
998 				xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
999 					(char *)&relp);
1000 				goto done;
1001 			}
1002 			/* Entry not found for this transport */
1003 			xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
1004 				    (char *)&relp);
1005 			/*
1006 			 * XXX: should have perhaps returned with error but
1007 			 * since the remote machine might not always be able
1008 			 * to send the address on all transports, we try the
1009 			 * regular way with regular_rpcbind
1010 			 */
1011 			goto regular_rpcbind;
1012 		} else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
1013 			    (clnt_st == RPC_PROGUNAVAIL)) {
1014 			start_vers = RPCBVERS;	/* Try version 3 now */
1015 			goto regular_rpcbind; /* Try different versions */
1016 		} else {
1017 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
1018 			clnt_geterr(client, &rpc_createerr.cf_error);
1019 			goto error;
1020 		}
1021 	}
1022 
1023 regular_rpcbind:
1024 
1025 	/* Now the same transport is to be used to get the address */
1026 	if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
1027 	    (nconf->nc_semantics == NC_TPI_COTS))) {
1028 		/* A CLTS type of client - destroy it */
1029 		CLNT_DESTROY(client);
1030 		client = NULL;
1031 	}
1032 
1033 	if (client == NULL) {
1034 		client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
1035 		if (client == NULL) {
1036 			address = NULL;
1037 			goto error;
1038 		}
1039 	}
1040 	if (parms.r_addr == NULL) {
1041 		parms.r_addr = strdup("");	/* for XDRing */
1042 		if (parms.r_addr == NULL) {
1043 			syslog(LOG_ERR, "__rpcb_findaddr_timed: "
1044 				"strdup failed.");
1045 			address = NULL;
1046 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1047 			goto error;
1048 		}
1049 	}
1050 
1051 	/* First try from start_vers and then version 3 (RPCBVERS) */
1052 
1053 	CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
1054 	for (vers = start_vers;  vers >= RPCBVERS; vers--) {
1055 		/* Set the version */
1056 		CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
1057 		clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
1058 				    (xdrproc_t)xdr_rpcb, (char *)&parms,
1059 				    (xdrproc_t)xdr_wrapstring,
1060 				    (char *)&ua, *tp);
1061 		if (clnt_st == RPC_SUCCESS) {
1062 			if ((ua == NULL) || (ua[0] == NULL)) {
1063 				/* address unknown */
1064 				rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1065 				goto error;
1066 			}
1067 			address = uaddr2taddr(nconf, ua);
1068 #ifdef ND_DEBUG
1069 			fprintf(stderr, "\tRemote address is [%s]\n", ua);
1070 			if (!address)
1071 				fprintf(stderr,
1072 					"\tCouldn't resolve remote address!\n");
1073 #endif
1074 			xdr_free((xdrproc_t)xdr_wrapstring, (char *)&ua);
1075 
1076 			if (! address) {
1077 				/* We don't know about your universal address */
1078 				rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1079 				goto error;
1080 			}
1081 			goto done;
1082 		} else if (clnt_st == RPC_PROGVERSMISMATCH) {
1083 			struct rpc_err rpcerr;
1084 
1085 			clnt_geterr(client, &rpcerr);
1086 			if (rpcerr.re_vers.low > RPCBVERS4)
1087 				goto error;  /* a new version, can't handle */
1088 		} else if (clnt_st != RPC_PROGUNAVAIL) {
1089 			/* Cant handle this error */
1090 			goto error;
1091 		}
1092 	}
1093 
1094 	if ((address == NULL) || (address->len == 0)) {
1095 		rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1096 		clnt_geterr(client, &rpc_createerr.cf_error);
1097 	}
1098 
1099 error:
1100 	if (client) {
1101 		CLNT_DESTROY(client);
1102 		client = NULL;
1103 	}
1104 done:
1105 	if (nconf->nc_semantics != NC_TPI_CLTS) {
1106 		/* This client is the connectionless one */
1107 		if (client) {
1108 			CLNT_DESTROY(client);
1109 			client = NULL;
1110 		}
1111 	}
1112 	if (clpp) {
1113 		*clpp = client;
1114 	} else if (client) {
1115 		CLNT_DESTROY(client);
1116 	}
1117 	if (parms.r_addr)
1118 		free(parms.r_addr);
1119 	trace3(TR_rpcb_findaddr, 1, program, version);
1120 	return (address);
1121 }
1122 
1123 
1124 /*
1125  * Find the mapped address for program, version.
1126  * Calls the rpcbind service remotely to do the lookup.
1127  * Uses the transport specified in nconf.
1128  * Returns FALSE (0) if no map exists, else returns 1.
1129  *
1130  * Assuming that the address is all properly allocated
1131  */
1132 int
1133 rpcb_getaddr(program, version, nconf, address, host)
1134 	rpcprog_t program;
1135 	rpcvers_t version;
1136 	const struct netconfig *nconf;
1137 	struct netbuf *address;
1138 	const char *host;
1139 {
1140 	struct netbuf *na;
1141 
1142 	trace3(TR_rpcb_getaddr, 0, program, version);
1143 	if ((na = __rpcb_findaddr_timed(program, version,
1144 	    (struct netconfig *)nconf, (char *)host,
1145 	    (CLIENT **)NULL, (struct timeval *)NULL)) == NULL)
1146 		return (FALSE);
1147 
1148 	if (na->len > address->maxlen) {
1149 		/* Too long address */
1150 		netdir_free((char *)na, ND_ADDR);
1151 		rpc_createerr.cf_stat = RPC_FAILED;
1152 		trace3(TR_rpcb_getaddr, 1, program, version);
1153 		return (FALSE);
1154 	}
1155 	memcpy(address->buf, na->buf, (int)na->len);
1156 	address->len = na->len;
1157 	netdir_free((char *)na, ND_ADDR);
1158 	trace3(TR_rpcb_getaddr, 1, program, version);
1159 	return (TRUE);
1160 }
1161 
1162 /*
1163  * Get a copy of the current maps.
1164  * Calls the rpcbind service remotely to get the maps.
1165  *
1166  * It returns only a list of the services
1167  * It returns NULL on failure.
1168  */
1169 rpcblist *
1170 rpcb_getmaps(nconf, host)
1171 	const struct netconfig *nconf;
1172 	const char *host;
1173 {
1174 	rpcblist_ptr head = (rpcblist_ptr)NULL;
1175 	CLIENT *client;
1176 	enum clnt_stat clnt_st;
1177 	int vers = 0;
1178 
1179 	trace1(TR_rpcb_getmaps, 0);
1180 	client = getclnthandle((char *)host,
1181 			(struct netconfig *)nconf, (char **)NULL);
1182 	if (client == (CLIENT *)NULL) {
1183 		trace1(TR_rpcb_getmaps, 1);
1184 		return (head);
1185 	}
1186 
1187 	clnt_st = CLNT_CALL(client, RPCBPROC_DUMP,
1188 			(xdrproc_t)xdr_void, NULL,
1189 			(xdrproc_t)xdr_rpcblist_ptr,
1190 			(char *)&head, tottimeout);
1191 	if (clnt_st == RPC_SUCCESS)
1192 		goto done;
1193 
1194 	if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1195 		    (clnt_st != RPC_PROGUNAVAIL)) {
1196 		rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1197 		clnt_geterr(client, &rpc_createerr.cf_error);
1198 		goto done;
1199 	}
1200 
1201 	/* fall back to earlier version */
1202 	CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
1203 	if (vers == RPCBVERS4) {
1204 		vers = RPCBVERS;
1205 		CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
1206 		if (CLNT_CALL(client, RPCBPROC_DUMP,
1207 			(xdrproc_t)xdr_void,
1208 			(char *)NULL, (xdrproc_t)xdr_rpcblist_ptr,
1209 			(char *)&head, tottimeout) == RPC_SUCCESS)
1210 				goto done;
1211 	}
1212 	rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1213 	clnt_geterr(client, &rpc_createerr.cf_error);
1214 
1215 done:
1216 	CLNT_DESTROY(client);
1217 	trace1(TR_rpcb_getmaps, 1);
1218 	return (head);
1219 }
1220 
1221 /*
1222  * rpcbinder remote-call-service interface.
1223  * This routine is used to call the rpcbind remote call service
1224  * which will look up a service program in the address maps, and then
1225  * remotely call that routine with the given parameters. This allows
1226  * programs to do a lookup and call in one step.
1227  */
1228 enum clnt_stat
1229 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1230 		xdrres, resp, tout, addr_ptr)
1231 	const struct netconfig *nconf;	/* Netconfig structure */
1232 	const char *host;			/* Remote host name */
1233 	rpcprog_t prog;
1234 	rpcvers_t vers;
1235 	rpcproc_t proc;			/* Remote proc identifiers */
1236 	xdrproc_t xdrargs, xdrres;	/* XDR routines */
1237 	caddr_t argsp, resp;		/* Argument and Result */
1238 	struct timeval tout;		/* Timeout value for this call */
1239 	struct netbuf *addr_ptr;	/* Preallocated netbuf address */
1240 {
1241 	CLIENT *client;
1242 	enum clnt_stat stat;
1243 	struct r_rpcb_rmtcallargs a;
1244 	struct r_rpcb_rmtcallres r;
1245 	int rpcb_vers;
1246 
1247 	trace4(TR_rpcb_rmtcall, 0, prog, vers, proc);
1248 
1249 	client = getclnthandle((char *)host,
1250 			(struct netconfig *)nconf, (char **)NULL);
1251 	if (client == (CLIENT *)NULL) {
1252 		trace4(TR_rpcb_rmtcall, 1, prog, vers, proc);
1253 		return (RPC_FAILED);
1254 	}
1255 	CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rmttimeout);
1256 	a.prog = prog;
1257 	a.vers = vers;
1258 	a.proc = proc;
1259 	a.args.args_val = argsp;
1260 	a.xdr_args = xdrargs;
1261 	r.addr = NULL;
1262 	r.results.results_val = resp;
1263 	r.xdr_res = xdrres;
1264 
1265 	for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1266 		CLNT_CONTROL(client, CLSET_VERS, (char *)&rpcb_vers);
1267 		stat = CLNT_CALL(client, RPCBPROC_CALLIT,
1268 			(xdrproc_t)xdr_rpcb_rmtcallargs, (char *)&a,
1269 			(xdrproc_t)xdr_rpcb_rmtcallres, (char *)&r, tout);
1270 		if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1271 			struct netbuf *na;
1272 
1273 			na = uaddr2taddr((struct netconfig *)nconf, r.addr);
1274 			if (! na) {
1275 				stat = RPC_N2AXLATEFAILURE;
1276 				((struct netbuf *)addr_ptr)->len = 0;
1277 				goto error;
1278 			}
1279 			if (na->len > addr_ptr->maxlen) {
1280 				/* Too long address */
1281 				stat = RPC_FAILED; /* XXX A better error no */
1282 				netdir_free((char *)na, ND_ADDR);
1283 				((struct netbuf *)addr_ptr)->len = 0;
1284 				goto error;
1285 			}
1286 			memcpy(addr_ptr->buf, na->buf, (int)na->len);
1287 			((struct netbuf *)addr_ptr)->len = na->len;
1288 			netdir_free((char *)na, ND_ADDR);
1289 			break;
1290 		} else if ((stat != RPC_PROGVERSMISMATCH) &&
1291 			    (stat != RPC_PROGUNAVAIL)) {
1292 			goto error;
1293 		}
1294 	}
1295 error:
1296 	CLNT_DESTROY(client);
1297 	if (r.addr)
1298 		xdr_free((xdrproc_t)xdr_wrapstring, (char *)&r.addr);
1299 	trace4(TR_rpcb_rmtcall, 1, prog, vers, proc);
1300 	return (stat);
1301 }
1302 
1303 /*
1304  * Gets the time on the remote host.
1305  * Returns 1 if succeeds else 0.
1306  */
1307 bool_t
1308 rpcb_gettime(host, timep)
1309 	const char *host;
1310 	time_t *timep;
1311 {
1312 	CLIENT *client = NULL;
1313 	void *handle;
1314 	struct netconfig *nconf;
1315 	int vers;
1316 	enum clnt_stat st;
1317 
1318 	trace1(TR_rpcb_gettime, 0);
1319 
1320 	if ((host == NULL) || (host[0] == NULL)) {
1321 		time(timep);
1322 		trace1(TR_rpcb_gettime, 1);
1323 		return (TRUE);
1324 	}
1325 
1326 	if ((handle = __rpc_setconf("netpath")) == NULL) {
1327 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1328 		trace1(TR_rpcb_gettime, 1);
1329 		return (FALSE);
1330 	}
1331 	rpc_createerr.cf_stat = RPC_SUCCESS;
1332 	while (client == (CLIENT *)NULL) {
1333 		if ((nconf = __rpc_getconf(handle)) == NULL) {
1334 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
1335 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1336 			break;
1337 		}
1338 		client = getclnthandle((char *)host, nconf, (char **)NULL);
1339 		if (client)
1340 			break;
1341 	}
1342 	__rpc_endconf(handle);
1343 	if (client == (CLIENT *) NULL) {
1344 		trace1(TR_rpcb_gettime, 1);
1345 		return (FALSE);
1346 	}
1347 
1348 	st = CLNT_CALL(client, RPCBPROC_GETTIME,
1349 		(xdrproc_t)xdr_void, (char *)NULL,
1350 		(xdrproc_t)xdr_time_t, (char *)timep, tottimeout);
1351 
1352 	if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1353 		CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
1354 		if (vers == RPCBVERS4) {
1355 			/* fall back to earlier version */
1356 			vers = RPCBVERS;
1357 			CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
1358 			st = CLNT_CALL(client, RPCBPROC_GETTIME,
1359 				(xdrproc_t)xdr_void, (char *)NULL,
1360 				(xdrproc_t)xdr_time_t, (char *)timep,
1361 				tottimeout);
1362 		}
1363 	}
1364 	trace1(TR_rpcb_gettime, 1);
1365 	CLNT_DESTROY(client);
1366 	return (st == RPC_SUCCESS? TRUE: FALSE);
1367 }
1368 
1369 /*
1370  * Converts taddr to universal address.  This routine should never
1371  * really be called because local n2a libraries are always provided.
1372  */
1373 char *
1374 rpcb_taddr2uaddr(nconf, taddr)
1375 	struct netconfig *nconf;
1376 	struct netbuf *taddr;
1377 {
1378 	CLIENT *client;
1379 	char *uaddr = NULL;
1380 
1381 	trace1(TR_rpcb_taddr2uaddr, 0);
1382 
1383 	/* parameter checking */
1384 	if (nconf == (struct netconfig *)NULL) {
1385 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1386 		trace1(TR_rpcb_taddr2uaddr, 1);
1387 		return (NULL);
1388 	}
1389 	if (taddr == NULL) {
1390 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1391 		trace1(TR_rpcb_taddr2uaddr, 1);
1392 		return (NULL);
1393 	}
1394 	client = local_rpcb();
1395 	if (! client) {
1396 		trace1(TR_rpcb_taddr2uaddr, 1);
1397 		return (NULL);
1398 	}
1399 
1400 	CLNT_CALL(client, RPCBPROC_TADDR2UADDR, (xdrproc_t)xdr_netbuf,
1401 		(char *)taddr, (xdrproc_t)xdr_wrapstring, (char *)&uaddr,
1402 		tottimeout);
1403 	CLNT_DESTROY(client);
1404 	trace1(TR_rpcb_taddr2uaddr, 1);
1405 	return (uaddr);
1406 }
1407 
1408 /*
1409  * Converts universal address to netbuf.  This routine should never
1410  * really be called because local n2a libraries are always provided.
1411  */
1412 struct netbuf *
1413 rpcb_uaddr2taddr(nconf, uaddr)
1414 	struct netconfig *nconf;
1415 	char *uaddr;
1416 {
1417 	CLIENT *client;
1418 	struct netbuf *taddr;
1419 
1420 	trace1(TR_rpcb_uaddr2taddr, 0);
1421 
1422 	/* parameter checking */
1423 	if (nconf == (struct netconfig *)NULL) {
1424 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1425 		trace1(TR_rpcb_uaddr2taddr, 1);
1426 		return (NULL);
1427 	}
1428 	if (uaddr == NULL) {
1429 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1430 		trace1(TR_rpcb_uaddr2taddr, 1);
1431 		return (NULL);
1432 	}
1433 	client = local_rpcb();
1434 	if (! client) {
1435 		trace1(TR_rpcb_uaddr2taddr, 1);
1436 		return (NULL);
1437 	}
1438 
1439 	taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1440 	if (taddr == NULL) {
1441 		CLNT_DESTROY(client);
1442 		trace1(TR_rpcb_uaddr2taddr, 1);
1443 		return (NULL);
1444 	}
1445 
1446 	if (CLNT_CALL(client, RPCBPROC_UADDR2TADDR, (xdrproc_t)xdr_wrapstring,
1447 		(char *)&uaddr, (xdrproc_t)xdr_netbuf, (char *)taddr,
1448 		tottimeout) != RPC_SUCCESS) {
1449 		free(taddr);
1450 		taddr = NULL;
1451 	}
1452 	CLNT_DESTROY(client);
1453 	trace1(TR_rpcb_uaddr2taddr, 1);
1454 	return (taddr);
1455 }
1456