Annotation of embedaddon/php/ext/ftp/tests/server.inc, revision 1.1.1.2
1.1 misho 1: <?php
2:
3: $socket = null;
4: $errno = 0;
5: $context = stream_context_create(array('ssl' => array('local_cert' => dirname(__FILE__).'/cert.pem', 'passphrase' => 'pass')));
6:
7: for ($i=0; $i<10 && !$socket; ++$i) {
8: $port = rand(50000, 65535);
9:
10: $socket = stream_socket_server("tcp://127.0.0.1:$port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
11: }
12: //set anther random port that is not the same as $port
13: do{
14: $pasv_port = rand(50000, 65535);
15: }while($pasv_port == $port);
16:
17: if (!$socket) {
18: die("could not start/bind the ftp server\n");
19: }
20:
21:
22:
23:
24: $pid = pcntl_fork();
25:
26:
27:
28: function pasv_listen($action){
29: global $pasv_port, $tmp_file;
30: $tmp_file = 'nm2.php';
31: $pid = pcntl_fork();
32: if($pid === 0){
33: $soc = stream_socket_server("tcp://127.0.0.1:$pasv_port");
34: $fs = stream_socket_accept($soc, 3);
35: switch ($action) {
36: case 'fget':
37: case 'get':
38: //listen for 3 seconds 3 seconds
39: fputs($fs, "I am passive.\r\n");
40: break;
41: case 'put':
42: file_put_contents($tmp_file, stream_get_contents($fs));
43: break;
44: case 'list':
45: fputs($fs, "drwxr-x--- 3 owner group 4096 Jul 12 12:16 .\r\n");
46: fputs($fs, "drwxr-x--- 3 owner group 4096 Jul 12 12:16 ..\r\n");
47: fputs($fs, "drwxr-x--- 3 owner group 4096 Jul 12 12:16 public_ftp\r\n");
48: break;
49: case 'list_null':
50: fputs($fs, "\r\n");
51: break;
52: }
53: fclose($fs);
54: exit;
55: }
56: }
57:
58:
59:
60: if ($pid) {
61:
62: function dump_and_exit($buf)
63: {
64: var_dump($buf);
65: fclose($GLOBALS['s']);
66: exit;
67: }
68:
69: function anonymous()
70: {
71: return $GLOBALS['user'] === 'anonymous';
72: }
73:
74: /* quick&dirty realpath() like function */
75: function change_dir($dir)
76: {
77: global $cwd;
78:
79: if ($dir[0] == '/') {
80: $cwd = $dir;
81: return;
82: }
83:
84: $cwd = "$cwd/$dir";
85:
86: do {
87: $old = $cwd;
88: $cwd = preg_replace('@/?[^/]+/\.\.@', '', $cwd);
89: } while ($old != $cwd);
90:
91: $cwd = strtr($cwd, array('//' => '/'));
92: if (!$cwd) $cwd = '/';
93: }
94:
95: $s = stream_socket_accept($socket);
96:
97: if (!$s) die("Error accepting a new connection\n");
98:
99: fputs($s, "220----- PHP FTP server 0.3 -----\r\n220 Service ready\r\n");
100: $buf = fread($s, 2048);
101:
102:
103: function user_auth($buf) {
104: global $user, $s, $ssl, $bug37799;
105:
106: if (!empty($ssl)) {
107: if ($buf !== "AUTH TLS\r\n") {
108: fputs($s, "500 Syntax error, command unrecognized.\r\n");
109: dump_and_exit($buf);
110: }
111:
112: if (empty($bug37799)) {
113: fputs($s, "234 auth type accepted\r\n");
114: } else {
115: fputs($s, "666 dummy\r\n");
116: fputs($s, "666 bogus msg\r\n");
117: exit;
118: }
119:
120: if (!stream_socket_enable_crypto($s, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER)) {
121: die("SSLv23 handshake failed.\n");
122: }
123:
124: if (!preg_match('/^PBSZ \d+\r\n$/', $buf = fread($s, 2048))) {
125: fputs($s, "501 bogus data\r\n");
126: dump_and_exit($buf);
127: }
128:
129: fputs($s, "200 OK\r\n");
130: $buf = fread($s, 2048);
131:
132: if ($buf !== "PROT P\r\n") {
133: fputs($s, "504 Wrong protection.\r\n");
134: dump_and_exit($buf);
135: }
136:
137: fputs($s, "200 OK\r\n");
138:
139: $buf = fread($s, 2048);
140: }
141:
142: if (!preg_match('/^USER (\w+)\r\n$/', $buf, $m)) {
143: fputs($s, "500 Syntax error, command unrecognized.\r\n");
144: dump_and_exit($buf);
145: }
146: $user = $m[1];
147: if ($user !== 'user' && $user !== 'anonymous') {
148: fputs($s, "530 Not logged in.\r\n");
149: fclose($s);
150: exit;
151: }
152:
153: if (anonymous()) {
154: fputs($s, "230 Anonymous user logged in\r\n");
155:
156: } else {
157: fputs($s, "331 User name ok, need password\r\n");
158:
159: if (!preg_match('/^PASS (\w+)\r\n$/', $buf = fread($s, 100), $m)) {
160: fputs($s, "500 Syntax error, command unrecognized.\r\n");
161: dump_and_exit($buf);
162: }
163:
164: $pass = $m[1];
165: if ($pass === 'pass') {
166: fputs($s, "230 User logged in\r\n");
167: } else {
168: fputs($s, "530 Not logged in.\r\n");
169: fclose($s);
170: exit;
171: }
172: }
173: }
174:
175: user_auth($buf);
176:
177: $cwd = '/';
178: $num_bogus_cmds = 0;
179:
180: while($buf = fread($s, 4098)) {
181: if (!empty($bogus)) {
182: fputs($s, "502 Command not implemented (".$num_bogus_cmds++.").\r\n");
183:
184: } else if ($buf === "HELP\r\n") {
185: fputs($s, "214-There is help available for the following commands:\r\n");
186: fputs($s, " USER\r\n");
187: fputs($s, " HELP\r\n");
188: fputs($s, "214 end of list\r\n");
189:
190: } elseif ($buf === "HELP HELP\r\n") {
191: fputs($s, "214 Syntax: HELP [<SP> <string>] <CRLF>\r\n");
192:
193: } elseif ($buf === "PWD\r\n") {
194: fputs($s, "257 \"$cwd\" is current directory.\r\n");
195:
196: } elseif ($buf === "CDUP\r\n") {
197: change_dir('..');
198: fputs($s, "250 CDUP command successful.\r\n");
199:
200: } elseif ($buf === "SYST\r\n") {
201: if (isset($bug27809)) {
202: fputs($s, "215 OS/400 is the remote operating system. The TCP/IP version is \"V5R2M0\"\r\n");
203: } else {
204: fputs($s, "215 UNIX Type: L8.\r\n");
205: }
206:
207: } elseif ($buf === "TYPE A\r\n") {
208: $ascii = true;
209: fputs($s, "200 OK\r\n");
210:
211: } elseif ($buf === "TYPE I\r\n") {
212: $ascii = false;
213: fputs($s, "200 OK\r\n");
214:
215: } elseif ($buf === "QUIT\r\n") {
216: break;
217:
218: } elseif (preg_match("~^PORT (\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\r\n$~", $buf, $m)) {
219: $host = "$m[1].$m[2].$m[3].$m[4]";
220: $port = ((int)$m[5] << 8) + (int)$m[6];
221: fputs($s, "200 OK.\r\n");
222:
223: } elseif (preg_match("~^STOR ([\w/.-]+)\r\n$~", $buf, $m)) {
224: fputs($s, "150 File status okay; about to open data connection\r\n");
225:
226: if(empty($pasv))
227: {
228: if (!$fs = stream_socket_client("tcp://$host:$port")) {
229: fputs($s, "425 Can't open data connection\r\n");
230: continue;
231: }
232:
233: $data = stream_get_contents($fs);
234: $orig = file_get_contents(dirname(__FILE__).'/'.$m[1]);
235:
236:
237: if (isset($ascii) && !$ascii && $orig === $data) {
238: fputs($s, "226 Closing data Connection.\r\n");
239:
240: } elseif ((!empty($ascii) || isset($bug39583)) && $data === strtr($orig, array("\r\n" => "\n", "\r" => "\n", "\n" => "\r\n"))) {
241: fputs($s, "226 Closing data Connection.\r\n");
242:
243: } else {
244: var_dump($data);
245: var_dump($orig);
246: fputs($s, "552 Requested file action aborted.\r\n");
247: }
248: fclose($fs);
249: }else{
250: $data = file_get_contents('nm2.php');
251: $orig = file_get_contents(dirname(__FILE__).'/'.$m[1]);
252: if ( $orig === $data) {
253: fputs($s, "226 Closing data Connection.\r\n");
254:
255: } else {
256: var_dump($data);
257: var_dump($orig);
258: fputs($s, "552 Requested file action aborted.\r\n");
259: }
260: }
261:
262: } elseif (preg_match("~^CWD ([A-Za-z./]+)\r\n$~", $buf, $m)) {
263: change_dir($m[1]);
264: fputs($s, "250 CWD command successful.\r\n");
265:
266: } elseif (preg_match("~^NLST(?: ([A-Za-z./]+))?\r\n$~", $buf, $m)) {
267:
268: if (isset($m[1]) && $m[1] === 'bogusdir') {
269: fputs($s, "250 $m[1]: No such file or directory\r\n");
270: continue;
271: }
272:
273: // there are some servers that don't open the ftp-data socket if there's nothing to send
274: if (isset($bug39458) && isset($m[1]) && $m[1] === 'emptydir') {
275: fputs($s, "226 Transfer complete.\r\n");
276: continue;
277: }
278:
279: fputs($s, "150 File status okay; about to open data connection\r\n");
280:
281: if (!$fs = stream_socket_client("tcp://$host:$port")) {
282: fputs($s, "425 Can't open data connection\r\n");
283: continue;
284: }
285:
286: if (empty($m[1]) || $m[1] !== 'emptydir') {
287: fputs($fs, "file1\r\nfile1\r\nfile\nb0rk\r\n");
288: }
289:
290: fputs($s, "226 Closing data Connection.\r\n");
291: fclose($fs);
292:
293: } elseif (preg_match("~^MKD ([A-Za-z./]+)\r\n$~", $buf, $m)) {
294: if (isset($bug7216)) {
295: fputs($s, "257 OK.\r\n");
296: } else {
297: fputs($s, "257 \"/path/to/ftproot$cwd$m[1]\" created.\r\n");
298: }
299:
300: } elseif (preg_match('/^USER /', $buf)) {
301: user_auth($buf);
302:
303: } elseif (preg_match('/^MDTM ([\w\h]+)/', $buf, $matches)) {
304: switch ($matches [1]){
305: case "A":
306: fputs($s, "213 19980615100045.014\r\n");
307: break;
308: case "B":
309: fputs($s, "213 19980615100045.014\r\n");
310: break;
311: case "C":
312: fputs($s, "213 19980705132316\r\n");
313: break;
314: case "19990929043300 File6":
315: fputs($s, "213 19991005213102\r\n");
316: break;
317: default :
318: fputs($s, "550 No file named \"{$matches [1]}\"\r\n");
319: break;
320: }
321: }elseif (preg_match('/^RETR ([\w\h]+)/', $buf, $matches)) {
322: if(!empty($pasv)){
323: ;
324: }
325: else if (!$fs = stream_socket_client("tcp://$host:$port")) {
326: fputs($s, "425 Can't open data connection\r\n");
327: continue;
328: }
329:
330: switch($matches[1]){
331:
332: case "pasv":
333: fputs($s, "150 File status okay; about to open data connection.\r\n");
334: //the data connection is handled in another forked process
335: // called from outside this while loop
336: fputs($s, "226 Closing data Connection.\r\n");
337: break;
338: case "a story":
339: fputs($s, "150 File status okay; about to open data connection.\r\n");
340: fputs($fs, "For sale: baby shoes, never worn.\r\n");
341: fputs($s, "226 Closing data Connection.\r\n");
342: break;
343: case "binary data":
344: fputs($s, "150 File status okay; about to open data connection.\r\n");
345: $transfer_type = $ascii? 'ASCII' : 'BINARY' ;
346: fputs($fs, $transfer_type."Foo\0Bar\r\n");
347: fputs($s, "226 Closing data Connection.\r\n");
348: break;
349: case "fget":
350: fputs($s, "150 File status okay; about to open data connection.\r\n");
351: $transfer_type = $ascii? 'ASCII' : 'BINARY' ;
352: fputs($fs, $transfer_type."FooBar\r\n");
353: fputs($s, "226 Closing data Connection.\r\n");
354: break;
355: case "fgetresume":
356: fputs($s, "150 File status okay; about to open data connection.\r\n");
357: $transfer_type = $ascii? 'ASCII' : 'BINARY' ;
358: fputs($fs, "Bar\r\n");
359: fputs($s, "226 Closing data Connection.\r\n");
1.1.1.2 ! misho 360: break;
! 361: case "fget_large":
! 362: fputs($s, "150 File status okay; about to open data connection.\r\n");
! 363: $transfer_type = $ascii? 'ASCII' : 'BINARY' ;
! 364: if ($GLOBALS['rest_pos'] == '5368709119') {
! 365: fputs($fs, "X");
! 366: } else {
! 367: fputs($fs, "Y");
! 368: }
! 369: fputs($s, "226 Closing data Connection.\r\n");
! 370: break;
1.1 misho 371: default:
372: fputs($s, "550 {$matches[1]}: No such file or directory \r\n");
373: break;
374: }
375: if(isset($fs))
376: fclose($fs);
377:
378:
379: }elseif (preg_match('/^PASV/', $buf, $matches)) {
380: $port = $pasv_port;
381: $p2 = $port % ((int) 1 << 8);
382: $p1 = ($port-$p2)/((int) 1 << 8);
383: $host = "127.0.0.1";
384: fputs($s, "227 Entering Passive Mode. (127,0,0,1,{$p1},{$p2})\r\n");
385:
386:
387: } elseif (preg_match('/^SITE EXEC/', $buf, $matches)) {
388: fputs($s, "200 OK\r\n");
389:
390: } elseif (preg_match('/^RMD/', $buf, $matches)) {
391: fputs($s, "250 OK\r\n");
392:
393: } elseif (preg_match('/^SITE CHMOD/', $buf, $matches)) {
394: fputs($s, "200 OK\r\n");
395:
396: } elseif (preg_match('/^ALLO (\d+)/', $buf, $matches)) {
397: fputs($s, "200 " . $matches[1] . " bytes allocated\r\n");
398:
399: }elseif (preg_match('/^LIST www\//', $buf, $matches)) {
400: fputs($s, "150 Opening ASCII mode data connection for file list\r\n");
401: fputs($s, "226 Transfer complete\r\n");
402:
403: }elseif (preg_match('/^LIST no_exists\//', $buf, $matches)) {
404: fputs($s, "425 Error establishing connection\r\n");
405:
1.1.1.2 ! misho 406: }elseif (preg_match('/^REST (\d+)/', $buf, $matches)) {
! 407: $GLOBALS['rest_pos'] = $matches[1];
1.1 misho 408: fputs($s, "350 OK\r\n");
1.1.1.2 ! misho 409: }elseif (preg_match('/^SIZE largefile/', $buf)) {
! 410: fputs($s, "213 5368709120\r\n");
! 411: }else {
1.1 misho 412: fputs($s, "500 Syntax error, command unrecognized.\r\n");
413: dump_and_exit($buf);
414: }
415: }
416: fclose($s);
417: exit;
418: }
419:
420: fclose($socket);
1.1.1.2 ! misho 421: ?>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>