Coverage Report

Created: 2026-02-26 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/ftp.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
#include "urldata.h"
26
27
#ifndef CURL_DISABLE_FTP
28
29
#ifdef HAVE_NETINET_IN_H
30
#include <netinet/in.h>
31
#endif
32
#ifdef HAVE_ARPA_INET_H
33
#include <arpa/inet.h>
34
#endif
35
#ifdef HAVE_NETDB_H
36
#include <netdb.h>
37
#endif
38
#ifdef __VMS
39
#include <in.h>
40
#include <inet.h>
41
#endif
42
43
#include "sendf.h"
44
#include "curl_addrinfo.h"
45
#include "curl_trc.h"
46
#include "if2ip.h"
47
#include "hostip.h"
48
#include "progress.h"
49
#include "transfer.h"
50
#include "escape.h"
51
#include "ftp.h"
52
#include "ftplistparser.h"
53
#include "curl_range.h"
54
#include "strcase.h"
55
#include "vtls/vtls.h"
56
#include "cfilters.h"
57
#include "cf-socket.h"
58
#include "connect.h"
59
#include "curlx/inet_ntop.h"
60
#include "curlx/inet_pton.h"
61
#include "select.h"
62
#include "parsedate.h" /* for the week day and month names */
63
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
64
#include "multiif.h"
65
#include "url.h"
66
#include "http_proxy.h"
67
#include "curlx/strdup.h"
68
#include "curlx/strerr.h"
69
#include "curlx/strparse.h"
70
71
#ifndef NI_MAXHOST
72
#define NI_MAXHOST 1025
73
#endif
74
#ifndef INET_ADDRSTRLEN
75
#define INET_ADDRSTRLEN 16
76
#endif
77
78
/* macro to check for a three-digit ftp status code at the start of the
79
   given string */
80
#define STATUSCODE(line) \
81
61.4k
  (ISDIGIT((line)[0]) && ISDIGIT((line)[1]) && ISDIGIT((line)[2]))
82
83
/* macro to check for the last line in an FTP server response */
84
615k
#define LASTLINE(line) (STATUSCODE(line) && (' ' == (line)[3]))
85
86
#ifdef CURLVERBOSE
87
/* for tracing purposes */
88
static const char * const ftp_state_names[] = {
89
  "STOP",
90
  "WAIT220",
91
  "AUTH",
92
  "USER",
93
  "PASS",
94
  "ACCT",
95
  "PBSZ",
96
  "PROT",
97
  "CCC",
98
  "PWD",
99
  "SYST",
100
  "NAMEFMT",
101
  "QUOTE",
102
  "RETR_PREQUOTE",
103
  "STOR_PREQUOTE",
104
  "LIST_PREQUOTE",
105
  "POSTQUOTE",
106
  "CWD",
107
  "MKD",
108
  "MDTM",
109
  "TYPE",
110
  "LIST_TYPE",
111
  "RETR_LIST_TYPE",
112
  "RETR_TYPE",
113
  "STOR_TYPE",
114
  "SIZE",
115
  "RETR_SIZE",
116
  "STOR_SIZE",
117
  "REST",
118
  "RETR_REST",
119
  "PORT",
120
  "PRET",
121
  "PASV",
122
  "LIST",
123
  "RETR",
124
  "STOR",
125
  "QUIT"
126
};
127
#define FTP_CSTATE(ftpc)   ((ftpc) ? ftp_state_names[(ftpc)->state] : "???")
128
129
#endif /* CURLVERBOSE */
130
131
/* This is the ONLY way to change FTP state! */
132
static void ftp_state_low(struct Curl_easy *data,
133
                          struct ftp_conn *ftpc,
134
                          ftpstate newstate
135
#ifdef DEBUGBUILD
136
                          , int lineno
137
#endif
138
  )
139
12.5k
{
140
12.5k
  if(ftpc->state != newstate) {
141
11.9k
#ifdef DEBUGBUILD
142
11.9k
    NOVERBOSE((void)lineno);
143
11.9k
    CURL_TRC_FTP(data, "[%s] -> [%s] (line %d)", FTP_CSTATE(ftpc),
144
11.9k
                 ftp_state_names[newstate], lineno);
145
#else
146
    CURL_TRC_FTP(data, "[%s] -> [%s]", FTP_CSTATE(ftpc),
147
                 ftp_state_names[newstate]);
148
#endif
149
11.9k
  }
150
12.5k
  ftpc->state = newstate;
151
12.5k
}
152
153
/* Local API functions */
154
#ifndef DEBUGBUILD
155
#define ftp_state(x, y, z) ftp_state_low(x, y, z)
156
#else /* !DEBUGBUILD */
157
12.5k
#define ftp_state(x, y, z) ftp_state_low(x, y, z, __LINE__)
158
#endif /* DEBUGBUILD */
159
160
static CURLcode ftp_state_mdtm(struct Curl_easy *data,
161
                               struct ftp_conn *ftpc,
162
                               struct FTP *ftp);
163
static CURLcode ftp_state_quote(struct Curl_easy *data,
164
                                struct ftp_conn *ftpc,
165
                                struct FTP *ftp,
166
                                bool init, ftpstate instate);
167
static CURLcode ftp_nb_type(struct Curl_easy *data,
168
                            struct ftp_conn *ftpc,
169
                            struct FTP *ftp,
170
                            bool ascii, ftpstate newstate);
171
static CURLcode getftpresponse(struct Curl_easy *data, size_t *nreadp,
172
                               int *ftpcodep);
173
174
static void freedirs(struct ftp_conn *ftpc)
175
4.37k
{
176
4.37k
  Curl_safefree(ftpc->dirs);
177
4.37k
  ftpc->dirdepth = 0;
178
4.37k
  Curl_safefree(ftpc->rawpath);
179
4.37k
  ftpc->file = NULL;
180
4.37k
}
181
182
static size_t numof_slashes(const char *str)
183
1.48k
{
184
1.48k
  const char *slashPos;
185
1.48k
  size_t num = 0;
186
2.08k
  do {
187
2.08k
    slashPos = strchr(str, '/');
188
2.08k
    if(slashPos) {
189
597
      ++num;
190
597
      str = slashPos + 1;
191
597
    }
192
2.08k
  } while(slashPos);
193
1.48k
  return num;
194
1.48k
}
195
196
1.48k
#define FTP_MAX_DIR_DEPTH 1000
197
198
/***********************************************************************
199
 *
200
 * ftp_parse_url_path()
201
 *
202
 * Parse the URL path into separate path components.
203
 *
204
 */
205
static CURLcode ftp_parse_url_path(struct Curl_easy *data,
206
                                   struct ftp_conn *ftpc,
207
                                   struct FTP *ftp)
208
1.49k
{
209
1.49k
  const char *slashPos = NULL;
210
1.49k
  const char *fileName = NULL;
211
1.49k
  CURLcode result = CURLE_OK;
212
1.49k
  const char *rawPath = NULL; /* url-decoded "raw" path */
213
1.49k
  size_t pathLen = 0;
214
215
1.49k
  ftpc->ctl_valid = FALSE;
216
1.49k
  ftpc->cwdfail = FALSE;
217
218
1.49k
  if(ftpc->rawpath)
219
8
    freedirs(ftpc);
220
  /* url-decode ftp path before further evaluation */
221
1.49k
  result = Curl_urldecode(ftp->path, 0, &ftpc->rawpath, &pathLen, REJECT_CTRL);
222
1.49k
  if(result) {
223
1
    failf(data, "path contains control characters");
224
1
    return result;
225
1
  }
226
1.49k
  rawPath = ftpc->rawpath;
227
228
1.49k
  switch(data->set.ftp_filemethod) {
229
6
  case FTPFILE_NOCWD: /* fastest, but less standard-compliant */
230
231
6
    if((pathLen > 0) && (rawPath[pathLen - 1] != '/'))
232
3
      fileName = rawPath;  /* this is a full file path */
233
    /*
234
      else: ftpc->file is not used anywhere other than for operations on
235
            a file. In other words, never for directory operations.
236
            So we can safely leave filename as NULL here and use it as a
237
            argument in dir/file decisions.
238
    */
239
6
    break;
240
241
3
  case FTPFILE_SINGLECWD:
242
3
    slashPos = strrchr(rawPath, '/');
243
3
    if(slashPos) {
244
      /* get path before last slash, except for / */
245
2
      size_t dirlen = slashPos - rawPath;
246
2
      if(dirlen == 0)
247
1
        dirlen = 1;
248
249
2
      ftpc->dirs = curlx_calloc(1, sizeof(ftpc->dirs[0]));
250
2
      if(!ftpc->dirs)
251
0
        return CURLE_OUT_OF_MEMORY;
252
253
2
      ftpc->dirs[0].start = 0;
254
2
      ftpc->dirs[0].len = (int)dirlen;
255
2
      ftpc->dirdepth = 1; /* we consider it to be a single directory */
256
2
      fileName = slashPos + 1; /* rest is filename */
257
2
    }
258
1
    else
259
1
      fileName = rawPath; /* filename only (or empty) */
260
3
    break;
261
262
3
  default: /* allow pretty much anything */
263
1.48k
  case FTPFILE_MULTICWD: {
264
    /* current position: begin of next path component */
265
1.48k
    const char *curPos = rawPath;
266
267
    /* number of entries to allocate for the 'dirs' array */
268
1.48k
    size_t dirAlloc = numof_slashes(rawPath);
269
270
1.48k
    if(dirAlloc >= FTP_MAX_DIR_DEPTH)
271
      /* suspiciously deep directory hierarchy */
272
0
      return CURLE_URL_MALFORMAT;
273
274
1.48k
    if(dirAlloc) {
275
141
      ftpc->dirs = curlx_calloc(dirAlloc, sizeof(ftpc->dirs[0]));
276
141
      if(!ftpc->dirs)
277
0
        return CURLE_OUT_OF_MEMORY;
278
279
      /* parse the URL path into separate path components */
280
738
      while(dirAlloc--) {
281
597
        const char *spos = strchr(curPos, '/');
282
597
        size_t clen = spos - curPos;
283
284
        /* path starts with a slash: add that as a directory */
285
597
        if(!clen && (ftpc->dirdepth == 0))
286
30
          ++clen;
287
288
        /* we skip empty path components, like "x//y" since the FTP command
289
           CWD requires a parameter and a non-existent parameter a) does not
290
           work on many servers and b) has no effect on the others. */
291
597
        if(clen) {
292
285
          ftpc->dirs[ftpc->dirdepth].start = (int)(curPos - rawPath);
293
285
          ftpc->dirs[ftpc->dirdepth].len = (int)clen;
294
285
          ftpc->dirdepth++;
295
285
        }
296
597
        curPos = spos + 1;
297
597
      }
298
141
    }
299
1.48k
    fileName = curPos; /* the rest is the filename (or empty) */
300
1.48k
  }
301
0
    break;
302
1.49k
  } /* switch */
303
304
1.49k
  if(fileName && *fileName)
305
769
    ftpc->file = fileName;
306
725
  else
307
725
    ftpc->file = NULL; /* instead of point to a zero byte,
308
                            we make it a NULL pointer */
309
310
1.49k
  if(data->state.upload && !ftpc->file && (ftp->transfer == PPTRANSFER_BODY)) {
311
    /* We need a filename when uploading. Return error! */
312
2
    failf(data, "Uploading to a URL without a filename");
313
2
    return CURLE_URL_MALFORMAT;
314
2
  }
315
316
1.49k
  ftpc->cwddone = FALSE; /* default to not done */
317
318
1.49k
  if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/'))
319
2
    ftpc->cwddone = TRUE; /* skip CWD for absolute paths */
320
1.49k
  else { /* newly created FTP connections are already in entry path */
321
1.49k
    const char *oldPath = data->conn->bits.reuse ? ftpc->prevpath : "";
322
1.49k
    if(oldPath) {
323
1.49k
      size_t n = pathLen;
324
1.49k
      if(data->set.ftp_filemethod == FTPFILE_NOCWD)
325
4
        n = 0; /* CWD to entry for relative paths */
326
1.48k
      else
327
1.48k
        n -= ftpc->file ? strlen(ftpc->file) : 0;
328
329
1.49k
      if((strlen(oldPath) == n) && rawPath && !strncmp(rawPath, oldPath, n)) {
330
1.35k
        infof(data, "Request has same path as previous transfer");
331
1.35k
        ftpc->cwddone = TRUE;
332
1.35k
      }
333
1.49k
    }
334
1.49k
  }
335
336
1.49k
  return CURLE_OK;
337
1.49k
}
338
339
/***********************************************************************
340
 *
341
 * ftp_need_type()
342
 *
343
 * Returns TRUE if we in the current situation should send TYPE
344
 */
345
static int ftp_need_type(struct ftp_conn *ftpc,
346
                         bool ascii_wanted)
347
39
{
348
39
  return ftpc->transfertype != (ascii_wanted ? 'A' : 'I');
349
39
}
350
351
static void close_secondarysocket(struct Curl_easy *data,
352
                                  struct ftp_conn *ftpc)
353
1.68k
{
354
1.68k
  (void)ftpc;
355
1.68k
  CURL_TRC_FTP(data, "[%s] closing DATA connection", FTP_CSTATE(ftpc));
356
1.68k
  Curl_conn_close(data, SECONDARYSOCKET);
357
1.68k
  Curl_conn_cf_discard_all(data, data->conn, SECONDARYSOCKET);
358
1.68k
}
359
360
#ifdef CURL_PREFER_LF_LINEENDS
361
/*
362
 * Lineend Conversions
363
 * On ASCII transfers, e.g. directory listings, we might get lines
364
 * ending in '\r\n' and we prefer just '\n'.
365
 * We might also get a lonely '\r' which we convert into a '\n'.
366
 */
367
struct ftp_cw_lc_ctx {
368
  struct Curl_cwriter super;
369
  bool newline_pending;
370
};
371
372
static CURLcode ftp_cw_lc_write(struct Curl_easy *data,
373
                                struct Curl_cwriter *writer, int type,
374
                                const char *buf, size_t blen)
375
476k
{
376
476k
  static const char nl = '\n';
377
476k
  struct ftp_cw_lc_ctx *ctx = writer->ctx;
378
476k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
379
380
476k
  if(!ftpc)
381
0
    return CURLE_FAILED_INIT;
382
383
476k
  if(!(type & CLIENTWRITE_BODY) || ftpc->transfertype != 'A')
384
475k
    return Curl_cwriter_write(data, writer->next, type, buf, blen);
385
386
  /* ASCII mode BODY data, convert lineends */
387
267k
  while(blen) {
388
    /* do not pass EOS when writing parts */
389
266k
    int chunk_type = (type & ~CLIENTWRITE_EOS);
390
266k
    const char *cp;
391
266k
    size_t chunk_len;
392
266k
    CURLcode result;
393
394
266k
    if(ctx->newline_pending) {
395
266k
      if(buf[0] != '\n') {
396
        /* previous chunk ended in '\r' and we do not see a '\n' in this one,
397
         * need to write a newline. */
398
265k
        result = Curl_cwriter_write(data, writer->next, chunk_type, &nl, 1);
399
265k
        if(result)
400
0
          return result;
401
265k
      }
402
      /* either we just wrote the newline or it is part of the next
403
       * chunk of bytes we write. */
404
266k
      ctx->newline_pending = FALSE;
405
266k
    }
406
407
266k
    cp = memchr(buf, '\r', blen);
408
266k
    if(!cp)
409
506
      break;
410
411
    /* write the bytes before the '\r', excluding the '\r' */
412
266k
    chunk_len = cp - buf;
413
266k
    if(chunk_len) {
414
29.9k
      result = Curl_cwriter_write(data, writer->next, chunk_type,
415
29.9k
                                  buf, chunk_len);
416
29.9k
      if(result)
417
0
        return result;
418
29.9k
    }
419
    /* skip the '\r', we now have a newline pending */
420
266k
    buf = cp + 1;
421
266k
    blen = blen - chunk_len - 1;
422
266k
    ctx->newline_pending = TRUE;
423
266k
  }
424
425
  /* Any remaining data does not contain a '\r' */
426
1.14k
  if(blen) {
427
506
    DEBUGASSERT(!ctx->newline_pending);
428
506
    return Curl_cwriter_write(data, writer->next, type, buf, blen);
429
506
  }
430
637
  else if(type & CLIENTWRITE_EOS) {
431
    /* EndOfStream, if we have a trailing cr, now is the time to write it */
432
417
    if(ctx->newline_pending) {
433
55
      ctx->newline_pending = FALSE;
434
55
      return Curl_cwriter_write(data, writer->next, type, &nl, 1);
435
55
    }
436
    /* Always pass on the EOS type indicator */
437
362
    return Curl_cwriter_write(data, writer->next, type, buf, 0);
438
417
  }
439
220
  return CURLE_OK;
440
1.14k
}
441
442
static const struct Curl_cwtype ftp_cw_lc = {
443
  "ftp-lineconv",
444
  NULL,
445
  Curl_cwriter_def_init,
446
  ftp_cw_lc_write,
447
  Curl_cwriter_def_close,
448
  sizeof(struct ftp_cw_lc_ctx)
449
};
450
451
#endif /* CURL_PREFER_LF_LINEENDS */
452
453
/***********************************************************************
454
 *
455
 * ftp_check_ctrl_on_data_wait()
456
 *
457
 */
458
static CURLcode ftp_check_ctrl_on_data_wait(struct Curl_easy *data,
459
                                            struct ftp_conn *ftpc)
460
0
{
461
0
  struct connectdata *conn = data->conn;
462
0
  curl_socket_t ctrl_sock = conn->sock[FIRSTSOCKET];
463
0
  struct pingpong *pp = &ftpc->pp;
464
0
  size_t nread;
465
0
  int ftpcode;
466
0
  bool response = FALSE;
467
468
  /* First check whether there is a cached response from server */
469
0
  if(curlx_dyn_len(&pp->recvbuf)) {
470
0
    const char *l = curlx_dyn_ptr(&pp->recvbuf);
471
0
    if(!ISDIGIT(*l) || (*l > '3')) {
472
      /* Data connection could not be established, let's return */
473
0
      infof(data, "There is negative response in cache while serv connect");
474
0
      (void)getftpresponse(data, &nread, &ftpcode);
475
0
      return CURLE_FTP_ACCEPT_FAILED;
476
0
    }
477
0
  }
478
479
0
  if(pp->overflow)
480
    /* there is pending control data still in the buffer to read */
481
0
    response = TRUE;
482
0
  else {
483
0
    int socketstate = SOCKET_READABLE(ctrl_sock, 0);
484
    /* see if the connection request is already here */
485
0
    switch(socketstate) {
486
0
    case -1: /* error */
487
      /* let's die here */
488
0
      failf(data, "Error while waiting for server connect");
489
0
      return CURLE_FTP_ACCEPT_FAILED;
490
0
    default:
491
0
      if(socketstate & CURL_CSELECT_IN)
492
0
        response = TRUE;
493
0
      break;
494
0
    }
495
0
  }
496
497
0
  if(response) {
498
0
    infof(data, "Ctrl conn has data while waiting for data conn");
499
0
    if(pp->overflow > 3) {
500
0
      const char *r = curlx_dyn_ptr(&pp->recvbuf);
501
0
      size_t len = curlx_dyn_len(&pp->recvbuf);
502
503
0
      DEBUGASSERT((pp->overflow + pp->nfinal) <= curlx_dyn_len(&pp->recvbuf));
504
      /* move over the most recently handled response line */
505
0
      r += pp->nfinal;
506
0
      len -= pp->nfinal;
507
508
0
      if((len > 3) && LASTLINE(r)) {
509
0
        curl_off_t status;
510
0
        if(!curlx_str_number(&r, &status, 999) && (status == 226)) {
511
          /* funny timing situation where we get the final message on the
512
             control connection before traffic on the data connection has been
513
             noticed. Leave the 226 in there and use this as a trigger to read
514
             the data socket. */
515
0
          infof(data, "Got 226 before data activity");
516
0
          return CURLE_OK;
517
0
        }
518
0
      }
519
0
    }
520
521
0
    (void)getftpresponse(data, &nread, &ftpcode);
522
523
0
    infof(data, "FTP code: %03d", ftpcode);
524
525
0
    if(ftpcode / 100 > 3)
526
0
      return CURLE_FTP_ACCEPT_FAILED;
527
528
0
    return CURLE_WEIRD_SERVER_REPLY;
529
0
  }
530
531
0
  return CURLE_OK;
532
0
}
533
534
/***********************************************************************
535
 *
536
 * ftp_initiate_transfer()
537
 *
538
 * After connection from server is accepted this function is called to
539
 * setup transfer parameters and initiate the data transfer.
540
 *
541
 */
542
static CURLcode ftp_initiate_transfer(struct Curl_easy *data,
543
                                      struct ftp_conn *ftpc)
544
627
{
545
627
  CURLcode result = CURLE_OK;
546
627
  bool connected;
547
548
627
  CURL_TRC_FTP(data, "ftp_initiate_transfer()");
549
627
  result = Curl_conn_connect(data, SECONDARYSOCKET, TRUE, &connected);
550
627
  if(result || !connected)
551
0
    return result;
552
553
627
  if(data->state.upload) {
554
    /* When we know we are uploading a specified file, we can get the file
555
       size prior to the actual upload. */
556
23
    Curl_pgrsSetUploadSize(data, data->state.infilesize);
557
558
    /* FTP upload, shutdown DATA, ignore shutdown errors, as we rely
559
     * on the server response on the CONTROL connection. */
560
23
    Curl_xfer_setup_send(data, SECONDARYSOCKET);
561
23
    Curl_xfer_set_shutdown(data, TRUE, TRUE);
562
23
  }
563
604
  else {
564
    /* FTP download, shutdown, do not ignore errors */
565
604
    Curl_xfer_setup_recv(data, SECONDARYSOCKET, data->req.size);
566
604
    Curl_xfer_set_shutdown(data, TRUE, FALSE);
567
604
  }
568
569
627
  ftpc->pp.pending_resp = TRUE; /* expect server response */
570
627
  ftp_state(data, ftpc, FTP_STOP);
571
572
627
  return CURLE_OK;
573
627
}
574
575
static bool ftp_endofresp(struct Curl_easy *data, struct connectdata *conn,
576
                          const char *line, size_t len, int *code)
577
584k
{
578
584k
  curl_off_t status;
579
584k
  (void)data;
580
584k
  (void)conn;
581
582
584k
  if((len > 3) && LASTLINE(line) && !curlx_str_number(&line, &status, 999)) {
583
13.6k
    *code = (int)status;
584
13.6k
    return TRUE;
585
13.6k
  }
586
587
570k
  return FALSE;
588
584k
}
589
590
static CURLcode ftp_readresp(struct Curl_easy *data,
591
                             struct ftp_conn *ftpc,
592
                             int sockindex,
593
                             struct pingpong *pp,
594
                             int *ftpcodep, /* return the ftp-code if done */
595
                             size_t *size) /* size of the response */
596
16.2k
{
597
16.2k
  int code;
598
16.2k
  CURLcode result = Curl_pp_readresp(data, sockindex, pp, &code, size);
599
16.2k
  DEBUGASSERT(ftpcodep);
600
601
  /* store the latest code for later retrieval, except during shutdown */
602
16.2k
  if(!ftpc->shutdown)
603
16.2k
    data->info.httpcode = code;
604
605
16.2k
  *ftpcodep = code;
606
607
16.2k
  if(code == 421) {
608
    /* 421 means "Service not available, closing control connection." and FTP
609
     * servers use it to signal that idle session timeout has been exceeded.
610
     * If we ignored the response, it could end up hanging in some cases.
611
     *
612
     * This response code can come at any point so having it treated
613
     * generically is a good idea.
614
     */
615
3
    infof(data, "We got a 421 - timeout");
616
3
    ftp_state(data, ftpc, FTP_STOP);
617
3
    return CURLE_OPERATION_TIMEDOUT;
618
3
  }
619
620
16.2k
  return result;
621
16.2k
}
622
623
/* --- parse FTP server responses --- */
624
625
/*
626
 * getftpresponse() is a BLOCKING function to read the full response from a
627
 * server after a command.
628
 *
629
 */
630
static CURLcode getftpresponse(struct Curl_easy *data,
631
                               size_t *nreadp, /* return number of bytes
632
                                                  read */
633
                               int *ftpcodep) /* return the ftp-code */
