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