/*
* Copyright (C) 1997 and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the project 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 PROJECT 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 PROJECT 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.
*/
/*
* The order of the following checks does (slightly) matter.
* It is important to visit all checks (do not use "continue"),
* otherwise some of the pipe may become full and we cannot
* relay correctly.
*/
if (pfd[1].revents & POLLIN)
{
/*
* copy control connection from the client.
* command translation is necessary.
*/
error = ftp_copycommand(ctl6, ctl4, &state);
if (error < 0)
goto bad;
else if (error == 0) {
(void)close(ctl4);
(void)close(ctl6);
exit_success("terminating ftp control connection");
/*NOTREACHED*/
}
}
if (pfd[0].revents & POLLIN)
{
/*
* copy control connection from the server
* translation of result code is necessary.
*/
error = ftp_copyresult(ctl4, ctl6, state);
if (error < 0)
goto bad;
else if (error == 0) {
(void)close(ctl4);
(void)close(ctl6);
exit_success("terminating ftp control connection");
/*NOTREACHED*/
}
}
if (0 <= port4 && 0 <= port6 && (pfd[2].revents & POLLIN))
{
/*
* copy data connection.
* no special treatment necessary.
*/
if (pfd[2].revents & POLLIN)
error = ftp_copy(port4, port6);
switch (error) {
case -1:
goto bad;
case 0:
if (port4 >= 0) {
(void)close(port4);
port4 = -1;
}
if (port6 >= 0) {
(void)close(port6);
port6 = -1;
}
syslog(LOG_INFO, "terminating data connection");
break;
default:
break;
}
}
if (0 <= port4 && 0 <= port6 && (pfd[3].revents & POLLIN))
{
/*
* copy data connection.
* no special treatment necessary.
*/
if (pfd[3].revents & POLLIN)
error = ftp_copy(port6, port4);
switch (error) {
case -1:
goto bad;
case 0:
if (port4 >= 0) {
(void)close(port4);
port4 = -1;
}
if (port6 >= 0) {
(void)close(port6);
port6 = -1;
}
syslog(LOG_INFO, "terminating data connection");
break;
default:
break;
}
}
#if 0
if (wport4 && (pfd[4].revents & POLLIN))
{
/*
* establish active data connection from the server.
*/
ftp_activeconn();
}
if (wport4 && (pfd[5].revents & POLLIN))
{
/*
* establish passive data connection from the client.
*/
ftp_passiveconn();
}
#endif
}
bad:
exit_failure("%s", strerror(errno));
}
static int
ftp_activeconn()
{
socklen_t n;
int error;
struct pollfd pfd[1];
struct sockaddr *sa;
/* get active connection from server */
pfd[0].fd = wport4;
pfd[0].events = POLLIN;
n = sizeof(data4);
if (poll(pfd, (unsigned int)(sizeof(pfd) / sizeof(pfd[0])),
120000) == 0 ||
(port4 = accept(wport4, (void *)&data4, &n)) < 0)
{
(void)close(wport4);
wport4 = -1;
syslog(LOG_INFO, "active mode data connection failed");
return -1;
}
/* ask active connection to client */
sa = (void *)&data6;
port6 = socket(sa->sa_family, SOCK_STREAM, 0);
if (port6 == -1) {
(void)close(port4);
(void)close(wport4);
port4 = wport4 = -1;
syslog(LOG_INFO, "active mode data connection failed");
return -1;
}
error = connect(port6, sa, (socklen_t)sa->sa_len);
if (error < 0) {
(void)close(port6);
(void)close(port4);
(void)close(wport4);
port6 = port4 = wport4 = -1;
syslog(LOG_INFO, "active mode data connection failed");
return -1;
}
syslog(LOG_INFO, "active mode data connection established");
return 0;
}
static int
ftp_passiveconn()
{
socklen_t len;
int error;
struct pollfd pfd[1];
struct sockaddr *sa;
/* get passive connection from client */
pfd[0].fd = wport6;
pfd[0].events = POLLIN;
len = sizeof(data6);
if (poll(pfd, (unsigned int)(sizeof(pfd) / sizeof(pfd[0])),
120000) == 0 ||
(port6 = accept(wport6, (void *)&data6, &len)) < 0)
{
(void)close(wport6);
wport6 = -1;
syslog(LOG_INFO, "passive mode data connection failed");
return -1;
}
/* ask passive connection to server */
sa = (void *)&data4;
port4 = socket(sa->sa_family, SOCK_STREAM, 0);
if (port4 == -1) {
(void)close(wport6);
(void)close(port6);
wport6 = port6 = -1;
syslog(LOG_INFO, "passive mode data connection failed");
return -1;
}
error = connect(port4, sa, (socklen_t)sa->sa_len);
if (error < 0) {
(void)close(wport6);
(void)close(port4);
(void)close(port6);
wport6 = port4 = port6 = -1;
syslog(LOG_INFO, "passive mode data connection failed");
return -1;
}
syslog(LOG_INFO, "passive mode data connection established");
return 0;
}
static ssize_t
ftp_copy(int src, int dst)
{
int error, atmark;
ssize_t n;
/* OOB data handling */
error = ioctl(src, SIOCATMARK, &atmark);
if (error != -1 && atmark == 1) {
n = read(src, rbuf, 1);
if (n == -1)
goto bad;
(void)send(dst, rbuf, (size_t)n, MSG_OOB);
#if 0
n = read(src, rbuf, sizeof(rbuf));
if (n == -1)
goto bad;
(void)write(dst, rbuf, (size_t)n);
return n;
#endif
}
n = read(src, rbuf, sizeof(rbuf));
switch (n) {
case -1:
case 0:
return n;
default:
(void)write(dst, rbuf, (size_t)n);
return n;
}
bad:
exit_failure("%s", strerror(errno));
/*NOTREACHED*/
return 0; /* to make gcc happy */
}
static ssize_t
ftp_copyresult(int src, int dst, enum state state)
{
int error, atmark;
ssize_t n;
socklen_t len;
char *param;
int code;
char *a, *p;
int i;
/* OOB data handling */
error = ioctl(src, SIOCATMARK, &atmark);
if (error != -1 && atmark == 1) {
n = read(src, rbuf, 1);
if (n == -1)
goto bad;
(void)send(dst, rbuf, (size_t)n, MSG_OOB);
#if 0
n = read(src, rbuf, sizeof(rbuf));
if (n == -1)
goto bad;
(void)write(dst, rbuf, (size_t)n);
return n;
#endif
}
n = read(src, rbuf, sizeof(rbuf));
if (n <= 0)
return n;
rbuf[n] = '\0';
/*
* parse argument
*/
p = rbuf;
for (i = 0; i < 3; i++) {
if (!isdigit((unsigned char)*p)) {
/* invalid reply */
(void)write(dst, rbuf, (size_t)n);
return n;
}
p++;
}
if (!isspace((unsigned char)*p)) {
/* invalid reply */
(void)write(dst, rbuf, (size_t)n);
return n;
}
code = atoi(rbuf);
param = p;
/* param points to first non-command token, if any */
while (*param && isspace((unsigned char)*param))
param++;
if (!*param)
param = NULL;
switch (state) {
case NONE:
if (!passivemode && rbuf[0] == '1') {
if (ftp_activeconn() < 0) {
n = snprintf(rbuf, sizeof(rbuf),
"425 Cannot open data connection\r\n");
if (n < 0 || n >= (int)sizeof(rbuf))
n = 0;
}
}
if (n)
(void)write(dst, rbuf, (size_t)n);
return n;
case LPRT:
case EPRT:
/* expecting "200 PORT command successful." */
if (code == 200) {
p = strstr(rbuf, "PORT");
if (p) {
p[0] = (state == LPRT) ? 'L' : 'E';
p[1] = 'P';
}
} else {
(void)close(wport4);
wport4 = -1;
}
(void)write(dst, rbuf, (size_t)n);
return n;
case LPSV:
case EPSV:
/*
* expecting "227 Entering Passive Mode (x,x,x,x,x,x,x)"
* (in some cases result comes without paren)
*/
if (code != 227) {
passivefail0:
(void)close(wport6);
wport6 = -1;
(void)write(dst, rbuf, (size_t)n);
return n;
}