2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1989, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A/*
2N/A * Portions of this source code were derived from Berkeley
2N/A * 4.3 BSD under license from the Regents of the University of
2N/A * California.
2N/A */
2N/A
2N/A/*
2N/A * interface to rpcbind rpc service.
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include "rpc_mt.h"
2N/A#include <assert.h>
2N/A#include <rpc/rpc.h>
2N/A#include <rpc/rpcb_prot.h>
2N/A#include <netconfig.h>
2N/A#include <netdir.h>
2N/A#include <rpc/nettype.h>
2N/A#include <syslog.h>
2N/A#ifdef PORTMAP
2N/A#include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
2N/A#include <rpc/pmap_prot.h>
2N/A#endif
2N/A#ifdef ND_DEBUG
2N/A#include <stdio.h>
2N/A#endif
2N/A#include <sys/utsname.h>
2N/A#include <errno.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A
2N/Astatic struct timeval tottimeout = { 60, 0 };
2N/Astatic const struct timeval rmttimeout = { 3, 0 };
2N/Astatic struct timeval rpcbrmttime = { 15, 0 };
2N/A
2N/Aextern bool_t xdr_wrapstring(XDR *, char **);
2N/A
2N/Astatic const char nullstring[] = "\000";
2N/A
2N/Aextern CLIENT *_clnt_tli_create_timed(int, const struct netconfig *,
2N/A struct netbuf *, rpcprog_t, rpcvers_t, uint_t, uint_t,
2N/A const struct timeval *);
2N/A
2N/Astatic CLIENT *_getclnthandle_timed(char *, struct netconfig *, char **,
2N/A struct timeval *);
2N/A
2N/A
2N/A/*
2N/A * The life time of a cached entry should not exceed 5 minutes
2N/A * since automountd attempts an unmount every 5 minutes.
2N/A * It is arbitrarily set a little lower (3 min = 180 sec)
2N/A * to reduce the time during which an entry is stale.
2N/A */
2N/A#define CACHE_TTL 180
2N/A#define CACHESIZE 6
2N/A
2N/Astruct address_cache {
2N/A char *ac_host;
2N/A char *ac_netid;
2N/A char *ac_uaddr;
2N/A struct netbuf *ac_taddr;
2N/A struct address_cache *ac_next;
2N/A time_t ac_maxtime;
2N/A};
2N/A
2N/Astatic struct address_cache *front;
2N/Astatic int cachesize;
2N/A
2N/Aextern int lowvers;
2N/Aextern int authdes_cachesz;
2N/A/*
2N/A * This routine adjusts the timeout used for calls to the remote rpcbind.
2N/A * Also, this routine can be used to set the use of portmapper version 2
2N/A * only when doing rpc_broadcasts
2N/A * These are private routines that may not be provided in future releases.
2N/A */
2N/Abool_t
2N/A__rpc_control(int request, void *info)
2N/A{
2N/A switch (request) {
2N/A case CLCR_GET_RPCB_TIMEOUT:
2N/A *(struct timeval *)info = tottimeout;
2N/A break;
2N/A case CLCR_SET_RPCB_TIMEOUT:
2N/A tottimeout = *(struct timeval *)info;
2N/A break;
2N/A case CLCR_GET_LOWVERS:
2N/A *(int *)info = lowvers;
2N/A break;
2N/A case CLCR_SET_LOWVERS:
2N/A lowvers = *(int *)info;
2N/A break;
2N/A case CLCR_GET_RPCB_RMTTIME:
2N/A *(struct timeval *)info = rpcbrmttime;
2N/A break;
2N/A case CLCR_SET_RPCB_RMTTIME:
2N/A rpcbrmttime = *(struct timeval *)info;
2N/A break;
2N/A case CLCR_GET_CRED_CACHE_SZ:
2N/A *(int *)info = authdes_cachesz;
2N/A break;
2N/A case CLCR_SET_CRED_CACHE_SZ:
2N/A authdes_cachesz = *(int *)info;
2N/A break;
2N/A default:
2N/A return (FALSE);
2N/A }
2N/A return (TRUE);
2N/A}
2N/A
2N/A/*
2N/A * It might seem that a reader/writer lock would be more reasonable here.
2N/A * However because getclnthandle(), the only user of the cache functions,
2N/A * may do a delete_cache() operation if a check_cache() fails to return an
2N/A * address useful to clnt_tli_create(), we may as well use a mutex.
2N/A */
2N/A/*
2N/A * As it turns out, if the cache lock is *not* a reader/writer lock, we will
2N/A * block all clnt_create's if we are trying to connect to a host that's down,
2N/A * since the lock will be held all during that time.
2N/A */
2N/Aextern rwlock_t rpcbaddr_cache_lock;
2N/A
2N/A/*
2N/A * The routines check_cache(), add_cache(), delete_cache() manage the
2N/A * cache of rpcbind addresses for (host, netid).
2N/A */
2N/A
2N/Astatic struct address_cache *
2N/Acheck_cache(char *host, char *netid)
2N/A{
2N/A struct address_cache *cptr;
2N/A
2N/A /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
2N/A
2N/A assert(RW_READ_HELD(&rpcbaddr_cache_lock));
2N/A for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
2N/A if ((strcmp(cptr->ac_host, host) == 0) &&
2N/A (strcmp(cptr->ac_netid, netid) == 0) &&
2N/A (time(NULL) <= cptr->ac_maxtime)) {
2N/A#ifdef ND_DEBUG
2N/A fprintf(stderr, "Found cache entry for %s: %s\n",
2N/A host, netid);
2N/A#endif
2N/A return (cptr);
2N/A }
2N/A }
2N/A return (NULL);
2N/A}
2N/A
2N/Astatic void
2N/Adelete_cache(struct netbuf *addr)
2N/A{
2N/A struct address_cache *cptr, *prevptr = NULL;
2N/A
2N/A /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
2N/A assert(RW_WRITE_HELD(&rpcbaddr_cache_lock));
2N/A for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
2N/A if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
2N/A free(cptr->ac_host);
2N/A free(cptr->ac_netid);
2N/A free(cptr->ac_taddr->buf);
2N/A free(cptr->ac_taddr);
2N/A if (cptr->ac_uaddr)
2N/A free(cptr->ac_uaddr);
2N/A if (prevptr)
2N/A prevptr->ac_next = cptr->ac_next;
2N/A else
2N/A front = cptr->ac_next;
2N/A free(cptr);
2N/A cachesize--;
2N/A break;
2N/A }
2N/A prevptr = cptr;
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Aadd_cache(char *host, char *netid, struct netbuf *taddr, char *uaddr)
2N/A{
2N/A struct address_cache *ad_cache, *cptr, *prevptr;
2N/A
2N/A ad_cache = malloc(sizeof (struct address_cache));
2N/A if (!ad_cache) {
2N/A goto memerr;
2N/A }
2N/A ad_cache->ac_maxtime = time(NULL) + CACHE_TTL;
2N/A ad_cache->ac_host = strdup(host);
2N/A ad_cache->ac_netid = strdup(netid);
2N/A ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
2N/A ad_cache->ac_taddr = malloc(sizeof (struct netbuf));
2N/A if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
2N/A (uaddr && !ad_cache->ac_uaddr)) {
2N/A goto memerr1;
2N/A }
2N/A
2N/A ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
2N/A ad_cache->ac_taddr->buf = malloc(taddr->len);
2N/A if (ad_cache->ac_taddr->buf == NULL) {
2N/A goto memerr1;
2N/A }
2N/A
2N/A (void) memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
2N/A#ifdef ND_DEBUG
2N/A (void) fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
2N/A#endif
2N/A
2N/A/* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
2N/A
2N/A (void) rw_wrlock(&rpcbaddr_cache_lock);
2N/A if (cachesize < CACHESIZE) {
2N/A ad_cache->ac_next = front;
2N/A front = ad_cache;
2N/A cachesize++;
2N/A } else {
2N/A /* Free the last entry */
2N/A cptr = front;
2N/A prevptr = NULL;
2N/A while (cptr->ac_next) {
2N/A prevptr = cptr;
2N/A cptr = cptr->ac_next;
2N/A }
2N/A
2N/A#ifdef ND_DEBUG
2N/A fprintf(stderr, "Deleted from cache: %s : %s\n",
2N/A cptr->ac_host, cptr->ac_netid);
2N/A#endif
2N/A free(cptr->ac_host);
2N/A free(cptr->ac_netid);
2N/A free(cptr->ac_taddr->buf);
2N/A free(cptr->ac_taddr);
2N/A if (cptr->ac_uaddr)
2N/A free(cptr->ac_uaddr);
2N/A
2N/A if (prevptr) {
2N/A prevptr->ac_next = NULL;
2N/A ad_cache->ac_next = front;
2N/A front = ad_cache;
2N/A } else {
2N/A front = ad_cache;
2N/A ad_cache->ac_next = NULL;
2N/A }
2N/A free(cptr);
2N/A }
2N/A (void) rw_unlock(&rpcbaddr_cache_lock);
2N/A return;
2N/Amemerr1:
2N/A if (ad_cache->ac_host)
2N/A free(ad_cache->ac_host);
2N/A if (ad_cache->ac_netid)
2N/A free(ad_cache->ac_netid);
2N/A if (ad_cache->ac_uaddr)
2N/A free(ad_cache->ac_uaddr);
2N/A if (ad_cache->ac_taddr)
2N/A free(ad_cache->ac_taddr);
2N/A free(ad_cache);
2N/Amemerr:
2N/A syslog(LOG_ERR, "add_cache : out of memory.");
2N/A}
2N/A
2N/A/*
2N/A * This routine will return a client handle that is connected to the
2N/A * rpcbind. Returns NULL on error and free's everything.
2N/A */
2N/Astatic CLIENT *
2N/Agetclnthandle(char *host, struct netconfig *nconf, char **targaddr)
2N/A{
2N/A return (_getclnthandle_timed(host, nconf, targaddr, NULL));
2N/A}
2N/A
2N/A/*
2N/A * Same as getclnthandle() except it takes an extra timeout argument.
2N/A * This is for bug 4049792: clnt_create_timed does not timeout.
2N/A *
2N/A * If tp is NULL, use default timeout to get a client handle.
2N/A */
2N/Astatic CLIENT *
2N/A_getclnthandle_timed(char *host, struct netconfig *nconf, char **targaddr,
2N/A struct timeval *tp)
2N/A{
2N/A CLIENT *client = NULL;
2N/A struct netbuf *addr;
2N/A struct netbuf addr_to_delete;
2N/A struct nd_addrlist *nas;
2N/A struct nd_hostserv rpcbind_hs;
2N/A struct address_cache *ad_cache;
2N/A char *tmpaddr;
2N/A int neterr;
2N/A int j;
2N/A
2N/A/* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
2N/A
2N/A /* Get the address of the rpcbind. Check cache first */
2N/A addr_to_delete.len = 0;
2N/A (void) rw_rdlock(&rpcbaddr_cache_lock);
2N/A ad_cache = check_cache(host, nconf->nc_netid);
2N/A if (ad_cache != NULL) {
2N/A addr = ad_cache->ac_taddr;
2N/A client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr,
2N/A RPCBPROG, RPCBVERS4, 0, 0, tp);
2N/A if (client != NULL) {
2N/A if (targaddr) {
2N/A /*
2N/A * case where a client handle is created
2N/A * without a targaddr and the handle is
2N/A * requested with a targaddr
2N/A */
2N/A if (ad_cache->ac_uaddr != NULL) {
2N/A *targaddr = strdup(ad_cache->ac_uaddr);
2N/A if (*targaddr == NULL) {
2N/A syslog(LOG_ERR,
2N/A "_getclnthandle_timed: strdup "
2N/A "failed.");
2N/A rpc_createerr.cf_stat =
2N/A RPC_SYSTEMERROR;
2N/A (void) rw_unlock(
2N/A &rpcbaddr_cache_lock);
2N/A return (NULL);
2N/A }
2N/A } else {
2N/A *targaddr = NULL;
2N/A }
2N/A }
2N/A (void) rw_unlock(&rpcbaddr_cache_lock);
2N/A return (client);
2N/A }
2N/A if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
2N/A (void) rw_unlock(&rpcbaddr_cache_lock);
2N/A return (NULL);
2N/A }
2N/A addr_to_delete.len = addr->len;
2N/A addr_to_delete.buf = malloc(addr->len);
2N/A if (addr_to_delete.buf == NULL) {
2N/A addr_to_delete.len = 0;
2N/A } else {
2N/A (void) memcpy(addr_to_delete.buf, addr->buf, addr->len);
2N/A }
2N/A }
2N/A (void) rw_unlock(&rpcbaddr_cache_lock);
2N/A if (addr_to_delete.len != 0) {
2N/A /*
2N/A * Assume this may be due to cache data being
2N/A * outdated
2N/A */
2N/A (void) rw_wrlock(&rpcbaddr_cache_lock);
2N/A delete_cache(&addr_to_delete);
2N/A (void) rw_unlock(&rpcbaddr_cache_lock);
2N/A free(addr_to_delete.buf);
2N/A }
2N/A rpcbind_hs.h_host = host;
2N/A rpcbind_hs.h_serv = "rpcbind";
2N/A#ifdef ND_DEBUG
2N/A fprintf(stderr, "rpcbind client routines: diagnostics :\n");
2N/A fprintf(stderr, "\tGetting address for (%s, %s, %s) ... \n",
2N/A rpcbind_hs.h_host, rpcbind_hs.h_serv, nconf->nc_netid);
2N/A#endif
2N/A
2N/A if ((neterr = netdir_getbyname(nconf, &rpcbind_hs, &nas)) != 0) {
2N/A if (neterr == ND_NOHOST)
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
2N/A else
2N/A rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2N/A return (NULL);
2N/A }
2N/A /* XXX nas should perhaps be cached for better performance */
2N/A
2N/A for (j = 0; j < nas->n_cnt; j++) {
2N/A addr = &(nas->n_addrs[j]);
2N/A#ifdef ND_DEBUG
2N/A{
2N/A int i;
2N/A char *ua;
2N/A
2N/A ua = taddr2uaddr(nconf, &(nas->n_addrs[j]));
2N/A fprintf(stderr, "Got it [%s]\n", ua);
2N/A free(ua);
2N/A
2N/A fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
2N/A addr->len, addr->maxlen);
2N/A fprintf(stderr, "\tAddress is ");
2N/A for (i = 0; i < addr->len; i++)
2N/A fprintf(stderr, "%u.", addr->buf[i]);
2N/A fprintf(stderr, "\n");
2N/A}
2N/A#endif
2N/A client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr, RPCBPROG,
2N/A RPCBVERS4, 0, 0, tp);
2N/A if (client)
2N/A break;
2N/A }
2N/A#ifdef ND_DEBUG
2N/A if (!client) {
2N/A clnt_pcreateerror("rpcbind clnt interface");
2N/A }
2N/A#endif
2N/A
2N/A if (client) {
2N/A tmpaddr = targaddr ? taddr2uaddr(nconf, addr) : NULL;
2N/A add_cache(host, nconf->nc_netid, addr, tmpaddr);
2N/A if (targaddr) {
2N/A *targaddr = tmpaddr;
2N/A }
2N/A }
2N/A netdir_free((char *)nas, ND_ADDRLIST);
2N/A return (client);
2N/A}
2N/A
2N/A/*
2N/A * This routine will return a client handle that is connected to the local
2N/A * rpcbind. Returns NULL on error and free's everything.
2N/A */
2N/Astatic CLIENT *
2N/Alocal_rpcb(void)
2N/A{
2N/A static struct netconfig *loopnconf;
2N/A static char *hostname;
2N/A extern mutex_t loopnconf_lock;
2N/A
2N/A/* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
2N/A (void) mutex_lock(&loopnconf_lock);
2N/A if (loopnconf == NULL) {
2N/A struct utsname utsname;
2N/A struct netconfig *nconf, *tmpnconf = NULL;
2N/A void *nc_handle;
2N/A
2N/A if (hostname == NULL) {
2N/A#if defined(__i386) && !defined(__amd64)
2N/A if ((_nuname(&utsname) == -1) ||
2N/A#else
2N/A if ((uname(&utsname) == -1) ||
2N/A#endif
2N/A ((hostname = strdup(utsname.nodename)) == NULL)) {
2N/A syslog(LOG_ERR, "local_rpcb : strdup failed.");
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
2N/A (void) mutex_unlock(&loopnconf_lock);
2N/A return (NULL);
2N/A }
2N/A }
2N/A nc_handle = setnetconfig();
2N/A if (nc_handle == NULL) {
2N/A /* fails to open netconfig file */
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A (void) mutex_unlock(&loopnconf_lock);
2N/A return (NULL);
2N/A }
2N/A while (nconf = getnetconfig(nc_handle)) {
2N/A if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
2N/A tmpnconf = nconf;
2N/A if (nconf->nc_semantics == NC_TPI_CLTS)
2N/A break;
2N/A }
2N/A }
2N/A if (tmpnconf == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A (void) mutex_unlock(&loopnconf_lock);
2N/A return (NULL);
2N/A }
2N/A loopnconf = getnetconfigent(tmpnconf->nc_netid);
2N/A /* loopnconf is never freed */
2N/A (void) endnetconfig(nc_handle);
2N/A }
2N/A (void) mutex_unlock(&loopnconf_lock);
2N/A return (getclnthandle(hostname, loopnconf, NULL));
2N/A}
2N/A
2N/A/*
2N/A * Set a mapping between program, version and address.
2N/A * Calls the rpcbind service to do the mapping.
2N/A */
2N/Abool_t
2N/Arpcb_set(const rpcprog_t program, const rpcvers_t version,
2N/A const struct netconfig *nconf, const struct netbuf *address)
2N/A{
2N/A CLIENT *client;
2N/A bool_t rslt = FALSE;
2N/A RPCB parms;
2N/A char uidbuf[32];
2N/A
2N/A /* parameter checking */
2N/A if (nconf == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (FALSE);
2N/A }
2N/A if (address == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
2N/A return (FALSE);
2N/A }
2N/A client = local_rpcb();
2N/A if (!client)
2N/A return (FALSE);
2N/A
2N/A parms.r_addr = taddr2uaddr((struct netconfig *)nconf,
2N/A (struct netbuf *)address); /* convert to universal */
2N/A if (!parms.r_addr) {
2N/A rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2N/A return (FALSE); /* no universal address */
2N/A }
2N/A parms.r_prog = program;
2N/A parms.r_vers = version;
2N/A parms.r_netid = nconf->nc_netid;
2N/A /*
2N/A * Though uid is not being used directly, we still send it for
2N/A * completeness. For non-unix platforms, perhaps some other
2N/A * string or an empty string can be sent.
2N/A */
2N/A (void) sprintf(uidbuf, "%d", (int)geteuid());
2N/A parms.r_owner = uidbuf;
2N/A
2N/A CLNT_CALL(client, RPCBPROC_SET, (xdrproc_t)xdr_rpcb, (char *)&parms,
2N/A (xdrproc_t)xdr_bool, (char *)&rslt, tottimeout);
2N/A
2N/A CLNT_DESTROY(client);
2N/A free(parms.r_addr);
2N/A return (rslt);
2N/A}
2N/A
2N/A/*
2N/A * Remove the mapping between program, version and netbuf address.
2N/A * Calls the rpcbind service to do the un-mapping.
2N/A * If netbuf is NULL, unset for all the transports, otherwise unset
2N/A * only for the given transport.
2N/A */
2N/Abool_t
2N/Arpcb_unset(const rpcprog_t program, const rpcvers_t version,
2N/A const struct netconfig *nconf)
2N/A{
2N/A CLIENT *client;
2N/A bool_t rslt = FALSE;
2N/A RPCB parms;
2N/A char uidbuf[32];
2N/A
2N/A client = local_rpcb();
2N/A if (!client)
2N/A return (FALSE);
2N/A
2N/A parms.r_prog = program;
2N/A parms.r_vers = version;
2N/A if (nconf)
2N/A parms.r_netid = nconf->nc_netid;
2N/A else
2N/A parms.r_netid = (char *)&nullstring[0]; /* unsets all */
2N/A parms.r_addr = (char *)&nullstring[0];
2N/A (void) sprintf(uidbuf, "%d", (int)geteuid());
2N/A parms.r_owner = uidbuf;
2N/A
2N/A CLNT_CALL(client, RPCBPROC_UNSET, (xdrproc_t)xdr_rpcb, (char *)&parms,
2N/A (xdrproc_t)xdr_bool, (char *)&rslt, tottimeout);
2N/A
2N/A CLNT_DESTROY(client);
2N/A return (rslt);
2N/A}
2N/A
2N/A/*
2N/A * From the merged list, find the appropriate entry
2N/A */
2N/Astatic struct netbuf *
2N/Agot_entry(rpcb_entry_list_ptr relp, struct netconfig *nconf)
2N/A{
2N/A struct netbuf *na = NULL;
2N/A rpcb_entry_list_ptr sp;
2N/A rpcb_entry *rmap;
2N/A
2N/A for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
2N/A rmap = &sp->rpcb_entry_map;
2N/A if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
2N/A (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
2N/A (nconf->nc_semantics == rmap->r_nc_semantics) &&
2N/A (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
2N/A na = uaddr2taddr(nconf, rmap->r_maddr);
2N/A#ifdef ND_DEBUG
2N/A fprintf(stderr, "\tRemote address is [%s].\n",
2N/A rmap->r_maddr);
2N/A if (!na)
2N/A fprintf(stderr,
2N/A "\tCouldn't resolve remote address!\n");
2N/A#endif
2N/A break;
2N/A }
2N/A }
2N/A return (na);
2N/A}
2N/A
2N/A/*
2N/A * Quick check to see if rpcbind is up. Tries to connect over
2N/A * local transport.
2N/A */
2N/Abool_t
2N/A__rpcbind_is_up(void)
2N/A{
2N/A struct utsname name;
2N/A char uaddr[SYS_NMLN];
2N/A struct netbuf *addr;
2N/A int fd;
2N/A struct t_call *sndcall;
2N/A struct netconfig *netconf;
2N/A bool_t res;
2N/A
2N/A#if defined(__i386) && !defined(__amd64)
2N/A if (_nuname(&name) == -1)
2N/A#else
2N/A if (uname(&name) == -1)
2N/A#endif
2N/A return (TRUE);
2N/A
2N/A if ((fd = t_open("/dev/ticotsord", O_RDWR, NULL)) == -1)
2N/A return (TRUE);
2N/A
2N/A if (t_bind(fd, NULL, NULL) == -1) {
2N/A (void) t_close(fd);
2N/A return (TRUE);
2N/A }
2N/A
2N/A if ((sndcall = (struct t_call *)t_alloc(fd, T_CALL, 0)) == NULL) {
2N/A (void) t_close(fd);
2N/A return (TRUE);
2N/A }
2N/A
2N/A uaddr[0] = '\0';
2N/A (void) strcpy(uaddr, name.nodename);
2N/A (void) strcat(uaddr, ".rpc");
2N/A if ((netconf = getnetconfigent("ticotsord")) == NULL) {
2N/A (void) t_free((char *)sndcall, T_CALL);
2N/A (void) t_close(fd);
2N/A return (FALSE);
2N/A }
2N/A addr = uaddr2taddr(netconf, uaddr);
2N/A freenetconfigent(netconf);
2N/A if (addr == NULL || addr->buf == NULL) {
2N/A if (addr)
2N/A free(addr);
2N/A (void) t_free((char *)sndcall, T_CALL);
2N/A (void) t_close(fd);
2N/A return (FALSE);
2N/A }
2N/A sndcall->addr.maxlen = addr->maxlen;
2N/A sndcall->addr.len = addr->len;
2N/A sndcall->addr.buf = addr->buf;
2N/A
2N/A if (t_connect(fd, sndcall, NULL) == -1)
2N/A res = FALSE;
2N/A else
2N/A res = TRUE;
2N/A
2N/A sndcall->addr.maxlen = sndcall->addr.len = 0;
2N/A sndcall->addr.buf = NULL;
2N/A (void) t_free((char *)sndcall, T_CALL);
2N/A free(addr->buf);
2N/A free(addr);
2N/A (void) t_close(fd);
2N/A
2N/A return (res);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * An internal function which optimizes rpcb_getaddr function. It also
2N/A * returns the client handle that it uses to contact the remote rpcbind.
2N/A *
2N/A * The algorithm used: If the transports is TCP or UDP, it first tries
2N/A * version 2 (portmap), 4 and then 3 (svr4). This order should be
2N/A * changed in the next OS release to 4, 2 and 3. We are assuming that by
2N/A * that time, version 4 would be available on many machines on the network.
2N/A * With this algorithm, we get performance as well as a plan for
2N/A * obsoleting version 2.
2N/A *
2N/A * For all other transports, the algorithm remains as 4 and then 3.
2N/A *
2N/A * XXX: Due to some problems with t_connect(), we do not reuse the same client
2N/A * handle for COTS cases and hence in these cases we do not return the
2N/A * client handle. This code will change if t_connect() ever
2N/A * starts working properly. Also look under clnt_vc.c.
2N/A */
2N/Astruct netbuf *
2N/A__rpcb_findaddr_timed(rpcprog_t program, rpcvers_t version,
2N/A struct netconfig *nconf, char *host, CLIENT **clpp, struct timeval *tp)
2N/A{
2N/A static bool_t check_rpcbind = TRUE;
2N/A CLIENT *client = NULL;
2N/A RPCB parms;
2N/A enum clnt_stat clnt_st;
2N/A char *ua = NULL;
2N/A uint_t vers;
2N/A struct netbuf *address = NULL;
2N/A uint_t start_vers = RPCBVERS4;
2N/A
2N/A /* parameter checking */
2N/A if (nconf == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A
2N/A parms.r_addr = NULL;
2N/A
2N/A /*
2N/A * Use default total timeout if no timeout is specified.
2N/A */
2N/A if (tp == NULL)
2N/A tp = &tottimeout;
2N/A
2N/A#ifdef PORTMAP
2N/A /* Try version 2 for TCP or UDP */
2N/A if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
2N/A ushort_t port = 0;
2N/A struct netbuf remote;
2N/A uint_t pmapvers = 2;
2N/A struct pmap pmapparms;
2N/A
2N/A /*
2N/A * Try UDP only - there are some portmappers out
2N/A * there that use UDP only.
2N/A */
2N/A if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
2N/A struct netconfig *newnconf;
2N/A void *handle;
2N/A
2N/A if ((handle = __rpc_setconf("udp")) == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * The following to reinforce that you can
2N/A * only request for remote address through
2N/A * the same transport you are requesting.
2N/A * ie. requesting unversial address
2N/A * of IPv4 has to be carried through IPv4.
2N/A * Can't use IPv6 to send out the request.
2N/A * The mergeaddr in rpcbind can't handle
2N/A * this.
2N/A */
2N/A for (;;) {
2N/A if ((newnconf = __rpc_getconf(handle))
2N/A == NULL) {
2N/A __rpc_endconf(handle);
2N/A rpc_createerr.cf_stat =
2N/A RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A /*
2N/A * here check the protocol family to
2N/A * be consistent with the request one
2N/A */
2N/A if (strcmp(newnconf->nc_protofmly,
2N/A nconf->nc_protofmly) == NULL)
2N/A break;
2N/A }
2N/A
2N/A client = _getclnthandle_timed(host, newnconf,
2N/A &parms.r_addr, tp);
2N/A __rpc_endconf(handle);
2N/A } else {
2N/A client = _getclnthandle_timed(host, nconf,
2N/A &parms.r_addr, tp);
2N/A }
2N/A if (client == NULL)
2N/A return (NULL);
2N/A
2N/A /*
2N/A * Set version and retry timeout.
2N/A */
2N/A CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
2N/A CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
2N/A
2N/A pmapparms.pm_prog = program;
2N/A pmapparms.pm_vers = version;
2N/A pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
2N/A IPPROTO_UDP : IPPROTO_TCP;
2N/A pmapparms.pm_port = 0; /* not needed */
2N/A clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
2N/A (xdrproc_t)xdr_pmap, (caddr_t)&pmapparms,
2N/A (xdrproc_t)xdr_u_short, (caddr_t)&port,
2N/A *tp);
2N/A if (clnt_st != RPC_SUCCESS) {
2N/A if ((clnt_st == RPC_PROGVERSMISMATCH) ||
2N/A (clnt_st == RPC_PROGUNAVAIL))
2N/A goto try_rpcbind; /* Try different versions */
2N/A rpc_createerr.cf_stat = RPC_PMAPFAILURE;
2N/A clnt_geterr(client, &rpc_createerr.cf_error);
2N/A goto error;
2N/A } else if (port == 0) {
2N/A address = NULL;
2N/A rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
2N/A goto error;
2N/A }
2N/A port = htons(port);
2N/A CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
2N/A if (((address = malloc(sizeof (struct netbuf))) == NULL) ||
2N/A ((address->buf = malloc(remote.len)) == NULL)) {
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A clnt_geterr(client, &rpc_createerr.cf_error);
2N/A if (address) {
2N/A free(address);
2N/A address = NULL;
2N/A }
2N/A goto error;
2N/A }
2N/A (void) memcpy(address->buf, remote.buf, remote.len);
2N/A (void) memcpy(&((char *)address->buf)[sizeof (short)], &port,
2N/A sizeof (short));
2N/A address->len = address->maxlen = remote.len;
2N/A goto done;
2N/A }
2N/A#endif
2N/A
2N/Atry_rpcbind:
2N/A /*
2N/A * Check if rpcbind is up. This prevents needless delays when
2N/A * accessing applications such as the keyserver while booting
2N/A * disklessly.
2N/A */
2N/A if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
2N/A if (!__rpcbind_is_up()) {
2N/A rpc_createerr.cf_stat = RPC_PMAPFAILURE;
2N/A rpc_createerr.cf_error.re_errno = 0;
2N/A rpc_createerr.cf_error.re_terrno = 0;
2N/A goto error;
2N/A }
2N/A check_rpcbind = FALSE;
2N/A }
2N/A
2N/A /*
2N/A * Now we try version 4 and then 3.
2N/A * We also send the remote system the address we used to
2N/A * contact it in case it can help to connect back with us
2N/A */
2N/A parms.r_prog = program;
2N/A parms.r_vers = version;
2N/A parms.r_owner = (char *)&nullstring[0]; /* not needed; */
2N/A /* just for xdring */
2N/A parms.r_netid = nconf->nc_netid; /* not really needed */
2N/A
2N/A /*
2N/A * If a COTS transport is being used, try getting address via CLTS
2N/A * transport. This works only with version 4.
2N/A */
2N/A if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
2N/A nconf->nc_semantics == NC_TPI_COTS) {
2N/A void *handle;
2N/A struct netconfig *nconf_clts;
2N/A rpcb_entry_list_ptr relp = NULL;
2N/A
2N/A if (client == NULL) {
2N/A /* This did not go through the above PORTMAP/TCP code */
2N/A if ((handle = __rpc_setconf("datagram_v")) != NULL) {
2N/A while ((nconf_clts = __rpc_getconf(handle))
2N/A != NULL) {
2N/A if (strcmp(nconf_clts->nc_protofmly,
2N/A nconf->nc_protofmly) != 0) {
2N/A continue;
2N/A }
2N/A client = _getclnthandle_timed(host,
2N/A nconf_clts, &parms.r_addr,
2N/A tp);
2N/A break;
2N/A }
2N/A __rpc_endconf(handle);
2N/A }
2N/A if (client == NULL)
2N/A goto regular_rpcbind; /* Go the regular way */
2N/A } else {
2N/A /* This is a UDP PORTMAP handle. Change to version 4 */
2N/A vers = RPCBVERS4;
2N/A CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
2N/A }
2N/A /*
2N/A * We also send the remote system the address we used to
2N/A * contact it in case it can help it connect back with us
2N/A */
2N/A if (parms.r_addr == NULL) {
2N/A parms.r_addr = strdup(""); /* for XDRing */
2N/A if (parms.r_addr == NULL) {
2N/A syslog(LOG_ERR, "__rpcb_findaddr_timed: "
2N/A "strdup failed.");
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A address = NULL;
2N/A goto error;
2N/A }
2N/A }
2N/A
2N/A CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
2N/A
2N/A clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
2N/A (xdrproc_t)xdr_rpcb, (char *)&parms,
2N/A (xdrproc_t)xdr_rpcb_entry_list_ptr,
2N/A (char *)&relp, *tp);
2N/A if (clnt_st == RPC_SUCCESS) {
2N/A if (address = got_entry(relp, nconf)) {
2N/A xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
2N/A (char *)&relp);
2N/A goto done;
2N/A }
2N/A /* Entry not found for this transport */
2N/A xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
2N/A (char *)&relp);
2N/A /*
2N/A * XXX: should have perhaps returned with error but
2N/A * since the remote machine might not always be able
2N/A * to send the address on all transports, we try the
2N/A * regular way with regular_rpcbind
2N/A */
2N/A goto regular_rpcbind;
2N/A } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
2N/A (clnt_st == RPC_PROGUNAVAIL)) {
2N/A start_vers = RPCBVERS; /* Try version 3 now */
2N/A goto regular_rpcbind; /* Try different versions */
2N/A } else {
2N/A rpc_createerr.cf_stat = RPC_PMAPFAILURE;
2N/A clnt_geterr(client, &rpc_createerr.cf_error);
2N/A goto error;
2N/A }
2N/A }
2N/A
2N/Aregular_rpcbind:
2N/A
2N/A /* Now the same transport is to be used to get the address */
2N/A if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
2N/A (nconf->nc_semantics == NC_TPI_COTS))) {
2N/A /* A CLTS type of client - destroy it */
2N/A CLNT_DESTROY(client);
2N/A client = NULL;
2N/A free(parms.r_addr);
2N/A parms.r_addr = NULL;
2N/A }
2N/A
2N/A if (client == NULL) {
2N/A client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
2N/A if (client == NULL) {
2N/A address = NULL;
2N/A goto error;
2N/A }
2N/A }
2N/A if (parms.r_addr == NULL) {
2N/A parms.r_addr = strdup(""); /* for XDRing */
2N/A if (parms.r_addr == NULL) {
2N/A syslog(LOG_ERR, "__rpcb_findaddr_timed: "
2N/A "strdup failed.");
2N/A address = NULL;
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A goto error;
2N/A }
2N/A }
2N/A
2N/A /* First try from start_vers and then version 3 (RPCBVERS) */
2N/A
2N/A CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
2N/A for (vers = start_vers; vers >= RPCBVERS; vers--) {
2N/A /* Set the version */
2N/A CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
2N/A clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
2N/A (xdrproc_t)xdr_rpcb, (char *)&parms,
2N/A (xdrproc_t)xdr_wrapstring,
2N/A (char *)&ua, *tp);
2N/A if (clnt_st == RPC_SUCCESS) {
2N/A if ((ua == NULL) || (ua[0] == NULL)) {
2N/A if (ua != NULL)
2N/A xdr_free(xdr_wrapstring, (char *)&ua);
2N/A
2N/A /* address unknown */
2N/A rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
2N/A goto error;
2N/A }
2N/A address = uaddr2taddr(nconf, ua);
2N/A#ifdef ND_DEBUG
2N/A fprintf(stderr, "\tRemote address is [%s]\n", ua);
2N/A if (!address)
2N/A fprintf(stderr,
2N/A "\tCouldn't resolve remote address!\n");
2N/A#endif
2N/A xdr_free((xdrproc_t)xdr_wrapstring, (char *)&ua);
2N/A
2N/A if (!address) {
2N/A /* We don't know about your universal address */
2N/A rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2N/A goto error;
2N/A }
2N/A goto done;
2N/A }
2N/A if (clnt_st == RPC_PROGVERSMISMATCH) {
2N/A struct rpc_err rpcerr;
2N/A
2N/A clnt_geterr(client, &rpcerr);
2N/A if (rpcerr.re_vers.low > RPCBVERS4)
2N/A goto error; /* a new version, can't handle */
2N/A } else if (clnt_st != RPC_PROGUNAVAIL) {
2N/A /* Cant handle this error */
2N/A goto error;
2N/A }
2N/A }
2N/A
2N/A if ((address == NULL) || (address->len == 0)) {
2N/A rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
2N/A clnt_geterr(client, &rpc_createerr.cf_error);
2N/A }
2N/A
2N/Aerror:
2N/A if (client) {
2N/A CLNT_DESTROY(client);
2N/A client = NULL;
2N/A }
2N/Adone:
2N/A if (nconf->nc_semantics != NC_TPI_CLTS) {
2N/A /* This client is the connectionless one */
2N/A if (client) {
2N/A CLNT_DESTROY(client);
2N/A client = NULL;
2N/A }
2N/A }
2N/A if (clpp) {
2N/A *clpp = client;
2N/A } else if (client) {
2N/A CLNT_DESTROY(client);
2N/A }
2N/A if (parms.r_addr)
2N/A free(parms.r_addr);
2N/A return (address);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Find the mapped address for program, version.
2N/A * Calls the rpcbind service remotely to do the lookup.
2N/A * Uses the transport specified in nconf.
2N/A * Returns FALSE (0) if no map exists, else returns 1.
2N/A *
2N/A * Assuming that the address is all properly allocated
2N/A */
2N/Aint
2N/Arpcb_getaddr(const rpcprog_t program, const rpcvers_t version,
2N/A const struct netconfig *nconf, struct netbuf *address, const char *host)
2N/A{
2N/A struct netbuf *na;
2N/A
2N/A if ((na = __rpcb_findaddr_timed(program, version,
2N/A (struct netconfig *)nconf, (char *)host, NULL, NULL)) == NULL)
2N/A return (FALSE);
2N/A
2N/A if (na->len > address->maxlen) {
2N/A /* Too long address */
2N/A netdir_free((char *)na, ND_ADDR);
2N/A rpc_createerr.cf_stat = RPC_FAILED;
2N/A return (FALSE);
2N/A }
2N/A (void) memcpy(address->buf, na->buf, (int)na->len);
2N/A address->len = na->len;
2N/A netdir_free((char *)na, ND_ADDR);
2N/A return (TRUE);
2N/A}
2N/A
2N/A/*
2N/A * Get a copy of the current maps.
2N/A * Calls the rpcbind service remotely to get the maps.
2N/A *
2N/A * It returns only a list of the services
2N/A * It returns NULL on failure.
2N/A */
2N/Arpcblist *
2N/Arpcb_getmaps(const struct netconfig *nconf, const char *host)
2N/A{
2N/A rpcblist_ptr head = NULL;
2N/A CLIENT *client;
2N/A enum clnt_stat clnt_st;
2N/A int vers = 0;
2N/A
2N/A client = getclnthandle((char *)host,
2N/A (struct netconfig *)nconf, NULL);
2N/A if (client == NULL)
2N/A return (NULL);
2N/A
2N/A clnt_st = CLNT_CALL(client, RPCBPROC_DUMP,
2N/A (xdrproc_t)xdr_void, NULL,
2N/A (xdrproc_t)xdr_rpcblist_ptr,
2N/A (char *)&head, tottimeout);
2N/A if (clnt_st == RPC_SUCCESS)
2N/A goto done;
2N/A
2N/A if ((clnt_st != RPC_PROGVERSMISMATCH) &&
2N/A (clnt_st != RPC_PROGUNAVAIL)) {
2N/A rpc_createerr.cf_stat = RPC_RPCBFAILURE;
2N/A clnt_geterr(client, &rpc_createerr.cf_error);
2N/A goto done;
2N/A }
2N/A
2N/A /* fall back to earlier version */
2N/A CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
2N/A if (vers == RPCBVERS4) {
2N/A vers = RPCBVERS;
2N/A CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
2N/A if (CLNT_CALL(client, RPCBPROC_DUMP,
2N/A (xdrproc_t)xdr_void,
2N/A NULL, (xdrproc_t)xdr_rpcblist_ptr,
2N/A (char *)&head, tottimeout) == RPC_SUCCESS)
2N/A goto done;
2N/A }
2N/A rpc_createerr.cf_stat = RPC_RPCBFAILURE;
2N/A clnt_geterr(client, &rpc_createerr.cf_error);
2N/A
2N/Adone:
2N/A CLNT_DESTROY(client);
2N/A return (head);
2N/A}
2N/A
2N/A/*
2N/A * rpcbinder remote-call-service interface.
2N/A * This routine is used to call the rpcbind remote call service
2N/A * which will look up a service program in the address maps, and then
2N/A * remotely call that routine with the given parameters. This allows
2N/A * programs to do a lookup and call in one step.
2N/A */
2N/Aenum clnt_stat
2N/Arpcb_rmtcall(const struct netconfig *nconf, const char *host,
2N/A const rpcprog_t prog, const rpcvers_t vers, const rpcproc_t proc,
2N/A const xdrproc_t xdrargs, const caddr_t argsp, const xdrproc_t xdrres,
2N/A const caddr_t resp, const struct timeval tout, struct netbuf *addr_ptr)
2N/A{
2N/A CLIENT *client;
2N/A enum clnt_stat stat;
2N/A struct r_rpcb_rmtcallargs a;
2N/A struct r_rpcb_rmtcallres r;
2N/A int rpcb_vers;
2N/A
2N/A client = getclnthandle((char *)host, (struct netconfig *)nconf, NULL);
2N/A if (client == NULL)
2N/A return (RPC_FAILED);
2N/A CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rmttimeout);
2N/A a.prog = prog;
2N/A a.vers = vers;
2N/A a.proc = proc;
2N/A a.args.args_val = argsp;
2N/A a.xdr_args = xdrargs;
2N/A r.addr = NULL;
2N/A r.results.results_val = resp;
2N/A r.xdr_res = xdrres;
2N/A
2N/A for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
2N/A CLNT_CONTROL(client, CLSET_VERS, (char *)&rpcb_vers);
2N/A stat = CLNT_CALL(client, RPCBPROC_CALLIT,
2N/A (xdrproc_t)xdr_rpcb_rmtcallargs, (char *)&a,
2N/A (xdrproc_t)xdr_rpcb_rmtcallres, (char *)&r, tout);
2N/A if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
2N/A struct netbuf *na;
2N/A
2N/A na = uaddr2taddr((struct netconfig *)nconf, r.addr);
2N/A if (!na) {
2N/A stat = RPC_N2AXLATEFAILURE;
2N/A ((struct netbuf *)addr_ptr)->len = 0;
2N/A goto error;
2N/A }
2N/A if (na->len > addr_ptr->maxlen) {
2N/A /* Too long address */
2N/A stat = RPC_FAILED; /* XXX A better error no */
2N/A netdir_free((char *)na, ND_ADDR);
2N/A ((struct netbuf *)addr_ptr)->len = 0;
2N/A goto error;
2N/A }
2N/A (void) memcpy(addr_ptr->buf, na->buf, (int)na->len);
2N/A ((struct netbuf *)addr_ptr)->len = na->len;
2N/A netdir_free((char *)na, ND_ADDR);
2N/A break;
2N/A }
2N/A if ((stat != RPC_PROGVERSMISMATCH) &&
2N/A (stat != RPC_PROGUNAVAIL))
2N/A goto error;
2N/A }
2N/Aerror:
2N/A CLNT_DESTROY(client);
2N/A if (r.addr)
2N/A xdr_free((xdrproc_t)xdr_wrapstring, (char *)&r.addr);
2N/A return (stat);
2N/A}
2N/A
2N/A/*
2N/A * Gets the time on the remote host.
2N/A * Returns 1 if succeeds else 0.
2N/A */
2N/Abool_t
2N/Arpcb_gettime(const char *host, time_t *timep)
2N/A{
2N/A CLIENT *client = NULL;
2N/A void *handle;
2N/A struct netconfig *nconf;
2N/A int vers;
2N/A enum clnt_stat st;
2N/A
2N/A if ((host == NULL) || (host[0] == NULL)) {
2N/A (void) time(timep);
2N/A return (TRUE);
2N/A }
2N/A
2N/A if ((handle = __rpc_setconf("netpath")) == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (FALSE);
2N/A }
2N/A rpc_createerr.cf_stat = RPC_SUCCESS;
2N/A while (client == NULL) {
2N/A if ((nconf = __rpc_getconf(handle)) == NULL) {
2N/A if (rpc_createerr.cf_stat == RPC_SUCCESS)
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A break;
2N/A }
2N/A client = getclnthandle((char *)host, nconf, NULL);
2N/A if (client)
2N/A break;
2N/A }
2N/A __rpc_endconf(handle);
2N/A if (client == NULL)
2N/A return (FALSE);
2N/A
2N/A st = CLNT_CALL(client, RPCBPROC_GETTIME,
2N/A (xdrproc_t)xdr_void, NULL,
2N/A (xdrproc_t)xdr_time_t, (char *)timep, tottimeout);
2N/A
2N/A if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
2N/A CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
2N/A if (vers == RPCBVERS4) {
2N/A /* fall back to earlier version */
2N/A vers = RPCBVERS;
2N/A CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
2N/A st = CLNT_CALL(client, RPCBPROC_GETTIME,
2N/A (xdrproc_t)xdr_void, NULL,
2N/A (xdrproc_t)xdr_time_t, (char *)timep,
2N/A tottimeout);
2N/A }
2N/A }
2N/A CLNT_DESTROY(client);
2N/A return (st == RPC_SUCCESS? TRUE : FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Converts taddr to universal address. This routine should never
2N/A * really be called because local n2a libraries are always provided.
2N/A */
2N/Achar *
2N/Arpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
2N/A{
2N/A CLIENT *client;
2N/A char *uaddr = NULL;
2N/A
2N/A /* parameter checking */
2N/A if (nconf == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A if (taddr == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
2N/A return (NULL);
2N/A }
2N/A client = local_rpcb();
2N/A if (!client)
2N/A return (NULL);
2N/A
2N/A CLNT_CALL(client, RPCBPROC_TADDR2UADDR, (xdrproc_t)xdr_netbuf,
2N/A (char *)taddr, (xdrproc_t)xdr_wrapstring, (char *)&uaddr,
2N/A tottimeout);
2N/A CLNT_DESTROY(client);
2N/A return (uaddr);
2N/A}
2N/A
2N/A/*
2N/A * Converts universal address to netbuf. This routine should never
2N/A * really be called because local n2a libraries are always provided.
2N/A */
2N/Astruct netbuf *
2N/Arpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
2N/A{
2N/A CLIENT *client;
2N/A struct netbuf *taddr;
2N/A
2N/A /* parameter checking */
2N/A if (nconf == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (NULL);
2N/A }
2N/A if (uaddr == NULL) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
2N/A return (NULL);
2N/A }
2N/A client = local_rpcb();
2N/A if (!client)
2N/A return (NULL);
2N/A
2N/A taddr = calloc(1, sizeof (struct netbuf));
2N/A if (taddr == NULL) {
2N/A CLNT_DESTROY(client);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (CLNT_CALL(client, RPCBPROC_UADDR2TADDR, (xdrproc_t)xdr_wrapstring,
2N/A (char *)&uaddr, (xdrproc_t)xdr_netbuf, (char *)taddr,
2N/A tottimeout) != RPC_SUCCESS) {
2N/A free(taddr);
2N/A taddr = NULL;
2N/A }
2N/A CLNT_DESTROY(client);
2N/A return (taddr);
2N/A}