2002-09-03 07:52:59 -04:00
|
|
|
/***************************************************************************
|
2004-06-03 07:41:05 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
1999-12-29 09:20:26 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-02-04 17:27:39 -05:00
|
|
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2004-06-03 07:41:05 -04:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 07:52:59 -04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
***************************************************************************/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2000-08-24 10:26:06 -04:00
|
|
|
|
2002-06-11 07:13:01 -04:00
|
|
|
#ifndef CURL_DISABLE_FTP
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2000-08-23 03:23:42 -04:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
1999-12-29 09:20:26 -05:00
|
|
|
#include <netinet/in.h>
|
2000-08-23 03:23:42 -04:00
|
|
|
#endif
|
1999-12-29 09:20:26 -05:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2004-10-21 04:22:07 -04:00
|
|
|
#ifdef HAVE_UTSNAME_H
|
1999-12-29 09:20:26 -05:00
|
|
|
#include <sys/utsname.h>
|
2004-10-21 04:22:07 -04:00
|
|
|
#endif
|
2000-08-23 03:23:42 -04:00
|
|
|
#ifdef HAVE_NETDB_H
|
1999-12-29 09:20:26 -05:00
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2009-12-30 12:59:56 -05:00
|
|
|
#ifdef __VMS
|
2002-02-20 08:46:53 -05:00
|
|
|
#include <in.h>
|
2001-08-06 08:47:39 -04:00
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2004-03-17 07:46:42 -05:00
|
|
|
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
|
|
|
|
#undef in_addr_t
|
|
|
|
#define in_addr_t unsigned long
|
|
|
|
#endif
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
#include <curl/curl.h>
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "if2ip.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "transfer.h"
|
|
|
|
#include "escape.h"
|
|
|
|
#include "http.h" /* for HTTP proxy tunnel stuff */
|
|
|
|
#include "ftp.h"
|
|
|
|
#include "fileinfo.h"
|
|
|
|
#include "ftplistparser.h"
|
2018-01-29 04:50:43 -05:00
|
|
|
#include "curl_range.h"
|
2013-08-26 05:51:18 -04:00
|
|
|
#include "curl_sec.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "strtoofft.h"
|
2016-09-30 11:15:05 -04:00
|
|
|
#include "strcase.h"
|
2013-12-17 17:32:47 -05:00
|
|
|
#include "vtls/vtls.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "connect.h"
|
|
|
|
#include "strerror.h"
|
|
|
|
#include "inet_ntop.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "parsedate.h" /* for the week day and month names */
|
|
|
|
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "url.h"
|
2016-09-30 12:54:02 -04:00
|
|
|
#include "strcase.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "speedcheck.h"
|
|
|
|
#include "warnless.h"
|
|
|
|
#include "http_proxy.h"
|
|
|
|
#include "non-ascii.h"
|
2020-02-14 10:16:54 -05:00
|
|
|
#include "socks.h"
|
2016-04-29 09:46:40 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2000-09-21 04:47:20 -04:00
|
|
|
|
2009-07-04 07:06:00 -04:00
|
|
|
#ifndef NI_MAXHOST
|
|
|
|
#define NI_MAXHOST 1025
|
2004-03-31 05:59:48 -05:00
|
|
|
#endif
|
2009-07-04 07:27:42 -04:00
|
|
|
#ifndef INET_ADDRSTRLEN
|
|
|
|
#define INET_ADDRSTRLEN 16
|
|
|
|
#endif
|
2004-03-31 05:59:48 -05:00
|
|
|
|
2007-03-24 22:30:58 -04:00
|
|
|
#ifdef CURL_DISABLE_VERBOSE_STRINGS
|
2011-09-03 10:06:10 -04:00
|
|
|
#define ftp_pasv_verbose(a,b,c,d) Curl_nop_stmt
|
2007-03-24 22:30:58 -04:00
|
|
|
#endif
|
|
|
|
|
2001-08-15 03:14:51 -04:00
|
|
|
/* Local API functions */
|
2013-01-17 06:59:23 -05:00
|
|
|
#ifndef DEBUGBUILD
|
|
|
|
static void _state(struct connectdata *conn,
|
|
|
|
ftpstate newstate);
|
|
|
|
#define state(x,y) _state(x,y)
|
|
|
|
#else
|
|
|
|
static void _state(struct connectdata *conn,
|
|
|
|
ftpstate newstate,
|
|
|
|
int lineno);
|
|
|
|
#define state(x,y) _state(x,y,__LINE__)
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 05:13:19 -04:00
|
|
|
static CURLcode ftp_sendquote(struct connectdata *conn,
|
|
|
|
struct curl_slist *quote);
|
2004-04-22 09:09:00 -04:00
|
|
|
static CURLcode ftp_quit(struct connectdata *conn);
|
2004-10-16 10:06:54 -04:00
|
|
|
static CURLcode ftp_parse_url_path(struct connectdata *conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_regular_transfer(struct connectdata *conn, bool *done);
|
2007-03-24 22:30:58 -04:00
|
|
|
#ifndef CURL_DISABLE_VERBOSE_STRINGS
|
2005-02-09 08:06:40 -05:00
|
|
|
static void ftp_pasv_verbose(struct connectdata *conn,
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *ai,
|
2005-02-09 08:06:40 -05:00
|
|
|
char *newhost, /* ascii version */
|
|
|
|
int port);
|
2007-03-24 22:30:58 -04:00
|
|
|
#endif
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_prepare_transfer(struct connectdata *conn);
|
|
|
|
static CURLcode ftp_state_mdtm(struct connectdata *conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_quote(struct connectdata *conn,
|
|
|
|
bool init, ftpstate instate);
|
2006-08-19 17:18:36 -04:00
|
|
|
static CURLcode ftp_nb_type(struct connectdata *conn,
|
2007-09-26 22:45:58 -04:00
|
|
|
bool ascii, ftpstate newstate);
|
2006-08-19 17:18:36 -04:00
|
|
|
static int ftp_need_type(struct connectdata *conn,
|
2006-09-07 17:49:20 -04:00
|
|
|
bool ascii);
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_do(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode ftp_done(struct connectdata *conn,
|
2009-11-26 14:07:54 -05:00
|
|
|
CURLcode, bool premature);
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_connect(struct connectdata *conn, bool *done);
|
2010-11-19 07:43:20 -05:00
|
|
|
static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection);
|
2013-08-05 17:49:53 -04:00
|
|
|
static CURLcode ftp_do_more(struct connectdata *conn, int *completed);
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_multi_statemach(struct connectdata *conn, bool *done);
|
2019-07-30 05:02:03 -04:00
|
|
|
static int ftp_getsock(struct connectdata *conn, curl_socket_t *socks);
|
|
|
|
static int ftp_domore_getsock(struct connectdata *conn, curl_socket_t *socks);
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_doing(struct connectdata *conn,
|
2009-11-26 14:07:54 -05:00
|
|
|
bool *dophase_done);
|
2020-05-13 06:56:53 -04:00
|
|
|
static CURLcode ftp_setup_connection(struct connectdata *conn);
|
2001-08-15 02:58:56 -04:00
|
|
|
|
2010-05-12 09:33:22 -04:00
|
|
|
static CURLcode init_wc_data(struct connectdata *conn);
|
|
|
|
static CURLcode wc_statemach(struct connectdata *conn);
|
|
|
|
|
2010-05-31 10:24:01 -04:00
|
|
|
static void wc_data_dtor(void *ptr);
|
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_retr(struct connectdata *conn, curl_off_t filesize);
|
2010-05-12 09:33:22 -04:00
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
static CURLcode ftp_readresp(curl_socket_t sockfd,
|
|
|
|
struct pingpong *pp,
|
|
|
|
int *ftpcode,
|
|
|
|
size_t *size);
|
2013-01-17 06:59:23 -05:00
|
|
|
static CURLcode ftp_dophase_done(struct connectdata *conn,
|
|
|
|
bool connected);
|
2011-12-19 08:35:20 -05:00
|
|
|
|
2001-01-05 05:11:41 -05:00
|
|
|
/* easy-to-use macro: */
|
2016-03-19 13:15:53 -04:00
|
|
|
#define PPSENDF(x,y,z) result = Curl_pp_sendf(x,y,z); \
|
|
|
|
if(result) \
|
|
|
|
return result
|
2001-01-05 05:11:41 -05:00
|
|
|
|
2007-08-17 18:22:43 -04:00
|
|
|
|
2007-10-12 09:36:37 -04:00
|
|
|
/*
|
|
|
|
* FTP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_ftp = {
|
2009-12-12 16:54:01 -05:00
|
|
|
"FTP", /* scheme */
|
2007-12-08 17:50:55 -05:00
|
|
|
ftp_setup_connection, /* setup_connection */
|
|
|
|
ftp_do, /* do_it */
|
|
|
|
ftp_done, /* done */
|
2011-12-20 06:52:24 -05:00
|
|
|
ftp_do_more, /* do_more */
|
2007-12-08 17:50:55 -05:00
|
|
|
ftp_connect, /* connect_it */
|
|
|
|
ftp_multi_statemach, /* connecting */
|
|
|
|
ftp_doing, /* doing */
|
|
|
|
ftp_getsock, /* proto_getsock */
|
|
|
|
ftp_getsock, /* doing_getsock */
|
2011-10-21 17:36:54 -04:00
|
|
|
ftp_domore_getsock, /* domore_getsock */
|
2008-12-19 16:14:52 -05:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
2007-12-08 17:50:55 -05:00
|
|
|
ftp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2008-12-19 16:14:52 -05:00
|
|
|
PORT_FTP, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_FTP, /* protocol */
|
2017-06-15 07:45:57 -04:00
|
|
|
PROTOPT_DUAL | PROTOPT_CLOSEACTION | PROTOPT_NEEDSPWD |
|
2017-10-25 17:51:50 -04:00
|
|
|
PROTOPT_NOURLQUERY | PROTOPT_PROXY_AS_HTTP |
|
|
|
|
PROTOPT_WILDCARD /* flags */
|
2007-10-12 09:36:37 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_SSL
|
|
|
|
/*
|
|
|
|
* FTPS protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_ftps = {
|
2009-12-12 16:54:01 -05:00
|
|
|
"FTPS", /* scheme */
|
2008-12-09 10:02:37 -05:00
|
|
|
ftp_setup_connection, /* setup_connection */
|
2007-12-08 17:50:55 -05:00
|
|
|
ftp_do, /* do_it */
|
|
|
|
ftp_done, /* done */
|
2011-12-20 06:52:24 -05:00
|
|
|
ftp_do_more, /* do_more */
|
2007-12-08 17:50:55 -05:00
|
|
|
ftp_connect, /* connect_it */
|
|
|
|
ftp_multi_statemach, /* connecting */
|
|
|
|
ftp_doing, /* doing */
|
|
|
|
ftp_getsock, /* proto_getsock */
|
|
|
|
ftp_getsock, /* doing_getsock */
|
2011-10-21 17:36:54 -04:00
|
|
|
ftp_domore_getsock, /* domore_getsock */
|
2008-12-19 16:14:52 -05:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
2007-12-08 17:50:55 -05:00
|
|
|
ftp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2008-12-19 16:14:52 -05:00
|
|
|
PORT_FTPS, /* defport */
|
2014-04-20 13:37:54 -04:00
|
|
|
CURLPROTO_FTPS, /* protocol */
|
2011-05-05 11:07:21 -04:00
|
|
|
PROTOPT_SSL | PROTOPT_DUAL | PROTOPT_CLOSEACTION |
|
2017-10-25 17:51:50 -04:00
|
|
|
PROTOPT_NEEDSPWD | PROTOPT_NOURLQUERY | PROTOPT_WILDCARD /* flags */
|
2007-10-12 09:36:37 -04:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-03-07 18:07:14 -05:00
|
|
|
static void close_secondarysocket(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET]) {
|
|
|
|
Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
|
|
|
|
conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
|
|
|
|
}
|
|
|
|
conn->bits.tcpconnect[SECONDARYSOCKET] = FALSE;
|
2020-05-13 06:22:49 -04:00
|
|
|
conn->bits.proxy_ssl_connected[SECONDARYSOCKET] = FALSE;
|
2016-03-07 18:07:14 -05:00
|
|
|
}
|
2007-10-12 09:36:37 -04:00
|
|
|
|
2007-08-17 18:22:43 -04:00
|
|
|
/*
|
|
|
|
* NOTE: back in the old days, we added code in the FTP code that made NOBODY
|
|
|
|
* requests on files respond with headers passed to the client/stdout that
|
|
|
|
* looked like HTTP ones.
|
|
|
|
*
|
|
|
|
* This approach is not very elegant, it causes confusion and is error-prone.
|
|
|
|
* It is subject for removal at the next (or at least a future) soname bump.
|
|
|
|
* Until then you can test the effects of the removal by undefining the
|
|
|
|
* following define named CURL_FTP_HTTPSTYLE_HEAD.
|
|
|
|
*/
|
|
|
|
#define CURL_FTP_HTTPSTYLE_HEAD 1
|
|
|
|
|
2007-10-24 17:14:11 -04:00
|
|
|
static void freedirs(struct ftp_conn *ftpc)
|
2003-08-05 09:04:10 -04:00
|
|
|
{
|
2006-09-07 17:49:20 -04:00
|
|
|
if(ftpc->dirs) {
|
2018-06-02 16:52:56 -04:00
|
|
|
int i;
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; i < ftpc->dirdepth; i++) {
|
2015-03-11 12:41:01 -04:00
|
|
|
free(ftpc->dirs[i]);
|
2017-09-09 17:09:06 -04:00
|
|
|
ftpc->dirs[i] = NULL;
|
2004-04-15 03:52:39 -04:00
|
|
|
}
|
2006-09-07 17:49:20 -04:00
|
|
|
free(ftpc->dirs);
|
|
|
|
ftpc->dirs = NULL;
|
2008-07-07 16:37:07 -04:00
|
|
|
ftpc->dirdepth = 0;
|
2004-04-15 03:52:39 -04:00
|
|
|
}
|
2015-01-08 12:30:01 -05:00
|
|
|
Curl_safefree(ftpc->file);
|
|
|
|
|
|
|
|
/* no longer of any use */
|
|
|
|
Curl_safefree(ftpc->newhost);
|
2003-08-05 09:04:10 -04:00
|
|
|
}
|
|
|
|
|
2001-11-01 07:47:22 -05:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2011-12-19 08:35:20 -05:00
|
|
|
* AcceptServerConnect()
|
2001-11-01 07:47:22 -05:00
|
|
|
*
|
2011-12-19 08:35:20 -05:00
|
|
|
* After connection request is received from the server this function is
|
|
|
|
* called to accept the connection and close the listening socket
|
2001-11-01 07:47:22 -05:00
|
|
|
*
|
|
|
|
*/
|
2011-12-19 08:35:20 -05:00
|
|
|
static CURLcode AcceptServerConnect(struct connectdata *conn)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2004-03-09 17:52:50 -05:00
|
|
|
curl_socket_t sock = conn->sock[SECONDARYSOCKET];
|
2009-11-26 14:07:54 -05:00
|
|
|
curl_socket_t s = CURL_SOCKET_BAD;
|
2006-08-22 17:21:01 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
2009-11-26 14:07:54 -05:00
|
|
|
struct Curl_sockaddr_storage add;
|
2006-08-22 17:21:01 -04:00
|
|
|
#else
|
2009-11-26 14:07:54 -05:00
|
|
|
struct sockaddr_in add;
|
2006-08-22 17:21:01 -04:00
|
|
|
#endif
|
2009-11-26 14:07:54 -05:00
|
|
|
curl_socklen_t size = (curl_socklen_t) sizeof(add);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
if(0 == getsockname(sock, (struct sockaddr *) &add, &size)) {
|
|
|
|
size = sizeof(add);
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
s = accept(sock, (struct sockaddr *) &add, &size);
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
|
|
|
Curl_closesocket(conn, sock); /* close the first socket */
|
|
|
|
|
|
|
|
if(CURL_SOCKET_BAD == s) {
|
|
|
|
failf(data, "Error accept()ing server connect");
|
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
|
|
|
infof(data, "Connection accepted from server\n");
|
2015-08-21 07:30:08 -04:00
|
|
|
/* when this happens within the DO state it is important that we mark us as
|
|
|
|
not needing DO_MORE anymore */
|
|
|
|
conn->bits.do_more = FALSE;
|
2011-12-19 08:35:20 -05:00
|
|
|
|
2012-06-24 19:06:50 -04:00
|
|
|
conn->sock[SECONDARYSOCKET] = s;
|
2014-10-04 09:14:27 -04:00
|
|
|
(void)curlx_nonblock(s, TRUE); /* enable non-blocking */
|
2020-05-26 08:42:47 -04:00
|
|
|
conn->bits.sock_accepted = TRUE;
|
2012-06-24 19:06:50 -04:00
|
|
|
|
2012-06-24 18:46:01 -04:00
|
|
|
if(data->set.fsockopt) {
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* activate callback for setting socket options */
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(data, true);
|
2012-06-24 18:46:01 -04:00
|
|
|
error = data->set.fsockopt(data->set.sockopt_client,
|
|
|
|
s,
|
|
|
|
CURLSOCKTYPE_ACCEPT);
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(data, false);
|
2012-06-24 18:46:01 -04:00
|
|
|
|
|
|
|
if(error) {
|
2016-03-07 18:07:14 -05:00
|
|
|
close_secondarysocket(conn);
|
2012-06-24 18:46:01 -04:00
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:55:54 -05:00
|
|
|
/*
|
|
|
|
* ftp_timeleft_accept() returns the amount of milliseconds left allowed for
|
|
|
|
* waiting server to connect. If the value is negative, the timeout time has
|
|
|
|
* already elapsed.
|
|
|
|
*
|
|
|
|
* The start time is stored in progress.t_acceptdata - as set with
|
|
|
|
* Curl_pgrsTime(..., TIMER_STARTACCEPT);
|
|
|
|
*
|
|
|
|
*/
|
2017-10-26 09:24:50 -04:00
|
|
|
static timediff_t ftp_timeleft_accept(struct Curl_easy *data)
|
2011-12-20 14:55:54 -05:00
|
|
|
{
|
2017-10-26 09:24:50 -04:00
|
|
|
timediff_t timeout_ms = DEFAULT_ACCEPT_TIMEOUT;
|
|
|
|
timediff_t other;
|
2017-07-28 09:49:36 -04:00
|
|
|
struct curltime now;
|
2011-12-20 14:55:54 -05:00
|
|
|
|
|
|
|
if(data->set.accepttimeout > 0)
|
|
|
|
timeout_ms = data->set.accepttimeout;
|
|
|
|
|
2017-10-25 05:59:43 -04:00
|
|
|
now = Curl_now();
|
2011-12-20 14:55:54 -05:00
|
|
|
|
|
|
|
/* check if the generic timeout possibly is set shorter */
|
2020-05-26 02:26:20 -04:00
|
|
|
other = Curl_timeleft(data, &now, FALSE);
|
2011-12-20 14:55:54 -05:00
|
|
|
if(other && (other < timeout_ms))
|
|
|
|
/* note that this also works fine for when other happens to be negative
|
|
|
|
due to it already having elapsed */
|
|
|
|
timeout_ms = other;
|
|
|
|
else {
|
|
|
|
/* subtract elapsed time */
|
2017-10-23 06:05:49 -04:00
|
|
|
timeout_ms -= Curl_timediff(now, data->progress.t_acceptdata);
|
2011-12-20 14:55:54 -05:00
|
|
|
if(!timeout_ms)
|
|
|
|
/* avoid returning 0 as that means no timeout! */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeout_ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* ReceivedServerConnect()
|
|
|
|
*
|
|
|
|
* After allowing server to connect to us from data port, this function
|
|
|
|
* checks both data connection for connection establishment and ctrl
|
|
|
|
* connection for a negative response regarding a failure in connecting
|
|
|
|
*
|
|
|
|
*/
|
2013-01-17 06:59:23 -05:00
|
|
|
static CURLcode ReceivedServerConnect(struct connectdata *conn, bool *received)
|
2011-12-19 08:35:20 -05:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2011-12-19 08:35:20 -05:00
|
|
|
curl_socket_t ctrl_sock = conn->sock[FIRSTSOCKET];
|
|
|
|
curl_socket_t data_sock = conn->sock[SECONDARYSOCKET];
|
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
|
|
|
struct pingpong *pp = &ftpc->pp;
|
|
|
|
int result;
|
2019-07-31 09:30:31 -04:00
|
|
|
timediff_t timeout_ms;
|
2011-12-19 08:35:20 -05:00
|
|
|
ssize_t nread;
|
|
|
|
int ftpcode;
|
|
|
|
|
|
|
|
*received = FALSE;
|
|
|
|
|
2011-12-20 14:55:54 -05:00
|
|
|
timeout_ms = ftp_timeleft_accept(data);
|
2011-12-19 08:35:20 -05:00
|
|
|
infof(data, "Checking for server connect\n");
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* if a timeout was already reached, bail out */
|
|
|
|
failf(data, "Accept timeout occurred while waiting server connect");
|
|
|
|
return CURLE_FTP_ACCEPT_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First check whether there is a cached response from server */
|
|
|
|
if(pp->cache_size && pp->cache && pp->cache[0] > '3') {
|
|
|
|
/* Data connection could not be established, let's return */
|
2012-01-16 15:14:05 -05:00
|
|
|
infof(data, "There is negative response in cache while serv connect\n");
|
2020-05-18 12:44:54 -04:00
|
|
|
(void)Curl_GetFTPResponse(&nread, conn, &ftpcode);
|
2011-12-19 08:35:20 -05:00
|
|
|
return CURLE_FTP_ACCEPT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Curl_socket_check(ctrl_sock, data_sock, CURL_SOCKET_BAD, 0);
|
|
|
|
|
|
|
|
/* see if the connection request is already here */
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(result) {
|
2011-12-19 08:35:20 -05:00
|
|
|
case -1: /* error */
|
|
|
|
/* let's die here */
|
|
|
|
failf(data, "Error while waiting for server connect");
|
|
|
|
return CURLE_FTP_ACCEPT_FAILED;
|
|
|
|
case 0: /* Server connect is not received yet */
|
|
|
|
break; /* loop */
|
|
|
|
default:
|
|
|
|
|
|
|
|
if(result & CURL_CSELECT_IN2) {
|
|
|
|
infof(data, "Ready to accept data connection from server\n");
|
|
|
|
*received = TRUE;
|
|
|
|
}
|
|
|
|
else if(result & CURL_CSELECT_IN) {
|
|
|
|
infof(data, "Ctrl conn has data while waiting for data conn\n");
|
2020-05-18 12:44:54 -04:00
|
|
|
(void)Curl_GetFTPResponse(&nread, conn, &ftpcode);
|
2011-12-19 08:35:20 -05:00
|
|
|
|
|
|
|
if(ftpcode/100 > 3)
|
|
|
|
return CURLE_FTP_ACCEPT_FAILED;
|
|
|
|
|
2016-08-22 15:37:03 -04:00
|
|
|
return CURLE_WEIRD_SERVER_REPLY;
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
} /* switch() */
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* InitiateTransfer()
|
|
|
|
*
|
|
|
|
* After connection from server is accepted this function is called to
|
|
|
|
* setup transfer parameters and initiate the data transfer.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static CURLcode InitiateTransfer(struct connectdata *conn)
|
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2011-12-19 08:35:20 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(conn->bits.ftp_use_data_ssl) {
|
2011-12-19 08:35:20 -05:00
|
|
|
/* since we only have a plaintext TCP connection here, we must now
|
|
|
|
* do the TLS stuff */
|
|
|
|
infof(data, "Doing the SSL/TLS handshake on the data stream\n");
|
|
|
|
result = Curl_ssl_connect(conn, SECONDARYSOCKET);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(conn->proto.ftpc.state_saved == FTP_STOR) {
|
|
|
|
/* When we know we're uploading a specified file, we can get the file
|
|
|
|
size prior to the actual upload. */
|
2014-04-15 07:49:18 -04:00
|
|
|
Curl_pgrsSetUploadSize(data, data->state.infilesize);
|
2011-12-19 08:35:20 -05:00
|
|
|
|
|
|
|
/* set the SO_SNDBUF for the secondary socket for those who need it */
|
|
|
|
Curl_sndbufset(conn->sock[SECONDARYSOCKET]);
|
|
|
|
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, SECONDARYSOCKET);
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* FTP download: */
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, SECONDARYSOCKET,
|
|
|
|
conn->proto.ftpc.retr_size_saved, FALSE, -1);
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
conn->proto.ftpc.pp.pending_resp = TRUE; /* expect server response */
|
|
|
|
state(conn, FTP_STOP);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2009-11-26 14:07:54 -05:00
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* AllowServerConnect()
|
|
|
|
*
|
2013-01-17 06:59:23 -05:00
|
|
|
* When we've issue the PORT command, we have told the server to connect to
|
|
|
|
* us. This function checks whether data connection is established if so it is
|
|
|
|
* accepted.
|
2011-12-19 08:35:20 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static CURLcode AllowServerConnect(struct connectdata *conn, bool *connected)
|
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2019-07-31 09:30:31 -04:00
|
|
|
timediff_t timeout_ms;
|
2014-10-23 16:56:35 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2011-12-19 08:35:20 -05:00
|
|
|
|
|
|
|
*connected = FALSE;
|
|
|
|
infof(data, "Preparing for accepting server on data port\n");
|
|
|
|
|
|
|
|
/* Save the time we start accepting server connect */
|
|
|
|
Curl_pgrsTime(data, TIMER_STARTACCEPT);
|
|
|
|
|
2013-01-17 06:59:23 -05:00
|
|
|
timeout_ms = ftp_timeleft_accept(data);
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* if a timeout was already reached, bail out */
|
|
|
|
failf(data, "Accept timeout occurred while waiting server connect");
|
|
|
|
return CURLE_FTP_ACCEPT_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see if the connection request is already here */
|
2014-10-23 16:56:35 -04:00
|
|
|
result = ReceivedServerConnect(conn, connected);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2009-11-26 14:07:54 -05:00
|
|
|
|
2013-01-17 06:59:23 -05:00
|
|
|
if(*connected) {
|
2014-10-23 16:56:35 -04:00
|
|
|
result = AcceptServerConnect(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2009-11-26 14:07:54 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
result = InitiateTransfer(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2013-01-17 06:59:23 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Add timeout to multi handle and break out of the loop */
|
2019-09-19 03:14:15 -04:00
|
|
|
if(*connected == FALSE) {
|
2017-05-09 06:47:49 -04:00
|
|
|
Curl_expire(data, data->set.accepttimeout > 0 ?
|
|
|
|
data->set.accepttimeout: DEFAULT_ACCEPT_TIMEOUT, 0);
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2011-12-19 08:35:20 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
return result;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2007-08-22 10:18:06 -04:00
|
|
|
/* macro to check for a three-digit ftp status code at the start of the
|
|
|
|
given string */
|
2009-12-12 16:54:01 -05:00
|
|
|
#define STATUSCODE(line) (ISDIGIT(line[0]) && ISDIGIT(line[1]) && \
|
|
|
|
ISDIGIT(line[2]))
|
2007-08-22 10:18:06 -04:00
|
|
|
|
2005-03-29 06:35:25 -05:00
|
|
|
/* macro to check for the last line in an FTP server response */
|
2007-08-22 10:18:06 -04:00
|
|
|
#define LASTLINE(line) (STATUSCODE(line) && (' ' == line[3]))
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2013-02-12 13:08:48 -05:00
|
|
|
static bool ftp_endofresp(struct connectdata *conn, char *line, size_t len,
|
|
|
|
int *code)
|
2009-12-12 16:54:01 -05:00
|
|
|
{
|
2013-02-12 13:08:48 -05:00
|
|
|
(void)conn;
|
2009-12-12 16:54:01 -05:00
|
|
|
|
|
|
|
if((len > 3) && LASTLINE(line)) {
|
2010-12-01 17:33:43 -05:00
|
|
|
*code = curlx_sltosi(strtol(line, NULL, 10));
|
2013-02-12 13:08:48 -05:00
|
|
|
return TRUE;
|
2009-12-12 16:54:01 -05:00
|
|
|
}
|
2013-02-12 13:08:48 -05:00
|
|
|
|
|
|
|
return FALSE;
|
2009-12-12 16:54:01 -05:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_readresp(curl_socket_t sockfd,
|
2009-12-12 16:54:01 -05:00
|
|
|
struct pingpong *pp,
|
2005-02-09 08:06:40 -05:00
|
|
|
int *ftpcode, /* return the ftp-code if done */
|
|
|
|
size_t *size) /* size of the response */
|
|
|
|
{
|
2009-12-12 16:54:01 -05:00
|
|
|
struct connectdata *conn = pp->conn;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-08-23 10:16:42 -04:00
|
|
|
#ifdef HAVE_GSSAPI
|
2008-08-11 19:16:08 -04:00
|
|
|
char * const buf = data->state.buffer;
|
2009-12-14 09:02:43 -05:00
|
|
|
#endif
|
2009-12-12 16:54:01 -05:00
|
|
|
int code;
|
2019-05-25 04:06:08 -04:00
|
|
|
CURLcode result = Curl_pp_readresp(sockfd, pp, &code, size);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2013-08-23 10:16:42 -04:00
|
|
|
#if defined(HAVE_GSSAPI)
|
2005-02-09 08:06:40 -05:00
|
|
|
/* handle the security-oriented responses 6xx ***/
|
|
|
|
switch(code) {
|
|
|
|
case 631:
|
2010-11-13 17:42:34 -05:00
|
|
|
code = Curl_sec_read_msg(conn, buf, PROT_SAFE);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
case 632:
|
2010-11-13 17:42:34 -05:00
|
|
|
code = Curl_sec_read_msg(conn, buf, PROT_PRIVATE);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
case 633:
|
2010-11-13 17:42:34 -05:00
|
|
|
code = Curl_sec_read_msg(conn, buf, PROT_CONFIDENTIAL);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* normal ftp stuff we pass through! */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* store the latest code for later retrieval */
|
2017-09-09 17:09:06 -04:00
|
|
|
data->info.httpcode = code;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
if(ftpcode)
|
|
|
|
*ftpcode = code;
|
2009-02-20 03:16:03 -05:00
|
|
|
|
2012-11-04 12:22:48 -05:00
|
|
|
if(421 == code) {
|
2010-11-09 11:04:04 -05:00
|
|
|
/* 421 means "Service not available, closing control connection." and FTP
|
|
|
|
* servers use it to signal that idle session timeout has been exceeded.
|
2012-11-04 12:22:48 -05:00
|
|
|
* If we ignored the response, it could end up hanging in some cases.
|
|
|
|
*
|
|
|
|
* This response code can come at any point so having it treated
|
|
|
|
* generically is a good idea.
|
|
|
|
*/
|
|
|
|
infof(data, "We got a 421 - timeout!\n");
|
|
|
|
state(conn, FTP_STOP);
|
2010-11-09 11:04:04 -05:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
2012-11-04 12:22:48 -05:00
|
|
|
}
|
2010-11-09 11:04:04 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
/* --- parse FTP server responses --- */
|
|
|
|
|
2001-08-23 02:10:01 -04:00
|
|
|
/*
|
2007-08-30 10:06:00 -04:00
|
|
|
* Curl_GetFTPResponse() is a BLOCKING function to read the full response
|
|
|
|
* from a server after a command.
|
|
|
|
*
|
2001-08-23 02:10:01 -04:00
|
|
|
*/
|
2001-04-26 06:29:24 -04:00
|
|
|
|
2003-03-11 13:58:21 -05:00
|
|
|
CURLcode Curl_GetFTPResponse(ssize_t *nreadp, /* return number of bytes read */
|
2002-12-03 05:25:31 -05:00
|
|
|
struct connectdata *conn,
|
|
|
|
int *ftpcode) /* return the ftp-code */
|
2001-04-26 06:29:24 -04:00
|
|
|
{
|
2005-02-09 08:06:40 -05:00
|
|
|
/*
|
|
|
|
* We cannot read just one byte per read() and then go back to select() as
|
|
|
|
* the OpenSSL read() doesn't grok that properly.
|
2001-04-26 06:29:24 -04:00
|
|
|
*
|
|
|
|
* Alas, read as much as possible, split up into lines, use the ending
|
2001-08-23 02:10:01 -04:00
|
|
|
* line in a response or continue reading. */
|
2001-04-26 06:29:24 -04:00
|
|
|
|
2004-03-09 17:52:50 -05:00
|
|
|
curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2002-12-03 05:25:31 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2009-12-12 16:54:01 -05:00
|
|
|
struct pingpong *pp = &ftpc->pp;
|
2007-08-30 10:06:00 -04:00
|
|
|
size_t nread;
|
2017-09-09 17:09:06 -04:00
|
|
|
int cache_skip = 0;
|
|
|
|
int value_to_be_ignored = 0;
|
2001-12-03 08:48:59 -05:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(ftpcode)
|
2001-09-04 22:49:04 -04:00
|
|
|
*ftpcode = 0; /* 0 for errors */
|
2008-05-07 17:02:21 -04:00
|
|
|
else
|
|
|
|
/* make the pointer point to something for the rest of this function */
|
|
|
|
ftpcode = &value_to_be_ignored;
|
2001-04-26 06:29:24 -04:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
*nreadp = 0;
|
2001-04-26 06:29:24 -04:00
|
|
|
|
2007-08-30 10:06:00 -04:00
|
|
|
while(!*ftpcode && !result) {
|
2002-04-27 09:24:06 -04:00
|
|
|
/* check and reset timeout value every lap */
|
2020-05-28 18:08:03 -04:00
|
|
|
timediff_t timeout = Curl_pp_state_timeout(pp, FALSE);
|
|
|
|
timediff_t interval_ms;
|
2002-12-03 05:25:31 -05:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
if(timeout <= 0) {
|
2004-03-05 04:37:48 -05:00
|
|
|
failf(data, "FTP response timeout");
|
2002-12-03 05:25:31 -05:00
|
|
|
return CURLE_OPERATION_TIMEDOUT; /* already too little time */
|
2002-04-27 09:24:06 -04:00
|
|
|
}
|
|
|
|
|
2009-11-26 14:07:54 -05:00
|
|
|
interval_ms = 1000; /* use 1 second timeout intervals */
|
2007-08-30 10:06:00 -04:00
|
|
|
if(timeout < interval_ms)
|
|
|
|
interval_ms = timeout;
|
|
|
|
|
2007-09-05 17:41:07 -04:00
|
|
|
/*
|
|
|
|
* Since this function is blocking, we need to wait here for input on the
|
|
|
|
* connection and only then we call the response reading function. We do
|
|
|
|
* timeout at least every second to make the timeout check run.
|
|
|
|
*
|
|
|
|
* A caution here is that the ftp_readresp() function has a cache that may
|
|
|
|
* contain pieces of a response from the previous invoke and we need to
|
|
|
|
* make sure we don't just wait for input while there is unhandled data in
|
|
|
|
* that cache. But also, if the cache is there, we call ftp_readresp() and
|
|
|
|
* the cache wasn't good enough to continue we must not just busy-loop
|
|
|
|
* around this function.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
if(pp->cache && (cache_skip < 2)) {
|
2007-09-05 17:41:07 -04:00
|
|
|
/*
|
|
|
|
* There's a cache left since before. We then skipping the wait for
|
|
|
|
* socket action, unless this is the same cache like the previous round
|
|
|
|
* as then the cache was deemed not enough to act on and we then need to
|
|
|
|
* wait for more data anyway.
|
|
|
|
*/
|
|
|
|
}
|
2016-12-08 16:32:36 -05:00
|
|
|
else if(!Curl_conn_data_pending(conn, FIRSTSOCKET)) {
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(SOCKET_READABLE(sockfd, interval_ms)) {
|
2007-09-05 17:41:07 -04:00
|
|
|
case -1: /* select() error, stop reading */
|
|
|
|
failf(data, "FTP response aborted due to select/poll error: %d",
|
|
|
|
SOCKERRNO);
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
|
|
|
|
case 0: /* timeout */
|
|
|
|
if(Curl_pgrsUpdate(conn))
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
continue; /* just continue in our loop for the timeout duration */
|
|
|
|
|
|
|
|
default: /* for clarity */
|
|
|
|
break;
|
|
|
|
}
|
2002-04-27 09:09:37 -04:00
|
|
|
}
|
2009-12-12 16:54:01 -05:00
|
|
|
result = ftp_readresp(sockfd, pp, ftpcode, &nread);
|
2007-08-30 10:06:00 -04:00
|
|
|
if(result)
|
|
|
|
break;
|
2001-12-03 08:48:59 -05:00
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
if(!nread && pp->cache)
|
2007-09-05 17:41:07 -04:00
|
|
|
/* bump cache skip counter as on repeated skips we must wait for more
|
|
|
|
data */
|
|
|
|
cache_skip++;
|
|
|
|
else
|
|
|
|
/* when we got data or there is no cache left, we reset the cache skip
|
|
|
|
counter */
|
2017-09-09 17:09:06 -04:00
|
|
|
cache_skip = 0;
|
2007-09-05 17:41:07 -04:00
|
|
|
|
2007-08-30 10:06:00 -04:00
|
|
|
*nreadp += nread;
|
2007-09-05 17:41:07 -04:00
|
|
|
|
2001-04-26 06:29:24 -04:00
|
|
|
} /* while there's buffer left and loop is requested */
|
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
pp->pending_resp = FALSE;
|
2009-02-20 03:16:03 -05:00
|
|
|
|
2002-12-03 05:25:31 -05:00
|
|
|
return result;
|
2001-04-26 06:29:24 -04:00
|
|
|
}
|
|
|
|
|
2013-08-14 16:28:32 -04:00
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
/* for debug purposes */
|
|
|
|
static const char * const ftp_state_names[]={
|
|
|
|
"STOP",
|
|
|
|
"WAIT220",
|
|
|
|
"AUTH",
|
|
|
|
"USER",
|
|
|
|
"PASS",
|
|
|
|
"ACCT",
|
|
|
|
"PBSZ",
|
|
|
|
"PROT",
|
|
|
|
"CCC",
|
|
|
|
"PWD",
|
|
|
|
"SYST",
|
|
|
|
"NAMEFMT",
|
|
|
|
"QUOTE",
|
|
|
|
"RETR_PREQUOTE",
|
|
|
|
"STOR_PREQUOTE",
|
|
|
|
"POSTQUOTE",
|
|
|
|
"CWD",
|
|
|
|
"MKD",
|
|
|
|
"MDTM",
|
|
|
|
"TYPE",
|
|
|
|
"LIST_TYPE",
|
|
|
|
"RETR_TYPE",
|
|
|
|
"STOR_TYPE",
|
|
|
|
"SIZE",
|
|
|
|
"RETR_SIZE",
|
|
|
|
"STOR_SIZE",
|
|
|
|
"REST",
|
|
|
|
"RETR_REST",
|
|
|
|
"PORT",
|
|
|
|
"PRET",
|
|
|
|
"PASV",
|
|
|
|
"LIST",
|
|
|
|
"RETR",
|
|
|
|
"STOR",
|
|
|
|
"QUIT"
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* This is the ONLY way to change FTP state! */
|
2013-01-17 06:59:23 -05:00
|
|
|
static void _state(struct connectdata *conn,
|
|
|
|
ftpstate newstate
|
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
, int lineno
|
|
|
|
#endif
|
|
|
|
)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2014-12-13 07:32:32 -05:00
|
|
|
|
|
|
|
#if defined(DEBUGBUILD)
|
|
|
|
|
|
|
|
#if defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
(void) lineno;
|
|
|
|
#else
|
2007-09-26 21:45:22 -04:00
|
|
|
if(ftpc->state != newstate)
|
2013-01-17 06:59:23 -05:00
|
|
|
infof(conn->data, "FTP %p (line %d) state change from %s to %s\n",
|
2013-08-14 16:28:32 -04:00
|
|
|
(void *)ftpc, lineno, ftp_state_names[ftpc->state],
|
|
|
|
ftp_state_names[newstate]);
|
2005-02-09 08:06:40 -05:00
|
|
|
#endif
|
2014-12-13 07:32:32 -05:00
|
|
|
#endif
|
|
|
|
|
2007-09-26 21:45:22 -04:00
|
|
|
ftpc->state = newstate;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2003-11-24 02:15:37 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_user(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
/* send USER */
|
2020-02-06 09:59:18 -05:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "USER %s", conn->user?conn->user:"");
|
2003-11-24 02:15:37 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_USER);
|
2006-07-25 18:45:21 -04:00
|
|
|
conn->data->state.ftp_trying_alternative = FALSE;
|
2003-11-24 02:15:37 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2003-12-02 08:21:53 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_pwd(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
2003-11-24 02:15:37 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* send PWD to discover our entry point */
|
2013-07-24 11:06:28 -04:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "%s", "PWD");
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_PWD);
|
2003-12-02 08:21:53 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2003-12-02 08:21:53 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* For the FTP "protocol connect" and "doing" phases only */
|
2007-12-08 17:50:55 -05:00
|
|
|
static int ftp_getsock(struct connectdata *conn,
|
2019-07-30 05:02:03 -04:00
|
|
|
curl_socket_t *socks)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
2019-07-30 05:02:03 -04:00
|
|
|
return Curl_pp_getsock(&conn->proto.ftpc.pp, socks);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2011-10-21 17:36:54 -04:00
|
|
|
/* For the FTP "DO_MORE" phase only */
|
2019-07-30 05:02:03 -04:00
|
|
|
static int ftp_domore_getsock(struct connectdata *conn, curl_socket_t *socks)
|
2011-10-21 17:36:54 -04:00
|
|
|
{
|
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
|
|
|
|
|
|
|
/* When in DO_MORE state, we could be either waiting for us to connect to a
|
2013-08-20 16:45:47 -04:00
|
|
|
* remote site, or we could wait for that site to connect to us. Or just
|
|
|
|
* handle ordinary commands.
|
|
|
|
*/
|
2011-10-21 17:36:54 -04:00
|
|
|
|
2020-02-14 10:16:54 -05:00
|
|
|
if(SOCKS_STATE(conn->cnnct.state))
|
|
|
|
return Curl_SOCKS_getsock(conn, socks, SECONDARYSOCKET);
|
|
|
|
|
2013-08-20 16:45:47 -04:00
|
|
|
if(FTP_STOP == ftpc->state) {
|
2014-03-24 18:21:10 -04:00
|
|
|
int bits = GETSOCK_READSOCK(0);
|
2020-05-14 18:40:25 -04:00
|
|
|
bool any = FALSE;
|
2014-03-24 18:21:10 -04:00
|
|
|
|
2013-08-20 16:45:47 -04:00
|
|
|
/* if stopped and still in this state, then we're also waiting for a
|
|
|
|
connect on the secondary connection */
|
|
|
|
socks[0] = conn->sock[FIRSTSOCKET];
|
2011-10-21 17:36:54 -04:00
|
|
|
|
2014-03-24 18:21:10 -04:00
|
|
|
if(!conn->data->set.ftp_use_port) {
|
|
|
|
int s;
|
|
|
|
int i;
|
|
|
|
/* PORT is used to tell the server to connect to us, and during that we
|
|
|
|
don't do happy eyeballs, but we do if we connect to the server */
|
2017-09-09 17:09:06 -04:00
|
|
|
for(s = 1, i = 0; i<2; i++) {
|
2014-03-24 18:21:10 -04:00
|
|
|
if(conn->tempsock[i] != CURL_SOCKET_BAD) {
|
|
|
|
socks[s] = conn->tempsock[i];
|
|
|
|
bits |= GETSOCK_WRITESOCK(s++);
|
2020-05-14 18:40:25 -04:00
|
|
|
any = TRUE;
|
2014-03-24 18:21:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 18:40:25 -04:00
|
|
|
if(!any) {
|
2014-03-24 18:21:10 -04:00
|
|
|
socks[1] = conn->sock[SECONDARYSOCKET];
|
2016-08-23 08:13:18 -04:00
|
|
|
bits |= GETSOCK_WRITESOCK(1) | GETSOCK_READSOCK(1);
|
2014-03-24 18:21:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return bits;
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
2019-07-30 05:02:03 -04:00
|
|
|
return Curl_pp_getsock(&conn->proto.ftpc.pp, socks);
|
2011-10-21 17:36:54 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* This is called after the FTP_QUOTE state is passed.
|
2004-10-01 07:22:11 -04:00
|
|
|
|
2009-01-19 14:09:39 -05:00
|
|
|
ftp_state_cwd() sends the range of CWD commands to the server to change to
|
2005-02-09 08:06:40 -05:00
|
|
|
the correct directory. It may also need to send MKD commands to create
|
|
|
|
missing ones, if that option is enabled.
|
|
|
|
*/
|
|
|
|
static CURLcode ftp_state_cwd(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
if(ftpc->cwddone)
|
2005-02-09 08:06:40 -05:00
|
|
|
/* already done and fine */
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_mdtm(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
else {
|
2019-09-19 08:59:13 -04:00
|
|
|
/* FTPFILE_NOCWD with full path: expect ftpc->cwddone! */
|
|
|
|
DEBUGASSERT((conn->data->set.ftp_filemethod != FTPFILE_NOCWD) ||
|
|
|
|
!(ftpc->dirdepth && ftpc->dirs[0][0] == '/'));
|
|
|
|
|
2009-02-17 04:07:25 -05:00
|
|
|
ftpc->count2 = 0; /* count2 counts failed CWDs */
|
|
|
|
|
|
|
|
/* count3 is set to allow a MKD to fail once. In the case when first CWD
|
|
|
|
fails and then MKD fails (due to another session raced it to create the
|
|
|
|
dir) this then allows for a second try to CWD to it */
|
2017-09-09 17:09:06 -04:00
|
|
|
ftpc->count3 = (conn->data->set.ftp_create_missing_dirs == 2)?1:0;
|
2009-02-17 04:07:25 -05:00
|
|
|
|
2019-09-19 08:59:13 -04:00
|
|
|
if(conn->bits.reuse && ftpc->entrypath &&
|
|
|
|
/* no need to go to entrypath when we have an absolute path */
|
|
|
|
!(ftpc->dirdepth && ftpc->dirs[0][0] == '/')) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* This is a re-used connection. Since we change directory to where the
|
|
|
|
transfer is taking place, we must first get back to the original dir
|
|
|
|
where we ended up after login: */
|
2017-08-03 17:48:57 -04:00
|
|
|
ftpc->cwdcount = 0; /* we count this as the first path, then we add one
|
|
|
|
for all upcoming ones in the ftp->dirs[] array */
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "CWD %s", ftpc->entrypath);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_CWD);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else {
|
2006-09-07 17:49:20 -04:00
|
|
|
if(ftpc->dirdepth) {
|
2017-08-03 17:48:57 -04:00
|
|
|
ftpc->cwdcount = 1;
|
2005-02-09 08:06:40 -05:00
|
|
|
/* issue the first CWD, the rest is sent when the CWD responses are
|
|
|
|
received... */
|
2017-08-03 17:48:57 -04:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "CWD %s", ftpc->dirs[ftpc->cwdcount -1]);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_CWD);
|
2005-01-25 17:13:12 -05:00
|
|
|
}
|
|
|
|
else {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* No CWD necessary */
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_mdtm(conn);
|
2005-01-25 17:13:12 -05:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2006-01-19 18:52:03 -05:00
|
|
|
typedef enum {
|
|
|
|
EPRT,
|
|
|
|
PORT,
|
|
|
|
DONE
|
|
|
|
} ftpport;
|
2000-05-22 10:12:12 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_use_port(struct connectdata *conn,
|
|
|
|
ftpport fcmd) /* start with this */
|
2003-12-01 05:14:39 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
|
|
|
curl_socket_t portsock = CURL_SOCKET_BAD;
|
2020-02-04 17:27:39 -05:00
|
|
|
char myhost[MAX_IPADR_LEN + 1] = "";
|
2003-12-01 05:14:39 -05:00
|
|
|
|
2005-11-12 17:10:42 -05:00
|
|
|
struct Curl_sockaddr_storage ss;
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *res, *ai;
|
2009-05-01 22:37:32 -04:00
|
|
|
curl_socklen_t sslen;
|
2005-02-09 08:06:40 -05:00
|
|
|
char hbuf[NI_MAXHOST];
|
2017-09-09 17:09:06 -04:00
|
|
|
struct sockaddr *sa = (struct sockaddr *)&ss;
|
2012-06-12 07:12:09 -04:00
|
|
|
struct sockaddr_in * const sa4 = (void *)sa;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_in6 * const sa6 = (void *)sa;
|
|
|
|
#endif
|
2008-10-22 01:46:29 -04:00
|
|
|
static const char mode[][5] = { "EPRT", "PORT" };
|
2020-02-14 10:16:54 -05:00
|
|
|
enum resolve_t rc;
|
2005-02-09 08:06:40 -05:00
|
|
|
int error;
|
2011-10-24 14:42:01 -04:00
|
|
|
char *host = NULL;
|
2009-06-29 16:46:01 -04:00
|
|
|
char *string_ftpport = data->set.str[STRING_FTPPORT];
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_dns_entry *h = NULL;
|
2009-06-29 16:46:01 -04:00
|
|
|
unsigned short port_min = 0;
|
|
|
|
unsigned short port_max = 0;
|
|
|
|
unsigned short port;
|
2011-11-06 10:53:07 -05:00
|
|
|
bool possibly_non_local = TRUE;
|
2019-02-25 12:12:51 -05:00
|
|
|
char buffer[STRERROR_LEN];
|
2009-06-29 16:46:01 -04:00
|
|
|
char *addr = NULL;
|
|
|
|
|
|
|
|
/* Step 1, figure out what is requested,
|
|
|
|
* accepted format :
|
|
|
|
* (ipv4|ipv6|domain|interface)?(:port(-range)?)?
|
|
|
|
*/
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2007-08-01 17:20:01 -04:00
|
|
|
if(data->set.str[STRING_FTPPORT] &&
|
|
|
|
(strlen(data->set.str[STRING_FTPPORT]) > 1)) {
|
2009-06-29 16:46:01 -04:00
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
size_t addrlen = INET6_ADDRSTRLEN > strlen(string_ftpport) ?
|
|
|
|
INET6_ADDRSTRLEN : strlen(string_ftpport);
|
|
|
|
#else
|
|
|
|
size_t addrlen = INET_ADDRSTRLEN > strlen(string_ftpport) ?
|
|
|
|
INET_ADDRSTRLEN : strlen(string_ftpport);
|
|
|
|
#endif
|
|
|
|
char *ip_start = string_ftpport;
|
|
|
|
char *ip_end = NULL;
|
|
|
|
char *port_start = NULL;
|
|
|
|
char *port_sep = NULL;
|
|
|
|
|
2017-09-09 17:55:08 -04:00
|
|
|
addr = calloc(addrlen + 1, 1);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!addr)
|
2009-08-10 22:30:53 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2009-06-29 16:46:01 -04:00
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(*string_ftpport == '[') {
|
|
|
|
/* [ipv6]:port(-range) */
|
|
|
|
ip_start = string_ftpport + 1;
|
2016-12-13 19:29:44 -05:00
|
|
|
ip_end = strchr(string_ftpport, ']');
|
|
|
|
if(ip_end)
|
2009-06-29 16:46:01 -04:00
|
|
|
strncpy(addr, ip_start, ip_end - ip_start);
|
2010-01-22 18:21:39 -05:00
|
|
|
}
|
|
|
|
else
|
2009-06-29 16:46:01 -04:00
|
|
|
#endif
|
2011-04-22 17:01:30 -04:00
|
|
|
if(*string_ftpport == ':') {
|
2009-06-29 16:46:01 -04:00
|
|
|
/* :port */
|
|
|
|
ip_end = string_ftpport;
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
2016-12-13 19:29:44 -05:00
|
|
|
else {
|
|
|
|
ip_end = strchr(string_ftpport, ':');
|
|
|
|
if(ip_end) {
|
|
|
|
/* either ipv6 or (ipv4|domain|interface):port(-range) */
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(Curl_inet_pton(AF_INET6, string_ftpport, sa6) == 1) {
|
|
|
|
/* ipv6 */
|
|
|
|
port_min = port_max = 0;
|
|
|
|
strcpy(addr, string_ftpport);
|
|
|
|
ip_end = NULL; /* this got no port ! */
|
|
|
|
}
|
|
|
|
else
|
2009-06-29 16:46:01 -04:00
|
|
|
#endif
|
2016-12-13 19:29:44 -05:00
|
|
|
/* (ipv4|domain|interface):port(-range) */
|
|
|
|
strncpy(addr, string_ftpport, ip_end - ip_start);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* ipv4|interface */
|
|
|
|
strcpy(addr, string_ftpport);
|
|
|
|
}
|
2009-06-29 16:46:01 -04:00
|
|
|
|
|
|
|
/* parse the port */
|
2011-04-22 17:01:30 -04:00
|
|
|
if(ip_end != NULL) {
|
2016-12-13 19:29:44 -05:00
|
|
|
port_start = strchr(ip_end, ':');
|
|
|
|
if(port_start) {
|
2017-09-09 17:55:08 -04:00
|
|
|
port_min = curlx_ultous(strtoul(port_start + 1, NULL, 10));
|
2016-12-13 19:29:44 -05:00
|
|
|
port_sep = strchr(port_start, '-');
|
|
|
|
if(port_sep) {
|
2010-02-23 19:03:06 -05:00
|
|
|
port_max = curlx_ultous(strtoul(port_sep + 1, NULL, 10));
|
2009-06-29 16:46:01 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
port_max = port_min;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* correct errors like:
|
|
|
|
* :1234-1230
|
2016-04-03 14:28:34 -04:00
|
|
|
* :-4711, in this case port_min is (unsigned)-1,
|
2009-06-29 16:46:01 -04:00
|
|
|
* therefore port_min > port_max for all cases
|
|
|
|
* but port_max = (unsigned)-1
|
|
|
|
*/
|
2016-04-03 14:28:34 -04:00
|
|
|
if(port_min > port_max)
|
2009-06-29 16:46:01 -04:00
|
|
|
port_min = port_max = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if(*addr != '\0') {
|
|
|
|
/* attempt to get the address of the given interface name */
|
2014-12-16 07:29:01 -05:00
|
|
|
switch(Curl_if2ip(conn->ip_addr->ai_family,
|
|
|
|
Curl_ipv6_scope(conn->ip_addr->ai_addr),
|
|
|
|
conn->scope_id, addr, hbuf, sizeof(hbuf))) {
|
2013-04-03 16:06:51 -04:00
|
|
|
case IF2IP_NOT_FOUND:
|
|
|
|
/* not an interface, use the given string as host name instead */
|
|
|
|
host = addr;
|
|
|
|
break;
|
|
|
|
case IF2IP_AF_NOT_SUPPORTED:
|
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
case IF2IP_FOUND:
|
|
|
|
host = hbuf; /* use the hbuf for host name */
|
|
|
|
}
|
2011-09-07 16:46:06 -04:00
|
|
|
}
|
|
|
|
else
|
2009-06-29 16:46:01 -04:00
|
|
|
/* there was only a port(-range) given, default the host */
|
|
|
|
host = NULL;
|
2005-02-09 08:06:40 -05:00
|
|
|
} /* data->set.ftpport */
|
2003-12-01 05:14:39 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(!host) {
|
|
|
|
/* not an interface and not a host name, get default by extracting
|
|
|
|
the IP from the control connection */
|
|
|
|
sslen = sizeof(ss);
|
2012-06-12 07:12:09 -04:00
|
|
|
if(getsockname(conn->sock[FIRSTSOCKET], sa, &sslen)) {
|
2006-09-08 18:17:39 -04:00
|
|
|
failf(data, "getsockname() failed: %s",
|
2019-02-25 12:12:51 -05:00
|
|
|
Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2015-03-16 10:01:15 -04:00
|
|
|
free(addr);
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
2012-06-12 07:12:09 -04:00
|
|
|
switch(sa->sa_family) {
|
2009-07-03 21:04:23 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
2011-04-20 09:17:42 -04:00
|
|
|
case AF_INET6:
|
2012-06-12 07:12:09 -04:00
|
|
|
Curl_inet_ntop(sa->sa_family, &sa6->sin6_addr, hbuf, sizeof(hbuf));
|
2011-04-20 09:17:42 -04:00
|
|
|
break;
|
2009-07-03 21:04:23 -04:00
|
|
|
#endif
|
2011-04-20 09:17:42 -04:00
|
|
|
default:
|
2012-06-12 07:12:09 -04:00
|
|
|
Curl_inet_ntop(sa->sa_family, &sa4->sin_addr, hbuf, sizeof(hbuf));
|
2011-04-20 09:17:42 -04:00
|
|
|
break;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
host = hbuf; /* use this host name */
|
2011-11-06 10:53:07 -05:00
|
|
|
possibly_non_local = FALSE; /* we know it is local now */
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
|
2009-06-29 16:46:01 -04:00
|
|
|
/* resolv ip/host to ip */
|
2019-05-09 04:58:04 -04:00
|
|
|
rc = Curl_resolv(conn, host, 0, FALSE, &h);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(rc == CURLRESOLV_PENDING)
|
2011-01-30 18:10:35 -05:00
|
|
|
(void)Curl_resolver_wait_resolv(conn, &h);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(h) {
|
|
|
|
res = h->addr;
|
|
|
|
/* when we return from this function, we can forget about this entry
|
|
|
|
to we can unlock it now already */
|
|
|
|
Curl_resolv_unlock(data, h);
|
|
|
|
} /* (h) */
|
|
|
|
else
|
|
|
|
res = NULL; /* failure! */
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(res == NULL) {
|
2011-10-04 10:24:50 -04:00
|
|
|
failf(data, "failed to resolve the address provided to PORT: %s", host);
|
2015-03-16 10:01:15 -04:00
|
|
|
free(addr);
|
2009-06-29 16:46:01 -04:00
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
2006-01-19 18:52:03 -05:00
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(addr);
|
2011-10-24 14:42:01 -04:00
|
|
|
host = NULL;
|
|
|
|
|
2006-01-19 18:52:03 -05:00
|
|
|
/* step 2, create a socket for the requested address */
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
portsock = CURL_SOCKET_BAD;
|
|
|
|
error = 0;
|
2011-04-20 09:17:42 -04:00
|
|
|
for(ai = res; ai; ai = ai->ai_next) {
|
2011-12-05 05:42:10 -05:00
|
|
|
result = Curl_socket(conn, ai, NULL, &portsock);
|
|
|
|
if(result) {
|
2007-02-16 13:19:35 -05:00
|
|
|
error = SOCKERRNO;
|
2005-02-09 08:06:40 -05:00
|
|
|
continue;
|
|
|
|
}
|
2006-01-19 18:52:03 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!ai) {
|
2019-02-25 12:12:51 -05:00
|
|
|
failf(data, "socket failure: %s",
|
|
|
|
Curl_strerror(error, buffer, sizeof(buffer)));
|
2006-01-19 18:52:03 -05:00
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2006-01-19 18:52:03 -05:00
|
|
|
/* step 3, bind to a suitable local address */
|
|
|
|
|
2012-06-12 07:12:09 -04:00
|
|
|
memcpy(sa, ai->ai_addr, ai->ai_addrlen);
|
2009-06-29 16:46:01 -04:00
|
|
|
sslen = ai->ai_addrlen;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2011-04-22 17:01:30 -04:00
|
|
|
for(port = port_min; port <= port_max;) {
|
2012-06-12 07:12:09 -04:00
|
|
|
if(sa->sa_family == AF_INET)
|
|
|
|
sa4->sin_port = htons(port);
|
2009-06-29 16:46:01 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
2006-01-19 18:52:03 -05:00
|
|
|
else
|
2012-06-12 07:12:09 -04:00
|
|
|
sa6->sin6_port = htons(port);
|
2009-06-29 16:46:01 -04:00
|
|
|
#endif
|
|
|
|
/* Try binding the given address. */
|
2012-06-12 07:12:09 -04:00
|
|
|
if(bind(portsock, sa, sslen) ) {
|
2009-06-29 16:46:01 -04:00
|
|
|
/* It failed. */
|
2010-01-29 11:38:43 -05:00
|
|
|
error = SOCKERRNO;
|
2011-11-06 10:53:07 -05:00
|
|
|
if(possibly_non_local && (error == EADDRNOTAVAIL)) {
|
2010-02-02 03:48:58 -05:00
|
|
|
/* The requested bind address is not local. Use the address used for
|
|
|
|
* the control connection instead and restart the port loop
|
2009-06-29 16:46:01 -04:00
|
|
|
*/
|
2012-01-16 15:14:05 -05:00
|
|
|
infof(data, "bind(port=%hu) on non-local address failed: %s\n", port,
|
2019-02-25 12:12:51 -05:00
|
|
|
Curl_strerror(error, buffer, sizeof(buffer)));
|
2009-06-29 16:46:01 -04:00
|
|
|
|
|
|
|
sslen = sizeof(ss);
|
2012-06-12 07:12:09 -04:00
|
|
|
if(getsockname(conn->sock[FIRSTSOCKET], sa, &sslen)) {
|
2009-06-29 16:46:01 -04:00
|
|
|
failf(data, "getsockname() failed: %s",
|
2019-02-25 12:12:51 -05:00
|
|
|
Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2011-05-16 17:46:43 -04:00
|
|
|
Curl_closesocket(conn, portsock);
|
2009-06-29 16:46:01 -04:00
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
|
|
|
port = port_min;
|
2011-11-06 10:53:07 -05:00
|
|
|
possibly_non_local = FALSE; /* don't try this again */
|
2009-06-29 16:46:01 -04:00
|
|
|
continue;
|
2010-02-02 03:48:58 -05:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(error != EADDRINUSE && error != EACCES) {
|
2010-02-04 14:44:31 -05:00
|
|
|
failf(data, "bind(port=%hu) failed: %s", port,
|
2019-02-25 12:12:51 -05:00
|
|
|
Curl_strerror(error, buffer, sizeof(buffer)));
|
2011-05-16 17:46:43 -04:00
|
|
|
Curl_closesocket(conn, portsock);
|
2009-06-29 16:46:01 -04:00
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
2010-01-22 18:21:39 -05:00
|
|
|
}
|
|
|
|
else
|
2009-06-29 16:46:01 -04:00
|
|
|
break;
|
2006-09-08 18:17:39 -04:00
|
|
|
|
2009-06-29 16:46:01 -04:00
|
|
|
port++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* maybe all ports were in use already*/
|
2011-04-20 09:17:42 -04:00
|
|
|
if(port > port_max) {
|
2009-06-29 16:46:01 -04:00
|
|
|
failf(data, "bind() failed, we ran out of ports!");
|
2011-05-16 17:46:43 -04:00
|
|
|
Curl_closesocket(conn, portsock);
|
2009-06-29 16:46:01 -04:00
|
|
|
return CURLE_FTP_PORT_FAILED;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
|
2006-01-19 18:52:03 -05:00
|
|
|
/* get the name again after the bind() so that we can extract the
|
|
|
|
port number it uses now */
|
|
|
|
sslen = sizeof(ss);
|
2012-06-12 07:12:09 -04:00
|
|
|
if(getsockname(portsock, (struct sockaddr *)sa, &sslen)) {
|
2006-01-19 18:52:03 -05:00
|
|
|
failf(data, "getsockname() failed: %s",
|
2019-02-25 12:12:51 -05:00
|
|
|
Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2011-05-16 17:46:43 -04:00
|
|
|
Curl_closesocket(conn, portsock);
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
|
|
|
|
2006-01-19 18:52:03 -05:00
|
|
|
/* step 4, listen on the socket */
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(listen(portsock, 1)) {
|
2019-02-25 12:12:51 -05:00
|
|
|
failf(data, "socket failure: %s",
|
|
|
|
Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2011-05-16 17:46:43 -04:00
|
|
|
Curl_closesocket(conn, portsock);
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
|
|
|
|
2006-01-19 18:52:03 -05:00
|
|
|
/* step 5, send the proper FTP command */
|
|
|
|
|
|
|
|
/* get a plain printable version of the numerical address to work with
|
|
|
|
below */
|
|
|
|
Curl_printable_address(ai, myhost, sizeof(myhost));
|
|
|
|
|
2009-06-30 17:30:39 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
2005-02-09 08:06:40 -05:00
|
|
|
if(!conn->bits.ftp_use_eprt && conn->bits.ipv6)
|
|
|
|
/* EPRT is disabled but we are connected to a IPv6 host, so we ignore the
|
|
|
|
request and enable EPRT again! */
|
|
|
|
conn->bits.ftp_use_eprt = TRUE;
|
|
|
|
#endif
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(; fcmd != DONE; fcmd++) {
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
if(!conn->bits.ftp_use_eprt && (EPRT == fcmd))
|
|
|
|
/* if disabled, goto next */
|
|
|
|
continue;
|
|
|
|
|
2012-06-12 07:12:09 -04:00
|
|
|
if((PORT == fcmd) && sa->sa_family != AF_INET)
|
2014-12-27 06:09:01 -05:00
|
|
|
/* PORT is IPv4 only */
|
2009-06-29 16:46:01 -04:00
|
|
|
continue;
|
|
|
|
|
2014-12-27 06:09:01 -05:00
|
|
|
switch(sa->sa_family) {
|
2005-02-09 08:06:40 -05:00
|
|
|
case AF_INET:
|
2012-06-12 07:12:09 -04:00
|
|
|
port = ntohs(sa4->sin_port);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2009-06-30 17:30:39 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
2005-02-09 08:06:40 -05:00
|
|
|
case AF_INET6:
|
2012-06-12 07:12:09 -04:00
|
|
|
port = ntohs(sa6->sin6_port);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2009-06-30 17:30:39 -04:00
|
|
|
#endif
|
2005-02-09 08:06:40 -05:00
|
|
|
default:
|
2009-06-30 17:30:39 -04:00
|
|
|
continue; /* might as well skip this */
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(EPRT == fcmd) {
|
2006-01-19 18:52:03 -05:00
|
|
|
/*
|
|
|
|
* Two fine examples from RFC2428;
|
|
|
|
*
|
|
|
|
* EPRT |1|132.235.1.2|6275|
|
|
|
|
*
|
|
|
|
* EPRT |2|1080::8:800:200C:417A|5282|
|
|
|
|
*/
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2010-02-04 14:44:31 -05:00
|
|
|
result = Curl_pp_sendf(&ftpc->pp, "%s |%d|%s|%hu|", mode[fcmd],
|
2012-06-12 07:12:09 -04:00
|
|
|
sa->sa_family == AF_INET?1:2,
|
2009-12-12 16:54:01 -05:00
|
|
|
myhost, port);
|
2011-09-12 09:16:07 -04:00
|
|
|
if(result) {
|
2011-09-14 04:36:29 -04:00
|
|
|
failf(data, "Failure sending EPRT command: %s",
|
|
|
|
curl_easy_strerror(result));
|
2011-09-12 09:16:07 -04:00
|
|
|
Curl_closesocket(conn, portsock);
|
|
|
|
/* don't retry using PORT */
|
|
|
|
ftpc->count1 = PORT;
|
|
|
|
/* bail out */
|
|
|
|
state(conn, FTP_STOP);
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
2011-09-12 09:16:07 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(PORT == fcmd) {
|
2020-02-04 17:27:39 -05:00
|
|
|
/* large enough for [IP address],[num],[num] */
|
|
|
|
char target[sizeof(myhost) + 20];
|
2006-01-19 18:52:03 -05:00
|
|
|
char *source = myhost;
|
2020-02-04 17:27:39 -05:00
|
|
|
char *dest = target;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2006-01-19 18:52:03 -05:00
|
|
|
/* translate x.x.x.x to x,x,x,x */
|
|
|
|
while(source && *source) {
|
|
|
|
if(*source == '.')
|
2017-09-09 17:09:06 -04:00
|
|
|
*dest = ',';
|
2005-02-09 08:06:40 -05:00
|
|
|
else
|
2006-01-19 18:52:03 -05:00
|
|
|
*dest = *source;
|
|
|
|
dest++;
|
|
|
|
source++;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2006-01-19 18:52:03 -05:00
|
|
|
*dest = 0;
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(dest, 20, ",%d,%d", (int)(port>>8), (int)(port&0xff));
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2020-02-04 17:27:39 -05:00
|
|
|
result = Curl_pp_sendf(&ftpc->pp, "%s %s", mode[fcmd], target);
|
2011-09-12 09:16:07 -04:00
|
|
|
if(result) {
|
2011-09-14 04:36:29 -04:00
|
|
|
failf(data, "Failure sending PORT command: %s",
|
|
|
|
curl_easy_strerror(result));
|
2011-09-12 09:16:07 -04:00
|
|
|
Curl_closesocket(conn, portsock);
|
|
|
|
/* bail out */
|
|
|
|
state(conn, FTP_STOP);
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
2011-09-12 09:16:07 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store which command was sent */
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->count1 = fcmd;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2016-03-07 18:07:14 -05:00
|
|
|
close_secondarysocket(conn);
|
|
|
|
|
2011-09-09 23:05:27 -04:00
|
|
|
/* we set the secondary socket variable to this for now, it is only so that
|
|
|
|
the cleanup function will close it in case we fail before the true
|
|
|
|
secondary stuff is made */
|
|
|
|
conn->sock[SECONDARYSOCKET] = portsock;
|
|
|
|
|
2006-12-05 08:49:29 -05:00
|
|
|
/* this tcpconnect assignment below is a hackish work-around to make the
|
|
|
|
multi interface with active FTP work - as it will not wait for a
|
|
|
|
(passive) connect in Curl_is_connected().
|
|
|
|
|
|
|
|
The *proper* fix is to make sure that the active connection from the
|
|
|
|
server is done in a non-blocking way. Currently, it is still BLOCKING.
|
|
|
|
*/
|
2011-08-18 17:35:15 -04:00
|
|
|
conn->bits.tcpconnect[SECONDARYSOCKET] = TRUE;
|
2006-12-05 08:49:29 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_PORT);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode ftp_state_use_pasv(struct connectdata *conn)
|
|
|
|
{
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
/*
|
2020-05-25 15:44:04 -04:00
|
|
|
Here's the executive summary on what to do:
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
PASV is RFC959, expect:
|
|
|
|
227 Entering Passive Mode (a1,a2,a3,a4,p1,p2)
|
|
|
|
|
|
|
|
LPSV is RFC1639, expect:
|
|
|
|
228 Entering Long Passive Mode (4,4,a1,a2,a3,a4,2,p1,p2)
|
|
|
|
|
|
|
|
EPSV is RFC2428, expect:
|
|
|
|
229 Entering Extended Passive Mode (|||port|)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-10-22 01:46:29 -04:00
|
|
|
static const char mode[][5] = { "EPSV", "PASV" };
|
2005-02-09 08:06:40 -05:00
|
|
|
int modeoff;
|
|
|
|
|
|
|
|
#ifdef PF_INET6
|
|
|
|
if(!conn->bits.ftp_use_epsv && conn->bits.ipv6)
|
|
|
|
/* EPSV is disabled but we are connected to a IPv6 host, so we ignore the
|
|
|
|
request and enable EPSV again! */
|
|
|
|
conn->bits.ftp_use_epsv = TRUE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
modeoff = conn->bits.ftp_use_epsv?0:1;
|
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "%s", mode[modeoff]);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->count1 = modeoff;
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_PASV);
|
|
|
|
infof(conn->data, "Connect data stream passively\n");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
/*
|
|
|
|
* ftp_state_prepare_transfer() starts PORT, PASV or PRET etc.
|
|
|
|
*
|
|
|
|
* REST is the last command in the chain of commands when a "head"-like
|
|
|
|
* request is made. Thus, if an actual transfer is to be made this is where we
|
|
|
|
* take off for real.
|
|
|
|
*/
|
|
|
|
static CURLcode ftp_state_prepare_transfer(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2007-09-26 08:00:01 -04:00
|
|
|
if(ftp->transfer != FTPTRANSFER_BODY) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* doesn't transfer any data */
|
2006-08-08 18:56:46 -04:00
|
|
|
|
|
|
|
/* still possibly do PRE QUOTE jobs */
|
|
|
|
state(conn, FTP_RETR_PREQUOTE);
|
|
|
|
result = ftp_state_quote(conn, TRUE, FTP_RETR_PREQUOTE);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
else if(data->set.ftp_use_port) {
|
|
|
|
/* We have chosen to use the PORT (or similar) command */
|
|
|
|
result = ftp_state_use_port(conn, EPRT);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* We have chosen (this is default) to use the PASV (or similar) command */
|
2010-01-01 09:44:44 -05:00
|
|
|
if(data->set.ftp_use_pret) {
|
|
|
|
/* The user has requested that we send a PRET command
|
|
|
|
to prepare the server for the upcoming PASV */
|
|
|
|
if(!conn->proto.ftpc.file) {
|
2010-01-22 18:21:39 -05:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "PRET %s",
|
|
|
|
data->set.str[STRING_CUSTOMREQUEST]?
|
|
|
|
data->set.str[STRING_CUSTOMREQUEST]:
|
|
|
|
(data->set.ftp_list_only?"NLST":"LIST"));
|
2010-01-01 09:44:44 -05:00
|
|
|
}
|
|
|
|
else if(data->set.upload) {
|
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "PRET STOR %s", conn->proto.ftpc.file);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "PRET RETR %s", conn->proto.ftpc.file);
|
|
|
|
}
|
|
|
|
state(conn, FTP_PRET);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = ftp_state_use_pasv(conn);
|
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_rest(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2007-10-22 11:05:35 -04:00
|
|
|
if((ftp->transfer != FTPTRANSFER_BODY) && ftpc->file) {
|
2007-08-17 18:22:43 -04:00
|
|
|
/* if a "head"-like request is being made (on a file) */
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
/* Determine if server can respond to REST command and therefore
|
|
|
|
whether it supports range */
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "REST %d", 0);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
state(conn, FTP_REST);
|
|
|
|
}
|
|
|
|
else
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_prepare_transfer(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_size(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2007-10-22 11:05:35 -04:00
|
|
|
if((ftp->transfer == FTPTRANSFER_INFO) && ftpc->file) {
|
2007-08-17 18:22:43 -04:00
|
|
|
/* if a "head"-like request is being made (on a file) */
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2007-10-22 11:05:35 -04:00
|
|
|
/* we know ftpc->file is a valid pointer to a file name */
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "SIZE %s", ftpc->file);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
state(conn, FTP_SIZE);
|
|
|
|
}
|
|
|
|
else
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_rest(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_list(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2018-09-14 17:33:28 -04:00
|
|
|
struct FTP *ftp = data->req.protop;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
/* If this output is to be machine-parsed, the NLST command might be better
|
|
|
|
to use, since the LIST command output is not specified or standard in any
|
|
|
|
way. It has turned out that the NLST list output is not the same on all
|
|
|
|
servers either... */
|
|
|
|
|
2007-09-24 17:47:35 -04:00
|
|
|
/*
|
2019-09-19 08:59:13 -04:00
|
|
|
if FTPFILE_NOCWD was specified, we should add the path
|
2007-09-24 17:47:35 -04:00
|
|
|
as argument for the LIST / NLST / or custom command.
|
|
|
|
Whether the server will support this, is uncertain.
|
|
|
|
|
|
|
|
The other ftp_filemethods will CWD into dir/dir/ first and
|
|
|
|
then just do LIST (in that case: nothing to do here)
|
|
|
|
*/
|
2019-09-25 11:48:53 -04:00
|
|
|
char *lstArg = NULL;
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
if((data->set.ftp_filemethod == FTPFILE_NOCWD) && ftp->path) {
|
|
|
|
/* url-decode before evaluation: e.g. paths starting/ending with %2f */
|
|
|
|
const char *slashPos = NULL;
|
|
|
|
char *rawPath = NULL;
|
|
|
|
result = Curl_urldecode(data, ftp->path, 0, &rawPath, NULL, TRUE);
|
2017-10-10 06:02:11 -04:00
|
|
|
if(result)
|
|
|
|
return result;
|
2019-09-25 11:48:53 -04:00
|
|
|
|
|
|
|
slashPos = strrchr(rawPath, '/');
|
|
|
|
if(slashPos) {
|
|
|
|
/* chop off the file part if format is dir/file otherwise remove
|
|
|
|
the trailing slash for dir/dir/ except for absolute path / */
|
|
|
|
size_t n = slashPos - rawPath;
|
|
|
|
if(n == 0)
|
|
|
|
++n;
|
|
|
|
|
|
|
|
lstArg = rawPath;
|
|
|
|
lstArg[n] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
free(rawPath);
|
2007-09-24 17:47:35 -04:00
|
|
|
}
|
|
|
|
|
2016-04-03 14:28:34 -04:00
|
|
|
cmd = aprintf("%s%s%s",
|
|
|
|
data->set.str[STRING_CUSTOMREQUEST]?
|
|
|
|
data->set.str[STRING_CUSTOMREQUEST]:
|
|
|
|
(data->set.ftp_list_only?"NLST":"LIST"),
|
|
|
|
lstArg? " ": "",
|
|
|
|
lstArg? lstArg: "");
|
2019-09-25 11:48:53 -04:00
|
|
|
free(lstArg);
|
2007-09-24 17:47:35 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
if(!cmd)
|
2007-09-24 17:47:35 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2011-08-30 19:58:06 -04:00
|
|
|
result = Curl_pp_sendf(&conn->proto.ftpc.pp, "%s", cmd);
|
2007-09-24 17:47:35 -04:00
|
|
|
free(cmd);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result)
|
2011-08-30 19:58:06 -04:00
|
|
|
return result;
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_LIST);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_retr_prequote(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
/* We've sent the TYPE, now we must send the list of prequote strings */
|
2019-05-25 04:06:08 -04:00
|
|
|
return ftp_state_quote(conn, TRUE, FTP_RETR_PREQUOTE);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_stor_prequote(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
/* We've sent the TYPE, now we must send the list of prequote strings */
|
2019-05-25 04:06:08 -04:00
|
|
|
return ftp_state_quote(conn, TRUE, FTP_STOR_PREQUOTE);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_type(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
/* If we have selected NOBODY and HEADER, it means that we only want file
|
|
|
|
information. Which in FTP can't be much more than the file size and
|
|
|
|
date. */
|
2008-01-31 07:04:33 -05:00
|
|
|
if(data->set.opt_no_body && ftpc->file &&
|
2006-08-19 17:18:36 -04:00
|
|
|
ftp_need_type(conn, data->set.prefer_ascii)) {
|
2018-02-23 18:29:01 -05:00
|
|
|
/* The SIZE command is _not_ RFC 959 specified, and therefore many servers
|
2005-02-09 08:06:40 -05:00
|
|
|
may not support it! It is however the only way we have to get a file's
|
|
|
|
size! */
|
|
|
|
|
2007-09-26 08:00:01 -04:00
|
|
|
ftp->transfer = FTPTRANSFER_INFO;
|
|
|
|
/* this means no actual transfer will be made */
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
/* Some servers return different sizes for different modes, and thus we
|
|
|
|
must set the proper type before we check the size */
|
2006-08-19 17:18:36 -04:00
|
|
|
result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_TYPE);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(result)
|
2006-08-19 17:18:36 -04:00
|
|
|
return result;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
else
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_size(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is called after the CWD commands have been done in the beginning of
|
|
|
|
the DO phase */
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_mdtm(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
/* Requested time of file or time-depended transfer? */
|
2007-10-22 11:05:35 -04:00
|
|
|
if((data->set.get_filetime || data->set.timecondition) && ftpc->file) {
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
/* we have requested to get the modified-time of the file, this is a white
|
|
|
|
spot as the MDTM is not mentioned in RFC959 */
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "MDTM %s", ftpc->file);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
state(conn, FTP_MDTM);
|
|
|
|
}
|
|
|
|
else
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_type(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This is called after the TYPE and possible quote commands have been sent */
|
2005-03-04 18:52:06 -05:00
|
|
|
static CURLcode ftp_state_ul_setup(struct connectdata *conn,
|
|
|
|
bool sizechecked)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2007-11-24 18:16:55 -05:00
|
|
|
if((data->state.resume_from && !sizechecked) ||
|
|
|
|
((data->state.resume_from > 0) && sizechecked)) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* we're about to continue the uploading of a file */
|
|
|
|
/* 1. get already existing file's size. We use the SIZE command for this
|
|
|
|
which may not exist in the server! The SIZE command is not in
|
|
|
|
RFC959. */
|
|
|
|
|
|
|
|
/* 2. This used to set REST. But since we can do append, we
|
|
|
|
don't another ftp command. We just skip the source file
|
|
|
|
offset and then we APPEND the rest on the file instead */
|
|
|
|
|
|
|
|
/* 3. pass file-size number of bytes in the source file */
|
|
|
|
/* 4. lower the infilesize counter */
|
|
|
|
/* => transfer as usual */
|
2018-06-02 16:52:56 -04:00
|
|
|
int seekerr = CURL_SEEKFUNC_OK;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2016-04-03 14:28:34 -04:00
|
|
|
if(data->state.resume_from < 0) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* Got no given size to start from, figure it out */
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "SIZE %s", ftpc->file);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_STOR_SIZE);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable append */
|
|
|
|
data->set.ftp_append = TRUE;
|
|
|
|
|
2008-01-10 05:30:19 -05:00
|
|
|
/* Let's read off the proper amount of bytes from the input. */
|
|
|
|
if(conn->seek_func) {
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(data, true);
|
2009-04-28 07:19:10 -04:00
|
|
|
seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
|
|
|
|
SEEK_SET);
|
2018-03-14 06:02:05 -04:00
|
|
|
Curl_set_in_callback(data, false);
|
2009-04-28 07:19:10 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2009-04-28 07:19:10 -04:00
|
|
|
if(seekerr != CURL_SEEKFUNC_OK) {
|
2017-09-09 17:09:06 -04:00
|
|
|
curl_off_t passed = 0;
|
2009-04-28 07:19:10 -04:00
|
|
|
if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
|
2008-01-10 05:30:19 -05:00
|
|
|
failf(data, "Could not seek stream");
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_FTP_COULDNT_USE_REST;
|
|
|
|
}
|
2009-04-28 07:19:10 -04:00
|
|
|
/* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
|
2017-03-10 08:28:37 -05:00
|
|
|
do {
|
|
|
|
size_t readthisamountnow =
|
2017-04-25 08:38:34 -04:00
|
|
|
(data->state.resume_from - passed > data->set.buffer_size) ?
|
|
|
|
(size_t)data->set.buffer_size :
|
|
|
|
curlx_sotouz(data->state.resume_from - passed);
|
2017-03-10 08:28:37 -05:00
|
|
|
|
|
|
|
size_t actuallyread =
|
|
|
|
data->state.fread_func(data->state.buffer, 1, readthisamountnow,
|
|
|
|
data->state.in);
|
|
|
|
|
|
|
|
passed += actuallyread;
|
|
|
|
if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
|
|
|
|
/* this checks for greater-than only to make sure that the
|
|
|
|
CURL_READFUNC_ABORT return code still aborts */
|
|
|
|
failf(data, "Failed to read data");
|
|
|
|
return CURLE_FTP_COULDNT_USE_REST;
|
|
|
|
}
|
|
|
|
} while(passed < data->state.resume_from);
|
2008-01-10 05:30:19 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
/* now, decrease the size of the read */
|
2014-04-15 07:49:18 -04:00
|
|
|
if(data->state.infilesize>0) {
|
|
|
|
data->state.infilesize -= data->state.resume_from;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2014-04-15 07:49:18 -04:00
|
|
|
if(data->state.infilesize <= 0) {
|
2005-02-09 08:06:40 -05:00
|
|
|
infof(data, "File already completely uploaded\n");
|
|
|
|
|
|
|
|
/* no data to transfer */
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2007-09-26 08:00:01 -04:00
|
|
|
/* Set ->transfer so that we won't get any error in
|
2007-12-08 17:50:55 -05:00
|
|
|
* ftp_done() because we didn't transfer anything! */
|
2007-09-26 08:00:01 -04:00
|
|
|
ftp->transfer = FTPTRANSFER_NONE;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
state(conn, FTP_STOP);
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2003-12-01 05:14:39 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
/* we've passed, proceed as normal */
|
|
|
|
} /* resume_from */
|
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, data->set.ftp_append?"APPE %s":"STOR %s",
|
|
|
|
ftpc->file);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
state(conn, FTP_STOR);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode ftp_state_quote(struct connectdata *conn,
|
|
|
|
bool init,
|
|
|
|
ftpstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = data->req.protop;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2017-09-09 17:09:06 -04:00
|
|
|
bool quote = FALSE;
|
2005-02-09 08:06:40 -05:00
|
|
|
struct curl_slist *item;
|
|
|
|
|
|
|
|
switch(instate) {
|
|
|
|
case FTP_QUOTE:
|
|
|
|
default:
|
|
|
|
item = data->set.quote;
|
|
|
|
break;
|
|
|
|
case FTP_RETR_PREQUOTE:
|
|
|
|
case FTP_STOR_PREQUOTE:
|
|
|
|
item = data->set.prequote;
|
|
|
|
break;
|
|
|
|
case FTP_POSTQUOTE:
|
|
|
|
item = data->set.postquote;
|
|
|
|
break;
|
2003-12-01 05:14:39 -05:00
|
|
|
}
|
|
|
|
|
2010-04-09 10:54:52 -04:00
|
|
|
/*
|
|
|
|
* This state uses:
|
|
|
|
* 'count1' to iterate over the commands to send
|
2017-03-26 11:02:22 -04:00
|
|
|
* 'count2' to store whether to allow commands to fail
|
2010-04-09 10:54:52 -04:00
|
|
|
*/
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(init)
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->count1 = 0;
|
2005-02-09 08:06:40 -05:00
|
|
|
else
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->count1++;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
if(item) {
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* Skip count1 items in the linked list */
|
2006-09-07 17:49:20 -04:00
|
|
|
while((i< ftpc->count1) && item) {
|
2005-02-09 08:06:40 -05:00
|
|
|
item = item->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if(item) {
|
2010-04-09 10:54:52 -04:00
|
|
|
char *cmd = item->data;
|
|
|
|
if(cmd[0] == '*') {
|
|
|
|
cmd++;
|
|
|
|
ftpc->count2 = 1; /* the sent command is allowed to fail */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ftpc->count2 = 0; /* failure means cancel operation */
|
|
|
|
|
|
|
|
PPSENDF(&ftpc->pp, "%s", cmd);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, instate);
|
|
|
|
quote = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!quote) {
|
|
|
|
/* No more quote to send, continue to ... */
|
|
|
|
switch(instate) {
|
|
|
|
case FTP_QUOTE:
|
|
|
|
default:
|
|
|
|
result = ftp_state_cwd(conn);
|
|
|
|
break;
|
|
|
|
case FTP_RETR_PREQUOTE:
|
2007-11-05 04:45:09 -05:00
|
|
|
if(ftp->transfer != FTPTRANSFER_BODY)
|
2006-08-08 18:56:46 -04:00
|
|
|
state(conn, FTP_STOP);
|
|
|
|
else {
|
2010-05-12 09:33:22 -04:00
|
|
|
if(ftpc->known_filesize != -1) {
|
|
|
|
Curl_pgrsSetDownloadSize(data, ftpc->known_filesize);
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_retr(conn, ftpc->known_filesize);
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
else {
|
2015-10-23 08:57:30 -04:00
|
|
|
if(data->set.ignorecl) {
|
|
|
|
/* This code is to support download of growing files. It prevents
|
|
|
|
the state machine from requesting the file size from the
|
|
|
|
server. With an unknown file size the download continues until
|
|
|
|
the server terminates it, otherwise the client stops if the
|
|
|
|
received byte count exceeds the reported file size. Set option
|
|
|
|
CURLOPT_IGNORE_CONTENT_LENGTH to 1 to enable this behavior.*/
|
|
|
|
PPSENDF(&ftpc->pp, "RETR %s", ftpc->file);
|
|
|
|
state(conn, FTP_RETR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PPSENDF(&ftpc->pp, "SIZE %s", ftpc->file);
|
|
|
|
state(conn, FTP_RETR_SIZE);
|
|
|
|
}
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
2006-08-08 18:56:46 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
case FTP_STOR_PREQUOTE:
|
2005-03-04 18:52:06 -05:00
|
|
|
result = ftp_state_ul_setup(conn, FALSE);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
case FTP_POSTQUOTE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-11-12 07:06:31 -05:00
|
|
|
/* called from ftp_state_pasv_resp to switch to PASV in case of EPSV
|
|
|
|
problems */
|
|
|
|
static CURLcode ftp_epsv_disable(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2015-01-08 14:43:32 -05:00
|
|
|
|
2018-03-28 10:42:33 -04:00
|
|
|
if(conn->bits.ipv6 && !(conn->bits.tunnel_proxy || conn->bits.socksproxy)) {
|
2015-01-08 14:43:32 -05:00
|
|
|
/* We can't disable EPSV when doing IPv6, so this is instead a fail */
|
|
|
|
failf(conn->data, "Failed EPSV attempt, exiting\n");
|
2016-08-22 15:37:03 -04:00
|
|
|
return CURLE_WEIRD_SERVER_REPLY;
|
2015-01-08 14:43:32 -05:00
|
|
|
}
|
|
|
|
|
2013-08-05 17:49:53 -04:00
|
|
|
infof(conn->data, "Failed EPSV attempt. Disabling EPSV\n");
|
2012-11-12 07:06:31 -05:00
|
|
|
/* disable it for next transfer */
|
|
|
|
conn->bits.ftp_use_epsv = FALSE;
|
|
|
|
conn->data->state.errorbuf = FALSE; /* allow error message to get
|
|
|
|
rewritten */
|
2013-07-24 11:06:28 -04:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "%s", "PASV");
|
2012-11-12 07:06:31 -05:00
|
|
|
conn->proto.ftpc.count1++;
|
2013-08-05 17:49:53 -04:00
|
|
|
/* remain in/go to the FTP_PASV state */
|
|
|
|
state(conn, FTP_PASV);
|
2012-11-12 07:06:31 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-26 14:19:27 -04:00
|
|
|
|
2015-05-08 07:55:15 -04:00
|
|
|
static char *control_address(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
/* Returns the control connection IP address.
|
|
|
|
If a proxy tunnel is used, returns the original host name instead, because
|
|
|
|
the effective control connection address is the proxy address,
|
|
|
|
not the ftp host. */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(conn->bits.tunnel_proxy || conn->bits.socksproxy)
|
2015-05-08 07:55:15 -04:00
|
|
|
return conn->host.name;
|
|
|
|
|
|
|
|
return conn->ip_addr_str;
|
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_pasv_resp(struct connectdata *conn,
|
|
|
|
int ftpcode)
|
|
|
|
{
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
|
|
|
struct Curl_dns_entry *addr = NULL;
|
2020-02-14 10:16:54 -05:00
|
|
|
enum resolve_t rc;
|
2005-02-09 08:06:40 -05:00
|
|
|
unsigned short connectport; /* the local port connect() should use! */
|
2017-09-09 17:09:06 -04:00
|
|
|
char *str = &data->state.buffer[4]; /* start on the first letter */
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2015-01-08 12:30:01 -05:00
|
|
|
/* if we come here again, make sure the former name is cleared */
|
|
|
|
Curl_safefree(ftpc->newhost);
|
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
if((ftpc->count1 == 0) &&
|
2005-02-09 08:06:40 -05:00
|
|
|
(ftpcode == 229)) {
|
|
|
|
/* positive EPSV response */
|
|
|
|
char *ptr = strchr(str, '(');
|
|
|
|
if(ptr) {
|
|
|
|
unsigned int num;
|
|
|
|
char separator[4];
|
|
|
|
ptr++;
|
2015-05-08 07:55:15 -04:00
|
|
|
if(5 == sscanf(ptr, "%c%c%c%u%c",
|
|
|
|
&separator[0],
|
|
|
|
&separator[1],
|
|
|
|
&separator[2],
|
|
|
|
&num,
|
|
|
|
&separator[3])) {
|
2005-03-15 16:00:46 -05:00
|
|
|
const char sep1 = separator[0];
|
2005-02-09 08:06:40 -05:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* The four separators should be identical, or else this is an oddly
|
|
|
|
formatted reply and we bail out immediately. */
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 1; i<4; i++) {
|
2005-02-09 08:06:40 -05:00
|
|
|
if(separator[i] != sep1) {
|
2017-09-09 17:09:06 -04:00
|
|
|
ptr = NULL; /* set to NULL to signal error */
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 16:35:48 -05:00
|
|
|
if(num > 0xffff) {
|
|
|
|
failf(data, "Illegal port number in EPSV reply");
|
|
|
|
return CURLE_FTP_WEIRD_PASV_REPLY;
|
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
if(ptr) {
|
2013-10-26 14:19:27 -04:00
|
|
|
ftpc->newport = (unsigned short)(num & 0xffff);
|
2015-05-08 07:55:15 -04:00
|
|
|
ftpc->newhost = strdup(control_address(conn));
|
2015-01-08 12:30:01 -05:00
|
|
|
if(!ftpc->newhost)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-09-09 17:09:06 -04:00
|
|
|
ptr = NULL;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
if(!ptr) {
|
|
|
|
failf(data, "Weirdly formatted EPSV reply");
|
|
|
|
return CURLE_FTP_WEIRD_PASV_REPLY;
|
|
|
|
}
|
|
|
|
}
|
2006-09-07 17:49:20 -04:00
|
|
|
else if((ftpc->count1 == 1) &&
|
2005-02-09 08:06:40 -05:00
|
|
|
(ftpcode == 227)) {
|
|
|
|
/* positive PASV response */
|
2017-10-19 08:41:14 -04:00
|
|
|
unsigned int ip[4];
|
|
|
|
unsigned int port[2];
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan for a sequence of six comma-separated numbers and use them as
|
|
|
|
* IP+port indicators.
|
|
|
|
*
|
|
|
|
* Found reply-strings include:
|
|
|
|
* "227 Entering Passive Mode (127,0,0,1,4,51)"
|
|
|
|
* "227 Data transfer will passively listen to 127,0,0,1,4,51"
|
|
|
|
* "227 Entering passive mode. 127,0,0,1,4,51"
|
|
|
|
*/
|
|
|
|
while(*str) {
|
2017-10-19 08:41:14 -04:00
|
|
|
if(6 == sscanf(str, "%u,%u,%u,%u,%u,%u",
|
2015-05-08 07:55:15 -04:00
|
|
|
&ip[0], &ip[1], &ip[2], &ip[3],
|
|
|
|
&port[0], &port[1]))
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
2017-10-19 08:41:14 -04:00
|
|
|
if(!*str || (ip[0] > 255) || (ip[1] > 255) || (ip[2] > 255) ||
|
|
|
|
(ip[3] > 255) || (port[0] > 255) || (port[1] > 255) ) {
|
2005-02-09 08:06:40 -05:00
|
|
|
failf(data, "Couldn't interpret the 227-response");
|
|
|
|
return CURLE_FTP_WEIRD_227_FORMAT;
|
|
|
|
}
|
|
|
|
|
2005-09-04 01:16:06 -04:00
|
|
|
/* we got OK from server */
|
|
|
|
if(data->set.ftp_skip_ip) {
|
2015-01-08 12:30:01 -05:00
|
|
|
/* told to ignore the remotely given IP but instead use the host we used
|
2005-09-04 01:16:06 -04:00
|
|
|
for the control connection */
|
2018-05-06 14:32:24 -04:00
|
|
|
infof(data, "Skip %u.%u.%u.%u for data connection, re-use %s instead\n",
|
2005-09-04 01:16:06 -04:00
|
|
|
ip[0], ip[1], ip[2], ip[3],
|
2015-01-08 12:30:01 -05:00
|
|
|
conn->host.name);
|
2015-05-08 07:55:15 -04:00
|
|
|
ftpc->newhost = strdup(control_address(conn));
|
2005-09-04 01:16:06 -04:00
|
|
|
}
|
|
|
|
else
|
2018-05-06 14:32:24 -04:00
|
|
|
ftpc->newhost = aprintf("%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
|
2015-01-08 12:30:01 -05:00
|
|
|
|
|
|
|
if(!ftpc->newhost)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2013-10-26 14:19:27 -04:00
|
|
|
ftpc->newport = (unsigned short)(((port[0]<<8) + port[1]) & 0xffff);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2006-09-07 17:49:20 -04:00
|
|
|
else if(ftpc->count1 == 0) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* EPSV failed, move on to PASV */
|
2013-08-05 17:49:53 -04:00
|
|
|
return ftp_epsv_disable(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
failf(data, "Bad PASV/EPSV response: %03d", ftpcode);
|
|
|
|
return CURLE_FTP_WEIRD_PASV_REPLY;
|
|
|
|
}
|
|
|
|
|
2013-04-26 10:45:31 -04:00
|
|
|
if(conn->bits.proxy) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/*
|
2013-04-26 10:45:31 -04:00
|
|
|
* This connection uses a proxy and we need to connect to the proxy again
|
|
|
|
* here. We don't want to rely on a former host lookup that might've
|
|
|
|
* expired now, instead we remake the lookup here and now!
|
2005-02-09 08:06:40 -05:00
|
|
|
*/
|
2016-11-16 12:49:15 -05:00
|
|
|
const char * const host_name = conn->bits.socksproxy ?
|
|
|
|
conn->socks_proxy.host.name : conn->http_proxy.host.name;
|
2019-05-09 04:58:04 -04:00
|
|
|
rc = Curl_resolv(conn, host_name, (int)conn->port, FALSE, &addr);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(rc == CURLRESOLV_PENDING)
|
2010-04-16 17:02:15 -04:00
|
|
|
/* BLOCKING, ignores the return code but 'addr' will be NULL in
|
|
|
|
case of failure */
|
2011-01-30 18:10:35 -05:00
|
|
|
(void)Curl_resolver_wait_resolv(conn, &addr);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
connectport =
|
|
|
|
(unsigned short)conn->port; /* we connect to the proxy's port */
|
|
|
|
|
2008-09-02 19:12:00 -04:00
|
|
|
if(!addr) {
|
2016-11-16 12:49:15 -05:00
|
|
|
failf(data, "Can't resolve proxy host %s:%hu", host_name, connectport);
|
2016-12-25 04:35:34 -05:00
|
|
|
return CURLE_COULDNT_RESOLVE_PROXY;
|
2008-09-02 19:12:00 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* normal, direct, ftp connection */
|
2019-05-09 04:58:04 -04:00
|
|
|
rc = Curl_resolv(conn, ftpc->newhost, ftpc->newport, FALSE, &addr);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(rc == CURLRESOLV_PENDING)
|
|
|
|
/* BLOCKING */
|
2011-01-30 18:10:35 -05:00
|
|
|
(void)Curl_resolver_wait_resolv(conn, &addr);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2013-10-26 14:19:27 -04:00
|
|
|
connectport = ftpc->newport; /* we connect to the remote port */
|
2008-09-02 19:12:00 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(!addr) {
|
2013-10-26 14:19:27 -04:00
|
|
|
failf(data, "Can't resolve new host %s:%hu", ftpc->newhost, connectport);
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_FTP_CANT_GET_HOST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 06:51:25 -04:00
|
|
|
conn->bits.tcpconnect[SECONDARYSOCKET] = FALSE;
|
|
|
|
result = Curl_connecthost(conn, addr);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2012-11-12 07:06:31 -05:00
|
|
|
if(result) {
|
2015-05-08 09:26:59 -04:00
|
|
|
Curl_resolv_unlock(data, addr); /* we're done using this address */
|
2012-11-12 07:06:31 -05:00
|
|
|
if(ftpc->count1 == 0 && ftpcode == 229)
|
|
|
|
return ftp_epsv_disable(conn);
|
2006-01-24 09:40:43 -05:00
|
|
|
|
2002-12-03 05:25:31 -05:00
|
|
|
return result;
|
2012-11-12 07:06:31 -05:00
|
|
|
}
|
2001-03-02 02:44:05 -05:00
|
|
|
|
2003-10-24 08:58:59 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/*
|
|
|
|
* When this is used from the multi interface, this might've returned with
|
|
|
|
* the 'connected' set to FALSE and thus we are now awaiting a non-blocking
|
2013-10-26 14:19:27 -04:00
|
|
|
* connect to connect.
|
2005-02-09 08:06:40 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
if(data->set.verbose)
|
|
|
|
/* this just dumps information about this second connection */
|
2015-05-08 09:26:59 -04:00
|
|
|
ftp_pasv_verbose(conn, addr->addr, ftpc->newhost, connectport);
|
2006-01-12 07:40:04 -05:00
|
|
|
|
2017-02-25 16:55:46 -05:00
|
|
|
Curl_resolv_unlock(data, addr); /* we're done using this address */
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
Curl_safefree(conn->secondaryhostname);
|
|
|
|
conn->secondary_port = ftpc->newport;
|
2017-02-25 16:55:46 -05:00
|
|
|
conn->secondaryhostname = strdup(ftpc->newhost);
|
|
|
|
if(!conn->secondaryhostname)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2016-11-16 12:49:15 -05:00
|
|
|
|
2013-01-17 06:59:23 -05:00
|
|
|
conn->bits.do_more = TRUE;
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_STOP); /* this phase is completed */
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode ftp_state_port_resp(struct connectdata *conn,
|
|
|
|
int ftpcode)
|
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
|
|
|
ftpport fcmd = (ftpport)ftpc->count1;
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2015-02-11 13:51:57 -05:00
|
|
|
/* The FTP spec tells a positive response should have code 200.
|
|
|
|
Be more permissive here to tolerate deviant servers. */
|
|
|
|
if(ftpcode / 100 != 2) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* the command failed */
|
2001-03-02 02:44:05 -05:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(EPRT == fcmd) {
|
2005-02-09 08:06:40 -05:00
|
|
|
infof(data, "disabling EPRT usage\n");
|
|
|
|
conn->bits.ftp_use_eprt = FALSE;
|
2001-03-02 02:44:05 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
fcmd++;
|
2001-03-02 02:44:05 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(fcmd == DONE) {
|
|
|
|
failf(data, "Failed to do PORT");
|
|
|
|
result = CURLE_FTP_PORT_FAILED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* try next */
|
|
|
|
result = ftp_state_use_port(conn, fcmd);
|
2001-03-02 02:44:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2005-02-09 08:06:40 -05:00
|
|
|
infof(data, "Connect data stream actively\n");
|
|
|
|
state(conn, FTP_STOP); /* end of DO phase */
|
2013-01-17 06:59:23 -05:00
|
|
|
result = ftp_dophase_done(conn, FALSE);
|
2001-03-02 02:44:05 -05:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
2000-05-22 10:12:12 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_mdtm_resp(struct connectdata *conn,
|
|
|
|
int ftpcode)
|
2000-05-22 10:12:12 -04:00
|
|
|
{
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = data->req.protop;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2004-12-16 17:20:33 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
switch(ftpcode) {
|
|
|
|
case 213:
|
|
|
|
{
|
|
|
|
/* we got a time. Format should be: "YYYYMMDDHHMMSS[.sss]" where the
|
|
|
|
last .sss part is optional and means fractions of a second */
|
|
|
|
int year, month, day, hour, minute, second;
|
2017-04-24 10:05:46 -04:00
|
|
|
if(6 == sscanf(&data->state.buffer[4], "%04d%02d%02d%02d%02d%02d",
|
2005-02-09 08:06:40 -05:00
|
|
|
&year, &month, &day, &hour, &minute, &second)) {
|
|
|
|
/* we have a time, reformat it */
|
2017-04-24 10:05:46 -04:00
|
|
|
char timebuf[24];
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(timebuf, sizeof(timebuf),
|
|
|
|
"%04d%02d%02d %02d:%02d:%02d GMT",
|
|
|
|
year, month, day, hour, minute, second);
|
2005-02-09 08:06:40 -05:00
|
|
|
/* now, convert this into a time() value: */
|
2019-11-28 09:27:58 -05:00
|
|
|
data->info.filetime = Curl_getdate_capped(timebuf);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2003-08-05 09:04:10 -04:00
|
|
|
|
2007-08-17 18:22:43 -04:00
|
|
|
#ifdef CURL_FTP_HTTPSTYLE_HEAD
|
2005-02-09 08:06:40 -05:00
|
|
|
/* If we asked for a time of the file and we actually got one as well,
|
|
|
|
we "emulate" a HTTP-style header in our output. */
|
2004-04-22 09:09:00 -04:00
|
|
|
|
2008-01-31 07:04:33 -05:00
|
|
|
if(data->set.opt_no_body &&
|
2007-10-22 11:05:35 -04:00
|
|
|
ftpc->file &&
|
2005-03-08 03:09:14 -05:00
|
|
|
data->set.get_filetime &&
|
2017-09-09 17:09:06 -04:00
|
|
|
(data->info.filetime >= 0) ) {
|
2017-04-24 10:05:46 -04:00
|
|
|
char headerbuf[128];
|
2018-01-31 18:24:19 -05:00
|
|
|
time_t filetime = data->info.filetime;
|
2005-02-09 08:06:40 -05:00
|
|
|
struct tm buffer;
|
2011-02-07 09:00:48 -05:00
|
|
|
const struct tm *tm = &buffer;
|
|
|
|
|
|
|
|
result = Curl_gmtime(filetime, &buffer);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* format: "Tue, 15 Nov 1994 12:45:26" */
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(headerbuf, sizeof(headerbuf),
|
|
|
|
"Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
|
|
|
|
Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
|
|
|
|
tm->tm_mday,
|
|
|
|
Curl_month[tm->tm_mon],
|
|
|
|
tm->tm_year + 1900,
|
|
|
|
tm->tm_hour,
|
|
|
|
tm->tm_min,
|
|
|
|
tm->tm_sec);
|
2017-04-24 10:05:46 -04:00
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_BOTH, headerbuf, 0);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2005-03-08 03:09:14 -05:00
|
|
|
} /* end of a ridiculous amount of conditionals */
|
2007-08-17 18:22:43 -04:00
|
|
|
#endif
|
2000-05-22 10:12:12 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
infof(data, "unsupported MDTM reply format\n");
|
|
|
|
break;
|
|
|
|
case 550: /* "No such file or directory" */
|
|
|
|
failf(data, "Given file does not exist");
|
|
|
|
result = CURLE_FTP_COULDNT_RETR_FILE;
|
|
|
|
break;
|
2000-05-22 10:12:12 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
if(data->set.timecondition) {
|
|
|
|
if((data->info.filetime > 0) && (data->set.timevalue > 0)) {
|
|
|
|
switch(data->set.timecondition) {
|
|
|
|
case CURL_TIMECOND_IFMODSINCE:
|
|
|
|
default:
|
2005-10-05 02:09:20 -04:00
|
|
|
if(data->info.filetime <= data->set.timevalue) {
|
2005-02-09 08:06:40 -05:00
|
|
|
infof(data, "The requested document is not new enough\n");
|
2010-01-22 18:21:39 -05:00
|
|
|
ftp->transfer = FTPTRANSFER_NONE; /* mark to not transfer data */
|
2009-02-11 16:47:14 -05:00
|
|
|
data->info.timecond = TRUE;
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_STOP);
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CURL_TIMECOND_IFUNMODSINCE:
|
|
|
|
if(data->info.filetime > data->set.timevalue) {
|
|
|
|
infof(data, "The requested document is not old enough\n");
|
2010-01-22 18:21:39 -05:00
|
|
|
ftp->transfer = FTPTRANSFER_NONE; /* mark to not transfer data */
|
2009-02-11 16:47:14 -05:00
|
|
|
data->info.timecond = TRUE;
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_STOP);
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} /* switch */
|
2000-05-22 10:12:12 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else {
|
|
|
|
infof(data, "Skipping time comparison\n");
|
2000-05-22 10:12:12 -04:00
|
|
|
}
|
|
|
|
}
|
2004-05-12 08:06:39 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(!result)
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_type(conn);
|
2001-05-11 07:38:13 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2004-11-02 05:12:22 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_type_resp(struct connectdata *conn,
|
|
|
|
int ftpcode,
|
|
|
|
ftpstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2004-11-02 05:12:22 -05:00
|
|
|
|
2005-05-24 05:39:56 -04:00
|
|
|
if(ftpcode/100 != 2) {
|
|
|
|
/* "sasserftpd" and "(u)r(x)bot ftpd" both responds with 226 after a
|
|
|
|
successful 'TYPE I'. While that is not as RFC959 says, it is still a
|
|
|
|
positive response code and we allow that. */
|
2005-02-09 08:06:40 -05:00
|
|
|
failf(data, "Couldn't set desired mode");
|
2007-08-30 16:34:57 -04:00
|
|
|
return CURLE_FTP_COULDNT_SET_TYPE;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2005-05-24 05:39:56 -04:00
|
|
|
if(ftpcode != 200)
|
|
|
|
infof(data, "Got a %03d response code instead of the assumed 200\n",
|
|
|
|
ftpcode);
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(instate == FTP_TYPE)
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_size(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
else if(instate == FTP_LIST_TYPE)
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_list(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
else if(instate == FTP_RETR_TYPE)
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_retr_prequote(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
else if(instate == FTP_STOR_TYPE)
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_stor_prequote(conn);
|
2004-11-02 05:12:22 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2004-11-02 05:12:22 -05:00
|
|
|
|
2013-08-08 06:19:01 -04:00
|
|
|
static CURLcode ftp_state_retr(struct connectdata *conn,
|
2005-02-09 08:06:40 -05:00
|
|
|
curl_off_t filesize)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = data->req.protop;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2000-05-22 10:12:12 -04:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(data->set.max_filesize && (filesize > data->set.max_filesize)) {
|
2005-02-09 08:06:40 -05:00
|
|
|
failf(data, "Maximum file size exceeded");
|
|
|
|
return CURLE_FILESIZE_EXCEEDED;
|
|
|
|
}
|
|
|
|
ftp->downloadsize = filesize;
|
2002-12-04 03:56:55 -05:00
|
|
|
|
2007-11-24 18:16:55 -05:00
|
|
|
if(data->state.resume_from) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* We always (attempt to) get the size of downloads, so it is done before
|
|
|
|
this even when not doing resumes. */
|
|
|
|
if(filesize == -1) {
|
|
|
|
infof(data, "ftp server doesn't support SIZE\n");
|
|
|
|
/* We couldn't get the size and therefore we can't know if there really
|
|
|
|
is a part of the file left to get, although the server will just
|
|
|
|
close the connection when we start the connection so it won't cause
|
|
|
|
us any harm, just not make us exit as nicely. */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* We got a file size report, so we check that there actually is a
|
|
|
|
part of the file left to get, or else we go home. */
|
2007-11-24 18:16:55 -05:00
|
|
|
if(data->state.resume_from< 0) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* We're supposed to download the last abs(from) bytes */
|
2007-11-24 18:16:55 -05:00
|
|
|
if(filesize < -data->state.resume_from) {
|
2013-12-31 06:10:25 -05:00
|
|
|
failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T
|
|
|
|
") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")",
|
2007-11-24 18:16:55 -05:00
|
|
|
data->state.resume_from, filesize);
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
|
|
|
/* convert to size to download */
|
2007-11-24 18:16:55 -05:00
|
|
|
ftp->downloadsize = -data->state.resume_from;
|
2005-02-09 08:06:40 -05:00
|
|
|
/* download from where? */
|
2007-11-24 18:16:55 -05:00
|
|
|
data->state.resume_from = filesize - ftp->downloadsize;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
else {
|
2007-11-24 18:16:55 -05:00
|
|
|
if(filesize < data->state.resume_from) {
|
2013-12-31 06:10:25 -05:00
|
|
|
failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T
|
|
|
|
") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")",
|
2007-11-24 18:16:55 -05:00
|
|
|
data->state.resume_from, filesize);
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
|
|
|
/* Now store the number of bytes we are expected to download */
|
2007-11-24 18:16:55 -05:00
|
|
|
ftp->downloadsize = filesize-data->state.resume_from;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
}
|
2002-12-04 03:56:55 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(ftp->downloadsize == 0) {
|
|
|
|
/* no data to transfer */
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2005-02-09 08:06:40 -05:00
|
|
|
infof(data, "File already completely downloaded\n");
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2007-12-08 17:50:55 -05:00
|
|
|
/* Set ->transfer so that we won't get any error in ftp_done()
|
2005-02-09 08:06:40 -05:00
|
|
|
* because we didn't transfer the any file */
|
2007-09-26 08:00:01 -04:00
|
|
|
ftp->transfer = FTPTRANSFER_NONE;
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_STOP);
|
|
|
|
return CURLE_OK;
|
2002-12-04 03:56:55 -05:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* Set resume file transfer offset */
|
2013-12-31 06:10:25 -05:00
|
|
|
infof(data, "Instructs server to resume from offset %"
|
|
|
|
CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
|
2000-08-31 08:04:55 -04:00
|
|
|
|
2013-12-31 06:10:25 -05:00
|
|
|
PPSENDF(&ftpc->pp, "REST %" CURL_FORMAT_CURL_OFF_T,
|
|
|
|
data->state.resume_from);
|
2000-05-22 10:12:12 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_RETR_REST);
|
2004-06-03 07:41:05 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else {
|
|
|
|
/* no resume */
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "RETR %s", ftpc->file);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_RETR);
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2001-11-12 17:27:05 -05:00
|
|
|
return result;
|
2000-05-22 10:12:12 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_size_resp(struct connectdata *conn,
|
|
|
|
int ftpcode,
|
|
|
|
ftpstate instate)
|
2001-08-15 02:52:42 -04:00
|
|
|
{
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2017-08-14 17:33:23 -04:00
|
|
|
curl_off_t filesize = -1;
|
2005-02-09 08:06:40 -05:00
|
|
|
char *buf = data->state.buffer;
|
2001-08-15 02:52:42 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* get the size from the ascii string: */
|
2019-09-12 11:50:21 -04:00
|
|
|
if(ftpcode == 213) {
|
|
|
|
/* To allow servers to prepend "rubbish" in the response string, we scan
|
|
|
|
for all the digits at the end of the response and parse only those as a
|
|
|
|
number. */
|
|
|
|
char *start = &buf[4];
|
|
|
|
char *fdigit = strchr(start, '\r');
|
|
|
|
if(fdigit) {
|
|
|
|
do
|
|
|
|
fdigit--;
|
|
|
|
while(ISDIGIT(*fdigit) && (fdigit > start));
|
|
|
|
if(!ISDIGIT(*fdigit))
|
|
|
|
fdigit++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fdigit = start;
|
2017-08-14 17:33:23 -04:00
|
|
|
/* ignores parsing errors, which will make the size remain unknown */
|
2019-09-12 11:50:21 -04:00
|
|
|
(void)curlx_strtoofft(fdigit, NULL, 0, &filesize);
|
|
|
|
|
|
|
|
}
|
2001-08-15 02:52:42 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(instate == FTP_SIZE) {
|
2007-08-17 18:22:43 -04:00
|
|
|
#ifdef CURL_FTP_HTTPSTYLE_HEAD
|
2005-02-09 08:06:40 -05:00
|
|
|
if(-1 != filesize) {
|
2017-04-24 10:05:46 -04:00
|
|
|
char clbuf[128];
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(clbuf, sizeof(clbuf),
|
|
|
|
"Content-Length: %" CURL_FORMAT_CURL_OFF_T "\r\n", filesize);
|
2017-04-24 10:05:46 -04:00
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_BOTH, clbuf, 0);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
2002-12-03 05:25:31 -05:00
|
|
|
return result;
|
2001-08-15 02:52:42 -04:00
|
|
|
}
|
2007-08-17 18:22:43 -04:00
|
|
|
#endif
|
2007-08-20 17:54:00 -04:00
|
|
|
Curl_pgrsSetDownloadSize(data, filesize);
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_rest(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2007-08-20 17:54:00 -04:00
|
|
|
else if(instate == FTP_RETR_SIZE) {
|
|
|
|
Curl_pgrsSetDownloadSize(data, filesize);
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_retr(conn, filesize);
|
2007-08-20 17:54:00 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else if(instate == FTP_STOR_SIZE) {
|
2007-11-24 18:16:55 -05:00
|
|
|
data->state.resume_from = filesize;
|
2005-03-04 18:52:06 -05:00
|
|
|
result = ftp_state_ul_setup(conn, TRUE);
|
2001-08-15 02:52:42 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
2001-08-15 02:52:42 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_rest_resp(struct connectdata *conn,
|
|
|
|
int ftpcode,
|
|
|
|
ftpstate instate)
|
2001-08-23 02:10:01 -04:00
|
|
|
{
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
switch(instate) {
|
|
|
|
case FTP_REST:
|
|
|
|
default:
|
2007-08-17 18:22:43 -04:00
|
|
|
#ifdef CURL_FTP_HTTPSTYLE_HEAD
|
2007-11-05 04:45:09 -05:00
|
|
|
if(ftpcode == 350) {
|
2007-08-17 18:31:51 -04:00
|
|
|
char buffer[24]= { "Accept-ranges: bytes\r\n" };
|
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_BOTH, buffer, 0);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
2007-08-17 18:22:43 -04:00
|
|
|
#endif
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_prepare_transfer(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_RETR_REST:
|
2007-11-05 04:45:09 -05:00
|
|
|
if(ftpcode != 350) {
|
2005-02-09 08:06:40 -05:00
|
|
|
failf(conn->data, "Couldn't use REST");
|
|
|
|
result = CURLE_FTP_COULDNT_USE_REST;
|
|
|
|
}
|
|
|
|
else {
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "RETR %s", ftpc->file);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_RETR);
|
2001-08-23 02:10:01 -04:00
|
|
|
}
|
2003-01-07 06:25:44 -05:00
|
|
|
break;
|
2001-08-23 02:10:01 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
return result;
|
2001-08-23 02:10:01 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_state_stor_resp(struct connectdata *conn,
|
2011-12-19 08:35:20 -05:00
|
|
|
int ftpcode, ftpstate instate)
|
2001-08-23 02:10:01 -04:00
|
|
|
{
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
if(ftpcode >= 400) {
|
2005-02-09 08:06:40 -05:00
|
|
|
failf(data, "Failed FTP upload: %0d", ftpcode);
|
2012-11-04 12:22:48 -05:00
|
|
|
state(conn, FTP_STOP);
|
2005-02-09 08:06:40 -05:00
|
|
|
/* oops, we never close the sockets! */
|
2007-07-13 16:04:53 -04:00
|
|
|
return CURLE_UPLOAD_FAILED;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
conn->proto.ftpc.state_saved = instate;
|
|
|
|
|
|
|
|
/* PORT means we are now awaiting the server to connect to us. */
|
2005-02-09 08:06:40 -05:00
|
|
|
if(data->set.ftp_use_port) {
|
2011-12-19 08:35:20 -05:00
|
|
|
bool connected;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2013-08-20 16:45:47 -04:00
|
|
|
state(conn, FTP_STOP); /* no longer in STOR state */
|
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
result = AllowServerConnect(conn, &connected);
|
2005-09-27 04:46:54 -04:00
|
|
|
if(result)
|
|
|
|
return result;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
if(!connected) {
|
2011-12-20 07:06:26 -05:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2011-12-19 08:35:20 -05:00
|
|
|
infof(data, "Data conn was not available immediately\n");
|
2011-12-20 07:06:26 -05:00
|
|
|
ftpc->wait_data_conn = TRUE;
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
2009-02-20 03:16:03 -05:00
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
return InitiateTransfer(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* for LIST and RETR responses */
|
|
|
|
static CURLcode ftp_state_get_resp(struct connectdata *conn,
|
|
|
|
int ftpcode,
|
|
|
|
ftpstate instate)
|
2001-08-23 02:10:01 -04:00
|
|
|
{
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = data->req.protop;
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if((ftpcode == 150) || (ftpcode == 125)) {
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/*
|
|
|
|
A;
|
|
|
|
150 Opening BINARY mode data connection for /etc/passwd (2241
|
2011-04-19 09:54:13 -04:00
|
|
|
bytes). (ok, the file is being transferred)
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
B:
|
|
|
|
150 Opening ASCII mode data connection for /bin/ls
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
C:
|
|
|
|
150 ASCII data connection for /bin/ls (137.167.104.91,37445) (0 bytes).
|
2001-09-28 03:46:39 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
D:
|
2011-04-20 09:17:42 -04:00
|
|
|
150 Opening ASCII mode data connection for [file] (0.0.0.0,0) (545 bytes)
|
2001-08-23 02:10:01 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
E:
|
|
|
|
125 Data connection already open; Transfer starting. */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
curl_off_t size = -1; /* default unknown size */
|
2001-03-14 13:24:07 -05:00
|
|
|
|
2005-01-28 03:26:36 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/*
|
|
|
|
* It appears that there are FTP-servers that return size 0 for files when
|
|
|
|
* SIZE is used on the file while being in BINARY mode. To work around
|
|
|
|
* that (stupid) behavior, we attempt to parse the RETR response even if
|
|
|
|
* the SIZE returned size zero.
|
|
|
|
*
|
|
|
|
* Debugging help from Salvatore Sorrentino on February 26, 2003.
|
|
|
|
*/
|
2005-01-28 03:26:36 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if((instate != FTP_LIST) &&
|
2006-08-19 17:18:36 -04:00
|
|
|
!data->set.prefer_ascii &&
|
2005-02-09 08:06:40 -05:00
|
|
|
(ftp->downloadsize < 1)) {
|
|
|
|
/*
|
|
|
|
* It seems directory listings either don't show the size or very
|
|
|
|
* often uses size 0 anyway. ASCII transfers may very well turn out
|
2011-04-19 09:54:13 -04:00
|
|
|
* that the transferred amount of data is not the same as this line
|
2005-02-09 08:06:40 -05:00
|
|
|
* tells, why using this number in those cases only confuses us.
|
|
|
|
*
|
|
|
|
* Example D above makes this parsing a little tricky */
|
|
|
|
char *bytes;
|
2017-04-24 10:05:46 -04:00
|
|
|
char *buf = data->state.buffer;
|
2017-09-09 17:09:06 -04:00
|
|
|
bytes = strstr(buf, " bytes");
|
2017-10-03 11:20:06 -04:00
|
|
|
if(bytes) {
|
|
|
|
long in = (long)(--bytes-buf);
|
2005-02-09 08:06:40 -05:00
|
|
|
/* this is a hint there is size information in there! ;-) */
|
|
|
|
while(--in) {
|
|
|
|
/* scan for the left parenthesis and break there */
|
|
|
|
if('(' == *bytes)
|
|
|
|
break;
|
|
|
|
/* skip only digits */
|
2006-10-17 17:32:56 -04:00
|
|
|
if(!ISDIGIT(*bytes)) {
|
2017-09-09 17:09:06 -04:00
|
|
|
bytes = NULL;
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* one more estep backwards */
|
|
|
|
bytes--;
|
|
|
|
}
|
|
|
|
/* if we have nothing but digits: */
|
|
|
|
if(bytes++) {
|
|
|
|
/* get the number! */
|
2017-08-14 17:33:23 -04:00
|
|
|
(void)curlx_strtoofft(bytes, NULL, 0, &size);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ftp->downloadsize > -1)
|
|
|
|
size = ftp->downloadsize;
|
2005-01-28 03:26:36 -05:00
|
|
|
|
2007-11-24 18:16:55 -05:00
|
|
|
if(size > data->req.maxdownload && data->req.maxdownload > 0)
|
|
|
|
size = data->req.size = data->req.maxdownload;
|
2010-01-19 16:39:10 -05:00
|
|
|
else if((instate != FTP_LIST) && (data->set.prefer_ascii))
|
|
|
|
size = -1; /* kludge for servers that understate ASCII mode file size */
|
2006-09-07 17:49:20 -04:00
|
|
|
|
2013-12-31 06:10:25 -05:00
|
|
|
infof(data, "Maxdownload = %" CURL_FORMAT_CURL_OFF_T "\n",
|
|
|
|
data->req.maxdownload);
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(instate != FTP_LIST)
|
2013-12-31 06:10:25 -05:00
|
|
|
infof(data, "Getting file with size: %" CURL_FORMAT_CURL_OFF_T "\n",
|
|
|
|
size);
|
2004-02-09 02:52:36 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* FTP download: */
|
2011-12-19 08:35:20 -05:00
|
|
|
conn->proto.ftpc.state_saved = instate;
|
|
|
|
conn->proto.ftpc.retr_size_saved = size;
|
|
|
|
|
|
|
|
if(data->set.ftp_use_port) {
|
|
|
|
bool connected;
|
|
|
|
|
|
|
|
result = AllowServerConnect(conn, &connected);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2011-12-19 08:35:20 -05:00
|
|
|
if(!connected) {
|
2011-12-20 07:06:26 -05:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2011-12-19 08:35:20 -05:00
|
|
|
infof(data, "Data conn was not available immediately\n");
|
|
|
|
state(conn, FTP_STOP);
|
2011-12-20 07:06:26 -05:00
|
|
|
ftpc->wait_data_conn = TRUE;
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return InitiateTransfer(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if((instate == FTP_LIST) && (ftpcode == 450)) {
|
|
|
|
/* simply no matching files in the dir listing */
|
2007-09-26 08:00:01 -04:00
|
|
|
ftp->transfer = FTPTRANSFER_NONE; /* don't download anything */
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_STOP); /* this phase is over */
|
2004-03-24 17:45:37 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else {
|
2005-05-02 18:53:55 -04:00
|
|
|
failf(data, "RETR response: %03d", ftpcode);
|
2010-01-22 18:21:39 -05:00
|
|
|
return instate == FTP_RETR && ftpcode == 550?
|
|
|
|
CURLE_REMOTE_FILE_NOT_FOUND:
|
|
|
|
CURLE_FTP_COULDNT_RETR_FILE;
|
2001-02-05 18:04:44 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* after USER, PASS and ACCT */
|
|
|
|
static CURLcode ftp_state_loggedin(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
if(conn->ssl[FIRSTSOCKET].use) {
|
|
|
|
/* PBSZ = PROTECTION BUFFER SIZE.
|
|
|
|
|
|
|
|
The 'draft-murray-auth-ftp-ssl' (draft 12, page 7) says:
|
|
|
|
|
|
|
|
Specifically, the PROT command MUST be preceded by a PBSZ
|
|
|
|
command and a PBSZ command MUST be preceded by a successful
|
|
|
|
security data exchange (the TLS negotiation in this case)
|
|
|
|
|
|
|
|
... (and on page 8):
|
|
|
|
|
|
|
|
Thus the PBSZ command must still be issued, but must have a
|
|
|
|
parameter of '0' to indicate that no buffering is taking place
|
|
|
|
and the data connection should not be encapsulated.
|
|
|
|
*/
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "PBSZ %d", 0);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_PBSZ);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = ftp_state_pwd(conn);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for USER and PASS responses */
|
|
|
|
static CURLcode ftp_state_user_resp(struct connectdata *conn,
|
|
|
|
int ftpcode,
|
|
|
|
ftpstate instate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
(void)instate; /* no use for this yet */
|
|
|
|
|
2007-07-01 18:01:18 -04:00
|
|
|
/* some need password anyway, and others just return 2xx ignored */
|
2007-07-20 05:35:58 -04:00
|
|
|
if((ftpcode == 331) && (ftpc->state == FTP_USER)) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* 331 Password required for ...
|
|
|
|
(the server requires to send the user's password too) */
|
2020-02-06 09:59:18 -05:00
|
|
|
PPSENDF(&ftpc->pp, "PASS %s", conn->passwd?conn->passwd:"");
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_PASS);
|
|
|
|
}
|
|
|
|
else if(ftpcode/100 == 2) {
|
|
|
|
/* 230 User ... logged in.
|
|
|
|
(the user logged in with or without password) */
|
|
|
|
result = ftp_state_loggedin(conn);
|
|
|
|
}
|
|
|
|
else if(ftpcode == 332) {
|
2007-08-01 17:20:01 -04:00
|
|
|
if(data->set.str[STRING_FTP_ACCOUNT]) {
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "ACCT %s", data->set.str[STRING_FTP_ACCOUNT]);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_ACCT);
|
|
|
|
}
|
|
|
|
else {
|
2005-02-09 08:59:40 -05:00
|
|
|
failf(data, "ACCT requested but none available");
|
2005-02-09 08:06:40 -05:00
|
|
|
result = CURLE_LOGIN_DENIED;
|
2001-02-05 18:04:44 -05:00
|
|
|
}
|
2001-09-28 04:58:18 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else {
|
|
|
|
/* All other response codes, like:
|
2005-01-28 03:26:36 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
530 User ... access denied
|
|
|
|
(the server denies to log the specified user) */
|
2006-07-25 18:45:21 -04:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(conn->data->set.str[STRING_FTP_ALTERNATIVE_TO_USER] &&
|
2006-07-25 18:45:21 -04:00
|
|
|
!conn->data->state.ftp_trying_alternative) {
|
|
|
|
/* Ok, USER failed. Let's try the supplied command. */
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "%s",
|
|
|
|
conn->data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]);
|
2006-07-25 18:45:21 -04:00
|
|
|
conn->data->state.ftp_trying_alternative = TRUE;
|
|
|
|
state(conn, FTP_USER);
|
|
|
|
result = CURLE_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
failf(data, "Access denied: %03d", ftpcode);
|
|
|
|
result = CURLE_LOGIN_DENIED;
|
|
|
|
}
|
2001-09-28 04:58:18 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2001-02-05 18:04:44 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* for ACCT response */
|
|
|
|
static CURLcode ftp_state_acct_resp(struct connectdata *conn,
|
|
|
|
int ftpcode)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2005-02-09 08:06:40 -05:00
|
|
|
if(ftpcode != 230) {
|
|
|
|
failf(data, "ACCT rejected by server: %03d", ftpcode);
|
|
|
|
result = CURLE_FTP_WEIRD_PASS_REPLY; /* FIX */
|
2001-09-28 04:58:18 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else
|
|
|
|
result = ftp_state_loggedin(conn);
|
2001-02-05 18:04:44 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2004-11-29 16:25:07 -05:00
|
|
|
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static CURLcode ftp_statemach_act(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
curl_socket_t sock = conn->sock[FIRSTSOCKET];
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2005-02-09 08:06:40 -05:00
|
|
|
int ftpcode;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2009-12-12 16:54:01 -05:00
|
|
|
struct pingpong *pp = &ftpc->pp;
|
2008-10-22 01:46:29 -04:00
|
|
|
static const char ftpauth[][4] = { "SSL", "TLS" };
|
2005-05-14 02:00:40 -04:00
|
|
|
size_t nread = 0;
|
2004-11-25 17:21:49 -05:00
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
if(pp->sendleft)
|
|
|
|
return Curl_pp_flushsend(pp);
|
2001-02-05 18:04:44 -05:00
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
result = ftp_readresp(sock, pp, &ftpcode, &nread);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2001-09-04 22:49:04 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(ftpcode) {
|
|
|
|
/* we have now received a full FTP server response */
|
2006-09-07 17:49:20 -04:00
|
|
|
switch(ftpc->state) {
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_WAIT220:
|
2013-02-18 17:40:29 -05:00
|
|
|
if(ftpcode == 230)
|
|
|
|
/* 230 User logged in - already! */
|
|
|
|
return ftp_state_user_resp(conn, ftpcode, ftpc->state);
|
|
|
|
else if(ftpcode != 220) {
|
2008-04-05 17:02:38 -04:00
|
|
|
failf(data, "Got a %03d ftp-server response when 220 was expected",
|
|
|
|
ftpcode);
|
2016-08-22 15:37:03 -04:00
|
|
|
return CURLE_WEIRD_SERVER_REPLY;
|
2001-09-28 04:58:18 -04:00
|
|
|
}
|
2001-09-04 22:49:04 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* We have received a 220 response fine, now we proceed. */
|
2013-08-23 10:16:42 -04:00
|
|
|
#ifdef HAVE_GSSAPI
|
2007-07-01 18:01:18 -04:00
|
|
|
if(data->set.krb) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* If not anonymous login, try a secure login. Note that this
|
|
|
|
procedure is still BLOCKING. */
|
2001-02-05 18:04:44 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
Curl_sec_request_prot(conn, "private");
|
|
|
|
/* We set private first as default, in case the line below fails to
|
|
|
|
set a valid level */
|
2007-08-24 10:00:42 -04:00
|
|
|
Curl_sec_request_prot(conn, data->set.str[STRING_KRB_LEVEL]);
|
2001-09-04 22:49:04 -04:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(Curl_sec_login(conn))
|
2005-02-09 08:06:40 -05:00
|
|
|
infof(data, "Logging in with password in cleartext!\n");
|
2001-09-28 04:58:18 -04:00
|
|
|
else
|
2005-02-09 08:06:40 -05:00
|
|
|
infof(data, "Authentication successful\n");
|
|
|
|
}
|
|
|
|
#endif
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(data->set.use_ssl &&
|
|
|
|
(!conn->ssl[FIRSTSOCKET].use ||
|
|
|
|
(conn->bits.proxy_ssl_connected[FIRSTSOCKET] &&
|
|
|
|
!conn->proxy_ssl[FIRSTSOCKET].use))) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* We don't have a SSL/TLS connection yet, but FTPS is
|
|
|
|
requested. Try a FTPS connection now */
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
ftpc->count3 = 0;
|
2005-02-09 08:06:40 -05:00
|
|
|
switch(data->set.ftpsslauth) {
|
|
|
|
case CURLFTPAUTH_DEFAULT:
|
|
|
|
case CURLFTPAUTH_SSL:
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->count2 = 1; /* add one to get next */
|
|
|
|
ftpc->count1 = 0;
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
case CURLFTPAUTH_TLS:
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->count2 = -1; /* subtract one to get next */
|
|
|
|
ftpc->count1 = 1;
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
default:
|
2008-01-15 18:19:02 -05:00
|
|
|
failf(data, "unsupported parameter to CURLOPT_FTPSSLAUTH: %d",
|
2010-02-04 14:44:31 -05:00
|
|
|
(int)data->set.ftpsslauth);
|
2011-04-05 09:14:02 -04:00
|
|
|
return CURLE_UNKNOWN_OPTION; /* we don't know what to do */
|
2001-09-04 22:49:04 -04:00
|
|
|
}
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "AUTH %s", ftpauth[ftpc->count1]);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_AUTH);
|
2001-09-28 04:58:18 -04:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else {
|
2006-03-13 18:33:46 -05:00
|
|
|
result = ftp_state_user(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2001-09-28 04:58:18 -04:00
|
|
|
}
|
2001-09-04 22:49:04 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_AUTH:
|
|
|
|
/* we have gotten the response to a previous AUTH command */
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* RFC2228 (page 5) says:
|
|
|
|
*
|
|
|
|
* If the server is willing to accept the named security mechanism,
|
|
|
|
* and does not require any security data, it must respond with
|
|
|
|
* reply code 234/334.
|
|
|
|
*/
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if((ftpcode == 234) || (ftpcode == 334)) {
|
2005-04-07 11:27:13 -04:00
|
|
|
/* Curl_ssl_connect is BLOCKING */
|
|
|
|
result = Curl_ssl_connect(conn, FIRSTSOCKET);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!result) {
|
2016-11-16 12:49:15 -05:00
|
|
|
conn->bits.ftp_use_data_ssl = FALSE; /* clear-text data */
|
2005-03-29 06:43:02 -05:00
|
|
|
result = ftp_state_user(conn);
|
|
|
|
}
|
2004-11-25 17:21:49 -05:00
|
|
|
}
|
2006-09-07 17:49:20 -04:00
|
|
|
else if(ftpc->count3 < 1) {
|
|
|
|
ftpc->count3++;
|
|
|
|
ftpc->count1 += ftpc->count2; /* get next attempt */
|
2009-12-12 16:54:01 -05:00
|
|
|
result = Curl_pp_sendf(&ftpc->pp, "AUTH %s", ftpauth[ftpc->count1]);
|
2005-02-09 08:06:40 -05:00
|
|
|
/* remain in this same state */
|
|
|
|
}
|
2006-03-20 17:51:08 -05:00
|
|
|
else {
|
2011-11-03 04:54:12 -04:00
|
|
|
if(data->set.use_ssl > CURLUSESSL_TRY)
|
2007-08-31 15:36:32 -04:00
|
|
|
/* we failed and CURLUSESSL_CONTROL or CURLUSESSL_ALL is set */
|
|
|
|
result = CURLE_USE_SSL_FAILED;
|
2006-03-20 17:51:08 -05:00
|
|
|
else
|
|
|
|
/* ignore the failure and continue */
|
|
|
|
result = ftp_state_user(conn);
|
|
|
|
}
|
2005-03-29 06:43:02 -05:00
|
|
|
|
|
|
|
if(result)
|
|
|
|
return result;
|
2001-09-28 04:58:18 -04:00
|
|
|
break;
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_USER:
|
|
|
|
case FTP_PASS:
|
2006-09-07 17:49:20 -04:00
|
|
|
result = ftp_state_user_resp(conn, ftpcode, ftpc->state);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2003-05-13 08:11:31 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_ACCT:
|
|
|
|
result = ftp_state_acct_resp(conn, ftpcode);
|
|
|
|
break;
|
2003-05-13 08:11:31 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_PBSZ:
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "PROT %c",
|
2011-11-03 04:54:12 -04:00
|
|
|
data->set.use_ssl == CURLUSESSL_CONTROL ? 'C' : 'P');
|
2008-12-09 10:02:37 -05:00
|
|
|
state(conn, FTP_PROT);
|
2002-08-01 10:25:12 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2002-08-01 10:25:12 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_PROT:
|
|
|
|
if(ftpcode/100 == 2)
|
|
|
|
/* We have enabled SSL for the data connection! */
|
2016-11-16 12:49:15 -05:00
|
|
|
conn->bits.ftp_use_data_ssl =
|
2011-11-03 04:54:12 -04:00
|
|
|
(data->set.use_ssl != CURLUSESSL_CONTROL) ? TRUE : FALSE;
|
2005-02-09 08:06:40 -05:00
|
|
|
/* FTP servers typically responds with 500 if they decide to reject
|
|
|
|
our 'P' request */
|
2011-11-03 04:54:12 -04:00
|
|
|
else if(data->set.use_ssl > CURLUSESSL_CONTROL)
|
2005-02-09 08:06:40 -05:00
|
|
|
/* we failed and bails out */
|
2007-08-31 15:36:32 -04:00
|
|
|
return CURLE_USE_SSL_FAILED;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2007-02-20 17:02:11 -05:00
|
|
|
if(data->set.ftp_ccc) {
|
2007-01-05 18:11:14 -05:00
|
|
|
/* CCC - Clear Command Channel
|
|
|
|
*/
|
2013-07-24 11:06:28 -04:00
|
|
|
PPSENDF(&ftpc->pp, "%s", "CCC");
|
2007-01-05 18:11:14 -05:00
|
|
|
state(conn, FTP_CCC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = ftp_state_pwd(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FTP_CCC:
|
2007-11-05 04:45:09 -05:00
|
|
|
if(ftpcode < 500) {
|
2007-08-24 10:00:42 -04:00
|
|
|
/* First shut down the SSL layer (note: this call will block) */
|
|
|
|
result = Curl_ssl_shutdown(conn, FIRSTSOCKET);
|
2007-01-24 14:09:12 -05:00
|
|
|
|
2007-08-24 10:00:42 -04:00
|
|
|
if(result) {
|
|
|
|
failf(conn->data, "Failed to clear the command channel (CCC)");
|
|
|
|
return result;
|
|
|
|
}
|
2007-01-08 06:24:11 -05:00
|
|
|
}
|
2007-01-05 18:11:14 -05:00
|
|
|
|
|
|
|
/* Then continue as normal */
|
2005-02-09 08:06:40 -05:00
|
|
|
result = ftp_state_pwd(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
break;
|
2001-09-28 04:58:18 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_PWD:
|
|
|
|
if(ftpcode == 257) {
|
2017-09-09 17:09:06 -04:00
|
|
|
char *ptr = &data->state.buffer[4]; /* start on the first letter */
|
2017-04-24 18:16:10 -04:00
|
|
|
const size_t buf_size = data->set.buffer_size;
|
2009-08-24 08:57:25 -04:00
|
|
|
char *dir;
|
2017-09-24 18:35:22 -04:00
|
|
|
bool entry_extracted = FALSE;
|
2002-08-01 10:25:12 -04:00
|
|
|
|
2009-08-24 08:57:25 -04:00
|
|
|
dir = malloc(nread + 1);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(!dir)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* Reply format is like
|
2013-04-09 16:18:33 -04:00
|
|
|
257<space>[rubbish]"<directory-name>"<space><commentary> and the
|
|
|
|
RFC959 says
|
2001-09-28 04:58:18 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
The directory name can contain any character; embedded
|
|
|
|
double-quotes should be escaped by double-quotes (the
|
|
|
|
"quote-doubling" convention).
|
|
|
|
*/
|
2013-04-09 16:18:33 -04:00
|
|
|
|
|
|
|
/* scan for the first double-quote for non-standard responses */
|
2016-12-20 05:48:15 -05:00
|
|
|
while(ptr < &data->state.buffer[buf_size]
|
2013-04-09 16:18:33 -04:00
|
|
|
&& *ptr != '\n' && *ptr != '\0' && *ptr != '"')
|
|
|
|
ptr++;
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if('\"' == *ptr) {
|
|
|
|
/* it started good */
|
2018-06-02 16:52:56 -04:00
|
|
|
char *store;
|
2005-02-09 08:06:40 -05:00
|
|
|
ptr++;
|
2011-04-20 09:17:42 -04:00
|
|
|
for(store = dir; *ptr;) {
|
2005-02-09 08:06:40 -05:00
|
|
|
if('\"' == *ptr) {
|
|
|
|
if('\"' == ptr[1]) {
|
|
|
|
/* "quote-doubling" */
|
|
|
|
*store = ptr[1];
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* end of path */
|
2017-09-24 18:35:22 -04:00
|
|
|
entry_extracted = TRUE;
|
2005-02-09 08:06:40 -05:00
|
|
|
break; /* get out of this loop */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*store = *ptr;
|
|
|
|
store++;
|
|
|
|
ptr++;
|
|
|
|
}
|
2017-09-24 18:35:22 -04:00
|
|
|
*store = '\0'; /* zero terminate */
|
|
|
|
}
|
|
|
|
if(entry_extracted) {
|
2009-08-24 08:57:25 -04:00
|
|
|
/* If the path name does not look like an absolute path (i.e.: it
|
|
|
|
does not start with a '/'), we probably need some server-dependent
|
|
|
|
adjustments. For example, this is the case when connecting to
|
|
|
|
an OS400 FTP server: this server supports two name syntaxes,
|
2017-03-26 11:02:22 -04:00
|
|
|
the default one being incompatible with standard paths. In
|
2009-08-24 08:57:25 -04:00
|
|
|
addition, this server switches automatically to the regular path
|
|
|
|
syntax when one is encountered in a command: this results in
|
|
|
|
having an entrypath in the wrong syntax when later used in CWD.
|
|
|
|
The method used here is to check the server OS: we do it only
|
|
|
|
if the path name looks strange to minimize overhead on other
|
|
|
|
systems. */
|
|
|
|
|
2011-08-31 12:14:38 -04:00
|
|
|
if(!ftpc->server_os && dir[0] != '/') {
|
2011-08-30 19:58:06 -04:00
|
|
|
|
2013-07-24 11:06:28 -04:00
|
|
|
result = Curl_pp_sendf(&ftpc->pp, "%s", "SYST");
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result) {
|
2011-08-31 12:14:38 -04:00
|
|
|
free(dir);
|
2011-08-30 19:58:06 -04:00
|
|
|
return result;
|
|
|
|
}
|
2011-08-31 12:14:38 -04:00
|
|
|
Curl_safefree(ftpc->entrypath);
|
|
|
|
ftpc->entrypath = dir; /* remember this */
|
|
|
|
infof(data, "Entry path is '%s'\n", ftpc->entrypath);
|
|
|
|
/* also save it where getinfo can access it: */
|
|
|
|
data->state.most_recent_ftp_entrypath = ftpc->entrypath;
|
2009-08-24 08:57:25 -04:00
|
|
|
state(conn, FTP_SYST);
|
|
|
|
break;
|
|
|
|
}
|
2011-08-31 12:14:38 -04:00
|
|
|
|
|
|
|
Curl_safefree(ftpc->entrypath);
|
|
|
|
ftpc->entrypath = dir; /* remember this */
|
|
|
|
infof(data, "Entry path is '%s'\n", ftpc->entrypath);
|
|
|
|
/* also save it where getinfo can access it: */
|
|
|
|
data->state.most_recent_ftp_entrypath = ftpc->entrypath;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
else {
|
|
|
|
/* couldn't get the path */
|
|
|
|
free(dir);
|
|
|
|
infof(data, "Failed to figure out path\n");
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_STOP); /* we are done with the CONNECT phase! */
|
2005-05-07 09:57:07 -04:00
|
|
|
DEBUGF(infof(data, "protocol connect phase DONE\n"));
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
|
|
|
|
2009-08-24 08:57:25 -04:00
|
|
|
case FTP_SYST:
|
|
|
|
if(ftpcode == 215) {
|
2017-09-09 17:09:06 -04:00
|
|
|
char *ptr = &data->state.buffer[4]; /* start on the first letter */
|
2009-08-24 08:57:25 -04:00
|
|
|
char *os;
|
|
|
|
char *store;
|
|
|
|
|
|
|
|
os = malloc(nread + 1);
|
|
|
|
if(!os)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Reply format is like
|
|
|
|
215<space><OS-name><space><commentary>
|
|
|
|
*/
|
2011-04-20 09:17:42 -04:00
|
|
|
while(*ptr == ' ')
|
2009-08-24 08:57:25 -04:00
|
|
|
ptr++;
|
2011-04-20 09:17:42 -04:00
|
|
|
for(store = os; *ptr && *ptr != ' ';)
|
2009-08-24 08:57:25 -04:00
|
|
|
*store++ = *ptr++;
|
|
|
|
*store = '\0'; /* zero terminate */
|
|
|
|
|
|
|
|
/* Check for special servers here. */
|
|
|
|
|
2016-09-30 11:15:05 -04:00
|
|
|
if(strcasecompare(os, "OS/400")) {
|
2009-08-24 08:57:25 -04:00
|
|
|
/* Force OS400 name format 1. */
|
2013-07-24 11:06:28 -04:00
|
|
|
result = Curl_pp_sendf(&ftpc->pp, "%s", "SITE NAMEFMT 1");
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result) {
|
2011-08-31 12:14:38 -04:00
|
|
|
free(os);
|
2011-08-30 19:58:06 -04:00
|
|
|
return result;
|
|
|
|
}
|
2011-08-31 12:14:38 -04:00
|
|
|
/* remember target server OS */
|
|
|
|
Curl_safefree(ftpc->server_os);
|
|
|
|
ftpc->server_os = os;
|
2009-08-24 08:57:25 -04:00
|
|
|
state(conn, FTP_NAMEFMT);
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
/* Nothing special for the target server. */
|
|
|
|
/* remember target server OS */
|
|
|
|
Curl_safefree(ftpc->server_os);
|
|
|
|
ftpc->server_os = os;
|
2009-08-24 08:57:25 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Cannot identify server OS. Continue anyway and cross fingers. */
|
|
|
|
}
|
|
|
|
|
|
|
|
state(conn, FTP_STOP); /* we are done with the CONNECT phase! */
|
|
|
|
DEBUGF(infof(data, "protocol connect phase DONE\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FTP_NAMEFMT:
|
|
|
|
if(ftpcode == 250) {
|
|
|
|
/* Name format change successful: reload initial path. */
|
|
|
|
ftp_state_pwd(conn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
state(conn, FTP_STOP); /* we are done with the CONNECT phase! */
|
|
|
|
DEBUGF(infof(data, "protocol connect phase DONE\n"));
|
|
|
|
break;
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_QUOTE:
|
|
|
|
case FTP_POSTQUOTE:
|
|
|
|
case FTP_RETR_PREQUOTE:
|
|
|
|
case FTP_STOR_PREQUOTE:
|
2010-04-09 10:54:52 -04:00
|
|
|
if((ftpcode >= 400) && !ftpc->count2) {
|
2011-04-19 09:54:13 -04:00
|
|
|
/* failure response code, and not allowed to fail */
|
2005-02-09 08:06:40 -05:00
|
|
|
failf(conn->data, "QUOT command failed with %03d", ftpcode);
|
2007-08-30 16:34:57 -04:00
|
|
|
return CURLE_QUOTE_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2006-09-07 17:49:20 -04:00
|
|
|
result = ftp_state_quote(conn, FALSE, ftpc->state);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_CWD:
|
|
|
|
if(ftpcode/100 != 2) {
|
|
|
|
/* failure to CWD there */
|
|
|
|
if(conn->data->set.ftp_create_missing_dirs &&
|
2017-08-03 17:48:57 -04:00
|
|
|
ftpc->cwdcount && !ftpc->count2) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* try making it */
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->count2++; /* counter to prevent CWD-MKD loops */
|
2017-08-03 17:48:57 -04:00
|
|
|
PPSENDF(&ftpc->pp, "MKD %s", ftpc->dirs[ftpc->cwdcount - 1]);
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_MKD);
|
|
|
|
}
|
2005-11-13 19:18:12 -05:00
|
|
|
else {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* return failure */
|
2005-11-13 19:18:12 -05:00
|
|
|
failf(data, "Server denied you to change to the given directory");
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->cwdfail = TRUE; /* don't remember this path as we failed
|
|
|
|
to enter it */
|
2007-08-30 16:34:57 -04:00
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
2005-11-13 19:18:12 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* success */
|
2017-09-09 17:09:06 -04:00
|
|
|
ftpc->count2 = 0;
|
2017-08-03 17:48:57 -04:00
|
|
|
if(++ftpc->cwdcount <= ftpc->dirdepth) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* send next CWD */
|
2017-08-03 17:48:57 -04:00
|
|
|
PPSENDF(&ftpc->pp, "CWD %s", ftpc->dirs[ftpc->cwdcount - 1]);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
else {
|
2013-08-08 06:19:01 -04:00
|
|
|
result = ftp_state_mdtm(conn);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_MKD:
|
2009-02-17 04:07:25 -05:00
|
|
|
if((ftpcode/100 != 2) && !ftpc->count3--) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* failure to MKD the dir */
|
|
|
|
failf(data, "Failed to MKD dir: %03d", ftpcode);
|
2007-08-30 16:34:57 -04:00
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
state(conn, FTP_CWD);
|
|
|
|
/* send CWD */
|
2017-08-03 17:48:57 -04:00
|
|
|
PPSENDF(&ftpc->pp, "CWD %s", ftpc->dirs[ftpc->cwdcount - 1]);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_MDTM:
|
|
|
|
result = ftp_state_mdtm_resp(conn, ftpcode);
|
|
|
|
break;
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_TYPE:
|
|
|
|
case FTP_LIST_TYPE:
|
|
|
|
case FTP_RETR_TYPE:
|
|
|
|
case FTP_STOR_TYPE:
|
2006-09-07 17:49:20 -04:00
|
|
|
result = ftp_state_type_resp(conn, ftpcode, ftpc->state);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_SIZE:
|
|
|
|
case FTP_RETR_SIZE:
|
|
|
|
case FTP_STOR_SIZE:
|
2006-09-07 17:49:20 -04:00
|
|
|
result = ftp_state_size_resp(conn, ftpcode, ftpc->state);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_REST:
|
|
|
|
case FTP_RETR_REST:
|
2006-09-07 17:49:20 -04:00
|
|
|
result = ftp_state_rest_resp(conn, ftpcode, ftpc->state);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2010-01-01 09:44:44 -05:00
|
|
|
case FTP_PRET:
|
|
|
|
if(ftpcode != 200) {
|
2011-04-20 09:17:42 -04:00
|
|
|
/* there only is this one standard OK return code. */
|
2010-01-01 09:44:44 -05:00
|
|
|
failf(data, "PRET command not accepted: %03d", ftpcode);
|
|
|
|
return CURLE_FTP_PRET_FAILED;
|
|
|
|
}
|
|
|
|
result = ftp_state_use_pasv(conn);
|
|
|
|
break;
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_PASV:
|
|
|
|
result = ftp_state_pasv_resp(conn, ftpcode);
|
|
|
|
break;
|
2001-09-28 04:58:18 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_PORT:
|
|
|
|
result = ftp_state_port_resp(conn, ftpcode);
|
|
|
|
break;
|
2001-09-28 04:58:18 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_LIST:
|
|
|
|
case FTP_RETR:
|
2006-09-07 17:49:20 -04:00
|
|
|
result = ftp_state_get_resp(conn, ftpcode, ftpc->state);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2001-09-28 04:58:18 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_STOR:
|
2011-12-19 08:35:20 -05:00
|
|
|
result = ftp_state_stor_resp(conn, ftpcode, ftpc->state);
|
2005-02-09 08:06:40 -05:00
|
|
|
break;
|
2001-11-26 19:48:45 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
case FTP_QUIT:
|
|
|
|
/* fallthrough, just stop! */
|
|
|
|
default:
|
|
|
|
/* internal error */
|
|
|
|
state(conn, FTP_STOP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} /* if(ftpcode) */
|
2001-11-26 19:48:45 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2001-11-26 19:48:45 -05:00
|
|
|
|
2001-09-28 04:58:18 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
/* called repeatedly until done from multi.c */
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_multi_statemach(struct connectdata *conn,
|
2009-12-12 16:54:01 -05:00
|
|
|
bool *done)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2018-12-14 03:21:47 -05:00
|
|
|
CURLcode result = Curl_pp_statemach(&ftpc->pp, FALSE, FALSE);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2016-10-18 04:58:58 -04:00
|
|
|
/* Check for the state outside of the Curl_socket_check() return code checks
|
2009-08-31 16:49:30 -04:00
|
|
|
since at times we are in fact already in this state when this function
|
|
|
|
gets called. */
|
2011-09-05 14:46:09 -04:00
|
|
|
*done = (ftpc->state == FTP_STOP) ? TRUE : FALSE;
|
2009-08-31 16:49:30 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2013-02-15 05:03:42 -05:00
|
|
|
static CURLcode ftp_block_statemach(struct connectdata *conn)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2009-12-12 16:54:01 -05:00
|
|
|
struct pingpong *pp = &ftpc->pp;
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2001-09-04 22:49:04 -04:00
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
while(ftpc->state != FTP_STOP) {
|
2018-12-14 03:21:47 -05:00
|
|
|
result = Curl_pp_statemach(pp, TRUE, TRUE /* disconnecting */);
|
2009-11-26 14:07:54 -05:00
|
|
|
if(result)
|
|
|
|
break;
|
2001-11-26 19:48:45 -05:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2003-08-05 10:40:59 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/*
|
2007-12-08 17:50:55 -05:00
|
|
|
* ftp_connect() should do everything that is to be considered a part of
|
2005-02-09 08:06:40 -05:00
|
|
|
* the connection phase.
|
|
|
|
*
|
|
|
|
* The variable 'done' points to will be TRUE if the protocol-layer connect
|
2013-08-09 17:39:09 -04:00
|
|
|
* phase is done when this function returns, or FALSE if not.
|
|
|
|
*
|
2005-02-09 08:06:40 -05:00
|
|
|
*/
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_connect(struct connectdata *conn,
|
2007-10-12 09:36:37 -04:00
|
|
|
bool *done) /* see description above */
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
|
|
|
CURLcode result;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2009-12-12 16:54:01 -05:00
|
|
|
struct pingpong *pp = &ftpc->pp;
|
2002-04-25 15:00:57 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
*done = FALSE; /* default to not done yet */
|
2003-08-05 10:40:59 -04:00
|
|
|
|
2011-04-19 09:54:13 -04:00
|
|
|
/* We always support persistent connections on ftp */
|
2014-05-20 04:32:23 -04:00
|
|
|
connkeep(conn, "FTP default");
|
2002-11-05 05:51:41 -05:00
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
pp->response_time = RESP_TIMEOUT; /* set default response time-out */
|
|
|
|
pp->statemach_act = ftp_statemach_act;
|
|
|
|
pp->endofresp = ftp_endofresp;
|
|
|
|
pp->conn = conn;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2011-05-05 09:49:43 -04:00
|
|
|
if(conn->handler->flags & PROTOPT_SSL) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* BLOCKING */
|
2005-04-07 11:27:13 -04:00
|
|
|
result = Curl_ssl_connect(conn, FIRSTSOCKET);
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
Curl_pp_init(pp); /* init the generic pingpong data */
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* When we connect, we start in the state where we await the 220
|
|
|
|
response */
|
|
|
|
state(conn, FTP_WAIT220);
|
|
|
|
|
2013-01-17 06:59:23 -05:00
|
|
|
result = ftp_multi_statemach(conn, done);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
return result;
|
2001-09-28 04:58:18 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2007-12-08 17:50:55 -05:00
|
|
|
* ftp_done()
|
2005-02-09 08:06:40 -05:00
|
|
|
*
|
|
|
|
* The DONE function. This does what needs to be done after a single DO has
|
|
|
|
* performed.
|
2001-11-01 07:47:22 -05:00
|
|
|
*
|
2005-02-09 08:06:40 -05:00
|
|
|
* Input argument is already checked for validity.
|
2001-11-01 07:47:22 -05:00
|
|
|
*/
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_done(struct connectdata *conn, CURLcode status,
|
2016-03-29 18:17:02 -04:00
|
|
|
bool premature)
|
2001-09-28 04:58:18 -04:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = data->req.protop;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2009-12-12 16:54:01 -05:00
|
|
|
struct pingpong *pp = &ftpc->pp;
|
2005-02-09 08:06:40 -05:00
|
|
|
ssize_t nread;
|
|
|
|
int ftpcode;
|
2011-10-24 14:42:01 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2019-09-25 11:48:53 -04:00
|
|
|
char *rawPath = NULL;
|
|
|
|
size_t pathLen = 0;
|
2006-09-07 17:49:20 -04:00
|
|
|
|
2006-09-16 16:57:59 -04:00
|
|
|
if(!ftp)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2006-12-11 04:32:58 -05:00
|
|
|
switch(status) {
|
|
|
|
case CURLE_BAD_DOWNLOAD_RESUME:
|
|
|
|
case CURLE_FTP_WEIRD_PASV_REPLY:
|
|
|
|
case CURLE_FTP_PORT_FAILED:
|
2011-12-19 08:35:20 -05:00
|
|
|
case CURLE_FTP_ACCEPT_FAILED:
|
|
|
|
case CURLE_FTP_ACCEPT_TIMEOUT:
|
2007-08-30 16:34:57 -04:00
|
|
|
case CURLE_FTP_COULDNT_SET_TYPE:
|
2006-12-11 04:32:58 -05:00
|
|
|
case CURLE_FTP_COULDNT_RETR_FILE:
|
2012-01-07 16:13:57 -05:00
|
|
|
case CURLE_PARTIAL_FILE:
|
2007-07-13 16:04:53 -04:00
|
|
|
case CURLE_UPLOAD_FAILED:
|
2007-08-30 16:34:57 -04:00
|
|
|
case CURLE_REMOTE_ACCESS_DENIED:
|
2007-03-29 21:13:47 -04:00
|
|
|
case CURLE_FILESIZE_EXCEEDED:
|
2009-02-18 06:40:16 -05:00
|
|
|
case CURLE_REMOTE_FILE_NOT_FOUND:
|
2009-02-20 03:16:03 -05:00
|
|
|
case CURLE_WRITE_ERROR:
|
2006-12-11 04:32:58 -05:00
|
|
|
/* the connection stays alive fine even though this happened */
|
|
|
|
/* fall-through */
|
|
|
|
case CURLE_OK: /* doesn't affect the control connection's status */
|
2016-08-26 03:34:41 -04:00
|
|
|
if(!premature)
|
2007-01-16 17:22:10 -05:00
|
|
|
break;
|
2016-08-26 03:34:41 -04:00
|
|
|
|
2007-02-25 23:24:26 -05:00
|
|
|
/* until we cope better with prematurely ended requests, let them
|
2007-01-16 17:22:10 -05:00
|
|
|
* fallback as if in complete failure */
|
2017-02-28 16:45:28 -05:00
|
|
|
/* FALLTHROUGH */
|
2006-12-11 04:32:58 -05:00
|
|
|
default: /* by default, an error means the control connection is
|
|
|
|
wedged and should not be used anymore */
|
|
|
|
ftpc->ctl_valid = FALSE;
|
|
|
|
ftpc->cwdfail = TRUE; /* set this TRUE to prevent us to remember the
|
|
|
|
current path, as this connection is going */
|
2014-05-20 04:32:23 -04:00
|
|
|
connclose(conn, "FTP ended with bad error code");
|
2009-04-20 17:41:17 -04:00
|
|
|
result = status; /* use the already set error code */
|
2006-12-11 04:32:58 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:51:50 -04:00
|
|
|
if(data->state.wildcardmatch) {
|
2010-05-12 09:33:22 -04:00
|
|
|
if(data->set.chunk_end && ftpc->file) {
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(data, true);
|
2010-05-12 09:33:22 -04:00
|
|
|
data->set.chunk_end(data->wildcard.customptr);
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(data, false);
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
ftpc->known_filesize = -1;
|
|
|
|
}
|
|
|
|
|
2016-11-02 02:22:27 -04:00
|
|
|
if(!result)
|
2019-09-25 11:48:53 -04:00
|
|
|
/* get the url-decoded "raw" path */
|
|
|
|
result = Curl_urldecode(data, ftp->path, 0, &rawPath, &pathLen, TRUE);
|
2016-10-08 05:21:38 -04:00
|
|
|
if(result) {
|
2016-11-02 02:22:27 -04:00
|
|
|
/* We can limp along anyway (and should try to since we may already be in
|
|
|
|
* the error path) */
|
2011-10-24 14:42:01 -04:00
|
|
|
ftpc->ctl_valid = FALSE; /* mark control connection as bad */
|
2014-05-20 04:32:23 -04:00
|
|
|
connclose(conn, "FTP: out of memory!"); /* mark for connection closure */
|
2019-09-19 08:59:13 -04:00
|
|
|
free(ftpc->prevpath);
|
2011-10-24 14:42:01 -04:00
|
|
|
ftpc->prevpath = NULL; /* no path remembering */
|
2010-01-22 18:21:39 -05:00
|
|
|
}
|
2019-09-19 08:59:13 -04:00
|
|
|
else { /* remember working directory for connection reuse */
|
2019-09-25 11:48:53 -04:00
|
|
|
if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/'))
|
|
|
|
free(rawPath); /* full path => no CWDs happened => keep ftpc->prevpath */
|
2019-09-19 08:59:13 -04:00
|
|
|
else {
|
|
|
|
free(ftpc->prevpath);
|
|
|
|
|
|
|
|
if(!ftpc->cwdfail) {
|
|
|
|
if(data->set.ftp_filemethod == FTPFILE_NOCWD)
|
2019-09-25 11:48:53 -04:00
|
|
|
pathLen = 0; /* relative path => working directory is FTP home */
|
2019-09-19 08:59:13 -04:00
|
|
|
else
|
2019-09-25 11:48:53 -04:00
|
|
|
pathLen -= ftpc->file?strlen(ftpc->file):0; /* file is url-decoded */
|
2019-09-19 08:59:13 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
rawPath[pathLen] = '\0';
|
|
|
|
ftpc->prevpath = rawPath;
|
2007-09-11 18:21:39 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-09-25 11:48:53 -04:00
|
|
|
free(rawPath);
|
2019-09-19 08:59:13 -04:00
|
|
|
ftpc->prevpath = NULL; /* no path */
|
2007-09-11 18:21:39 -04:00
|
|
|
}
|
2007-04-11 21:26:02 -04:00
|
|
|
}
|
2019-09-19 08:59:13 -04:00
|
|
|
|
|
|
|
if(ftpc->prevpath)
|
|
|
|
infof(data, "Remembering we are in dir \"%s\"\n", ftpc->prevpath);
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2019-09-19 08:59:13 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* free the dir tree and file parts */
|
2007-10-24 17:14:11 -04:00
|
|
|
freedirs(ftpc);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* shut down the socket to inform the server we're done */
|
2001-05-11 07:38:13 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
#ifdef _WIN32_WCE
|
2015-03-17 08:41:49 -04:00
|
|
|
shutdown(conn->sock[SECONDARYSOCKET], 2); /* SD_BOTH */
|
2005-02-09 08:06:40 -05:00
|
|
|
#endif
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2008-08-11 19:16:08 -04:00
|
|
|
if(conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD) {
|
2013-04-06 17:11:20 -04:00
|
|
|
if(!result && ftpc->dont_check && data->req.maxdownload > 0) {
|
2010-11-04 16:44:02 -04:00
|
|
|
/* partial download completed */
|
2013-07-24 11:06:28 -04:00
|
|
|
result = Curl_pp_sendf(pp, "%s", "ABOR");
|
2011-10-24 14:42:01 -04:00
|
|
|
if(result) {
|
|
|
|
failf(data, "Failure sending ABOR command: %s",
|
|
|
|
curl_easy_strerror(result));
|
|
|
|
ftpc->ctl_valid = FALSE; /* mark control connection as bad */
|
2014-05-20 04:32:23 -04:00
|
|
|
connclose(conn, "ABOR command failed"); /* connection closure */
|
2011-10-24 14:42:01 -04:00
|
|
|
}
|
2013-04-06 17:11:20 -04:00
|
|
|
}
|
2010-10-27 04:54:35 -04:00
|
|
|
|
2008-08-11 19:16:08 -04:00
|
|
|
if(conn->ssl[SECONDARYSOCKET].use) {
|
2020-05-13 06:22:49 -04:00
|
|
|
/* The secondary socket used SSL so we must close down that part first
|
|
|
|
before we close the socket for real */
|
|
|
|
result = Curl_ssl_shutdown(conn, SECONDARYSOCKET);
|
2007-07-29 08:54:05 -04:00
|
|
|
|
2008-08-11 19:16:08 -04:00
|
|
|
/* Note that we keep "use" set to TRUE since that (next) connection is
|
|
|
|
still requested to use SSL */
|
|
|
|
}
|
2016-03-07 18:07:14 -05:00
|
|
|
close_secondarysocket(conn);
|
2008-08-11 19:16:08 -04:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2010-10-27 04:54:35 -04:00
|
|
|
if(!result && (ftp->transfer == FTPTRANSFER_BODY) && ftpc->ctl_valid &&
|
2009-12-12 16:54:01 -05:00
|
|
|
pp->pending_resp && !premature) {
|
2006-04-18 19:14:30 -04:00
|
|
|
/*
|
|
|
|
* Let's see what the server says about the transfer we just performed,
|
2005-02-09 08:06:40 -05:00
|
|
|
* but lower the timeout as sometimes this connection has died while the
|
2011-04-19 09:54:13 -04:00
|
|
|
* data has been transferred. This happens when doing through NATs etc that
|
2005-02-09 08:06:40 -05:00
|
|
|
* abandon old silent connections.
|
|
|
|
*/
|
2020-05-28 18:08:03 -04:00
|
|
|
timediff_t old_time = pp->response_time;
|
2006-04-18 19:14:30 -04:00
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
pp->response_time = 60*1000; /* give it only a minute for now */
|
2017-10-25 05:59:43 -04:00
|
|
|
pp->response = Curl_now(); /* timeout relative now */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2002-12-03 05:25:31 -05:00
|
|
|
result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
pp->response_time = old_time; /* set this back to previous value */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(!nread && (CURLE_OPERATION_TIMEDOUT == result)) {
|
|
|
|
failf(data, "control connection looks dead");
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->ctl_valid = FALSE; /* mark control connection as bad */
|
2014-05-20 04:32:23 -04:00
|
|
|
connclose(conn, "Timeout or similar in FTP DONE operation"); /* close */
|
2003-12-01 05:14:39 -05:00
|
|
|
}
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2010-11-04 16:44:02 -04:00
|
|
|
if(ftpc->dont_check && data->req.maxdownload > 0) {
|
|
|
|
/* we have just sent ABOR and there is no reliable way to check if it was
|
|
|
|
* successful or not; we have to close the connection now */
|
|
|
|
infof(data, "partial download completed, closing connection\n");
|
2014-05-20 04:32:23 -04:00
|
|
|
connclose(conn, "Partial download with no ability to check");
|
2010-11-04 16:44:02 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
if(!ftpc->dont_check) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* 226 Transfer complete, 250 Requested file action okay, completed. */
|
|
|
|
if((ftpcode != 226) && (ftpcode != 250)) {
|
|
|
|
failf(data, "server did not report OK, got %d", ftpcode);
|
2006-04-18 19:14:30 -04:00
|
|
|
result = CURLE_PARTIAL_FILE;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
2002-02-28 18:31:23 -05:00
|
|
|
|
2007-01-16 17:22:10 -05:00
|
|
|
if(result || premature)
|
2006-04-18 19:14:30 -04:00
|
|
|
/* the response code from the transfer showed an error already so no
|
|
|
|
use checking further */
|
|
|
|
;
|
|
|
|
else if(data->set.upload) {
|
2014-04-15 07:49:18 -04:00
|
|
|
if((-1 != data->state.infilesize) &&
|
2019-02-28 05:36:26 -05:00
|
|
|
(data->state.infilesize != data->req.writebytecount) &&
|
2006-04-18 19:14:30 -04:00
|
|
|
!data->set.crlf &&
|
2007-09-26 08:00:01 -04:00
|
|
|
(ftp->transfer == FTPTRANSFER_BODY)) {
|
2013-12-31 06:10:25 -05:00
|
|
|
failf(data, "Uploaded unaligned file size (%" CURL_FORMAT_CURL_OFF_T
|
|
|
|
" out of %" CURL_FORMAT_CURL_OFF_T " bytes)",
|
2019-02-28 05:36:26 -05:00
|
|
|
data->req.bytecount, data->state.infilesize);
|
2006-04-18 19:14:30 -04:00
|
|
|
result = CURLE_PARTIAL_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-11-24 18:16:55 -05:00
|
|
|
if((-1 != data->req.size) &&
|
2019-02-28 05:36:26 -05:00
|
|
|
(data->req.size != data->req.bytecount) &&
|
2006-04-26 03:40:37 -04:00
|
|
|
#ifdef CURL_DO_LINEEND_CONV
|
|
|
|
/* Most FTP servers don't adjust their file SIZE response for CRLFs, so
|
|
|
|
* we'll check to see if the discrepancy can be explained by the number
|
|
|
|
* of CRLFs we've changed to LFs.
|
|
|
|
*/
|
2007-11-24 18:16:55 -05:00
|
|
|
((data->req.size + data->state.crlf_conversions) !=
|
2019-02-28 05:36:26 -05:00
|
|
|
data->req.bytecount) &&
|
2006-04-26 03:40:37 -04:00
|
|
|
#endif /* CURL_DO_LINEEND_CONV */
|
2019-02-28 05:36:26 -05:00
|
|
|
(data->req.maxdownload != data->req.bytecount)) {
|
2013-12-31 06:10:25 -05:00
|
|
|
failf(data, "Received only partial file: %" CURL_FORMAT_CURL_OFF_T
|
2019-02-28 05:36:26 -05:00
|
|
|
" bytes", data->req.bytecount);
|
2006-04-18 19:14:30 -04:00
|
|
|
result = CURLE_PARTIAL_FILE;
|
|
|
|
}
|
2006-09-07 17:49:20 -04:00
|
|
|
else if(!ftpc->dont_check &&
|
2019-02-28 05:36:26 -05:00
|
|
|
!data->req.bytecount &&
|
2007-11-24 18:16:55 -05:00
|
|
|
(data->req.size>0)) {
|
2006-04-18 19:14:30 -04:00
|
|
|
failf(data, "No data was received!");
|
|
|
|
result = CURLE_FTP_COULDNT_RETR_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* clear these for next connection */
|
2007-09-26 08:00:01 -04:00
|
|
|
ftp->transfer = FTPTRANSFER_BODY;
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->dont_check = FALSE;
|
2001-11-28 08:05:39 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* Send any post-transfer QUOTE strings? */
|
2007-01-16 17:22:10 -05:00
|
|
|
if(!status && !result && !premature && data->set.postquote)
|
2005-02-09 08:06:40 -05:00
|
|
|
result = ftp_sendquote(conn, data->set.postquote);
|
2018-09-14 17:33:28 -04:00
|
|
|
Curl_safefree(ftp->pathalloc);
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
|
|
|
}
|
2001-05-11 07:38:13 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* ftp_sendquote()
|
|
|
|
*
|
|
|
|
* Where a 'quote' means a list of custom commands to send to the server.
|
|
|
|
* The quote list is passed as an argument.
|
2009-07-27 14:36:56 -04:00
|
|
|
*
|
|
|
|
* BLOCKING
|
2005-02-09 08:06:40 -05:00
|
|
|
*/
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
static
|
|
|
|
CURLcode ftp_sendquote(struct connectdata *conn, struct curl_slist *quote)
|
|
|
|
{
|
|
|
|
struct curl_slist *item;
|
|
|
|
ssize_t nread;
|
|
|
|
int ftpcode;
|
|
|
|
CURLcode result;
|
2010-07-17 18:44:25 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
|
|
|
struct pingpong *pp = &ftpc->pp;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
item = quote;
|
2007-11-05 04:45:09 -05:00
|
|
|
while(item) {
|
|
|
|
if(item->data) {
|
2009-07-27 14:36:56 -04:00
|
|
|
char *cmd = item->data;
|
|
|
|
bool acceptfail = FALSE;
|
|
|
|
|
|
|
|
/* if a command starts with an asterisk, which a legal FTP command never
|
|
|
|
can, the command will be allowed to fail without it causing any
|
|
|
|
aborts or cancels etc. It will cause libcurl to act as if the command
|
|
|
|
is successful, whatever the server reponds. */
|
|
|
|
|
|
|
|
if(cmd[0] == '*') {
|
|
|
|
cmd++;
|
|
|
|
acceptfail = TRUE;
|
|
|
|
}
|
|
|
|
|
2013-03-29 16:19:45 -04:00
|
|
|
PPSENDF(&conn->proto.ftpc.pp, "%s", cmd);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2017-10-25 05:59:43 -04:00
|
|
|
pp->response = Curl_now(); /* timeout relative now */
|
2010-07-17 18:44:25 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(result)
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2009-07-27 14:36:56 -04:00
|
|
|
if(!acceptfail && (ftpcode >= 400)) {
|
|
|
|
failf(conn->data, "QUOT string not accepted: %s", cmd);
|
2007-08-30 16:34:57 -04:00
|
|
|
return CURLE_QUOTE_ERROR;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
item = item->next;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2006-08-19 17:18:36 -04:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* ftp_need_type()
|
|
|
|
*
|
|
|
|
* Returns TRUE if we in the current situation should send TYPE
|
|
|
|
*/
|
|
|
|
static int ftp_need_type(struct connectdata *conn,
|
|
|
|
bool ascii_wanted)
|
|
|
|
{
|
2006-09-07 17:49:20 -04:00
|
|
|
return conn->proto.ftpc.transfertype != (ascii_wanted?'A':'I');
|
2006-08-19 17:18:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* ftp_nb_type()
|
|
|
|
*
|
|
|
|
* Set TYPE. We only deal with ASCII or BINARY so this function
|
|
|
|
* sets one of them.
|
|
|
|
* If the transfer type is not sent, simulate on OK response in newstate
|
|
|
|
*/
|
|
|
|
static CURLcode ftp_nb_type(struct connectdata *conn,
|
|
|
|
bool ascii, ftpstate newstate)
|
|
|
|
{
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2006-08-19 17:18:36 -04:00
|
|
|
CURLcode result;
|
2007-10-02 11:26:30 -04:00
|
|
|
char want = (char)(ascii?'A':'I');
|
2006-08-19 17:18:36 -04:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(ftpc->transfertype == want) {
|
2006-08-19 17:18:36 -04:00
|
|
|
state(conn, newstate);
|
|
|
|
return ftp_state_type_resp(conn, 200, newstate);
|
|
|
|
}
|
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
PPSENDF(&ftpc->pp, "TYPE %c", want);
|
2006-08-19 17:18:36 -04:00
|
|
|
state(conn, newstate);
|
|
|
|
|
|
|
|
/* keep track of our current transfer type */
|
2007-08-27 02:31:28 -04:00
|
|
|
ftpc->transfertype = want;
|
2006-08-19 17:18:36 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* ftp_pasv_verbose()
|
|
|
|
*
|
|
|
|
* This function only outputs some informationals about this second connection
|
|
|
|
* when we've issued a PASV command before and thus we have connected to a
|
|
|
|
* possibly new IP address.
|
|
|
|
*
|
|
|
|
*/
|
2007-03-24 22:30:58 -04:00
|
|
|
#ifndef CURL_DISABLE_VERBOSE_STRINGS
|
2005-02-09 08:06:40 -05:00
|
|
|
static void
|
|
|
|
ftp_pasv_verbose(struct connectdata *conn,
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *ai,
|
2005-02-09 08:06:40 -05:00
|
|
|
char *newhost, /* ascii version */
|
|
|
|
int port)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
Curl_printable_address(ai, buf, sizeof(buf));
|
|
|
|
infof(conn->data, "Connecting to %s (%s) port %d\n", newhost, buf, port);
|
|
|
|
}
|
2007-03-24 22:30:58 -04:00
|
|
|
#endif
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/*
|
2011-12-20 06:52:24 -05:00
|
|
|
* ftp_do_more()
|
2005-02-09 08:06:40 -05:00
|
|
|
*
|
|
|
|
* This function shall be called when the second FTP (data) connection is
|
|
|
|
* connected.
|
2013-08-05 17:49:53 -04:00
|
|
|
*
|
|
|
|
* 'complete' can return 0 for incomplete, 1 for done and -1 for go back
|
|
|
|
* (which basically is only for when PASV is being sent to retry a failed
|
|
|
|
* EPSV).
|
2005-02-09 08:06:40 -05:00
|
|
|
*/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2013-08-05 17:49:53 -04:00
|
|
|
static CURLcode ftp_do_more(struct connectdata *conn, int *completep)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2007-10-22 11:05:35 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2011-12-20 06:52:24 -05:00
|
|
|
bool connected = FALSE;
|
2013-08-05 17:49:53 -04:00
|
|
|
bool complete = FALSE;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2007-12-08 17:50:55 -05:00
|
|
|
/* the ftp struct is inited in ftp_connect() */
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = data->req.protop;
|
2003-12-01 05:14:39 -05:00
|
|
|
|
2011-12-20 06:52:24 -05:00
|
|
|
/* if the second connection isn't done yet, wait for it */
|
|
|
|
if(!conn->bits.tcpconnect[SECONDARYSOCKET]) {
|
2017-06-07 17:02:26 -04:00
|
|
|
if(Curl_connect_ongoing(conn)) {
|
2013-01-17 06:59:23 -05:00
|
|
|
/* As we're in TUNNEL_CONNECT state now, we know the proxy name and port
|
2019-05-14 10:36:15 -04:00
|
|
|
aren't used so we blank their arguments. */
|
2017-04-25 18:34:22 -04:00
|
|
|
result = Curl_proxyCONNECT(conn, SECONDARYSOCKET, NULL, 0);
|
2013-01-17 06:59:23 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-12-20 06:52:24 -05:00
|
|
|
result = Curl_is_connected(conn, SECONDARYSOCKET, &connected);
|
|
|
|
|
|
|
|
/* Ready to do more? */
|
|
|
|
if(connected) {
|
|
|
|
DEBUGF(infof(data, "DO-MORE connected phase starts\n"));
|
|
|
|
}
|
2013-08-05 17:49:53 -04:00
|
|
|
else {
|
|
|
|
if(result && (ftpc->count1 == 0)) {
|
|
|
|
*completep = -1; /* go back to DOING please */
|
|
|
|
/* this is a EPSV connect failing, try PASV instead */
|
|
|
|
return ftp_epsv_disable(conn);
|
|
|
|
}
|
2011-12-20 06:52:24 -05:00
|
|
|
return result;
|
2013-08-05 17:49:53 -04:00
|
|
|
}
|
2011-12-20 06:52:24 -05:00
|
|
|
}
|
2003-12-03 03:26:31 -05:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
result = Curl_proxy_connect(conn, SECONDARYSOCKET);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if(CONNECT_SECONDARYSOCKET_PROXY_SSL())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
|
2017-06-07 17:02:26 -04:00
|
|
|
Curl_connect_ongoing(conn))
|
2016-11-16 12:49:15 -05:00
|
|
|
return result;
|
|
|
|
|
|
|
|
|
2013-01-17 06:59:23 -05:00
|
|
|
if(ftpc->state) {
|
2017-03-26 11:02:22 -04:00
|
|
|
/* already in a state so skip the initial commands.
|
2012-11-04 12:22:48 -05:00
|
|
|
They are only done to kickstart the do_more state */
|
2013-08-05 17:49:53 -04:00
|
|
|
result = ftp_multi_statemach(conn, &complete);
|
|
|
|
|
|
|
|
*completep = (int)complete;
|
2012-11-04 12:22:48 -05:00
|
|
|
|
|
|
|
/* if we got an error or if we don't wait for a data connection return
|
|
|
|
immediately */
|
2019-09-19 03:16:41 -04:00
|
|
|
if(result || !ftpc->wait_data_conn)
|
2012-11-04 12:22:48 -05:00
|
|
|
return result;
|
2013-01-17 06:59:23 -05:00
|
|
|
|
2019-09-19 03:16:41 -04:00
|
|
|
/* if we reach the end of the FTP state machine here, *complete will be
|
|
|
|
TRUE but so is ftpc->wait_data_conn, which says we need to wait for the
|
|
|
|
data connection and therefore we're not actually complete */
|
|
|
|
*completep = 0;
|
2012-11-04 12:22:48 -05:00
|
|
|
}
|
|
|
|
|
2007-09-26 08:00:01 -04:00
|
|
|
if(ftp->transfer <= FTPTRANSFER_INFO) {
|
|
|
|
/* a transfer is about to take place, or if not a file name was given
|
|
|
|
so we'll do a SIZE on it later and then we need the right TYPE first */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2011-12-20 07:06:26 -05:00
|
|
|
if(ftpc->wait_data_conn == TRUE) {
|
2011-12-19 08:35:20 -05:00
|
|
|
bool serv_conned;
|
|
|
|
|
|
|
|
result = ReceivedServerConnect(conn, &serv_conned);
|
|
|
|
if(result)
|
|
|
|
return result; /* Failed to accept data connection */
|
|
|
|
|
|
|
|
if(serv_conned) {
|
|
|
|
/* It looks data connection is established */
|
|
|
|
result = AcceptServerConnect(conn);
|
2011-12-20 07:06:26 -05:00
|
|
|
ftpc->wait_data_conn = FALSE;
|
2012-07-13 08:12:39 -04:00
|
|
|
if(!result)
|
2011-12-19 08:35:20 -05:00
|
|
|
result = InitiateTransfer(conn);
|
2012-07-13 08:12:39 -04:00
|
|
|
|
|
|
|
if(result)
|
|
|
|
return result;
|
2013-07-19 17:02:30 -04:00
|
|
|
|
2013-08-05 17:49:53 -04:00
|
|
|
*completep = 1; /* this state is now complete when the server has
|
|
|
|
connected back to us */
|
2011-12-19 08:35:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(data->set.upload) {
|
2007-09-26 08:00:01 -04:00
|
|
|
result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_STOR_TYPE);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(result)
|
2006-08-19 17:18:36 -04:00
|
|
|
return result;
|
2013-01-17 06:59:23 -05:00
|
|
|
|
2013-08-05 17:49:53 -04:00
|
|
|
result = ftp_multi_statemach(conn, &complete);
|
2019-09-19 03:18:26 -04:00
|
|
|
/* ftpc->wait_data_conn is always false here */
|
|
|
|
*completep = (int)complete;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
else {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* download */
|
|
|
|
ftp->downloadsize = -1; /* unknown as of yet */
|
|
|
|
|
2018-01-29 04:50:43 -05:00
|
|
|
result = Curl_range(conn);
|
|
|
|
|
|
|
|
if(result == CURLE_OK && data->req.maxdownload >= 0) {
|
|
|
|
/* Don't check for successful transfer */
|
|
|
|
ftpc->dont_check = TRUE;
|
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
;
|
2007-10-22 11:05:35 -04:00
|
|
|
else if(data->set.ftp_list_only || !ftpc->file) {
|
2005-02-09 08:06:40 -05:00
|
|
|
/* The specified path ends with a slash, and therefore we think this
|
|
|
|
is a directory that is requested, use LIST. But before that we
|
|
|
|
need to set ASCII transfer mode. */
|
2007-09-26 08:00:01 -04:00
|
|
|
|
|
|
|
/* But only if a body transfer was requested. */
|
|
|
|
if(ftp->transfer == FTPTRANSFER_BODY) {
|
2009-08-11 16:43:12 -04:00
|
|
|
result = ftp_nb_type(conn, TRUE, FTP_LIST_TYPE);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(result)
|
2007-09-26 08:00:01 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/* otherwise just fall through */
|
2003-05-23 07:14:09 -04:00
|
|
|
}
|
|
|
|
else {
|
2006-08-19 17:18:36 -04:00
|
|
|
result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_RETR_TYPE);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(result)
|
2006-08-19 17:18:36 -04:00
|
|
|
return result;
|
2003-05-23 07:14:09 -04:00
|
|
|
}
|
2012-11-04 12:22:48 -05:00
|
|
|
|
2013-08-05 17:49:53 -04:00
|
|
|
result = ftp_multi_statemach(conn, &complete);
|
|
|
|
*completep = (int)complete;
|
2012-11-04 12:22:48 -05:00
|
|
|
}
|
2013-01-17 06:59:23 -05:00
|
|
|
return result;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2019-09-19 03:19:44 -04:00
|
|
|
/* no data to transfer */
|
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2011-12-20 07:06:26 -05:00
|
|
|
if(!ftpc->wait_data_conn) {
|
2011-12-20 06:52:24 -05:00
|
|
|
/* no waiting for the data connection so this is now complete */
|
2013-08-05 17:49:53 -04:00
|
|
|
*completep = 1;
|
2011-12-20 06:52:24 -05:00
|
|
|
DEBUGF(infof(data, "DO-MORE phase ends with %d\n", (int)result));
|
|
|
|
}
|
2000-01-10 18:36:14 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
|
2002-08-12 05:43:20 -04:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* ftp_perform()
|
|
|
|
*
|
|
|
|
* This is the actual DO function for FTP. Get a file/directory according to
|
|
|
|
* the options previously setup.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static
|
|
|
|
CURLcode ftp_perform(struct connectdata *conn,
|
2005-02-09 08:06:40 -05:00
|
|
|
bool *connected, /* connect status after PASV / PORT */
|
|
|
|
bool *dophase_done)
|
2002-08-12 05:43:20 -04:00
|
|
|
{
|
|
|
|
/* this is FTP and no proxy */
|
2017-09-09 17:09:06 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2002-08-12 05:43:20 -04:00
|
|
|
|
2005-05-07 16:41:16 -04:00
|
|
|
DEBUGF(infof(conn->data, "DO phase starts\n"));
|
2002-08-12 05:43:20 -04:00
|
|
|
|
2008-01-31 07:04:33 -05:00
|
|
|
if(conn->data->set.opt_no_body) {
|
2007-06-24 15:32:33 -04:00
|
|
|
/* requested no body means no transfer... */
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
2007-09-26 08:00:01 -04:00
|
|
|
ftp->transfer = FTPTRANSFER_INFO;
|
2007-06-24 15:32:33 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
*dophase_done = FALSE; /* not done yet */
|
2003-08-08 05:13:19 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* start the first command in the DO phase */
|
|
|
|
result = ftp_state_quote(conn, TRUE, FTP_QUOTE);
|
|
|
|
if(result)
|
2004-10-16 10:06:54 -04:00
|
|
|
return result;
|
2002-08-12 05:43:20 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* run the state-machine */
|
2013-01-17 06:59:23 -05:00
|
|
|
result = ftp_multi_statemach(conn, dophase_done);
|
|
|
|
|
2013-08-05 17:49:53 -04:00
|
|
|
*connected = conn->bits.tcpconnect[SECONDARYSOCKET];
|
|
|
|
|
|
|
|
infof(conn->data, "ftp_perform ends with SECONDARY: %d\n", *connected);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
|
|
|
if(*dophase_done)
|
2013-01-17 06:59:23 -05:00
|
|
|
DEBUGF(infof(conn->data, "DO phase is complete1\n"));
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2002-08-12 05:43:20 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-05-31 10:24:01 -04:00
|
|
|
static void wc_data_dtor(void *ptr)
|
2010-05-12 09:33:22 -04:00
|
|
|
{
|
2018-04-20 04:29:44 -04:00
|
|
|
struct ftp_wc *ftpwc = ptr;
|
2018-04-19 09:47:05 -04:00
|
|
|
if(ftpwc && ftpwc->parser)
|
2018-04-20 04:29:44 -04:00
|
|
|
Curl_ftp_parselist_data_free(&ftpwc->parser);
|
|
|
|
free(ftpwc);
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode init_wc_data(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
char *last_slash;
|
2018-09-14 17:33:28 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
|
|
|
char *path = ftp->path;
|
2010-05-12 09:33:22 -04:00
|
|
|
struct WildcardData *wildcard = &(conn->data->wildcard);
|
2014-10-23 16:56:35 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2018-04-19 09:47:05 -04:00
|
|
|
struct ftp_wc *ftpwc = NULL;
|
2010-05-12 09:33:22 -04:00
|
|
|
|
2018-09-14 17:33:28 -04:00
|
|
|
last_slash = strrchr(ftp->path, '/');
|
2010-05-12 09:33:22 -04:00
|
|
|
if(last_slash) {
|
|
|
|
last_slash++;
|
|
|
|
if(last_slash[0] == '\0') {
|
|
|
|
wildcard->state = CURLWC_CLEAN;
|
2014-10-23 16:56:35 -04:00
|
|
|
result = ftp_parse_url_path(conn);
|
|
|
|
return result;
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
wildcard->pattern = strdup(last_slash);
|
|
|
|
if(!wildcard->pattern)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
last_slash[0] = '\0'; /* cut file from path */
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
else { /* there is only 'wildcard pattern' or nothing */
|
|
|
|
if(path[0]) {
|
|
|
|
wildcard->pattern = strdup(path);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!wildcard->pattern)
|
2010-05-12 09:33:22 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
path[0] = '\0';
|
|
|
|
}
|
|
|
|
else { /* only list */
|
2010-06-24 04:10:29 -04:00
|
|
|
wildcard->state = CURLWC_CLEAN;
|
2014-10-23 16:56:35 -04:00
|
|
|
result = ftp_parse_url_path(conn);
|
|
|
|
return result;
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* program continues only if URL is not ending with slash, allocate needed
|
|
|
|
resources for wildcard transfer */
|
|
|
|
|
2018-04-20 04:29:44 -04:00
|
|
|
/* allocate ftp protocol specific wildcard data */
|
|
|
|
ftpwc = calloc(1, sizeof(struct ftp_wc));
|
|
|
|
if(!ftpwc) {
|
2018-04-19 09:47:05 -04:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* INITIALIZE parselist structure */
|
2018-04-20 04:29:44 -04:00
|
|
|
ftpwc->parser = Curl_ftp_parselist_data_alloc();
|
|
|
|
if(!ftpwc->parser) {
|
2018-04-19 09:47:05 -04:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
2010-06-17 13:56:25 -04:00
|
|
|
}
|
2010-05-12 09:33:22 -04:00
|
|
|
|
2018-04-20 04:29:44 -04:00
|
|
|
wildcard->protdata = ftpwc; /* put it to the WildcardData tmp pointer */
|
|
|
|
wildcard->dtor = wc_data_dtor;
|
2010-05-12 09:33:22 -04:00
|
|
|
|
|
|
|
/* wildcard does not support NOCWD option (assert it?) */
|
|
|
|
if(conn->data->set.ftp_filemethod == FTPFILE_NOCWD)
|
|
|
|
conn->data->set.ftp_filemethod = FTPFILE_MULTICWD;
|
|
|
|
|
|
|
|
/* try to parse ftp url */
|
2014-10-23 16:56:35 -04:00
|
|
|
result = ftp_parse_url_path(conn);
|
|
|
|
if(result) {
|
2018-04-19 09:47:05 -04:00
|
|
|
goto fail;
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
|
2018-09-14 17:33:28 -04:00
|
|
|
wildcard->path = strdup(ftp->path);
|
2012-04-04 11:30:12 -04:00
|
|
|
if(!wildcard->path) {
|
2018-04-19 09:47:05 -04:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
2012-04-04 11:30:12 -04:00
|
|
|
}
|
|
|
|
|
2010-05-12 09:33:22 -04:00
|
|
|
/* backup old write_function */
|
2018-04-20 04:29:44 -04:00
|
|
|
ftpwc->backup.write_function = conn->data->set.fwrite_func;
|
2011-04-20 09:17:42 -04:00
|
|
|
/* parsing write function */
|
2010-06-09 09:45:46 -04:00
|
|
|
conn->data->set.fwrite_func = Curl_ftp_parselist;
|
2010-05-12 09:33:22 -04:00
|
|
|
/* backup old file descriptor */
|
2018-04-20 04:29:44 -04:00
|
|
|
ftpwc->backup.file_descriptor = conn->data->set.out;
|
2010-05-12 09:33:22 -04:00
|
|
|
/* let the writefunc callback know what curl pointer is working with */
|
|
|
|
conn->data->set.out = conn;
|
|
|
|
|
|
|
|
infof(conn->data, "Wildcard - Parsing started\n");
|
|
|
|
return CURLE_OK;
|
2018-04-19 09:47:05 -04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if(ftpwc) {
|
|
|
|
Curl_ftp_parselist_data_free(&ftpwc->parser);
|
|
|
|
free(ftpwc);
|
|
|
|
}
|
|
|
|
Curl_safefree(wildcard->pattern);
|
|
|
|
wildcard->dtor = ZERO_NULL;
|
|
|
|
wildcard->protdata = NULL;
|
|
|
|
return result;
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
|
2010-10-20 21:15:14 -04:00
|
|
|
/* This is called recursively */
|
2010-05-12 09:33:22 -04:00
|
|
|
static CURLcode wc_statemach(struct connectdata *conn)
|
|
|
|
{
|
2010-10-20 21:15:14 -04:00
|
|
|
struct WildcardData * const wildcard = &(conn->data->wildcard);
|
2014-10-23 16:56:35 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2010-10-20 21:15:14 -04:00
|
|
|
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(wildcard->state) {
|
2010-05-12 09:33:22 -04:00
|
|
|
case CURLWC_INIT:
|
2014-10-23 16:56:35 -04:00
|
|
|
result = init_wc_data(conn);
|
2010-05-12 09:33:22 -04:00
|
|
|
if(wildcard->state == CURLWC_CLEAN)
|
|
|
|
/* only listing! */
|
|
|
|
break;
|
2017-03-10 08:28:37 -05:00
|
|
|
wildcard->state = result ? CURLWC_ERROR : CURLWC_MATCHING;
|
2010-05-12 09:33:22 -04:00
|
|
|
break;
|
|
|
|
|
2010-10-20 21:15:14 -04:00
|
|
|
case CURLWC_MATCHING: {
|
2010-05-12 09:33:22 -04:00
|
|
|
/* In this state is LIST response successfully parsed, so lets restore
|
|
|
|
previous WRITEFUNCTION callback and WRITEDATA pointer */
|
2018-04-20 04:29:44 -04:00
|
|
|
struct ftp_wc *ftpwc = wildcard->protdata;
|
|
|
|
conn->data->set.fwrite_func = ftpwc->backup.write_function;
|
|
|
|
conn->data->set.out = ftpwc->backup.file_descriptor;
|
|
|
|
ftpwc->backup.write_function = ZERO_NULL;
|
|
|
|
ftpwc->backup.file_descriptor = NULL;
|
2010-05-12 09:33:22 -04:00
|
|
|
wildcard->state = CURLWC_DOWNLOADING;
|
|
|
|
|
2018-04-20 04:29:44 -04:00
|
|
|
if(Curl_ftp_parselist_geterror(ftpwc->parser)) {
|
2010-05-12 09:33:22 -04:00
|
|
|
/* error found in LIST parsing */
|
|
|
|
wildcard->state = CURLWC_CLEAN;
|
|
|
|
return wc_statemach(conn);
|
|
|
|
}
|
2017-04-03 04:32:43 -04:00
|
|
|
if(wildcard->filelist.size == 0) {
|
2010-05-12 09:33:22 -04:00
|
|
|
/* no corresponding file */
|
|
|
|
wildcard->state = CURLWC_CLEAN;
|
|
|
|
return CURLE_REMOTE_FILE_NOT_FOUND;
|
|
|
|
}
|
2010-10-20 21:15:14 -04:00
|
|
|
return wc_statemach(conn);
|
2010-12-02 10:35:02 -05:00
|
|
|
}
|
2010-05-12 09:33:22 -04:00
|
|
|
|
|
|
|
case CURLWC_DOWNLOADING: {
|
|
|
|
/* filelist has at least one file, lets get first one */
|
2010-10-20 21:15:14 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2017-04-03 04:32:43 -04:00
|
|
|
struct curl_fileinfo *finfo = wildcard->filelist.head->ptr;
|
2018-09-14 17:33:28 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
2013-03-06 07:27:51 -05:00
|
|
|
|
|
|
|
char *tmp_path = aprintf("%s%s", wildcard->path, finfo->filename);
|
|
|
|
if(!tmp_path)
|
2010-05-12 09:33:22 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2018-09-14 17:33:28 -04:00
|
|
|
/* switch default ftp->path and tmp_path */
|
|
|
|
free(ftp->pathalloc);
|
|
|
|
ftp->pathalloc = ftp->path = tmp_path;
|
2010-05-12 09:33:22 -04:00
|
|
|
|
|
|
|
infof(conn->data, "Wildcard - START of \"%s\"\n", finfo->filename);
|
|
|
|
if(conn->data->set.chunk_bgn) {
|
2018-02-10 09:13:15 -05:00
|
|
|
long userresponse;
|
|
|
|
Curl_set_in_callback(conn->data, true);
|
|
|
|
userresponse = conn->data->set.chunk_bgn(
|
2017-04-03 04:32:43 -04:00
|
|
|
finfo, wildcard->customptr, (int)wildcard->filelist.size);
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(conn->data, false);
|
2010-05-12 09:33:22 -04:00
|
|
|
switch(userresponse) {
|
|
|
|
case CURL_CHUNK_BGN_FUNC_SKIP:
|
|
|
|
infof(conn->data, "Wildcard - \"%s\" skipped by user\n",
|
|
|
|
finfo->filename);
|
|
|
|
wildcard->state = CURLWC_SKIP;
|
|
|
|
return wc_statemach(conn);
|
|
|
|
case CURL_CHUNK_BGN_FUNC_FAIL:
|
|
|
|
return CURLE_CHUNK_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(finfo->filetype != CURLFILETYPE_FILE) {
|
|
|
|
wildcard->state = CURLWC_SKIP;
|
|
|
|
return wc_statemach(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(finfo->flags & CURLFINFOFLAG_KNOWN_SIZE)
|
|
|
|
ftpc->known_filesize = finfo->size;
|
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
result = ftp_parse_url_path(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2010-05-12 09:33:22 -04:00
|
|
|
|
|
|
|
/* we don't need the Curl_fileinfo of first file anymore */
|
2017-04-03 04:32:43 -04:00
|
|
|
Curl_llist_remove(&wildcard->filelist, wildcard->filelist.head, NULL);
|
2010-05-12 09:33:22 -04:00
|
|
|
|
2017-04-03 04:32:43 -04:00
|
|
|
if(wildcard->filelist.size == 0) { /* remains only one file to down. */
|
2010-05-12 09:33:22 -04:00
|
|
|
wildcard->state = CURLWC_CLEAN;
|
|
|
|
/* after that will be ftp_do called once again and no transfer
|
|
|
|
will be done because of CURLWC_CLEAN state */
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case CURLWC_SKIP: {
|
2018-02-10 09:13:15 -05:00
|
|
|
if(conn->data->set.chunk_end) {
|
|
|
|
Curl_set_in_callback(conn->data, true);
|
2010-05-12 09:33:22 -04:00
|
|
|
conn->data->set.chunk_end(conn->data->wildcard.customptr);
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(conn->data, false);
|
|
|
|
}
|
2017-04-03 04:32:43 -04:00
|
|
|
Curl_llist_remove(&wildcard->filelist, wildcard->filelist.head, NULL);
|
|
|
|
wildcard->state = (wildcard->filelist.size == 0) ?
|
2010-05-12 09:33:22 -04:00
|
|
|
CURLWC_CLEAN : CURLWC_DOWNLOADING;
|
2010-10-20 21:15:14 -04:00
|
|
|
return wc_statemach(conn);
|
2010-12-02 10:35:02 -05:00
|
|
|
}
|
2010-05-12 09:33:22 -04:00
|
|
|
|
2010-10-20 21:15:14 -04:00
|
|
|
case CURLWC_CLEAN: {
|
2018-04-20 04:29:44 -04:00
|
|
|
struct ftp_wc *ftpwc = wildcard->protdata;
|
2014-10-23 16:56:35 -04:00
|
|
|
result = CURLE_OK;
|
2018-04-20 04:29:44 -04:00
|
|
|
if(ftpwc)
|
|
|
|
result = Curl_ftp_parselist_geterror(ftpwc->parser);
|
2014-10-23 16:56:35 -04:00
|
|
|
|
|
|
|
wildcard->state = result ? CURLWC_ERROR : CURLWC_DONE;
|
2010-10-20 21:15:14 -04:00
|
|
|
} break;
|
2010-05-12 09:33:22 -04:00
|
|
|
|
|
|
|
case CURLWC_DONE:
|
|
|
|
case CURLWC_ERROR:
|
2017-04-03 04:32:43 -04:00
|
|
|
case CURLWC_CLEAR:
|
2018-04-19 09:47:05 -04:00
|
|
|
if(wildcard->dtor)
|
|
|
|
wildcard->dtor(wildcard->protdata);
|
2010-05-12 09:33:22 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
return result;
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
|
2001-11-01 07:47:22 -05:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2007-12-08 17:50:55 -05:00
|
|
|
* ftp_do()
|
2001-11-01 07:47:22 -05:00
|
|
|
*
|
|
|
|
* This function is registered as 'curl_do' function. It decodes the path
|
|
|
|
* parts etc as a wrapper to the actual DO function (ftp_perform).
|
|
|
|
*
|
|
|
|
* The input argument is already checked for validity.
|
|
|
|
*/
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_do(struct connectdata *conn, bool *done)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2014-10-23 16:56:35 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2011-12-20 07:06:26 -05:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2003-07-19 20:18:11 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
*done = FALSE; /* default to false */
|
2011-12-20 07:06:26 -05:00
|
|
|
ftpc->wait_data_conn = FALSE; /* default to no such wait */
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2017-10-25 17:51:50 -04:00
|
|
|
if(conn->data->state.wildcardmatch) {
|
2014-10-23 16:56:35 -04:00
|
|
|
result = wc_statemach(conn);
|
2010-05-12 09:33:22 -04:00
|
|
|
if(conn->data->wildcard.state == CURLWC_SKIP ||
|
|
|
|
conn->data->wildcard.state == CURLWC_DONE) {
|
|
|
|
/* do not call ftp_regular_transfer */
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result) /* error, loop or skipping the file */
|
|
|
|
return result;
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
|
|
|
else { /* no wildcard FSM needed */
|
2014-10-23 16:56:35 -04:00
|
|
|
result = ftp_parse_url_path(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2010-05-12 09:33:22 -04:00
|
|
|
}
|
2004-10-16 10:06:54 -04:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
result = ftp_regular_transfer(conn, done);
|
2004-04-22 09:09:00 -04:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
return result;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2016-10-08 07:39:29 -04:00
|
|
|
CURLcode Curl_ftpsend(struct connectdata *conn, const char *cmd)
|
2001-01-05 05:11:41 -05:00
|
|
|
{
|
2002-01-16 09:46:00 -05:00
|
|
|
ssize_t bytes_written;
|
2009-12-12 16:54:01 -05:00
|
|
|
#define SBUF_SIZE 1024
|
2007-07-01 18:01:18 -04:00
|
|
|
char s[SBUF_SIZE];
|
2004-03-03 08:24:14 -05:00
|
|
|
size_t write_len;
|
2017-09-09 17:09:06 -04:00
|
|
|
char *sptr = s;
|
2014-10-23 16:56:35 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2013-08-23 10:16:42 -04:00
|
|
|
#ifdef HAVE_GSSAPI
|
2007-07-01 18:01:18 -04:00
|
|
|
enum protection_level data_sec = conn->data_prot;
|
|
|
|
#endif
|
2001-01-05 05:11:41 -05:00
|
|
|
|
2018-09-13 04:10:18 -04:00
|
|
|
if(!cmd)
|
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
|
2016-10-08 07:39:29 -04:00
|
|
|
write_len = strlen(cmd);
|
2018-09-13 04:10:18 -04:00
|
|
|
if(!write_len || write_len > (sizeof(s) -3))
|
2016-10-08 07:39:29 -04:00
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2018-09-13 04:10:18 -04:00
|
|
|
memcpy(&s, cmd, write_len);
|
2013-03-06 07:27:51 -05:00
|
|
|
strcpy(&s[write_len], "\r\n"); /* append a trailing CRLF */
|
2017-09-09 17:09:06 -04:00
|
|
|
write_len += 2;
|
|
|
|
bytes_written = 0;
|
2001-03-29 03:16:55 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
result = Curl_convert_to_network(conn->data, s, write_len);
|
2006-04-07 17:50:47 -04:00
|
|
|
/* Curl_convert_to_network calls failf if unsuccessful */
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result)
|
2015-03-17 08:05:01 -04:00
|
|
|
return result;
|
2006-04-07 17:50:47 -04:00
|
|
|
|
2010-02-02 11:25:07 -05:00
|
|
|
for(;;) {
|
2013-08-23 10:16:42 -04:00
|
|
|
#ifdef HAVE_GSSAPI
|
2010-11-13 17:42:34 -05:00
|
|
|
conn->data_prot = PROT_CMD;
|
2007-07-01 18:01:18 -04:00
|
|
|
#endif
|
2014-10-23 16:56:35 -04:00
|
|
|
result = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
|
|
|
|
&bytes_written);
|
2013-08-23 10:16:42 -04:00
|
|
|
#ifdef HAVE_GSSAPI
|
2010-11-13 17:42:34 -05:00
|
|
|
DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
|
2007-07-01 18:01:18 -04:00
|
|
|
conn->data_prot = data_sec;
|
|
|
|
#endif
|
2002-01-16 09:46:00 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(result)
|
2002-01-16 09:46:00 -05:00
|
|
|
break;
|
|
|
|
|
2002-04-12 03:21:11 -04:00
|
|
|
if(conn->data->set.verbose)
|
2018-06-11 18:10:43 -04:00
|
|
|
Curl_debug(conn->data, CURLINFO_HEADER_OUT, sptr, (size_t)bytes_written);
|
2002-04-12 03:21:11 -04:00
|
|
|
|
2004-03-03 08:24:14 -05:00
|
|
|
if(bytes_written != (ssize_t)write_len) {
|
2002-01-16 09:46:00 -05:00
|
|
|
write_len -= bytes_written;
|
|
|
|
sptr += bytes_written;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2004-01-29 08:56:45 -05:00
|
|
|
}
|
2002-01-16 09:46:00 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
return result;
|
2001-01-05 05:11:41 -05:00
|
|
|
}
|
|
|
|
|
2004-01-21 03:50:08 -05:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2004-04-22 09:09:00 -04:00
|
|
|
* ftp_quit()
|
2004-01-21 03:50:08 -05:00
|
|
|
*
|
|
|
|
* This should be called before calling sclose() on an ftp control connection
|
2004-06-03 07:41:05 -04:00
|
|
|
* (not data connections). We should then wait for the response from the
|
2004-01-21 03:50:08 -05:00
|
|
|
* server before returning. The calling code should then try to close the
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
*/
|
2004-04-22 09:09:00 -04:00
|
|
|
static CURLcode ftp_quit(struct connectdata *conn)
|
2004-01-21 03:50:08 -05:00
|
|
|
{
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2004-01-21 03:50:08 -05:00
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
if(conn->proto.ftpc.ctl_valid) {
|
2013-07-24 11:06:28 -04:00
|
|
|
result = Curl_pp_sendf(&conn->proto.ftpc.pp, "%s", "QUIT");
|
2011-10-24 14:42:01 -04:00
|
|
|
if(result) {
|
|
|
|
failf(conn->data, "Failure sending QUIT command: %s",
|
|
|
|
curl_easy_strerror(result));
|
|
|
|
conn->proto.ftpc.ctl_valid = FALSE; /* mark control connection as bad */
|
2014-05-20 04:32:23 -04:00
|
|
|
connclose(conn, "QUIT command failed"); /* mark for connection closure */
|
2011-10-24 14:42:01 -04:00
|
|
|
state(conn, FTP_STOP);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
state(conn, FTP_QUIT);
|
|
|
|
|
2013-02-15 05:03:42 -05:00
|
|
|
result = ftp_block_statemach(conn);
|
2004-02-27 02:08:37 -05:00
|
|
|
}
|
2004-01-21 03:50:08 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
2004-01-21 03:50:08 -05:00
|
|
|
}
|
|
|
|
|
2001-11-01 07:47:22 -05:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2007-12-08 17:50:55 -05:00
|
|
|
* ftp_disconnect()
|
2001-11-01 07:47:22 -05:00
|
|
|
*
|
|
|
|
* Disconnect from an FTP server. Cleanup protocol-specific per-connection
|
2005-02-09 08:06:40 -05:00
|
|
|
* resources. BLOCKING.
|
2001-11-01 07:47:22 -05:00
|
|
|
*/
|
2010-11-19 07:43:20 -05:00
|
|
|
static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection)
|
2001-03-02 02:44:05 -05:00
|
|
|
{
|
2017-09-09 17:09:06 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2009-12-12 16:54:01 -05:00
|
|
|
struct pingpong *pp = &ftpc->pp;
|
2001-03-02 02:44:05 -05:00
|
|
|
|
2004-01-21 03:50:08 -05:00
|
|
|
/* We cannot send quit unconditionally. If this connection is stale or
|
|
|
|
bad in any way, sending quit and waiting around here will make the
|
|
|
|
disconnect wait in vain and cause more problems than we need to.
|
2004-02-27 02:08:37 -05:00
|
|
|
|
2004-04-22 09:09:00 -04:00
|
|
|
ftp_quit() will check the state of ftp->ctl_valid. If it's ok it
|
2004-02-27 02:08:37 -05:00
|
|
|
will try to send the QUIT command, otherwise it will just return.
|
2004-01-21 03:50:08 -05:00
|
|
|
*/
|
2010-11-19 07:43:20 -05:00
|
|
|
if(dead_connection)
|
|
|
|
ftpc->ctl_valid = FALSE;
|
2004-01-21 03:50:08 -05:00
|
|
|
|
2001-04-03 06:20:23 -04:00
|
|
|
/* The FTP session may or may not have been allocated/setup at this point! */
|
2007-10-24 17:14:11 -04:00
|
|
|
(void)ftp_quit(conn); /* ignore errors on the QUIT */
|
|
|
|
|
|
|
|
if(ftpc->entrypath) {
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(data->state.most_recent_ftp_entrypath == ftpc->entrypath) {
|
2007-10-24 17:14:11 -04:00
|
|
|
data->state.most_recent_ftp_entrypath = NULL;
|
2004-12-16 13:18:23 -05:00
|
|
|
}
|
2007-10-24 17:14:11 -04:00
|
|
|
free(ftpc->entrypath);
|
|
|
|
ftpc->entrypath = NULL;
|
|
|
|
}
|
2009-12-12 16:54:01 -05:00
|
|
|
|
2007-10-24 17:14:11 -04:00
|
|
|
freedirs(ftpc);
|
2015-03-11 12:41:01 -04:00
|
|
|
free(ftpc->prevpath);
|
|
|
|
ftpc->prevpath = NULL;
|
|
|
|
free(ftpc->server_os);
|
|
|
|
ftpc->server_os = NULL;
|
2007-10-24 17:14:11 -04:00
|
|
|
|
2009-12-12 16:54:01 -05:00
|
|
|
Curl_pp_disconnect(pp);
|
|
|
|
|
2013-08-23 10:16:42 -04:00
|
|
|
#ifdef HAVE_GSSAPI
|
2010-10-02 03:33:24 -04:00
|
|
|
Curl_sec_end(conn);
|
|
|
|
#endif
|
|
|
|
|
2001-03-02 02:44:05 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2001-09-04 22:49:04 -04:00
|
|
|
|
2004-06-03 07:41:05 -04:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
2004-10-16 10:06:54 -04:00
|
|
|
* ftp_parse_url_path()
|
2004-06-03 07:41:05 -04:00
|
|
|
*
|
2004-10-16 10:06:54 -04:00
|
|
|
* Parse the URL path into separate path components.
|
2004-06-03 07:41:05 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static
|
2004-10-16 10:06:54 -04:00
|
|
|
CURLcode ftp_parse_url_path(struct connectdata *conn)
|
2004-06-03 07:41:05 -04:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2006-09-07 17:49:20 -04:00
|
|
|
/* the ftp struct is already inited in ftp_connect() */
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = data->req.protop;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2019-09-25 11:48:53 -04:00
|
|
|
const char *slashPos = NULL;
|
|
|
|
const char *fileName = NULL;
|
2019-09-19 08:59:13 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2019-09-25 11:48:53 -04:00
|
|
|
char *rawPath = NULL; /* url-decoded "raw" path */
|
|
|
|
size_t pathLen = 0;
|
2006-09-07 17:49:20 -04:00
|
|
|
|
|
|
|
ftpc->ctl_valid = FALSE;
|
|
|
|
ftpc->cwdfail = FALSE;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
/* url-decode ftp path before further evaluation */
|
|
|
|
result = Curl_urldecode(data, ftp->path, 0, &rawPath, &pathLen, TRUE);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2019-09-25 11:48:53 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
switch(data->set.ftp_filemethod) {
|
|
|
|
case FTPFILE_NOCWD: /* fastest, but less standard-compliant */
|
2019-09-26 08:52:30 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
if((pathLen > 0) && (rawPath[pathLen - 1] != '/'))
|
|
|
|
fileName = rawPath; /* this is a full file path */
|
|
|
|
/*
|
|
|
|
else: ftpc->file is not used anywhere other than for operations on
|
|
|
|
a file. In other words, never for directory operations.
|
|
|
|
So we can safely leave filename as NULL here and use it as a
|
|
|
|
argument in dir/file decisions.
|
|
|
|
*/
|
2007-06-19 08:33:28 -04:00
|
|
|
break;
|
2013-04-12 09:29:28 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
case FTPFILE_SINGLECWD:
|
|
|
|
slashPos = strrchr(rawPath, '/');
|
|
|
|
if(slashPos) {
|
|
|
|
/* get path before last slash, except for / */
|
|
|
|
size_t dirlen = slashPos - rawPath;
|
|
|
|
if(dirlen == 0)
|
|
|
|
dirlen++;
|
|
|
|
|
|
|
|
ftpc->dirs = calloc(1, sizeof(ftpc->dirs[0]));
|
|
|
|
if(!ftpc->dirs) {
|
|
|
|
free(rawPath);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2005-11-28 18:06:00 -05:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
ftpc->dirs[0] = calloc(1, dirlen + 1);
|
|
|
|
if(!ftpc->dirs[0]) {
|
|
|
|
free(rawPath);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2013-04-12 09:29:28 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
strncpy(ftpc->dirs[0], rawPath, dirlen);
|
|
|
|
ftpc->dirdepth = 1; /* we consider it to be a single dir */
|
|
|
|
fileName = slashPos + 1; /* rest is file name */
|
2004-06-03 07:41:05 -04:00
|
|
|
}
|
2019-09-25 11:48:53 -04:00
|
|
|
else
|
|
|
|
fileName = rawPath; /* file name only (or empty) */
|
|
|
|
break;
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
default: /* allow pretty much anything */
|
|
|
|
case FTPFILE_MULTICWD: {
|
|
|
|
/* current position: begin of next path component */
|
|
|
|
const char *curPos = rawPath;
|
|
|
|
|
|
|
|
int dirAlloc = 0; /* number of entries allocated for the 'dirs' array */
|
|
|
|
const char *str = rawPath;
|
|
|
|
for(; *str != 0; ++str)
|
|
|
|
if (*str == '/')
|
|
|
|
++dirAlloc;
|
|
|
|
|
|
|
|
if(dirAlloc > 0) {
|
|
|
|
ftpc->dirs = calloc(dirAlloc, sizeof(ftpc->dirs[0]));
|
|
|
|
if(!ftpc->dirs) {
|
|
|
|
free(rawPath);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse the URL path into separate path components */
|
|
|
|
while((slashPos = strchr(curPos, '/')) != NULL) {
|
|
|
|
size_t compLen = slashPos - curPos;
|
|
|
|
|
|
|
|
/* path starts with a slash: add that as a directory */
|
|
|
|
if((compLen == 0) && (ftpc->dirdepth == 0))
|
|
|
|
++compLen;
|
2019-09-26 08:52:30 -04:00
|
|
|
|
|
|
|
/* we skip empty path components, like "x//y" since the FTP command
|
|
|
|
CWD requires a parameter and a non-existent parameter a) doesn't
|
|
|
|
work on many servers and b) has no effect on the others. */
|
2019-09-25 11:48:53 -04:00
|
|
|
if(compLen > 0) {
|
|
|
|
char *comp = calloc(1, compLen + 1);
|
|
|
|
if(!comp) {
|
|
|
|
free(rawPath);
|
2019-09-26 08:52:30 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2019-09-25 11:48:53 -04:00
|
|
|
strncpy(comp, curPos, compLen);
|
|
|
|
ftpc->dirs[ftpc->dirdepth++] = comp;
|
2019-09-26 08:52:30 -04:00
|
|
|
}
|
2019-09-25 11:48:53 -04:00
|
|
|
curPos = slashPos + 1;
|
2007-06-18 17:04:45 -04:00
|
|
|
}
|
2004-06-03 07:41:05 -04:00
|
|
|
}
|
2019-09-25 11:48:53 -04:00
|
|
|
DEBUGASSERT(ftpc->dirdepth <= dirAlloc);
|
|
|
|
fileName = curPos; /* the rest is the file name (or empty) */
|
2004-06-03 07:41:05 -04:00
|
|
|
}
|
2008-10-07 21:17:51 -04:00
|
|
|
break;
|
|
|
|
} /* switch */
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
if(fileName && *fileName)
|
|
|
|
ftpc->file = strdup(fileName);
|
2004-06-03 07:41:05 -04:00
|
|
|
else
|
2019-09-25 11:48:53 -04:00
|
|
|
ftpc->file = NULL; /* instead of point to a zero byte,
|
|
|
|
we make it a NULL pointer */
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2007-10-22 11:05:35 -04:00
|
|
|
if(data->set.upload && !ftpc->file && (ftp->transfer == FTPTRANSFER_BODY)) {
|
2006-03-03 08:09:30 -05:00
|
|
|
/* We need a file name when uploading. Return error! */
|
|
|
|
failf(data, "Uploading to a URL without a file name!");
|
2019-09-25 11:48:53 -04:00
|
|
|
free(rawPath);
|
2006-03-03 08:09:30 -05:00
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->cwddone = FALSE; /* default to not done */
|
2004-12-16 13:18:23 -05:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/'))
|
2019-09-19 08:59:13 -04:00
|
|
|
ftpc->cwddone = TRUE; /* skip CWD for absolute paths */
|
|
|
|
else { /* newly created FTP connections are already in entry path */
|
2019-09-25 11:48:53 -04:00
|
|
|
const char *oldPath = conn->bits.reuse ? ftpc->prevpath : "";
|
|
|
|
if(oldPath) {
|
|
|
|
size_t n = pathLen;
|
2019-09-19 08:59:13 -04:00
|
|
|
if(data->set.ftp_filemethod == FTPFILE_NOCWD)
|
2019-09-25 11:48:53 -04:00
|
|
|
n = 0; /* CWD to entry for relative paths */
|
2019-09-19 08:59:13 -04:00
|
|
|
else
|
2019-09-25 11:48:53 -04:00
|
|
|
n -= ftpc->file?strlen(ftpc->file):0;
|
2004-12-16 17:20:33 -05:00
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
if((strlen(oldPath) == n) && !strncmp(rawPath, oldPath, n)) {
|
2019-09-19 08:59:13 -04:00
|
|
|
infof(data, "Request has same path as previous transfer\n");
|
|
|
|
ftpc->cwddone = TRUE;
|
|
|
|
}
|
2004-11-25 17:21:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:48:53 -04:00
|
|
|
free(rawPath);
|
2007-04-10 20:25:41 -04:00
|
|
|
return CURLE_OK;
|
2004-10-16 10:06:54 -04:00
|
|
|
}
|
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
/* call this when the DO phase has completed */
|
|
|
|
static CURLcode ftp_dophase_done(struct connectdata *conn,
|
|
|
|
bool connected)
|
|
|
|
{
|
2013-08-05 04:32:08 -04:00
|
|
|
struct FTP *ftp = conn->data->req.protop;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2011-12-20 06:52:24 -05:00
|
|
|
if(connected) {
|
2013-08-05 17:49:53 -04:00
|
|
|
int completed;
|
2012-11-04 12:22:48 -05:00
|
|
|
CURLcode result = ftp_do_more(conn, &completed);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2012-11-04 12:22:48 -05:00
|
|
|
if(result) {
|
2016-03-07 18:07:14 -05:00
|
|
|
close_secondarysocket(conn);
|
2012-11-04 12:22:48 -05:00
|
|
|
return result;
|
|
|
|
}
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
|
2007-09-26 08:00:01 -04:00
|
|
|
if(ftp->transfer != FTPTRANSFER_BODY)
|
2005-02-09 08:06:40 -05:00
|
|
|
/* no data to transfer */
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(conn->data, -1, -1, FALSE, -1);
|
2005-02-09 08:06:40 -05:00
|
|
|
else if(!connected)
|
|
|
|
/* since we didn't connect now, we want do_more to get called */
|
|
|
|
conn->bits.do_more = TRUE;
|
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->ctl_valid = TRUE; /* seems good */
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2012-11-04 12:22:48 -05:00
|
|
|
return CURLE_OK;
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
/* called from multi.c while DOing */
|
2007-12-08 17:50:55 -05:00
|
|
|
static CURLcode ftp_doing(struct connectdata *conn,
|
2011-08-08 04:41:35 -04:00
|
|
|
bool *dophase_done)
|
2005-02-09 08:06:40 -05:00
|
|
|
{
|
2011-08-08 04:41:35 -04:00
|
|
|
CURLcode result = ftp_multi_statemach(conn, dophase_done);
|
2005-02-09 08:06:40 -05:00
|
|
|
|
2011-08-08 04:41:35 -04:00
|
|
|
if(result)
|
|
|
|
DEBUGF(infof(conn->data, "DO phase failed\n"));
|
|
|
|
else if(*dophase_done) {
|
2005-02-09 08:06:40 -05:00
|
|
|
result = ftp_dophase_done(conn, FALSE /* not connected */);
|
|
|
|
|
2013-01-17 06:59:23 -05:00
|
|
|
DEBUGF(infof(conn->data, "DO phase is complete2\n"));
|
2005-02-09 08:06:40 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2004-10-16 10:06:54 -04:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* ftp_regular_transfer()
|
|
|
|
*
|
|
|
|
* The input argument is already checked for validity.
|
2005-02-09 08:06:40 -05:00
|
|
|
*
|
|
|
|
* Performs all commands done before a regular transfer between a local and a
|
|
|
|
* remote host.
|
2004-10-16 10:06:54 -04:00
|
|
|
*
|
|
|
|
* ftp->ctl_valid starts out as FALSE, and gets set to TRUE if we reach the
|
2007-12-08 17:50:55 -05:00
|
|
|
* ftp_done() function without finding any major problem.
|
2004-10-16 10:06:54 -04:00
|
|
|
*/
|
|
|
|
static
|
2005-02-09 08:06:40 -05:00
|
|
|
CURLcode ftp_regular_transfer(struct connectdata *conn,
|
|
|
|
bool *dophase_done)
|
2004-10-16 10:06:54 -04:00
|
|
|
{
|
2017-09-09 17:09:06 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
bool connected = FALSE;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2006-09-07 17:49:20 -04:00
|
|
|
struct ftp_conn *ftpc = &conn->proto.ftpc;
|
2007-11-24 18:16:55 -05:00
|
|
|
data->req.size = -1; /* make sure this is unknown at this point */
|
2004-10-16 10:06:54 -04:00
|
|
|
|
|
|
|
Curl_pgrsSetUploadCounter(data, 0);
|
|
|
|
Curl_pgrsSetDownloadCounter(data, 0);
|
2014-08-29 17:48:03 -04:00
|
|
|
Curl_pgrsSetUploadSize(data, -1);
|
|
|
|
Curl_pgrsSetDownloadSize(data, -1);
|
2004-10-16 10:06:54 -04:00
|
|
|
|
2006-09-07 17:49:20 -04:00
|
|
|
ftpc->ctl_valid = TRUE; /* starts good */
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
result = ftp_perform(conn,
|
|
|
|
&connected, /* have we connected after PASV/PORT */
|
|
|
|
dophase_done); /* all commands in the DO-phase done? */
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!result) {
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(!*dophase_done)
|
|
|
|
/* the DO phase has not completed yet */
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
result = ftp_dophase_done(conn, connected);
|
2013-01-17 06:59:23 -05:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2004-06-03 07:41:05 -04:00
|
|
|
}
|
|
|
|
else
|
2007-10-24 17:14:11 -04:00
|
|
|
freedirs(ftpc);
|
2004-06-03 07:41:05 -04:00
|
|
|
|
2005-02-09 08:06:40 -05:00
|
|
|
return result;
|
2004-06-03 07:41:05 -04:00
|
|
|
}
|
|
|
|
|
2013-08-04 13:34:16 -04:00
|
|
|
static CURLcode ftp_setup_connection(struct connectdata *conn)
|
2007-10-12 09:36:37 -04:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-08-04 13:34:16 -04:00
|
|
|
char *type;
|
|
|
|
struct FTP *ftp;
|
2007-10-12 09:36:37 -04:00
|
|
|
|
2018-09-14 17:33:28 -04:00
|
|
|
conn->data->req.protop = ftp = calloc(sizeof(struct FTP), 1);
|
2013-08-04 13:34:16 -04:00
|
|
|
if(NULL == ftp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2018-09-14 17:33:28 -04:00
|
|
|
ftp->path = &data->state.up.path[1]; /* don't include the initial slash */
|
2007-10-12 09:36:37 -04:00
|
|
|
|
|
|
|
/* FTP URLs support an extension like ";type=<typecode>" that
|
|
|
|
* we'll try to get now! */
|
2018-09-14 17:33:28 -04:00
|
|
|
type = strstr(ftp->path, ";type=");
|
2007-10-12 09:36:37 -04:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(!type)
|
2007-10-12 09:36:37 -04:00
|
|
|
type = strstr(conn->host.rawalloc, ";type=");
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(type) {
|
2018-06-02 16:52:56 -04:00
|
|
|
char command;
|
2007-10-12 09:36:37 -04:00
|
|
|
*type = 0; /* it was in the middle of the hostname */
|
2009-01-20 23:42:47 -05:00
|
|
|
command = Curl_raw_toupper(type[6]);
|
2007-10-12 09:36:37 -04:00
|
|
|
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(command) {
|
2007-10-12 09:36:37 -04:00
|
|
|
case 'A': /* ASCII mode */
|
|
|
|
data->set.prefer_ascii = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D': /* directory mode */
|
|
|
|
data->set.ftp_list_only = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I': /* binary mode */
|
|
|
|
default:
|
|
|
|
/* switch off ASCII */
|
|
|
|
data->set.prefer_ascii = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 13:34:16 -04:00
|
|
|
/* get some initial data into the ftp struct */
|
|
|
|
ftp->transfer = FTPTRANSFER_BODY;
|
|
|
|
ftp->downloadsize = 0;
|
|
|
|
conn->proto.ftpc.known_filesize = -1; /* unknown size for now */
|
|
|
|
|
2007-10-12 09:36:37 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2002-06-11 07:13:01 -04:00
|
|
|
#endif /* CURL_DISABLE_FTP */
|