rpcb_clnt.c revision 6935f61b0d202f1b87f0234824e4a6ab88c492ac
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Copyright 2014 Gary Mills
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley
* 4.3 BSD under license from the Regents of the University of
* California.
*/
/*
* interface to rpcbind rpc service.
*/
#include "mt.h"
#include "rpc_mt.h"
#include <assert.h>
#include <rpc/rpcb_prot.h>
#include <netconfig.h>
#include <netdir.h>
#include <netdb.h>
#include <syslog.h>
#ifdef PORTMAP
#include <rpc/pmap_prot.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static const char nullstring[] = "\000";
const struct timeval *);
struct timeval *);
/*
* The life time of a cached entry should not exceed 5 minutes
* since automountd attempts an unmount every 5 minutes.
* It is arbitrarily set a little lower (3 min = 180 sec)
* to reduce the time during which an entry is stale.
*/
#define CACHE_TTL 180
#define CACHESIZE 6
struct address_cache {
char *ac_host;
char *ac_netid;
char *ac_uaddr;
struct address_cache *ac_next;
};
static struct address_cache *front;
static int cachesize;
extern int lowvers;
extern int authdes_cachesz;
/*
* This routine adjusts the timeout used for calls to the remote rpcbind.
* Also, this routine can be used to set the use of portmapper version 2
* only when doing rpc_broadcasts
* These are private routines that may not be provided in future releases.
*/
{
switch (request) {
case CLCR_GET_RPCB_TIMEOUT:
break;
case CLCR_SET_RPCB_TIMEOUT:
break;
case CLCR_GET_LOWVERS:
break;
case CLCR_SET_LOWVERS:
break;
case CLCR_GET_RPCB_RMTTIME:
break;
case CLCR_SET_RPCB_RMTTIME:
break;
case CLCR_GET_CRED_CACHE_SZ:
*(int *)info = authdes_cachesz;
break;
case CLCR_SET_CRED_CACHE_SZ:
authdes_cachesz = *(int *)info;
break;
default:
return (FALSE);
}
return (TRUE);
}
/*
* However because getclnthandle(), the only user of the cache functions,
* may do a delete_cache() operation if a check_cache() fails to return an
* address useful to clnt_tli_create(), we may as well use a mutex.
*/
/*
* block all clnt_create's if we are trying to connect to a host that's down,
* since the lock will be held all during that time.
*/
extern rwlock_t rpcbaddr_cache_lock;
/*
* The routines check_cache(), add_cache(), delete_cache() manage the
* cache of rpcbind addresses for (host, netid).
*/
static struct address_cache *
{
struct address_cache *cptr;
/* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
return (cptr);
}
}
return (NULL);
}
static void
{
/* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
if (prevptr)
else
cachesize--;
break;
}
}
}
static void
{
if (!ad_cache) {
goto memerr;
}
goto memerr1;
}
goto memerr1;
}
/* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
(void) rw_wrlock(&rpcbaddr_cache_lock);
cachesize++;
} else {
/* Free the last entry */
}
if (prevptr) {
} else {
}
}
(void) rw_unlock(&rpcbaddr_cache_lock);
return;
}
/*
* This routine will return a client handle that is connected to the
* rpcbind. Returns NULL on error and free's everything.
*/
static CLIENT *
{
}
/*
* Same as getclnthandle() except it takes an extra timeout argument.
* This is for bug 4049792: clnt_create_timed does not timeout.
*
* If tp is NULL, use default timeout to get a client handle.
*/
static CLIENT *
{
struct netbuf addr_to_delete;
struct nd_addrlist *nas;
struct nd_hostserv rpcbind_hs;
struct address_cache *ad_cache;
char *tmpaddr;
int neterr;
int j;
/* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
/* Get the address of the rpcbind. Check cache first */
addr_to_delete.len = 0;
(void) rw_rdlock(&rpcbaddr_cache_lock);
if (targaddr) {
/*
* case where a client handle is created
* without a targaddr and the handle is
* requested with a targaddr
*/
"_getclnthandle_timed: strdup "
"failed.");
(void) rw_unlock(
return (NULL);
}
} else {
}
}
(void) rw_unlock(&rpcbaddr_cache_lock);
return (client);
}
(void) rw_unlock(&rpcbaddr_cache_lock);
return (NULL);
}
addr_to_delete.len = 0;
} else {
}
}
(void) rw_unlock(&rpcbaddr_cache_lock);
if (addr_to_delete.len != 0) {
/*
* Assume this may be due to cache data being
* outdated
*/
(void) rw_wrlock(&rpcbaddr_cache_lock);
(void) rw_unlock(&rpcbaddr_cache_lock);
}
else
return (NULL);
}
/* XXX nas should perhaps be cached for better performance */
if (client)
break;
}
if (client) {
if (targaddr) {
}
}
return (client);
}
/*
* This routine will return a client handle that is connected to the local
* rpcbind. Returns NULL on error.
*/
static CLIENT *
local_rpcb(void)
{
extern mutex_t loopnconf_lock;
/* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
(void) mutex_lock(&loopnconf_lock);
void *nc_handle;
nc_handle = setnetconfig();
/* fails to open netconfig file */
(void) mutex_unlock(&loopnconf_lock);
return (NULL);
}
break;
}
}
(void) mutex_unlock(&loopnconf_lock);
return (NULL);
}
/* loopnconf is never freed */
(void) endnetconfig(nc_handle);
}
(void) mutex_unlock(&loopnconf_lock);
}
/*
* Set a mapping between program, version and address.
* Calls the rpcbind service to do the mapping.
*/
{
char uidbuf[32];
/* parameter checking */
return (FALSE);
}
return (FALSE);
}
client = local_rpcb();
if (!client)
return (FALSE);
return (FALSE); /* no universal address */
}
/*
* Though uid is not being used directly, we still send it for
* completeness. For non-unix platforms, perhaps some other
* string or an empty string can be sent.
*/
return (rslt);
}
/*
* Remove the mapping between program, version and netbuf address.
* Calls the rpcbind service to do the un-mapping.
* If netbuf is NULL, unset for all the transports, otherwise unset
* only for the given transport.
*/
{
char uidbuf[32];
client = local_rpcb();
if (!client)
return (FALSE);
if (nconf)
else
return (rslt);
}
/*
* From the merged list, find the appropriate entry
*/
static struct netbuf *
{
break;
}
}
return (na);
}
/*
* Quick check to see if rpcbind is up. Tries to connect over
* local transport.
*/
__rpcbind_is_up(void)
{
int fd;
return (TRUE);
return (TRUE);
}
/* LINTED pointer cast */
return (TRUE);
}
return (FALSE);
}
if (addr)
return (FALSE);
}
else
return (res);
}
/*
* An internal function which optimizes rpcb_getaddr function. It returns
* the universal address of the remote service or NULL. It also optionally
* returns the client handle that it uses to contact the remote rpcbind.
* The caller will re-purpose the client handle to contact the remote service.
*
* The algorithm used: First try version 4. Then try version 3 (svr4).
* Finally, if the transport is TCP or UDP, try version 2 (portmap).
* Version 4 is now available with all current systems on the network.
* With this algorithm, we get performance as well as a plan for
* obsoleting version 2.
*
* XXX: Due to some problems with t_connect(), we do not reuse the same client
* handle for COTS cases and hence in these cases we do not return the
* client handle. This code will change if t_connect() ever
* starts working properly. Also look under clnt_vc.c.
*/
struct netbuf *
{
void *handle;
/* parameter checking */
/*
* Setting rpc_createerr.cf_stat is sufficient.
* No details in rpc_createerr.cf_error needed.
*/
return (NULL);
}
/*
* Use default total timeout if no timeout is specified.
*/
tp = &tottimeout;
/*
* Check if rpcbind is up. This prevents needless delays when
* accessing applications such as the keyserver while booting
* disklessly.
*/
if (!__rpcbind_is_up()) {
goto error;
}
}
/*
* First try version 4.
*/
/* just for xdring */
/*
* If a COTS transport is being used, try getting address via CLTS
* transport. This works only with version 4.
*/
tmp_client = TRUE;
struct netconfig *nconf_clts;
nconf->nc_protofmly) != 0) {
continue;
}
/*
* Sets rpc_createerr.cf_error members
* on failure
*/
break;
}
}
} else {
/* Sets rpc_createerr.cf_error members on failure */
}
/* Set rpcbind version 4 */
/*
* We also send the remote system the address we used to
* contact it in case it can help it connect back with us
*/
"strdup failed.");
/* Construct a system error */
goto error;
}
}
(char *)&rpcbrmttime);
/* Sets error structure members in client handle */
switch (clnt_st) {
case RPC_SUCCESS: /* Call succeeded */
(char *)&relp);
/* Program number and version number matched */
goto done;
}
/* Program and version not found for this transport */
/*
* XXX: should have returned with RPC_PROGUNAVAIL
* or perhaps RPC_PROGNOTREGISTERED error but
* since the remote machine might not always be able
* to send the address on all transports, we try the
* regular way with version 3, then 2
*/
/* Try the next version */
break;
case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
goto error; /* a new version, can't handle */
}
/* Try the next version */
break;
case RPC_PROCUNAVAIL: /* Procedure unavailable */
case RPC_PROGUNAVAIL: /* Program not available */
case RPC_TIMEDOUT: /* Call timed out */
/* Try the next version */
break;
default:
goto error;
break;
}
} else {
/* No client */
tmp_client = FALSE;
} /* End of version 4 */
/* Destroy a temporary client */
}
tmp_client = FALSE;
/*
* Try version 3
*/
/* Now the same transport is to be used to get the address */
/* Sets rpc_createerr.cf_error members on failure */
}
"strdup failed.");
/* Construct a system error */
goto error;
}
}
(char *)&rpcbrmttime);
/* Sets error structure members in client handle */
switch (clnt_st) {
case RPC_SUCCESS: /* Call succeeded */
if (ua[0] != '\0') {
}
(char *)&ua);
goto done;
}
/* NULL universal address */
/* But client call was successful */
goto error;
}
#ifndef PORTMAP
goto error;
#endif
/* Try the next version */
break;
case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
#ifdef PORTMAP
goto error; /* a new version, can't handle */
}
#else
goto error;
#endif
/* Try the next version */
break;
#ifdef PORTMAP
case RPC_PROCUNAVAIL: /* Procedure unavailable */
case RPC_PROGUNAVAIL: /* Program not available */
case RPC_TIMEDOUT: /* Call timed out */
/* Try the next version */
break;
#endif
default:
goto error;
break;
}
} /* End of version 3 */
#ifndef PORTMAP
/* cf_error members set by creation failure */
#endif
/*
* Try version 2
*/
#ifdef PORTMAP
/* Try version 2 for TCP or UDP */
/*
* Try UDP only - there are some portmappers out
* there that use UDP only.
*/
}
/* Construct an unknown protocol error */
goto error;
}
/*
* The following to reinforce that you can
* only request for remote address through
* the same transport you are requesting.
* ie. requesting unversial address
* of IPv4 has to be carried through IPv4.
* Can't use IPv6 to send out the request.
* The mergeaddr in rpcbind can't handle
* this.
*/
for (;;) {
== NULL) {
/*
* Construct an unknown protocol
* error
*/
goto error;
}
/*
* here check the protocol family to
* be consistent with the request one
*/
nconf->nc_protofmly) == 0)
break;
}
/* Sets rpc_createerr.cf_error members on failure */
tmp_client = TRUE;
}
/*
* rpc_createerr. cf_error members were set by
* creation failure
*/
tmp_client = FALSE;
goto error;
}
/*
* Set version and retry timeout.
*/
/* Sets error structure members in client handle */
if (clnt_st != RPC_SUCCESS) {
goto error;
} else if (port == 0) {
/* Will be NULL universal address */
/* But client call was successful */
goto error;
}
/* Construct a system error */
goto error;
}
sizeof (short));
goto done;
} else {
/*
* This is not NC_INET.
* Always an error for version 2.
*/
/* There is a client that failed */
} else {
/* Something else */
/*
* Setting rpc_createerr.cf_stat is sufficient.
* No details in rpc_createerr.cf_error needed.
*/
}
}
#endif
/* Return NULL address and NULL client */
}
done:
/* Return an address and optional client */
/* This client is the temporary one */
}
}
return (address);
}
/*
* Find the mapped address for program, version.
* Calls the rpcbind service remotely to do the lookup.
* Uses the transport specified in nconf.
* Returns FALSE (0) if no map exists, else returns 1.
*
* Assuming that the address is all properly allocated
*/
int
{
return (FALSE);
/* Too long address */
return (FALSE);
}
return (TRUE);
}
/*
* Get a copy of the current maps.
* Calls the rpcbind service remotely to get the maps.
*
* It returns only a list of the services
* It returns NULL on failure.
*/
rpcblist *
{
int vers = 0;
return (NULL);
(char *)&head, tottimeout);
if (clnt_st == RPC_SUCCESS)
goto done;
if ((clnt_st != RPC_PROGVERSMISMATCH) &&
(clnt_st != RPC_PROGUNAVAIL)) {
goto done;
}
/* fall back to earlier version */
goto done;
}
done:
return (head);
}
/*
* rpcbinder remote-call-service interface.
* This routine is used to call the rpcbind remote call service
* which will look up a service program in the address maps, and then
* remotely call that routine with the given parameters. This allows
* programs to do a lookup and call in one step.
*/
enum clnt_stat
{
struct r_rpcb_rmtcallargs a;
struct r_rpcb_rmtcallres r;
int rpcb_vers;
return (RPC_FAILED);
(xdrproc_t)xdr_rpcb_rmtcallargs, (char *)&a,
if (!na) {
goto error;
}
/* Too long address */
goto error;
}
break;
}
if ((stat != RPC_PROGVERSMISMATCH) &&
(stat != RPC_PROGUNAVAIL))
goto error;
}
if (r.addr)
return (stat);
}
/*
* Gets the time on the remote host.
* Returns 1 if succeeds else 0.
*/
{
void *handle;
int vers;
return (TRUE);
}
return (FALSE);
}
break;
}
if (client)
break;
}
return (FALSE);
/* fall back to earlier version */
}
}
}
/*
* Converts taddr to universal address. This routine should never
* really be called because local n2a libraries are always provided.
*/
char *
{
/* parameter checking */
return (NULL);
}
return (NULL);
}
client = local_rpcb();
if (!client)
return (NULL);
return (uaddr);
}
/*
* Converts universal address to netbuf. This routine should never
* really be called because local n2a libraries are always provided.
*/
struct netbuf *
{
/* parameter checking */
return (NULL);
}
return (NULL);
}
client = local_rpcb();
if (!client)
return (NULL);
return (NULL);
}
tottimeout) != RPC_SUCCESS) {
}
return (taddr);
}