634
604
{
635
  /*
636
   * We cannot read just one byte per read() and then go back to select() as
637
   * the OpenSSL read() does not grok that properly.
638
   *
639
   * Alas, read as much as possible, split up into lines, use the ending
640
   * line in a response or continue reading. */
641
642
604
  struct connectdata *conn = data->conn;
643
604
  curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
644
604
  CURLcode result = CURLE_OK;
645
604
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
646
604
  struct pingpong *pp = &ftpc->pp;
647
604
  size_t nread;
648
604
  int cache_skip = 0;
649
604
  DEBUGASSERT(ftpcodep);
650
651
604
  CURL_TRC_FTP(data, "getftpresponse start");
652
604
  *nreadp = 0;
653
604
  *ftpcodep = 0; /* 0 for errors */
654
655
604
  if(!ftpc)
656
0
    return CURLE_FAILED_INIT;
657
658
3.28k
  while(!*ftpcodep && !result) {
659
    /* check and reset timeout value every lap */
660
3.18k
    timediff_t timeout = Curl_pp_state_timeout(data, pp);
661
3.18k
    timediff_t interval_ms;
662
663
3.18k
    if(timeout <= 0) {
664
6
      failf(data, "FTP response timeout");
665
6
      return CURLE_OPERATION_TIMEDOUT; /* already too little time */
666
6
    }
667
668
3.18k
    interval_ms = 1000;  /* use 1 second timeout intervals */
669
3.18k
    if(timeout < interval_ms)
670
3.17k
      interval_ms = timeout;
671
672
    /*
673
     * Since this function is blocking, we need to wait here for input on the
674
     * connection and only then we call the response reading function. We do
675
     * timeout at least every second to make the timeout check run.
676
     *
677
     * A caution here is that the ftp_readresp() function has a cache that may
678
     * contain pieces of a response from the previous invoke and we need to
679
     * make sure we do not just wait for input while there is unhandled data in
680
     * that cache. But also, if the cache is there, we call ftp_readresp() and
681
     * the cache was not good enough to continue we must not just busy-loop
682
     * around this function.
683
     *
684
     */
685
686
3.18k
    if(curlx_dyn_len(&pp->recvbuf) && (cache_skip < 2)) {
687
      /*
688
       * There is a cache left since before. We then skipping the wait for
689
       * socket action, unless this is the same cache like the previous round
690
       * as then the cache was deemed not enough to act on and we then need to
691
       * wait for more data anyway.
692
       */
693
1.38k
    }
694
1.79k
    else if(!Curl_conn_data_pending(data, FIRSTSOCKET)) {
695
1.79k
      curl_socket_t wsock = Curl_pp_needs_flush(data, pp) ?
696
1.79k
        sockfd : CURL_SOCKET_BAD;
697
1.79k
      int ev = Curl_socket_check(sockfd, CURL_SOCKET_BAD, wsock, interval_ms);
698
1.79k
      if(ev < 0) {
699
0
        failf(data, "FTP response aborted due to select/poll error: %d",
700
0
              SOCKERRNO);
701
0
        return CURLE_RECV_ERROR;
702
0
      }
703
1.79k
      else if(ev == 0) {
704
7
        result = Curl_pgrsUpdate(data);
705
7
        continue; /* just continue in our loop for the timeout duration */
706
7
      }
707
1.79k
    }
708
709
3.17k
    if(Curl_pp_needs_flush(data, pp)) {
710
0
      result = Curl_pp_flushsend(data, pp);
711
0
      if(result)
712
0
        break;
713
0
    }
714
715
3.17k
    result = ftp_readresp(data, ftpc, FIRSTSOCKET, pp, ftpcodep, &nread);
716
3.17k
    if(result)
717
505
      break;
718
719
2.66k
    if(!nread && curlx_dyn_len(&pp->recvbuf))
720
      /* bump cache skip counter as on repeated skips we must wait for more
721
         data */
722
2.30k
      cache_skip++;
723
361
    else
724
      /* when we got data or there is no cache left, we reset the cache skip
725
         counter */
726
361
      cache_skip = 0;
727
728
2.66k
    *nreadp += nread;
729
730
2.66k
  } /* while there is buffer left and loop is requested */
731
732
598
  pp->pending_resp = FALSE;
733
598
  CURL_TRC_FTP(data, "getftpresponse -> result=%d, nread=%zd, ftpcode=%d",
734
598
               result, *nreadp, *ftpcodep);
735
736
598
  return result;
737
604
}
738
739
static CURLcode ftp_state_user(struct Curl_easy *data,
740
                               struct ftp_conn *ftpc,
741
                               struct connectdata *conn)
742
119
{
743
119
  CURLcode result = Curl_pp_sendf(data, &ftpc->pp, "USER %s",
744
119
                                  conn->user ? conn->user : "");
745
119
  if(!result) {
746
119
    ftpc->ftp_trying_alternative = FALSE;
747
119
    ftp_state(data, ftpc, FTP_USER);
748
119
  }
749
119
  return result;
750
119
}
751
752
static CURLcode ftp_state_pwd(struct Curl_easy *data,
753
                              struct ftp_conn *ftpc)
754
1.52k
{
755
1.52k
  CURLcode result;
756
1.52k
#ifdef DEBUGBUILD
757
1.52k
  if(!data->id && getenv("CURL_FTP_PWD_STOP"))
758
0
    return CURLE_OK;
759
1.52k
#endif
760
1.52k
  result = Curl_pp_sendf(data, &ftpc->pp, "%s", "PWD");
761
1.52k
  if(!result)
762
1.52k
    ftp_state(data, ftpc, FTP_PWD);
763
764
1.52k
  return result;
765
1.52k
}
766
767
/* For the FTP "protocol connect" and "doing" phases only */
768
static CURLcode ftp_pollset(struct Curl_easy *data,
769
                            struct easy_pollset *ps)
770
4.64k
{
771
4.64k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
772
4.64k
  return ftpc ? Curl_pp_pollset(data, &ftpc->pp, ps) : CURLE_OK;
773
4.64k
}
774
775
/* For the FTP "DO_MORE" phase only */
776
static CURLcode ftp_domore_pollset(struct Curl_easy *data,
777
                                   struct easy_pollset *ps)
778
1.92k
{
779
1.92k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
780
781
1.92k
  if(!ftpc)
782
0
    return CURLE_OK;
783
784
  /* When in DO_MORE state, we could be either waiting for us to connect to a
785
   * remote site, or we could wait for that site to connect to us. Or just
786
   * handle ordinary commands.
787
   */
788
1.92k
  CURL_TRC_FTP(data, "[%s] ftp_domore_pollset()", FTP_CSTATE(ftpc));
789
790
1.92k
  if(FTP_STOP == ftpc->state) {
791
    /* if stopped and still in this state, then we are also waiting for a
792
       connect on the secondary connection */
793
2
    DEBUGASSERT(data->conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD ||
794
2
                (data->conn->cfilter[SECONDARYSOCKET] &&
795
2
                 !Curl_conn_is_connected(data->conn, SECONDARYSOCKET)));
796
    /* An unconnected SECONDARY will add its socket by itself
797
     * via its adjust_pollset() */
798
2
    return Curl_pollset_add_in(data, ps, data->conn->sock[FIRSTSOCKET]);
799
2
  }
800
1.92k
  return Curl_pp_pollset(data, &ftpc->pp, ps);
801
1.92k
}
802
803
static int pathlen(struct ftp_conn *ftpc, int num)
804
192
{
805
192
  DEBUGASSERT(ftpc->dirs);
806
192
  DEBUGASSERT(ftpc->dirdepth > num);
807
192
  return ftpc->dirs[num].len;
808
192
}
809
810
static const char *pathpiece(struct ftp_conn *ftpc, int num)
811
192
{
812
192
  DEBUGASSERT(ftpc->dirs);
813
192
  DEBUGASSERT(ftpc->dirdepth > num);
814
192
  return &ftpc->rawpath[ftpc->dirs[num].start];
815
192
}
816
817
/* This is called after the FTP_QUOTE state is passed.
818
819
   ftp_state_cwd() sends the range of CWD commands to the server to change to
820
   the correct directory. It may also need to send MKD commands to create
821
   missing ones, if that option is enabled.
822
*/
823
static CURLcode ftp_state_cwd(struct Curl_easy *data,
824
                              struct ftp_conn *ftpc,
825
                              struct FTP *ftp)
826
1.49k
{
827
1.49k
  CURLcode result = CURLE_OK;
828
829
1.49k
  if(ftpc->cwddone)
830
    /* already done and fine */
831
1.35k
    result = ftp_state_mdtm(data, ftpc, ftp);
832
138
  else {
833
    /* FTPFILE_NOCWD with full path: expect ftpc->cwddone! */
834
138
    DEBUGASSERT((data->set.ftp_filemethod != FTPFILE_NOCWD) ||
835
138
                !(ftpc->dirdepth && ftpc->rawpath[0] == '/'));
836
837
138
    ftpc->count2 = 0; /* count2 counts failed CWDs */
838
839
138
    if(data->conn->bits.reuse && ftpc->entrypath &&
840
       /* no need to go to entrypath when we have an absolute path */
841
0
       !(ftpc->dirdepth && ftpc->rawpath[0] == '/')) {
842
      /* This is a reused connection. Since we change directory to where the
843
         transfer is taking place, we must first get back to the original dir
844
         where we ended up after login: */
845
0
      ftpc->cwdcount = 0; /* we count this as the first path, then we add one
846
                             for all upcoming ones in the ftp->dirs[] array */
847
0
      result = Curl_pp_sendf(data, &ftpc->pp, "CWD %s", ftpc->entrypath);
848
0
      if(!result)
849
0
        ftp_state(data, ftpc, FTP_CWD);
850
0
    }
851
138
    else {
852
138
      if(ftpc->dirdepth) {
853
138
        ftpc->cwdcount = 1;
854
        /* issue the first CWD, the rest is sent when the CWD responses are
855
           received... */
856
138
        result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s",
857
138
                               pathlen(ftpc, 0), pathpiece(ftpc, 0));
858
138
        if(!result)
859
138
          ftp_state(data, ftpc, FTP_CWD);
860
138
      }
861
0
      else {
862
        /* No CWD necessary */
863
0
        result = ftp_state_mdtm(data, ftpc, ftp);
864
0
      }
865
138
    }
866
138
  }
867
1.49k
  return result;
868
1.49k
}
869
870
typedef enum {
871
  EPRT,
872
  PORT,
873
  DONE
874
} ftpport;
875
876
/*
877
 * Parse the CURLOPT_FTPPORT string
878
 * "(ipv4|ipv6|domain|interface)?(:port(-range)?)?"
879
 * and extract addr/addrlen and port_min/port_max.
880
 */
881
static CURLcode ftp_port_parse_string(struct Curl_easy *data,
882
                                      struct connectdata *conn,
883
                                      const char *string_ftpport,
884
                                      struct Curl_sockaddr_storage *ss,
885
                                      unsigned short *port_minp,
886
                                      unsigned short *port_maxp,
887
                                      const char **hostp,
888
                                      char *hbuf, size_t hbuflen)
889
30
{
890
30
  const char *ip_end = NULL;
891
30
  const char *addr = NULL;
892
30
  size_t addrlen = 0;
893
30
  unsigned short port_min = 0;
894
30
  unsigned short port_max = 0;
895
30
  char ipstr[50];
896
#ifndef USE_IPV6
897
  (void)conn;
898
  (void)ss;
899
#endif
900
901
  /* default to nothing */
902
30
  *hostp = NULL;
903
30
  *port_minp = *port_maxp = 0;
904
905
30
  if(!string_ftpport || (strlen(string_ftpport) <= 1))
906
1
    goto done;
907
908
29
#ifdef USE_IPV6
909
29
  if(*string_ftpport == '[') {
910
    /* [ipv6]:port(-range) */
911
13
    const char *ip_start = string_ftpport + 1;
912
13
    ip_end = strchr(ip_start, ']');
913
13
    if(ip_end) {
914
11
      addrlen = ip_end - ip_start;
915
11
      addr = ip_start;
916
11
    }
917
13
  }
918
16
  else
919
16
#endif
920
16
    if(*string_ftpport == ':') {
921
      /* :port */
922
2
      ip_end = string_ftpport;
923
2
    }
924
14
    else {
925
14
      ip_end = strchr(string_ftpport, ':');
926
14
      addr = string_ftpport;
927
14
      if(ip_end) {
928
7
#ifdef USE_IPV6
929
7
        struct sockaddr_in6 * const sa6 = (void *)ss;
930
7
#endif
931
        /* either ipv6 or (ipv4|domain|interface):port(-range) */
932
7
        addrlen = ip_end - string_ftpport;
933
7
#ifdef USE_IPV6
934
7
        if(curlx_inet_pton(AF_INET6, string_ftpport, &sa6->sin6_addr) == 1) {
935
          /* ipv6 */
936
1
          port_min = port_max = 0;
937
1
          addrlen = strlen(string_ftpport);
938
1
          ip_end = NULL; /* this got no port ! */
939
1
        }
940
7
#endif
941
7
      }
942
7
      else
943
        /* ipv4|interface */
944
7
        addrlen = strlen(string_ftpport);
945
14
    }
946
947
  /* parse the port */
948
29
  if(ip_end) {
949
19
    const char *portp = strchr(ip_end, ':');
950
19
    if(portp) {
951
8
      curl_off_t start;
952
8
      curl_off_t end;
953
8
      portp++;
954
8
      if(!curlx_str_number(&portp, &start, 0xffff)) {
955
4
        port_min = (unsigned short)start;
956
4
        if(!curlx_str_single(&portp, '-') &&
957
3
           !curlx_str_number(&portp, &end, 0xffff))
958
1
          port_max = (unsigned short)end;
959
3
        else
960
3
          port_max = port_min;
961
4
      }
962
4
      else
963
4
        port_max = port_min;
964
8
    }
965
19
  }
966
967
  /* correct errors like :1234-1230 or :-4711 */
968
29
  if(port_min > port_max)
969
1
    port_min = port_max = 0;
970
971
29
  if(addrlen) {
972
25
    const struct Curl_sockaddr_ex *remote_addr =
973
25
      Curl_conn_get_remote_addr(data, FIRSTSOCKET);
974
975
25
    DEBUGASSERT(remote_addr);
976
25
    DEBUGASSERT(addr);
977
25
    if(!remote_addr || (addrlen >= sizeof(ipstr)) || (addrlen >= hbuflen))
978
1
      return CURLE_FTP_PORT_FAILED;
979
24
    memcpy(ipstr, addr, addrlen);
980
24
    ipstr[addrlen] = 0;
981
982
    /* attempt to get the address of the given interface name */
983
24
    switch(Curl_if2ip(remote_addr->family,
984
24
#ifdef USE_IPV6
985
24
                      Curl_ipv6_scope(&remote_addr->curl_sa_addr),
986
24
                      conn->scope_id,
987
24
#endif
988
24
                      ipstr, hbuf, hbuflen)) {
989
24
    case IF2IP_NOT_FOUND:
990
      /* not an interface, use the string as hostname instead */
991
24
      memcpy(hbuf, addr, addrlen);
992
24
      hbuf[addrlen] = 0;
993
24
      *hostp = hbuf;
994
24
      break;
995
0
    case IF2IP_AF_NOT_SUPPORTED:
996
0
      return CURLE_FTP_PORT_FAILED;
997
0
    case IF2IP_FOUND:
998
0
      *hostp = hbuf; /* use the hbuf for hostname */
999
0
      break;
1000
24
    }
1001
24
  }
1002
  /* else: only a port(-range) given, leave host as NULL */
1003
1004
29
done:
1005
29
  *port_minp = port_min;
1006
29
  *port_maxp = port_max;
1007
29
  return CURLE_OK;
1008
29
}
1009
1010
/*
1011
 * If no host was derived from the FTPPORT string, fall back to the IP address
1012
 * of the control connection's local socket.
1013
 */
1014
static CURLcode ftp_port_default_host(struct Curl_easy *data,
1015
                                      struct connectdata *conn,
1016
                                      struct Curl_sockaddr_storage *ss,
1017
                                      curl_socklen_t *sslenp,
1018
                                      const char **hostp,
1019
                                      char *hbuf, size_t hbuflen,
1020
                                      bool *non_localp)
