Newer
Older
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
}
if(finfo->flags & CURLFINFOFLAG_KNOWN_SIZE)
ftpc->known_filesize = finfo->size;
ret = ftp_parse_url_path(conn);
if(ret) {
return ret;
}
/* we don't need the Curl_fileinfo of first file anymore */
Curl_llist_remove(wildcard->filelist, wildcard->filelist->head, NULL);
if(wildcard->filelist->size == 0) { /* remains only one file to down. */
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: {
if(conn->data->set.chunk_end)
conn->data->set.chunk_end(conn->data->wildcard.customptr);
Curl_llist_remove(wildcard->filelist, wildcard->filelist->head, NULL);
wildcard->state = (wildcard->filelist->size == 0) ?
CURLWC_CLEAN : CURLWC_DOWNLOADING;
return wc_statemach(conn);
case CURLWC_CLEAN: {
struct ftp_wc_tmpdata *ftp_tmp = wildcard->tmp;
ret = CURLE_OK;
if(ftp_tmp) {
ret = Curl_ftp_parselist_geterror(ftp_tmp->parser);
}
wildcard->state = ret ? CURLWC_ERROR : CURLWC_DONE;
case CURLWC_DONE:
case CURLWC_ERROR:
break;
}
return ret;
}
/***********************************************************************
*
* ftp_do()
*
* 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.
*/
static CURLcode ftp_do(struct connectdata *conn, bool *done)
CURLcode retcode = CURLE_OK;
struct ftp_conn *ftpc = &conn->proto.ftpc;
Daniel Stenberg
committed
*done = FALSE; /* default to false */
ftpc->wait_data_conn = FALSE; /* default to no such wait */
/*
Since connections can be re-used between SessionHandles, this might be a
connection already existing but on a fresh SessionHandle struct so we must
make sure we have a good 'struct FTP' to play with. For new connections,
the struct FTP is allocated and setup in the ftp_connect() function.
*/
retcode = ftp_init(conn);
if(retcode)
return retcode;
if(conn->data->set.wildcardmatch) {
retcode = wc_statemach(conn);
if(conn->data->wildcard.state == CURLWC_SKIP ||
conn->data->wildcard.state == CURLWC_DONE) {
/* do not call ftp_regular_transfer */
return CURLE_OK;
}
if(retcode) /* error, loop or skipping the file */
return retcode;
}
else { /* no wildcard FSM needed */
retcode = ftp_parse_url_path(conn);
if(retcode)
return retcode;
}
Daniel Stenberg
committed
retcode = ftp_regular_transfer(conn, done);
Daniel Stenberg
committed
Daniel Stenberg
committed
CURLcode Curl_ftpsendf(struct connectdata *conn,
const char *fmt, ...)
ssize_t bytes_written;
#define SBUF_SIZE 1024
char s[SBUF_SIZE];
char *sptr=s;
CURLcode res = CURLE_OK;
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
enum protection_level data_sec = conn->data_prot;
#endif
va_list ap;
va_start(ap, fmt);
vsnprintf(s, SBUF_SIZE-3, fmt, ap);
va_end(ap);
strcat(s, "\r\n"); /* append a trailing CRLF */
Daniel Stenberg
committed
write_len = strlen(s);
res = Curl_convert_to_network(conn->data, s, write_len);
/* Curl_convert_to_network calls failf if unsuccessful */
return(res);
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
conn->data_prot = PROT_CMD;
#endif
Daniel Stenberg
committed
res = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
&bytes_written);
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
conn->data_prot = data_sec;
#endif
if(CURLE_OK != res)
break;
if(conn->data->set.verbose)
Curl_debug(conn->data, CURLINFO_HEADER_OUT,
sptr, (size_t)bytes_written, conn);
if(bytes_written != (ssize_t)write_len) {
write_len -= bytes_written;
sptr += bytes_written;
}
else
break;
}
return res;
}
/***********************************************************************
*
Daniel Stenberg
committed
* ftp_quit()
*
* This should be called before calling sclose() on an ftp control connection
* (not data connections). We should then wait for the response from the
* server before returning. The calling code should then try to close the
* connection.
*
*/
Daniel Stenberg
committed
static CURLcode ftp_quit(struct connectdata *conn)
{
CURLcode result = CURLE_OK;
if(conn->proto.ftpc.ctl_valid) {
result = Curl_pp_sendf(&conn->proto.ftpc.pp, "QUIT", NULL);
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 */
conn->bits.close = TRUE; /* mark for connection closure */
state(conn, FTP_STOP);
return result;
}
state(conn, FTP_QUIT);
result = ftp_easy_statemach(conn);
}
return result;
}
/***********************************************************************
*
* ftp_disconnect()
*
* Disconnect from an FTP server. Cleanup protocol-specific per-connection
* resources. BLOCKING.
static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection)
struct ftp_conn *ftpc= &conn->proto.ftpc;
struct pingpong *pp = &ftpc->pp;
/* 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.
Daniel Stenberg
committed
ftp_quit() will check the state of ftp->ctl_valid. If it's ok it
will try to send the QUIT command, otherwise it will just return.
*/
if(dead_connection)
ftpc->ctl_valid = FALSE;
Daniel Stenberg
committed
/* The FTP session may or may not have been allocated/setup at this point! */
Daniel Stenberg
committed
(void)ftp_quit(conn); /* ignore errors on the QUIT */
if(ftpc->entrypath) {
struct SessionHandle *data = conn->data;
Daniel Stenberg
committed
if(data->state.most_recent_ftp_entrypath == ftpc->entrypath) {
Daniel Stenberg
committed
data->state.most_recent_ftp_entrypath = NULL;
Daniel Stenberg
committed
free(ftpc->entrypath);
ftpc->entrypath = NULL;
}
Daniel Stenberg
committed
freedirs(ftpc);
if(ftpc->prevpath) {
free(ftpc->prevpath);
ftpc->prevpath = NULL;
Daniel Stenberg
committed
}
Patrick Monnerat
committed
if(ftpc->server_os) {
free(ftpc->server_os);
ftpc->server_os = NULL;
}
Daniel Stenberg
committed
Curl_pp_disconnect(pp);
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
Curl_sec_end(conn);
#endif
Sterling Hughes
committed
/***********************************************************************
*
Daniel Stenberg
committed
* ftp_parse_url_path()
Daniel Stenberg
committed
* Parse the URL path into separate path components.
*
*/
static
Daniel Stenberg
committed
CURLcode ftp_parse_url_path(struct connectdata *conn)
{
struct SessionHandle *data = conn->data;
/* the ftp struct is already inited in ftp_connect() */
Daniel Stenberg
committed
struct FTP *ftp = data->state.proto.ftp;
struct ftp_conn *ftpc = &conn->proto.ftpc;
const char *slash_pos; /* position of the first '/' char in curpos */
const char *path_to_use = data->state.path;
const char *cur_pos;
const char *filename = NULL;
cur_pos = path_to_use; /* current position in path. point at the begin
of next path component */
ftpc->ctl_valid = FALSE;
ftpc->cwdfail = FALSE;
switch(data->set.ftp_filemethod) {
case FTPFILE_NOCWD:
/* fastest, but less standard-compliant */
/*
The best time to check whether the path is a file or directory is right
here. so:
the first condition in the if() right here, is there just in case
someone decides to set path to NULL one day
*/
Daniel Stenberg
committed
if(data->state.path &&
data->state.path[0] &&
(data->state.path[strlen(data->state.path) - 1] != '/') )
filename = data->state.path; /* this is a full file path */
/*
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.
*/
case FTPFILE_SINGLECWD:
/* get the last slash */
Daniel Stenberg
committed
if(!path_to_use[0]) {
/* no dir, no file */
ftpc->dirdepth = 0;
break;
}
Daniel Stenberg
committed
if(slash_pos || !*cur_pos) {
ftpc->dirs = calloc(1, sizeof(ftpc->dirs[0]));
if(!ftpc->dirs)
ftpc->dirs[0] = curl_easy_unescape(conn->data, slash_pos ? cur_pos : "/",
slash_pos ?
curlx_sztosi(slash_pos-cur_pos) : 1,
NULL);
if(!ftpc->dirs[0]) {
Daniel Stenberg
committed
freedirs(ftpc);
return CURLE_OUT_OF_MEMORY;
}
ftpc->dirdepth = 1; /* we consider it to be a single dir */
filename = slash_pos ? slash_pos+1 : cur_pos; /* rest is file name */
filename = cur_pos; /* this is a file name only */
default: /* allow pretty much anything */
case FTPFILE_MULTICWD:
ftpc->dirdepth = 0;
ftpc->diralloc = 5; /* default dir depth to allocate */
ftpc->dirs = calloc(ftpc->diralloc, sizeof(ftpc->dirs[0]));
if(!ftpc->dirs)
Daniel Stenberg
committed
/* we have a special case for listing the root dir only */
if(strequal(path_to_use, "/")) {
cur_pos++; /* make it point to the zero byte */
ftpc->dirs[0] = strdup("/");
ftpc->dirdepth++;
}
else {
/* parse the URL path into separate path components */
Daniel Stenberg
committed
while((slash_pos = strchr(cur_pos, '/')) != NULL) {
/* 1 or 0 pointer offset to indicate absolute directory */
ssize_t absolute_dir = ((cur_pos - data->state.path > 0) &&
(ftpc->dirdepth == 0))?1:0;
Daniel Stenberg
committed
/* seek out the next path component */
Daniel Stenberg
committed
if(slash_pos-cur_pos) {
Daniel Stenberg
committed
/* we skip empty path components, like "x//y" since the FTP command
CWD requires a parameter and a non-existent parameter a) doesn't
Daniel Stenberg
committed
work on many servers and b) has no effect on the others. */
int len = curlx_sztosi(slash_pos - cur_pos + absolute_dir);
Daniel Stenberg
committed
ftpc->dirs[ftpc->dirdepth] =
curl_easy_unescape(conn->data, cur_pos - absolute_dir, len, NULL);
Daniel Stenberg
committed
if(!ftpc->dirs[ftpc->dirdepth]) { /* run out of memory ... */
Daniel Stenberg
committed
failf(data, "no memory");
Daniel Stenberg
committed
freedirs(ftpc);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
}
Daniel Stenberg
committed
if(isBadFtpString(ftpc->dirs[ftpc->dirdepth])) {
Daniel Stenberg
committed
free(ftpc->dirs[ftpc->dirdepth]);
Daniel Stenberg
committed
freedirs(ftpc);
Daniel Stenberg
committed
return CURLE_URL_MALFORMAT;
}
Daniel Stenberg
committed
else {
cur_pos = slash_pos + 1; /* jump to the rest of the string */
continue;
Daniel Stenberg
committed
cur_pos = slash_pos + 1; /* jump to the rest of the string */
if(++ftpc->dirdepth >= ftpc->diralloc) {
/* enlarge array */
Daniel Stenberg
committed
ftpc->diralloc *= 2; /* double the size each time */
bigger = realloc(ftpc->dirs, ftpc->diralloc * sizeof(ftpc->dirs[0]));
if(!bigger) {
Daniel Stenberg
committed
freedirs(ftpc);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
}
Daniel Stenberg
committed
}
}
}
filename = cur_pos; /* the rest is the file name */
break;
} /* switch */
if(filename && *filename) {
ftpc->file = curl_easy_unescape(conn->data, filename, 0, NULL);
Daniel Stenberg
committed
freedirs(ftpc);
failf(data, "no memory");
return CURLE_OUT_OF_MEMORY;
}
Daniel Stenberg
committed
if(isBadFtpString(ftpc->file)) {
Daniel Stenberg
committed
freedirs(ftpc);
return CURLE_URL_MALFORMAT;
}
}
else
ftpc->file=NULL; /* instead of point to a zero byte, we make it a NULL
pointer */
if(data->set.upload && !ftpc->file && (ftp->transfer == FTPTRANSFER_BODY)) {
/* We need a file name when uploading. Return error! */
failf(data, "Uploading to a URL without a file name!");
return CURLE_URL_MALFORMAT;
}
ftpc->cwddone = FALSE; /* default to not done */
if(ftpc->prevpath) {
Daniel Stenberg
committed
/* prevpath is "raw" so we convert the input path before we compare the
strings */
int dlen;
char *path = curl_easy_unescape(conn->data, data->state.path, 0, &dlen);
Daniel Stenberg
committed
freedirs(ftpc);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
Daniel Stenberg
committed
dlen -= ftpc->file?curlx_uztosi(strlen(ftpc->file)):0;
if((dlen == curlx_uztosi(strlen(ftpc->prevpath))) &&
strnequal(path, ftpc->prevpath, dlen)) {
infof(data, "Request has same path as previous transfer\n");
ftpc->cwddone = TRUE;
Daniel Stenberg
committed
free(path);
Daniel Stenberg
committed
}
/* call this when the DO phase has completed */
static CURLcode ftp_dophase_done(struct connectdata *conn,
bool connected)
{
Daniel Stenberg
committed
struct FTP *ftp = conn->data->state.proto.ftp;
struct ftp_conn *ftpc = &conn->proto.ftpc;
if(connected) {
bool completed;
CURLcode result = ftp_do_more(conn, &completed);
if(result) {
if(conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD) {
/* close the second socket if it was created already */
Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
}
return result;
}
}
if(ftp->transfer != FTPTRANSFER_BODY)
/* no data to transfer */
Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
else if(!connected)
/* since we didn't connect now, we want do_more to get called */
conn->bits.do_more = TRUE;
ftpc->ctl_valid = TRUE; /* seems good */
}
/* called from multi.c while DOing */
static CURLcode ftp_doing(struct connectdata *conn,
{
CURLcode result = ftp_multi_statemach(conn, dophase_done);
if(result)
DEBUGF(infof(conn->data, "DO phase failed\n"));
else if(*dophase_done) {
result = ftp_dophase_done(conn, FALSE /* not connected */);
DEBUGF(infof(conn->data, "DO phase is complete\n"));
}
return result;
}
Daniel Stenberg
committed
/***********************************************************************
*
* ftp_regular_transfer()
*
* The input argument is already checked for validity.
*
* Performs all commands done before a regular transfer between a local and a
* remote host.
Daniel Stenberg
committed
*
* ftp->ctl_valid starts out as FALSE, and gets set to TRUE if we reach the
* ftp_done() function without finding any major problem.
Daniel Stenberg
committed
*/
static
CURLcode ftp_regular_transfer(struct connectdata *conn,
bool *dophase_done)
Daniel Stenberg
committed
{
CURLcode result=CURLE_OK;
Daniel Stenberg
committed
struct SessionHandle *data = conn->data;
struct ftp_conn *ftpc = &conn->proto.ftpc;
Daniel Stenberg
committed
data->req.size = -1; /* make sure this is unknown at this point */
Daniel Stenberg
committed
Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0);
Curl_pgrsSetDownloadSize(data, 0);
ftpc->ctl_valid = TRUE; /* starts good */
result = ftp_perform(conn,
&connected, /* have we connected after PASV/PORT */
dophase_done); /* all commands in the DO-phase done? */
if(CURLE_OK == result) {
if(!*dophase_done)
/* the DO phase has not completed yet */
return CURLE_OK;
result = ftp_dophase_done(conn, connected);
if(result)
return result;
}
else
Daniel Stenberg
committed
freedirs(ftpc);
return result;
}
static CURLcode ftp_setup_connection(struct connectdata * conn)
Patrick Monnerat
committed
{
struct SessionHandle *data = conn->data;
char * type;
char command;
if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
/* Unless we have asked to tunnel ftp operations through the proxy, we
switch and use HTTP operations only */
#ifndef CURL_DISABLE_HTTP
Daniel Stenberg
committed
if(conn->handler == &Curl_handler_ftp)
conn->handler = &Curl_handler_ftp_proxy;
else {
#ifdef USE_SSL
conn->handler = &Curl_handler_ftps_proxy;
#else
failf(data, "FTPS not supported!");
return CURLE_UNSUPPORTED_PROTOCOL;
#endif
}
Daniel Stenberg
committed
/*
* We explicitly mark this connection as persistent here as we're doing
* FTP over HTTP and thus we accidentally avoid setting this value
* otherwise.
*/
conn->bits.close = FALSE;
Patrick Monnerat
committed
#else
failf(data, "FTP over http proxy requires HTTP support built-in!");
return CURLE_UNSUPPORTED_PROTOCOL;
#endif
}
Daniel Stenberg
committed
data->state.path++; /* don't include the initial slash */
data->state.slash_removed = TRUE; /* we've skipped the slash */
Patrick Monnerat
committed
/* FTP URLs support an extension like ";type=<typecode>" that
* we'll try to get now! */
Daniel Stenberg
committed
type = strstr(data->state.path, ";type=");
Patrick Monnerat
committed
Daniel Stenberg
committed
if(!type)
Patrick Monnerat
committed
type = strstr(conn->host.rawalloc, ";type=");
Daniel Stenberg
committed
if(type) {
Patrick Monnerat
committed
*type = 0; /* it was in the middle of the hostname */
command = Curl_raw_toupper(type[6]);
conn->bits.type_set = TRUE;
Patrick Monnerat
committed
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
switch (command) {
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;
}
}
return CURLE_OK;
}
#endif /* CURL_DISABLE_FTP */