alias_ftp.c revision 4dd5020158b69719fd7942ff7b996df2d4535bb4
/*-
* Copyright (c) 2001 Charles Mott <cm@linktel.net>
* 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
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
#ifndef VBOX
__FBSDID("$FreeBSD: src/sys/netinet/libalias/alias_ftp.c,v 1.29.2.1.4.1 2009/04/15 03:14:26 kensmith Exp $");
/*
Alias_ftp.c performs special processing for FTP sessions under
side or 227/229 reply from the server is sent, it is intercepted
and modified. The address is changed to the gateway machine
and an aliasing port is used.
For this routine to work, the message must fit entirely into a
single TCP packet. This is typically the case, but exceptions
can easily be envisioned under the actual specifications.
Probably the most troubling aspect of the approach taken here is
that the new message will typically be a different length, and
this causes a certain amount of bookkeeping to keep track of the
changes of sequence and acknowledgment numbers, since the client
machine is totally unaware of the modification to the TCP stream.
References: RFC 959, RFC 2428.
Initial version: August, 1996 (cjm)
Version 1.6
Brian Somers and Martin Renters identified an IP checksum
error for modified IP packets.
Version 1.7: January 9, 1996 (cjm)
Differential checksum computation for change
in IP packet length.
Version 2.1: May, 1997 (cjm)
Very minor changes to conform with
within the packet aliasing module.
Version 3.1: May, 2000 (eds)
Add support for passive mode, alias the 227 replies.
See HISTORY file for record of revisions.
*/
/* Includes */
#ifdef _KERNEL
#else
#include <errno.h>
#include <stdio.h>
#include <string.h>
#endif
#include <netinet/in_systm.h>
#ifdef _KERNEL
#else
#include "alias_local.h"
#include "alias_mod.h"
#endif
#else /* VBOX */
# include <slirp.h>
# include "alias_local.h"
# include "alias_mod.h"
#endif /* VBOX */
#define FTP_CONTROL_PORT_NUMBER 21
static void
int maxpacketsize);
static int
{
#ifdef VBOX
#endif
ah->maxpktsize == 0)
return (-1);
return (0);
return (-1);
}
static int
{
return (0);
}
#ifndef VBOX
struct proto_handler handlers[] = {
{
.pri = 80,
.fingerprint = &fingerprint,
},
{ EOH }
};
#else /* VBOX */
#endif /* VBOX */
#ifndef VBOX
static int
#else
int
{
}
int
{
}
static int
#endif
{
int error;
#ifdef VBOX
#endif /* VBOX */
switch (type) {
case MOD_LOAD:
error = 0;
#ifdef VBOX
#else
#endif
break;
case MOD_UNLOAD:
error = 0;
#ifdef VBOX
#else
#endif
break;
default:
}
return (error);
}
#ifndef VBOX
#ifdef _KERNEL
static
#endif
};
#endif /* !VBOX */
#ifdef _KERNEL
#endif
#define FTP_CONTROL_PORT_NUMBER 21
#define MAX_MESSAGE_SIZE 128
/* FTP protocol flags. */
#define WAIT_CRLF 0x01
enum ftp_message_type {
};
static void
int maxpacketsize /* The maximum size this packet can grow to
(including headers) */ )
{
char *sptr;
int ftp_message_type;
/* Calculate data length of TCP packet */
/* Place string pointer and beginning of data */
/*
* Check that data length is not too long and previous message was
* properly terminated with CRLF.
*/
/*
*/
} else {
/*
* When aliasing a server, check for the 227/229 reply.
*/
}
}
if (ftp_message_type != FTP_UNKNOWN_MESSAGE)
}
if (dlen) { /* only if there's data */
* have grown */
else
}
}
static int
{
char ch;
int i, state;
/* Format: "PORT A,D,D,R,PO,RT". */
/* Return if data length is too short. */
if (dlen < 18)
return (0);
return (0);
state = 0;
for (i = 5; i < dlen; i++) {
switch (state) {
case 0:
break;
else
state++;
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
state++;
} else
return (0);
break;
case 2:
case 4:
case 6:
case 8:
else if (ch == ',') {
state++;
} else
return (0);
break;
case 10:
case 12:
state++;
} else
return (0);
break;
}
}
if (state == 13) {
return (1);
} else
return (0);
}
static int
{
int i, state;
/* Format: "EPRT |1|A.D.D.R|PORT|". */
/* Return if data length is too short. */
if (dlen < 18)
return (0);
return (0);
state = 0;
for (i = 5; i < dlen; i++) {
switch (state) {
case 0:
state++;
}
break;
case 1:
state++;
else
return (0);
break;
case 2:
state++;
else
return (0);
break;
case 3:
case 5:
case 7:
case 9:
state++;
} else
return (0);
break;
case 4:
case 6:
case 8:
case 10:
state++;
} else
return (0);
break;
case 11:
state++;
} else
return (0);
break;
case 12:
state++;
else
return (0);
break;
}
}
if (state == 13) {
return (1);
} else
return (0);
}
static int
{
char ch;
int i, state;
/* Format: "227 Entering Passive Mode (A,D,D,R,PO,RT)" */
/* Return if data length is too short. */
if (dlen < 17)
return (0);
return (0);
state = 0;
for (i = 4; i < dlen; i++) {
switch (state) {
case 0:
if (ch == '(')
state++;
break;
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
state++;
} else
return (0);
break;
case 2:
case 4:
case 6:
case 8:
else if (ch == ',') {
state++;
} else
return (0);
break;
case 10:
case 12:
state++;
} else
return (0);
break;
}
}
if (state == 13) {
if (addr != INADDR_LOOPBACK)
else
return (1);
} else
return (0);
}
static int
{
int i, state;
/* Format: "229 Entering Extended Passive Mode (|||PORT|)" */
/* Return if data length is too short. */
if (dlen < 11)
return (0);
return (0);
port = 0;
state = 0;
for (i = 4; i < dlen; i++) {
switch (state) {
case 0:
if (ch == '(')
state++;
break;
case 1:
state++;
break;
case 2:
case 3:
state++;
else
return (0);
break;
case 4:
state++;
} else
return (0);
break;
case 5:
state++;
else
return (0);
break;
case 6:
if (ch == ')')
state++;
else
return (0);
break;
}
}
if (state == 7) {
return (1);
} else
return (0);
}
static void
struct alias_link *lnk,
int maxpacketsize,
int ftp_message_type)
{
struct alias_link *ftp_lnk;
/* Security checks. */
return;
return;
/* Establish link to address and port found in FTP control message. */
#ifndef NO_FW_PUNCH
/* Punch hole in firewall */
#endif
/* Calculate data length of TCP packet */
/* Create new FTP message. */
{
char *sptr;
struct in_addr alias_address;
/* Decompose alias address into quad format */
/* Prepare new command */
switch (ftp_message_type) {
case FTP_PORT_COMMAND:
case FTP_227_REPLY:
/* Decompose alias port into pair format. */
if (ftp_message_type == FTP_PORT_COMMAND) {
/* Generate PORT command string. */
#ifndef VBOX
#else
#endif
} else {
/* Generate 227 reply string. */
#ifndef VBOX
"227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n",
#else
"227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n",
#endif
}
break;
case FTP_EPRT_COMMAND:
/* Generate EPRT command string. */
#ifndef VBOX
#else
#endif
break;
case FTP_229_REPLY:
/* Generate 229 reply string. */
#ifndef VBOX
ntohs(alias_port));
#else
ntohs(alias_port));
#endif
break;
}
/* Save string length for IP header modification */
/* Copy modified buffer into IP packet. */
}
/* Save information regarding modified seq and ack numbers */
{
int delta;
}
/* Revise IP header */
{
&new_len,
1);
}
/* Compute TCP checksum for revised packet */
#ifdef _KERNEL
#else
#endif
} else {
#ifdef LIBALIAS_DEBUG
"PacketAlias/HandleFtpOut: Cannot allocate FTP data port\n");
#endif
}
}