1021
5
{
1022
5
  struct sockaddr *sa = (struct sockaddr *)ss;
1023
5
  struct sockaddr_in * const sa4 = (void *)sa;
1024
5
#ifdef USE_IPV6
1025
5
  struct sockaddr_in6 * const sa6 = (void *)sa;
1026
5
#endif
1027
5
  char buffer[STRERROR_LEN];
1028
5
  const char *r;
1029
1030
5
  *sslenp = sizeof(*ss);
1031
5
  if(getsockname(conn->sock[FIRSTSOCKET], sa, sslenp)) {
1032
0
    failf(data, "getsockname() failed: %s",
1033
0
          curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1034
0
    return CURLE_FTP_PORT_FAILED;
1035
0
  }
1036
5
  switch(sa->sa_family) {
1037
0
#ifdef USE_IPV6
1038
0
  case AF_INET6:
1039
0
    r = curlx_inet_ntop(sa->sa_family, &sa6->sin6_addr, hbuf, hbuflen);
1040
0
    break;
1041
0
#endif
1042
5
  default:
1043
5
    r = curlx_inet_ntop(sa->sa_family, &sa4->sin_addr, hbuf, hbuflen);
1044
5
    break;
1045
5
  }
1046
5
  if(!r)
1047
5
    return CURLE_FTP_PORT_FAILED;
1048
1049
0
  *hostp = hbuf;
1050
0
  *non_localp = FALSE; /* we know it is local now */
1051
0
  return CURLE_OK;
1052
5
}
1053
1054
/*
1055
 * Resolve the host string to a list of addresses.
1056
 */
1057
static CURLcode ftp_port_resolve_host(struct Curl_easy *data,
1058
                                      struct connectdata *conn,
1059
                                      const char *host,
1060
                                      struct Curl_dns_entry **dns_entryp,
1061
                                      const struct Curl_addrinfo **resp)
1062
24
{
1063
24
  CURLcode result;
1064
1065
24
  *resp = NULL;
1066
24
  result = Curl_resolv_blocking(data, host, 0, conn->ip_version,
1067
24
                                dns_entryp);
1068
24
  if(result)
1069
8
    failf(data, "failed to resolve the address provided to PORT: %s", host);
1070
16
  else {
1071
16
    DEBUGASSERT(*dns_entryp);
1072
16
    *resp = (*dns_entryp)->addr;
1073
16
  }
1074
24
  return result;
1075
24
}
1076
1077
/*
1078
 * Open a TCP socket for the resolved address family.
1079
 */
1080
static CURLcode ftp_port_open_socket(struct Curl_easy *data,
1081
                                     struct connectdata *conn,
1082
                                     const struct Curl_addrinfo *res,
1083
                                     const struct Curl_addrinfo **aip,
1084
                                     curl_socket_t *portsockp)
1085
16
{
1086
16
  char buffer[STRERROR_LEN];
1087
16
  int error = 0;
1088
16
  const struct Curl_addrinfo *ai;
1089
16
  CURLcode result = CURLE_FTP_PORT_FAILED;
1090
1091
16
  for(ai = res; ai; ai = ai->ai_next) {
1092
16
    result =
1093
16
      Curl_socket_open(data, ai, NULL,
1094
16
                       Curl_conn_get_transport(data, conn), portsockp);
1095
16
    if(result) {
1096
0
      if(result == CURLE_OUT_OF_MEMORY)
1097
0
        return result;
1098
0
      result = CURLE_FTP_PORT_FAILED;
1099
0
      error = SOCKERRNO;
1100
0
      continue;
1101
0
    }
1102
16
    break;
1103
16
  }
1104
16
  if(!ai) {
1105
0
    failf(data, "socket failure: %s",
1106
0
          curlx_strerror(error, buffer, sizeof(buffer)));
1107
0
    return CURLE_FTP_PORT_FAILED;
1108
0
  }
1109
16
  *aip = ai;
1110
16
  return result;
1111
16
}
1112
1113
/*
1114
 * Bind the socket to a local address and port within the requested range.
1115
 * Falls back to the control-connection address if the user-requested address
1116
 * is non-local.
1117
 */
1118
static CURLcode ftp_port_bind_socket(struct Curl_easy *data,
1119
                                     struct connectdata *conn,
1120
                                     curl_socket_t portsock,
1121
                                     const struct Curl_addrinfo *ai,
1122
                                     struct Curl_sockaddr_storage *ss,
1123
                                     curl_socklen_t *sslen_io,
1124
                                     unsigned short port_min,
1125
                                     unsigned short port_max,
1126
                                     bool non_local)
1127
16
{
1128
16
  struct sockaddr *sa = (struct sockaddr *)ss;
1129
16
  struct sockaddr_in * const sa4 = (void *)sa;
1130
16
#ifdef USE_IPV6
1131
16
  struct sockaddr_in6 * const sa6 = (void *)sa;
1132
16
#endif
1133
16
  char buffer[STRERROR_LEN];
1134
16
  unsigned short port;
1135
16
  int error;
1136
1137
16
  memcpy(sa, ai->ai_addr, ai->ai_addrlen);
1138
16
  *sslen_io = ai->ai_addrlen;
1139
1140
16
  for(port = port_min; port <= port_max;) {
1141
16
    if(sa->sa_family == AF_INET)
1142
15
      sa4->sin_port = htons(port);
1143
1
#ifdef USE_IPV6
1144
1
    else
1145
1
      sa6->sin6_port = htons(port);
1146
16
#endif
1147
16
    if(bind(portsock, sa, *sslen_io)) {
1148
16
      error = SOCKERRNO;
1149
16
      if(non_local && (error == SOCKEADDRNOTAVAIL)) {
1150
        /* The requested bind address is not local. Use the address used for
1151
         * the control connection instead and restart the port loop.
1152
         */
1153
0
        infof(data, "bind(port=%hu) on non-local address failed: %s", port,
1154
0
              curlx_strerror(error, buffer, sizeof(buffer)));
1155
1156
0
        *sslen_io = sizeof(*ss);
1157
0
        if(getsockname(conn->sock[FIRSTSOCKET], sa, sslen_io)) {
1158
0
          failf(data, "getsockname() failed: %s",
1159
0
                curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1160
0
          return CURLE_FTP_PORT_FAILED;
1161
0
        }
1162
0
        port = port_min;
1163
0
        non_local = FALSE; /* do not try this again */
1164
0
        continue;
1165
0
      }
1166
16
      if(error != SOCKEADDRINUSE && error != SOCKEACCES) {
1167
16
        failf(data, "bind(port=%hu) failed: %s", port,
1168
16
              curlx_strerror(error, buffer, sizeof(buffer)));
1169
16
        return CURLE_FTP_PORT_FAILED;
1170
16
      }
1171
16
    }
1172
0
    else
1173
0
      break;
1174
1175
    /* check if port is the maximum value here, because it might be 0xffff
1176
       and then the increment below will wrap the 16-bit counter */
1177
0
    if(port == port_max) {
1178
0
      failf(data, "bind() failed, ran out of ports");
1179
0
      return CURLE_FTP_PORT_FAILED;
1180
0
    }
1181
0
    port++;
1182
0
  }
1183
1184
  /* re-read the name so we can extract the actual port chosen */
1185
0
  *sslen_io = sizeof(*ss);
1186
0
  if(getsockname(portsock, sa, sslen_io)) {
1187
0
    failf(data, "getsockname() failed: %s",
1188
0
          curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1189
0
    return CURLE_FTP_PORT_FAILED;
1190
0
  }
1191
0
  CURL_TRC_FTP(data, "ftp_port_bind_socket(), socket bound to port %d",
1192
0
               port);
1193
0
  return CURLE_OK;
1194
0
}
1195
1196
/*
1197
 * Start listening on the data socket.
1198
 */
1199
static CURLcode ftp_port_listen(struct Curl_easy *data, curl_socket_t portsock)
1200
0
{
1201
0
  char buffer[STRERROR_LEN];
1202
1203
0
  if(listen(portsock, 1)) {
1204
0
    failf(data, "socket failure: %s",
1205
0
          curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1206
0
    return CURLE_FTP_PORT_FAILED;
1207
0
  }
1208
0
  CURL_TRC_FTP(data, "ftp_port_listen(), listening on port");
1209
0
  return CURLE_OK;
1210
0
}
1211
1212
/*
1213
 * Send the EPRT or PORT command to the server.
1214
 */
1215
static CURLcode ftp_port_send_command(struct Curl_easy *data,
1216
                                      struct ftp_conn *ftpc,
1217
                                      struct connectdata *conn,
1218
                                      struct Curl_sockaddr_storage *ss,
1219
                                      const struct Curl_addrinfo *ai,
1220
                                      ftpport fcmd)
1221
0
{
1222
0
  static const char mode[][5] = { "EPRT", "PORT" };
1223
0
  struct sockaddr *sa = (struct sockaddr *)ss;
1224
0
  struct sockaddr_in * const sa4 = (void *)sa;
1225
0
#ifdef USE_IPV6
1226
0
  struct sockaddr_in6 * const sa6 = (void *)sa;
1227
0
#endif
1228
0
  char myhost[MAX_IPADR_LEN + 1] = "";
1229
0
  unsigned short port;
1230
0
  CURLcode result;
1231
1232
  /* Get a plain printable version of the numerical address to work with. This
1233
     logic uses the address provided by the FTPPORT option, which at times
1234
     might differ from the address in 'ss' used to bind to: when a user asks
1235
     the server to connect to a specific address knowing that it works, but
1236
     curl instead selects to listen to the local address because it cannot use
1237
     the provided address. FTP is strange. */
1238
0
  Curl_printable_address(ai, myhost, sizeof(myhost));
1239
1240
0
#ifdef USE_IPV6
1241
0
  if(!conn->bits.ftp_use_eprt && conn->bits.ipv6)
1242
    /* EPRT is disabled but we are connected to an IPv6 host, so we ignore the
1243
       request and enable EPRT again! */
1244
0
    conn->bits.ftp_use_eprt = TRUE;
1245
0
#endif
1246
1247
0
  for(; fcmd != DONE; fcmd++) {
1248
1249
0
    if(!conn->bits.ftp_use_eprt && (EPRT == fcmd))
1250
      /* if disabled, goto next */
1251
0
      continue;
1252
1253
0
    if((PORT == fcmd) && sa->sa_family != AF_INET)
1254
      /* PORT is IPv4 only */
1255
0
      continue;
1256
1257
0
    switch(sa->sa_family) {
1258
0
    case AF_INET:
1259
0
      port = ntohs(sa4->sin_port);
1260
0
      break;
1261
0
#ifdef USE_IPV6
1262
0
    case AF_INET6:
1263
0
      port = ntohs(sa6->sin6_port);
1264
0
      break;
1265
0
#endif
1266
0
    default:
1267
0
      continue; /* might as well skip this */
1268
0
    }
1269
1270
0
    if(EPRT == fcmd) {
1271
      /*
1272
       * Two fine examples from RFC2428;
1273
       *
1274
       * EPRT |1|132.235.1.2|6275|
1275
       *
1276
       * EPRT |2|1080::8:800:200C:417A|5282|
1277
       */
1278
0
      result = Curl_pp_sendf(data, &ftpc->pp, "%s |%d|%s|%hu|", mode[fcmd],
1279
0
                             sa->sa_family == AF_INET ? 1 : 2,
1280
0
                             myhost, port);
1281
0
      if(result) {
1282
0
        failf(data, "Failure sending EPRT command: %s",
1283
0
              curl_easy_strerror(result));
1284
0
        return result;
1285
0
      }
1286
0
      break;
1287
0
    }
1288
0
    if(PORT == fcmd) {
1289
      /* large enough for [IP address],[num],[num] */
1290
0
      char target[sizeof(myhost) + 20];
1291
0
      const char *source = myhost;
1292
0
      char *dest = target;
1293
1294
      /* translate x.x.x.x to x,x,x,x */
1295
0
      while(*source) {
1296
0
        if(*source == '.')
1297
0
          *dest = ',';
1298
0
        else
1299
0
          *dest = *source;
1300
0
        dest++;
1301
0
        source++;
1302
0
      }
1303
0
      *dest = 0;
1304
0
      curl_msnprintf(dest, 20, ",%d,%d", (int)(port >> 8), (int)(port & 0xff));
1305
1306
0
      result = Curl_pp_sendf(data, &ftpc->pp, "%s %s", mode[fcmd], target);
1307
0
      if(result) {
1308
0
        failf(data, "Failure sending PORT command: %s",
1309
0
              curl_easy_strerror(result));
1310
0
        return result;
1311
0
      }
1312
0
      break;
1313
0
    }
1314
0
  }
1315
1316
  /* store which command was sent */
1317
0
  ftpc->count1 = fcmd;
1318
0
  ftp_state(data, ftpc, FTP_PORT);
1319
0
  return CURLE_OK;
1320
0
}
1321
1322
/*
1323
 * ftp_state_use_port()
1324
 *
1325
 * Set up an active-mode FTP data connection (using PORT or EPRT) and start
1326
 * listening for the server's incoming connection on SECONDARYSOCKET.
1327
 */
1328
static CURLcode ftp_state_use_port(struct Curl_easy *data,
1329
                                   struct ftp_conn *ftpc,
1330
                                   ftpport fcmd) /* start with this */
1331
30
{
1332
30
  CURLcode result = CURLE_FTP_PORT_FAILED;
1333
30
  struct connectdata *conn = data->conn;
1334
30
  curl_socket_t portsock = CURL_SOCKET_BAD;
1335
1336
30
  struct Curl_sockaddr_storage ss;
1337
30
  curl_socklen_t sslen;
1338
30
  char hbuf[NI_MAXHOST];
1339
30
  const char *host = NULL;
1340
30
  const char *string_ftpport = data->set.str[STRING_FTPPORT];
1341
30
  struct Curl_dns_entry *dns_entry = NULL;
1342
30
  const struct Curl_addrinfo *res = NULL;
1343
30
  const struct Curl_addrinfo *ai = NULL;
1344
30
  unsigned short port_min = 0;
1345
30
  unsigned short port_max = 0;
1346
30
  bool non_local = TRUE;
1347
1348
  /* parse the FTPPORT string for address and port range */
1349
30
  result = ftp_port_parse_string(data, conn, string_ftpport,
1350
30
                                 &ss, &port_min, &port_max,
1351
30
                                 &host, hbuf, sizeof(hbuf));
1352
30
  if(!result && !host)
1353
    /* if no host was specified, use the control connection's local IP */
1354
5
    result = ftp_port_default_host(data, conn, &ss, &sslen, &host,
1355
5
                                   hbuf, sizeof(hbuf), &non_local);
1356
1357
  /* resolve host string to address list */
1358
30
  if(!result)
1359
24
    result = ftp_port_resolve_host(data, conn, host, &dns_entry, &res);
1360
1361
  /* Open a TCP socket for the data connection */
1362
30
  if(!result)
1363
16
    result = ftp_port_open_socket(data, conn, res, &ai, &portsock);
1364
30
  if(!result) {
1365
16
    CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), opened socket",
1366
16
                 FTP_CSTATE(ftpc));
1367
1368
    /* bind to a suitable local address / port */
1369
16
    result = ftp_port_bind_socket(data, conn, portsock, ai, &ss, &sslen,
1370
16
                                  port_min, port_max, non_local);
1371
16
  }
1372
1373
  /* listen */
1374
30
  if(!result)
1375
0
    result = ftp_port_listen(data, portsock);
1376
1377
  /* send the PORT / EPRT command */
1378
30
  if(!result)
1379
0
    result = ftp_port_send_command(data, ftpc, conn, &ss, ai, fcmd);
1380
1381
  /* replace any filter on SECONDARY with one listening on this socket */
1382
30
  if(!result)
1383
0
    result = Curl_conn_tcp_listen_set(data, conn, SECONDARYSOCKET, &portsock);
1384
1385
30
  if(!result)
1386
0
    portsock = CURL_SOCKET_BAD; /* now held in filter */
1387
1388
  /* cleanup */
1389
1390
30
  if(dns_entry)
1391
16
    Curl_resolv_unlink(data, &dns_entry);
1392
30
  if(result) {
1393
30
    ftp_state(data, ftpc, FTP_STOP);
1394
30
  }
1395
0
  else {
1396
    /* successfully set up the listen socket filter. SSL needed? */
1397
0
    if(conn->bits.ftp_use_data_ssl && data->set.ftp_use_port &&
1398
0
       !Curl_conn_is_ssl(conn, SECONDARYSOCKET)) {
1399
0
      result = Curl_ssl_cfilter_add(data, conn, SECONDARYSOCKET);
1400
0
    }
1401
0
    conn->bits.do_more = FALSE;
1402
0
    Curl_pgrsTime(data, TIMER_STARTACCEPT);
1403
0
    Curl_expire(data, (data->set.accepttimeout > 0) ?
1404
0
                data->set.accepttimeout: DEFAULT_ACCEPT_TIMEOUT,
1405
0
                EXPIRE_FTP_ACCEPT);
1406
0
  }
1407
30
  if(portsock != CURL_SOCKET_BAD)
1408
16
    Curl_socket_close(data, conn, portsock);
1409
30
  return result;
1410
30
}
1411
1412
static CURLcode ftp_state_use_pasv(struct Curl_easy *data,
1413
                                   struct ftp_conn *ftpc,
1414
                                   struct connectdata *conn)
1415
1.31k
{
1416
1.31k
  CURLcode result = CURLE_OK;
1417
  /*
1418
    Here's the executive summary on what to do:
1419
1420
    PASV is RFC959, expect:
1421
    227 Entering Passive Mode (a1,a2,a3,a4,p1,p2)
1422
1423
    LPSV is RFC1639, expect:
1424
    228 Entering Long Passive Mode (4,4,a1,a2,a3,a4,2,p1,p2)
1425
1426
    EPSV is RFC2428, expect:
1427
    229 Entering Extended Passive Mode (|||port|)
1428
1429
  */
1430
1431
1.31k
  static const char mode[][5] = { "EPSV", "PASV" };
1432
1.31k
  int modeoff;
1433
1434
1.31k
#ifdef PF_INET6
1435
1.31k
  if(!conn->bits.ftp_use_epsv && conn->bits.ipv6)
1436
    /* EPSV is disabled but we are connected to an IPv6 host, so we ignore the
1437
       request and enable EPSV again! */
1438
0
    conn->bits.ftp_use_epsv = TRUE;
1439
1.31k
#endif
1440
1441
1.31k
  modeoff = conn->bits.ftp_use_epsv ? 0 : 1;
1442
1443
1.31k
  result = Curl_pp_sendf(data, &ftpc->pp, "%s", mode[modeoff]);
1444
1.31k
  if(!result) {
1445
1.31k
    ftpc->count1 = modeoff;
1446
1.31k
    ftp_state(data, ftpc, FTP_PASV);
1447
1.31k
    infof(data, "Connect data stream passively");
1448
1.31k
  }
1449
1.31k
  return result;
1450
1.31k
}
1451
1452
/*
1453
 * ftp_state_prepare_transfer() starts PORT, PASV or PRET etc.
1454
 *
1455
 * REST is the last command in the chain of commands when a "head"-like
1456
 * request is made. Thus, if an actual transfer is to be made this is where we
1457
 * take off for real.
1458
 */
1459
static CURLcode ftp_state_prepare_transfer(struct Curl_easy *data,
1460
                                           struct ftp_conn *ftpc,
1461
                                           struct FTP *ftp)
1462
1.40k
{
1463
1.40k
  CURLcode result = CURLE_OK;
1464
1.40k
  struct connectdata *conn = data->conn;
1465
1466
1.40k
  if(ftp->transfer != PPTRANSFER_BODY) {
1467
    /* does not transfer any data */
1468
1469
    /* still possibly do PRE QUOTE jobs */
1470
64
    ftp_state(data, ftpc, FTP_RETR_PREQUOTE);
1471
64
    result = ftp_state_quote(data, ftpc, ftp, TRUE, FTP_RETR_PREQUOTE);
1472
64
  }
1473
1.34k
  else if(data->set.ftp_use_port) {
1474
    /* We have chosen to use the PORT (or similar) command */
1475
30
    result = ftp_state_use_port(data, ftpc, EPRT);
1476
30
  }
1477
1.31k
  else {
1478
    /* We have chosen (this is default) to use the PASV (or similar) command */
1479
1.31k
    if(data->set.ftp_use_pret) {
1480
      /* The user has requested that we send a PRET command
1481
         to prepare the server for the upcoming PASV */
1482
4
      if(!ftpc->file)
1483
2
        result = Curl_pp_sendf(data, &ftpc->pp, "PRET %s",
1484
2
                               data->set.str[STRING_CUSTOMREQUEST] ?
1485
0
                               data->set.str[STRING_CUSTOMREQUEST] :
1486
2
                               (data->state.list_only ? "NLST" : "LIST"));
1487
2
      else if(data->state.upload)
1488
1
        result = Curl_pp_sendf(data, &ftpc->pp, "PRET STOR %s", ftpc->file);
1489
1
      else
1490
1
        result = Curl_pp_sendf(data, &ftpc->pp, "PRET RETR %s", ftpc->file);
1491
4
      if(!result)
1492
4
        ftp_state(data, ftpc, FTP_PRET);
1493
4
    }
1494
1.30k
    else
1495
1.30k
      result = ftp_state_use_pasv(data, ftpc, conn);
1496
1.31k
  }
1497
1.40k
  return result;
1498
1.40k
}
1499
1500
static CURLcode ftp_state_rest(struct Curl_easy *data,
1501
                               struct ftp_conn *ftpc,
1502
                               struct FTP *ftp)
1503
1.43k
{
1504
1.43k
  CURLcode result = CURLE_OK;
1505
1506
1.43k
  if((ftp->transfer != PPTRANSFER_BODY) && ftpc->file) {
1507
    /* if a "head"-like request is being made (on a file) */
1508
1509
    /* Determine if server can respond to REST command and therefore
1510
       whether it supports range */
1511
33
    result = Curl_pp_sendf(data, &ftpc->pp, "REST %d", 0);
1512
33
    if(!result)
1513
33
      ftp_state(data, ftpc, FTP_REST);
1514
33
  }
1515
1.40k
  else
1516
1.40k
    result = ftp_state_prepare_transfer(data, ftpc, ftp);
1517
1518
1.43k
  return result;
1519
1.43k
}
1520
1521
static CURLcode ftp_state_size(struct Curl_easy *data,
1522
                               struct ftp_conn *ftpc,
1523
                               struct FTP *ftp)
1524
1.43k
{
1525
1.43k
  CURLcode result = CURLE_OK;
1526
1527
1.43k
  if((ftp->transfer == PPTRANSFER_INFO) && ftpc->file) {
1528
    /* if a "head"-like request is being made (on a file) */
1529
1530
    /* we know ftpc->file is a valid pointer to a filename */
1531
35
    result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
1532
35
    if(!result)
1533
35
      ftp_state(data, ftpc, FTP_SIZE);
1534
35
  }
1535
1.40k
  else
1536
1.40k
    result = ftp_state_rest(data, ftpc, ftp);
1537
1538
1.43k
  return result;
1539
1.43k
}
1540
1541
static CURLcode ftp_state_list(struct Curl_easy *data,
1542
                               struct ftp_conn *ftpc,
1543
                               struct FTP *ftp)
1544
436
{
1545
436
  CURLcode result = CURLE_OK;
1546
1547
  /* If this output is to be machine-parsed, the NLST command might be better
1548
     to use, since the LIST command output is not specified or standard in any
1549
     way. It has turned out that the NLST list output is not the same on all
1550
     servers either... */
1551
1552
  /*
1553
     if FTPFILE_NOCWD was specified, we should add the path
1554
     as argument for the LIST / NLST / or custom command.
1555
     Whether the server will support this, is uncertain.
1556
1557
     The other ftp_filemethods will CWD into dir/dir/ first and
1558
     then just do LIST (in that case: nothing to do here)
1559
  */
1560
436
  const char *lstArg = NULL;
1561
436
  int lstArglen = 0;
1562
436
  char *cmd;
1563
1564
436
  if((data->set.ftp_filemethod == FTPFILE_NOCWD) && ftp->path) {
1565
    /* url-decode before evaluation: e.g. paths starting/ending with %2f */
1566
1
    const char *rawPath = ftpc->rawpath;
1567
1
    const char *slashPos = strrchr(rawPath, '/');
1568
1
    if(slashPos) {
1569
      /* chop off the file part if format is dir/file otherwise remove
1570
         the trailing slash for dir/dir/ except for absolute path / */
1571
0
      size_t n = slashPos - rawPath;
1572
0
      if(n == 0)
1573
0
        ++n;
1574
1575
0
      lstArg = rawPath;
1576
0
      lstArglen = (int)n;
1577
0
    }
1578
1
  }
1579
1580
436
  cmd = curl_maprintf("%s%s%.*s",
1581
436
                      data->set.str[STRING_CUSTOMREQUEST] ?
1582
1
                      data->set.str[STRING_CUSTOMREQUEST] :
1583
436
                      (data->state.list_only ? "NLST" : "LIST"),
1584
436
                      lstArg ? " " : "",
1585
436
                      lstArglen, lstArg ? lstArg : "");
1586
1587
436
  if(!cmd)
1588
0
    return CURLE_OUT_OF_MEMORY;
1589
1590
436
  result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd);
1591
436
  curlx_free(cmd);
1592
1593
436
  if(!result)
1594
436
    ftp_state(data, ftpc, FTP_LIST);
1595
1596
436
  return result;
1597
436
}
1598
1599
static CURLcode ftp_state_list_prequote(struct Curl_easy *data,
1600
                                        struct ftp_conn *ftpc,
1601
                                        struct FTP *ftp)
1602
0
{
1603
  /* We have sent the TYPE, now we must send the list of prequote strings */
1604
0
  return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_LIST_PREQUOTE);
1605
0
}
1606
1607
static CURLcode ftp_state_retr_prequote(struct Curl_easy *data,
1608
                                        struct ftp_conn *ftpc,
1609
                                        struct FTP *ftp)
1610
471
{
1611
  /* We have sent the TYPE, now we must send the list of prequote strings */
1612
471
  return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_RETR_PREQUOTE);
1613
471
}
1614
1615
static CURLcode ftp_state_stor_prequote(struct Curl_easy *data,
1616
                                        struct ftp_conn *ftpc,
1617
                                        struct FTP *ftp)
1618
54
{
1619
  /* We have sent the TYPE, now we must send the list of prequote strings */
1620
54
  return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_STOR_PREQUOTE);
1621
54
}
1622
1623
static CURLcode ftp_state_type(struct Curl_easy *data,
1624
                               struct ftp_conn *ftpc,
1625
                               struct FTP *ftp)
1626
1.44k
{
1627
1.44k
  CURLcode result = CURLE_OK;
1628
1629
  /* If we have selected NOBODY and HEADER, it means that we only want file
1630
     information. Which in FTP cannot be much more than the file size and
1631
     date. */
1632
1.44k
  if(data->req.no_body && ftpc->file &&
1633
39
     ftp_need_type(ftpc, (bool)data->state.prefer_ascii)) {
1634
    /* The SIZE command is _not_ RFC 959 specified, and therefore many servers
1635
       may not support it! It is however the only way we have to get a file's
1636
       size! */
1637
1638
39
    ftp->transfer = PPTRANSFER_INFO;
1639
    /* this means no actual transfer will be made */
1640
1641
    /* Some servers return different sizes for different modes, and thus we
1642
       must set the proper type before we check the size */
1643
39
    result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii,
1644
39
                         FTP_TYPE);
1645
39
    if(result)
1646
0
      return result;
1647
39
  }
1648
1.40k
  else
1649
1.40k
    result = ftp_state_size(data, ftpc, ftp);
1650
1651
1.44k
  return result;
1652
1.44k
}
1653
1654
/* This is called after the CWD commands have been done in the beginning of
1655
   the DO phase */
1656
static CURLcode ftp_state_mdtm(struct Curl_easy *data,
1657
                               struct ftp_conn *ftpc,
1658
                               struct FTP *ftp)
1659
1.44k
{
1660
1.44k
  CURLcode result = CURLE_OK;
1661
1662
  /* Requested time of file or time-depended transfer? */
1663
1.44k
  if((data->set.get_filetime || data->set.timecondition) && ftpc->file) {
1664
1665
    /* we have requested to get the modified-time of the file, this is a white
1666
       spot as the MDTM is not mentioned in RFC959 */
1667
79
    result = Curl_pp_sendf(data, &ftpc->pp, "MDTM %s", ftpc->file);
1668
1669
79
    if(!result)
1670
79
      ftp_state(data, ftpc, FTP_MDTM);
1671
79
  }
1672
1.37k
  else
1673
1.37k
    result = ftp_state_type(data, ftpc, ftp);
1674
1675
1.44k
  return result;
1676
1.44k
}
1677
1678
/* This is called after the TYPE and possible quote commands have been sent */
1679
static CURLcode ftp_state_ul_setup(struct Curl_easy *data,
1680
                                   struct ftp_conn *ftpc,
1681
                                   struct FTP *ftp,
1682
                                   bool sizechecked)
1683
54
{
1684
54
  CURLcode result = CURLE_OK;
1685
54
  curl_bit append = data->set.remote_append;
1686
1687
54
  if((data->state.resume_from && !sizechecked) ||
1688
28
     ((data->state.resume_from > 0) && sizechecked)) {
1689
    /* we are about to continue the uploading of a file */
1690
    /* 1. get already existing file's size. We use the SIZE command for this
1691
       which may not exist in the server!  The SIZE command is not in
1692
       RFC959. */
1693
1694
    /* 2. This used to set REST. But since we can do append, we
1695
       do not another ftp command. We just skip the source file
1696
       offset and then we APPEND the rest on the file instead */
1697
1698
    /* 3. pass file-size number of bytes in the source file */
1699
    /* 4. lower the infilesize counter */
1700
    /* => transfer as usual */
1701
28
    int seekerr = CURL_SEEKFUNC_OK;
1702
1703
28
    if(data->state.resume_from < 0) {
1704
      /* Got no given size to start from, figure it out */
1705
0
      result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
1706
0
      if(!result)
1707
0
        ftp_state(data, ftpc, FTP_STOR_SIZE);
1708
0
      return result;
1709
0
    }
1710
1711
    /* enable append */
1712
28
    append = TRUE;
1713
1714
    /* Let's read off the proper amount of bytes from the input. */
1715
28
    if(data->set.seek_func) {
1716
0
      Curl_set_in_callback(data, TRUE);
1717
0
      seekerr = data->set.seek_func(data->set.seek_client,
1718
0
                                    data->state.resume_from, SEEK_SET);
1719
0
      Curl_set_in_callback(data, FALSE);
1720
0
    }
1721
1722
28
    if(seekerr != CURL_SEEKFUNC_OK) {
1723
0
      curl_off_t passed = 0;
1724
0
      if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
1725
0
        failf(data, "Could not seek stream");
1726
0
        return CURLE_FTP_COULDNT_USE_REST;
1727
0
      }
1728
      /* seekerr == CURL_SEEKFUNC_CANTSEEK (cannot seek to offset) */
1729
0
      do {
1730
0
        char scratch[4 * 1024];
1731
0
        size_t readthisamountnow =
1732
0
          (data->state.resume_from - passed > (curl_off_t)sizeof(scratch)) ?
1733
0
          sizeof(scratch) :
1734
0
          curlx_sotouz(data->state.resume_from - passed);
1735
1736
0
        size_t actuallyread =
1737
0
          data->state.fread_func(scratch, 1, readthisamountnow,
1738
0
                                 data->state.in);
1739
1740
0
        passed += actuallyread;
1741
0
        if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
1742
          /* this checks for greater-than only to make sure that the
1743
             CURL_READFUNC_ABORT return code still aborts */
1744
0
          failf(data, "Failed to read data");
1745
0
          return CURLE_FTP_COULDNT_USE_REST;
1746
0
        }
1747
0
      } while(passed < data->state.resume_from);
1748
0
    }
1749
    /* now, decrease the size of the read */
