1*1e0adbedSjustin /* $NetBSD: rpcb_clnt.c,v 1.31 2015/03/26 11:31:57 justin Exp $ */
27df0ccbaSfvdl
37df0ccbaSfvdl /*
415c1b43eStron * Copyright (c) 2010, Oracle America, Inc.
57df0ccbaSfvdl *
615c1b43eStron * Redistribution and use in source and binary forms, with or without
715c1b43eStron * modification, are permitted provided that the following conditions are
815c1b43eStron * met:
97df0ccbaSfvdl *
1015c1b43eStron * * Redistributions of source code must retain the above copyright
1115c1b43eStron * notice, this list of conditions and the following disclaimer.
1215c1b43eStron * * Redistributions in binary form must reproduce the above
1315c1b43eStron * copyright notice, this list of conditions and the following
1415c1b43eStron * disclaimer in the documentation and/or other materials
1515c1b43eStron * provided with the distribution.
1615c1b43eStron * * Neither the name of the "Oracle America, Inc." nor the names of its
1715c1b43eStron * contributors may be used to endorse or promote products derived
1815c1b43eStron * from this software without specific prior written permission.
197df0ccbaSfvdl *
2015c1b43eStron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2115c1b43eStron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2215c1b43eStron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2315c1b43eStron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2415c1b43eStron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2515c1b43eStron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2615c1b43eStron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2715c1b43eStron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2815c1b43eStron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2915c1b43eStron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3015c1b43eStron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3115c1b43eStron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
327df0ccbaSfvdl */
337df0ccbaSfvdl /*
347df0ccbaSfvdl * Copyright (c) 1986-1991 by Sun Microsystems Inc.
357df0ccbaSfvdl */
367df0ccbaSfvdl
377df0ccbaSfvdl /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
387df0ccbaSfvdl
395c945215Sitojun #include <sys/cdefs.h>
405c945215Sitojun #if defined(LIBC_SCCS) && !defined(lint)
417df0ccbaSfvdl #if 0
427df0ccbaSfvdl static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
435c945215Sitojun #else
44*1e0adbedSjustin __RCSID("$NetBSD: rpcb_clnt.c,v 1.31 2015/03/26 11:31:57 justin Exp $");
457df0ccbaSfvdl #endif
467df0ccbaSfvdl #endif
477df0ccbaSfvdl
487df0ccbaSfvdl /*
497df0ccbaSfvdl * rpcb_clnt.c
507df0ccbaSfvdl * interface to rpcbind rpc service.
517df0ccbaSfvdl *
527df0ccbaSfvdl * Copyright (C) 1988, Sun Microsystems, Inc.
537df0ccbaSfvdl */
547df0ccbaSfvdl
557df0ccbaSfvdl #include "namespace.h"
567df0ccbaSfvdl #include "reentrant.h"
577df0ccbaSfvdl #include <sys/types.h>
587df0ccbaSfvdl #include <sys/socket.h>
597df0ccbaSfvdl #include <sys/un.h>
607df0ccbaSfvdl #include <sys/utsname.h>
617df0ccbaSfvdl #include <rpc/rpc.h>
627df0ccbaSfvdl #include <rpc/rpcb_prot.h>
63deb154d2Schristos #include <rpc/nettype.h>
647df0ccbaSfvdl #include <netconfig.h>
657df0ccbaSfvdl #ifdef PORTMAP
667df0ccbaSfvdl #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
677df0ccbaSfvdl #include <rpc/pmap_prot.h>
687df0ccbaSfvdl #endif
690e8cfd8fSlukem #include <assert.h>
707df0ccbaSfvdl #include <errno.h>
710e8cfd8fSlukem #include <netdb.h>
720e8cfd8fSlukem #include <stdio.h>
737df0ccbaSfvdl #include <stdlib.h>
747df0ccbaSfvdl #include <string.h>
75598be7b0Sassar #include <syslog.h>
760e8cfd8fSlukem #include <unistd.h>
777df0ccbaSfvdl
786c0ae2b0Schristos #include "svc_fdset.h"
7979d5b270Sfvdl #include "rpc_internal.h"
807df0ccbaSfvdl
817df0ccbaSfvdl #ifdef __weak_alias
827df0ccbaSfvdl __weak_alias(rpcb_set,_rpcb_set)
837df0ccbaSfvdl __weak_alias(rpcb_unset,_rpcb_unset)
847df0ccbaSfvdl __weak_alias(rpcb_getmaps,_rpcb_getmaps)
857df0ccbaSfvdl __weak_alias(rpcb_taddr2uaddr,_rpcb_taddr2uaddr)
867df0ccbaSfvdl __weak_alias(rpcb_uaddr2taddr,_rpcb_uaddr2taddr)
877df0ccbaSfvdl #endif
887df0ccbaSfvdl
897df0ccbaSfvdl static struct timeval tottimeout = { 60, 0 };
907df0ccbaSfvdl static const struct timeval rmttimeout = { 3, 0 };
917df0ccbaSfvdl
927df0ccbaSfvdl static const char nullstring[] = "\000";
937df0ccbaSfvdl
947df0ccbaSfvdl #define CACHESIZE 6
957df0ccbaSfvdl
967df0ccbaSfvdl struct address_cache {
977df0ccbaSfvdl char *ac_host;
987df0ccbaSfvdl char *ac_netid;
997df0ccbaSfvdl char *ac_uaddr;
1007df0ccbaSfvdl struct netbuf *ac_taddr;
1017df0ccbaSfvdl struct address_cache *ac_next;
1027df0ccbaSfvdl };
1037df0ccbaSfvdl
1047df0ccbaSfvdl static struct address_cache *front;
1057df0ccbaSfvdl static int cachesize;
1067df0ccbaSfvdl
1077df0ccbaSfvdl #define CLCR_GET_RPCB_TIMEOUT 1
1087df0ccbaSfvdl #define CLCR_SET_RPCB_TIMEOUT 2
1097df0ccbaSfvdl
1107df0ccbaSfvdl
1117df0ccbaSfvdl extern int __rpc_lowvers;
1127df0ccbaSfvdl
113c30c4c40Smatt static struct address_cache *check_cache(const char *, const char *);
114c30c4c40Smatt static void delete_cache(struct netbuf *);
115c30c4c40Smatt static void add_cache(const char *, const char *, struct netbuf *, char *);
116c30c4c40Smatt static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
117c30c4c40Smatt static CLIENT *local_rpcb(void);
118c30c4c40Smatt static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
1197df0ccbaSfvdl
1207df0ccbaSfvdl /*
1217df0ccbaSfvdl * This routine adjusts the timeout used for calls to the remote rpcbind.
1227df0ccbaSfvdl * Also, this routine can be used to set the use of portmapper version 2
1237df0ccbaSfvdl * only when doing rpc_broadcasts
1247df0ccbaSfvdl * These are private routines that may not be provided in future releases.
1257df0ccbaSfvdl */
1267df0ccbaSfvdl bool_t
__rpc_control(int request,void * info)127c30c4c40Smatt __rpc_control(int request, void *info)
1287df0ccbaSfvdl {
1290e8cfd8fSlukem
1300e8cfd8fSlukem _DIAGASSERT(info != NULL);
1310e8cfd8fSlukem
1327df0ccbaSfvdl switch (request) {
1337df0ccbaSfvdl case CLCR_GET_RPCB_TIMEOUT:
1347df0ccbaSfvdl *(struct timeval *)info = tottimeout;
1357df0ccbaSfvdl break;
1367df0ccbaSfvdl case CLCR_SET_RPCB_TIMEOUT:
1377df0ccbaSfvdl tottimeout = *(struct timeval *)info;
1387df0ccbaSfvdl break;
1397df0ccbaSfvdl case CLCR_SET_LOWVERS:
1407df0ccbaSfvdl __rpc_lowvers = *(int *)info;
1417df0ccbaSfvdl break;
1427df0ccbaSfvdl case CLCR_GET_LOWVERS:
1437df0ccbaSfvdl *(int *)info = __rpc_lowvers;
1447df0ccbaSfvdl break;
1457df0ccbaSfvdl default:
1467df0ccbaSfvdl return (FALSE);
1477df0ccbaSfvdl }
1487df0ccbaSfvdl return (TRUE);
1497df0ccbaSfvdl }
1507df0ccbaSfvdl
1517df0ccbaSfvdl /*
1527df0ccbaSfvdl * It might seem that a reader/writer lock would be more reasonable here.
1537df0ccbaSfvdl * However because getclnthandle(), the only user of the cache functions,
1547df0ccbaSfvdl * may do a delete_cache() operation if a check_cache() fails to return an
1557df0ccbaSfvdl * address useful to clnt_tli_create(), we may as well use a mutex.
1567df0ccbaSfvdl */
1577df0ccbaSfvdl /*
1587df0ccbaSfvdl * As it turns out, if the cache lock is *not* a reader/writer lock, we will
1597df0ccbaSfvdl * block all clnt_create's if we are trying to connect to a host that's down,
1607df0ccbaSfvdl * since the lock will be held all during that time.
1617df0ccbaSfvdl */
1623fdac2b8Sthorpej #ifdef _REENTRANT
1637df0ccbaSfvdl extern rwlock_t rpcbaddr_cache_lock;
1647df0ccbaSfvdl #endif
1657df0ccbaSfvdl
1667df0ccbaSfvdl /*
1677df0ccbaSfvdl * The routines check_cache(), add_cache(), delete_cache() manage the
1687df0ccbaSfvdl * cache of rpcbind addresses for (host, netid).
1697df0ccbaSfvdl */
1707df0ccbaSfvdl
1717df0ccbaSfvdl static struct address_cache *
check_cache(const char * host,const char * netid)172c30c4c40Smatt check_cache(const char *host, const char *netid)
1737df0ccbaSfvdl {
1747df0ccbaSfvdl struct address_cache *cptr;
1757df0ccbaSfvdl
1760e8cfd8fSlukem _DIAGASSERT(host != NULL);
1770e8cfd8fSlukem _DIAGASSERT(netid != NULL);
1780e8cfd8fSlukem
1797df0ccbaSfvdl /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
1807df0ccbaSfvdl
1817df0ccbaSfvdl for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
1827df0ccbaSfvdl if (!strcmp(cptr->ac_host, host) &&
1837df0ccbaSfvdl !strcmp(cptr->ac_netid, netid)) {
1847df0ccbaSfvdl #ifdef ND_DEBUG
1857df0ccbaSfvdl fprintf(stderr, "Found cache entry for %s: %s\n",
1867df0ccbaSfvdl host, netid);
1877df0ccbaSfvdl #endif
1887df0ccbaSfvdl return (cptr);
1897df0ccbaSfvdl }
1907df0ccbaSfvdl }
191c9cdc302Schristos return NULL;
1927df0ccbaSfvdl }
1937df0ccbaSfvdl
1947df0ccbaSfvdl static void
delete_cache(struct netbuf * addr)195c30c4c40Smatt delete_cache(struct netbuf *addr)
1967df0ccbaSfvdl {
1977df0ccbaSfvdl struct address_cache *cptr, *prevptr = NULL;
1987df0ccbaSfvdl
1990e8cfd8fSlukem _DIAGASSERT(addr != NULL);
2000e8cfd8fSlukem
2017df0ccbaSfvdl /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
2027df0ccbaSfvdl for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
2037df0ccbaSfvdl if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
2047df0ccbaSfvdl free(cptr->ac_host);
2057df0ccbaSfvdl free(cptr->ac_netid);
2067df0ccbaSfvdl free(cptr->ac_taddr->buf);
2077df0ccbaSfvdl free(cptr->ac_taddr);
2087df0ccbaSfvdl if (cptr->ac_uaddr)
2097df0ccbaSfvdl free(cptr->ac_uaddr);
2107df0ccbaSfvdl if (prevptr)
2117df0ccbaSfvdl prevptr->ac_next = cptr->ac_next;
2127df0ccbaSfvdl else
2137df0ccbaSfvdl front = cptr->ac_next;
2147df0ccbaSfvdl free(cptr);
2157df0ccbaSfvdl cachesize--;
2167df0ccbaSfvdl break;
2177df0ccbaSfvdl }
2187df0ccbaSfvdl prevptr = cptr;
2197df0ccbaSfvdl }
2207df0ccbaSfvdl }
2217df0ccbaSfvdl
2227df0ccbaSfvdl static void
add_cache(const char * host,const char * netid,struct netbuf * taddr,char * uaddr)223c30c4c40Smatt add_cache(const char *host, const char *netid, struct netbuf *taddr,
224c30c4c40Smatt char *uaddr)
2257df0ccbaSfvdl {
2267df0ccbaSfvdl struct address_cache *ad_cache, *cptr, *prevptr;
2277df0ccbaSfvdl
2280e8cfd8fSlukem _DIAGASSERT(host != NULL);
2290e8cfd8fSlukem _DIAGASSERT(netid != NULL);
2300e8cfd8fSlukem /* uaddr may be NULL */
2310e8cfd8fSlukem /* taddr may be NULL ??? */
2320e8cfd8fSlukem
233c9cdc302Schristos ad_cache = malloc(sizeof(*ad_cache));
2347df0ccbaSfvdl if (!ad_cache) {
2357df0ccbaSfvdl return;
2367df0ccbaSfvdl }
2377df0ccbaSfvdl ad_cache->ac_host = strdup(host);
2387df0ccbaSfvdl ad_cache->ac_netid = strdup(netid);
2397df0ccbaSfvdl ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
240c9cdc302Schristos ad_cache->ac_taddr = malloc(sizeof(*ad_cache->ac_taddr));
2417df0ccbaSfvdl if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
2427df0ccbaSfvdl (uaddr && !ad_cache->ac_uaddr)) {
24350e6bed2Schristos goto out;
2447df0ccbaSfvdl }
2457df0ccbaSfvdl ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
246c9cdc302Schristos ad_cache->ac_taddr->buf = malloc(taddr->len);
2477df0ccbaSfvdl if (ad_cache->ac_taddr->buf == NULL) {
24850e6bed2Schristos out:
24950e6bed2Schristos if (ad_cache->ac_host)
25050e6bed2Schristos free(ad_cache->ac_host);
25150e6bed2Schristos if (ad_cache->ac_netid)
25250e6bed2Schristos free(ad_cache->ac_netid);
25350e6bed2Schristos if (ad_cache->ac_uaddr)
25450e6bed2Schristos free(ad_cache->ac_uaddr);
25550e6bed2Schristos if (ad_cache->ac_taddr)
25650e6bed2Schristos free(ad_cache->ac_taddr);
25750e6bed2Schristos free(ad_cache);
2587df0ccbaSfvdl return;
2597df0ccbaSfvdl }
2607df0ccbaSfvdl memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
2617df0ccbaSfvdl #ifdef ND_DEBUG
2627df0ccbaSfvdl fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
2637df0ccbaSfvdl #endif
2647df0ccbaSfvdl
2657df0ccbaSfvdl /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
2667df0ccbaSfvdl
2677df0ccbaSfvdl rwlock_wrlock(&rpcbaddr_cache_lock);
2687df0ccbaSfvdl if (cachesize < CACHESIZE) {
2697df0ccbaSfvdl ad_cache->ac_next = front;
2707df0ccbaSfvdl front = ad_cache;
2717df0ccbaSfvdl cachesize++;
2727df0ccbaSfvdl } else {
2737df0ccbaSfvdl /* Free the last entry */
2747df0ccbaSfvdl cptr = front;
2757df0ccbaSfvdl prevptr = NULL;
2767df0ccbaSfvdl while (cptr->ac_next) {
2777df0ccbaSfvdl prevptr = cptr;
2787df0ccbaSfvdl cptr = cptr->ac_next;
2797df0ccbaSfvdl }
2807df0ccbaSfvdl
2817df0ccbaSfvdl #ifdef ND_DEBUG
2827df0ccbaSfvdl fprintf(stderr, "Deleted from cache: %s : %s\n",
2837df0ccbaSfvdl cptr->ac_host, cptr->ac_netid);
2847df0ccbaSfvdl #endif
2857df0ccbaSfvdl free(cptr->ac_host);
2867df0ccbaSfvdl free(cptr->ac_netid);
2877df0ccbaSfvdl free(cptr->ac_taddr->buf);
2887df0ccbaSfvdl free(cptr->ac_taddr);
2897df0ccbaSfvdl if (cptr->ac_uaddr)
2907df0ccbaSfvdl free(cptr->ac_uaddr);
2917df0ccbaSfvdl
2927df0ccbaSfvdl if (prevptr) {
2937df0ccbaSfvdl prevptr->ac_next = NULL;
2947df0ccbaSfvdl ad_cache->ac_next = front;
2957df0ccbaSfvdl front = ad_cache;
2967df0ccbaSfvdl } else {
2977df0ccbaSfvdl front = ad_cache;
2987df0ccbaSfvdl ad_cache->ac_next = NULL;
2997df0ccbaSfvdl }
3007df0ccbaSfvdl free(cptr);
3017df0ccbaSfvdl }
3027df0ccbaSfvdl rwlock_unlock(&rpcbaddr_cache_lock);
3037df0ccbaSfvdl }
3047df0ccbaSfvdl
3057df0ccbaSfvdl /*
3067df0ccbaSfvdl * This routine will return a client handle that is connected to the
3077df0ccbaSfvdl * rpcbind. Returns NULL on error and free's everything.
3087df0ccbaSfvdl */
3097df0ccbaSfvdl static CLIENT *
getclnthandle(const char * host,const struct netconfig * nconf,char ** targaddr)310c30c4c40Smatt getclnthandle(const char *host, const struct netconfig *nconf, char **targaddr)
3117df0ccbaSfvdl {
312deb154d2Schristos CLIENT *client;
3137df0ccbaSfvdl struct netbuf *addr, taddr;
3147df0ccbaSfvdl struct netbuf addr_to_delete;
3157df0ccbaSfvdl struct __rpc_sockinfo si;
3167df0ccbaSfvdl struct addrinfo hints, *res, *tres;
3177df0ccbaSfvdl struct address_cache *ad_cache;
3187df0ccbaSfvdl char *tmpaddr;
3197df0ccbaSfvdl
3200e8cfd8fSlukem _DIAGASSERT(host != NULL);
3210e8cfd8fSlukem _DIAGASSERT(nconf != NULL);
3220e8cfd8fSlukem /* targaddr may be NULL */
3230e8cfd8fSlukem
3247df0ccbaSfvdl /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
3257df0ccbaSfvdl
3267df0ccbaSfvdl /* Get the address of the rpcbind. Check cache first */
3273af3e6a4Slukem client = NULL;
3287df0ccbaSfvdl addr_to_delete.len = 0;
329674681d3Schristos addr_to_delete.buf = NULL;
3307df0ccbaSfvdl rwlock_rdlock(&rpcbaddr_cache_lock);
3317df0ccbaSfvdl ad_cache = check_cache(host, nconf->nc_netid);
3327df0ccbaSfvdl if (ad_cache != NULL) {
3337df0ccbaSfvdl addr = ad_cache->ac_taddr;
334deb154d2Schristos client = clnt_tli_create(RPC_ANYFD, nconf, addr,
335deb154d2Schristos (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
3367df0ccbaSfvdl if (client != NULL) {
3377df0ccbaSfvdl if (targaddr)
3387df0ccbaSfvdl *targaddr = ad_cache->ac_uaddr;
3397df0ccbaSfvdl rwlock_unlock(&rpcbaddr_cache_lock);
3407df0ccbaSfvdl return (client);
3417df0ccbaSfvdl }
3427df0ccbaSfvdl addr_to_delete.len = addr->len;
343c9cdc302Schristos addr_to_delete.buf = malloc(addr->len);
3447df0ccbaSfvdl if (addr_to_delete.buf == NULL) {
3457df0ccbaSfvdl addr_to_delete.len = 0;
3467df0ccbaSfvdl } else {
3477df0ccbaSfvdl memcpy(addr_to_delete.buf, addr->buf, addr->len);
3487df0ccbaSfvdl }
3497df0ccbaSfvdl }
3507df0ccbaSfvdl rwlock_unlock(&rpcbaddr_cache_lock);
3517df0ccbaSfvdl if (addr_to_delete.len != 0) {
3527df0ccbaSfvdl /*
3537df0ccbaSfvdl * Assume this may be due to cache data being
3547df0ccbaSfvdl * outdated
3557df0ccbaSfvdl */
3567df0ccbaSfvdl rwlock_wrlock(&rpcbaddr_cache_lock);
3577df0ccbaSfvdl delete_cache(&addr_to_delete);
3587df0ccbaSfvdl rwlock_unlock(&rpcbaddr_cache_lock);
3597df0ccbaSfvdl free(addr_to_delete.buf);
3607df0ccbaSfvdl }
361eaee8c44Sfvdl if (!__rpc_nconf2sockinfo(nconf, &si)) {
362eaee8c44Sfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3637df0ccbaSfvdl return NULL;
364eaee8c44Sfvdl }
3657df0ccbaSfvdl
3667df0ccbaSfvdl memset(&hints, 0, sizeof hints);
3677df0ccbaSfvdl hints.ai_family = si.si_af;
3687df0ccbaSfvdl hints.ai_socktype = si.si_socktype;
3697df0ccbaSfvdl hints.ai_protocol = si.si_proto;
3707df0ccbaSfvdl
3717df0ccbaSfvdl #ifdef CLNT_DEBUG
3727df0ccbaSfvdl printf("trying netid %s family %d proto %d socktype %d\n",
3737df0ccbaSfvdl nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
3747df0ccbaSfvdl #endif
3757df0ccbaSfvdl
376eaee8c44Sfvdl if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
377eaee8c44Sfvdl rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
3787df0ccbaSfvdl return NULL;
379eaee8c44Sfvdl }
3807df0ccbaSfvdl
3817df0ccbaSfvdl for (tres = res; tres != NULL; tres = tres->ai_next) {
3827df0ccbaSfvdl taddr.buf = tres->ai_addr;
3837df0ccbaSfvdl taddr.len = taddr.maxlen = tres->ai_addrlen;
3847df0ccbaSfvdl
3857df0ccbaSfvdl #ifdef ND_DEBUG
3867df0ccbaSfvdl {
3877df0ccbaSfvdl char *ua;
3887df0ccbaSfvdl
3897df0ccbaSfvdl ua = taddr2uaddr(nconf, &taddr);
3907df0ccbaSfvdl fprintf(stderr, "Got it [%s]\n", ua);
3917df0ccbaSfvdl free(ua);
3927df0ccbaSfvdl }
3937df0ccbaSfvdl #endif
3947df0ccbaSfvdl
3957df0ccbaSfvdl #ifdef ND_DEBUG
3967df0ccbaSfvdl {
3977df0ccbaSfvdl int i;
3987df0ccbaSfvdl
3997df0ccbaSfvdl fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
4007df0ccbaSfvdl taddr.len, taddr.maxlen);
4017df0ccbaSfvdl fprintf(stderr, "\tAddress is ");
4027df0ccbaSfvdl for (i = 0; i < taddr.len; i++)
403eaee8c44Sfvdl fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
4047df0ccbaSfvdl fprintf(stderr, "\n");
4057df0ccbaSfvdl }
4067df0ccbaSfvdl #endif
407deb154d2Schristos client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
408deb154d2Schristos (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
4097df0ccbaSfvdl #ifdef ND_DEBUG
4107df0ccbaSfvdl if (! client) {
4117df0ccbaSfvdl clnt_pcreateerror("rpcbind clnt interface");
4127df0ccbaSfvdl }
4137df0ccbaSfvdl #endif
4147df0ccbaSfvdl
4157df0ccbaSfvdl if (client) {
4167df0ccbaSfvdl tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
4177df0ccbaSfvdl add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
4187df0ccbaSfvdl if (targaddr)
4197df0ccbaSfvdl *targaddr = tmpaddr;
4207df0ccbaSfvdl break;
4217df0ccbaSfvdl }
4227df0ccbaSfvdl }
4237df0ccbaSfvdl freeaddrinfo(res);
4247df0ccbaSfvdl return (client);
4257df0ccbaSfvdl }
4267df0ccbaSfvdl
4277df0ccbaSfvdl /* XXX */
4287df0ccbaSfvdl #define IN4_LOCALHOST_STRING "127.0.0.1"
4297df0ccbaSfvdl #define IN6_LOCALHOST_STRING "::1"
4307df0ccbaSfvdl
4317df0ccbaSfvdl /*
4327df0ccbaSfvdl * This routine will return a client handle that is connected to the local
4337df0ccbaSfvdl * rpcbind. Returns NULL on error and free's everything.
4347df0ccbaSfvdl */
4357df0ccbaSfvdl static CLIENT *
local_rpcb(void)436c30c4c40Smatt local_rpcb(void)
4377df0ccbaSfvdl {
4387df0ccbaSfvdl CLIENT *client;
4397df0ccbaSfvdl static struct netconfig *loopnconf;
44003256c6eSchristos static const char *hostname;
4413fdac2b8Sthorpej #ifdef _REENTRANT
4427df0ccbaSfvdl extern mutex_t loopnconf_lock;
4437df0ccbaSfvdl #endif
444deb154d2Schristos int sock;
445deb154d2Schristos size_t tsize;
4467df0ccbaSfvdl struct netbuf nbuf;
4477df0ccbaSfvdl struct sockaddr_un sun;
4487df0ccbaSfvdl
4497df0ccbaSfvdl /*
4507df0ccbaSfvdl * Try connecting to the local rpcbind through a local socket
4517df0ccbaSfvdl * first. If this doesn't work, try all transports defined in
4527df0ccbaSfvdl * the netconfig file.
4537df0ccbaSfvdl */
4547df0ccbaSfvdl memset(&sun, 0, sizeof sun);
4557df0ccbaSfvdl sock = socket(AF_LOCAL, SOCK_STREAM, 0);
4567df0ccbaSfvdl if (sock < 0)
4577df0ccbaSfvdl goto try_nconf;
4587df0ccbaSfvdl sun.sun_family = AF_LOCAL;
4597df0ccbaSfvdl strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
460e826745eSchristos tsize = SUN_LEN(&sun);
461e826745eSchristos _DIAGASSERT(__type_fit(uint8_t, tsize));
462e826745eSchristos nbuf.len = sun.sun_len = (uint8_t)tsize;
4637df0ccbaSfvdl nbuf.maxlen = sizeof (struct sockaddr_un);
4647df0ccbaSfvdl nbuf.buf = &sun;
4657df0ccbaSfvdl
4667df0ccbaSfvdl tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
467e826745eSchristos _DIAGASSERT(__type_fit(u_int, tsize));
468deb154d2Schristos client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
469e826745eSchristos (rpcvers_t)RPCBVERS, (u_int)tsize, (u_int)tsize);
4707df0ccbaSfvdl
4714da6558cSlukem if (client != NULL) {
4724da6558cSlukem /* XXX - mark the socket to be closed in destructor */
4734da6558cSlukem (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
4747df0ccbaSfvdl return client;
4754da6558cSlukem }
4764da6558cSlukem
4774da6558cSlukem /* XXX - nobody needs this socket anymore, free the descriptor */
4784da6558cSlukem close(sock);
4797df0ccbaSfvdl
4807df0ccbaSfvdl try_nconf:
4817df0ccbaSfvdl
4827df0ccbaSfvdl /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
4837df0ccbaSfvdl mutex_lock(&loopnconf_lock);
4847df0ccbaSfvdl if (loopnconf == NULL) {
4857df0ccbaSfvdl struct netconfig *nconf, *tmpnconf = NULL;
4867df0ccbaSfvdl void *nc_handle;
4877df0ccbaSfvdl int fd;
4887df0ccbaSfvdl
4897df0ccbaSfvdl nc_handle = setnetconfig();
4907df0ccbaSfvdl if (nc_handle == NULL) {
4917df0ccbaSfvdl /* fails to open netconfig file */
492598be7b0Sassar syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
4937df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
4947df0ccbaSfvdl mutex_unlock(&loopnconf_lock);
4957df0ccbaSfvdl return (NULL);
4967df0ccbaSfvdl }
497deb154d2Schristos while ((nconf = getnetconfig(nc_handle)) != NULL) {
4987df0ccbaSfvdl #ifdef INET6
4997df0ccbaSfvdl if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
5007df0ccbaSfvdl #else
5017462273dSjdolecek if ((
5027df0ccbaSfvdl #endif
5037df0ccbaSfvdl strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
5047df0ccbaSfvdl (nconf->nc_semantics == NC_TPI_COTS ||
5057df0ccbaSfvdl nconf->nc_semantics == NC_TPI_COTS_ORD)) {
5067df0ccbaSfvdl fd = __rpc_nconf2fd(nconf);
5077df0ccbaSfvdl /*
5087df0ccbaSfvdl * Can't create a socket, assume that
5097df0ccbaSfvdl * this family isn't configured in the kernel.
5107df0ccbaSfvdl */
5117df0ccbaSfvdl if (fd < 0)
5127df0ccbaSfvdl continue;
5137df0ccbaSfvdl close(fd);
5147df0ccbaSfvdl tmpnconf = nconf;
5157df0ccbaSfvdl if (!strcmp(nconf->nc_protofmly, NC_INET))
5167df0ccbaSfvdl hostname = IN4_LOCALHOST_STRING;
5177df0ccbaSfvdl else
5187df0ccbaSfvdl hostname = IN6_LOCALHOST_STRING;
5197df0ccbaSfvdl }
5207df0ccbaSfvdl }
5217df0ccbaSfvdl if (tmpnconf == NULL) {
5227df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
5237df0ccbaSfvdl mutex_unlock(&loopnconf_lock);
5247df0ccbaSfvdl return (NULL);
5257df0ccbaSfvdl }
5267df0ccbaSfvdl loopnconf = getnetconfigent(tmpnconf->nc_netid);
5277df0ccbaSfvdl /* loopnconf is never freed */
5287df0ccbaSfvdl endnetconfig(nc_handle);
5297df0ccbaSfvdl }
5307df0ccbaSfvdl mutex_unlock(&loopnconf_lock);
531deb154d2Schristos client = getclnthandle(hostname, loopnconf, NULL);
5327df0ccbaSfvdl return (client);
5337df0ccbaSfvdl }
5347df0ccbaSfvdl
5357df0ccbaSfvdl /*
5367df0ccbaSfvdl * Set a mapping between program, version and address.
5377df0ccbaSfvdl * Calls the rpcbind service to do the mapping.
5387df0ccbaSfvdl */
5397df0ccbaSfvdl bool_t
rpcb_set(rpcprog_t program,rpcvers_t version,const struct netconfig * nconf,const struct netbuf * address)540c30c4c40Smatt rpcb_set(rpcprog_t program, rpcvers_t version,
541c30c4c40Smatt const struct netconfig *nconf, /* Network structure of transport */
542c30c4c40Smatt const struct netbuf *address) /* Services netconfig address */
5437df0ccbaSfvdl {
544deb154d2Schristos CLIENT *client;
5457df0ccbaSfvdl bool_t rslt = FALSE;
5467df0ccbaSfvdl RPCB parms;
5477df0ccbaSfvdl char uidbuf[32];
5487df0ccbaSfvdl
5497df0ccbaSfvdl /* parameter checking */
550deb154d2Schristos if (nconf == NULL) {
5517df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
5527df0ccbaSfvdl return (FALSE);
5537df0ccbaSfvdl }
5547df0ccbaSfvdl if (address == NULL) {
5557df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
5567df0ccbaSfvdl return (FALSE);
5577df0ccbaSfvdl }
5587df0ccbaSfvdl client = local_rpcb();
5597df0ccbaSfvdl if (! client) {
5607df0ccbaSfvdl return (FALSE);
5617df0ccbaSfvdl }
5627df0ccbaSfvdl
5637df0ccbaSfvdl /* convert to universal */
56403256c6eSchristos parms.r_addr = taddr2uaddr(__UNCONST(nconf), __UNCONST(address));
5657df0ccbaSfvdl if (!parms.r_addr) {
5664da6558cSlukem CLNT_DESTROY(client);
5677df0ccbaSfvdl rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
5687df0ccbaSfvdl return (FALSE); /* no universal address */
5697df0ccbaSfvdl }
5707df0ccbaSfvdl parms.r_prog = program;
5717df0ccbaSfvdl parms.r_vers = version;
5727df0ccbaSfvdl parms.r_netid = nconf->nc_netid;
5737df0ccbaSfvdl /*
5747df0ccbaSfvdl * Though uid is not being used directly, we still send it for
5757df0ccbaSfvdl * completeness. For non-unix platforms, perhaps some other
5767df0ccbaSfvdl * string or an empty string can be sent.
5777df0ccbaSfvdl */
5787df0ccbaSfvdl (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
5797df0ccbaSfvdl parms.r_owner = uidbuf;
5807df0ccbaSfvdl
581bbf77bceSchristos if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
582deb154d2Schristos (char *)(void *)&parms, (xdrproc_t) xdr_bool,
583bbf77bceSchristos (char *)(void *)&rslt, tottimeout) != RPC_SUCCESS) {
584bbf77bceSchristos rpc_createerr.cf_stat = RPC_PMAPFAILURE;
585bbf77bceSchristos clnt_geterr(client, &rpc_createerr.cf_error);
586bbf77bceSchristos }
5877df0ccbaSfvdl
5887df0ccbaSfvdl CLNT_DESTROY(client);
5897df0ccbaSfvdl free(parms.r_addr);
5907df0ccbaSfvdl return (rslt);
5917df0ccbaSfvdl }
5927df0ccbaSfvdl
5937df0ccbaSfvdl /*
5947df0ccbaSfvdl * Remove the mapping between program, version and netbuf address.
5957df0ccbaSfvdl * Calls the rpcbind service to do the un-mapping.
5967df0ccbaSfvdl * If netbuf is NULL, unset for all the transports, otherwise unset
5977df0ccbaSfvdl * only for the given transport.
5987df0ccbaSfvdl */
5997df0ccbaSfvdl bool_t
rpcb_unset(rpcprog_t program,rpcvers_t version,const struct netconfig * nconf)600c30c4c40Smatt rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
6017df0ccbaSfvdl {
602deb154d2Schristos CLIENT *client;
6037df0ccbaSfvdl bool_t rslt = FALSE;
6047df0ccbaSfvdl RPCB parms;
6057df0ccbaSfvdl char uidbuf[32];
6067df0ccbaSfvdl
6077df0ccbaSfvdl client = local_rpcb();
6087df0ccbaSfvdl if (! client) {
6097df0ccbaSfvdl return (FALSE);
6107df0ccbaSfvdl }
6117df0ccbaSfvdl
6127df0ccbaSfvdl parms.r_prog = program;
6137df0ccbaSfvdl parms.r_vers = version;
6147df0ccbaSfvdl if (nconf)
6157df0ccbaSfvdl parms.r_netid = nconf->nc_netid;
616deb154d2Schristos else {
61703256c6eSchristos parms.r_netid = __UNCONST(&nullstring[0]); /* unsets all */
618deb154d2Schristos }
61903256c6eSchristos parms.r_addr = __UNCONST(&nullstring[0]);
6207df0ccbaSfvdl (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
6217df0ccbaSfvdl parms.r_owner = uidbuf;
6227df0ccbaSfvdl
623bbf77bceSchristos if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
624deb154d2Schristos (char *)(void *)&parms, (xdrproc_t) xdr_bool,
625bbf77bceSchristos (char *)(void *)&rslt, tottimeout) != RPC_SUCCESS) {
626bbf77bceSchristos rpc_createerr.cf_stat = RPC_PMAPFAILURE;
627bbf77bceSchristos clnt_geterr(client, &rpc_createerr.cf_error);
628bbf77bceSchristos }
6297df0ccbaSfvdl
6307df0ccbaSfvdl CLNT_DESTROY(client);
6317df0ccbaSfvdl return (rslt);
6327df0ccbaSfvdl }
6337df0ccbaSfvdl
6347df0ccbaSfvdl /*
6357df0ccbaSfvdl * From the merged list, find the appropriate entry
6367df0ccbaSfvdl */
6377df0ccbaSfvdl static struct netbuf *
got_entry(rpcb_entry_list_ptr relp,const struct netconfig * nconf)638c30c4c40Smatt got_entry(rpcb_entry_list_ptr relp, const struct netconfig *nconf)
6397df0ccbaSfvdl {
6407df0ccbaSfvdl struct netbuf *na = NULL;
6417df0ccbaSfvdl rpcb_entry_list_ptr sp;
6427df0ccbaSfvdl rpcb_entry *rmap;
6437df0ccbaSfvdl
6440e8cfd8fSlukem _DIAGASSERT(nconf != NULL);
6450e8cfd8fSlukem
6467df0ccbaSfvdl for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
6477df0ccbaSfvdl rmap = &sp->rpcb_entry_map;
6487df0ccbaSfvdl if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
6497df0ccbaSfvdl (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
6507df0ccbaSfvdl (nconf->nc_semantics == rmap->r_nc_semantics) &&
651b279ed63Sfvdl (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
6527df0ccbaSfvdl na = uaddr2taddr(nconf, rmap->r_maddr);
6537df0ccbaSfvdl #ifdef ND_DEBUG
6547df0ccbaSfvdl fprintf(stderr, "\tRemote address is [%s].\n",
6557df0ccbaSfvdl rmap->r_maddr);
6567df0ccbaSfvdl if (!na)
6577df0ccbaSfvdl fprintf(stderr,
6587df0ccbaSfvdl "\tCouldn't resolve remote address!\n");
6597df0ccbaSfvdl #endif
6607df0ccbaSfvdl break;
6617df0ccbaSfvdl }
6627df0ccbaSfvdl }
6637df0ccbaSfvdl return (na);
6647df0ccbaSfvdl }
6657df0ccbaSfvdl
6667df0ccbaSfvdl /*
6677df0ccbaSfvdl * An internal function which optimizes rpcb_getaddr function. It also
6687df0ccbaSfvdl * returns the client handle that it uses to contact the remote rpcbind.
6697df0ccbaSfvdl *
6707df0ccbaSfvdl * The algorithm used: If the transports is TCP or UDP, it first tries
6717df0ccbaSfvdl * version 2 (portmap), 4 and then 3 (svr4). This order should be
6727df0ccbaSfvdl * changed in the next OS release to 4, 2 and 3. We are assuming that by
6737df0ccbaSfvdl * that time, version 4 would be available on many machines on the network.
6747df0ccbaSfvdl * With this algorithm, we get performance as well as a plan for
6757df0ccbaSfvdl * obsoleting version 2.
6767df0ccbaSfvdl *
6777df0ccbaSfvdl * For all other transports, the algorithm remains as 4 and then 3.
6787df0ccbaSfvdl *
6797df0ccbaSfvdl * XXX: Due to some problems with t_connect(), we do not reuse the same client
6807df0ccbaSfvdl * handle for COTS cases and hence in these cases we do not return the
6817df0ccbaSfvdl * client handle. This code will change if t_connect() ever
6827df0ccbaSfvdl * starts working properly. Also look under clnt_vc.c.
6837df0ccbaSfvdl */
6847df0ccbaSfvdl struct netbuf *
__rpcb_findaddr(rpcprog_t program,rpcvers_t version,const struct netconfig * nconf,const char * host,CLIENT ** clpp)685c30c4c40Smatt __rpcb_findaddr(rpcprog_t program, rpcvers_t version,
686c30c4c40Smatt const struct netconfig *nconf, const char *host, CLIENT **clpp)
6877df0ccbaSfvdl {
688deb154d2Schristos CLIENT *client = NULL;
6897df0ccbaSfvdl RPCB parms;
6907df0ccbaSfvdl enum clnt_stat clnt_st;
6917df0ccbaSfvdl char *ua = NULL;
6927df0ccbaSfvdl rpcvers_t vers;
6937df0ccbaSfvdl struct netbuf *address = NULL;
6947df0ccbaSfvdl rpcvers_t start_vers = RPCBVERS4;
6957df0ccbaSfvdl struct netbuf servaddr;
6967df0ccbaSfvdl
6970e8cfd8fSlukem /* nconf is handled below */
6980e8cfd8fSlukem _DIAGASSERT(host != NULL);
6990e8cfd8fSlukem /* clpp may be NULL */
7000e8cfd8fSlukem
7017df0ccbaSfvdl /* parameter checking */
702deb154d2Schristos if (nconf == NULL) {
7037df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
7047df0ccbaSfvdl return (NULL);
7057df0ccbaSfvdl }
7067df0ccbaSfvdl
7077df0ccbaSfvdl parms.r_addr = NULL;
7087df0ccbaSfvdl
7097df0ccbaSfvdl #ifdef PORTMAP
7107df0ccbaSfvdl /* Try version 2 for TCP or UDP */
7117df0ccbaSfvdl if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
7127df0ccbaSfvdl u_short port = 0;
7137df0ccbaSfvdl struct netbuf remote;
7144063df83Sfvdl rpcvers_t pmapvers = 2;
7157df0ccbaSfvdl struct pmap pmapparms;
7167df0ccbaSfvdl
7177df0ccbaSfvdl /*
7187df0ccbaSfvdl * Try UDP only - there are some portmappers out
7197df0ccbaSfvdl * there that use UDP only.
7207df0ccbaSfvdl */
7217df0ccbaSfvdl if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
7227df0ccbaSfvdl struct netconfig *newnconf;
7237df0ccbaSfvdl
724eaee8c44Sfvdl if ((newnconf = getnetconfigent("udp")) == NULL) {
7257df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
7267df0ccbaSfvdl return (NULL);
7277df0ccbaSfvdl }
7287df0ccbaSfvdl client = getclnthandle(host, newnconf, &parms.r_addr);
729eaee8c44Sfvdl freenetconfigent(newnconf);
7307df0ccbaSfvdl } else {
7317df0ccbaSfvdl client = getclnthandle(host, nconf, &parms.r_addr);
7327df0ccbaSfvdl }
733deb154d2Schristos if (client == NULL) {
7347df0ccbaSfvdl return (NULL);
7357df0ccbaSfvdl }
7367df0ccbaSfvdl
7377df0ccbaSfvdl /* Set the version */
738deb154d2Schristos CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&pmapvers);
7397df0ccbaSfvdl pmapparms.pm_prog = program;
7407df0ccbaSfvdl pmapparms.pm_vers = version;
7417df0ccbaSfvdl pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
7427df0ccbaSfvdl IPPROTO_UDP : IPPROTO_TCP;
7437df0ccbaSfvdl pmapparms.pm_port = 0; /* not needed */
744deb154d2Schristos clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
745deb154d2Schristos (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
746deb154d2Schristos (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
7477df0ccbaSfvdl tottimeout);
7487df0ccbaSfvdl if (clnt_st != RPC_SUCCESS) {
7497df0ccbaSfvdl if ((clnt_st == RPC_PROGVERSMISMATCH) ||
7507df0ccbaSfvdl (clnt_st == RPC_PROGUNAVAIL))
7517df0ccbaSfvdl goto try_rpcbind; /* Try different versions */
7527df0ccbaSfvdl rpc_createerr.cf_stat = RPC_PMAPFAILURE;
7537df0ccbaSfvdl clnt_geterr(client, &rpc_createerr.cf_error);
7547df0ccbaSfvdl goto error;
7557df0ccbaSfvdl } else if (port == 0) {
7567df0ccbaSfvdl address = NULL;
7577df0ccbaSfvdl rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
7587df0ccbaSfvdl goto error;
7597df0ccbaSfvdl }
7607df0ccbaSfvdl port = htons(port);
761deb154d2Schristos CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&remote);
7629fdae5aaSdogcow if (((address = malloc(sizeof(struct netbuf))) == NULL) ||
763c9cdc302Schristos ((address->buf = malloc(remote.len)) == NULL)) {
7647df0ccbaSfvdl rpc_createerr.cf_stat = RPC_SYSTEMERROR;
7657df0ccbaSfvdl clnt_geterr(client, &rpc_createerr.cf_error);
7667df0ccbaSfvdl if (address) {
7677df0ccbaSfvdl free(address);
7687df0ccbaSfvdl address = NULL;
7697df0ccbaSfvdl }
7707df0ccbaSfvdl goto error;
7717df0ccbaSfvdl }
7727df0ccbaSfvdl memcpy(address->buf, remote.buf, remote.len);
7737df0ccbaSfvdl memcpy(&((char *)address->buf)[sizeof (short)],
774deb154d2Schristos (char *)(void *)&port, sizeof (short));
7757df0ccbaSfvdl address->len = address->maxlen = remote.len;
7767df0ccbaSfvdl goto done;
7777df0ccbaSfvdl }
7787df0ccbaSfvdl #endif
7797df0ccbaSfvdl
7807df0ccbaSfvdl try_rpcbind:
7817df0ccbaSfvdl /*
7827df0ccbaSfvdl * Now we try version 4 and then 3.
7837df0ccbaSfvdl * We also send the remote system the address we used to
7847df0ccbaSfvdl * contact it in case it can help to connect back with us
7857df0ccbaSfvdl */
7867df0ccbaSfvdl parms.r_prog = program;
7877df0ccbaSfvdl parms.r_vers = version;
78803256c6eSchristos parms.r_owner = __UNCONST(&nullstring[0]); /* not needed; */
7897df0ccbaSfvdl /* just for xdring */
7907df0ccbaSfvdl parms.r_netid = nconf->nc_netid; /* not really needed */
7917df0ccbaSfvdl
7927df0ccbaSfvdl /*
7937df0ccbaSfvdl * If a COTS transport is being used, try getting address via CLTS
7947df0ccbaSfvdl * transport. This works only with version 4.
7957df0ccbaSfvdl * NOTE: This is being done for all transports EXCEPT LOOPBACK
7967df0ccbaSfvdl * because with loopback the cost to go to a COTS is same as
7977df0ccbaSfvdl * the cost to go through CLTS, plus you get the advantage of
7987df0ccbaSfvdl * finding out immediately if the local rpcbind process is dead.
7997df0ccbaSfvdl */
8007df0ccbaSfvdl #if 1
8017df0ccbaSfvdl if ((nconf->nc_semantics == NC_TPI_COTS_ORD ||
8027df0ccbaSfvdl nconf->nc_semantics == NC_TPI_COTS) &&
8030e8cfd8fSlukem (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0))
8047df0ccbaSfvdl #else
8057df0ccbaSfvdl if (client != NULL) {
8067df0ccbaSfvdl CLNT_DESTROY(client);
8077df0ccbaSfvdl client = NULL;
8087df0ccbaSfvdl }
8090e8cfd8fSlukem if (nconf->nc_semantics == NC_TPI_CLTS)
8107df0ccbaSfvdl #endif
8110e8cfd8fSlukem {
8127df0ccbaSfvdl void *handle;
8137df0ccbaSfvdl struct netconfig *nconf_clts;
8147df0ccbaSfvdl rpcb_entry_list_ptr relp = NULL;
8157df0ccbaSfvdl
8167df0ccbaSfvdl if (client == NULL) {
8177df0ccbaSfvdl /* This did not go through the above PORTMAP/TCP code */
8187df0ccbaSfvdl #if 1
8190e8cfd8fSlukem if ((handle = __rpc_setconf("datagram_v")) != NULL)
8207df0ccbaSfvdl #else
8210e8cfd8fSlukem if ((handle = __rpc_setconf("circuit_v")) != NULL)
8227df0ccbaSfvdl #endif
8230e8cfd8fSlukem {
8247df0ccbaSfvdl while ((nconf_clts = __rpc_getconf(handle))
8257df0ccbaSfvdl != NULL) {
8267df0ccbaSfvdl if (strcmp(nconf_clts->nc_protofmly,
8277df0ccbaSfvdl nconf->nc_protofmly) != 0) {
8287df0ccbaSfvdl continue;
8297df0ccbaSfvdl }
8307df0ccbaSfvdl client = getclnthandle(host, nconf_clts,
8317df0ccbaSfvdl &parms.r_addr);
8327df0ccbaSfvdl break;
8337df0ccbaSfvdl }
8347df0ccbaSfvdl __rpc_endconf(handle);
8357df0ccbaSfvdl }
836deb154d2Schristos if (client == NULL)
8377df0ccbaSfvdl goto regular_rpcbind; /* Go the regular way */
8387df0ccbaSfvdl } else {
8397df0ccbaSfvdl /* This is a UDP PORTMAP handle. Change to version 4 */
8407df0ccbaSfvdl vers = RPCBVERS4;
841deb154d2Schristos CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
8427df0ccbaSfvdl }
8437df0ccbaSfvdl /*
8447df0ccbaSfvdl * We also send the remote system the address we used to
8457df0ccbaSfvdl * contact it in case it can help it connect back with us
8467df0ccbaSfvdl */
847deb154d2Schristos if (parms.r_addr == NULL) {
84803256c6eSchristos /* for XDRing */
84903256c6eSchristos parms.r_addr = __UNCONST(&nullstring[0]);
850deb154d2Schristos }
851deb154d2Schristos clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
852deb154d2Schristos (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
8537df0ccbaSfvdl (xdrproc_t) xdr_rpcb_entry_list_ptr,
854deb154d2Schristos (char *)(void *)&relp, tottimeout);
8557df0ccbaSfvdl if (clnt_st == RPC_SUCCESS) {
856deb154d2Schristos if ((address = got_entry(relp, nconf)) != NULL) {
8577df0ccbaSfvdl xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
858deb154d2Schristos (char *)(void *)&relp);
859b8bc8c45Syamt CLNT_CONTROL(client, CLGET_SVC_ADDR,
860b8bc8c45Syamt (char *)(void *)&servaddr);
861b8bc8c45Syamt __rpc_fixup_addr(address, &servaddr);
8627df0ccbaSfvdl goto done;
8637df0ccbaSfvdl }
8647df0ccbaSfvdl /* Entry not found for this transport */
8657df0ccbaSfvdl xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
866deb154d2Schristos (char *)(void *)&relp);
8677df0ccbaSfvdl /*
8687df0ccbaSfvdl * XXX: should have perhaps returned with error but
8697df0ccbaSfvdl * since the remote machine might not always be able
8707df0ccbaSfvdl * to send the address on all transports, we try the
8717df0ccbaSfvdl * regular way with regular_rpcbind
8727df0ccbaSfvdl */
8737df0ccbaSfvdl goto regular_rpcbind;
8747df0ccbaSfvdl } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
8757df0ccbaSfvdl (clnt_st == RPC_PROGUNAVAIL)) {
8767df0ccbaSfvdl start_vers = RPCBVERS; /* Try version 3 now */
8777df0ccbaSfvdl goto regular_rpcbind; /* Try different versions */
8787df0ccbaSfvdl } else {
8797df0ccbaSfvdl rpc_createerr.cf_stat = RPC_PMAPFAILURE;
8807df0ccbaSfvdl clnt_geterr(client, &rpc_createerr.cf_error);
8817df0ccbaSfvdl goto error;
8827df0ccbaSfvdl }
8837df0ccbaSfvdl }
8847df0ccbaSfvdl
8857df0ccbaSfvdl regular_rpcbind:
8867df0ccbaSfvdl
8877df0ccbaSfvdl /* Now the same transport is to be used to get the address */
8887df0ccbaSfvdl #if 1
8897df0ccbaSfvdl if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
8900e8cfd8fSlukem (nconf->nc_semantics == NC_TPI_COTS)))
8917df0ccbaSfvdl #else
8920e8cfd8fSlukem if (client && nconf->nc_semantics == NC_TPI_CLTS)
8937df0ccbaSfvdl #endif
8940e8cfd8fSlukem {
8957df0ccbaSfvdl /* A CLTS type of client - destroy it */
8967df0ccbaSfvdl CLNT_DESTROY(client);
8977df0ccbaSfvdl client = NULL;
8987df0ccbaSfvdl }
8997df0ccbaSfvdl
9007df0ccbaSfvdl if (client == NULL) {
9017df0ccbaSfvdl client = getclnthandle(host, nconf, &parms.r_addr);
9027df0ccbaSfvdl if (client == NULL) {
9037df0ccbaSfvdl goto error;
9047df0ccbaSfvdl }
9057df0ccbaSfvdl }
90603256c6eSchristos if (parms.r_addr == NULL)
90703256c6eSchristos parms.r_addr = __UNCONST(&nullstring[0]);
9087df0ccbaSfvdl
9097df0ccbaSfvdl /* First try from start_vers and then version 3 (RPCBVERS) */
9107df0ccbaSfvdl for (vers = start_vers; vers >= RPCBVERS; vers--) {
9117df0ccbaSfvdl /* Set the version */
912deb154d2Schristos CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
913deb154d2Schristos clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
914deb154d2Schristos (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
91552f552d8Sitojun (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua,
91652f552d8Sitojun tottimeout);
9177df0ccbaSfvdl if (clnt_st == RPC_SUCCESS) {
918b279ed63Sfvdl if ((ua == NULL) || (ua[0] == 0)) {
9197df0ccbaSfvdl /* address unknown */
9207df0ccbaSfvdl rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
9217df0ccbaSfvdl goto error;
9227df0ccbaSfvdl }
9237df0ccbaSfvdl address = uaddr2taddr(nconf, ua);
9247df0ccbaSfvdl #ifdef ND_DEBUG
9257df0ccbaSfvdl fprintf(stderr, "\tRemote address is [%s]\n", ua);
9267df0ccbaSfvdl if (!address)
9277df0ccbaSfvdl fprintf(stderr,
9287df0ccbaSfvdl "\tCouldn't resolve remote address!\n");
9297df0ccbaSfvdl #endif
930deb154d2Schristos xdr_free((xdrproc_t)xdr_wrapstring,
931deb154d2Schristos (char *)(void *)&ua);
9327df0ccbaSfvdl
9337df0ccbaSfvdl if (! address) {
9347df0ccbaSfvdl /* We don't know about your universal address */
9357df0ccbaSfvdl rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
9367df0ccbaSfvdl goto error;
9377df0ccbaSfvdl }
938deb154d2Schristos CLNT_CONTROL(client, CLGET_SVC_ADDR,
939deb154d2Schristos (char *)(void *)&servaddr);
9407df0ccbaSfvdl __rpc_fixup_addr(address, &servaddr);
9417df0ccbaSfvdl goto done;
9427df0ccbaSfvdl } else if (clnt_st == RPC_PROGVERSMISMATCH) {
9437df0ccbaSfvdl struct rpc_err rpcerr;
9447df0ccbaSfvdl
9457df0ccbaSfvdl clnt_geterr(client, &rpcerr);
9467df0ccbaSfvdl if (rpcerr.re_vers.low > RPCBVERS4)
9477df0ccbaSfvdl goto error; /* a new version, can't handle */
9487df0ccbaSfvdl } else if (clnt_st != RPC_PROGUNAVAIL) {
9497df0ccbaSfvdl /* Cant handle this error */
9507df0ccbaSfvdl rpc_createerr.cf_stat = clnt_st;
9517df0ccbaSfvdl clnt_geterr(client, &rpc_createerr.cf_error);
9527df0ccbaSfvdl goto error;
9537df0ccbaSfvdl }
9547df0ccbaSfvdl }
9557df0ccbaSfvdl
9567df0ccbaSfvdl error:
9577df0ccbaSfvdl if (client) {
9587df0ccbaSfvdl CLNT_DESTROY(client);
9597df0ccbaSfvdl client = NULL;
9607df0ccbaSfvdl }
9617df0ccbaSfvdl done:
9627df0ccbaSfvdl if (nconf->nc_semantics != NC_TPI_CLTS) {
9637df0ccbaSfvdl /* This client is the connectionless one */
9647df0ccbaSfvdl if (client) {
9657df0ccbaSfvdl CLNT_DESTROY(client);
9667df0ccbaSfvdl client = NULL;
9677df0ccbaSfvdl }
9687df0ccbaSfvdl }
9697df0ccbaSfvdl if (clpp) {
9707df0ccbaSfvdl *clpp = client;
9717df0ccbaSfvdl } else if (client) {
9727df0ccbaSfvdl CLNT_DESTROY(client);
9737df0ccbaSfvdl }
9747df0ccbaSfvdl return (address);
9757df0ccbaSfvdl }
9767df0ccbaSfvdl
9777df0ccbaSfvdl
9787df0ccbaSfvdl /*
9797df0ccbaSfvdl * Find the mapped address for program, version.
9807df0ccbaSfvdl * Calls the rpcbind service remotely to do the lookup.
9817df0ccbaSfvdl * Uses the transport specified in nconf.
9827df0ccbaSfvdl * Returns FALSE (0) if no map exists, else returns 1.
9837df0ccbaSfvdl *
9847df0ccbaSfvdl * Assuming that the address is all properly allocated
9857df0ccbaSfvdl */
986*1e0adbedSjustin bool_t
rpcb_getaddr(const rpcprog_t program,const rpcvers_t version,const struct netconfig * nconf,struct netbuf * address,const char * host)987*1e0adbedSjustin rpcb_getaddr(const rpcprog_t program, const rpcvers_t version,
988c30c4c40Smatt const struct netconfig *nconf, struct netbuf *address,
989c30c4c40Smatt const char *host)
9907df0ccbaSfvdl {
9917df0ccbaSfvdl struct netbuf *na;
9927df0ccbaSfvdl
9930e8cfd8fSlukem _DIAGASSERT(address != NULL);
9940e8cfd8fSlukem
9957df0ccbaSfvdl if ((na = __rpcb_findaddr(program, version, nconf,
996c9cdc302Schristos host, NULL)) == NULL)
9977df0ccbaSfvdl return (FALSE);
9987df0ccbaSfvdl
9997df0ccbaSfvdl if (na->len > address->maxlen) {
10007df0ccbaSfvdl /* Too long address */
10017df0ccbaSfvdl free(na->buf);
10027df0ccbaSfvdl free(na);
10037df0ccbaSfvdl rpc_createerr.cf_stat = RPC_FAILED;
10047df0ccbaSfvdl return (FALSE);
10057df0ccbaSfvdl }
1006deb154d2Schristos memcpy(address->buf, na->buf, (size_t)na->len);
10077df0ccbaSfvdl address->len = na->len;
10087df0ccbaSfvdl free(na->buf);
10097df0ccbaSfvdl free(na);
10107df0ccbaSfvdl return (TRUE);
10117df0ccbaSfvdl }
10127df0ccbaSfvdl
10137df0ccbaSfvdl /*
10147df0ccbaSfvdl * Get a copy of the current maps.
10157df0ccbaSfvdl * Calls the rpcbind service remotely to get the maps.
10167df0ccbaSfvdl *
10177df0ccbaSfvdl * It returns only a list of the services
10187df0ccbaSfvdl * It returns NULL on failure.
10197df0ccbaSfvdl */
10207df0ccbaSfvdl rpcblist *
rpcb_getmaps(const struct netconfig * nconf,const char * host)1021c30c4c40Smatt rpcb_getmaps(const struct netconfig *nconf, const char *host)
10227df0ccbaSfvdl {
1023deb154d2Schristos rpcblist_ptr head = NULL;
1024deb154d2Schristos CLIENT *client;
10257df0ccbaSfvdl enum clnt_stat clnt_st;
10264063df83Sfvdl rpcvers_t vers = 0;
10277df0ccbaSfvdl
1028deb154d2Schristos client = getclnthandle(host, nconf, NULL);
1029deb154d2Schristos if (client == NULL) {
10307df0ccbaSfvdl return (head);
10317df0ccbaSfvdl }
1032deb154d2Schristos clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1033deb154d2Schristos (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1034deb154d2Schristos (char *)(void *)&head, tottimeout);
10357df0ccbaSfvdl if (clnt_st == RPC_SUCCESS)
10367df0ccbaSfvdl goto done;
10377df0ccbaSfvdl
10387df0ccbaSfvdl if ((clnt_st != RPC_PROGVERSMISMATCH) &&
10397df0ccbaSfvdl (clnt_st != RPC_PROGUNAVAIL)) {
10407df0ccbaSfvdl rpc_createerr.cf_stat = RPC_RPCBFAILURE;
10417df0ccbaSfvdl clnt_geterr(client, &rpc_createerr.cf_error);
10427df0ccbaSfvdl goto done;
10437df0ccbaSfvdl }
10447df0ccbaSfvdl
10457df0ccbaSfvdl /* fall back to earlier version */
1046deb154d2Schristos CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
10477df0ccbaSfvdl if (vers == RPCBVERS4) {
10487df0ccbaSfvdl vers = RPCBVERS;
1049deb154d2Schristos CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1050deb154d2Schristos if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1051deb154d2Schristos (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1052deb154d2Schristos (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
10537df0ccbaSfvdl goto done;
10547df0ccbaSfvdl }
10557df0ccbaSfvdl rpc_createerr.cf_stat = RPC_RPCBFAILURE;
10567df0ccbaSfvdl clnt_geterr(client, &rpc_createerr.cf_error);
10577df0ccbaSfvdl
10587df0ccbaSfvdl done:
10597df0ccbaSfvdl CLNT_DESTROY(client);
10607df0ccbaSfvdl return (head);
10617df0ccbaSfvdl }
10627df0ccbaSfvdl
10637df0ccbaSfvdl /*
10647df0ccbaSfvdl * rpcbinder remote-call-service interface.
10657df0ccbaSfvdl * This routine is used to call the rpcbind remote call service
10667df0ccbaSfvdl * which will look up a service program in the address maps, and then
10677df0ccbaSfvdl * remotely call that routine with the given parameters. This allows
10687df0ccbaSfvdl * programs to do a lookup and call in one step.
10697df0ccbaSfvdl */
10707df0ccbaSfvdl enum clnt_stat
rpcb_rmtcall(const struct netconfig * nconf,const char * host,rpcprog_t prog,rpcvers_t vers,rpcproc_t proc,xdrproc_t xdrargs,const char * argsp,xdrproc_t xdrres,caddr_t resp,struct timeval tout,const struct netbuf * addr_ptr)1071c30c4c40Smatt rpcb_rmtcall(
1072c30c4c40Smatt const struct netconfig *nconf, /* Netconfig structure */
1073c30c4c40Smatt const char *host, /* Remote host name */
1074c30c4c40Smatt rpcprog_t prog,
1075c30c4c40Smatt rpcvers_t vers,
1076c30c4c40Smatt rpcproc_t proc, /* Remote proc identifiers */
1077c30c4c40Smatt xdrproc_t xdrargs,
1078c30c4c40Smatt const char *argsp, /* Argument */
1079c30c4c40Smatt xdrproc_t xdrres, /* XDR routines */
1080c30c4c40Smatt caddr_t resp, /* Result */
1081c30c4c40Smatt struct timeval tout, /* Timeout value for this call */
1082c30c4c40Smatt const struct netbuf *addr_ptr) /* Preallocated netbuf address */
10837df0ccbaSfvdl {
1084deb154d2Schristos CLIENT *client;
10857df0ccbaSfvdl enum clnt_stat stat;
10867df0ccbaSfvdl struct r_rpcb_rmtcallargs a;
10877df0ccbaSfvdl struct r_rpcb_rmtcallres r;
10884063df83Sfvdl rpcvers_t rpcb_vers;
10897df0ccbaSfvdl
109087292a60She stat = RPC_FAILED; /* XXXGCC -Wuninitialized [dreamcast] */
109187292a60She
1092deb154d2Schristos client = getclnthandle(host, nconf, NULL);
1093deb154d2Schristos if (client == NULL) {
10947df0ccbaSfvdl return (RPC_FAILED);
10957df0ccbaSfvdl }
109603256c6eSchristos CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, __UNCONST(&rmttimeout));
10977df0ccbaSfvdl a.prog = prog;
10987df0ccbaSfvdl a.vers = vers;
10997df0ccbaSfvdl a.proc = proc;
11007df0ccbaSfvdl a.args.args_val = argsp;
11017df0ccbaSfvdl a.xdr_args = xdrargs;
11027df0ccbaSfvdl r.addr = NULL;
11037df0ccbaSfvdl r.results.results_val = resp;
11047df0ccbaSfvdl r.xdr_res = xdrres;
11057df0ccbaSfvdl
11067df0ccbaSfvdl for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1107deb154d2Schristos CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1108deb154d2Schristos stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1109deb154d2Schristos (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1110deb154d2Schristos (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
11117df0ccbaSfvdl if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
11127df0ccbaSfvdl struct netbuf *na;
111303256c6eSchristos na = uaddr2taddr(__UNCONST(nconf), r.addr);
11147df0ccbaSfvdl if (!na) {
11157df0ccbaSfvdl stat = RPC_N2AXLATEFAILURE;
111603256c6eSchristos ((struct netbuf *)__UNCONST(addr_ptr))->len = 0;
11177df0ccbaSfvdl goto error;
11187df0ccbaSfvdl }
11197df0ccbaSfvdl if (na->len > addr_ptr->maxlen) {
11207df0ccbaSfvdl /* Too long address */
11217df0ccbaSfvdl stat = RPC_FAILED; /* XXX A better error no */
11227df0ccbaSfvdl free(na->buf);
11237df0ccbaSfvdl free(na);
112403256c6eSchristos ((struct netbuf *)__UNCONST(addr_ptr))->len = 0;
11257df0ccbaSfvdl goto error;
11267df0ccbaSfvdl }
1127deb154d2Schristos memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
112803256c6eSchristos ((struct netbuf *)__UNCONST(addr_ptr))->len = na->len;
11297df0ccbaSfvdl free(na->buf);
11307df0ccbaSfvdl free(na);
11317df0ccbaSfvdl break;
11327df0ccbaSfvdl } else if ((stat != RPC_PROGVERSMISMATCH) &&
11337df0ccbaSfvdl (stat != RPC_PROGUNAVAIL)) {
11347df0ccbaSfvdl goto error;
11357df0ccbaSfvdl }
11367df0ccbaSfvdl }
11377df0ccbaSfvdl error:
11387df0ccbaSfvdl CLNT_DESTROY(client);
11397df0ccbaSfvdl if (r.addr)
1140deb154d2Schristos xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
11417df0ccbaSfvdl return (stat);
11427df0ccbaSfvdl }
11437df0ccbaSfvdl
11447df0ccbaSfvdl /*
11457df0ccbaSfvdl * Gets the time on the remote host.
11467df0ccbaSfvdl * Returns 1 if succeeds else 0.
11477df0ccbaSfvdl */
11487df0ccbaSfvdl bool_t
rpcb_gettime(const char * host,time_t * timep)1149c30c4c40Smatt rpcb_gettime(const char *host, time_t *timep)
11507df0ccbaSfvdl {
11517df0ccbaSfvdl CLIENT *client = NULL;
11527df0ccbaSfvdl void *handle;
11537df0ccbaSfvdl struct netconfig *nconf;
11544063df83Sfvdl rpcvers_t vers;
11557df0ccbaSfvdl enum clnt_stat st;
11567df0ccbaSfvdl
11577df0ccbaSfvdl
1158b279ed63Sfvdl if ((host == NULL) || (host[0] == 0)) {
11597df0ccbaSfvdl time(timep);
11607df0ccbaSfvdl return (TRUE);
11617df0ccbaSfvdl }
11627df0ccbaSfvdl
11637df0ccbaSfvdl if ((handle = __rpc_setconf("netpath")) == NULL) {
11647df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
11657df0ccbaSfvdl return (FALSE);
11667df0ccbaSfvdl }
11677df0ccbaSfvdl rpc_createerr.cf_stat = RPC_SUCCESS;
1168deb154d2Schristos while (client == NULL) {
11697df0ccbaSfvdl if ((nconf = __rpc_getconf(handle)) == NULL) {
11707df0ccbaSfvdl if (rpc_createerr.cf_stat == RPC_SUCCESS)
11717df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
11727df0ccbaSfvdl break;
11737df0ccbaSfvdl }
1174deb154d2Schristos client = getclnthandle(host, nconf, NULL);
11757df0ccbaSfvdl if (client)
11767df0ccbaSfvdl break;
11777df0ccbaSfvdl }
11787df0ccbaSfvdl __rpc_endconf(handle);
1179c9cdc302Schristos if (client == NULL) {
11807df0ccbaSfvdl return (FALSE);
11817df0ccbaSfvdl }
11827df0ccbaSfvdl
1183deb154d2Schristos st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1184deb154d2Schristos (xdrproc_t) xdr_void, NULL,
1185deb154d2Schristos (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
11867df0ccbaSfvdl
11877df0ccbaSfvdl if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1188deb154d2Schristos CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
11897df0ccbaSfvdl if (vers == RPCBVERS4) {
11907df0ccbaSfvdl /* fall back to earlier version */
11917df0ccbaSfvdl vers = RPCBVERS;
1192deb154d2Schristos CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1193deb154d2Schristos st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1194deb154d2Schristos (xdrproc_t) xdr_void, NULL,
1195deb154d2Schristos (xdrproc_t) xdr_int, (char *)(void *)timep,
11967df0ccbaSfvdl tottimeout);
11977df0ccbaSfvdl }
11987df0ccbaSfvdl }
11997df0ccbaSfvdl CLNT_DESTROY(client);
12007df0ccbaSfvdl return (st == RPC_SUCCESS? TRUE: FALSE);
12017df0ccbaSfvdl }
12027df0ccbaSfvdl
12037df0ccbaSfvdl /*
12047df0ccbaSfvdl * Converts taddr to universal address. This routine should never
12057df0ccbaSfvdl * really be called because local n2a libraries are always provided.
12067df0ccbaSfvdl */
12077df0ccbaSfvdl char *
rpcb_taddr2uaddr(struct netconfig * nconf,struct netbuf * taddr)1208c30c4c40Smatt rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
12097df0ccbaSfvdl {
12107df0ccbaSfvdl CLIENT *client;
12117df0ccbaSfvdl char *uaddr = NULL;
12127df0ccbaSfvdl
12137df0ccbaSfvdl /* parameter checking */
1214deb154d2Schristos if (nconf == NULL) {
12157df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
12167df0ccbaSfvdl return (NULL);
12177df0ccbaSfvdl }
12187df0ccbaSfvdl if (taddr == NULL) {
12197df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
12207df0ccbaSfvdl return (NULL);
12217df0ccbaSfvdl }
12227df0ccbaSfvdl client = local_rpcb();
12237df0ccbaSfvdl if (! client) {
12247df0ccbaSfvdl return (NULL);
12257df0ccbaSfvdl }
12267df0ccbaSfvdl
1227bbf77bceSchristos if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1228deb154d2Schristos (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1229bbf77bceSchristos (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout)
1230bbf77bceSchristos != RPC_SUCCESS) {
1231bbf77bceSchristos rpc_createerr.cf_stat = RPC_PMAPFAILURE;
1232bbf77bceSchristos clnt_geterr(client, &rpc_createerr.cf_error);
1233bbf77bceSchristos }
12347df0ccbaSfvdl CLNT_DESTROY(client);
12357df0ccbaSfvdl return (uaddr);
12367df0ccbaSfvdl }
12377df0ccbaSfvdl
12387df0ccbaSfvdl /*
12397df0ccbaSfvdl * Converts universal address to netbuf. This routine should never
12407df0ccbaSfvdl * really be called because local n2a libraries are always provided.
12417df0ccbaSfvdl */
12427df0ccbaSfvdl struct netbuf *
rpcb_uaddr2taddr(struct netconfig * nconf,char * uaddr)1243c30c4c40Smatt rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
12447df0ccbaSfvdl {
12457df0ccbaSfvdl CLIENT *client;
12467df0ccbaSfvdl struct netbuf *taddr;
12477df0ccbaSfvdl
12487df0ccbaSfvdl
12497df0ccbaSfvdl /* parameter checking */
1250deb154d2Schristos if (nconf == NULL) {
12517df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
12527df0ccbaSfvdl return (NULL);
12537df0ccbaSfvdl }
12547df0ccbaSfvdl if (uaddr == NULL) {
12557df0ccbaSfvdl rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
12567df0ccbaSfvdl return (NULL);
12577df0ccbaSfvdl }
12587df0ccbaSfvdl client = local_rpcb();
12597df0ccbaSfvdl if (! client) {
12607df0ccbaSfvdl return (NULL);
12617df0ccbaSfvdl }
12627df0ccbaSfvdl
12637df0ccbaSfvdl taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
12647df0ccbaSfvdl if (taddr == NULL) {
12657df0ccbaSfvdl CLNT_DESTROY(client);
12667df0ccbaSfvdl return (NULL);
12677df0ccbaSfvdl }
1268deb154d2Schristos if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1269deb154d2Schristos (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1270deb154d2Schristos (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
12717df0ccbaSfvdl tottimeout) != RPC_SUCCESS) {
1272bbf77bceSchristos rpc_createerr.cf_stat = RPC_PMAPFAILURE;
1273bbf77bceSchristos clnt_geterr(client, &rpc_createerr.cf_error);
12747df0ccbaSfvdl free(taddr);
12757df0ccbaSfvdl taddr = NULL;
12767df0ccbaSfvdl }
12777df0ccbaSfvdl CLNT_DESTROY(client);
12787df0ccbaSfvdl return (taddr);
12797df0ccbaSfvdl }
1280