#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <rpc/rpc.h>
#include <rpc/rpc_com.h>
#ifdef PORTMAP
#include <netinet/in.h>
#endif
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <netconfig.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <err.h>
#include <libutil.h>
#include <pwd.h>
#include <string.h>
#include "rpcbind.h"
int debugging = 0;
int doabort = 0;
rpcblist_ptr list_rbl;
#define RUN_AS "daemon"
#define RPCBINDDLOCK "/var/run/rpcbind.lock"
int runasdaemon = 0;
int insecure = 0;
int oldstyle_local = 0;
int verboselog = 0;
char **hosts = NULL;
int ipv6_only = 0;
int nhosts = 0;
int on = 1;
int rpcbindlockfd;
#ifdef WARMSTART
static int warmstart = 0;
#endif
#ifdef PORTMAP
struct pmaplist *list_pml;
char *udptrans;
char *tcptrans;
char *udp_uaddr;
char *tcp_uaddr;
#endif
static char servname[] = "rpcbind";
static char superuser[] = "superuser";
static int init_transport(struct netconfig *);
static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
struct netbuf *);
static void terminate(int);
static void parseargs(int, char *[]);
int
main(int argc, char *argv[])
{
struct netconfig *nconf;
void *nc_handle;
struct rlimit rl;
int maxrec = RPC_MAXDATASIZE;
parseargs(argc, argv);
if ((rpcbindlockfd = (open(RPCBINDDLOCK,
O_RDONLY|O_CREAT, 0444))) == -1)
err(1, "%s", RPCBINDDLOCK);
if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
errx(1, "another rpcbind is already running. Aborting");
getrlimit(RLIMIT_NOFILE, &rl);
if (rl.rlim_cur < 128) {
if (rl.rlim_max <= 128)
rl.rlim_cur = rl.rlim_max;
else
rl.rlim_cur = 128;
setrlimit(RLIMIT_NOFILE, &rl);
}
openlog("rpcbind", LOG_CONS, LOG_DAEMON);
if (geteuid()) {
fprintf(stderr, "Sorry. You are not superuser\n");
exit(1);
}
nc_handle = setnetconfig();
if (nc_handle == NULL) {
syslog(LOG_ERR, "could not read /etc/netconfig");
exit(1);
}
#ifdef PORTMAP
udptrans = "";
tcptrans = "";
#endif
nconf = getnetconfigent("local");
if (nconf == NULL)
nconf = getnetconfigent("unix");
if (nconf == NULL) {
syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
exit(1);
}
rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
init_transport(nconf);
while ((nconf = getnetconfig(nc_handle))) {
if (nconf->nc_flag & NC_VISIBLE)
if (ipv6_only != 1 || strcmp(nconf->nc_protofmly, "inet") != 0)
init_transport(nconf);
}
endnetconfig(nc_handle);
signal(SIGCHLD, reap);
signal(SIGINT, terminate);
signal(SIGTERM, terminate);
signal(SIGQUIT, terminate);
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
#ifdef WARMSTART
if (warmstart) {
read_warmstart();
}
#endif
if (debugging) {
printf("rpcbind debugging enabled.");
if (doabort) {
printf(" Will abort on errors!\n");
} else {
printf("\n");
}
} else {
if (daemon(0, 0))
err(1, "fork failed");
}
if (runasdaemon) {
struct passwd *p;
if((p = getpwnam(RUN_AS)) == NULL) {
syslog(LOG_ERR, "cannot get uid of daemon: %m");
exit(1);
}
if (setuid(p->pw_uid) == -1) {
syslog(LOG_ERR, "setuid to daemon failed: %m");
exit(1);
}
}
network_init();
my_svc_run();
syslog(LOG_ERR, "svc_run returned unexpectedly");
rpcbind_abort();
return 0;
}
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;
int aicode;
int addrlen;
int nhostsbak;
int bound;
struct sockaddr *sa;
u_int32_t host_addr[4];
struct sockaddr_un sun;
mode_t oldmask;
if ((nconf->nc_semantics != NC_TPI_CLTS) &&
(nconf->nc_semantics != NC_TPI_COTS) &&
(nconf->nc_semantics != NC_TPI_COTS_ORD))
return (1);
#ifdef ND_DEBUG
if (debugging) {
int i;
char **s;
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++)
fprintf(stderr, "[%d] - %s\n", i, *s);
}
#endif
if ((strcmp(nconf->nc_netid, "local") == 0) ||
(strcmp(nconf->nc_netid, "unix") == 0)) {
if ((fd = __rpc_nconf2fd(nconf)) < 0) {
int non_fatal = 0;
if (errno == EPROTONOSUPPORT)
non_fatal = 1;
syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
nconf->nc_netid);
return (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) ||
(strcmp(nconf->nc_netid, "unix") == 0)) {
memset(&sun, 0, sizeof sun);
sun.sun_family = AF_LOCAL;
unlink(_PATH_RPCBINDSOCK);
strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
sun.sun_len = SUN_LEN(&sun);
addrlen = sizeof (struct sockaddr_un);
sa = (struct sockaddr *)&sun;
} else {
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_PASSIVE;
hints.ai_family = si.si_af;
hints.ai_socktype = si.si_socktype;
hints.ai_protocol = si.si_proto;
}
if ((strcmp(nconf->nc_netid, "local") != 0) &&
(strcmp(nconf->nc_netid, "unix") != 0)) {
nhostsbak = nhosts;
nhostsbak++;
hosts = realloc(hosts, nhostsbak * sizeof(char *));
if (nhostsbak == 1)
hosts[0] = "*";
else {
if (hints.ai_family == AF_INET) {
hosts[nhostsbak - 1] = "127.0.0.1";
} else if (hints.ai_family == AF_INET6) {
hosts[nhostsbak - 1] = "::1";
} else
return 1;
}
bound = 0;
while (nhostsbak > 0) {
--nhostsbak;
if ((fd = __rpc_nconf2fd(nconf)) < 0) {
int non_fatal = 0;
if (errno == EPROTONOSUPPORT &&
nconf->nc_semantics != NC_TPI_CLTS)
non_fatal = 1;
syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
"cannot create socket for %s", nconf->nc_netid);
return (1);
}
switch (hints.ai_family) {
case AF_INET:
if (inet_pton(AF_INET, hosts[nhostsbak],
host_addr) == 1) {
hints.ai_flags &= AI_NUMERICHOST;
} else {
if (inet_pton(AF_INET6,
hosts[nhostsbak], host_addr) == 1) {
close(fd);
continue;
}
}
break;
case AF_INET6:
if (inet_pton(AF_INET6, hosts[nhostsbak],
host_addr) == 1) {
hints.ai_flags &= AI_NUMERICHOST;
} else {
if (inet_pton(AF_INET, hosts[nhostsbak],
host_addr) == 1) {
close(fd);
continue;
}
}
if (setsockopt(fd, IPPROTO_IPV6,
IPV6_V6ONLY, &on, sizeof on) < 0) {
syslog(LOG_ERR,
"can't set v6-only binding for "
"ipv6 socket: %m");
continue;
}
break;
default:
break;
}
if (strcmp("*", hosts[nhostsbak]) == 0)
hosts[nhostsbak] = NULL;
if ((strcmp(nconf->nc_netid, "local") != 0) &&
(strcmp(nconf->nc_netid, "unix") != 0)) {
if ((aicode = getaddrinfo(hosts[nhostsbak],
servname, &hints, &res)) != 0) {
syslog(LOG_ERR,
"cannot get local address for %s: %s",
nconf->nc_netid, gai_strerror(aicode));
continue;
}
addrlen = res->ai_addrlen;
sa = (struct sockaddr *)res->ai_addr;
}
oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
if (bind(fd, sa, addrlen) != 0) {
syslog(LOG_ERR, "cannot bind %s on %s: %m",
(hosts[nhostsbak] == NULL) ? "*" :
hosts[nhostsbak], nconf->nc_netid);
if (res != NULL)
freeaddrinfo(res);
continue;
} else
bound = 1;
umask(oldmask);
taddr.addr.len = taddr.addr.maxlen = addrlen;
taddr.addr.buf = malloc(addrlen);
if (taddr.addr.buf == NULL) {
syslog(LOG_ERR,
"cannot allocate memory for %s address",
nconf->nc_netid);
if (res != NULL)
freeaddrinfo(res);
return 1;
}
memcpy(taddr.addr.buf, sa, addrlen);
#ifdef ND_DEBUG
if (debugging) {
char *uaddr;
struct netbuf nb;
nb.buf = sa;
nb.len = nb.maxlen = sa->sa_len;
uaddr = taddr2uaddr(nconf, &nb);
fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
free(uaddr);
}
#endif
if (nconf->nc_semantics != NC_TPI_CLTS)
listen(fd, SOMAXCONN);
my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
RPC_MAXDATASIZE, RPC_MAXDATASIZE);
if (my_xprt == NULL) {
syslog(LOG_ERR, "%s: could not create service",
nconf->nc_netid);
goto error;
}
}
if (!bound)
return 1;
} else {
oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
if (bind(fd, sa, addrlen) < 0) {
syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
if (res != NULL)
freeaddrinfo(res);
return 1;
}
umask(oldmask);
taddr.addr.len = taddr.addr.maxlen = addrlen;
taddr.addr.buf = malloc(addrlen);
if (taddr.addr.buf == NULL) {
syslog(LOG_ERR, "cannot allocate memory for %s address",
nconf->nc_netid);
if (res != NULL)
freeaddrinfo(res);
return 1;
}
memcpy(taddr.addr.buf, sa, addrlen);
#ifdef ND_DEBUG
if (debugging) {
char *uaddr;
struct netbuf nb;
nb.buf = sa;
nb.len = nb.maxlen = sa->sa_len;
uaddr = taddr2uaddr(nconf, &nb);
fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
free(uaddr);
}
#endif
if (nconf->nc_semantics != NC_TPI_CLTS)
listen(fd, SOMAXCONN);
my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
RPC_MAXDATASIZE, RPC_MAXDATASIZE);
if (my_xprt == NULL) {
syslog(LOG_ERR, "%s: could not create service",
nconf->nc_netid);
goto error;
}
}
#ifdef PORTMAP
if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
(strcmp(nconf->nc_proto, NC_TCP) == 0 ||
strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
(strcmp(nconf->nc_netid, "unix") == 0) ||
(strcmp(nconf->nc_netid, "local") == 0)) {
struct pmaplist *pml;
if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
pmap_service, 0)) {
syslog(LOG_ERR, "could not register on %s",
nconf->nc_netid);
goto error;
}
pml = malloc(sizeof (struct pmaplist));
if (pml == NULL) {
syslog(LOG_ERR, "no memory!");
exit(1);
}
pml->pml_map.pm_prog = PMAPPROG;
pml->pml_map.pm_vers = PMAPVERS;
pml->pml_map.pm_port = PMAPPORT;
if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
if (tcptrans[0]) {
syslog(LOG_ERR,
"cannot have more than one TCP transport");
goto error;
}
tcptrans = strdup(nconf->nc_netid);
pml->pml_map.pm_prot = IPPROTO_TCP;
tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
} else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
if (udptrans[0]) {
syslog(LOG_ERR,
"cannot have more than one UDP transport");
goto error;
}
udptrans = strdup(nconf->nc_netid);
pml->pml_map.pm_prot = IPPROTO_UDP;
udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
} else if (strcmp(nconf->nc_netid, "local") == 0)
pml->pml_map.pm_prot = IPPROTO_ST;
else if (strcmp(nconf->nc_netid, "unix") == 0)
pml->pml_map.pm_prot = IPPROTO_ST;
pml->pml_next = list_pml;
list_pml = pml;
pml = malloc(sizeof (struct pmaplist));
if (pml == NULL) {
syslog(LOG_ERR, "no memory!");
exit(1);
}
pml->pml_map = list_pml->pml_map;
pml->pml_map.pm_vers = RPCBVERS;
pml->pml_next = list_pml;
list_pml = pml;
pml = malloc (sizeof (struct pmaplist));
if (pml == NULL) {
syslog(LOG_ERR, "no memory!");
exit(1);
}
pml->pml_map = list_pml->pml_map;
pml->pml_map.pm_vers = RPCBVERS4;
pml->pml_next = list_pml;
list_pml = pml;
rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
}
#endif
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);
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);
status = add_bndlist(nconf, &taddr.addr);
#ifdef BIND_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);
}
}
#endif
if (nconf->nc_semantics == NC_TPI_CLTS) {
status = create_rmtcall_fd(nconf);
#ifdef BIND_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:
close(fd);
return (1);
}
static void
rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
struct netbuf *addr)
{
rpcblist_ptr rbl;
rbl = malloc(sizeof (rpcblist));
if (rbl == NULL) {
syslog(LOG_ERR, "no memory!");
exit(1);
}
rbl->rpcb_map.r_prog = prog;
rbl->rpcb_map.r_vers = vers;
rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
rbl->rpcb_map.r_owner = strdup(superuser);
rbl->rpcb_next = list_rbl;
list_rbl = rbl;
}
static void
terminate(int dummy __unused)
{
close(rpcbindlockfd);
#ifdef WARMSTART
syslog(LOG_ERR,
"rpcbind terminating on signal. Restart with \"rpcbind -w\"");
write_warmstart();
#endif
exit(2);
}
void
rpcbind_abort(void)
{
#ifdef WARMSTART
write_warmstart();
#endif
abort();
}
static void
parseargs(int argc, char *argv[])
{
int c;
#ifdef WARMSTART
#define WSOP "w"
#else
#define WSOP ""
#endif
while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
switch (c) {
case '6':
ipv6_only = 1;
break;
case 'a':
doabort = 1;
break;
case 'd':
debugging = 1;
break;
case 'h':
++nhosts;
hosts = realloc(hosts, nhosts * sizeof(char *));
if (hosts == NULL)
errx(1, "Out of memory");
hosts[nhosts - 1] = strdup(optarg);
if (hosts[nhosts - 1] == NULL)
errx(1, "Out of memory");
break;
case 'i':
insecure = 1;
break;
case 'L':
oldstyle_local = 1;
break;
case 'l':
verboselog = 1;
break;
case 's':
runasdaemon = 1;
break;
#ifdef WARMSTART
case 'w':
warmstart = 1;
break;
#endif
default:
fprintf(stderr,
"usage: rpcbind [-6adiLls%s] [-h bindip]\n",
WSOP);
exit (1);
}
}
if (doabort && !debugging) {
fprintf(stderr,
"-a (abort) specified without -d (debugging) -- ignored.\n");
doabort = 0;
}
#undef WSOP
}
void
reap(int dummy __unused)
{
int save_errno = errno;
while (wait3(NULL, WNOHANG, NULL) > 0)
;
errno = save_errno;
}
void
toggle_verboselog(int dummy __unused)
{
verboselog = !verboselog;
}