1750
28
    if(data->state.infilesize > 0) {
1751
27
      data->state.infilesize -= data->state.resume_from;
1752
1753
27
      if(data->state.infilesize <= 0) {
1754
26
        infof(data, "File already completely uploaded");
1755
1756
        /* no data to transfer */
1757
26
        Curl_xfer_setup_nop(data);
1758
1759
        /* Set ->transfer so that we will not get any error in
1760
         * ftp_done() because we did not transfer anything! */
1761
26
        ftp->transfer = PPTRANSFER_NONE;
1762
1763
26
        ftp_state(data, ftpc, FTP_STOP);
1764
26
        return CURLE_OK;
1765
26
      }
1766
27
    }
1767
    /* we have passed, proceed as normal */
1768
28
  } /* resume_from */
1769
1770
28
  result = Curl_pp_sendf(data, &ftpc->pp, append ? "APPE %s" : "STOR %s",
1771
28
                         ftpc->file);
1772
28
  if(!result)
1773
28
    ftp_state(data, ftpc, FTP_STOR);
1774
1775
28
  return result;
1776
54
}
1777
1778
static CURLcode ftp_state_retr(struct Curl_easy *data,
1779
                               struct ftp_conn *ftpc,
1780
                               struct FTP *ftp,
1781
                               curl_off_t filesize)
1782
463
{
1783
463
  CURLcode result = CURLE_OK;
1784
1785
463
  CURL_TRC_FTP(data, "[%s] ftp_state_retr()", FTP_CSTATE(ftpc));
1786
463
  if(data->set.max_filesize && (filesize > data->set.max_filesize)) {
1787
32
    failf(data, "Maximum file size exceeded");
1788
32
    return CURLE_FILESIZE_EXCEEDED;
1789
32
  }
1790
431
  ftp->downloadsize = filesize;
1791
1792
431
  if(data->state.resume_from) {
1793
    /* We always (attempt to) get the size of downloads, so it is done before
1794
       this even when not doing resumes. */
1795
232
    if(filesize == -1) {
1796
48
      infof(data, "ftp server does not support SIZE");
1797
      /* We could not get the size and therefore we cannot know if there really
1798
         is a part of the file left to get, although the server will just
1799
         close the connection when we start the connection so it will not cause
1800
         us any harm, just not make us exit as nicely. */
1801
48
    }
1802
184
    else {
1803
      /* We got a file size report, so we check that there actually is a
1804
         part of the file left to get, or else we go home. */
1805
184
      if(data->state.resume_from < 0) {
1806
        /* We are supposed to download the last abs(from) bytes */
1807
88
        if(filesize < -data->state.resume_from) {
1808
48
          failf(data, "Offset (%" FMT_OFF_T
1809
48
                ") was beyond file size (%" FMT_OFF_T ")",
1810
48
                data->state.resume_from, filesize);
1811
48
          return CURLE_BAD_DOWNLOAD_RESUME;
1812
48
        }
1813
        /* convert to size to download */
1814
40
        ftp->downloadsize = -data->state.resume_from;
1815
        /* download from where? */
1816
40
        data->state.resume_from = filesize - ftp->downloadsize;
1817
40
      }
1818
96
      else {
1819
96
        if(filesize < data->state.resume_from) {
1820
8
          failf(data, "Offset (%" FMT_OFF_T
1821
8
                ") was beyond file size (%" FMT_OFF_T ")",
1822
8
                data->state.resume_from, filesize);
1823
8
          return CURLE_BAD_DOWNLOAD_RESUME;
1824
8
        }
1825
        /* Now store the number of bytes we are expected to download */
1826
88
        ftp->downloadsize = filesize - data->state.resume_from;
1827
88
      }
1828
184
    }
1829
1830
176
    if(ftp->downloadsize == 0) {
1831
      /* no data to transfer */
1832
1
      Curl_xfer_setup_nop(data);
1833
1
      infof(data, "File already completely downloaded");
1834
1835
      /* Set ->transfer so that we will not get any error in ftp_done()
1836
       * because we did not transfer the any file */
1837
1
      ftp->transfer = PPTRANSFER_NONE;
1838
1
      ftp_state(data, ftpc, FTP_STOP);
1839
1
      return CURLE_OK;
1840
1
    }
1841
1842
    /* Set resume file transfer offset */
1843
175
    infof(data, "Instructs server to resume from offset %" FMT_OFF_T,
1844
175
          data->state.resume_from);
1845
1846
175
    result = Curl_pp_sendf(data, &ftpc->pp, "REST %" FMT_OFF_T,
1847
175
                           data->state.resume_from);
1848
175
    if(!result)
1849
175
      ftp_state(data, ftpc, FTP_RETR_REST);
1850
175
  }
1851
199
  else {
1852
    /* no resume */
1853
199
    result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
1854
199
    if(!result)
1855
199
      ftp_state(data, ftpc, FTP_RETR);
1856
199
  }
1857
1858
374
  return result;
1859
431
}
1860
1861
static CURLcode ftp_state_quote(struct Curl_easy *data,
1862
                                struct ftp_conn *ftpc,
1863
                                struct FTP *ftp,
1864
                                bool init,
1865
                                ftpstate instate)
1866
2.08k
{
1867
2.08k
  CURLcode result = CURLE_OK;
1868
2.08k
  bool quote = FALSE;
1869
2.08k
  struct curl_slist *item;
1870
1871
2.08k
  switch(instate) {
1872
1.49k
  case FTP_QUOTE:
1873
1.49k
  default:
1874
1.49k
    item = data->set.quote;
1875
1.49k
    break;
1876
535
  case FTP_RETR_PREQUOTE:
1877
589
  case FTP_STOR_PREQUOTE:
1878
589
  case FTP_LIST_PREQUOTE:
1879
589
    item = data->set.prequote;
1880
589
    break;
1881
0
  case FTP_POSTQUOTE:
1882
0
    item = data->set.postquote;
1883
0
    break;
1884
2.08k
  }
1885
1886
  /*
1887
   * This state uses:
1888
   * 'count1' to iterate over the commands to send
1889
   * 'count2' to store whether to allow commands to fail
1890
   */
1891
1892
2.08k
  if(init)
1893
2.08k
    ftpc->count1 = 0;
1894
0
  else
1895
0
    ftpc->count1++;
1896
1897
2.08k
  if(item) {
1898
0
    int i = 0;
1899
1900
    /* Skip count1 items in the linked list */
1901
0
    while((i < ftpc->count1) && item) {
1902
0
      item = item->next;
1903
0
      i++;
1904
0
    }
1905
0
    if(item) {
1906
0
      const char *cmd = item->data;
1907
0
      if(cmd[0] == '*') {
1908
0
        cmd++;
1909
0
        ftpc->count2 = 1; /* the sent command is allowed to fail */
1910
0
      }
1911
0
      else
1912
0
        ftpc->count2 = 0; /* failure means cancel operation */
1913
1914
0
      result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd);
1915
0
      if(result)
1916
0
        return result;
1917
0
      ftp_state(data, ftpc, instate);
1918
0
      quote = TRUE;
1919
0
    }
1920
0
  }
1921
1922
2.08k
  if(!quote) {
1923
    /* No more quote to send, continue to ... */
1924
2.08k
    switch(instate) {
1925
1.49k
    case FTP_QUOTE:
1926
1.49k
    default:
1927
1.49k
      result = ftp_state_cwd(data, ftpc, ftp);
1928
1.49k
      break;
1929
535
    case FTP_RETR_PREQUOTE:
1930
535
      if(ftp->transfer != PPTRANSFER_BODY)
1931
64
        ftp_state(data, ftpc, FTP_STOP);
1932
471
      else {
1933
471
        if(ftpc->known_filesize != -1) {
1934
0
          Curl_pgrsSetDownloadSize(data, ftpc->known_filesize);
1935
0
          result = ftp_state_retr(data, ftpc, ftp, ftpc->known_filesize);
1936
0
        }
1937
471
        else {
1938
471
          if(data->set.ignorecl || data->state.prefer_ascii) {
1939
            /* 'ignorecl' is used to support download of growing files. It
1940
               prevents the state machine from requesting the file size from
1941
               the server. With an unknown file size the download continues
1942
               until the server terminates it, otherwise the client stops if
1943
               the received byte count exceeds the reported file size. Set
1944
               option CURLOPT_IGNORE_CONTENT_LENGTH to 1 to enable this
1945
               behavior.
1946
1947
               In addition: asking for the size for 'TYPE A' transfers is not
1948
               constructive since servers do not report the converted size. So
1949
               skip it.
1950
            */
1951
3
            result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
1952
3
            if(!result)
1953
3
              ftp_state(data, ftpc, FTP_RETR);
1954
3
          }
1955
468
          else {
1956
468
            result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
1957
468
            if(!result)
1958
468
              ftp_state(data, ftpc, FTP_RETR_SIZE);
1959
468
          }
1960
471
        }
1961
471
      }
1962
535
      break;
1963
54
    case FTP_STOR_PREQUOTE:
1964
54
      result = ftp_state_ul_setup(data, ftpc, ftp, FALSE);
1965
54
      break;
1966
0
    case FTP_POSTQUOTE:
1967
0
      break;
1968
0
    case FTP_LIST_PREQUOTE:
1969
0
      ftp_state(data, ftpc, FTP_LIST_TYPE);
1970
0
      result = ftp_state_list(data, ftpc, ftp);
1971
0
      break;
1972
2.08k
    }
1973
2.08k
  }
1974
1975
2.08k
  return result;
1976
2.08k
}
1977
1978
/* called from ftp_state_pasv_resp to switch to PASV in case of EPSV
1979
   problems */
1980
static CURLcode ftp_epsv_disable(struct Curl_easy *data,
1981
                                 struct ftp_conn *ftpc,
1982
                                 struct connectdata *conn)
1983
589
{
1984
589
  CURLcode result = CURLE_OK;
1985
1986
589
  if(conn->bits.ipv6
1987
0
#ifndef CURL_DISABLE_PROXY
1988
0
     && !(conn->bits.tunnel_proxy || conn->bits.socksproxy)
1989
589
#endif
1990
589
    ) {
1991
    /* We cannot disable EPSV when doing IPv6, so this is instead a fail */
1992
0
    failf(data, "Failed EPSV attempt, exiting");
1993
0
    return CURLE_WEIRD_SERVER_REPLY;
1994
0
  }
1995
1996
589
  infof(data, "Failed EPSV attempt. Disabling EPSV");
1997
  /* disable it for next transfer */
1998
589
  conn->bits.ftp_use_epsv = FALSE;
1999
589
  close_secondarysocket(data, ftpc);
2000
589
  data->state.errorbuf = FALSE; /* allow error message to get
2001
                                         rewritten */
2002
589
  result = Curl_pp_sendf(data, &ftpc->pp, "%s", "PASV");
2003
589
  if(!result) {
2004
589
    ftpc->count1++;
2005
    /* remain in/go to the FTP_PASV state */
2006
589
    ftp_state(data, ftpc, FTP_PASV);
2007
589
  }
2008
589
  return result;
2009
589
}
2010
2011
static CURLcode ftp_control_addr_dup(struct Curl_easy *data, char **newhostp)
2012
1.11k
{
2013
1.11k
  struct connectdata *conn = data->conn;
2014
1.11k
  struct ip_quadruple ipquad;
2015
1.11k
  bool is_ipv6;
2016
2017
  /* Returns the control connection IP address.
2018
     If a proxy tunnel is used, returns the original hostname instead, because
2019
     the effective control connection address is the proxy address,
2020
     not the ftp host. */
2021
1.11k
#ifndef CURL_DISABLE_PROXY
2022
1.11k
  if(conn->bits.tunnel_proxy || conn->bits.socksproxy)
2023
13
    *newhostp = curlx_strdup(conn->host.name);
2024
1.09k
  else
2025
1.09k
#endif
2026
1.09k
  if(!Curl_conn_get_ip_info(data, conn, FIRSTSOCKET, &is_ipv6, &ipquad) &&
2027
1.09k
     *ipquad.remote_ip)
2028
1.09k
    *newhostp = curlx_strdup(ipquad.remote_ip);
2029
3
  else {
2030
    /* failed to get the remote_ip of the DATA connection */
2031
3
    failf(data, "unable to get peername of DATA connection");
2032
3
    *newhostp = NULL;
2033
3
    return CURLE_FTP_CANT_GET_HOST;
2034
3
  }
2035
1.10k
  return *newhostp ? CURLE_OK : CURLE_OUT_OF_MEMORY;
2036
1.11k
}
2037
2038
static bool match_pasv_6nums(const char *p,
2039
                             unsigned int *array) /* 6 numbers */
2040
18.1k
{
2041
18.1k
  int i;
2042
26.1k
  for(i = 0; i < 6; i++) {
2043
25.6k
    curl_off_t num;
2044
25.6k
    if(i) {
2045
7.47k
      if(*p != ',')
2046
3.97k
        return FALSE;
2047
3.50k
      p++;
2048
3.50k
    }
2049
21.6k
    if(curlx_str_number(&p, &num, 0xff))
2050
13.6k
      return FALSE;
2051
8.01k
    array[i] = (unsigned int)num;
2052
8.01k
  }
2053
539
  return TRUE;
2054
18.1k
}
2055
2056
static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
2057
                                    struct ftp_conn *ftpc,
2058
                                    int ftpcode)
2059
1.75k
{
2060
1.75k
  struct connectdata *conn = data->conn;
2061
1.75k
  CURLcode result;
2062
1.75k
  struct Curl_dns_entry *dns = NULL;
2063
1.75k
  unsigned short connectport; /* the local port connect() should use! */
2064
1.75k
  const struct pingpong *pp = &ftpc->pp;
2065
1.75k
  char *newhost = NULL;
2066
1.75k
  unsigned short newport = 0;
2067
1.75k
  const char *str = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first
2068
                                                        letter */
2069
1.75k
  if((ftpc->count1 == 0) &&
2070
1.16k
     (ftpcode == 229)) {
2071
    /* positive EPSV response */
2072
588
    const char *ptr = strchr(str, '(');
2073
588
    if(ptr) {
2074
587
      char sep;
2075
587
      ptr++;
2076
      /* |||12345| */
2077
587
      sep = ptr[0];
2078
587
      if((ptr[1] == sep) && (ptr[2] == sep) && ISDIGIT(ptr[3])) {
2079
579
        const char *p = &ptr[3];
2080
579
        curl_off_t num;
2081
579
        if(curlx_str_number(&p, &num, 0xffff) || (*p != sep)) {
2082
5
          failf(data, "Illegal port number in EPSV reply");
2083
5
          return CURLE_FTP_WEIRD_PASV_REPLY;
2084
5
        }
2085
574
        newport = (unsigned short)num;
2086
574
        result = ftp_control_addr_dup(data, &newhost);
2087
574
        if(result)
2088
1
          return result;
2089
574
      }
2090
8
      else
2091
8
        ptr = NULL;
2092
587
    }
2093
582
    if(!ptr) {
2094
9
      failf(data, "Weirdly formatted EPSV reply");
2095
9
      return CURLE_FTP_WEIRD_PASV_REPLY;
2096
9
    }
2097
582
  }
2098
1.16k
  else if((ftpc->count1 == 1) &&
2099
584
          (ftpcode == 227)) {
2100
    /* positive PASV response */
2101
569
    unsigned int ip[6];
2102
2103
    /*
2104
     * Scan for a sequence of six comma-separated numbers and use them as
2105
     * IP+port indicators.
2106
     *
2107
     * Found reply-strings include:
2108
     * "227 Entering Passive Mode (127,0,0,1,4,51)"
2109
     * "227 Data transfer will passively listen to 127,0,0,1,4,51"
2110
     * "227 Entering passive mode. 127,0,0,1,4,51"
2111
     */
2112
18.1k
    while(*str) {
2113
18.1k
      if(match_pasv_6nums(str, ip))
2114
539
        break;
2115
17.6k
      str++;
2116
17.6k
    }
2117
2118
569
    if(!*str) {
2119
30
      failf(data, "Could not interpret the 227-response");
2120
30
      return CURLE_FTP_WEIRD_227_FORMAT;
2121
30
    }
2122
2123
    /* we got OK from server */
2124
539
    if(data->set.ftp_skip_ip) {
2125
      /* told to ignore the remotely given IP but instead use the host we used
2126
         for the control connection */
2127
538
      infof(data, "Skip %u.%u.%u.%u for data connection, reuse %s instead",
2128
538
            ip[0], ip[1], ip[2], ip[3], conn->host.name);
2129
538
      result = ftp_control_addr_dup(data, &newhost);
2130
538
      if(result)
2131
2
        return result;
2132
538
    }
2133
1
    else
2134
1
      newhost = curl_maprintf("%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
2135
2136
537
    if(!newhost)
2137
0
      return CURLE_OUT_OF_MEMORY;
2138
2139
537
    newport = (unsigned short)(((ip[4] << 8) + ip[5]) & 0xffff);
2140
537
  }
2141
594
  else if(ftpc->count1 == 0) {
2142
    /* EPSV failed, move on to PASV */
2143
579
    return ftp_epsv_disable(data, ftpc, conn);
2144
579
  }
2145
15
  else {
2146
15
    failf(data, "Bad PASV/EPSV response: %03d", ftpcode);
2147
15
    return CURLE_FTP_WEIRD_PASV_REPLY;
2148
15
  }
2149
2150
1.11k
#ifndef CURL_DISABLE_PROXY
2151
1.11k
  if(conn->bits.proxy) {
2152
    /* This connection uses a proxy and we need to connect to the proxy again
2153
     * here. We do not want to rely on a former host lookup that might have
2154
     * expired now, instead we remake the lookup here and now! */
2155
13
    struct ip_quadruple ipquad;
2156
13
    bool is_ipv6;
2157
13
    const char * const host_name = conn->bits.socksproxy ?
2158
8
      conn->socks_proxy.host.name : conn->http_proxy.host.name;
2159
2160
13
    result = Curl_conn_get_ip_info(data, data->conn, FIRSTSOCKET,
2161
13
                                   &is_ipv6, &ipquad);
2162
13
    if(result)
2163
0
      goto error;
2164
2165
13
    (void)Curl_resolv_blocking(data, host_name, ipquad.remote_port,
2166
13
                               is_ipv6 ? CURL_IPRESOLVE_V6 : CURL_IPRESOLVE_V4,
2167
13
                               &dns);
2168
    /* we connect to the proxy's port */
2169
13
    connectport = (unsigned short)ipquad.remote_port;
2170
2171
13
    if(!dns) {
2172
1
      failf(data, "cannot resolve proxy host %s:%hu", host_name, connectport);
2173
1
      result = CURLE_COULDNT_RESOLVE_PROXY;
2174
1
      goto error;
2175
1
    }
2176
13
  }
2177
1.09k
  else
2178
1.09k
#endif
2179
1.09k
  {
2180
    /* normal, direct, ftp connection */
2181
1.09k
    DEBUGASSERT(newhost);
2182
2183
    /* postponed address resolution in case of tcp fastopen */
2184
1.09k
    if(conn->bits.tcp_fastopen && !conn->bits.reuse && !newhost[0]) {
2185
0
      curlx_free(newhost);
2186
0
      result = ftp_control_addr_dup(data, &newhost);
2187
0
      if(result)
2188
0
        goto error;
2189
0
    }
2190
2191
1.09k
    (void)Curl_resolv_blocking(data, newhost, newport, conn->ip_version, &dns);
2192
1.09k
    connectport = newport; /* we connect to the remote port */
2193
2194
1.09k
    if(!dns) {
2195
1
      failf(data, "cannot resolve new host %s:%hu", newhost, connectport);
2196
1
      result = CURLE_FTP_CANT_GET_HOST;
2197
1
      goto error;
2198
1
    }
2199
1.09k
  }
2200
2201
1.10k
  result = Curl_conn_setup(data, conn, SECONDARYSOCKET, dns,
2202
1.10k
                           conn->bits.ftp_use_data_ssl ?
2203
1.10k
                           CURL_CF_SSL_ENABLE : CURL_CF_SSL_DISABLE);
2204
2205
1.10k
  if(result) {
2206
0
    if((result != CURLE_OUT_OF_MEMORY) &&
2207
0
       (ftpc->count1 == 0) && (ftpcode == 229)) {
2208
0
      curlx_free(newhost);
2209
0
      return ftp_epsv_disable(data, ftpc, conn);
2210
0
    }
2211
2212
0
    goto error;
2213
0
  }
2214
2215
  /*
2216
   * When this is used from the multi interface, this might have returned with
2217
   * the 'connected' set to FALSE and thus we are now awaiting a non-blocking
2218
   * connect to connect.
2219
   */
2220
2221
1.10k
#ifdef CURLVERBOSE
2222
1.10k
  if(data->set.verbose) {
2223
    /* Dump information about this second connection when we have issued a PASV
2224
     * command before and thus we have connected to a possibly new IP address.
2225
     */
2226
0
    char buf[256];
2227
0
    Curl_printable_address(dns->addr, buf, sizeof(buf));
2228
0
    infof(data, "Connecting to %s (%s) port %d", newhost, buf, connectport);
2229
0
  }
2230
1.10k
#endif
2231
2232
1.10k
  curlx_free(conn->secondaryhostname);
2233
1.10k
  conn->secondary_port = newport;
2234
1.10k
  conn->secondaryhostname = curlx_strdup(newhost);
2235
1.10k
  if(!conn->secondaryhostname) {
2236
0
    result = CURLE_OUT_OF_MEMORY;
2237
0
    goto error;
2238
0
  }
2239
2240
1.10k
  conn->bits.do_more = TRUE;
2241
1.10k
  ftp_state(data, ftpc, FTP_STOP); /* this phase is completed */
2242
2243
1.11k
error:
2244
1.11k
  curlx_free(newhost);
2245
1.11k
  return result;
2246
1.10k
}
2247
2248
/* called repeatedly until done from multi.c */
2249
static CURLcode ftp_statemach(struct Curl_easy *data,
2250
                              struct ftp_conn *ftpc,
2251
                              bool *done)
2252
13.4k
{
2253
13.4k
  CURLcode result = Curl_pp_statemach(data, &ftpc->pp, FALSE, FALSE);
2254
2255
  /* Check for the state outside of the Curl_socket_check() return code checks
2256
     since at times we are in fact already in this state when this function
2257
     gets called. */
2258
13.4k
  *done = (ftpc->state == FTP_STOP);
2259
2260
13.4k
  return result;
2261
13.4k
}
2262
2263
/*
2264
 * ftp_do_more()
2265
 *
2266
 * This function shall be called when the second FTP (data) connection is
2267
 * connected.
2268
 *
2269
 * 'complete' can return 0 for incomplete, 1 for done and -1 for go back
2270
 * (which basically is only for when PASV is being sent to retry a failed
2271
 * EPSV).
2272
 */
2273
static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
2274
3.03k
{
2275
3.03k
  struct connectdata *conn = data->conn;
2276
3.03k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
2277
3.03k
  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
2278
3.03k
  CURLcode result = CURLE_OK;
2279
3.03k
  bool connected = FALSE;
2280
3.03k
  bool complete = FALSE;
2281
  /* the ftp struct is inited in ftp_connect(). If we are connecting to an HTTP
2282
   * proxy then the state will not be valid until after that connection is
2283
   * complete */
2284
2285
3.03k
  if(!ftpc || !ftp)
2286
0
    return CURLE_FAILED_INIT;
2287
2288
3.03k
  *completep = 0; /* default to stay in the state */
2289
2290
  /* if the second connection has been set up, try to connect it fully
2291
   * to the remote host. This may not complete at this time, for several
2292
   * reasons:
2293
   * - we do EPTR and the server will not connect to our listen socket
2294
   *   until we send more FTP commands
2295
   * - an SSL filter is in place and the server will not start the TLS
2296
   *   handshake until we send more FTP commands
2297
   */
2298
3.03k
  if(conn->cfilter[SECONDARYSOCKET]) {
2299
3.03k
    bool is_eptr = Curl_conn_is_tcp_listen(data, SECONDARYSOCKET);
2300
3.03k
    result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected);
2301
3.03k
    if(result == CURLE_OUT_OF_MEMORY)
2302
0
      return result;
2303
3.03k
    if(result || (!connected && !is_eptr &&
2304
16
                  !Curl_conn_is_ip_connected(data, SECONDARYSOCKET))) {
2305
16
      if(result && !is_eptr && (ftpc->count1 == 0)) {
2306
10
        *completep = -1; /* go back to DOING please */
2307
        /* this is a EPSV connect failing, try PASV instead */
2308
10
        return ftp_epsv_disable(data, ftpc, conn);
2309
10
      }
2310
6
      return result;
2311
16
    }
2312
3.03k
  }
2313
2314
3.01k
  if(ftpc->state) {
2315
    /* already in a state so skip the initial commands.
2316
       They are only done to kickstart the do_more state */
2317
1.92k
    result = ftp_statemach(data, ftpc, &complete);
2318
2319
1.92k
    *completep = (int)complete;
2320
2321
    /* if we got an error or if we do not wait for a data connection return
2322
       immediately */
2323
1.92k
    if(result || !ftpc->wait_data_conn)
2324
1.92k
      return result;
2325
2326
    /* if we reach the end of the FTP state machine here, *complete will be
2327
       TRUE but so is ftpc->wait_data_conn, which says we need to wait for the
2328
       data connection and therefore we are not actually complete */
2329
0
    *completep = 0;
2330
0
  }
2331
2332
1.09k
  if(ftp->transfer <= PPTRANSFER_INFO) {
2333
    /* a transfer is about to take place, or if not a filename was given so we
2334
       will do a SIZE on it later and then we need the right TYPE first */
2335
2336
1.09k
    if(ftpc->wait_data_conn) {
2337
0
      bool serv_conned;
2338
2339
0
      result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &serv_conned);
2340
0
      if(result)
2341
0
        return result; /* Failed to accept data connection */
2342
2343
0
      if(serv_conned) {
2344
        /* It looks data connection is established */
2345
0
        ftpc->wait_data_conn = FALSE;
2346
0
        result = ftp_initiate_transfer(data, ftpc);
2347
2348
0
        if(result)
2349
0
          return result;
2350
2351
0
        *completep = 1; /* this state is now complete when the server has
2352
                           connected back to us */
2353
0
      }
2354
0
      else {
2355
0
        result = ftp_check_ctrl_on_data_wait(data, ftpc);
2356
0
        if(result)
2357
0
          return result;
2358
0
      }
2359
0
    }
2360
1.09k
    else if(data->state.upload) {
2361
57
      result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii,
2362
57
                           FTP_STOR_TYPE);
2363
57
      if(result)
2364
0
        return result;
2365
2366
57
      result = ftp_statemach(data, ftpc, &complete);
2367
      /* ftp_nb_type() might have skipped sending `TYPE A|I` when not
2368
       * deemed necessary and directly sent `STORE name`. If this was
2369
       * then complete, but we are still waiting on the data connection,
2370
       * the transfer has not been initiated yet. */
2371
57
      *completep = (int)(ftpc->wait_data_conn ? 0 : complete);
2372
57
    }
