server for ftp tests

This commit is contained in:
Zoe Slattery 2009-08-30 09:31:54 +00:00
parent d4ba889aa4
commit acbc3d54f8

View file

@ -6,15 +6,57 @@ $context = stream_context_create(array('ssl' => array('local_cert' => dirname(__
for ($i=0; $i<10 && !$socket; ++$i) {
$port = rand(50000, 65535);
$socket = stream_socket_server("tcp://127.0.0.1:$port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
}
//set anther random port that is not the same as $port
do{
$pasv_port = rand(50000, 65535);
}while($pasv_port == $port);
if (!$socket) {
die("could not start/bind the ftp server\n");
}
$pid = pcntl_fork();
function pasv_listen($action){
global $pasv_port, $tmp_file;
$tmp_file = 'nm2.php';
$pid = pcntl_fork();
if($pid === 0){
$soc = stream_socket_server("tcp://127.0.0.1:$pasv_port");
$fs = stream_socket_accept($soc, 3);
switch ($action) {
case 'fget':
case 'get':
//listen for 3 seconds 3 seconds
fputs($fs, "I am passive.\r\n");
break;
case 'put':
file_put_contents($tmp_file, stream_get_contents($fs));
break;
case 'list':
fputs($fs, "drwxr-x--- 3 owner group 4096 Jul 12 12:16 .\r\n");
fputs($fs, "drwxr-x--- 3 owner group 4096 Jul 12 12:16 ..\r\n");
fputs($fs, "drwxr-x--- 3 owner group 4096 Jul 12 12:16 public_ftp\r\n");
break;
case 'list_null':
fputs($fs, "\r\n");
break;
}
fclose($fs);
exit;
}
}
if ($pid) {
function dump_and_exit($buf)
@ -50,8 +92,8 @@ function change_dir($dir)
if (!$cwd) $cwd = '/';
}
$s = stream_socket_accept($socket);
if (!$s) die("Error accepting a new connection\n");
fputs($s, "220----- PHP FTP server 0.3 -----\r\n220 Service ready\r\n");
@ -181,6 +223,8 @@ while($buf = fread($s, 4098)) {
} elseif (preg_match("~^STOR ([\w/.-]+)\r\n$~", $buf, $m)) {
fputs($s, "150 File status okay; about to open data connection\r\n");
if(empty($pasv))
{
if (!$fs = stream_socket_client("tcp://$host:$port")) {
fputs($s, "425 Can't open data connection\r\n");
continue;
@ -189,6 +233,7 @@ while($buf = fread($s, 4098)) {
$data = stream_get_contents($fs);
$orig = file_get_contents(dirname(__FILE__).'/'.$m[1]);
if (isset($ascii) && !$ascii && $orig === $data) {
fputs($s, "226 Closing data Connection.\r\n");
@ -201,6 +246,18 @@ while($buf = fread($s, 4098)) {
fputs($s, "552 Requested file action aborted.\r\n");
}
fclose($fs);
}else{
$data = file_get_contents('nm2.php');
$orig = file_get_contents(dirname(__FILE__).'/'.$m[1]);
if ( $orig === $data) {
fputs($s, "226 Closing data Connection.\r\n");
} else {
var_dump($data);
var_dump($orig);
fputs($s, "552 Requested file action aborted.\r\n");
}
}
} elseif (preg_match("~^CWD ([A-Za-z./]+)\r\n$~", $buf, $m)) {
change_dir($m[1]);
@ -262,11 +319,22 @@ while($buf = fread($s, 4098)) {
break;
}
}elseif (preg_match('/^RETR ([\w\h]+)/', $buf, $matches)) {
if (!$fs = stream_socket_client("tcp://$host:$port")) {
if(!empty($pasv)){
;
}
else if (!$fs = stream_socket_client("tcp://$host:$port")) {
fputs($s, "425 Can't open data connection\r\n");
continue;
}
switch($matches[1]){
case "pasv":
fputs($s, "150 File status okay; about to open data connection.\r\n");
//the data connection is handled in another forked process
// called from outside this while loop
fputs($s, "226 Closing data Connection.\r\n");
break;
case "a story":
fputs($s, "150 File status okay; about to open data connection.\r\n");
fputs($fs, "For sale: baby shoes, never worn.\r\n");
@ -278,18 +346,62 @@ while($buf = fread($s, 4098)) {
fputs($fs, $transfer_type."Foo\0Bar\r\n");
fputs($s, "226 Closing data Connection.\r\n");
break;
case "fget":
fputs($s, "150 File status okay; about to open data connection.\r\n");
$transfer_type = $ascii? 'ASCII' : 'BINARY' ;
fputs($fs, $transfer_type."FooBar\r\n");
fputs($s, "226 Closing data Connection.\r\n");
break;
case "fgetresume":
fputs($s, "150 File status okay; about to open data connection.\r\n");
$transfer_type = $ascii? 'ASCII' : 'BINARY' ;
fputs($fs, "Bar\r\n");
fputs($s, "226 Closing data Connection.\r\n");
break;
default:
fputs($s, "550 {$matches[1]}: No such file or directory \r\n");
break;
}
if(isset($fs))
fclose($fs);
}elseif (preg_match('/^PASV/', $buf, $matches)) {
$port = $pasv_port;
$p2 = $port % ((int) 1 << 8);
$p1 = ($port-$p2)/((int) 1 << 8);
$host = "127.0.0.1";
fputs($s, "227 Entering Passive Mode. (127,0,0,1,{$p1},{$p2})\r\n");
} elseif (preg_match('/^SITE EXEC/', $buf, $matches)) {
fputs($s, "200 OK\r\n");
} elseif (preg_match('/^RMD/', $buf, $matches)) {
fputs($s, "250 OK\r\n");
} elseif (preg_match('/^SITE CHMOD/', $buf, $matches)) {
fputs($s, "200 OK\r\n");
} elseif (preg_match('/^ALLO (\d+)/', $buf, $matches)) {
fputs($s, "200 " . $matches[1] . " bytes allocated\r\n");
}elseif (preg_match('/^LIST www\//', $buf, $matches)) {
fputs($s, "150 Opening ASCII mode data connection for file list\r\n");
fputs($s, "226 Transfer complete\r\n");
}elseif (preg_match('/^LIST no_exists\//', $buf, $matches)) {
fputs($s, "425 Error establishing connection\r\n");
}elseif (preg_match('/^REST \d+/', $buf, $matches)) {
fputs($s, "350 OK\r\n");
}
else {
fputs($s, "500 Syntax error, command unrecognized.\r\n");
dump_and_exit($buf);
}
}
fclose($s);
exit;
}