Coverage Report

Created: 2025-08-03 06:36

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