2373
1.03k
    else {
2374
      /* download */
2375
1.03k
      ftp->downloadsize = -1; /* unknown as of yet */
2376
2377
1.03k
      result = Curl_range(data);
2378
2379
1.03k
      if(result == CURLE_OK && data->req.maxdownload >= 0) {
2380
        /* Do not check for successful transfer */
2381
266
        ftpc->dont_check = TRUE;
2382
266
      }
2383
2384
1.03k
      if(result)
2385
49
        ;
2386
988
      else if((data->state.list_only || !ftpc->file) &&
2387
482
              !(data->set.prequote)) {
2388
        /* The specified path ends with a slash, and therefore we think this
2389
           is a directory that is requested, use LIST. But before that we
2390
           need to set ASCII transfer mode. */
2391
2392
        /* But only if a body transfer was requested. */
2393
482
        if(ftp->transfer == PPTRANSFER_BODY) {
2394
482
          result = ftp_nb_type(data, ftpc, ftp, TRUE, FTP_LIST_TYPE);
2395
482
          if(result)
2396
0
            return result;
2397
482
        }
2398
        /* otherwise just fall through */
2399
482
      }
2400
506
      else {
2401
506
        if(data->set.prequote && !ftpc->file) {
2402
0
          result = ftp_nb_type(data, ftpc, ftp, TRUE,
2403
0
                               FTP_RETR_LIST_TYPE);
2404
0
        }
2405
506
        else {
2406
506
          result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii,
2407
506
                               FTP_RETR_TYPE);
2408
506
        }
2409
506
        if(result)
2410
0
          return result;
2411
506
      }
2412
2413
1.03k
      result = ftp_statemach(data, ftpc, &complete);
2414
1.03k
      *completep = (int)complete;
2415
1.03k
    }
2416
1.09k
    return result;
2417
1.09k
  }
2418
2419
  /* no data to transfer */
2420
0
  Curl_xfer_setup_nop(data);
2421
2422
0
  if(!ftpc->wait_data_conn) {
2423
    /* no waiting for the data connection so this is now complete */
2424
0
    *completep = 1;
2425
0
    CURL_TRC_FTP(data, "[%s] DO-MORE phase ends with %d", FTP_CSTATE(ftpc),
2426
0
                 (int)result);
2427
0
  }
2428
2429
0
  return result;
2430
1.09k
}
2431
2432
/* call this when the DO phase has completed */
2433
static CURLcode ftp_dophase_done(struct Curl_easy *data,
2434
                                 struct ftp_conn *ftpc,
2435
                                 struct FTP *ftp,
2436
                                 bool connected)
2437
1.17k
{
2438
1.17k
  if(connected) {
2439
0
    int completed;
2440
0
    CURLcode result = ftp_do_more(data, &completed);
2441
2442
0
    if(result) {
2443
0
      close_secondarysocket(data, ftpc);
2444
0
      return result;
2445
0
    }
2446
0
  }
2447
2448
1.17k
  if(ftp->transfer != PPTRANSFER_BODY)
2449
    /* no data to transfer */
2450
70
    Curl_xfer_setup_nop(data);
2451
1.10k
  else if(!connected)
2452
    /* since we did not connect now, we want do_more to get called */
2453
1.10k
    data->conn->bits.do_more = TRUE;
2454
2455
1.17k
  ftpc->ctl_valid = TRUE; /* seems good */
2456
2457
1.17k
  return CURLE_OK;
2458
1.17k
}
2459
2460
static CURLcode ftp_state_port_resp(struct Curl_easy *data,
2461
                                    struct ftp_conn *ftpc,
2462
                                    struct FTP *ftp,
2463
                                    int ftpcode)
2464
0
{
2465
0
  struct connectdata *conn = data->conn;
2466
0
  ftpport fcmd = (ftpport)ftpc->count1;
2467
0
  CURLcode result = CURLE_OK;
2468
2469
  /* The FTP spec tells a positive response should have code 200.
2470
     Be more permissive here to tolerate deviant servers. */
2471
0
  if(ftpcode / 100 != 2) {
2472
    /* the command failed */
2473
2474
0
    if(EPRT == fcmd) {
2475
0
      infof(data, "disabling EPRT usage");
2476
0
      conn->bits.ftp_use_eprt = FALSE;
2477
0
    }
2478
0
    fcmd++;
2479
2480
0
    if(fcmd == DONE) {
2481
0
      failf(data, "Failed to do PORT");
2482
0
      result = CURLE_FTP_PORT_FAILED;
2483
0
    }
2484
0
    else
2485
      /* try next */
2486
0
      result = ftp_state_use_port(data, ftpc, fcmd);
2487
0
  }
2488
0
  else {
2489
0
    infof(data, "Connect data stream actively");
2490
0
    ftp_state(data, ftpc, FTP_STOP); /* end of DO phase */
2491
0
    result = ftp_dophase_done(data, ftpc, ftp, FALSE);
2492
0
  }
2493
2494
0
  return result;
2495
0
}
2496
2497
static int twodigit(const char *p)
2498
462
{
2499
462
  return ((p[0] - '0') * 10) + (p[1] - '0');
2500
462
}
2501
2502
static bool ftp_213_date(const char *p, int *year, int *month, int *day,
2503
                         int *hour, int *minute, int *second)
2504
68
{
2505
68
  size_t len = strlen(p);
2506
68
  if(len < 14)
2507
2
    return FALSE;
2508
66
  *year = (twodigit(&p[0]) * 100) + twodigit(&p[2]);
2509
66
  *month = twodigit(&p[4]);
2510
66
  *day = twodigit(&p[6]);
2511
66
  *hour = twodigit(&p[8]);
2512
66
  *minute = twodigit(&p[10]);
2513
66
  *second = twodigit(&p[12]);
2514
2515
66
  if((*month > 12) || (*day > 31) || (*hour > 23) || (*minute > 59) ||
2516
59
     (*second > 60))
2517
10
    return FALSE;
2518
56
  return TRUE;
2519
66
}
2520
2521
static CURLcode client_write_header(struct Curl_easy *data,
2522
                                    char *buf, size_t blen)
2523
29
{
2524
  /* Some replies from an FTP server are written to the client
2525
   * as CLIENTWRITE_HEADER, formatted as if they came from a
2526
   * HTTP conversation.
2527
   * In all protocols, CLIENTWRITE_HEADER data is only passed to
2528
   * the body write callback when data->set.include_header is set
2529
   * via CURLOPT_HEADER.
2530
   * For historic reasons, FTP never played this game and expects
2531
   * all its headers to do that always. Set that flag during the
2532
   * call to Curl_client_write() so it does the right thing.
2533
   *
2534
   * Notice that we cannot enable this flag for FTP in general,
2535
   * as an FTP transfer might involve an HTTP proxy connection and
2536
   * headers from CONNECT should not automatically be part of the
2537
   * output. */
2538
29
  CURLcode result;
2539
29
  bool save = (bool)data->set.include_header;
2540
29
  data->set.include_header = TRUE;
2541
29
  result = Curl_client_write(data, CLIENTWRITE_HEADER, buf, blen);
2542
29
  data->set.include_header = save;
2543
29
  return result;
2544
29
}
2545
2546
static CURLcode ftp_state_mdtm_resp(struct Curl_easy *data,
2547
                                    struct ftp_conn *ftpc,
2548
                                    struct FTP *ftp,
2549
                                    int ftpcode)
2550
78
{
2551
78
  CURLcode result = CURLE_OK;
2552
2553
78
  switch(ftpcode) {
2554
68
  case 213: {
2555
    /* we got a time. Format should be: "YYYYMMDDHHMMSS[.sss]" where the
2556
       last .sss part is optional and means fractions of a second */
2557
68
    int year, month, day, hour, minute, second;
2558
68
    struct pingpong *pp = &ftpc->pp;
2559
68
    const char *resp = curlx_dyn_ptr(&pp->recvbuf) + 4;
2560
68
    bool showtime = FALSE;
2561
68
    if(ftp_213_date(resp, &year, &month, &day, &hour, &minute, &second)) {
2562
      /* we have a time, reformat it */
2563
56
      char timebuf[24];
2564
56
      curl_msnprintf(timebuf, sizeof(timebuf),
2565
56
                     "%04d%02d%02d %02d:%02d:%02d GMT",
2566
56
                     year, month, day, hour, minute, second);
2567
      /* now, convert this into a time() value: */
2568
56
      if(!Curl_getdate_capped(timebuf, &data->info.filetime))
2569
33
        showtime = TRUE;
2570
56
    }
2571
2572
    /* If we asked for a time of the file and we actually got one as well,
2573
       we "emulate" an HTTP-style header in our output. */
2574
2575
#if defined(__GNUC__) && (defined(__DJGPP__) || defined(__AMIGA__))
2576
#pragma GCC diagnostic push
2577
/* 'time_t' is unsigned in MSDOS and AmigaOS. Silence:
2578
   warning: comparison of unsigned expression in '>= 0' is always true */
2579
#pragma GCC diagnostic ignored "-Wtype-limits"
2580
#endif
2581
68
    if(data->req.no_body && ftpc->file &&
2582
4
       data->set.get_filetime && showtime) {
2583
#if defined(__GNUC__) && (defined(__DJGPP__) || defined(__AMIGA__))
2584
#pragma GCC diagnostic pop
2585
#endif
2586
2
      char headerbuf[128];
2587
2
      int headerbuflen;
2588
2
      time_t filetime = data->info.filetime;
2589
2
      struct tm buffer;
2590
2
      const struct tm *tm = &buffer;
2591
2592
2
      result = curlx_gmtime(filetime, &buffer);
2593
2
      if(result)
2594
0
        return result;
2595
2596
      /* format: "Tue, 15 Nov 1994 12:45:26" */
2597
2
      headerbuflen =
2598
2
        curl_msnprintf(headerbuf, sizeof(headerbuf),
2599
2
                       "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d "
2600
2
                       "GMT\r\n",
2601
2
                       Curl_wkday[tm->tm_wday ? tm->tm_wday-1 : 6],
2602
2
                       tm->tm_mday,
2603
2
                       Curl_month[tm->tm_mon],
2604
2
                       tm->tm_year + 1900,
2605
2
                       tm->tm_hour,
2606
2
                       tm->tm_min,
2607
2
                       tm->tm_sec);
2608
2
      result = client_write_header(data, headerbuf, headerbuflen);
2609
2
      if(result)
2610
0
        return result;
2611
2
    } /* end of a ridiculous amount of conditionals */
2612
68
  }
2613
68
    break;
2614
68
  default:
2615
9
    infof(data, "unsupported MDTM reply format");
2616
9
    break;
2617
1
  case 550: /* 550 is used for several different problems, e.g.
2618
               "No such file or directory" or "Permission denied".
2619
               It does not mean that the file does not exist at all. */
2620
1
    infof(data, "MDTM failed: file does not exist or permission problem,"
2621
1
          " continuing");
2622
1
    break;
2623
78
  }
2624
2625
78
  if(data->set.timecondition) {
2626
76
    if((data->info.filetime > 0) && (data->set.timevalue > 0)) {
2627
19
      switch(data->set.timecondition) {
2628
2
      case CURL_TIMECOND_IFMODSINCE:
2629
13
      default:
2630
13
        if(data->info.filetime <= data->set.timevalue) {
2631
1
          infof(data, "The requested document is not new enough");
2632
1
          ftp->transfer = PPTRANSFER_NONE; /* mark to not transfer data */
2633
1
          data->info.timecond = TRUE;
2634
1
          ftp_state(data, ftpc, FTP_STOP);
2635
1
          return CURLE_OK;
2636
1
        }
2637
12
        break;
2638
12
      case CURL_TIMECOND_IFUNMODSINCE:
2639
6
        if(data->info.filetime > data->set.timevalue) {
2640
5
          infof(data, "The requested document is not old enough");
2641
5
          ftp->transfer = PPTRANSFER_NONE; /* mark to not transfer data */
2642
5
          data->info.timecond = TRUE;
2643
5
          ftp_state(data, ftpc, FTP_STOP);
2644
5
          return CURLE_OK;
2645
5
        }
2646
1
        break;
2647
19
      } /* switch */
2648
19
    }
2649
57
    else {
2650
57
      infof(data, "Skipping time comparison");
2651
57
    }
2652
76
  }
2653
2654
72
  if(!result)
2655
72
    result = ftp_state_type(data, ftpc, ftp);
2656
2657
72
  return result;
2658
78
}
2659
2660
static CURLcode ftp_state_type_resp(struct Curl_easy *data,
2661
                                    struct ftp_conn *ftpc,
2662
                                    struct FTP *ftp,
2663
                                    int ftpcode,
2664
                                    ftpstate instate)
2665
1.06k
{
2666
1.06k
  CURLcode result = CURLE_OK;
2667
2668
1.06k
  if(ftpcode / 100 != 2) {
2669
    /* "sasserftpd" and "(u)r(x)bot ftpd" both responds with 226 after a
2670
       successful 'TYPE I'. While that is not as RFC959 says, it is still a
2671
       positive response code and we allow that. */
2672
73
    failf(data, "Could not set desired mode");
2673
73
    return CURLE_FTP_COULDNT_SET_TYPE;
2674
73
  }
2675
996
  if(ftpcode != 200)
2676
959
    infof(data, "Got a %03d response code instead of the assumed 200",
2677
996
          ftpcode);
2678
2679
996
  if(instate == FTP_TYPE)
2680
35
    result = ftp_state_size(data, ftpc, ftp);
2681
961
  else if(instate == FTP_LIST_TYPE)
2682
436
    result = ftp_state_list(data, ftpc, ftp);
2683
525
  else if(instate == FTP_RETR_TYPE)
2684
471
    result = ftp_state_retr_prequote(data, ftpc, ftp);
2685
54
  else if(instate == FTP_STOR_TYPE)
2686
54
    result = ftp_state_stor_prequote(data, ftpc, ftp);
2687
0
  else if(instate == FTP_RETR_LIST_TYPE)
2688
0
    result = ftp_state_list_prequote(data, ftpc, ftp);
2689
2690
996
  return result;
2691
1.06k
}
2692
2693
static CURLcode ftp_state_size_resp(struct Curl_easy *data,
2694
                                    struct ftp_conn *ftpc,
2695
                                    struct FTP *ftp,
2696
                                    int ftpcode,
2697
                                    ftpstate instate)
2698
497
{
2699
497
  CURLcode result = CURLE_OK;
2700
497
  curl_off_t filesize = -1;
2701
497
  const char *buf = curlx_dyn_ptr(&ftpc->pp.recvbuf);
2702
497
  size_t len = ftpc->pp.nfinal;
2703
2704
  /* get the size from the ascii string: */
2705
497
  if(ftpcode == 213) {
2706
    /* To allow servers to prepend "rubbish" in the response string, we scan
2707
       for all the digits at the end of the response and parse only those as a
2708
       number. */
2709
392
    const char *start = &buf[4];
2710
392
    const char *fdigit = memchr(start, '\r', len - 4);
2711
392
    if(fdigit) {
2712
14
      fdigit--;
2713
14
      if(*fdigit == '\n')
2714
0
        fdigit--;
2715
59
      while(ISDIGIT(fdigit[-1]) && (fdigit > start))
2716
45
        fdigit--;
2717
14
    }
2718
378
    else
2719
378
      fdigit = start;
2720
392
    if(curlx_str_number(&fdigit, &filesize, CURL_OFF_T_MAX))
2721
17
      filesize = -1; /* size remain unknown */
2722
392
  }
2723
105
  else if(ftpcode == 550) { /* "No such file or directory" */
2724
    /* allow a SIZE failure for (resumed) uploads, when probing what command
2725
       to use */
2726
1
    if(instate != FTP_STOR_SIZE) {
2727
1
      failf(data, "The file does not exist");
2728
1
      return CURLE_REMOTE_FILE_NOT_FOUND;
2729
1
    }
2730
1
  }
2731
2732
496
  if(instate == FTP_SIZE) {
2733
33
    if(filesize != -1) {
2734
27
      char clbuf[128];
2735
27
      int clbuflen = curl_msnprintf(clbuf, sizeof(clbuf),
2736
27
                                    "Content-Length: %" FMT_OFF_T "\r\n",
2737
27
                                    filesize);
2738
27
      result = client_write_header(data, clbuf, clbuflen);
2739
27
      if(result)
2740
0
        return result;
2741
27
    }
2742
33
    Curl_pgrsSetDownloadSize(data, filesize);
2743
33
    result = ftp_state_rest(data, ftpc, ftp);
2744
33
  }
2745
463
  else if(instate == FTP_RETR_SIZE) {
2746
463
    Curl_pgrsSetDownloadSize(data, filesize);
2747
463
    result = ftp_state_retr(data, ftpc, ftp, filesize);
2748
463
  }
2749
0
  else if(instate == FTP_STOR_SIZE) {
2750
0
    data->state.resume_from = filesize;
2751
0
    result = ftp_state_ul_setup(data, ftpc, ftp, TRUE);
2752
0
  }
2753
2754
496
  return result;
2755
496
}
2756
2757
static CURLcode ftp_state_rest_resp(struct Curl_easy *data,
2758
                                    struct ftp_conn *ftpc,
2759
                                    struct FTP *ftp,
2760
                                    int ftpcode,
2761
                                    ftpstate instate)
2762
18
{
2763
18
  CURLcode result = CURLE_OK;
2764
2765
18
  switch(instate) {
2766
4
  case FTP_REST:
2767
4
  default:
2768
4
    if(ftpcode == 350) {
2769
0
      char buffer[24] = { "Accept-ranges: bytes\r\n" };
2770
0
      result = client_write_header(data, buffer, strlen(buffer));
2771
0
      if(result)
2772
0
        return result;
2773
0
    }
2774
4
    result = ftp_state_prepare_transfer(data, ftpc, ftp);
2775
4
    break;
2776
2777
14
  case FTP_RETR_REST:
2778
14
    if(ftpcode != 350) {
2779
13
      failf(data, "Could not use REST");
2780
13
      result = CURLE_FTP_COULDNT_USE_REST;
2781
13
    }
2782
1
    else {
2783
1
      result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
2784
1
      if(!result)
2785
1
        ftp_state(data, ftpc, FTP_RETR);
2786
1
    }
2787
14
    break;
2788
18
  }
2789
2790
18
  return result;
2791
18
}
2792
2793
static CURLcode ftp_state_stor_resp(struct Curl_easy *data,
2794
                                    struct ftp_conn *ftpc,
2795
                                    int ftpcode)
2796
24
{
2797
24
  CURLcode result = CURLE_OK;
2798
2799
24
  if(ftpcode >= 400) {
2800
1
    failf(data, "Failed FTP upload: %0d", ftpcode);
2801
1
    ftp_state(data, ftpc, FTP_STOP);
2802
1
    return CURLE_UPLOAD_FAILED;
2803
1
  }
2804
2805
  /* PORT means we are now awaiting the server to connect to us. */
2806
23
  if(data->set.ftp_use_port) {
2807
0
    bool connected;
2808
2809
0
    ftp_state(data, ftpc, FTP_STOP); /* no longer in STOR state */
2810
2811
0
    result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected);
2812
0
    if(result)
2813
0
      return result;
2814
2815
0
    if(!connected) {
2816
0
      infof(data, "Data conn was not available immediately");
2817
0
      ftpc->wait_data_conn = TRUE;
2818
0
      return ftp_check_ctrl_on_data_wait(data, ftpc);
2819
0
    }
2820
0
    ftpc->wait_data_conn = FALSE;
2821
0
  }
2822
23
  return ftp_initiate_transfer(data, ftpc);
2823
23
}
2824
2825
/* for LIST and RETR responses */
2826
static CURLcode ftp_state_get_resp(struct Curl_easy *data,
2827
                                   struct ftp_conn *ftpc,
2828
                                   struct FTP *ftp,
2829
                                   int ftpcode,
2830
                                   ftpstate instate)
2831
618
{
2832
618
  CURLcode result = CURLE_OK;
2833
2834
618
  if((ftpcode == 150) || (ftpcode == 125)) {
2835
2836
    /*
2837
      A;
2838
      150 Opening BINARY mode data connection for /etc/passwd (2241
2839
      bytes).  (ok, the file is being transferred)
2840
2841
      B:
2842
      150 Opening ASCII mode data connection for /bin/ls
2843
2844
      C:
2845
      150 ASCII data connection for /bin/ls (137.167.104.91,37445) (0 bytes).
2846
2847
      D:
2848
      150 Opening ASCII mode data connection for [file] (0.0.0.0,0) (545 bytes)
2849
2850
      E:
2851
      125 Data connection already open; Transfer starting. */
2852
2853
604
    data->req.size = -1; /* default unknown size */
2854
2855
    /*
2856
     * It appears that there are FTP-servers that return size 0 for files when
2857
     * SIZE is used on the file while being in BINARY mode. To work around
2858
     * that (stupid) behavior, we attempt to parse the RETR response even if
2859
     * the SIZE returned size zero.
2860
     *
2861
     * Debugging help from Salvatore Sorrentino on February 26, 2003.
2862
     */
2863
2864
604
    if((instate != FTP_LIST) &&
2865
179
       !data->state.prefer_ascii &&
2866
178
       !data->set.ignorecl &&
2867
178
       (ftp->downloadsize < 1)) {
2868
      /*
2869
       * It seems directory listings either do not show the size or often uses
2870
       * size 0 anyway. ASCII transfers may cause that the transferred amount
2871
       * of data is not the same as this line tells, why using this number in
2872
       * those cases only confuses us.
2873
       *
2874
       * Example D above makes this parsing a little tricky */
2875
53
      size_t len = curlx_dyn_len(&ftpc->pp.recvbuf);
2876
53
      if(len >= 7) { /* "1 bytes" is 7 characters */
2877
52
        size_t i;
2878
1.64k
        for(i = 0; i < len - 7; i++) {
2879
1.59k
          curl_off_t what;
2880
1.59k
          const char *buf = curlx_dyn_ptr(&ftpc->pp.recvbuf);
2881
1.59k
          const char *c = &buf[i];
2882
1.59k
          if(!curlx_str_number(&c, &what, CURL_OFF_T_MAX) &&
2883
666
             !curlx_str_single(&c, ' ') &&
2884
357
             !strncmp(c, "bytes", 5)) {
2885
1
            data->req.size = what;
2886
1
            break;
2887
1
          }
2888
1.59k
        }
2889
52
      }
2890
53
    }
2891
551
    else if(ftp->downloadsize > -1)
2892
125
      data->req.size = ftp->downloadsize;
2893
2894
604
    if(data->req.size > data->req.maxdownload && data->req.maxdownload > 0)
2895
20
      data->req.size = data->req.maxdownload;
2896
584
    else if((instate != FTP_LIST) && (data->state.prefer_ascii))
2897
1
      data->req.size = -1; /* for servers that understate ASCII mode file
2898
                              size */
2899
2900
604
    infof(data, "Maxdownload = %" FMT_OFF_T, data->req.maxdownload);
2901
2902
604
    if(instate != FTP_LIST)
2903
179
      infof(data, "Getting file with size: %" FMT_OFF_T, data->req.size);
2904
2905
604
    if(data->set.ftp_use_port) {
2906
0
      bool connected;
2907
2908
0
      result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected);
2909
0
      if(result)
2910
0
        return result;
2911
2912
0
      if(!connected) {
2913
0
        infof(data, "Data conn was not available immediately");
2914
0
        ftp_state(data, ftpc, FTP_STOP);
2915
0
        ftpc->wait_data_conn = TRUE;
2916
0
        return ftp_check_ctrl_on_data_wait(data, ftpc);
2917
0
      }
2918
0
      ftpc->wait_data_conn = FALSE;
2919
0
    }
2920
604
    return ftp_initiate_transfer(data, ftpc);
2921
604
  }
