Artifactspublic-v0

curl

master
ply4 git calls30 ms328 KiB3 cachedprotocol v2
4520 lines139 KiBRaw

Showing 4000 of 4520 lines.

1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curl_setup.h"
25#include "urldata.h"
27#ifndef CURL_DISABLE_FTP
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
43#include "sendf.h"
44#include "curl_addrinfo.h"
45#include "curl_trc.h"
46#include "if2ip.h"
47#include "progress.h"
48#include "transfer.h"
49#include "escape.h"
50#include "ftp.h"
51#include "ftp-int.h"
52#include "ftplistparser.h"
53#include "curl_range.h"
54#include "strcase.h"
55#include "vtls/vtls.h"
56#include "cfilters.h"
57#include "cf-socket.h"
58#include "connect.h"
59#include "curlx/inet_ntop.h"
60#include "curlx/inet_pton.h"
61#include "select.h"
62#include "parsedate.h" /* for the week day and month names */
63#include "sockaddr.h" /* required for Curl_sockaddr_storage */
64#include "multiif.h"
65#include "url.h"
66#include "http_proxy.h"
67#include "vdns/hostip.h"
68#include "curlx/strdup.h"
69#include "curlx/strerr.h"
70#include "curlx/strparse.h"
71#include "curl_ctype.h"
73#ifndef NI_MAXHOST
74#define NI_MAXHOST 1025
75#endif
76#ifndef INET_ADDRSTRLEN
77#define INET_ADDRSTRLEN 16
78#endif
80/* macro to check for a three-digit ftp status code at the start of the
81 given string */
82#define STATUSCODE(line) \
83 (ISDIGIT((line)[0]) && ISDIGIT((line)[1]) && ISDIGIT((line)[2]))
85/* macro to check for the last line in an FTP server response */
86#define LASTLINE(line) (STATUSCODE(line) && (' ' == (line)[3]))
88#ifdef CURLVERBOSE
89/* for tracing purposes */
90static const char * const ftp_state_names[] = {
91 "STOP",
92 "WAIT220",
93 "AUTH",
94 "USER",
95 "PASS",
96 "ACCT",
97 "PBSZ",
98 "PROT",
99 "CCC",
100 "PWD",
101 "SYST",
102 "NAMEFMT",
103 "QUOTE",
104 "RETR_PREQUOTE",
105 "STOR_PREQUOTE",
106 "LIST_PREQUOTE",
107 "POSTQUOTE",
108 "CWD",
109 "MKD",
110 "MDTM",
111 "TYPE",
112 "LIST_TYPE",
113 "RETR_LIST_TYPE",
114 "RETR_TYPE",
115 "STOR_TYPE",
116 "SIZE",
117 "RETR_SIZE",
118 "STOR_SIZE",
119 "REST",
120 "RETR_REST",
121 "PORT",
122 "PRET",
123 "PASV",
124 "LIST",
125 "RETR",
126 "STOR",
127 "QUIT"
128};
129#define FTP_CSTATE(ftpc) ((ftpc) ? ftp_state_names[(ftpc)->state] : "???")
131#endif /* CURLVERBOSE */
133/* This is the ONLY way to change FTP state! */
134static void ftp_state_low(struct Curl_easy *data,
135 struct ftp_conn *ftpc,
136 ftpstate newstate
137#ifdef DEBUGBUILD
138 , int lineno
139#endif
140 )
142 if(ftpc->state != newstate) {
143#ifdef DEBUGBUILD
144 NOVERBOSE((void)lineno);
145 CURL_TRC_FTP(data, "[%s] -> [%s] (line %d)", FTP_CSTATE(ftpc),
146 ftp_state_names[newstate], lineno);
147#else
148 CURL_TRC_FTP(data, "[%s] -> [%s]", FTP_CSTATE(ftpc),
149 ftp_state_names[newstate]);
150#endif
151 }
152 ftpc->state = newstate;
155/* Local API functions */
156#ifndef DEBUGBUILD
157#define ftp_state(x, y, z) ftp_state_low(x, y, z)
158#else /* !DEBUGBUILD */
159#define ftp_state(x, y, z) ftp_state_low(x, y, z, __LINE__)
160#endif /* DEBUGBUILD */
162static CURLcode ftp_state_mdtm(struct Curl_easy *data,
163 struct ftp_conn *ftpc,
164 struct FTP *ftp);
165static CURLcode ftp_state_quote(struct Curl_easy *data,
166 struct ftp_conn *ftpc,
167 struct FTP *ftp,
168 bool init, ftpstate instate);
169static CURLcode ftp_nb_type(struct Curl_easy *data,
170 struct ftp_conn *ftpc,
171 struct FTP *ftp,
172 bool ascii, ftpstate newstate);
173static CURLcode getftpresponse(struct Curl_easy *data, size_t *nreadp,
174 int *ftpcodep);
176static void freedirs(struct ftp_conn *ftpc)
178 curlx_safefree(ftpc->dirs);
179 ftpc->dirdepth = 0;
180 curlx_safefree(ftpc->rawpath);
181 ftpc->file = NULL;
184static size_t numof_slashes(const char *str)
186 const char *slashPos;
187 size_t num = 0;
188 do {
189 slashPos = strchr(str, '/');
190 if(slashPos) {
191 ++num;
192 str = slashPos + 1;
193 }
194 } while(slashPos);
195 return num;
198#define FTP_MAX_DIR_DEPTH 1000
200/***********************************************************************
201 *
202 * ftp_parse_url_path()
203 *
204 * Parse the URL path into separate path components.
205 *
206 */
207static CURLcode ftp_parse_url_path(struct Curl_easy *data,
208 struct ftp_conn *ftpc,
209 struct FTP *ftp)
211 const char *slashPos = NULL;
212 const char *fileName = NULL;
213 CURLcode result = CURLE_OK;
214 const char *rawPath = NULL; /* URL-decoded "raw" path */
215 size_t pathLen = 0;
217 ftpc->ctl_valid = FALSE;
218 ftpc->cwdfail = FALSE;
220 if(ftpc->rawpath)
221 freedirs(ftpc);
222 /* URL-decode ftp path before further evaluation */
223 result = Curl_urldecode(ftp->path, 0, &ftpc->rawpath, &pathLen, REJECT_CTRL);
224 if(result) {
225 failf(data, "path contains control characters");
226 return result;
227 }
228 rawPath = ftpc->rawpath;
230 switch(data->set.ftp_filemethod) {
231 case FTPFILE_NOCWD: /* fastest, but less standard-compliant */
233 if((pathLen > 0) && (rawPath[pathLen - 1] != '/'))
234 fileName = rawPath; /* this is a full file path */
235 /* else: ftpc->file is not used anywhere other than for operations on
236 a file. In other words, never for directory operations,
237 so we can safely leave filename as NULL here and use it as a
238 argument in dir/file decisions. */
239 break;
241 case FTPFILE_SINGLECWD:
242 slashPos = strrchr(rawPath, '/');
243 if(slashPos) {
244 /* get path before last slash, except for / */
245 size_t dirlen = slashPos - rawPath;
246 if(dirlen == 0)
247 dirlen = 1;
249 ftpc->dirs = curlx_calloc(1, sizeof(ftpc->dirs[0]));
250 if(!ftpc->dirs)
251 return CURLE_OUT_OF_MEMORY;
253 ftpc->dirs[0].start = 0;
254 ftpc->dirs[0].len = (int)dirlen;
255 ftpc->dirdepth = 1; /* we consider it to be a single directory */
256 fileName = slashPos + 1; /* rest is filename */
257 }
258 else
259 fileName = rawPath; /* filename only (or empty) */
260 break;
262 default: /* allow pretty much anything */
263 case FTPFILE_MULTICWD: {
264 /* current position: begin of next path component */
265 const char *curPos = rawPath;
267 /* number of entries to allocate for the 'dirs' array */
268 size_t dirAlloc = numof_slashes(rawPath);
270 if(dirAlloc >= FTP_MAX_DIR_DEPTH)
271 /* suspiciously deep directory hierarchy */
272 return CURLE_URL_MALFORMAT;
274 if(dirAlloc) {
275 ftpc->dirs = curlx_calloc(dirAlloc, sizeof(ftpc->dirs[0]));
276 if(!ftpc->dirs)
277 return CURLE_OUT_OF_MEMORY;
279 /* parse the URL path into separate path components */
280 while(dirAlloc--) {
281 const char *spos = strchr(curPos, '/');
282 size_t clen = spos - curPos;
284 /* path starts with a slash: add that as a directory */
285 if(!clen && (ftpc->dirdepth == 0))
286 ++clen;
288 /* we skip empty path components, like "x//y" since the FTP command
289 CWD requires a parameter and a non-existent parameter a) does not
290 work on many servers and b) has no effect on the others. */
291 if(clen) {
292 ftpc->dirs[ftpc->dirdepth].start = (int)(curPos - rawPath);
293 ftpc->dirs[ftpc->dirdepth].len = (int)clen;
294 ftpc->dirdepth++;
295 }
296 curPos = spos + 1;
297 }
298 }
299 fileName = curPos; /* the rest is the filename (or empty) */
300 }
301 break;
302 } /* switch */
304 if(fileName && *fileName)
305 ftpc->file = fileName;
306 else
307 ftpc->file = NULL; /* instead of point to a NUL byte,
308 we make it a NULL pointer */
310 if(data->state.upload && !ftpc->file && (ftp->transfer == PPTRANSFER_BODY)) {
311 /* We need a filename when uploading. Return error! */
312 failf(data, "Uploading to a URL without a filename");
313 return CURLE_URL_MALFORMAT;
314 }
316 ftpc->cwddone = FALSE; /* default to not done */
318 if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/'))
319 ftpc->cwddone = TRUE; /* skip CWD for absolute paths */
320 else { /* newly created FTP connections are already in entry path */
321 const char *oldPath = data->conn->bits.reuse ? ftpc->prevpath : "";
322 if(oldPath) {
323 size_t n = pathLen;
324 if(data->set.ftp_filemethod == FTPFILE_NOCWD)
325 n = 0; /* CWD to entry for relative paths */
326 else
327 n -= ftpc->file ? strlen(ftpc->file) : 0;
329 if((strlen(oldPath) == n) && rawPath && !strncmp(rawPath, oldPath, n)) {
330 infof(data, "Request has same path as previous transfer");
331 ftpc->cwddone = TRUE;
332 }
333 }
334 }
336 return CURLE_OK;
339/***********************************************************************
340 *
341 * ftp_need_type()
342 *
343 * Returns TRUE if we in the current situation should send TYPE
344 */
345static int ftp_need_type(struct ftp_conn *ftpc,
346 bool ascii_wanted)
348 return ftpc->transfertype != (ascii_wanted ? 'A' : 'I');
351static void close_secondarysocket(struct Curl_easy *data,
352 struct ftp_conn *ftpc)
354 (void)ftpc;
355 CURL_TRC_FTP(data, "[%s] closing DATA connection", FTP_CSTATE(ftpc));
356 Curl_conn_cf_discard_all(data, data->conn, SECONDARYSOCKET);
359#ifdef CURL_PREFER_LF_LINEENDS
360/*
361 * Lineend Conversions
362 * On ASCII transfers, e.g. directory listings, we might get lines
363 * ending in '\r\n' and we prefer '\n'.
364 * We might also get a lonely '\r' which we convert into a '\n'.
365 */
366struct ftp_cw_lc_ctx {
367 struct Curl_cwriter super;
368 bool newline_pending;
369};
371static CURLcode ftp_cw_lc_write(struct Curl_easy *data,
372 struct Curl_cwriter *writer, int type,
373 const char *buf, size_t blen)
375 static const char nl = '\n';
376 struct ftp_cw_lc_ctx *ctx = writer->ctx;
377 struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
379 if(!ftpc)
380 return CURLE_FAILED_INIT;
382 if(!(type & CLIENTWRITE_BODY) || ftpc->transfertype != 'A')
383 return Curl_cwriter_write(data, writer->next, type, buf, blen);
385 /* ASCII mode BODY data, convert lineends */
386 while(blen) {
387 /* do not pass EOS when writing parts */
388 int chunk_type = (type & ~CLIENTWRITE_EOS);
389 const char *cp;
390 size_t chunk_len;
391 CURLcode result;
393 if(ctx->newline_pending) {
394 if(buf[0] != '\n') {
395 /* previous chunk ended in '\r' and we do not see a '\n' in this one,
396 * need to write a newline. */
397 result = Curl_cwriter_write(data, writer->next, chunk_type, &nl, 1);
398 if(result)
399 return result;
400 }
401 /* either we wrote the newline or it is part of the next chunk of bytes
402 * we write. */
403 ctx->newline_pending = FALSE;
404 }
406 cp = memchr(buf, '\r', blen);
407 if(!cp)
408 break;
410 /* write the bytes before the '\r', excluding the '\r' */
411 chunk_len = cp - buf;
412 if(chunk_len) {
413 result = Curl_cwriter_write(data, writer->next, chunk_type,
414 buf, chunk_len);
415 if(result)
416 return result;
417 }
418 /* skip the '\r', we now have a newline pending */
419 buf = cp + 1;
420 blen = blen - chunk_len - 1;
421 ctx->newline_pending = TRUE;
422 }
424 /* Any remaining data does not contain a '\r' */
425 if(blen) {
426 DEBUGASSERT(!ctx->newline_pending);
427 return Curl_cwriter_write(data, writer->next, type, buf, blen);
428 }
429 else if(type & CLIENTWRITE_EOS) {
430 /* EndOfStream, if we have a trailing cr, now is the time to write it */
431 if(ctx->newline_pending) {
432 ctx->newline_pending = FALSE;
433 return Curl_cwriter_write(data, writer->next, type, &nl, 1);
434 }
435 /* Always pass on the EOS type indicator */
436 return Curl_cwriter_write(data, writer->next, type, buf, 0);
437 }
438 return CURLE_OK;
441static const struct Curl_cwtype ftp_cw_lc = {
442 "ftp-lineconv",
443 NULL,
444 0,
445 Curl_cwriter_def_init,
446 ftp_cw_lc_write,
447 Curl_cwriter_def_flush,
448 Curl_cwriter_def_close,
449 sizeof(struct ftp_cw_lc_ctx)
450};
452#endif /* CURL_PREFER_LF_LINEENDS */
454/***********************************************************************
455 *
456 * ftp_check_ctrl_on_data_wait()
457 *
458 */
459static CURLcode ftp_check_ctrl_on_data_wait(struct Curl_easy *data,
460 struct ftp_conn *ftpc)
462 struct connectdata *conn = data->conn;
463 curl_socket_t ctrl_sock = conn->sock[FIRSTSOCKET];
464 struct pingpong *pp = &ftpc->pp;
465 size_t nread;
466 int ftpcode;
467 bool response = FALSE;
469 /* First check whether there is a cached response from server */
470 if(curlx_dyn_len(&pp->recvbuf)) {
471 const char *l = curlx_dyn_ptr(&pp->recvbuf);
472 if(!ISDIGIT(*l) || (*l > '3')) {
473 /* Data connection could not be established, let's return */
474 infof(data, "There is negative response in cache while serv connect");
475 (void)getftpresponse(data, &nread, &ftpcode);
476 return CURLE_FTP_ACCEPT_FAILED;
477 }
478 }
480 if(pp->overflow)
481 /* there is pending control data still in the buffer to read */
482 response = TRUE;
483 else {
484 int socketstate = SOCKET_READABLE(ctrl_sock, 0);
485 /* see if the connection request is already here */
486 switch(socketstate) {
487 case -1: /* error */
488 /* let's die here */
489 failf(data, "Error while waiting for server connect");
490 return CURLE_FTP_ACCEPT_FAILED;
491 default:
492 if(socketstate & CURL_CSELECT_IN)
493 response = TRUE;
494 break;
495 }
496 }
498 if(response) {
499 infof(data, "Ctrl conn has data while waiting for data conn");
500 if(pp->overflow > 3) {
501 const char *r = curlx_dyn_ptr(&pp->recvbuf);
502 size_t len = curlx_dyn_len(&pp->recvbuf);
504 DEBUGASSERT((pp->overflow + pp->nfinal) <= curlx_dyn_len(&pp->recvbuf));
505 /* move over the most recently handled response line */
506 r += pp->nfinal;
507 len -= pp->nfinal;
509 if((len > 3) && LASTLINE(r)) {
510 curl_off_t status;
511 if(!curlx_str_number(&r, &status, 999) && (status == 226)) {
512 /* funny timing situation where we get the final message on the
513 control connection before traffic on the data connection has been
514 noticed. Leave the 226 in there and use this as a trigger to read
515 the data socket. */
516 infof(data, "Got 226 before data activity");
517 return CURLE_OK;
518 }
519 }
520 }
522 (void)getftpresponse(data, &nread, &ftpcode);
524 infof(data, "FTP code: %03d", ftpcode);
526 if(ftpcode / 100 > 3)
527 return CURLE_FTP_ACCEPT_FAILED;
529 return CURLE_WEIRD_SERVER_REPLY;
530 }
532 return CURLE_OK;
535/***********************************************************************
536 *
537 * ftp_initiate_transfer()
538 *
539 * After connection from server is accepted this function is called to
540 * setup transfer parameters and initiate the data transfer.
541 *
542 */
543static CURLcode ftp_initiate_transfer(struct Curl_easy *data,
544 struct ftp_conn *ftpc)
546 CURLcode result = CURLE_OK;
547 bool connected;
549 CURL_TRC_FTP(data, "ftp_initiate_transfer()");
550 result = Curl_conn_connect(data, SECONDARYSOCKET, TRUE, &connected);
551 if(result || !connected)
552 return result;
554 if(data->state.upload) {
555 /* When we know we are uploading a specified file, we can get the file
556 size prior to the actual upload. */
557 Curl_pgrsSetUploadSize(data, data->state.infilesize);
559 /* FTP upload, shutdown DATA, ignore shutdown errors, as we rely
560 * on the server response on the CONTROL connection. */
561 Curl_xfer_setup_send(data, SECONDARYSOCKET);
562 Curl_xfer_set_shutdown(data, TRUE, TRUE);
563 }
564 else {
565 /* FTP download, shutdown, do not ignore errors */
566 Curl_xfer_setup_recv(data, SECONDARYSOCKET, data->req.size);
567 Curl_xfer_set_shutdown(data, TRUE, FALSE);
568 }
570 ftpc->pp.pending_resp = TRUE; /* expect server response */
571 ftp_state(data, ftpc, FTP_STOP);
573 return CURLE_OK;
576static bool ftp_endofresp(struct Curl_easy *data, struct connectdata *conn,
577 const char *line, size_t len, int *code)
579 curl_off_t status;
580 (void)data;
581 (void)conn;
583 if((len > 3) && LASTLINE(line) && !curlx_str_number(&line, &status, 999)) {
584 *code = (int)status;
585 return TRUE;
586 }
588 return FALSE;
591static CURLcode ftp_readresp(struct Curl_easy *data,
592 struct ftp_conn *ftpc,
593 int8_t sockindex,
594 struct pingpong *pp,
595 int *ftpcodep, /* return the ftp-code if done */
596 size_t *size) /* size of the response */
598 int code;
599 CURLcode result = Curl_pp_readresp(data, sockindex, pp, &code, size);
600 DEBUGASSERT(ftpcodep);
602 /* store the latest code for later retrieval, except during shutdown */
603 if(!ftpc->shutdown)
604 data->info.httpcode = code;
606 *ftpcodep = code;
608 if(code == 421) {
609 /* 421 means "Service not available, closing control connection." and FTP
610 * servers use it to signal that idle session timeout has been exceeded.
611 * If we ignored the response, it could end up hanging in some cases.
612 *
613 * This response code can come at any point so having it treated
614 * generically is a good idea.
615 */
616 infof(data, "We got a 421 - timeout");
617 ftp_state(data, ftpc, FTP_STOP);
618 return CURLE_OPERATION_TIMEDOUT;
619 }
621 return result;
624/* --- parse FTP server responses --- */
626/*
627 * getftpresponse() is a BLOCKING function to read the full response from a
628 * server after a command.
629 *
630 */
631static CURLcode getftpresponse(struct Curl_easy *data,
632 size_t *nreadp, /* return number of bytes
633 read */
634 int *ftpcodep) /* return the ftp-code */
636 /*
637 * We cannot read one byte per read() and then go back to select() as the
638 * OpenSSL read() does not grok that properly.
639 *
640 * Alas, read as much as possible, split up into lines, use the ending
641 * line in a response or continue reading. */
643 struct connectdata *conn = data->conn;
644 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
645 CURLcode result = CURLE_OK;
646 struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
647 struct pingpong *pp = &ftpc->pp;
648 size_t nread;
649 int cache_skip = 0;
650 DEBUGASSERT(ftpcodep);
652 CURL_TRC_FTP(data, "getftpresponse start");
653 *nreadp = 0;
654 *ftpcodep = 0; /* 0 for errors */
656 if(!ftpc)
657 return CURLE_FAILED_INIT;
659 while(!*ftpcodep && !result) {
660 /* check and reset timeout value every lap */
661 timediff_t timeout = Curl_pp_state_timeleft_ms(data, pp);
662 timediff_t interval_ms;
664 if(timeout <= 0) {
665 failf(data, "FTP response timeout");
666 return CURLE_OPERATION_TIMEDOUT; /* already too little time */
667 }
669 interval_ms = 1000; /* use 1 second timeout intervals */
670 if(timeout < interval_ms)
671 interval_ms = timeout;
673 /*
674 * Since this function is blocking, we need to wait here for input on the
675 * connection and only then we call the response reading function. We do
676 * timeout at least every second to make the timeout check run.
677 *
678 * A caution here is that the ftp_readresp() function has a cache that may
679 * contain pieces of a response from the previous invoke and we need to
680 * make sure we do not wait for input while there is unhandled data in
681 * that cache. Also, if the cache is there, we call ftp_readresp() and
682 * the cache was not good enough to continue we must not busy-loop around
683 * this function.
684 *
685 */
687 if(curlx_dyn_len(&pp->recvbuf) && (cache_skip < 2)) {
688 /*
689 * There is a cache left since before. We then skipping the wait for
690 * socket action, unless this is the same cache like the previous round
691 * as then the cache was deemed not enough to act on and we then need to
692 * wait for more data anyway.
693 */
694 }
695 else if(!Curl_conn_data_pending(data, FIRSTSOCKET)) {
696 curl_socket_t wsock = Curl_pp_needs_flush(data, pp) ?
697 sockfd : CURL_SOCKET_BAD;
698 int ev = Curl_socket_check(sockfd, CURL_SOCKET_BAD, wsock, interval_ms);
699 if(ev < 0) {
700 failf(data, "FTP response aborted due to select/poll error: %d",
701 SOCKERRNO);
702 return CURLE_RECV_ERROR;
703 }
704 else if(ev == 0) {
705 result = Curl_pgrsUpdate(data);
706 continue; /* continue in our loop for the timeout duration */
707 }
708 }
710 if(Curl_pp_needs_flush(data, pp)) {
711 result = Curl_pp_flushsend(data, pp);
712 if(result)
713 break;
714 }
716 result = ftp_readresp(data, ftpc, FIRSTSOCKET, pp, ftpcodep, &nread);
717 if(result)
718 break;
720 if(!nread && curlx_dyn_len(&pp->recvbuf))
721 /* bump cache skip counter as on repeated skips we must wait for more
722 data */
723 cache_skip++;
724 else
725 /* when we got data or there is no cache left, we reset the cache skip
726 counter */
727 cache_skip = 0;
729 *nreadp += nread;
731 } /* while there is buffer left and loop is requested */
733 pp->pending_resp = FALSE;
734 CURL_TRC_FTP(data, "getftpresponse -> result=%d, nread=%zu, ftpcode=%d",
735 (int)result, *nreadp, *ftpcodep);
737 return result;
740static CURLcode ftp_state_user(struct Curl_easy *data,
741 struct ftp_conn *ftpc,
742 struct connectdata *conn)
744 CURLcode result = Curl_pp_sendf(data, &ftpc->pp, "USER %s",
745 Curl_creds_user(conn->creds));
746 if(!result) {
747 ftpc->ftp_trying_alternative = FALSE;
748 ftp_state(data, ftpc, FTP_USER);
749 }
750 return result;
753static CURLcode ftp_state_pwd(struct Curl_easy *data,
754 struct ftp_conn *ftpc)
756 CURLcode result;
757#ifdef DEBUGBUILD
758 if(!data->id && getenv("CURL_FTP_PWD_STOP"))
759 return CURLE_OK;
760#endif
761 result = Curl_pp_sendf(data, &ftpc->pp, "%s", "PWD");
762 if(!result)
763 ftp_state(data, ftpc, FTP_PWD);
765 return result;
768/* For the FTP "protocol connect" and "doing" phases only */
769static CURLcode ftp_pollset(struct Curl_easy *data,
770 struct easy_pollset *ps)
772 struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
773 return ftpc ? Curl_pp_pollset(data, &ftpc->pp, ps) : CURLE_OK;
776/* For the FTP "DO_MORE" phase only */
777static CURLcode ftp_domore_pollset(struct Curl_easy *data,
778 struct easy_pollset *ps)
780 struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
782 if(!ftpc)
783 return CURLE_OK;
785 /* When in DO_MORE state, we could be either waiting for us to connect to a
786 * remote site, or we could wait for that site to connect to us. Or handle
787 * ordinary commands.
788 */
789 CURL_TRC_FTP(data, "[%s] ftp_domore_pollset()", FTP_CSTATE(ftpc));
791 if(FTP_STOP == ftpc->state) {
792 /* if stopped and still in this state, then we are also waiting for a
793 connect on the secondary connection */
794 DEBUGASSERT(data->conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD ||
795 (data->conn->cfilter[SECONDARYSOCKET] &&
796 !Curl_conn_is_connected(data->conn, SECONDARYSOCKET)));
797 /* An unconnected SECONDARY will add its socket by itself
798 * via its adjust_pollset() */
799 return Curl_pollset_add_in(data, ps, data->conn->sock[FIRSTSOCKET]);
800 }
801 return Curl_pp_pollset(data, &ftpc->pp, ps);
804static int pathlen(struct ftp_conn *ftpc, int num)
806 DEBUGASSERT(ftpc->dirs);
807 DEBUGASSERT(ftpc->dirdepth > num);
808 return ftpc->dirs[num].len;
811static const char *pathpiece(struct ftp_conn *ftpc, int num)
813 DEBUGASSERT(ftpc->dirs);
814 DEBUGASSERT(ftpc->dirdepth > num);
815 return &ftpc->rawpath[ftpc->dirs[num].start];
818/* This is called after the FTP_QUOTE state is passed.
820 ftp_state_cwd() sends the range of CWD commands to the server to change to
821 the correct directory. It may also need to send MKD commands to create
822 missing ones, if that option is enabled. */
823static CURLcode ftp_state_cwd(struct Curl_easy *data,
824 struct ftp_conn *ftpc,
825 struct FTP *ftp)
827 CURLcode result = CURLE_OK;
829 if(ftpc->cwddone)
830 /* already done and fine */
831 result = ftp_state_mdtm(data, ftpc, ftp);
832 else {
833 /* FTPFILE_NOCWD with full path: expect ftpc->cwddone! */
834 DEBUGASSERT((data->set.ftp_filemethod != FTPFILE_NOCWD) ||
835 !(ftpc->dirdepth && ftpc->rawpath[0] == '/'));
837 ftpc->count2 = 0; /* count2 counts failed CWDs */
839 if(data->conn->bits.reuse && ftpc->entrypath &&
840 /* no need to go to entrypath when we have an absolute path */
841 !(ftpc->dirdepth && ftpc->rawpath[0] == '/')) {
842 /* This is a reused connection. Since we change directory to where the
843 transfer is taking place, we must first get back to the original dir
844 where we ended up after login: */
845 ftpc->cwdcount = 0; /* we count this as the first path, then we add one
846 for all upcoming ones in the ftp->dirs[] array */
847 result = Curl_pp_sendf(data, &ftpc->pp, "CWD %s", ftpc->entrypath);
848 if(!result)
849 ftp_state(data, ftpc, FTP_CWD);
850 }
851 else {
852 if(ftpc->dirdepth) {
853 ftpc->cwdcount = 1;
854 /* issue the first CWD, the rest is sent when the CWD responses are
855 received... */
856 result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s",
857 pathlen(ftpc, 0), pathpiece(ftpc, 0));
858 if(!result)
859 ftp_state(data, ftpc, FTP_CWD);
860 }
861 else {
862 /* No CWD necessary */
863 result = ftp_state_mdtm(data, ftpc, ftp);
864 }
865 }
866 }
867 return result;
870typedef enum {
871 EPRT,
872 PORT,
873 DONE
874} ftpport;
876/*
877 * Parse the CURLOPT_FTPPORT string
878 * "(ipv4|ipv6|domain|interface)?(:port(-range)?)?"
879 * and extract addr/addrlen and port_min/port_max.
880 */
881static CURLcode ftp_port_parse_string(struct Curl_easy *data,
882 struct connectdata *conn,
883 const char *string_ftpport,
884 struct Curl_sockaddr_storage *ss,
885 unsigned short *port_minp,
886 unsigned short *port_maxp,
887 const char **hostp,
888 char *hbuf, size_t hbuflen)
890 const char *ip_end = NULL;
891 const char *addr = NULL;
892 size_t addrlen = 0;
893 unsigned short port_min = 0;
894 unsigned short port_max = 0;
895 char ipstr[50];
896#ifndef USE_IPV6
897 (void)conn;
898 (void)ss;
899#endif
901 /* default to nothing */
902 *hostp = NULL;
903 *port_minp = *port_maxp = 0;
905 if(!string_ftpport || (strlen(string_ftpport) <= 1))
906 goto done;
908#ifdef USE_IPV6
909 if(*string_ftpport == '[') {
910 /* [ipv6]:port(-range) */
911 const char *ip_start = string_ftpport + 1;
912 ip_end = strchr(ip_start, ']');
913 if(ip_end) {
914 addrlen = ip_end - ip_start;
915 addr = ip_start;
916 }
917 }
918 else
919#endif
920 if(*string_ftpport == ':') {
921 /* :port */
922 ip_end = string_ftpport;
923 }
924 else {
925 ip_end = strchr(string_ftpport, ':');
926 addr = string_ftpport;
927 if(ip_end) {
928#ifdef USE_IPV6
929 struct sockaddr_in6 * const sa6 = (void *)ss;
930#endif
931 /* either IPv6 or (ipv4|domain|interface):port(-range) */
932 addrlen = ip_end - string_ftpport;
933#ifdef USE_IPV6
934 if(curlx_inet_pton(AF_INET6, string_ftpport, &sa6->sin6_addr) == 1) {
935 /* IPv6 */
936 addrlen = strlen(string_ftpport);
937 ip_end = NULL; /* this got no port ! */
938 }
939#endif
940 }
941 else
942 /* ipv4|interface */
943 addrlen = strlen(string_ftpport);
944 }
946 /* parse the port */
947 if(ip_end) {
948 const char *portp = strchr(ip_end, ':');
949 if(portp) {
950 curl_off_t start;
951 curl_off_t end;
952 portp++;
953 if(!curlx_str_number(&portp, &start, 0xffff)) {
954 port_min = (unsigned short)start;
955 if(!curlx_str_single(&portp, '-') &&
956 !curlx_str_number(&portp, &end, 0xffff))
957 port_max = (unsigned short)end;
958 else
959 port_max = port_min;
960 }
961 }
962 }
964 /* correct errors like :1234-1230 or :-4711 */
965 if(port_min > port_max)
966 port_min = port_max = 0;
968 if(addrlen) {
969 const struct Curl_sockaddr_ex *remote_addr =
970 Curl_conn_get_remote_addr(data, FIRSTSOCKET);
972 DEBUGASSERT(remote_addr);
973 DEBUGASSERT(addr);
974 if(!remote_addr || (addrlen >= sizeof(ipstr)) || (addrlen >= hbuflen))
975 return CURLE_FTP_PORT_FAILED;
976 memcpy(ipstr, addr, addrlen);
977 ipstr[addrlen] = 0;
979 /* attempt to get the address of the given interface name */
980 switch(Curl_if2ip(remote_addr->family,
981#ifdef USE_IPV6
982 Curl_ipv6_scope(&remote_addr->curl_sa_addr),
983 conn->scope_id,
984#endif
985 ipstr, hbuf, hbuflen)) {
986 case IF2IP_NOT_FOUND:
987 /* not an interface, use the string as hostname instead */
988 memcpy(hbuf, addr, addrlen);
989 hbuf[addrlen] = 0;
990 *hostp = hbuf;
991 break;
992 case IF2IP_AF_NOT_SUPPORTED:
993 return CURLE_FTP_PORT_FAILED;
994 case IF2IP_FOUND:
995 *hostp = hbuf; /* use the hbuf for hostname */
996 break;
997 }
998 }
999 /* else: only a port(-range) given, leave host as NULL */
1001done:
1002 *port_minp = port_min;
1003 *port_maxp = port_max;
1004 return CURLE_OK;
1008 * If no host was derived from the FTPPORT string, fall back to the IP address
1009 * of the control connection's local socket.
1010 */
1011static CURLcode ftp_port_default_host(struct Curl_easy *data,
1012 struct connectdata *conn,
1013 struct Curl_sockaddr_storage *ss,
1014 curl_socklen_t *sslenp,
1015 const char **hostp,
1016 char *hbuf, size_t hbuflen,
1017 bool *non_localp)
1019 struct sockaddr *sa = (struct sockaddr *)ss;
1020 struct sockaddr_in * const sa4 = (void *)sa;
1021#ifdef USE_IPV6
1022 struct sockaddr_in6 * const sa6 = (void *)sa;
1023#endif
1024 char buffer[STRERROR_LEN];
1025 CURLcode result;
1027 *sslenp = sizeof(*ss);
1028 if(getsockname(conn->sock[FIRSTSOCKET], sa, sslenp)) {
1029 failf(data, "getsockname() failed: %s",
1030 curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1031 return CURLE_FTP_PORT_FAILED;
1033 switch(sa->sa_family) {
1034#ifdef USE_IPV6
1035 case AF_INET6:
1036 result = curlx_inet_ntop(sa->sa_family, &sa6->sin6_addr, hbuf, hbuflen);
1037 break;
1038#endif
1039 default:
1040 result = curlx_inet_ntop(sa->sa_family, &sa4->sin_addr, hbuf, hbuflen);
1041 break;
1043 if(result)
1044 return CURLE_FTP_PORT_FAILED;
1046 *hostp = hbuf;
1047 *non_localp = FALSE; /* we know it is local now */
1048 return CURLE_OK;
1052 * Resolve the host string to a list of addresses.
1053 */
1054static CURLcode ftp_port_resolve_host(struct Curl_easy *data,
1055 struct connectdata *conn,
1056 const char *host,
1057 struct Curl_dns_entry **dns_entryp,
1058 const struct Curl_addrinfo **resp)
1060 CURLcode result;
1062 *resp = NULL;
1063 result = Curl_resolv_blocking(
1064 data, Curl_resolv_dns_queries(data, conn->ip_version),
1065 host, 0, Curl_conn_get_transport(data, conn), dns_entryp);
1066 if(result)
1067 failf(data, "failed to resolve the address provided to PORT: %s", host);
1068 else {
1069 DEBUGASSERT(*dns_entryp);
1070 *resp = (*dns_entryp)->addr;
1072 return result;
1076 * Open a TCP socket for the resolved address family.
1077 */
1078static CURLcode ftp_port_open_socket(struct Curl_easy *data,
1079 struct connectdata *conn,
1080 const struct Curl_addrinfo *res,
1081 const struct Curl_addrinfo **aip,
1082 curl_socket_t *portsockp)
1084 char buffer[STRERROR_LEN];
1085 int sockerr = 0;
1086 const struct Curl_addrinfo *ai;
1087 CURLcode result = CURLE_FTP_PORT_FAILED;
1089 for(ai = res; ai; ai = ai->ai_next) {
1090 result =
1091 Curl_socket_open(data, ai, NULL,
1092 Curl_conn_get_transport(data, conn), portsockp);
1093 if(result) {
1094 if(result == CURLE_OUT_OF_MEMORY)
1095 return result;
1096 result = CURLE_FTP_PORT_FAILED;
1097 sockerr = SOCKERRNO;
1098 continue;
1100 break;
1102 if(!ai) {
1103 failf(data, "socket failure: %s",
1104 curlx_strerror(sockerr, buffer, sizeof(buffer)));
1105 return CURLE_FTP_PORT_FAILED;
1107 *aip = ai;
1108 return result;
1112 * Bind the socket to a local address and port within the requested range.
1113 * Falls back to the control-connection address if the user-requested address
1114 * is non-local.
1115 */
1116static CURLcode ftp_port_bind_socket(struct Curl_easy *data,
1117 struct connectdata *conn,
1118 curl_socket_t portsock,
1119 const struct Curl_addrinfo *ai,
1120 struct Curl_sockaddr_storage *ss,
1121 curl_socklen_t *sslen_io,
1122 unsigned short port_min,
1123 unsigned short port_max,
1124 bool non_local)
1126 struct sockaddr *sa = (struct sockaddr *)ss;
1127 struct sockaddr_in * const sa4 = (void *)sa;
1128#ifdef USE_IPV6
1129 struct sockaddr_in6 * const sa6 = (void *)sa;
1130#endif
1131 char buffer[STRERROR_LEN];
1132 unsigned short port;
1133 int sockerr;
1135 memcpy(sa, ai->ai_addr, ai->ai_addrlen);
1136 *sslen_io = ai->ai_addrlen;
1138 for(port = port_min; port <= port_max;) {
1139 if(sa->sa_family == AF_INET)
1140 sa4->sin_port = htons(port);
1141#ifdef USE_IPV6
1142 else
1143 sa6->sin6_port = htons(port);
1144#endif
1145 if(bind(portsock, sa, *sslen_io)) {
1146 sockerr = SOCKERRNO;
1147 if(non_local && (sockerr == SOCKEADDRNOTAVAIL)) {
1148 /* The requested bind address is not local. Use the address used for
1149 * the control connection instead and restart the port loop.
1150 */
1151 infof(data, "bind(port=%hu) on non-local address failed: %s", port,
1152 curlx_strerror(sockerr, buffer, sizeof(buffer)));
1154 *sslen_io = sizeof(*ss);
1155 if(getsockname(conn->sock[FIRSTSOCKET], sa, sslen_io)) {
1156 failf(data, "getsockname() failed: %s",
1157 curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1158 return CURLE_FTP_PORT_FAILED;
1160 port = port_min;
1161 non_local = FALSE; /* do not try this again */
1162 continue;
1164 if(sockerr != SOCKEADDRINUSE && sockerr != SOCKEACCES) {
1165 failf(data, "bind(port=%hu) failed: %s", port,
1166 curlx_strerror(sockerr, buffer, sizeof(buffer)));
1167 return CURLE_FTP_PORT_FAILED;
1170 else
1171 break;
1173 /* check if port is the maximum value here, because it might be 0xffff
1174 and then the increment below will wrap the 16-bit counter */
1175 if(port == port_max) {
1176 failf(data, "bind() failed, ran out of ports");
1177 return CURLE_FTP_PORT_FAILED;
1179 port++;
1182 /* re-read the name so we can extract the actual port chosen */
1183 *sslen_io = sizeof(*ss);
1184 if(getsockname(portsock, sa, sslen_io)) {
1185 failf(data, "getsockname() failed: %s",
1186 curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1187 return CURLE_FTP_PORT_FAILED;
1189 CURL_TRC_FTP(data, "ftp_port_bind_socket(), socket bound to port %d", port);
1190 return CURLE_OK;
1194 * Start listening on the data socket.
1195 */
1196static CURLcode ftp_port_listen(struct Curl_easy *data, curl_socket_t portsock)
1198 char buffer[STRERROR_LEN];
1200 if(listen(portsock, 1)) {
1201 failf(data, "socket failure: %s",
1202 curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1203 return CURLE_FTP_PORT_FAILED;
1205 CURL_TRC_FTP(data, "ftp_port_listen(), listening on port");
1206 return CURLE_OK;
1210 * Send the EPRT or PORT command to the server.
1211 */
1212static CURLcode ftp_port_send_command(struct Curl_easy *data,
1213 struct ftp_conn *ftpc,
1214 struct connectdata *conn,
1215 struct Curl_sockaddr_storage *ss,
1216 const struct Curl_addrinfo *ai,
1217 ftpport fcmd)
1219 static const char mode[][5] = { "EPRT", "PORT" };
1220 struct sockaddr *sa = (struct sockaddr *)ss;
1221 struct sockaddr_in * const sa4 = (void *)sa;
1222#ifdef USE_IPV6
1223 struct sockaddr_in6 * const sa6 = (void *)sa;
1224#endif
1225 char myhost[MAX_IPADR_LEN + 1] = "";
1226 unsigned short port;
1227 CURLcode result;
1229 /* Get a plain printable version of the numerical address to work with. This
1230 logic uses the address provided by the FTPPORT option, which at times
1231 might differ from the address in 'ss' used to bind to: when a user asks
1232 the server to connect to a specific address knowing that it works, but
1233 curl instead selects to listen to the local address because it cannot use
1234 the provided address. FTP is strange. */
1235 Curl_printable_address(ai, myhost, sizeof(myhost));
1237#ifdef USE_IPV6
1238 if(!conn->bits.ftp_use_eprt && conn->bits.ipv6)
1239 /* EPRT is disabled but we are connected to an IPv6 host, so we ignore the
1240 request and enable EPRT again! */
1241 conn->bits.ftp_use_eprt = TRUE;
1242#endif
1244 for(; fcmd != DONE; fcmd++) {
1246 if(!conn->bits.ftp_use_eprt && (EPRT == fcmd))
1247 /* if disabled, goto next */
1248 continue;
1250 if((PORT == fcmd) && sa->sa_family != AF_INET)
1251 /* PORT is IPv4 only */
1252 continue;
1254 switch(sa->sa_family) {
1255 case AF_INET:
1256 port = ntohs(sa4->sin_port);
1257 break;
1258#ifdef USE_IPV6
1259 case AF_INET6:
1260 port = ntohs(sa6->sin6_port);
1261 break;
1262#endif
1263 default:
1264 continue; /* might as well skip this */
1267 if(EPRT == fcmd) {
1268 /*
1269 * Two fine examples from RFC2428;
1271 * EPRT |1|132.235.1.2|6275|
1273 * EPRT |2|1080::8:800:200C:417A|5282|
1274 */
1275 result = Curl_pp_sendf(data, &ftpc->pp, "%s |%d|%s|%hu|", mode[fcmd],
1276 sa->sa_family == AF_INET ? 1 : 2, myhost, port);
1277 if(result) {
1278 failf(data, "Failure sending EPRT command: %s",
1279 curl_easy_strerror(result));
1280 return result;
1282 break;
1284 if(PORT == fcmd) {
1285 /* large enough for [IP address],[num],[num] */
1286 char target[sizeof(myhost) + 20];
1287 const char *source = myhost;
1288 char *dest = target;
1290 /* translate x.x.x.x to x,x,x,x */
1291 while(*source) {
1292 if(*source == '.')
1293 *dest = ',';
1294 else
1295 *dest = *source;
1296 dest++;
1297 source++;
1299 *dest = 0;
1300 curl_msnprintf(dest, 20, ",%d,%d", (int)(port >> 8), (int)(port & 0xff));
1302 result = Curl_pp_sendf(data, &ftpc->pp, "%s %s", mode[fcmd], target);
1303 if(result) {
1304 failf(data, "Failure sending PORT command: %s",
1305 curl_easy_strerror(result));
1306 return result;
1308 break;
1312 /* store which command was sent */
1313 ftpc->count1 = fcmd;
1314 ftp_state(data, ftpc, FTP_PORT);
1315 return CURLE_OK;
1319 * ftp_state_use_port()
1321 * Set up an active-mode FTP data connection (using PORT or EPRT) and start
1322 * listening for the server's incoming connection on SECONDARYSOCKET.
1323 */
1324static CURLcode ftp_state_use_port(struct Curl_easy *data,
1325 struct ftp_conn *ftpc,
1326 ftpport fcmd) /* start with this */
1328 CURLcode result = CURLE_FTP_PORT_FAILED;
1329 struct connectdata *conn = data->conn;
1330 curl_socket_t portsock = CURL_SOCKET_BAD;
1332 struct Curl_sockaddr_storage ss;
1333 curl_socklen_t sslen;
1334 char hbuf[NI_MAXHOST];
1335 const char *host = NULL;
1336 const char *string_ftpport = CURL_EASY_STR(data, STRING_FTPPORT);
1337 struct Curl_dns_entry *dns_entry = NULL;
1338 const struct Curl_addrinfo *res = NULL;
1339 const struct Curl_addrinfo *ai = NULL;
1340 unsigned short port_min = 0;
1341 unsigned short port_max = 0;
1342 bool non_local = TRUE;
1344 /* parse the FTPPORT string for address and port range */
1345 result = ftp_port_parse_string(data, conn, string_ftpport,
1346 &ss, &port_min, &port_max,
1347 &host, hbuf, sizeof(hbuf));
1348 if(!result && !host)
1349 /* if no host was specified, use the control connection's local IP */
1350 result = ftp_port_default_host(data, conn, &ss, &sslen, &host,
1351 hbuf, sizeof(hbuf), &non_local);
1353 /* resolve host string to address list */
1354 if(!result)
1355 result = ftp_port_resolve_host(data, conn, host, &dns_entry, &res);
1357 /* Open a TCP socket for the data connection */
1358 if(!result)
1359 result = ftp_port_open_socket(data, conn, res, &ai, &portsock);
1360 if(!result) {
1361 CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), opened socket",
1362 FTP_CSTATE(ftpc));
1364 /* bind to a suitable local address / port */
1365 result = ftp_port_bind_socket(data, conn, portsock, ai, &ss, &sslen,
1366 port_min, port_max, non_local);
1369 /* listen */
1370 if(!result)
1371 result = ftp_port_listen(data, portsock);
1373 /* send the PORT / EPRT command */
1374 if(!result)
1375 result = ftp_port_send_command(data, ftpc, conn, &ss, ai, fcmd);
1377 /* replace any filter on SECONDARY with one listening on this socket */
1378 if(!result)
1379 result = Curl_conn_tcp_listen_set(data, conn, SECONDARYSOCKET, &portsock);
1381 if(!result)
1382 portsock = CURL_SOCKET_BAD; /* now held in filter */
1384 /* cleanup */
1386 if(dns_entry)
1387 Curl_dns_entry_unlink(data, &dns_entry);
1388 if(result) {
1389 ftp_state(data, ftpc, FTP_STOP);
1391 else {
1392 /* successfully set up the listen socket filter. SSL needed?
1393 * Use the control connections origin for cert verification. */
1394 if(conn->bits.ftp_use_data_ssl && data->set.ftp_use_port &&
1395 !Curl_conn_is_ssl(conn, SECONDARYSOCKET)) {
1396 result = Curl_ssl_cfilter_add(
1397 data, Curl_conn_get_origin(conn, FIRSTSOCKET),
1398 conn, SECONDARYSOCKET);
1400 conn->bits.do_more = FALSE;
1401 Curl_expire(data, (data->set.accepttimeout > 0) ?
1402 data->set.accepttimeout: DEFAULT_ACCEPT_TIMEOUT,
1403 EXPIRE_FTP_ACCEPT);
1405 if(portsock != CURL_SOCKET_BAD)
1406 Curl_socket_close(data, conn, portsock);
1407 return result;
1410static CURLcode ftp_state_use_pasv(struct Curl_easy *data,
1411 struct ftp_conn *ftpc,
1412 struct connectdata *conn)
1414 CURLcode result = CURLE_OK;
1415 /* Here's the executive summary on what to do:
1417 PASV is RFC959, expect:
1418 227 Entering Passive Mode (a1,a2,a3,a4,p1,p2)
1420 LPSV is RFC1639, expect:
1421 228 Entering Long Passive Mode (4,4,a1,a2,a3,a4,2,p1,p2)
1423 EPSV is RFC2428, expect:
1424 229 Entering Extended Passive Mode (|||port|)
1425 */
1427 static const char mode[][5] = { "EPSV", "PASV" };
1428 int modeoff;
1430#ifdef PF_INET6
1431 if(!conn->bits.ftp_use_epsv && conn->bits.ipv6)
1432 /* EPSV is disabled but we are connected to an IPv6 host, so we ignore the
1433 request and enable EPSV again! */
1434 conn->bits.ftp_use_epsv = TRUE;
1435#endif
1437 modeoff = conn->bits.ftp_use_epsv ? 0 : 1;
1439 result = Curl_pp_sendf(data, &ftpc->pp, "%s", mode[modeoff]);
1440 if(!result) {
1441 ftpc->count1 = modeoff;
1442 ftp_state(data, ftpc, FTP_PASV);
1443 infof(data, "Connect data stream passively");
1445 return result;
1449 * ftp_state_prepare_transfer() starts PORT, PASV or PRET etc.
1451 * REST is the last command in the chain of commands when a "head"-like
1452 * request is made. Thus, if an actual transfer is to be made this is where we
1453 * take off for real.
1454 */
1455static CURLcode ftp_state_prepare_transfer(struct Curl_easy *data,
1456 struct ftp_conn *ftpc,
1457 struct FTP *ftp)
1459 CURLcode result = CURLE_OK;
1460 struct connectdata *conn = data->conn;
1462 if(ftp->transfer != PPTRANSFER_BODY) {
1463 /* does not transfer any data */
1465 /* still possibly do PRE QUOTE jobs */
1466 ftp_state(data, ftpc, FTP_RETR_PREQUOTE);
1467 result = ftp_state_quote(data, ftpc, ftp, TRUE, FTP_RETR_PREQUOTE);
1469 else if(data->set.ftp_use_port) {
1470 /* We have chosen to use the PORT (or similar) command */
1471 result = ftp_state_use_port(data, ftpc, EPRT);
1473 else {
1474 /* We have chosen (this is default) to use the PASV (or similar) command */
1475 if(data->set.ftp_use_pret) {
1476 /* The user has requested that we send a PRET command
1477 to prepare the server for the upcoming PASV */
1478 if(!ftpc->file)
1479 result = Curl_pp_sendf(data, &ftpc->pp, "PRET %s",
1480 CURL_EASY_STR(data, STRING_CUSTOMREQUEST) ?
1481 CURL_EASY_STR(data, STRING_CUSTOMREQUEST) :
1482 (data->state.list_only ? "NLST" : "LIST"));
1483 else if(data->state.upload)
1484 result = Curl_pp_sendf(data, &ftpc->pp, "PRET STOR %s", ftpc->file);
1485 else
1486 result = Curl_pp_sendf(data, &ftpc->pp, "PRET RETR %s", ftpc->file);
1487 if(!result)
1488 ftp_state(data, ftpc, FTP_PRET);
1490 else
1491 result = ftp_state_use_pasv(data, ftpc, conn);
1493 return result;
1496static CURLcode ftp_state_rest(struct Curl_easy *data,
1497 struct ftp_conn *ftpc,
1498 struct FTP *ftp)
1500 CURLcode result = CURLE_OK;
1502 if((ftp->transfer != PPTRANSFER_BODY) && ftpc->file) {
1503 /* if a "head"-like request is being made (on a file) */
1505 /* Determine if server can respond to REST command and therefore
1506 whether it supports range */
1507 result = Curl_pp_sendf(data, &ftpc->pp, "REST %d", 0);
1508 if(!result)
1509 ftp_state(data, ftpc, FTP_REST);
1511 else
1512 result = ftp_state_prepare_transfer(data, ftpc, ftp);
1514 return result;
1517static CURLcode ftp_state_size(struct Curl_easy *data,
1518 struct ftp_conn *ftpc,
1519 struct FTP *ftp)
1521 CURLcode result = CURLE_OK;
1523 if((ftp->transfer == PPTRANSFER_INFO) && ftpc->file) {
1524 /* if a "head"-like request is being made (on a file) */
1526 /* we know ftpc->file is a valid pointer to a filename */
1527 result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
1528 if(!result)
1529 ftp_state(data, ftpc, FTP_SIZE);
1531 else
1532 result = ftp_state_rest(data, ftpc, ftp);
1534 return result;
1537static CURLcode ftp_state_list(struct Curl_easy *data,
1538 struct ftp_conn *ftpc,
1539 struct FTP *ftp)
1541 CURLcode result = CURLE_OK;
1543 /* If this output is to be machine-parsed, the NLST command might be better
1544 to use, since the LIST command output is not specified or standard in any
1545 way. It has turned out that the NLST list output is not the same on all
1546 servers either... */
1548 /* if FTPFILE_NOCWD was specified, we should add the path
1549 as argument for the LIST / NLST / or custom command.
1550 Whether the server will support this, is uncertain.
1552 The other ftp_filemethods will CWD into dir/dir/ first and
1553 then do LIST (in that case: nothing to do here) */
1554 const char *lstArg = NULL;
1555 int lstArglen = 0;
1556 char *cmd;
1558 if((data->set.ftp_filemethod == FTPFILE_NOCWD) && ftp->path) {
1559 /* URL-decode before evaluation: e.g. paths starting/ending with %2f */
1560 const char *rawPath = ftpc->rawpath;
1561 const char *slashPos = strrchr(rawPath, '/');
1562 if(slashPos) {
1563 /* chop off the file part if format is dir/file otherwise remove
1564 the trailing slash for dir/dir/ except for absolute path / */
1565 size_t n = slashPos - rawPath;
1566 if(n == 0)
1567 ++n;
1569 lstArg = rawPath;
1570 lstArglen = (int)n;
1574 cmd = curl_maprintf("%s%s%.*s",
1575 CURL_EASY_STR(data, STRING_CUSTOMREQUEST) ?
1576 CURL_EASY_STR(data, STRING_CUSTOMREQUEST) :
1577 (data->state.list_only ? "NLST" : "LIST"),
1578 lstArg ? " " : "",
1579 lstArglen, lstArg ? lstArg : "");
1581 if(!cmd)
1582 return CURLE_OUT_OF_MEMORY;
1584 result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd);
1585 curlx_free(cmd);
1587 if(!result)
1588 ftp_state(data, ftpc, FTP_LIST);
1590 return result;
1593static CURLcode ftp_state_list_prequote(struct Curl_easy *data,
1594 struct ftp_conn *ftpc,
1595 struct FTP *ftp)
1597 /* We have sent the TYPE, now we must send the list of prequote strings */
1598 return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_LIST_PREQUOTE);
1601static CURLcode ftp_state_retr_prequote(struct Curl_easy *data,
1602 struct ftp_conn *ftpc,
1603 struct FTP *ftp)
1605 /* We have sent the TYPE, now we must send the list of prequote strings */
1606 return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_RETR_PREQUOTE);
1609static CURLcode ftp_state_stor_prequote(struct Curl_easy *data,
1610 struct ftp_conn *ftpc,
1611 struct FTP *ftp)
1613 /* We have sent the TYPE, now we must send the list of prequote strings */
1614 return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_STOR_PREQUOTE);
1617static CURLcode ftp_state_type(struct Curl_easy *data,
1618 struct ftp_conn *ftpc,
1619 struct FTP *ftp)
1621 CURLcode result = CURLE_OK;
1623 /* If we have selected NOBODY and HEADER, it means that we only want file
1624 information. Which in FTP cannot be much more than the file size and
1625 date. */
1626 if(data->req.no_body && ftpc->file &&
1627 ftp_need_type(ftpc, (bool)data->state.prefer_ascii)) {
1628 /* The SIZE command is _not_ RFC 959 specified, and therefore many servers
1629 may not support it! It is however the only way we have to get a file's
1630 size! */
1632 ftp->transfer = PPTRANSFER_INFO;
1633 /* this means no actual transfer will be made */
1635 /* Some servers return different sizes for different modes, and thus we
1636 must set the proper type before we check the size */
1637 result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii,
1638 FTP_TYPE);
1639 if(result)
1640 return result;
1642 else
1643 result = ftp_state_size(data, ftpc, ftp);
1645 return result;
1648/* This is called after the CWD commands have been done in the beginning of
1649 the DO phase */
1650static CURLcode ftp_state_mdtm(struct Curl_easy *data,
1651 struct ftp_conn *ftpc,
1652 struct FTP *ftp)
1654 CURLcode result = CURLE_OK;
1656 /* Requested time of file or time-depended transfer? */
1657 if((data->set.get_filetime || data->set.timecondition) && ftpc->file) {
1659 /* we have requested to get the modified-time of the file, this is a white
1660 spot as the MDTM is not mentioned in RFC959 */
1661 result = Curl_pp_sendf(data, &ftpc->pp, "MDTM %s", ftpc->file);
1663 if(!result)
1664 ftp_state(data, ftpc, FTP_MDTM);
1666 else
1667 result = ftp_state_type(data, ftpc, ftp);
1669 return result;
1672/* This is called after the TYPE and possible quote commands have been sent */
1673static CURLcode ftp_state_ul_setup(struct Curl_easy *data,
1674 struct ftp_conn *ftpc,
1675 struct FTP *ftp,
1676 bool sizechecked)
1678 CURLcode result = CURLE_OK;
1679 curl_bit append = data->set.remote_append;
1681 if((data->state.resume_from && !sizechecked) ||
1682 ((data->state.resume_from > 0) && sizechecked)) {
1683 /* we are about to continue the uploading of a file
1684 1. get already existing file's size. We use the SIZE command for this
1685 which may not exist in the server! The SIZE command is not in
1686 RFC959.
1688 2. This used to set REST, but since we can do append, we issue no
1689 another ftp command. Skip the source file offset and APPEND the rest
1690 on the file instead
1692 3. pass file-size number of bytes in the source file
1693 4. lower the infilesize counter */
1694 /* => transfer as usual */
1695 int seekerr = CURL_SEEKFUNC_OK;
1697 if(data->state.resume_from < 0) {
1698 /* Got no given size to start from, figure it out */
1699 result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
1700 if(!result)
1701 ftp_state(data, ftpc, FTP_STOR_SIZE);
1702 return result;
1705 /* enable append */
1706 append = TRUE;
1708 /* Let's read off the proper amount of bytes from the input. */
1709 if(data->set.seek_func) {
1710 struct Curl_mapi_guard guard;
1711 CURL_CBAPI_START(&guard, data, easy_seek_func);
1712 seekerr = data->set.seek_func(data->set.seek_client,
1713 data->state.resume_from, SEEK_SET);
1714 CURL_CBAPI_END(&guard);
1717 if(seekerr != CURL_SEEKFUNC_OK) {
1718 curl_off_t passed = 0;
1719 if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
1720 failf(data, "Could not seek stream");
1721 return CURLE_FTP_COULDNT_USE_REST;
1723 /* seekerr == CURL_SEEKFUNC_CANTSEEK (cannot seek to offset) */
1724 do {
1725 char scratch[4 * 1024];
1726 size_t readthisamountnow =
1727 (data->state.resume_from - passed > (curl_off_t)sizeof(scratch)) ?
1728 sizeof(scratch) :
1729 curlx_sotouz(data->state.resume_from - passed);
1731 size_t actuallyread =
1732 data->state.fread_func(scratch, 1, readthisamountnow,
1733 data->state.in);
1735 passed += actuallyread;
1736 if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
1737 /* this checks for greater-than only to make sure that the
1738 CURL_READFUNC_ABORT return code still aborts */
1739 failf(data, "Failed to read data");
1740 return CURLE_FTP_COULDNT_USE_REST;
1742 } while(passed < data->state.resume_from);
1744 /* now, decrease the size of the read */
1745 if(data->state.infilesize > 0) {
1746 data->state.infilesize -= data->state.resume_from;
1748 if(data->state.infilesize <= 0) {
1749 infof(data, "File already completely uploaded");
1751 /* no data to transfer */
1752 Curl_xfer_setup_nop(data);
1754 /* Set ->transfer so that we will not get any error in
1755 * ftp_done() because we did not transfer anything! */
1756 ftp->transfer = PPTRANSFER_NONE;
1758 ftp_state(data, ftpc, FTP_STOP);
1759 return CURLE_OK;
1762 /* we have passed, proceed as normal */
1763 } /* resume_from */
1765 result = Curl_pp_sendf(data, &ftpc->pp, append ? "APPE %s" : "STOR %s",
1766 ftpc->file);
1767 if(!result)
1768 ftp_state(data, ftpc, FTP_STOR);
1770 return result;
1773static CURLcode ftp_state_retr(struct Curl_easy *data,
1774 struct ftp_conn *ftpc,
1775 struct FTP *ftp,
1776 curl_off_t filesize)
1778 CURLcode result = CURLE_OK;
1780 CURL_TRC_FTP(data, "[%s] ftp_state_retr()", FTP_CSTATE(ftpc));
1781 if(data->set.max_filesize && (filesize > data->set.max_filesize)) {
1782 failf(data, "Maximum file size exceeded");
1783 return CURLE_FILESIZE_EXCEEDED;
1785 ftp->downloadsize = filesize;
1787 if(data->state.resume_from) {
1788 /* We always (attempt to) get the size of downloads, so it is done before
1789 this even when not doing resumes. */
1790 if(filesize == -1) {
1791 infof(data, "ftp server does not support SIZE");
1792 /* We could not get the size and therefore we cannot know if there
1793 really is a part of the file left to get, although the server will
1794 close the connection when we start the connection so it will not
1795 cause us any harm, not make us exit as nicely. */
1797 else {
1798 /* We got a file size report, so we check that there actually is a
1799 part of the file left to get, or else we go home. */
1800 if(data->state.resume_from < 0) {
1801 /* We are supposed to download the last abs(from) bytes */
1802 if(filesize < -data->state.resume_from) {
1803 failf(data, "Offset (%" FMT_OFF_T
1804 ") was beyond file size (%" FMT_OFF_T ")",
1805 data->state.resume_from, filesize);
1806 return CURLE_BAD_DOWNLOAD_RESUME;
1808 /* convert to size to download */
1809 ftp->downloadsize = -data->state.resume_from;
1810 /* download from where? */
1811 data->state.resume_from = filesize - ftp->downloadsize;
1813 else {
1814 if(filesize < data->state.resume_from) {
1815 failf(data, "Offset (%" FMT_OFF_T
1816 ") was beyond file size (%" FMT_OFF_T ")",
1817 data->state.resume_from, filesize);
1818 return CURLE_BAD_DOWNLOAD_RESUME;
1820 /* Now store the number of bytes we are expected to download */
1821 ftp->downloadsize = filesize - data->state.resume_from;
1825 if(ftp->downloadsize == 0) {
1826 /* no data to transfer */
1827 Curl_xfer_setup_nop(data);
1828 infof(data, "File already completely downloaded");
1830 /* Set ->transfer so that we will not get any error in ftp_done()
1831 * because we did not transfer the any file */
1832 ftp->transfer = PPTRANSFER_NONE;
1833 ftp_state(data, ftpc, FTP_STOP);
1834 return CURLE_OK;
1837 /* Set resume file transfer offset */
1838 infof(data, "Instructs server to resume from offset %" FMT_OFF_T,
1839 data->state.resume_from);
1841 result = Curl_pp_sendf(data, &ftpc->pp, "REST %" FMT_OFF_T,
1842 data->state.resume_from);
1843 if(!result)
1844 ftp_state(data, ftpc, FTP_RETR_REST);
1846 else {
1847 /* no resume */
1848 result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
1849 if(!result)
1850 ftp_state(data, ftpc, FTP_RETR);
1853 return result;
1856static CURLcode ftp_state_quote(struct Curl_easy *data,
1857 struct ftp_conn *ftpc,
1858 struct FTP *ftp,
1859 bool init,
1860 ftpstate instate)
1862 CURLcode result = CURLE_OK;
1863 bool quote = FALSE;
1864 struct curl_slist *item;
1866 switch(instate) {
1867 case FTP_QUOTE:
1868 default:
1869 item = data->set.quote;
1870 break;
1871 case FTP_RETR_PREQUOTE:
1872 case FTP_STOR_PREQUOTE:
1873 case FTP_LIST_PREQUOTE:
1874 item = data->set.prequote;
1875 break;
1876 case FTP_POSTQUOTE:
1877 item = data->set.postquote;
1878 break;
1881 /*
1882 * This state uses:
1883 * 'count1' to iterate over the commands to send
1884 * 'count2' to store whether to allow commands to fail
1885 */
1887 if(init)
1888 ftpc->count1 = 0;
1889 else
1890 ftpc->count1++;
1892 if(item) {
1893 int i = 0;
1895 /* Skip count1 items in the linked list */
1896 while((i < ftpc->count1) && item) {
1897 item = item->next;
1898 i++;
1900 if(item) {
1901 const char *cmd = item->data;
1902 if(cmd[0] == '*') {
1903 cmd++;
1904 ftpc->count2 = 1; /* the sent command is allowed to fail */
1906 else
1907 ftpc->count2 = 0; /* failure means cancel operation */
1909 result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd);
1910 if(result)
1911 return result;
1912 ftp_state(data, ftpc, instate);
1913 quote = TRUE;
1917 if(!quote) {
1918 /* No more quote to send, continue to ... */
1919 switch(instate) {
1920 case FTP_QUOTE:
1921 default:
1922 result = ftp_state_cwd(data, ftpc, ftp);
1923 break;
1924 case FTP_RETR_PREQUOTE:
1925 if(ftp->transfer != PPTRANSFER_BODY)
1926 ftp_state(data, ftpc, FTP_STOP);
1927 else {
1928 if(ftpc->known_filesize != -1) {
1929 Curl_pgrsSetDownloadSize(data, ftpc->known_filesize);
1930 result = ftp_state_retr(data, ftpc, ftp, ftpc->known_filesize);
1932 else {
1933 if(data->set.ignorecl || data->state.prefer_ascii) {
1934 /* 'ignorecl' is used to support download of growing files. It
1935 prevents the state machine from requesting the file size from
1936 the server. With an unknown file size the download continues
1937 until the server terminates it, otherwise the client stops if
1938 the received byte count exceeds the reported file size. Set
1939 option CURLOPT_IGNORE_CONTENT_LENGTH to 1 to enable this
1940 behavior.
1942 In addition: asking for the size for 'TYPE A' transfers is not
1943 constructive since servers do not report the converted size.
1944 Thus, skip it. */
1945 result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
1946 if(!result)
1947 ftp_state(data, ftpc, FTP_RETR);
1949 else {
1950 result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
1951 if(!result)
1952 ftp_state(data, ftpc, FTP_RETR_SIZE);
1956 break;
1957 case FTP_STOR_PREQUOTE:
1958 result = ftp_state_ul_setup(data, ftpc, ftp, FALSE);
1959 break;
1960 case FTP_POSTQUOTE:
1961 break;
1962 case FTP_LIST_PREQUOTE:
1963 ftp_state(data, ftpc, FTP_LIST_TYPE);
1964 result = ftp_state_list(data, ftpc, ftp);
1965 break;
1969 return result;
1972/* called from ftp_state_pasv_resp to switch to PASV in case of EPSV
1973 problems */
1974static CURLcode ftp_epsv_disable(struct Curl_easy *data,
1975 struct ftp_conn *ftpc,
1976 struct connectdata *conn)
1978 CURLcode result = CURLE_OK;
1980 if(conn->bits.ipv6 && !Curl_conn_is_tunneling(conn, FIRSTSOCKET)) {
1981 /* We cannot disable EPSV when doing IPv6, so this is instead a fail */
1982 failf(data, "Failed EPSV attempt, exiting");
1983 return CURLE_WEIRD_SERVER_REPLY;
1986 infof(data, "Failed EPSV attempt. Disabling EPSV");
1987 /* disable it for next transfer */
1988 conn->bits.ftp_use_epsv = FALSE;
1989 close_secondarysocket(data, ftpc);
1990 data->state.errorbuf = FALSE; /* allow error message to get
1991 rewritten */
1992 result = Curl_pp_sendf(data, &ftpc->pp, "%s", "PASV");
1993 if(!result) {
1994 ftpc->count1++;
1995 /* remain in/go to the FTP_PASV state */
1996 ftp_state(data, ftpc, FTP_PASV);
1998 return result;
2001static CURLcode ftp_control_addr_dup(struct Curl_easy *data, char **newhostp)
2003 struct connectdata *conn = data->conn;
2004 struct ip_quadruple ipquad;
2005 bool is_ipv6;
2007 /* Returns the control connection IP address.
2008 If a proxy tunnel is used, returns the original hostname instead, because
2009 the effective control connection address is the proxy address,
2010 not the ftp host. */
2011#ifndef CURL_DISABLE_PROXY
2012 if(Curl_conn_is_tunneling(conn, FIRSTSOCKET))
2013 *newhostp = curlx_strdup(conn->origin->hostname);
2014 else
2015#endif
2016 if(!Curl_conn_get_ip_info(data, conn, FIRSTSOCKET, &is_ipv6, &ipquad) &&
2017 *ipquad.remote_ip)
2018 *newhostp = curlx_strdup(ipquad.remote_ip);
2019 else {
2020 /* failed to get the remote_ip of the DATA connection */
2021 failf(data, "unable to get peername of DATA connection");
2022 *newhostp = NULL;
2023 return CURLE_FTP_CANT_GET_HOST;
2025 return *newhostp ? CURLE_OK : CURLE_OUT_OF_MEMORY;
2028static bool match_pasv_6nums(const char *p,
2029 unsigned int *array) /* 6 numbers */
2031 int i;
2032 for(i = 0; i < 6; i++) {
2033 curl_off_t num;
2034 if(i) {
2035 if(*p != ',')
2036 return FALSE;
2037 p++;
2039 if(curlx_str_number(&p, &num, 0xff))
2040 return FALSE;
2041 array[i] = (unsigned int)num;
2043 return TRUE;
2046static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
2047 struct ftp_conn *ftpc,
2048 int ftpcode)
2050 struct connectdata *conn = data->conn;
2051 CURLcode result;
2052 const struct pingpong *pp = &ftpc->pp;
2053 char *newhost = NULL;
2054 unsigned short newport = 0;
2055 const char *str = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first
2056 letter */
2057 if((ftpc->count1 == 0) &&
2058 (ftpcode == 229)) {
2059 /* positive EPSV response */
2060 const char *ptr = strchr(str, '(');
2061 if(ptr) {
2062 char sep;
2063 ptr++;
2064 /* |||12345| */
2065 sep = ptr[0];
2066 if(sep && (ptr[1] == sep) && (ptr[2] == sep) && ISDIGIT(ptr[3])) {
2067 const char *p = &ptr[3];
2068 curl_off_t num;
2069 if(curlx_str_number(&p, &num, 0xffff) || (*p != sep)) {
2070 failf(data, "Illegal port number in EPSV reply");
2071 return CURLE_FTP_WEIRD_PASV_REPLY;
2073 newport = (unsigned short)num;
2074 result = ftp_control_addr_dup(data, &newhost);
2075 if(result)
2076 return result;
2078 else
2079 ptr = NULL;
2081 if(!ptr) {
2082 failf(data, "Weirdly formatted EPSV reply");
2083 return CURLE_FTP_WEIRD_PASV_REPLY;
2086 else if((ftpc->count1 == 1) &&
2087 (ftpcode == 227)) {
2088 /* positive PASV response */
2089 unsigned int ip[6];
2091 /*
2092 * Scan for a sequence of six comma-separated numbers and use them as
2093 * IP+port indicators.
2095 * Found reply-strings include:
2096 * "227 Entering Passive Mode (127,0,0,1,4,51)"
2097 * "227 Data transfer will passively listen to 127,0,0,1,4,51"
2098 * "227 Entering passive mode. 127,0,0,1,4,51"
2099 */
2100 while(*str) {
2101 if(match_pasv_6nums(str, ip))
2102 break;
2103 str++;
2106 if(!*str) {
2107 failf(data, "Could not interpret the 227-response");
2108 return CURLE_FTP_WEIRD_227_FORMAT;
2111 /* we got OK from server */
2112 if(data->set.ftp_skip_ip) {
2113 /* told to ignore the remotely given IP but instead use the host we used
2114 for the control connection */
2115 infof(data, "Skip %u.%u.%u.%u for data connection, reuse %s instead",
2116 ip[0], ip[1], ip[2], ip[3], conn->origin->hostname);
2117 result = ftp_control_addr_dup(data, &newhost);
2118 if(result)
2119 return result;
2121 else
2122 newhost = curl_maprintf("%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
2124 if(!newhost)
2125 return CURLE_OUT_OF_MEMORY;
2127 newport = (unsigned short)(((ip[4] << 8) + ip[5]) & 0xffff);
2129 else if(ftpc->count1 == 0) {
2130 /* EPSV failed, move on to PASV */
2131 return ftp_epsv_disable(data, ftpc, conn);
2133 else {
2134 failf(data, "Bad PASV/EPSV response: %03d", ftpcode);
2135 return CURLE_FTP_WEIRD_PASV_REPLY;
2138 DEBUGASSERT(newhost);
2139 Curl_peer_unlink(&conn->origin2);
2140 result = Curl_peer_create(data, conn->scheme, newhost, newport,
2141 &conn->origin2);
2142 if(result)
2143 goto error;
2145 /* If FIRSTSOCKET goes via another peer, SECONDARY needs as well,
2146 * but with its new port. */
2147 if(conn->via_peer) {
2148 Curl_peer_unlink(&conn->via_peer2);
2149 result = Curl_peer_create(data, conn->via_peer->scheme,
2150 conn->via_peer->hostname, newport,
2151 &conn->via_peer2);
2152 if(result)
2153 goto error;
2156 result = Curl_conn_setup(data, conn, SECONDARYSOCKET,
2157 conn->bits.ftp_use_data_ssl ?
2158 CURL_CF_SSL_ENABLE : CURL_CF_SSL_DISABLE);
2160 if(result) {
2161 if((result != CURLE_OUT_OF_MEMORY) &&
2162 (ftpc->count1 == 0) && (ftpcode == 229)) {
2163 result = ftp_epsv_disable(data, ftpc, conn);
2165 goto error;
2168 /*
2169 * When this is used from the multi interface, this might have returned with
2170 * the 'connected' set to FALSE and thus we are now awaiting a non-blocking
2171 * connect to connect.
2172 */
2174#ifdef CURLVERBOSE
2175 if(data->set.verbose) {
2176 /* Dump information about this second connection when we have issued
2177 * a PASV command. */
2178 infof(data, "Connecting to %s port %d",
2179 conn->origin2->hostname, conn->origin2->port);
2181#endif
2183 conn->bits.do_more = TRUE;
2184 ftp_state(data, ftpc, FTP_STOP); /* this phase is completed */
2186error:
2187 curlx_free(newhost);
2188 return result;
2191/* called repeatedly until done from multi.c */
2192static CURLcode ftp_statemach(struct Curl_easy *data,
2193 struct ftp_conn *ftpc,
2194 bool *done)
2196 CURLcode result = Curl_pp_statemach(data, &ftpc->pp, FALSE, FALSE);
2198 /* Check for the state outside of the Curl_socket_check() return code checks
2199 since at times we are in fact already in this state when this function
2200 gets called. */
2201 *done = (ftpc->state == FTP_STOP);
2203 return result;
2207 * ftp_do_more()
2209 * This function shall be called when the second FTP (data) connection is
2210 * connected.
2212 * 'more' can return DOMORE_INCOMPLETE, DOMORE_DONE or DOMORE_GOBACK
2213 * (which is for when PASV is being sent to retry a failed EPSV).
2214 */
2215static CURLcode ftp_do_more(struct Curl_easy *data, domore *more)
2217 struct connectdata *conn = data->conn;
2218 struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
2219 struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
2220 CURLcode result = CURLE_OK;
2221 bool connected = FALSE;
2222 bool complete = FALSE;
2223 /* the ftp struct is inited in ftp_connect(). If we are connecting to an HTTP
2224 * proxy then the state will not be valid until after that connection is
2225 * complete */
2227 if(!ftpc || !ftp)
2228 return CURLE_FAILED_INIT;
2230 *more = DOMORE_INCOMPLETE; /* default to stay in the state */
2232 /* if the second connection has been set up, try to connect it fully
2233 * to the remote host. This may not complete at this time, for several
2234 * reasons:
2235 * - we do EPTR and the server will not connect to our listen socket
2236 * until we send more FTP commands
2237 * - an SSL filter is in place and the server will not start the TLS
2238 * handshake until we send more FTP commands
2239 */
2240 if(conn->cfilter[SECONDARYSOCKET]) {
2241 bool is_eptr = Curl_conn_is_tcp_listen(data, SECONDARYSOCKET);
2242 result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected);
2243 if(result == CURLE_OUT_OF_MEMORY)
2244 return result;
2245 if(result || (!connected && !is_eptr &&
2246 !Curl_conn_is_ip_connected(data, SECONDARYSOCKET))) {
2247 if(result && !is_eptr && (ftpc->count1 == 0)) {
2248 *more = DOMORE_GOBACK; /* go back to DOING please */
2249 /* this is a EPSV connect failing, try PASV instead */
2250 return ftp_epsv_disable(data, ftpc, conn);
2252 return result;
2256 if(ftpc->state) {
2257 /* already in a state so skip the initial commands.
2258 They are only done to kickstart the do_more state */
2259 result = ftp_statemach(data, ftpc, &complete);
2261 if(complete)
2262 *more = DOMORE_DONE;
2264 /* if we got an error or if we do not wait for a data connection return
2265 immediately */
2266 if(result || !ftpc->wait_data_conn)
2267 return result;
2269 /* if we reach the end of the FTP state machine here, *complete will be
2270 TRUE but so is ftpc->wait_data_conn, which says we need to wait for the
2271 data connection and therefore we are not actually complete */
2272 *more = DOMORE_INCOMPLETE;
2275 if(ftp->transfer <= PPTRANSFER_INFO) {
2276 /* a transfer is about to take place, or if not a filename was given so we
2277 will do a SIZE on it later and then we need the right TYPE first */
2279 if(ftpc->wait_data_conn) {
2280 bool serv_conned;
2282 result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &serv_conned);
2283 if(result)
2284 return result; /* Failed to accept data connection */
2286 if(serv_conned) {
2287 /* It looks data connection is established */
2288 ftpc->wait_data_conn = FALSE;
2289 result = ftp_initiate_transfer(data, ftpc);
2291 if(result)
2292 return result;
2294 *more = DOMORE_DONE; /* this state is now complete when the server has
2295 connected back to us */
2297 else {
2298 result = ftp_check_ctrl_on_data_wait(data, ftpc);
2299 if(result)
2300 return result;
2303 else if(data->state.upload) {
2304 result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii,
2305 FTP_STOR_TYPE);
2306 if(result)
2307 return result;
2309 result = ftp_statemach(data, ftpc, &complete);
2310 /* ftp_nb_type() might have skipped sending `TYPE A|I` when not
2311 * deemed necessary and directly sent `STORE name`. If this was
2312 * then complete, but we are still waiting on the data connection,
2313 * the transfer has not been initiated yet. */
2314 *more = (!ftpc->wait_data_conn && complete) ?
2315 DOMORE_DONE : DOMORE_INCOMPLETE;
2317 else {
2318 /* download */
2319 ftp->downloadsize = -1; /* unknown as of yet */
2321 result = Curl_range(data);
2323 if(result == CURLE_OK && data->req.maxdownload >= 0) {
2324 /* Do not check for successful transfer */
2325 ftpc->dont_check = TRUE;
2328 if(result)
2329 return result;
2331 if((data->state.list_only || !ftpc->file) &&
2332 !data->set.prequote) {
2333 /* The specified path ends with a slash, and therefore we think this
2334 is a directory that is requested, use LIST. Before that, we also
2335 need to set ASCII transfer mode. */
2337 /* Only if a body transfer was requested. */
2338 if(ftp->transfer == PPTRANSFER_BODY) {
2339 result = ftp_nb_type(data, ftpc, ftp, TRUE, FTP_LIST_TYPE);
2340 if(result)
2341 return result;
2343 /* otherwise fall through */
2345 else {
2346 if(data->set.prequote && !ftpc->file) {
2347 result = ftp_nb_type(data, ftpc, ftp, TRUE,
2348 FTP_RETR_LIST_TYPE);
2350 else {
2351 result = ftp_nb_type(data, ftpc, ftp, (bool)data->state.prefer_ascii,
2352 FTP_RETR_TYPE);
2354 if(result)
2355 return result;
2358 result = ftp_statemach(data, ftpc, &complete);
2359 if(complete)
2360 *more = DOMORE_DONE;
2362 return result;
2365 /* no data to transfer */
2366 Curl_xfer_setup_nop(data);
2368 if(!ftpc->wait_data_conn) {
2369 /* no waiting for the data connection so this is now complete */
2370 *more = DOMORE_DONE;
2371 CURL_TRC_FTP(data, "[%s] DO-MORE phase ends with %d", FTP_CSTATE(ftpc),
2372 (int)result);
2375 return result;
2378/* call this when the DO phase has completed */
2379static CURLcode ftp_dophase_done(struct Curl_easy *data,
2380 struct ftp_conn *ftpc,
2381 struct FTP *ftp,
2382 bool connected)
2384 if(connected) {
2385 domore completed;
2386 CURLcode result = ftp_do_more(data, &completed);
2388 if(result) {
2389 close_secondarysocket(data, ftpc);
2390 return result;
2394 if(ftp->transfer != PPTRANSFER_BODY)
2395 /* no data to transfer */
2396 Curl_xfer_setup_nop(data);
2397 else if(!connected)
2398 /* since we did not connect now, we want do_more to get called */
2399 data->conn->bits.do_more = TRUE;
2401 ftpc->ctl_valid = TRUE; /* seems good */
2403 return CURLE_OK;
2406static CURLcode ftp_state_port_resp(struct Curl_easy *data,
2407 struct ftp_conn *ftpc,
2408 struct FTP *ftp,
2409 int ftpcode)
2411 struct connectdata *conn = data->conn;
2412 ftpport fcmd = (ftpport)ftpc->count1;
2413 CURLcode result = CURLE_OK;
2415 /* The FTP spec tells a positive response should have code 200.
2416 Be more permissive here to tolerate deviant servers. */
2417 if(ftpcode / 100 != 2) {
2418 /* the command failed */
2420 if(EPRT == fcmd) {
2421 infof(data, "disabling EPRT usage");
2422 conn->bits.ftp_use_eprt = FALSE;
2424 fcmd++;
2426 if(fcmd == DONE) {
2427 failf(data, "Failed to do PORT");
2428 result = CURLE_FTP_PORT_FAILED;
2430 else
2431 /* try next */
2432 result = ftp_state_use_port(data, ftpc, fcmd);
2434 else {
2435 infof(data, "Connect data stream actively");
2436 ftp_state(data, ftpc, FTP_STOP); /* end of DO phase */
2437 result = ftp_dophase_done(data, ftpc, ftp, FALSE);
2440 return result;
2443/* return TRUE on error, FALSE on success */
2444static bool twodigit(const char *p, int *val)
2446 if(!ISDIGIT(p[0]) || !ISDIGIT(p[1]))
2447 return TRUE;
2448 /* curlx_hexval() works fine here since we make sure it is decimal above */
2449 *val = (curlx_hexval(p[0]) * 10) + curlx_hexval(p[1]);
2450 return FALSE;
2454 * @unittest 1668
2455 */
2456UNITTEST bool ftp_213_date(const char *p, int *year, int *month, int *day,
2457 int *hour, int *minute, int *second);
2458UNITTEST bool ftp_213_date(const char *p, int *year, int *month, int *day,
2459 int *hour, int *minute, int *second)
2461 int century;
2462 if((strlen(p) < 14) || twodigit(&p[0], &century) || twodigit(&p[2], year) ||
2463 twodigit(&p[4], month) || twodigit(&p[6], day) ||
2464 twodigit(&p[8], hour) || twodigit(&p[10], minute) ||
2465 twodigit(&p[12], second))
2466 return FALSE;
2468 *year += century * 100;
2469 if((*month > 12) || (*day > 31) || (*hour > 23) || (*minute > 59) ||
2470 (*second > 60))
2471 return FALSE;
2472 return TRUE;
2475static CURLcode client_write_header(struct Curl_easy *data,
2476 char *buf, size_t blen)
2478 /* Some replies from an FTP server are written to the client
2479 * as CLIENTWRITE_HEADER, formatted as if they came from a
2480 * HTTP conversation.
2481 * In all protocols, CLIENTWRITE_HEADER data is only passed to
2482 * the body write callback when data->set.include_header is set
2483 * via CURLOPT_HEADER.
2484 * For historic reasons, FTP never played this game and expects
2485 * all its headers to do that always. Set that flag during the
2486 * call to Curl_client_write() so it does the right thing.
2488 * Notice that we cannot enable this flag for FTP in general,
2489 * as an FTP transfer might involve an HTTP proxy connection and
2490 * headers from CONNECT should not automatically be part of the
2491 * output. */
2492 CURLcode result;
2493 bool save = (bool)data->set.include_header;
2494 data->set.include_header = TRUE;
2495 result = Curl_client_write(data, CLIENTWRITE_HEADER, buf, blen);
2496 data->set.include_header = save;
2497 return result;
2500static CURLcode ftp_state_mdtm_resp(struct Curl_easy *data,
2501 struct ftp_conn *ftpc,
2502 struct FTP *ftp,
2503 int ftpcode)
2505 CURLcode result = CURLE_OK;
2507 switch(ftpcode) {
2508 case 213: {
2509 /* we got a time. Format should be: "YYYYMMDDHHMMSS[.sss]" where the
2510 last .sss part is optional and means fractions of a second */
2511 int year, month, day, hour, minute, second;
2512 struct pingpong *pp = &ftpc->pp;
2513 const char *resp = curlx_dyn_ptr(&pp->recvbuf) + 4;
2514 bool showtime = FALSE;
2515 if(ftp_213_date(resp, &year, &month, &day, &hour, &minute, &second)) {
2516 /* we have a time, reformat it */
2517 char timebuf[24];
2518 curl_msnprintf(timebuf, sizeof(timebuf),
2519 "%04d%02d%02d %02d:%02d:%02d GMT",
2520 year, month, day, hour, minute, second);
2521 /* now, convert this into a time() value: */
2522 if(!Curl_getdate_capped(timebuf, &data->info.filetime))
2523 showtime = TRUE;
2526 /* If we asked for a time of the file and we actually got one as well,
2527 we "emulate" an HTTP-style header in our output. */
2529#if defined(CURL_HAVE_DIAG) && (defined(__DJGPP__) || defined(__AMIGA__))
2530#pragma GCC diagnostic push
2531/* 'time_t' is unsigned in MSDOS and AmigaOS. Silence:
2532 warning: comparison of unsigned expression in '>= 0' is always true */
2533#pragma GCC diagnostic ignored "-Wtype-limits"
2534#endif
2535 if(data->req.no_body && ftpc->file &&
2536 data->set.get_filetime && showtime) {
2537#if defined(CURL_HAVE_DIAG) && (defined(__DJGPP__) || defined(__AMIGA__))
2538#pragma GCC diagnostic pop
2539#endif
2540 char headerbuf[128];
2541 int headerbuflen;
2542 time_t filetime = data->info.filetime;
2543 struct tm buffer;
2544 const struct tm *tm = &buffer;
2546 result = curlx_gmtime(filetime, &buffer);
2547 if(result)
2548 return result;
2550 /* format: "Tue, 15 Nov 1994 12:45:26" */
2551 headerbuflen =
2552 curl_msnprintf(headerbuf, sizeof(headerbuf),
2553 "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d "
2554 "GMT\r\n",
2555 Curl_wkday[tm->tm_wday ? tm->tm_wday-1 : 6],
2556 tm->tm_mday,
2557 Curl_month[tm->tm_mon],
2558 tm->tm_year + 1900,
2559 tm->tm_hour,
2560 tm->tm_min,
2561 tm->tm_sec);
2562 result = client_write_header(data, headerbuf, headerbuflen);
2563 if(result)
2564 return result;
2565 } /* end of a ridiculous amount of conditionals */
2567 break;
2568 default:
2569 infof(data, "unsupported MDTM reply format");
2570 break;
2571 case 550: /* 550 is used for several different problems, e.g.
2572 "No such file or directory" or "Permission denied".
2573 It does not mean that the file does not exist at all. */
2574 infof(data, "MDTM failed: file does not exist or permission problem,"
2575 " continuing");
2576 break;
2579 if(data->set.timecondition) {
2580 if((data->info.filetime > 0) && (data->set.timevalue > 0)) {
2581 switch(data->set.timecondition) {
2582 case CURL_TIMECOND_IFMODSINCE:
2583 default:
2584 if(data->info.filetime <= data->set.timevalue) {
2585 infof(data, "The requested document is not new enough");
2586 ftp->transfer = PPTRANSFER_NONE; /* mark to not transfer data */
2587 data->info.timecond = TRUE;
2588 ftp_state(data, ftpc, FTP_STOP);
2589 return CURLE_OK;
2591 break;
2592 case CURL_TIMECOND_IFUNMODSINCE:
2593 if(data->info.filetime > data->set.timevalue) {
2594 infof(data, "The requested document is not old enough");
2595 ftp->transfer = PPTRANSFER_NONE; /* mark to not transfer data */
2596 data->info.timecond = TRUE;
2597 ftp_state(data, ftpc, FTP_STOP);
2598 return CURLE_OK;
2600 break;
2601 } /* switch */
2603 else {
2604 infof(data, "Skipping time comparison");
2608 if(!result)
2609 result = ftp_state_type(data, ftpc, ftp);
2611 return result;
2614static CURLcode ftp_state_type_resp(struct Curl_easy *data,
2615 struct ftp_conn *ftpc,
2616 struct FTP *ftp,
2617 int ftpcode,
2618 ftpstate instate)
2620 CURLcode result = CURLE_OK;
2622 if(ftpcode / 100 != 2) {
2623 /* "sasserftpd" and "(u)r(x)bot ftpd" both responds with 226 after a
2624 successful 'TYPE I'. While that is not as RFC959 says, it is still a
2625 positive response code and we allow that. */
2626 failf(data, "Could not set desired mode");
2627 return CURLE_FTP_COULDNT_SET_TYPE;
2629 if(ftpcode != 200)
2630 infof(data, "Got a %03d response code instead of the assumed 200",
2631 ftpcode);
2633 if(instate == FTP_TYPE)
2634 result = ftp_state_size(data, ftpc, ftp);
2635 else if(instate == FTP_LIST_TYPE)
2636 result = ftp_state_list(data, ftpc, ftp);
2637 else if(instate == FTP_RETR_TYPE)
2638 result = ftp_state_retr_prequote(data, ftpc, ftp);
2639 else if(instate == FTP_STOR_TYPE)
2640 result = ftp_state_stor_prequote(data, ftpc, ftp);
2641 else if(instate == FTP_RETR_LIST_TYPE)
2642 result = ftp_state_list_prequote(data, ftpc, ftp);
2644 return result;
2647static CURLcode ftp_state_size_resp(struct Curl_easy *data,
2648 struct ftp_conn *ftpc,
2649 struct FTP *ftp,
2650 int ftpcode,
2651 ftpstate instate)
2653 CURLcode result = CURLE_OK;
2654 curl_off_t filesize = -1;
2655 const char *buf = curlx_dyn_ptr(&ftpc->pp.recvbuf);
2656 size_t len = ftpc->pp.nfinal;
2658 /* get the size from the ascii string: */
2659 if(ftpcode == 213) {
2660 /* To allow servers to prepend "rubbish" in the response string, we scan
2661 for all the digits at the end of the response and parse only those as a
2662 number. */
2663 const char *start = &buf[4];
2664 const char *fdigit = memchr(start, '\r', len - 4);
2665 if(fdigit) {
2666 fdigit--;
2667 if(*fdigit == '\n')
2668 fdigit--;
2669 while(ISDIGIT(fdigit[-1]) && (fdigit > start))
2670 fdigit--;
2672 else
2673 fdigit = start;
2674 if(curlx_str_number(&fdigit, &filesize, CURL_OFF_T_MAX))
2675 filesize = -1; /* size remain unknown */
2677 else if(ftpcode == 550 && /* "No such file or directory" */
2678 /* allow a SIZE failure for (resumed) uploads, when probing what
2679 command to use */
2680 instate != FTP_STOR_SIZE) {
2681 failf(data, "The file does not exist");
2682 return CURLE_REMOTE_FILE_NOT_FOUND;
2685 if(instate == FTP_SIZE) {
2686 if(filesize != -1) {
2687 char clbuf[128];
2688 int clbuflen = curl_msnprintf(clbuf, sizeof(clbuf),
2689 "Content-Length: %" FMT_OFF_T "\r\n",
2690 filesize);
2691 result = client_write_header(data, clbuf, clbuflen);
2692 if(result)
2693 return result;
2695 Curl_pgrsSetDownloadSize(data, filesize);
2696 result = ftp_state_rest(data, ftpc, ftp);
2698 else if(instate == FTP_RETR_SIZE) {
2699 Curl_pgrsSetDownloadSize(data, filesize);
2700 result = ftp_state_retr(data, ftpc, ftp, filesize);
2702 else if(instate == FTP_STOR_SIZE) {
2703 data->state.resume_from = filesize;
2704 result = ftp_state_ul_setup(data, ftpc, ftp, TRUE);
2707 return result;
2710static CURLcode ftp_state_rest_resp(struct Curl_easy *data,
2711 struct ftp_conn *ftpc,
2712 struct FTP *ftp,
2713 int ftpcode,
2714 ftpstate instate)
2716 CURLcode result = CURLE_OK;
2718 switch(instate) {
2719 case FTP_REST:
2720 default:
2721 if(ftpcode == 350) {
2722 char buffer[24] = { "Accept-ranges: bytes\r\n" };
2723 result = client_write_header(data, buffer, strlen(buffer));
2724 if(result)
2725 return result;
2727 result = ftp_state_prepare_transfer(data, ftpc, ftp);
2728 break;
2730 case FTP_RETR_REST:
2731 if(ftpcode != 350) {
2732 failf(data, "Could not use REST");
2733 result = CURLE_FTP_COULDNT_USE_REST;
2735 else {
2736 result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
2737 if(!result)
2738 ftp_state(data, ftpc, FTP_RETR);
2740 break;
2743 return result;
2746static CURLcode ftp_state_stor_resp(struct Curl_easy *data,
2747 struct ftp_conn *ftpc,
2748 int ftpcode)
2750 CURLcode result = CURLE_OK;
2752 if(ftpcode >= 400) {
2753 failf(data, "Failed FTP upload: %0d", ftpcode);
2754 ftp_state(data, ftpc, FTP_STOP);
2755 return CURLE_UPLOAD_FAILED;
2758 /* PORT means we are now awaiting the server to connect to us. */
2759 if(data->set.ftp_use_port) {
2760 bool connected;
2762 ftp_state(data, ftpc, FTP_STOP); /* no longer in STOR state */
2764 result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected);
2765 if(result)
2766 return result;
2768 if(!connected) {
2769 infof(data, "Data conn was not available immediately");
2770 ftpc->wait_data_conn = TRUE;
2771 return ftp_check_ctrl_on_data_wait(data, ftpc);
2773 ftpc->wait_data_conn = FALSE;
2775 return ftp_initiate_transfer(data, ftpc);
2778/* for LIST and RETR responses */
2779static CURLcode ftp_state_get_resp(struct Curl_easy *data,
2780 struct ftp_conn *ftpc,
2781 struct FTP *ftp,
2782 int ftpcode,
2783 ftpstate instate)
2785 CURLcode result = CURLE_OK;
2787 if((ftpcode == 150) || (ftpcode == 125)) {
2789 /*
2790 A;
2791 150 Opening BINARY mode data connection for /etc/passwd (2241
2792 bytes). (ok, the file is being transferred)
2794 B:
2795 150 Opening ASCII mode data connection for /bin/ls
2797 C:
2798 150 ASCII data connection for /bin/ls (137.167.104.91,37445) (0 bytes).
2800 D:
2801 150 Opening ASCII mode data connection for [file] (0.0.0.0,0) (545 bytes)
2803 E:
2804 125 Data connection already open; Transfer starting. */
2806 data->req.size = -1; /* default unknown size */
2808 /*
2809 * It appears that there are FTP-servers that return size 0 for files when
2810 * SIZE is used on the file while being in BINARY mode. To work around
2811 * that (stupid) behavior, we attempt to parse the RETR response even if
2812 * the SIZE returned size zero.
2814 * Debugging help from Salvatore Sorrentino on February 26, 2003.
2815 */
2817 if((instate != FTP_LIST) &&
2818 !data->state.prefer_ascii &&
2819 !data->set.ignorecl &&
2820 (ftp->downloadsize < 1)) {
2821 /*
2822 * It seems directory listings either do not show the size or often uses
2823 * size 0 anyway. ASCII transfers may cause that the transferred amount
2824 * of data is not the same as this line tells, why using this number in
2825 * those cases only confuses us.
2827 * Example D above makes this parsing a little tricky */
2828 size_t len = curlx_dyn_len(&ftpc->pp.recvbuf);
2829 if(len >= 7) { /* "1 bytes" is 7 characters */
2830 size_t i;
2831 for(i = 0; i < len - 7; i++) {
2832 curl_off_t what;
2833 const char *buf = curlx_dyn_ptr(&ftpc->pp.recvbuf);
2834 const char *c = &buf[i];
2835 if(!curlx_str_number(&c, &what, CURL_OFF_T_MAX) &&
2836 !curlx_str_single(&c, ' ') &&
2837 !strncmp(c, "bytes", 5)) {
2838 data->req.size = what;
2839 break;
2844 else if(ftp->downloadsize > -1)
2845 data->req.size = ftp->downloadsize;
2847 if(data->req.size > data->req.maxdownload && data->req.maxdownload > 0)
2848 data->req.size = data->req.maxdownload;
2849 else if((instate != FTP_LIST) && data->state.prefer_ascii)
2850 data->req.size = -1; /* for servers that understate ASCII mode file
2851 size */
2853 infof(data, "Maxdownload = %" FMT_OFF_T, data->req.maxdownload);
2855 if(instate != FTP_LIST)
2856 infof(data, "Getting file with size: %" FMT_OFF_T, data->req.size);
2858 if(data->set.ftp_use_port) {
2859 bool connected;
2861 result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected);
2862 if(result)
2863 return result;
2865 if(!connected) {
2866 infof(data, "Data conn was not available immediately");
2867 ftp_state(data, ftpc, FTP_STOP);
2868 ftpc->wait_data_conn = TRUE;
2869 return ftp_check_ctrl_on_data_wait(data, ftpc);
2871 ftpc->wait_data_conn = FALSE;
2873 return ftp_initiate_transfer(data, ftpc);
2875 else {
2876 if((instate == FTP_LIST) && (ftpcode == 450)) {
2877 /* no matching files in the directory listing */
2878 ftp->transfer = PPTRANSFER_NONE; /* do not download anything */
2879 ftp_state(data, ftpc, FTP_STOP); /* this phase is over */
2881 else {
2882 failf(data, "RETR response: %03d", ftpcode);
2883 return instate == FTP_RETR && ftpcode == 550 ?
2884 CURLE_REMOTE_FILE_NOT_FOUND :
2885 CURLE_FTP_COULDNT_RETR_FILE;
2889 return result;
2892/* after USER, PASS and ACCT */
2893static CURLcode ftp_state_loggedin(struct Curl_easy *data,
2894 struct ftp_conn *ftpc)
2896 CURLcode result = CURLE_OK;
2898 if(Curl_conn_is_ssl(data->conn, FIRSTSOCKET)) {
2899 /* PBSZ = PROTECTION BUFFER SIZE.
2901 The 'draft-murray-auth-ftp-ssl' (draft 12, page 7) says:
2903 Specifically, the PROT command MUST be preceded by a PBSZ
2904 command and a PBSZ command MUST be preceded by a successful
2905 security data exchange (the TLS negotiation in this case)
2907 ... (and on page 8):
2909 Thus the PBSZ command must still be issued, but must have a
2910 parameter of '0' to indicate that no buffering is taking place
2911 and the data connection should not be encapsulated.
2912 */
2913 result = Curl_pp_sendf(data, &ftpc->pp, "PBSZ %d", 0);
2914 if(!result)
2915 ftp_state(data, ftpc, FTP_PBSZ);
2917 else {
2918 result = ftp_state_pwd(data, ftpc);
2920 return result;
2923/* A value that becomes part of an FTP control command must not carry a
2924 control byte: a CR or LF would end the command line and let a second
2925 command be smuggled onto the control connection. */
2926static bool ftp_has_ctrl(const char *string)
2928 const unsigned char *s = (const unsigned char *)string;
2929 while(*s) {
2930 if(*s < 0x20)
2931 return TRUE;
2932 s++;
2934 return FALSE;
2937/* for USER and PASS responses */
2938static CURLcode ftp_state_user_resp(struct Curl_easy *data,
2939 struct ftp_conn *ftpc,
2940 int ftpcode)
2942 CURLcode result = CURLE_OK;
2944 /* some need password anyway, and others return 2xx ignored */
2945 if((ftpcode == 331) && (ftpc->state == FTP_USER)) {
2946 /* 331 Password required for ...
2947 (the server requires to send the user's password too) */
2948 result = Curl_pp_sendf(data, &ftpc->pp, "PASS %s",
2949 Curl_creds_passwd(data->conn->creds));
2950 if(!result)
2951 ftp_state(data, ftpc, FTP_PASS);
2953 else if(ftpcode / 100 == 2) {
2954 /* 230 User ... logged in.
2955 (the user logged in with or without password) */
2956 result = ftp_state_loggedin(data, ftpc);
2958 else if(ftpcode == 332) {
2959 const char *account = CURL_EASY_STR(data, STRING_FTP_ACCOUNT);
2960 if(!account) {
2961 failf(data, "ACCT requested but none available");
2962 result = CURLE_LOGIN_DENIED;
2964 else if(ftp_has_ctrl(account)) {
2965 failf(data, "Control byte in FTP account");
2966 result = CURLE_BAD_FUNCTION_ARGUMENT;
2968 else {
2969 result = Curl_pp_sendf(data, &ftpc->pp, "ACCT %s", account);
2970 if(!result)
2971 ftp_state(data, ftpc, FTP_ACCT);
2974 else {
2975 /* All other response codes, like:
2977 530 User ... access denied
2978 (the server denies to log the specified user) */
2980 const char *alt = CURL_EASY_STR(data, STRING_FTP_ALTERNATIVE_TO_USER);
2981 if(alt && !ftpc->ftp_trying_alternative) {
2982 /* Ok, USER failed. Let's try the supplied command. */
2983 if(ftp_has_ctrl(alt)) {
2984 failf(data, "Control byte in FTP alternative-to-user command");
2985 result = CURLE_BAD_FUNCTION_ARGUMENT;
2987 else {
2988 result = Curl_pp_sendf(data, &ftpc->pp, "%s", alt);
2989 if(!result) {
2990 ftpc->ftp_trying_alternative = TRUE;
2991 ftp_state(data, ftpc, FTP_USER);
2995 else {
2996 failf(data, "Access denied: %03d", ftpcode);
2997 result = CURLE_LOGIN_DENIED;
3000 return result;
3003/* for ACCT response */
3004static CURLcode ftp_state_acct_resp(struct Curl_easy *data,
3005 struct ftp_conn *ftpc,
3006 int ftpcode)
3008 CURLcode result = CURLE_OK;
3009 if(ftpcode != 230) {
3010 failf(data, "ACCT rejected by server: %03d", ftpcode);
3011 result = CURLE_FTP_WEIRD_PASS_REPLY; /* FIX */
3013 else
3014 result = ftp_state_loggedin(data, ftpc);
3016 return result;
3019static CURLcode ftp_pwd_resp(struct Curl_easy *data,
3020 struct ftp_conn *ftpc,
3021 int ftpcode)
3023 struct pingpong *pp = &ftpc->pp;
3024 CURLcode result;
3026 if(ftpcode == 257) {
3027 const char *ptr = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first
3028 letter */
3029 bool entry_extracted = FALSE;
3030 struct dynbuf out;
3031 curlx_dyn_init(&out, 1000);
3033 /* Reply format is like
3034 257<space>[rubbish]"<directory-name>"<space><commentary> and the
3035 RFC959 says
3037 The directory name can contain any character; embedded
3038 double-quotes should be escaped by double-quotes (the
3039 "quote-doubling" convention).
3040 */
3042 /* scan for the first double-quote for non-standard responses */
3043 while(*ptr != '\n' && *ptr != '\0' && *ptr != '"')
3044 ptr++;
3046 if('\"' == *ptr) {
3047 /* it started good */
3048 for(ptr++; *ptr; ptr++) {
3049 if('\"' == *ptr) {
3050 if('\"' == ptr[1]) {
3051 /* "quote-doubling" */
3052 result = curlx_dyn_addn(&out, &ptr[1], 1);
3053 ptr++;
3055 else {
3056 /* end of path */
3057 if(curlx_dyn_len(&out))
3058 entry_extracted = TRUE;
3059 break; /* get out of this loop */
3062 else {
3063 if(ISCNTRL(*ptr)) {
3064 /* control characters have no business in a path */
3065 curlx_dyn_free(&out);
3066 return CURLE_WEIRD_SERVER_REPLY;
3068 result = curlx_dyn_addn(&out, ptr, 1);
3070 if(result) {
3071 curlx_dyn_free(&out);
3072 return result;
3076 if(entry_extracted) {
3077 /* If the path name does not look like an absolute path (i.e.: it
3078 does not start with a '/'), we probably need some server-dependent
3079 adjustments. For example, this is the case when connecting to
3080 an OS400 FTP server: this server supports two name syntaxes,
3081 the default one being incompatible with standard paths. In
3082 addition, this server switches automatically to the regular path
3083 syntax when one is encountered in a command: this results in
3084 having an entrypath in the wrong syntax when later used in CWD.
3085 The method used here is to check the server OS: we do it only
3086 if the path name looks strange to minimize overhead on other
3087 systems. */
3088 char *dir = curlx_dyn_ptr(&out);
3090 if(!ftpc->server_os && dir[0] != '/') {
3091 result = Curl_pp_sendf(data, &ftpc->pp, "%s", "SYST");
3092 if(result) {
3093 curlx_dyn_free(&out);
3094 return result;
3098 curlx_free(ftpc->entrypath);
3099 ftpc->entrypath = dir; /* remember this */
3100 infof(data, "Entry path is '%s'", ftpc->entrypath);
3101 /* also save it where getinfo can access it: */
3102 curlx_free(data->state.most_recent_ftp_entrypath);
3103 data->state.most_recent_ftp_entrypath = curlx_strdup(ftpc->entrypath);
3104 if(!data->state.most_recent_ftp_entrypath)
3105 return CURLE_OUT_OF_MEMORY;
3107 if(!ftpc->server_os && dir[0] != '/') {
3108 ftp_state(data, ftpc, FTP_SYST);
3109 return CURLE_OK;
3112 else {
3113 /* could not get the path */
3114 curlx_dyn_free(&out);
3115 infof(data, "Failed to figure out path");
3118 ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
3119 CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
3120 return CURLE_OK;
3123static const char * const ftpauth[] = { "SSL", "TLS" };
3125static CURLcode ftp_wait_resp(struct Curl_easy *data,
3126 struct connectdata *conn,
3127 struct ftp_conn *ftpc,
3128 int ftpcode)
3130 CURLcode result = CURLE_OK;
3131 if(ftpcode == 230) {
3132 /* 230 User logged in - already! Take as 220 if TLS required. */
3133 if(ftpc->use_ssl <= CURLUSESSL_TRY ||
3134 Curl_conn_is_ssl(conn, FIRSTSOCKET))
3135 return ftp_state_user_resp(data, ftpc, ftpcode);
3137 else if(ftpcode != 220) {
3138 failf(data, "Got a %03d ftp-server response when 220 was expected",
3139 ftpcode);
3140 return CURLE_WEIRD_SERVER_REPLY;
3143 if(ftpc->use_ssl && !Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
3144 /* We do not have an SSL/TLS control connection yet, but FTPS is
3145 requested. Try an FTPS connection now */
3147 ftpc->count3 = 0;
3148 switch((long)data->set.ftpsslauth) {
3149 case CURLFTPAUTH_DEFAULT:
3150 case CURLFTPAUTH_SSL:
3151 ftpc->count2 = 1; /* add one to get next */
3152 ftpc->count1 = 0;
3153 break;
3154 case CURLFTPAUTH_TLS:
3155 ftpc->count2 = -1; /* subtract one to get next */
3156 ftpc->count1 = 1;
3157 break;
3158 default:
3159 failf(data, "unsupported parameter to CURLOPT_FTPSSLAUTH: %d",
3160 (int)data->set.ftpsslauth);
3161 return CURLE_UNKNOWN_OPTION; /* we do not know what to do */
3163 result = Curl_pp_sendf(data, &ftpc->pp, "AUTH %s", ftpauth[ftpc->count1]);
3164 if(!result)
3165 ftp_state(data, ftpc, FTP_AUTH);
3167 else
3168 result = ftp_state_user(data, ftpc, conn);
3169 return result;
3172/* an implicit TLS scheme implies TLS on both connections */
3173static unsigned char ftp_use_ssl(struct connectdata *conn,
3174 struct ftp_conn *ftpc)
3176 if(!ftpc->use_ssl && (conn->scheme->flags & PROTOPT_SSL))
3177 return CURLUSESSL_ALL;
3178 return ftpc->use_ssl;
3181static CURLcode ftp_pp_statemachine(struct Curl_easy *data,
3182 struct connectdata *conn)
3184 CURLcode result;
3185 int ftpcode;
3186 struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
3187 struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
3188 struct pingpong *pp;
3189 size_t nread = 0;
3191 if(!ftpc || !ftp)
3192 return CURLE_FAILED_INIT;
3193 pp = &ftpc->pp;
3194 if(pp->sendleft)
3195 return Curl_pp_flushsend(data, pp);
3197 result = ftp_readresp(data, ftpc, FIRSTSOCKET, pp, &ftpcode, &nread);
3198 if(result || !ftpcode)
3199 return result;
3201 /* we have now received a full FTP server response */
3202 switch(ftpc->state) {
3203 case FTP_WAIT220:
3204 result = ftp_wait_resp(data, conn, ftpc, ftpcode);
3205 break;
3207 case FTP_AUTH:
3208 /* we have gotten the response to a previous AUTH command */
3210 if(pp->overflow)
3211 return CURLE_WEIRD_SERVER_REPLY; /* Forbid pipelining in response. */
3213 /* RFC2228 (page 5) says:
3215 * If the server is willing to accept the named security mechanism,
3216 * and does not require any security data, it must respond with
3217 * reply code 234/334.
3218 */
3220 if((ftpcode == 234) || (ftpcode == 334)) {
3221 /* this was BLOCKING, keep it so for now */
3222 bool done;
3223 if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
3224 result = Curl_ssl_cfilter_add(
3225 data, Curl_conn_get_origin(conn, FIRSTSOCKET), conn, FIRSTSOCKET);
3226 if(result) {
3227 /* we failed and bail out */
3228 return CURLE_USE_SSL_FAILED;
3231 /* BLOCKING */
3232 result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, &done);
3233 if(!result) {
3234 conn->bits.ftp_use_data_ssl = FALSE; /* clear-text data */
3235 result = ftp_state_user(data, ftpc, conn);
3238 else if(ftpc->count3 < 1) {
3239 ftpc->count3++;
3240 ftpc->count1 += ftpc->count2; /* get next attempt */
3241 result = Curl_pp_sendf(data, &ftpc->pp, "AUTH %s",
3242 ftpauth[ftpc->count1]);
3243 /* remain in this same state */
3245 else {
3246 if(ftpc->use_ssl > CURLUSESSL_TRY)
3247 /* we failed and CURLUSESSL_CONTROL or CURLUSESSL_ALL is set */
3248 result = CURLE_USE_SSL_FAILED;
3249 else
3250 /* ignore the failure and continue */
3251 result = ftp_state_user(data, ftpc, conn);
3253 break;
3255 case FTP_USER:
3256 case FTP_PASS:
3257 result = ftp_state_user_resp(data, ftpc, ftpcode);
3258 break;
3260 case FTP_ACCT:
3261 result = ftp_state_acct_resp(data, ftpc, ftpcode);
3262 break;
3264 case FTP_PBSZ:
3265 result =
3266 Curl_pp_sendf(data, &ftpc->pp, "PROT %c",
3267 ftp_use_ssl(conn, ftpc) == CURLUSESSL_CONTROL ? 'C' : 'P');
3268 if(!result)
3269 ftp_state(data, ftpc, FTP_PROT);
3270 break;
3272 case FTP_PROT:
3273 if(ftpcode / 100 == 2)
3274 /* We have enabled SSL for the data connection! */
3275 conn->bits.ftp_use_data_ssl =
3276 (ftp_use_ssl(conn, ftpc) != CURLUSESSL_CONTROL);
3277 /* FTP servers typically responds with 500 if they decide to reject
3278 our 'P' request */
3279 else if(ftp_use_ssl(conn, ftpc) > CURLUSESSL_CONTROL)
3280 /* we failed and bails out */
3281 return CURLE_USE_SSL_FAILED;
3283 if(data->set.ftp_ccc) {
3284 /* CCC - Clear Command Channel
3285 */
3286 result = Curl_pp_sendf(data, &ftpc->pp, "%s", "CCC");
3287 if(!result)
3288 ftp_state(data, ftpc, FTP_CCC);
3290 else
3291 result = ftp_state_pwd(data, ftpc);
3292 break;
3294 case FTP_CCC:
3295 if(ftpcode < 500) {
3296 /* First shut down the SSL layer (note: this call will block) */
3297 /* This has only been tested on the proftpd server, and the mod_tls
3298 * code sends a close notify alert without waiting for a close notify
3299 * alert in response. Thus we wait for a close notify alert from the
3300 * server, but we do not send one. Let's hope other servers do
3301 * the same... */
3302 result = Curl_ssl_cfilter_remove(data, FIRSTSOCKET,
3303 (data->set.ftp_ccc ==
3304 (unsigned char)CURLFTPSSL_CCC_ACTIVE));
3305 if(result)
3306 failf(data, "Failed to clear the command channel (CCC)");
3308 if(!result)
3309 /* Then continue as normal */
3310 result = ftp_state_pwd(data, ftpc);
3311 break;
3313 case FTP_PWD:
3314 result = ftp_pwd_resp(data, ftpc, ftpcode);
3315 break;
3317 case FTP_SYST:
3318 if(ftpcode == 215) {
3319 const char *ptr = curlx_dyn_ptr(&pp->recvbuf) + 4; /* start on the first
3320 letter */
3321 const char *start;
3322 char *os;
3324 /* Reply format is like
3325 215<space><OS-name><space><commentary>
3326 */
3327 while(*ptr == ' ')
3328 ptr++;
3329 for(start = ptr; *ptr && *ptr != ' '; ptr++)
3331 os = curlx_memdup0(start, ptr - start);
3332 if(!os)
3333 return CURLE_OUT_OF_MEMORY;
3335 /* Check for special servers here. */
3336 if(curl_strequal(os, "OS/400")) {
3337 /* Force OS400 name format 1. */
3338 result = Curl_pp_sendf(data, &ftpc->pp, "%s", "SITE NAMEFMT 1");
3339 if(result) {
3340 curlx_free(os);
3341 return result;
3343 /* remember target server OS */
3344 curlx_free(ftpc->server_os);
3345 ftpc->server_os = os;
3346 ftp_state(data, ftpc, FTP_NAMEFMT);
3347 break;
3349 /* Nothing special for the target server. */
3350 /* remember target server OS */
3351 curlx_free(ftpc->server_os);
3352 ftpc->server_os = os;
3354 else {
3355 /* Cannot identify server OS. Continue anyway and cross fingers. */
3358 ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
3359 CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
3360 break;
3362 case FTP_NAMEFMT:
3363 if(ftpcode == 250) {
3364 /* Name format change successful: reload initial path. */
3365 ftp_state_pwd(data, ftpc);
3366 break;
3369 ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
3370 CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
3371 break;
3373 case FTP_QUOTE:
3374 case FTP_POSTQUOTE:
3375 case FTP_RETR_PREQUOTE:
3376 case FTP_STOR_PREQUOTE:
3377 case FTP_LIST_PREQUOTE:
3378 if((ftpcode >= 400) && !ftpc->count2) {
3379 /* failure response code, and not allowed to fail */
3380 failf(data, "QUOT command failed with %03d", ftpcode);
3381 result = CURLE_QUOTE_ERROR;
3383 else
3384 result = ftp_state_quote(data, ftpc, ftp, FALSE, ftpc->state);
3385 break;
3387 case FTP_CWD:
3388 if(ftpcode / 100 != 2) {
3389 /* failure to CWD there */
3390 if(data->set.ftp_create_missing_dirs &&
3391 ftpc->cwdcount && !ftpc->count2) {
3392 /* try making it */
3393 ftpc->count2++; /* counter to prevent CWD-MKD loops */
3395 /* count3 is set to allow MKD to fail once per dir. In the case when
3396 CWD fails and then MKD fails (due to another session raced it to
3397 create the dir) this then allows for a second try to CWD to it. */
3398 ftpc->count3 = (data->set.ftp_create_missing_dirs == 2) ? 1 : 0;
3400 result = Curl_pp_sendf(data, &ftpc->pp, "MKD %.*s",
3401 pathlen(ftpc, ftpc->cwdcount - 1),
3402 pathpiece(ftpc, ftpc->cwdcount - 1));
3403 if(!result)
3404 ftp_state(data, ftpc, FTP_MKD);
3406 else {
3407 /* return failure */
3408 failf(data, "Server denied you to change to the given directory");
3409 ftpc->cwdfail = TRUE; /* do not remember this path as we failed
3410 to enter it */
3411 result = CURLE_REMOTE_ACCESS_DENIED;
3414 else {
3415 /* success */
3416 ftpc->count2 = 0;
3417 if(ftpc->cwdcount >= ftpc->dirdepth)
3418 result = ftp_state_mdtm(data, ftpc, ftp);
3419 else {
3420 ftpc->cwdcount++;
3421 /* send next CWD */
3422 result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s",
3423 pathlen(ftpc, ftpc->cwdcount - 1),
3424 pathpiece(ftpc, ftpc->cwdcount - 1));
3427 break;
3429 case FTP_MKD:
3430 if((ftpcode / 100 != 2) && !ftpc->count3--) {
3431 /* failure to MKD the directory */
3432 failf(data, "Failed to MKD dir: %03d", ftpcode);
3433 result = CURLE_REMOTE_ACCESS_DENIED;
3435 else {
3436 ftp_state(data, ftpc, FTP_CWD);
3437 /* send CWD */
3438 result = Curl_pp_sendf(data, &ftpc->pp, "CWD %.*s",
3439 pathlen(ftpc, ftpc->cwdcount - 1),
3440 pathpiece(ftpc, ftpc->cwdcount - 1));
3442 break;
3444 case FTP_MDTM:
3445 result = ftp_state_mdtm_resp(data, ftpc, ftp, ftpcode);
3446 break;
3448 case FTP_TYPE:
3449 case FTP_LIST_TYPE:
3450 case FTP_RETR_TYPE:
3451 case FTP_STOR_TYPE:
3452 case FTP_RETR_LIST_TYPE:
3453 result = ftp_state_type_resp(data, ftpc, ftp, ftpcode, ftpc->state);
3454 break;
3456 case FTP_SIZE:
3457 case FTP_RETR_SIZE:
3458 case FTP_STOR_SIZE:
3459 result = ftp_state_size_resp(data, ftpc, ftp, ftpcode, ftpc->state);
3460 break;
3462 case FTP_REST:
3463 case FTP_RETR_REST:
3464 result = ftp_state_rest_resp(data, ftpc, ftp, ftpcode, ftpc->state);
3465 break;
3467 case FTP_PRET:
3468 if(ftpcode != 200) {
3469 /* there only is this one standard OK return code. */
3470 failf(data, "PRET command not accepted: %03d", ftpcode);
3471 return CURLE_FTP_PRET_FAILED;
3473 result = ftp_state_use_pasv(data, ftpc, conn);
3474 break;
3476 case FTP_PASV:
3477 result = ftp_state_pasv_resp(data, ftpc, ftpcode);
3478 break;
3480 case FTP_PORT:
3481 result = ftp_state_port_resp(data, ftpc, ftp, ftpcode);
3482 break;
3484 case FTP_LIST:
3485 case FTP_RETR:
3486 result = ftp_state_get_resp(data, ftpc, ftp, ftpcode, ftpc->state);
3487 break;
3489 case FTP_STOR:
3490 result = ftp_state_stor_resp(data, ftpc, ftpcode);
3491 break;
3493 case FTP_QUIT:
3494 default:
3495 /* internal error */
3496 ftp_state(data, ftpc, FTP_STOP);
3497 break;
3500 return result;
3503/* called repeatedly until done from multi.c */
3504static CURLcode ftp_multi_statemach(struct Curl_easy *data,
3505 bool *done)
3507 struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
3508 return ftpc ? ftp_statemach(data, ftpc, done) : CURLE_FAILED_INIT;
3511static CURLcode ftp_block_statemach(struct Curl_easy *data,
3512 struct ftp_conn *ftpc)
3514 struct pingpong *pp = &ftpc->pp;
3515 CURLcode result = CURLE_OK;
3517 while(ftpc->state != FTP_STOP) {
3518 if(ftpc->shutdown)
3519 CURL_TRC_FTP(data, "in shutdown, waiting for server response");
3520 result = Curl_pp_statemach(data, pp, TRUE, TRUE /* disconnecting */);
3521 if(result)
3522 break;
3525 return result;
3529 * ftp_connect() should do everything that is to be considered a part of
3530 * the connection phase.
3532 * The variable 'done' points to will be TRUE if the protocol-layer connect
3533 * phase is done when this function returns, or FALSE if not.
3535 */
3536static CURLcode ftp_connect(struct Curl_easy *data,
3537 bool *done) /* see description above */
3539 CURLcode result;
3540 struct connectdata *conn = data->conn;
3541 struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
3542 struct pingpong *pp;
3544 *done = FALSE; /* default to not done yet */
3545 if(!ftpc)
3546 return CURLE_FAILED_INIT;
3547 pp = &ftpc->pp;
3548 PINGPONG_SETUP(pp, ftp_pp_statemachine, ftp_endofresp);
3550 if(Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
3551 /* BLOCKING */
3552 result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done);
3553 if(result)
3554 return result;
3557 Curl_pp_init(pp, Curl_pgrs_now(data)); /* once per transfer */
3559 /* When we connect, we start in the state where we await the 220
3560 response */
3561 ftp_state(data, ftpc, FTP_WAIT220);
3563 result = ftp_statemach(data, ftpc, done);
3565 return result;
3568/***********************************************************************
3570 * ftp_sendquote()
3572 * Where a 'quote' means a list of custom commands to send to the server.
3573 * The quote list is passed as an argument.
3575 * BLOCKING
3576 */
3577static CURLcode ftp_sendquote(struct Curl_easy *data,
3578 struct ftp_conn *ftpc,
3579 struct curl_slist *quote)
3581 struct curl_slist *item;
3582 struct pingpong *pp = &ftpc->pp;
3584 item = quote;
3585 while(item) {
3586 if(item->data) {
3587 size_t nread;
3588 const char *cmd = item->data;
3589 bool acceptfail = FALSE;
3590 CURLcode result;
3591 int ftpcode = 0;
3593 /* if a command starts with an asterisk, which a legal FTP command never
3594 can, the command will be allowed to fail without it causing any
3595 aborts or cancels etc. It will cause libcurl to act as if the command
3596 is successful, whatever the server responds. */
3598 if(cmd[0] == '*') {
3599 cmd++;
3600 acceptfail = TRUE;
3603 result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd);
3604 if(!result) {
3605 pp->response = *Curl_pgrs_now(data); /* timeout relative now */
3606 result = getftpresponse(data, &nread, &ftpcode);
3608 if(result)
3609 return result;
3611 if(!acceptfail && (ftpcode >= 400)) {
3612 failf(data, "QUOT string not accepted: %s", cmd);
3613 return CURLE_QUOTE_ERROR;
3617 item = item->next;
3620 return CURLE_OK;
3623static CURLcode ftp_done_status(struct Curl_easy *data,
3624 struct ftp_conn *ftpc, CURLcode status,
3625 bool premature)
3627 switch(status) {
3628 case CURLE_BAD_DOWNLOAD_RESUME:
3629 case CURLE_FTP_WEIRD_PASV_REPLY:
3630 case CURLE_FTP_PORT_FAILED:
3631 case CURLE_FTP_ACCEPT_FAILED:
3632 case CURLE_FTP_ACCEPT_TIMEOUT:
3633 case CURLE_FTP_COULDNT_SET_TYPE:
3634 case CURLE_FTP_COULDNT_RETR_FILE:
3635 case CURLE_PARTIAL_FILE:
3636 case CURLE_UPLOAD_FAILED:
3637 case CURLE_REMOTE_ACCESS_DENIED:
3638 case CURLE_FILESIZE_EXCEEDED:
3639 case CURLE_REMOTE_FILE_NOT_FOUND:
3640 case CURLE_WRITE_ERROR:
3641 /* the connection stays alive fine even though this happened */
3642 case CURLE_OK: /* does not affect the control connection's status */
3643 if(!premature)
3644 break;
3646 /* until we cope better with prematurely ended requests, let them
3647 * fallback as if in complete failure */
3648 FALLTHROUGH();
3649 default: /* by default, an error means the control connection is
3650 wedged and should not be used anymore */
3651 ftpc->ctl_valid = FALSE;
3652 ftpc->cwdfail = TRUE; /* set this TRUE to prevent us to remember the
3653 current path, as this connection is going */
3654 CURL_TRC_FTP(data, "FTP ended with bad error code");
3655 connclose(data->conn);
3656 return status; /* use the already set error code */
3658 return CURLE_OK;
3661static void ftp_done_wildcard(struct Curl_easy *data, struct ftp_conn *ftpc)
3663 if(data->state.wildcardmatch) {
3664 if(data->set.chunk_end && ftpc->file) {
3665 struct Curl_mapi_guard guard;
3666 CURL_CBAPI_START(&guard, data, easy_chunk_end);
3667 data->set.chunk_end(data->set.wildcardptr);
3668 CURL_CBAPI_END(&guard);
3669 freedirs(ftpc);
3671 ftpc->known_filesize = -1;
3675static void ftp_done_path(struct Curl_easy *data, struct ftp_conn *ftpc,
3676 CURLcode result)
3678 struct connectdata *conn = data->conn;
3679 if(result) {
3680 /* We can limp along anyway (and should try to since we may already be in
3681 * the error path) */
3682 ftpc->ctl_valid = FALSE; /* mark control connection as bad */
3683 connclose(conn); /* mark for connection closure */
3684 curlx_safefree(ftpc->prevpath); /* no path remembering */
3686 else { /* remember working directory for connection reuse */
3687 const char *rawPath = ftpc->rawpath;
3688 if(rawPath) {
3689 if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/'))
3690 ; /* full path => no CWDs happened => keep ftpc->prevpath */
3691 else {
3692 size_t pathLen = strlen(ftpc->rawpath);
3694 curlx_free(ftpc->prevpath);
3696 if(!ftpc->cwdfail) {
3697 if(data->set.ftp_filemethod == FTPFILE_NOCWD)
3698 pathLen = 0; /* relative path => working directory is FTP home */
3699 else
3700 /* file is URL-decoded */
3701 pathLen -= ftpc->file ? strlen(ftpc->file) : 0;
3702 ftpc->prevpath = curlx_memdup0(rawPath, pathLen);
3704 else
3705 ftpc->prevpath = NULL; /* no path */
3708 if(ftpc->prevpath)
3709 infof(data, "Remembering we are in directory \"%s\"", ftpc->prevpath);
3713static CURLcode ftp_done_secondary_socket(struct Curl_easy *data,
3714 struct ftp_conn *ftpc,
3715 CURLcode result)
3717 struct connectdata *conn = data->conn;
3718 if(Curl_conn_is_setup(conn, SECONDARYSOCKET)) {
3719 if(!result && ftpc->dont_check && data->req.maxdownload > 0) {
3720 /* partial download completed */
3721 result = Curl_pp_sendf(data, &ftpc->pp, "%s", "ABOR");
3722 if(result) {
3723 failf(data, "Failure sending ABOR command: %s",
3724 curl_easy_strerror(result));
3725 ftpc->ctl_valid = FALSE; /* mark control connection as bad */
3726 connclose(conn); /* connection closure */
3730 close_secondarysocket(data, ftpc);
3732 return result;
3735static CURLcode ftp_done_control_reply(struct Curl_easy *data,
3736 struct ftp_conn *ftpc,
3737 struct FTP *ftp, CURLcode result,
3738 bool premature)
3740 struct connectdata *conn = data->conn;
3741 size_t nread;
3742 int ftpcode;
3744 if(!result && (ftp->transfer == PPTRANSFER_BODY) && ftpc->ctl_valid &&
3745 ftpc->pp.pending_resp && !premature) {
3746 /*
3747 * Let's see what the server says about the transfer we performed, but
3748 * lower the timeout as sometimes this connection has died while the data
3749 * has been transferred. This happens when doing through NATs etc that
3750 * abandon old silent connections.
3751 */
3752 ftpc->pp.response = *Curl_pgrs_now(data); /* timeout relative now */
3753 result = getftpresponse(data, &nread, &ftpcode);
3755 if(!nread && (result == CURLE_OPERATION_TIMEDOUT)) {
3756 failf(data, "control connection looks dead");
3757 ftpc->ctl_valid = FALSE; /* mark control connection as bad */
3758 connclose(conn); /* close */
3761 if(result)
3762 return result;
3764 if(ftpc->dont_check && data->req.maxdownload > 0) {
3765 /* we have sent ABOR and there is no reliable way to check if it was
3766 * successful or not; we have to close the connection now */
3767 infof(data, "partial download completed, closing connection");
3768 connclose(conn);
3769 return result;
3772 if(!ftpc->dont_check) {
3773 /* 226 Transfer complete, 250 Requested file action okay, completed. */
3774 switch(ftpcode) {
3775 case 226:
3776 case 250:
3777 break;
3778 case 552:
3779 failf(data, "Exceeded storage allocation");
3780 result = CURLE_REMOTE_DISK_FULL;
3781 break;
3782 default:
3783 failf(data, "server did not report OK, got %d", ftpcode);
3784 result = CURLE_PARTIAL_FILE;
3785 break;
3789 return result;
3792static CURLcode ftp_done_check_partial(struct Curl_easy *data,
3793 struct ftp_conn *ftpc,
3794 struct FTP *ftp, CURLcode result,
3795 bool premature)
3797 if(result || premature)
3798 /* the response code from the transfer showed an error already so no
3799 use checking further */
3801 else if(data->state.upload) {
3802 if((ftp->transfer == PPTRANSFER_BODY) &&
3803 (data->state.infilesize != -1) && /* upload with known size */
3804 ((!data->set.crlf && !data->state.prefer_ascii && /* no conversion */
3805 (data->state.infilesize != data->req.writebytecount)) ||
3806 ((data->set.crlf || data->state.prefer_ascii) && /* maybe CRLF conv */
3807 (data->state.infilesize > data->req.writebytecount))
3808 )) {
3809 failf(data, "Uploaded unaligned file size (%" FMT_OFF_T
3810 " out of %" FMT_OFF_T " bytes)",
3811 data->req.writebytecount, data->state.infilesize);
3812 result = CURLE_PARTIAL_FILE;
3815 else {
3816 if((data->req.size != -1) &&
3817 (data->req.size != data->req.bytecount) &&
3818 (data->req.maxdownload != data->req.bytecount)) {
3819 failf(data, "Received only partial file: %" FMT_OFF_T " bytes",
3820 data->req.bytecount);
3821 result = CURLE_PARTIAL_FILE;
3823 else if(!ftpc->dont_check &&
3824 !data->req.bytecount &&
3825 (data->req.size > 0)) {
3826 failf(data, "No data was received");
3827 result = CURLE_FTP_COULDNT_RETR_FILE;
3830 return result;
3833/***********************************************************************
3835 * ftp_done()
3837 * The DONE function. This does what needs to be done after a single DO has
3838 * performed.
3840 * Input argument is already checked for validity.
3841 */
3842static CURLcode ftp_done(struct Curl_easy *data, CURLcode status,
3843 bool premature)
3845 struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
3846 struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
3847 CURLcode result;
3849 if(!ftp || !ftpc)
3850 return CURLE_OK;
3852 result = ftp_done_status(data, ftpc, status, premature);
3854 ftp_done_wildcard(data, ftpc);
3855 ftp_done_path(data, ftpc, result);
3856 result = ftp_done_secondary_socket(data, ftpc, result);
3857 result = ftp_done_control_reply(data, ftpc, ftp, result, premature);
3858 result = ftp_done_check_partial(data, ftpc, ftp, result, premature);
3860 /* clear these for next connection */
3861 ftp->transfer = PPTRANSFER_BODY;
3862 ftpc->dont_check = FALSE;
3864 /* Send any post-transfer QUOTE strings? */
3865 if(!status && !result && !premature && data->set.postquote)
3866 result = ftp_sendquote(data, ftpc, data->set.postquote);
3867 CURL_TRC_FTP(data, "[%s] done, result=%d", FTP_CSTATE(ftpc), (int)result);
3868 return result;
3871/***********************************************************************
3873 * ftp_nb_type()
3875 * Set TYPE. We only deal with ASCII or BINARY so this function
3876 * sets one of them.
3877 * If the transfer type is not sent, simulate on OK response in newstate
3878 */
3879static CURLcode ftp_nb_type(struct Curl_easy *data,
3880 struct ftp_conn *ftpc,
3881 struct FTP *ftp,
3882 bool ascii, ftpstate newstate)
3884 CURLcode result;
3885 char want = (char)(ascii ? 'A' : 'I');
3887 if(ftpc->transfertype == want) {
3888 ftp_state(data, ftpc, newstate);
3889 return ftp_state_type_resp(data, ftpc, ftp, 200, newstate);
3892 result = Curl_pp_sendf(data, &ftpc->pp, "TYPE %c", want);
3893 if(!result) {
3894 ftp_state(data, ftpc, newstate);
3896 /* keep track of our current transfer type */
3897 ftpc->transfertype = want;
3899 return result;
3902/***********************************************************************
3904 * ftp_perform()
3906 * This is the actual DO function for FTP. Get a file/directory according to
3907 * the options previously setup.
3908 */
3909static CURLcode ftp_perform(
3910 struct Curl_easy *data,
3911 struct ftp_conn *ftpc,
3912 struct FTP *ftp,
3913 bool *connected, /* connect status after PASV / PORT */
3914 bool *dophase_done)
3916 /* this is FTP and no proxy */
3917 CURLcode result = CURLE_OK;
3919 CURL_TRC_FTP(data, "[%s] DO phase starts", FTP_CSTATE(ftpc));
3921 if(data->req.no_body) {
3922 /* requested no body means no transfer... */
3923 ftp->transfer = PPTRANSFER_INFO;
3926 *dophase_done = FALSE; /* not done yet */
3928 /* start the first command in the DO phase */
3929 result = ftp_state_quote(data, ftpc, ftp, TRUE, FTP_QUOTE);
3930 if(result)
3931 return result;
3933 /* run the state-machine */
3934 result = ftp_statemach(data, ftpc, dophase_done);
3936 *connected = Curl_conn_is_connected(data->conn, SECONDARYSOCKET);
3938 if(*connected)
3939 infof(data, "[FTP] [%s] perform, DATA connection established",
3940 FTP_CSTATE(ftpc));
3941 else
3942 CURL_TRC_FTP(data, "[%s] perform, awaiting DATA connect",
3943 FTP_CSTATE(ftpc));
3945 if(*dophase_done)
3946 CURL_TRC_FTP(data, "[%s] DO phase is complete1", FTP_CSTATE(ftpc));
3948 return result;
3951static void wc_data_dtor(void *ptr)
3953 struct ftp_wc *ftpwc = ptr;
3954 if(ftpwc && ftpwc->parser)
3955 Curl_ftp_parselist_data_free(&ftpwc->parser);
3956 curlx_free(ftpwc);
3959static CURLcode init_wc_data(struct Curl_easy *data,
3960 struct ftp_conn *ftpc,
3961 struct FTP *ftp)
3963 char *last_slash;
3964 char *path = ftp->path;
3965 struct WildcardData *wildcard = data->wildcard;
3966 CURLcode result = CURLE_OK;
3967 struct ftp_wc *ftpwc = NULL;
3969 last_slash = strrchr(ftp->path, '/');
3970 if(last_slash) {
3971 last_slash++;
3972 if(last_slash[0] == '\0') {
3973 wildcard->state = CURLWC_CLEAN;
3974 return ftp_parse_url_path(data, ftpc, ftp);
3976 wildcard->pattern = curlx_strdup(last_slash);
3977 if(!wildcard->pattern)
3978 return CURLE_OUT_OF_MEMORY;
3979 last_slash[0] = '\0'; /* cut file from path */
3981 else { /* there is only 'wildcard pattern' or nothing */
3982 if(path[0]) {
3983 wildcard->pattern = curlx_strdup(path);
3984 if(!wildcard->pattern)
3985 return CURLE_OUT_OF_MEMORY;
3986 path[0] = '\0';
3988 else { /* only list */
3989 wildcard->state = CURLWC_CLEAN;
3990 return ftp_parse_url_path(data, ftpc, ftp);
3994 /* program continues only if URL is not ending with slash, allocate needed
3995 resources for wildcard transfer */
3997 /* allocate ftp protocol specific wildcard data */
3998 ftpwc = curlx_calloc(1, sizeof(struct ftp_wc));
3999 if(!ftpwc) {
4000 result = CURLE_OUT_OF_MEMORY;
Diagnostics4 git calls · 30 ms
CallStatusSizeTime030 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20041 KiB · hit25 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=24
git.response.bytes=41933
git.response.cache=HIT
git.response.duration_ms=25
git.fetch.wants=9e74bf277669303ad125b71795359dbaeac55923
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=41933
git.pkt.payload.max=41912
git.sideband.band1.bytes=41911
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e15f4dace6725b
Request
b20e6f19…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format