Coverage Report

Created: 2025-10-30 06:17

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