2922
14
  else {
2923
14
    if((instate == FTP_LIST) && (ftpcode == 450)) {
2924
      /* simply no matching files in the directory listing */
2925
3
      ftp->transfer = PPTRANSFER_NONE; /* do not download anything */
2926
3
      ftp_state(data, ftpc, FTP_STOP); /* this phase is over */
2927
3
    }
2928
11
    else {
2929
11
      failf(data, "RETR response: %03d", ftpcode);
2930
11
      return instate == FTP_RETR && ftpcode == 550 ?
2931
1
        CURLE_REMOTE_FILE_NOT_FOUND :
2932
11
        CURLE_FTP_COULDNT_RETR_FILE;
2933
11
    }
2934
14
  }
2935
2936
3
  return result;
2937
618
}
2938
2939
/* after USER, PASS and ACCT */
2940
static CURLcode ftp_state_loggedin(struct Curl_easy *data,
2941
                                   struct ftp_conn *ftpc)
2942
1.52k
{
2943
1.52k
  CURLcode result = CURLE_OK;
2944
2945
1.52k
  if(data->conn->bits.ftp_use_control_ssl) {
2946
    /* PBSZ = PROTECTION BUFFER SIZE.
2947
2948
    The 'draft-murray-auth-ftp-ssl' (draft 12, page 7) says:
2949
2950
    Specifically, the PROT command MUST be preceded by a PBSZ
2951
    command and a PBSZ command MUST be preceded by a successful
2952
    security data exchange (the TLS negotiation in this case)
2953
2954
    ... (and on page 8):
2955
2956
    Thus the PBSZ command must still be issued, but must have a
2957
    parameter of '0' to indicate that no buffering is taking place
2958
    and the data connection should not be encapsulated.
2959
    */
2960
0
    result = Curl_pp_sendf(data, &ftpc->pp, "PBSZ %d", 0);
2961
0
    if(!result)
2962
0
      ftp_state(data, ftpc, FTP_PBSZ);
2963
0
  }
2964
1.52k
  else {
2965
1.52k
    result = ftp_state_pwd(data, ftpc);
2966
1.52k
  }
2967
1.52k
  return result;
2968
1.52k
}
2969
2970
/* for USER and PASS responses */
2971
static CURLcode ftp_state_user_resp(struct Curl_easy *data,
2972
                                    struct ftp_conn *ftpc,
2973
                                    int ftpcode)
2974
1.54k
{
2975
1.54k
  CURLcode result = CURLE_OK;
2976
2977
  /* some need password anyway, and others just return 2xx ignored */
2978
1.54k
  if((ftpcode == 331) && (ftpc->state == FTP_USER)) {
2979
    /* 331 Password required for ...
2980
       (the server requires to send the user's password too) */
2981
5
    result = Curl_pp_sendf(data, &ftpc->pp, "PASS %s", data->conn->passwd);
2982
5
    if(!result)
2983
5
      ftp_state(data, ftpc, FTP_PASS);
2984
5
  }
2985
1.54k
  else if(ftpcode / 100 == 2) {
2986
    /* 230 User ... logged in.
2987
       (the user logged in with or without password) */
2988
1.52k
    result = ftp_state_loggedin(data, ftpc);
2989
1.52k
  }
2990
15
  else if(ftpcode == 332) {
2991
1
    if(data->set.str[STRING_FTP_ACCOUNT]) {
2992
0
      result = Curl_pp_sendf(data, &ftpc->pp, "ACCT %s",
2993
0
                             data->set.str[STRING_FTP_ACCOUNT]);
2994
0
      if(!result)
2995
0
        ftp_state(data, ftpc, FTP_ACCT);
2996
0
    }
2997
1
    else {
2998
1
      failf(data, "ACCT requested but none available");
2999
1
      result = CURLE_LOGIN_DENIED;
3000
1
    }
3001
1
  }
3002
14
  else {
3003
    /* All other response codes, like:
3004
3005
    530 User ... access denied
3006
    (the server denies to log the specified user) */
3007
3008
14
    if(data->set.str[STRING_FTP_ALTERNATIVE_TO_USER] &&
3009
7
       !ftpc->ftp_trying_alternative) {
3010
      /* Ok, USER failed. Let's try the supplied command. */
3011
4
      result = Curl_pp_sendf(data, &ftpc->pp, "%s",
3012
4
                             data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]);
3013
4
      if(!result) {
3014
4
        ftpc->ftp_trying_alternative = TRUE;
3015
4
        ftp_state(data, ftpc, FTP_USER);
3016
4
      }
3017
4
    }
3018
10
    else {
3019
10
      failf(data, "Access denied: %03d", ftpcode);
3020
10
      result = CURLE_LOGIN_DENIED;
3021
10
    }
3022
14
  }
3023
1.54k
  return result;
3024
1.54k
}
3025
3026
/* for ACCT response */
3027
static CURLcode ftp_state_acct_resp(struct Curl_easy *data,
3028
                                    struct ftp_conn *ftpc,
3029
                                    int ftpcode)
3030
0
{
3031
0
  CURLcode result = CURLE_OK;
3032
0
  if(ftpcode != 230) {
3033
0
    failf(data, "ACCT rejected by server: %03d", ftpcode);
3034
0
    result = CURLE_FTP_WEIRD_PASS_REPLY; /* FIX */
3035
0
  }
3036
0
  else
3037
0
    result = ftp_state_loggedin(data, ftpc);
3038
3039
0
  return result;
3040
0
}
3041
3042
static CURLcode ftp_pwd_resp(struct Curl_easy *data,
3043
                             struct ftp_conn *ftpc,
3044
                             int ftpcode)
3045
1.51k
{
3046
1.51k
  struct pingpong *pp = &ftpc->pp;
3047
1.51k
  CURLcode result;
3048
3049
1.51k
  if(ftpcode == 257) {
3050
87
    const char *ptr = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first
3051
                                                          letter */
3052
87
    bool entry_extracted = FALSE;
3053
87
    struct dynbuf out;
3054
87
    curlx_dyn_init(&out, 1000);
3055
3056
    /* Reply format is like
3057
       257<space>[rubbish]"<directory-name>"<space><commentary> and the
3058
       RFC959 says
3059
3060
       The directory name can contain any character; embedded
3061
       double-quotes should be escaped by double-quotes (the
3062
       "quote-doubling" convention).
3063
    */
3064
3065
    /* scan for the first double-quote for non-standard responses */
3066
329
    while(*ptr != '\n' && *ptr != '\0' && *ptr != '"')
3067
242
      ptr++;
3068
3069
87
    if('\"' == *ptr) {
3070
      /* it started good */
3071
2.46k
      for(ptr++; *ptr; ptr++) {
3072
2.44k
        if('\"' == *ptr) {
3073
1.14k
          if('\"' == ptr[1]) {
3074
            /* "quote-doubling" */
3075
1.11k
            result = curlx_dyn_addn(&out, &ptr[1], 1);
3076
1.11k
            ptr++;
3077
1.11k
          }
3078
22
          else {
3079
            /* end of path */
3080
22
            if(curlx_dyn_len(&out))
3081
21
              entry_extracted = TRUE;
3082
22
            break; /* get out of this loop */
3083
22
          }
3084
1.14k
        }
3085
1.30k
        else
3086
1.30k
          result = curlx_dyn_addn(&out, ptr, 1);
3087
2.41k
        if(result)
3088
1
          return result;
3089
2.41k
      }
3090
43
    }
3091
86
    if(entry_extracted) {
3092
      /* If the path name does not look like an absolute path (i.e.: it
3093
         does not start with a '/'), we probably need some server-dependent
3094
         adjustments. For example, this is the case when connecting to
3095
         an OS400 FTP server: this server supports two name syntaxes,
3096
         the default one being incompatible with standard paths. In
3097
         addition, this server switches automatically to the regular path
3098
         syntax when one is encountered in a command: this results in
3099
         having an entrypath in the wrong syntax when later used in CWD.
3100
         The method used here is to check the server OS: we do it only
3101
         if the path name looks strange to minimize overhead on other
3102
         systems. */
3103
21
      char *dir = curlx_dyn_ptr(&out);
3104
3105
21
      if(!ftpc->server_os && dir[0] != '/') {
3106
18
        result = Curl_pp_sendf(data, &ftpc->pp, "%s", "SYST");
3107
18
        if(result) {
3108
0
          curlx_dyn_free(&out);
3109
0
          return result;
3110
0
        }
3111
18
      }
3112
3113
21
      curlx_free(ftpc->entrypath);
3114
21
      ftpc->entrypath = dir; /* remember this */
3115
21
      infof(data, "Entry path is '%s'", ftpc->entrypath);
3116
      /* also save it where getinfo can access it: */
3117
21
      curlx_free(data->state.most_recent_ftp_entrypath);
3118
21
      data->state.most_recent_ftp_entrypath = curlx_strdup(ftpc->entrypath);
3119
21
      if(!data->state.most_recent_ftp_entrypath)
3120
0
        return CURLE_OUT_OF_MEMORY;
3121
3122
21
      if(!ftpc->server_os && dir[0] != '/') {
3123
18
        ftp_state(data, ftpc, FTP_SYST);
3124
18
        return CURLE_OK;
3125
18
      }
3126
21
    }
3127
65
    else {
3128
      /* could not get the path */
3129
65
      curlx_dyn_free(&out);
3130
65
      infof(data, "Failed to figure out path");
3131
65
    }
3132
86
  }
3133
1.49k
  ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
3134
1.49k
  CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
3135
1.49k
  return CURLE_OK;
3136
1.51k
}
3137
3138
static const char * const ftpauth[] = { "SSL", "TLS" };
3139
3140
static CURLcode ftp_wait_resp(struct Curl_easy *data,
3141
                              struct connectdata *conn,
3142
                              struct ftp_conn *ftpc,
3143
                              int ftpcode)
3144
1.56k
{
3145
1.56k
  CURLcode result = CURLE_OK;
3146
1.56k
  if(ftpcode == 230) {
3147
    /* 230 User logged in - already! Take as 220 if TLS required. */
3148
1.42k
    if(data->set.use_ssl <= CURLUSESSL_TRY ||
3149
4
       conn->bits.ftp_use_control_ssl)
3150
1.42k
      return ftp_state_user_resp(data, ftpc, ftpcode);
3151
1.42k
  }
3152
133
  else if(ftpcode != 220) {
3153
13
    failf(data, "Got a %03d ftp-server response when 220 was expected",
3154
13
          ftpcode);
3155
13
    return CURLE_WEIRD_SERVER_REPLY;
3156
13
  }
3157
3158
124
  if(data->set.use_ssl && !conn->bits.ftp_use_control_ssl) {
3159
    /* We do not have an SSL/TLS control connection yet, but FTPS is
3160
       requested. Try an FTPS connection now */
3161
3162
6
    ftpc->count3 = 0;
3163
6
    switch((long)data->set.ftpsslauth) {
3164
6
    case CURLFTPAUTH_DEFAULT:
3165
6
    case CURLFTPAUTH_SSL:
3166
6
      ftpc->count2 = 1; /* add one to get next */
3167
6
      ftpc->count1 = 0;
3168
6
      break;
3169
0
    case CURLFTPAUTH_TLS:
3170
0
      ftpc->count2 = -1; /* subtract one to get next */
3171
0
      ftpc->count1 = 1;
3172
0
      break;
3173
0
    default:
3174
0
      failf(data, "unsupported parameter to CURLOPT_FTPSSLAUTH: %d",
3175
0
            (int)data->set.ftpsslauth);
3176
0
      return CURLE_UNKNOWN_OPTION; /* we do not know what to do */
3177
6
    }
3178
6
    result = Curl_pp_sendf(data, &ftpc->pp, "AUTH %s", ftpauth[ftpc->count1]);
3179
6
    if(!result)
3180
6
      ftp_state(data, ftpc, FTP_AUTH);
3181
6
  }
3182
118
  else
3183
118
    result = ftp_state_user(data, ftpc, conn);
3184
124
  return result;
3185
124
}
3186
3187
static CURLcode ftp_pp_statemachine(struct Curl_easy *data,
3188
                                    struct connectdata *conn)
3189
13.9k
{
3190
13.9k
  CURLcode result;
3191
13.9k
  int ftpcode;
3192
13.9k
  struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
3193
13.9k
  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
3194
13.9k
  struct pingpong *pp;
3195
13.9k
  size_t nread = 0;
3196
3197
13.9k
  if(!ftpc || !ftp)
3198
847
    return CURLE_FAILED_INIT;
3199
13.0k
  pp = &ftpc->pp;
3200
13.0k
  if(pp->sendleft)
3201
0
    return Curl_pp_flushsend(data, pp);
3202
3203
13.0k
  result = ftp_readresp(data, ftpc, FIRSTSOCKET, pp, &ftpcode, &nread);
3204
13.0k
  if(result || !ftpcode)
3205
5.59k
    return result;
3206
3207
  /* we have now received a full FTP server response */
3208
7.46k
  switch(ftpc->state) {
3209
1.56k
  case FTP_WAIT220:
3210
1.56k
    result = ftp_wait_resp(data, conn, ftpc, ftpcode);
3211
1.56k
    break;
3212
3213
8
  case FTP_AUTH:
3214
    /* we have gotten the response to a previous AUTH command */
3215
3216
8
    if(pp->overflow)
3217
1
      return CURLE_WEIRD_SERVER_REPLY; /* Forbid pipelining in response. */
3218
3219
    /* RFC2228 (page 5) says:
3220
     *
3221
     * If the server is willing to accept the named security mechanism,
3222
     * and does not require any security data, it must respond with
3223
     * reply code 234/334.
3224
     */
3225
3226
7
    if((ftpcode == 234) || (ftpcode == 334)) {
3227
      /* this was BLOCKING, keep it so for now */
3228
2
      bool done;
3229
2
      if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
3230
2
        result = Curl_ssl_cfilter_add(data, conn, FIRSTSOCKET);
3231
2
        if(result) {
3232
          /* we failed and bail out */
3233
0
          return CURLE_USE_SSL_FAILED;
3234
0
        }
3235
2
      }
3236
2
      result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, &done);
3237
2
      if(!result) {
3238
0
        conn->bits.ftp_use_data_ssl = FALSE; /* clear-text data */
3239
0
        conn->bits.ftp_use_control_ssl = TRUE; /* SSL on control */
3240
0
        result = ftp_state_user(data, ftpc, conn);
3241
0
      }
3242
2
    }
3243
5
    else if(ftpc->count3 < 1) {
3244
3
      ftpc->count3++;
3245
3
      ftpc->count1 += ftpc->count2; /* get next attempt */
3246
3
      result = Curl_pp_sendf(data, &ftpc->pp, "AUTH %s",
3247
3
                             ftpauth[ftpc->count1]);
3248
      /* remain in this same state */
3249
3
    }
3250
2
    else {
3251
2
      if(data->set.use_ssl > CURLUSESSL_TRY)
3252
        /* we failed and CURLUSESSL_CONTROL or CURLUSESSL_ALL is set */
3253
1
        result = CURLE_USE_SSL_FAILED;
3254
1
      else
3255
        /* ignore the failure and continue */
3256
1
        result = ftp_state_user(data, ftpc, conn);
3257
2
    }
3258
7
    break;
3259
3260
117
  case FTP_USER:
3261
121
  case FTP_PASS:
3262
121
    result = ftp_state_user_resp(data, ftpc, ftpcode);
3263
121
    break;
3264
3265
0
  case FTP_ACCT:
3266
0
    result = ftp_state_acct_resp(data, ftpc, ftpcode);
3267
0
    break;
3268
3269
0
  case FTP_PBSZ:
3270
0
    result =
3271
0
      Curl_pp_sendf(data, &ftpc->pp, "PROT %c",
3272
0
                    data->set.use_ssl == CURLUSESSL_CONTROL ? 'C' : 'P');
3273
0
    if(!result)
3274
0
      ftp_state(data, ftpc, FTP_PROT);
3275
0
    break;
3276
3277
0
  case FTP_PROT:
3278
0
    if(ftpcode / 100 == 2)
3279
      /* We have enabled SSL for the data connection! */
3280
0
      conn->bits.ftp_use_data_ssl = (data->set.use_ssl != CURLUSESSL_CONTROL);
3281
    /* FTP servers typically responds with 500 if they decide to reject
3282
       our 'P' request */
3283
0
    else if(data->set.use_ssl > CURLUSESSL_CONTROL)
3284
      /* we failed and bails out */
3285
0
      return CURLE_USE_SSL_FAILED;
3286
3287
0
    if(data->set.ftp_ccc) {
3288
      /* CCC - Clear Command Channel
3289
       */
3290
0
      result = Curl_pp_sendf(data, &ftpc->pp, "%s", "CCC");
3291
0
      if(!result)
3292
0
        ftp_state(data, ftpc, FTP_CCC);
3293
0
    }
3294
0
    else
3295
0
      result = ftp_state_pwd(data, ftpc);
3296
0
    break;
3297
3298
0
  case FTP_CCC:
3299
0
    if(ftpcode < 500) {
3300
      /* First shut down the SSL layer (note: this call will block) */
3301
      /* This has only been tested on the proftpd server, and the mod_tls
3302
       * code sends a close notify alert without waiting for a close notify
3303
       * alert in response. Thus we wait for a close notify alert from the
3304
       * server, but we do not send one. Let's hope other servers do
3305
       * the same... */
3306
0
      result = Curl_ssl_cfilter_remove(data, FIRSTSOCKET,
3307
0
                                       (data->set.ftp_ccc ==
3308
0
                                        (unsigned char)CURLFTPSSL_CCC_ACTIVE));
3309
0
      if(result)
3310
0
        failf(data, "Failed to clear the command channel (CCC)");
3311
0
    }
3312
0
    if(!result)
3313
      /* Then continue as normal */
3314
0
      result = ftp_state_pwd(data, ftpc);
3315
0
    break;
3316
3317
1.51k
  case FTP_PWD:
3318
1.51k
    result = ftp_pwd_resp(data, ftpc, ftpcode);
3319
1.51k
    break;
3320
3321
15
  case FTP_SYST:
3322
15
    if(ftpcode == 215) {
3323
13
      const char *ptr = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first
3324
                                                            letter */
3325
13
      const char *start;
3326
13
      char *os;
3327
3328
      /* Reply format is like
3329
         215<space><OS-name><space><commentary>
3330
      */
3331
99
      while(*ptr == ' ')
3332
86
        ptr++;
3333
256
      for(start = ptr; *ptr && *ptr != ' '; ptr++)
3334
243
        ;
3335
13
      os = curlx_memdup0(start, ptr - start);
3336
13
      if(!os)
3337
0
        return CURLE_OUT_OF_MEMORY;
3338
3339
      /* Check for special servers here. */
3340
13
      if(curl_strequal(os, "OS/400")) {
3341
        /* Force OS400 name format 1. */
3342
0
        result = Curl_pp_sendf(data, &ftpc->pp, "%s", "SITE NAMEFMT 1");
3343
0
        if(result) {
3344
0
          curlx_free(os);
3345
0
          return result;
3346
0
        }
3347
        /* remember target server OS */
3348
0
        curlx_free(ftpc->server_os);
3349
0
        ftpc->server_os = os;
3350
0
        ftp_state(data, ftpc, FTP_NAMEFMT);
3351
0
        break;
3352
0
      }
3353
      /* Nothing special for the target server. */
3354
      /* remember target server OS */
3355
13
      curlx_free(ftpc->server_os);
3356
13
      ftpc->server_os = os;
3357
13
    }
3358
2
    else {
3359
      /* Cannot identify server OS. Continue anyway and cross fingers. */
3360
2
    }
3361
3362
15
    ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
3363
15
    CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
3364
15
    break;
3365
3366
0
  case FTP_NAMEFMT:
3367
0
    if(ftpcode == 250) {
3368
      /* Name format change successful: reload initial path. */
3369
0
      ftp_state_pwd(data, ftpc);
3370
0
      break;
3371
0
    }
3372
3373
0
    ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
3374
0
    CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
3375
0
    break;
3376
3377
0
  case FTP_QUOTE:
3378
0
  case FTP_POSTQUOTE:
3379
0
  case FTP_RETR_PREQUOTE:
3380
0
  case FTP_STOR_PREQUOTE:
3381
0
  case FTP_LIST_PREQUOTE:
3382
0
    if((ftpcode >= 400) && !ftpc->count2) {
3383
      /* failure response code, and not allowed to fail */
3384
0
      failf(data, "QUOT command failed with %03d", ftpcode);
3385
0
      result = CURLE_QUOTE_ERROR;
3386
0
    }
3387
0
    else
3388
0
      result = ftp_state_quote(data, ftpc, ftp, FALSE, ftpc->state);
3389
0
    break;
3390
3391
142
  case FTP_CWD:
3392
142
    if(ftpcode / 100 != 2) {
3393
      /* failure to CWD there */
3394
26
      if(data->set.ftp_create_missing_dirs &&
3395
19
         ftpc->cwdcount && !ftpc->count2) {
3396
        /* try making it */
3397
18
        ftpc->count2++; /* counter to prevent CWD-MKD loops */
3398
3399
        /* count3 is set to allow MKD to fail once per dir. In the case when
3400
           CWD fails and then MKD fails (due to another session raced it to
3401
           create the dir) this then allows for a second try to CWD to it. */
3402
18
        ftpc->count3 = (data->set.ftp_create_missing_dirs == 2) ? 1 : 0;
3403
3404
18
        result = Curl_pp_sendf(data, &ftpc->pp, "MKD %.*s",
3405
18
                               pathlen(ftpc, ftpc->cwdcount - 1),
3406
18
                               pathpiece(ftpc, ftpc->cwdcount - 1));
3407
18
        if(!result)
3408
18
          ftp_state(data, ftpc, FTP_MKD);
3409
18
      }
3410
8
      else {
3411
        /* return failure */
3412
8
        failf(data, "Server denied you to change to the given directory");
3413
8
        ftpc->cwdfail = TRUE; /* do not remember this path as we failed
3414
                                 to enter it */
3415
8
        result = CURLE_REMOTE_ACCESS_DENIED;
3416
8
      }
3417
26
    }
3418
116
    else {
3419
      /* success */
3420
116
      ftpc->count2 = 0;
3421
116
      if(ftpc->cwdcount >= ftpc->dirdepth)
3422
95
        result = ftp_state_mdtm(data, ftpc, ftp);
3423
21
      else {
3424
21
        ftpc->cwdcount++;
3425
        /* send next CWD */
3426
21
        result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s",
3427
21
                               pathlen(ftpc, ftpc->cwdcount - 1),
3428
21
                               pathpiece(ftpc, ftpc->cwdcount - 1));
3429
21
      }
