📄 ftpserver.pl
字号:
#!/usr/bin/perl## $Id: ftpserver.pl,v 1.36 2003/08/08 10:21:47 bagder Exp $# This is the FTP server designed for the curl test suite.## It is meant to exercise curl, it is not meant to be 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;use strict;require "getpart.pm";open(FTPLOG, ">log/ftpd.log") || print STDERR "failed to open log file, runs without logging\n";sub logmsg { print FTPLOG "$$: "; print FTPLOG @_; }sub ftpmsg { # append to the server.input file open(INPUT, ">>log/server.input") || logmsg "failed to open log/server.input\n"; INPUT->autoflush(1); print INPUT @_; close(INPUT); # use this, open->print->close system only to make the file # open as little as possible, to make the test suite run # better on windows/cygwin}my $verbose=0; # set to 1 for debuggingmy $retrweirdo=0;my $retrnosize=0;my $port = 8921; # just a defaultdo { if($ARGV[0] eq "-v") { $verbose=1; } elsif($ARGV[0] =~ /^(\d+)$/) { $port = $1; }} while(shift @ARGV);my $proto = getprotobyname('tcp') || 6;socket(Server, PF_INET, SOCK_STREAM, $proto)|| die "socket: $!";setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";bind(Server, sockaddr_in($port, INADDR_ANY))|| die "bind: $!";listen(Server,SOMAXCONN) || die "listen: $!";#print "FTP server started on port $port\n";open(PID, ">.ftp.pid");print PID $$;close(PID);my $waitedpid = 0;my $paddr;sub REAPER { $waitedpid = wait; $SIG{CHLD} = \&REAPER; # loathe sysV logmsg "reaped $waitedpid" . ($? ? " with exit $?\n" : "\n");}# USER is ok in fresh statemy %commandok = ( 'USER' => 'fresh', 'PASS' => 'passwd', 'PASV' => 'loggedin|twosock', 'EPSV' => 'loggedin|twosock', 'PORT' => 'loggedin|twosock', 'TYPE' => 'loggedin|twosock', 'LIST' => 'twosock', 'NLST' => 'twosock', 'RETR' => 'twosock', 'STOR' => 'twosock', 'APPE' => 'twosock', 'REST' => 'twosock', 'CWD' => 'loggedin|twosock', 'SYST' => 'loggedin', 'SIZE' => 'loggedin|twosock', 'PWD' => 'loggedin|twosock', 'MKD' => 'loggedin|twosock', 'QUIT' => 'loggedin|twosock', 'RNFR' => 'loggedin|twosock', 'RNTO' => 'loggedin|twosock', 'DELE' => 'loggedin|twosock', 'MDTM' => 'loggedin|twosock', );# initially, we're in 'fresh' statemy %statechange = ( 'USER' => 'passwd', # USER goes to passwd state 'PASS' => 'loggedin', # PASS goes to loggedin state 'PORT' => 'twosock', # PORT goes to twosock 'PASV' => 'twosock', # PASV goes to twosock 'EPSV' => 'twosock', # EPSV goes to twosock );# this text is shown before the function specified below is runmy %displaytext = ('USER' => '331 We are happy you popped in!', 'PASS' => '230 Welcome you silly person', 'PORT' => '200 You said PORT - I say FINE', 'TYPE' => '200 I modify TYPE as you wanted', 'LIST' => '150 here comes a directory', 'NLST' => '150 here comes a directory', 'CWD' => '250 CWD command successful.', 'SYST' => '215 UNIX Type: L8', # just fake something 'QUIT' => '221 bye bye baby', # just reply something 'PWD' => '257 "/nowhere/anywhere" is current directory', 'MKD' => '257 Created your requested directory', 'REST' => '350 Yeah yeah we set it there for you', 'DELE' => '200 OK OK OK whatever you say', 'RNFR' => '350 Received your order. Please provide more', 'RNTO' => '250 Ok, thanks. File renaming completed.', );# callback functions for certain commandsmy %commandfunc = ( 'PORT' => \&PORT_command, 'LIST' => \&LIST_command, 'NLST' => \&NLST_command, 'PASV' => \&PASV_command, 'EPSV' => \&PASV_command, 'RETR' => \&RETR_command, 'SIZE' => \&SIZE_command, 'REST' => \&REST_command, 'STOR' => \&STOR_command, 'APPE' => \&STOR_command, # append looks like upload 'MDTM' => \&MDTM_command, );my $rest=0;sub REST_command { $rest = $_[0]; logmsg "Set REST position to $rest\n"}sub LIST_command { # print "150 ASCII data connection for /bin/ls (193.15.23.1,59196) (0 bytes)\r\n";# this is a built-in fake-dir ;-)my @ftpdir=("total 20\r\n","drwxr-xr-x 8 98 98 512 Oct 22 13:06 .\r\n","drwxr-xr-x 8 98 98 512 Oct 22 13:06 ..\r\n","drwxr-xr-x 2 98 98 512 May 2 1996 .NeXT\r\n","-r--r--r-- 1 0 1 35 Jul 16 1996 README\r\n","lrwxrwxrwx 1 0 1 7 Dec 9 1999 bin -> usr/bin\r\n","dr-xr-xr-x 2 0 1 512 Oct 1 1997 dev\r\n","drwxrwxrwx 2 98 98 512 May 29 16:04 download.html\r\n","dr-xr-xr-x 2 0 1 512 Nov 30 1995 etc\r\n","drwxrwxrwx 2 98 1 512 Oct 30 14:33 pub\r\n","dr-xr-xr-x 5 0 1 512 Oct 1 1997 usr\r\n"); logmsg "$$: pass data to child pid\n"; for(@ftpdir) { print SOCK $_; } close(SOCK); logmsg "$$: done passing data to child pid\n"; print "226 ASCII transfer complete\r\n"; return 0;}sub NLST_command { my @ftpdir=("file", "with space", "fake", "..", " ..", "funny", "README"); for(@ftpdir) { print SOCK "$_\r\n"; } close(SOCK); print "226 ASCII transfer complete\r\n"; return 0;}sub MDTM_command { my $testno = $_[0]; loadtest("data/test$testno"); logmsg "MDTM $testno\n"; my @data = getpart("reply", "mdtm"); my $reply = $data[0]; chomp $reply; if($reply <0) { print "550 $testno: no such file.\r\n"; logmsg "MDTM $testno: no such file\n"; } elsif($reply) { print "$reply\r\n"; logmsg "MDTM $testno returned $reply\n"; } else { print "500 MDTM: no such command.\r\n"; logmsg "MDTM: no such command\n"; } return 0;}sub SIZE_command { my $testno = $_[0]; loadtest("data/test$testno"); logmsg "SIZE number $testno\n"; my @data = getpart("reply", "size"); my $size = $data[0]; if($size) { if($size > -1) { print "213 $size\r\n"; logmsg "SIZE $testno returned $size\n"; } else { print "550 $testno: No such file or directory.\r\n"; logmsg "SIZE $testno: no such file\n"; } } else { $size=0; @data = getpart("reply", "data"); for(@data) { $size += length($_); } if($size) { print "213 $size\r\n"; logmsg "SIZE $testno returned $size\n"; } else { print "550 $testno: No such file or directory.\r\n"; logmsg "SIZE $testno: no such file\n"; } } return 0;}sub RETR_command { my $testno = $_[0]; logmsg "RETR test number $testno\n"; if($testno =~ /^verifiedserver$/) { # this is the secret command that verifies that this actually is # the curl test server print "150 Binary junk (10 bytes).\r\n"; print SOCK "WE ROOLZ: $$\r\n"; close(SOCK); print "226 File transfer complete\r\n"; if($verbose) { print STDERR "FTPD: We returned proof we are the test server\n"; } logmsg "we returned proof that we are the test server\n"; return 0; } loadtest("data/test$testno"); my @data = getpart("reply", "data"); my $size=0; for(@data) { $size += length($_); } if($size) { if($rest) { # move read pointer forward $size -= $rest; logmsg "REST $rest was removed from size, makes $size left\n"; $rest = 0; # reset REST offset again } if($retrweirdo) { print "150 Binary data connection for $testno () ($size bytes).\r\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -