Coverage Report

Created: 2026-07-25 06:39

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