2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 1998 - 2010, 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 @INC=(@INC, $ENV{'srcdir'}, '.');
43 # sub second timestamping needs Time::HiRes
47 import Time::HiRes qw( gettimeofday );
57 require "directories.pm";
69 #**********************************************************************
72 my $verbose = 0; # set to 1 for debugging
73 my $idstr = ""; # server instance string
74 my $idnum = 1; # server instance number
75 my $ipvnum = 4; # server IPv number (4 or 6)
76 my $proto = 'ftp'; # default server protocol
77 my $srcdir; # directory where ftpserver.pl is located
78 my $srvrname; # server name for presentation purposes
82 my $logdir = $path .'/log';
84 #**********************************************************************
85 # global vars used for server address and primary listener port
87 my $port = 8921; # default primary listener port
88 my $listenaddr = '127.0.0.1'; # default address for listener port
90 #**********************************************************************
91 # global vars used for file names
93 my $pidfile; # server pid file name
94 my $logfile; # server log file name
95 my $mainsockf_pidfile; # pid file for primary connection sockfilt process
96 my $mainsockf_logfile; # log file for primary connection sockfilt process
97 my $datasockf_pidfile; # pid file for secondary connection sockfilt process
98 my $datasockf_logfile; # log file for secondary connection sockfilt process
100 #**********************************************************************
101 # global vars used for server logs advisor read lock handling
103 my $SERVERLOGS_LOCK = 'log/serverlogs.lock';
104 my $serverlogslocked = 0;
106 #**********************************************************************
107 # global vars used for child processes PID tracking
109 my $sfpid; # PID for primary connection sockfilt process
110 my $slavepid; # PID for secondary connection sockfilt process
112 #**********************************************************************
113 # global typeglob filehandle vars to read/write from/to sockfilters
115 local *SFREAD; # used to read from primary connection
116 local *SFWRITE; # used to write to primary connection
117 local *DREAD; # used to read from secondary connection
118 local *DWRITE; # used to write to secondary connection
120 #**********************************************************************
121 # global vars which depend on server protocol selection
123 my %commandfunc; # protocol command specific function callbacks
124 my %displaytext; # text returned to client before callback runs
125 my @welcome; # text returned to client upon connection
127 #**********************************************************************
128 # global vars customized for each test from the server commands file
130 my $ctrldelay; # set if server should throttle ctrl stream
131 my $datadelay; # set if server should throttle data stream
132 my $retrweirdo; # set if ftp server should use RETRWEIRDO
133 my $retrnosize; # set if ftp server should use RETRNOSIZE
134 my $pasvbadip; # set if ftp server should use PASVBADIP
135 my $nosave; # set if ftp server should not save uploaded data
140 #**********************************************************************
141 # global variables for to test ftp wildcardmatching or other test that
142 # need flexible LIST responses.. and corresponding files.
143 # $ftptargetdir is keeping the fake "name" of LIST directory.
144 my $ftplistparserstate;
147 #**********************************************************************
148 # global vars used for signal handling
150 my $got_exit_signal = 0; # set if program should finish execution ASAP
151 my $exit_signal; # first signal handled in exit_signal_handler
153 #**********************************************************************
154 # exit_signal_handler will be triggered to indicate that the program
155 # should finish its execution in a controlled way as soon as possible.
156 # For now, program will also terminate from within this handler.
158 sub exit_signal_handler {
160 # For now, simply mimic old behavior.
161 killsockfilters($proto, $ipvnum, $idnum, $verbose);
163 if($serverlogslocked) {
164 $serverlogslocked = 0;
165 clear_advisor_read_lock($SERVERLOGS_LOCK);
170 #**********************************************************************
171 # logmsg is general message logging subroutine for our test servers.
175 # sub second timestamping needs Time::HiRes
176 if($Time::HiRes::VERSION) {
177 my ($seconds, $usec) = gettimeofday();
178 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
180 $now = sprintf("%02d:%02d:%02d.%06d ", $hour, $min, $sec, $usec);
183 my $seconds = time();
184 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
186 $now = sprintf("%02d:%02d:%02d ", $hour, $min, $sec);
188 if(open(LOGFILEFH, ">>$logfile")) {
189 print LOGFILEFH $now;
196 # append to the server.input file
197 open(INPUT, ">>log/server$idstr.input") ||
198 logmsg "failed to open log/server$idstr.input\n";
203 # use this, open->print->close system only to make the file
204 # open as little as possible, to make the test suite run
205 # better on windows/cygwin
217 $result = sysread($$FH, $$scalar, $length);
219 if(not defined $result) {
220 ($fcaller, $lcaller) = (caller)[1,2];
221 logmsg "Failed to read input\n";
222 logmsg "Error: $srvrname server, sysread error: $!\n";
223 logmsg "Exited from sysread_or_die() at $fcaller " .
224 "line $lcaller. $srvrname server, sysread error: $!\n";
225 killsockfilters($proto, $ipvnum, $idnum, $verbose);
227 if($serverlogslocked) {
228 $serverlogslocked = 0;
229 clear_advisor_read_lock($SERVERLOGS_LOCK);
233 elsif($result == 0) {
234 ($fcaller, $lcaller) = (caller)[1,2];
235 logmsg "Failed to read input\n";
236 logmsg "Error: $srvrname server, read zero\n";
237 logmsg "Exited from sysread_or_die() at $fcaller " .
238 "line $lcaller. $srvrname server, read zero\n";
239 killsockfilters($proto, $ipvnum, $idnum, $verbose);
241 if($serverlogslocked) {
242 $serverlogslocked = 0;
243 clear_advisor_read_lock($SERVERLOGS_LOCK);
252 my $mainsockfcmd = "./server/sockfilt " .
253 "--ipv$ipvnum --port $port " .
254 "--pidfile \"$mainsockf_pidfile\" " .
255 "--logfile \"$mainsockf_logfile\"";
256 $sfpid = open2(*SFREAD, *SFWRITE, $mainsockfcmd);
258 print STDERR "$mainsockfcmd\n" if($verbose);
260 print SFWRITE "PING\n";
262 sysread_or_die(\*SFREAD, \$pong, 5);
264 if($pong !~ /^PONG/) {
265 logmsg "Failed sockfilt command: $mainsockfcmd\n";
266 killsockfilters($proto, $ipvnum, $idnum, $verbose);
268 if($serverlogslocked) {
269 $serverlogslocked = 0;
270 clear_advisor_read_lock($SERVERLOGS_LOCK);
272 die "Failed to start sockfilt!";
280 printf SFWRITE "DATA\n%04x\n", length($l);
286 sub sockfiltsecondary {
289 printf DWRITE "DATA\n%04x\n", length($l);
295 # Send data to the client on the control stream, which happens to be plain
300 # spit it all out at once
304 my $a = join("", @_);
305 my @a = split("", $a);
309 select(undef, undef, undef, 0.01);
320 # Send data to the client on the data stream
326 # spit it all out at once
327 sockfiltsecondary $l;
330 # pause between each byte
332 sockfiltsecondary $_;
333 select(undef, undef, undef, 0.01);
339 #**********************************************************************
340 # protocolsetup initializes the 'displaytext' and 'commandfunc' hashes
341 # for the given protocol. References to protocol command callbacks are
342 # stored in 'commandfunc' hash, and text which will be returned to the
343 # client before the command callback runs is stored in 'displaytext'.
348 if($proto eq 'ftp') {
350 'PORT' => \&PORT_ftp,
351 'EPRT' => \&PORT_ftp,
352 'LIST' => \&LIST_ftp,
353 'NLST' => \&NLST_ftp,
354 'PASV' => \&PASV_ftp,
357 'EPSV' => \&PASV_ftp,
358 'RETR' => \&RETR_ftp,
359 'SIZE' => \&SIZE_ftp,
360 'REST' => \&REST_ftp,
361 'STOR' => \&STOR_ftp,
362 'APPE' => \&STOR_ftp, # append looks like upload
363 'MDTM' => \&MDTM_ftp,
366 'USER' => '331 We are happy you popped in!',
367 'PASS' => '230 Welcome you silly person',
368 'PORT' => '200 You said PORT - I say FINE',
369 'TYPE' => '200 I modify TYPE as you wanted',
370 'LIST' => '150 here comes a directory',
371 'NLST' => '150 here comes a directory',
372 'CWD' => '250 CWD command successful.',
373 'SYST' => '215 UNIX Type: L8', # just fake something
374 'QUIT' => '221 bye bye baby', # just reply something
375 'MKD' => '257 Created your requested directory',
376 'REST' => '350 Yeah yeah we set it there for you',
377 'DELE' => '200 OK OK OK whatever you say',
378 'RNFR' => '350 Received your order. Please provide more',
379 'RNTO' => '250 Ok, thanks. File renaming completed.',
380 'NOOP' => '200 Yes, I\'m very good at doing nothing.',
381 'PBSZ' => '500 PBSZ not implemented',
382 'PROT' => '500 PROT not implemented',
385 '220- _ _ ____ _ '."\r\n",
386 '220- ___| | | | _ \| | '."\r\n",
387 '220- / __| | | | |_) | | '."\r\n",
388 '220- | (__| |_| | _ <| |___ '."\r\n",
389 '220 \___|\___/|_| \_\_____|'."\r\n"
392 elsif($proto eq 'pop3') {
394 'RETR' => \&RETR_pop3,
397 'USER' => '+OK We are happy you popped in!',
398 'PASS' => '+OK Access granted',
399 'QUIT' => '+OK byebye',
402 ' _ _ ____ _ '."\r\n",
403 ' ___| | | | _ \| | '."\r\n",
404 ' / __| | | | |_) | | '."\r\n",
405 ' | (__| |_| | _ <| |___ '."\r\n",
406 ' \___|\___/|_| \_\_____|'."\r\n",
407 '+OK cURL POP3 server ready to serve'."\r\n"
410 elsif($proto eq 'imap') {
412 'FETCH' => \&FETCH_imap,
413 'SELECT' => \&SELECT_imap,
416 'LOGIN' => ' OK We are happy you popped in!',
417 'SELECT' => ' OK selection done',
418 'LOGOUT' => ' OK thanks for the fish',
421 ' _ _ ____ _ '."\r\n",
422 ' ___| | | | _ \| | '."\r\n",
423 ' / __| | | | |_) | | '."\r\n",
424 ' | (__| |_| | _ <| |___ '."\r\n",
425 ' \___|\___/|_| \_\_____|'."\r\n",
426 '* OK cURL IMAP server ready to serve'."\r\n"
429 elsif($proto eq 'smtp') {
431 'DATA' => \&DATA_smtp,
432 'RCPT' => \&RCPT_smtp,
435 'EHLO' => '230 We are happy you popped in!',
436 'MAIL' => '200 Note taken',
437 'RCPT' => '200 Receivers accepted',
438 'QUIT' => '200 byebye',
441 '220- _ _ ____ _ '."\r\n",
442 '220- ___| | | | _ \| | '."\r\n",
443 '220- / __| | | | |_) | | '."\r\n",
444 '220- | (__| |_| | _ <| |___ '."\r\n",
445 '220 \___|\___/|_| \_\_____|'."\r\n"
451 my ($closed)=@_; # non-zero if already disconnected
453 my $datapid = processexists($datasockf_pidfile);
456 logmsg "* disconnect data connection\n";
458 print DWRITE "DISC\n";
460 sysread DREAD, $i, 5;
464 logmsg "data connection already disconnected\n";
466 logmsg "=====> Closed data connection\n";
468 logmsg "* quit sockfilt for data (pid $datapid)\n";
470 print DWRITE "QUIT\n";
471 waitpid($datapid, 0);
472 unlink($datasockf_pidfile) if(-f $datasockf_pidfile);
477 ################ SMTP commands
486 if($smtp_rcpt =~ /^TO:(.*)/) {
493 if($testno eq "<verifiedserver>") {
494 sendcontrol "554 WE ROOLZ: $$\r\n";
495 return 0; # don't wait for data now
498 $testno =~ s/^([^0-9]*)([0-9]+).*/$2/;
499 sendcontrol "354 Show me the mail\r\n";
502 logmsg "===> rcpt $testno was $smtp_rcpt\n";
504 my $filename = "log/upload.$testno";
506 logmsg "Store test number $testno in $filename\n";
508 open(FILE, ">$filename") ||
509 return 0; # failed to open output
515 while (5 == (sysread \*SFREAD, $line, 5)) {
516 if($line eq "DATA\n") {
519 sysread \*SFREAD, $i, 5;
522 if($i =~ /^([0-9a-fA-F]{4})\n/) {
526 sysread \*SFREAD, $line, $size;
529 print FILE $line if(!$nosave);
532 if($raw =~ /\x0d\x0a\x2e\x0d\x0a/) {
533 # end of data marker!
536 logmsg "> Appending $size bytes to file\n";
538 logmsg "Found SMTP EOB marker\n";
542 elsif($line eq "DISC\n") {
548 logmsg "No support for: $line";
553 print FILE "$ulsize bytes would've been stored here\n";
556 sendcontrol "250 OK, data received!\r\n";
557 logmsg "received $ulsize bytes upload\n";
568 ################ IMAP commands
571 # global to allow the command functions to read it
574 # what was picked by SELECT
582 logmsg "SELECT_imap got test $testno\n";
595 logmsg "FETCH_imap got test $testno\n";
599 if($testno =~ /^verifiedserver$/) {
600 # this is the secret command that verifies that this actually is
601 # the curl test server
602 my $response = "WE ROOLZ: $$\r\n";
604 print STDERR "FTPD: We returned proof we are the test server\n";
606 $data[0] = $response;
607 logmsg "return proof we are we\n";
610 logmsg "retrieve a mail\n";
612 $testno =~ s/^([^0-9]*)//;
614 if ($testno > 10000) {
615 $testpart = $testno % 10000;
616 $testno = int($testno / 10000);
620 loadtest("$srcdir/data/test$testno");
622 @data = getpart("reply", "data$testpart");
629 sendcontrol "* FETCH starts {$size}\r\n";
635 sendcontrol "$cmdid OK FETCH completed\r\n";
641 ################ POP3 commands
648 if($testno =~ /^verifiedserver$/) {
649 # this is the secret command that verifies that this actually is
650 # the curl test server
651 my $response = "WE ROOLZ: $$\r\n";
653 print STDERR "FTPD: We returned proof we are the test server\n";
655 $data[0] = $response;
656 logmsg "return proof we are we\n";
659 logmsg "retrieve a mail\n";
661 $testno =~ s/^([^0-9]*)//;
663 if ($testno > 10000) {
664 $testpart = $testno % 10000;
665 $testno = int($testno / 10000);
669 loadtest("$srcdir/data/test$testno");
671 @data = getpart("reply", "data$testpart");
674 sendcontrol "+OK Mail transfer starts\r\n";
680 # end with the magic 5-byte end of mail marker
681 sendcontrol "\r\n.\r\n";
687 ################ FTP commands
692 logmsg "Set REST position to $rest\n"
695 sub switch_directory_goto {
702 if($target_dir eq "") {
705 elsif($target_dir eq "..") {
706 if($ftptargetdir eq "/") {
710 $ftptargetdir =~ s/[[:alnum:]]+\/$//;
714 $ftptargetdir .= $target_dir . "/";
718 sub switch_directory {
719 my $target_dir = $_[0];
721 if($target_dir eq "/") {
725 my @dirs = split("/", $target_dir);
727 switch_directory_goto($_);
733 my ($folder, $fullcommand) = $_[0];
734 switch_directory($folder);
735 if($ftptargetdir =~ /^\/fully_simulated/) {
736 $ftplistparserstate = "enabled";
739 undef $ftplistparserstate;
745 $mydir = $ftptargetdir ? $ftptargetdir : "/";
750 sendcontrol "257 \"$mydir\" is current directory\r\n";
754 # print "150 ASCII data connection for /bin/ls (193.15.23.1,59196) (0 bytes)\r\n";
756 # this is a built-in fake-dir ;-)
757 my @ftpdir=("total 20\r\n",
758 "drwxr-xr-x 8 98 98 512 Oct 22 13:06 .\r\n",
759 "drwxr-xr-x 8 98 98 512 Oct 22 13:06 ..\r\n",
760 "drwxr-xr-x 2 98 98 512 May 2 1996 .NeXT\r\n",
761 "-r--r--r-- 1 0 1 35 Jul 16 1996 README\r\n",
762 "lrwxrwxrwx 1 0 1 7 Dec 9 1999 bin -> usr/bin\r\n",
763 "dr-xr-xr-x 2 0 1 512 Oct 1 1997 dev\r\n",
764 "drwxrwxrwx 2 98 98 512 May 29 16:04 download.html\r\n",
765 "dr-xr-xr-x 2 0 1 512 Nov 30 1995 etc\r\n",
766 "drwxrwxrwx 2 98 1 512 Oct 30 14:33 pub\r\n",
767 "dr-xr-xr-x 5 0 1 512 Oct 1 1997 usr\r\n");
769 if($ftplistparserstate) {
770 @ftpdir = ftp_contentlist($ftptargetdir);
773 logmsg "pass LIST data on data connection\n";
778 sendcontrol "226 ASCII transfer complete\r\n";
783 my @ftpdir=("file", "with space", "fake", "..", " ..", "funny", "README");
784 logmsg "pass NLST data on data connection\n";
789 sendcontrol "226 ASCII transfer complete\r\n";
796 if ($testno > 10000) {
797 $testpart = $testno % 10000;
798 $testno = int($testno / 10000);
801 loadtest("$srcdir/data/test$testno");
803 my @data = getpart("reply", "mdtm");
805 my $reply = $data[0];
806 chomp $reply if($reply);
808 if($reply && ($reply =~ /^[+-]?\d+$/) && ($reply < 0)) {
809 sendcontrol "550 $testno: no such file.\r\n";
812 sendcontrol "$reply\r\n";
815 sendcontrol "500 MDTM: no such command.\r\n";
822 if($ftplistparserstate) {
823 my $size = wildcard_filesize($ftptargetdir, $testno);
825 sendcontrol "550 $testno: No such file or directory.\r\n";
828 sendcontrol "213 $size\r\n";
833 if($testno =~ /^verifiedserver$/) {
834 my $response = "WE ROOLZ: $$\r\n";
835 my $size = length($response);
836 sendcontrol "213 $size\r\n";
840 if($testno =~ /(\d+)\/?$/) {
844 print STDERR "SIZE_ftp: invalid test number: $testno\n";
849 if($testno > 10000) {
850 $testpart = $testno % 10000;
851 $testno = int($testno / 10000);
854 loadtest("$srcdir/data/test$testno");
856 my @data = getpart("reply", "size");
862 sendcontrol "213 $size\r\n";
865 sendcontrol "550 $testno: No such file or directory.\r\n";
870 @data = getpart("reply", "data$testpart");
875 sendcontrol "213 $size\r\n";
878 sendcontrol "550 $testno: No such file or directory.\r\n";
887 if($ftplistparserstate) {
888 my @content = wildcard_getfile($ftptargetdir, $testno);
889 if($content[0] == -1) {
893 my $size = length $content[1];
894 sendcontrol "150 Binary data connection for $testno ($size bytes).\r\n",
895 senddata $content[1];
897 sendcontrol "226 File transfer complete\r\n";
902 if($testno =~ /^verifiedserver$/) {
903 # this is the secret command that verifies that this actually is
904 # the curl test server
905 my $response = "WE ROOLZ: $$\r\n";
906 my $len = length($response);
907 sendcontrol "150 Binary junk ($len bytes).\r\n";
908 senddata "WE ROOLZ: $$\r\n";
910 sendcontrol "226 File transfer complete\r\n";
912 print STDERR "FTPD: We returned proof we are the test server\n";
917 $testno =~ s/^([^0-9]*)//;
919 if ($testno > 10000) {
920 $testpart = $testno % 10000;
921 $testno = int($testno / 10000);
924 loadtest("$srcdir/data/test$testno");
926 my @data = getpart("reply", "data$testpart");
933 my %hash = getpartattr("reply", "data$testpart");
935 if($size || $hash{'sendzero'}) {
938 # move read pointer forward
940 logmsg "REST $rest was removed from size, makes $size left\n";
941 $rest = 0; # reset REST offset again
944 sendcontrol "150 Binary data connection for $testno () ($size bytes).\r\n",
945 "226 File transfer complete\r\n";
952 $retrweirdo=0; # switch off the weirdo again!
955 my $sz = "($size bytes)";
960 sendcontrol "150 Binary data connection for $testno () $sz.\r\n";
967 sendcontrol "226 File transfer complete\r\n";
971 sendcontrol "550 $testno: No such file or directory.\r\n";
979 my $filename = "log/upload.$testno";
981 logmsg "STOR test number $testno in $filename\n";
983 sendcontrol "125 Gimme gimme gimme!\r\n";
985 open(FILE, ">$filename") ||
986 return 0; # failed to open output
991 while (5 == (sysread DREAD, $line, 5)) {
992 if($line eq "DATA\n") {
994 sysread DREAD, $i, 5;
997 if($i =~ /^([0-9a-fA-F]{4})\n/) {
1001 sysread DREAD, $line, $size;
1003 #print STDERR " GOT: $size bytes\n";
1006 print FILE $line if(!$nosave);
1007 logmsg "> Appending $size bytes to file\n";
1009 elsif($line eq "DISC\n") {
1015 logmsg "No support for: $line";
1020 print FILE "$ulsize bytes would've been stored here\n";
1023 close_dataconn($disc);
1024 logmsg "received $ulsize bytes upload\n";
1025 sendcontrol "226 File transfer complete\r\n";
1033 # kill previous data connection sockfilt when alive
1034 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
1036 # We fire up a new sockfilt to do the data transfer for us.
1037 my $datasockfcmd = "./server/sockfilt " .
1038 "--ipv$ipvnum --port 0 " .
1039 "--pidfile \"$datasockf_pidfile\" " .
1040 "--logfile \"$datasockf_logfile\"";
1041 $slavepid = open2(\*DREAD, \*DWRITE, $datasockfcmd);
1043 print DWRITE "PING\n";
1045 sysread_or_die(\*DREAD, \$pong, 5);
1047 if($pong !~ /^PONG/) {
1048 logmsg "failed to run sockfilt for data connection\n";
1049 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
1050 sendcontrol "500 no free ports!\r\n";
1054 logmsg "Run sockfilt for data on pid $slavepid\n";
1056 # Find out what port we listen on
1058 print DWRITE "PORT\n";
1060 # READ the response code
1061 sysread_or_die(\*DREAD, \$i, 5);
1063 # READ the response size
1064 sysread_or_die(\*DREAD, \$i, 5);
1067 if($i =~ /^([0-9a-fA-F]{4})\n/) {
1071 # READ the response data
1072 sysread_or_die(\*DREAD, \$i, $size);
1074 # The data is in the format
1077 if($i =~ /IPv(\d)\/(\d+)/) {
1078 # FIX: deal with IP protocol version
1082 if($cmd ne "EPSV") {
1089 sendcontrol sprintf("227 Entering Passive Mode ($p,%d,%d)\n",
1090 ($pasvport/256), ($pasvport%256));
1094 sendcontrol sprintf("229 Entering Passive Mode (|||%d|)\n", $pasvport);
1098 local $SIG{ALRM} = sub { die "alarm\n" };
1100 # assume swift operations unless explicitly slow
1101 alarm ($datadelay?20:10);
1106 while(sysread(DREAD, $input, 5)) {
1108 if($input !~ /^CNCT/) {
1109 # we wait for a connected client
1110 logmsg "Odd, we got $input from client\n";
1113 logmsg "====> Client DATA connect\n";
1120 logmsg "$srvrname server timed out awaiting data connection ".
1121 "on port $pasvport\n";
1122 logmsg "accept failed or connection not even attempted\n";
1123 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
1127 logmsg "data connection setup on port $pasvport\n";
1133 # Support both PORT and EPRT here. Consider LPRT too.
1136 my ($arg, $cmd) = @_;
1140 # We always ignore the given IP and use localhost.
1142 if($cmd eq "PORT") {
1143 if($arg !~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {
1144 logmsg "bad PORT-line: $arg\n";
1145 sendcontrol "500 silly you, go away\r\n";
1149 $addr = "$1.$2.$3.$4";
1151 # EPRT |2|::1|49706|
1152 elsif(($cmd eq "EPRT") && ($grok_eprt)) {
1153 if($arg !~ /(\d+)\|([^\|]+)\|(\d+)/) {
1154 sendcontrol "500 silly you, go away\r\n";
1157 sendcontrol "200 Thanks for dropping by. We contact you later\r\n";
1162 sendcontrol "500 we don't like $cmd now\r\n";
1166 if(!$port || $port > 65535) {
1167 print STDERR "very illegal PORT number: $port\n";
1171 # We fire up a new sockfilt to do the data transfer for us.
1172 my $datasockfcmd = "./server/sockfilt " .
1173 "--ipv$ipvnum --connect $port --addr \"$addr\" " .
1174 "--pidfile \"$datasockf_pidfile\" " .
1175 "--logfile \"$datasockf_logfile\"";
1176 $slavepid = open2(\*DREAD, \*DWRITE, $datasockfcmd);
1178 print STDERR "$datasockfcmd\n" if($verbose);
1180 print DWRITE "PING\n";
1182 sysread_or_die(\*DREAD, \$pong, 5);
1184 if($pong !~ /^PONG/) {
1185 logmsg "Failed sockfilt for data connection\n";
1186 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
1189 logmsg "====> Client DATA connect to port $port\n";
1194 #**********************************************************************
1195 # customize configures test server operation for each curl test, reading
1196 # configuration commands/parameters from server commands file each time
1197 # a new client control connection is established with the test server.
1198 # On success returns 1, otherwise zero.
1201 $ctrldelay = 0; # default is no throttling of the ctrl stream
1202 $datadelay = 0; # default is no throttling of the data stream
1203 $retrweirdo = 0; # default is no use of RETRWEIRDO
1204 $retrnosize = 0; # default is no use of RETRNOSIZE
1205 $pasvbadip = 0; # default is no use of PASVBADIP
1206 $nosave = 0; # default is to actually save uploaded data to file
1207 %customreply = (); #
1208 %customcount = (); #
1211 open(CUSTOM, "<log/ftpserver.cmd") ||
1214 logmsg "FTPD: Getting commands from log/ftpserver.cmd\n";
1217 if($_ =~ /REPLY ([A-Za-z0-9+\/=]+) (.*)/) {
1218 $customreply{$1}=eval "qq{$2}";
1219 logmsg "FTPD: set custom reply for $1\n";
1221 if($_ =~ /COUNT ([A-Z]+) (.*)/) {
1222 # we blank the customreply for this command when having
1223 # been used this number of times
1224 $customcount{$1}=$2;
1225 logmsg "FTPD: blank custom reply for $1 after $2 uses\n";
1227 elsif($_ =~ /DELAY ([A-Z]+) (\d*)/) {
1229 logmsg "FTPD: delay reply for $1 with $2 seconds\n";
1231 elsif($_ =~ /SLOWDOWN/) {
1234 logmsg "FTPD: send response with 0.01 sec delay between each byte\n";
1236 elsif($_ =~ /RETRWEIRDO/) {
1237 logmsg "FTPD: instructed to use RETRWEIRDO\n";
1240 elsif($_ =~ /RETRNOSIZE/) {
1241 logmsg "FTPD: instructed to use RETRNOSIZE\n";
1244 elsif($_ =~ /PASVBADIP/) {
1245 logmsg "FTPD: instructed to use PASVBADIP\n";
1248 elsif($_ =~ /NOSAVE/) {
1249 # don't actually store the file we upload - to be used when
1250 # uploading insanely huge amounts
1252 logmsg "FTPD: NOSAVE prevents saving of uploaded data\n";
1258 #----------------------------------------------------------------------
1259 #----------------------------------------------------------------------
1260 #--------------------------- END OF SUBS ----------------------------
1261 #----------------------------------------------------------------------
1262 #----------------------------------------------------------------------
1264 #**********************************************************************
1265 # Parse command line options
1269 # --verbose # verbose
1270 # --srcdir # source directory
1271 # --id # server instance number
1272 # --proto # server protocol
1273 # --pidfile # server pid file
1274 # --logfile # server log file
1275 # --ipv4 # server IP version 4
1276 # --ipv6 # server IP version 6
1277 # --port # server listener port
1278 # --addr # server address for listener port binding
1281 if($ARGV[0] eq '--verbose') {
1284 elsif($ARGV[0] eq '--srcdir') {
1290 elsif($ARGV[0] eq '--id') {
1291 if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
1292 $idnum = $1 if($1 > 0);
1296 elsif($ARGV[0] eq '--proto') {
1297 if($ARGV[1] && ($ARGV[1] =~ /^(ftp|imap|pop3|smtp)$/)) {
1302 die "unsupported protocol $ARGV[1]";
1305 elsif($ARGV[0] eq '--pidfile') {
1307 $pidfile = $ARGV[1];
1311 elsif($ARGV[0] eq '--logfile') {
1313 $logfile = $ARGV[1];
1317 elsif($ARGV[0] eq '--ipv4') {
1319 $listenaddr = '127.0.0.1' if($listenaddr eq '::1');
1322 elsif($ARGV[0] eq '--ipv6') {
1324 $listenaddr = '::1' if($listenaddr eq '127.0.0.1');
1327 elsif($ARGV[0] eq '--port') {
1328 if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
1329 $port = $1 if($1 > 1024);
1333 elsif($ARGV[0] eq '--addr') {
1335 my $tmpstr = $ARGV[1];
1336 if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) {
1337 $listenaddr = "$1.$2.$3.$4" if($ipvnum == 4);
1339 elsif($ipvnum == 6) {
1340 $listenaddr = $tmpstr;
1341 $listenaddr =~ s/^\[(.*)\]$/$1/;
1347 print STDERR "\nWarning: ftpserver.pl unknown parameter: $ARGV[0]\n";
1352 #***************************************************************************
1353 # Initialize command line option dependant variables
1357 $srcdir = $ENV{'srcdir'} || '.';
1360 $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
1363 $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
1366 $mainsockf_pidfile = "$path/".
1367 mainsockf_pidfilename($proto, $ipvnum, $idnum);
1368 $mainsockf_logfile =
1369 mainsockf_logfilename($logdir, $proto, $ipvnum, $idnum);
1371 if($proto eq 'ftp') {
1372 $datasockf_pidfile = "$path/".
1373 datasockf_pidfilename($proto, $ipvnum, $idnum);
1374 $datasockf_logfile =
1375 datasockf_logfilename($logdir, $proto, $ipvnum, $idnum);
1378 $srvrname = servername_str($proto, $ipvnum, $idnum);
1380 $idstr = "$idnum" if($idnum > 1);
1382 protocolsetup($proto);
1384 $SIG{INT} = \&exit_signal_handler;
1385 $SIG{TERM} = \&exit_signal_handler;
1389 logmsg sprintf("%s server listens on port IPv${ipvnum}/${port}\n", uc($proto));
1391 open(PID, ">$pidfile");
1395 logmsg("logged pid $$ in $pidfile\n");
1400 # We read 'sockfilt' commands.
1404 logmsg "Awaiting input\n";
1405 sysread_or_die(\*SFREAD, \$input, 5);
1407 if($input !~ /^CNCT/) {
1408 # we wait for a connected client
1409 logmsg "sockfilt said: $input";
1412 logmsg "====> Client connect\n";
1414 set_advisor_read_lock($SERVERLOGS_LOCK);
1415 $serverlogslocked = 1;
1420 killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
1422 &customize(); # read test control instructions
1424 sendcontrol @welcome;
1426 #remove global variables from last connection
1427 if($ftplistparserstate) {
1428 undef $ftplistparserstate;
1431 undef $ftptargetdir;
1436 print STDERR "OUT: $_";
1443 # Now we expect to read DATA\n[hex size]\n[prot], where the [prot]
1444 # part only is FTP lingo.
1447 sysread_or_die(\*SFREAD, \$i, 5);
1450 logmsg "sockfilt said $i";
1459 sysread_or_die(\*SFREAD, \$i, 5);
1462 if($i =~ /^([0-9a-fA-F]{4})\n/) {
1467 sysread SFREAD, $_, $size;
1471 # Remove trailing CRLF.
1477 if($proto eq "imap") {
1478 # IMAP is different with its identifier first on the command line
1479 unless (m/^([^ ]+) ([^ ]+) (.*)/ ||
1480 m/^([^ ]+) ([^ ]+)/) {
1481 sendcontrol "$1 '$_': command not understood.\r\n";
1484 $cmdid=$1; # set the global variable
1488 elsif (m/^([A-Z]{3,4})(\s(.*))?$/i) {
1492 elsif($proto eq "smtp" && m/^[A-Z0-9+\/]{0,512}={0,2}$/i) {
1493 # SMTP long "commands" are base64 authentication data.
1498 sendcontrol "500 '$_': command not understood.\r\n";
1502 logmsg "< \"$full\"\n";
1505 print STDERR "IN: $full\n";
1508 my $delay = $delayreply{$FTPCMD};
1510 # just go sleep this many seconds!
1511 logmsg("Sleep for $delay seconds\n");
1512 my $twentieths = $delay * 20;
1513 while($twentieths--) {
1514 select(undef, undef, undef, 0.05) unless($got_exit_signal);
1519 $text = $customreply{$FTPCMD};
1522 if($text && ($text ne "")) {
1523 if($customcount{$FTPCMD} && (!--$customcount{$FTPCMD})) {
1524 # used enough number of times, now blank the customreply
1525 $customreply{$FTPCMD}="";
1529 $text = $displaytext{$FTPCMD};
1532 if($text && ($text ne "")) {
1533 if($cmdid && ($cmdid ne "")) {
1534 sendcontrol "$cmdid$text\r\n";
1537 sendcontrol "$text\r\n";
1541 $check=1; # no response yet
1544 unless($fake && ($fake ne "")) {
1545 # only perform this if we're not faking a reply
1546 my $func = $commandfunc{$FTPCMD};
1548 &$func($FTPARG, $FTPCMD);
1549 $check=0; # taken care of
1554 logmsg "$FTPCMD wasn't handled!\n";
1555 sendcontrol "500 $FTPCMD is not dealt with!\r\n";
1559 logmsg "====> Client disconnected\n";
1561 if($serverlogslocked) {
1562 $serverlogslocked = 0;
1563 clear_advisor_read_lock($SERVERLOGS_LOCK);
1567 killsockfilters($proto, $ipvnum, $idnum, $verbose);
1569 if($serverlogslocked) {
1570 $serverlogslocked = 0;
1571 clear_advisor_read_lock($SERVERLOGS_LOCK);