#!/usr/bin/perl
+#
+# $Id$
+# This is the FTP server designed for the curl test suite.
+#
+# It is meant to excersive curl, it is not meant to become a fully working
+# or even very standard compliant server.
+#
+# You may optionally specify port on the command line, otherwise it'll
+# default to port 8921.
+#
+
use Socket;
use Carp;
use FileHandle;
sub logmsg { print FTPLOG @_; }
+sub ftpmsg { print INPUT @_; }
+
my $verbose=0; # set to 1 for debugging
my $port = 8921; # just a default
sub REAPER {
$waitedpid = wait;
$SIG{CHLD} = \&REAPER; # loathe sysV
- # logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
+ logmsg "reaped $waitedpid" . ($? ? " with exit $?\n" : "\n");
}
# USER is ok in fresh state
"LIST" => "twosock",
"RETR" => "twosock",
"CWD" => "loggedin",
+ "QUIT" => "loggedin|twosock",
);
# initially, we're in 'fresh' state
'TYPE' => '200 I modify TYPE as you wanted',
'LIST' => '150 here comes a directory',
'CWD' => '250 CWD command successful.',
-
+ 'QUIT' => '221 bye bye baby',
);
# callback functions for certain commands
}
$port++; # try next port please
}
+ if(11000 == $port) {
+ print "500 no free ports!\r\n";
+ exit;
+ }
listen(Server2,SOMAXCONN) || die "listen: $!";
printf("227 Entering Passive Mode (127,0,0,1,%d,%d)\n",
my($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);
- logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port\n";
+ logmsg "data connection from $name [", inet_ntoa($iaddr), "] at port $port\n";
open(STDOUT, ">&Client2") || die "can't dup client to stdout";
if($arg !~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {
logmsg "bad PORT-line: $arg\n";
- print "314 silly you, go away\r\n";
- return 1;
+ print "500 silly you, go away\r\n";
+ exit;
}
my $iaddr = inet_aton("$1.$2.$3.$4");
my $paddr = sockaddr_in(($5<<8)+$6, $iaddr);
last unless defined ($_ = <STDIN>);
+ ftpmsg $_;
# Remove trailing CRLF.
s/[\n\r]+$//;
unless (m/^([A-Z]{3,4})\s?(.*)/i) {
- print STDERR
- "badly formed command received: ".$_;
- exit 0;
+ print "500 '$_': command not understood.\r\n";
+ next;
}
my $FTPCMD=$1;
my $FTPARG=$2;
my $full=$_;
logmsg "GOT: ($1) $_\n";
- print INPUT "$$: $full\n";
my $ok = $commandok{$FTPCMD};
if($ok !~ /$state/) {
- print "314 $FTPCMD not OK ($ok) in state: $state!\r\n";
- exit;
+ print "500 $FTPCMD not OK in state: $state!\r\n";
+ next;
}
my $newstate=$statechange{$FTPCMD};
sub spawn {
my $coderef = shift;
-
unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
confess "usage: spawn CODEREF";
}
-
my $pid;
if (!defined($pid = fork)) {
logmsg "cannot fork: $!\n";