Commit fdd91b22 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

moved out the FTP part

parent 7ea4551b
Loading
Loading
Loading
Loading
+12 −135
Original line number Diff line number Diff line
@@ -9,19 +9,19 @@ sub spawn; # forward declaration
sub logmsg { #print "$0 $$: @_ at ", scalar localtime, "\n"
 }

my $port = $ARGV[0];
my $proto = getprotobyname('tcp') || 6;
$port = $1 if $port =~ /(\d+)/; # untaint port number
my $verbose=0; # set to 1 for debugging

my $protocol;
if($ARGV[1] =~ /^ftp$/i) {
    $protocol="FTP";
my $port = 8999; # just a default
do {
    if($ARGV[0] eq "-v") {
        $verbose=1;
    }
else {
    $protocol="HTTP";
    elsif($ARGV[0] =~ /^(\d+)$/) {
        $port = $1;
    }
} while(shift @ARGV);

my $verbose=0; # set to 1 for debugging
my $proto = getprotobyname('tcp') || 6;

socket(Server, PF_INET, SOCK_STREAM, $proto)|| die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
@@ -29,7 +29,7 @@ setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
bind(Server, sockaddr_in($port, INADDR_ANY))|| die "bind: $!";
listen(Server,SOMAXCONN) || die "listen: $!";

print "$protocol server started on port $port\n";
print "HTTP server started on port $port\n";

open(PID, ">.server.pid");
print PID $$;
@@ -44,55 +44,6 @@ sub REAPER {
    logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
}

# USER is ok in fresh state
my %commandok = ( "USER" => "fresh",
                  "PASS" => "passwd",
                  # "PASV" => "loggedin", we can't handle PASV yet
                  "PORT" => "loggedin",
                  );

my %statechange = ( 'USER' => 'passwd',    # USER goes to passwd state
                    'PASS' => 'loggedin',  # PASS goes to loggedin state
                    'PORT' => 'ported',    # PORT goes to ported
                    );

my %displaytext = ('USER' => '331 We are happy you popped in!', # output FTP line
                   'PASS' => '230 Welcome you silly person',
                   );

my %commandfunc = ( 'PORT', \&PORT_command );

sub PORT_command {
    my $arg = $_[0];
    print STDERR "fooo: $arg\n";

    # "193,15,23,1,172,201"

    if($arg !~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {
        print STDERR "bad PORT-line: $arg\n";
        print "314 silly you, go away\r\n";
        return 1;
    }
    my $iaddr = inet_aton("$1.$2.$3.$4");
    my $paddr = sockaddr_in(($5<<8)+$6, $iaddr);
    my $proto   = getprotobyname('tcp') || 6;

    socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "major failure";
    print STDERR "socket()\n";

    connect(SOCK, $paddr)    || return 1;
    print STDERR "connect()\n";

    my $line;
    while (defined($line = <SOCK>)) {
        print STDERR $line;
    }

    close(SOCK);
    print STDERR "close()\n";

}

$SIG{CHLD} = \&REAPER;

for ( $waitedpid = 0;
@@ -109,80 +60,6 @@ for ( $waitedpid = 0;
    spawn sub {
        my ($request, $path, $ver, $left, $cl);

        if($protocol eq "FTP") {

            # < 220 pm1 FTP server (SunOS 5.7) ready.
            # > USER anonymous
            # < 331 Guest login ok, send ident as password.
            # > PASS curl_by_daniel@haxx.se
            # < 230 Guest login ok, access restrictions apply.
            # * We have successfully logged in
            # * Connected to pm1 (193.15.23.1)
            # > PASV
            # < 227 Entering Passive Mode (193,15,23,1,231,59)
            # * Connecting to pm1 (193.15.23.1) port 59195
            # > TYPE A
            # < 200 Type set to A.
            # > LIST
            # < 150 ASCII data connection for /bin/ls (193.15.23.1,59196) (0 bytes).
            # * Getting file with size: -1

            # flush data:
            $| = 1;

            print "220-running the curl suite test server\r\n",
            "220-running the curl suite test server\r\n",
            "220 running the curl suite test server\r\n";

            my $state="fresh";

            while(1) {

                last unless defined ($_ = <STDIN>);

                # Remove trailing CRLF.
                s/[\n\r]+$//;

                unless (m/^([A-Z]{3,4})\s?(.*)/i)
                {
                    print STDERR
                        "badly formed command received: ".$_;
                    exit 0;
                }
                my $FTPCMD=$1;
                my $FTPARG=$2;
                my $full=$_;
                 
                print STDERR "GOT: ($1) $_\n";

                my $ok = $commandok{$FTPCMD};
                if($ok !~ /$state/) {
                    print "314 $FTPCMD not OK ($ok) in state: $state!\r\n";
                    exit;
                }

                $state=$statechange{$FTPCMD};
                if($state eq "") {
                    print "314 Wwwwweeeeird internal error state: $state\r\n";
                    exit;
                }

                # see if the new state is a function caller.
                my $func = $commandfunc{$FTPCMD};
                if($func) {
                    # it is!
                    spawn \&$func($FTPARG);
                }

                print STDERR "gone to state $state\n";

                my $text = $displaytext{$FTPCMD};
                print "$text\r\n";
            }
            exit;
        }
        # otherwise, we're doing HTTP

        my @headers;
        while(<STDIN>) {
            if($_ =~ /([A-Z]*) (.*) HTTP\/1.(\d)/) {