2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 1998 - 2014, 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 http://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_USERNAME = "user";
178 my $TEXT_PASSWORD = "secret";
179 my $POP3_TIMESTAMP = "<1972.987654321\@curl>";
181 #**********************************************************************
182 # exit_signal_handler will be triggered to indicate that the program
183 # should finish its execution in a controlled way as soon as possible.
184 # For now, program will also terminate from within this handler.
186 sub exit_signal_handler {
188 # For now, simply mimic old behavior.
189 killsockfilters($proto, $ipvnum, $idnum, $verbose);
191 if($serverlogslocked) {
192 $serverlogslocked = 0;
193 clear_advisor_read_lock($SERVERLOGS_LOCK);
198 #**********************************************************************
199 # logmsg is general message logging subroutine for our test servers.
203 # sub second timestamping needs Time::HiRes
204 if($Time::HiRes::VERSION) {
205 my ($seconds, $usec) = gettimeofday();
206 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
208 $now = sprintf("%02d:%02d:%02d.%06d ", $hour, $min, $sec, $usec);
211 my $seconds = time();
212 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
214 $now = sprintf("%02d:%02d:%02d ", $hour, $min, $sec);
216 if(open(LOGFILEFH, ">>$logfile")) {
217 print LOGFILEFH $now;
224 # append to the server.input file
225 open(INPUT, ">>log/server$idstr.input") ||
226 logmsg "failed to open log/server$idstr.input\n";
231 # use this, open->print->close system only to make the file
232 # open as little as possible, to make the test suite run
233 # better on windows/cygwin
236 #**********************************************************************
237 # eXsysread is a wrapper around perl's sysread() function. This will
238 # repeat the call to sysread() until it has actually read the complete
239 # number of requested bytes or an unrecoverable condition occurs.
240 # On success returns a positive value, the number of bytes requested.
241 # On failure or timeout returns zero.
247 my $timeout = shift; # A zero timeout disables eXsysread() time limit
249 my $time_limited = 0;
250 my $timeout_rest = 0;
257 if((not defined $nbytes) || ($nbytes < 1)) {
258 logmsg "Error: eXsysread() failure: " .
259 "length argument must be positive\n";
262 if((not defined $timeout) || ($timeout < 0)) {
263 logmsg "Error: eXsysread() failure: " .
264 "timeout argument must be zero or positive\n";
268 # caller sets eXsysread() time limit
270 $timeout_rest = $timeout;
271 $start_time = int(time());
274 while($nread < $nbytes) {
277 local $SIG{ALRM} = sub { die "alarm\n"; };
279 $rc = sysread($FH, $$scalar, $nbytes - $nread, $nread);
282 $timeout_rest = $timeout - (int(time()) - $start_time);
283 if($timeout_rest < 1) {
284 logmsg "Error: eXsysread() failure: timed out\n";
289 $rc = sysread($FH, $$scalar, $nbytes - $nread, $nread);
291 if($got_exit_signal) {
292 logmsg "Error: eXsysread() failure: signalled to die\n";
295 if(not defined $rc) {
297 logmsg "Warning: retrying sysread() interrupted system call\n";
301 logmsg "Warning: retrying sysread() due to EAGAIN\n";
304 if($!{EWOULDBLOCK}) {
305 logmsg "Warning: retrying sysread() due to EWOULDBLOCK\n";
308 logmsg "Error: sysread() failure: $!\n";
312 logmsg "Error: sysread() failure: returned negative value $rc\n";
316 logmsg "Error: sysread() failure: read zero bytes\n";
324 #**********************************************************************
325 # read_mainsockf attempts to read the given amount of output from the
326 # sockfilter which is in use for the main or primary connection. This
327 # reads untranslated sockfilt lingo which may hold data read from the
328 # main or primary socket. On success returns 1, otherwise zero.
333 my $timeout = shift; # Optional argument, if zero blocks indefinitively
336 if(not defined $timeout) {
337 $timeout = $sockfilt_timeout + ($nbytes >> 12);
339 if(eXsysread($FH, $scalar, $nbytes, $timeout) != $nbytes) {
340 my ($fcaller, $lcaller) = (caller)[1,2];
341 logmsg "Error: read_mainsockf() failure at $fcaller " .
342 "line $lcaller. Due to eXsysread() failure\n";
348 #**********************************************************************
349 # read_datasockf attempts to read the given amount of output from the
350 # sockfilter which is in use for the data or secondary connection. This
351 # reads untranslated sockfilt lingo which may hold data read from the
352 # data or secondary socket. On success returns 1, otherwise zero.
357 my $timeout = shift; # Optional argument, if zero blocks indefinitively
360 if(not defined $timeout) {
361 $timeout = $sockfilt_timeout + ($nbytes >> 12);
363 if(eXsysread($FH, $scalar, $nbytes, $timeout) != $nbytes) {
364 my ($fcaller, $lcaller) = (caller)[1,2];
365 logmsg "Error: read_datasockf() failure at $fcaller " .
366 "line $lcaller. Due to eXsysread() failure\n";
380 $result = sysread($$FH, $$scalar, $length);
382 if(not defined $result) {
383 ($fcaller, $lcaller) = (caller)[1,2];
384 logmsg "Failed to read input\n";
385 logmsg "Error: $srvrname server, sysread error: $!\n";
386 logmsg "Exited from sysread_or_die() at $fcaller " .
387 "line $lcaller. $srvrname server, sysread error: $!\n";
388 killsockfilters($proto, $ipvnum, $idnum, $verbose);
390 if($serverlogslocked) {
391 $serverlogslocked = 0;
392 clear_advisor_read_lock($SERVERLOGS_LOCK);
396 elsif($result == 0) {
397 ($fcaller, $lcaller) = (caller)[1,2];
398 logmsg "Failed to read input\n";
399 logmsg "Error: $srvrname server, read zero\n";
400 logmsg "Exited from sysread_or_die() at $fcaller " .
401 "line $lcaller. $srvrname server, read zero\n";
402 killsockfilters($proto, $ipvnum, $idnum, $verbose);
404 if($serverlogslocked) {
405 $serverlogslocked = 0;
406 clear_advisor_read_lock($SERVERLOGS_LOCK);
415 my $mainsockfcmd = "./server/sockfilt " .
416 "--ipv$ipvnum --port $port " .
417 "--pidfile \"$mainsockf_pidfile\" " .
418 "--logfile \"$mainsockf_logfile\"";
419 $sfpid = open2(*SFREAD, *SFWRITE, $mainsockfcmd);
421 print STDERR "$mainsockfcmd\n" if($verbose);
423 print SFWRITE "PING\n";
425 sysread_or_die(\*SFREAD, \$pong, 5);
427 if($pong !~ /^PONG/) {
428 logmsg "Failed sockfilt command: $mainsockfcmd\n";
429 killsockfilters($proto, $ipvnum, $idnum, $verbose);
431 if($serverlogslocked) {
432 $serverlogslocked = 0;
433 clear_advisor_read_lock($SERVERLOGS_LOCK);
435 die "Failed to start sockfilt!";
439 #**********************************************************************
440 # Returns the given test's reply data
446 $testno =~ s/^([^0-9]*)//;
447 if($testno > 10000) {
448 $testpart = $testno % 10000;
449 $testno = int($testno / 10000);
452 loadtest("$srcdir/data/test$testno");
454 my @data = getpart("reply", "data$testpart");
455 if((!@data) && ($testpart ne "")) {
456 @data = getpart("reply", "data");
465 printf SFWRITE "DATA\n%04x\n", length($l);
470 sub sockfiltsecondary {
473 printf DWRITE "DATA\n%04x\n", length($l);
478 #**********************************************************************
479 # Send data to the client on the control stream, which happens to be plain
484 # spit it all out at once
488 my $a = join("", @_);
489 my @a = split("", $a);
493 select(undef, undef, undef, 0.01);
505 #**********************************************************************
506 # Send data to the FTP client on the data stream when data connection
507 # is actually established. Given that this sub should only be called
508 # when a data connection is supposed to be established, calling this
509 # without a data connection is an indication of weak logic somewhere.
513 if($datasockf_conn eq 'no') {
514 logmsg "WARNING: Detected data sending attempt without DATA channel\n";
516 logmsg "WARNING: Data swallowed: $l\n"
523 # spit it all out at once
524 sockfiltsecondary $l;
527 # pause between each byte
529 sockfiltsecondary $_;
530 select(undef, undef, undef, 0.01);
536 #**********************************************************************
537 # protocolsetup initializes the 'displaytext' and 'commandfunc' hashes
538 # for the given protocol. References to protocol command callbacks are
539 # stored in 'commandfunc' hash, and text which will be returned to the
540 # client before the command callback runs is stored in 'displaytext'.
545 if($proto eq 'ftp') {
547 'PORT' => \&PORT_ftp,
548 'EPRT' => \&PORT_ftp,
549 'LIST' => \&LIST_ftp,
550 'NLST' => \&NLST_ftp,
551 'PASV' => \&PASV_ftp,
554 'EPSV' => \&PASV_ftp,
555 'RETR' => \&RETR_ftp,
556 'SIZE' => \&SIZE_ftp,
557 'REST' => \&REST_ftp,
558 'STOR' => \&STOR_ftp,
559 'APPE' => \&STOR_ftp, # append looks like upload
560 'MDTM' => \&MDTM_ftp,
563 'USER' => '331 We are happy you popped in!',
564 'PASS' => '230 Welcome you silly person',
565 'PORT' => '200 You said PORT - I say FINE',
566 'TYPE' => '200 I modify TYPE as you wanted',
567 'LIST' => '150 here comes a directory',
568 'NLST' => '150 here comes a directory',
569 'CWD' => '250 CWD command successful.',
570 'SYST' => '215 UNIX Type: L8', # just fake something
571 'QUIT' => '221 bye bye baby', # just reply something
572 'MKD' => '257 Created your requested directory',
573 'REST' => '350 Yeah yeah we set it there for you',
574 'DELE' => '200 OK OK OK whatever you say',
575 'RNFR' => '350 Received your order. Please provide more',
576 'RNTO' => '250 Ok, thanks. File renaming completed.',
577 'NOOP' => '200 Yes, I\'m very good at doing nothing.',
578 'PBSZ' => '500 PBSZ not implemented',
579 'PROT' => '500 PROT not implemented',
580 'welcome' => join("",
581 '220- _ _ ____ _ '."\r\n",
582 '220- ___| | | | _ \| | '."\r\n",
583 '220- / __| | | | |_) | | '."\r\n",
584 '220- | (__| |_| | _ {| |___ '."\r\n",
585 '220 \___|\___/|_| \_\_____|'."\r\n")
588 elsif($proto eq 'pop3') {
590 'APOP' => \&APOP_pop3,
591 'AUTH' => \&AUTH_pop3,
592 'CAPA' => \&CAPA_pop3,
593 'DELE' => \&DELE_pop3,
594 'LIST' => \&LIST_pop3,
595 'NOOP' => \&NOOP_pop3,
596 'PASS' => \&PASS_pop3,
597 'QUIT' => \&QUIT_pop3,
598 'RETR' => \&RETR_pop3,
599 'RSET' => \&RSET_pop3,
600 'STAT' => \&STAT_pop3,
602 'UIDL' => \&UIDL_pop3,
603 'USER' => \&USER_pop3,
606 'welcome' => join("",
607 ' _ _ ____ _ '."\r\n",
608 ' ___| | | | _ \| | '."\r\n",
609 ' / __| | | | |_) | | '."\r\n",
610 ' | (__| |_| | _ {| |___ '."\r\n",
611 ' \___|\___/|_| \_\_____|'."\r\n",
612 '+OK cURL POP3 server ready to serve '."\r\n")
615 elsif($proto eq 'imap') {
617 'APPEND' => \&APPEND_imap,
618 'CAPABILITY' => \&CAPABILITY_imap,
619 'CHECK' => \&CHECK_imap,
620 'CLOSE' => \&CLOSE_imap,
621 'COPY' => \©_imap,
622 'CREATE' => \&CREATE_imap,
623 'DELETE' => \&DELETE_imap,
624 'EXAMINE' => \&EXAMINE_imap,
625 'EXPUNGE' => \&EXPUNGE_imap,
626 'FETCH' => \&FETCH_imap,
627 'LIST' => \&LIST_imap,
628 'LSUB' => \&LSUB_imap,
629 'LOGIN' => \&LOGIN_imap,
630 'LOGOUT' => \&LOGOUT_imap,
631 'NOOP' => \&NOOP_imap,
632 'RENAME' => \&RENAME_imap,
633 'SEARCH' => \&SEARCH_imap,
634 'SELECT' => \&SELECT_imap,
635 'STATUS' => \&STATUS_imap,
636 'STORE' => \&STORE_imap,
640 'welcome' => join("",
641 ' _ _ ____ _ '."\r\n",
642 ' ___| | | | _ \| | '."\r\n",
643 ' / __| | | | |_) | | '."\r\n",
644 ' | (__| |_| | _ {| |___ '."\r\n",
645 ' \___|\___/|_| \_\_____|'."\r\n",
646 '* OK cURL IMAP server ready to serve'."\r\n")
649 elsif($proto eq 'smtp') {
651 'DATA' => \&DATA_smtp,
652 'EHLO' => \&EHLO_smtp,
653 'EXPN' => \&EXPN_smtp,
654 'HELO' => \&HELO_smtp,
655 'HELP' => \&HELP_smtp,
656 'MAIL' => \&MAIL_smtp,
657 'NOOP' => \&NOOP_smtp,
658 'RSET' => \&RSET_smtp,
659 'RCPT' => \&RCPT_smtp,
660 'VRFY' => \&VRFY_smtp,
661 'QUIT' => \&QUIT_smtp,
664 'welcome' => join("",
665 '220- _ _ ____ _ '."\r\n",
666 '220- ___| | | | _ \| | '."\r\n",
667 '220- / __| | | | |_) | | '."\r\n",
668 '220- | (__| |_| | _ {| |___ '."\r\n",
669 '220 \___|\___/|_| \_\_____|'."\r\n")
675 my ($closed)=@_; # non-zero if already disconnected
677 my $datapid = processexists($datasockf_pidfile);
679 logmsg "=====> Closing $datasockf_mode DATA connection...\n";
683 logmsg "Server disconnects $datasockf_mode DATA connection\n";
684 print DWRITE "DISC\n";
686 sysread DREAD, $i, 5;
689 logmsg "Server finds $datasockf_mode DATA connection already ".
694 logmsg "Server knows $datasockf_mode DATA connection is already ".
699 print DWRITE "QUIT\n";
700 waitpid($datapid, 0);
701 unlink($datasockf_pidfile) if(-f $datasockf_pidfile);
702 logmsg "DATA sockfilt for $datasockf_mode data channel quits ".
706 logmsg "DATA sockfilt for $datasockf_mode data channel already ".
710 logmsg "=====> Closed $datasockf_mode DATA connection\n";
712 datasockf_state('STOPPED');
716 ################ SMTP commands
719 # The type of server (SMTP or ESMTP)
722 # The client (which normally contains the test number)
729 # TODO: Get the IP address of the client connection to use in the
730 # EHLO response when the client doesn't specify one but for now use
733 $client = "[127.0.0.1]";
736 # Set the server type to ESMTP
737 $smtp_type = "ESMTP";
739 # Calculate the EHLO response
740 push @data, "$smtp_type pingpong test server Hello $client";
742 if((@capabilities) || (@auth_mechs)) {
745 for my $c (@capabilities) {
749 for my $am (@auth_mechs) {
759 push @data, "AUTH $mechs";
763 # Send the EHLO response
764 for(my $i = 0; $i < @data; $i++) {
768 sendcontrol "250-$d\r\n";
771 sendcontrol "250 $d\r\n";
775 # Store the client (as it may contain the test number)
776 $smtp_client = $client;
784 # TODO: Get the IP address of the client connection to use in the HELO
785 # response when the client doesn't specify one but for now use 127.0.0.1
787 $client = "[127.0.0.1]";
790 # Set the server type to SMTP
793 # Send the HELO response
794 sendcontrol "250 $smtp_type pingpong test server Hello $client\r\n";
796 # Store the client (as it may contain the test number)
797 $smtp_client = $client;
805 logmsg "MAIL_smtp got $args\n";
808 sendcontrol "501 Unrecognized parameter\r\n";
813 my @elements = split(/ /, $args);
815 # Get the FROM and SIZE parameters
816 for my $e (@elements) {
817 if($e =~ /^FROM:(.*)$/) {
820 elsif($e =~ /^SIZE=(\d+)$/) {
825 # Validate the from address (only <> and a valid email address inside
826 # <> are allowed, such as <user@example.com>)
827 if ((!$from) || (($from ne "<>") && ($from !~
828 /^<([a-zA-Z0-9._%+-]+)\@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})>$/))) {
829 sendcontrol "501 Invalid address\r\n";
835 # Check the capabilities for SIZE and if the specified size is
836 # greater than the message size then reject it
837 if (@found = grep /^SIZE (\d+)$/, @capabilities) {
838 if ($found[0] =~ /^SIZE (\d+)$/) {
846 sendcontrol "552 Message size too large\r\n";
849 sendcontrol "250 Sender OK\r\n";
860 logmsg "RCPT_smtp got $args\n";
862 # Get the TO parameter
863 if($args !~ /^TO:(.*)/) {
864 sendcontrol "501 Unrecognized parameter\r\n";
869 # Validate the to address (only a valid email address inside <> is
870 # allowed, such as <user@example.com>)
872 /^<([a-zA-Z0-9._%+-]+)\@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})>$/) {
873 sendcontrol "501 Invalid address\r\n";
876 sendcontrol "250 Recipient OK\r\n";
887 sendcontrol "501 Unrecognized parameter\r\n";
889 elsif ($smtp_client !~ /^(\d*)$/) {
890 sendcontrol "501 Invalid arguments\r\n";
893 sendcontrol "354 Show me the mail\r\n";
895 my $testno = $smtp_client;
896 my $filename = "log/upload.$testno";
898 logmsg "Store test number $testno in $filename\n";
900 open(FILE, ">$filename") ||
901 return 0; # failed to open output
907 while (5 == (sysread \*SFREAD, $line, 5)) {
908 if($line eq "DATA\n") {
911 sysread \*SFREAD, $i, 5;
914 if($i =~ /^([0-9a-fA-F]{4})\n/) {
918 read_mainsockf(\$line, $size);
921 print FILE $line if(!$nosave);
924 if($raw =~ /\x0d\x0a\x2e\x0d\x0a/) {
925 # end of data marker!
929 logmsg "> Appending $size bytes to file\n";
932 logmsg "Found SMTP EOB marker\n";
936 elsif($line eq "DISC\n") {
942 logmsg "No support for: $line";
948 print FILE "$ulsize bytes would've been stored here\n";
953 logmsg "received $ulsize bytes upload\n";
955 sendcontrol "250 OK, data received!\r\n";
965 sendcontrol "501 Unrecognized parameter\r\n";
968 sendcontrol "250 OK\r\n";
978 sendcontrol "501 Unrecognized parameter\r\n";
981 sendcontrol "250 Resetting\r\n";
990 # One argument is optional
992 logmsg "HELP_smtp got $args\n";
995 if($smtp_client eq "verifiedserver") {
996 # This is the secret command that verifies that this actually is
997 # the curl test server
998 sendcontrol "214 WE ROOLZ: $$\r\n";
1001 print STDERR "FTPD: We returned proof we are the test server\n";
1004 logmsg "return proof we are we\n";
1007 sendcontrol "214-This server supports the following commands:\r\n";
1010 sendcontrol "214 HELO EHLO RCPT DATA RSET MAIL VRFY EXPN QUIT HELP AUTH\r\n";
1013 sendcontrol "214 HELO EHLO RCPT DATA RSET MAIL VRFY EXPN QUIT HELP\r\n";
1022 my ($username, $address) = split(/ /, $args, 2);
1024 logmsg "VRFY_smtp got $args\n";
1026 if($username eq "") {
1027 sendcontrol "501 Unrecognized parameter\r\n";
1030 my @data = getreplydata($smtp_client);
1041 my ($list_name) = @_;
1043 logmsg "EXPN_smtp got $list_name\n";
1046 sendcontrol "501 Unrecognized parameter\r\n";
1049 my @data = getreplydata($smtp_client);
1060 sendcontrol "221 cURL $smtp_type server signing off\r\n";
1065 # What was deleted by IMAP STORE / POP3 DELE commands
1069 ################ IMAP commands
1072 # global to allow the command functions to read it
1075 # what was picked by SELECT
1078 # Any IMAP parameter can come in escaped and in double quotes.
1079 # This function is dumb (so far) and just removes the quotes if present.
1080 sub fix_imap_params {
1082 $_ = $1 if /^"(.*)"$/;
1086 sub CAPABILITY_imap {
1087 if((!@capabilities) && (!@auth_mechs)) {
1088 sendcontrol "$cmdid BAD Command\r\n";
1093 # Calculate the CAPABILITY response
1094 $data = "* CAPABILITY IMAP4";
1096 for my $c (@capabilities) {
1100 for my $am (@auth_mechs) {
1101 $data .= " AUTH=$am";
1104 $data .= " pingpong test server\r\n";
1106 # Send the CAPABILITY response
1108 sendcontrol "$cmdid OK CAPABILITY completed\r\n";
1116 my ($user, $password) = split(/ /, $args, 2);
1117 fix_imap_params($user, $password);
1119 logmsg "LOGIN_imap got $args\n";
1122 sendcontrol "$cmdid BAD Command Argument\r\n";
1124 elsif (($user ne $TEXT_USERNAME) || ($password ne $TEXT_PASSWORD)) {
1125 sendcontrol "$cmdid NO LOGIN failed\r\n";
1128 sendcontrol "$cmdid OK LOGIN completed\r\n";
1136 fix_imap_params($mailbox);
1138 logmsg "SELECT_imap got test $mailbox\n";
1140 if($mailbox eq "") {
1141 sendcontrol "$cmdid BAD Command Argument\r\n";
1144 # Example from RFC 3501, 6.3.1. SELECT Command
1145 sendcontrol "* 172 EXISTS\r\n";
1146 sendcontrol "* 1 RECENT\r\n";
1147 sendcontrol "* OK [UNSEEN 12] Message 12 is first unseen\r\n";
1148 sendcontrol "* OK [UIDVALIDITY 3857529045] UIDs valid\r\n";
1149 sendcontrol "* OK [UIDNEXT 4392] Predicted next UID\r\n";
1150 sendcontrol "* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)\r\n";
1151 sendcontrol "* OK [PERMANENTFLAGS (\\Deleted \\Seen \\*)] Limited\r\n";
1152 sendcontrol "$cmdid OK [READ-WRITE] SELECT completed\r\n";
1154 $selected = $mailbox;
1162 my ($uid, $how) = split(/ /, $args, 2);
1163 fix_imap_params($uid, $how);
1165 logmsg "FETCH_imap got $args\n";
1167 if ($selected eq "") {
1168 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1174 if($selected eq "verifiedserver") {
1175 # this is the secret command that verifies that this actually is
1176 # the curl test server
1177 my $response = "WE ROOLZ: $$\r\n";
1179 print STDERR "FTPD: We returned proof we are the test server\n";
1181 $data[0] = $response;
1182 logmsg "return proof we are we\n";
1186 logmsg "retrieve a mail\n";
1188 @data = getreplydata($selected);
1192 $size += length($_);
1195 sendcontrol "* $uid FETCH ($how {$size}\r\n";
1201 sendcontrol ")\r\n";
1202 sendcontrol "$cmdid OK FETCH completed\r\n";
1211 logmsg "APPEND_imap got $args\r\n";
1213 $args =~ /^([^ ]+) [^{]*\{(\d+)\}$/;
1214 my ($mailbox, $size) = ($1, $2);
1215 fix_imap_params($mailbox);
1217 if($mailbox eq "") {
1218 sendcontrol "$cmdid BAD Command Argument\r\n";
1221 sendcontrol "+ Ready for literal data\r\n";
1223 my $testno = $mailbox;
1224 my $filename = "log/upload.$testno";
1226 logmsg "Store test number $testno in $filename\n";
1228 open(FILE, ">$filename") ||
1229 return 0; # failed to open output
1233 while(5 == (sysread \*SFREAD, $line, 5)) {
1234 if($line eq "DATA\n") {
1235 sysread \*SFREAD, $line, 5;
1238 if($line =~ /^([0-9a-fA-F]{4})\n/) {
1239 $chunksize = hex($1);
1242 read_mainsockf(\$line, $chunksize);
1244 my $left = $size - $received;
1245 my $datasize = ($left > $chunksize) ? $chunksize : $left;
1248 logmsg "> Appending $datasize bytes to file\n";
1249 print FILE substr($line, 0, $datasize) if(!$nosave);
1250 $line = substr($line, $datasize);
1252 $received += $datasize;
1253 if($received == $size) {
1254 logmsg "Received all data, waiting for final CRLF.\n";
1258 if($received == $size && $line eq "\r\n") {
1262 elsif($line eq "DISC\n") {
1263 logmsg "Unexpected disconnect!\n";
1267 logmsg "No support for: $line";
1273 print FILE "$size bytes would've been stored here\n";
1278 logmsg "received $size bytes upload\n";
1280 sendcontrol "$cmdid OK APPEND completed\r\n";
1288 my ($uid, $what, $value) = split(/ /, $args, 3);
1289 fix_imap_params($uid);
1291 logmsg "STORE_imap got $args\n";
1293 if ($selected eq "") {
1294 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1296 elsif (($uid eq "") || ($what ne "+Flags") || ($value eq "")) {
1297 sendcontrol "$cmdid BAD Command Argument\r\n";
1300 if($value eq "\\Deleted") {
1301 push(@deleted, $uid);
1304 sendcontrol "* $uid FETCH (FLAGS (\\Seen $value))\r\n";
1305 sendcontrol "$cmdid OK STORE completed\r\n";
1313 my ($reference, $mailbox) = split(/ /, $args, 2);
1314 fix_imap_params($reference, $mailbox);
1316 logmsg "LIST_imap got $args\n";
1318 if ($reference eq "") {
1319 sendcontrol "$cmdid BAD Command Argument\r\n";
1321 elsif ($reference eq "verifiedserver") {
1322 # this is the secret command that verifies that this actually is
1323 # the curl test server
1324 sendcontrol "* LIST () \"/\" \"WE ROOLZ: $$\"\r\n";
1325 sendcontrol "$cmdid OK LIST Completed\r\n";
1328 print STDERR "FTPD: We returned proof we are the test server\n";
1331 logmsg "return proof we are we\n";
1334 my @data = getreplydata($reference);
1340 sendcontrol "$cmdid OK LIST Completed\r\n";
1348 my ($reference, $mailbox) = split(/ /, $args, 2);
1349 fix_imap_params($reference, $mailbox);
1351 logmsg "LSUB_imap got $args\n";
1353 if ($reference eq "") {
1354 sendcontrol "$cmdid BAD Command Argument\r\n";
1357 my @data = getreplydata($reference);
1363 sendcontrol "$cmdid OK LSUB Completed\r\n";
1371 fix_imap_params($mailbox);
1373 logmsg "EXAMINE_imap got $mailbox\n";
1375 if ($mailbox eq "") {
1376 sendcontrol "$cmdid BAD Command Argument\r\n";
1379 my @data = getreplydata($mailbox);
1385 sendcontrol "$cmdid OK [READ-ONLY] EXAMINE completed\r\n";
1393 my ($mailbox, $what) = split(/ /, $args, 2);
1394 fix_imap_params($mailbox);
1396 logmsg "STATUS_imap got $args\n";
1398 if ($mailbox eq "") {
1399 sendcontrol "$cmdid BAD Command Argument\r\n";
1402 my @data = getreplydata($mailbox);
1408 sendcontrol "$cmdid OK STATUS completed\r\n";
1416 fix_imap_params($what);
1418 logmsg "SEARCH_imap got $what\n";
1420 if ($selected eq "") {
1421 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1423 elsif ($what eq "") {
1424 sendcontrol "$cmdid BAD Command Argument\r\n";
1427 my @data = getreplydata($selected);
1433 sendcontrol "$cmdid OK SEARCH completed\r\n";
1441 fix_imap_params($args);
1443 logmsg "CREATE_imap got $args\n";
1446 sendcontrol "$cmdid BAD Command Argument\r\n";
1449 sendcontrol "$cmdid OK CREATE completed\r\n";
1457 fix_imap_params($args);
1459 logmsg "DELETE_imap got $args\n";
1462 sendcontrol "$cmdid BAD Command Argument\r\n";
1465 sendcontrol "$cmdid OK DELETE completed\r\n";
1473 my ($from_mailbox, $to_mailbox) = split(/ /, $args, 2);
1474 fix_imap_params($from_mailbox, $to_mailbox);
1476 logmsg "RENAME_imap got $args\n";
1478 if (($from_mailbox eq "") || ($to_mailbox eq "")) {
1479 sendcontrol "$cmdid BAD Command Argument\r\n";
1482 sendcontrol "$cmdid OK RENAME completed\r\n";
1489 if ($selected eq "") {
1490 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1493 sendcontrol "$cmdid OK CHECK completed\r\n";
1500 if ($selected eq "") {
1501 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1504 sendcontrol "$cmdid BAD Command Argument\r\n";
1507 sendcontrol "$cmdid OK CLOSE completed\r\n";
1516 if ($selected eq "") {
1517 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1521 # Report the number of existing messages as per the SELECT
1523 sendcontrol "* 172 EXISTS\r\n";
1526 # Report the message UIDs being deleted
1527 for my $d (@deleted) {
1528 sendcontrol "* $d EXPUNGE\r\n";
1534 sendcontrol "$cmdid OK EXPUNGE completed\r\n";
1542 my ($uid, $mailbox) = split(/ /, $args, 2);
1543 fix_imap_params($uid, $mailbox);
1545 logmsg "COPY_imap got $args\n";
1547 if (($uid eq "") || ($mailbox eq "")) {
1548 sendcontrol "$cmdid BAD Command Argument\r\n";
1551 sendcontrol "$cmdid OK COPY completed\r\n";
1559 my ($command) = split(/ /, $args, 1);
1560 fix_imap_params($command);
1562 logmsg "UID_imap got $args\n";
1564 if ($selected eq "") {
1565 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1567 elsif (($command ne "COPY") && ($command ne "FETCH") &&
1568 ($command ne "STORE") && ($command ne "SEARCH")) {
1569 sendcontrol "$cmdid BAD Command Argument\r\n";
1572 my @data = getreplydata($selected);
1578 sendcontrol "$cmdid OK $command completed\r\n";
1590 "* 14 FETCH (FLAGS (\\Seen \\Deleted))\r\n",
1594 sendcontrol "$cmdid BAD Command Argument\r\n";
1601 sendcontrol "$cmdid OK NOOP completed\r\n";
1608 sendcontrol "* BYE cURL IMAP server signing off\r\n";
1609 sendcontrol "$cmdid OK LOGOUT completed\r\n";
1615 ################ POP3 commands
1618 # Who is attempting to log in
1625 # Calculate the capability list based on the specified capabilities
1626 # (except APOP) and any authentication mechanisms
1627 for my $c (@capabilities) {
1628 push @list, "$c\r\n" unless $c eq "APOP";
1631 for my $am (@auth_mechs) {
1641 push @list, "SASL $mechs\r\n";
1645 sendcontrol "-ERR Unrecognized command\r\n";
1650 # Calculate the CAPA response
1651 push @data, "+OK List of capabilities follows\r\n";
1654 push @data, "$l\r\n";
1657 push @data, "IMPLEMENTATION POP3 pingpong test server\r\n";
1659 # Send the CAPA response
1664 # End with the magic 3-byte end of listing marker
1665 sendcontrol ".\r\n";
1673 my ($user, $secret) = split(/ /, $args, 2);
1675 if (!grep /^APOP$/, @capabilities) {
1676 sendcontrol "-ERR Unrecognized command\r\n";
1678 elsif (($user eq "") || ($secret eq "")) {
1679 sendcontrol "-ERR Protocol error\r\n";
1682 my $digest = Digest::MD5::md5_hex($POP3_TIMESTAMP, $TEXT_PASSWORD);
1684 if (($user ne $TEXT_USERNAME) || ($secret ne $digest)) {
1685 sendcontrol "-ERR Login failure\r\n";
1688 sendcontrol "+OK Login successful\r\n";
1697 sendcontrol "-ERR Unrecognized command\r\n";
1702 # Calculate the AUTH response
1703 push @data, "+OK List of supported mechanisms follows\r\n";
1705 for my $am (@auth_mechs) {
1706 push @data, "$am\r\n";
1709 # Send the AUTH response
1714 # End with the magic 3-byte end of listing marker
1715 sendcontrol ".\r\n";
1724 logmsg "USER_pop3 got $user\n";
1727 sendcontrol "-ERR Protocol error\r\n";
1732 sendcontrol "+OK\r\n";
1739 my ($password) = @_;
1741 logmsg "PASS_pop3 got $password\n";
1743 if (($username ne $TEXT_USERNAME) || ($password ne $TEXT_PASSWORD)) {
1744 sendcontrol "-ERR Login failure\r\n";
1747 sendcontrol "+OK Login successful\r\n";
1757 if($msgid =~ /^verifiedserver$/) {
1758 # this is the secret command that verifies that this actually is
1759 # the curl test server
1760 my $response = "WE ROOLZ: $$\r\n";
1762 print STDERR "FTPD: We returned proof we are the test server\n";
1764 $data[0] = $response;
1765 logmsg "return proof we are we\n";
1769 logmsg "retrieve a mail\n";
1771 @data = getreplydata($msgid);
1774 sendcontrol "+OK Mail transfer starts\r\n";
1780 # end with the magic 3-byte end of mail marker, assumes that the
1781 # mail body ends with a CRLF!
1782 sendcontrol ".\r\n";
1788 # This is a built-in fake-message list
1791 "2 4294967400\r\n", # > 4 GB
1795 logmsg "retrieve a message list\n";
1797 sendcontrol "+OK Listing starts\r\n";
1803 # End with the magic 3-byte end of listing marker
1804 sendcontrol ".\r\n";
1812 logmsg "DELE_pop3 got $msgid\n";
1815 sendcontrol "-ERR Protocol error\r\n";
1818 push (@deleted, $msgid);
1820 sendcontrol "+OK\r\n";
1830 sendcontrol "-ERR Protocol error\r\n";
1833 # Send statistics for the built-in fake message list as
1834 # detailed in the LIST_pop3 function above
1835 sendcontrol "+OK 3 4294967800\r\n";
1845 sendcontrol "-ERR Protocol error\r\n";
1848 sendcontrol "+OK\r\n";
1855 # This is a built-in fake-message UID list
1859 "3 4\r\n", # Note that UID 3 is a simulated "deleted" message
1862 if (!grep /^UIDL$/, @capabilities) {
1863 sendcontrol "-ERR Unrecognized command\r\n";
1866 logmsg "retrieve a message UID list\n";
1868 sendcontrol "+OK Listing starts\r\n";
1874 # End with the magic 3-byte end of listing marker
1875 sendcontrol ".\r\n";
1883 my ($msgid, $lines) = split(/ /, $args, 2);
1885 logmsg "TOP_pop3 got $args\n";
1887 if (!grep /^TOP$/, @capabilities) {
1888 sendcontrol "-ERR Unrecognized command\r\n";
1890 elsif (($msgid eq "") || ($lines eq "")) {
1891 sendcontrol "-ERR Protocol error\r\n";
1894 if ($lines == "0") {
1895 logmsg "retrieve header of mail\n";
1898 logmsg "retrieve top $lines lines of mail\n";
1901 my @data = getreplydata($msgid);
1903 sendcontrol "+OK Mail transfer starts\r\n";
1910 # End with the magic 3-byte end of mail marker, assumes that the
1911 # mail body ends with a CRLF!
1912 sendcontrol ".\r\n";
1922 sendcontrol "-ERR Protocol error\r\n";
1926 logmsg "resetting @deleted message(s)\n";
1931 sendcontrol "+OK\r\n";
1939 logmsg "deleting @deleted message(s)\n";
1944 sendcontrol "+OK cURL POP3 server signing off\r\n";
1950 ################ FTP commands
1955 logmsg "Set REST position to $rest\n"
1958 sub switch_directory_goto {
1959 my $target_dir = $_;
1961 if(!$ftptargetdir) {
1962 $ftptargetdir = "/";
1965 if($target_dir eq "") {
1966 $ftptargetdir = "/";
1968 elsif($target_dir eq "..") {
1969 if($ftptargetdir eq "/") {
1970 $ftptargetdir = "/";
1973 $ftptargetdir =~ s/[[:alnum:]]+\/$//;
1977 $ftptargetdir .= $target_dir . "/";
1981 sub switch_directory {
1982 my $target_dir = $_[0];
1984 if($target_dir =~ /^test-(\d+)/) {
1987 elsif($target_dir eq "/") {
1988 $ftptargetdir = "/";
1991 my @dirs = split("/", $target_dir);
1993 switch_directory_goto($_);
1999 my ($folder, $fullcommand) = $_[0];
2000 switch_directory($folder);
2001 if($ftptargetdir =~ /^\/fully_simulated/) {
2002 $ftplistparserstate = "enabled";
2005 undef $ftplistparserstate;
2011 $mydir = $ftptargetdir ? $ftptargetdir : "/";
2016 sendcontrol "257 \"$mydir\" is current directory\r\n";
2020 # print "150 ASCII data connection for /bin/ls (193.15.23.1,59196) (0 bytes)\r\n";
2022 # this is a built-in fake-dir ;-)
2023 my @ftpdir=("total 20\r\n",
2024 "drwxr-xr-x 8 98 98 512 Oct 22 13:06 .\r\n",
2025 "drwxr-xr-x 8 98 98 512 Oct 22 13:06 ..\r\n",
2026 "drwxr-xr-x 2 98 98 512 May 2 1996 .NeXT\r\n",
2027 "-r--r--r-- 1 0 1 35 Jul 16 1996 README\r\n",
2028 "lrwxrwxrwx 1 0 1 7 Dec 9 1999 bin -> usr/bin\r\n",
2029 "dr-xr-xr-x 2 0 1 512 Oct 1 1997 dev\r\n",
2030 "drwxrwxrwx 2 98 98 512 May 29 16:04 download.html\r\n",
2031 "dr-xr-xr-x 2 0 1 512 Nov 30 1995 etc\r\n",
2032 "drwxrwxrwx 2 98 1 512 Oct 30 14:33 pub\r\n",
2033 "dr-xr-xr-x 5 0 1 512 Oct 1 1997 usr\r\n");
2035 if($datasockf_conn eq 'no') {
2036 if($nodataconn425) {
2037 sendcontrol "150 Opening data connection\r\n";
2038 sendcontrol "425 Can't open data connection\r\n";
2040 elsif($nodataconn421) {
2041 sendcontrol "150 Opening data connection\r\n";
2042 sendcontrol "421 Connection timed out\r\n";
2044 elsif($nodataconn150) {
2045 sendcontrol "150 Opening data connection\r\n";
2046 # client shall timeout
2049 # client shall timeout
2054 if($ftplistparserstate) {
2055 @ftpdir = ftp_contentlist($ftptargetdir);
2058 logmsg "pass LIST data on data connection\n";
2061 loadtest("$srcdir/data/test$cwd_testno");
2063 my @data = getpart("reply", "data");
2066 # convert all \n to \r\n for ASCII transfer
2067 $send =~ s/\r\n/\n/g;
2068 $send =~ s/\n/\r\n/g;
2069 logmsg "send $send as data\n";
2072 $cwd_testno = 0; # forget it again
2075 # old hard-coded style
2081 sendcontrol "226 ASCII transfer complete\r\n";
2086 my @ftpdir=("file", "with space", "fake", "..", " ..", "funny", "README");
2088 if($datasockf_conn eq 'no') {
2089 if($nodataconn425) {
2090 sendcontrol "150 Opening data connection\r\n";
2091 sendcontrol "425 Can't open data connection\r\n";
2093 elsif($nodataconn421) {
2094 sendcontrol "150 Opening data connection\r\n";
2095 sendcontrol "421 Connection timed out\r\n";
2097 elsif($nodataconn150) {
2098 sendcontrol "150 Opening data connection\r\n";
2099 # client shall timeout
2102 # client shall timeout
2107 logmsg "pass NLST data on data connection\n";
2112 sendcontrol "226 ASCII transfer complete\r\n";
2119 if ($testno > 10000) {
2120 $testpart = $testno % 10000;
2121 $testno = int($testno / 10000);
2124 loadtest("$srcdir/data/test$testno");
2126 my @data = getpart("reply", "mdtm");
2128 my $reply = $data[0];
2129 chomp $reply if($reply);
2131 if($reply && ($reply =~ /^[+-]?\d+$/) && ($reply < 0)) {
2132 sendcontrol "550 $testno: no such file.\r\n";
2135 sendcontrol "$reply\r\n";
2138 sendcontrol "500 MDTM: no such command.\r\n";
2145 if($ftplistparserstate) {
2146 my $size = wildcard_filesize($ftptargetdir, $testno);
2148 sendcontrol "550 $testno: No such file or directory.\r\n";
2151 sendcontrol "213 $size\r\n";
2156 if($testno =~ /^verifiedserver$/) {
2157 my $response = "WE ROOLZ: $$\r\n";
2158 my $size = length($response);
2159 sendcontrol "213 $size\r\n";
2163 if($testno =~ /(\d+)\/?$/) {
2167 print STDERR "SIZE_ftp: invalid test number: $testno\n";
2172 if($testno > 10000) {
2173 $testpart = $testno % 10000;
2174 $testno = int($testno / 10000);
2177 loadtest("$srcdir/data/test$testno");
2179 my @data = getpart("reply", "size");
2181 my $size = $data[0];
2185 sendcontrol "213 $size\r\n";
2188 sendcontrol "550 $testno: No such file or directory.\r\n";
2193 @data = getpart("reply", "data$testpart");
2195 $size += length($_);
2198 sendcontrol "213 $size\r\n";
2201 sendcontrol "550 $testno: No such file or directory.\r\n";
2210 if($datasockf_conn eq 'no') {
2211 if($nodataconn425) {
2212 sendcontrol "150 Opening data connection\r\n";
2213 sendcontrol "425 Can't open data connection\r\n";
2215 elsif($nodataconn421) {
2216 sendcontrol "150 Opening data connection\r\n";
2217 sendcontrol "421 Connection timed out\r\n";
2219 elsif($nodataconn150) {
2220 sendcontrol "150 Opening data connection\r\n";
2221 # client shall timeout
2224 # client shall timeout
2229 if($ftplistparserstate) {
2230 my @content = wildcard_getfile($ftptargetdir, $testno);
2231 if($content[0] == -1) {
2235 my $size = length $content[1];
2236 sendcontrol "150 Binary data connection for $testno ($size bytes).\r\n",
2237 senddata $content[1];
2239 sendcontrol "226 File transfer complete\r\n";
2244 if($testno =~ /^verifiedserver$/) {
2245 # this is the secret command that verifies that this actually is
2246 # the curl test server
2247 my $response = "WE ROOLZ: $$\r\n";
2248 my $len = length($response);
2249 sendcontrol "150 Binary junk ($len bytes).\r\n";
2250 senddata "WE ROOLZ: $$\r\n";
2252 sendcontrol "226 File transfer complete\r\n";
2254 print STDERR "FTPD: We returned proof we are the test server\n";
2259 $testno =~ s/^([^0-9]*)//;
2261 if ($testno > 10000) {
2262 $testpart = $testno % 10000;
2263 $testno = int($testno / 10000);
2266 loadtest("$srcdir/data/test$testno");
2268 my @data = getpart("reply", "data$testpart");
2272 $size += length($_);
2275 my %hash = getpartattr("reply", "data$testpart");
2277 if($size || $hash{'sendzero'}) {
2280 # move read pointer forward
2282 logmsg "REST $rest was removed from size, makes $size left\n";
2283 $rest = 0; # reset REST offset again
2286 sendcontrol "150 Binary data connection for $testno () ($size bytes).\r\n",
2287 "226 File transfer complete\r\n";
2294 $retrweirdo=0; # switch off the weirdo again!
2297 my $sz = "($size bytes)";
2302 sendcontrol "150 Binary data connection for $testno () $sz.\r\n";
2309 sendcontrol "226 File transfer complete\r\n";
2313 sendcontrol "550 $testno: No such file or directory.\r\n";
2321 my $filename = "log/upload.$testno";
2323 if($datasockf_conn eq 'no') {
2324 if($nodataconn425) {
2325 sendcontrol "150 Opening data connection\r\n";
2326 sendcontrol "425 Can't open data connection\r\n";
2328 elsif($nodataconn421) {
2329 sendcontrol "150 Opening data connection\r\n";
2330 sendcontrol "421 Connection timed out\r\n";
2332 elsif($nodataconn150) {
2333 sendcontrol "150 Opening data connection\r\n";
2334 # client shall timeout
2337 # client shall timeout
2342 logmsg "STOR test number $testno in $filename\n";
2344 sendcontrol "125 Gimme gimme gimme!\r\n";
2346 open(FILE, ">$filename") ||
2347 return 0; # failed to open output
2352 while (5 == (sysread DREAD, $line, 5)) {
2353 if($line eq "DATA\n") {
2355 sysread DREAD, $i, 5;
2358 if($i =~ /^([0-9a-fA-F]{4})\n/) {
2362 read_datasockf(\$line, $size);
2364 #print STDERR " GOT: $size bytes\n";
2367 print FILE $line if(!$nosave);
2368 logmsg "> Appending $size bytes to file\n";
2370 elsif($line eq "DISC\n") {
2376 logmsg "No support for: $line";
2381 print FILE "$ulsize bytes would've been stored here\n";
2384 close_dataconn($disc);
2385 logmsg "received $ulsize bytes upload\n";
2386 sendcontrol "226 File transfer complete\r\n";
2393 my $bindonly = ($nodataconn) ? '--bindonly' : '';
2395 # kill previous data connection sockfilt when alive
2396 if($datasockf_runs eq 'yes') {
2397 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2398 logmsg "DATA sockfilt for $datasockf_mode data channel killed\n";
2400 datasockf_state('STOPPED');
2402 logmsg "====> Passive DATA channel requested by client\n";
2404 logmsg "DATA sockfilt for passive data channel starting...\n";
2406 # We fire up a new sockfilt to do the data transfer for us.
2407 my $datasockfcmd = "./server/sockfilt " .
2408 "--ipv$ipvnum $bindonly --port 0 " .
2409 "--pidfile \"$datasockf_pidfile\" " .
2410 "--logfile \"$datasockf_logfile\"";
2411 $slavepid = open2(\*DREAD, \*DWRITE, $datasockfcmd);
2414 datasockf_state('PASSIVE_NODATACONN');
2417 datasockf_state('PASSIVE');
2420 print STDERR "$datasockfcmd\n" if($verbose);
2422 print DWRITE "PING\n";
2424 sysread_or_die(\*DREAD, \$pong, 5);
2426 if($pong =~ /^FAIL/) {
2427 logmsg "DATA sockfilt said: FAIL\n";
2428 logmsg "DATA sockfilt for passive data channel failed\n";
2429 logmsg "DATA sockfilt not running\n";
2430 datasockf_state('STOPPED');
2431 sendcontrol "500 no free ports!\r\n";
2434 elsif($pong !~ /^PONG/) {
2435 logmsg "DATA sockfilt unexpected response: $pong\n";
2436 logmsg "DATA sockfilt for passive data channel failed\n";
2437 logmsg "DATA sockfilt killed now\n";
2438 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2439 logmsg "DATA sockfilt not running\n";
2440 datasockf_state('STOPPED');
2441 sendcontrol "500 no free ports!\r\n";
2445 logmsg "DATA sockfilt for passive data channel started (pid $slavepid)\n";
2447 # Find out on what port we listen on or have bound
2449 print DWRITE "PORT\n";
2451 # READ the response code
2452 sysread_or_die(\*DREAD, \$i, 5);
2454 # READ the response size
2455 sysread_or_die(\*DREAD, \$i, 5);
2458 if($i =~ /^([0-9a-fA-F]{4})\n/) {
2462 # READ the response data
2463 read_datasockf(\$i, $size);
2465 # The data is in the format
2468 if($i =~ /IPv(\d)\/(\d+)/) {
2469 # FIX: deal with IP protocol version
2474 logmsg "DATA sockfilt unknown listener port\n";
2475 logmsg "DATA sockfilt for passive data channel failed\n";
2476 logmsg "DATA sockfilt killed now\n";
2477 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2478 logmsg "DATA sockfilt not running\n";
2479 datasockf_state('STOPPED');
2480 sendcontrol "500 no free ports!\r\n";
2485 my $str = nodataconn_str();
2486 logmsg "DATA sockfilt for passive data channel ($str) bound on port ".
2490 logmsg "DATA sockfilt for passive data channel listens on port ".
2494 if($cmd ne "EPSV") {
2501 sendcontrol sprintf("227 Entering Passive Mode ($p,%d,%d)\n",
2502 int($pasvport/256), int($pasvport%256));
2506 sendcontrol sprintf("229 Entering Passive Mode (|||%d|)\n", $pasvport);
2509 logmsg "Client has been notified that DATA conn ".
2510 "will be accepted on port $pasvport\n";
2513 my $str = nodataconn_str();
2514 logmsg "====> Client fooled ($str)\n";
2519 local $SIG{ALRM} = sub { die "alarm\n" };
2521 # assume swift operations unless explicitly slow
2522 alarm ($datadelay?20:10);
2527 # FIX: Monitor ctrl conn for disconnect
2529 while(sysread(DREAD, $input, 5)) {
2531 if($input !~ /^CNCT/) {
2532 # we wait for a connected client
2533 logmsg "Odd, we got $input from client\n";
2536 logmsg "Client connects to port $pasvport\n";
2543 logmsg "$srvrname server timed out awaiting data connection ".
2544 "on port $pasvport\n";
2545 logmsg "accept failed or connection not even attempted\n";
2546 logmsg "DATA sockfilt killed now\n";
2547 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2548 logmsg "DATA sockfilt not running\n";
2549 datasockf_state('STOPPED');
2553 logmsg "====> Client established passive DATA connection ".
2554 "on port $pasvport\n";
2561 # Support both PORT and EPRT here.
2565 my ($arg, $cmd) = @_;
2569 # kill previous data connection sockfilt when alive
2570 if($datasockf_runs eq 'yes') {
2571 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2572 logmsg "DATA sockfilt for $datasockf_mode data channel killed\n";
2574 datasockf_state('STOPPED');
2576 logmsg "====> Active DATA channel requested by client\n";
2578 # We always ignore the given IP and use localhost.
2580 if($cmd eq "PORT") {
2581 if($arg !~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {
2582 logmsg "DATA sockfilt for active data channel not started ".
2583 "(bad PORT-line: $arg)\n";
2584 sendcontrol "500 silly you, go away\r\n";
2588 $addr = "$1.$2.$3.$4";
2590 # EPRT |2|::1|49706|
2591 elsif($cmd eq "EPRT") {
2592 if($arg !~ /(\d+)\|([^\|]+)\|(\d+)/) {
2593 logmsg "DATA sockfilt for active data channel not started ".
2594 "(bad EPRT-line: $arg)\n";
2595 sendcontrol "500 silly you, go away\r\n";
2598 sendcontrol "200 Thanks for dropping by. We contact you later\r\n";
2603 logmsg "DATA sockfilt for active data channel not started ".
2604 "(invalid command: $cmd)\n";
2605 sendcontrol "500 we don't like $cmd now\r\n";
2609 if(!$port || $port > 65535) {
2610 logmsg "DATA sockfilt for active data channel not started ".
2611 "(illegal PORT number: $port)\n";
2616 my $str = nodataconn_str();
2617 logmsg "DATA sockfilt for active data channel not started ($str)\n";
2618 datasockf_state('ACTIVE_NODATACONN');
2619 logmsg "====> Active DATA channel not established\n";
2623 logmsg "DATA sockfilt for active data channel starting...\n";
2625 # We fire up a new sockfilt to do the data transfer for us.
2626 my $datasockfcmd = "./server/sockfilt " .
2627 "--ipv$ipvnum --connect $port --addr \"$addr\" " .
2628 "--pidfile \"$datasockf_pidfile\" " .
2629 "--logfile \"$datasockf_logfile\"";
2630 $slavepid = open2(\*DREAD, \*DWRITE, $datasockfcmd);
2632 datasockf_state('ACTIVE');
2634 print STDERR "$datasockfcmd\n" if($verbose);
2636 print DWRITE "PING\n";
2638 sysread_or_die(\*DREAD, \$pong, 5);
2640 if($pong =~ /^FAIL/) {
2641 logmsg "DATA sockfilt said: FAIL\n";
2642 logmsg "DATA sockfilt for active data channel failed\n";
2643 logmsg "DATA sockfilt not running\n";
2644 datasockf_state('STOPPED');
2645 # client shall timeout awaiting connection from server
2648 elsif($pong !~ /^PONG/) {
2649 logmsg "DATA sockfilt unexpected response: $pong\n";
2650 logmsg "DATA sockfilt for active data channel failed\n";
2651 logmsg "DATA sockfilt killed now\n";
2652 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2653 logmsg "DATA sockfilt not running\n";
2654 datasockf_state('STOPPED');
2655 # client shall timeout awaiting connection from server
2659 logmsg "DATA sockfilt for active data channel started (pid $slavepid)\n";
2661 logmsg "====> Active DATA channel connected to client port $port\n";
2666 #**********************************************************************
2667 # datasockf_state is used to change variables that keep state info
2668 # relative to the FTP secondary or data sockfilt process as soon as
2669 # one of the five possible stable states is reached. Variables that
2670 # are modified by this sub may be checked independently but should
2671 # not be changed except by calling this sub.
2673 sub datasockf_state {
2676 if($state eq 'STOPPED') {
2677 # Data sockfilter initial state, not running,
2678 # not connected and not used.
2679 $datasockf_state = $state;
2680 $datasockf_mode = 'none';
2681 $datasockf_runs = 'no';
2682 $datasockf_conn = 'no';
2684 elsif($state eq 'PASSIVE') {
2685 # Data sockfilter accepted connection from client.
2686 $datasockf_state = $state;
2687 $datasockf_mode = 'passive';
2688 $datasockf_runs = 'yes';
2689 $datasockf_conn = 'yes';
2691 elsif($state eq 'ACTIVE') {
2692 # Data sockfilter has connected to client.
2693 $datasockf_state = $state;
2694 $datasockf_mode = 'active';
2695 $datasockf_runs = 'yes';
2696 $datasockf_conn = 'yes';
2698 elsif($state eq 'PASSIVE_NODATACONN') {
2699 # Data sockfilter bound port without listening,
2700 # client won't be able to establish data connection.
2701 $datasockf_state = $state;
2702 $datasockf_mode = 'passive';
2703 $datasockf_runs = 'yes';
2704 $datasockf_conn = 'no';
2706 elsif($state eq 'ACTIVE_NODATACONN') {
2707 # Data sockfilter does not even run,
2708 # client awaits data connection from server in vain.
2709 $datasockf_state = $state;
2710 $datasockf_mode = 'active';
2711 $datasockf_runs = 'no';
2712 $datasockf_conn = 'no';
2715 die "Internal error. Unknown datasockf state: $state!";
2719 #**********************************************************************
2720 # nodataconn_str returns string of efective nodataconn command. Notice
2721 # that $nodataconn may be set alone or in addition to a $nodataconnXXX.
2723 sub nodataconn_str {
2726 $str = 'NODATACONN' if($nodataconn);
2727 $str = 'NODATACONN425' if($nodataconn425);
2728 $str = 'NODATACONN421' if($nodataconn421);
2729 $str = 'NODATACONN150' if($nodataconn150);
2733 #**********************************************************************
2734 # customize configures test server operation for each curl test, reading
2735 # configuration commands/parameters from server commands file each time
2736 # a new client control connection is established with the test server.
2737 # On success returns 1, otherwise zero.
2740 $ctrldelay = 0; # default is no throttling of the ctrl stream
2741 $datadelay = 0; # default is no throttling of the data stream
2742 $retrweirdo = 0; # default is no use of RETRWEIRDO
2743 $retrnosize = 0; # default is no use of RETRNOSIZE
2744 $pasvbadip = 0; # default is no use of PASVBADIP
2745 $nosave = 0; # default is to actually save uploaded data to file
2746 $nodataconn = 0; # default is to establish or accept data channel
2747 $nodataconn425 = 0; # default is to not send 425 without data channel
2748 $nodataconn421 = 0; # default is to not send 421 without data channel
2749 $nodataconn150 = 0; # default is to not send 150 without data channel
2750 @capabilities = (); # default is to not support capability commands
2751 @auth_mechs = (); # default is to not support authentication commands
2752 %fulltextreply = ();#
2753 %commandreply = (); #
2754 %customcount = (); #
2757 open(CUSTOM, "<log/ftpserver.cmd") ||
2760 logmsg "FTPD: Getting commands from log/ftpserver.cmd\n";
2763 if($_ =~ /REPLY \"([A-Z]+ [A-Za-z0-9+-\/=\*. ]+)\" (.*)/) {
2764 $fulltextreply{$1}=eval "qq{$2}";
2765 logmsg "FTPD: set custom reply for $1\n";
2767 elsif($_ =~ /REPLY ([A-Za-z0-9+\/=\*]*) (.*)/) {
2768 $commandreply{$1}=eval "qq{$2}";
2770 logmsg "FTPD: set custom reply for empty command\n";
2773 logmsg "FTPD: set custom reply for $1 command\n";
2776 elsif($_ =~ /COUNT ([A-Z]+) (.*)/) {
2777 # we blank the custom reply for this command when having
2778 # been used this number of times
2779 $customcount{$1}=$2;
2780 logmsg "FTPD: blank custom reply for $1 command after $2 uses\n";
2782 elsif($_ =~ /DELAY ([A-Z]+) (\d*)/) {
2784 logmsg "FTPD: delay reply for $1 with $2 seconds\n";
2786 elsif($_ =~ /SLOWDOWN/) {
2789 logmsg "FTPD: send response with 0.01 sec delay between each byte\n";
2791 elsif($_ =~ /RETRWEIRDO/) {
2792 logmsg "FTPD: instructed to use RETRWEIRDO\n";
2795 elsif($_ =~ /RETRNOSIZE/) {
2796 logmsg "FTPD: instructed to use RETRNOSIZE\n";
2799 elsif($_ =~ /PASVBADIP/) {
2800 logmsg "FTPD: instructed to use PASVBADIP\n";
2803 elsif($_ =~ /NODATACONN425/) {
2804 # applies to both active and passive FTP modes
2805 logmsg "FTPD: instructed to use NODATACONN425\n";
2809 elsif($_ =~ /NODATACONN421/) {
2810 # applies to both active and passive FTP modes
2811 logmsg "FTPD: instructed to use NODATACONN421\n";
2815 elsif($_ =~ /NODATACONN150/) {
2816 # applies to both active and passive FTP modes
2817 logmsg "FTPD: instructed to use NODATACONN150\n";
2821 elsif($_ =~ /NODATACONN/) {
2822 # applies to both active and passive FTP modes
2823 logmsg "FTPD: instructed to use NODATACONN\n";
2826 elsif($_ =~ /CAPA (.*)/) {
2827 logmsg "FTPD: instructed to support CAPABILITY command\n";
2828 @capabilities = split(/ (?!(?:[^" ]|[^"] [^"])+")/, $1);
2829 foreach (@capabilities) {
2830 $_ = $1 if /^"(.*)"$/;
2833 elsif($_ =~ /AUTH (.*)/) {
2834 logmsg "FTPD: instructed to support AUTHENTICATION command\n";
2835 @auth_mechs = split(/ /, $1);
2837 elsif($_ =~ /NOSAVE/) {
2838 # don't actually store the file we upload - to be used when
2839 # uploading insanely huge amounts
2841 logmsg "FTPD: NOSAVE prevents saving of uploaded data\n";
2847 #----------------------------------------------------------------------
2848 #----------------------------------------------------------------------
2849 #--------------------------- END OF SUBS ----------------------------
2850 #----------------------------------------------------------------------
2851 #----------------------------------------------------------------------
2853 #**********************************************************************
2854 # Parse command line options
2858 # --verbose # verbose
2859 # --srcdir # source directory
2860 # --id # server instance number
2861 # --proto # server protocol
2862 # --pidfile # server pid file
2863 # --logfile # server log file
2864 # --ipv4 # server IP version 4
2865 # --ipv6 # server IP version 6
2866 # --port # server listener port
2867 # --addr # server address for listener port binding
2870 if($ARGV[0] eq '--verbose') {
2873 elsif($ARGV[0] eq '--srcdir') {
2879 elsif($ARGV[0] eq '--id') {
2880 if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
2881 $idnum = $1 if($1 > 0);
2885 elsif($ARGV[0] eq '--proto') {
2886 if($ARGV[1] && ($ARGV[1] =~ /^(ftp|imap|pop3|smtp)$/)) {
2891 die "unsupported protocol $ARGV[1]";
2894 elsif($ARGV[0] eq '--pidfile') {
2896 $pidfile = $ARGV[1];
2900 elsif($ARGV[0] eq '--logfile') {
2902 $logfile = $ARGV[1];
2906 elsif($ARGV[0] eq '--ipv4') {
2908 $listenaddr = '127.0.0.1' if($listenaddr eq '::1');
2910 elsif($ARGV[0] eq '--ipv6') {
2912 $listenaddr = '::1' if($listenaddr eq '127.0.0.1');
2914 elsif($ARGV[0] eq '--port') {
2915 if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
2916 $port = $1 if($1 > 1024);
2920 elsif($ARGV[0] eq '--addr') {
2922 my $tmpstr = $ARGV[1];
2923 if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) {
2924 $listenaddr = "$1.$2.$3.$4" if($ipvnum == 4);
2926 elsif($ipvnum == 6) {
2927 $listenaddr = $tmpstr;
2928 $listenaddr =~ s/^\[(.*)\]$/$1/;
2934 print STDERR "\nWarning: ftpserver.pl unknown parameter: $ARGV[0]\n";
2939 #***************************************************************************
2940 # Initialize command line option dependant variables
2944 $srcdir = $ENV{'srcdir'} || '.';
2947 $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
2950 $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
2953 $mainsockf_pidfile = "$path/".
2954 mainsockf_pidfilename($proto, $ipvnum, $idnum);
2955 $mainsockf_logfile =
2956 mainsockf_logfilename($logdir, $proto, $ipvnum, $idnum);
2958 if($proto eq 'ftp') {
2959 $datasockf_pidfile = "$path/".
2960 datasockf_pidfilename($proto, $ipvnum, $idnum);
2961 $datasockf_logfile =
2962 datasockf_logfilename($logdir, $proto, $ipvnum, $idnum);
2965 $srvrname = servername_str($proto, $ipvnum, $idnum);
2967 $idstr = "$idnum" if($idnum > 1);
2969 protocolsetup($proto);
2971 $SIG{INT} = \&exit_signal_handler;
2972 $SIG{TERM} = \&exit_signal_handler;
2976 logmsg sprintf("%s server listens on port IPv${ipvnum}/${port}\n", uc($proto));
2978 open(PID, ">$pidfile");
2982 logmsg("logged pid $$ in $pidfile\n");
2987 # kill previous data connection sockfilt when alive
2988 if($datasockf_runs eq 'yes') {
2989 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
2990 logmsg "DATA sockfilt for $datasockf_mode data channel killed now\n";
2992 datasockf_state('STOPPED');
2995 # We read 'sockfilt' commands.
2999 logmsg "Awaiting input\n";
3000 sysread_or_die(\*SFREAD, \$input, 5);
3002 if($input !~ /^CNCT/) {
3003 # we wait for a connected client
3004 logmsg "MAIN sockfilt said: $input";
3007 logmsg "====> Client connect\n";
3009 set_advisor_read_lock($SERVERLOGS_LOCK);
3010 $serverlogslocked = 1;
3015 &customize(); # read test control instructions
3017 my $welcome = $commandreply{"welcome"};
3019 $welcome = $displaytext{"welcome"};
3022 # clear it after use
3023 $commandreply{"welcome"}="";
3024 if($welcome !~ /\r\n\z/) {
3028 sendcontrol $welcome;
3030 #remove global variables from last connection
3031 if($ftplistparserstate) {
3032 undef $ftplistparserstate;
3035 undef $ftptargetdir;
3039 print STDERR "OUT: $welcome";
3047 # Now we expect to read DATA\n[hex size]\n[prot], where the [prot]
3048 # part only is FTP lingo.
3051 sysread_or_die(\*SFREAD, \$i, 5);
3054 logmsg "MAIN sockfilt said $i";
3063 sysread_or_die(\*SFREAD, \$i, 5);
3066 if($i =~ /^([0-9a-fA-F]{4})\n/) {
3071 read_mainsockf(\$input, $size);
3077 # Loop until command completion
3078 next unless($full =~ /\r\n$/);
3080 # Remove trailing CRLF.
3081 $full =~ s/[\n\r]+$//;
3085 if($proto eq "imap") {
3086 # IMAP is different with its identifier first on the command line
3087 if(($full =~ /^([^ ]+) ([^ ]+) (.*)/) ||
3088 ($full =~ /^([^ ]+) ([^ ]+)/)) {
3089 $cmdid=$1; # set the global variable
3093 # IMAP authentication cancellation
3094 elsif($full =~ /^\*$/) {
3095 # Command id has already been set
3099 # IMAP long "commands" are base64 authentication data
3100 elsif($full =~ /^[A-Z0-9+\/]*={0,2}$/i) {
3101 # Command id has already been set
3106 sendcontrol "$full BAD Command\r\n";
3110 elsif($full =~ /^([A-Z]{3,4})(\s(.*))?$/i) {
3114 elsif($proto eq "pop3") {
3115 # POP3 authentication cancellation
3116 if($full =~ /^\*$/) {
3120 # POP3 long "commands" are base64 authentication data
3121 elsif($full =~ /^[A-Z0-9+\/]*={0,2}$/i) {
3126 sendcontrol "-ERR Unrecognized command\r\n";
3130 elsif($proto eq "smtp") {
3131 # SMTP authentication cancellation
3132 if($full =~ /^\*$/) {
3136 # SMTP long "commands" are base64 authentication data
3137 elsif($full =~ /^[A-Z0-9+\/]{0,512}={0,2}$/i) {
3142 sendcontrol "500 Unrecognized command\r\n";
3147 sendcontrol "500 Unrecognized command\r\n";
3151 logmsg "< \"$full\"\n";
3154 print STDERR "IN: $full\n";
3159 my $delay = $delayreply{$FTPCMD};
3161 # just go sleep this many seconds!
3162 logmsg("Sleep for $delay seconds\n");
3163 my $twentieths = $delay * 20;
3164 while($twentieths--) {
3165 select(undef, undef, undef, 0.05) unless($got_exit_signal);
3169 my $check = 1; # no response yet
3171 # See if there is a custom reply for the full text
3172 my $fulltext = $FTPARG ? $FTPCMD . " " . $FTPARG : $FTPCMD;
3173 my $text = $fulltextreply{$fulltext};
3174 if($text && ($text ne "")) {
3175 sendcontrol "$text\r\n";
3179 # See if there is a custom reply for the command
3180 $text = $commandreply{$FTPCMD};
3181 if($text && ($text ne "")) {
3182 if($customcount{$FTPCMD} && (!--$customcount{$FTPCMD})) {
3183 # used enough times so blank the custom command reply
3184 $commandreply{$FTPCMD}="";
3187 sendcontrol "$text\r\n";
3191 # See if there is any display text for the command
3192 $text = $displaytext{$FTPCMD};
3193 if($text && ($text ne "")) {
3194 if($proto eq 'imap') {
3195 sendcontrol "$cmdid $text\r\n";
3198 sendcontrol "$text\r\n";
3204 # only perform this if we're not faking a reply
3205 my $func = $commandfunc{$FTPCMD};
3207 &$func($FTPARG, $FTPCMD);
3214 logmsg "$FTPCMD wasn't handled!\n";
3215 if($proto eq 'pop3') {
3216 sendcontrol "-ERR $FTPCMD is not dealt with!\r\n";
3218 elsif($proto eq 'imap') {
3219 sendcontrol "$cmdid BAD $FTPCMD is not dealt with!\r\n";
3222 sendcontrol "500 $FTPCMD is not dealt with!\r\n";
3227 logmsg "====> Client disconnected\n";
3229 if($serverlogslocked) {
3230 $serverlogslocked = 0;
3231 clear_advisor_read_lock($SERVERLOGS_LOCK);
3235 killsockfilters($proto, $ipvnum, $idnum, $verbose);
3237 if($serverlogslocked) {
3238 $serverlogslocked = 0;
3239 clear_advisor_read_lock($SERVERLOGS_LOCK);