ip_ftp_pxy.c revision f4b3ec61df05330d25f55a36b975b4d7519fdeb1
/*
* Copyright (C) 1997-2003 by Darren Reed
*
* See the IPFILTER.LICENCE file for details on licencing.
*
* $Id: ip_ftp_pxy.c,v 2.88.2.15 2005/03/19 19:38:10 darrenr Exp $
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Simple FTP transparent proxy for in-kernel use. For use with the NAT
* code.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#define IPF_FTP_PROXY
#define IPF_MINPORTLEN 18
#define IPF_MAXPORTLEN 30
#define IPF_MIN227LEN 39
#define IPF_MAX227LEN 51
#define IPF_MIN229LEN 47
#define IPF_MAX229LEN 51
#define FTPXY_GO 0
#define FTPXY_INIT 1
#define FTPXY_USER_1 2
#define FTPXY_USOK_1 3
#define FTPXY_PASS_1 4
#define FTPXY_PAOK_1 5
#define FTPXY_AUTH_1 6
#define FTPXY_AUOK_1 7
#define FTPXY_ADAT_1 8
#define FTPXY_ADOK_1 9
#define FTPXY_ACCT_1 10
#define FTPXY_ACOK_1 11
#define FTPXY_USER_2 12
#define FTPXY_USOK_2 13
#define FTPXY_PASS_2 14
#define FTPXY_PAOK_2 15
/*
* Values for FTP commands. Numerics cover 0-999
*/
#define FTPXY_C_PASV 1000
typedef struct ifs_ftppxy {
int ftp_proxy_init;
int ippr_ftp_pasvonly;
int ippr_ftp_insecure;
/* Do not require logins before transfers */
int ippr_ftp_pasvrdr;
int ippr_ftp_forcepasv;
/* PASV must be last command prior to 227 */
/*
* 1 - security
* 2 - errors
* 3 - error debugging
* 4 - parsing errors
* 5 - parsing info
* 6 - parsing debug
*/
int ippr_ftp_debug;
} ifs_ftppxy_t;
ifs_ftppxy_t *));
ifs_ftppxy_t *));
ifs_ftppxy_t *));
ifs_ftppxy_t *));
ifs_ftppxy_t *));
ifs_ftppxy_t *));
/*
* Initialize local structures.
*/
void **private;
{
return -1;
ifsftp->ippr_ftp_pasvonly = 0;
ifsftp->ippr_ftp_insecure = 0;
ifsftp->ippr_ftp_pasvrdr = 0;
ifsftp->ippr_ftp_forcepasv = 0;
#if defined(_KERNEL)
ifsftp->ippr_ftp_debug = 0;
#else
#endif
return 0;
}
void **private;
{
ifsftp->ftp_proxy_init = 0;
}
}
/*ARGSUSED*/
void *private;
{
ftpside_t *f;
return -1;
return 0;
}
ftpside_t *f;
int dlen;
{
char newbuf[IPF_FTPBUFSZ], *s;
mb_t *m;
/*
* Check for client sending out PORT message.
*/
if (dlen < IPF_MINPORTLEN) {
printf("ippr_ftp_port:dlen(%d) < IPF_MINPORTLEN\n",
dlen);
return 0;
}
/*
* Skip the PORT command + space
*/
s = f->ftps_rptr + 5;
/*
* Pick out the address components, two at a time.
*/
a1 = ippr_ftp_atoi(&s);
if (s == NULL) {
return 0;
}
a2 = ippr_ftp_atoi(&s);
if (s == NULL) {
return 0;
}
/*
* sender of the command - prevents using PORT for port scanning.
*/
a1 <<= 16;
if (ifsftp->ippr_ftp_debug > 0)
return APR_ERR(1);
}
a5 = ippr_ftp_atoi(&s);
if (s == NULL) {
return 0;
}
if (*s == ')')
s++;
/*
* check for CR-LF at the end.
*/
if (*s == '\n')
s--;
if ((*s == '\r') && (*(s + 1) == '\n')) {
s += 2;
} else {
return 0;
}
a5 >>= 8;
a5 &= 0xff;
/*
* Don't allow the PORT command to specify a port < 1024 due to
* security crap.
*/
if (sp < 1024) {
if (ifsftp->ippr_ftp_debug > 0)
return 0;
}
/*
* Calculate new address parts for PORT command
*/
else
a1 >>= 24;
/* DO NOT change this to snprintf! */
#else
#endif
if (ifsftp->ippr_ftp_debug > 0)
printf("ippr_ftp_port:inc(%d) + ip->ip_len > 65535\n",
inc);
return 0;
}
#if !defined(_KERNEL)
#else
# if defined(MENTAT)
if (inc < 0)
# else /* defined(MENTAT) */
/*
* m_adj takes care of pkthdr.len, if required and treats inc<0 to
* mean remove -len bytes from the end of the packet.
* The mbuf chain will be extended if necessary by m_copyback().
*/
if (inc < 0)
# endif /* defined(MENTAT) */
#endif /* !defined(_KERNEL) */
if (inc != 0) {
}
/*
* The server may not make the connection back from port 20, but
* it is the most likely so use it here to check for a conflicting
* mapping.
*/
/*
* Add skeleton NAT entry for connection which will come back the
* other way.
*/
else
int slen;
}
flags |= NAT_NOTRULEPORT;
}
ifs);
}
} else {
}
}
}
int dlen;
{
ftpside_t *f;
int inc, i;
inc = 0;
for (i = 0; (i < 5) && (i < dlen); i++) {
c = rptr[i];
if (ISALPHA(c)) {
} else {
cmd[i] = c;
}
}
cmd[i] = '\0';
} else {
}
}
!ifsftp->ippr_ftp_pasvonly &&
}
;
return inc;
}
int dlen;
{
char newbuf[IPF_FTPBUFSZ];
char *s, *brackets[2];
ftpside_t *f;
if (ifsftp->ippr_ftp_forcepasv != 0 &&
if (ifsftp->ippr_ftp_debug > 0)
printf("ippr_ftp_pasv:ftps_cmds(%d) != FTPXY_C_PASV\n",
return 0;
}
#define PASV_REPLEN 24
/*
* Check for PASV reply message.
*/
if (dlen < IPF_MIN227LEN) {
printf("ippr_ftp_pasv:dlen(%d) < IPF_MIN227LEN\n",
dlen);
return 0;
"227 Entering Passive Mod", PASV_REPLEN)) {
if (ifsftp->ippr_ftp_debug > 0)
return 0;
}
brackets[0] = "";
/*
* Skip the PASV reply + space
*/
s = f->ftps_rptr + PASV_REPLEN;
while (*s && !ISDIGIT(*s)) {
if (*s == '(') {
brackets[0] = "(";
}
s++;
}
/*
* Pick out the address components, two at a time.
*/
a1 = ippr_ftp_atoi(&s);
if (s == NULL) {
return 0;
}
a2 = ippr_ftp_atoi(&s);
if (s == NULL) {
return 0;
}
/*
* check that IP address in the PASV reply is the same as the
* sender of the command - prevents using PASV for port scanning.
*/
a1 <<= 16;
if (ifsftp->ippr_ftp_debug > 0)
return 0;
}
a5 = ippr_ftp_atoi(&s);
if (s == NULL) {
return 0;
}
if (*s == ')')
s++;
if (*s == '.')
s++;
if (*s == '\n')
s--;
/*
* check for CR-LF at the end.
*/
if ((*s == '\r') && (*(s + 1) == '\n')) {
s += 2;
} else {
return 0;
}
a5 >>= 8;
/*
* Calculate new address parts for 227 reply
*/
} else
a1 >>= 24;
#else
#endif
}
ftpside_t *f;
char *newmsg;
char *s;
{
mb_t *m;
inc = 0;
if (ifsftp->ippr_ftp_debug > 0)
printf("ippr_ftp_pasv:inc(%d) + ip->ip_len > 65535\n",
inc);
return 0;
}
#if !defined(_KERNEL)
#else
# if defined(MENTAT)
if (inc < 0)
# else /* defined(MENTAT) */
/*
* m_adj takes care of pkthdr.len, if required and treats inc<0 to
* mean remove -len bytes from the end of the packet.
* The mbuf chain will be extended if necessary by m_copyback().
*/
if (inc < 0)
# endif /* defined(MENTAT) */
#endif /* !defined(_KERNEL) */
if (inc != 0) {
}
/*
* Add skeleton NAT entry for connection which will come back the
* other way.
*/
nflags |= SI_W_DPORT;
else
int slen;
}
}
ifs);
}
} else {
}
}
return inc;
}
int dlen;
{
ftpside_t *f;
int inc;
inc = 0;
if (*rptr == ' ')
goto server_cmd_ok;
return 0;
if (*rptr == '3') {
else
ftp->ftp_passok++;
} else if (*rptr == '2') {
switch (ftp->ftp_passok)
{
case FTPXY_USER_1 :
case FTPXY_USER_2 :
case FTPXY_PASS_1 :
case FTPXY_PASS_2 :
case FTPXY_ACCT_1 :
break;
default :
break;
}
}
}
;
return inc;
}
/*
* Look to see if the buffer starts with something which we recognise as
* being the correct syntax for the FTP protocol.
*/
char *buf;
{
register char *s, c, pc;
char cmd[5];
s = buf;
return 1;
if (i < 5) {
printf("ippr_ftp_client_valid:i(%d) < 5\n", (int)i);
return 2;
}
i--;
c = *s++;
if (ISALPHA(c)) {
c = *s++;
i--;
if (ISALPHA(c)) {
c = *s++;
i--;
if (ISALPHA(c)) {
c = *s++;
i--;
if (ISALPHA(c)) {
c = *s++;
i--;
if ((c != ' ') && (c != '\r'))
goto bad_client_command;
} else if ((c != ' ') && (c != '\r'))
goto bad_client_command;
} else
goto bad_client_command;
} else
goto bad_client_command;
} else {
printf("%s:bad:junk %d len %d/%d c 0x%x buf [%*s]\n",
"ippr_ftp_client_valid",
return 1;
}
for (; i; i--) {
pc = c;
c = *s++;
else
return 0;
}
}
#if !defined(_KERNEL)
printf("ippr_ftp_client_valid:junk after cmd[%*.*s]\n",
#endif
return 2;
}
char *buf;
{
register char *s, c, pc;
int cmd;
s = buf;
cmd = 0;
return 1;
if (i < 5) {
printf("ippr_ftp_servert_valid:i(%d) < 5\n", (int)i);
return 2;
}
c = *s++;
i--;
if (c == ' ')
goto search_eol;
if (ISDIGIT(c)) {
c = *s++;
i--;
if (ISDIGIT(c)) {
c = *s++;
i--;
if (ISDIGIT(c)) {
cmd += (c - '0');
c = *s++;
i--;
if ((c != '-') && (c != ' '))
goto bad_server_command;
} else
goto bad_server_command;
} else
goto bad_server_command;
} else {
printf("%s:bad:junk %d len %d/%d c 0x%x buf [%*s]\n",
"ippr_ftp_server_valid",
return 1;
}
for (; i; i--) {
pc = c;
c = *s++;
return 0;
}
}
printf("ippr_ftp_server_valid:junk after cmd[%*s]\n",
return 2;
}
int side;
char *buf;
{
int ret;
if (side == 0)
else
return ret;
}
/*
* For map rules, the following applies:
* rv == 0 for outbound processing,
* rv == 1 for inbound processing.
* For rdr rules, the following applies:
* rv == 0 for inbound processing,
* rv == 1 for outbound processing.
*/
int rv;
{
ftpside_t *f, *t;
mb_t *m;
#ifdef __sgi
#else
#endif
if (mlen <= 0) {
}
return 0;
}
if (rv == 0) {
} else {
if (ackoff > 0) {
} else {
}
}
printf("%s: %x seq %x/%d ack %x/%d len %d/%d off %d\n",
}
/*
* XXX - Ideally, this packet should get dropped because we now know
* that it is out of order (and there is no real danger in doing so
* apart from causing packets to go through here ordered).
*/
printf("rv %d t:seq[0] %x seq[1] %x %d/%d\n",
}
ok = 0;
if (t->ftps_seq[0] == 0) {
ok = 1;
} else {
if (ackoff == 0) {
ok = 1;
ok = 1;
}
} else {
ok = 1;
ok = 1;
ok = 1;
ok = 1;
}
}
}
if (!ok)
}
if (!mlen) {
printf("%s:seq[0](%x) + (%x) != (%x)\n",
"ippr_ftp_process", t->ftps_seq[0],
}
return APR_ERR(1);
}
printf("ippr_ftp_process:f:seq[0] %x seq[1] %x\n",
}
} else {
printf("FIN: thseq %x seqoff %d ftps_seq %x\n",
}
return APR_ERR(1);
}
}
f->ftps_len = 0;
return 0;
}
ok = 0;
ok = 1;
/*
* Retransmitted data packet.
*/
ok = 1;
}
if (ok == 0) {
printf("th_seq %x ftps_seq %x/%x\n",
}
return APR_ERR(1);
}
inc = 0;
while (mlen > 0) {
printf("%s:len %d/%d off %d wptr %lx junk %d [%*s]\n",
"ippr_ftp_process",
if (f->ftps_junk != 0) {
i = f->ftps_junk;
printf("%s:junk %d -> %d\n",
"ippr_ftp_process", i, f->ftps_junk);
if (f->ftps_junk != 0) {
printf("%s:full buffer\n",
"ippr_ftp_process");
/*
* Because we throw away data here that
* we would otherwise parse, set the
* junk flag to indicate just ignore
* any data upto the next CRLF.
*/
f->ftps_junk = 1;
continue;
}
}
}
printf("%s=%d len %d rv %d ptr %lx/%lx ",
"ippr_ftp_valid",
}
if (f->ftps_junk == 0) {
if (rv)
else
}
}
/*
* Off to a bad start so lets just forget about using the
* ftp proxy for this connection.
*/
/* f->ftps_seq[1] += inc; */
printf("%s:cmds == 0 junk == 1\n",
"ippr_ftp_process");
return APR_ERR(2);
}
(*(s + 1) == '\n')) {
rptr = s + 2;
f->ftps_junk = 0;
break;
}
}
}
} else {
/*
* Compact the buffer back to the start. The junk
* flag should already be set and because we're not
* throwing away any data, it is preserved from its
* current state.
*/
}
}
}
/* f->ftps_seq[1] += inc; */
f->ftps_seq[1]++;
#ifdef __sgi
#else
#endif
printf("ftps_seq[1] = %x inc %d len %d\n",
}
}
void *private;
{
int rev;
return 0;
}
void *private;
{
int rev;
return 0;
}
/*
* ippr_ftp_atoi - implement a version of atoi which processes numbers in
* pairs separated by commas (which are expected to be in the range 0 - 255),
* returning a 16 bit number combining either side of the , as the MSB and
* LSB.
*/
char **ptr;
{
register char *s = *ptr, c;
register u_char i = 0, j = 0;
while (((c = *s++) != '\0') && ISDIGIT(c)) {
i *= 10;
i += c - '0';
}
if (c != ',') {
return 0;
}
while (((c = *s++) != '\0') && ISDIGIT(c)) {
j *= 10;
j += c - '0';
}
*ptr = s;
i &= 0xff;
j &= 0xff;
return (i << 8) | j;
}
ftpside_t *f;
int dlen;
{
char newbuf[IPF_FTPBUFSZ];
char *s;
#define EPSV_REPLEN 33
/*
* Check for EPSV reply message.
*/
if (dlen < IPF_MIN229LEN)
return (0);
"229 Entering Extended Passive Mode", EPSV_REPLEN))
return (0);
/*
* Skip the EPSV command + space
*/
s = f->ftps_rptr + 33;
while (*s && !ISDIGIT(*s))
s++;
/*
* As per RFC 2428, there are no addres components in the EPSV
* response. So we'll go straight to getting the port.
*/
while (*s && ISDIGIT(*s)) {
ap *= 10;
ap += *s++ - '0';
}
if (!s)
return 0;
if (*s == '|')
s++;
if (*s == ')')
s++;
if (*s == '\n')
s--;
/*
* check for CR-LF at the end.
*/
if ((*s == '\r') && (*(s + 1) == '\n')) {
s += 2;
} else
return 0;
"229 Entering Extended Passive Mode", ap);
#else
"229 Entering Extended Passive Mode", ap);
#endif
}