3430
116
    }
3431
142
    break;
3432
3433
16
  case FTP_MKD:
3434
16
    if((ftpcode / 100 != 2) && !ftpc->count3--) {
3435
      /* failure to MKD the directory */
3436
1
      failf(data, "Failed to MKD dir: %03d", ftpcode);
3437
1
      result = CURLE_REMOTE_ACCESS_DENIED;
3438
1
    }
3439
15
    else {
3440
15
      ftp_state(data, ftpc, FTP_CWD);
3441
      /* send CWD */
3442
15
      result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s",
3443
15
                             pathlen(ftpc, ftpc->cwdcount - 1),
3444
15
                             pathpiece(ftpc, ftpc->cwdcount - 1));
3445
15
    }
3446
16
    break;
3447
3448
78
  case FTP_MDTM:
3449
78
    result = ftp_state_mdtm_resp(data, ftpc, ftp, ftpcode);
3450
78
    break;
3451
3452
37
  case FTP_TYPE:
3453
514
  case FTP_LIST_TYPE:
3454
1.01k
  case FTP_RETR_TYPE:
3455
1.06k
  case FTP_STOR_TYPE:
3456
1.06k
  case FTP_RETR_LIST_TYPE:
3457
1.06k
    result = ftp_state_type_resp(data, ftpc, ftp, ftpcode, ftpc->state);
3458
1.06k
    break;
3459
3460
33
  case FTP_SIZE:
3461
497
  case FTP_RETR_SIZE:
3462
497
  case FTP_STOR_SIZE:
3463
497
    result = ftp_state_size_resp(data, ftpc, ftp, ftpcode, ftpc->state);
3464
497
    break;
3465
3466
4
  case FTP_REST:
3467
18
  case FTP_RETR_REST:
3468
18
    result = ftp_state_rest_resp(data, ftpc, ftp, ftpcode, ftpc->state);
3469
18
    break;
3470
3471
2
  case FTP_PRET:
3472
2
    if(ftpcode != 200) {
3473
      /* there only is this one standard OK return code. */
3474
1
      failf(data, "PRET command not accepted: %03d", ftpcode);
3475
1
      return CURLE_FTP_PRET_FAILED;
3476
1
    }
3477
1
    result = ftp_state_use_pasv(data, ftpc, conn);
3478
1
    break;
3479
3480
1.75k
  case FTP_PASV:
3481
1.75k
    result = ftp_state_pasv_resp(data, ftpc, ftpcode);
3482
1.75k
    break;
3483
3484
0
  case FTP_PORT:
3485
0
    result = ftp_state_port_resp(data, ftpc, ftp, ftpcode);
3486
0
    break;
3487
3488
431
  case FTP_LIST:
3489
618
  case FTP_RETR:
3490
618
    result = ftp_state_get_resp(data, ftpc, ftp, ftpcode, ftpc->state);
3491
618
    break;
3492
3493
24
  case FTP_STOR:
3494
24
    result = ftp_state_stor_resp(data, ftpc, ftpcode);
3495
24
    break;
3496
3497
0
  case FTP_QUIT:
3498
28
  default:
3499
    /* internal error */
3500
28
    ftp_state(data, ftpc, FTP_STOP);
3501
28
    break;
3502
7.46k
  }
3503
3504
7.46k
  return result;
3505
7.46k
}
3506
3507
/* called repeatedly until done from multi.c */
3508
static CURLcode ftp_multi_statemach(struct Curl_easy *data,
3509
                                    bool *done)
3510
4.53k
{
3511
4.53k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
3512
4.53k
  return ftpc ? ftp_statemach(data, ftpc, done) : CURLE_FAILED_INIT;
3513
4.53k
}
3514
3515
static CURLcode ftp_block_statemach(struct Curl_easy *data,
3516
                                    struct ftp_conn *ftpc)
3517
853
{
3518
853
  struct pingpong *pp = &ftpc->pp;
3519
853
  CURLcode result = CURLE_OK;
3520
3521
853
  while(ftpc->state != FTP_STOP) {
3522
853
    if(ftpc->shutdown)
3523
853
      CURL_TRC_FTP(data, "in shutdown, waiting for server response");
3524
853
    result = Curl_pp_statemach(data, pp, TRUE, TRUE /* disconnecting */);
3525
853
    if(result)
3526
853
      break;
3527
853
  }
3528
3529
853
  return result;
3530
853
}
3531
3532
/*
3533
 * ftp_connect() should do everything that is to be considered a part of
3534
 * the connection phase.
3535
 *
3536
 * The variable 'done' points to will be TRUE if the protocol-layer connect
3537
 * phase is done when this function returns, or FALSE if not.
3538
 *
3539
 */
3540
static CURLcode ftp_connect(struct Curl_easy *data,
3541
                            bool *done) /* see description above */
3542
1.97k
{
3543
1.97k
  CURLcode result;
3544
1.97k
  struct connectdata *conn = data->conn;
3545
1.97k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
3546
1.97k
  struct pingpong *pp;
3547
3548
1.97k
  *done = FALSE; /* default to not done yet */
3549
1.97k
  if(!ftpc)
3550
0
    return CURLE_FAILED_INIT;
3551
1.97k
  pp = &ftpc->pp;
3552
1.97k
  PINGPONG_SETUP(pp, ftp_pp_statemachine, ftp_endofresp);
3553
3554
1.97k
  if(Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
3555
    /* BLOCKING */
3556
0
    result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done);
3557
0
    if(result)
3558
0
      return result;
3559
0
    conn->bits.ftp_use_control_ssl = TRUE;
3560
0
  }
3561
3562
1.97k
  Curl_pp_init(pp, Curl_pgrs_now(data)); /* once per transfer */
3563
3564
  /* When we connect, we start in the state where we await the 220
3565
     response */
3566
1.97k
  ftp_state(data, ftpc, FTP_WAIT220);
3567
3568
1.97k
  result = ftp_statemach(data, ftpc, done);
3569
3570
1.97k
  return result;
3571
1.97k
}
3572
3573
/***********************************************************************
3574
 *
3575
 * ftp_sendquote()
3576
 *
3577
 * Where a 'quote' means a list of custom commands to send to the server.
3578
 * The quote list is passed as an argument.
3579
 *
3580
 * BLOCKING
3581
 */
3582
static CURLcode ftp_sendquote(struct Curl_easy *data,
3583
                              struct ftp_conn *ftpc,
3584
                              struct curl_slist *quote)
3585
0
{
3586
0
  struct curl_slist *item;
3587
0
  struct pingpong *pp = &ftpc->pp;
3588
3589
0
  item = quote;
3590
0
  while(item) {
3591
0
    if(item->data) {
3592
0
      size_t nread;
3593
0
      const char *cmd = item->data;
3594
0
      bool acceptfail = FALSE;
3595
0
      CURLcode result;
3596
0
      int ftpcode = 0;
3597
3598
      /* if a command starts with an asterisk, which a legal FTP command never
3599
         can, the command will be allowed to fail without it causing any
3600
         aborts or cancels etc. It will cause libcurl to act as if the command
3601
         is successful, whatever the server responds. */
3602
3603
0
      if(cmd[0] == '*') {
3604
0
        cmd++;
3605
0
        acceptfail = TRUE;
3606
0
      }
3607
3608
0
      result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd);
3609
0
      if(!result) {
3610
0
        pp->response = *Curl_pgrs_now(data); /* timeout relative now */
3611
0
        result = getftpresponse(data, &nread, &ftpcode);
3612
0
      }
3613
0
      if(result)
3614
0
        return result;
3615
3616
0
      if(!acceptfail && (ftpcode >= 400)) {
3617
0
        failf(data, "QUOT string not accepted: %s", cmd);
3618
0
        return CURLE_QUOTE_ERROR;
3619
0
      }
3620
0
    }
3621
3622
0
    item = item->next;
3623
0
  }
3624
3625
0
  return CURLE_OK;
3626
0
}
3627
3628
/***********************************************************************
3629
 *
3630
 * ftp_done()
3631
 *
3632
 * The DONE function. This does what needs to be done after a single DO has
3633
 * performed.
3634
 *
3635
 * Input argument is already checked for validity.
3636
 */
3637
static CURLcode ftp_done(struct Curl_easy *data, CURLcode status,
3638
                         bool premature)
3639
1.98k
{
3640
1.98k
  struct connectdata *conn = data->conn;
3641
1.98k
  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
3642
1.98k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
3643
1.98k
  struct pingpong *pp;
3644
1.98k
  size_t nread;
3645
1.98k
  int ftpcode;
3646
1.98k
  CURLcode result = CURLE_OK;
3647
3648
1.98k
  if(!ftp || !ftpc)
3649
0
    return CURLE_OK;
3650
3651
1.98k
  pp = &ftpc->pp;
3652
1.98k
  switch(status) {
3653
56
  case CURLE_BAD_DOWNLOAD_RESUME:
3654
85
  case CURLE_FTP_WEIRD_PASV_REPLY:
3655
107
  case CURLE_FTP_PORT_FAILED:
3656
107
  case CURLE_FTP_ACCEPT_FAILED:
3657
107
  case CURLE_FTP_ACCEPT_TIMEOUT:
3658
180
  case CURLE_FTP_COULDNT_SET_TYPE:
3659
190
  case CURLE_FTP_COULDNT_RETR_FILE:
3660
314
  case CURLE_PARTIAL_FILE:
3661
315
  case CURLE_UPLOAD_FAILED:
3662
324
  case CURLE_REMOTE_ACCESS_DENIED:
3663
356
  case CURLE_FILESIZE_EXCEEDED:
3664
359
  case CURLE_REMOTE_FILE_NOT_FOUND:
3665
359
  case CURLE_WRITE_ERROR:
3666
    /* the connection stays alive fine even though this happened */
3667
1.06k
  case CURLE_OK: /* does not affect the control connection's status */
3668
1.06k
    if(!premature)
3669
900
      break;
3670
3671
    /* until we cope better with prematurely ended requests, let them
3672
     * fallback as if in complete failure */
3673
161
    FALLTHROUGH();
3674
1.08k
  default:       /* by default, an error means the control connection is
3675
                    wedged and should not be used anymore */
3676
1.08k
    ftpc->ctl_valid = FALSE;
3677
1.08k
    ftpc->cwdfail = TRUE; /* set this TRUE to prevent us to remember the
3678
                             current path, as this connection is going */
3679
1.08k
    connclose(conn, "FTP ended with bad error code");
3680
1.08k
    result = status;      /* use the already set error code */
3681
1.08k
    break;
3682
1.98k
  }
3683
3684
1.98k
  if(data->state.wildcardmatch) {
3685
487
    if(data->set.chunk_end && ftpc->file) {
3686
0
      Curl_set_in_callback(data, TRUE);
3687
0
      data->set.chunk_end(data->set.wildcardptr);
3688
0
      Curl_set_in_callback(data, FALSE);
3689
0
      freedirs(ftpc);
3690
0
    }
3691
487
    ftpc->known_filesize = -1;
3692
487
  }
3693
3694
1.98k
  if(result) {
3695
    /* We can limp along anyway (and should try to since we may already be in
3696
     * the error path) */
3697
1.05k
    ftpc->ctl_valid = FALSE; /* mark control connection as bad */
3698
1.05k
    connclose(conn, "FTP: out of memory!"); /* mark for connection closure */
3699
1.05k
    curlx_free(ftpc->prevpath);
3700
1.05k
    ftpc->prevpath = NULL; /* no path remembering */
3701
1.05k
  }
3702
937
  else { /* remember working directory for connection reuse */
3703
937
    const char *rawPath = ftpc->rawpath;
3704
937
    if(rawPath) {
3705
850
      if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/'))
3706
0
        ; /* full path => no CWDs happened => keep ftpc->prevpath */
3707
850
      else {
3708
850
        size_t pathLen = strlen(ftpc->rawpath);
3709
3710
850
        curlx_free(ftpc->prevpath);
3711
3712
850
        if(!ftpc->cwdfail) {
3713
829
          if(data->set.ftp_filemethod == FTPFILE_NOCWD)
3714
2
            pathLen = 0; /* relative path => working directory is FTP home */
3715
827
          else
3716
            /* file is url-decoded */
3717
827
            pathLen -= ftpc->file ? strlen(ftpc->file) : 0;
3718
829
          ftpc->prevpath = curlx_memdup0(rawPath, pathLen);
3719
829
        }
3720
21
        else
3721
21
          ftpc->prevpath = NULL; /* no path */
3722
850
      }
3723
850
    }
3724
937
    if(ftpc->prevpath)
3725
829
      infof(data, "Remembering we are in directory \"%s\"", ftpc->prevpath);
3726
937
  }
3727
3728
  /* shut down the socket to inform the server we are done */
3729
3730
1.98k
  if(Curl_conn_is_setup(conn, SECONDARYSOCKET)) {
3731
1.09k
    if(!result && ftpc->dont_check && data->req.maxdownload > 0) {
3732
      /* partial download completed */
3733
147
      result = Curl_pp_sendf(data, pp, "%s", "ABOR");
3734
147
      if(result) {
3735
0
        failf(data, "Failure sending ABOR command: %s",
3736
0
              curl_easy_strerror(result));
3737
0
        ftpc->ctl_valid = FALSE; /* mark control connection as bad */
3738
0
        connclose(conn, "ABOR command failed"); /* connection closure */
3739
0
      }
3740
147
    }
3741
3742
1.09k
    close_secondarysocket(data, ftpc);
3743
1.09k
  }
3744
3745
1.98k
  if(!result && (ftp->transfer == PPTRANSFER_BODY) && ftpc->ctl_valid &&
3746
774
     pp->pending_resp && !premature) {
3747
    /*
3748
     * Let's see what the server says about the transfer we just performed,
3749
     * but lower the timeout as sometimes this connection has died while the
3750
     * data has been transferred. This happens when doing through NATs etc that
3751
     * abandon old silent connections.
3752
     */
3753
604
    pp->response = *Curl_pgrs_now(data); /* timeout relative now */
3754
604
    result = getftpresponse(data, &nread, &ftpcode);
3755
3756
604
    if(!nread && (result == CURLE_OPERATION_TIMEDOUT)) {
3757
5
      failf(data, "control connection looks dead");
3758
5
      ftpc->ctl_valid = FALSE; /* mark control connection as bad */
3759
5
      connclose(conn, "Timeout or similar in FTP DONE operation"); /* close */
3760
5
    }
3761
3762
604
    if(result)
3763
511
      return result;
3764
3765
93
    if(ftpc->dont_check && data->req.maxdownload > 0) {
3766
      /* we have just sent ABOR and there is no reliable way to check if it was
3767
       * successful or not; we have to close the connection now */
3768
51
      infof(data, "partial download completed, closing connection");
3769
51
      connclose(conn, "Partial download with no ability to check");
3770
51
      return result;
3771
51
    }
3772
3773
42
    if(!ftpc->dont_check) {
3774
      /* 226 Transfer complete, 250 Requested file action okay, completed. */
3775
42
      switch(ftpcode) {
3776
17
      case 226:
3777
20
      case 250:
3778
20
        break;
3779
1
      case 552:
3780
1
        failf(data, "Exceeded storage allocation");
3781
1
        result = CURLE_REMOTE_DISK_FULL;
3782
1
        break;
3783
21
      default:
3784
21
        failf(data, "server did not report OK, got %d", ftpcode);
3785
21
        result = CURLE_PARTIAL_FILE;
3786
21
        break;
3787
42
      }
3788
42
    }
3789
42
  }
3790
3791
1.42k
  if(result || premature)
3792
    /* the response code from the transfer showed an error already so no
3793
       use checking further */
3794
1.11k
    ;
3795
316
  else if(data->state.upload) {
3796
34
    if((ftp->transfer == PPTRANSFER_BODY) &&
3797
8
       (data->state.infilesize != -1) && /* upload with known size */
3798
8
       ((!data->set.crlf && !data->state.prefer_ascii && /* no conversion */
3799
6
         (data->state.infilesize != data->req.writebytecount)) ||
3800
5
        ((data->set.crlf || data->state.prefer_ascii) && /* maybe crlf conv */
3801
2
         (data->state.infilesize > data->req.writebytecount))
3802
8
       )) {
3803
4
      failf(data, "Uploaded unaligned file size (%" FMT_OFF_T
3804
4
            " out of %" FMT_OFF_T " bytes)",
3805
4
            data->req.writebytecount, data->state.infilesize);
3806
4
      result = CURLE_PARTIAL_FILE;
3807
4
    }
3808
34
  }
3809
282
  else {
3810
282
    if((data->req.size != -1) &&
3811
0
       (data->req.size != data->req.bytecount) &&
3812
0
       (data->req.maxdownload != data->req.bytecount)) {
3813
0
      failf(data, "Received only partial file: %" FMT_OFF_T " bytes",
3814
0
            data->req.bytecount);
3815
0
      result = CURLE_PARTIAL_FILE;
3816
0
    }
3817
282
    else if(!ftpc->dont_check &&
3818
281
            !data->req.bytecount &&
3819
267
            (data->req.size > 0)) {
3820
0
      failf(data, "No data was received");
3821
0
      result = CURLE_FTP_COULDNT_RETR_FILE;
3822
0
    }
3823
282
  }
3824
3825
  /* clear these for next connection */
3826
1.42k
  ftp->transfer = PPTRANSFER_BODY;
3827
1.42k
  ftpc->dont_check = FALSE;
3828
3829
  /* Send any post-transfer QUOTE strings? */
3830
1.42k
  if(!status && !result && !premature && data->set.postquote)
3831
0
    result = ftp_sendquote(data, ftpc, data->set.postquote);
3832
1.42k
  CURL_TRC_FTP(data, "[%s] done, result=%d", FTP_CSTATE(ftpc), result);
3833
1.42k
  return result;
3834
1.98k
}
3835
3836
/***********************************************************************
3837
 *
3838
 * ftp_nb_type()
3839
 *
3840
 * Set TYPE. We only deal with ASCII or BINARY so this function
3841
 * sets one of them.
3842
 * If the transfer type is not sent, simulate on OK response in newstate
3843
 */
3844
static CURLcode ftp_nb_type(struct Curl_easy *data,
3845
                            struct ftp_conn *ftpc,
3846
                            struct FTP *ftp,
3847
                            bool ascii, ftpstate newstate)
3848
1.08k
{
3849
1.08k
  CURLcode result;
3850
1.08k
  char want = (char)(ascii ? 'A' : 'I');
3851
3852
1.08k
  if(ftpc->transfertype == want) {
3853
0
    ftp_state(data, ftpc, newstate);
3854
0
    return ftp_state_type_resp(data, ftpc, ftp, 200, newstate);
3855
0
  }
3856
3857
1.08k
  result = Curl_pp_sendf(data, &ftpc->pp, "TYPE %c", want);
3858
1.08k
  if(!result) {
3859
1.08k
    ftp_state(data, ftpc, newstate);
3860
3861
    /* keep track of our current transfer type */
3862
1.08k
    ftpc->transfertype = want;
3863
1.08k
  }
3864
1.08k
  return result;
3865
1.08k
}
3866
3867
/***********************************************************************
3868
 *
3869
 * ftp_perform()
3870
 *
3871
 * This is the actual DO function for FTP. Get a file/directory according to
3872
 * the options previously setup.
3873
 */
3874
static
3875
CURLcode ftp_perform(struct Curl_easy *data,
3876
                     struct ftp_conn *ftpc,
3877
                     struct FTP *ftp,
3878
                     bool *connected,  /* connect status after PASV / PORT */
3879
                     bool *dophase_done)
3880
1.49k
{
3881
  /* this is FTP and no proxy */
3882
1.49k
  CURLcode result = CURLE_OK;
3883
3884
1.49k
  CURL_TRC_FTP(data, "[%s] DO phase starts", FTP_CSTATE(ftpc));
3885
3886
1.49k
  if(data->req.no_body) {
3887
    /* requested no body means no transfer... */
3888
99
    ftp->transfer = PPTRANSFER_INFO;
3889
99
  }
3890
3891
1.49k
  *dophase_done = FALSE; /* not done yet */
3892
3893
  /* start the first command in the DO phase */
3894
1.49k
  result = ftp_state_quote(data, ftpc, ftp, TRUE, FTP_QUOTE);
3895
1.49k
  if(result)
3896
19
    return result;
3897
3898
  /* run the state-machine */
3899
1.47k
  result = ftp_statemach(data, ftpc, dophase_done);
3900
3901
1.47k
  *connected = Curl_conn_is_connected(data->conn, SECONDARYSOCKET);
3902
3903
1.47k
  if(*connected)
3904
0
    infof(data, "[FTP] [%s] perform, DATA connection established",
3905
1.47k
          FTP_CSTATE(ftpc));
3906
1.47k
  else
3907
1.47k
    CURL_TRC_FTP(data, "[%s] perform, awaiting DATA connect",
3908
1.47k
                 FTP_CSTATE(ftpc));
3909
3910
1.47k
  if(*dophase_done)
3911
642
    CURL_TRC_FTP(data, "[%s] DO phase is complete1", FTP_CSTATE(ftpc));
3912
3913
1.47k
  return result;
3914
1.49k
}
3915
3916
static void wc_data_dtor(void *ptr)
3917
342
{
3918
342
  struct ftp_wc *ftpwc = ptr;
3919
342
  if(ftpwc && ftpwc->parser)
3920
342
    Curl_ftp_parselist_data_free(&ftpwc->parser);
3921
342
  curlx_free(ftpwc);
3922
342
}
3923
3924
static CURLcode init_wc_data(struct Curl_easy *data,
3925
                             struct ftp_conn *ftpc,
3926
                             struct FTP *ftp)
3927
394
{
3928
394
  char *last_slash;
3929
394
  char *path = ftp->path;
3930
394
  struct WildcardData *wildcard = data->wildcard;
3931
394
  CURLcode result = CURLE_OK;
3932
394
  struct ftp_wc *ftpwc = NULL;
3933
3934
394
  last_slash = strrchr(ftp->path, '/');
3935
394
  if(last_slash) {
3936
58
    last_slash++;
3937
58
    if(last_slash[0] == '\0') {
3938
3
      wildcard->state = CURLWC_CLEAN;
3939
3
      return ftp_parse_url_path(data, ftpc, ftp);
3940
3
    }
3941
55
    wildcard->pattern = curlx_strdup(last_slash);
3942
55
    if(!wildcard->pattern)
3943
0
      return CURLE_OUT_OF_MEMORY;
3944
55
    last_slash[0] = '\0'; /* cut file from path */
3945
55
  }
3946
336
  else { /* there is only 'wildcard pattern' or nothing */
3947
336
    if(path[0]) {
3948
288
      wildcard->pattern = curlx_strdup(path);
3949
288
      if(!wildcard->pattern)
3950
0
        return CURLE_OUT_OF_MEMORY;
3951
288
      path[0] = '\0';
3952
288
    }
3953
48
    else { /* only list */
3954
48
      wildcard->state = CURLWC_CLEAN;
3955
48
      return ftp_parse_url_path(data, ftpc, ftp);
3956
48
    }
3957
336
  }
3958
3959
  /* program continues only if URL is not ending with slash, allocate needed
3960
     resources for wildcard transfer */
3961
3962
  /* allocate ftp protocol specific wildcard data */
3963
343
  ftpwc = curlx_calloc(1, sizeof(struct ftp_wc));
3964
343
  if(!ftpwc) {
3965
0
    result = CURLE_OUT_OF_MEMORY;
3966
0
    goto fail;
3967
0
  }
3968
3969
  /* INITIALIZE parselist structure */
3970
343
  ftpwc->parser = Curl_ftp_parselist_data_alloc();
3971
343
  if(!ftpwc->parser) {
3972
0
    result = CURLE_OUT_OF_MEMORY;
3973
0
    goto fail;
3974
0
  }
3975
3976
343
  wildcard->ftpwc = ftpwc; /* put it to the WildcardData tmp pointer */
3977
343
  wildcard->dtor = wc_data_dtor;
3978
3979
  /* wildcard does not support NOCWD option (assert it?) */
3980
343
  if(data->set.ftp_filemethod == FTPFILE_NOCWD)
3981
1
    data->set.ftp_filemethod = FTPFILE_MULTICWD;
3982
3983
  /* try to parse ftp URL */
3984
343
  result = ftp_parse_url_path(data, ftpc, ftp);
3985
343
  if(result) {
3986
1
    goto fail;
3987
1
  }
3988
3989
342
  wildcard->path = curlx_strdup(ftp->path);
3990
342
  if(!wildcard->path) {
3991
0
    result = CURLE_OUT_OF_MEMORY;
3992
0
    goto fail;
3993
0
  }
3994
3995
  /* backup old write_function */
3996
342
  ftpwc->backup.write_function = data->set.fwrite_func;
3997
  /* parsing write function */
3998
342
  data->set.fwrite_func = Curl_ftp_parselist;
3999
  /* backup old file descriptor */
4000
342
  ftpwc->backup.file_descriptor = data->set.out;
4001
  /* let the writefunc callback know the transfer */
4002
342
  data->set.out = data;
4003
4004
342
  infof(data, "Wildcard - Parsing started");
4005
342
  return CURLE_OK;
4006
4007
1
fail:
4008
1
  if(ftpwc) {
4009
1
    Curl_ftp_parselist_data_free(&ftpwc->parser);
4010
1
    curlx_free(ftpwc);
4011
1
  }
4012
1
  Curl_safefree(wildcard->pattern);
4013
1
  wildcard->dtor = ZERO_NULL;
4014
1
  wildcard->ftpwc = NULL;
4015
1
  return result;
4016
342
}
4017
4018
static CURLcode wc_statemach(struct Curl_easy *data,
4019
                             struct ftp_conn *ftpc,
4020
                             struct FTP *ftp)
