ip_ftp_pxy.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (C) 1997-2003 by Darren Reed
*
* See the IPFILTER.LICENCE file for details on licencing.
*
* Copyright 2003 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"
#ifdef USE_MUTEXES
extern ipfmutex_t ipf_rw;
#endif
#define IPF_FTP_DEBUG
#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
int ippr_ftp_init __P((void));
void ippr_ftp_fini __P((void));
int ftp_proxy_init = 0;
int ippr_ftp_pasvonly = 0;
int ippr_ftp_insecure = 0; /* Do not require logins before transfers */
int ippr_ftp_pasvrdr = 0;
int ippr_ftp_forcepasv = 0; /* PASV must be last command prior to 227 */
/*
* Initialize local structures.
*/
int ippr_ftp_init()
{
ftp_proxy_init = 1;
return 0;
}
void ippr_ftp_fini()
{
if (ftp_proxy_init == 1) {
ftp_proxy_init = 0;
}
}
{
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) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
#endif
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) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_port:ippr_ftp_atoi(1) failed\n");
#endif
return 0;
}
a2 = ippr_ftp_atoi(&s);
if (s == NULL) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_port:ippr_ftp_atoi(2) failed\n");
#endif
return 0;
}
/*
* sender of the command - prevents using PORT for port scanning.
*/
a1 <<= 16;
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_port:a1 != nat->nat_inip\n");
#endif
return APR_ERR(1);
}
a5 = ippr_ftp_atoi(&s);
if (s == NULL) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_port:ippr_ftp_atoi(3) failed\n");
#endif
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 {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_port:missing cr-lf\n");
#endif
return 0;
}
a5 >>= 8;
a5 &= 0xff;
/*
* Calculate new address parts for PORT command
*/
else
a1 >>= 24;
/* DO NOT change this to snprintf! */
#else
#endif
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
#endif
return 0;
}
#if !defined(_KERNEL)
#else
# if defined(MENTAT)
if (inc < 0)
# else
if (inc < 0)
# ifdef M_PKTHDR
# endif
# endif
#endif
/* the mbuf chain will be extended if necessary by m_copyback() */
if (inc != 0) {
}
/*
* Add skeleton NAT entry for connection which will come back the
* other way.
*/
/*
* Don't allow the PORT command to specify a port < 1024 due to
* security crap.
*/
if (sp < 1024) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
#endif
return 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.
*/
else
int slen;
}
flags |= NAT_NOTRULEPORT;
}
}
} 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 {
}
}
} else if (ippr_ftp_insecure && !ippr_ftp_pasvonly &&
}
;
return inc;
}
int dlen;
{
char newbuf[IPF_FTPBUFSZ];
ftpside_t *f;
char *s;
if (ippr_ftp_forcepasv != 0 &&
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_pasv:ftps_cmds(%d) != FTPXY_C_PASV\n",
#endif
return 0;
}
#define PASV_REPLEN 24
/*
* Check for PASV reply message.
*/
if (dlen < IPF_MIN227LEN) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
#endif
return 0;
"227 Entering Passive Mod", PASV_REPLEN)) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_pasv:227 reply wrong\n");
#endif
return 0;
}
/*
* Skip the PASV reply + space
*/
s = f->ftps_rptr + PASV_REPLEN;
while (*s && !isdigit(*s))
s++;
/*
* Pick out the address components, two at a time.
*/
a1 = ippr_ftp_atoi(&s);
if (s == NULL) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_pasv:ippr_ftp_atoi(1) failed\n");
#endif
return 0;
}
a2 = ippr_ftp_atoi(&s);
if (s == NULL) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_pasv:ippr_ftp_atoi(2) failed\n");
#endif
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 !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_pasv:a1 != nat->nat_oip\n");
#endif
return 0;
}
a5 = ippr_ftp_atoi(&s);
if (s == NULL) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_pasv:ippr_ftp_atoi(3) failed\n");
#endif
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 {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_pasv:missing cr-lf\n");
#endif
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 !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
#endif
return 0;
}
#if !defined(_KERNEL)
#else
# if defined(MENTAT)
if (inc < 0)
# else /* defined(MENTAT) */
if (inc < 0)
/* the mbuf chain will be extended if necessary by m_copyback() */
# endif /* defined(MENTAT) */
#endif /* !defined(_KERNEL) */
if (inc != 0) {
}
/*
* Add skeleton NAT entry for connection which will come back the
* other way.
*/
if (ippr_ftp_pasvrdr && f->ftps_ifp)
nflags |= SI_W_DPORT;
else
int slen;
}
}
}
} else {
}
}
return inc;
}
int dlen;
{
ftpside_t *f;
int inc;
inc = 0;
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;
char cmd[5];
if (i < 5) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_client_valid:i(%d) < 5\n", (int)i);
#endif
return 2;
}
s = buf;
c = *s++;
i--;
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 {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_client_valid:bad cmd:len %d i %d c 0x%x\n",
(int)i, (int)len, c);
#endif
return 1;
}
for (; i; i--) {
c = *s++;
if (c == '\n') {
else
return 0;
}
}
#if !defined(_KERNEL)
#endif
return 2;
}
char *buf;
{
register char *s, c;
int cmd;
if (i < 5)
return 2;
s = buf;
c = *s++;
cmd = 0;
i--;
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 {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_server_valid:bad cmd:len %d i %d c 0x%x\n",
(int)i, (int)len, c);
#endif
return 1;
}
for (; i; i--) {
c = *s++;
if (c == '\n') {
return 0;
}
}
#if !defined(_KERNEL)
#endif
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;
if (mlen <= 0) {
}
return 0;
}
if (rv == 0) {
} else {
#if PROXY_DEBUG
#endif
#if PROXY_DEBUG
#endif
if (ackoff > 0) {
} else {
}
}
#if PROXY_DEBUG
#endif
/*
* 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).
*/
#if PROXY_DEBUG
printf("rv %d t:seq[0] %x seq[1] %x %d/%d\n",
#endif
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 PROXY_DEBUG
if (!ok)
printf("not ok\n");
#endif
if (!mlen) {
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
"ippr_ftp_process:seq[0](%x) + ackoff(%x) != thack(%x)\n",
#endif
return APR_ERR(1);
}
#if PROXY_DEBUG
#endif
} else {
printf("FIN: thseq %x seqoff %d ftps_seq %x\n",
#endif
return APR_ERR(1);
}
}
f->ftps_len = 0;
return 0;
}
ok = 0;
ok = 1;
/*
* Retransmitted data packet.
*/
ok = 1;
}
if (ok == 0) {
#if PROXY_DEBUG || !defined(_KERNEL)
f->ftps_seq[1]);
#endif
return APR_ERR(1);
}
inc = 0;
while (mlen > 0) {
if (f->ftps_junk == 2)
if (f->ftps_junk == 0) {
f->ftps_cmds++;
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; */
#if !defined(_KERNEL) || defined(IPF_FTP_DEBUG)
printf("ippr_ftp_process:cmds == 0 junk == 1\n");
#endif
return APR_ERR(2);
}
rptr++;
if (*rptr == '\r') {
rptr += 2;
f->ftps_junk = 0;
} else
rptr++;
} else
break;
}
}
} else {
f->ftps_junk = 1;
} else {
}
}
}
}
/* f->ftps_seq[1] += inc; */
f->ftps_seq[1]++;
#if PROXY_DEBUG
#endif
}
{
int rev;
return 0;
}
{
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
}
#if 0
{
char *s, c;
if (len < 5)
return -1;
s = str;
if (*s++ != '|')
return -1;
c = *s++;
if (c != '|') {
if (*s++ != '|')
return -1;
} else
c = '1';
if (c == '1') {
/*
* IPv4 dotted quad.
*/
return 0;
} else if (c == '2') {
/*
* IPv6 hex string
*/
return 0;
}
return -1;
}
#endif