Artifactspublic-v0

curl

master
ply4 git calls157 ms309 KiB2 cachedprotocol v2
3387 lines91 KiBexecutableRaw
1#!/usr/bin/env perl
2#***************************************************************************
3# _ _ ____ _
4# Project ___| | | | _ \| |
5# / __| | | | |_) | |
6# | (__| |_| | _ <| |___
7# \___|\___/|_| \_\_____|
8#
9# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11# This software is licensed as described in the file COPYING, which
12# you should have received as part of this distribution. The terms
13# are also available at https://curl.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# SPDX-License-Identifier: curl
24###########################################################################
26# This is a server designed for the curl test suite.
28# In December 2009 we started remaking the server to support more protocols
29# that are similar in spirit. Like POP3, IMAP and SMTP in addition to the FTP
30# it already supported since a long time. Note that it still only supports one
31# protocol per invoke. You need to start multiple servers to support multiple
32# protocols simultaneously.
34# It is meant to exercise curl, it is not meant to be a fully working
35# or even overly standard compliant server.
37# You may optionally specify port on the command line, otherwise it
38# defaults to port 8921.
40# All socket/network/TCP related stuff is done by the 'sockfilt' program.
43use strict;
44use warnings;
46BEGIN {
47 push(@INC, $ENV{'srcdir'}) if(defined $ENV{'srcdir'});
48 push(@INC, ".");
51use IPC::Open2;
52use Digest::MD5;
53use File::Basename;
54use Time::HiRes;
56use directories;
58use getpart qw(
59 getpartattr
60 getpart
61 loadtest
62 );
64use processhelp;
66use serverhelp qw(
67 logmsg
68 $logfile
69 servername_str
70 server_pidfilename
71 server_logfilename
72 server_exe_args
73 mainsockf_pidfilename
74 mainsockf_logfilename
75 datasockf_pidfilename
76 datasockf_logfilename
77 );
79use globalconfig qw(
80 $SERVERCMD
81 $LOCKDIR
82 );
84#**********************************************************************
85# global vars...
87my $verbose = 0; # set to 1 for debugging
88my $idstr = ""; # server instance string
89my $idnum = 1; # server instance number
90my $ipvnum = 4; # server IPv number (4 or 6)
91my $proto = 'ftp'; # default server protocol
92my $srcdir; # directory where ftpserver.pl is located
93my $srvrname; # server name for presentation purposes
94my $cwd_testno; # test case numbers extracted from CWD command
95my $testno = 0; # test case number (read from server.cmd)
96my $path = '.';
97my $logdir = $path . '/log';
98my $piddir;
100#**********************************************************************
101# global vars used for server address and primary listener port
103my $port = 8921; # default primary listener port
104my $listenaddr = '127.0.0.1'; # default address for listener port
106#**********************************************************************
107# global vars used for filenames
109my $PORTFILE = "ftpserver.port"; # server port filename
110my $portfile; # server port file path
111my $pidfile; # server pid filename
112my $mainsockf_pidfile; # pid file for primary connection sockfilt process
113my $mainsockf_logfile; # log file for primary connection sockfilt process
114my $datasockf_pidfile; # pid file for secondary connection sockfilt process
115my $datasockf_logfile; # log file for secondary connection sockfilt process
117#**********************************************************************
118# global vars used for server logs advisor read lock handling
120my $serverlogs_lockfile;
121my $serverlogslocked = 0;
123#**********************************************************************
124# global vars used for child processes PID tracking
126my $sfpid; # PID for primary connection sockfilt process
127my $slavepid; # PID for secondary connection sockfilt process
129#**********************************************************************
130# global typeglob filehandle vars to read/write from/to sockfilters
132local *SFREAD; # used to read from primary connection
133local *SFWRITE; # used to write to primary connection
134local *DREAD; # used to read from secondary connection
135local *DWRITE; # used to write to secondary connection
137my $sockfilt_timeout = 5; # default timeout for sockfilter eXsysreads
139#**********************************************************************
140# global vars which depend on server protocol selection
142my %commandfunc; # protocol command specific function callbacks
143my %displaytext; # text returned to client before callback runs
145#**********************************************************************
146# global vars customized for each test from the server commands file
148my $ctrldelay; # set if server should throttle ctrl stream
149my $datadelay; # set if server should throttle data stream
150my $retrweirdo; # set if FTP server should use RETRWEIRDO
151my $retrnosize; # set if FTP server should use RETRNOSIZE
152my $retrsize; # set if FTP server should use RETRSIZE
153my $pasvbadip; # set if FTP server should use PASVBADIP
154my $nosave; # set if FTP server should not save uploaded data
155my $nodataconn; # set if FTP srvr does not establish or accepts data channel
156my $nodataconn425; # set if FTP srvr does not establish data ch and replies 425
157my $nodataconn421; # set if FTP srvr does not establish data ch and replies 421
158my $nodataconn150; # set if FTP srvr does not establish data ch and replies 150
159my $stalldata; # set if the data connection should accept and stay silent
160my $storeresp;
161my $postfetch;
162my @capabilities; # set if server supports capability commands
163my @auth_mechs; # set if server supports authentication commands
164my %fulltextreply; #
165my %commandreply; #
166my %customcount; #
167my %delayreply; #
169#**********************************************************************
170# global variables for to test FTP wildcardmatching or other test that
171# need flexible LIST responses.. and corresponding files.
172# $ftptargetdir is keeping the fake "name" of LIST directory.
174my $ftplistparserstate;
175my $ftptargetdir = "";
177#**********************************************************************
178# global variables used when running an FTP server to keep state info
179# relative to the secondary or data sockfilt process. Values of these
180# variables should only be modified using datasockf_state() sub, given
181# that they are closely related and relationship is a bit awkward.
183my $datasockf_state = 'STOPPED'; # see datasockf_state() sub
184my $datasockf_mode = 'none'; # ['none', 'active', 'passive']
185my $datasockf_runs = 'no'; # ['no', 'yes']
186my $datasockf_conn = 'no'; # ['no', 'yes']
188#**********************************************************************
189# global vars used for signal handling
191my $got_exit_signal = 0; # set if program should finish execution ASAP
193#**********************************************************************
194# Mail related definitions
196my $TEXT_PASSWORD = "secret";
197my $POP3_TIMESTAMP = "<1972.987654321\@curl>";
199#**********************************************************************
200# exit_signal_handler is triggered to indicate that the program
201# should finish its execution in a controlled way as soon as possible.
202# For now, program also terminates from within this handler.
204sub exit_signal_handler {
205 my $signame = shift;
206 # For now, mimic old behavior.
207 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose);
208 unlink($pidfile);
209 unlink($portfile);
210 if($serverlogslocked) {
211 $serverlogslocked = 0;
212 clear_advisor_read_lock($serverlogs_lockfile);
213 }
214 exit;
217sub ftpmsg {
218 # append to the server.input file
219 open(my $input, ">>", "$logdir/server$idstr.input") or
220 logmsg "failed to open $logdir/server$idstr.input\n";
222 print $input @_;
223 close($input);
225 # use this, open->print->close system only to make the file
226 # open as little as possible, to make the test suite run
227 # better on Windows/Cygwin
230#**********************************************************************
231# eXsysread is a wrapper around perl's sysread() function. This repeats
232# the call to sysread() until it has actually read the complete number
233# of requested bytes or an unrecoverable condition occurs.
234# On success returns a positive value, the number of bytes requested.
235# On failure or timeout returns zero.
237sub eXsysread {
238 my $FH = shift;
239 my $scalar = shift;
240 my $nbytes = shift;
241 my $timeout = shift; # A zero timeout disables eXsysread() time limit
242 #
243 my $time_limited = 0;
244 my $timeout_rest = 0;
245 my $start_time = 0;
246 my $nread = 0;
247 my $rc;
249 $$scalar = "";
251 if((not defined $nbytes) || ($nbytes < 1)) {
252 logmsg "Error: eXsysread() failure: " .
253 "length argument must be positive\n";
254 return 0;
255 }
256 if((not defined $timeout) || ($timeout < 0)) {
257 logmsg "Error: eXsysread() failure: " .
258 "timeout argument must be zero or positive\n";
259 return 0;
260 }
261 if($timeout > 0) {
262 # caller sets eXsysread() time limit
263 $time_limited = 1;
264 $timeout_rest = $timeout;
265 $start_time = int(time());
266 }
268 while($nread < $nbytes) {
269 if($time_limited) {
270 eval {
271 local $SIG{ALRM} = sub { die "alarm\n"; };
272 alarm $timeout_rest;
273 $rc = sysread($FH, $$scalar, $nbytes - $nread, $nread);
274 alarm 0;
275 };
276 $timeout_rest = $timeout - (int(time()) - $start_time);
277 if($timeout_rest < 1) {
278 logmsg "Error: eXsysread() failure: timed out\n";
279 return 0;
280 }
281 }
282 else {
283 $rc = sysread($FH, $$scalar, $nbytes - $nread, $nread);
284 }
285 if($got_exit_signal) {
286 logmsg "Error: eXsysread() failure: signalled to die\n";
287 return 0;
288 }
289 if(not defined $rc) {
290 if($!{EINTR}) {
291 logmsg "Warning: retrying sysread() interrupted system call\n";
292 next;
293 }
294 if($!{EAGAIN}) {
295 logmsg "Warning: retrying sysread() due to EAGAIN\n";
296 next;
297 }
298 if($!{EWOULDBLOCK}) {
299 logmsg "Warning: retrying sysread() due to EWOULDBLOCK\n";
300 next;
301 }
302 logmsg "Error: sysread() failure: $!\n";
303 return 0;
304 }
305 if($rc < 0) {
306 logmsg "Error: sysread() failure: returned negative value $rc\n";
307 return 0;
308 }
309 if($rc == 0) {
310 logmsg "Error: sysread() failure: read zero bytes\n";
311 return 0;
312 }
313 $nread += $rc;
314 }
315 return $nread;
318#**********************************************************************
319# read_mainsockf attempts to read the given amount of output from the
320# sockfilter which is in use for the main or primary connection. This
321# reads untranslated sockfilt lingo which may hold data read from the
322# main or primary socket. On success returns 1, otherwise zero.
324sub read_mainsockf {
325 my $scalar = shift;
326 my $nbytes = shift;
327 my $timeout = shift; # Optional argument, if zero blocks indefinitely
328 my $FH = \*SFREAD;
330 if(not defined $timeout) {
331 $timeout = $sockfilt_timeout + ($nbytes >> 12);
332 }
333 if(eXsysread($FH, $scalar, $nbytes, $timeout) != $nbytes) {
334 my ($fcaller, $lcaller) = (caller)[1, 2];
335 logmsg "Error: read_mainsockf() failure at $fcaller " .
336 "line $lcaller. Due to eXsysread() failure\n";
337 return 0;
338 }
339 return 1;
342#**********************************************************************
343# read_datasockf attempts to read the given amount of output from the
344# sockfilter which is in use for the data or secondary connection. This
345# reads untranslated sockfilt lingo which may hold data read from the
346# data or secondary socket. On success returns 1, otherwise zero.
348sub read_datasockf {
349 my $scalar = shift;
350 my $nbytes = shift;
351 my $timeout = shift; # Optional argument, if zero blocks indefinitely
352 my $FH = \*DREAD;
354 if(not defined $timeout) {
355 $timeout = $sockfilt_timeout + ($nbytes >> 12);
356 }
357 if(eXsysread($FH, $scalar, $nbytes, $timeout) != $nbytes) {
358 my ($fcaller, $lcaller) = (caller)[1, 2];
359 logmsg "Error: read_datasockf() failure at $fcaller " .
360 "line $lcaller. Due to eXsysread() failure\n";
361 return 0;
362 }
363 return 1;
366sub sysread_or_die {
367 my $FH = shift;
368 my $scalar = shift;
369 my $length = shift;
370 my $fcaller;
371 my $lcaller;
372 my $result;
374 $result = sysread($$FH, $$scalar, $length);
376 if(not defined $result) {
377 ($fcaller, $lcaller) = (caller)[1, 2];
378 logmsg "Failed to read input\n";
379 logmsg "Error: $srvrname server, sysread error: $!\n";
380 logmsg "Exited from sysread_or_die() at $fcaller " .
381 "line $lcaller. $srvrname server, sysread error: $!\n";
382 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose);
383 unlink($pidfile);
384 unlink($portfile);
385 if($serverlogslocked) {
386 $serverlogslocked = 0;
387 clear_advisor_read_lock($serverlogs_lockfile);
388 }
389 exit;
390 }
391 elsif($result == 0) {
392 ($fcaller, $lcaller) = (caller)[1, 2];
393 logmsg "Failed to read input\n";
394 logmsg "Error: $srvrname server, read zero\n";
395 logmsg "Exited from sysread_or_die() at $fcaller " .
396 "line $lcaller. $srvrname server, read zero\n";
397 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose);
398 unlink($pidfile);
399 unlink($portfile);
400 if($serverlogslocked) {
401 $serverlogslocked = 0;
402 clear_advisor_read_lock($serverlogs_lockfile);
403 }
404 exit;
405 }
407 return $result;
410sub startsf {
411 my @mainsockfcmd = (server_exe_args('sockfilt'),
412 "--ipv$ipvnum",
413 "--port", $port,
414 "--pidfile", $mainsockf_pidfile,
415 "--portfile", $portfile,
416 "--logfile", $mainsockf_logfile);
417 $sfpid = open2(*SFREAD, *SFWRITE, @mainsockfcmd);
419 print STDERR "@mainsockfcmd\n" if($verbose);
421 print SFWRITE "PING\n";
422 my $pong;
423 sysread_or_die(\*SFREAD, \$pong, 5);
425 if($pong !~ /^PONG/) {
426 logmsg "Failed sockfilt command: @mainsockfcmd\n";
427 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose);
428 unlink($pidfile);
429 unlink($portfile);
430 if($serverlogslocked) {
431 $serverlogslocked = 0;
432 clear_advisor_read_lock($serverlogs_lockfile);
433 }
434 die "Failed to start sockfilt!";
435 }
438#**********************************************************************
439# Returns the given test's reply data
441sub getreplydata {
442 my ($num) = @_;
443 my $testpart = "";
445 $num =~ s/^([^0-9]*)//;
446 if($num && $num > 10000) {
447 $testpart = $num % 10000;
448 }
450 my @data = getpart("reply", "data$testpart");
451 if((!@data) && ($testpart ne "")) {
452 @data = getpart("reply", "data");
453 }
455 return @data;
458sub sockfilt {
459 my $l;
460 foreach $l (@_) {
461 printf SFWRITE "DATA\n%04x\n", length($l);
462 print SFWRITE $l;
463 }
466sub sockfiltsecondary {
467 my $l;
468 foreach $l (@_) {
469 printf DWRITE "DATA\n%04x\n", length($l);
470 print DWRITE $l;
471 }
474#**********************************************************************
475# Send data to the client on the control stream, which happens to be plain
476# stdout.
478sub sendcontrol {
479 if(!$ctrldelay) {
480 # spit it all out at once
481 sockfilt @_;
482 }
483 else {
484 my $a = join("", @_);
485 my @a = split("", $a);
487 for(@a) {
488 sockfilt $_;
489 Time::HiRes::sleep($ctrldelay);
490 }
491 }
492 my $log;
493 foreach $log (@_) {
494 my $l = $log;
495 $l =~ s/\r/[CR]/g;
496 $l =~ s/\n/[LF]/g;
497 logmsg "> \"$l\"\n";
498 }
501#**********************************************************************
502# Send data to the FTP client on the data stream when data connection
503# is actually established. Given that this sub should only be called
504# when a data connection is supposed to be established, calling this
505# without a data connection is an indication of weak logic somewhere.
507sub senddata {
508 my $l;
509 if($datasockf_conn eq 'no') {
510 logmsg "WARNING: Detected data sending attempt without DATA channel\n";
511 foreach $l (@_) {
512 logmsg "WARNING: Data swallowed: $l\n"
513 }
514 return;
515 }
517 foreach $l (@_) {
518 if(!$datadelay) {
519 # spit it all out at once
520 sockfiltsecondary $l;
521 }
522 else {
523 # pause between each byte
524 for(split(//, $l)) {
525 sockfiltsecondary $_;
526 Time::HiRes::sleep($datadelay);
527 }
528 }
529 }
532#**********************************************************************
533# protocolsetup initializes the 'displaytext' and 'commandfunc' hashes
534# for the given protocol. References to protocol command callbacks are
535# stored in 'commandfunc' hash, and text which is returned to the
536# client before the command callback runs is stored in 'displaytext'.
538sub protocolsetup {
539 my $proto = $_[0];
541 if($proto eq 'ftp') {
542 %commandfunc = (
543 'PORT' => \&PORT_ftp,
544 'EPRT' => \&PORT_ftp,
545 'LIST' => \&LIST_ftp,
546 'NLST' => \&NLST_ftp,
547 'PASV' => \&PASV_ftp,
548 'CWD' => \&CWD_ftp,
549 'PWD' => \&PWD_ftp,
550 'EPSV' => \&PASV_ftp,
551 'RETR' => \&RETR_ftp,
552 'SIZE' => \&SIZE_ftp,
553 'REST' => \&REST_ftp,
554 'STOR' => \&STOR_ftp,
555 'APPE' => \&STOR_ftp, # append looks like upload
556 'MDTM' => \&MDTM_ftp,
557 );
558 %displaytext = (
559 'USER' => '331 We are happy you popped in!',
560 'PASS' => '230 Welcome you silly person',
561 'PORT' => '200 You said PORT - I say FINE',
562 'TYPE' => '200 I modify TYPE as you wanted',
563 'LIST' => '150 here comes a directory',
564 'NLST' => '150 here comes a directory',
565 'CWD' => '250 CWD command successful.',
566 'SYST' => '215 UNIX Type: L8', # fake something
567 'QUIT' => '221 bye bye baby', # reply something
568 'MKD' => '257 Created your requested directory',
569 'REST' => '350 Yeah yeah we set it there for you',
570 'DELE' => '200 OK OK OK whatever you say',
571 'RNFR' => '350 Received your order. Please provide more',
572 'RNTO' => '250 Ok, thanks. File renaming completed.',
573 'NOOP' => '200 Yes, I\'m very good at doing nothing.',
574 'PBSZ' => '500 PBSZ not implemented',
575 'PROT' => '500 PROT not implemented',
576 'welcome' => join("",
577 '220- _ _ ____ _' . "\r\n",
578 '220- ___| | | | _ \| |' . "\r\n",
579 '220- / __| | | | |_) | |' . "\r\n",
580 '220- | (__| |_| | _ {| |___' . "\r\n",
581 '220 \___|\___/|_| \_\_____|' . "\r\n")
582 );
583 }
584 elsif($proto eq 'pop3') {
585 %commandfunc = (
586 'APOP' => \&APOP_pop3,
587 'AUTH' => \&AUTH_pop3,
588 'CAPA' => \&CAPA_pop3,
589 'DELE' => \&DELE_pop3,
590 'LIST' => \&LIST_pop3,
591 'NOOP' => \&NOOP_pop3,
592 'PASS' => \&PASS_pop3,
593 'QUIT' => \&QUIT_pop3,
594 'RETR' => \&RETR_pop3,
595 'RSET' => \&RSET_pop3,
596 'STAT' => \&STAT_pop3,
597 'TOP' => \&TOP_pop3,
598 'UIDL' => \&UIDL_pop3,
599 'USER' => \&USER_pop3,
600 );
601 %displaytext = (
602 'welcome' => join("",
603 ' _ _ ____ _' . "\r\n",
604 ' ___| | | | _ \| |' . "\r\n",
605 ' / __| | | | |_) | |' . "\r\n",
606 ' | (__| |_| | _ {| |___' . "\r\n",
607 ' \___|\___/|_| \_\_____|' . "\r\n",
608 '+OK curl POP3 server ready to serve ' . "\r\n")
609 );
610 }
611 elsif($proto eq 'imap') {
612 %commandfunc = (
613 'APPEND' => \&APPEND_imap,
614 'CAPABILITY' => \&CAPABILITY_imap,
615 'CHECK' => \&CHECK_imap,
616 'CLOSE' => \&CLOSE_imap,
617 'COPY' => \&COPY_imap,
618 'CREATE' => \&CREATE_imap,
619 'DELETE' => \&DELETE_imap,
620 'EXAMINE' => \&EXAMINE_imap,
621 'EXPUNGE' => \&EXPUNGE_imap,
622 'FETCH' => \&FETCH_imap,
623 'LIST' => \&LIST_imap,
624 'LSUB' => \&LSUB_imap,
625 'LOGIN' => \&LOGIN_imap,
626 'LOGOUT' => \&LOGOUT_imap,
627 'NOOP' => \&NOOP_imap,
628 'RENAME' => \&RENAME_imap,
629 'SEARCH' => \&SEARCH_imap,
630 'SELECT' => \&SELECT_imap,
631 'STATUS' => \&STATUS_imap,
632 'STORE' => \&STORE_imap,
633 'UID' => \&UID_imap,
634 'IDLE' => \&IDLE_imap,
635 );
636 %displaytext = (
637 'welcome' => join("",
638 ' _ _ ____ _' . "\r\n",
639 ' ___| | | | _ \| |' . "\r\n",
640 ' / __| | | | |_) | |' . "\r\n",
641 ' | (__| |_| | _ {| |___' . "\r\n",
642 ' \___|\___/|_| \_\_____|' . "\r\n",
643 '* OK curl IMAP server ready to serve' . "\r\n")
644 );
645 }
646 elsif($proto eq 'smtp') {
647 %commandfunc = (
648 'DATA' => \&DATA_smtp,
649 'EHLO' => \&EHLO_smtp,
650 'EXPN' => \&EXPN_smtp,
651 'HELO' => \&HELO_smtp,
652 'HELP' => \&HELP_smtp,
653 'MAIL' => \&MAIL_smtp,
654 'NOOP' => \&NOOP_smtp,
655 'RSET' => \&RSET_smtp,
656 'RCPT' => \&RCPT_smtp,
657 'VRFY' => \&VRFY_smtp,
658 'QUIT' => \&QUIT_smtp,
659 );
660 %displaytext = (
661 'welcome' => join("",
662 '220- _ _ ____ _' . "\r\n",
663 '220- ___| | | | _ \| |' . "\r\n",
664 '220- / __| | | | |_) | |' . "\r\n",
665 '220- | (__| |_| | _ {| |___' . "\r\n",
666 '220 \___|\___/|_| \_\_____|' . "\r\n")
667 );
668 }
671# Perform the disconnect handshake with sockfilt on the secondary connection
672# (the only connection we actively disconnect).
673# This involves waiting for the disconnect acknowledgment after the DISC
674# command, while throwing away anything else that might come in before
675# that.
676sub disc_handshake {
677 print DWRITE "DISC\n";
678 my $line;
679 my $nr;
680 while(5 == ($nr = sysread DREAD, $line, 5)) {
681 if($line eq "DATA\n") {
682 # Must read the data bytes to stay in sync
683 my $i;
684 sysread DREAD, $i, 5;
686 my $size = 0;
687 if($i =~ /^([0-9a-fA-F]{4})\n/) {
688 $size = hex($1);
689 }
691 logmsg "> Throwing away $size bytes on closed connection\n";
692 read_datasockf(\$line, $size);
693 }
694 elsif($line eq "DISC\n") {
695 logmsg "Fancy that; client wants to DISC, too\n";
696 printf DWRITE "ACKD\n";
697 }
698 elsif($line eq "ACKD\n") {
699 # Got the ack we were waiting for
700 last;
701 }
702 else {
703 logmsg "Ignoring: $line";
704 # sockfilt should not be sending us any other commands
705 }
706 }
707 if(!defined($nr)) {
708 logmsg "Error: pipe read error ($!) while waiting for ACKD";
709 }
710 elsif($nr <= 0) {
711 logmsg "Error: pipe EOF while waiting for ACKD";
712 }
715sub close_dataconn {
716 my ($closed) = @_; # non-zero if already disconnected
718 my $datapid = processexists($datasockf_pidfile);
720 logmsg "=====> Closing $datasockf_mode DATA connection...\n";
722 if(!$closed) {
723 if($datapid > 0) {
724 logmsg "Server disconnects $datasockf_mode DATA connection\n";
725 disc_handshake();
726 logmsg "Server disconnected $datasockf_mode DATA connection\n";
727 }
728 else {
729 logmsg "Server finds $datasockf_mode DATA connection already " .
730 "disconnected\n";
731 }
732 }
733 else {
734 logmsg "Server knows $datasockf_mode DATA connection is already " .
735 "disconnected\n";
736 }
738 if($datapid > 0) {
739 logmsg "DATA sockfilt for $datasockf_mode data channel quits " .
740 "(pid $datapid)\n";
741 print DWRITE "QUIT\n";
742 pidwait($datapid, 0);
743 unlink($datasockf_pidfile) if(-f $datasockf_pidfile);
744 logmsg "DATA sockfilt for $datasockf_mode data channel quit " .
745 "(pid $datapid)\n";
746 }
747 else {
748 logmsg "DATA sockfilt for $datasockf_mode data channel already " .
749 "dead\n";
750 }
752 logmsg "=====> Closed $datasockf_mode DATA connection\n";
754 datasockf_state('STOPPED');
757################
758################ SMTP commands
759################
761# The type of server (SMTP or ESMTP)
762my $smtp_type;
764# The client (which normally contains the test number)
765my $smtp_client;
767sub EHLO_smtp {
768 my ($client) = @_;
769 my @data;
771 # TODO: Get the IP address of the client connection to use in the
772 # EHLO response when the client does not specify one but for now use
773 # 127.0.0.1
774 if(!$client) {
775 $client = "[127.0.0.1]";
776 }
778 # Set the server type to ESMTP
779 $smtp_type = "ESMTP";
781 # Calculate the EHLO response
782 push @data, "$smtp_type pingpong test server Hello $client";
784 if((@capabilities) || (@auth_mechs)) {
785 my $mechs;
787 for my $c (@capabilities) {
788 push @data, $c;
789 }
791 for my $am (@auth_mechs) {
792 if(!$mechs) {
793 $mechs = $am;
794 }
795 else {
796 $mechs .= " $am";
797 }
798 }
800 if($mechs) {
801 push @data, "AUTH $mechs";
802 }
803 }
805 # Send the EHLO response
806 for(my $i = 0; $i < @data; $i++) {
807 my $d = $data[$i];
809 if($i < @data - 1) {
810 sendcontrol "250-$d\r\n";
811 }
812 else {
813 sendcontrol "250 $d\r\n";
814 }
815 }
817 # Store the client (as it may contain the test number)
818 $smtp_client = $client;
820 return 0;
823sub HELO_smtp {
824 my ($client) = @_;
826 # TODO: Get the IP address of the client connection to use in the HELO
827 # response when the client does not specify one but for now use 127.0.0.1
828 if(!$client) {
829 $client = "[127.0.0.1]";
830 }
832 # Set the server type to SMTP
833 $smtp_type = "SMTP";
835 # Send the HELO response
836 sendcontrol "250 $smtp_type pingpong test server Hello $client\r\n";
838 # Store the client (as it may contain the test number)
839 $smtp_client = $client;
841 return 0;
844sub MAIL_smtp {
845 my ($args) = @_;
847 logmsg "MAIL_smtp got $args\n";
849 if(!$args) {
850 sendcontrol "501 Unrecognized parameter\r\n";
851 }
852 else {
853 my $from;
854 my $size;
855 my $smtputf8 = grep /^SMTPUTF8$/, @capabilities;
856 my @elements = split(/ /, $args);
858 # Get the FROM and SIZE parameters
859 for my $e (@elements) {
860 if($e =~ /^FROM:(.*)$/) {
861 $from = $1;
862 }
863 elsif($e =~ /^SIZE=(\d+)$/) {
864 $size = $1;
865 }
866 }
868 # this server does not "validate" MAIL FROM addresses
869 if(length($from)) {
870 my @found;
871 my $valid = 1;
873 # Check the capabilities for SIZE and if the specified size is
874 # greater than the message size then reject it
875 if(@found = grep /^SIZE (\d+)$/, @capabilities) {
876 if($found[0] =~ /^SIZE (\d+)$/) {
877 if($size > $1) {
878 $valid = 0;
879 }
880 }
881 }
883 if(!$valid) {
884 sendcontrol "552 Message size too large\r\n";
885 }
886 else {
887 sendcontrol "250 Sender OK\r\n";
888 }
889 }
890 else {
891 sendcontrol "501 Invalid address\r\n";
892 }
893 }
895 return 0;
898sub RCPT_smtp {
899 my ($args) = @_;
901 logmsg "RCPT_smtp got $args\n";
903 # Get the TO parameter
904 if($args !~ /^TO:(.*)/) {
905 sendcontrol "501 Unrecognized parameter\r\n";
906 }
907 else {
908 my $smtputf8 = grep /^SMTPUTF8$/, @capabilities;
909 my $to = $1;
911 # Validate the to address (only a valid email address inside <> is
912 # allowed, such as <user@example.com>)
913 if((!$smtputf8 && $to =~
914 /^<([a-zA-Z0-9._%+-]+)\@(([a-zA-Z0-9-]+)\.)+([a-zA-Z]{2,4})>$/) ||
915 ($smtputf8 && $to =~
916 /^<([a-zA-Z0-9\x{80}-\x{ff}._%+-]+)\@(([a-zA-Z0-9\x{80}-\x{ff}-]+)\.)+([a-zA-Z]{2,4})>$/)) {
917 sendcontrol "250 Recipient OK\r\n";
918 }
919 else {
920 sendcontrol "501 Invalid address\r\n";
921 }
922 }
924 return 0;
927sub DATA_smtp {
928 my ($args) = @_;
930 if($args) {
931 sendcontrol "501 Unrecognized parameter\r\n";
932 }
933 elsif($smtp_client !~ /^(\d*)$/) {
934 sendcontrol "501 Invalid arguments\r\n";
935 }
936 else {
937 sendcontrol "354 Show me the mail\r\n";
939 my $testno = $smtp_client;
940 my $filename = "$logdir/upload.$testno";
942 logmsg "Store test number $testno in $filename\n";
944 open(my $file, ">", $filename) or
945 return 0; # failed to open output
947 my $line;
948 my $ulsize = 0;
949 my $disc = 0;
950 my $raw;
951 while(5 == (sysread \*SFREAD, $line, 5)) {
952 if($line eq "DATA\n") {
953 my $i;
954 my $eob;
955 sysread \*SFREAD, $i, 5;
957 my $size = 0;
958 if($i =~ /^([0-9a-fA-F]{4})\n/) {
959 $size = hex($1);
960 }
962 read_mainsockf(\$line, $size);
964 $ulsize += $size;
965 print $file $line if(!$nosave);
967 $raw .= $line;
968 if($raw =~ /(?:^|\x0d\x0a)\x2e\x0d\x0a/) {
969 # end of data marker!
970 $eob = 1;
971 }
973 logmsg "> Appending $size bytes to file\n";
975 if($eob) {
976 logmsg "Found SMTP EOB marker\n";
977 last;
978 }
979 }
980 elsif($line eq "DISC\n") {
981 # disconnect!
982 $disc = 1;
983 printf SFWRITE "ACKD\n";
984 last;
985 }
986 else {
987 logmsg "No support for: $line";
988 last;
989 }
990 }
992 if($nosave) {
993 print $file "$ulsize bytes would have been stored here\n";
994 }
996 close($file);
998 logmsg "received $ulsize bytes upload\n";
1000 sendcontrol "250 OK, data received!\r\n";
1003 return 0;
1006sub NOOP_smtp {
1007 my ($args) = @_;
1009 if($args) {
1010 sendcontrol "501 Unrecognized parameter\r\n";
1012 else {
1013 sendcontrol "250 OK\r\n";
1016 return 0;
1019sub RSET_smtp {
1020 my ($args) = @_;
1022 if($args) {
1023 sendcontrol "501 Unrecognized parameter\r\n";
1025 else {
1026 sendcontrol "250 Resetting\r\n";
1029 return 0;
1032sub HELP_smtp {
1033 my ($args) = @_;
1035 # One argument is optional
1036 if($args) {
1037 logmsg "HELP_smtp got $args\n";
1040 if($smtp_client eq "verifiedserver") {
1041 # This is the secret command that verifies that this actually is
1042 # the curl test server
1043 sendcontrol "214 WE ROOLZ: $$\r\n";
1045 if($verbose) {
1046 print STDERR "FTPD: We returned proof we are the test server\n";
1049 logmsg "return proof we are we\n";
1051 else {
1052 sendcontrol "214-This server supports the following commands:\r\n";
1054 if(@auth_mechs) {
1055 sendcontrol "214 HELO EHLO RCPT DATA RSET MAIL VRFY EXPN QUIT HELP AUTH\r\n";
1057 else {
1058 sendcontrol "214 HELO EHLO RCPT DATA RSET MAIL VRFY EXPN QUIT HELP\r\n";
1062 return 0;
1065sub VRFY_smtp {
1066 my ($args) = @_;
1067 my ($username, $address) = split(/ /, $args, 2);
1069 logmsg "VRFY_smtp got $args\n";
1071 if($username eq "") {
1072 sendcontrol "501 Unrecognized parameter\r\n";
1074 else {
1075 my $smtputf8 = grep /^SMTPUTF8$/, @capabilities;
1077 # Validate the username (only a valid local or external username is
1078 # allowed, such as user or user@example.com)
1079 if((!$smtputf8 && $username =~
1080 /^([a-zA-Z0-9._%+-]+)(\@(([a-zA-Z0-9-]+)\.)+([a-zA-Z]{2,4}))?$/) ||
1081 ($smtputf8 && $username =~
1082 /^([a-zA-Z0-9\x{80}-\x{ff}._%+-]+)(\@(([a-zA-Z0-9\x{80}-\x{ff}-]+)\.)+([a-zA-Z]{2,4}))?$/)) {
1084 my @data = getreplydata($smtp_client);
1086 if(!@data) {
1087 if($username !~
1088 /^([a-zA-Z0-9._%+-]+)\@(([a-zA-Z0-9-]+)\.)+([a-zA-Z]{2,4})$/) {
1089 push @data, "250 <$username\@example.com>\r\n"
1091 else {
1092 push @data, "250 <$username>\r\n"
1096 for my $d (@data) {
1097 sendcontrol $d;
1100 else {
1101 sendcontrol "501 Invalid address\r\n";
1105 return 0;
1108sub EXPN_smtp {
1109 my ($list_name) = @_;
1111 logmsg "EXPN_smtp got $list_name\n";
1113 if(!$list_name) {
1114 sendcontrol "501 Unrecognized parameter\r\n";
1116 else {
1117 my @data = getreplydata($smtp_client);
1119 for my $d (@data) {
1120 sendcontrol $d;
1124 return 0;
1127sub QUIT_smtp {
1128 sendcontrol "221 curl $smtp_type server signing off\r\n";
1130 return 0;
1133# What was deleted by IMAP STORE / POP3 DELE commands
1134my @deleted;
1136################
1137################ IMAP commands
1138################
1140# global to allow the command functions to read it
1141my $cmdid;
1143# what was picked by SELECT
1144my $selected;
1146# Any IMAP parameter can come in escaped and in double quotes.
1147# This function is dumb (so far) and removes the quotes if present.
1148sub fix_imap_params {
1149 foreach (@_) {
1150 $_ = $1 if /^"(.*)"$/;
1154sub CAPABILITY_imap {
1155 if((!@capabilities) && (!@auth_mechs)) {
1156 sendcontrol "$cmdid BAD Command\r\n";
1158 else {
1159 my $data;
1161 # Calculate the CAPABILITY response
1162 $data = "* CAPABILITY IMAP4";
1164 for my $c (@capabilities) {
1165 $data .= " $c";
1168 for my $am (@auth_mechs) {
1169 $data .= " AUTH=$am";
1172 $data .= " pingpong test server\r\n";
1174 # Send the CAPABILITY response
1175 sendcontrol $data;
1176 sendcontrol "$cmdid OK CAPABILITY completed\r\n";
1179 return 0;
1182sub LOGIN_imap {
1183 my ($args) = @_;
1184 my ($user, $password) = split(/ /, $args, 2);
1185 fix_imap_params($user, $password);
1187 logmsg "LOGIN_imap got $args\n";
1189 if($user eq "") {
1190 sendcontrol "$cmdid BAD Command Argument\r\n";
1192 else {
1193 sendcontrol "$cmdid OK LOGIN completed\r\n";
1196 return 0;
1199sub SELECT_imap {
1200 my ($mailbox) = @_;
1201 fix_imap_params($mailbox);
1203 logmsg "SELECT_imap got test $mailbox\n";
1205 if($mailbox eq "") {
1206 sendcontrol "$cmdid BAD Command Argument\r\n";
1208 else {
1209 # Example from RFC 3501, 6.3.1. SELECT Command
1210 sendcontrol "* 172 EXISTS\r\n";
1211 sendcontrol "* 1 RECENT\r\n";
1212 sendcontrol "* OK [UNSEEN 12] Message 12 is first unseen\r\n";
1213 sendcontrol "* OK [UIDVALIDITY 3857529045] UIDs valid\r\n";
1214 sendcontrol "* OK [UIDNEXT 4392] Predicted next UID\r\n";
1215 sendcontrol "* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)\r\n";
1216 sendcontrol "* OK [PERMANENTFLAGS (\\Deleted \\Seen \\*)] Limited\r\n";
1217 sendcontrol "$cmdid OK [READ-WRITE] SELECT completed\r\n";
1219 $selected = $mailbox;
1222 return 0;
1225sub FETCH_imap {
1226 my ($args) = @_;
1227 my ($uid, $how) = split(/ /, $args, 2);
1228 fix_imap_params($uid, $how);
1230 logmsg "FETCH_imap got $args\n";
1232 if($selected eq "") {
1233 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1235 else {
1236 my @data;
1237 my $size;
1239 if($selected eq "verifiedserver") {
1240 # this is the secret command that verifies that this actually is
1241 # the curl test server
1242 my $response = "WE ROOLZ: $$\r\n";
1243 if($verbose) {
1244 print STDERR "FTPD: We returned proof we are the test server\n";
1246 $data[0] = $response;
1247 logmsg "return proof we are we\n";
1249 else {
1250 # send mail content
1251 logmsg "retrieve a mail\n";
1253 @data = getreplydata($selected);
1256 for(@data) {
1257 $size += length($_);
1260 sendcontrol "* $uid FETCH ($how {$size}\r\n";
1262 for my $d (@data) {
1263 sendcontrol $d;
1266 # Set the custom extra header content with POSTFETCH
1267 sendcontrol "$postfetch)\r\n";
1268 sendcontrol "$cmdid OK FETCH completed\r\n";
1271 return 0;
1274sub APPEND_imap {
1275 my ($args) = @_;
1277 logmsg "APPEND_imap got $args\n";
1279 $args =~ /^([^ ]+) [^{]*\{(\d+)\}$/;
1280 my ($mailbox, $size) = ($1, $2);
1281 fix_imap_params($mailbox);
1283 if($mailbox eq "") {
1284 sendcontrol "$cmdid BAD Command Argument\r\n";
1286 else {
1287 sendcontrol "+ Ready for literal data\r\n";
1289 my $testno = $mailbox;
1290 my $filename = "$logdir/upload.$testno";
1292 logmsg "Store test number $testno in $filename\n";
1294 open(my $file, ">", $filename) or
1295 return 0; # failed to open output
1297 my $received = 0;
1298 my $line;
1299 while(5 == (sysread \*SFREAD, $line, 5)) {
1300 if($line eq "DATA\n") {
1301 sysread \*SFREAD, $line, 5;
1303 my $chunksize = 0;
1304 if($line =~ /^([0-9a-fA-F]{4})\n/) {
1305 $chunksize = hex($1);
1308 read_mainsockf(\$line, $chunksize);
1310 my $left = $size - $received;
1311 my $datasize = ($left > $chunksize) ? $chunksize : $left;
1313 if($datasize > 0) {
1314 logmsg "> Appending $datasize bytes to file\n";
1315 print $file substr($line, 0, $datasize) if(!$nosave);
1316 $line = substr($line, $datasize);
1318 $received += $datasize;
1319 if($received == $size) {
1320 logmsg "Received all data, waiting for final CRLF.\n";
1324 if($received == $size && $line eq "\r\n") {
1325 last;
1328 elsif($line eq "DISC\n") {
1329 logmsg "Unexpected disconnect!\n";
1330 printf SFWRITE "ACKD\n";
1331 last;
1333 else {
1334 logmsg "No support for: $line";
1335 last;
1339 if($nosave) {
1340 print $file "$size bytes would have been stored here\n";
1343 close($file);
1345 logmsg "received $size bytes upload\n";
1347 sendcontrol "$cmdid OK APPEND completed\r\n";
1350 return 0;
1353sub STORE_imap {
1354 my ($args) = @_;
1355 my ($uid, $what, $value) = split(/ /, $args, 3);
1356 fix_imap_params($uid);
1358 logmsg "STORE_imap got $args\n";
1360 if($selected eq "") {
1361 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1363 elsif(($uid eq "") || ($what ne "+Flags") || ($value eq "")) {
1364 sendcontrol "$cmdid BAD Command Argument\r\n";
1366 else {
1367 if($value eq "\\Deleted") {
1368 push(@deleted, $uid);
1371 sendcontrol "* $uid FETCH (FLAGS (\\Seen $value))\r\n";
1372 sendcontrol "$cmdid OK STORE completed\r\n";
1375 return 0;
1378sub LIST_imap {
1379 my ($args) = @_;
1380 my ($reference, $mailbox) = split(/ /, $args, 2);
1381 fix_imap_params($reference, $mailbox);
1383 logmsg "LIST_imap got $args\n";
1385 if($reference eq "") {
1386 sendcontrol "$cmdid BAD Command Argument\r\n";
1388 elsif($reference eq "verifiedserver") {
1389 # this is the secret command that verifies that this actually is
1390 # the curl test server
1391 sendcontrol "* LIST () \"/\" \"WE ROOLZ: $$\"\r\n";
1392 sendcontrol "$cmdid OK LIST Completed\r\n";
1394 if($verbose) {
1395 print STDERR "FTPD: We returned proof we are the test server\n";
1398 logmsg "return proof we are we\n";
1400 else {
1401 my @data = getreplydata($reference);
1403 for my $d (@data) {
1404 sendcontrol $d;
1407 sendcontrol "$cmdid OK LIST Completed\r\n";
1410 return 0;
1413sub LSUB_imap {
1414 my ($args) = @_;
1415 my ($reference, $mailbox) = split(/ /, $args, 2);
1416 fix_imap_params($reference, $mailbox);
1418 logmsg "LSUB_imap got $args\n";
1420 if($reference eq "") {
1421 sendcontrol "$cmdid BAD Command Argument\r\n";
1423 else {
1424 my @data = getreplydata($reference);
1426 for my $d (@data) {
1427 sendcontrol $d;
1430 sendcontrol "$cmdid OK LSUB Completed\r\n";
1433 return 0;
1436sub EXAMINE_imap {
1437 my ($mailbox) = @_;
1438 fix_imap_params($mailbox);
1440 logmsg "EXAMINE_imap got $mailbox\n";
1442 if($mailbox eq "") {
1443 sendcontrol "$cmdid BAD Command Argument\r\n";
1445 else {
1446 my @data = getreplydata($mailbox);
1448 for my $d (@data) {
1449 sendcontrol $d;
1452 sendcontrol "$cmdid OK [READ-ONLY] EXAMINE completed\r\n";
1455 return 0;
1458sub STATUS_imap {
1459 my ($args) = @_;
1460 my ($mailbox, $what) = split(/ /, $args, 2);
1461 fix_imap_params($mailbox);
1463 logmsg "STATUS_imap got $args\n";
1465 if($mailbox eq "") {
1466 sendcontrol "$cmdid BAD Command Argument\r\n";
1468 else {
1469 my @data = getreplydata($mailbox);
1471 for my $d (@data) {
1472 sendcontrol $d;
1475 sendcontrol "$cmdid OK STATUS completed\r\n";
1478 return 0;
1481sub SEARCH_imap {
1482 my ($what) = @_;
1483 fix_imap_params($what);
1485 logmsg "SEARCH_imap got $what\n";
1487 if($selected eq "") {
1488 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1490 elsif($what eq "") {
1491 sendcontrol "$cmdid BAD Command Argument\r\n";
1493 else {
1494 my @data = getreplydata($selected);
1496 for my $d (@data) {
1497 sendcontrol $d;
1500 sendcontrol "$cmdid OK SEARCH completed\r\n";
1503 return 0;
1506sub CREATE_imap {
1507 my ($args) = @_;
1508 fix_imap_params($args);
1510 logmsg "CREATE_imap got $args\n";
1512 if($args eq "") {
1513 sendcontrol "$cmdid BAD Command Argument\r\n";
1515 else {
1516 sendcontrol "$cmdid OK CREATE completed\r\n";
1519 return 0;
1522sub DELETE_imap {
1523 my ($args) = @_;
1524 fix_imap_params($args);
1526 logmsg "DELETE_imap got $args\n";
1528 if($args eq "") {
1529 sendcontrol "$cmdid BAD Command Argument\r\n";
1531 else {
1532 sendcontrol "$cmdid OK DELETE completed\r\n";
1535 return 0;
1538sub RENAME_imap {
1539 my ($args) = @_;
1540 my ($from_mailbox, $to_mailbox) = split(/ /, $args, 2);
1541 fix_imap_params($from_mailbox, $to_mailbox);
1543 logmsg "RENAME_imap got $args\n";
1545 if(($from_mailbox eq "") || ($to_mailbox eq "")) {
1546 sendcontrol "$cmdid BAD Command Argument\r\n";
1548 else {
1549 sendcontrol "$cmdid OK RENAME completed\r\n";
1552 return 0;
1555sub CHECK_imap {
1556 if($selected eq "") {
1557 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1559 else {
1560 sendcontrol "$cmdid OK CHECK completed\r\n";
1563 return 0;
1566sub CLOSE_imap {
1567 if($selected eq "") {
1568 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1570 elsif(!@deleted) {
1571 sendcontrol "$cmdid BAD Command Argument\r\n";
1573 else {
1574 sendcontrol "$cmdid OK CLOSE completed\r\n";
1576 @deleted = ();
1579 return 0;
1582sub EXPUNGE_imap {
1583 if($selected eq "") {
1584 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1586 else {
1587 if(!@deleted) {
1588 # Report the number of existing messages as per the SELECT
1589 # command
1590 sendcontrol "* 172 EXISTS\r\n";
1592 else {
1593 # Report the message UIDs being deleted
1594 for my $d (@deleted) {
1595 sendcontrol "* $d EXPUNGE\r\n";
1598 @deleted = ();
1601 sendcontrol "$cmdid OK EXPUNGE completed\r\n";
1604 return 0;
1607sub COPY_imap {
1608 my ($args) = @_;
1609 my ($uid, $mailbox) = split(/ /, $args, 2);
1610 fix_imap_params($uid, $mailbox);
1612 logmsg "COPY_imap got $args\n";
1614 if(($uid eq "") || ($mailbox eq "")) {
1615 sendcontrol "$cmdid BAD Command Argument\r\n";
1617 else {
1618 sendcontrol "$cmdid OK COPY completed\r\n";
1621 return 0;
1624sub IDLE_imap {
1625 logmsg "IDLE received\n";
1627 sendcontrol "+ entering idle mode\r\n";
1628 return 0;
1631sub UID_imap {
1632 my ($args) = @_;
1633 my ($command) = split(/ /, $args, 1);
1634 fix_imap_params($command);
1636 logmsg "UID_imap got $args\n";
1638 if($selected eq "") {
1639 sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
1641 elsif(substr($command, 0, 5) eq "FETCH"){
1642 my $func = $commandfunc{"FETCH"};
1643 if($func) {
1644 &$func($args, $command);
1647 elsif(($command ne "COPY") &&
1648 ($command ne "STORE") && ($command ne "SEARCH")) {
1649 sendcontrol "$cmdid BAD Command Argument\r\n";
1651 else {
1652 my @data = getreplydata($selected);
1654 for my $d (@data) {
1655 sendcontrol $d;
1658 sendcontrol "$cmdid OK $command completed\r\n";
1661 return 0;
1664sub NOOP_imap {
1665 my ($args) = @_;
1666 my @data = (
1667 "* 22 EXPUNGE\r\n",
1668 "* 23 EXISTS\r\n",
1669 "* 3 RECENT\r\n",
1670 "* 14 FETCH (FLAGS (\\Seen \\Deleted))\r\n",
1671 );
1673 if($args) {
1674 sendcontrol "$cmdid BAD Command Argument\r\n";
1676 else {
1677 for my $d (@data) {
1678 sendcontrol $d;
1681 sendcontrol "$cmdid OK NOOP completed\r\n";
1684 return 0;
1687sub LOGOUT_imap {
1688 sendcontrol "* BYE curl IMAP server signing off\r\n";
1689 sendcontrol "$cmdid OK LOGOUT completed\r\n";
1691 return 0;
1694################
1695################ POP3 commands
1696################
1698# Who is attempting to log in
1699my $username;
1701sub CAPA_pop3 {
1702 my @list = ();
1703 my $mechs;
1705 # Calculate the capability list based on the specified capabilities
1706 # (except APOP) and any authentication mechanisms
1707 for my $c (@capabilities) {
1708 push @list, "$c\r\n" unless $c eq "APOP";
1711 for my $am (@auth_mechs) {
1712 if(!$mechs) {
1713 $mechs = $am;
1715 else {
1716 $mechs .= " $am";
1720 if($mechs) {
1721 push @list, "SASL $mechs\r\n";
1724 if(!@list) {
1725 sendcontrol "-ERR Unrecognized command\r\n";
1727 else {
1728 my @data = ();
1730 # Calculate the CAPA response
1731 push @data, "+OK List of capabilities follows\r\n";
1733 for my $l (@list) {
1734 push @data, "$l\r\n";
1737 push @data, "IMPLEMENTATION POP3 pingpong test server\r\n";
1739 # Send the CAPA response
1740 for my $d (@data) {
1741 sendcontrol $d;
1744 # End with the magic 3-byte end of listing marker
1745 sendcontrol ".\r\n";
1748 return 0;
1751sub APOP_pop3 {
1752 my ($args) = @_;
1753 my ($user, $secret) = split(/ /, $args, 2);
1755 if(!grep /^APOP$/, @capabilities) {
1756 sendcontrol "-ERR Unrecognized command\r\n";
1758 elsif(($user eq "") || ($secret eq "")) {
1759 sendcontrol "-ERR Protocol error\r\n";
1761 else {
1762 my $digest = Digest::MD5::md5_hex($POP3_TIMESTAMP, $TEXT_PASSWORD);
1764 if($secret ne $digest) {
1765 sendcontrol "-ERR Login failure\r\n";
1767 else {
1768 sendcontrol "+OK Login successful\r\n";
1772 return 0;
1775sub AUTH_pop3 {
1776 if(!@auth_mechs) {
1777 sendcontrol "-ERR Unrecognized command\r\n";
1779 else {
1780 my @data = ();
1782 # Calculate the AUTH response
1783 push @data, "+OK List of supported mechanisms follows\r\n";
1785 for my $am (@auth_mechs) {
1786 push @data, "$am\r\n";
1789 # Send the AUTH response
1790 for my $d (@data) {
1791 sendcontrol $d;
1794 # End with the magic 3-byte end of listing marker
1795 sendcontrol ".\r\n";
1798 return 0;
1801sub USER_pop3 {
1802 my ($user) = @_;
1804 logmsg "USER_pop3 got $user\n";
1806 if(!$user) {
1807 sendcontrol "-ERR Protocol error\r\n";
1809 else {
1810 $username = $user;
1812 sendcontrol "+OK\r\n";
1815 return 0;
1818sub PASS_pop3 {
1819 my ($password) = @_;
1821 logmsg "PASS_pop3 got $password\n";
1823 sendcontrol "+OK Login successful\r\n";
1825 return 0;
1828sub RETR_pop3 {
1829 my ($msgid) = @_;
1830 my @data;
1832 if($msgid =~ /^verifiedserver$/) {
1833 # this is the secret command that verifies that this actually is
1834 # the curl test server
1835 my $response = "WE ROOLZ: $$\r\n";
1836 if($verbose) {
1837 print STDERR "FTPD: We returned proof we are the test server\n";
1839 $data[0] = $response;
1840 logmsg "return proof we are we\n";
1842 else {
1843 # send mail content
1844 logmsg "retrieve a mail\n";
1846 @data = getreplydata($msgid);
1849 sendcontrol "+OK Mail transfer starts\r\n";
1851 for my $d (@data) {
1852 sendcontrol $d;
1855 # end with the magic 3-byte end of mail marker, assumes that the
1856 # mail body ends with a CRLF!
1857 sendcontrol ".\r\n";
1859 return 0;
1862sub LIST_pop3 {
1863 my @data = getpart("reply", "data");
1865 logmsg "retrieve a message list\n";
1867 sendcontrol "+OK Listing starts\r\n";
1869 for my $d (@data) {
1870 sendcontrol $d;
1873 # End with the magic 3-byte end of listing marker
1874 sendcontrol ".\r\n";
1876 return 0;
1879sub DELE_pop3 {
1880 my ($msgid) = @_;
1882 logmsg "DELE_pop3 got $msgid\n";
1884 if(!$msgid) {
1885 sendcontrol "-ERR Protocol error\r\n";
1887 else {
1888 push (@deleted, $msgid);
1890 sendcontrol "+OK\r\n";
1893 return 0;
1896sub STAT_pop3 {
1897 my ($args) = @_;
1899 if($args) {
1900 sendcontrol "-ERR Protocol error\r\n";
1902 else {
1903 # Send statistics for the built-in fake message list as
1904 # detailed in the LIST_pop3 function above
1905 sendcontrol "+OK 3 4294967800\r\n";
1908 return 0;
1911sub NOOP_pop3 {
1912 my ($args) = @_;
1914 if($args) {
1915 sendcontrol "-ERR Protocol error\r\n";
1917 else {
1918 sendcontrol "+OK\r\n";
1921 return 0;
1924sub UIDL_pop3 {
1925 # This is a built-in fake-message UID list
1926 my @data = (
1927 "1 1\r\n",
1928 "2 2\r\n",
1929 "3 4\r\n", # Note that UID 3 is a simulated "deleted" message
1930 );
1932 if(!grep /^UIDL$/, @capabilities) {
1933 sendcontrol "-ERR Unrecognized command\r\n";
1935 else {
1936 logmsg "retrieve a message UID list\n";
1938 sendcontrol "+OK Listing starts\r\n";
1940 for my $d (@data) {
1941 sendcontrol $d;
1944 # End with the magic 3-byte end of listing marker
1945 sendcontrol ".\r\n";
1948 return 0;
1951sub TOP_pop3 {
1952 my ($args) = @_;
1953 my ($msgid, $lines) = split(/ /, $args, 2);
1955 logmsg "TOP_pop3 got $args\n";
1957 if(!grep /^TOP$/, @capabilities) {
1958 sendcontrol "-ERR Unrecognized command\r\n";
1960 elsif(($msgid eq "") || ($lines eq "")) {
1961 sendcontrol "-ERR Protocol error\r\n";
1963 else {
1964 if($lines == "0") {
1965 logmsg "retrieve header of mail\n";
1967 else {
1968 logmsg "retrieve top $lines lines of mail\n";
1971 my @data = getreplydata($msgid);
1973 sendcontrol "+OK Mail transfer starts\r\n";
1975 # Send mail content
1976 for my $d (@data) {
1977 sendcontrol $d;
1980 # End with the magic 3-byte end of mail marker, assumes that the
1981 # mail body ends with a CRLF!
1982 sendcontrol ".\r\n";
1985 return 0;
1988sub RSET_pop3 {
1989 my ($args) = @_;
1991 if($args) {
1992 sendcontrol "-ERR Protocol error\r\n";
1994 else {
1995 if(@deleted) {
1996 logmsg "resetting @deleted message(s)\n";
1998 @deleted = ();
2001 sendcontrol "+OK\r\n";
2004 return 0;
2007sub QUIT_pop3 {
2008 if(@deleted) {
2009 logmsg "deleting @deleted message(s)\n";
2011 @deleted = ();
2014 sendcontrol "+OK curl POP3 server signing off\r\n";
2016 return 0;
2019################
2020################ FTP commands
2021################
2022my $rest = 0;
2023sub REST_ftp {
2024 $rest = $_[0];
2025 logmsg "Set REST position to $rest\n"
2028sub switch_directory_goto {
2029 my $target_dir = $_;
2031 if(!$ftptargetdir) {
2032 $ftptargetdir = "/";
2035 if($target_dir eq "") {
2036 $ftptargetdir = "/";
2038 elsif($target_dir eq "..") {
2039 if($ftptargetdir eq "/") {
2040 $ftptargetdir = "/";
2042 else {
2043 $ftptargetdir =~ s/[[:alnum:]]+\/$//;
2046 else {
2047 $ftptargetdir .= $target_dir . "/";
2051sub switch_directory {
2052 my $target_dir = $_[0];
2054 if($target_dir =~ /^test-(\d+)/) {
2055 $cwd_testno = $1;
2057 elsif($target_dir eq "/") {
2058 $ftptargetdir = "/";
2060 else {
2061 my @dirs = split("/", $target_dir);
2062 for(@dirs) {
2063 switch_directory_goto($_);
2068sub CWD_ftp {
2069 my ($folder, $fullcommand) = $_[0];
2070 switch_directory($folder);
2071 if($ftptargetdir =~ /^\/fully_simulated/) {
2072 $ftplistparserstate = "enabled";
2073 logmsg "enabled FTP list parser mode\n";
2075 else {
2076 undef $ftplistparserstate;
2080sub PWD_ftp {
2081 my $mydir;
2082 $mydir = $ftptargetdir ? $ftptargetdir : "/";
2084 if($mydir ne "/") {
2085 $mydir =~ s/\/$//;
2087 sendcontrol "257 \"$mydir\" is current directory\r\n";
2090sub LIST_ftp {
2091 # print "150 ASCII data connection for /bin/ls (193.15.23.1,59196) (0 bytes)\r\n";
2093 if($datasockf_conn eq 'no') {
2094 if($nodataconn425) {
2095 sendcontrol "150 Opening data connection\r\n";
2096 sendcontrol "425 Can't open data connection\r\n";
2098 elsif($nodataconn421) {
2099 sendcontrol "150 Opening data connection\r\n";
2100 sendcontrol "421 Connection timed out\r\n";
2102 elsif($nodataconn150) {
2103 sendcontrol "150 Opening data connection\r\n";
2104 # client shall timeout
2106 else {
2107 # client shall timeout
2109 return 0;
2112 logmsg "pass LIST data on data connection\n";
2114 if($ftplistparserstate) {
2115 # provide a synthetic response
2116 my @ftpdir = ftp_contentlist($ftptargetdir);
2117 # old hard-coded style
2118 for(@ftpdir) {
2119 logmsg "LIST: $_";
2120 senddata $_;
2123 else {
2124 my @data = getpart("reply", "data");
2125 for(@data) {
2126 my $send = $_;
2127 # convert all \n to \r\n for ASCII transfer
2128 $send =~ s/\r\n/\n/g;
2129 $send =~ s/\n/\r\n/g;
2130 logmsg "send $send as data\n";
2131 senddata $send;
2134 close_dataconn(0);
2135 sendcontrol "226 ASCII transfer complete\r\n";
2136 return 0;
2139sub NLST_ftp {
2140 my @ftpdir = ("file", "with space", "fake", "..", " ..", "funny", "README");
2142 if($datasockf_conn eq 'no') {
2143 if($nodataconn425) {
2144 sendcontrol "150 Opening data connection\r\n";
2145 sendcontrol "425 Can't open data connection\r\n";
2147 elsif($nodataconn421) {
2148 sendcontrol "150 Opening data connection\r\n";
2149 sendcontrol "421 Connection timed out\r\n";
2151 elsif($nodataconn150) {
2152 sendcontrol "150 Opening data connection\r\n";
2153 # client shall timeout
2155 else {
2156 # client shall timeout
2158 return 0;
2161 logmsg "pass NLST data on data connection\n";
2162 for(@ftpdir) {
2163 senddata "$_\r\n";
2165 close_dataconn(0);
2166 sendcontrol "226 ASCII transfer complete\r\n";
2167 return 0;
2170sub MDTM_ftp {
2171 my $testno = $_[0];
2172 my $testpart = "";
2173 if($testno > 10000) {
2174 $testpart = $testno % 10000;
2175 $testno = int($testno / 10000);
2178 loadtest("$logdir/test$testno");
2180 my @data = getpart("reply", "mdtm");
2182 my $reply = $data[0];
2183 chomp $reply if($reply);
2185 if($reply && ($reply =~ /^[+-]?\d+$/) && ($reply < 0)) {
2186 sendcontrol "550 $testno: no such file.\r\n";
2188 elsif($reply) {
2189 sendcontrol "$reply\r\n";
2191 else {
2192 sendcontrol "500 MDTM: no such command.\r\n";
2194 return 0;
2197sub SIZE_ftp {
2198 my $testno = $_[0];
2200 if($ftplistparserstate) {
2201 my $size = wildcard_filesize($ftptargetdir, $testno);
2202 if($size == -1) {
2203 sendcontrol "550 $testno: No such file or directory.\r\n";
2205 else {
2206 sendcontrol "213 $size\r\n";
2208 return 0;
2211 if($testno =~ /^verifiedserver$/) {
2212 my $response = "WE ROOLZ: $$\r\n";
2213 my $size = length($response);
2214 sendcontrol "213 $size\r\n";
2215 return 0;
2218 if($testno =~ /(\d+)\/?$/) {
2219 $testno = $1;
2221 else {
2222 print STDERR "SIZE_ftp: invalid test number: $testno\n";
2223 return 1;
2226 my $testpart = "";
2227 if($testno > 10000) {
2228 $testpart = $testno % 10000;
2229 $testno = int($testno / 10000);
2232 loadtest("$logdir/test$testno");
2233 my @data = getpart("reply", "size");
2235 my $size = $data[0];
2237 if($size) {
2238 $size += 0; # make it a number
2239 if($size > -1) {
2240 sendcontrol "213 $size\r\n";
2242 else {
2243 sendcontrol "550 $testno: No such file or directory.\r\n";
2246 else {
2247 $size = 0;
2248 @data = getpart("reply", "data$testpart");
2249 for(@data) {
2250 $size += length($_);
2252 if($size) {
2253 sendcontrol "213 $size\r\n";
2255 else {
2256 sendcontrol "550 $testno: No such file or directory.\r\n";
2259 return 0;
2262sub RETR_ftp {
2263 my ($testno) = @_;
2265 if($datasockf_conn eq 'no') {
2266 if($nodataconn425) {
2267 sendcontrol "150 Opening data connection\r\n";
2268 sendcontrol "425 Can't open data connection\r\n";
2270 elsif($nodataconn421) {
2271 sendcontrol "150 Opening data connection\r\n";
2272 sendcontrol "421 Connection timed out\r\n";
2274 elsif($nodataconn150) {
2275 sendcontrol "150 Opening data connection\r\n";
2276 # client shall timeout
2278 else {
2279 # client shall timeout
2281 return 0;
2284 if($ftplistparserstate) {
2285 my @content = wildcard_getfile($ftptargetdir, $testno);
2286 if($content[0] == -1) {
2287 #file not found
2289 else {
2290 my $size = length $content[1];
2291 sendcontrol "150 Binary data connection for $testno ($size bytes).\r\n",
2292 senddata $content[1];
2293 close_dataconn(0);
2294 sendcontrol "226 File transfer complete\r\n";
2296 return 0;
2299 if($testno =~ /^verifiedserver$/) {
2300 # this is the secret command that verifies that this actually is
2301 # the curl test server
2302 my $response = "WE ROOLZ: $$\r\n";
2303 my $len = length($response);
2304 sendcontrol "150 Binary junk ($len bytes).\r\n";
2305 senddata "WE ROOLZ: $$\r\n";
2306 close_dataconn(0);
2307 sendcontrol "226 File transfer complete\r\n";
2308 if($verbose) {
2309 print STDERR "FTPD: We returned proof we are the test server\n";
2311 return 0;
2314 $testno =~ s/^([^0-9]*)//;
2315 my $testpart = "";
2316 if($testno > 10000) {
2317 $testpart = $testno % 10000;
2318 $testno = int($testno / 10000);
2321 loadtest("$logdir/test$testno");
2323 my @data = getpart("reply", "data$testpart");
2325 my $size = 0;
2326 for(@data) {
2327 $size += length($_);
2330 my %hash = getpartattr("reply", "data$testpart");
2332 if($size || $hash{'sendzero'}) {
2334 if($rest) {
2335 # move read pointer forward
2336 $size -= $rest;
2337 logmsg "REST $rest was removed from size, makes $size left\n";
2338 $rest = 0; # reset REST offset again
2340 if($retrweirdo) {
2341 sendcontrol "150 Binary data connection for $testno () ($size bytes).\r\n",
2342 "226 File transfer complete\r\n";
2344 for(@data) {
2345 my $send = $_;
2346 senddata $send;
2348 close_dataconn(0);
2349 $retrweirdo = 0; # switch off the weirdo again!
2351 else {
2352 my $sz = "($size bytes)";
2353 if($retrnosize) {
2354 $sz = "size?";
2356 elsif($retrsize > 0) {
2357 $sz = "($retrsize bytes)";
2360 sendcontrol "150 Binary data connection for $testno ($testpart) $sz.\r\n";
2362 if($stalldata) {
2363 # Keep the data socket open without answering the TLS
2364 # handshake so the client has to time out on its own.
2365 return 0;
2368 for(@data) {
2369 my $send = $_;
2370 senddata $send;
2372 close_dataconn(0);
2373 sendcontrol "226 File transfer complete\r\n";
2376 else {
2377 sendcontrol "550 $testno: No such file or directory.\r\n";
2379 return 0;
2382sub STOR_ftp {
2383 my $testno = $_[0];
2385 my $filename = "$logdir/upload.$testno";
2387 if($datasockf_conn eq 'no') {
2388 if($nodataconn425) {
2389 sendcontrol "150 Opening data connection\r\n";
2390 sendcontrol "425 Can't open data connection\r\n";
2392 elsif($nodataconn421) {
2393 sendcontrol "150 Opening data connection\r\n";
2394 sendcontrol "421 Connection timed out\r\n";
2396 elsif($nodataconn150) {
2397 sendcontrol "150 Opening data connection\r\n";
2398 # client shall timeout
2400 else {
2401 # client shall timeout
2403 return 0;
2406 logmsg "STOR test number $testno in $filename\n";
2408 sendcontrol "125 Gimme gimme gimme!\r\n";
2410 open(my $file, ">", $filename) or
2411 return 0; # failed to open output
2413 my $line;
2414 my $ulsize = 0;
2415 my $disc = 0;
2416 while(5 == (sysread DREAD, $line, 5)) {
2417 if($line eq "DATA\n") {
2418 my $i;
2419 sysread DREAD, $i, 5;
2421 my $size = 0;
2422 if($i =~ /^([0-9a-fA-F]{4})\n/) {
2423 $size = hex($1);
2426 read_datasockf(\$line, $size);
2428 #print STDERR " GOT: $size bytes\n";
2430 $ulsize += $size;
2431 print $file $line if(!$nosave);
2432 logmsg "> Appending $size bytes to file\n";
2434 elsif($line eq "DISC\n") {
2435 # disconnect!
2436 $disc = 1;
2437 printf DWRITE "ACKD\n";
2438 last;
2440 else {
2441 logmsg "No support for: $line";
2442 last;
2444 if($storeresp) {
2445 # abort early
2446 last;
2449 if($nosave) {
2450 print $file "$ulsize bytes would have been stored here\n";
2452 close($file);
2453 close_dataconn($disc);
2454 logmsg "received $ulsize bytes upload\n";
2455 if($storeresp) {
2456 sendcontrol "$storeresp\r\n";
2458 else {
2459 sendcontrol "226 File transfer complete\r\n";
2461 return 0;
2464sub PASV_ftp {
2465 my ($arg, $cmd) = @_;
2466 my $pasvport;
2468 # kill previous data connection sockfilt when alive
2469 if($datasockf_runs eq 'yes') {
2470 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose, 'data');
2471 logmsg "DATA sockfilt for $datasockf_mode data channel killed\n";
2473 datasockf_state('STOPPED');
2475 logmsg "====> Passive DATA channel requested by client\n";
2477 logmsg "DATA sockfilt for passive data channel starting...\n";
2479 # We fire up a new sockfilt to do the data transfer for us.
2480 my @datasockfcmd = (server_exe_args('sockfilt'),
2481 "--ipv$ipvnum", "--port", 0,
2482 "--pidfile", $datasockf_pidfile,
2483 "--logfile", $datasockf_logfile);
2484 if($nodataconn) {
2485 push(@datasockfcmd, '--bindonly');
2487 $slavepid = open2(\*DREAD, \*DWRITE, @datasockfcmd);
2489 if($nodataconn) {
2490 datasockf_state('PASSIVE_NODATACONN');
2492 else {
2493 datasockf_state('PASSIVE');
2496 print STDERR "@datasockfcmd\n" if($verbose);
2498 print DWRITE "PING\n";
2499 my $pong;
2500 sysread_or_die(\*DREAD, \$pong, 5);
2502 if($pong =~ /^FAIL/) {
2503 logmsg "DATA sockfilt said: FAIL\n";
2504 logmsg "DATA sockfilt for passive data channel failed\n";
2505 logmsg "DATA sockfilt not running\n";
2506 datasockf_state('STOPPED');
2507 sendcontrol "500 no free ports!\r\n";
2508 return;
2510 elsif($pong !~ /^PONG/) {
2511 logmsg "DATA sockfilt unexpected response: $pong\n";
2512 logmsg "DATA sockfilt for passive data channel failed\n";
2513 logmsg "DATA sockfilt killed now\n";
2514 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose, 'data');
2515 logmsg "DATA sockfilt not running\n";
2516 datasockf_state('STOPPED');
2517 sendcontrol "500 no free ports!\r\n";
2518 return;
2521 logmsg "DATA sockfilt for passive data channel started (pid $slavepid)\n";
2523 # Find out on what port we listen on or have bound
2524 my $i;
2525 print DWRITE "PORT\n";
2527 # READ the response code
2528 sysread_or_die(\*DREAD, \$i, 5);
2530 # READ the response size
2531 sysread_or_die(\*DREAD, \$i, 5);
2533 my $size = 0;
2534 if($i =~ /^([0-9a-fA-F]{4})\n/) {
2535 $size = hex($1);
2538 # READ the response data
2539 read_datasockf(\$i, $size);
2541 # The data is in the format
2542 # IPvX/NNN
2544 if($i =~ /IPv(\d)\/(\d+)/) {
2545 # FIX: deal with IP protocol version
2546 $pasvport = $2;
2549 if(!$pasvport) {
2550 logmsg "DATA sockfilt unknown listener port\n";
2551 logmsg "DATA sockfilt for passive data channel failed\n";
2552 logmsg "DATA sockfilt killed now\n";
2553 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose, 'data');
2554 logmsg "DATA sockfilt not running\n";
2555 datasockf_state('STOPPED');
2556 sendcontrol "500 no free ports!\r\n";
2557 return;
2560 if($nodataconn) {
2561 my $str = nodataconn_str();
2562 logmsg "DATA sockfilt for passive data channel ($str) bound on port " .
2563 "$pasvport\n";
2565 else {
2566 logmsg "DATA sockfilt for passive data channel listens on port " .
2567 "$pasvport\n";
2570 if($cmd ne "EPSV") {
2571 # PASV reply
2572 my $p = $listenaddr;
2573 $p =~ s/\./,/g;
2574 if($pasvbadip) {
2575 $p = "1,2,3,4";
2577 sendcontrol sprintf("227 Entering Passive Mode ($p,%d,%d)\r\n",
2578 int($pasvport / 256), int($pasvport % 256));
2580 else {
2581 # EPSV reply
2582 sendcontrol sprintf("229 Entering Passive Mode (|||%d|)\r\n", $pasvport);
2585 logmsg "Client has been notified that upcoming DATA connection " .
2586 "is awaited on port $pasvport\n";
2588 if($nodataconn) {
2589 my $str = nodataconn_str();
2590 logmsg "====> Client fooled ($str)\n";
2591 return;
2594 eval {
2595 local $SIG{ALRM} = sub { die "alarm\n" };
2597 # assume swift operations unless explicitly slow
2598 alarm ($datadelay ? 20 : 2);
2600 # Wait for 'CNCT'
2601 my $input;
2603 # FIX: Monitor ctrl conn for disconnect
2605 while(sysread(DREAD, $input, 5)) {
2607 if($input !~ /^CNCT/) {
2608 # we wait for a connected client
2609 logmsg "Odd, we got $input from client\n";
2610 next;
2612 logmsg "Client connects to port $pasvport\n";
2613 last;
2615 alarm 0;
2616 };
2617 if($@) {
2618 # timed out
2619 logmsg "$srvrname server timed out awaiting data connection " .
2620 "on port $pasvport\n";
2621 logmsg "accept failed or connection not even attempted\n";
2622 logmsg "DATA sockfilt killed now\n";
2623 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose, 'data');
2624 logmsg "DATA sockfilt not running\n";
2625 datasockf_state('STOPPED');
2626 return;
2628 else {
2629 logmsg "====> Client established passive DATA connection " .
2630 "on port $pasvport\n";
2633 return;
2637# Support both PORT and EPRT here.
2640sub PORT_ftp {
2641 my ($arg, $cmd) = @_;
2642 my $port;
2643 my $addr;
2645 # kill previous data connection sockfilt when alive
2646 if($datasockf_runs eq 'yes') {
2647 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose, 'data');
2648 logmsg "DATA sockfilt for $datasockf_mode data channel killed\n";
2650 datasockf_state('STOPPED');
2652 logmsg "====> Active DATA channel requested by client\n";
2654 # We always ignore the given IP and use localhost.
2656 if($cmd eq "PORT") {
2657 if($arg !~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {
2658 logmsg "DATA sockfilt for active data channel not started " .
2659 "(bad PORT-line: $arg)\n";
2660 sendcontrol "500 silly you, go away\r\n";
2661 return;
2663 $port = ($5 << 8) + $6;
2664 $addr = "$1.$2.$3.$4";
2666 # EPRT |2|::1|49706|
2667 elsif($cmd eq "EPRT") {
2668 if($arg !~ /(\d+)\|([^\|]+)\|(\d+)/) {
2669 logmsg "DATA sockfilt for active data channel not started " .
2670 "(bad EPRT-line: $arg)\n";
2671 sendcontrol "500 silly you, go away\r\n";
2672 return;
2674 sendcontrol "200 Thanks for dropping by. We contact you later\r\n";
2675 $port = $3;
2676 $addr = $2;
2678 else {
2679 logmsg "DATA sockfilt for active data channel not started " .
2680 "(invalid command: $cmd)\n";
2681 sendcontrol "500 we don't like $cmd now\r\n";
2682 return;
2685 if(!$port || $port > 65535) {
2686 logmsg "DATA sockfilt for active data channel not started " .
2687 "(illegal PORT number: $port)\n";
2688 return;
2691 if($nodataconn) {
2692 my $str = nodataconn_str();
2693 logmsg "DATA sockfilt for active data channel not started ($str)\n";
2694 datasockf_state('ACTIVE_NODATACONN');
2695 logmsg "====> Active DATA channel not established\n";
2696 return;
2699 logmsg "DATA sockfilt for active data channel starting...\n";
2701 # We fire up a new sockfilt to do the data transfer for us.
2702 my @datasockfcmd = (server_exe_args('sockfilt'),
2703 "--ipv$ipvnum", "--connect", $port, "--addr", $addr,
2704 "--pidfile", $datasockf_pidfile,
2705 "--logfile", $datasockf_logfile);
2706 $slavepid = open2(\*DREAD, \*DWRITE, @datasockfcmd);
2708 datasockf_state('ACTIVE');
2710 print STDERR "@datasockfcmd\n" if($verbose);
2712 print DWRITE "PING\n";
2713 my $pong;
2714 sysread_or_die(\*DREAD, \$pong, 5);
2716 if($pong =~ /^FAIL/) {
2717 logmsg "DATA sockfilt said: FAIL\n";
2718 logmsg "DATA sockfilt for active data channel failed\n";
2719 logmsg "DATA sockfilt not running\n";
2720 datasockf_state('STOPPED');
2721 # client shall timeout awaiting connection from server
2722 return;
2724 elsif($pong !~ /^PONG/) {
2725 logmsg "DATA sockfilt unexpected response: $pong\n";
2726 logmsg "DATA sockfilt for active data channel failed\n";
2727 logmsg "DATA sockfilt killed now\n";
2728 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose, 'data');
2729 logmsg "DATA sockfilt not running\n";
2730 datasockf_state('STOPPED');
2731 # client shall timeout awaiting connection from server
2732 return;
2735 logmsg "DATA sockfilt for active data channel started (pid $slavepid)\n";
2737 logmsg "====> Active DATA channel connected to client port $port\n";
2739 return;
2742#**********************************************************************
2743# datasockf_state is used to change variables that keep state info
2744# relative to the FTP secondary or data sockfilt process as soon as
2745# one of the five possible stable states is reached. Variables that
2746# are modified by this sub may be checked independently but should
2747# not be changed except by calling this sub.
2749sub datasockf_state {
2750 my $state = $_[0];
2752 if($state eq 'STOPPED') {
2753 # Data sockfilter initial state, not running,
2754 # not connected and not used.
2755 $datasockf_state = $state;
2756 $datasockf_mode = 'none';
2757 $datasockf_runs = 'no';
2758 $datasockf_conn = 'no';
2760 elsif($state eq 'PASSIVE') {
2761 # Data sockfilter accepted connection from client.
2762 $datasockf_state = $state;
2763 $datasockf_mode = 'passive';
2764 $datasockf_runs = 'yes';
2765 $datasockf_conn = 'yes';
2767 elsif($state eq 'ACTIVE') {
2768 # Data sockfilter has connected to client.
2769 $datasockf_state = $state;
2770 $datasockf_mode = 'active';
2771 $datasockf_runs = 'yes';
2772 $datasockf_conn = 'yes';
2774 elsif($state eq 'PASSIVE_NODATACONN') {
2775 # Data sockfilter bound port without listening,
2776 # client is unable to establish data connection.
2777 $datasockf_state = $state;
2778 $datasockf_mode = 'passive';
2779 $datasockf_runs = 'yes';
2780 $datasockf_conn = 'no';
2782 elsif($state eq 'ACTIVE_NODATACONN') {
2783 # Data sockfilter does not even run,
2784 # client awaits data connection from server in vain.
2785 $datasockf_state = $state;
2786 $datasockf_mode = 'active';
2787 $datasockf_runs = 'no';
2788 $datasockf_conn = 'no';
2790 else {
2791 die "Internal error. Unknown datasockf state: $state!";
2795#**********************************************************************
2796# nodataconn_str returns string of effective nodataconn command. Notice
2797# that $nodataconn may be set alone or in addition to a $nodataconnXXX.
2799sub nodataconn_str {
2800 my $str;
2801 # order matters
2802 $str = 'NODATACONN' if($nodataconn);
2803 $str = 'NODATACONN425' if($nodataconn425);
2804 $str = 'NODATACONN421' if($nodataconn421);
2805 $str = 'NODATACONN150' if($nodataconn150);
2806 return $str;
2809#**********************************************************************
2810# customize configures test server operation for each curl test, reading
2811# configuration commands/parameters from server commands file each time
2812# a new client control connection is established with the test server.
2813# On success returns 1, otherwise zero.
2815sub customize {
2816 my($cmdfile) = @_;
2817 $ctrldelay = 0; # default is no throttling of the ctrl stream
2818 $datadelay = 0; # default is no throttling of the data stream
2819 $retrweirdo = 0; # default is no use of RETRWEIRDO
2820 $retrnosize = 0; # default is no use of RETRNOSIZE
2821 $retrsize = 0; # default is no use of RETRSIZE
2822 $pasvbadip = 0; # default is no use of PASVBADIP
2823 $nosave = 0; # default is to actually save uploaded data to file
2824 $nodataconn = 0; # default is to establish or accept data channel
2825 $nodataconn425 = 0; # default is to not send 425 without data channel
2826 $nodataconn421 = 0; # default is to not send 421 without data channel
2827 $nodataconn150 = 0; # default is to not send 150 without data channel
2828 $stalldata = 0; # default is to send response data
2829 $storeresp = ""; # send as ultimate STOR response
2830 $postfetch = ""; # send as header after a FETCH response
2831 @capabilities = (); # default is to not support capability commands
2832 @auth_mechs = (); # default is to not support authentication commands
2833 %fulltextreply = ();#
2834 %commandreply = (); #
2835 %customcount = (); #
2836 %delayreply = (); #
2838 open(my $custom, "<", "$logdir/$SERVERCMD") or
2839 return 1;
2841 logmsg "FTPD: Getting commands from $logdir/$SERVERCMD\n";
2843 while(<$custom>) {
2844 if($_ =~ /REPLY \"([A-Z]+ [A-Za-z0-9+-\/=\*. ]+)\" (.*)/) {
2845 $fulltextreply{$1} = eval "qq{$2}";
2846 logmsg "FTPD: set custom reply for $1\n";
2848 elsif($_ =~ /REPLY(LF|) ([A-Za-z0-9+\/=\*]*) (.*)/) {
2849 $commandreply{$2} = eval "qq{$3}";
2850 if($1 ne "LF") {
2851 $commandreply{$2} .= "\r\n";
2853 else {
2854 $commandreply{$2} .= "\n";
2856 if($2 eq "") {
2857 logmsg "FTPD: set custom reply for empty command\n";
2859 else {
2860 logmsg "FTPD: set custom reply for $2 command\n";
2863 elsif($_ =~ /COUNT ([A-Z]+) (.*)/) {
2864 # we blank the custom reply for this command when having
2865 # been used this number of times
2866 $customcount{$1} = $2;
2867 logmsg "FTPD: blank custom reply for $1 command after $2 uses\n";
2869 elsif($_ =~ /DELAY ([A-Z]+) (\d*)/) {
2870 $delayreply{$1} = $2;
2871 logmsg "FTPD: delay reply for $1 with $2 seconds\n";
2873 elsif($_ =~ /POSTFETCH (.*)/) {
2874 logmsg "FTPD: read POSTFETCH header data\n";
2875 $postfetch = $1;
2877 elsif($_ =~ /SLOWDOWNDATA/) {
2878 $ctrldelay = 0;
2879 $datadelay = 0.005;
2880 logmsg "FTPD: send response data with 5ms delay per byte\n";
2882 elsif($_ =~ /STALLDATA/) {
2883 $stalldata = 1;
2884 logmsg "FTPD: accept the data connection and send nothing\n";
2886 elsif($_ =~ /SLOWDOWN/) {
2887 $ctrldelay = 0.005;
2888 $datadelay = 0.005;
2889 logmsg "FTPD: send response with 5ms delay between each byte\n";
2891 elsif($_ =~ /RETRWEIRDO/) {
2892 logmsg "FTPD: instructed to use RETRWEIRDO\n";
2893 $retrweirdo = 1;
2895 elsif($_ =~ /RETRNOSIZE/) {
2896 logmsg "FTPD: instructed to use RETRNOSIZE\n";
2897 $retrnosize = 1;
2899 elsif($_ =~ /RETRSIZE (\d+)/) {
2900 $retrsize = $1;
2901 logmsg "FTPD: instructed to use RETRSIZE = $1\n";
2903 elsif($_ =~ /PASVBADIP/) {
2904 logmsg "FTPD: instructed to use PASVBADIP\n";
2905 $pasvbadip = 1;
2907 elsif($_ =~ /NODATACONN425/) {
2908 # applies to both active and passive FTP modes
2909 logmsg "FTPD: instructed to use NODATACONN425\n";
2910 $nodataconn425 = 1;
2911 $nodataconn = 1;
2913 elsif($_ =~ /NODATACONN421/) {
2914 # applies to both active and passive FTP modes
2915 logmsg "FTPD: instructed to use NODATACONN421\n";
2916 $nodataconn421 = 1;
2917 $nodataconn = 1;
2919 elsif($_ =~ /NODATACONN150/) {
2920 # applies to both active and passive FTP modes
2921 logmsg "FTPD: instructed to use NODATACONN150\n";
2922 $nodataconn150 = 1;
2923 $nodataconn = 1;
2925 elsif($_ =~ /NODATACONN/) {
2926 # applies to both active and passive FTP modes
2927 logmsg "FTPD: instructed to use NODATACONN\n";
2928 $nodataconn = 1;
2930 elsif($_ =~ /^STOR (.*)/) {
2931 $storeresp = $1;
2932 logmsg "FTPD: instructed to use respond to STOR with '$storeresp'\n";
2934 elsif($_ =~ /CAPA (.*)/) {
2935 logmsg "FTPD: instructed to support CAPABILITY command\n";
2936 @capabilities = split(/ (?!(?:[^" ]|[^"] [^"])+")/, $1);
2937 foreach (@capabilities) {
2938 $_ = $1 if /^"(.*)"$/;
2941 elsif($_ =~ /AUTH (.*)/) {
2942 logmsg "FTPD: instructed to support AUTHENTICATION command\n";
2943 @auth_mechs = split(/ /, $1);
2945 elsif($_ =~ /NOSAVE/) {
2946 # do not actually store the file we upload - to be used when
2947 # uploading insanely huge amounts
2948 $nosave = 1;
2949 logmsg "FTPD: NOSAVE prevents saving of uploaded data\n";
2951 elsif($_ =~ /^Testnum (\d+)/){
2952 $testno = $1;
2953 logmsg "FTPD: run test case number: $testno\n";
2956 close($custom);
2959#----------------------------------------------------------------------
2960#----------------------------------------------------------------------
2961#--------------------------- END OF SUBS ----------------------------
2962#----------------------------------------------------------------------
2963#----------------------------------------------------------------------
2965#**********************************************************************
2966# Parse command line options
2968# Options:
2970# --verbose # verbose
2971# --srcdir # source directory
2972# --id # server instance number
2973# --proto # server protocol
2974# --pidfile # server pid file
2975# --portfile # server port file
2976# --logfile # server log file
2977# --logdir # server log directory
2978# --ipv4 # server IP version 4
2979# --ipv6 # server IP version 6
2980# --port # server listener port
2981# --addr # server address for listener port binding
2983while(@ARGV) {
2984 if($ARGV[0] eq '--verbose') {
2985 $verbose = 1;
2987 elsif($ARGV[0] eq '--srcdir') {
2988 if($ARGV[1]) {
2989 $srcdir = $ARGV[1];
2990 shift @ARGV;
2993 elsif($ARGV[0] eq '--id') {
2994 if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
2995 $idnum = $1 if($1 > 0);
2996 shift @ARGV;
2999 elsif($ARGV[0] eq '--proto') {
3000 if($ARGV[1] && ($ARGV[1] =~ /^(ftp|imap|pop3|smtp)$/)) {
3001 $proto = $1;
3002 shift @ARGV;
3004 else {
3005 die "unsupported protocol $ARGV[1]";
3008 elsif($ARGV[0] eq '--pidfile') {
3009 if($ARGV[1]) {
3010 $pidfile = $ARGV[1];
3011 shift @ARGV;
3014 elsif($ARGV[0] eq '--portfile') {
3015 if($ARGV[1]) {
3016 $portfile = $ARGV[1];
3017 shift @ARGV;
3020 elsif($ARGV[0] eq '--logfile') {
3021 if($ARGV[1]) {
3022 $logfile = $ARGV[1];
3023 shift @ARGV;
3026 elsif($ARGV[0] eq '--logdir') {
3027 if($ARGV[1]) {
3028 $logdir = $ARGV[1];
3029 shift @ARGV;
3032 elsif($ARGV[0] eq '--ipv4') {
3033 $ipvnum = 4;
3034 $listenaddr = '127.0.0.1' if($listenaddr eq '::1');
3036 elsif($ARGV[0] eq '--ipv6') {
3037 $ipvnum = 6;
3038 $listenaddr = '::1' if($listenaddr eq '127.0.0.1');
3040 elsif($ARGV[0] eq '--port') {
3041 if($ARGV[1] =~ /^(\d+)$/) {
3042 $port = $1;
3043 shift @ARGV;
3046 elsif($ARGV[0] eq '--addr') {
3047 if($ARGV[1]) {
3048 my $tmpstr = $ARGV[1];
3049 if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) {
3050 $listenaddr = "$1.$2.$3.$4" if($ipvnum == 4);
3052 elsif($ipvnum == 6) {
3053 $listenaddr = $tmpstr;
3054 $listenaddr =~ s/^\[(.*)\]$/$1/;
3056 shift @ARGV;
3059 else {
3060 print STDERR "\nWarning: ftpserver.pl unknown parameter: '$ARGV[0]'\n";
3062 shift @ARGV;
3065#***************************************************************************
3066# Initialize command line option dependent variables
3069if($pidfile) {
3070 # Use our pidfile directory to store the other pidfiles
3071 $piddir = dirname($pidfile);
3073else {
3074 # Use the current directory to store all the pidfiles
3075 $piddir = $path;
3076 $pidfile = server_pidfilename($piddir, $proto, $ipvnum, $idnum);
3078if(!$portfile) {
3079 $portfile = $piddir . "/" . $PORTFILE;
3081if(!$srcdir) {
3082 $srcdir = $ENV{'srcdir'} || '.';
3084if(!$logfile) {
3085 $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
3088$mainsockf_pidfile = mainsockf_pidfilename($piddir, $proto, $ipvnum, $idnum);
3089$mainsockf_logfile =
3090 mainsockf_logfilename($logdir, $proto, $ipvnum, $idnum);
3092if($proto eq 'ftp') {
3093 $datasockf_pidfile = datasockf_pidfilename($piddir, $proto, $ipvnum, $idnum);
3094 $datasockf_logfile =
3095 datasockf_logfilename($logdir, $proto, $ipvnum, $idnum);
3098$srvrname = servername_str($proto, $ipvnum, $idnum);
3099$serverlogs_lockfile = "$logdir/$LOCKDIR/${srvrname}.lock";
3101$idstr = $idnum if($idnum > 1);
3103protocolsetup($proto);
3105$SIG{INT} = \&exit_signal_handler;
3106$SIG{TERM} = \&exit_signal_handler;
3108startsf();
3110# actual port
3111if($portfile && !$port) {
3112 my $aport;
3113 open(my $p, "<", $portfile);
3114 $aport = <$p>;
3115 close($p);
3116 $port = 0 + $aport;
3119logmsg sprintf("%s server listens on port IPv${ipvnum}/${port}\n", uc($proto));
3121open(my $pid, ">", $pidfile);
3122print $pid $$ . "\n";
3123close($pid);
3125logmsg("logged pid $$ in $pidfile\n");
3127while(1) {
3129 # kill previous data connection sockfilt when alive
3130 if($datasockf_runs eq 'yes') {
3131 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose, 'data');
3132 logmsg "DATA sockfilt for $datasockf_mode data channel killed now\n";
3134 datasockf_state('STOPPED');
3137 # We read 'sockfilt' commands.
3139 my $input;
3141 logmsg "Awaiting input\n";
3142 sysread_or_die(\*SFREAD, \$input, 5);
3144 if($input !~ /^CNCT/) {
3145 # we wait for a connected client
3146 logmsg "MAIN sockfilt said: $input";
3147 next;
3149 logmsg "====> Client connect\n";
3151 set_advisor_read_lock($serverlogs_lockfile);
3152 $serverlogslocked = 1;
3154 # flush data:
3155 $| = 1;
3157 &customize(); # read test control instructions
3158 # force a reload, a test retry may have regenerated the file
3159 loadtest("$logdir/test$testno", 0, 1);
3161 my $welcome = $commandreply{"welcome"};
3162 if(!$welcome) {
3163 $welcome = $displaytext{"welcome"};
3165 else {
3166 # clear it after use
3167 $commandreply{"welcome"} = "";
3168 if($welcome !~ /\r\n\z/) {
3169 $welcome .= "\r\n";
3172 sendcontrol $welcome;
3174 #remove global variables from last connection
3175 if($ftplistparserstate) {
3176 undef $ftplistparserstate;
3178 if($ftptargetdir) {
3179 $ftptargetdir = "";
3182 if($verbose) {
3183 print STDERR "OUT: $welcome";
3186 my $full = "";
3188 while(1) {
3189 my $i;
3191 # Now we expect to read DATA\n[hex size]\n[prot], where the [prot]
3192 # part only is FTP lingo.
3194 # COMMAND
3195 sysread_or_die(\*SFREAD, \$i, 5);
3197 if($i !~ /^DATA/) {
3198 logmsg "MAIN sockfilt said $i";
3199 if($i =~ /^DISC/) {
3200 # disconnect
3201 printf SFWRITE "ACKD\n";
3202 last;
3204 next;
3207 # SIZE of data
3208 sysread_or_die(\*SFREAD, \$i, 5);
3210 my $size = 0;
3211 if($i =~ /^([0-9a-fA-F]{4})\n/) {
3212 $size = hex($1);
3215 # data
3216 read_mainsockf(\$input, $size);
3218 ftpmsg $input;
3220 $full .= $input;
3222 # Loop until command completion
3223 next unless($full =~ /\r\n$/);
3225 # Remove trailing CRLF.
3226 $full =~ s/[\n\r]+$//;
3228 my $FTPCMD;
3229 my $FTPARG;
3230 if($proto eq "imap") {
3231 # IMAP is different with its identifier first on the command line
3232 if(($full =~ /^([^ ]+) ([^ ]+) (.*)/) ||
3233 ($full =~ /^([^ ]+) ([^ ]+)/)) {
3234 $cmdid = $1; # set the global variable
3235 $FTPCMD = $2;
3236 $FTPARG = $3;
3238 # IMAP authentication cancellation
3239 elsif($full =~ /^\*$/) {
3240 # Command id has already been set
3241 $FTPCMD = "*";
3242 $FTPARG = "";
3244 # IMAP long "commands" are base64 authentication data
3245 elsif($full =~ /^[A-Z0-9+\/]*={0,2}$/i) {
3246 # Command id has already been set
3247 $FTPCMD = $full;
3248 $FTPARG = "";
3250 else {
3251 sendcontrol "$full BAD Command\r\n";
3252 last;
3255 elsif($full =~ /^([A-Z]{3,4})(\s(.*))?$/i) {
3256 $FTPCMD = $1;
3257 $FTPARG = $3;
3259 elsif($proto eq "pop3") {
3260 # POP3 authentication cancellation
3261 if($full =~ /^\*$/) {
3262 $FTPCMD = "*";
3263 $FTPARG = "";
3265 # POP3 long "commands" are base64 authentication data
3266 elsif($full =~ /^[A-Z0-9+\/]*={0,2}$/i) {
3267 $FTPCMD = $full;
3268 $FTPARG = "";
3270 else {
3271 sendcontrol "-ERR Unrecognized command\r\n";
3272 last;
3275 elsif($proto eq "smtp") {
3276 # SMTP authentication cancellation
3277 if($full =~ /^\*$/) {
3278 $FTPCMD = "*";
3279 $FTPARG = "";
3281 # SMTP long "commands" are base64 authentication data
3282 elsif($full =~ /^[A-Z0-9+\/]{0,512}={0,2}$/i) {
3283 $FTPCMD = $full;
3284 $FTPARG = "";
3286 else {
3287 sendcontrol "500 Unrecognized command\r\n";
3288 last;
3291 else {
3292 sendcontrol "500 Unrecognized command\r\n";
3293 last;
3296 logmsg "< \"$full\"\n";
3298 if($verbose) {
3299 print STDERR "IN: $full\n";
3302 $full = "";
3304 my $delay = $delayreply{$FTPCMD};
3305 if($delay) {
3306 # go sleep this many seconds!
3307 logmsg("Sleep for $delay seconds\n");
3308 my $twentieths = $delay * 20;
3309 while($twentieths--) {
3310 Time::HiRes::sleep(0.05) unless($got_exit_signal);
3314 my $check = 1; # no response yet
3316 # See if there is a custom reply for the full text
3317 my $fulltext = $FTPARG ? $FTPCMD . " " . $FTPARG : $FTPCMD;
3318 my $text = $fulltextreply{$fulltext};
3319 if($text && ($text ne "")) {
3320 sendcontrol "$text\r\n";
3321 $check = 0;
3323 else {
3324 # See if there is a custom reply for the command
3325 $text = $commandreply{$FTPCMD};
3326 if($text && ($text ne "")) {
3327 if($customcount{$FTPCMD} && (!--$customcount{$FTPCMD})) {
3328 # used enough times so blank the custom command reply
3329 $commandreply{$FTPCMD} = "";
3332 sendcontrol $text;
3333 $check = 0;
3335 else {
3336 # See if there is any display text for the command
3337 $text = $displaytext{$FTPCMD};
3338 if($text && ($text ne "")) {
3339 if($proto eq 'imap') {
3340 sendcontrol "$cmdid $text\r\n";
3342 else {
3343 sendcontrol "$text\r\n";
3346 $check = 0;
3349 # only perform this if we are not faking a reply
3350 my $func = $commandfunc{uc($FTPCMD)};
3351 if($func) {
3352 &$func($FTPARG, $FTPCMD);
3353 $check = 0;
3358 if($check) {
3359 logmsg "$FTPCMD was not handled!\n";
3360 if($proto eq 'pop3') {
3361 sendcontrol "-ERR $FTPCMD is not dealt with!\r\n";
3363 elsif($proto eq 'imap') {
3364 sendcontrol "$cmdid BAD $FTPCMD is not dealt with!\r\n";
3366 else {
3367 sendcontrol "500 $FTPCMD is not dealt with!\r\n";
3371 } # while(1)
3372 logmsg "====> Client disconnected\n";
3374 if($serverlogslocked) {
3375 $serverlogslocked = 0;
3376 clear_advisor_read_lock($serverlogs_lockfile);
3380killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose);
3381unlink($pidfile);
3382if($serverlogslocked) {
3383 $serverlogslocked = 0;
3384 clear_advisor_read_lock($serverlogs_lockfile);
3387exit;
Diagnostics4 git calls · 157 ms
CallStatusSizeTime0157 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20022 KiB151 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=151
git.response.bytes=22665
git.response.cache=MISS
git.response.duration_ms=151
git.fetch.wants=c1aeeb5fe26f3aa3b9cb21f17844d3eb4e811cb6
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=22665
git.pkt.payload.max=22644
git.sideband.band1.bytes=22643
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e2a54c2d0d1293
Request
9ea0149e…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format