Commit 44137c79 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

fancier login text

removed lots of wasted comments
cleaned up a little
STOR doesn't work
parent 19a754dc
Loading
Loading
Loading
Loading
+122 −140
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@ use FileHandle;

use strict;

sub spawn;  # forward declaration

open(FTPLOG, ">log/ftpd.log") ||
    print STDERR "failed to open log file, runs without logging\n";

@@ -63,14 +61,17 @@ sub REAPER {
}

# USER is ok in fresh state
my %commandok = ( "USER" => "fresh",
my %commandok = (
                 "USER" => "fresh",
                 "PASS" => "passwd",
                 "PASV" => "loggedin",
                 "PORT" => "loggedin",
                 "TYPE" => "loggedin|twosock",
                 "LIST" => "twosock",
                 "RETR" => "twosock",
                 "STOR" => "twosock",
                 "CWD"  => "loggedin",
                 "SIZE" => "loggedin|twosock",
                 "QUIT"  => "loggedin|twosock",
                 );

@@ -92,13 +93,15 @@ my %displaytext = ('USER' => '331 We are happy you popped in!', # output FTP lin
                   );

# callback functions for certain commands
my %commandfunc = ( 'PORT', \&PORT_command,
                    'LIST', \&LIST_command,
                    'PASV', \&PASV_command,
                    'RETR', \&RETR_command);

my $pid;
my %commandfunc = ( 'PORT' => \&PORT_command,
                    'LIST' => \&LIST_command,
                    'PASV' => \&PASV_command,
                    'RETR' => \&RETR_command,   
                    'SIZE' => \&SIZE_command,
                    'STOR' => \&STOR_command,
                    );

# this is a built-in fake-dir ;-)
my @ftpdir=("total 20\r\n",
"drwxr-xr-x   8 98       98           512 Oct 22 13:06 .\r\n",
"drwxr-xr-x   8 98       98           512 Oct 22 13:06 ..\r\n",
@@ -111,7 +114,6 @@ my @ftpdir=("total 20\r\n",
"drwxrwxrwx   2 98       1            512 Oct 30 14:33 pub\r\n",
"dr-xr-xr-x   5 0        1            512 Oct  1  1997 usr\r\n");


sub LIST_command {
  #  print "150 ASCII data connection for /bin/ls (193.15.23.1,59196) (0 bytes)\r\n";

@@ -127,6 +129,26 @@ sub LIST_command {
    return 0;
}

sub SIZE_command {
    my $testno = $_[0];

    logmsg "SIZE number $testno\n";

    my $filename = "data/reply$testno.txt";

    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        $atime,$mtime,$ctime,$blksize,$blocks)
        = stat($filename);

    if($size) {
        print "213 $size\r\n";
    }
    else {
        print "550 $testno: No such file or directory.\r\n";
    }
    return 0;
}

sub RETR_command {
    my $testno = $_[0];

@@ -157,23 +179,26 @@ sub RETR_command {
    return 0;
}

sub STOR_command {
    my $testno=$_[0];

# < 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 127.0.0.1 (127.0.0.1)
# > PASV
# < 227 Entering Passive Mode (127,0,0,1,210,112)
# * Connecting to localhost (127.0.0.1) port 53872
# * Connected the data stream!
# > TYPE A
# < 200 Type set to A.
# > LIST
# < 150 ASCII data connection for /bin/ls (127.0.0.1,53873) (0 bytes).
#
    logmsg "STOR test number $testno\n";

    my $filename = "log/ftp.upload";

    open(FILE, ">$filename") ||
        return 0; # failed to open output

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

    print "226 File transfer complete\r\n";
    return 0;
}

sub PASV_command {
    socket(Server2, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
@@ -227,6 +252,13 @@ sub PORT_command {

$SIG{CHLD} = \&REAPER;

my @welcome=(
            '220-        _   _ ____  _     '."\r\n",
            '220-    ___| | | |  _ \| |    '."\r\n",
            '220-   / __| | | | |_) | |    '."\r\n",
            '220-  | (__| |_| |  _ <| |___ '."\r\n",
            '220    \___|\___/|_| \_\_____|'."\r\n");

for ( $waitedpid = 0;
      ($paddr = accept(Client,Server)) || $waitedpid;
        $waitedpid = 0, close Client)
@@ -238,36 +270,18 @@ for ( $waitedpid = 0;
    # flush data:
    $| = 1;
        

    logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port\n";
    
    # this code is forked and run
    open(STDIN,  "<&Client")   || die "can't dup client to stdin";
    open(STDOUT, ">&Client")   || die "can't dup client to stdout";
    
    open(INPUT, ">log/server.input") ||
        logmsg "failed to open log/server.input\n";

        # < 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

        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";
    FTPLOG->autoflush(1);
    INPUT->autoflush(1);

    print @welcome;
    my $state="fresh";

    while(1) {
@@ -298,8 +312,6 @@ for ( $waitedpid = 0;
        my $newstate=$statechange{$FTPCMD};
        if($newstate eq "") {
            # remain in the same state
                #print "314 Wwwwweeeeird internal error state: $state\r\n";
                #exit;
        }
        else {
            $state = $newstate;
@@ -314,43 +326,13 @@ for ( $waitedpid = 0;
        my $func = $commandfunc{$FTPCMD};
        if($func) {
            # it is!
                # flush the handles before the possible fork
                FTPLOG->autoflush(1);
                INPUT->autoflush(1);
            \&$func($FTPARG);
        }

            logmsg "gone to state $state\n";
        logmsg "set to state $state\n";
            
    } # while(1)
    close(Client);
    close(Client2);
    close(Server2);
         #   print "Hello there, $name, it's now ", scalar localtime, "\r\n";

}


sub spawn {
    my $coderef = shift;

    unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
        confess "usage: spawn CODEREF";
    }

    my $pid;
    if (!defined($pid = fork)) {
        logmsg "cannot fork: $!\n";
        return;
    } elsif ($pid) {
        logmsg "begat $pid\n";
        return; # I'm the parent
    }
    # else I'm the child -- go spawn


    open(STDIN,  "<&Client")   || die "can't dup client to stdin";
    open(STDOUT, ">&Client")   || die "can't dup client to stdout";
    ## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
    exit &$coderef();
}