]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/clients/FtpClient.cc
2 * Copyright (C) 1996-2018 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 "errorpage.h"
21 #include "ftp/Parsing.h"
22 #include "http/Stream.h"
24 #include "SquidConfig.h"
25 #include "SquidString.h"
26 #include "StatCounters.h"
35 const char *const crlf
= "\r\n";
38 escapeIAC(const char *buf
)
42 unsigned const char *p
;
45 for (p
= (unsigned const char *)buf
, n
= 1; *p
; ++n
, ++p
)
49 ret
= (char *)xmalloc(n
);
51 for (p
= (unsigned const char *)buf
, r
=(unsigned char *)ret
; *p
; ++p
) {
63 assert((r
- (unsigned char *)ret
) == n
);
69 /// configures the channel with a descriptor and registers a close handler
71 Ftp::Channel::opened(const Comm::ConnectionPointer
&newConn
,
72 const AsyncCall::Pointer
&aCloser
)
74 assert(!Comm::IsConnOpen(conn
));
75 assert(closer
== NULL
);
77 assert(Comm::IsConnOpen(newConn
));
78 assert(aCloser
!= NULL
);
82 comm_add_close_handler(conn
->fd
, closer
);
85 /// planned close: removes the close handler and calls comm_close
89 // channels with active listeners will be closed when the listener handler dies.
90 if (Comm::IsConnOpen(conn
)) {
91 comm_remove_close_handler(conn
->fd
, closer
);
92 conn
->close(); // we do not expect to be called back
98 Ftp::Channel::forget()
100 if (Comm::IsConnOpen(conn
)) {
101 commUnsetConnTimeout(conn
);
102 comm_remove_close_handler(conn
->fd
, closer
);
108 Ftp::Channel::clear()
114 /* Ftp::CtrlChannel */
116 Ftp::CtrlChannel::CtrlChannel():
125 buf
= static_cast<char*>(memAllocBuf(4096, &size
));
128 Ftp::CtrlChannel::~CtrlChannel()
130 memFreeBuf(size
, buf
);
132 wordlistDestroy(&message
);
133 safe_free(last_command
);
134 safe_free(last_reply
);
137 /* Ftp::DataChannel */
139 Ftp::DataChannel::DataChannel():
147 Ftp::DataChannel::~DataChannel()
153 Ftp::DataChannel::addr(const Ip::Address
&import
)
155 static char addrBuf
[MAX_IPSTRLEN
];
156 import
.toStr(addrBuf
, sizeof(addrBuf
));
158 host
= xstrdup(addrBuf
);
159 port
= import
.port();
164 Ftp::Client::Client(FwdState
*fwdState
):
165 AsyncJob("Ftp::Client"),
172 shortenReadTimeout(false)
174 ++statCounter
.server
.all
.requests
;
175 ++statCounter
.server
.ftp
.requests
;
177 ctrl
.last_command
= xstrdup("Connect to server");
179 typedef CommCbMemFunT
<Client
, CommCloseCbParams
> Dialer
;
180 const AsyncCall::Pointer closer
= JobCallback(9, 5, Dialer
, this,
181 Ftp::Client::ctrlClosed
);
182 ctrl
.opened(fwdState
->serverConnection(), closer
);
185 Ftp::Client::~Client()
187 if (data
.opener
!= NULL
) {
188 data
.opener
->cancel("Ftp::Client destructed");
193 safe_free(old_request
);
194 safe_free(old_reply
);
195 fwd
= NULL
; // refcounted
201 scheduleReadControlReply(0);
205 Ftp::Client::initReadBuf()
207 if (data
.readBuf
== NULL
) {
208 data
.readBuf
= new MemBuf
;
209 data
.readBuf
->init(4096, SQUID_TCP_SO_RCVBUF
);
214 * Close the FTP server connection(s). Used by serverComplete().
217 Ftp::Client::closeServer()
219 if (Comm::IsConnOpen(ctrl
.conn
)) {
220 debugs(9, 3, "closing FTP server FD " << ctrl
.conn
->fd
<< ", this " << this);
221 fwd
->unregister(ctrl
.conn
);
225 if (Comm::IsConnOpen(data
.conn
)) {
226 debugs(9, 3, "closing FTP data FD " << data
.conn
->fd
<< ", this " << this);
230 debugs(9, 3, "FTP ctrl and data connections closed. this " << this);
234 * Did we close all FTP server connection(s)?
236 \retval true Both server control and data channels are closed. And not waiting for a new data connection to open.
237 \retval false Either control channel or data is still active.
240 Ftp::Client::doneWithServer() const
242 return !Comm::IsConnOpen(ctrl
.conn
) && !Comm::IsConnOpen(data
.conn
);
246 Ftp::Client::failed(err_type error
, int xerrno
, ErrorState
*err
)
248 debugs(9, 3, "entry-null=" << (entry
?entry
->isEmpty():0) << ", entry=" << entry
);
250 const char *command
, *reply
;
254 debugs(9, 6, "error=" << err
->type
<< ", code=" << xerrno
<<
255 ", status=" << err
->httpStatus
);
259 Http::StatusCode httpStatus
= failedHttpStatus(error
);
260 ftperr
= new ErrorState(error
, httpStatus
, fwd
->request
);
263 ftperr
->xerrno
= xerrno
;
265 ftperr
->ftp
.server_msg
= ctrl
.message
;
269 command
= old_request
;
271 command
= ctrl
.last_command
;
273 if (command
&& strncmp(command
, "PASS", 4) == 0)
274 command
= "PASS <yourpassword>";
279 reply
= ctrl
.last_reply
;
282 ftperr
->ftp
.request
= xstrdup(command
);
285 ftperr
->ftp
.reply
= xstrdup(reply
);
288 fwd
->request
->detailError(error
, xerrno
);
290 closeServer(); // we failed, so no serverComplete()
295 Ftp::Client::failedHttpStatus(err_type
&error
)
297 if (error
== ERR_NONE
)
298 error
= ERR_FTP_FAILURE
;
299 return error
== ERR_READ_TIMEOUT
? Http::scGatewayTimeout
:
305 * Looks like there are no longer anymore callers that set
306 * buffered_ok=1. Perhaps it can be removed at some point.
309 Ftp::Client::scheduleReadControlReply(int buffered_ok
)
311 debugs(9, 3, ctrl
.conn
);
313 if (buffered_ok
&& ctrl
.offset
> 0) {
314 /* We've already read some reply data */
315 handleControlReply();
318 if (!Comm::IsConnOpen(ctrl
.conn
)) {
319 debugs(9, 3, "cannot read without ctrl " << ctrl
.conn
);
323 * Cancel the timeout on the Data socket (if any) and
324 * establish one on the control socket.
326 if (Comm::IsConnOpen(data
.conn
)) {
327 commUnsetConnTimeout(data
.conn
);
330 const time_t tout
= shortenReadTimeout
?
331 min(Config
.Timeout
.connect
, Config
.Timeout
.read
):
333 shortenReadTimeout
= false; // we only need to do this once, after PASV
335 typedef CommCbMemFunT
<Client
, CommTimeoutCbParams
> TimeoutDialer
;
336 AsyncCall::Pointer timeoutCall
= JobCallback(9, 5, TimeoutDialer
, this, Ftp::Client::timeout
);
337 commSetConnTimeout(ctrl
.conn
, tout
, timeoutCall
);
339 typedef CommCbMemFunT
<Client
, CommIoCbParams
> Dialer
;
340 AsyncCall::Pointer reader
= JobCallback(9, 5, Dialer
, this, Ftp::Client::readControlReply
);
341 comm_read(ctrl
.conn
, ctrl
.buf
+ ctrl
.offset
, ctrl
.size
- ctrl
.offset
, reader
);
346 Ftp::Client::readControlReply(const CommIoCbParams
&io
)
348 debugs(9, 3, "FD " << io
.fd
<< ", Read " << io
.size
<< " bytes");
351 statCounter
.server
.all
.kbytes_in
+= io
.size
;
352 statCounter
.server
.ftp
.kbytes_in
+= io
.size
;
355 if (io
.flag
== Comm::ERR_CLOSING
)
358 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
359 if (abortOnData("entry aborted during control reply read"))
363 assert(ctrl
.offset
< ctrl
.size
);
365 if (io
.flag
== Comm::OK
&& io
.size
> 0) {
366 fd_bytes(io
.fd
, io
.size
, FD_READ
);
369 if (io
.flag
!= Comm::OK
) {
370 debugs(50, ignoreErrno(io
.xerrno
) ? 3 : DBG_IMPORTANT
,
371 "FTP control reply read error: " << xstrerr(io
.xerrno
));
373 if (ignoreErrno(io
.xerrno
)) {
374 scheduleReadControlReply(0);
376 failed(ERR_READ_ERROR
, io
.xerrno
);
377 /* failed closes ctrl.conn and frees ftpState */
383 if (entry
->store_status
== STORE_PENDING
) {
384 failed(ERR_FTP_FAILURE
, 0);
385 /* failed closes ctrl.conn and frees ftpState */
389 /* XXX this may end up having to be serverComplete() .. */
390 abortAll("zero control reply read");
394 unsigned int len
=io
.size
+ ctrl
.offset
;
396 assert(len
<= ctrl
.size
);
397 if (Comm::IsConnOpen(ctrl
.conn
))
398 commUnsetConnTimeout(ctrl
.conn
); // we are done waiting for ctrl reply
399 handleControlReply();
403 Ftp::Client::handleControlReply()
405 debugs(9, 3, status());
407 size_t bytes_used
= 0;
408 wordlistDestroy(&ctrl
.message
);
410 if (!parseControlReply(bytes_used
)) {
411 /* didn't get complete reply yet */
413 if (ctrl
.offset
== ctrl
.size
) {
414 ctrl
.buf
= static_cast<char*>(memReallocBuf(ctrl
.buf
, ctrl
.size
<< 1, &ctrl
.size
));
417 scheduleReadControlReply(0);
421 assert(ctrl
.message
); // the entire FTP server response, line by line
422 assert(ctrl
.replycode
>= 0); // FTP status code (from the last line)
423 assert(ctrl
.last_reply
); // FTP reason (from the last line)
425 if (ctrl
.offset
== bytes_used
) {
429 /* Got some data past the complete reply */
430 assert(bytes_used
< ctrl
.offset
);
431 ctrl
.offset
-= bytes_used
;
432 memmove(ctrl
.buf
, ctrl
.buf
+ bytes_used
, ctrl
.offset
);
435 debugs(9, 3, "state=" << state
<< ", code=" << ctrl
.replycode
);
439 Ftp::Client::handlePasvReply(Ip::Address
&srvAddr
)
441 int code
= ctrl
.replycode
;
443 debugs(9, 3, status());
445 if (!Comm::IsConnOpen(ctrl
.conn
)) {
446 debugs(9, 5, "The control connection to the remote end is closed");
451 debugs(9, 2, "PASV not supported by remote end");
455 /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */
456 /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */
457 debugs(9, 5, "scanning: " << ctrl
.last_reply
);
459 buf
= ctrl
.last_reply
+ strcspn(ctrl
.last_reply
, "0123456789");
461 const char *forceIp
= Config
.Ftp
.sanitycheck
?
462 fd_table
[ctrl
.conn
->fd
].ipaddr
: NULL
;
463 if (!Ftp::ParseIpPort(buf
, forceIp
, srvAddr
)) {
464 debugs(9, DBG_IMPORTANT
, "Unsafe PASV reply from " <<
465 ctrl
.conn
->remote
<< ": " << ctrl
.last_reply
);
475 Ftp::Client::handleEpsvReply(Ip::Address
&remoteAddr
)
477 int code
= ctrl
.replycode
;
479 debugs(9, 3, status());
481 if (!Comm::IsConnOpen(ctrl
.conn
)) {
482 debugs(9, 5, "The control connection to the remote end is closed");
486 if (code
!= 229 && code
!= 522) {
488 /* handle broken servers (RFC 2428 says OK code for EPSV MUST be 229 not 200) */
489 /* vsftpd for one send '200 EPSV ALL ok.' without even port info.
490 * Its okay to re-send EPSV 1/2 but nothing else. */
491 debugs(9, DBG_IMPORTANT
, "Broken FTP Server at " << ctrl
.conn
->remote
<< ". Wrong accept code for EPSV");
493 debugs(9, 2, "EPSV not supported by remote end");
495 return sendPassive();
499 /* Peer responded with a list of supported methods:
500 * 522 Network protocol not supported, use (1)
501 * 522 Network protocol not supported, use (1,2)
502 * 522 Network protocol not supported, use (2)
503 * TODO: Handle the (1,2) case which may happen after EPSV ALL. Close
504 * data + control without self-destructing and re-open from scratch.
506 debugs(9, 5, "scanning: " << ctrl
.last_reply
);
507 buf
= ctrl
.last_reply
;
508 while (buf
!= NULL
&& *buf
!= '\0' && *buf
!= '\n' && *buf
!= '(')
510 if (buf
!= NULL
&& *buf
== '\n')
513 if (buf
== NULL
|| *buf
== '\0') {
514 /* handle broken server (RFC 2428 says MUST specify supported protocols in 522) */
515 debugs(9, DBG_IMPORTANT
, "Broken FTP Server at " << ctrl
.conn
->remote
<< ". 522 error missing protocol negotiation hints");
516 return sendPassive();
517 } else if (strcmp(buf
, "(1)") == 0) {
518 state
= SENT_EPSV_2
; /* simulate having sent and failed EPSV 2 */
519 return sendPassive();
520 } else if (strcmp(buf
, "(2)") == 0) {
521 if (Ip::EnableIpv6
) {
522 /* If server only supports EPSV 2 and we have already tried that. Go straight to EPRT */
523 if (state
== SENT_EPSV_2
) {
526 /* or try the next Passive mode down the chain. */
527 return sendPassive();
530 /* Server only accept EPSV in IPv6 traffic. */
531 state
= SENT_EPSV_1
; /* simulate having sent and failed EPSV 1 */
532 return sendPassive();
535 /* handle broken server (RFC 2428 says MUST specify supported protocols in 522) */
536 debugs(9, DBG_IMPORTANT
, "WARNING: Server at " << ctrl
.conn
->remote
<< " sent unknown protocol negotiation hint: " << buf
);
537 return sendPassive();
539 /* coverity[unreachable] */
540 /* safeguard against possible future bugs in above conditions */
541 failed(ERR_FTP_FAILURE
, 0);
545 /* 229 Entering Extended Passive Mode (|||port|) */
546 /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */
547 debugs(9, 5, "scanning: " << ctrl
.last_reply
);
549 buf
= ctrl
.last_reply
+ strcspn(ctrl
.last_reply
, "(");
553 int n
= sscanf(buf
, "(%c%c%c%hu%c)", &h1
, &h2
, &h3
, &port
, &h4
);
555 if (n
< 4 || h1
!= h2
|| h1
!= h3
|| h1
!= h4
) {
556 debugs(9, DBG_IMPORTANT
, "Invalid EPSV reply from " <<
557 ctrl
.conn
->remote
<< ": " <<
560 return sendPassive();
564 debugs(9, DBG_IMPORTANT
, "Unsafe EPSV reply from " <<
565 ctrl
.conn
->remote
<< ": " <<
568 return sendPassive();
571 if (Config
.Ftp
.sanitycheck
) {
573 debugs(9, DBG_IMPORTANT
, "Unsafe EPSV reply from " <<
574 ctrl
.conn
->remote
<< ": " <<
577 return sendPassive();
581 remoteAddr
= ctrl
.conn
->remote
;
582 remoteAddr
.port(port
);
583 data
.addr(remoteAddr
);
587 // FTP clients do not support EPRT and PORT commands yet.
588 // The Ftp::Client::sendEprt() will fail because of the unimplemented
589 // openListenSocket() or sendPort() methods
591 Ftp::Client::sendEprt()
593 if (!Config
.Ftp
.eprt
) {
594 /* Disabled. Switch immediately to attempting old PORT command. */
595 debugs(9, 3, "EPRT disabled by local administrator");
599 debugs(9, 3, status());
601 if (!openListenSocket()) {
602 failed(ERR_FTP_FAILURE
, 0);
606 debugs(9, 3, "Listening for FTP data connection with FD " << data
.conn
);
607 if (!Comm::IsConnOpen(data
.conn
)) {
608 // TODO: Set error message.
609 failed(ERR_FTP_FAILURE
, 0);
615 char buf
[MAX_IPSTRLEN
];
616 /* RFC 2428 defines EPRT as IPv6 equivalent to IPv4 PORT command. */
617 /* Which can be used by EITHER protocol. */
618 debugs(9, 3, "Listening for FTP data connection on port" << comm_local_port(data
.conn
->fd
) << " or port?" << data
.conn
->local
.port());
619 mb
.appendf("EPRT |%d|%s|%d|%s",
620 ( data
.conn
->local
.isIPv6() ? 2 : 1 ),
621 data
.conn
->local
.toStr(buf
,MAX_IPSTRLEN
),
622 comm_local_port(data
.conn
->fd
), Ftp::crlf
);
625 writeCommand(mb
.content());
630 Ftp::Client::sendPort()
632 failed(ERR_FTP_FAILURE
, 0);
637 Ftp::Client::sendPassive()
639 debugs(9, 3, status());
642 * Checks for EPSV ALL special conditions:
643 * If enabled to be sent, squid MUST NOT request any other connect methods.
644 * If 'ALL' is sent and fails the entire FTP Session fails.
645 * NP: By my reading exact EPSV protocols maybe attempted, but only EPSV method. */
646 if (Config
.Ftp
.epsv_all
&& state
== SENT_EPSV_1
) {
647 // We are here because the last "EPSV 1" failed, but because of epsv_all
648 // no other method allowed.
649 debugs(9, DBG_IMPORTANT
, "FTP does not allow PASV method after 'EPSV ALL' has been sent.");
650 failed(ERR_FTP_FAILURE
, 0);
654 /// Closes any old FTP-Data connection which may exist. */
658 * Checks for previous EPSV/PASV failures on this server/session.
659 * Diverts to EPRT immediately if they are not working. */
660 if (!Config
.Ftp
.passive
|| state
== SENT_PASV
) {
668 * Send EPSV (ALL,2,1) or PASV on the control channel.
670 * - EPSV ALL is used if enabled.
671 * - EPSV 2 is used if ALL is disabled and IPv6 is available and ctrl channel is IPv6.
672 * - EPSV 1 is used if EPSV 2 (IPv6) fails or is not available or ctrl channel is IPv4.
673 * - PASV is used if EPSV 1 fails.
676 case SENT_EPSV_ALL
: /* EPSV ALL resulted in a bad response. Try ther EPSV methods. */
677 if (ctrl
.conn
->local
.isIPv6()) {
678 debugs(9, 5, "FTP Channel is IPv6 (" << ctrl
.conn
->remote
<< ") attempting EPSV 2 after EPSV ALL has failed.");
679 mb
.appendf("EPSV 2%s", Ftp::crlf
);
683 // else fall through to skip EPSV 2
685 case SENT_EPSV_2
: /* EPSV IPv6 failed. Try EPSV IPv4 */
686 if (ctrl
.conn
->local
.isIPv4()) {
687 debugs(9, 5, "FTP Channel is IPv4 (" << ctrl
.conn
->remote
<< ") attempting EPSV 1 after EPSV ALL has failed.");
688 mb
.appendf("EPSV 1%s", Ftp::crlf
);
691 } else if (Config
.Ftp
.epsv_all
) {
692 debugs(9, DBG_IMPORTANT
, "FTP does not allow PASV method after 'EPSV ALL' has been sent.");
693 failed(ERR_FTP_FAILURE
, 0);
696 // else fall through to skip EPSV 1
698 case SENT_EPSV_1
: /* EPSV options exhausted. Try PASV now. */
699 debugs(9, 5, "FTP Channel (" << ctrl
.conn
->remote
<< ") rejects EPSV connection attempts. Trying PASV instead.");
700 mb
.appendf("PASV%s", Ftp::crlf
);
706 if (Config
.accessList
.ftp_epsv
) {
707 ACLFilledChecklist
checklist(Config
.accessList
.ftp_epsv
, fwd
->request
, NULL
);
708 doEpsv
= checklist
.fastCheck().allowed();
711 debugs(9, 5, "EPSV support manually disabled. Sending PASV for FTP Channel (" << ctrl
.conn
->remote
<<")");
712 mb
.appendf("PASV%s", Ftp::crlf
);
714 } else if (Config
.Ftp
.epsv_all
) {
715 debugs(9, 5, "EPSV ALL manually enabled. Attempting with FTP Channel (" << ctrl
.conn
->remote
<<")");
716 mb
.appendf("EPSV ALL%s", Ftp::crlf
);
717 state
= SENT_EPSV_ALL
;
719 if (ctrl
.conn
->local
.isIPv6()) {
720 debugs(9, 5, "FTP Channel (" << ctrl
.conn
->remote
<< "). Sending default EPSV 2");
721 mb
.appendf("EPSV 2%s", Ftp::crlf
);
724 if (ctrl
.conn
->local
.isIPv4()) {
725 debugs(9, 5, "Channel (" << ctrl
.conn
->remote
<<"). Sending default EPSV 1");
726 mb
.appendf("EPSV 1%s", Ftp::crlf
);
735 wordlistDestroy(&ctrl
.message
);
736 ctrl
.message
= NULL
; //No message to return to client.
737 ctrl
.offset
= 0; //reset readed response, to make room read the next response
739 writeCommand(mb
.content());
741 shortenReadTimeout
= true;
746 Ftp::Client::connectDataChannel()
748 if (!Comm::IsConnOpen(ctrl
.conn
)) {
749 debugs(9, 5, "The control connection to the remote end is closed");
753 safe_free(ctrl
.last_command
);
755 safe_free(ctrl
.last_reply
);
757 ctrl
.last_command
= xstrdup("Connect to server data port");
759 // Generate a new data channel descriptor to be opened.
760 Comm::ConnectionPointer conn
= new Comm::Connection
;
761 conn
->setAddrs(ctrl
.conn
->local
, data
.host
);
763 conn
->remote
.port(data
.port
);
764 conn
->tos
= ctrl
.conn
->tos
;
765 conn
->nfmark
= ctrl
.conn
->nfmark
;
767 debugs(9, 3, "connecting to " << conn
->remote
);
769 typedef CommCbMemFunT
<Client
, CommConnectCbParams
> Dialer
;
770 data
.opener
= JobCallback(9, 3, Dialer
, this, Ftp::Client::dataChannelConnected
);
771 Comm::ConnOpener
*cs
= new Comm::ConnOpener(conn
, data
.opener
, Config
.Timeout
.connect
);
772 cs
->setHost(data
.host
);
777 Ftp::Client::openListenSocket()
782 /// creates a data channel Comm close callback
784 Ftp::Client::dataCloser()
786 typedef CommCbMemFunT
<Client
, CommCloseCbParams
> Dialer
;
787 return JobCallback(9, 5, Dialer
, this, Ftp::Client::dataClosed
);
790 /// handler called by Comm when FTP data channel is closed unexpectedly
792 Ftp::Client::dataClosed(const CommCloseCbParams
&)
794 debugs(9, 4, status());
795 if (data
.listenConn
!= NULL
) {
796 data
.listenConn
->close();
797 data
.listenConn
= NULL
;
798 // NP clear() does the: data.fd = -1;
804 Ftp::Client::writeCommand(const char *buf
)
807 /* trace FTP protocol communications at level 2 */
808 debugs(9, 2, "ftp<< " << buf
);
810 if (Config
.Ftp
.telnet
)
811 ebuf
= escapeIAC(buf
);
815 safe_free(ctrl
.last_command
);
817 safe_free(ctrl
.last_reply
);
819 ctrl
.last_command
= ebuf
;
821 if (!Comm::IsConnOpen(ctrl
.conn
)) {
822 debugs(9, 2, "cannot send to closing ctrl " << ctrl
.conn
);
823 // TODO: assert(ctrl.closer != NULL);
827 typedef CommCbMemFunT
<Client
, CommIoCbParams
> Dialer
;
828 AsyncCall::Pointer call
= JobCallback(9, 5, Dialer
, this,
829 Ftp::Client::writeCommandCallback
);
830 Comm::Write(ctrl
.conn
, ctrl
.last_command
, strlen(ctrl
.last_command
), call
, NULL
);
832 scheduleReadControlReply(0);
836 Ftp::Client::writeCommandCallback(const CommIoCbParams
&io
)
839 debugs(9, 5, "wrote " << io
.size
<< " bytes");
842 fd_bytes(io
.fd
, io
.size
, FD_WRITE
);
843 statCounter
.server
.all
.kbytes_out
+= io
.size
;
844 statCounter
.server
.ftp
.kbytes_out
+= io
.size
;
847 if (io
.flag
== Comm::ERR_CLOSING
)
851 debugs(9, DBG_IMPORTANT
, "FTP command write error: " << io
.conn
<< ": " << xstrerr(io
.xerrno
));
852 failed(ERR_WRITE_ERROR
, io
.xerrno
);
853 /* failed closes ctrl.conn and frees ftpState */
858 /// handler called by Comm when FTP control channel is closed unexpectedly
860 Ftp::Client::ctrlClosed(const CommCloseCbParams
&)
862 debugs(9, 4, status());
864 doneWithFwd
= "ctrlClosed()"; // assume FwdState is monitoring too
865 mustStop("Ftp::Client::ctrlClosed");
869 Ftp::Client::timeout(const CommTimeoutCbParams
&io
)
871 debugs(9, 4, io
.conn
<< ": '" << entry
->url() << "'" );
873 if (abortOnBadEntry("entry went bad while waiting for a timeout"))
876 failed(ERR_READ_TIMEOUT
, 0);
877 /* failed() closes ctrl.conn and frees ftpState */
880 const Comm::ConnectionPointer
&
881 Ftp::Client::dataConnection() const
887 Ftp::Client::maybeReadVirginBody()
890 if (!Comm::IsConnOpen(data
.conn
) || fd_table
[data
.conn
->fd
].closing())
893 if (data
.read_pending
)
898 const int read_sz
= replyBodySpace(*data
.readBuf
, 0);
900 debugs(9, 9, "FTP may read up to " << read_sz
<< " bytes");
902 if (read_sz
< 2) // see http.cc
905 data
.read_pending
= true;
907 typedef CommCbMemFunT
<Client
, CommTimeoutCbParams
> TimeoutDialer
;
908 AsyncCall::Pointer timeoutCall
= JobCallback(9, 5,
909 TimeoutDialer
, this, Ftp::Client::timeout
);
910 commSetConnTimeout(data
.conn
, Config
.Timeout
.read
, timeoutCall
);
912 debugs(9,5,"queueing read on FD " << data
.conn
->fd
);
914 typedef CommCbMemFunT
<Client
, CommIoCbParams
> Dialer
;
915 entry
->delayAwareRead(data
.conn
, data
.readBuf
->space(), read_sz
,
916 JobCallback(9, 5, Dialer
, this, Ftp::Client::dataRead
));
920 Ftp::Client::dataRead(const CommIoCbParams
&io
)
925 data
.read_pending
= false;
927 debugs(9, 3, "FD " << io
.fd
<< " Read " << io
.size
<< " bytes");
930 statCounter
.server
.all
.kbytes_in
+= io
.size
;
931 statCounter
.server
.ftp
.kbytes_in
+= io
.size
;
934 if (io
.flag
== Comm::ERR_CLOSING
)
937 assert(io
.fd
== data
.conn
->fd
);
939 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
940 abortOnData("entry aborted during dataRead");
944 if (io
.flag
== Comm::OK
&& io
.size
> 0) {
945 debugs(9, 5, "appended " << io
.size
<< " bytes to readBuf");
946 data
.readBuf
->appended(io
.size
);
948 DelayId delayId
= entry
->mem_obj
->mostBytesAllowed();
949 delayId
.bytesIn(io
.size
);
951 ++ IOStats
.Ftp
.reads
;
953 for (j
= io
.size
- 1, bin
= 0; j
; ++bin
)
956 ++ IOStats
.Ftp
.read_hist
[bin
];
959 if (io
.flag
!= Comm::OK
) {
960 debugs(50, ignoreErrno(io
.xerrno
) ? 3 : DBG_IMPORTANT
,
961 "FTP data read error: " << xstrerr(io
.xerrno
));
963 if (ignoreErrno(io
.xerrno
)) {
964 maybeReadVirginBody();
966 failed(ERR_READ_ERROR
, 0);
967 /* failed closes ctrl.conn and frees ftpState */
970 } else if (io
.size
== 0) {
971 debugs(9, 3, "Calling dataComplete() because io.size == 0");
974 * Dangerous curves ahead. This call to dataComplete was
975 * calling scheduleReadControlReply, handleControlReply,
976 * and then ftpReadTransferDone. If ftpReadTransferDone
977 * gets unexpected status code, it closes down the control
978 * socket and our FtpStateData object gets destroyed. As
979 * a workaround we no longer set the 'buffered_ok' flag in
980 * the scheduleReadControlReply call.
989 Ftp::Client::dataComplete()
991 debugs(9, 3,status());
993 /* Connection closed; transfer done. */
995 /// Close data channel, if any, to conserve resources while we wait.
998 /* expect the "transfer complete" message on the control socket */
1001 * Previously, this was the only place where we set the
1002 * 'buffered_ok' flag when calling scheduleReadControlReply().
1003 * It caused some problems if the FTP server returns an unexpected
1004 * status code after the data command. FtpStateData was being
1005 * deleted in the middle of dataRead().
1007 /* AYJ: 2011-01-13: Bug 2581.
1008 * 226 status is possibly waiting in the ctrl buffer.
1009 * The connection will hang if we DONT send buffered_ok.
1010 * This happens on all transfers which can be completly sent by the
1011 * server before the 150 started status message is read in by Squid.
1012 * ie all transfers of about one packet hang.
1014 scheduleReadControlReply(1);
1018 Ftp::Client::abortAll(const char *reason
)
1020 debugs(9, 3, "aborting transaction for " << reason
<<
1021 "; FD " << (ctrl
.conn
!=NULL
?ctrl
.conn
->fd
:-1) << ", Data FD " << (data
.conn
!=NULL
?data
.conn
->fd
:-1) << ", this " << this);
1026 * Cancel the timeout on the Control socket and establish one
1027 * on the data socket
1030 Ftp::Client::switchTimeoutToDataChannel()
1032 commUnsetConnTimeout(ctrl
.conn
);
1034 typedef CommCbMemFunT
<Client
, CommTimeoutCbParams
> TimeoutDialer
;
1035 AsyncCall::Pointer timeoutCall
= JobCallback(9, 5, TimeoutDialer
, this,
1036 Ftp::Client::timeout
);
1037 commSetConnTimeout(data
.conn
, Config
.Timeout
.read
, timeoutCall
);
1041 Ftp::Client::sentRequestBody(const CommIoCbParams
&io
)
1044 statCounter
.server
.ftp
.kbytes_out
+= io
.size
;
1045 ::Client::sentRequestBody(io
);
1049 * called after we wrote the last byte of the request body
1052 Ftp::Client::doneSendingRequestBody()
1054 ::Client::doneSendingRequestBody();
1055 debugs(9, 3, status());
1057 /* NP: RFC 959 3.3. DATA CONNECTION MANAGEMENT
1058 * if transfer type is 'stream' call dataComplete()
1059 * otherwise leave open. (reschedule control channel read?)
1063 /// Parses FTP server control response into ctrl structure fields,
1064 /// setting bytesUsed and returning true on success.
1066 Ftp::Client::parseControlReply(size_t &bytesUsed
)
1073 wordlist
*head
= NULL
;
1075 wordlist
**tail
= &head
;
1077 debugs(9, 3, status());
1079 * We need a NULL-terminated buffer for scanning, ick
1081 const size_t len
= ctrl
.offset
;
1082 sbuf
= (char *)xmalloc(len
+ 1);
1083 xstrncpy(sbuf
, ctrl
.buf
, len
+ 1);
1084 end
= sbuf
+ len
- 1;
1086 while (*end
!= '\r' && *end
!= '\n' && end
> sbuf
)
1089 usable
= end
- sbuf
;
1091 debugs(9, 3, "usable = " << usable
);
1094 debugs(9, 3, "didn't find end of line");
1099 debugs(9, 3, len
<< " bytes to play with");
1102 s
+= strspn(s
, crlf
);
1104 for (; s
< end
; s
+= strcspn(s
, crlf
), s
+= strspn(s
, crlf
)) {
1108 debugs(9, 5, "s = {" << s
<< "}");
1110 linelen
= strcspn(s
, crlf
) + 1;
1116 complete
= (*s
>= '0' && *s
<= '9' && *(s
+ 3) == ' ');
1118 list
= new wordlist();
1120 list
->key
= (char *)xmalloc(linelen
);
1122 xstrncpy(list
->key
, s
, linelen
);
1124 /* trace the FTP communication chat at level 2 */
1125 debugs(9, 2, "ftp>> " << list
->key
);
1128 // use list->key for last_reply because s contains the new line
1129 ctrl
.last_reply
= xstrdup(list
->key
+ 4);
1130 ctrl
.replycode
= atoi(list
->key
);
1138 bytesUsed
= static_cast<size_t>(s
- sbuf
);
1142 wordlistDestroy(&head
);
1146 ctrl
.message
= head
;
1147 assert(ctrl
.replycode
>= 0);
1148 assert(ctrl
.last_reply
);
1149 assert(ctrl
.message
);