4021
410
{
4022
410
  struct WildcardData * const wildcard = data->wildcard;
4023
410
  CURLcode result = CURLE_OK;
4024
4025
452
  for(;;) {
4026
452
    switch(wildcard->state) {
4027
394
    case CURLWC_INIT:
4028
394
      result = init_wc_data(data, ftpc, ftp);
4029
394
      if(wildcard->state == CURLWC_CLEAN)
4030
        /* only listing! */
4031
51
        return result;
4032
343
      wildcard->state = result ? CURLWC_ERROR : CURLWC_MATCHING;
4033
343
      return result;
4034
4035
13
    case CURLWC_MATCHING: {
4036
      /* In this state is LIST response successfully parsed, so lets restore
4037
         previous WRITEFUNCTION callback and WRITEDATA pointer */
4038
13
      struct ftp_wc *ftpwc = wildcard->ftpwc;
4039
13
      data->set.fwrite_func = ftpwc->backup.write_function;
4040
13
      data->set.out = ftpwc->backup.file_descriptor;
4041
13
      ftpwc->backup.write_function = ZERO_NULL;
4042
13
      ftpwc->backup.file_descriptor = NULL;
4043
13
      wildcard->state = CURLWC_DOWNLOADING;
4044
4045
13
      if(Curl_ftp_parselist_geterror(ftpwc->parser)) {
4046
        /* error found in LIST parsing */
4047
3
        wildcard->state = CURLWC_CLEAN;
4048
3
        continue;
4049
3
      }
4050
10
      if(Curl_llist_count(&wildcard->filelist) == 0) {
4051
        /* no corresponding file */
4052
1
        wildcard->state = CURLWC_CLEAN;
4053
1
        return CURLE_REMOTE_FILE_NOT_FOUND;
4054
1
      }
4055
9
      continue;
4056
10
    }
4057
4058
23
    case CURLWC_DOWNLOADING: {
4059
      /* filelist has at least one file, lets get first one */
4060
23
      struct Curl_llist_node *head = Curl_llist_head(&wildcard->filelist);
4061
23
      struct curl_fileinfo *finfo = Curl_node_elem(head);
4062
4063
23
      char *tmp_path = curl_maprintf("%s%s", wildcard->path, finfo->filename);
4064
23
      if(!tmp_path)
4065
0
        return CURLE_OUT_OF_MEMORY;
4066
4067
      /* switch default ftp->path and tmp_path */
4068
23
      curlx_free(ftp->pathalloc);
4069
23
      ftp->pathalloc = ftp->path = tmp_path;
4070
4071
23
      infof(data, "Wildcard - START of \"%s\"", finfo->filename);
4072
23
      if(data->set.chunk_bgn) {
4073
0
        long userresponse;
4074
0
        Curl_set_in_callback(data, TRUE);
4075
0
        userresponse = data->set.chunk_bgn(
4076
0
          finfo, data->set.wildcardptr,
4077
0
          (int)Curl_llist_count(&wildcard->filelist));
4078
0
        Curl_set_in_callback(data, FALSE);
4079
0
        switch(userresponse) {
4080
0
        case CURL_CHUNK_BGN_FUNC_SKIP:
4081
0
          infof(data, "Wildcard - \"%s\" skipped by user", finfo->filename);
4082
0
          wildcard->state = CURLWC_SKIP;
4083
0
          continue;
4084
0
        case CURL_CHUNK_BGN_FUNC_FAIL:
4085
0
          return CURLE_CHUNK_FAILED;
4086
0
        }
4087
0
      }
4088
4089
23
      if(finfo->filetype != CURLFILETYPE_FILE) {
4090
15
        wildcard->state = CURLWC_SKIP;
4091
15
        continue;
4092
15
      }
4093
4094
8
      if(finfo->flags & CURLFINFOFLAG_KNOWN_SIZE)
4095
8
        ftpc->known_filesize = finfo->size;
4096
4097
8
      result = ftp_parse_url_path(data, ftpc, ftp);
4098
8
      if(result)
4099
1
        return result;
4100
4101
      /* we do not need the Curl_fileinfo of first file anymore */
4102
7
      Curl_node_remove(Curl_llist_head(&wildcard->filelist));
4103
4104
7
      if(Curl_llist_count(&wildcard->filelist) == 0) {
4105
        /* remains only one file to down. */
4106
5
        wildcard->state = CURLWC_CLEAN;
4107
        /* after that will be ftp_do called once again and no transfer
4108
           will be done because of CURLWC_CLEAN state */
4109
5
        return CURLE_OK;
4110
5
      }
4111
2
      return result;
4112
7
    }
4113
4114
15
    case CURLWC_SKIP: {
4115
15
      if(data->set.chunk_end) {
4116
0
        Curl_set_in_callback(data, TRUE);
4117
0
        data->set.chunk_end(data->set.wildcardptr);
4118
0
        Curl_set_in_callback(data, FALSE);
4119
0
      }
4120
15
      Curl_node_remove(Curl_llist_head(&wildcard->filelist));
4121
15
      wildcard->state = (Curl_llist_count(&wildcard->filelist) == 0) ?
4122
14
        CURLWC_CLEAN : CURLWC_DOWNLOADING;
4123
15
      continue;
4124
7
    }
4125
4126
7
    case CURLWC_CLEAN: {
4127
7
      struct ftp_wc *ftpwc = wildcard->ftpwc;
4128
7
      result = CURLE_OK;
4129
7
      if(ftpwc)
4130
4
        result = Curl_ftp_parselist_geterror(ftpwc->parser);
4131
4132
7
      wildcard->state = result ? CURLWC_ERROR : CURLWC_DONE;
4133
7
      return result;
4134
7
    }
4135
4136
0
    case CURLWC_DONE:
4137
0
    case CURLWC_ERROR:
4138
0
    case CURLWC_CLEAR:
4139
0
      if(wildcard->dtor) {
4140
0
        wildcard->dtor(wildcard->ftpwc);
4141
0
        wildcard->ftpwc = NULL;
4142
0
      }
4143
0
      return result;
4144
452
    }
4145
452
  }
4146
  /* UNREACHABLE */
4147
410
}
4148
4149
/***********************************************************************
4150
 *
4151
 * ftp_regular_transfer()
4152
 *
4153
 * The input argument is already checked for validity.
4154
 *
4155
 * Performs all commands done before a regular transfer between a local and a
4156
 * remote host.
4157
 *
4158
 * ftp->ctl_valid starts out as FALSE, and gets set to TRUE if we reach the
4159
 * ftp_done() function without finding any major problem.
4160
 */
4161
static CURLcode ftp_regular_transfer(struct Curl_easy *data,
4162
                                     struct ftp_conn *ftpc,
4163
                                     struct FTP *ftp,
4164
                                     bool *dophase_done)
4165
1.49k
{
4166
1.49k
  CURLcode result = CURLE_OK;
4167
1.49k
  bool connected = FALSE;
4168
1.49k
  data->req.size = -1; /* make sure this is unknown at this point */
4169
4170
1.49k
  Curl_pgrsReset(data);
4171
4172
1.49k
  ftpc->ctl_valid = TRUE; /* starts good */
4173
4174
1.49k
  result = ftp_perform(data, ftpc, ftp,
4175
1.49k
                       &connected, /* have we connected after PASV/PORT */
4176
1.49k
                       dophase_done); /* all commands in the DO-phase done? */
4177
4178
1.49k
  if(!result) {
4179
4180
1.39k
    if(!*dophase_done)
4181
      /* the DO phase has not completed yet */
4182
765
      return CURLE_OK;
4183
4184
631
    result = ftp_dophase_done(data, ftpc, ftp, connected);
4185
4186
631
    if(result)
4187
0
      return result;
4188
631
  }
4189
96
  else
4190
96
    freedirs(ftpc);
4191
4192
727
  return result;
4193
1.49k
}
4194
4195
/***********************************************************************
4196
 *
4197
 * ftp_do()
4198
 *
4199
 * This function is registered as 'curl_do' function. It decodes the path
4200
 * parts etc as a wrapper to the actual DO function (ftp_perform).
4201
 *
4202
 * The input argument is already checked for validity.
4203
 */
4204
static CURLcode ftp_do(struct Curl_easy *data, bool *done)
4205
1.50k
{
4206
1.50k
  CURLcode result = CURLE_OK;
4207
1.50k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
4208
1.50k
  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
4209
4210
1.50k
  *done = FALSE; /* default to false */
4211
1.50k
  if(!ftpc || !ftp)
4212
0
    return CURLE_FAILED_INIT;
4213
1.50k
  ftpc->wait_data_conn = FALSE; /* default to no such wait */
4214
4215
1.50k
#ifdef CURL_PREFER_LF_LINEENDS
4216
1.50k
  {
4217
    /* FTP data may need conversion. */
4218
1.50k
    struct Curl_cwriter *ftp_lc_writer;
4219
4220
1.50k
    result = Curl_cwriter_create(&ftp_lc_writer, data, &ftp_cw_lc,
4221
1.50k
                                 CURL_CW_CONTENT_DECODE);
4222
1.50k
    if(result)
4223
0
      return result;
4224
4225
1.50k
    result = Curl_cwriter_add(data, ftp_lc_writer);
4226
1.50k
    if(result) {
4227
0
      Curl_cwriter_free(data, ftp_lc_writer);
4228
0
      return result;
4229
0
    }
4230
1.50k
  }
4231
1.50k
#endif /* CURL_PREFER_LF_LINEENDS */
4232
4233
1.50k
  if(data->state.wildcardmatch) {
4234
410
    result = wc_statemach(data, ftpc, ftp);
4235
410
    if(data->wildcard->state == CURLWC_SKIP ||
4236
410
       data->wildcard->state == CURLWC_DONE) {
4237
      /* do not call ftp_regular_transfer */
4238
4
      return CURLE_OK;
4239
4
    }
4240
406
    if(result) /* error, loop or skipping the file */
4241
6
      return result;
4242
406
  }
4243
1.09k
  else { /* no wildcard FSM needed */
4244
1.09k
    result = ftp_parse_url_path(data, ftpc, ftp);
4245
1.09k
    if(result)
4246
1
      return result;
4247
1.09k
  }
4248
4249
1.49k
  result = ftp_regular_transfer(data, ftpc, ftp, done);
4250
4251
1.49k
  return result;
4252
1.50k
}
4253
4254
/***********************************************************************
4255
 *
4256
 * ftp_quit()
4257
 *
4258
 * This should be called before calling sclose() on an ftp control connection
4259
 * (not data connections). We should then wait for the response from the
4260
 * server before returning. The calling code should then try to close the
4261
 * connection.
4262
 *
4263
 */
4264
static CURLcode ftp_quit(struct Curl_easy *data,
4265
                         struct ftp_conn *ftpc)
4266
4.27k
{
4267
4.27k
  CURLcode result = CURLE_OK;
4268
4269
4.27k
  if(ftpc->ctl_valid) {
4270
853
    CURL_TRC_FTP(data, "sending QUIT to close session");
4271
853
    result = Curl_pp_sendf(data, &ftpc->pp, "%s", "QUIT");
4272
853
    if(result) {
4273
0
      failf(data, "Failure sending QUIT command: %s",
4274
0
            curl_easy_strerror(result));
4275
0
      ftpc->ctl_valid = FALSE; /* mark control connection as bad */
4276
0
      connclose(data->conn, "QUIT command failed"); /* mark for closure */
4277
0
      ftp_state(data, ftpc, FTP_STOP);
4278
0
      return result;
4279
0
    }
4280
4281
853
    ftp_state(data, ftpc, FTP_QUIT);
4282
4283
853
    result = ftp_block_statemach(data, ftpc);
4284
853
  }
4285
4286
4.27k
  return result;
4287
4.27k
}
4288
4289
/***********************************************************************
4290
 *
4291
 * ftp_disconnect()
4292
 *
4293
 * Disconnect from an FTP server. Cleanup protocol-specific per-connection
4294
 * resources. BLOCKING.
4295
 */
4296
static CURLcode ftp_disconnect(struct Curl_easy *data,
4297
                               struct connectdata *conn,
4298
                               bool dead_connection)
4299
4.29k
{
4300
4.29k
  struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
4301
4302
4.29k
  if(!ftpc)
4303
16
    return CURLE_FAILED_INIT;
4304
  /* We cannot send quit unconditionally. If this connection is stale or
4305
     bad in any way, sending quit and waiting around here will make the
4306
     disconnect wait in vain and cause more problems than we need to.
4307
4308
     ftp_quit() will check the state of ftp->ctl_valid. If it is ok it
4309
     will try to send the QUIT command, otherwise it will just return.
4310
  */
4311
4.27k
  ftpc->shutdown = TRUE;
4312
4.27k
  if(dead_connection || Curl_pp_needs_flush(data, &ftpc->pp))
4313
2.91k
    ftpc->ctl_valid = FALSE;
4314
4315
  /* The FTP session may or may not have been allocated/setup at this point! */
4316
4.27k
  (void)ftp_quit(data, ftpc); /* ignore errors on the QUIT */
4317
4.27k
  return CURLE_OK;
4318
4.29k
}
4319
4320
/* called from multi.c while DOing */
4321
static CURLcode ftp_doing(struct Curl_easy *data,
4322
                          bool *dophase_done)
4323
2.49k
{
4324
2.49k
  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
4325
2.49k
  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
4326
2.49k
  CURLcode result;
4327
4328
2.49k
  if(!ftpc || !ftp)
4329
0
    return CURLE_FAILED_INIT;
4330
2.49k
  result = ftp_statemach(data, ftpc, dophase_done);
4331
4332
2.49k
  if(result)
4333
227
    CURL_TRC_FTP(data, "[%s] DO phase failed", FTP_CSTATE(ftpc));
4334
2.26k
  else if(*dophase_done) {
4335
547
    result = ftp_dophase_done(data, ftpc, ftp, FALSE /* not connected */);
4336
4337
547
    CURL_TRC_FTP(data, "[%s] DO phase is complete2", FTP_CSTATE(ftpc));
4338
547
  }
4339
2.49k
  return result;
4340
2.49k
}
4341
4342
static void ftp_easy_dtor(void *key, size_t klen, void *entry)
4343
4.27k
{
4344
4.27k
  struct FTP *ftp = entry;
4345
4.27k
  (void)key;
4346
4.27k
  (void)klen;
4347
4.27k
  Curl_safefree(ftp->pathalloc);
4348
4.27k
  curlx_free(ftp);
4349
4.27k
}
4350
4351
static void ftp_conn_dtor(void *key, size_t klen, void *entry)
4352
4.27k
{
4353
4.27k
  struct ftp_conn *ftpc = entry;
4354
4.27k
  (void)key;
4355
4.27k
  (void)klen;
4356
4.27k
  freedirs(ftpc);
4357
4.27k
  Curl_safefree(ftpc->account);
4358
4.27k
  Curl_safefree(ftpc->alternative_to_user);
4359
4.27k
  Curl_safefree(ftpc->entrypath);
4360
4.27k
  Curl_safefree(ftpc->prevpath);
4361
4.27k
  Curl_safefree(ftpc->server_os);
4362
4.27k
  Curl_pp_disconnect(&ftpc->pp);
4363
4.27k
  curlx_free(ftpc);
4364
4.27k
}
4365
4366
static void type_url_check(struct Curl_easy *data, struct FTP *ftp)
4367
4.27k
{
4368
4.27k
  size_t len = strlen(ftp->path);
4369
  /* FTP URLs support an extension like ";type=<typecode>" that
4370
   * we will try to get now! */
4371
4.27k
  if((len >= 7) && !memcmp(&ftp->path[len - 7], ";type=", 6)) {
4372
40
    char *type = &ftp->path[len - 7];
4373
40
    char command = Curl_raw_toupper(type[6]);
4374
4375
40
    *type = 0; /* cut it off */
4376
4377
40
    switch(command) {
4378
19
    case 'A': /* ASCII mode */
4379
19
      data->state.prefer_ascii = TRUE;
4380
19
      break;
4381
4382
7
    case 'D': /* directory mode */
4383
7
      data->state.list_only = TRUE;
4384
7
      break;
4385
4386
0
    case 'I': /* binary mode */
4387
14
    default:
4388
      /* switch off ASCII */
4389
14
      data->state.prefer_ascii = FALSE;
4390
14
      break;
4391
40
    }
4392
40
  }
4393
4.27k
}
4394
4395
static CURLcode ftp_setup_connection(struct Curl_easy *data,
4396
                                     struct connectdata *conn)
4397
4.27k
{
4398
4.27k
  struct FTP *ftp;
4399
4.27k
  CURLcode result = CURLE_OK;
4400
4.27k
  struct ftp_conn *ftpc;
4401
4402
4.27k
  ftp = curlx_calloc(1, sizeof(*ftp));
4403
4.27k
  if(!ftp ||
4404
4.27k
     Curl_meta_set(data, CURL_META_FTP_EASY, ftp, ftp_easy_dtor))
4405
0
    return CURLE_OUT_OF_MEMORY;
4406
4407
4.27k
  ftpc = curlx_calloc(1, sizeof(*ftpc));
4408
4.27k
  if(!ftpc ||
4409
4.27k
     Curl_conn_meta_set(conn, CURL_META_FTP_CONN, ftpc, ftp_conn_dtor))
4410
0
    return CURLE_OUT_OF_MEMORY;
4411
4412
  /* clone connection related data that is FTP specific */
4413
4.27k
  if(data->set.str[STRING_FTP_ACCOUNT]) {
4414
7
    ftpc->account = curlx_strdup(data->set.str[STRING_FTP_ACCOUNT]);
4415
7
    if(!ftpc->account) {
4416
0
      Curl_conn_meta_remove(conn, CURL_META_FTP_CONN);
4417
0
      return CURLE_OUT_OF_MEMORY;
4418
0
    }
4419
7
  }
4420
4.27k
  if(data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]) {
4421
11
    ftpc->alternative_to_user =
4422
11
      curlx_strdup(data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]);
4423
11
    if(!ftpc->alternative_to_user) {
4424
0
      Curl_safefree(ftpc->account);
4425
0
      Curl_conn_meta_remove(conn, CURL_META_FTP_CONN);
4426
0
      return CURLE_OUT_OF_MEMORY;
4427
0
    }
4428
11
  }
4429
4430
4.27k
  ftp->path = &data->state.up.path[1]; /* do not include the initial slash */
4431
4432
4.27k
  type_url_check(data, ftp);
4433
4434
  /* get some initial data into the ftp struct */
4435
4.27k
  ftp->transfer = PPTRANSFER_BODY;
4436
4.27k
  ftp->downloadsize = 0;
4437
4.27k
  ftpc->known_filesize = -1; /* unknown size for now */
4438
4.27k
  ftpc->use_ssl = data->set.use_ssl;
4439
4.27k
  ftpc->ccc = data->set.ftp_ccc;
4440
4441
4.27k
  CURL_TRC_FTP(data, "[%s] setup connection -> %d", FTP_CSTATE(ftpc), result);
4442
4.27k
  return result;
4443
4.27k
}
4444
4445
bool ftp_conns_match(struct connectdata *needle, struct connectdata *conn)
4446
70
{
4447
70
  struct ftp_conn *nftpc = Curl_conn_meta_get(needle, CURL_META_FTP_CONN);
4448
70
  struct ftp_conn *cftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
4449
  /* Also match ACCOUNT, ALTERNATIVE-TO-USER, USE_SSL and CCC options */
4450
70
  if(!nftpc || !cftpc ||
4451
70
     Curl_timestrcmp(nftpc->account, cftpc->account) ||
4452
70
     Curl_timestrcmp(nftpc->alternative_to_user,
4453
70
                     cftpc->alternative_to_user) ||
4454
70
     (nftpc->use_ssl != cftpc->use_ssl) ||
4455
70
     (nftpc->ccc != cftpc->ccc))
4456
0
    return FALSE;
4457
70
  return TRUE;
4458
70
}
4459
4460
/*
4461
 * FTP protocol.
4462
 */
4463
static const struct Curl_protocol Curl_protocol_ftp = {
4464
  ftp_setup_connection,            /* setup_connection */
4465
  ftp_do,                          /* do_it */
4466
  ftp_done,                        /* done */
4467
  ftp_do_more,                     /* do_more */
4468
  ftp_connect,                     /* connect_it */
4469
  ftp_multi_statemach,             /* connecting */
4470
  ftp_doing,                       /* doing */
4471
  ftp_pollset,                     /* proto_pollset */
4472
  ftp_pollset,                     /* doing_pollset */
4473
  ftp_domore_pollset,              /* domore_pollset */
4474
  ZERO_NULL,                       /* perform_pollset */
4475
  ftp_disconnect,                  /* disconnect */
4476
  ZERO_NULL,                       /* write_resp */
4477
  ZERO_NULL,                       /* write_resp_hd */
4478
  ZERO_NULL,                       /* connection_check */
4479
  ZERO_NULL,                       /* attach connection */
4480
  ZERO_NULL,                       /* follow */
4481
};
4482
4483
#endif /* CURL_DISABLE_FTP */
4484
4485
/*
4486
 * FTP protocol handler.
4487
 */
4488
const struct Curl_scheme Curl_scheme_ftp = {
4489
  "ftp",                           /* scheme */
4490
#ifdef CURL_DISABLE_FTP
4491
  ZERO_NULL,
4492
#else
4493
  &Curl_protocol_ftp,
4494
#endif
4495
  CURLPROTO_FTP,                   /* protocol */
4496
  CURLPROTO_FTP,                   /* family */
4497
  PROTOPT_DUAL | PROTOPT_CLOSEACTION | PROTOPT_NEEDSPWD |
4498
  PROTOPT_NOURLQUERY | PROTOPT_PROXY_AS_HTTP |
4499
  PROTOPT_WILDCARD | PROTOPT_SSL_REUSE |
4500
  PROTOPT_CONN_REUSE, /* flags */
4501
  PORT_FTP,                        /* defport */
4502
};
4503
4504
/*
4505
 * FTPS protocol handler.
4506
 */
4507
const struct Curl_scheme Curl_scheme_ftps = {
4508
  "ftps",                          /* scheme */
4509
#if defined(CURL_DISABLE_FTP) || !defined(USE_SSL)
4510
  ZERO_NULL,
4511
#else
4512
  &Curl_protocol_ftp,
4513
#endif
4514
  CURLPROTO_FTPS,                  /* protocol */
4515
  CURLPROTO_FTP,                   /* family */
4516
  PROTOPT_SSL | PROTOPT_DUAL | PROTOPT_CLOSEACTION |
4517
  PROTOPT_NEEDSPWD | PROTOPT_NOURLQUERY | PROTOPT_WILDCARD |
4518
  PROTOPT_CONN_REUSE, /* flags */
4519
  PORT_FTPS,                       /* defport */
4520
};