2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 1998 - 2014, 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
11 # This software is licensed as described in the file COPYING, which
12 # you should have received as part of this distribution. The terms
13 # are also available at https://curl.haxx.se/docs/copyright.html.
15 # You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 # copies of the Software, and permit persons to whom the Software is
17 # furnished to do so, under the terms of the COPYING file.
19 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # KIND, either express or implied.
22 ###########################################################################
24 # This is a server designed for the curl test suite.
26 # In December 2009 we started remaking the server to support more protocols
27 # that are similar in spirit. Like POP3, IMAP and SMTP in addition to the FTP
28 # it already supported since a long time. Note that it still only supports one
29 # protocol per invoke. You need to start multiple servers to support multiple
30 # protocols simultaneously.
32 # It is meant to exercise curl, it is not meant to be a fully working
33 # or even very standard compliant server.
35 # You may optionally specify port on the command line, otherwise it'll
36 # default to port 8921.
38 # All socket/network/TCP related stuff is done by the 'sockfilt' program.
42 push(@INC, $ENV{'srcdir'}) if(defined $ENV{'srcdir'});
44 # sub second timestamping needs Time::HiRes
48 import Time::HiRes qw( gettimeofday );
59 require "directories.pm";
71 #**********************************************************************
74 my $verbose = 0; # set to 1 for debugging
75 my $idstr = ""; # server instance string
76 my $idnum = 1; # server instance number
77 my $ipvnum = 4; # server IPv number (4 or 6)
78 my $proto = 'ftp'; # default server protocol
79 my $srcdir; # directory where ftpserver.pl is located
80 my $srvrname; # server name for presentation purposes
81 my $cwd_testno; # test case numbers extracted from CWD command
83 my $logdir = $path .'/log';
85 #**********************************************************************
86 # global vars used for server address and primary listener port
88 my $port = 8921; # default primary listener port
89 my $listenaddr = '127.0.0.1'; # default address for listener port
91 #**********************************************************************
92 # global vars used for file names
94 my $pidfile; # server pid file name
95 my $logfile; # server log file name
96 my $mainsockf_pidfile; # pid file for primary connection sockfilt process
97 my $mainsockf_logfile; # log file for primary connection sockfilt process
98 my $datasockf_pidfile; # pid file for secondary connection sockfilt process
99 my $datasockf_logfile; # log file for secondary connection sockfilt process
101 #**********************************************************************
102 # global vars used for server logs advisor read lock handling
104 my $SERVERLOGS_LOCK = 'log/serverlogs.lock';
105 my $serverlogslocked = 0;
107 #**********************************************************************
108 # global vars used for child processes PID tracking
110 my $sfpid; # PID for primary connection sockfilt process
111 my $slavepid; # PID for secondary connection sockfilt process
113 #**********************************************************************
114 # global typeglob filehandle vars to read/write from/to sockfilters
116 local *SFREAD; # used to read from primary connection
117 local *SFWRITE; # used to write to primary connection
118 local *DREAD; # used to read from secondary connection
119 local *DWRITE; # used to write to secondary connection
121 my $sockfilt_timeout = 5; # default timeout for sockfilter eXsysreads
123 #**********************************************************************
124 # global vars which depend on server protocol selection
126 my %commandfunc; # protocol command specific function callbacks
127 my %displaytext; # text returned to client before callback runs
129 #**********************************************************************
130 # global vars customized for each test from the server commands file
132 my $ctrldelay; # set if server should throttle ctrl stream
133 my $datadelay; # set if server should throttle data stream
134 my $retrweirdo; # set if ftp server should use RETRWEIRDO
135 my $retrnosize; # set if ftp server should use RETRNOSIZE
136 my $pasvbadip; # set if ftp server should use PASVBADIP
137 my $nosave; # set if ftp server should not save uploaded data
138 my $nodataconn; # set if ftp srvr doesn't establish or accepts data channel
139 my $nodataconn425; # set if ftp srvr doesn't establish data ch and replies 425
140 my $nodataconn421; # set if ftp srvr doesn't establish data ch and replies 421
141 my $nodataconn150; # set if ftp srvr doesn't establish data ch and replies 150
142 my @capabilities; # set if server supports capability commands
143 my @auth_mechs; # set if server supports authentication commands
149 #**********************************************************************
150 # global variables for to test ftp wildcardmatching or other test that
151 # need flexible LIST responses.. and corresponding files.
152 # $ftptargetdir is keeping the fake "name" of LIST directory.
154 my $ftplistparserstate;
157 #**********************************************************************
158 # global variables used when running a ftp server to keep state info
159 # relative to the secondary or data sockfilt process. Values of these
160 # variables should only be modified using datasockf_state() sub, given
161 # that they are closely related and relationship is a bit awkward.
163 my $datasockf_state = 'STOPPED'; # see datasockf_state() sub
164 my $datasockf_mode = 'none'; # ['none','active','passive']
165 my $datasockf_runs = 'no'; # ['no','yes']
166 my $datasockf_conn = 'no'; # ['no','yes']
168 #**********************************************************************
169 # global vars used for signal handling
171 my $got_exit_signal = 0; # set if program should finish execution ASAP
172 my $exit_signal; # first signal handled in exit_signal_handler
174 #**********************************************************************
175 # Mail related definitions
177 my $TEXT_PASSWORD = "secret";
178 my $POP3_TIMESTAMP = "<1972.987654321\@curl>";
180 #**********************************************************************
181 # exit_signal_handler will be triggered to indicate that the program
182 # should finish its execution in a controlled way as soon as possible.
183 # For now, program will also terminate from within this handler.
185 sub exit_signal_handler {
187 # For now, simply mimic old behavior.
188 killsockfilters($proto, $ipvnum, $idnum, $verbose);
190 if($serverlogslocked) {
191 $serverlogslocked = 0;
192 clear_advisor_read_lock($SERVERLOGS_LOCK);
197 #**********************************************************************
198 # logmsg is general message logging subroutine for our test servers.
202 # sub second timestamping needs Time::HiRes
203 if($Time::HiRes::VERSION) {
204 my ($seconds, $usec) = gettimeofday();
205 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
207 $now = sprintf("%02d:%02d:%02d.%06d ", $hour, $min, $sec, $usec);
210 my $seconds = time();
211 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
213 $now = sprintf("%02d:%02d:%02d ", $hour, $min, $sec);
215 if(open(LOGFILEFH, ">>$logfile")) {
216 print LOGFILEFH $now;
223 # append to the server.input file
224 open(INPUT, ">>log/server$idstr.input") ||
225 logmsg "failed to open log/server$idstr.input\n";
230 # use this, open->print->close system only to make the file
231 # open as little as possible, to make the test suite run
232 # better on windows/cygwin
235 #**********************************************************************
236 # eXsysread is a wrapper around perl's sysread() function. This will
237 # repeat the call to sysread() until it has actually read the complete
238 # number of requested bytes or an unrecoverable condition occurs.
239 # On success returns a positive value, the number of bytes requested.
240 # On failure or timeout returns zero.
246 my $timeout = shift; # A zero timeout disables eXsysread() time limit
248 my $time_limited = 0;
249 my $timeout_rest = 0;
256 if((not defined $nbytes) || ($nbytes < 1)) {
257 logmsg "Error: eXsysread() failure: " .
258 "length argument must be positive\n";
261 if((not defined $timeout) || ($timeout < 0)) {
262 logmsg "Error: eXsysread() failure: " .
263 "timeout argument must be zero or positive\n";
267 # caller sets eXsysread() time limit
269 $timeout_rest = $timeout;
270 $start_time = int(time());
273 while($nread < $nbytes) {
276 local $SIG{ALRM} = sub { die "alarm\n"; };
278 $rc = sysread($FH, $$scalar, $nbytes - $nread, $nread);
281 $timeout_rest = $timeout - (int(time()) - $start_time);
282 if($timeout_rest < 1) {
283 logmsg "Error: eXsysread() failure: timed out\n";
288 $rc = sysread($FH, $$scalar, $nbytes - $nread, $nread);
290 if($got_exit_signal) {
291 logmsg "Error: eXsysread() failure: signalled to die\n";
294 if(not defined $rc) {
296 logmsg "Warning: retrying sysread() interrupted system call\n";
300 logmsg "Warning: retrying sysread() due to EAGAIN\n";
303 if($!{EWOULDBLOCK}) {
304 logmsg "Warning: retrying sysread() due to EWOULDBLOCK\n";
307 logmsg "Error: sysread() failure: $!\n";
311 logmsg "Error: sysread() failure: returned negative value $rc\n";
315 logmsg "Error: sysread() failure: read zero bytes\n";
323 #**********************************************************************
324 # read_mainsockf attempts to read the given amount of output from the
325 # sockfilter which is in use for the main or primary connection. This
326 # reads untranslated sockfilt lingo which may hold data read from the
327 # main or primary socket. On success returns 1, otherwise zero.
332 my $timeout = shift; # Optional argument, if zero blocks indefinitively
335 if(not defined $timeout) {
336 $timeout = $sockfilt_timeout + ($nbytes >> 12);
338 if(eXsysread($FH, $scalar, $nbytes, $timeout) != $nbytes) {
339 my ($fcaller, $lcaller) = (caller)[1,2];
340 logmsg "Error: read_mainsockf() failure at $fcaller " .
341 "line $lcaller. Due to eXsysread() failure\n";
347 #**********************************************************************
348 # read_datasockf attempts to read the given amount of output from the
349 # sockfilter which is in use for the data or secondary connection. This
350 # reads untranslated sockfilt lingo which may hold data read from the
351 # data or secondary socket. On success returns 1, otherwise zero.
356 my $timeout = shift; # Optional argument, if zero blocks indefinitively
359 if(not defined $timeout) {
360 $timeout = $sockfilt_timeout + ($nbytes >> 12);
362 if(eXsysread($FH, $scalar, $nbytes, $timeout) != $nbytes) {
363 my ($fcaller, $lcaller) = (caller)[1,2];
364 logmsg "Error: read_datasockf() failure at $fcaller " .
365 "line $lcaller. Due to eXsysread() failure\n";
379 $result = sysread($$FH, $$scalar, $length);
381 if(not defined $result) {
382 ($fcaller, $lcaller) = (caller)[1,2];
383 logmsg "Failed to read input\n";
384 logmsg "Error: $srvrname server, sysread error: $!\n";
385 logmsg "Exited from sysread_or_die() at $fcaller " .
386 "line $lcaller. $srvrname server, sysread error: $!\n";
387 killsockfilters($proto, $ipvnum, $idnum, $verbose);
389 if($serverlogslocked) {
390 $serverlogslocked = 0;
391 clear_advisor_read_lock($SERVERLOGS_LOCK);
395 elsif($result == 0) {
396 ($fcaller, $lcaller) = (caller)[1,2];
397 logmsg "Failed to read input\n";
398 logmsg "Error: $srvrname server, read zero\n";
399 logmsg "Exited from sysread_or_die() at $fcaller " .
400 "line $lcaller. $srvrname server, read zero\n";
401 killsockfilters($proto, $ipvnum, $idnum, $verbose);
403 if($serverlogslocked) {
404 $serverlogslocked = 0;
405 clear_advisor_read_lock($SERVERLOGS_LOCK);
414 my $mainsockfcmd = "./server/sockfilt " .
415 "--ipv$ipvnum --port $port " .
416 "--pidfile \"$mainsockf_pidfile\" " .
417 "--logfile \"$mainsockf_logfile\"";
418 $sfpid = open2(*SFREAD, *SFWRITE, $mainsockfcmd);
420 print STDERR "$mainsockfcmd\n" if($verbose);
422 print SFWRITE "PING\n";
424 sysread_or_die(\*SFREAD, \$pong, 5);
426 if($pong !~ /^PONG/) {
427 logmsg "Failed sockfilt command: $mainsockfcmd\n";
428 killsockfilters($proto, $ipvnum, $idnum, $verbose);
430 if($serverlogslocked) {
431 $serverlogslocked = 0;
432 clear_advisor_read_lock($SERVERLOGS_LOCK);
434 die "Failed to start sockfilt!";
438 #**********************************************************************
439 # Returns the given test's reply data
445 $testno =~ s/^([^0-9]*)//;
446 if($testno > 10000) {
447 $testpart = $testno % 10000;
448 $testno = int($testno / 10000);
451 loadtest("$srcdir/data/test$testno");
453 my @data = getpart("reply", "data$testpart");
454 if((!@data) && ($testpart ne "")) {
455 @data = getpart("reply", "data");
464 printf SFWRITE "DATA\n%04x\n", length($l);
469 sub sockfiltsecondary {
472 printf DWRITE "DATA\n%04x\n", length($l);
477 #**********************************************************************
478 # Send data to the client on the control stream, which happens to be plain
483 # spit it all out at once
487 my $a = join("", @_);
488 my @a = split("", $a);
492 select(undef, undef, undef, 0.01);
504 #**********************************************************************
505 # Send data to the FTP client on the data stream when data connection
506 # is actually established. Given that this sub should only be called
507 # when a data connection is supposed to be established, calling this
508 # without a data connection is an indication of weak logic somewhere.
512 if($datasockf_conn eq 'no') {
513 logmsg "WARNING: Detected data sending attempt without DATA channel\n";
515 logmsg "WARNING: Data swallowed: $l\n"
522 # spit it all out at once
523 sockfiltsecondary $l;
526 # pause between each byte
528 sockfiltsecondary $_;
529 select(undef, undef, undef, 0.01);
535 #**********************************************************************
536 # protocolsetup initializes the 'displaytext' and 'commandfunc' hashes
537 # for the given protocol. References to protocol command callbacks are
538 # stored in 'commandfunc' hash, and text which will be returned to the
539 # client before the command callback runs is stored in 'displaytext'.
544 if($proto eq 'ftp') {
546 'PORT' => \&PORT_ftp,
547 'EPRT' => \&PORT_ftp,
548 'LIST' => \&LIST_ftp,
549 'NLST' => \&NLST_ftp,
550 'PASV' => \&PASV_ftp,
553 'EPSV' => \&PASV_ftp,
554 'RETR' => \&RETR_ftp,
555 'SIZE' => \&SIZE_ftp,
556 'REST' => \&REST_ftp,
557 'STOR' => \&STOR_ftp,
558 'APPE' => \&STOR_ftp, # append looks like upload
559 'MDTM' => \&MDTM_ftp,
562 'USER' => '331 We are happy you popped in!',
563 'PASS' => '230 Welcome you silly person',
564 'PORT' => '200 You said PORT - I say FINE',
565 'TYPE' => '200 I modify TYPE as you wanted',
566 'LIST' => '150 here comes a directory',
567 'NLST' => '150 here comes a directory',
568 'CWD' => '250 CWD command successful.',
569 'SYST' => '215 UNIX Type: L8', # just fake something
570 'QUIT' => '221 bye bye baby', # just reply something
571 'MKD' => '257 Created your requested directory',
572 'REST' => '350 Yeah yeah we set it there for you',
573 'DELE' => '200 OK OK OK whatever you say',
574 'RNFR' => '350 Received your order. Please provide more',
575 'RNTO' => '250 Ok, thanks. File renaming completed.',
576 'NOOP' => '200 Yes, I\'m very good at doing nothing.',
577 'PBSZ' => '500 PBSZ not implemented',
578 'PROT' => '500 PROT not implemented',
579 'welcome' => join("",
580 '220- _ _ ____ _ '."\r\n",
581 '220- ___| | | | _ \| | '."\r\n",
582 '220- / __| | | | |_) | | '."\r\n",
583 '220- | (__| |_| | _ {| |___ '."\r\n",
584 '220 \___|\___/|_| \_\_____|'."\r\n")
587 elsif($proto eq 'pop3') {
589 'APOP' => \&APOP_pop3,
590 'AUTH' => \&AUTH_pop3,
591 'CAPA' => \&CAPA_pop3,
592 'DELE' => \&DELE_pop3,
593 'LIST' => \&LIST_pop3,
594 'NOOP' => \&NOOP_pop3,
595 'PASS' => \&PASS_pop3,
596 'QUIT' => \&QUIT_pop3,
597 'RETR' => \&RETR_pop3,
598 'RSET' => \&RSET_pop3,
599 'STAT' => \&STAT_pop3,
601 'UIDL' => \&UIDL_pop3,
602 'USER' => \&USER_pop3,
605 'welcome' => join("",
606 ' _ _ ____ _ '."\r\n",
607 ' ___| | | | _ \| | '."\r\n",
608 ' / __| | | | |_) | | '."\r\n",
609 ' | (__| |_| | _ {| |___ '."\r\n",
610 ' \___|\___/|_| \_\_____|'."\r\n",
611 '+OK curl POP3 server ready to serve '."\r\n")
614 elsif($proto eq 'imap') {
616 'APPEND' => \&APPEND_imap,
617 'CAPABILITY' => \&CAPABILITY_imap,
618 'CHECK' => \&CHECK_imap,
619 'CLOSE' => \&CLOSE_imap,
620 'COPY' => \©_imap,
621 'CREATE' => \&CREATE_imap,
622 'DELETE' => \&DELETE_imap,
623 'EXAMINE' => \&EXAMINE_imap,
624 'EXPUNGE' => \&EXPUNGE_imap,
625 'FETCH' => \&FETCH_imap,
626 'LIST' => \&LIST_imap,
627 'LSUB' => \&LSUB_imap,
628 'LOGIN' => \&LOGIN_imap,
629 'LOGOUT' => \&LOGOUT_imap,
630 'NOOP' => \&NOOP_imap,
631 'RENAME' => \&RENAME_imap,
632 'SEARCH' => \&SEARCH_imap,
633 'SELECT' => \&SELECT_imap,
634 'STATUS' => \&STATUS_imap,
635 'STORE' => \&STORE_imap,
639 'welcome' => join("",
640 ' _ _ ____ _ '."\r\n",
641 ' ___| | | | _ \| | '."\r\n",
642 ' / __| | | | |_) | | '."\r\n",
643 ' | (__| |_| | _ {| |___ '."\r\n",
644 ' \___|\___/|_| \_\_____|'."\r\n",
645 '* OK curl IMAP server ready to serve'."\r\n")
648 elsif($proto eq 'smtp') {
650 'DATA' => \&DATA_smtp,
651 'EHLO' => \&EHLO_smtp,
652 'EXPN' => \&EXPN_smtp,
653 'HELO' => \&HELO_smtp,
654 'HELP' => \&HELP_smtp,
655 'MAIL' => \&MAIL_smtp,
656 'NOOP' => \&NOOP_smtp,
657 'RSET' => \&RSET_smtp,
658 'RCPT' => \&RCPT_smtp,
659 'VRFY' => \&VRFY_smtp,
660 'QUIT' => \&QUIT_smtp,
663 'welcome' => join("",
664 '220- _ _ ____ _ '."\r\n",
665 '220- ___| | | | _ \| | '."\r\n",
666 '220- / __| | | | |_) | | '."\r\n",
667 '220- | (__| |_| | _ {| |___ '."\r\n",
668 '220 \___|\___/|_| \_\_____|'."\r\n")
674 my ($closed)=@_; # non-zero if already disconnected
676 my $datapid = processexists($datasockf_pidfile);
678 logmsg "=====> Closing $datasockf_mode DATA connection...\n";
682 logmsg "Server disconnects $datasockf_mode DATA connection\n";
683 print DWRITE "DISC\n";
685 sysread DREAD, $i, 5;
688 logmsg "Server finds $datasockf_mode DATA connection already ".
693 logmsg "Server knows $datasockf_mode DATA connection is already ".
698 print DWRITE "QUIT\n";
699 waitpid($datapid, 0);
700 unlink($datasockf_pidfile) if(-f $datasockf_pidfile);
701 logmsg "DATA sockfilt for $datasockf_mode data channel quits ".
705 logmsg "DATA sockfilt for $datasockf_mode data channel already ".
709 logmsg "=====> Closed $datasockf_mode DATA connection\n";
711 datasockf_state('STOPPED');
715 ################ SMTP commands
718 # The type of server (SMTP or ESMTP)
721 # The client (which normally contains the test number)
728 # TODO: Get the IP address of the client connection to use in the
729 # EHLO response when the client doesn't specify one but for now use
732 $client = "[127.0.0.1]";
735 # Set the server type to ESMTP
736 $smtp_type = "ESMTP";
738 # Calculate the EHLO response
739 push @data, "$smtp_type pingpong test server Hello $client";
741 if((@capabilities) || (@auth_mechs)) {
744 for my $c (@capabilities) {
748 for my $am (@auth_mechs) {
758 push @data, "AUTH $mechs";
762 # Send the EHLO response
763 for(my $i = 0; $i < @data; $i++) {
767 sendcontrol "250-$d\r\n";
770 sendcontrol "250 $d\r\n";
774 # Store the client (as it may contain the test number)
775 $smtp_client = $client;
783 # TODO: Get the IP address of the client connection to use in the HELO
784 # response when the client doesn't specify one but for now use 127.0.0.1
786 $client = "[127.0.0.1]";
789 # Set the server type to SMTP
792 # Send the HELO response
793 sendcontrol "250 $smtp_type pingpong test server Hello $client\r\n";
795 # Store the client (as it may contain the test number)
796 $smtp_client = $client;
804 logmsg "MAIL_smtp got $args\n";
807 sendcontrol "501 Unrecognized parameter\r\n";
812 my @elements = split(/ /, $args);
814 # Get the FROM and SIZE parameters
815 for my $e (@elements) {
816 if($e =~ /^FROM:(.*)$/) {
819 elsif($e =~ /^SIZE=(\d+)$/) {
824 # Validate the from address (only <> and a valid email address inside
825 # <> are allowed, such as <user@example.com>)
826 if ((!$from) || (($from ne "<>") && ($from !~
827 /^<([a-zA-Z0-9._%+-]+)\@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})>$/))) {
828 sendcontrol "501 Invalid address\r\n";
834 # Check the capabilities for SIZE and if the specified size is
835 # greater than the message size then reject it
836 if (@found = grep /^SIZE (\d+)$/, @capabilities) {
837 if ($found[0] =~ /^SIZE (\d+)$/) {
845 sendcontrol "552 Message size too large\r\n";
848 sendcontrol "250 Sender OK\r\n";
859 logmsg "RCPT_smtp got $args\n";
861 # Get the TO parameter
862 if($args !~ /^TO:(.*)/) {
863 sendcontrol "501 Unrecognized parameter\r\n";
868 # Validate the to address (only a valid email address inside <> is
869 # allowed, such as <user@example.com>)
871 /^<([a-zA-Z0-9._%+-]+)\@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})>$/) {
872 sendcontrol "501 Invalid address\r\n";
875 sendcontrol "250 Recipient OK\r\n";
886 sendcontrol "501 Unrecognized parameter\r\n";
888 elsif ($smtp_client !~ /^(\d*)$/) {
889 sendcontrol "501 Invalid arguments\r\n";
892 sendcontrol "354 Show me the mail\r\n";
894 my $testno = $smtp_client;
895 my $filename = "log/upload.$testno";
897 logmsg "Store test number $testno in $filename\n";
899 open(FILE, ">$filename") ||
900 return 0; # failed to open output
906 while (5 == (sysread \*SFREAD, $line, 5)) {
907 if($line eq "DATA\n") {
910 sysread \*SFREAD, $i, 5;
913 if($i =~ /^([0-9a-fA-F]{4})\n/) {
917 read_mainsockf(\$line, $size);
920 print FILE $line if(!$nosave);
923 if($raw =~ /\x0d\x0a\x2e\x0d\x0a/) {
924 # end of data marker!
928 logmsg "> Appending $size bytes to file\n";
931 logmsg "Found SMTP EOB marker\n";
935 elsif($line eq "DISC\n") {
941 logmsg "No support for: $line";
947 print FILE "$ulsize bytes would've been stored here\n";
952 logmsg "received $ulsize bytes upload\n";
954 sendcontrol "250 OK, data received!\r\n";
964 sendcontrol "501 Unrecognized parameter\r\n";
967 sendcontrol "250 OK\r\n";
977 sendcontrol "501 Unrecognized parameter\r\n";
980 sendcontrol "250 Resetting\r\n";
989 # One argument is optional
991 logmsg "HELP_smtp got $args\n";
994 if($smtp_client eq "verifiedserver") {
995 # This is the secret command that verifies that this actually is
996 # the curl test server
997 sendcontrol "214 WE ROOLZ: $$\r\n";
1000 print STDERR "FTPD: We returned proof we are the test server\n";
1003 logmsg "return proof we are we\n";
1006 sendcontrol "214-This server supports the following commands:\r\n";
1009 sendcontrol "214 HELO EHLO RCPT DATA RSET MAIL VRFY EXPN QUIT HELP AUTH\r\n";
1012 sendcontrol "214 HELO EHLO RCPT DATA RSET MAIL VRFY EXPN QUIT HELP\r\n";
1021 my ($username, $address) = split(/ /, $args, 2);
1023 logmsg "VRFY_smtp got $args\n";
1025 if($username eq "") {
1026 sendcontrol "501 Unrecognized parameter\r\n";
1029 my @data = getreplydata($smtp_client);
1040 my ($list_name) = @_;
1042 logmsg "EXPN_smtp got $list_name\n";
1045 sendcontrol "501 Unrecognized parameter\r\n";
1048 my @data = getreplydata($smtp_client);
1059 sendcontrol "221 curl $smtp_type server signing off\r\n";
1064 # What was deleted by IMAP STORE / POP3 DELE commands
1068 ################ IMAP commands
1071 # global to allow the command functions to read it
1074 # what was picked by SELECT
1077 # Any IMAP parameter can come in escaped and in double quotes.
1078 # This function is dumb (so far) and just removes the quotes if present.
1079 sub fix_imap_params {
1081 $_ = $1 if /^"(.*)"$/;
1085 sub CAPABILITY_imap {
1086 if((!@capabilities) && (!@auth_mechs)) {
1087 sendcontrol "$cmdid BAD Command\r\n";
1092 # Calculate the CAPABILITY response
1093 $data = "* CAPABILITY IMAP4";
1095 for my $c (@capabilities) {
1099 for my $am (@auth_mechs) {
1100 $data .= " AUTH=$am";
1103 $data .= " pingpong test server\r\n";
1105 # Send the CAPABILITY response
1107 sendcontrol "$cmdid OK CAPABILITY completed\r\n";
1115 my ($user, $password) = split(/ /, $args, 2);
1116 fix_imap_params($user, $password);
1118 logmsg "LOGIN_imap got $args\n";
1121 sendcontrol "$cmdid BAD Command Argument\r\n";
1124 sendcontrol "$cmdid OK LOGIN completed\r\n";
1132 fix_imap_params($mailbox);
1134 logmsg "SELECT_imap got test $mailbox\n";
1136 if($mailbox eq "") {
1137 sendcontrol "$cmdid BAD Command Argument\r\n";
1140 # Example from RFC 3501, 6.3.1. SELECT Command
1141 sendcontrol "* 172 EXISTS\r\n";
1142 sendcontrol "* 1 RECENT\r\n";
1143 sendcontrol "* OK [UNSEEN 12] Message 12 is first unseen\r\n";
1144 sendcontrol "* OK [UIDVALIDITY 3857529045] UIDs valid\r\n";
1145 sendcontrol "* OK [UIDNEXT 4392] Predicted next UID\r\n";
1146 sendcontrol "* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)\r\n";
1147 sendcontrol "* OK [PERMANENTFLAGS (\\Deleted \\Seen \\*)] Limited\r\n";
1148 sendcontrol "$cmdid OK [READ-WRITE] SELECT completed\r\n";
1150 $selected = $mailbox;
1158 my ($uid, $how) = split(/ /, $args, 2);
1159 fix_imap_params($uid, $how);
1161 logmsg "FETCH_imap got $args\n";
1163 if ($selected eq "") {
1164 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1170 if($selected eq "verifiedserver") {
1171 # this is the secret command that verifies that this actually is
1172 # the curl test server
1173 my $response = "WE ROOLZ: $$\r\n";
1175 print STDERR "FTPD: We returned proof we are the test server\n";
1177 $data[0] = $response;
1178 logmsg "return proof we are we\n";
1182 logmsg "retrieve a mail\n";
1184 @data = getreplydata($selected);
1188 $size += length($_);
1191 sendcontrol "* $uid FETCH ($how {$size}\r\n";
1197 sendcontrol ")\r\n";
1198 sendcontrol "$cmdid OK FETCH completed\r\n";
1207 logmsg "APPEND_imap got $args\r\n";
1209 $args =~ /^([^ ]+) [^{]*\{(\d+)\}$/;
1210 my ($mailbox, $size) = ($1, $2);
1211 fix_imap_params($mailbox);
1213 if($mailbox eq "") {
1214 sendcontrol "$cmdid BAD Command Argument\r\n";
1217 sendcontrol "+ Ready for literal data\r\n";
1219 my $testno = $mailbox;
1220 my $filename = "log/upload.$testno";
1222 logmsg "Store test number $testno in $filename\n";
1224 open(FILE, ">$filename") ||
1225 return 0; # failed to open output
1229 while(5 == (sysread \*SFREAD, $line, 5)) {
1230 if($line eq "DATA\n") {
1231 sysread \*SFREAD, $line, 5;
1234 if($line =~ /^([0-9a-fA-F]{4})\n/) {
1235 $chunksize = hex($1);
1238 read_mainsockf(\$line, $chunksize);
1240 my $left = $size - $received;
1241 my $datasize = ($left > $chunksize) ? $chunksize : $left;
1244 logmsg "> Appending $datasize bytes to file\n";
1245 print FILE substr($line, 0, $datasize) if(!$nosave);
1246 $line = substr($line, $datasize);
1248 $received += $datasize;
1249 if($received == $size) {
1250 logmsg "Received all data, waiting for final CRLF.\n";
1254 if($received == $size && $line eq "\r\n") {
1258 elsif($line eq "DISC\n") {
1259 logmsg "Unexpected disconnect!\n";
1263 logmsg "No support for: $line";
1269 print FILE "$size bytes would've been stored here\n";
1274 logmsg "received $size bytes upload\n";
1276 sendcontrol "$cmdid OK APPEND completed\r\n";
1284 my ($uid, $what, $value) = split(/ /, $args, 3);
1285 fix_imap_params($uid);
1287 logmsg "STORE_imap got $args\n";
1289 if ($selected eq "") {
1290 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1292 elsif (($uid eq "") || ($what ne "+Flags") || ($value eq "")) {
1293 sendcontrol "$cmdid BAD Command Argument\r\n";
1296 if($value eq "\\Deleted") {
1297 push(@deleted, $uid);
1300 sendcontrol "* $uid FETCH (FLAGS (\\Seen $value))\r\n";
1301 sendcontrol "$cmdid OK STORE completed\r\n";
1309 my ($reference, $mailbox) = split(/ /, $args, 2);
1310 fix_imap_params($reference, $mailbox);
1312 logmsg "LIST_imap got $args\n";
1314 if ($reference eq "") {
1315 sendcontrol "$cmdid BAD Command Argument\r\n";
1317 elsif ($reference eq "verifiedserver") {
1318 # this is the secret command that verifies that this actually is
1319 # the curl test server
1320 sendcontrol "* LIST () \"/\" \"WE ROOLZ: $$\"\r\n";
1321 sendcontrol "$cmdid OK LIST Completed\r\n";
1324 print STDERR "FTPD: We returned proof we are the test server\n";
1327 logmsg "return proof we are we\n";
1330 my @data = getreplydata($reference);
1336 sendcontrol "$cmdid OK LIST Completed\r\n";
1344 my ($reference, $mailbox) = split(/ /, $args, 2);
1345 fix_imap_params($reference, $mailbox);
1347 logmsg "LSUB_imap got $args\n";
1349 if ($reference eq "") {
1350 sendcontrol "$cmdid BAD Command Argument\r\n";
1353 my @data = getreplydata($reference);
1359 sendcontrol "$cmdid OK LSUB Completed\r\n";
1367 fix_imap_params($mailbox);
1369 logmsg "EXAMINE_imap got $mailbox\n";
1371 if ($mailbox eq "") {
1372 sendcontrol "$cmdid BAD Command Argument\r\n";
1375 my @data = getreplydata($mailbox);
1381 sendcontrol "$cmdid OK [READ-ONLY] EXAMINE completed\r\n";
1389 my ($mailbox, $what) = split(/ /, $args, 2);
1390 fix_imap_params($mailbox);
1392 logmsg "STATUS_imap got $args\n";
1394 if ($mailbox eq "") {
1395 sendcontrol "$cmdid BAD Command Argument\r\n";
1398 my @data = getreplydata($mailbox);
1404 sendcontrol "$cmdid OK STATUS completed\r\n";
1412 fix_imap_params($what);
1414 logmsg "SEARCH_imap got $what\n";
1416 if ($selected eq "") {
1417 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1419 elsif ($what eq "") {
1420 sendcontrol "$cmdid BAD Command Argument\r\n";
1423 my @data = getreplydata($selected);
1429 sendcontrol "$cmdid OK SEARCH completed\r\n";
1437 fix_imap_params($args);
1439 logmsg "CREATE_imap got $args\n";
1442 sendcontrol "$cmdid BAD Command Argument\r\n";
1445 sendcontrol "$cmdid OK CREATE completed\r\n";
1453 fix_imap_params($args);
1455 logmsg "DELETE_imap got $args\n";
1458 sendcontrol "$cmdid BAD Command Argument\r\n";
1461 sendcontrol "$cmdid OK DELETE completed\r\n";
1469 my ($from_mailbox, $to_mailbox) = split(/ /, $args, 2);
1470 fix_imap_params($from_mailbox, $to_mailbox);
1472 logmsg "RENAME_imap got $args\n";
1474 if (($from_mailbox eq "") || ($to_mailbox eq "")) {
1475 sendcontrol "$cmdid BAD Command Argument\r\n";
1478 sendcontrol "$cmdid OK RENAME completed\r\n";
1485 if ($selected eq "") {
1486 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1489 sendcontrol "$cmdid OK CHECK completed\r\n";
1496 if ($selected eq "") {
1497 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1500 sendcontrol "$cmdid BAD Command Argument\r\n";
1503 sendcontrol "$cmdid OK CLOSE completed\r\n";
1512 if ($selected eq "") {
1513 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1517 # Report the number of existing messages as per the SELECT
1519 sendcontrol "* 172 EXISTS\r\n";
1522 # Report the message UIDs being deleted
1523 for my $d (@deleted) {
1524 sendcontrol "* $d EXPUNGE\r\n";
1530 sendcontrol "$cmdid OK EXPUNGE completed\r\n";
1538 my ($uid, $mailbox) = split(/ /, $args, 2);
1539 fix_imap_params($uid, $mailbox);
1541 logmsg "COPY_imap got $args\n";
1543 if (($uid eq "") || ($mailbox eq "")) {
1544 sendcontrol "$cmdid BAD Command Argument\r\n";
1547 sendcontrol "$cmdid OK COPY completed\r\n";
1555 my ($command) = split(/ /, $args, 1);
1556 fix_imap_params($command);
1558 logmsg "UID_imap got $args\n";
1560 if ($selected eq "") {
1561 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1563 elsif (($command ne "COPY") && ($command ne "FETCH") &&
1564 ($command ne "STORE") && ($command ne "SEARCH")) {
1565 sendcontrol "$cmdid BAD Command Argument\r\n";
1568 my @data = getreplydata($selected);
1574 sendcontrol "$cmdid OK $command completed\r\n";
1586 "* 14 FETCH (FLAGS (\\Seen \\Deleted))\r\n",
1590 sendcontrol "$cmdid BAD Command Argument\r\n";
1597 sendcontrol "$cmdid OK NOOP completed\r\n";
1604 sendcontrol "* BYE curl IMAP server signing off\r\n";
1605 sendcontrol "$cmdid OK LOGOUT completed\r\n";
1611 ################ POP3 commands
1614 # Who is attempting to log in
1621 # Calculate the capability list based on the specified capabilities
1622 # (except APOP) and any authentication mechanisms
1623 for my $c (@capabilities) {
1624 push @list, "$c\r\n" unless $c eq "APOP";
1627 for my $am (@auth_mechs) {
1637 push @list, "SASL $mechs\r\n";
1641 sendcontrol "-ERR Unrecognized command\r\n";
1646 # Calculate the CAPA response
1647 push @data, "+OK List of capabilities follows\r\n";
1650 push @data, "$l\r\n";
1653 push @data, "IMPLEMENTATION POP3 pingpong test server\r\n";
1655 # Send the CAPA response
1660 # End with the magic 3-byte end of listing marker
1661 sendcontrol ".\r\n";
1669 my ($user, $secret) = split(/ /, $args, 2);
1671 if (!grep /^APOP$/, @capabilities) {
1672 sendcontrol "-ERR Unrecognized command\r\n";
1674 elsif (($user eq "") || ($secret eq "")) {
1675 sendcontrol "-ERR Protocol error\r\n";
1678 my $digest = Digest::MD5::md5_hex($POP3_TIMESTAMP, $TEXT_PASSWORD);
1680 if ($secret ne $digest) {
1681 sendcontrol "-ERR Login failure\r\n";
1684 sendcontrol "+OK Login successful\r\n";
1693 sendcontrol "-ERR Unrecognized command\r\n";
1698 # Calculate the AUTH response
1699 push @data, "+OK List of supported mechanisms follows\r\n";
1701 for my $am (@auth_mechs) {
1702 push @data, "$am\r\n";
1705 # Send the AUTH response
1710 # End with the magic 3-byte end of listing marker
1711 sendcontrol ".\r\n";
1720 logmsg "USER_pop3 got $user\n";
1723 sendcontrol "-ERR Protocol error\r\n";
1728 sendcontrol "+OK\r\n";
1735 my ($password) = @_;
1737 logmsg "PASS_pop3 got $password\n";
1739 sendcontrol "+OK Login successful\r\n";
1748 if($msgid =~ /^verifiedserver$/) {
1749 # this is the secret command that verifies that this actually is
1750 # the curl test server
1751 my $response = "WE ROOLZ: $$\r\n";
1753 print STDERR "FTPD: We returned proof we are the test server\n";
1755 $data[0] = $response;
1756 logmsg "return proof we are we\n";
1760 logmsg "retrieve a mail\n";
1762 @data = getreplydata($msgid);
1765 sendcontrol "+OK Mail transfer starts\r\n";
1771 # end with the magic 3-byte end of mail marker, assumes that the
1772 # mail body ends with a CRLF!
1773 sendcontrol ".\r\n";
1779 # This is a built-in fake-message list
1782 "2 4294967400\r\n", # > 4 GB
1786 logmsg "retrieve a message list\n";
1788 sendcontrol "+OK Listing starts\r\n";
1794 # End with the magic 3-byte end of listing marker
1795 sendcontrol ".\r\n";
1803 logmsg "DELE_pop3 got $msgid\n";
1806 sendcontrol "-ERR Protocol error\r\n";
1809 push (@deleted, $msgid);
1811 sendcontrol "+OK\r\n";
1821 sendcontrol "-ERR Protocol error\r\n";
1824 # Send statistics for the built-in fake message list as
1825 # detailed in the LIST_pop3 function above
1826 sendcontrol "+OK 3 4294967800\r\n";
1836 sendcontrol "-ERR Protocol error\r\n";
1839 sendcontrol "+OK\r\n";
1846 # This is a built-in fake-message UID list
1850 "3 4\r\n", # Note that UID 3 is a simulated "deleted" message
1853 if (!grep /^UIDL$/, @capabilities) {
1854 sendcontrol "-ERR Unrecognized command\r\n";
1857 logmsg "retrieve a message UID list\n";
1859 sendcontrol "+OK Listing starts\r\n";
1865 # End with the magic 3-byte end of listing marker
1866 sendcontrol ".\r\n";
1874 my ($msgid, $lines) = split(/ /, $args, 2);
1876 logmsg "TOP_pop3 got $args\n";
1878 if (!grep /^TOP$/, @capabilities) {
1879 sendcontrol "-ERR Unrecognized command\r\n";
1881 elsif (($msgid eq "") || ($lines eq "")) {
1882 sendcontrol "-ERR Protocol error\r\n";
1885 if ($lines == "0") {
1886 logmsg "retrieve header of mail\n";
1889 logmsg "retrieve top $lines lines of mail\n";
1892 my @data = getreplydata($msgid);
1894 sendcontrol "+OK Mail transfer starts\r\n";
1901 # End with the magic 3-byte end of mail marker, assumes that the
1902 # mail body ends with a CRLF!
1903 sendcontrol ".\r\n";
1913 sendcontrol "-ERR Protocol error\r\n";
1917 logmsg "resetting @deleted message(s)\n";
1922 sendcontrol "+OK\r\n";
1930 logmsg "deleting @deleted message(s)\n";
1935 sendcontrol "+OK curl POP3 server signing off\r\n";
1941 ################ FTP commands
1946 logmsg "Set REST position to $rest\n"
1949 sub switch_directory_goto {
1950 my $target_dir = $_;
1952 if(!$ftptargetdir) {
1953 $ftptargetdir = "/";
1956 if($target_dir eq "") {
1957 $ftptargetdir = "/";
1959 elsif($target_dir eq "..") {
1960 if($ftptargetdir eq "/") {
1961 $ftptargetdir = "/";
1964 $ftptargetdir =~ s/[[:alnum:]]+\/$//;
1968 $ftptargetdir .= $target_dir . "/";
1972 sub switch_directory {
1973 my $target_dir = $_[0];
1975 if($target_dir =~ /^test-(\d+)/) {
1978 elsif($target_dir eq "/") {
1979 $ftptargetdir = "/";
1982 my @dirs = split("/", $target_dir);
1984 switch_directory_goto($_);
1990 my ($folder, $fullcommand) = $_[0];
1991 switch_directory($folder);
1992 if($ftptargetdir =~ /^\/fully_simulated/) {
1993 $ftplistparserstate = "enabled";
1996 undef $ftplistparserstate;
2002 $mydir = $ftptargetdir ? $ftptargetdir : "/";
2007 sendcontrol "257 \"$mydir\" is current directory\r\n";
2011 # print "150 ASCII data connection for /bin/ls (193.15.23.1,59196) (0 bytes)\r\n";
2013 # this is a built-in fake-dir ;-)
2014 my @ftpdir=("total 20\r\n",
2015 "drwxr-xr-x 8 98 98 512 Oct 22 13:06 .\r\n",
2016 "drwxr-xr-x 8 98 98 512 Oct 22 13:06 ..\r\n",
2017 "drwxr-xr-x 2 98 98 512 May 2 1996 .NeXT\r\n",
2018 "-r--r--r-- 1 0 1 35 Jul 16 1996 README\r\n",
2019 "lrwxrwxrwx 1 0 1 7 Dec 9 1999 bin -> usr/bin\r\n",
2020 "dr-xr-xr-x 2 0 1 512 Oct 1 1997 dev\r\n",
2021 "drwxrwxrwx 2 98 98 512 May 29 16:04 download.html\r\n",
2022 "dr-xr-xr-x 2 0 1 512 Nov 30 1995 etc\r\n",
2023 "drwxrwxrwx 2 98 1 512 Oct 30 14:33 pub\r\n",
2024 "dr-xr-xr-x 5 0 1 512 Oct 1 1997 usr\r\n");
2026 if($datasockf_conn eq 'no') {
2027 if($nodataconn425) {
2028 sendcontrol "150 Opening data connection\r\n";
2029 sendcontrol "425 Can't open data connection\r\n";
2031 elsif($nodataconn421) {
2032 sendcontrol "150 Opening data connection\r\n";
2033 sendcontrol "421 Connection timed out\r\n";
2035 elsif($nodataconn150) {
2036 sendcontrol "150 Opening data connection\r\n";
2037 # client shall timeout
2040 # client shall timeout
2045 if($ftplistparserstate) {
2046 @ftpdir = ftp_contentlist($ftptargetdir);
2049 logmsg "pass LIST data on data connection\n";
2052 loadtest("$srcdir/data/test$cwd_testno");
2054 my @data = getpart("reply", "data");
2057 # convert all \n to \r\n for ASCII transfer
2058 $send =~ s/\r\n/\n/g;
2059 $send =~ s/\n/\r\n/g;
2060 logmsg "send $send as data\n";
2063 $cwd_testno = 0; # forget it again
2066 # old hard-coded style
2072 sendcontrol "226 ASCII transfer complete\r\n";
2077 my @ftpdir=("file", "with space", "fake", "..", " ..", "funny", "README");
2079 if($datasockf_conn eq 'no') {
2080 if($nodataconn425) {
2081 sendcontrol "150 Opening data connection\r\n";
2082 sendcontrol "425 Can't open data connection\r\n";
2084 elsif($nodataconn421) {
2085 sendcontrol "150 Opening data connection\r\n";
2086 sendcontrol "421 Connection timed out\r\n";
2088 elsif($nodataconn150) {
2089 sendcontrol "150 Opening data connection\r\n";
2090 # client shall timeout
2093 # client shall timeout
2098 logmsg "pass NLST data on data connection\n";
2103 sendcontrol "226 ASCII transfer complete\r\n";
2110 if ($testno > 10000) {
2111 $testpart = $testno % 10000;
2112 $testno = int($testno / 10000);
2115 loadtest("$srcdir/data/test$testno");
2117 my @data = getpart("reply", "mdtm");
2119 my $reply = $data[0];
2120 chomp $reply if($reply);
2122 if($reply && ($reply =~ /^[+-]?\d+$/) && ($reply < 0)) {
2123 sendcontrol "550 $testno: no such file.\r\n";
2126 sendcontrol "$reply\r\n";
2129 sendcontrol "500 MDTM: no such command.\r\n";
2136 if($ftplistparserstate) {
2137 my $size = wildcard_filesize($ftptargetdir, $testno);
2139 sendcontrol "550 $testno: No such file or directory.\r\n";
2142 sendcontrol "213 $size\r\n";
2147 if($testno =~ /^verifiedserver$/) {
2148 my $response = "WE ROOLZ: $$\r\n";
2149 my $size = length($response);
2150 sendcontrol "213 $size\r\n";
2154 if($testno =~ /(\d+)\/?$/) {
2158 print STDERR "SIZE_ftp: invalid test number: $testno\n";
2163 if($testno > 10000) {
2164 $testpart = $testno % 10000;
2165 $testno = int($testno / 10000);
2168 loadtest("$srcdir/data/test$testno");
2170 my @data = getpart("reply", "size");
2172 my $size = $data[0];
2176 sendcontrol "213 $size\r\n";
2179 sendcontrol "550 $testno: No such file or directory.\r\n";
2184 @data = getpart("reply", "data$testpart");
2186 $size += length($_);
2189 sendcontrol "213 $size\r\n";
2192 sendcontrol "550 $testno: No such file or directory.\r\n";
2201 if($datasockf_conn eq 'no') {
2202 if($nodataconn425) {
2203 sendcontrol "150 Opening data connection\r\n";
2204 sendcontrol "425 Can't open data connection\r\n";
2206 elsif($nodataconn421) {
2207 sendcontrol "150 Opening data connection\r\n";
2208 sendcontrol "421 Connection timed out\r\n";
2210 elsif($nodataconn150) {
2211 sendcontrol "150 Opening data connection\r\n";
2212 # client shall timeout
2215 # client shall timeout
2220 if($ftplistparserstate) {
2221 my @content = wildcard_getfile($ftptargetdir, $testno);
2222 if($content[0] == -1) {
2226 my $size = length $content[1];
2227 sendcontrol "150 Binary data connection for $testno ($size bytes).\r\n",
2228 senddata $content[1];
2230 sendcontrol "226 File transfer complete\r\n";
2235 if($testno =~ /^verifiedserver$/) {
2236 # this is the secret command that verifies that this actually is
2237 # the curl test server
2238 my $response = "WE ROOLZ: $$\r\n";
2239 my $len = length($response);
2240 sendcontrol "150 Binary junk ($len bytes).\r\n";
2241 senddata "WE ROOLZ: $$\r\n";
2243 sendcontrol "226 File transfer complete\r\n";
2245 print STDERR "FTPD: We returned proof we are the test server\n";
2250 $testno =~ s/^([^0-9]*)//;
2252 if ($testno > 10000) {
2253 $testpart = $testno % 10000;
2254 $testno = int($testno / 10000);
2257 loadtest("$srcdir/data/test$testno");
2259 my @data = getpart("reply", "data$testpart");
2263 $size += length($_);
2266 my %hash = getpartattr("reply", "data$testpart");
2268 if($size || $hash{'sendzero'}) {
2271 # move read pointer forward
2273 logmsg "REST $rest was removed from size, makes $size left\n";
2274 $rest = 0; # reset REST offset again
2277 sendcontrol "150 Binary data connection for $testno () ($size bytes).\r\n",
2278 "226 File transfer complete\r\n";
2285 $retrweirdo=0; # switch off the weirdo again!
2288 my $sz = "($size bytes)";
2293 sendcontrol "150 Binary data connection for $testno () $sz.\r\n";
2300 sendcontrol "226 File transfer complete\r\n";
2304 sendcontrol "550 $testno: No such file or directory.\r\n";
2312 my $filename = "log/upload.$testno";
2314 if($datasockf_conn eq 'no') {
2315 if($nodataconn425) {
2316 sendcontrol "150 Opening data connection\r\n";
2317 sendcontrol "425 Can't open data connection\r\n";
2319 elsif($nodataconn421) {
2320 sendcontrol "150 Opening data connection\r\n";
2321 sendcontrol "421 Connection timed out\r\n";
2323 elsif($nodataconn150) {
2324 sendcontrol "150 Opening data connection\r\n";
2325 # client shall timeout
2328 # client shall timeout
2333 logmsg "STOR test number $testno in $filename\n";
2335 sendcontrol "125 Gimme gimme gimme!\r\n";
2337 open(FILE, ">$filename") ||
2338 return 0; # failed to open output
2343 while (5 == (sysread DREAD, $line, 5)) {
2344 if($line eq "DATA\n") {
2346 sysread DREAD, $i, 5;
2349 if($i =~ /^([0-9a-fA-F]{4})\n/) {
2353 read_datasockf(\$line, $size);
2355 #print STDERR " GOT: $size bytes\n";
2358 print FILE $line if(!$nosave);
2359 logmsg "> Appending $size bytes to file\n";
2361 elsif($line eq "DISC\n") {
2367 logmsg "No support for: $line";
2372 print FILE "$ulsize bytes would've been stored here\n";
2375 close_dataconn($disc);
2376 logmsg "received $ulsize bytes upload\n";
2377 sendcontrol "226 File transfer complete\r\n";
2384 my $bindonly = ($nodataconn) ? '--bindonly' : '';
2386 # kill previous data connection sockfilt when alive
2387 if($datasockf_runs eq 'yes') {
2388 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2389 logmsg "DATA sockfilt for $datasockf_mode data channel killed\n";
2391 datasockf_state('STOPPED');
2393 logmsg "====> Passive DATA channel requested by client\n";
2395 logmsg "DATA sockfilt for passive data channel starting...\n";
2397 # We fire up a new sockfilt to do the data transfer for us.
2398 my $datasockfcmd = "./server/sockfilt " .
2399 "--ipv$ipvnum $bindonly --port 0 " .
2400 "--pidfile \"$datasockf_pidfile\" " .
2401 "--logfile \"$datasockf_logfile\"";
2402 $slavepid = open2(\*DREAD, \*DWRITE, $datasockfcmd);
2405 datasockf_state('PASSIVE_NODATACONN');
2408 datasockf_state('PASSIVE');
2411 print STDERR "$datasockfcmd\n" if($verbose);
2413 print DWRITE "PING\n";
2415 sysread_or_die(\*DREAD, \$pong, 5);
2417 if($pong =~ /^FAIL/) {
2418 logmsg "DATA sockfilt said: FAIL\n";
2419 logmsg "DATA sockfilt for passive data channel failed\n";
2420 logmsg "DATA sockfilt not running\n";
2421 datasockf_state('STOPPED');
2422 sendcontrol "500 no free ports!\r\n";
2425 elsif($pong !~ /^PONG/) {
2426 logmsg "DATA sockfilt unexpected response: $pong\n";
2427 logmsg "DATA sockfilt for passive data channel failed\n";
2428 logmsg "DATA sockfilt killed now\n";
2429 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2430 logmsg "DATA sockfilt not running\n";
2431 datasockf_state('STOPPED');
2432 sendcontrol "500 no free ports!\r\n";
2436 logmsg "DATA sockfilt for passive data channel started (pid $slavepid)\n";
2438 # Find out on what port we listen on or have bound
2440 print DWRITE "PORT\n";
2442 # READ the response code
2443 sysread_or_die(\*DREAD, \$i, 5);
2445 # READ the response size
2446 sysread_or_die(\*DREAD, \$i, 5);
2449 if($i =~ /^([0-9a-fA-F]{4})\n/) {
2453 # READ the response data
2454 read_datasockf(\$i, $size);
2456 # The data is in the format
2459 if($i =~ /IPv(\d)\/(\d+)/) {
2460 # FIX: deal with IP protocol version
2465 logmsg "DATA sockfilt unknown listener port\n";
2466 logmsg "DATA sockfilt for passive data channel failed\n";
2467 logmsg "DATA sockfilt killed now\n";
2468 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2469 logmsg "DATA sockfilt not running\n";
2470 datasockf_state('STOPPED');
2471 sendcontrol "500 no free ports!\r\n";
2476 my $str = nodataconn_str();
2477 logmsg "DATA sockfilt for passive data channel ($str) bound on port ".
2481 logmsg "DATA sockfilt for passive data channel listens on port ".
2485 if($cmd ne "EPSV") {
2492 sendcontrol sprintf("227 Entering Passive Mode ($p,%d,%d)\n",
2493 int($pasvport/256), int($pasvport%256));
2497 sendcontrol sprintf("229 Entering Passive Mode (|||%d|)\n", $pasvport);
2500 logmsg "Client has been notified that DATA conn ".
2501 "will be accepted on port $pasvport\n";
2504 my $str = nodataconn_str();
2505 logmsg "====> Client fooled ($str)\n";
2510 local $SIG{ALRM} = sub { die "alarm\n" };
2512 # assume swift operations unless explicitly slow
2513 alarm ($datadelay?20:10);
2518 # FIX: Monitor ctrl conn for disconnect
2520 while(sysread(DREAD, $input, 5)) {
2522 if($input !~ /^CNCT/) {
2523 # we wait for a connected client
2524 logmsg "Odd, we got $input from client\n";
2527 logmsg "Client connects to port $pasvport\n";
2534 logmsg "$srvrname server timed out awaiting data connection ".
2535 "on port $pasvport\n";
2536 logmsg "accept failed or connection not even attempted\n";
2537 logmsg "DATA sockfilt killed now\n";
2538 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2539 logmsg "DATA sockfilt not running\n";
2540 datasockf_state('STOPPED');
2544 logmsg "====> Client established passive DATA connection ".
2545 "on port $pasvport\n";
2552 # Support both PORT and EPRT here.
2556 my ($arg, $cmd) = @_;
2560 # kill previous data connection sockfilt when alive
2561 if($datasockf_runs eq 'yes') {
2562 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2563 logmsg "DATA sockfilt for $datasockf_mode data channel killed\n";
2565 datasockf_state('STOPPED');
2567 logmsg "====> Active DATA channel requested by client\n";
2569 # We always ignore the given IP and use localhost.
2571 if($cmd eq "PORT") {
2572 if($arg !~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {
2573 logmsg "DATA sockfilt for active data channel not started ".
2574 "(bad PORT-line: $arg)\n";
2575 sendcontrol "500 silly you, go away\r\n";
2579 $addr = "$1.$2.$3.$4";
2581 # EPRT |2|::1|49706|
2582 elsif($cmd eq "EPRT") {
2583 if($arg !~ /(\d+)\|([^\|]+)\|(\d+)/) {
2584 logmsg "DATA sockfilt for active data channel not started ".
2585 "(bad EPRT-line: $arg)\n";
2586 sendcontrol "500 silly you, go away\r\n";
2589 sendcontrol "200 Thanks for dropping by. We contact you later\r\n";
2594 logmsg "DATA sockfilt for active data channel not started ".
2595 "(invalid command: $cmd)\n";
2596 sendcontrol "500 we don't like $cmd now\r\n";
2600 if(!$port || $port > 65535) {
2601 logmsg "DATA sockfilt for active data channel not started ".
2602 "(illegal PORT number: $port)\n";
2607 my $str = nodataconn_str();
2608 logmsg "DATA sockfilt for active data channel not started ($str)\n";
2609 datasockf_state('ACTIVE_NODATACONN');
2610 logmsg "====> Active DATA channel not established\n";
2614 logmsg "DATA sockfilt for active data channel starting...\n";
2616 # We fire up a new sockfilt to do the data transfer for us.
2617 my $datasockfcmd = "./server/sockfilt " .
2618 "--ipv$ipvnum --connect $port --addr \"$addr\" " .
2619 "--pidfile \"$datasockf_pidfile\" " .
2620 "--logfile \"$datasockf_logfile\"";
2621 $slavepid = open2(\*DREAD, \*DWRITE, $datasockfcmd);
2623 datasockf_state('ACTIVE');
2625 print STDERR "$datasockfcmd\n" if($verbose);
2627 print DWRITE "PING\n";
2629 sysread_or_die(\*DREAD, \$pong, 5);
2631 if($pong =~ /^FAIL/) {
2632 logmsg "DATA sockfilt said: FAIL\n";
2633 logmsg "DATA sockfilt for active data channel failed\n";
2634 logmsg "DATA sockfilt not running\n";
2635 datasockf_state('STOPPED');
2636 # client shall timeout awaiting connection from server
2639 elsif($pong !~ /^PONG/) {
2640 logmsg "DATA sockfilt unexpected response: $pong\n";
2641 logmsg "DATA sockfilt for active data channel failed\n";
2642 logmsg "DATA sockfilt killed now\n";
2643 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2644 logmsg "DATA sockfilt not running\n";
2645 datasockf_state('STOPPED');
2646 # client shall timeout awaiting connection from server
2650 logmsg "DATA sockfilt for active data channel started (pid $slavepid)\n";
2652 logmsg "====> Active DATA channel connected to client port $port\n";
2657 #**********************************************************************
2658 # datasockf_state is used to change variables that keep state info
2659 # relative to the FTP secondary or data sockfilt process as soon as
2660 # one of the five possible stable states is reached. Variables that
2661 # are modified by this sub may be checked independently but should
2662 # not be changed except by calling this sub.
2664 sub datasockf_state {
2667 if($state eq 'STOPPED') {
2668 # Data sockfilter initial state, not running,
2669 # not connected and not used.
2670 $datasockf_state = $state;
2671 $datasockf_mode = 'none';
2672 $datasockf_runs = 'no';
2673 $datasockf_conn = 'no';
2675 elsif($state eq 'PASSIVE') {
2676 # Data sockfilter accepted connection from client.
2677 $datasockf_state = $state;
2678 $datasockf_mode = 'passive';
2679 $datasockf_runs = 'yes';
2680 $datasockf_conn = 'yes';
2682 elsif($state eq 'ACTIVE') {
2683 # Data sockfilter has connected to client.
2684 $datasockf_state = $state;
2685 $datasockf_mode = 'active';
2686 $datasockf_runs = 'yes';
2687 $datasockf_conn = 'yes';
2689 elsif($state eq 'PASSIVE_NODATACONN') {
2690 # Data sockfilter bound port without listening,
2691 # client won't be able to establish data connection.
2692 $datasockf_state = $state;
2693 $datasockf_mode = 'passive';
2694 $datasockf_runs = 'yes';
2695 $datasockf_conn = 'no';
2697 elsif($state eq 'ACTIVE_NODATACONN') {
2698 # Data sockfilter does not even run,
2699 # client awaits data connection from server in vain.
2700 $datasockf_state = $state;
2701 $datasockf_mode = 'active';
2702 $datasockf_runs = 'no';
2703 $datasockf_conn = 'no';
2706 die "Internal error. Unknown datasockf state: $state!";
2710 #**********************************************************************
2711 # nodataconn_str returns string of efective nodataconn command. Notice
2712 # that $nodataconn may be set alone or in addition to a $nodataconnXXX.
2714 sub nodataconn_str {
2717 $str = 'NODATACONN' if($nodataconn);
2718 $str = 'NODATACONN425' if($nodataconn425);
2719 $str = 'NODATACONN421' if($nodataconn421);
2720 $str = 'NODATACONN150' if($nodataconn150);
2724 #**********************************************************************
2725 # customize configures test server operation for each curl test, reading
2726 # configuration commands/parameters from server commands file each time
2727 # a new client control connection is established with the test server.
2728 # On success returns 1, otherwise zero.
2731 $ctrldelay = 0; # default is no throttling of the ctrl stream
2732 $datadelay = 0; # default is no throttling of the data stream
2733 $retrweirdo = 0; # default is no use of RETRWEIRDO
2734 $retrnosize = 0; # default is no use of RETRNOSIZE
2735 $pasvbadip = 0; # default is no use of PASVBADIP
2736 $nosave = 0; # default is to actually save uploaded data to file
2737 $nodataconn = 0; # default is to establish or accept data channel
2738 $nodataconn425 = 0; # default is to not send 425 without data channel
2739 $nodataconn421 = 0; # default is to not send 421 without data channel
2740 $nodataconn150 = 0; # default is to not send 150 without data channel
2741 @capabilities = (); # default is to not support capability commands
2742 @auth_mechs = (); # default is to not support authentication commands
2743 %fulltextreply = ();#
2744 %commandreply = (); #
2745 %customcount = (); #
2748 open(CUSTOM, "<log/ftpserver.cmd") ||
2751 logmsg "FTPD: Getting commands from log/ftpserver.cmd\n";
2754 if($_ =~ /REPLY \"([A-Z]+ [A-Za-z0-9+-\/=\*. ]+)\" (.*)/) {
2755 $fulltextreply{$1}=eval "qq{$2}";
2756 logmsg "FTPD: set custom reply for $1\n";
2758 elsif($_ =~ /REPLY ([A-Za-z0-9+\/=\*]*) (.*)/) {
2759 $commandreply{$1}=eval "qq{$2}";
2761 logmsg "FTPD: set custom reply for empty command\n";
2764 logmsg "FTPD: set custom reply for $1 command\n";
2767 elsif($_ =~ /COUNT ([A-Z]+) (.*)/) {
2768 # we blank the custom reply for this command when having
2769 # been used this number of times
2770 $customcount{$1}=$2;
2771 logmsg "FTPD: blank custom reply for $1 command after $2 uses\n";
2773 elsif($_ =~ /DELAY ([A-Z]+) (\d*)/) {
2775 logmsg "FTPD: delay reply for $1 with $2 seconds\n";
2777 elsif($_ =~ /SLOWDOWN/) {
2780 logmsg "FTPD: send response with 0.01 sec delay between each byte\n";
2782 elsif($_ =~ /RETRWEIRDO/) {
2783 logmsg "FTPD: instructed to use RETRWEIRDO\n";
2786 elsif($_ =~ /RETRNOSIZE/) {
2787 logmsg "FTPD: instructed to use RETRNOSIZE\n";
2790 elsif($_ =~ /PASVBADIP/) {
2791 logmsg "FTPD: instructed to use PASVBADIP\n";
2794 elsif($_ =~ /NODATACONN425/) {
2795 # applies to both active and passive FTP modes
2796 logmsg "FTPD: instructed to use NODATACONN425\n";
2800 elsif($_ =~ /NODATACONN421/) {
2801 # applies to both active and passive FTP modes
2802 logmsg "FTPD: instructed to use NODATACONN421\n";
2806 elsif($_ =~ /NODATACONN150/) {
2807 # applies to both active and passive FTP modes
2808 logmsg "FTPD: instructed to use NODATACONN150\n";
2812 elsif($_ =~ /NODATACONN/) {
2813 # applies to both active and passive FTP modes
2814 logmsg "FTPD: instructed to use NODATACONN\n";
2817 elsif($_ =~ /CAPA (.*)/) {
2818 logmsg "FTPD: instructed to support CAPABILITY command\n";
2819 @capabilities = split(/ (?!(?:[^" ]|[^"] [^"])+")/, $1);
2820 foreach (@capabilities) {
2821 $_ = $1 if /^"(.*)"$/;
2824 elsif($_ =~ /AUTH (.*)/) {
2825 logmsg "FTPD: instructed to support AUTHENTICATION command\n";
2826 @auth_mechs = split(/ /, $1);
2828 elsif($_ =~ /NOSAVE/) {
2829 # don't actually store the file we upload - to be used when
2830 # uploading insanely huge amounts
2832 logmsg "FTPD: NOSAVE prevents saving of uploaded data\n";
2838 #----------------------------------------------------------------------
2839 #----------------------------------------------------------------------
2840 #--------------------------- END OF SUBS ----------------------------
2841 #----------------------------------------------------------------------
2842 #----------------------------------------------------------------------
2844 #**********************************************************************
2845 # Parse command line options
2849 # --verbose # verbose
2850 # --srcdir # source directory
2851 # --id # server instance number
2852 # --proto # server protocol
2853 # --pidfile # server pid file
2854 # --logfile # server log file
2855 # --ipv4 # server IP version 4
2856 # --ipv6 # server IP version 6
2857 # --port # server listener port
2858 # --addr # server address for listener port binding
2861 if($ARGV[0] eq '--verbose') {
2864 elsif($ARGV[0] eq '--srcdir') {
2870 elsif($ARGV[0] eq '--id') {
2871 if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
2872 $idnum = $1 if($1 > 0);
2876 elsif($ARGV[0] eq '--proto') {
2877 if($ARGV[1] && ($ARGV[1] =~ /^(ftp|imap|pop3|smtp)$/)) {
2882 die "unsupported protocol $ARGV[1]";
2885 elsif($ARGV[0] eq '--pidfile') {
2887 $pidfile = $ARGV[1];
2891 elsif($ARGV[0] eq '--logfile') {
2893 $logfile = $ARGV[1];
2897 elsif($ARGV[0] eq '--ipv4') {
2899 $listenaddr = '127.0.0.1' if($listenaddr eq '::1');
2901 elsif($ARGV[0] eq '--ipv6') {
2903 $listenaddr = '::1' if($listenaddr eq '127.0.0.1');
2905 elsif($ARGV[0] eq '--port') {
2906 if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
2907 $port = $1 if($1 > 1024);
2911 elsif($ARGV[0] eq '--addr') {
2913 my $tmpstr = $ARGV[1];
2914 if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) {
2915 $listenaddr = "$1.$2.$3.$4" if($ipvnum == 4);
2917 elsif($ipvnum == 6) {
2918 $listenaddr = $tmpstr;
2919 $listenaddr =~ s/^\[(.*)\]$/$1/;
2925 print STDERR "\nWarning: ftpserver.pl unknown parameter: $ARGV[0]\n";
2930 #***************************************************************************
2931 # Initialize command line option dependant variables
2935 $srcdir = $ENV{'srcdir'} || '.';
2938 $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
2941 $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
2944 $mainsockf_pidfile = "$path/".
2945 mainsockf_pidfilename($proto, $ipvnum, $idnum);
2946 $mainsockf_logfile =
2947 mainsockf_logfilename($logdir, $proto, $ipvnum, $idnum);
2949 if($proto eq 'ftp') {
2950 $datasockf_pidfile = "$path/".
2951 datasockf_pidfilename($proto, $ipvnum, $idnum);
2952 $datasockf_logfile =
2953 datasockf_logfilename($logdir, $proto, $ipvnum, $idnum);
2956 $srvrname = servername_str($proto, $ipvnum, $idnum);
2958 $idstr = "$idnum" if($idnum > 1);
2960 protocolsetup($proto);
2962 $SIG{INT} = \&exit_signal_handler;
2963 $SIG{TERM} = \&exit_signal_handler;
2967 logmsg sprintf("%s server listens on port IPv${ipvnum}/${port}\n", uc($proto));
2969 open(PID, ">$pidfile");
2973 logmsg("logged pid $$ in $pidfile\n");
2978 # kill previous data connection sockfilt when alive
2979 if($datasockf_runs eq 'yes') {
2980 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2981 logmsg "DATA sockfilt for $datasockf_mode data channel killed now\n";
2983 datasockf_state('STOPPED');
2986 # We read 'sockfilt' commands.
2990 logmsg "Awaiting input\n";
2991 sysread_or_die(\*SFREAD, \$input, 5);
2993 if($input !~ /^CNCT/) {
2994 # we wait for a connected client
2995 logmsg "MAIN sockfilt said: $input";
2998 logmsg "====> Client connect\n";
3000 set_advisor_read_lock($SERVERLOGS_LOCK);
3001 $serverlogslocked = 1;
3006 &customize(); # read test control instructions
3008 my $welcome = $commandreply{"welcome"};
3010 $welcome = $displaytext{"welcome"};
3013 # clear it after use
3014 $commandreply{"welcome"}="";
3015 if($welcome !~ /\r\n\z/) {
3019 sendcontrol $welcome;
3021 #remove global variables from last connection
3022 if($ftplistparserstate) {
3023 undef $ftplistparserstate;
3030 print STDERR "OUT: $welcome";
3038 # Now we expect to read DATA\n[hex size]\n[prot], where the [prot]
3039 # part only is FTP lingo.
3042 sysread_or_die(\*SFREAD, \$i, 5);
3045 logmsg "MAIN sockfilt said $i";
3054 sysread_or_die(\*SFREAD, \$i, 5);
3057 if($i =~ /^([0-9a-fA-F]{4})\n/) {
3062 read_mainsockf(\$input, $size);
3068 # Loop until command completion
3069 next unless($full =~ /\r\n$/);
3071 # Remove trailing CRLF.
3072 $full =~ s/[\n\r]+$//;
3076 if($proto eq "imap") {
3077 # IMAP is different with its identifier first on the command line
3078 if(($full =~ /^([^ ]+) ([^ ]+) (.*)/) ||
3079 ($full =~ /^([^ ]+) ([^ ]+)/)) {
3080 $cmdid=$1; # set the global variable
3084 # IMAP authentication cancellation
3085 elsif($full =~ /^\*$/) {
3086 # Command id has already been set
3090 # IMAP long "commands" are base64 authentication data
3091 elsif($full =~ /^[A-Z0-9+\/]*={0,2}$/i) {
3092 # Command id has already been set
3097 sendcontrol "$full BAD Command\r\n";
3101 elsif($full =~ /^([A-Z]{3,4})(\s(.*))?$/i) {
3105 elsif($proto eq "pop3") {
3106 # POP3 authentication cancellation
3107 if($full =~ /^\*$/) {
3111 # POP3 long "commands" are base64 authentication data
3112 elsif($full =~ /^[A-Z0-9+\/]*={0,2}$/i) {
3117 sendcontrol "-ERR Unrecognized command\r\n";
3121 elsif($proto eq "smtp") {
3122 # SMTP authentication cancellation
3123 if($full =~ /^\*$/) {
3127 # SMTP long "commands" are base64 authentication data
3128 elsif($full =~ /^[A-Z0-9+\/]{0,512}={0,2}$/i) {
3133 sendcontrol "500 Unrecognized command\r\n";
3138 sendcontrol "500 Unrecognized command\r\n";
3142 logmsg "< \"$full\"\n";
3145 print STDERR "IN: $full\n";
3150 my $delay = $delayreply{$FTPCMD};
3152 # just go sleep this many seconds!
3153 logmsg("Sleep for $delay seconds\n");
3154 my $twentieths = $delay * 20;
3155 while($twentieths--) {
3156 select(undef, undef, undef, 0.05) unless($got_exit_signal);
3160 my $check = 1; # no response yet
3162 # See if there is a custom reply for the full text
3163 my $fulltext = $FTPARG ? $FTPCMD . " " . $FTPARG : $FTPCMD;
3164 my $text = $fulltextreply{$fulltext};
3165 if($text && ($text ne "")) {
3166 sendcontrol "$text\r\n";
3170 # See if there is a custom reply for the command
3171 $text = $commandreply{$FTPCMD};
3172 if($text && ($text ne "")) {
3173 if($customcount{$FTPCMD} && (!--$customcount{$FTPCMD})) {
3174 # used enough times so blank the custom command reply
3175 $commandreply{$FTPCMD}="";
3178 sendcontrol "$text\r\n";
3182 # See if there is any display text for the command
3183 $text = $displaytext{$FTPCMD};
3184 if($text && ($text ne "")) {
3185 if($proto eq 'imap') {
3186 sendcontrol "$cmdid $text\r\n";
3189 sendcontrol "$text\r\n";
3195 # only perform this if we're not faking a reply
3196 my $func = $commandfunc{uc($FTPCMD)};
3198 &$func($FTPARG, $FTPCMD);
3205 logmsg "$FTPCMD wasn't handled!\n";
3206 if($proto eq 'pop3') {
3207 sendcontrol "-ERR $FTPCMD is not dealt with!\r\n";
3209 elsif($proto eq 'imap') {
3210 sendcontrol "$cmdid BAD $FTPCMD is not dealt with!\r\n";
3213 sendcontrol "500 $FTPCMD is not dealt with!\r\n";
3218 logmsg "====> Client disconnected\n";
3220 if($serverlogslocked) {
3221 $serverlogslocked = 0;
3222 clear_advisor_read_lock($SERVERLOGS_LOCK);
3226 killsockfilters($proto, $ipvnum, $idnum, $verbose);
3228 if($serverlogslocked) {
3229 $serverlogslocked = 0;
3230 clear_advisor_read_lock($SERVERLOGS_LOCK);