/src/php-src/ext/standard/ftp_fopen_wrapper.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Rasmus Lerdorf <rasmus@php.net> | |
12 | | | Jim Winstead <jimw@php.net> | |
13 | | | Hartmut Holzgraefe <hholzgra@php.net> | |
14 | | | Sara Golemon <pollita@php.net> | |
15 | | +----------------------------------------------------------------------+ |
16 | | */ |
17 | | |
18 | | #include "php.h" |
19 | | #include "php_globals.h" |
20 | | #include "php_network.h" |
21 | | #include "php_ini.h" |
22 | | #include "zend_exceptions.h" |
23 | | |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <errno.h> |
27 | | #include <sys/types.h> |
28 | | #include <sys/stat.h> |
29 | | #include <fcntl.h> |
30 | | |
31 | | #ifdef PHP_WIN32 |
32 | | #include <winsock2.h> |
33 | | #endif |
34 | | |
35 | | #include "php_standard.h" |
36 | | #include "ext/uri/php_uri.h" |
37 | | |
38 | | #ifdef HAVE_SYS_SOCKET_H |
39 | | #include <sys/socket.h> |
40 | | #endif |
41 | | |
42 | | #ifdef PHP_WIN32 |
43 | | #include <winsock2.h> |
44 | | #else |
45 | | #include <netinet/in.h> |
46 | | #include <netdb.h> |
47 | | #ifdef HAVE_ARPA_INET_H |
48 | | #include <arpa/inet.h> |
49 | | #endif |
50 | | #endif |
51 | | |
52 | | #if defined(PHP_WIN32) || defined(__riscos__) |
53 | | #undef AF_UNIX |
54 | | #endif |
55 | | |
56 | | #if defined(AF_UNIX) |
57 | | #include <sys/un.h> |
58 | | #endif |
59 | | |
60 | | #include "php_fopen_wrappers.h" |
61 | | |
62 | | #define FTPS_ENCRYPT_DATA 1 |
63 | 0 | #define GET_FTP_RESULT(stream) get_ftp_result((stream), tmp_line, sizeof(tmp_line)) |
64 | | |
65 | | typedef struct _php_ftp_dirstream_data { |
66 | | php_stream *datastream; |
67 | | php_stream *controlstream; |
68 | | php_stream *dirstream; |
69 | | } php_ftp_dirstream_data; |
70 | | |
71 | | /* {{{ get_ftp_result */ |
72 | | static inline int get_ftp_result(php_stream *stream, char *buffer, size_t buffer_size) |
73 | 0 | { |
74 | 0 | buffer[0] = '\0'; /* in case read fails to read anything */ |
75 | 0 | while (php_stream_gets(stream, buffer, buffer_size-1) && |
76 | 0 | !(isdigit((unsigned char)buffer[0]) && isdigit((unsigned char)buffer[1]) && |
77 | 0 | isdigit((unsigned char)buffer[2]) && buffer[3] == ' ')); |
78 | 0 | return strtol(buffer, NULL, 10); |
79 | 0 | } |
80 | | /* }}} */ |
81 | | |
82 | | /* {{{ php_stream_ftp_stream_stat */ |
83 | | static int php_stream_ftp_stream_stat(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb) |
84 | 0 | { |
85 | | /* For now, we return with a failure code to prevent the underlying |
86 | | * file's details from being used instead. */ |
87 | 0 | return -1; |
88 | 0 | } |
89 | | /* }}} */ |
90 | | |
91 | | /* {{{ php_stream_ftp_stream_close */ |
92 | | static int php_stream_ftp_stream_close(php_stream_wrapper *wrapper, php_stream *stream) |
93 | 0 | { |
94 | 0 | php_stream *controlstream = stream->wrapperthis; |
95 | 0 | int ret = 0; |
96 | |
|
97 | 0 | if (controlstream) { |
98 | 0 | if (strpbrk(stream->mode, "wa+")) { |
99 | 0 | char tmp_line[512]; |
100 | 0 | int result; |
101 | | |
102 | | /* For write modes close data stream first to signal EOF to server */ |
103 | 0 | result = GET_FTP_RESULT(controlstream); |
104 | 0 | if (result != 226 && result != 250) { |
105 | 0 | php_stream_wrapper_warn(wrapper, PHP_STREAM_CONTEXT(stream), REPORT_ERRORS, |
106 | 0 | ProtocolError, |
107 | 0 | "FTP server error %d:%s", result, tmp_line); |
108 | 0 | ret = EOF; |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | 0 | php_stream_write_string(controlstream, "QUIT\r\n"); |
113 | 0 | php_stream_close(controlstream); |
114 | 0 | stream->wrapperthis = NULL; |
115 | 0 | } |
116 | |
|
117 | 0 | return ret; |
118 | 0 | } |
119 | | /* }}} */ |
120 | | |
121 | | /* {{{ php_ftp_fopen_connect */ |
122 | | static php_stream *php_ftp_fopen_connect(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, |
123 | | zend_string **opened_path, php_stream_context *context, php_stream **preuseid, |
124 | | php_uri **presource, int *puse_ssl, int *puse_ssl_on_data) |
125 | 0 | { |
126 | 0 | php_stream *stream = NULL, *reuseid = NULL; |
127 | 0 | php_uri *resource = NULL; |
128 | 0 | int result, use_ssl, use_ssl_on_data = 0; |
129 | 0 | char tmp_line[512]; |
130 | |
|
131 | 0 | const php_uri_parser *uri_parser = php_stream_context_get_uri_parser("ftp", context); |
132 | 0 | if (uri_parser == NULL) { |
133 | 0 | zend_value_error("%s(): Provided stream context has invalid value for the \"uri_parser_class\" option", get_active_function_name()); |
134 | 0 | return NULL; |
135 | 0 | } |
136 | | |
137 | 0 | resource = php_uri_parse_to_struct(uri_parser, path, strlen(path), PHP_URI_COMPONENT_READ_MODE_RAW, true); |
138 | 0 | if (resource == NULL || resource->path == NULL) { |
139 | 0 | if (resource && presource) { |
140 | 0 | *presource = resource; |
141 | 0 | } |
142 | 0 | return NULL; |
143 | 0 | } |
144 | | |
145 | | /* Searching for a substring is a bit expensive, and this is just a |
146 | | * hardening assertion to confirm php_uri_parse_to_struct() stripped any |
147 | | * CRLF sequence, only run in debug mode. */ |
148 | 0 | #if ZEND_DEBUG |
149 | 0 | char *pos = strstr(ZSTR_VAL(resource->path), "\r\n"); |
150 | 0 | ZEND_ASSERT(pos == NULL); |
151 | 0 | #endif |
152 | |
|
153 | 0 | use_ssl = resource->scheme && (ZSTR_LEN(resource->scheme) > 3) && ZSTR_VAL(resource->scheme)[3] == 's'; |
154 | | |
155 | | /* use port 21 if one wasn't specified */ |
156 | 0 | if (resource->port == 0) |
157 | 0 | resource->port = 21; |
158 | |
|
159 | 0 | char *transport; |
160 | 0 | size_t transport_len = spprintf(&transport, 0, "tcp://%s:" ZEND_LONG_FMT, ZSTR_VAL(resource->host), resource->port); |
161 | 0 | stream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, NULL, NULL); |
162 | 0 | efree(transport); |
163 | 0 | if (stream == NULL) { |
164 | 0 | result = 0; /* silence */ |
165 | 0 | goto connect_errexit; |
166 | 0 | } |
167 | | |
168 | 0 | php_stream_context_set(stream, context); |
169 | 0 | php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0); |
170 | | |
171 | | /* Start talking to ftp server */ |
172 | 0 | result = GET_FTP_RESULT(stream); |
173 | 0 | if (result > 299 || result < 200) { |
174 | 0 | php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result); |
175 | 0 | goto connect_errexit; |
176 | 0 | } |
177 | | |
178 | 0 | if (use_ssl) { |
179 | | |
180 | | /* send the AUTH TLS request name */ |
181 | 0 | php_stream_write_string(stream, "AUTH TLS\r\n"); |
182 | | |
183 | | /* get the response */ |
184 | 0 | result = GET_FTP_RESULT(stream); |
185 | 0 | if (result != 234) { |
186 | | /* AUTH TLS not supported try AUTH SSL */ |
187 | 0 | php_stream_write_string(stream, "AUTH SSL\r\n"); |
188 | | |
189 | | /* get the response */ |
190 | 0 | result = GET_FTP_RESULT(stream); |
191 | 0 | if (result != 334) { |
192 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, SslNotSupported, |
193 | 0 | "Server doesn't support FTPS."); |
194 | 0 | goto connect_errexit; |
195 | 0 | } else { |
196 | | /* we must reuse the old SSL session id */ |
197 | | /* if we talk to an old ftpd-ssl */ |
198 | 0 | reuseid = stream; |
199 | 0 | } |
200 | 0 | } else { |
201 | | /* encrypt data etc */ |
202 | | |
203 | |
|
204 | 0 | } |
205 | |
|
206 | 0 | } |
207 | | |
208 | 0 | if (use_ssl) { |
209 | 0 | if (php_stream_xport_crypto_setup(stream, |
210 | 0 | STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL) < 0 |
211 | 0 | || php_stream_xport_crypto_enable(stream, 1) < 0) { |
212 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, SslNotSupported, |
213 | 0 | "Unable to activate SSL mode"); |
214 | 0 | php_stream_close(stream); |
215 | 0 | stream = NULL; |
216 | 0 | goto connect_errexit; |
217 | 0 | } |
218 | | |
219 | | /* set PBSZ to 0 */ |
220 | 0 | php_stream_write_string(stream, "PBSZ 0\r\n"); |
221 | | |
222 | | /* ignore the response */ |
223 | 0 | result = GET_FTP_RESULT(stream); |
224 | | |
225 | | /* set data connection protection level */ |
226 | 0 | #if FTPS_ENCRYPT_DATA |
227 | 0 | php_stream_write_string(stream, "PROT P\r\n"); |
228 | | |
229 | | /* get the response */ |
230 | 0 | result = GET_FTP_RESULT(stream); |
231 | 0 | use_ssl_on_data = (result >= 200 && result<=299) || reuseid; |
232 | | #else |
233 | | php_stream_write_string(stream, "PROT C\r\n"); |
234 | | |
235 | | /* get the response */ |
236 | | result = GET_FTP_RESULT(stream); |
237 | | #endif |
238 | 0 | } |
239 | | |
240 | 0 | #define PHP_FTP_CNTRL_CHK(val, val_len, err_msg) { \ |
241 | 0 | unsigned char *s = (unsigned char *) val, *e = (unsigned char *) s + val_len; \ |
242 | 0 | while (s < e) { \ |
243 | 0 | if (iscntrl((unsigned char)*s)) { \ |
244 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, AuthFailed, err_msg, val); \ |
245 | 0 | goto connect_errexit; \ |
246 | 0 | } \ |
247 | 0 | s++; \ |
248 | 0 | } \ |
249 | 0 | } |
250 | | |
251 | | /* send the user name */ |
252 | 0 | if (resource->user != NULL) { |
253 | 0 | ZSTR_LEN(resource->user) = php_raw_url_decode(ZSTR_VAL(resource->user), ZSTR_LEN(resource->user)); |
254 | |
|
255 | 0 | PHP_FTP_CNTRL_CHK(ZSTR_VAL(resource->user), ZSTR_LEN(resource->user), "Invalid login %s") |
256 | | |
257 | 0 | php_stream_printf(stream, "USER %s\r\n", ZSTR_VAL(resource->user)); |
258 | 0 | } else { |
259 | 0 | php_stream_write_string(stream, "USER anonymous\r\n"); |
260 | 0 | } |
261 | | |
262 | | /* get the response */ |
263 | 0 | result = GET_FTP_RESULT(stream); |
264 | | |
265 | | /* if a password is required, send it */ |
266 | 0 | if (result >= 300 && result <= 399) { |
267 | 0 | php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, tmp_line, 0); |
268 | |
|
269 | 0 | if (resource->password != NULL) { |
270 | 0 | ZSTR_LEN(resource->password) = php_raw_url_decode(ZSTR_VAL(resource->password), ZSTR_LEN(resource->password)); |
271 | |
|
272 | 0 | PHP_FTP_CNTRL_CHK(ZSTR_VAL(resource->password), ZSTR_LEN(resource->password), "Invalid password %s") |
273 | | |
274 | 0 | php_stream_printf(stream, "PASS %s\r\n", ZSTR_VAL(resource->password)); |
275 | 0 | } else { |
276 | | /* if the user has configured who they are, |
277 | | send that as the password */ |
278 | 0 | if (FG(from_address)) { |
279 | 0 | php_stream_printf(stream, "PASS %s\r\n", ZSTR_VAL(FG(from_address))); |
280 | 0 | } else { |
281 | 0 | php_stream_write_string(stream, "PASS anonymous\r\n"); |
282 | 0 | } |
283 | 0 | } |
284 | | |
285 | | /* read the response */ |
286 | 0 | result = GET_FTP_RESULT(stream); |
287 | |
|
288 | 0 | if (result > 299 || result < 200) { |
289 | 0 | php_stream_notify_error(context, PHP_STREAM_NOTIFY_AUTH_RESULT, tmp_line, result); |
290 | 0 | } else { |
291 | 0 | php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_RESULT, tmp_line, result); |
292 | 0 | } |
293 | 0 | } |
294 | 0 | if (result > 299 || result < 200) { |
295 | 0 | goto connect_errexit; |
296 | 0 | } |
297 | | |
298 | 0 | if (puse_ssl) { |
299 | 0 | *puse_ssl = use_ssl; |
300 | 0 | } |
301 | 0 | if (puse_ssl_on_data) { |
302 | 0 | *puse_ssl_on_data = use_ssl_on_data; |
303 | 0 | } |
304 | 0 | if (preuseid) { |
305 | 0 | *preuseid = reuseid; |
306 | 0 | } |
307 | 0 | if (presource) { |
308 | 0 | *presource = resource; |
309 | 0 | } |
310 | |
|
311 | 0 | return stream; |
312 | | |
313 | 0 | connect_errexit: |
314 | 0 | php_uri_struct_free(resource); |
315 | |
|
316 | 0 | if (stream) { |
317 | 0 | php_stream_close(stream); |
318 | 0 | } |
319 | |
|
320 | 0 | return NULL; |
321 | 0 | } |
322 | | /* }}} */ |
323 | | |
324 | | /* {{{ php_fopen_do_pasv */ |
325 | | static unsigned short php_fopen_do_pasv(php_stream *stream, char *ip, size_t ip_size, char **phoststart) |
326 | 0 | { |
327 | 0 | char tmp_line[512]; |
328 | 0 | int result, i; |
329 | 0 | unsigned short portno; |
330 | 0 | char *tpath, *ttpath, *hoststart=NULL; |
331 | |
|
332 | 0 | #ifdef HAVE_IPV6 |
333 | | /* We try EPSV first, needed for IPv6 and works on some IPv4 servers */ |
334 | 0 | php_stream_write_string(stream, "EPSV\r\n"); |
335 | 0 | result = GET_FTP_RESULT(stream); |
336 | | |
337 | | /* check if we got a 229 response */ |
338 | 0 | if (result != 229) { |
339 | 0 | #endif |
340 | | /* EPSV failed, let's try PASV */ |
341 | 0 | php_stream_write_string(stream, "PASV\r\n"); |
342 | 0 | result = GET_FTP_RESULT(stream); |
343 | | |
344 | | /* make sure we got a 227 response */ |
345 | 0 | if (result != 227) { |
346 | 0 | return 0; |
347 | 0 | } |
348 | | |
349 | | /* parse pasv command (129, 80, 95, 25, 13, 221) */ |
350 | 0 | tpath = tmp_line; |
351 | | /* skip over the "227 Some message " part */ |
352 | 0 | for (tpath += 4; *tpath && !isdigit((unsigned char)*tpath); tpath++); |
353 | 0 | if (!*tpath) { |
354 | 0 | return 0; |
355 | 0 | } |
356 | | /* skip over the host ip, to get the port */ |
357 | 0 | hoststart = tpath; |
358 | 0 | for (i = 0; i < 4; i++) { |
359 | 0 | for (; isdigit((unsigned char)*tpath); tpath++); |
360 | 0 | if (*tpath != ',') { |
361 | 0 | return 0; |
362 | 0 | } |
363 | 0 | *tpath='.'; |
364 | 0 | tpath++; |
365 | 0 | } |
366 | 0 | tpath[-1] = '\0'; |
367 | 0 | memcpy(ip, hoststart, ip_size); |
368 | 0 | ip[ip_size-1] = '\0'; |
369 | 0 | hoststart = ip; |
370 | | |
371 | | /* pull out the MSB of the port */ |
372 | 0 | portno = (unsigned short) strtoul(tpath, &ttpath, 10) * 256; |
373 | 0 | if (ttpath == NULL) { |
374 | | /* didn't get correct response from PASV */ |
375 | 0 | return 0; |
376 | 0 | } |
377 | 0 | tpath = ttpath; |
378 | 0 | if (*tpath != ',') { |
379 | 0 | return 0; |
380 | 0 | } |
381 | 0 | tpath++; |
382 | | /* pull out the LSB of the port */ |
383 | 0 | portno += (unsigned short) strtoul(tpath, &ttpath, 10); |
384 | 0 | #ifdef HAVE_IPV6 |
385 | 0 | } else { |
386 | | /* parse epsv command (|||6446|) */ |
387 | 0 | for (i = 0, tpath = tmp_line + 4; *tpath; tpath++) { |
388 | 0 | if (*tpath == '|') { |
389 | 0 | i++; |
390 | 0 | if (i == 3) |
391 | 0 | break; |
392 | 0 | } |
393 | 0 | } |
394 | 0 | if (i < 3) { |
395 | 0 | return 0; |
396 | 0 | } |
397 | | /* pull out the port */ |
398 | 0 | portno = (unsigned short) strtoul(tpath + 1, &ttpath, 10); |
399 | 0 | } |
400 | 0 | #endif |
401 | 0 | if (ttpath == NULL) { |
402 | | /* didn't get correct response from EPSV/PASV */ |
403 | 0 | return 0; |
404 | 0 | } |
405 | | |
406 | 0 | if (phoststart) { |
407 | 0 | *phoststart = hoststart; |
408 | 0 | } |
409 | |
|
410 | 0 | return portno; |
411 | 0 | } |
412 | | /* }}} */ |
413 | | |
414 | | /* {{{ php_fopen_url_wrap_ftp */ |
415 | | php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *path, const char *mode, |
416 | | int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) |
417 | 0 | { |
418 | 0 | php_stream *stream = NULL, *datastream = NULL; |
419 | 0 | php_uri *resource = NULL; |
420 | 0 | char tmp_line[512]; |
421 | 0 | char ip[sizeof("123.123.123.123")]; |
422 | 0 | unsigned short portno; |
423 | 0 | char *hoststart = NULL; |
424 | 0 | int result = 0, use_ssl, use_ssl_on_data=0; |
425 | 0 | php_stream *reuseid=NULL; |
426 | 0 | size_t file_size = 0; |
427 | 0 | zval *tmpzval; |
428 | 0 | bool allow_overwrite = false; |
429 | 0 | int8_t read_write = 0; |
430 | 0 | zend_string *error_message = NULL; |
431 | |
|
432 | 0 | tmp_line[0] = '\0'; |
433 | |
|
434 | 0 | if (strpbrk(mode, "r+")) { |
435 | 0 | read_write = 1; /* Open for reading */ |
436 | 0 | } |
437 | 0 | if (strpbrk(mode, "wa+")) { |
438 | 0 | if (read_write) { |
439 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, ModeNotSupported, |
440 | 0 | "FTP does not support simultaneous read/write connections"); |
441 | 0 | return NULL; |
442 | 0 | } |
443 | 0 | if (strchr(mode, 'a')) { |
444 | 0 | read_write = 3; /* Open for Appending */ |
445 | 0 | } else { |
446 | 0 | read_write = 2; /* Open for writing */ |
447 | 0 | } |
448 | 0 | } |
449 | 0 | if (!read_write) { |
450 | | /* No mode specified? */ |
451 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, InvalidMode, |
452 | 0 | "Unknown file open mode"); |
453 | 0 | return NULL; |
454 | 0 | } |
455 | | |
456 | 0 | if (context && |
457 | 0 | (tmpzval = php_stream_context_get_option(context, "ftp", "proxy")) != NULL) { |
458 | 0 | if (read_write == 1) { |
459 | | /* Use http wrapper to proxy ftp request */ |
460 | 0 | return php_stream_url_wrap_http(wrapper, path, mode, options, opened_path, context STREAMS_CC); |
461 | 0 | } else { |
462 | | /* ftp proxy is read-only */ |
463 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, ModeNotSupported, |
464 | 0 | "FTP proxy may only be used in read mode"); |
465 | 0 | return NULL; |
466 | 0 | } |
467 | 0 | } |
468 | | |
469 | 0 | stream = php_ftp_fopen_connect(wrapper, path, mode, options, opened_path, context, &reuseid, &resource, &use_ssl, &use_ssl_on_data); |
470 | 0 | if (!stream) { |
471 | 0 | goto errexit; |
472 | 0 | } |
473 | | |
474 | | /* set the connection to be binary */ |
475 | 0 | php_stream_write_string(stream, "TYPE I\r\n"); |
476 | 0 | result = GET_FTP_RESULT(stream); |
477 | 0 | if (result > 299 || result < 200) |
478 | 0 | goto errexit; |
479 | | |
480 | | /* find out the size of the file (verifying it exists) */ |
481 | 0 | php_stream_printf(stream, "SIZE %s\r\n", ZSTR_VAL(resource->path)); |
482 | | |
483 | | /* read the response */ |
484 | 0 | result = GET_FTP_RESULT(stream); |
485 | 0 | if (read_write == 1) { |
486 | | /* Read Mode */ |
487 | 0 | char *sizestr; |
488 | | |
489 | | /* when reading file, it must exist */ |
490 | 0 | if (result > 299 || result < 200) { |
491 | 0 | errno = ENOENT; |
492 | 0 | goto errexit; |
493 | 0 | } |
494 | | |
495 | 0 | sizestr = strchr(tmp_line, ' '); |
496 | 0 | if (sizestr) { |
497 | 0 | sizestr++; |
498 | 0 | file_size = atoi(sizestr); |
499 | 0 | php_stream_notify_file_size(context, file_size, tmp_line, result); |
500 | 0 | } |
501 | 0 | } else if (read_write == 2) { |
502 | | /* when writing file (but not appending), it must NOT exist, unless a context option exists which allows it */ |
503 | 0 | if (context && (tmpzval = php_stream_context_get_option(context, "ftp", "overwrite")) != NULL) { |
504 | 0 | allow_overwrite = zend_is_true(tmpzval); |
505 | 0 | } |
506 | 0 | if (result <= 299 && result >= 200) { |
507 | 0 | if (allow_overwrite) { |
508 | | /* Context permits overwriting file, |
509 | | so we just delete whatever's there in preparation */ |
510 | 0 | php_stream_printf(stream, "DELE %s\r\n", ZSTR_VAL(resource->path)); |
511 | 0 | result = GET_FTP_RESULT(stream); |
512 | 0 | if (result >= 300 || result <= 199) { |
513 | 0 | goto errexit; |
514 | 0 | } |
515 | 0 | } else { |
516 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, AlreadyExists, |
517 | 0 | "Remote file already exists and overwrite context option not specified"); |
518 | 0 | errno = EEXIST; |
519 | 0 | goto errexit; |
520 | 0 | } |
521 | 0 | } |
522 | 0 | } |
523 | | |
524 | | /* set up the passive connection */ |
525 | 0 | portno = php_fopen_do_pasv(stream, ip, sizeof(ip), &hoststart); |
526 | |
|
527 | 0 | if (!portno) { |
528 | 0 | goto errexit; |
529 | 0 | } |
530 | | |
531 | | /* Send RETR/STOR command */ |
532 | 0 | if (read_write == 1) { |
533 | | /* set resume position if applicable */ |
534 | 0 | if (context && |
535 | 0 | (tmpzval = php_stream_context_get_option(context, "ftp", "resume_pos")) != NULL && |
536 | 0 | Z_TYPE_P(tmpzval) == IS_LONG && |
537 | 0 | Z_LVAL_P(tmpzval) > 0) { |
538 | 0 | php_stream_printf(stream, "REST " ZEND_LONG_FMT "\r\n", Z_LVAL_P(tmpzval)); |
539 | 0 | result = GET_FTP_RESULT(stream); |
540 | 0 | if (result < 300 || result > 399) { |
541 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, ResumptionFailed, |
542 | 0 | "Unable to resume from offset " ZEND_LONG_FMT, Z_LVAL_P(tmpzval)); |
543 | 0 | goto errexit; |
544 | 0 | } |
545 | 0 | } |
546 | | |
547 | | /* retrieve file */ |
548 | 0 | memcpy(tmp_line, "RETR", sizeof("RETR")); |
549 | 0 | } else if (read_write == 2) { |
550 | | /* Write new file */ |
551 | 0 | memcpy(tmp_line, "STOR", sizeof("STOR")); |
552 | 0 | } else { |
553 | | /* Append */ |
554 | 0 | memcpy(tmp_line, "APPE", sizeof("APPE")); |
555 | 0 | } |
556 | 0 | php_stream_printf(stream, "%s %s\r\n", tmp_line, (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); |
557 | | |
558 | | /* open the data channel */ |
559 | 0 | if (hoststart == NULL) { |
560 | 0 | hoststart = ZSTR_VAL(resource->host); |
561 | 0 | } |
562 | |
|
563 | 0 | char *transport; |
564 | 0 | size_t transport_len = spprintf(&transport, 0, "tcp://%s:%d", hoststart, portno); |
565 | 0 | datastream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, &error_message, NULL); |
566 | 0 | efree(transport); |
567 | 0 | if (datastream == NULL) { |
568 | 0 | tmp_line[0]='\0'; |
569 | 0 | goto errexit; |
570 | 0 | } |
571 | | |
572 | 0 | result = GET_FTP_RESULT(stream); |
573 | 0 | if (result != 150 && result != 125) { |
574 | | /* Could not retrieve or send the file |
575 | | * this data will only be sent to us after connection on the data port was initiated. |
576 | | */ |
577 | 0 | php_stream_close(datastream); |
578 | 0 | datastream = NULL; |
579 | 0 | goto errexit; |
580 | 0 | } |
581 | | |
582 | 0 | php_stream_context_set(datastream, context); |
583 | 0 | php_stream_notify_progress_init(context, 0, file_size); |
584 | |
|
585 | 0 | if (use_ssl_on_data && (php_stream_xport_crypto_setup(datastream, |
586 | 0 | STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL) < 0 || |
587 | 0 | php_stream_xport_crypto_enable(datastream, 1) < 0)) { |
588 | |
|
589 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, SslNotSupported, |
590 | 0 | "Unable to activate SSL mode"); |
591 | 0 | php_stream_close(datastream); |
592 | 0 | datastream = NULL; |
593 | 0 | tmp_line[0]='\0'; |
594 | 0 | goto errexit; |
595 | 0 | } |
596 | | |
597 | | /* remember control stream */ |
598 | 0 | datastream->wrapperthis = stream; |
599 | |
|
600 | 0 | php_uri_struct_free(resource); |
601 | 0 | return datastream; |
602 | | |
603 | 0 | errexit: |
604 | 0 | if (resource) { |
605 | 0 | php_uri_struct_free(resource); |
606 | 0 | } |
607 | 0 | if (stream) { |
608 | 0 | php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result); |
609 | 0 | php_stream_close(stream); |
610 | 0 | } |
611 | 0 | if (tmp_line[0] != '\0') |
612 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, ProtocolError, |
613 | 0 | "FTP server reports %s", tmp_line); |
614 | |
|
615 | 0 | if (error_message) { |
616 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, NetworkSendFailed, |
617 | 0 | "Failed to set up data channel: %s", ZSTR_VAL(error_message)); |
618 | 0 | zend_string_release(error_message); |
619 | 0 | } |
620 | 0 | return NULL; |
621 | 0 | } |
622 | | /* }}} */ |
623 | | |
624 | | /* {{{ php_ftp_dirsteam_read */ |
625 | | static ssize_t php_ftp_dirstream_read(php_stream *stream, char *buf, size_t count) |
626 | 0 | { |
627 | 0 | php_stream_dirent *ent = (php_stream_dirent *)buf; |
628 | 0 | php_stream *innerstream; |
629 | 0 | size_t tmp_len; |
630 | 0 | zend_string *basename; |
631 | |
|
632 | 0 | innerstream = ((php_ftp_dirstream_data *)stream->abstract)->datastream; |
633 | |
|
634 | 0 | if (count != sizeof(php_stream_dirent)) { |
635 | 0 | return -1; |
636 | 0 | } |
637 | | |
638 | 0 | if (php_stream_eof(innerstream)) { |
639 | 0 | return 0; |
640 | 0 | } |
641 | | |
642 | 0 | if (!php_stream_get_line(innerstream, ent->d_name, sizeof(ent->d_name), &tmp_len)) { |
643 | 0 | return -1; |
644 | 0 | } |
645 | | |
646 | 0 | basename = php_basename(ent->d_name, tmp_len, NULL, 0); |
647 | |
|
648 | 0 | tmp_len = MIN(sizeof(ent->d_name) - 1, ZSTR_LEN(basename)); |
649 | 0 | memcpy(ent->d_name, ZSTR_VAL(basename), tmp_len); |
650 | 0 | ent->d_name[tmp_len] = '\0'; |
651 | 0 | zend_string_release_ex(basename, 0); |
652 | 0 | ent->d_type = DT_UNKNOWN; |
653 | | |
654 | | /* Trim off trailing whitespace characters */ |
655 | 0 | while (tmp_len > 0 && |
656 | 0 | (ent->d_name[tmp_len - 1] == '\n' || ent->d_name[tmp_len - 1] == '\r' || |
657 | 0 | ent->d_name[tmp_len - 1] == '\t' || ent->d_name[tmp_len - 1] == ' ')) { |
658 | 0 | ent->d_name[--tmp_len] = '\0'; |
659 | 0 | } |
660 | |
|
661 | 0 | return sizeof(php_stream_dirent); |
662 | 0 | } |
663 | | /* }}} */ |
664 | | |
665 | | /* {{{ php_ftp_dirstream_close */ |
666 | | static int php_ftp_dirstream_close(php_stream *stream, int close_handle) |
667 | 0 | { |
668 | 0 | php_ftp_dirstream_data *data = stream->abstract; |
669 | | |
670 | | /* close control connection */ |
671 | 0 | if (data->controlstream) { |
672 | 0 | php_stream_close(data->controlstream); |
673 | 0 | data->controlstream = NULL; |
674 | 0 | } |
675 | | /* close data connection */ |
676 | 0 | php_stream_close(data->datastream); |
677 | 0 | data->datastream = NULL; |
678 | |
|
679 | 0 | efree(data); |
680 | 0 | stream->abstract = NULL; |
681 | |
|
682 | 0 | return 0; |
683 | 0 | } |
684 | | /* }}} */ |
685 | | |
686 | | /* ftp dirstreams only need to support read and close operations, |
687 | | They can't be rewound because the underlying ftp stream can't be rewound. */ |
688 | | static const php_stream_ops php_ftp_dirstream_ops = { |
689 | | NULL, /* write */ |
690 | | php_ftp_dirstream_read, /* read */ |
691 | | php_ftp_dirstream_close, /* close */ |
692 | | NULL, /* flush */ |
693 | | "ftpdir", |
694 | | NULL, /* rewind */ |
695 | | NULL, /* cast */ |
696 | | NULL, /* stat */ |
697 | | NULL /* set option */ |
698 | | }; |
699 | | |
700 | | /* {{{ php_stream_ftp_opendir */ |
701 | | static php_stream * php_stream_ftp_opendir(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, |
702 | | zend_string **opened_path, php_stream_context *context STREAMS_DC) |
703 | 0 | { |
704 | 0 | php_stream *stream, *reuseid, *datastream = NULL; |
705 | 0 | php_ftp_dirstream_data *dirsdata; |
706 | 0 | php_uri *resource = NULL; |
707 | 0 | int result = 0, use_ssl, use_ssl_on_data = 0; |
708 | 0 | char *hoststart = NULL, tmp_line[512]; |
709 | 0 | char ip[sizeof("123.123.123.123")]; |
710 | 0 | unsigned short portno; |
711 | |
|
712 | 0 | tmp_line[0] = '\0'; |
713 | |
|
714 | 0 | stream = php_ftp_fopen_connect(wrapper, path, mode, options, opened_path, context, &reuseid, &resource, &use_ssl, &use_ssl_on_data); |
715 | 0 | if (!stream) { |
716 | 0 | goto opendir_errexit; |
717 | 0 | } |
718 | | |
719 | | /* set the connection to be ascii */ |
720 | 0 | php_stream_write_string(stream, "TYPE A\r\n"); |
721 | 0 | result = GET_FTP_RESULT(stream); |
722 | 0 | if (result > 299 || result < 200) |
723 | 0 | goto opendir_errexit; |
724 | | |
725 | | // tmp_line isn't relevant after the php_fopen_do_pasv(). |
726 | 0 | tmp_line[0] = '\0'; |
727 | | |
728 | | /* set up the passive connection */ |
729 | 0 | portno = php_fopen_do_pasv(stream, ip, sizeof(ip), &hoststart); |
730 | |
|
731 | 0 | if (!portno) { |
732 | 0 | goto opendir_errexit; |
733 | 0 | } |
734 | | |
735 | | /* open the data channel */ |
736 | 0 | if (hoststart == NULL) { |
737 | 0 | hoststart = ZSTR_VAL(resource->host); |
738 | 0 | } |
739 | |
|
740 | 0 | datastream = php_stream_sock_open_host(hoststart, portno, SOCK_STREAM, 0, 0); |
741 | 0 | if (datastream == NULL) { |
742 | 0 | goto opendir_errexit; |
743 | 0 | } |
744 | | |
745 | 0 | php_stream_printf(stream, "NLST %s\r\n", (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); |
746 | |
|
747 | 0 | result = GET_FTP_RESULT(stream); |
748 | 0 | if (result != 150 && result != 125) { |
749 | | /* Could not retrieve or send the file |
750 | | * this data will only be sent to us after connection on the data port was initiated. |
751 | | */ |
752 | 0 | php_stream_close(datastream); |
753 | 0 | datastream = NULL; |
754 | 0 | goto opendir_errexit; |
755 | 0 | } |
756 | | |
757 | 0 | php_stream_context_set(datastream, context); |
758 | 0 | if (use_ssl_on_data && (php_stream_xport_crypto_setup(datastream, |
759 | 0 | STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL) < 0 || |
760 | 0 | php_stream_xport_crypto_enable(datastream, 1) < 0)) { |
761 | |
|
762 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, SslNotSupported, |
763 | 0 | "Unable to activate SSL mode"); |
764 | 0 | php_stream_close(datastream); |
765 | 0 | datastream = NULL; |
766 | 0 | goto opendir_errexit; |
767 | 0 | } |
768 | | |
769 | 0 | php_uri_struct_free(resource); |
770 | |
|
771 | 0 | dirsdata = emalloc(sizeof *dirsdata); |
772 | 0 | dirsdata->datastream = datastream; |
773 | 0 | dirsdata->controlstream = stream; |
774 | 0 | dirsdata->dirstream = php_stream_alloc(&php_ftp_dirstream_ops, dirsdata, 0, mode); |
775 | |
|
776 | 0 | return dirsdata->dirstream; |
777 | | |
778 | 0 | opendir_errexit: |
779 | 0 | if (resource) { |
780 | 0 | php_uri_struct_free(resource); |
781 | 0 | } |
782 | 0 | if (stream) { |
783 | 0 | php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result); |
784 | 0 | php_stream_close(stream); |
785 | 0 | } |
786 | 0 | if (tmp_line[0] != '\0') { |
787 | 0 | php_stream_wrapper_log_warn(wrapper, context, options, ProtocolError, |
788 | 0 | "FTP server reports %s", tmp_line); |
789 | 0 | } |
790 | 0 | return NULL; |
791 | 0 | } |
792 | | /* }}} */ |
793 | | |
794 | | /* {{{ php_stream_ftp_url_stat */ |
795 | | static int php_stream_ftp_url_stat(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context) |
796 | 0 | { |
797 | 0 | php_stream *stream = NULL; |
798 | 0 | php_uri *resource = NULL; |
799 | 0 | int result; |
800 | 0 | char tmp_line[512]; |
801 | | |
802 | | /* If ssb is NULL then someone is misbehaving */ |
803 | 0 | if (!ssb) return -1; |
804 | | |
805 | 0 | stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL); |
806 | 0 | if (!stream) { |
807 | 0 | goto stat_errexit; |
808 | 0 | } |
809 | | |
810 | 0 | ssb->sb.st_mode = 0644; /* FTP won't give us a valid mode, so approximate one based on being readable */ |
811 | 0 | php_stream_printf(stream, "CWD %s\r\n", (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); /* If we can CWD to it, it's a directory (maybe a link, but we can't tell) */ |
812 | 0 | result = GET_FTP_RESULT(stream); |
813 | 0 | if (result < 200 || result > 299) { |
814 | 0 | ssb->sb.st_mode |= S_IFREG; |
815 | 0 | } else { |
816 | 0 | ssb->sb.st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; |
817 | 0 | } |
818 | |
|
819 | 0 | php_stream_write_string(stream, "TYPE I\r\n"); /* we need this since some servers refuse to accept SIZE command in ASCII mode */ |
820 | |
|
821 | 0 | result = GET_FTP_RESULT(stream); |
822 | |
|
823 | 0 | if(result < 200 || result > 299) { |
824 | 0 | goto stat_errexit; |
825 | 0 | } |
826 | | |
827 | 0 | php_stream_printf(stream, "SIZE %s\r\n", (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); |
828 | 0 | result = GET_FTP_RESULT(stream); |
829 | 0 | if (result < 200 || result > 299) { |
830 | | /* Failure either means it doesn't exist |
831 | | or it's a directory and this server |
832 | | fails on listing directory sizes */ |
833 | 0 | if (ssb->sb.st_mode & S_IFDIR) { |
834 | 0 | ssb->sb.st_size = 0; |
835 | 0 | } else { |
836 | 0 | goto stat_errexit; |
837 | 0 | } |
838 | 0 | } else { |
839 | 0 | ssb->sb.st_size = atoi(tmp_line + 4); |
840 | 0 | } |
841 | | |
842 | 0 | php_stream_printf(stream, "MDTM %s\r\n", (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); |
843 | 0 | result = GET_FTP_RESULT(stream); |
844 | 0 | if (result == 213) { |
845 | 0 | char *p = tmp_line + 4; |
846 | 0 | int n; |
847 | 0 | struct tm tm, tmbuf, *gmt; |
848 | 0 | time_t stamp; |
849 | |
|
850 | 0 | while ((size_t)(p - tmp_line) < sizeof(tmp_line) && !isdigit((unsigned char)*p)) { |
851 | 0 | p++; |
852 | 0 | } |
853 | |
|
854 | 0 | if ((size_t)(p - tmp_line) > sizeof(tmp_line)) { |
855 | 0 | goto mdtm_error; |
856 | 0 | } |
857 | | |
858 | 0 | n = sscanf(p, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec); |
859 | 0 | if (n != 6) { |
860 | 0 | goto mdtm_error; |
861 | 0 | } |
862 | | |
863 | 0 | tm.tm_year -= 1900; |
864 | 0 | tm.tm_mon--; |
865 | 0 | tm.tm_isdst = -1; |
866 | | |
867 | | /* figure out the GMT offset */ |
868 | 0 | stamp = time(NULL); |
869 | 0 | gmt = php_gmtime_r(&stamp, &tmbuf); |
870 | 0 | if (!gmt) { |
871 | 0 | goto mdtm_error; |
872 | 0 | } |
873 | 0 | gmt->tm_isdst = -1; |
874 | | |
875 | | /* apply the GMT offset */ |
876 | 0 | tm.tm_sec += (long)(stamp - mktime(gmt)); |
877 | 0 | tm.tm_isdst = gmt->tm_isdst; |
878 | |
|
879 | 0 | ssb->sb.st_mtime = mktime(&tm); |
880 | 0 | } else { |
881 | | /* error or unsupported command */ |
882 | 0 | mdtm_error: |
883 | 0 | ssb->sb.st_mtime = -1; |
884 | 0 | } |
885 | | |
886 | 0 | ssb->sb.st_ino = 0; /* Unknown values */ |
887 | 0 | ssb->sb.st_dev = 0; |
888 | 0 | ssb->sb.st_uid = 0; |
889 | 0 | ssb->sb.st_gid = 0; |
890 | 0 | ssb->sb.st_atime = -1; |
891 | 0 | ssb->sb.st_ctime = -1; |
892 | |
|
893 | 0 | ssb->sb.st_nlink = 1; |
894 | 0 | ssb->sb.st_rdev = -1; |
895 | 0 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
896 | 0 | ssb->sb.st_blksize = 4096; /* Guess since FTP won't expose this information */ |
897 | 0 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
898 | 0 | ssb->sb.st_blocks = (int)((4095 + ssb->sb.st_size) / ssb->sb.st_blksize); /* emulate ceil */ |
899 | 0 | #endif |
900 | 0 | #endif |
901 | 0 | php_stream_close(stream); |
902 | 0 | php_uri_struct_free(resource); |
903 | 0 | return 0; |
904 | | |
905 | 0 | stat_errexit: |
906 | 0 | if (resource) { |
907 | 0 | php_uri_struct_free(resource); |
908 | 0 | } |
909 | 0 | if (stream) { |
910 | 0 | php_stream_close(stream); |
911 | 0 | } |
912 | 0 | return -1; |
913 | 0 | } |
914 | | /* }}} */ |
915 | | |
916 | | /* {{{ php_stream_ftp_unlink */ |
917 | | static int php_stream_ftp_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) |
918 | 0 | { |
919 | 0 | php_stream *stream = NULL; |
920 | 0 | php_uri *resource = NULL; |
921 | 0 | int result; |
922 | 0 | char tmp_line[512]; |
923 | |
|
924 | 0 | stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL); |
925 | 0 | if (!stream) { |
926 | 0 | php_stream_wrapper_warn(wrapper, context, options, AuthFailed, |
927 | 0 | "Unable to connect to %s", url); |
928 | 0 | goto unlink_errexit; |
929 | 0 | } |
930 | | |
931 | 0 | if (resource->path == NULL) { |
932 | 0 | php_stream_wrapper_warn(wrapper, context, options, InvalidPath, |
933 | 0 | "Invalid path provided in %s", url); |
934 | 0 | goto unlink_errexit; |
935 | 0 | } |
936 | | |
937 | | /* Attempt to delete the file */ |
938 | 0 | php_stream_printf(stream, "DELE %s\r\n", ZSTR_VAL(resource->path)); |
939 | |
|
940 | 0 | result = GET_FTP_RESULT(stream); |
941 | 0 | if (result < 200 || result > 299) { |
942 | 0 | php_stream_wrapper_warn(wrapper, context, options, UnlinkFailed, |
943 | 0 | "Error Deleting file: %s", tmp_line); |
944 | 0 | goto unlink_errexit; |
945 | 0 | } |
946 | | |
947 | 0 | php_uri_struct_free(resource); |
948 | 0 | php_stream_close(stream); |
949 | 0 | return 1; |
950 | | |
951 | 0 | unlink_errexit: |
952 | 0 | if (resource) { |
953 | 0 | php_uri_struct_free(resource); |
954 | 0 | } |
955 | 0 | if (stream) { |
956 | 0 | php_stream_close(stream); |
957 | 0 | } |
958 | 0 | return 0; |
959 | 0 | } |
960 | | /* }}} */ |
961 | | |
962 | | /* {{{ php_stream_ftp_rename */ |
963 | | static int php_stream_ftp_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context) |
964 | 0 | { |
965 | 0 | php_stream *stream = NULL; |
966 | 0 | php_uri *resource_from = NULL, *resource_to = NULL; |
967 | 0 | int result; |
968 | 0 | char tmp_line[512]; |
969 | |
|
970 | 0 | const php_uri_parser *uri_parser = php_stream_context_get_uri_parser("ftp", context); |
971 | 0 | if (uri_parser == NULL) { |
972 | 0 | zend_value_error("%s(): Provided stream context has invalid value for the \"uri_parser_class\" option", get_active_function_name()); |
973 | 0 | return 0; |
974 | 0 | } |
975 | | |
976 | 0 | resource_from = php_uri_parse_to_struct(uri_parser, url_from, strlen(url_from), PHP_URI_COMPONENT_READ_MODE_RAW, true); |
977 | 0 | if (!resource_from) { |
978 | 0 | return 0; |
979 | 0 | } |
980 | | |
981 | 0 | resource_to = php_uri_parse_to_struct(uri_parser, url_to, strlen(url_to), PHP_URI_COMPONENT_READ_MODE_RAW, true); |
982 | 0 | if (!resource_to) { |
983 | 0 | goto rename_errexit; |
984 | 0 | } |
985 | | |
986 | | /* Must be same scheme (ftp/ftp or ftps/ftps), same host, and same port |
987 | | (or a 21/0 0/21 combination which is also "same") |
988 | | Also require paths to/from */ |
989 | 0 | if (!resource_from->scheme || |
990 | 0 | !resource_to->scheme || |
991 | 0 | !zend_string_equals(resource_from->scheme, resource_to->scheme) || |
992 | 0 | !resource_from->host || |
993 | 0 | !resource_to->host || |
994 | 0 | !zend_string_equals(resource_from->host, resource_to->host) || |
995 | 0 | (resource_from->port != resource_to->port && |
996 | 0 | resource_from->port * resource_to->port != 0 && |
997 | 0 | resource_from->port + resource_to->port != 21) || |
998 | 0 | !resource_from->path || |
999 | 0 | !resource_to->path) { |
1000 | 0 | goto rename_errexit; |
1001 | 0 | } |
1002 | | |
1003 | 0 | stream = php_ftp_fopen_connect(wrapper, url_from, "r", 0, NULL, context, NULL, NULL, NULL, NULL); |
1004 | 0 | if (!stream) { |
1005 | 0 | php_stream_wrapper_warn(wrapper, context, options, AuthFailed, |
1006 | 0 | "Unable to connect to %s", ZSTR_VAL(resource_from->host)); |
1007 | 0 | goto rename_errexit; |
1008 | 0 | } |
1009 | | |
1010 | | /* Rename FROM */ |
1011 | 0 | php_stream_printf(stream, "RNFR %s\r\n", ZSTR_VAL(resource_from->path)); |
1012 | |
|
1013 | 0 | result = GET_FTP_RESULT(stream); |
1014 | 0 | if (result < 300 || result > 399) { |
1015 | 0 | php_stream_wrapper_warn(wrapper, context, options, RenameFailed, |
1016 | 0 | "Error Renaming file: %s", tmp_line); |
1017 | 0 | goto rename_errexit; |
1018 | 0 | } |
1019 | | |
1020 | | /* Rename TO */ |
1021 | 0 | php_stream_printf(stream, "RNTO %s\r\n", ZSTR_VAL(resource_to->path)); |
1022 | |
|
1023 | 0 | result = GET_FTP_RESULT(stream); |
1024 | 0 | if (result < 200 || result > 299) { |
1025 | 0 | php_stream_wrapper_warn(wrapper, context, options, RenameFailed, |
1026 | 0 | "Error Renaming file: %s", tmp_line); |
1027 | 0 | goto rename_errexit; |
1028 | 0 | } |
1029 | | |
1030 | 0 | php_uri_struct_free(resource_from); |
1031 | 0 | php_uri_struct_free(resource_to); |
1032 | 0 | php_stream_close(stream); |
1033 | 0 | return 1; |
1034 | | |
1035 | 0 | rename_errexit: |
1036 | 0 | php_uri_struct_free(resource_from); |
1037 | 0 | if (resource_to) { |
1038 | 0 | php_uri_struct_free(resource_to); |
1039 | 0 | } |
1040 | 0 | if (stream) { |
1041 | 0 | php_stream_close(stream); |
1042 | 0 | } |
1043 | 0 | return 0; |
1044 | 0 | } |
1045 | | /* }}} */ |
1046 | | |
1047 | | /* {{{ php_stream_ftp_mkdir */ |
1048 | | static int php_stream_ftp_mkdir(php_stream_wrapper *wrapper, const char *url, int mode, int options, php_stream_context *context) |
1049 | 0 | { |
1050 | 0 | php_stream *stream = NULL; |
1051 | 0 | php_uri *resource = NULL; |
1052 | 0 | int result, recursive = options & PHP_STREAM_MKDIR_RECURSIVE; |
1053 | 0 | char tmp_line[512]; |
1054 | |
|
1055 | 0 | stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL); |
1056 | 0 | if (!stream) { |
1057 | 0 | php_stream_wrapper_warn(wrapper, context, options, AuthFailed, |
1058 | 0 | "Unable to connect to %s", url); |
1059 | 0 | goto mkdir_errexit; |
1060 | 0 | } |
1061 | | |
1062 | 0 | if (resource->path == NULL) { |
1063 | 0 | php_stream_wrapper_warn(wrapper, context, options, InvalidPath, |
1064 | 0 | "Invalid path provided in %s", url); |
1065 | 0 | goto mkdir_errexit; |
1066 | 0 | } |
1067 | | |
1068 | 0 | if (!recursive) { |
1069 | 0 | php_stream_printf(stream, "MKD %s\r\n", ZSTR_VAL(resource->path)); |
1070 | 0 | result = GET_FTP_RESULT(stream); |
1071 | 0 | } else { |
1072 | | /* we look for directory separator from the end of string, thus hopefully reducing our work load */ |
1073 | 0 | char *p, *e, *buf; |
1074 | |
|
1075 | 0 | buf = estrndup(ZSTR_VAL(resource->path), ZSTR_LEN(resource->path)); |
1076 | 0 | e = buf + ZSTR_LEN(resource->path); |
1077 | | |
1078 | | /* find a top level directory we need to create */ |
1079 | 0 | while ((p = strrchr(buf, '/'))) { |
1080 | 0 | *p = '\0'; |
1081 | 0 | php_stream_printf(stream, "CWD %s\r\n", strlen(buf) ? buf : "/"); |
1082 | 0 | result = GET_FTP_RESULT(stream); |
1083 | 0 | if (result >= 200 && result <= 299) { |
1084 | 0 | *p = '/'; |
1085 | 0 | break; |
1086 | 0 | } |
1087 | 0 | } |
1088 | |
|
1089 | 0 | php_stream_printf(stream, "MKD %s\r\n", strlen(buf) ? buf : "/"); |
1090 | 0 | result = GET_FTP_RESULT(stream); |
1091 | |
|
1092 | 0 | if (result >= 200 && result <= 299) { |
1093 | 0 | if (!p) { |
1094 | 0 | p = buf; |
1095 | 0 | } |
1096 | | /* create any needed directories if the creation of the 1st directory worked */ |
1097 | 0 | while (p != e) { |
1098 | 0 | if (*p == '\0' && *(p + 1) != '\0') { |
1099 | 0 | *p = '/'; |
1100 | 0 | php_stream_printf(stream, "MKD %s\r\n", buf); |
1101 | 0 | result = GET_FTP_RESULT(stream); |
1102 | 0 | if (result < 200 || result > 299) { |
1103 | 0 | php_stream_wrapper_warn(wrapper, context, options, MkdirFailed, |
1104 | 0 | "%s", tmp_line); |
1105 | 0 | break; |
1106 | 0 | } |
1107 | 0 | } |
1108 | 0 | ++p; |
1109 | 0 | } |
1110 | 0 | } |
1111 | |
|
1112 | 0 | efree(buf); |
1113 | 0 | } |
1114 | |
|
1115 | 0 | php_uri_struct_free(resource); |
1116 | 0 | php_stream_close(stream); |
1117 | |
|
1118 | 0 | if (result < 200 || result > 299) { |
1119 | | /* Failure */ |
1120 | 0 | return 0; |
1121 | 0 | } |
1122 | | |
1123 | 0 | return 1; |
1124 | | |
1125 | 0 | mkdir_errexit: |
1126 | 0 | if (resource) { |
1127 | 0 | php_uri_struct_free(resource); |
1128 | 0 | } |
1129 | 0 | if (stream) { |
1130 | 0 | php_stream_close(stream); |
1131 | 0 | } |
1132 | 0 | return 0; |
1133 | 0 | } |
1134 | | /* }}} */ |
1135 | | |
1136 | | /* {{{ php_stream_ftp_rmdir */ |
1137 | | static int php_stream_ftp_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) |
1138 | 0 | { |
1139 | 0 | php_stream *stream = NULL; |
1140 | 0 | php_uri *resource = NULL; |
1141 | 0 | int result; |
1142 | 0 | char tmp_line[512]; |
1143 | |
|
1144 | 0 | stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL); |
1145 | 0 | if (!stream) { |
1146 | 0 | php_stream_wrapper_warn(wrapper, context, options, AuthFailed, |
1147 | 0 | "Unable to connect to %s", url); |
1148 | 0 | goto rmdir_errexit; |
1149 | 0 | } |
1150 | | |
1151 | 0 | if (resource->path == NULL) { |
1152 | 0 | php_stream_wrapper_warn(wrapper, context, options, InvalidPath, |
1153 | 0 | "Invalid path provided in %s", url); |
1154 | 0 | goto rmdir_errexit; |
1155 | 0 | } |
1156 | | |
1157 | 0 | php_stream_printf(stream, "RMD %s\r\n", ZSTR_VAL(resource->path)); |
1158 | 0 | result = GET_FTP_RESULT(stream); |
1159 | |
|
1160 | 0 | if (result < 200 || result > 299) { |
1161 | 0 | php_stream_wrapper_warn(wrapper, context, options, RmdirFailed, |
1162 | 0 | "%s", tmp_line); |
1163 | 0 | goto rmdir_errexit; |
1164 | 0 | } |
1165 | | |
1166 | 0 | php_uri_struct_free(resource); |
1167 | 0 | php_stream_close(stream); |
1168 | |
|
1169 | 0 | return 1; |
1170 | | |
1171 | 0 | rmdir_errexit: |
1172 | 0 | if (resource) { |
1173 | 0 | php_uri_struct_free(resource); |
1174 | 0 | } |
1175 | 0 | if (stream) { |
1176 | 0 | php_stream_close(stream); |
1177 | 0 | } |
1178 | 0 | return 0; |
1179 | 0 | } |
1180 | | /* }}} */ |
1181 | | |
1182 | | static const php_stream_wrapper_ops ftp_stream_wops = { |
1183 | | php_stream_url_wrap_ftp, |
1184 | | php_stream_ftp_stream_close, /* stream_close */ |
1185 | | php_stream_ftp_stream_stat, |
1186 | | php_stream_ftp_url_stat, /* stat_url */ |
1187 | | php_stream_ftp_opendir, /* opendir */ |
1188 | | "ftp", |
1189 | | php_stream_ftp_unlink, /* unlink */ |
1190 | | php_stream_ftp_rename, /* rename */ |
1191 | | php_stream_ftp_mkdir, /* mkdir */ |
1192 | | php_stream_ftp_rmdir, /* rmdir */ |
1193 | | NULL |
1194 | | }; |
1195 | | |
1196 | | PHPAPI const php_stream_wrapper php_stream_ftp_wrapper = { |
1197 | | &ftp_stream_wops, |
1198 | | NULL, |
1199 | | 1 /* is_url */ |
1200 | | }; |