1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/ftp/ftp_network_transaction.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/metrics/histogram.h"
11 #include "base/string_number_conversions.h"
12 #include "base/string_util.h"
13 #include "base/strings/string_split.h"
14 #include "base/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "net/base/address_list.h"
17 #include "net/base/connection_type_histograms.h"
18 #include "net/base/escape.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/net_log.h"
21 #include "net/base/net_util.h"
22 #include "net/ftp/ftp_network_session.h"
23 #include "net/ftp/ftp_request_info.h"
24 #include "net/ftp/ftp_util.h"
25 #include "net/socket/client_socket_factory.h"
26 #include "net/socket/stream_socket.h"
28 const char kCRLF
[] = "\r\n";
30 const int kCtrlBufLen
= 1024;
34 // Returns true if |input| can be safely used as a part of FTP command.
35 bool IsValidFTPCommandString(const std::string
& input
) {
36 // RFC 959 only allows ASCII strings, but at least Firefox can send non-ASCII
37 // characters in the command if the request path contains them. To be
38 // compatible, we do the same and allow non-ASCII characters in a command.
40 // Protect agains newline injection attack.
41 if (input
.find_first_of("\r\n") != std::string::npos
)
48 // The requested action was initiated. The client should expect another
49 // reply before issuing the next command.
50 ERROR_CLASS_INITIATED
,
52 // The requested action has been successfully completed.
55 // The command has been accepted, but to complete the operation, more
56 // information must be sent by the client.
57 ERROR_CLASS_INFO_NEEDED
,
59 // The command was not accepted and the requested action did not take place.
60 // This condition is temporary, and the client is encouraged to restart the
62 ERROR_CLASS_TRANSIENT_ERROR
,
64 // The command was not accepted and the requested action did not take place.
65 // This condition is rather permanent, and the client is discouraged from
66 // repeating the exact request.
67 ERROR_CLASS_PERMANENT_ERROR
,
70 // Returns the error class for given response code. Caller should ensure
71 // that |response_code| is in range 100-599.
72 ErrorClass
GetErrorClass(int response_code
) {
73 if (response_code
>= 100 && response_code
<= 199)
74 return ERROR_CLASS_INITIATED
;
76 if (response_code
>= 200 && response_code
<= 299)
77 return ERROR_CLASS_OK
;
79 if (response_code
>= 300 && response_code
<= 399)
80 return ERROR_CLASS_INFO_NEEDED
;
82 if (response_code
>= 400 && response_code
<= 499)
83 return ERROR_CLASS_TRANSIENT_ERROR
;
85 if (response_code
>= 500 && response_code
<= 599)
86 return ERROR_CLASS_PERMANENT_ERROR
;
88 // We should not be called on invalid error codes.
89 NOTREACHED() << response_code
;
90 return ERROR_CLASS_PERMANENT_ERROR
;
93 // Returns network error code for received FTP |response_code|.
94 int GetNetErrorCodeForFtpResponseCode(int response_code
) {
95 switch (response_code
) {
97 return net::ERR_FTP_SERVICE_UNAVAILABLE
;
99 return net::ERR_FTP_TRANSFER_ABORTED
;
101 return net::ERR_FTP_FILE_BUSY
;
104 return net::ERR_FTP_SYNTAX_ERROR
;
107 return net::ERR_FTP_COMMAND_NOT_SUPPORTED
;
109 return net::ERR_FTP_BAD_COMMAND_SEQUENCE
;
111 return net::ERR_FTP_FAILED
;
115 // From RFC 2428 Section 3:
116 // The text returned in response to the EPSV command MUST be:
117 // <some text> (<d><d><d><tcp-port><d>)
118 // <d> is a delimiter character, ideally to be |
119 bool ExtractPortFromEPSVResponse(const net::FtpCtrlResponse
& response
,
121 if (response
.lines
.size() != 1)
123 const char* ptr
= response
.lines
[0].c_str();
124 while (*ptr
&& *ptr
!= '(')
129 if (!sep
|| isdigit(sep
) || *(++ptr
) != sep
|| *(++ptr
) != sep
)
131 if (!isdigit(*(++ptr
)))
134 while (isdigit(*(++ptr
))) {
144 // There are two way we can receive IP address and port.
145 // (127,0,0,1,23,21) IP address and port encapsulated in ().
146 // 127,0,0,1,23,21 IP address and port without ().
148 // See RFC 959, Section 4.1.2
149 bool ExtractPortFromPASVResponse(const net::FtpCtrlResponse
& response
,
151 if (response
.lines
.size() != 1)
154 std::string
line(response
.lines
[0]);
155 if (!IsStringASCII(line
))
157 if (line
.length() < 2)
160 size_t paren_pos
= line
.find('(');
161 if (paren_pos
== std::string::npos
) {
162 // Find the first comma and use it to locate the beginning
163 // of the response data.
164 size_t comma_pos
= line
.find(',');
165 if (comma_pos
== std::string::npos
)
168 size_t space_pos
= line
.rfind(' ', comma_pos
);
169 if (space_pos
!= std::string::npos
)
170 line
= line
.substr(space_pos
+ 1);
172 // Remove the parentheses and use the text inside them.
173 size_t closing_paren_pos
= line
.rfind(')');
174 if (closing_paren_pos
== std::string::npos
)
176 if (closing_paren_pos
<= paren_pos
)
179 line
= line
.substr(paren_pos
+ 1, closing_paren_pos
- paren_pos
- 1);
182 // Split the line into comma-separated pieces and extract
184 std::vector
<std::string
> pieces
;
185 base::SplitString(line
, ',', &pieces
);
186 if (pieces
.size() != 6)
189 // Ignore the IP address supplied in the response. We are always going
190 // to connect back to the same server to prevent FTP PASV port scanning.
192 if (!base::StringToInt(pieces
[4], &p0
))
194 if (!base::StringToInt(pieces
[5], &p1
))
196 *port
= (p0
<< 8) + p1
;
205 FtpNetworkTransaction::FtpNetworkTransaction(
206 FtpNetworkSession
* session
,
207 ClientSocketFactory
* socket_factory
)
208 : command_sent_(COMMAND_NONE
),
209 ALLOW_THIS_IN_INITIALIZER_LIST(
210 io_callback_(base::Bind(&FtpNetworkTransaction::OnIOComplete
,
211 base::Unretained(this)))),
214 resolver_(session
->host_resolver()),
215 read_ctrl_buf_(new IOBuffer(kCtrlBufLen
)),
216 ctrl_response_buffer_(NULL
),
217 read_data_buf_len_(0),
219 system_type_(SYSTEM_TYPE_UNKNOWN
),
220 // Use image (binary) transfer by default. It should always work,
221 // whereas the ascii transfer may damage binary data.
222 data_type_(DATA_TYPE_IMAGE
),
223 resource_type_(RESOURCE_TYPE_UNKNOWN
),
225 data_connection_port_(0),
226 socket_factory_(socket_factory
),
227 next_state_(STATE_NONE
),
228 state_after_data_connect_complete_(STATE_CTRL_WRITE_SIZE
) {
231 FtpNetworkTransaction::~FtpNetworkTransaction() {
234 int FtpNetworkTransaction::Stop(int error
) {
235 if (command_sent_
== COMMAND_QUIT
)
238 next_state_
= STATE_CTRL_WRITE_QUIT
;
243 int FtpNetworkTransaction::RestartIgnoringLastError(
244 const CompletionCallback
& callback
) {
245 return ERR_NOT_IMPLEMENTED
;
248 int FtpNetworkTransaction::Start(const FtpRequestInfo
* request_info
,
249 const CompletionCallback
& callback
,
250 const BoundNetLog
& net_log
) {
252 request_
= request_info
;
254 ctrl_response_buffer_
.reset(new FtpCtrlResponseBuffer(net_log_
));
256 if (request_
->url
.has_username()) {
257 base::string16 username
;
258 base::string16 password
;
259 GetIdentityFromURL(request_
->url
, &username
, &password
);
260 credentials_
.Set(username
, password
);
262 credentials_
.Set(ASCIIToUTF16("anonymous"),
263 ASCIIToUTF16("chrome@example.com"));
268 next_state_
= STATE_CTRL_RESOLVE_HOST
;
270 if (rv
== ERR_IO_PENDING
)
271 user_callback_
= callback
;
275 int FtpNetworkTransaction::RestartWithAuth(const AuthCredentials
& credentials
,
276 const CompletionCallback
& callback
) {
277 ResetStateForRestart();
279 credentials_
= credentials
;
281 next_state_
= STATE_CTRL_RESOLVE_HOST
;
283 if (rv
== ERR_IO_PENDING
)
284 user_callback_
= callback
;
288 int FtpNetworkTransaction::Read(IOBuffer
* buf
,
290 const CompletionCallback
& callback
) {
292 DCHECK_GT(buf_len
, 0);
294 read_data_buf_
= buf
;
295 read_data_buf_len_
= buf_len
;
297 next_state_
= STATE_DATA_READ
;
299 if (rv
== ERR_IO_PENDING
)
300 user_callback_
= callback
;
304 const FtpResponseInfo
* FtpNetworkTransaction::GetResponseInfo() const {
308 LoadState
FtpNetworkTransaction::GetLoadState() const {
309 if (next_state_
== STATE_CTRL_RESOLVE_HOST_COMPLETE
)
310 return LOAD_STATE_RESOLVING_HOST
;
312 if (next_state_
== STATE_CTRL_CONNECT_COMPLETE
||
313 next_state_
== STATE_DATA_CONNECT_COMPLETE
)
314 return LOAD_STATE_CONNECTING
;
316 if (next_state_
== STATE_DATA_READ_COMPLETE
)
317 return LOAD_STATE_READING_RESPONSE
;
319 if (command_sent_
== COMMAND_RETR
&& read_data_buf_
.get())
320 return LOAD_STATE_READING_RESPONSE
;
322 if (command_sent_
== COMMAND_QUIT
)
323 return LOAD_STATE_IDLE
;
325 if (command_sent_
!= COMMAND_NONE
)
326 return LOAD_STATE_SENDING_REQUEST
;
328 return LOAD_STATE_IDLE
;
331 uint64
FtpNetworkTransaction::GetUploadProgress() const {
335 void FtpNetworkTransaction::ResetStateForRestart() {
336 command_sent_
= COMMAND_NONE
;
337 user_callback_
.Reset();
338 response_
= FtpResponseInfo();
339 read_ctrl_buf_
= new IOBuffer(kCtrlBufLen
);
340 ctrl_response_buffer_
.reset(new FtpCtrlResponseBuffer(net_log_
));
341 read_data_buf_
= NULL
;
342 read_data_buf_len_
= 0;
344 write_buf_
->SetOffset(0);
346 data_connection_port_
= 0;
347 ctrl_socket_
.reset();
348 data_socket_
.reset();
349 next_state_
= STATE_NONE
;
350 state_after_data_connect_complete_
= STATE_CTRL_WRITE_SIZE
;
353 void FtpNetworkTransaction::ResetDataConnectionAfterError(State next_state
) {
354 // The server _might_ have reset the data connection
355 // (see RFC 959 3.2. ESTABLISHING DATA CONNECTIONS:
356 // "The server MUST close the data connection under the following
359 // 5. An irrecoverable error condition occurs.")
361 // It is ambiguous what an irrecoverable error condition is,
362 // so we take no chances.
363 state_after_data_connect_complete_
= next_state
;
364 next_state_
= use_epsv_
? STATE_CTRL_WRITE_EPSV
: STATE_CTRL_WRITE_PASV
;
367 void FtpNetworkTransaction::DoCallback(int rv
) {
368 DCHECK(rv
!= ERR_IO_PENDING
);
369 DCHECK(!user_callback_
.is_null());
371 // Since Run may result in Read being called, clear callback_ up front.
372 CompletionCallback c
= user_callback_
;
373 user_callback_
.Reset();
377 void FtpNetworkTransaction::OnIOComplete(int result
) {
378 int rv
= DoLoop(result
);
379 if (rv
!= ERR_IO_PENDING
)
383 int FtpNetworkTransaction::ProcessCtrlResponse() {
384 FtpCtrlResponse response
= ctrl_response_buffer_
->PopResponse();
387 switch (command_sent_
) {
389 // TODO(phajdan.jr): Check for errors in the welcome message.
390 next_state_
= STATE_CTRL_WRITE_USER
;
393 rv
= ProcessResponseUSER(response
);
396 rv
= ProcessResponsePASS(response
);
399 rv
= ProcessResponseSYST(response
);
402 rv
= ProcessResponsePWD(response
);
405 rv
= ProcessResponseTYPE(response
);
408 rv
= ProcessResponseEPSV(response
);
411 rv
= ProcessResponsePASV(response
);
414 rv
= ProcessResponseSIZE(response
);
417 rv
= ProcessResponseRETR(response
);
420 rv
= ProcessResponseCWD(response
);
423 rv
= ProcessResponseLIST(response
);
426 rv
= ProcessResponseQUIT(response
);
429 LOG(DFATAL
) << "Unexpected value of command_sent_: " << command_sent_
;
430 return ERR_UNEXPECTED
;
433 // We may get multiple responses for some commands,
434 // see http://crbug.com/18036.
435 while (ctrl_response_buffer_
->ResponseAvailable() && rv
== OK
) {
436 response
= ctrl_response_buffer_
->PopResponse();
438 switch (command_sent_
) {
440 rv
= ProcessResponseRETR(response
);
443 rv
= ProcessResponseLIST(response
);
446 // Multiple responses for other commands are invalid.
447 return Stop(ERR_INVALID_RESPONSE
);
454 // Used to prepare and send FTP command.
455 int FtpNetworkTransaction::SendFtpCommand(const std::string
& command
,
456 const std::string
& command_for_log
,
458 // If we send a new command when we still have unprocessed responses
459 // for previous commands, the response receiving code will have no way to know
460 // which responses are for which command.
461 DCHECK(!ctrl_response_buffer_
->ResponseAvailable());
463 DCHECK(!write_command_buf_
);
466 if (!IsValidFTPCommandString(command
)) {
467 // Callers should validate the command themselves and return a more specific
470 return Stop(ERR_UNEXPECTED
);
475 write_command_buf_
= new IOBufferWithSize(command
.length() + 2);
476 write_buf_
= new DrainableIOBuffer(write_command_buf_
,
477 write_command_buf_
->size());
478 memcpy(write_command_buf_
->data(), command
.data(), command
.length());
479 memcpy(write_command_buf_
->data() + command
.length(), kCRLF
, 2);
481 net_log_
.AddEvent(NetLog::TYPE_FTP_COMMAND_SENT
,
482 NetLog::StringCallback("command", &command_for_log
));
484 next_state_
= STATE_CTRL_WRITE
;
488 std::string
FtpNetworkTransaction::GetRequestPathForFtpCommand(
489 bool is_directory
) const {
490 std::string
path(current_remote_directory_
);
491 if (request_
->url
.has_path()) {
492 std::string
gurl_path(request_
->url
.path());
494 // Get rid of the typecode, see RFC 1738 section 3.2.2. FTP url-path.
495 std::string::size_type pos
= gurl_path
.rfind(';');
496 if (pos
!= std::string::npos
)
497 gurl_path
.resize(pos
);
499 path
.append(gurl_path
);
501 // Make sure that if the path is expected to be a file, it won't end
502 // with a trailing slash.
503 if (!is_directory
&& path
.length() > 1 && path
[path
.length() - 1] == '/')
504 path
.erase(path
.length() - 1);
505 UnescapeRule::Type unescape_rules
= UnescapeRule::SPACES
|
506 UnescapeRule::URL_SPECIAL_CHARS
;
507 // This may unescape to non-ASCII characters, but we allow that. See the
508 // comment for IsValidFTPCommandString.
509 path
= net::UnescapeURLComponent(path
, unescape_rules
);
511 if (system_type_
== SYSTEM_TYPE_VMS
) {
513 path
= FtpUtil::UnixDirectoryPathToVMS(path
);
515 path
= FtpUtil::UnixFilePathToVMS(path
);
518 DCHECK(IsValidFTPCommandString(path
));
522 void FtpNetworkTransaction::DetectTypecode() {
523 if (!request_
->url
.has_path())
525 std::string
gurl_path(request_
->url
.path());
527 // Extract the typecode, see RFC 1738 section 3.2.2. FTP url-path.
528 std::string::size_type pos
= gurl_path
.rfind(';');
529 if (pos
== std::string::npos
)
531 std::string
typecode_string(gurl_path
.substr(pos
));
532 if (typecode_string
== ";type=a") {
533 data_type_
= DATA_TYPE_ASCII
;
534 resource_type_
= RESOURCE_TYPE_FILE
;
535 } else if (typecode_string
== ";type=i") {
536 data_type_
= DATA_TYPE_IMAGE
;
537 resource_type_
= RESOURCE_TYPE_FILE
;
538 } else if (typecode_string
== ";type=d") {
539 resource_type_
= RESOURCE_TYPE_DIRECTORY
;
543 int FtpNetworkTransaction::DoLoop(int result
) {
544 DCHECK(next_state_
!= STATE_NONE
);
548 State state
= next_state_
;
549 next_state_
= STATE_NONE
;
551 case STATE_CTRL_RESOLVE_HOST
:
553 rv
= DoCtrlResolveHost();
555 case STATE_CTRL_RESOLVE_HOST_COMPLETE
:
556 rv
= DoCtrlResolveHostComplete(rv
);
558 case STATE_CTRL_CONNECT
:
560 rv
= DoCtrlConnect();
562 case STATE_CTRL_CONNECT_COMPLETE
:
563 rv
= DoCtrlConnectComplete(rv
);
565 case STATE_CTRL_READ
:
569 case STATE_CTRL_READ_COMPLETE
:
570 rv
= DoCtrlReadComplete(rv
);
572 case STATE_CTRL_WRITE
:
576 case STATE_CTRL_WRITE_COMPLETE
:
577 rv
= DoCtrlWriteComplete(rv
);
579 case STATE_CTRL_WRITE_USER
:
581 rv
= DoCtrlWriteUSER();
583 case STATE_CTRL_WRITE_PASS
:
585 rv
= DoCtrlWritePASS();
587 case STATE_CTRL_WRITE_SYST
:
589 rv
= DoCtrlWriteSYST();
591 case STATE_CTRL_WRITE_PWD
:
593 rv
= DoCtrlWritePWD();
595 case STATE_CTRL_WRITE_TYPE
:
597 rv
= DoCtrlWriteTYPE();
599 case STATE_CTRL_WRITE_EPSV
:
601 rv
= DoCtrlWriteEPSV();
603 case STATE_CTRL_WRITE_PASV
:
605 rv
= DoCtrlWritePASV();
607 case STATE_CTRL_WRITE_RETR
:
609 rv
= DoCtrlWriteRETR();
611 case STATE_CTRL_WRITE_SIZE
:
613 rv
= DoCtrlWriteSIZE();
615 case STATE_CTRL_WRITE_CWD
:
617 rv
= DoCtrlWriteCWD();
619 case STATE_CTRL_WRITE_LIST
:
621 rv
= DoCtrlWriteLIST();
623 case STATE_CTRL_WRITE_QUIT
:
625 rv
= DoCtrlWriteQUIT();
627 case STATE_DATA_CONNECT
:
629 rv
= DoDataConnect();
631 case STATE_DATA_CONNECT_COMPLETE
:
632 rv
= DoDataConnectComplete(rv
);
634 case STATE_DATA_READ
:
638 case STATE_DATA_READ_COMPLETE
:
639 rv
= DoDataReadComplete(rv
);
642 NOTREACHED() << "bad state";
646 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
650 int FtpNetworkTransaction::DoCtrlResolveHost() {
651 next_state_
= STATE_CTRL_RESOLVE_HOST_COMPLETE
;
653 HostResolver::RequestInfo
info(HostPortPair::FromURL(request_
->url
));
654 // No known referrer.
655 return resolver_
.Resolve(
657 base::Bind(&FtpNetworkTransaction::OnIOComplete
, base::Unretained(this)),
661 int FtpNetworkTransaction::DoCtrlResolveHostComplete(int result
) {
663 next_state_
= STATE_CTRL_CONNECT
;
667 int FtpNetworkTransaction::DoCtrlConnect() {
668 next_state_
= STATE_CTRL_CONNECT_COMPLETE
;
669 ctrl_socket_
.reset(socket_factory_
->CreateTransportClientSocket(
670 addresses_
, net_log_
.net_log(), net_log_
.source()));
672 NetLog::TYPE_FTP_CONTROL_CONNECTION
,
673 ctrl_socket_
->NetLog().source().ToEventParametersCallback());
674 return ctrl_socket_
->Connect(io_callback_
);
677 int FtpNetworkTransaction::DoCtrlConnectComplete(int result
) {
679 // Put the peer's IP address and port into the response.
680 IPEndPoint ip_endpoint
;
681 result
= ctrl_socket_
->GetPeerAddress(&ip_endpoint
);
683 response_
.socket_address
= HostPortPair::FromIPEndPoint(ip_endpoint
);
684 next_state_
= STATE_CTRL_READ
;
686 if (ip_endpoint
.GetFamily() == ADDRESS_FAMILY_IPV4
) {
687 // Do not use EPSV for IPv4 connections. Some servers become confused
688 // and we time out while waiting to connect. PASV is perfectly fine for
689 // IPv4. Note that this blacklists IPv4 not to use EPSV instead of
690 // whitelisting IPv6 to use it, to make the code more future-proof:
691 // all future protocols should just use EPSV.
699 int FtpNetworkTransaction::DoCtrlRead() {
700 next_state_
= STATE_CTRL_READ_COMPLETE
;
701 return ctrl_socket_
->Read(read_ctrl_buf_
, kCtrlBufLen
, io_callback_
);
704 int FtpNetworkTransaction::DoCtrlReadComplete(int result
) {
706 // Some servers (for example Pure-FTPd) apparently close the control
707 // connection when anonymous login is not permitted. For more details
708 // see http://crbug.com/25023.
709 if (command_sent_
== COMMAND_USER
&&
710 credentials_
.username() == ASCIIToUTF16("anonymous")) {
711 response_
.needs_auth
= true;
713 return Stop(ERR_EMPTY_RESPONSE
);
718 ctrl_response_buffer_
->ConsumeData(read_ctrl_buf_
->data(), result
);
720 if (!ctrl_response_buffer_
->ResponseAvailable()) {
721 // Read more data from the control socket.
722 next_state_
= STATE_CTRL_READ
;
726 return ProcessCtrlResponse();
729 int FtpNetworkTransaction::DoCtrlWrite() {
730 next_state_
= STATE_CTRL_WRITE_COMPLETE
;
732 return ctrl_socket_
->Write(write_buf_
,
733 write_buf_
->BytesRemaining(),
737 int FtpNetworkTransaction::DoCtrlWriteComplete(int result
) {
741 write_buf_
->DidConsume(result
);
742 if (write_buf_
->BytesRemaining() == 0) {
743 // Clear the write buffer.
745 write_command_buf_
= NULL
;
747 next_state_
= STATE_CTRL_READ
;
749 next_state_
= STATE_CTRL_WRITE
;
754 // FTP Commands and responses
757 int FtpNetworkTransaction::DoCtrlWriteUSER() {
758 std::string command
= "USER " + UTF16ToUTF8(credentials_
.username());
760 if (!IsValidFTPCommandString(command
))
761 return Stop(ERR_MALFORMED_IDENTITY
);
763 next_state_
= STATE_CTRL_READ
;
764 return SendFtpCommand(command
, "USER ***", COMMAND_USER
);
767 int FtpNetworkTransaction::ProcessResponseUSER(
768 const FtpCtrlResponse
& response
) {
769 switch (GetErrorClass(response
.status_code
)) {
771 next_state_
= STATE_CTRL_WRITE_SYST
;
773 case ERROR_CLASS_INFO_NEEDED
:
774 next_state_
= STATE_CTRL_WRITE_PASS
;
776 case ERROR_CLASS_TRANSIENT_ERROR
:
777 case ERROR_CLASS_PERMANENT_ERROR
:
778 response_
.needs_auth
= true;
779 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
782 return Stop(ERR_UNEXPECTED
);
788 int FtpNetworkTransaction::DoCtrlWritePASS() {
789 std::string command
= "PASS " + UTF16ToUTF8(credentials_
.password());
791 if (!IsValidFTPCommandString(command
))
792 return Stop(ERR_MALFORMED_IDENTITY
);
794 next_state_
= STATE_CTRL_READ
;
795 return SendFtpCommand(command
, "PASS ***", COMMAND_PASS
);
798 int FtpNetworkTransaction::ProcessResponsePASS(
799 const FtpCtrlResponse
& response
) {
800 switch (GetErrorClass(response
.status_code
)) {
802 next_state_
= STATE_CTRL_WRITE_SYST
;
804 case ERROR_CLASS_INFO_NEEDED
:
805 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
806 case ERROR_CLASS_TRANSIENT_ERROR
:
807 case ERROR_CLASS_PERMANENT_ERROR
:
808 response_
.needs_auth
= true;
809 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
812 return Stop(ERR_UNEXPECTED
);
818 int FtpNetworkTransaction::DoCtrlWriteSYST() {
819 std::string command
= "SYST";
820 next_state_
= STATE_CTRL_READ
;
821 return SendFtpCommand(command
, command
, COMMAND_SYST
);
824 int FtpNetworkTransaction::ProcessResponseSYST(
825 const FtpCtrlResponse
& response
) {
826 switch (GetErrorClass(response
.status_code
)) {
827 case ERROR_CLASS_INITIATED
:
828 return Stop(ERR_INVALID_RESPONSE
);
829 case ERROR_CLASS_OK
: {
830 // All important info should be on the first line.
831 std::string line
= response
.lines
[0];
832 // The response should be ASCII, which allows us to do case-insensitive
833 // comparisons easily. If it is not ASCII, we leave the system type
835 if (IsStringASCII(line
)) {
836 line
= StringToLowerASCII(line
);
838 // Remove all whitespace, to correctly handle cases like fancy "V M S"
839 // response instead of "VMS".
840 RemoveChars(line
, kWhitespaceASCII
, &line
);
842 // The "magic" strings we test for below have been gathered by an
843 // empirical study. VMS needs to come first because some VMS systems
844 // also respond with "UNIX emulation", which is not perfect. It is much
845 // more reliable to talk to these servers in their native language.
846 if (line
.find("vms") != std::string::npos
) {
847 system_type_
= SYSTEM_TYPE_VMS
;
848 } else if (line
.find("l8") != std::string::npos
||
849 line
.find("unix") != std::string::npos
||
850 line
.find("bsd") != std::string::npos
) {
851 system_type_
= SYSTEM_TYPE_UNIX
;
852 } else if (line
.find("win32") != std::string::npos
||
853 line
.find("windows") != std::string::npos
) {
854 system_type_
= SYSTEM_TYPE_WINDOWS
;
855 } else if (line
.find("os/2") != std::string::npos
) {
856 system_type_
= SYSTEM_TYPE_OS2
;
859 next_state_
= STATE_CTRL_WRITE_PWD
;
862 case ERROR_CLASS_INFO_NEEDED
:
863 return Stop(ERR_INVALID_RESPONSE
);
864 case ERROR_CLASS_TRANSIENT_ERROR
:
865 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
866 case ERROR_CLASS_PERMANENT_ERROR
:
867 // Server does not recognize the SYST command so proceed.
868 next_state_
= STATE_CTRL_WRITE_PWD
;
872 return Stop(ERR_UNEXPECTED
);
878 int FtpNetworkTransaction::DoCtrlWritePWD() {
879 std::string command
= "PWD";
880 next_state_
= STATE_CTRL_READ
;
881 return SendFtpCommand(command
, command
, COMMAND_PWD
);
884 int FtpNetworkTransaction::ProcessResponsePWD(const FtpCtrlResponse
& response
) {
885 switch (GetErrorClass(response
.status_code
)) {
886 case ERROR_CLASS_INITIATED
:
887 return Stop(ERR_INVALID_RESPONSE
);
888 case ERROR_CLASS_OK
: {
889 // The info we look for should be on the first line.
890 std::string line
= response
.lines
[0];
892 return Stop(ERR_INVALID_RESPONSE
);
893 std::string::size_type quote_pos
= line
.find('"');
894 if (quote_pos
!= std::string::npos
) {
895 line
= line
.substr(quote_pos
+ 1);
896 quote_pos
= line
.find('"');
897 if (quote_pos
== std::string::npos
)
898 return Stop(ERR_INVALID_RESPONSE
);
899 line
= line
.substr(0, quote_pos
);
901 if (system_type_
== SYSTEM_TYPE_VMS
)
902 line
= FtpUtil::VMSPathToUnix(line
);
903 if (line
.length() && line
[line
.length() - 1] == '/')
904 line
.erase(line
.length() - 1);
905 current_remote_directory_
= line
;
906 next_state_
= STATE_CTRL_WRITE_TYPE
;
909 case ERROR_CLASS_INFO_NEEDED
:
910 return Stop(ERR_INVALID_RESPONSE
);
911 case ERROR_CLASS_TRANSIENT_ERROR
:
912 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
913 case ERROR_CLASS_PERMANENT_ERROR
:
914 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
917 return Stop(ERR_UNEXPECTED
);
923 int FtpNetworkTransaction::DoCtrlWriteTYPE() {
924 std::string command
= "TYPE ";
925 if (data_type_
== DATA_TYPE_ASCII
) {
927 } else if (data_type_
== DATA_TYPE_IMAGE
) {
931 return Stop(ERR_UNEXPECTED
);
933 next_state_
= STATE_CTRL_READ
;
934 return SendFtpCommand(command
, command
, COMMAND_TYPE
);
937 int FtpNetworkTransaction::ProcessResponseTYPE(
938 const FtpCtrlResponse
& response
) {
939 switch (GetErrorClass(response
.status_code
)) {
940 case ERROR_CLASS_INITIATED
:
941 return Stop(ERR_INVALID_RESPONSE
);
943 next_state_
= use_epsv_
? STATE_CTRL_WRITE_EPSV
: STATE_CTRL_WRITE_PASV
;
945 case ERROR_CLASS_INFO_NEEDED
:
946 return Stop(ERR_INVALID_RESPONSE
);
947 case ERROR_CLASS_TRANSIENT_ERROR
:
948 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
949 case ERROR_CLASS_PERMANENT_ERROR
:
950 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
953 return Stop(ERR_UNEXPECTED
);
959 int FtpNetworkTransaction::DoCtrlWriteEPSV() {
960 const std::string command
= "EPSV";
961 next_state_
= STATE_CTRL_READ
;
962 return SendFtpCommand(command
, command
, COMMAND_EPSV
);
965 int FtpNetworkTransaction::ProcessResponseEPSV(
966 const FtpCtrlResponse
& response
) {
967 switch (GetErrorClass(response
.status_code
)) {
968 case ERROR_CLASS_INITIATED
:
969 return Stop(ERR_INVALID_RESPONSE
);
971 if (!ExtractPortFromEPSVResponse( response
, &data_connection_port_
))
972 return Stop(ERR_INVALID_RESPONSE
);
973 if (data_connection_port_
< 1024 ||
974 !IsPortAllowedByFtp(data_connection_port_
))
975 return Stop(ERR_UNSAFE_PORT
);
976 next_state_
= STATE_DATA_CONNECT
;
978 case ERROR_CLASS_INFO_NEEDED
:
979 return Stop(ERR_INVALID_RESPONSE
);
980 case ERROR_CLASS_TRANSIENT_ERROR
:
981 case ERROR_CLASS_PERMANENT_ERROR
:
983 next_state_
= STATE_CTRL_WRITE_PASV
;
987 return Stop(ERR_UNEXPECTED
);
993 int FtpNetworkTransaction::DoCtrlWritePASV() {
994 std::string command
= "PASV";
995 next_state_
= STATE_CTRL_READ
;
996 return SendFtpCommand(command
, command
, COMMAND_PASV
);
999 int FtpNetworkTransaction::ProcessResponsePASV(
1000 const FtpCtrlResponse
& response
) {
1001 switch (GetErrorClass(response
.status_code
)) {
1002 case ERROR_CLASS_INITIATED
:
1003 return Stop(ERR_INVALID_RESPONSE
);
1004 case ERROR_CLASS_OK
:
1005 if (!ExtractPortFromPASVResponse(response
, &data_connection_port_
))
1006 return Stop(ERR_INVALID_RESPONSE
);
1007 if (data_connection_port_
< 1024 ||
1008 !IsPortAllowedByFtp(data_connection_port_
))
1009 return Stop(ERR_UNSAFE_PORT
);
1010 next_state_
= STATE_DATA_CONNECT
;
1012 case ERROR_CLASS_INFO_NEEDED
:
1013 return Stop(ERR_INVALID_RESPONSE
);
1014 case ERROR_CLASS_TRANSIENT_ERROR
:
1015 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1016 case ERROR_CLASS_PERMANENT_ERROR
:
1017 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1020 return Stop(ERR_UNEXPECTED
);
1026 int FtpNetworkTransaction::DoCtrlWriteRETR() {
1027 std::string command
= "RETR " + GetRequestPathForFtpCommand(false);
1028 next_state_
= STATE_CTRL_READ
;
1029 return SendFtpCommand(command
, command
, COMMAND_RETR
);
1032 int FtpNetworkTransaction::ProcessResponseRETR(
1033 const FtpCtrlResponse
& response
) {
1034 switch (GetErrorClass(response
.status_code
)) {
1035 case ERROR_CLASS_INITIATED
:
1036 // We want the client to start reading the response at this point.
1037 // It got here either through Start or RestartWithAuth. We want that
1038 // method to complete. Not setting next state here will make DoLoop exit
1039 // and in turn make Start/RestartWithAuth complete.
1040 resource_type_
= RESOURCE_TYPE_FILE
;
1042 case ERROR_CLASS_OK
:
1043 resource_type_
= RESOURCE_TYPE_FILE
;
1044 next_state_
= STATE_CTRL_WRITE_QUIT
;
1046 case ERROR_CLASS_INFO_NEEDED
:
1047 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1048 case ERROR_CLASS_TRANSIENT_ERROR
:
1049 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1050 case ERROR_CLASS_PERMANENT_ERROR
:
1051 // Code 550 means "Failed to open file". Other codes are unrelated,
1052 // like "Not logged in" etc.
1053 if (response
.status_code
!= 550 || resource_type_
== RESOURCE_TYPE_FILE
)
1054 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1056 // It's possible that RETR failed because the path is a directory.
1057 resource_type_
= RESOURCE_TYPE_DIRECTORY
;
1059 // We're going to try CWD next, but first send a PASV one more time,
1060 // because some FTP servers, including FileZilla, require that.
1061 // See http://crbug.com/25316.
1062 next_state_
= use_epsv_
? STATE_CTRL_WRITE_EPSV
: STATE_CTRL_WRITE_PASV
;
1066 return Stop(ERR_UNEXPECTED
);
1069 // We should be sure about our resource type now. Otherwise we risk
1070 // an infinite loop (RETR can later send CWD, and CWD can later send RETR).
1071 DCHECK_NE(RESOURCE_TYPE_UNKNOWN
, resource_type_
);
1077 int FtpNetworkTransaction::DoCtrlWriteSIZE() {
1078 std::string command
= "SIZE " + GetRequestPathForFtpCommand(false);
1079 next_state_
= STATE_CTRL_READ
;
1080 return SendFtpCommand(command
, command
, COMMAND_SIZE
);
1083 int FtpNetworkTransaction::ProcessResponseSIZE(
1084 const FtpCtrlResponse
& response
) {
1085 State state_after_size
;
1086 if (resource_type_
== RESOURCE_TYPE_FILE
)
1087 state_after_size
= STATE_CTRL_WRITE_RETR
;
1089 state_after_size
= STATE_CTRL_WRITE_CWD
;
1091 switch (GetErrorClass(response
.status_code
)) {
1092 case ERROR_CLASS_INITIATED
:
1093 next_state_
= state_after_size
;
1095 case ERROR_CLASS_OK
:
1096 if (response
.lines
.size() != 1)
1097 return Stop(ERR_INVALID_RESPONSE
);
1099 if (!base::StringToInt64(response
.lines
[0], &size
))
1100 return Stop(ERR_INVALID_RESPONSE
);
1102 return Stop(ERR_INVALID_RESPONSE
);
1104 // A successful response to SIZE does not mean the resource is a file.
1105 // Some FTP servers (for example, the qnx one) send a SIZE even for
1107 response_
.expected_content_size
= size
;
1109 next_state_
= state_after_size
;
1111 case ERROR_CLASS_INFO_NEEDED
:
1112 next_state_
= state_after_size
;
1114 case ERROR_CLASS_TRANSIENT_ERROR
:
1115 ResetDataConnectionAfterError(state_after_size
);
1117 case ERROR_CLASS_PERMANENT_ERROR
:
1118 // It's possible that SIZE failed because the path is a directory.
1119 if (resource_type_
== RESOURCE_TYPE_UNKNOWN
&&
1120 response
.status_code
!= 550) {
1121 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1124 ResetDataConnectionAfterError(state_after_size
);
1128 return Stop(ERR_UNEXPECTED
);
1135 int FtpNetworkTransaction::DoCtrlWriteCWD() {
1136 std::string command
= "CWD " + GetRequestPathForFtpCommand(true);
1137 next_state_
= STATE_CTRL_READ
;
1138 return SendFtpCommand(command
, command
, COMMAND_CWD
);
1141 int FtpNetworkTransaction::ProcessResponseCWD(const FtpCtrlResponse
& response
) {
1142 // We should never issue CWD if we know the target resource is a file.
1143 DCHECK_NE(RESOURCE_TYPE_FILE
, resource_type_
);
1145 switch (GetErrorClass(response
.status_code
)) {
1146 case ERROR_CLASS_INITIATED
:
1147 return Stop(ERR_INVALID_RESPONSE
);
1148 case ERROR_CLASS_OK
:
1149 next_state_
= STATE_CTRL_WRITE_LIST
;
1151 case ERROR_CLASS_INFO_NEEDED
:
1152 return Stop(ERR_INVALID_RESPONSE
);
1153 case ERROR_CLASS_TRANSIENT_ERROR
:
1154 // Some FTP servers send response 451 (not a valid CWD response according
1155 // to RFC 959) instead of 550.
1156 if (response
.status_code
== 451)
1157 return ProcessResponseCWDNotADirectory();
1159 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1160 case ERROR_CLASS_PERMANENT_ERROR
:
1161 if (response
.status_code
== 550)
1162 return ProcessResponseCWDNotADirectory();
1164 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1167 return Stop(ERR_UNEXPECTED
);
1173 int FtpNetworkTransaction::ProcessResponseCWDNotADirectory() {
1174 if (resource_type_
== RESOURCE_TYPE_DIRECTORY
) {
1175 // We're assuming that the resource is a directory, but the server
1176 // says it's not true. The most probable interpretation is that it
1177 // doesn't exist (with FTP we can't be sure).
1178 return Stop(ERR_FILE_NOT_FOUND
);
1181 // We are here because SIZE failed and we are not sure what the resource
1182 // type is. It could still be file, and SIZE could fail because of
1183 // an access error (http://crbug.com/56734). Try RETR just to be sure.
1184 resource_type_
= RESOURCE_TYPE_FILE
;
1186 ResetDataConnectionAfterError(STATE_CTRL_WRITE_RETR
);
1191 int FtpNetworkTransaction::DoCtrlWriteLIST() {
1192 // Use the -l option for mod_ftp configured in LISTIsNLST mode: the option
1193 // forces LIST output instead of NLST (which would be ambiguous for us
1195 std::string
command("LIST -l");
1196 if (system_type_
== SYSTEM_TYPE_VMS
)
1197 command
= "LIST *.*;0";
1199 next_state_
= STATE_CTRL_READ
;
1200 return SendFtpCommand(command
, command
, COMMAND_LIST
);
1203 int FtpNetworkTransaction::ProcessResponseLIST(
1204 const FtpCtrlResponse
& response
) {
1205 switch (GetErrorClass(response
.status_code
)) {
1206 case ERROR_CLASS_INITIATED
:
1207 // We want the client to start reading the response at this point.
1208 // It got here either through Start or RestartWithAuth. We want that
1209 // method to complete. Not setting next state here will make DoLoop exit
1210 // and in turn make Start/RestartWithAuth complete.
1211 response_
.is_directory_listing
= true;
1213 case ERROR_CLASS_OK
:
1214 response_
.is_directory_listing
= true;
1215 next_state_
= STATE_CTRL_WRITE_QUIT
;
1217 case ERROR_CLASS_INFO_NEEDED
:
1218 return Stop(ERR_INVALID_RESPONSE
);
1219 case ERROR_CLASS_TRANSIENT_ERROR
:
1220 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1221 case ERROR_CLASS_PERMANENT_ERROR
:
1222 return Stop(GetNetErrorCodeForFtpResponseCode(response
.status_code
));
1225 return Stop(ERR_UNEXPECTED
);
1231 int FtpNetworkTransaction::DoCtrlWriteQUIT() {
1232 std::string command
= "QUIT";
1233 next_state_
= STATE_CTRL_READ
;
1234 return SendFtpCommand(command
, command
, COMMAND_QUIT
);
1237 int FtpNetworkTransaction::ProcessResponseQUIT(
1238 const FtpCtrlResponse
& response
) {
1239 ctrl_socket_
->Disconnect();
1245 int FtpNetworkTransaction::DoDataConnect() {
1246 next_state_
= STATE_DATA_CONNECT_COMPLETE
;
1247 IPEndPoint ip_endpoint
;
1248 AddressList data_address
;
1249 // Connect to the same host as the control socket to prevent PASV port
1250 // scanning attacks.
1251 int rv
= ctrl_socket_
->GetPeerAddress(&ip_endpoint
);
1254 data_address
= AddressList::CreateFromIPAddress(
1255 ip_endpoint
.address(), data_connection_port_
);
1256 data_socket_
.reset(socket_factory_
->CreateTransportClientSocket(
1257 data_address
, net_log_
.net_log(), net_log_
.source()));
1259 NetLog::TYPE_FTP_DATA_CONNECTION
,
1260 data_socket_
->NetLog().source().ToEventParametersCallback());
1261 return data_socket_
->Connect(io_callback_
);
1264 int FtpNetworkTransaction::DoDataConnectComplete(int result
) {
1265 if (result
!= OK
&& use_epsv_
) {
1266 // It's possible we hit a broken server, sadly. They can break in different
1267 // ways. Some time out, some reset a connection. Fall back to PASV.
1268 // TODO(phajdan.jr): remember it for future transactions with this server.
1269 // TODO(phajdan.jr): write a test for this code path.
1271 next_state_
= STATE_CTRL_WRITE_PASV
;
1275 // Only record the connection error after we've applied all our fallbacks.
1276 // We want to capture the final error, one we're not going to recover from.
1277 RecordDataConnectionError(result
);
1280 return Stop(result
);
1282 next_state_
= state_after_data_connect_complete_
;
1286 int FtpNetworkTransaction::DoDataRead() {
1287 DCHECK(read_data_buf_
);
1288 DCHECK_GT(read_data_buf_len_
, 0);
1290 if (data_socket_
== NULL
|| !data_socket_
->IsConnected()) {
1291 // If we don't destroy the data socket completely, some servers will wait
1292 // for us (http://crbug.com/21127). The half-closed TCP connection needs
1293 // to be closed on our side too.
1294 data_socket_
.reset();
1296 if (ctrl_socket_
->IsConnected()) {
1297 // Wait for the server's response, we should get it before sending QUIT.
1298 next_state_
= STATE_CTRL_READ
;
1302 // We are no longer connected to the server, so just finish the transaction.
1306 next_state_
= STATE_DATA_READ_COMPLETE
;
1307 read_data_buf_
->data()[0] = 0;
1308 return data_socket_
->Read(read_data_buf_
, read_data_buf_len_
, io_callback_
);
1311 int FtpNetworkTransaction::DoDataReadComplete(int result
) {
1315 // We're using a histogram as a group of counters, with one bucket for each
1316 // enumeration value. We're only interested in the values of the counters.
1317 // Ignore the shape, average, and standard deviation of the histograms because
1318 // they are meaningless.
1320 // We use two histograms. In the first histogram we tally whether the user has
1321 // seen an error of that type during the session. In the second histogram we
1322 // tally the total number of times the users sees each errer.
1323 void FtpNetworkTransaction::RecordDataConnectionError(int result
) {
1324 // Gather data for http://crbug.com/3073. See how many users have trouble
1325 // establishing FTP data connection in passive FTP mode.
1327 // Data connection successful.
1330 // Local firewall blocked the connection.
1331 NET_ERROR_ACCESS_DENIED
= 1,
1333 // Connection timed out.
1334 NET_ERROR_TIMED_OUT
= 2,
1336 // Connection has been estabilished, but then got broken (either reset
1338 NET_ERROR_CONNECTION_BROKEN
= 3,
1340 // Connection has been refused.
1341 NET_ERROR_CONNECTION_REFUSED
= 4,
1343 // No connection to the internet.
1344 NET_ERROR_INTERNET_DISCONNECTED
= 5,
1346 // Could not reach the destination address.
1347 NET_ERROR_ADDRESS_UNREACHABLE
= 6,
1349 // A programming error in our network stack.
1350 NET_ERROR_UNEXPECTED
= 7,
1352 // Other kind of error.
1353 NET_ERROR_OTHER
= 20,
1355 NUM_OF_NET_ERROR_TYPES
1359 type
= NET_ERROR_OK
;
1361 case ERR_ACCESS_DENIED
:
1362 case ERR_NETWORK_ACCESS_DENIED
:
1363 type
= NET_ERROR_ACCESS_DENIED
;
1366 type
= NET_ERROR_TIMED_OUT
;
1368 case ERR_CONNECTION_ABORTED
:
1369 case ERR_CONNECTION_RESET
:
1370 case ERR_CONNECTION_CLOSED
:
1371 type
= NET_ERROR_CONNECTION_BROKEN
;
1373 case ERR_CONNECTION_FAILED
:
1374 case ERR_CONNECTION_REFUSED
:
1375 type
= NET_ERROR_CONNECTION_REFUSED
;
1377 case ERR_INTERNET_DISCONNECTED
:
1378 type
= NET_ERROR_INTERNET_DISCONNECTED
;
1380 case ERR_ADDRESS_INVALID
:
1381 case ERR_ADDRESS_UNREACHABLE
:
1382 type
= NET_ERROR_ADDRESS_UNREACHABLE
;
1384 case ERR_UNEXPECTED
:
1385 type
= NET_ERROR_UNEXPECTED
;
1388 type
= NET_ERROR_OTHER
;
1391 static bool had_error_type
[NUM_OF_NET_ERROR_TYPES
];
1393 DCHECK(type
>= 0 && type
< NUM_OF_NET_ERROR_TYPES
);
1394 if (!had_error_type
[type
]) {
1395 had_error_type
[type
] = true;
1396 UMA_HISTOGRAM_ENUMERATION("Net.FtpDataConnectionErrorHappened",
1397 type
, NUM_OF_NET_ERROR_TYPES
);
1399 UMA_HISTOGRAM_ENUMERATION("Net.FtpDataConnectionErrorCount",
1400 type
, NUM_OF_NET_ERROR_TYPES
);