Coverage Report

Created: 2026-04-29 07:01

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