1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: ftp.c,v 1.240 2004/03/17 12:46:45 bagder Exp $
22 ***************************************************************************/
26 #ifndef CURL_DISABLE_FTP
37 #ifdef HAVE_SYS_SELECT_H
38 #include <sys/select.h>
41 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
43 #else /* some kind of unix */
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
47 #include <sys/types.h>
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
54 #include <sys/utsname.h>
64 #if defined(WIN32) && defined(__GNUC__) || defined(__MINGW32__)
68 #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
70 #define in_addr_t unsigned long
73 #include <curl/curl.h>
82 #include "http.h" /* for HTTP proxy tunnel stuff */
90 #include "strtoofft.h"
95 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
96 #include "inet_ntoa_r.h"
99 #define _MPRINTF_REPLACE /* use our functions only */
100 #include <curl/mprintf.h>
102 /* The last #include file should be: */
104 #include "memdebug.h"
107 /* Local API functions */
108 static CURLcode ftp_sendquote(struct connectdata *conn,
109 struct curl_slist *quote);
110 static CURLcode ftp_cwd(struct connectdata *conn, char *path);
111 static CURLcode ftp_mkd(struct connectdata *conn, char *path);
112 static CURLcode ftp_cwd_and_mkd(struct connectdata *conn, char *path);
114 /* easy-to-use macro: */
115 #define FTPSENDF(x,y,z) if((result = Curl_ftpsendf(x,y,z))) return result
117 static void freedirs(struct FTP *ftp)
120 for (i=0; ftp->dirs[i]; i++){
126 /***********************************************************************
128 * AllowServerConnect()
130 * When we've issue the PORT command, we have told the server to connect
131 * to us. This function will sit and wait here until the server has
135 static CURLcode AllowServerConnect(struct connectdata *conn)
139 struct SessionHandle *data = conn->data;
140 curl_socket_t sock = conn->sock[SECONDARYSOCKET];
141 struct timeval now = Curl_tvnow();
142 long timespent = Curl_tvdiff(Curl_tvnow(), now)/1000;
143 long timeout = data->set.connecttimeout?data->set.connecttimeout:
144 (data->set.timeout?data->set.timeout: 0);
148 FD_SET(sock, &rdset);
151 timeout -= timespent;
153 failf(data, "Timed out before server could connect to us");
154 return CURLE_OPERATION_TIMEDOUT;
158 /* we give the server 60 seconds to connect to us, or a custom timeout */
159 dt.tv_sec = (int)(timeout?timeout:60);
162 switch (select(sock+1, &rdset, NULL, NULL, &dt)) {
165 failf(data, "Error while waiting for server connect");
166 return CURLE_FTP_PORT_FAILED;
167 case 0: /* timeout */
169 failf(data, "Timeout while waiting for server connect");
170 return CURLE_FTP_PORT_FAILED;
172 /* we have received data here */
175 size_t size = sizeof(struct sockaddr_in);
176 struct sockaddr_in add;
178 getsockname(sock, (struct sockaddr *) &add, (socklen_t *)&size);
179 s=accept(sock, (struct sockaddr *) &add, (socklen_t *)&size);
181 sclose(sock); /* close the first socket */
183 if (CURL_SOCKET_BAD == s) {
185 failf(data, "Error accept()ing server connect");
186 return CURLE_FTP_PORT_FAILED;
188 infof(data, "Connection accepted from server\n");
190 conn->sock[SECONDARYSOCKET] = s;
191 Curl_nonblock(s, TRUE); /* enable non-blocking */
200 /* --- parse FTP server responses --- */
203 * Curl_GetFTPResponse() is supposed to be invoked after each command sent to
204 * a remote FTP server. This function will wait and read all lines of the
205 * response and extract the relevant return code for the invoking function.
208 CURLcode Curl_GetFTPResponse(ssize_t *nreadp, /* return number of bytes read */
209 struct connectdata *conn,
210 int *ftpcode) /* return the ftp-code */
212 /* Brand new implementation.
213 * We cannot read just one byte per read() and then go back to select()
214 * as it seems that the OpenSSL read() stuff doesn't grok that properly.
216 * Alas, read as much as possible, split up into lines, use the ending
217 * line in a response or continue reading. */
219 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
220 int perline; /* count bytes per line */
224 long timeout; /* timeout in seconds */
225 struct timeval interval;
228 struct SessionHandle *data = conn->data;
230 int code=0; /* default ftp "error code" to return */
231 char *buf = data->state.buffer;
232 CURLcode result = CURLE_OK;
233 struct FTP *ftp = conn->proto.ftp;
234 struct timeval now = Curl_tvnow();
237 *ftpcode = 0; /* 0 for errors */
239 FD_ZERO (&readfd); /* clear it */
240 FD_SET (sockfd, &readfd); /* read socket */
242 /* get this in a backup variable to be able to restore it on each lap in the
253 while((*nreadp<BUFSIZE) && (keepon && !result)) {
254 /* check and reset timeout value every lap */
255 if(data->set.ftp_response_timeout )
256 /* if CURLOPT_FTP_RESPONSE_TIMEOUT is set, use that to determine
257 remaining time. Also, use "now" as opposed to "conn->now"
258 because ftp_response_timeout is only supposed to govern
259 the response for any given ftp response, not for the time
260 from connect to the given ftp response. */
261 timeout = data->set.ftp_response_timeout - /* timeout time */
262 Curl_tvdiff(Curl_tvnow(), now)/1000; /* spent time */
263 else if(data->set.timeout)
264 /* if timeout is requested, find out how much remaining time we have */
265 timeout = data->set.timeout - /* timeout time */
266 Curl_tvdiff(Curl_tvnow(), conn->now)/1000; /* spent time */
268 /* Even without a requested timeout, we only wait response_time
269 seconds for the full response to arrive before we bail out */
270 timeout = ftp->response_time -
271 Curl_tvdiff(Curl_tvnow(), now)/1000; /* spent time */
274 failf(data, "FTP response timeout");
275 return CURLE_OPERATION_TIMEDOUT; /* already too little time */
279 readfd = rkeepfd; /* set every lap */
280 interval.tv_sec = 1; /* use 1 second timeout intervals */
281 interval.tv_usec = 0;
283 switch (select (sockfd+1, &readfd, NULL, NULL, &interval)) {
284 case -1: /* select() error, stop reading */
285 result = CURLE_RECV_ERROR;
286 failf(data, "FTP response aborted due to select() error: %d", errno);
288 case 0: /* timeout */
289 if(Curl_pgrsUpdate(conn))
290 return CURLE_ABORTED_BY_CALLBACK;
291 continue; /* just continue in our loop for the timeout duration */
297 if(CURLE_OK == result) {
299 * This code previously didn't use the kerberos sec_read() code
300 * to read, but when we use Curl_read() it may do so. Do confirm
301 * that this is still ok and then remove this comment!
304 /* we had data in the "cache", copy that instead of doing an actual
307 * Dave Meyer, December 2003:
308 * ftp->cache_size is cast to int here. This should be safe,
309 * because it would have been populated with something of size
310 * int to begin with, even though its datatype may be larger
313 memcpy(ptr, ftp->cache, (int)ftp->cache_size);
314 gotbytes = (int)ftp->cache_size;
315 free(ftp->cache); /* free the cache */
316 ftp->cache = NULL; /* clear the pointer */
317 ftp->cache_size = 0; /* zero the size just in case */
320 int res = Curl_read(conn, sockfd, ptr, BUFSIZE-*nreadp, &gotbytes);
323 continue; /* go looping again */
331 else if(gotbytes <= 0) {
333 result = CURLE_RECV_ERROR;
334 failf(data, "FTP response reading failed");
337 /* we got a whole chunk of data, which can be anything from one
338 * byte to a set of lines and possible just a piece of the last
343 for(i = 0; i < gotbytes; ptr++, i++) {
346 /* a newline is CRLF in ftp-talk, so the CR is ignored as
347 the line isn't really terminated until the LF comes */
349 /* output debug output if that is requested */
350 if(data->set.verbose)
351 Curl_debug(data, CURLINFO_HEADER_IN, line_start, perline);
354 * We pass all response-lines to the callback function registered
355 * for "headers". The response lines can be seen as a kind of
358 result = Curl_client_write(data, CLIENTWRITE_HEADER,
359 line_start, perline);
363 #define lastline(line) (isdigit((int)line[0]) && isdigit((int)line[1]) && \
364 isdigit((int)line[2]) && (' ' == line[3]))
366 if(perline>3 && lastline(line_start)) {
367 /* This is the end of the last line, copy the last
368 * line to the start of the buffer and zero terminate,
369 * for old times sake (and krb4)! */
372 for(meow=line_start, n=0; meow<ptr; meow++, n++)
374 *meow=0; /* zero terminate */
376 line_start = ptr+1; /* advance pointer */
377 i++; /* skip this before getting out */
380 perline=0; /* line starts over here */
384 if(!keepon && (i != gotbytes)) {
385 /* We found the end of the response lines, but we didn't parse the
386 full chunk of data we have read from the server. We therefore
387 need to store the rest of the data to be checked on the next
388 invoke as it may actually contain another end of response
389 already! Cleverly figured out by Eric Lavigne in December
391 ftp->cache_size = gotbytes - i;
392 ftp->cache = (char *)malloc((int)ftp->cache_size);
394 memcpy(ftp->cache, line_start, (int)ftp->cache_size);
396 return CURLE_OUT_OF_MEMORY; /**BANG**/
398 } /* there was data */
400 } /* while there's buffer left and loop is requested */
406 /* handle the security-oriented responses 6xx ***/
407 /* FIXME: some errorchecking perhaps... ***/
410 Curl_sec_read_msg(conn, buf, prot_safe);
413 Curl_sec_read_msg(conn, buf, prot_private);
416 Curl_sec_read_msg(conn, buf, prot_confidential);
419 /* normal ftp stuff we pass through! */
425 *ftpcode=code; /* return the initial number like this */
427 /* store the latest code for later retrieval */
428 conn->data->info.httpcode=code;
433 static const char *ftpauth[]= {
438 * Curl_ftp_connect() should do everything that is to be considered a part of
439 * the connection phase.
441 CURLcode Curl_ftp_connect(struct connectdata *conn)
443 /* this is FTP and no proxy */
445 struct SessionHandle *data=conn->data;
446 char *buf = data->state.buffer; /* this is our buffer */
451 ftp = (struct FTP *)malloc(sizeof(struct FTP));
453 return CURLE_OUT_OF_MEMORY;
455 memset(ftp, 0, sizeof(struct FTP));
456 conn->proto.ftp = ftp;
458 /* We always support persistant connections on ftp */
459 conn->bits.close = FALSE;
461 /* get some initial data into the ftp struct */
462 ftp->bytecountp = &conn->bytecount;
464 /* no need to duplicate them, this connectdata struct won't change */
465 ftp->user = conn->user;
466 ftp->passwd = conn->passwd;
467 ftp->response_time = 3600; /* set default response time-out */
469 if (data->set.tunnel_thru_httpproxy) {
470 /* We want "seamless" FTP operations through HTTP proxy tunnel */
471 result = Curl_ConnectHTTPProxyTunnel(conn, FIRSTSOCKET,
472 conn->hostname, conn->remote_port);
473 if(CURLE_OK != result)
477 if(conn->protocol & PROT_FTPS) {
478 /* FTPS is simply ftp with SSL for the control channel */
479 /* now, perform the SSL initialization for this socket */
480 result = Curl_SSLConnect(conn, FIRSTSOCKET);
485 /* The first thing we do is wait for the "220*" line: */
486 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
491 failf(data, "This doesn't seem like a nice ftp-server response");
492 return CURLE_FTP_WEIRD_SERVER_REPLY;
496 /* if not anonymous login, try a secure login */
499 /* request data protection level (default is 'clear') */
500 Curl_sec_request_prot(conn, "private");
502 /* We set private first as default, in case the line below fails to
504 Curl_sec_request_prot(conn, data->set.krb4_level);
506 if(Curl_sec_login(conn) != 0)
507 infof(data, "Logging in with password in cleartext!\n");
509 infof(data, "Authentication successful\n");
513 if(data->set.ftp_ssl && !conn->ssl[FIRSTSOCKET].use) {
514 /* we don't have a SSL/TLS connection, try a FTPS connection now */
516 for (try = 0; ftpauth[try]; try++) {
518 FTPSENDF(conn, "AUTH %s", ftpauth[try]);
520 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
525 /* RFC2228 (page 5) says:
527 * If the server is willing to accept the named security mechanism, and
528 * does not require any security data, it must respond with reply code
532 if((ftpcode == 234) || (ftpcode == 334)) {
533 result = Curl_SSLConnect(conn, FIRSTSOCKET);
536 conn->protocol |= PROT_FTPS;
537 conn->ssl[SECONDARYSOCKET].use = FALSE; /* clear-text data */
544 FTPSENDF(conn, "USER %s", ftp->user?ftp->user:"");
546 /* wait for feedback */
547 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
552 /* 530 User ... access denied
553 (the server denies to log the specified user) */
554 failf(data, "Access denied: %s", &buf[4]);
555 return CURLE_FTP_ACCESS_DENIED;
557 else if(ftpcode == 331) {
558 /* 331 Password required for ...
559 (the server requires to send the user's password too) */
560 FTPSENDF(conn, "PASS %s", ftp->passwd?ftp->passwd:"");
561 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
566 /* 530 Login incorrect.
567 (the username and/or the password are incorrect) */
568 failf(data, "the username and/or the password are incorrect");
569 return CURLE_FTP_USER_PASSWORD_INCORRECT;
571 else if(ftpcode == 230) {
572 /* 230 User ... logged in.
573 (user successfully logged in) */
575 infof(data, "We have successfully logged in\n");
578 failf(data, "Odd return code after PASS");
579 return CURLE_FTP_WEIRD_PASS_REPLY;
582 else if(buf[0] == '2') {
583 /* 230 User ... logged in.
584 (the user logged in without password) */
585 infof(data, "We have successfully logged in\n");
586 if (conn->ssl[FIRSTSOCKET].use) {
588 /* we are logged in (with Kerberos)
589 * now set the requested protection level
591 if(conn->sec_complete)
592 Curl_sec_set_protection_level(conn);
594 /* we may need to issue a KAUTH here to have access to the files
595 * do it if user supplied a password
597 if(conn->passwd && *conn->passwd) {
598 result = Curl_krb_kauth(conn);
606 failf(data, "Odd return code after USER");
607 return CURLE_FTP_WEIRD_USER_REPLY;
610 if(conn->ssl[FIRSTSOCKET].use) {
611 /* PBSZ = PROTECTION BUFFER SIZE.
613 The 'draft-murray-auth-ftp-ssl' (draft 12, page 7) says:
615 Specifically, the PROT command MUST be preceded by a PBSZ command
616 and a PBSZ command MUST be preceded by a successful security data
617 exchange (the TLS negotiation in this case)
621 Thus the PBSZ command must still be issued, but must have a parameter
622 of '0' to indicate that no buffering is taking place and the data
623 connection should not be encapsulated.
625 FTPSENDF(conn, "PBSZ %d", 0);
626 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
630 /* For TLS, the data connection can have one of two security levels.
632 1)Clear (requested by 'PROT C')
634 2)Private (requested by 'PROT P')
636 if(!conn->ssl[SECONDARYSOCKET].use) {
637 FTPSENDF(conn, "PROT %c", 'P');
638 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
643 /* We have enabled SSL for the data connection! */
644 conn->ssl[SECONDARYSOCKET].use = TRUE;
646 /* FTP servers typically responds with 500 if they decide to reject
651 /* send PWD to discover our entry point */
652 FTPSENDF(conn, "PWD", NULL);
654 /* wait for feedback */
655 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
660 char *dir = (char *)malloc(nread+1);
662 char *ptr=&buf[4]; /* start on the first letter */
665 return CURLE_OUT_OF_MEMORY;
667 /* Reply format is like
668 257<space>"<directory-name>"<space><commentary> and the RFC959 says
670 The directory name can contain any character; embedded double-quotes
671 should be escaped by double-quotes (the "quote-doubling" convention).
674 /* it started good */
679 /* "quote-doubling" */
685 *store = '\0'; /* zero terminate */
686 break; /* get out of this loop */
694 ftp->entrypath =dir; /* remember this */
695 infof(data, "Entry path is '%s'\n", ftp->entrypath);
698 /* couldn't get the path */
700 infof(data, "Failed to figure out path\n");
705 /* We couldn't read the PWD response! */
711 /***********************************************************************
715 * The DONE function. This does what needs to be done after a single DO has
718 * Input argument is already checked for validity.
720 CURLcode Curl_ftp_done(struct connectdata *conn)
722 struct SessionHandle *data = conn->data;
723 struct FTP *ftp = conn->proto.ftp;
726 CURLcode result=CURLE_OK;
728 /* free the dir tree parts */
736 if(data->set.upload) {
737 if((-1 != data->set.infilesize) &&
738 (data->set.infilesize != *ftp->bytecountp) &&
740 failf(data, "Uploaded unaligned file size (%" FORMAT_OFF_T
741 " out of %" FORMAT_OFF_T " bytes)",
742 *ftp->bytecountp, data->set.infilesize);
743 conn->bits.close = TRUE; /* close this connection since we don't
744 know what state this error leaves us in */
745 return CURLE_PARTIAL_FILE;
749 if((-1 != conn->size) && (conn->size != *ftp->bytecountp) &&
750 (conn->maxdownload != *ftp->bytecountp)) {
751 failf(data, "Received only partial file: %" FORMAT_OFF_T " bytes",
753 conn->bits.close = TRUE; /* close this connection since we don't
754 know what state this error leaves us in */
755 return CURLE_PARTIAL_FILE;
757 else if(!ftp->dont_check &&
760 /* We consider this an error, but there's no true FTP error received
761 why we need to continue to "read out" the server response too.
762 We don't want to leave a "waiting" server reply if we'll get told
763 to make a second request on this same connection! */
764 failf(data, "No data was received!");
765 result = CURLE_FTP_COULDNT_RETR_FILE;
770 Curl_sec_fflush_fd(conn, conn->sock[SECONDARYSOCKET]);
772 /* shut down the socket to inform the server we're done */
773 sclose(conn->sock[SECONDARYSOCKET]);
774 conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
776 if(!ftp->no_transfer) {
777 /* Let's see what the server says about the transfer we just performed,
778 but lower the timeout as sometimes this connection has died while
779 the data has been transfered. This happens when doing through NATs
780 etc that abandon old silent connections.
782 ftp->response_time = 60; /* give it only a minute for now */
784 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
786 ftp->response_time = 3600; /* set this back to one hour waits */
788 if(!nread && (CURLE_OPERATION_TIMEDOUT == result)) {
789 failf(data, "control connection looks dead");
796 if(!ftp->dont_check) {
797 /* 226 Transfer complete, 250 Requested file action okay, completed. */
798 if((ftpcode != 226) && (ftpcode != 250)) {
799 failf(data, "server did not report OK, got %d", ftpcode);
800 return CURLE_FTP_WRITE_ERROR;
805 /* clear these for next connection */
806 ftp->no_transfer = FALSE;
807 ftp->dont_check = FALSE;
809 /* Send any post-transfer QUOTE strings? */
810 if(!result && data->set.postquote)
811 result = ftp_sendquote(conn, data->set.postquote);
816 /***********************************************************************
820 * Where a 'quote' means a list of custom commands to send to the server.
821 * The quote list is passed as an argument.
825 CURLcode ftp_sendquote(struct connectdata *conn, struct curl_slist *quote)
827 struct curl_slist *item;
835 FTPSENDF(conn, "%s", item->data);
837 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
841 if (ftpcode >= 400) {
842 failf(conn->data, "QUOT string not accepted: %s", item->data);
843 return CURLE_FTP_QUOTE_ERROR;
853 /***********************************************************************
857 * Get the timestamp of the given file.
860 CURLcode ftp_getfiletime(struct connectdata *conn, char *file)
862 CURLcode result=CURLE_OK;
863 int ftpcode; /* for ftp status */
865 char *buf = conn->data->state.buffer;
867 /* we have requested to get the modified-time of the file, this is yet
868 again a grey area as the MDTM is not kosher RFC959 */
869 FTPSENDF(conn, "MDTM %s", file);
871 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
878 /* we got a time. Format should be: "YYYYMMDDHHMMSS[.sss]" where the
879 last .sss part is optional and means fractions of a second */
880 int year, month, day, hour, minute, second;
881 if(6 == sscanf(buf+4, "%04d%02d%02d%02d%02d%02d",
882 &year, &month, &day, &hour, &minute, &second)) {
883 /* we have a time, reformat it */
884 time_t secs=time(NULL);
885 sprintf(buf, "%04d%02d%02d %02d:%02d:%02d GMT",
886 year, month, day, hour, minute, second);
887 /* now, convert this into a time() value: */
888 conn->data->info.filetime = curl_getdate(buf, &secs);
893 infof(conn->data, "unsupported MDTM reply format\n");
895 case 550: /* "No such file or directory" */
896 failf(conn->data, "Given file does not exist");
897 result = CURLE_FTP_COULDNT_RETR_FILE;
903 /***********************************************************************
907 * Set transfer type. We only deal with ASCII or BINARY so this function
910 static CURLcode ftp_transfertype(struct connectdata *conn,
913 struct SessionHandle *data = conn->data;
918 FTPSENDF(conn, "TYPE %s", ascii?"A":"I");
920 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
925 failf(data, "Couldn't set %s mode",
926 ascii?"ASCII":"binary");
927 return ascii? CURLE_FTP_COULDNT_SET_ASCII:CURLE_FTP_COULDNT_SET_BINARY;
933 /***********************************************************************
937 * Returns the file size (in bytes) of the given remote file.
941 CURLcode ftp_getsize(struct connectdata *conn, char *file,
944 struct SessionHandle *data = conn->data;
947 char *buf=data->state.buffer;
950 FTPSENDF(conn, "SIZE %s", file);
951 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
956 /* get the size from the ascii string: */
957 *size = strtoofft(buf+4, NULL, 0);
960 return CURLE_FTP_COULDNT_GET_SIZE;
965 /***************************************************************************
969 * This function only outputs some informationals about this second connection
970 * when we've issued a PASV command before and thus we have connected to a
971 * possibly new IP address.
975 ftp_pasv_verbose(struct connectdata *conn,
976 Curl_ipconnect *addr,
977 char *newhost, /* ascii version */
981 /*****************************************************************
983 * IPv4-only code section
987 struct hostent * answer;
989 #ifdef HAVE_INET_NTOA_R
992 /* The array size trick below is to make this a large chunk of memory
993 suitably 8-byte aligned on 64-bit platforms. This was thoughtfully
994 suggested by Philip Gladstone. */
995 long bigbuf[9000 / sizeof(long)];
997 #if defined(HAVE_INET_ADDR)
999 # if defined(HAVE_GETHOSTBYADDR_R)
1002 char *hostent_buf = (char *)bigbuf; /* get a char * to the buffer */
1004 address = inet_addr(newhost);
1005 # ifdef HAVE_GETHOSTBYADDR_R
1007 # ifdef HAVE_GETHOSTBYADDR_R_5
1008 /* AIX, Digital Unix (OSF1, Tru64) style:
1009 extern int gethostbyaddr_r(char *addr, size_t len, int type,
1010 struct hostent *htent, struct hostent_data *ht_data); */
1012 /* Fred Noz helped me try this out, now it at least compiles! */
1014 /* Bjorn Reese (November 28 2001):
1015 The Tru64 man page on gethostbyaddr_r() says that
1016 the hostent struct must be filled with zeroes before the call to
1019 ... as must be struct hostent_data Craig Markwardt 19 Sep 2002. */
1021 memset(hostent_buf, 0, sizeof(struct hostent)+sizeof(struct hostent_data));
1023 if(gethostbyaddr_r((char *) &address,
1024 sizeof(address), AF_INET,
1025 (struct hostent *)hostent_buf,
1026 (struct hostent_data *)(hostent_buf + sizeof(*answer))))
1029 answer=(struct hostent *)hostent_buf;
1032 # ifdef HAVE_GETHOSTBYADDR_R_7
1033 /* Solaris and IRIX */
1034 answer = gethostbyaddr_r((char *) &address, sizeof(address), AF_INET,
1035 (struct hostent *)bigbuf,
1036 hostent_buf + sizeof(*answer),
1037 sizeof(bigbuf) - sizeof(*answer),
1040 # ifdef HAVE_GETHOSTBYADDR_R_8
1042 if(gethostbyaddr_r((char *) &address, sizeof(address), AF_INET,
1043 (struct hostent *)hostent_buf,
1044 hostent_buf + sizeof(*answer),
1045 sizeof(bigbuf) - sizeof(*answer),
1048 answer=NULL; /* error */
1052 (void)hostent_buf; /* avoid compiler warning */
1053 answer = gethostbyaddr((char *) &address, sizeof(address), AF_INET);
1058 (void) memcpy(&in.s_addr, addr, sizeof (Curl_ipconnect));
1059 infof(conn->data, "Connecting to %s (%s) port %u\n",
1060 answer?answer->h_name:newhost,
1061 #if defined(HAVE_INET_NTOA_R)
1062 inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf)),
1069 /*****************************************************************
1071 * IPv6-only code section
1073 char hbuf[NI_MAXHOST]; /* ~1KB */
1074 char nbuf[NI_MAXHOST]; /* ~1KB */
1075 char sbuf[NI_MAXSERV]; /* around 32 */
1076 #ifdef NI_WITHSCOPEID
1077 const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID;
1079 const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
1081 (void)port; /* prevent compiler warning */
1082 if (getnameinfo(addr->ai_addr, addr->ai_addrlen,
1083 nbuf, sizeof(nbuf), sbuf, sizeof(sbuf), niflags)) {
1084 snprintf(nbuf, sizeof(nbuf), "?");
1085 snprintf(sbuf, sizeof(sbuf), "?");
1088 if (getnameinfo(addr->ai_addr, addr->ai_addrlen,
1089 hbuf, sizeof(hbuf), NULL, 0, 0)) {
1090 infof(conn->data, "Connecting to %s (%s) port %s\n", nbuf, newhost, sbuf);
1093 infof(conn->data, "Connecting to %s (%s) port %s\n", hbuf, nbuf, sbuf);
1098 /***********************************************************************
1102 * Send the proper PORT command. PORT is the ftp client's way of telling the
1103 * server that *WE* open a port that we listen on an awaits the server to
1104 * connect to. This is the opposite of PASV.
1108 CURLcode ftp_use_port(struct connectdata *conn)
1110 struct SessionHandle *data=conn->data;
1111 curl_socket_t portsock= CURL_SOCKET_BAD;
1113 int ftpcode; /* receive FTP response codes in this */
1117 /******************************************************************
1119 * Here's a piece of IPv6-specific code coming up
1123 struct addrinfo hints, *res, *ai;
1124 struct sockaddr_storage ss;
1126 char hbuf[NI_MAXHOST];
1128 struct sockaddr *sa=(struct sockaddr *)&ss;
1129 #ifdef NI_WITHSCOPEID
1130 #define NIFLAGS NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID
1132 #define NIFLAGS NI_NUMERICHOST | NI_NUMERICSERV
1136 char portmsgbuf[4096], tmp[4096];
1138 const char *mode[] = { "EPRT", "LPRT", "PORT", NULL };
1143 * we should use Curl_if2ip? given pickiness of recent ftpd,
1144 * I believe we should use the same address as the control connection.
1147 rc = getsockname(conn->sock[FIRSTSOCKET], (struct sockaddr *)&ss, &sslen);
1149 failf(data, "getsockname() returned %d\n", rc);
1150 return CURLE_FTP_PORT_FAILED;
1153 rc = getnameinfo((struct sockaddr *)&ss, sslen, hbuf, sizeof(hbuf), NULL, 0,
1156 failf(data, "getnameinfo() returned %d\n", rc);
1157 return CURLE_FTP_PORT_FAILED;
1160 memset(&hints, 0, sizeof(hints));
1161 hints.ai_family = sa->sa_family;
1162 /*hints.ai_family = ss.ss_family;
1163 this way can be used if sockaddr_storage is properly defined, as glibc
1165 hints.ai_socktype = SOCK_STREAM;
1166 hints.ai_flags = AI_PASSIVE;
1168 rc = getaddrinfo(hbuf, NULL, &hints, &res);
1170 failf(data, "getaddrinfo() returned %d\n", rc);
1171 return CURLE_FTP_PORT_FAILED;
1174 portsock = CURL_SOCKET_BAD;
1175 for (ai = res; ai; ai = ai->ai_next) {
1177 * Workaround for AIX5 getaddrinfo() problem (it doesn't set ai_socktype):
1179 if (ai->ai_socktype == 0)
1180 ai->ai_socktype = hints.ai_socktype;
1182 portsock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1183 if (portsock == CURL_SOCKET_BAD)
1186 if (bind(portsock, ai->ai_addr, ai->ai_addrlen) < 0) {
1188 portsock = CURL_SOCKET_BAD;
1192 if (listen(portsock, 1) < 0) {
1194 portsock = CURL_SOCKET_BAD;
1201 if (portsock == CURL_SOCKET_BAD) {
1202 failf(data, "%s", strerror(errno));
1203 return CURLE_FTP_PORT_FAILED;
1207 if (getsockname(portsock, sa, &sslen) < 0) {
1208 failf(data, "%s", strerror(errno));
1209 return CURLE_FTP_PORT_FAILED;
1212 for (modep = (char **)(data->set.ftp_use_eprt?&mode[0]:&mode[2]);
1213 modep && *modep; modep++) {
1217 switch (sa->sa_family) {
1219 ap = (unsigned char *)&((struct sockaddr_in *)&ss)->sin_addr;
1220 alen = sizeof(((struct sockaddr_in *)&ss)->sin_addr);
1221 pp = (unsigned char *)&((struct sockaddr_in *)&ss)->sin_port;
1222 plen = sizeof(((struct sockaddr_in *)&ss)->sin_port);
1227 ap = (unsigned char *)&((struct sockaddr_in6 *)&ss)->sin6_addr;
1228 alen = sizeof(((struct sockaddr_in6 *)&ss)->sin6_addr);
1229 pp = (unsigned char *)&((struct sockaddr_in6 *)&ss)->sin6_port;
1230 plen = sizeof(((struct sockaddr_in6 *)&ss)->sin6_port);
1236 lprtaf = eprtaf = -1;
1240 if (strcmp(*modep, "EPRT") == 0) {
1243 if (getnameinfo((struct sockaddr *)&ss, sslen,
1244 portmsgbuf, sizeof(portmsgbuf), tmp, sizeof(tmp),
1248 /* do not transmit IPv6 scope identifier to the wire */
1249 if (sa->sa_family == AF_INET6) {
1250 char *q = strchr(portmsgbuf, '%');
1255 result = Curl_ftpsendf(conn, "%s |%d|%s|%s|", *modep, eprtaf,
1259 } else if (strcmp(*modep, "LPRT") == 0 ||
1260 strcmp(*modep, "PORT") == 0) {
1263 if (strcmp(*modep, "LPRT") == 0 && lprtaf < 0)
1265 if (strcmp(*modep, "PORT") == 0 && sa->sa_family != AF_INET)
1268 portmsgbuf[0] = '\0';
1269 if (strcmp(*modep, "LPRT") == 0) {
1270 snprintf(tmp, sizeof(tmp), "%d,%d", lprtaf, alen);
1271 if (strlcat(portmsgbuf, tmp, sizeof(portmsgbuf)) >=
1272 sizeof(portmsgbuf)) {
1277 for (i = 0; i < alen; i++) {
1279 snprintf(tmp, sizeof(tmp), ",%u", ap[i]);
1281 snprintf(tmp, sizeof(tmp), "%u", ap[i]);
1283 if (strlcat(portmsgbuf, tmp, sizeof(portmsgbuf)) >=
1284 sizeof(portmsgbuf)) {
1289 if (strcmp(*modep, "LPRT") == 0) {
1290 snprintf(tmp, sizeof(tmp), ",%d", plen);
1292 if (strlcat(portmsgbuf, tmp, sizeof(portmsgbuf)) >= sizeof(portmsgbuf))
1296 for (i = 0; i < plen; i++) {
1297 snprintf(tmp, sizeof(tmp), ",%u", pp[i]);
1299 if (strlcat(portmsgbuf, tmp, sizeof(portmsgbuf)) >=
1300 sizeof(portmsgbuf)) {
1305 result = Curl_ftpsendf(conn, "%s %s", *modep, portmsgbuf);
1310 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
1314 if (ftpcode != 200) {
1323 failf(data, "PORT command attempts failed");
1324 return CURLE_FTP_PORT_FAILED;
1326 /* we set the secondary socket variable to this for now, it
1327 is only so that the cleanup function will close it in case
1328 we fail before the true secondary stuff is made */
1329 conn->sock[SECONDARYSOCKET] = portsock;
1332 /******************************************************************
1334 * Here's a piece of IPv4-specific code coming up
1337 struct sockaddr_in sa;
1338 struct Curl_dns_entry *h=NULL;
1339 unsigned short porttouse;
1340 char myhost[256] = "";
1341 bool sa_filled_in = FALSE;
1343 if(data->set.ftpport) {
1347 /* First check if the given name is an IP address */
1348 in=inet_addr(data->set.ftpport);
1350 if((in == CURL_INADDR_NONE) &&
1351 Curl_if2ip(data->set.ftpport, myhost, sizeof(myhost))) {
1352 rc = Curl_resolv(conn, myhost, 0, &h);
1354 rc = Curl_wait_for_resolv(conn, &h);
1357 size_t len = strlen(data->set.ftpport);
1359 rc = Curl_resolv(conn, data->set.ftpport, 0, &h);
1361 rc = Curl_wait_for_resolv(conn, &h);
1364 strcpy(myhost, data->set.ftpport); /* buffer overflow risk */
1368 /* pick a suitable default here */
1373 if (getsockname(conn->sock[FIRSTSOCKET],
1374 (struct sockaddr *)&sa, &sslen) < 0) {
1375 failf(data, "getsockname() failed");
1376 return CURLE_FTP_PORT_FAILED;
1379 sa_filled_in = TRUE; /* the sa struct is filled in */
1383 /* when we return from here, we can forget about this */
1384 Curl_resolv_unlock(data, h);
1386 if ( h || sa_filled_in) {
1387 if( (portsock = socket(AF_INET, SOCK_STREAM, 0)) != CURL_SOCKET_BAD ) {
1390 /* we set the secondary socket variable to this for now, it
1391 is only so that the cleanup function will close it in case
1392 we fail before the true secondary stuff is made */
1393 conn->sock[SECONDARYSOCKET] = portsock;
1396 memset((char *)&sa, 0, sizeof(sa));
1397 memcpy((char *)&sa.sin_addr,
1400 sa.sin_family = AF_INET;
1401 sa.sin_addr.s_addr = INADDR_ANY;
1407 if(bind(portsock, (struct sockaddr *)&sa, size) >= 0) {
1408 /* we succeeded to bind */
1409 struct sockaddr_in add;
1410 socklen_t socksize = sizeof(add);
1412 if(getsockname(portsock, (struct sockaddr *) &add,
1414 failf(data, "getsockname() failed");
1415 return CURLE_FTP_PORT_FAILED;
1417 porttouse = ntohs(add.sin_port);
1419 if ( listen(portsock, 1) < 0 ) {
1420 failf(data, "listen(2) failed on socket");
1421 return CURLE_FTP_PORT_FAILED;
1425 failf(data, "bind(2) failed on socket");
1426 return CURLE_FTP_PORT_FAILED;
1430 failf(data, "socket(2) failed (%s)");
1431 return CURLE_FTP_PORT_FAILED;
1435 failf(data, "could't find my own IP address (%s)", myhost);
1436 return CURLE_FTP_PORT_FAILED;
1439 #ifdef HAVE_INET_NTOA_R
1443 unsigned short ip[5];
1444 (void) memcpy(&in.s_addr,
1445 h?*h->addr->h_addr_list:(char *)&sa.sin_addr.s_addr,
1446 sizeof (in.s_addr));
1448 #ifdef HAVE_INET_NTOA_R
1449 /* ignore the return code from inet_ntoa_r() as it is int or
1450 char * depending on system */
1451 inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf));
1452 sscanf( ntoa_buf, "%hu.%hu.%hu.%hu",
1453 &ip[0], &ip[1], &ip[2], &ip[3]);
1455 sscanf( inet_ntoa(in), "%hu.%hu.%hu.%hu",
1456 &ip[0], &ip[1], &ip[2], &ip[3]);
1458 infof(data, "Telling server to connect to %d.%d.%d.%d:%d\n",
1459 ip[0], ip[1], ip[2], ip[3], porttouse);
1461 result=Curl_ftpsendf(conn, "PORT %d,%d,%d,%d,%d,%d",
1462 ip[0], ip[1], ip[2], ip[3],
1469 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
1473 if(ftpcode != 200) {
1474 failf(data, "Server does not grok PORT, try without it!");
1475 return CURLE_FTP_PORT_FAILED;
1477 #endif /* end of ipv4-specific code */
1482 /***********************************************************************
1486 * Send the PASV command. PASV is the ftp client's way of asking the server to
1487 * open a second port that we can connect to (for the data transfer). This is
1488 * the opposite of PORT.
1492 CURLcode ftp_use_pasv(struct connectdata *conn,
1495 struct SessionHandle *data = conn->data;
1497 char *buf = data->state.buffer; /* this is our buffer */
1498 int ftpcode; /* receive FTP response codes in this */
1500 struct Curl_dns_entry *addr=NULL;
1501 Curl_ipconnect *conninfo;
1505 Here's the excecutive summary on what to do:
1507 PASV is RFC959, expect:
1508 227 Entering Passive Mode (a1,a2,a3,a4,p1,p2)
1510 LPSV is RFC1639, expect:
1511 228 Entering Long Passive Mode (4,4,a1,a2,a3,a4,2,p1,p2)
1513 EPSV is RFC2428, expect:
1514 229 Entering Extended Passive Mode (|||port|)
1518 const char *mode[] = { "EPSV", "PASV", NULL };
1519 int results[] = { 229, 227, 0 };
1521 unsigned short connectport; /* the local port connect() should use! */
1522 unsigned short newport=0; /* remote port, not necessary the local one */
1524 /* newhost must be able to hold a full IP-style address in ASCII, which
1525 in the IPv6 case means 5*8-1 = 39 letters */
1527 char *newhostp=NULL;
1529 for (modeoff = (data->set.ftp_use_epsv?0:1);
1530 mode[modeoff]; modeoff++) {
1531 result = Curl_ftpsendf(conn, "%s", mode[modeoff]);
1534 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
1537 if (ftpcode == results[modeoff])
1541 if (!mode[modeoff]) {
1542 failf(data, "Odd return code after PASV");
1543 return CURLE_FTP_WEIRD_PASV_REPLY;
1545 else if (227 == results[modeoff]) {
1551 * New 227-parser June 3rd 1999.
1552 * It now scans for a sequence of six comma-separated numbers and
1553 * will take them as IP+port indicators.
1555 * Found reply-strings include:
1556 * "227 Entering Passive Mode (127,0,0,1,4,51)"
1557 * "227 Data transfer will passively listen to 127,0,0,1,4,51"
1558 * "227 Entering passive mode. 127,0,0,1,4,51"
1562 if (6 == sscanf(str, "%d,%d,%d,%d,%d,%d",
1563 &ip[0], &ip[1], &ip[2], &ip[3],
1564 &port[0], &port[1]))
1570 failf(data, "Couldn't interpret this 227-reply: %s", buf);
1571 return CURLE_FTP_WEIRD_227_FORMAT;
1574 sprintf(newhost, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
1576 newport = (port[0]<<8) + port[1];
1578 else if (229 == results[modeoff]) {
1579 char *ptr = strchr(buf, '(');
1584 if(5 == sscanf(ptr, "%c%c%c%u%c",
1590 char sep1 = separator[0];
1593 /* The four separators should be identical, or else this is an oddly
1594 formatted reply and we bail out immediately. */
1595 for(i=1; i<4; i++) {
1596 if(separator[i] != sep1) {
1597 ptr=NULL; /* set to NULL to signal error */
1604 /* we should use the same host we already are connected to */
1605 newhostp = conn->name;
1612 failf(data, "Weirdly formatted EPSV reply");
1613 return CURLE_FTP_WEIRD_PASV_REPLY;
1617 return CURLE_FTP_CANT_RECONNECT;
1619 if(data->change.proxy && *data->change.proxy) {
1621 * This is a tunnel through a http proxy and we need to connect to the
1624 * We don't want to rely on a former host lookup that might've expired
1625 * now, instead we remake the lookup here and now!
1627 rc = Curl_resolv(conn, conn->proxyhost, conn->port, &addr);
1629 rc = Curl_wait_for_resolv(conn, &addr);
1632 (unsigned short)conn->port; /* we connect to the proxy's port */
1636 /* normal, direct, ftp connection */
1637 rc = Curl_resolv(conn, newhostp, newport, &addr);
1639 rc = Curl_wait_for_resolv(conn, &addr);
1642 failf(data, "Can't resolve new host %s:%d", newhostp, newport);
1643 return CURLE_FTP_CANT_GET_HOST;
1645 connectport = newport; /* we connect to the remote port */
1648 result = Curl_connecthost(conn,
1651 &conn->sock[SECONDARYSOCKET],
1655 Curl_resolv_unlock(data, addr); /* we're done using this address */
1661 * When this is used from the multi interface, this might've returned with
1662 * the 'connected' set to FALSE and thus we are now awaiting a non-blocking
1663 * connect to connect and we should not be "hanging" here waiting.
1666 if(data->set.verbose)
1667 /* this just dumps information about this second connection */
1668 ftp_pasv_verbose(conn, conninfo, newhostp, connectport);
1670 if(data->set.tunnel_thru_httpproxy) {
1671 /* We want "seamless" FTP operations through HTTP proxy tunnel */
1672 result = Curl_ConnectHTTPProxyTunnel(conn, SECONDARYSOCKET,
1674 if(CURLE_OK != result)
1682 * Curl_ftp_nextconnect()
1684 * This function shall be called when the second FTP connection has been
1685 * established and is confirmed connected.
1688 CURLcode Curl_ftp_nextconnect(struct connectdata *conn)
1690 struct SessionHandle *data=conn->data;
1691 char *buf = data->state.buffer; /* this is our buffer */
1694 int ftpcode; /* for ftp status */
1696 /* the ftp struct is already inited in Curl_ftp_connect() */
1697 struct FTP *ftp = conn->proto.ftp;
1698 curl_off_t *bytecountp = ftp->bytecountp;
1700 if(data->set.upload) {
1702 /* Set type to binary (unless specified ASCII) */
1703 result = ftp_transfertype(conn, data->set.ftp_ascii);
1707 /* Send any PREQUOTE strings after transfer type is set? (Wesley Laxton)*/
1708 if(data->set.prequote) {
1709 if ((result = ftp_sendquote(conn, data->set.prequote)) != CURLE_OK)
1713 if(conn->resume_from) {
1714 /* we're about to continue the uploading of a file */
1715 /* 1. get already existing file's size. We use the SIZE
1716 command for this which may not exist in the server!
1717 The SIZE command is not in RFC959. */
1719 /* 2. This used to set REST. But since we can do append, we
1720 don't another ftp command. We just skip the source file
1721 offset and then we APPEND the rest on the file instead */
1723 /* 3. pass file-size number of bytes in the source file */
1724 /* 4. lower the infilesize counter */
1725 /* => transfer as usual */
1727 if(conn->resume_from < 0 ) {
1728 /* we could've got a specified offset from the command line,
1729 but now we know we didn't */
1730 curl_off_t gottensize;
1732 if(CURLE_OK != ftp_getsize(conn, ftp->file, &gottensize)) {
1733 failf(data, "Couldn't get remote file size");
1734 return CURLE_FTP_COULDNT_GET_SIZE;
1736 conn->resume_from = gottensize;
1739 if(conn->resume_from) {
1740 /* do we still game? */
1741 curl_off_t passed=0;
1742 /* enable append instead */
1743 data->set.ftp_append = 1;
1745 /* Now, let's read off the proper amount of bytes from the
1746 input. If we knew it was a proper file we could've just
1747 fseek()ed but we only have a stream here */
1749 curl_off_t readthisamountnow = (conn->resume_from - passed);
1750 curl_off_t actuallyread;
1752 if(readthisamountnow > BUFSIZE)
1753 readthisamountnow = BUFSIZE;
1756 conn->fread(data->state.buffer, 1, (size_t)readthisamountnow,
1759 passed += actuallyread;
1760 if(actuallyread != readthisamountnow) {
1761 failf(data, "Could only read %" FORMAT_OFF_T
1762 " bytes from the input", passed);
1763 return CURLE_FTP_COULDNT_USE_REST;
1766 while(passed != conn->resume_from);
1768 /* now, decrease the size of the read */
1769 if(data->set.infilesize>0) {
1770 data->set.infilesize -= conn->resume_from;
1772 if(data->set.infilesize <= 0) {
1773 infof(data, "File already completely uploaded\n");
1775 /* no data to transfer */
1776 result=Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
1778 /* Set no_transfer so that we won't get any error in
1779 * Curl_ftp_done() because we didn't transfer anything! */
1780 ftp->no_transfer = TRUE;
1785 /* we've passed, proceed as normal */
1789 /* Send everything on data->state.in to the socket */
1790 if(data->set.ftp_append) {
1791 /* we append onto the file instead of rewriting it */
1792 FTPSENDF(conn, "APPE %s", ftp->file);
1795 FTPSENDF(conn, "STOR %s", ftp->file);
1798 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
1803 failf(data, "Failed FTP upload:%s", buf+3);
1804 /* oops, we never close the sockets! */
1805 return CURLE_FTP_COULDNT_STOR_FILE;
1808 if(data->set.ftp_use_port) {
1809 /* PORT means we are now awaiting the server to connect to us. */
1810 result = AllowServerConnect(conn);
1815 if(conn->ssl[SECONDARYSOCKET].use) {
1816 /* since we only have a plaintext TCP connection here, we must now
1818 infof(data, "Doing the SSL/TLS handshake on the data stream\n");
1819 result = Curl_SSLConnect(conn, SECONDARYSOCKET);
1826 /* When we know we're uploading a specified file, we can get the file
1827 size prior to the actual upload. */
1829 Curl_pgrsSetUploadSize(data, data->set.infilesize);
1831 result = Curl_Transfer(conn, -1, -1, FALSE, NULL, /* no download */
1832 SECONDARYSOCKET, bytecountp);
1837 else if(!data->set.no_body) {
1838 /* Retrieve file or directory */
1840 curl_off_t downloadsize=-1;
1842 if(conn->bits.use_range && conn->range) {
1843 curl_off_t from, to;
1844 curl_off_t totalsize=-1;
1848 from=strtoofft(conn->range, &ptr, 0);
1849 while(ptr && *ptr && (isspace((int)*ptr) || (*ptr=='-')))
1851 to=strtoofft(ptr, &ptr2, 0);
1853 /* we didn't get any digit */
1856 if((-1 == to) && (from>=0)) {
1858 conn->resume_from = from;
1859 infof(data, "FTP RANGE %" FORMAT_OFF_T " to end of file\n", from);
1864 conn->maxdownload = -from;
1865 conn->resume_from = from;
1866 infof(data, "FTP RANGE the last %" FORMAT_OFF_T " bytes\n", totalsize);
1870 totalsize = to-from;
1871 conn->maxdownload = totalsize+1; /* include the last mentioned byte */
1872 conn->resume_from = from;
1873 infof(data, "FTP RANGE from %" FORMAT_OFF_T
1874 " getting %" FORMAT_OFF_T " bytes\n", from, conn->maxdownload);
1876 infof(data, "range-download from %" FORMAT_OFF_T
1877 " to %" FORMAT_OFF_T ", totally %" FORMAT_OFF_T " bytes\n",
1878 from, to, conn->maxdownload);
1879 ftp->dont_check = TRUE; /* dont check for successful transfer */
1882 if((data->set.ftp_list_only) || !ftp->file) {
1883 /* The specified path ends with a slash, and therefore we think this
1884 is a directory that is requested, use LIST. But before that we
1885 need to set ASCII transfer mode. */
1888 /* Set type to ASCII */
1889 result = ftp_transfertype(conn, TRUE /* ASCII enforced */);
1893 /* if this output is to be machine-parsed, the NLST command will be
1894 better used since the LIST command output is not specified or
1895 standard in any way */
1897 FTPSENDF(conn, "%s",
1898 data->set.customrequest?data->set.customrequest:
1899 (data->set.ftp_list_only?"NLST":"LIST"));
1902 curl_off_t foundsize;
1904 /* Set type to binary (unless specified ASCII) */
1905 result = ftp_transfertype(conn, data->set.ftp_ascii);
1909 /* Send any PREQUOTE strings after transfer type is set? */
1910 if(data->set.prequote) {
1911 if ((result = ftp_sendquote(conn, data->set.prequote)) != CURLE_OK)
1915 /* Attempt to get the size, it'll be useful in some cases: for resumed
1916 downloads and when talking to servers that don't give away the size
1917 in the RETR response line. */
1918 result = ftp_getsize(conn, ftp->file, &foundsize);
1919 if(CURLE_OK == result) {
1920 if (data->set.max_filesize && foundsize > data->set.max_filesize) {
1921 failf(data, "Maximum file size exceeded");
1922 return CURLE_FILESIZE_EXCEEDED;
1924 downloadsize = foundsize;
1927 if(conn->resume_from) {
1929 /* Daniel: (August 4, 1999)
1931 * We start with trying to use the SIZE command to figure out the size
1932 * of the file we're gonna get. If we can get the size, this is by far
1933 * the best way to know if we're trying to resume beyond the EOF.
1935 * Daniel, November 28, 2001. We *always* get the size on downloads
1936 * now, so it is done before this even when not doing resumes. I saved
1937 * the comment above for nostalgical reasons! ;-)
1939 if(CURLE_OK != result) {
1940 infof(data, "ftp server doesn't support SIZE\n");
1941 /* We couldn't get the size and therefore we can't know if there
1942 really is a part of the file left to get, although the server
1943 will just close the connection when we start the connection so it
1944 won't cause us any harm, just not make us exit as nicely. */
1947 /* We got a file size report, so we check that there actually is a
1948 part of the file left to get, or else we go home. */
1949 if(conn->resume_from< 0) {
1950 /* We're supposed to download the last abs(from) bytes */
1951 if(foundsize < -conn->resume_from) {
1952 failf(data, "Offset (%" FORMAT_OFF_T
1953 ") was beyond file size (%" FORMAT_OFF_T ")",
1954 conn->resume_from, foundsize);
1955 return CURLE_FTP_BAD_DOWNLOAD_RESUME;
1957 /* convert to size to download */
1958 downloadsize = -conn->resume_from;
1959 /* download from where? */
1960 conn->resume_from = foundsize - downloadsize;
1963 if(foundsize < conn->resume_from) {
1964 failf(data, "Offset (%" FORMAT_OFF_T
1965 ") was beyond file size (%" FORMAT_OFF_T ")",
1966 conn->resume_from, foundsize);
1967 return CURLE_FTP_BAD_DOWNLOAD_RESUME;
1969 /* Now store the number of bytes we are expected to download */
1970 downloadsize = foundsize-conn->resume_from;
1974 if (downloadsize == 0) {
1975 /* no data to transfer */
1976 result=Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
1977 infof(data, "File already completely downloaded\n");
1979 /* Set no_transfer so that we won't get any error in Curl_ftp_done()
1980 * because we didn't transfer the any file */
1981 ftp->no_transfer = TRUE;
1985 /* Set resume file transfer offset */
1986 infof(data, "Instructs server to resume from offset %" FORMAT_OFF_T
1990 FTPSENDF(conn, "REST %" FORMAT_OFF_T, conn->resume_from);
1992 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
1996 if(ftpcode != 350) {
1997 failf(data, "Couldn't use REST: %s", buf+4);
1998 return CURLE_FTP_COULDNT_USE_REST;
2002 FTPSENDF(conn, "RETR %s", ftp->file);
2005 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
2009 if((ftpcode == 150) || (ftpcode == 125)) {
2013 150 Opening BINARY mode data connection for /etc/passwd (2241
2014 bytes). (ok, the file is being transfered)
2017 150 Opening ASCII mode data connection for /bin/ls
2020 150 ASCII data connection for /bin/ls (137.167.104.91,37445) (0 bytes).
2023 150 Opening ASCII mode data connection for /linux/fisk/kpanelrc (0.0.0.0,0) (545 bytes).
2026 125 Data connection already open; Transfer starting. */
2028 curl_off_t size=-1; /* default unknown size */
2032 * It appears that there are FTP-servers that return size 0 for files
2033 * when SIZE is used on the file while being in BINARY mode. To work
2034 * around that (stupid) behavior, we attempt to parse the RETR response
2035 * even if the SIZE returned size zero.
2037 * Debugging help from Salvatore Sorrentino on February 26, 2003.
2041 !data->set.ftp_ascii &&
2042 (downloadsize < 1)) {
2044 * It seems directory listings either don't show the size or very
2045 * often uses size 0 anyway. ASCII transfers may very well turn out
2046 * that the transfered amount of data is not the same as this line
2047 * tells, why using this number in those cases only confuses us.
2049 * Example D above makes this parsing a little tricky */
2051 bytes=strstr(buf, " bytes");
2054 /* this is a hint there is size information in there! ;-) */
2056 /* scan for the parenthesis and break there */
2059 /* if only skip digits, or else we're in deep trouble */
2060 if(!isdigit((int)*bytes)) {
2064 /* one more estep backwards */
2067 /* only if we have nothing but digits: */
2069 /* get the number! */
2070 size = strtoofft(bytes, NULL, 0);
2075 else if(downloadsize > -1)
2076 size = downloadsize;
2078 if(data->set.ftp_use_port) {
2079 result = AllowServerConnect(conn);
2084 if(conn->ssl[SECONDARYSOCKET].use) {
2085 /* since we only have a plaintext TCP connection here, we must now
2087 infof(data, "Doing the SSL/TLS handshake on the data stream\n");
2088 result = Curl_SSLConnect(conn, SECONDARYSOCKET);
2093 if(size > conn->maxdownload && conn->maxdownload > 0)
2094 size = conn->size = conn->maxdownload;
2096 infof(data, "Getting file with size: %" FORMAT_OFF_T "\n", size);
2099 result=Curl_Transfer(conn, SECONDARYSOCKET, size, FALSE,
2101 -1, NULL); /* no upload here */
2106 if(dirlist && (ftpcode == 450)) {
2107 /* simply no matching files */
2108 ftp->no_transfer = TRUE; /* don't think we should download anything */
2111 failf(data, "%s", buf+4);
2112 return CURLE_FTP_COULDNT_RETR_FILE;
2117 /* end of transfer */
2122 /***********************************************************************
2126 * This is the actual DO function for FTP. Get a file/directory according to
2127 * the options previously setup.
2131 CURLcode ftp_perform(struct connectdata *conn,
2132 bool *connected) /* for the TCP connect status after
2135 /* this is FTP and no proxy */
2136 CURLcode result=CURLE_OK;
2137 struct SessionHandle *data=conn->data;
2138 char *buf = data->state.buffer; /* this is our buffer */
2140 /* the ftp struct is already inited in Curl_ftp_connect() */
2141 struct FTP *ftp = conn->proto.ftp;
2143 /* Send any QUOTE strings? */
2144 if(data->set.quote) {
2145 if ((result = ftp_sendquote(conn, data->set.quote)) != CURLE_OK)
2149 /* This is a re-used connection. Since we change directory to where the
2150 transfer is taking place, we must now get back to the original dir
2151 where we ended up after login: */
2152 if (conn->bits.reuse && ftp->entrypath) {
2153 if ((result = ftp_cwd_and_mkd(conn, ftp->entrypath)) != CURLE_OK)
2158 int i; /* counter for loop */
2159 for (i=0; ftp->dirs[i]; i++) {
2160 /* RFC 1738 says empty components should be respected too, but
2161 that is plain stupid since CWD can't be used with an empty argument */
2162 if ((result = ftp_cwd_and_mkd(conn, ftp->dirs[i])) != CURLE_OK)
2167 /* Requested time of file or time-depended transfer? */
2168 if((data->set.get_filetime || data->set.timecondition) &&
2170 result = ftp_getfiletime(conn, ftp->file);
2173 case CURLE_FTP_COULDNT_RETR_FILE:
2175 if(data->set.timecondition) {
2176 if((data->info.filetime > 0) && (data->set.timevalue > 0)) {
2177 switch(data->set.timecondition) {
2178 case CURL_TIMECOND_IFMODSINCE:
2180 if(data->info.filetime < data->set.timevalue) {
2181 infof(data, "The requested document is not new enough\n");
2182 ftp->no_transfer = TRUE; /* mark this to not transfer data */
2186 case CURL_TIMECOND_IFUNMODSINCE:
2187 if(data->info.filetime > data->set.timevalue) {
2188 infof(data, "The requested document is not old enough\n");
2189 ftp->no_transfer = TRUE; /* mark this to not transfer data */
2196 infof(data, "Skipping time comparison\n");
2205 /* If we have selected NOBODY and HEADER, it means that we only want file
2206 information. Which in FTP can't be much more than the file size and
2208 if(data->set.no_body && data->set.include_header && ftp->file) {
2209 /* The SIZE command is _not_ RFC 959 specified, and therefor many servers
2210 may not support it! It is however the only way we have to get a file's
2212 curl_off_t filesize;
2216 ftp->no_transfer = TRUE; /* this means no actual transfer is made */
2218 /* Some servers return different sizes for different modes, and thus we
2219 must set the proper type before we check the size */
2220 result = ftp_transfertype(conn, data->set.ftp_ascii);
2224 /* failing to get size is not a serious error */
2225 result = ftp_getsize(conn, ftp->file, &filesize);
2227 if(CURLE_OK == result) {
2228 sprintf(buf, "Content-Length: %" FORMAT_OFF_T "\r\n", filesize);
2229 result = Curl_client_write(data, CLIENTWRITE_BOTH, buf, 0);
2234 /* Determine if server can respond to REST command and therefore
2235 whether it can do a range */
2236 FTPSENDF(conn, "REST 0", NULL);
2237 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
2239 if ((CURLE_OK == result) && (ftpcode == 350)) {
2240 result = Curl_client_write(data, CLIENTWRITE_BOTH,
2241 (char *)"Accept-ranges: bytes\r\n", 0);
2246 /* If we asked for a time of the file and we actually got one as
2247 well, we "emulate" a HTTP-style header in our output. */
2249 #ifdef HAVE_STRFTIME
2250 if(data->set.get_filetime && (data->info.filetime>=0) ) {
2252 time_t clock = (time_t)data->info.filetime;
2253 #ifdef HAVE_GMTIME_R
2255 tm = (struct tm *)gmtime_r(&clock, &buffer);
2257 tm = gmtime(&clock);
2259 /* format: "Tue, 15 Nov 1994 12:45:26" */
2260 strftime(buf, BUFSIZE-1, "Last-Modified: %a, %d %b %Y %H:%M:%S GMT\r\n",
2262 result = Curl_client_write(data, CLIENTWRITE_BOTH, buf, 0);
2271 if(data->set.no_body)
2272 /* doesn't really transfer any data */
2273 ftp->no_transfer = TRUE;
2274 /* Get us a second connection up and connected */
2275 else if(data->set.ftp_use_port) {
2276 /* We have chosen to use the PORT command */
2277 result = ftp_use_port(conn);
2278 if(CURLE_OK == result) {
2279 /* we have the data connection ready */
2280 infof(data, "Ordered connect of the data stream with PORT!\n");
2281 *connected = TRUE; /* mark us "still connected" */
2285 /* We have chosen (this is default) to use the PASV command */
2286 result = ftp_use_pasv(conn, connected);
2287 if(CURLE_OK == result && *connected)
2288 infof(data, "Connected the data stream with PASV!\n");
2294 /***********************************************************************
2298 * This function is registered as 'curl_do' function. It decodes the path
2299 * parts etc as a wrapper to the actual DO function (ftp_perform).
2301 * The input argument is already checked for validity.
2303 * ftp->ctl_valid starts out as FALSE, and gets set to TRUE if we reach the
2304 * end of the function.
2306 CURLcode Curl_ftp(struct connectdata *conn)
2308 CURLcode retcode=CURLE_OK;
2310 struct SessionHandle *data = conn->data;
2313 char *slash_pos; /* position of the first '/' char in curpos */
2314 char *cur_pos=conn->ppath; /* current position in ppath. point at the begin
2315 of next path component */
2316 int path_part=0;/* current path component */
2318 /* the ftp struct is already inited in ftp_connect() */
2319 ftp = conn->proto.ftp;
2320 ftp->ctl_valid = FALSE;
2321 conn->size = -1; /* make sure this is unknown at this point */
2323 Curl_pgrsSetUploadCounter(data, 0);
2324 Curl_pgrsSetDownloadCounter(data, 0);
2325 Curl_pgrsSetUploadSize(data, 0);
2326 Curl_pgrsSetDownloadSize(data, 0);
2328 /* fixed : initialize ftp->dirs[xxx] to NULL !
2329 is done in Curl_ftp_connect() */
2331 /* parse the URL path into separate path components */
2332 while((slash_pos=strchr(cur_pos, '/'))) {
2333 /* 1 or 0 to indicate absolute directory */
2334 bool absolute_dir = (cur_pos - conn->ppath > 0) && (path_part == 0);
2336 /* seek out the next path component */
2337 if (slash_pos-cur_pos) {
2338 /* we skip empty path components, like "x//y" since the FTP command CWD
2339 requires a parameter and a non-existant parameter a) doesn't work on
2340 many servers and b) has no effect on the others. */
2341 ftp->dirs[path_part] = curl_unescape(cur_pos - absolute_dir,
2342 slash_pos - cur_pos + absolute_dir);
2344 if (!ftp->dirs[path_part]) { /* run out of memory ... */
2345 failf(data, "no memory");
2347 return CURLE_OUT_OF_MEMORY;
2351 cur_pos = slash_pos + 1; /* jump to the rest of the string */
2356 cur_pos = slash_pos + 1; /* jump to the rest of the string */
2357 if(++path_part >= (CURL_MAX_FTP_DIRDEPTH-1)) {
2358 /* too deep, we need the last entry to be kept NULL at all
2359 times to signal end of list */
2360 failf(data, "too deep dir hierarchy");
2362 return CURLE_URL_MALFORMAT;
2367 ftp->file = cur_pos; /* the rest is the file name */
2370 ftp->file = curl_unescape(ftp->file, 0);
2371 if(NULL == ftp->file) {
2373 failf(data, "no memory");
2374 return CURLE_OUT_OF_MEMORY;
2378 ftp->file=NULL; /* instead of point to a zero byte, we make it a NULL
2381 retcode = ftp_perform(conn, &connected);
2383 if(CURLE_OK == retcode) {
2385 retcode = Curl_ftp_nextconnect(conn);
2387 if(retcode && (conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD)) {
2388 /* Failure detected, close the second socket if it was created already */
2389 sclose(conn->sock[SECONDARYSOCKET]);
2390 conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
2393 if(ftp->no_transfer)
2394 /* no data to transfer */
2395 retcode=Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
2397 /* since we didn't connect now, we want do_more to get called */
2398 conn->bits.do_more = TRUE;
2403 ftp->ctl_valid = TRUE;
2407 /***********************************************************************
2411 * Sends the formated string as a ftp command to a ftp server
2413 * NOTE: we build the command in a fixed-length buffer, which sets length
2414 * restrictions on the command!
2416 CURLcode Curl_ftpsendf(struct connectdata *conn,
2417 const char *fmt, ...)
2419 ssize_t bytes_written;
2423 CURLcode res = CURLE_OK;
2427 vsnprintf(s, 250, fmt, ap);
2430 strcat(s, "\r\n"); /* append a trailing CRLF */
2433 write_len = strlen(s);
2436 res = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
2442 if(conn->data->set.verbose)
2443 Curl_debug(conn->data, CURLINFO_HEADER_OUT, sptr, bytes_written);
2445 if(bytes_written != (ssize_t)write_len) {
2446 write_len -= bytes_written;
2447 sptr += bytes_written;
2456 /***********************************************************************
2460 * This should be called before calling sclose() on an ftp control connection
2461 * (not data connections). We should then wait for the response from the
2462 * server before returning. The calling code should then try to close the
2466 CURLcode Curl_ftp_quit(struct connectdata *conn)
2470 CURLcode ret = CURLE_OK;
2472 if(conn->proto.ftp->ctl_valid) {
2473 ret = Curl_ftpsendf(conn, "%s", "QUIT");
2475 ret = Curl_GetFTPResponse(&nread, conn, &ftpcode);
2481 /***********************************************************************
2483 * Curl_ftp_disconnect()
2485 * Disconnect from an FTP server. Cleanup protocol-specific per-connection
2488 CURLcode Curl_ftp_disconnect(struct connectdata *conn)
2490 struct FTP *ftp= conn->proto.ftp;
2492 /* We cannot send quit unconditionally. If this connection is stale or
2493 bad in any way, sending quit and waiting around here will make the
2494 disconnect wait in vain and cause more problems than we need to.
2496 Curl_ftp_quit() will check the state of ftp->ctl_valid. If it's ok it
2497 will try to send the QUIT command, otherwise it will just return.
2500 /* The FTP session may or may not have been allocated/setup at this point! */
2502 (void)Curl_ftp_quit(conn); /* ignore errors on the QUIT */
2505 free(ftp->entrypath);
2512 ftp->file = NULL; /* zero */
2519 /***********************************************************************
2523 * Makes a directory on the FTP server.
2527 CURLcode ftp_mkd(struct connectdata *conn, char *path)
2529 CURLcode result=CURLE_OK;
2530 int ftpcode; /* for ftp status */
2533 /* Create a directory on the remote server */
2534 FTPSENDF(conn, "MKD %s", path);
2536 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
2543 infof( conn->data , "Created remote directory %s\n" , path );
2546 failf(conn->data, "Permission denied to make directory %s", path);
2547 result = CURLE_FTP_ACCESS_DENIED;
2550 failf(conn->data, "unrecognized MKD response: %d", ftpcode );
2551 result = CURLE_FTP_ACCESS_DENIED;
2557 /***********************************************************************
2561 * Send 'CWD' to the remote server to Change Working Directory. It is the ftp
2562 * version of the unix 'cd' command. This function is only called from the
2563 * ftp_cwd_and_mkd() function these days.
2565 * This function does NOT call failf().
2568 CURLcode ftp_cwd(struct connectdata *conn, char *path)
2574 FTPSENDF(conn, "CWD %s", path);
2575 result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
2577 /* According to RFC959, CWD is supposed to return 250 on success, but
2578 there seem to be non-compliant FTP servers out there that return 200,
2579 so we accept any '2xy' code here. */
2580 if (ftpcode/100 != 2)
2581 result = CURLE_FTP_ACCESS_DENIED;
2587 /***********************************************************************
2591 * Change to the given directory. If the directory is not present, and we
2592 * have been told to allow it, then create the directory and cd to it.
2595 static CURLcode ftp_cwd_and_mkd(struct connectdata *conn, char *path)
2599 result = ftp_cwd(conn, path);
2601 if(conn->data->set.ftp_create_missing_dirs) {
2602 result = ftp_mkd(conn, path);
2604 /* ftp_mkd() calls failf() itself */
2606 result = ftp_cwd(conn, path);
2609 failf(conn->data, "Couldn't cd to %s", path);
2614 #endif /* CURL_DISABLE_FTP */