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,32 +6,74 @@ $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)
{
function dump_and_exit($buf)
{
var_dump($buf);
fclose($GLOBALS['s']);
exit;
}
}
function anonymous()
{
function anonymous()
{
return $GLOBALS['user'] === 'anonymous';
}
}
/* quick&dirty realpath() like function */
function change_dir($dir)
{
/* quick&dirty realpath() like function */
function change_dir($dir)
{
global $cwd;
if ($dir[0] == '/') {
@ -48,20 +90,20 @@ function change_dir($dir)
$cwd = strtr($cwd, array('//' => '/'));
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");
$buf = fread($s, 2048);
$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");
$buf = fread($s, 2048);
function user_auth($buf) {
function user_auth($buf) {
global $user, $s, $ssl, $bug37799;
if (!empty($ssl)) {
if (!empty($ssl)) {
if ($buf !== "AUTH TLS\r\n") {
fputs($s, "500 Syntax error, command unrecognized.\r\n");
dump_and_exit($buf);
@ -95,23 +137,23 @@ if (!empty($ssl)) {
fputs($s, "200 OK\r\n");
$buf = fread($s, 2048);
}
}
if (!preg_match('/^USER (\w+)\r\n$/', $buf, $m)) {
if (!preg_match('/^USER (\w+)\r\n$/', $buf, $m)) {
fputs($s, "500 Syntax error, command unrecognized.\r\n");
dump_and_exit($buf);
}
$user = $m[1];
if ($user !== 'user' && $user !== 'anonymous') {
}
$user = $m[1];
if ($user !== 'user' && $user !== 'anonymous') {
fputs($s, "530 Not logged in.\r\n");
fclose($s);
exit;
}
}
if (anonymous()) {
if (anonymous()) {
fputs($s, "230 Anonymous user logged in\r\n");
} else {
} else {
fputs($s, "331 User name ok, need password\r\n");
if (!preg_match('/^PASS (\w+)\r\n$/', $buf = fread($s, 100), $m)) {
@ -127,15 +169,15 @@ if (anonymous()) {
fclose($s);
exit;
}
}
}
}
}
user_auth($buf);
user_auth($buf);
$cwd = '/';
$num_bogus_cmds = 0;
$cwd = '/';
$num_bogus_cmds = 0;
while($buf = fread($s, 4098)) {
while($buf = fread($s, 4098)) {
if (!empty($bogus)) {
fputs($s, "502 Command not implemented (".$num_bogus_cmds++.").\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,20 +346,64 @@ 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;
}
fclose($s);
exit;
}
fclose($socket);