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