#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1990, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)portmap.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$FreeBSD: src/usr.sbin/portmap/portmap.c,v 1.10.2.1 2001/04/09 23:35:19 dillon Exp $";
#endif
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <rpc/pmap_prot.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <sys/resource.h>
#include "pmap_check.h"
static void reg_service __P((struct svc_req *, SVCXPRT *));
static void reap __P((int));
static void callit __P((struct svc_req *, SVCXPRT *));
static void usage __P((void));
struct pmaplist *pmaplist;
int debugging = 0;
int
main(argc, argv)
int argc;
char **argv;
{
SVCXPRT *xprt;
int sock, c;
char **hosts = NULL;
int nhosts = 0;
struct sockaddr_in addr;
int len = sizeof(struct sockaddr_in);
register struct pmaplist *pml;
while ((c = getopt(argc, argv, "dvh:")) != -1) {
switch (c) {
case 'd':
debugging = 1;
break;
case 'v':
verboselog = 1;
break;
case 'h':
++nhosts;
hosts = realloc(hosts, nhosts * sizeof(char *));
hosts[nhosts - 1] = optarg;
break;
default:
usage();
}
}
if (!debugging && daemon(0, 0))
err(1, "fork");
openlog("portmap", debugging ? LOG_PID | LOG_PERROR : LOG_PID,
LOG_DAEMON);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PMAPPORT);
++nhosts;
hosts = realloc(hosts, nhosts * sizeof(char *));
if (nhosts == 1)
hosts[0] = "0.0.0.0";
else
hosts[nhosts - 1] = "127.0.0.1";
while (nhosts > 0) {
--nhosts;
if (inet_aton(hosts[nhosts], &addr.sin_addr) < 0) {
syslog(LOG_ERR, "bad IP address: %s", hosts[nhosts]);
exit(1);
}
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
syslog(LOG_ERR, "cannot create udp socket: %m");
exit(1);
}
if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
syslog(LOG_ERR, "cannot bind udp: %m");
exit(1);
}
if ((xprt = svcudp_create(sock)) == (SVCXPRT *)NULL) {
syslog(LOG_ERR, "couldn't do udp_create");
exit(1);
}
}
pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
pml->pml_next = 0;
pml->pml_map.pm_prog = PMAPPROG;
pml->pml_map.pm_vers = PMAPVERS;
pml->pml_map.pm_prot = IPPROTO_UDP;
pml->pml_map.pm_port = PMAPPORT;
pmaplist = pml;
addr.sin_addr.s_addr = 0;
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
syslog(LOG_ERR, "cannot create tcp socket: %m");
exit(1);
}
if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
syslog(LOG_ERR, "cannot bind tcp: %m");
exit(1);
}
if ((xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE))
== (SVCXPRT *)NULL) {
syslog(LOG_ERR, "couldn't do tcp_create");
exit(1);
}
pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
pml->pml_map.pm_prog = PMAPPROG;
pml->pml_map.pm_vers = PMAPVERS;
pml->pml_map.pm_prot = IPPROTO_TCP;
pml->pml_map.pm_port = PMAPPORT;
pml->pml_next = pmaplist;
pmaplist = pml;
(void)svc_register(xprt, PMAPPROG, PMAPVERS, reg_service, FALSE);
check_startup();
(void)signal(SIGCHLD, reap);
svc_run();
syslog(LOG_ERR, "svc_run returned unexpectedly");
abort();
}
static void
usage()
{
fprintf(stderr, "usage: portmap [-dv]\n");
exit(1);
}
#ifndef lint
void
perror(what)
const char *what;
{
syslog(LOG_ERR, "%s: %m", what);
}
#endif
static struct pmaplist *
find_service(prog, vers, prot)
u_long prog, vers, prot;
{
register struct pmaplist *hit = NULL;
register struct pmaplist *pml;
for (pml = pmaplist; pml != NULL; pml = pml->pml_next) {
if ((pml->pml_map.pm_prog != prog) ||
(pml->pml_map.pm_prot != prot))
continue;
hit = pml;
if (pml->pml_map.pm_vers == vers)
break;
}
return (hit);
}
static void
reg_service(rqstp, xprt)
struct svc_req *rqstp;
SVCXPRT *xprt;
{
struct pmap reg;
struct pmaplist *pml, *prevpml, *fnd;
int ans, port;
caddr_t t;
allow_severity = LOG_INFO;
deny_severity = LOG_WARNING;
if (debugging)
(void) fprintf(stderr, "server: about to do a switch\n");
switch (rqstp->rq_proc) {
case PMAPPROC_NULL:
check_default(svc_getcaller(xprt), rqstp->rq_proc, (u_long) 0);
if (!svc_sendreply(xprt, xdr_void, (caddr_t)0) && debugging) {
abort();
}
break;
case PMAPPROC_SET:
if (!svc_getargs(xprt, xdr_pmap, (caddr_t)®))
svcerr_decode(xprt);
else {
if (!check_setunset(svc_getcaller(xprt),
rqstp->rq_proc, reg.pm_prog, reg.pm_port)) {
ans = 0;
goto done;
}
fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
if (fnd && fnd->pml_map.pm_vers == reg.pm_vers) {
if (fnd->pml_map.pm_port == reg.pm_port) {
ans = 1;
goto done;
}
else {
ans = 0;
goto done;
}
} else {
pml = (struct pmaplist *)
malloc((u_int)sizeof(struct pmaplist));
pml->pml_map = reg;
pml->pml_next = 0;
if (pmaplist == 0) {
pmaplist = pml;
} else {
for (fnd= pmaplist; fnd->pml_next != 0;
fnd = fnd->pml_next);
fnd->pml_next = pml;
}
ans = 1;
}
done:
if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
debugging) {
(void) fprintf(stderr, "svc_sendreply\n");
abort();
}
}
break;
case PMAPPROC_UNSET:
if (!svc_getargs(xprt, xdr_pmap, (caddr_t)®))
svcerr_decode(xprt);
else {
ans = 0;
if (!check_setunset(svc_getcaller(xprt),
rqstp->rq_proc, reg.pm_prog, (u_long) 0))
goto done;
for (prevpml = NULL, pml = pmaplist; pml != NULL; ) {
if ((pml->pml_map.pm_prog != reg.pm_prog) ||
(pml->pml_map.pm_vers != reg.pm_vers)) {
prevpml = pml;
pml = pml->pml_next;
continue;
}
if (!check_privileged_port(svc_getcaller(xprt),
rqstp->rq_proc,
reg.pm_prog,
pml->pml_map.pm_port)) {
ans = 0;
break;
}
ans = 1;
t = (caddr_t)pml;
pml = pml->pml_next;
if (prevpml == NULL)
pmaplist = pml;
else
prevpml->pml_next = pml;
free(t);
}
if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
debugging) {
(void) fprintf(stderr, "svc_sendreply\n");
abort();
}
}
break;
case PMAPPROC_GETPORT:
if (!svc_getargs(xprt, xdr_pmap, (caddr_t)®))
svcerr_decode(xprt);
else {
if (!check_default(svc_getcaller(xprt),
rqstp->rq_proc,
reg.pm_prog)) {
ans = 0;
goto done;
}
fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
if (fnd)
port = fnd->pml_map.pm_port;
else
port = 0;
if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&port)) &&
debugging) {
(void) fprintf(stderr, "svc_sendreply\n");
abort();
}
}
break;
case PMAPPROC_DUMP:
if (!svc_getargs(xprt, xdr_void, NULL))
svcerr_decode(xprt);
else {
struct pmaplist *p;
if (!check_default(svc_getcaller(xprt),
rqstp->rq_proc, (u_long) 0)) {
p = 0;
} else {
p = pmaplist;
}
if ((!svc_sendreply(xprt, xdr_pmaplist,
(caddr_t)&p)) && debugging) {
(void) fprintf(stderr, "svc_sendreply\n");
abort();
}
}
break;
case PMAPPROC_CALLIT:
callit(rqstp, xprt);
break;
default:
check_default(svc_getcaller(xprt), rqstp->rq_proc, (u_long) 0);
svcerr_noproc(xprt);
break;
}
}
#define ARGSIZE 9000
struct encap_parms {
u_int arglen;
char *args;
};
static bool_t
xdr_encap_parms(xdrs, epp)
XDR *xdrs;
struct encap_parms *epp;
{
return (xdr_bytes(xdrs, &(epp->args), &(epp->arglen), ARGSIZE));
}
struct rmtcallargs {
u_long rmt_prog;
u_long rmt_vers;
u_long rmt_port;
u_long rmt_proc;
struct encap_parms rmt_args;
};
static bool_t
xdr_rmtcall_args(xdrs, cap)
XDR *xdrs;
struct rmtcallargs *cap;
{
if (xdr_u_long(xdrs, &(cap->rmt_prog)) &&
xdr_u_long(xdrs, &(cap->rmt_vers)) &&
xdr_u_long(xdrs, &(cap->rmt_proc))) {
return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
}
return (FALSE);
}
static bool_t
xdr_rmtcall_result(xdrs, cap)
XDR *xdrs;
struct rmtcallargs *cap;
{
if (xdr_u_long(xdrs, &(cap->rmt_port)))
return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
return (FALSE);
}
static bool_t
xdr_opaque_parms(xdrs, cap)
XDR *xdrs;
struct rmtcallargs *cap;
{
return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
}
static bool_t
xdr_len_opaque_parms(xdrs, cap)
XDR *xdrs;
struct rmtcallargs *cap;
{
register u_int beginpos, lowpos, highpos, currpos, pos;
beginpos = lowpos = pos = xdr_getpos(xdrs);
highpos = lowpos + ARGSIZE;
while ((int)(highpos - lowpos) >= 0) {
currpos = (lowpos + highpos) / 2;
if (xdr_setpos(xdrs, currpos)) {
pos = currpos;
lowpos = currpos + 1;
} else {
highpos = currpos - 1;
}
}
xdr_setpos(xdrs, beginpos);
cap->rmt_args.arglen = pos - beginpos;
return (xdr_opaque_parms(xdrs, cap));
}
static void
callit(rqstp, xprt)
struct svc_req *rqstp;
SVCXPRT *xprt;
{
struct rmtcallargs a;
struct pmaplist *pml;
u_short port;
struct sockaddr_in me;
int pid, so = -1;
CLIENT *client;
struct authunix_parms *au = (struct authunix_parms *)rqstp->rq_clntcred;
struct timeval timeout;
char buf[ARGSIZE];
timeout.tv_sec = 5;
timeout.tv_usec = 0;
a.rmt_args.args = buf;
if (!svc_getargs(xprt, xdr_rmtcall_args, (caddr_t)&a))
return;
if (!check_callit(svc_getcaller(xprt),
rqstp->rq_proc, a.rmt_prog, a.rmt_proc))
return;
if ((pml = find_service(a.rmt_prog, a.rmt_vers,
(u_long)IPPROTO_UDP)) == NULL)
return;
if ((pid = fork()) != 0) {
if (pid < 0)
syslog(LOG_ERR, "CALLIT (prog %lu): fork: %m",
a.rmt_prog);
return;
}
port = pml->pml_map.pm_port;
get_myaddress(&me);
me.sin_port = htons(port);
client = clntudp_create(&me, a.rmt_prog, a.rmt_vers, timeout, &so);
if (client != (CLIENT *)NULL) {
if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) {
client->cl_auth = authunix_create(au->aup_machname,
au->aup_uid, au->aup_gid, au->aup_len, au->aup_gids);
}
a.rmt_port = (u_long)port;
if (clnt_call(client, a.rmt_proc, xdr_opaque_parms, &a,
xdr_len_opaque_parms, &a, timeout) == RPC_SUCCESS) {
svc_sendreply(xprt, xdr_rmtcall_result, (caddr_t)&a);
}
AUTH_DESTROY(client->cl_auth);
clnt_destroy(client);
}
(void)close(so);
exit(0);
}
static void
reap(sig)
int sig;
{
int save_errno;
save_errno = errno;
while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
errno = save_errno;
}