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