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