Skip to content

Commit 742783c

Browse files
Ville Hukkamakinikic
Ville Hukkamaki
authored andcommitted
Fixed bug #73457
Correctly report errors when opening FTP data connection.
1 parent f26172f commit 742783c

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed

NEWS

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ PHP NEWS
33
?? ??? 2018, PHP 7.1.23
44

55
- POSIX:
6-
Fixed bug #75696 (posix_getgrnam fails to print details of group). (cmb)
6+
. Fixed bug #75696 (posix_getgrnam fails to print details of group). (cmb)
7+
8+
- Standard:
9+
. Fixed bug #73457 (Wrong error message when fopen FTP wrapped fails to open
10+
data connection). (Ville Hukkamäki)
711

812
13 Sep 2018, PHP 7.1.22
913

ext/ftp/tests/server.inc

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ if ($pid) {
335335
fputs($s, "550 No file named \"{$matches [1]}\"\r\n");
336336
break;
337337
}
338-
}elseif (preg_match('/^RETR ([\w\h]+)/', $buf, $matches)) {
338+
}elseif (preg_match('/^RETR ([\/]*[\w\h]+)/', $buf, $matches)) {
339339
if(!empty($pasv)){
340340
;
341341
}
@@ -391,6 +391,10 @@ if ($pid) {
391391
fputs($fs, "This is line $i of the test data.\n");
392392
}
393393
fputs($s, "226 Closing data Connection.\r\n");
394+
break;
395+
case "/bug73457":
396+
fputs($s, "150 File status okay; about to open data connection.\r\n");
397+
break;
394398

395399
default:
396400
fputs($s, "550 {$matches[1]}: No such file or directory \r\n");
@@ -405,29 +409,35 @@ if ($pid) {
405409
$host = "127.0.0.1";
406410
$i=0;
407411

408-
do {
409-
if (!empty($ssl)) {
410-
$soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
411-
} else {
412-
$soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port");
413-
}
414-
/* Could bind port, Try another port */
412+
if (empty($bug73457)) {
413+
do {
414+
if (!empty($ssl)) {
415+
$soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
416+
} else {
417+
$soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port");
418+
}
419+
/* Could bind port, Try another port */
420+
if (!$soc) {
421+
$pasv_port = rand(50000, 65535);
422+
}
423+
$i++;
424+
} while ($i<10 && !$soc);
425+
415426
if (!$soc) {
416-
$pasv_port = rand(50000, 65535);
427+
echo "$errstr ($errno)\n";
428+
die("could not bind passive port\n");
417429
}
418-
$i++;
419-
} while ($i<10 && !$soc);
420-
421-
if (!$soc) {
422-
echo "$errstr ($errno)\n";
423-
die("could not bind passive port\n");
430+
} else {
431+
$pasv_port=1234;
424432
}
425433

426434
$p2 = $pasv_port % ((int) 1 << 8);
427435
$p1 = ($pasv_port-$p2)/((int) 1 << 8);
428436
fputs($s, "227 Entering Passive Mode. (127,0,0,1,{$p1},{$p2})\r\n");
429437

430-
$pasvs = stream_socket_accept($soc,10);
438+
if (empty($bug73457)) {
439+
$pasvs = stream_socket_accept($soc,10);
440+
}
431441

432442
} elseif (preg_match('/^EPSV/', $buf, $matches)) {
433443
fputs($s, "550 Extended passsive mode not supported.\r\n");
@@ -455,6 +465,8 @@ if ($pid) {
455465
fputs($s, "350 OK\r\n");
456466
}elseif (preg_match('/^SIZE largefile/', $buf)) {
457467
fputs($s, "213 5368709120\r\n");
468+
}elseif (preg_match('/^SIZE \/bug73457/', $buf)) {
469+
fputs($s, "213 10\r\n");
458470
}else {
459471
fputs($s, "500 Syntax error, command unrecognized.\r\n");
460472
dump_and_exit($buf);

ext/standard/ftp_fopen_wrapper.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *pa
430430
int8_t read_write = 0;
431431
char *transport;
432432
int transport_len;
433+
zend_string *error_message = NULL;
433434

434435
tmp_line[0] = '\0';
435436

@@ -557,9 +558,10 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *pa
557558
hoststart = resource->host;
558559
}
559560
transport_len = (int)spprintf(&transport, 0, "tcp://%s:%d", hoststart, portno);
560-
datastream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, NULL, NULL);
561+
datastream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, &error_message, NULL);
561562
efree(transport);
562563
if (datastream == NULL) {
564+
tmp_line[0]='\0';
563565
goto errexit;
564566
}
565567

@@ -583,6 +585,7 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *pa
583585
php_stream_wrapper_log_error(wrapper, options, "Unable to activate SSL mode");
584586
php_stream_close(datastream);
585587
datastream = NULL;
588+
tmp_line[0]='\0';
586589
goto errexit;
587590
}
588591

@@ -602,6 +605,11 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *pa
602605
}
603606
if (tmp_line[0] != '\0')
604607
php_stream_wrapper_log_error(wrapper, options, "FTP server reports %s", tmp_line);
608+
609+
if (error_message) {
610+
php_stream_wrapper_log_error(wrapper, options, "Failed to set up data channel: %s", ZSTR_VAL(error_message));
611+
zend_string_release(error_message);
612+
}
605613
return NULL;
606614
}
607615
/* }}} */
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #73457. Wrong error message when fopen FTP wrapped fails to open data connection
3+
--SKIPIF--
4+
<?php
5+
if (array_search('ftp',stream_get_wrappers()) === FALSE) die("skip ftp wrapper not available.");
6+
if (!function_exists('pcntl_fork')) die("skip pcntl_fork() not available.");
7+
?>
8+
--FILE--
9+
<?php
10+
11+
$bug73457=true;
12+
require __DIR__ . "/../../../ftp/tests/server.inc";
13+
14+
$path="ftp://127.0.0.1:" . $port."/bug73457";
15+
16+
$ds=file_get_contents($path);
17+
var_dump($ds);
18+
?>
19+
==DONE==
20+
--EXPECTF--
21+
Warning: file_get_contents(ftp://127.0.0.1:%d/bug73457): failed to open stream: Failed to set up data channel: Connection refused in %s on line %d
22+
bool(false)
23+
==DONE==

0 commit comments

Comments
 (0)