Skip to content
ftpserver.pl 31.1 KiB
Newer Older
        elsif($_ =~ /PASVBADIP/) {
            logmsg "FTPD: instructed to use PASVBADIP\n";
            $pasvbadip=1;
        }
        elsif($_ =~ /NOSAVE/) {
            # don't actually store the file we upload - to be used when
            # uploading insanely huge amounts
            $nosave = 1;
            logmsg "FTPD: NOSAVE prevents saving of uploaded data\n";
        }
my @welcome;

if($proto eq "ftp") {
    @welcome=(
        '220-        _   _ ____  _     '."\r\n",
        '220-    ___| | | |  _ \| |    '."\r\n",
        '220-   / __| | | | |_) | |    '."\r\n",
        '220-  | (__| |_| |  _ <| |___ '."\r\n",
        '220    \___|\___/|_| \_\_____|'."\r\n");
}
elsif($proto eq "pop3") {
    @welcome=(
        '        _   _ ____  _     '."\r\n",
        '    ___| | | |  _ \| |    '."\r\n",
        '   / __| | | | |_) | |    '."\r\n",
        '  | (__| |_| |  _ <| |___ '."\r\n",
        '   \___|\___/|_| \_\_____|'."\r\n",
        '+OK cURL POP3 server ready to serve'."\r\n");
}
elsif($proto eq "imap") {
    @welcome=(
        '        _   _ ____  _     '."\r\n",
        '    ___| | | |  _ \| |    '."\r\n",
        '   / __| | | | |_) | |    '."\r\n",
        '  | (__| |_| |  _ <| |___ '."\r\n",
        '   \___|\___/|_| \_\_____|'."\r\n",
        '* OK cURL IMAP server ready to serve'."\r\n");
}
    sysread_or_die(\*SFREAD, \$input, 5);

    if($input !~ /^CNCT/) {
        # we wait for a connected client
    set_advisor_read_lock($SERVERLOGS_LOCK);
Daniel Stenberg's avatar
Daniel Stenberg committed
    # flush data:
    $| = 1;
    killpid($verbose, $slavepid);
    &customize(); # read test control instructions

Daniel Stenberg's avatar
Daniel Stenberg committed
    if($verbose) {
        for(@welcome) {
            print STDERR "OUT: $_";
        }
Daniel Stenberg's avatar
Daniel Stenberg committed
    while(1) {
        my $i;

        # Now we expect to read DATA\n[hex size]\n[prot], where the [prot]
        # part only is FTP lingo.

        # COMMAND
        sysread_or_die(\*SFREAD, \$i, 5);
Daniel Stenberg's avatar
Daniel Stenberg committed

        if($i !~ /^DATA/) {
            logmsg "sockfilt said $i";
            if($i =~ /^DISC/) {
                # disconnect
                last;
            }
            next;
        }

        # SIZE of data
Daniel Stenberg's avatar
Daniel Stenberg committed
        
        ftpmsg $_;
        
        # Remove trailing CRLF.
        s/[\n\r]+$//;
Daniel Stenberg's avatar
Daniel Stenberg committed
        my $full=$_;
        if($proto eq "imap") {
            # IMAP is different with its identifier first on the command line
            unless (m/^([^ ]+) ([^ ]+) (.*)/ ||
                    m/^([^ ]+) ([^ ]+)/) {
                sendcontrol "$1 '$_': command not understood.\r\n";
            $cmdid=$1; # set the global variable
            $FTPCMD=$2;
            $FTPARG=$3;
        }
        else {
            unless (m/^([A-Z]{3,4})\s?(.*)/i) {
                sendcontrol "500 '$_': command not understood.\r\n";
                last;
            }
            $FTPCMD=$1;
            $FTPARG=$2;
        }
        if($verbose) {
            print STDERR "IN: $full\n";
        }

        my $delay = $delayreply{$FTPCMD};
        if($delay) {
            # just go sleep this many seconds!
            logmsg("Sleep for $delay seconds\n");
Yang Tse's avatar
 
Yang Tse committed
            my $twentieths = $delay * 20;
            while($twentieths--) {
                select(undef, undef, undef, 0.05) unless($got_exit_signal);
            }
        my $text;
        $text = $customreply{$FTPCMD};
        my $fake = $text;
        if($text eq "") {
            $text = $displaytext{$FTPCMD};
        }
        else {
            if($customcount{$FTPCMD} && (!--$customcount{$FTPCMD})) {
                # used enough number of times, now blank the customreply
                $customreply{$FTPCMD}="";
            }
Daniel Stenberg's avatar
Daniel Stenberg committed
        if($text) {
Daniel Stenberg's avatar
Daniel Stenberg committed
        }
            $check=1; # no response yet
        if($fake eq "") {
            # only perform this if we're not faking a reply
            my $func = $commandfunc{$FTPCMD};
            if($func) {
Daniel Stenberg's avatar
Daniel Stenberg committed
        }
            logmsg "$FTPCMD wasn't handled!\n";
            sendcontrol "500 $FTPCMD is not dealt with!\r\n";
        }
Daniel Stenberg's avatar
Daniel Stenberg committed
            
    } # while(1)
    if($serverlogslocked) {
        $serverlogslocked = 0;
        clear_advisor_read_lock($SERVERLOGS_LOCK);
    }
Yang Tse's avatar
 
Yang Tse committed
unlink($pidfile);

if($serverlogslocked) {
    $serverlogslocked = 0;
    clear_advisor_read_lock($SERVERLOGS_LOCK);
}