]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/clients/FtpClient.cc
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 09 File Transfer Protocol (FTP) */
12 #include "acl/FilledChecklist.h"
13 #include "client_side.h"
14 #include "clients/FtpClient.h"
15 #include "comm/ConnOpener.h"
16 #include "comm/Read.h"
17 #include "comm/TcpAcceptor.h"
18 #include "comm/Write.h"
19 #include "error/SysErrorDetail.h"
20 #include "errorpage.h"
22 #include "ftp/Parsing.h"
23 #include "http/Stream.h"
25 #include "sbuf/SBuf.h"
26 #include "sbuf/Stream.h"
27 #include "SquidConfig.h"
28 #include "SquidString.h"
29 #include "StatCounters.h"
38 const char *const crlf
= "\r\n";
41 escapeIAC(const char *buf
)
45 unsigned const char *p
;
48 for (p
= (unsigned const char *)buf
, n
= 1; *p
; ++n
, ++p
)
52 ret
= (char *)xmalloc(n
);
54 for (p
= (unsigned const char *)buf
, r
=(unsigned char *)ret
; *p
; ++p
) {
66 assert((r
- (unsigned char *)ret
) == n
);
70 /* Ftp::ErrorDetail */
73 Ftp::ErrorDetail::brief() const
75 return ToSBuf("FTP_REPLY_CODE=", completionCode
);
79 Ftp::ErrorDetail::verbose(const HttpRequest::Pointer
&) const
81 return ToSBuf("FTP reply with completion code ", completionCode
);
86 /// configures the channel with a descriptor and registers a close handler
88 Ftp::Channel::opened(const Comm::ConnectionPointer
&newConn
,
89 const AsyncCall::Pointer
&aCloser
)
91 assert(!Comm::IsConnOpen(conn
));
92 assert(closer
== NULL
);
94 assert(Comm::IsConnOpen(newConn
));
95 assert(aCloser
!= NULL
);
99 comm_add_close_handler(conn
->fd
, closer
);
102 /// planned close: removes the close handler and calls comm_close
104 Ftp::Channel::close()
106 // channels with active listeners will be closed when the listener handler dies.
107 if (Comm::IsConnOpen(conn
)) {
108 comm_remove_close_handler(conn
->fd
, closer
);
109 conn
->close(); // we do not expect to be called back
115 Ftp::Channel::forget()
117 if (Comm::IsConnOpen(conn
)) {
118 commUnsetConnTimeout(conn
);
119 comm_remove_close_handler(conn
->fd
, closer
);
125 Ftp::Channel::clear()
131 /* Ftp::CtrlChannel */
133 Ftp::CtrlChannel::CtrlChannel():
142 buf
= static_cast<char*>(memAllocBuf(4096, &size
));
145 Ftp::CtrlChannel::~CtrlChannel()
147 memFreeBuf(size
, buf
);
149 wordlistDestroy(&message
);
150 safe_free(last_command
);
151 safe_free(last_reply
);
154 /* Ftp::DataChannel */
156 Ftp::DataChannel::DataChannel():
164 Ftp::DataChannel::~DataChannel()
170 Ftp::DataChannel::addr(const Ip::Address
&import
)
172 static char addrBuf
[MAX_IPSTRLEN
];
173 import
.toStr(addrBuf
, sizeof(addrBuf
));
175 host
= xstrdup(addrBuf
);
176 port
= import
.port();
181 Ftp::Client::Client(FwdState
*fwdState
):
182 AsyncJob("Ftp::Client"),
189 shortenReadTimeout(false)
191 ++statCounter
.server
.all
.requests
;
192 ++statCounter
.server
.ftp
.requests
;
194 ctrl
.last_command
= xstrdup("Connect to server");
196 typedef CommCbMemFunT
<Client
, CommCloseCbParams
> Dialer
;
197 const AsyncCall::Pointer closer
= JobCallback(9, 5, Dialer
, this,
198 Ftp::Client::ctrlClosed
);
199 ctrl
.opened(fwdState
->serverConnection(), closer
);
202 Ftp::Client::~Client()
204 if (data
.opener
!= NULL
) {
205 data
.opener
->cancel("Ftp::Client destructed");
210 safe_free(old_request
);
211 safe_free(old_reply
);
212 fwd
= NULL
; // refcounted
218 scheduleReadControlReply(0);
222 Ftp::Client::initReadBuf()
224 if (data
.readBuf
== NULL
) {
225 data
.readBuf
= new MemBuf
;
226 data
.readBuf
->init(4096, SQUID_TCP_SO_RCVBUF
);
231 * Close the FTP server connection(s). Used by serverComplete().
234 Ftp::Client::closeServer()
236 if (Comm::IsConnOpen(ctrl
.conn
)) {
237 debugs(9, 3, "closing FTP server FD " << ctrl
.conn
->fd
<< ", this " << this);
238 fwd
->unregister(ctrl
.conn
);
242 if (Comm::IsConnOpen(data
.conn
)) {
243 debugs(9, 3, "closing FTP data FD " << data
.conn
->fd
<< ", this " << this);
247 debugs(9, 3, "FTP ctrl and data connections closed. this " << this);
251 * Did we close all FTP server connection(s)?
253 \retval true Both server control and data channels are closed. And not waiting for a new data connection to open.
254 \retval false Either control channel or data is still active.
257 Ftp::Client::doneWithServer() const
259 return !Comm::IsConnOpen(ctrl
.conn
) && !Comm::IsConnOpen(data
.conn
);
263 Ftp::Client::failed(err_type error
, int xerrno
, ErrorState
*err
)
265 debugs(9, 3, "entry-null=" << (entry
?entry
->isEmpty():0) << ", entry=" << entry
);
267 const char *command
, *reply
;
271 debugs(9, 6, "error=" << err
->type
<< ", code=" << xerrno
<<
272 ", status=" << err
->httpStatus
);
276 Http::StatusCode httpStatus
= failedHttpStatus(error
);
277 ftperr
= new ErrorState(error
, httpStatus
, fwd
->request
, fwd
->al
);
280 ftperr
->xerrno
= xerrno
;
282 ftperr
->ftp
.server_msg
= ctrl
.message
;
286 command
= old_request
;
288 command
= ctrl
.last_command
;
290 if (command
&& strncmp(command
, "PASS", 4) == 0)
291 command
= "PASS <yourpassword>";
296 reply
= ctrl
.last_reply
;
299 ftperr
->ftp
.request
= xstrdup(command
);
302 ftperr
->ftp
.reply
= xstrdup(reply
);
305 fwd
->request
->detailError(error
, SysErrorDetail::NewIfAny(xerrno
));
307 closeServer(); // we failed, so no serverComplete()
312 Ftp::Client::failedHttpStatus(err_type
&error
)
314 if (error
== ERR_NONE
)
315 error
= ERR_FTP_FAILURE
;
316 return error
== ERR_READ_TIMEOUT
? Http::scGatewayTimeout
:
322 * Looks like there are no longer anymore callers that set
323 * buffered_ok=1. Perhaps it can be removed at some point.
326 Ftp::Client::scheduleReadControlReply(int buffered_ok
)
328 debugs(9, 3, ctrl
.conn
);
330 if (buffered_ok
&& ctrl
.offset
> 0) {
331 /* We've already read some reply data */
332 handleControlReply();
335 if (!Comm::IsConnOpen(ctrl
.conn
)) {
336 debugs(9, 3, "cannot read without ctrl " << ctrl
.conn
);
340 * Cancel the timeout on the Data socket (if any) and
341 * establish one on the control socket.
343 if (Comm::IsConnOpen(data
.conn
)) {
344 commUnsetConnTimeout(data
.conn
);
347 const time_t tout
= shortenReadTimeout
?
348 min(Config
.Timeout
.connect
, Config
.Timeout
.read
):
350 shortenReadTimeout
= false; // we only need to do this once, after PASV
352 typedef CommCbMemFunT
<Client
, CommTimeoutCbParams
> TimeoutDialer
;
353 AsyncCall::Pointer timeoutCall
= JobCallback(9, 5, TimeoutDialer
, this, Ftp::Client::timeout
);
354 commSetConnTimeout(ctrl
.conn
, tout
, timeoutCall
);
356 typedef CommCbMemFunT
<Client
, CommIoCbParams
> Dialer
;
357 AsyncCall::Pointer reader
= JobCallback(9, 5, Dialer
, this, Ftp::Client::readControlReply
);
358 comm_read(ctrl
.conn
, ctrl
.buf
+ ctrl
.offset
, ctrl
.size
- ctrl
.offset
, reader
);
363 Ftp::Client::readControlReply(const CommIoCbParams
&io
)
365 debugs(9, 3, "FD " << io
.fd
<< ", Read " << io
.size
<< " bytes");
368 statCounter
.server
.all
.kbytes_in
+= io
.size
;
369 statCounter
.server
.ftp
.kbytes_in
+= io
.size
;
372 if (io
.flag
== Comm::ERR_CLOSING
)
375 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
376 if (abortOnData("entry aborted during control reply read"))
380 assert(ctrl
.offset
< ctrl
.size
);
382 if (io
.flag
== Comm::OK
&& io
.size
> 0) {
383 fd_bytes(io
.fd
, io
.size
, FD_READ
);
386 if (io
.flag
!= Comm::OK
) {
387 debugs(50, ignoreErrno(io
.xerrno
) ? 3 : DBG_IMPORTANT
,
388 "FTP control reply read error: " << xstrerr(io
.xerrno
));
390 if (ignoreErrno(io
.xerrno
)) {
391 scheduleReadControlReply(0);
393 failed(ERR_READ_ERROR
, io
.xerrno
);
394 /* failed closes ctrl.conn and frees ftpState */
400 if (entry
->store_status
== STORE_PENDING
) {
401 failed(ERR_FTP_FAILURE
, 0);
402 /* failed closes ctrl.conn and frees ftpState */
406 /* XXX this may end up having to be serverComplete() .. */
407 abortAll("zero control reply read");
411 unsigned int len
=io
.size
+ ctrl
.offset
;
413 assert(len
<= ctrl
.size
);
414 if (Comm::IsConnOpen(ctrl
.conn
))
415 commUnsetConnTimeout(ctrl
.conn
); // we are done waiting for ctrl reply
416 handleControlReply();
420 Ftp::Client::handleControlReply()
422 debugs(9, 3, status());
424 size_t bytes_used
= 0;
425 wordlistDestroy(&ctrl
.message
);
427 if (!parseControlReply(bytes_used
)) {
428 /* didn't get complete reply yet */
430 if (ctrl
.offset
== ctrl
.size
) {
431 ctrl
.buf
= static_cast<char*>(memReallocBuf(ctrl
.buf
, ctrl
.size
<< 1, &ctrl
.size
));
434 scheduleReadControlReply(0);
438 assert(ctrl
.message
); // the entire FTP server response, line by line
439 assert(ctrl
.replycode
>= 0); // FTP status code (from the last line)
440 assert(ctrl
.last_reply
); // FTP reason (from the last line)
442 if (ctrl
.offset
== bytes_used
) {
446 /* Got some data past the complete reply */
447 assert(bytes_used
< ctrl
.offset
);
448 ctrl
.offset
-= bytes_used
;
449 memmove(ctrl
.buf
, ctrl
.buf
+ bytes_used
, ctrl
.offset
);
452 debugs(9, 3, "state=" << state
<< ", code=" << ctrl
.replycode
);
456 Ftp::Client::handlePasvReply(Ip::Address
&srvAddr
)
458 int code
= ctrl
.replycode
;
460 debugs(9, 3, status());
462 if (!Comm::IsConnOpen(ctrl
.conn
)) {
463 debugs(9, 5, "The control connection to the remote end is closed");
468 debugs(9, 2, "PASV not supported by remote end");
472 /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */
473 /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */
474 debugs(9, 5, "scanning: " << ctrl
.last_reply
);
476 buf
= ctrl
.last_reply
+ strcspn(ctrl
.last_reply
, "0123456789");
478 const char *forceIp
= Config
.Ftp
.sanitycheck
?
479 fd_table
[ctrl
.conn
->fd
].ipaddr
: NULL
;
480 if (!Ftp::ParseIpPort(buf
, forceIp
, srvAddr
)) {
481 debugs(9, DBG_IMPORTANT
, "Unsafe PASV reply from " <<
482 ctrl
.conn
->remote
<< ": " << ctrl
.last_reply
);
492 Ftp::Client::handleEpsvReply(Ip::Address
&remoteAddr
)
494 int code
= ctrl
.replycode
;
496 debugs(9, 3, status());
498 if (!Comm::IsConnOpen(ctrl
.conn
)) {
499 debugs(9, 5, "The control connection to the remote end is closed");
503 if (code
!= 229 && code
!= 522) {
505 /* handle broken servers (RFC 2428 says OK code for EPSV MUST be 229 not 200) */
506 /* vsftpd for one send '200 EPSV ALL ok.' without even port info.
507 * Its okay to re-send EPSV 1/2 but nothing else. */
508 debugs(9, DBG_IMPORTANT
, "Broken FTP Server at " << ctrl
.conn
->remote
<< ". Wrong accept code for EPSV");
510 debugs(9, 2, "EPSV not supported by remote end");
512 return sendPassive();
516 /* Peer responded with a list of supported methods:
517 * 522 Network protocol not supported, use (1)
518 * 522 Network protocol not supported, use (1,2)
519 * 522 Network protocol not supported, use (2)
520 * TODO: Handle the (1,2) case which may happen after EPSV ALL. Close
521 * data + control without self-destructing and re-open from scratch.
523 debugs(9, 5, "scanning: " << ctrl
.last_reply
);
524 buf
= ctrl
.last_reply
;
525 while (buf
!= NULL
&& *buf
!= '\0' && *buf
!= '\n' && *buf
!= '(')
527 if (buf
!= NULL
&& *buf
== '\n')
530 if (buf
== NULL
|| *buf
== '\0') {
531 /* handle broken server (RFC 2428 says MUST specify supported protocols in 522) */
532 debugs(9, DBG_IMPORTANT
, "Broken FTP Server at " << ctrl
.conn
->remote
<< ". 522 error missing protocol negotiation hints");
533 return sendPassive();
534 } else if (strcmp(buf
, "(1)") == 0) {
535 state
= SENT_EPSV_2
; /* simulate having sent and failed EPSV 2 */
536 return sendPassive();
537 } else if (strcmp(buf
, "(2)") == 0) {
538 if (Ip::EnableIpv6
) {
539 /* If server only supports EPSV 2 and we have already tried that. Go straight to EPRT */
540 if (state
== SENT_EPSV_2
) {
543 /* or try the next Passive mode down the chain. */
544 return sendPassive();
547 /* Server only accept EPSV in IPv6 traffic. */
548 state
= SENT_EPSV_1
; /* simulate having sent and failed EPSV 1 */
549 return sendPassive();
552 /* handle broken server (RFC 2428 says MUST specify supported protocols in 522) */
553 debugs(9, DBG_IMPORTANT
, "WARNING: Server at " << ctrl
.conn
->remote
<< " sent unknown protocol negotiation hint: " << buf
);
554 return sendPassive();
556 /* coverity[unreachable] */
557 /* safeguard against possible future bugs in above conditions */
558 failed(ERR_FTP_FAILURE
, 0);
562 /* 229 Entering Extended Passive Mode (|||port|) */
563 /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */
564 debugs(9, 5, "scanning: " << ctrl
.last_reply
);
566 buf
= ctrl
.last_reply
+ strcspn(ctrl
.last_reply
, "(");
570 int n
= sscanf(buf
, "(%c%c%c%hu%c)", &h1
, &h2
, &h3
, &port
, &h4
);
572 if (n
< 4 || h1
!= h2
|| h1
!= h3
|| h1
!= h4
) {
573 debugs(9, DBG_IMPORTANT
, "Invalid EPSV reply from " <<
574 ctrl
.conn
->remote
<< ": " <<
577 return sendPassive();
581 debugs(9, DBG_IMPORTANT
, "Unsafe EPSV reply from " <<
582 ctrl
.conn
->remote
<< ": " <<
585 return sendPassive();
588 if (Config
.Ftp
.sanitycheck
) {
590 debugs(9, DBG_IMPORTANT
, "Unsafe EPSV reply from " <<
591 ctrl
.conn
->remote
<< ": " <<
594 return sendPassive();
598 remoteAddr
= ctrl
.conn
->remote
;
599 remoteAddr
.port(port
);
600 data
.addr(remoteAddr
);
604 // FTP clients do not support EPRT and PORT commands yet.
605 // The Ftp::Client::sendEprt() will fail because of the unimplemented
606 // openListenSocket() or sendPort() methods
608 Ftp::Client::sendEprt()
610 if (!Config
.Ftp
.eprt
) {
611 /* Disabled. Switch immediately to attempting old PORT command. */
612 debugs(9, 3, "EPRT disabled by local administrator");
616 debugs(9, 3, status());
618 if (!openListenSocket()) {
619 failed(ERR_FTP_FAILURE
, 0);
623 debugs(9, 3, "Listening for FTP data connection with FD " << data
.conn
);
624 if (!Comm::IsConnOpen(data
.conn
)) {
625 // TODO: Set error message.
626 failed(ERR_FTP_FAILURE
, 0);
632 char buf
[MAX_IPSTRLEN
];
633 /* RFC 2428 defines EPRT as IPv6 equivalent to IPv4 PORT command. */
634 /* Which can be used by EITHER protocol. */
635 debugs(9, 3, "Listening for FTP data connection on port" << comm_local_port(data
.conn
->fd
) << " or port?" << data
.conn
->local
.port());
636 mb
.appendf("EPRT |%d|%s|%d|%s",
637 ( data
.conn
->local
.isIPv6() ? 2 : 1 ),
638 data
.conn
->local
.toStr(buf
,MAX_IPSTRLEN
),
639 comm_local_port(data
.conn
->fd
), Ftp::crlf
);
642 writeCommand(mb
.content());
647 Ftp::Client::sendPort()
649 failed(ERR_FTP_FAILURE
, 0);
654 Ftp::Client::sendPassive()
656 debugs(9, 3, status());
659 * Checks for EPSV ALL special conditions:
660 * If enabled to be sent, squid MUST NOT request any other connect methods.
661 * If 'ALL' is sent and fails the entire FTP Session fails.
662 * NP: By my reading exact EPSV protocols maybe attempted, but only EPSV method. */
663 if (Config
.Ftp
.epsv_all
&& state
== SENT_EPSV_1
) {
664 // We are here because the last "EPSV 1" failed, but because of epsv_all
665 // no other method allowed.
666 debugs(9, DBG_IMPORTANT
, "FTP does not allow PASV method after 'EPSV ALL' has been sent.");
667 failed(ERR_FTP_FAILURE
, 0);
671 /// Closes any old FTP-Data connection which may exist. */
675 * Checks for previous EPSV/PASV failures on this server/session.
676 * Diverts to EPRT immediately if they are not working. */
677 if (!Config
.Ftp
.passive
|| state
== SENT_PASV
) {
685 * Send EPSV (ALL,2,1) or PASV on the control channel.
687 * - EPSV ALL is used if enabled.
688 * - EPSV 2 is used if ALL is disabled and IPv6 is available and ctrl channel is IPv6.
689 * - EPSV 1 is used if EPSV 2 (IPv6) fails or is not available or ctrl channel is IPv4.
690 * - PASV is used if EPSV 1 fails.
693 case SENT_EPSV_ALL
: /* EPSV ALL resulted in a bad response. Try ther EPSV methods. */
694 if (ctrl
.conn
->local
.isIPv6()) {
695 debugs(9, 5, "FTP Channel is IPv6 (" << ctrl
.conn
->remote
<< ") attempting EPSV 2 after EPSV ALL has failed.");
696 mb
.appendf("EPSV 2%s", Ftp::crlf
);
700 // else fall through to skip EPSV 2
702 case SENT_EPSV_2
: /* EPSV IPv6 failed. Try EPSV IPv4 */
703 if (ctrl
.conn
->local
.isIPv4()) {
704 debugs(9, 5, "FTP Channel is IPv4 (" << ctrl
.conn
->remote
<< ") attempting EPSV 1 after EPSV ALL has failed.");
705 mb
.appendf("EPSV 1%s", Ftp::crlf
);
708 } else if (Config
.Ftp
.epsv_all
) {
709 debugs(9, DBG_IMPORTANT
, "FTP does not allow PASV method after 'EPSV ALL' has been sent.");
710 failed(ERR_FTP_FAILURE
, 0);
713 // else fall through to skip EPSV 1
715 case SENT_EPSV_1
: /* EPSV options exhausted. Try PASV now. */
716 debugs(9, 5, "FTP Channel (" << ctrl
.conn
->remote
<< ") rejects EPSV connection attempts. Trying PASV instead.");
717 mb
.appendf("PASV%s", Ftp::crlf
);
723 if (Config
.accessList
.ftp_epsv
) {
724 ACLFilledChecklist
checklist(Config
.accessList
.ftp_epsv
, fwd
->request
, NULL
);
725 doEpsv
= checklist
.fastCheck().allowed();
728 debugs(9, 5, "EPSV support manually disabled. Sending PASV for FTP Channel (" << ctrl
.conn
->remote
<<")");
729 mb
.appendf("PASV%s", Ftp::crlf
);
731 } else if (Config
.Ftp
.epsv_all
) {
732 debugs(9, 5, "EPSV ALL manually enabled. Attempting with FTP Channel (" << ctrl
.conn
->remote
<<")");
733 mb
.appendf("EPSV ALL%s", Ftp::crlf
);
734 state
= SENT_EPSV_ALL
;
736 if (ctrl
.conn
->local
.isIPv6()) {
737 debugs(9, 5, "FTP Channel (" << ctrl
.conn
->remote
<< "). Sending default EPSV 2");
738 mb
.appendf("EPSV 2%s", Ftp::crlf
);
741 if (ctrl
.conn
->local
.isIPv4()) {
742 debugs(9, 5, "Channel (" << ctrl
.conn
->remote
<<"). Sending default EPSV 1");
743 mb
.appendf("EPSV 1%s", Ftp::crlf
);
752 wordlistDestroy(&ctrl
.message
);
753 ctrl
.message
= NULL
; //No message to return to client.
754 ctrl
.offset
= 0; //reset readed response, to make room read the next response
756 writeCommand(mb
.content());
758 shortenReadTimeout
= true;
763 Ftp::Client::connectDataChannel()
765 if (!Comm::IsConnOpen(ctrl
.conn
)) {
766 debugs(9, 5, "The control connection to the remote end is closed");
770 safe_free(ctrl
.last_command
);
772 safe_free(ctrl
.last_reply
);
774 ctrl
.last_command
= xstrdup("Connect to server data port");
776 // Generate a new data channel descriptor to be opened.
777 Comm::ConnectionPointer conn
= new Comm::Connection
;
778 conn
->setAddrs(ctrl
.conn
->local
, data
.host
);
780 conn
->remote
.port(data
.port
);
781 conn
->tos
= ctrl
.conn
->tos
;
782 conn
->nfmark
= ctrl
.conn
->nfmark
;
783 // Using non-local addresses in TPROXY mode requires appropriate socket option.
784 conn
->flags
|= ctrl
.conn
->flags
& COMM_TRANSPARENT
;
786 debugs(9, 3, "connecting to " << conn
->remote
);
788 typedef CommCbMemFunT
<Client
, CommConnectCbParams
> Dialer
;
789 data
.opener
= JobCallback(9, 3, Dialer
, this, Ftp::Client::dataChannelConnected
);
790 Comm::ConnOpener
*cs
= new Comm::ConnOpener(conn
, data
.opener
, Config
.Timeout
.connect
);
791 cs
->setHost(data
.host
);
796 Ftp::Client::openListenSocket()
801 /// creates a data channel Comm close callback
803 Ftp::Client::dataCloser()
805 typedef CommCbMemFunT
<Client
, CommCloseCbParams
> Dialer
;
806 return JobCallback(9, 5, Dialer
, this, Ftp::Client::dataClosed
);
809 /// handler called by Comm when FTP data channel is closed unexpectedly
811 Ftp::Client::dataClosed(const CommCloseCbParams
&)
813 debugs(9, 4, status());
814 if (data
.listenConn
!= NULL
) {
815 data
.listenConn
->close();
816 data
.listenConn
= NULL
;
817 // NP clear() does the: data.fd = -1;
823 Ftp::Client::writeCommand(const char *buf
)
826 /* trace FTP protocol communications at level 2 */
827 debugs(9, 2, "ftp<< " << buf
);
829 if (Config
.Ftp
.telnet
)
830 ebuf
= escapeIAC(buf
);
834 safe_free(ctrl
.last_command
);
836 safe_free(ctrl
.last_reply
);
838 ctrl
.last_command
= ebuf
;
840 if (!Comm::IsConnOpen(ctrl
.conn
)) {
841 debugs(9, 2, "cannot send to closing ctrl " << ctrl
.conn
);
842 // TODO: assert(ctrl.closer != NULL);
846 typedef CommCbMemFunT
<Client
, CommIoCbParams
> Dialer
;
847 AsyncCall::Pointer call
= JobCallback(9, 5, Dialer
, this,
848 Ftp::Client::writeCommandCallback
);
849 Comm::Write(ctrl
.conn
, ctrl
.last_command
, strlen(ctrl
.last_command
), call
, NULL
);
851 scheduleReadControlReply(0);
855 Ftp::Client::writeCommandCallback(const CommIoCbParams
&io
)
858 debugs(9, 5, "wrote " << io
.size
<< " bytes");
861 fd_bytes(io
.fd
, io
.size
, FD_WRITE
);
862 statCounter
.server
.all
.kbytes_out
+= io
.size
;
863 statCounter
.server
.ftp
.kbytes_out
+= io
.size
;
866 if (io
.flag
== Comm::ERR_CLOSING
)
870 debugs(9, DBG_IMPORTANT
, "FTP command write error: " << io
.conn
<< ": " << xstrerr(io
.xerrno
));
871 failed(ERR_WRITE_ERROR
, io
.xerrno
);
872 /* failed closes ctrl.conn and frees ftpState */
877 /// handler called by Comm when FTP control channel is closed unexpectedly
879 Ftp::Client::ctrlClosed(const CommCloseCbParams
&)
881 debugs(9, 4, status());
883 doneWithFwd
= "ctrlClosed()"; // assume FwdState is monitoring too
884 mustStop("Ftp::Client::ctrlClosed");
888 Ftp::Client::timeout(const CommTimeoutCbParams
&io
)
890 debugs(9, 4, io
.conn
<< ": '" << entry
->url() << "'" );
892 if (abortOnBadEntry("entry went bad while waiting for a timeout"))
895 failed(ERR_READ_TIMEOUT
, 0);
896 /* failed() closes ctrl.conn and frees ftpState */
899 const Comm::ConnectionPointer
&
900 Ftp::Client::dataConnection() const
906 Ftp::Client::maybeReadVirginBody()
909 if (!Comm::IsConnOpen(data
.conn
) || fd_table
[data
.conn
->fd
].closing())
912 if (data
.read_pending
)
917 const int read_sz
= replyBodySpace(*data
.readBuf
, 0);
919 debugs(9, 9, "FTP may read up to " << read_sz
<< " bytes");
921 if (read_sz
< 2) // see http.cc
924 data
.read_pending
= true;
926 typedef CommCbMemFunT
<Client
, CommTimeoutCbParams
> TimeoutDialer
;
927 AsyncCall::Pointer timeoutCall
= JobCallback(9, 5,
928 TimeoutDialer
, this, Ftp::Client::timeout
);
929 commSetConnTimeout(data
.conn
, Config
.Timeout
.read
, timeoutCall
);
931 debugs(9,5,"queueing read on FD " << data
.conn
->fd
);
933 typedef CommCbMemFunT
<Client
, CommIoCbParams
> Dialer
;
934 entry
->delayAwareRead(data
.conn
, data
.readBuf
->space(), read_sz
,
935 JobCallback(9, 5, Dialer
, this, Ftp::Client::dataRead
));
939 Ftp::Client::dataRead(const CommIoCbParams
&io
)
944 data
.read_pending
= false;
946 debugs(9, 3, "FD " << io
.fd
<< " Read " << io
.size
<< " bytes");
949 statCounter
.server
.all
.kbytes_in
+= io
.size
;
950 statCounter
.server
.ftp
.kbytes_in
+= io
.size
;
953 if (io
.flag
== Comm::ERR_CLOSING
)
956 assert(io
.fd
== data
.conn
->fd
);
958 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
959 abortOnData("entry aborted during dataRead");
963 if (io
.flag
== Comm::OK
&& io
.size
> 0) {
964 debugs(9, 5, "appended " << io
.size
<< " bytes to readBuf");
965 data
.readBuf
->appended(io
.size
);
967 DelayId delayId
= entry
->mem_obj
->mostBytesAllowed();
968 delayId
.bytesIn(io
.size
);
970 ++ IOStats
.Ftp
.reads
;
972 for (j
= io
.size
- 1, bin
= 0; j
; ++bin
)
975 ++ IOStats
.Ftp
.read_hist
[bin
];
978 if (io
.flag
!= Comm::OK
) {
979 debugs(50, ignoreErrno(io
.xerrno
) ? 3 : DBG_IMPORTANT
,
980 "FTP data read error: " << xstrerr(io
.xerrno
));
982 if (ignoreErrno(io
.xerrno
)) {
983 maybeReadVirginBody();
985 failed(ERR_READ_ERROR
, 0);
986 /* failed closes ctrl.conn and frees ftpState */
989 } else if (io
.size
== 0) {
990 debugs(9, 3, "Calling dataComplete() because io.size == 0");
993 * Dangerous curves ahead. This call to dataComplete was
994 * calling scheduleReadControlReply, handleControlReply,
995 * and then ftpReadTransferDone. If ftpReadTransferDone
996 * gets unexpected status code, it closes down the control
997 * socket and our FtpStateData object gets destroyed. As
998 * a workaround we no longer set the 'buffered_ok' flag in
999 * the scheduleReadControlReply call.
1008 Ftp::Client::dataComplete()
1010 debugs(9, 3,status());
1012 /* Connection closed; transfer done. */
1014 /// Close data channel, if any, to conserve resources while we wait.
1017 /* expect the "transfer complete" message on the control socket */
1020 * Previously, this was the only place where we set the
1021 * 'buffered_ok' flag when calling scheduleReadControlReply().
1022 * It caused some problems if the FTP server returns an unexpected
1023 * status code after the data command. FtpStateData was being
1024 * deleted in the middle of dataRead().
1026 /* AYJ: 2011-01-13: Bug 2581.
1027 * 226 status is possibly waiting in the ctrl buffer.
1028 * The connection will hang if we DONT send buffered_ok.
1029 * This happens on all transfers which can be completely sent by the
1030 * server before the 150 started status message is read in by Squid.
1031 * ie all transfers of about one packet hang.
1033 scheduleReadControlReply(1);
1037 Ftp::Client::abortAll(const char *reason
)
1039 debugs(9, 3, "aborting transaction for " << reason
<<
1040 "; FD " << (ctrl
.conn
!=NULL
?ctrl
.conn
->fd
:-1) << ", Data FD " << (data
.conn
!=NULL
?data
.conn
->fd
:-1) << ", this " << this);
1045 * Cancel the timeout on the Control socket and establish one
1046 * on the data socket
1049 Ftp::Client::switchTimeoutToDataChannel()
1051 commUnsetConnTimeout(ctrl
.conn
);
1053 typedef CommCbMemFunT
<Client
, CommTimeoutCbParams
> TimeoutDialer
;
1054 AsyncCall::Pointer timeoutCall
= JobCallback(9, 5, TimeoutDialer
, this,
1055 Ftp::Client::timeout
);
1056 commSetConnTimeout(data
.conn
, Config
.Timeout
.read
, timeoutCall
);
1060 Ftp::Client::sentRequestBody(const CommIoCbParams
&io
)
1063 statCounter
.server
.ftp
.kbytes_out
+= io
.size
;
1064 ::Client::sentRequestBody(io
);
1068 * called after we wrote the last byte of the request body
1071 Ftp::Client::doneSendingRequestBody()
1073 ::Client::doneSendingRequestBody();
1074 debugs(9, 3, status());
1076 /* NP: RFC 959 3.3. DATA CONNECTION MANAGEMENT
1077 * if transfer type is 'stream' call dataComplete()
1078 * otherwise leave open. (reschedule control channel read?)
1082 /// Parses FTP server control response into ctrl structure fields,
1083 /// setting bytesUsed and returning true on success.
1085 Ftp::Client::parseControlReply(size_t &bytesUsed
)
1092 wordlist
*head
= NULL
;
1094 wordlist
**tail
= &head
;
1096 debugs(9, 3, status());
1098 * We need a NULL-terminated buffer for scanning, ick
1100 const size_t len
= ctrl
.offset
;
1101 sbuf
= (char *)xmalloc(len
+ 1);
1102 xstrncpy(sbuf
, ctrl
.buf
, len
+ 1);
1103 end
= sbuf
+ len
- 1;
1105 while (*end
!= '\r' && *end
!= '\n' && end
> sbuf
)
1108 usable
= end
- sbuf
;
1110 debugs(9, 3, "usable = " << usable
);
1113 debugs(9, 3, "didn't find end of line");
1118 debugs(9, 3, len
<< " bytes to play with");
1121 s
+= strspn(s
, crlf
);
1123 for (; s
< end
; s
+= strcspn(s
, crlf
), s
+= strspn(s
, crlf
)) {
1127 debugs(9, 5, "s = {" << s
<< "}");
1129 linelen
= strcspn(s
, crlf
) + 1;
1135 complete
= (*s
>= '0' && *s
<= '9' && *(s
+ 3) == ' ');
1137 list
= new wordlist();
1139 list
->key
= (char *)xmalloc(linelen
);
1141 xstrncpy(list
->key
, s
, linelen
);
1143 /* trace the FTP communication chat at level 2 */
1144 debugs(9, 2, "ftp>> " << list
->key
);
1147 // use list->key for last_reply because s contains the new line
1148 ctrl
.last_reply
= xstrdup(list
->key
+ 4);
1149 ctrl
.replycode
= atoi(list
->key
);
1157 bytesUsed
= static_cast<size_t>(s
- sbuf
);
1161 wordlistDestroy(&head
);
1165 ctrl
.message
= head
;
1166 assert(ctrl
.replycode
>= 0);
1167 assert(ctrl
.last_reply
);
1168 assert(ctrl
.message
);