/*-
* Copyright (c) 2009, Sun Microsystems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Sun Microsystems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
*/
/* Global variables */
int debugging = DEBUGGING; /* Tell me what's going on */
int doabort = 0; /* When debugging, do an abort on errors */
rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
/* who to suid to if -s is given */
#define RUN_AS "daemon"
#define RPCBINDDLOCK "/var/run/rpcbind.lock"
static int runasdaemon = 0;
int insecure = 0;
int oldstyle_local = 0;
#ifdef LIBWRAP
int libwrap = 0;
#endif
int verboselog = 0;
static char **hosts = NULL;
static struct sockaddr **bound_sa;
static int ipv6_only = 0;
static int nhosts = 0;
static int on = 1;
#ifndef RPCBIND_RUMP
static int rpcbindlockfd;
#endif
#ifdef WARMSTART
/* Local Variable */
static int warmstart = 0; /* Grab an old copy of registrations */
#endif
#ifdef PORTMAP
struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
const char *udptrans; /* Name of UDP transport */
const char *tcptrans; /* Name of TCP transport */
const char *udp_uaddr; /* Universal UDP address */
const char *tcp_uaddr; /* Universal TCP address */
#endif
static const char servname[] = "sunrpc";
if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
err(EXIT_FAILURE, "getrlimit(RLIMIT_NOFILE)");
if (rl.rlim_cur < 128) {
if (rl.rlim_max <= 128)
rl.rlim_cur = rl.rlim_max;
else
rl.rlim_cur = 128;
if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
err(EXIT_FAILURE, "setrlimit(RLIMIT_NOFILE)");
}
update_bound_sa();
#ifndef RPCBIND_RUMP
/* Check that another rpcbind isn't already running. */
if ((rpcbindlockfd = open(RPCBINDDLOCK, O_RDONLY|O_CREAT, 0444)) == -1)
err(EXIT_FAILURE, "%s", RPCBINDDLOCK);
if (flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
errx(EXIT_FAILURE,
"another rpcbind is already running. Aborting");
if (geteuid()) /* This command allowed only to root */
errx(EXIT_FAILURE, "Sorry. You are not superuser\n");
#endif
nc_handle = setnetconfig(); /* open netconfig file */
if (nc_handle == NULL)
errx(EXIT_FAILURE, "could not read /etc/netconfig");
/*
* Adds the entry into the rpcbind database.
* If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
* Returns 0 if succeeds, else fails
*/
static int
init_transport(struct netconfig *nconf)
{
int fd;
struct t_bind taddr;
struct addrinfo hints, *res = NULL;
struct __rpc_sockinfo si;
SVCXPRT *my_xprt;
int status; /* bound checking ? */
int aicode;
int addrlen;
int nhostsbak;
int bound;
u_int32_t host_addr[4]; /* IPv4 or IPv6 */
struct sockaddr *sa;
struct sockaddr_un sun;
#ifndef RPCBIND_RUMP
mode_t oldmask;
#endif
if ((nconf->nc_semantics != NC_TPI_CLTS) &&
(nconf->nc_semantics != NC_TPI_COTS) &&
(nconf->nc_semantics != NC_TPI_COTS_ORD))
return 1; /* not my type */
#ifdef RPCBIND_DEBUG
if (debugging) {
unsigned int i;
char **s;
(void)fprintf(stderr, "%s: %ld lookup routines :\n",
nconf->nc_netid, nconf->nc_nlookups);
for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
i++, s++)
(void)fprintf(stderr, "[%u] - %s\n", i, *s);
}
#endif
/*
* XXX - using RPC library internal functions.
*/
if (strcmp(nconf->nc_netid, "local") == 0) {
/*
* For other transports we call this later, for each socket we
* like to bind.
*/
if ((fd = __rpc_nconf2fd(nconf)) < 0) {
int non_fatal = 0;
if (errno == EAFNOSUPPORT)
non_fatal = 1;
syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
"Cannot create socket for `%s'", nconf->nc_netid);
return 1;
}
} else
fd = -1;
if (!__rpc_nconf2sockinfo(nconf, &si)) {
syslog(LOG_ERR, "Cannot get information for `%s'",
nconf->nc_netid);
return 1;
}
if (strcmp(nconf->nc_netid, "local") == 0) {
(void)memset(&sun, 0, sizeof sun);
sun.sun_family = AF_LOCAL;
#ifdef RPCBIND_RUMP
(void)rump_sys_unlink(_PATH_RPCBINDSOCK);
#else
(void)unlink(_PATH_RPCBINDSOCK);
#endif
(void)strlcpy(sun.sun_path, _PATH_RPCBINDSOCK,
sizeof(sun.sun_path));
sun.sun_len = SUN_LEN(&sun);
addrlen = sizeof(struct sockaddr_un);
sa = (struct sockaddr *)&sun;
} else {
/* Get rpcbind's address on this transport */
/* Also add version 2 stuff to rpcbind list */
rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
}
#endif
/* version 3 registration */
if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
syslog(LOG_ERR, "Could not register %s version 3",
nconf->nc_netid);
goto error;
}
rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
/* version 4 registration */
if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
syslog(LOG_ERR, "Could not register %s version 4",
nconf->nc_netid);
goto error;
}
rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
/* decide if bound checking works for this transport */
status = add_bndlist(nconf, &taddr.addr);
#ifdef RPCBIND_DEBUG
if (debugging) {
if (status < 0) {
fprintf(stderr, "Error in finding bind status for %s\n",
nconf->nc_netid);
} else if (status == 0) {
fprintf(stderr, "check binding for %s\n",
nconf->nc_netid);
} else if (status > 0) {
fprintf(stderr, "No check binding for %s\n",
nconf->nc_netid);
}
}
#else
__USE(status);
#endif
/*
* rmtcall only supported on CLTS transports for now.
*/
if (nconf->nc_semantics == NC_TPI_CLTS) {
status = create_rmtcall_fd(nconf);
#ifdef RPCBIND_DEBUG
if (debugging) {
if (status < 0) {
fprintf(stderr,
"Could not create rmtcall fd for %s\n",
nconf->nc_netid);
} else {
fprintf(stderr, "rmtcall fd for %s is %d\n",
nconf->nc_netid, status);
}
}
#endif
}
return (0);
error:
#ifdef RPCBIND_RUMP
(void)rump_sys_close(fd);
#else
(void)close(fd);
#endif
return (1);
}
/*
* Create the list of addresses that we're bound to. Normally, this
* list is empty because we're listening on the wildcard address
* (nhost == 0). If -h is specified on the command line, then
* bound_sa will have a list of the addresses that the program binds
* to specifically. This function takes that list and converts them to
* struct sockaddr * and stores them in bound_sa.
*/
static void
update_bound_sa(void)
{
struct addrinfo hints, *res = NULL;
int i;
if (nhosts == 0)
return;
bound_sa = calloc(nhosts, sizeof(*bound_sa));
if (bound_sa == NULL)
err(EXIT_FAILURE, "no space for bound address array");
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
for (i = 0; i < nhosts; i++) {
if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
continue;
bound_sa[i] = malloc(res->ai_addrlen);
if (bound_sa[i] == NULL)
err(EXIT_FAILURE, "no space for bound address");
memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
}
}
/*
* Match the sa against the list of addresses we've bound to. If
* we've not specifically bound to anything, we match everything.
* Otherwise, if the IPv4 or IPv6 address matches one of the addresses
* in bound_sa, we return true. If not, we return false.
*/
int
listen_addr(const struct sockaddr *sa)
{
int i;
/*
* If nhosts == 0, then there were no -h options on the
* command line, so all addresses are addresses we're
* listening to.
*/
if (nhosts == 0)
return 1;
for (i = 0; i < nhosts; i++) {
if (bound_sa[i] == NULL ||
sa->sa_family != bound_sa[i]->sa_family)
continue;
switch (sa->sa_family) {
case AF_INET:
if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
sizeof(struct in_addr)) == 0)
return (1);
break;
#ifdef INET6
case AF_INET6:
if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
sizeof(struct in6_addr)) == 0)
return (1);
break;
#endif
default:
break;
}
}
return (0);
}