2 * Copyright (C) 1996-2016 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 33 Transfer protocol servers */
12 #include "acl/FilledChecklist.h"
13 #include "base/CharacterSet.h"
14 #include "base/RefCount.h"
15 #include "base/Subscription.h"
16 #include "client_side_reply.h"
17 #include "client_side_request.h"
18 #include "clientStream.h"
19 #include "comm/ConnOpener.h"
20 #include "comm/Read.h"
21 #include "comm/TcpAcceptor.h"
22 #include "comm/Write.h"
23 #include "errorpage.h"
25 #include "ftp/Elements.h"
26 #include "ftp/Parsing.h"
28 #include "http/one/RequestParser.h"
29 #include "http/Stream.h"
30 #include "HttpHdrCc.h"
32 #include "ipc/FdNotes.h"
33 #include "parser/Tokenizer.h"
34 #include "servers/forward.h"
35 #include "servers/FtpServer.h"
36 #include "SquidConfig.h"
37 #include "StatCounters.h"
43 CBDATA_NAMESPACED_CLASS_INIT(Ftp
, Server
);
47 static void PrintReply(MemBuf
&mb
, const HttpReply
*reply
, const char *const prefix
= "");
48 static bool SupportedCommand(const SBuf
&name
);
49 static bool CommandHasPathParameter(const SBuf
&cmd
);
52 Ftp::Server::Server(const MasterXaction::Pointer
&xact
):
53 AsyncJob("Ftp::Server"),
55 master(new MasterState
),
66 waitingForOrigin(false),
67 originDataDownloadAbortedOnError(false)
69 flags
.readMore
= false; // we need to announce ourselves first
73 Ftp::Server::~Server()
75 closeDataConnection();
79 Ftp::Server::pipelinePrefetchMax() const
81 return 0; // no support for concurrent FTP requests
85 Ftp::Server::idleTimeout() const
87 return Config
.Timeout
.ftpClientIdle
;
93 ConnStateData::start();
96 char buf
[MAX_IPSTRLEN
];
97 clientConnection
->local
.toUrl(buf
, MAX_IPSTRLEN
);
100 debugs(33, 5, "FTP transparent URL: " << uri
);
103 writeEarlyReply(220, "Service ready");
106 /// schedules another data connection read if needed
108 Ftp::Server::maybeReadUploadData()
113 const size_t availSpace
= sizeof(uploadBuf
) - uploadAvailSize
;
117 debugs(33, 4, dataConn
<< ": reading FTP data...");
119 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
120 reader
= JobCallback(33, 5, Dialer
, this, Ftp::Server::readUploadData
);
121 comm_read(dataConn
, uploadBuf
+ uploadAvailSize
, availSpace
,
125 /// react to the freshly parsed request
127 Ftp::Server::doProcessRequest()
129 // zero pipelinePrefetchMax() ensures that there is only parsed request
130 Must(pipeline
.count() == 1);
131 Http::StreamPointer context
= pipeline
.front();
132 Must(context
!= nullptr);
134 ClientHttpRequest
*const http
= context
->http
;
135 assert(http
!= NULL
);
137 HttpRequest
*const request
= http
->request
;
138 Must(http
->storeEntry() || request
);
139 const bool mayForward
= !http
->storeEntry() && handleRequest(request
);
141 if (http
->storeEntry() != NULL
) {
142 debugs(33, 4, "got an immediate response");
143 clientSetKeepaliveFlag(http
);
145 } else if (mayForward
) {
146 debugs(33, 4, "forwarding request to server side");
147 assert(http
->storeEntry() == NULL
);
148 clientProcessRequest(this, Http1::RequestParserPointer(), context
.getRaw());
150 debugs(33, 4, "will resume processing later");
155 Ftp::Server::processParsedRequest(Http::Stream
*)
157 Must(pipeline
.count() == 1);
159 // Process FTP request asynchronously to make sure FTP
160 // data connection accept callback is fired first.
161 CallJobHere(33, 4, CbcPointer
<Server
>(this),
162 Ftp::Server
, doProcessRequest
);
165 /// imports more upload data from the data connection
167 Ftp::Server::readUploadData(const CommIoCbParams
&io
)
169 debugs(33, 5, io
.conn
<< " size " << io
.size
);
170 Must(reader
!= NULL
);
173 assert(Comm::IsConnOpen(dataConn
));
174 assert(io
.conn
->fd
== dataConn
->fd
);
176 if (io
.flag
== Comm::OK
&& bodyPipe
!= NULL
) {
178 statCounter
.client_http
.kbytes_in
+= io
.size
;
180 char *const current_buf
= uploadBuf
+ uploadAvailSize
;
181 if (io
.buf
!= current_buf
)
182 memmove(current_buf
, io
.buf
, io
.size
);
183 uploadAvailSize
+= io
.size
;
185 } else if (io
.size
== 0) {
186 debugs(33, 5, io
.conn
<< " closed");
187 closeDataConnection();
188 if (uploadAvailSize
<= 0)
189 finishDechunkingRequest(true);
191 } else { // not Comm::Flags::OK or unexpected read
192 debugs(33, 5, io
.conn
<< " closed");
193 closeDataConnection();
194 finishDechunkingRequest(false);
199 /// shovel upload data from the internal buffer to the body pipe if possible
201 Ftp::Server::shovelUploadData()
203 assert(bodyPipe
!= NULL
);
205 debugs(33, 5, "handling FTP request data for " << clientConnection
);
206 const size_t putSize
= bodyPipe
->putMoreData(uploadBuf
,
209 uploadAvailSize
-= putSize
;
210 if (uploadAvailSize
> 0)
211 memmove(uploadBuf
, uploadBuf
+ putSize
, uploadAvailSize
);
214 if (Comm::IsConnOpen(dataConn
))
215 maybeReadUploadData();
216 else if (uploadAvailSize
<= 0)
217 finishDechunkingRequest(true);
221 Ftp::Server::noteMoreBodySpaceAvailable(BodyPipe::Pointer
)
223 if (!isOpen()) // if we are closing, nothing to do
230 Ftp::Server::noteBodyConsumerAborted(BodyPipe::Pointer ptr
)
232 if (!isOpen()) // if we are closing, nothing to do
235 ConnStateData::noteBodyConsumerAborted(ptr
);
236 closeDataConnection();
239 /// accept a new FTP control connection and hand it to a dedicated Server
241 Ftp::Server::AcceptCtrlConnection(const CommAcceptCbParams
¶ms
)
243 MasterXaction::Pointer xact
= params
.xaction
;
244 AnyP::PortCfgPointer s
= xact
->squidPort
;
246 // NP: it is possible the port was reconfigured when the call or accept() was queued.
248 if (params
.flag
!= Comm::OK
) {
249 // Its possible the call was still queued when the client disconnected
250 debugs(33, 2, s
->listenConn
<< ": FTP accept failure: " << xstrerr(params
.xerrno
));
254 debugs(33, 4, params
.conn
<< ": accepted");
255 fd_note(params
.conn
->fd
, "client ftp connect");
257 if (s
->tcp_keepalive
.enabled
)
258 commSetTcpKeepalive(params
.conn
->fd
, s
->tcp_keepalive
.idle
, s
->tcp_keepalive
.interval
, s
->tcp_keepalive
.timeout
);
260 ++incoming_sockets_accepted
;
262 AsyncJob::Start(new Server(xact
));
266 Ftp::StartListening()
268 for (AnyP::PortCfgPointer s
= FtpPortList
; s
!= NULL
; s
= s
->next
) {
269 if (MAXTCPLISTENPORTS
== NHttpSockets
) {
270 debugs(1, DBG_IMPORTANT
, "Ignoring ftp_port lines exceeding the" <<
271 " limit of " << MAXTCPLISTENPORTS
<< " ports.");
275 // direct new connections accepted by listenConn to Accept()
276 typedef CommCbFunPtrCallT
<CommAcceptCbPtrFun
> AcceptCall
;
277 RefCount
<AcceptCall
> subCall
= commCbCall(5, 5, "Ftp::Server::AcceptCtrlConnection",
278 CommAcceptCbPtrFun(Ftp::Server::AcceptCtrlConnection
,
279 CommAcceptCbParams(NULL
)));
280 clientStartListeningOn(s
, subCall
, Ipc::fdnFtpSocket
);
287 for (AnyP::PortCfgPointer s
= FtpPortList
; s
!= NULL
; s
= s
->next
) {
288 if (s
->listenConn
!= NULL
) {
289 debugs(1, DBG_IMPORTANT
, "Closing FTP port " << s
->listenConn
->local
);
290 s
->listenConn
->close();
291 s
->listenConn
= NULL
;
297 Ftp::Server::notePeerConnection(Comm::ConnectionPointer conn
)
300 Http::StreamPointer context
= pipeline
.front();
301 Must(context
!= nullptr);
302 ClientHttpRequest
*const http
= context
->http
;
304 HttpRequest
*const request
= http
->request
;
305 Must(request
!= NULL
);
307 // this is not an idle connection, so we do not want I/O monitoring
308 const bool monitor
= false;
310 // make FTP peer connection exclusive to our request
311 pinConnection(conn
, request
, conn
->getPeer(), false, monitor
);
315 Ftp::Server::clientPinnedConnectionClosed(const CommCloseCbParams
&io
)
317 ConnStateData::clientPinnedConnectionClosed(io
);
319 // TODO: Keep the control connection open after fixing the reset
321 if (Comm::IsConnOpen(clientConnection
))
322 clientConnection
->close();
324 // TODO: If the server control connection is gone, reset state to login
325 // again. Reseting login alone is not enough: FtpRelay::sendCommand() will
326 // not re-login because FtpRelay::serverState() is not going to be
327 // fssConnected. Calling resetLogin() alone is also harmful because
328 // it does not reset correctly the client-to-squid control connection (eg
329 // respond if required with an error code, in all cases)
330 // resetLogin("control connection closure");
333 /// clear client and server login-related state after the old login is gone
335 Ftp::Server::resetLogin(const char *reason
)
337 debugs(33, 5, "will need to re-login due to " << reason
);
338 master
->clientReadGreeting
= false;
339 changeState(fssBegin
, reason
);
342 /// computes uri member from host and, if tracked, working dir with file name
344 Ftp::Server::calcUri(const SBuf
*file
)
348 if (port
->ftp_track_dirs
&& master
->workingDir
.length()) {
349 if (master
->workingDir
[0] != '/')
351 uri
.append(master
->workingDir
);
354 if (uri
[uri
.length() - 1] != '/')
357 if (port
->ftp_track_dirs
&& file
) {
358 static const CharacterSet
Slash("/", "/");
359 Parser::Tokenizer
tok(*file
);
361 uri
.append(tok
.remaining());
365 /// Starts waiting for a data connection. Returns listening port.
366 /// On errors, responds with an error and returns zero.
368 Ftp::Server::listenForDataConnection()
370 closeDataConnection();
372 Comm::ConnectionPointer conn
= new Comm::Connection
;
373 conn
->flags
= COMM_NONBLOCKING
;
374 conn
->local
= transparent() ? port
->s
: clientConnection
->local
;
376 const char *const note
= uri
.c_str();
377 comm_open_listener(SOCK_STREAM
, IPPROTO_TCP
, conn
, note
);
378 if (!Comm::IsConnOpen(conn
)) {
379 debugs(5, DBG_CRITICAL
, "comm_open_listener failed for FTP data: " <<
380 conn
->local
<< " error: " << errno
);
381 writeCustomReply(451, "Internal error");
385 typedef CommCbMemFunT
<Server
, CommAcceptCbParams
> AcceptDialer
;
386 typedef AsyncCallT
<AcceptDialer
> AcceptCall
;
387 RefCount
<AcceptCall
> call
= static_cast<AcceptCall
*>(JobCallback(5, 5, AcceptDialer
, this, Ftp::Server::acceptDataConnection
));
388 Subscription::Pointer sub
= new CallSubscription
<AcceptCall
>(call
);
389 listener
= call
.getRaw();
390 dataListenConn
= conn
;
391 AsyncJob::Start(new Comm::TcpAcceptor(conn
, note
, sub
));
393 const unsigned int listeningPort
= comm_local_port(conn
->fd
);
394 conn
->local
.port(listeningPort
);
395 return listeningPort
;
399 Ftp::Server::acceptDataConnection(const CommAcceptCbParams
¶ms
)
401 if (params
.flag
!= Comm::OK
) {
402 // Its possible the call was still queued when the client disconnected
403 debugs(33, 2, dataListenConn
<< ": accept "
404 "failure: " << xstrerr(params
.xerrno
));
408 debugs(33, 4, "accepted " << params
.conn
);
409 fd_note(params
.conn
->fd
, "passive client ftp data");
410 ++incoming_sockets_accepted
;
412 if (!clientConnection
) {
413 debugs(33, 5, "late data connection?");
414 closeDataConnection(); // in case we are still listening
415 params
.conn
->close();
416 } else if (params
.conn
->remote
!= clientConnection
->remote
) {
417 debugs(33, 2, "rogue data conn? ctrl: " << clientConnection
->remote
);
418 params
.conn
->close();
419 // Some FTP servers close control connection here, but it may make
420 // things worse from DoS p.o.v. and no better from data stealing p.o.v.
422 closeDataConnection();
423 dataConn
= params
.conn
;
425 debugs(33, 7, "ready for data");
426 if (onDataAcceptCall
!= NULL
) {
427 AsyncCall::Pointer call
= onDataAcceptCall
;
428 onDataAcceptCall
= NULL
;
429 // If we got an upload request, start reading data from the client.
430 if (master
->serverState
== fssHandleUploadRequest
)
431 maybeReadUploadData();
433 Must(master
->serverState
== fssHandleDataRequest
);
436 mb
.appendf("150 Data connection opened.\r\n");
437 Comm::Write(clientConnection
, &mb
, call
);
443 Ftp::Server::closeDataConnection()
445 if (listener
!= NULL
) {
446 listener
->cancel("no longer needed");
450 if (Comm::IsConnOpen(dataListenConn
)) {
451 debugs(33, 5, "FTP closing client data listen socket: " <<
453 dataListenConn
->close();
455 dataListenConn
= NULL
;
457 if (reader
!= NULL
) {
458 // Comm::ReadCancel can deal with negative FDs
459 Comm::ReadCancel(dataConn
->fd
, reader
);
463 if (Comm::IsConnOpen(dataConn
)) {
464 debugs(33, 5, "FTP closing client data connection: " <<
471 /// Writes FTP [error] response before we fully parsed the FTP request and
472 /// created the corresponding HTTP request wrapper for that FTP request.
474 Ftp::Server::writeEarlyReply(const int code
, const char *msg
)
476 debugs(33, 7, code
<< ' ' << msg
);
477 assert(99 < code
&& code
< 1000);
481 mb
.appendf("%i %s\r\n", code
, msg
);
483 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
484 AsyncCall::Pointer call
= JobCallback(33, 5, Dialer
, this, Ftp::Server::wroteEarlyReply
);
485 Comm::Write(clientConnection
, &mb
, call
);
487 flags
.readMore
= false;
489 // TODO: Create master transaction. Log it in wroteEarlyReply().
493 Ftp::Server::writeReply(MemBuf
&mb
)
495 debugs(9, 2, "FTP Client " << clientConnection
);
496 debugs(9, 2, "FTP Client REPLY:\n---------\n" << mb
.buf
<<
499 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
500 AsyncCall::Pointer call
= JobCallback(33, 5, Dialer
, this, Ftp::Server::wroteReply
);
501 Comm::Write(clientConnection
, &mb
, call
);
505 Ftp::Server::writeCustomReply(const int code
, const char *msg
, const HttpReply
*reply
)
507 debugs(33, 7, code
<< ' ' << msg
);
508 assert(99 < code
&& code
< 1000);
510 const bool sendDetails
= reply
!= NULL
&&
511 reply
->header
.has(Http::HdrType::FTP_STATUS
) && reply
->header
.has(Http::HdrType::FTP_REASON
);
516 mb
.appendf("%i-%s\r\n", code
, msg
);
517 mb
.appendf(" Server reply:\r\n");
518 Ftp::PrintReply(mb
, reply
, " ");
519 mb
.appendf("%i \r\n", code
);
521 mb
.appendf("%i %s\r\n", code
, msg
);
527 Ftp::Server::changeState(const ServerState newState
, const char *reason
)
529 if (master
->serverState
== newState
) {
530 debugs(33, 3, "client state unchanged at " << master
->serverState
<<
531 " because " << reason
);
532 master
->serverState
= newState
;
534 debugs(33, 3, "client state was " << master
->serverState
<<
535 ", now " << newState
<< " because " << reason
);
536 master
->serverState
= newState
;
540 /// whether the given FTP command has a pathname parameter
542 Ftp::CommandHasPathParameter(const SBuf
&cmd
)
544 static std::set
<SBuf
> PathedCommands
;
545 if (!PathedCommands
.size()) {
546 PathedCommands
.insert(cmdMlst());
547 PathedCommands
.insert(cmdMlsd());
548 PathedCommands
.insert(cmdStat());
549 PathedCommands
.insert(cmdNlst());
550 PathedCommands
.insert(cmdList());
551 PathedCommands
.insert(cmdMkd());
552 PathedCommands
.insert(cmdRmd());
553 PathedCommands
.insert(cmdDele());
554 PathedCommands
.insert(cmdRnto());
555 PathedCommands
.insert(cmdRnfr());
556 PathedCommands
.insert(cmdAppe());
557 PathedCommands
.insert(cmdStor());
558 PathedCommands
.insert(cmdRetr());
559 PathedCommands
.insert(cmdSmnt());
560 PathedCommands
.insert(cmdCwd());
563 return PathedCommands
.find(cmd
) != PathedCommands
.end();
566 /// creates a context filled with an error message for a given early error
568 Ftp::Server::earlyError(const EarlyErrorKind eek
)
570 /* Default values, to be updated by the switch statement below */
572 const char *reason
= "Internal error";
573 const char *errUri
= "error:ftp-internal-early-error";
576 case EarlyErrorKind::HugeRequest
:
578 reason
= "Huge request";
579 errUri
= "error:ftp-huge-request";
582 case EarlyErrorKind::MissingLogin
:
584 reason
= "Must login first";
585 errUri
= "error:ftp-must-login-first";
588 case EarlyErrorKind::MissingUsername
:
590 reason
= "Missing username";
591 errUri
= "error:ftp-missing-username";
594 case EarlyErrorKind::MissingHost
:
596 reason
= "Missing host";
597 errUri
= "error:ftp-missing-host";
600 case EarlyErrorKind::UnsupportedCommand
:
602 reason
= "Unknown or unsupported command";
603 errUri
= "error:ftp-unsupported-command";
606 case EarlyErrorKind::InvalidUri
:
608 reason
= "Invalid URI";
609 errUri
= "error:ftp-invalid-uri";
612 case EarlyErrorKind::MalformedCommand
:
614 reason
= "Malformed command";
615 errUri
= "error:ftp-malformed-command";
618 // no default so that a compiler can check that we have covered all cases
621 Http::Stream
*context
= abortRequestParsing(errUri
);
622 clientStreamNode
*node
= context
->getClientReplyContext();
624 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
627 // We cannot relay FTP scode/reason via HTTP-specific ErrorState.
628 // TODO: When/if ErrorState can handle native FTP errors, use it instead.
629 HttpReply
*reply
= Ftp::HttpReplyWrapper(scode
, reason
, Http::scBadRequest
, -1);
630 repContext
->setReplyToReply(reply
);
634 /// Parses a single FTP request on the control connection.
635 /// Returns a new Http::Stream on valid requests and all errors.
636 /// Returns NULL on incomplete requests that may still succeed given more data.
638 Ftp::Server::parseOneRequest()
640 flags
.readMore
= false; // common for all but one case below
642 // OWS <command> [ RWS <parameter> ] OWS LF
644 // InlineSpaceChars are isspace(3) or RFC 959 Section 3.1.1.5.2, except
645 // for the LF character that we must exclude here (but see FullWhiteSpace).
646 static const char * const InlineSpaceChars
= " \f\r\t\v";
647 static const CharacterSet InlineSpace
= CharacterSet("Ftp::Inline", InlineSpaceChars
);
648 static const CharacterSet FullWhiteSpace
= (InlineSpace
+ CharacterSet::LF
).rename("Ftp::FWS");
649 static const CharacterSet CommandChars
= FullWhiteSpace
.complement("Ftp::Command");
650 static const CharacterSet TailChars
= CharacterSet::LF
.complement("Ftp::Tail");
652 // This set is used to ignore empty commands without allowing an attacker
653 // to keep us endlessly busy by feeding us whitespace or empty commands.
654 static const CharacterSet
&LeadingSpace
= FullWhiteSpace
;
659 Parser::Tokenizer
tok(inBuf
);
661 (void)tok
.skipAll(LeadingSpace
); // leading OWS and empty commands
662 const bool parsed
= tok
.prefix(cmd
, CommandChars
); // required command
664 // note that the condition below will eat either RWS or trailing OWS
665 if (parsed
&& tok
.skipAll(InlineSpace
) && tok
.prefix(params
, TailChars
)) {
666 // now params may include trailing OWS
667 // TODO: Support right-trimming using CharacterSet in Tokenizer instead
668 static const SBuf
bufWhiteSpace(InlineSpaceChars
);
669 params
.trim(bufWhiteSpace
, false, true);
672 // Why limit command line and parameters size? Did not we just parse them?
673 // XXX: Our good old String cannot handle very long strings.
674 const SBuf::size_type tokenMax
= min(
675 static_cast<SBuf::size_type
>(32*1024), // conservative
676 static_cast<SBuf::size_type
>(Config
.maxRequestHeaderSize
));
677 if (cmd
.length() > tokenMax
|| params
.length() > tokenMax
) {
678 changeState(fssError
, "huge req token");
679 quitAfterError(NULL
);
680 return earlyError(EarlyErrorKind::HugeRequest
);
683 // technically, we may skip multiple NLs below, but that is OK
684 if (!parsed
|| !tok
.skipAll(CharacterSet::LF
)) { // did not find terminating LF yet
685 // we need more data, but can we buffer more?
686 if (inBuf
.length() >= Config
.maxRequestHeaderSize
) {
687 changeState(fssError
, "huge req");
688 quitAfterError(NULL
);
689 return earlyError(EarlyErrorKind::HugeRequest
);
691 flags
.readMore
= true;
692 debugs(33, 5, "Waiting for more, up to " <<
693 (Config
.maxRequestHeaderSize
- inBuf
.length()));
698 Must(parsed
&& cmd
.length());
699 consumeInput(tok
.parsedSize()); // TODO: Would delaying optimize copying?
701 debugs(33, 2, ">>ftp " << cmd
<< (params
.isEmpty() ? "" : " ") << params
);
703 cmd
.toUpper(); // this should speed up and simplify future comparisons
705 // interception cases do not need USER to calculate the uri
706 if (!transparent()) {
707 if (!master
->clientReadGreeting
) {
708 // the first command must be USER
709 if (!pinning
.pinned
&& cmd
!= cmdUser())
710 return earlyError(EarlyErrorKind::MissingLogin
);
713 // process USER request now because it sets FTP peer host name
714 if (cmd
== cmdUser()) {
715 if (Http::Stream
*errCtx
= handleUserRequest(cmd
, params
))
720 if (!Ftp::SupportedCommand(cmd
))
721 return earlyError(EarlyErrorKind::UnsupportedCommand
);
723 const HttpRequestMethod method
=
724 cmd
== cmdAppe() || cmd
== cmdStor() || cmd
== cmdStou() ?
725 Http::METHOD_PUT
: Http::METHOD_GET
;
727 const SBuf
*path
= (params
.length() && CommandHasPathParameter(cmd
)) ?
730 char *newUri
= xstrdup(uri
.c_str());
731 HttpRequest
*const request
= HttpRequest::CreateFromUrl(newUri
, method
);
733 debugs(33, 5, "Invalid FTP URL: " << uri
);
736 return earlyError(EarlyErrorKind::InvalidUri
);
739 request
->flags
.ftpNative
= true;
740 request
->http_ver
= Http::ProtocolVersion(Ftp::ProtocolVersion().major
, Ftp::ProtocolVersion().minor
);
742 // Our fake Request-URIs are not distinctive enough for caching to work
743 request
->flags
.cachable
= false; // XXX: reset later by maybeCacheable()
744 request
->flags
.noCache
= true;
746 request
->header
.putStr(Http::HdrType::FTP_COMMAND
, cmd
.c_str());
747 request
->header
.putStr(Http::HdrType::FTP_ARGUMENTS
, params
.c_str()); // may be ""
748 if (method
== Http::METHOD_PUT
) {
749 request
->header
.putStr(Http::HdrType::EXPECT
, "100-continue");
750 request
->header
.putStr(Http::HdrType::TRANSFER_ENCODING
, "chunked");
753 ClientHttpRequest
*const http
= new ClientHttpRequest(this);
754 http
->request
= request
;
755 HTTPMSGLOCK(http
->request
);
756 http
->req_sz
= tok
.parsedSize();
759 Http::Stream
*const result
=
760 new Http::Stream(clientConnection
, http
);
762 StoreIOBuffer tempBuffer
;
763 tempBuffer
.data
= result
->reqbuf
;
764 tempBuffer
.length
= HTTP_REQBUF_SZ
;
766 ClientStreamData newServer
= new clientReplyContext(http
);
767 ClientStreamData newClient
= result
;
768 clientStreamInit(&http
->client_stream
, clientGetMoreData
, clientReplyDetach
,
769 clientReplyStatus
, newServer
, clientSocketRecipient
,
770 clientSocketDetach
, newClient
, tempBuffer
);
772 result
->flags
.parsed_ok
= 1;
777 Ftp::Server::handleReply(HttpReply
*reply
, StoreIOBuffer data
)
779 // the caller guarantees that we are dealing with the current context only
780 Http::StreamPointer context
= pipeline
.front();
781 assert(context
!= nullptr);
783 if (context
->http
&& context
->http
->al
!= NULL
&&
784 !context
->http
->al
->reply
&& reply
) {
785 context
->http
->al
->reply
= reply
;
786 HTTPMSGLOCK(context
->http
->al
->reply
);
789 static ReplyHandler handlers
[] = {
791 NULL
, // fssConnected
792 &Ftp::Server::handleFeatReply
, // fssHandleFeat
793 &Ftp::Server::handlePasvReply
, // fssHandlePasv
794 &Ftp::Server::handlePortReply
, // fssHandlePort
795 &Ftp::Server::handleDataReply
, // fssHandleDataRequest
796 &Ftp::Server::handleUploadReply
, // fssHandleUploadRequest
797 &Ftp::Server::handleEprtReply
,// fssHandleEprt
798 &Ftp::Server::handleEpsvReply
,// fssHandleEpsv
799 NULL
, // fssHandleCwd
800 NULL
, // fssHandlePass
801 NULL
, // fssHandleCdup
802 &Ftp::Server::handleErrorReply
// fssError
805 const Server
&server
= dynamic_cast<const Ftp::Server
&>(*context
->getConn());
806 if (const ReplyHandler handler
= handlers
[server
.master
->serverState
])
807 (this->*handler
)(reply
, data
);
809 writeForwardedReply(reply
);
810 } catch (const std::exception
&e
) {
812 throw TexcHere(e
.what());
817 Ftp::Server::handleFeatReply(const HttpReply
*reply
, StoreIOBuffer
)
819 if (pipeline
.front()->http
->request
->errType
!= ERR_NONE
) {
820 writeCustomReply(502, "Server does not support FEAT", reply
);
825 HttpReply::Pointer featReply
= Ftp::HttpReplyWrapper(211, "End", Http::scNoContent
, 0);
826 HttpHeader
const &serverReplyHeader
= reply
->header
;
828 HttpHeaderPos pos
= HttpHeaderInitPos
;
829 bool hasEPRT
= false;
830 bool hasEPSV
= false;
831 int prependSpaces
= 1;
833 featReply
->header
.putStr(Http::HdrType::FTP_PRE
, "\"211-Features:\"");
834 const int scode
= serverReplyHeader
.getInt(Http::HdrType::FTP_STATUS
);
836 while (const HttpHeaderEntry
*e
= serverReplyHeader
.getEntry(&pos
)) {
837 if (e
->id
== Http::HdrType::FTP_PRE
) {
838 // assume RFC 2389 FEAT response format, quoted by Squid:
839 // <"> SP NAME [SP PARAMS] <">
840 // but accommodate MS servers sending four SPs before NAME
842 // command name ends with (SP parameter) or quote
843 static const CharacterSet
AfterFeatNameChars("AfterFeatName", " \"");
844 static const CharacterSet FeatNameChars
= AfterFeatNameChars
.complement("FeatName");
846 Parser::Tokenizer
tok(SBuf(e
->value
.termedBuf()));
847 if (!tok
.skip('"') || !tok
.skip(' '))
850 // optional spaces; remember their number to accomodate MS servers
851 prependSpaces
= 1 + tok
.skipAll(CharacterSet::SP
);
854 if (!tok
.prefix(cmd
, FeatNameChars
))
858 if (Ftp::SupportedCommand(cmd
)) {
859 featReply
->header
.addEntry(e
->clone());
862 if (cmd
== cmdEprt())
864 else if (cmd
== cmdEpsv())
868 } // else we got a FEAT error and will only report Squid-supported features
872 snprintf(buf
, sizeof(buf
), "\"%*s\"", prependSpaces
+ 4, "EPRT");
873 featReply
->header
.putStr(Http::HdrType::FTP_PRE
, buf
);
876 snprintf(buf
, sizeof(buf
), "\"%*s\"", prependSpaces
+ 4, "EPSV");
877 featReply
->header
.putStr(Http::HdrType::FTP_PRE
, buf
);
880 featReply
->header
.refreshMask();
882 writeForwardedReply(featReply
.getRaw());
886 Ftp::Server::handlePasvReply(const HttpReply
*reply
, StoreIOBuffer
)
888 const Http::StreamPointer
context(pipeline
.front());
889 assert(context
!= nullptr);
891 if (context
->http
->request
->errType
!= ERR_NONE
) {
892 writeCustomReply(502, "Server does not support PASV", reply
);
896 const unsigned short localPort
= listenForDataConnection();
900 char addr
[MAX_IPSTRLEN
];
901 // remote server in interception setups and local address otherwise
902 const Ip::Address
&server
= transparent() ?
903 clientConnection
->local
: dataListenConn
->local
;
904 server
.toStr(addr
, MAX_IPSTRLEN
, AF_INET
);
905 addr
[MAX_IPSTRLEN
- 1] = '\0';
906 for (char *c
= addr
; *c
!= '\0'; ++c
) {
911 // In interception setups, we combine remote server address with a
912 // local port number and hope that traffic will be redirected to us.
913 // Do not use "227 =a,b,c,d,p1,p2" format or omit parens: some nf_ct_ftp
914 // versions block responses that use those alternative syntax rules!
917 mb
.appendf("227 Entering Passive Mode (%s,%i,%i).\r\n",
919 static_cast<int>(localPort
/ 256),
920 static_cast<int>(localPort
% 256));
921 debugs(9, 3, Raw("writing", mb
.buf
, mb
.size
));
926 Ftp::Server::handlePortReply(const HttpReply
*reply
, StoreIOBuffer
)
928 if (pipeline
.front()->http
->request
->errType
!= ERR_NONE
) {
929 writeCustomReply(502, "Server does not support PASV (converted from PORT)", reply
);
933 writeCustomReply(200, "PORT successfully converted to PASV.");
939 Ftp::Server::handleErrorReply(const HttpReply
*reply
, StoreIOBuffer
)
941 if (!pinning
.pinned
) // we failed to connect to server
943 // 421: we will close due to fssError
944 writeErrorReply(reply
, 421);
948 Ftp::Server::handleDataReply(const HttpReply
*reply
, StoreIOBuffer data
)
950 if (reply
!= NULL
&& reply
->sline
.status() != Http::scOkay
) {
951 writeForwardedReply(reply
);
952 if (Comm::IsConnOpen(dataConn
)) {
953 debugs(33, 3, "closing " << dataConn
<< " on KO reply");
954 closeDataConnection();
960 // We got STREAM_COMPLETE (or error) and closed the client data conn.
961 debugs(33, 3, "ignoring FTP srv data response after clt data closure");
965 if (!checkDataConnPost()) {
966 writeCustomReply(425, "Data connection is not established.");
967 closeDataConnection();
971 debugs(33, 7, data
.length
);
973 if (data
.length
<= 0) {
974 replyDataWritingCheckpoint(); // skip the actual write call
979 mb
.init(data
.length
+ 1, data
.length
+ 1);
980 mb
.append(data
.data
, data
.length
);
982 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
983 AsyncCall::Pointer call
= JobCallback(33, 5, Dialer
, this, Ftp::Server::wroteReplyData
);
984 Comm::Write(dataConn
, &mb
, call
);
986 pipeline
.front()->noteSentBodyBytes(data
.length
);
989 /// called when we are done writing a chunk of the response data
991 Ftp::Server::wroteReplyData(const CommIoCbParams
&io
)
993 if (io
.flag
== Comm::ERR_CLOSING
)
996 if (io
.flag
!= Comm::OK
) {
997 debugs(33, 3, "FTP reply data writing failed: " << xstrerr(io
.xerrno
));
998 userDataCompletionCheckpoint(426);
1002 assert(pipeline
.front()->http
);
1003 pipeline
.front()->http
->out
.size
+= io
.size
;
1004 replyDataWritingCheckpoint();
1007 /// ClientStream checks after (actual or skipped) reply data writing
1009 Ftp::Server::replyDataWritingCheckpoint()
1011 switch (pipeline
.front()->socketState()) {
1013 debugs(33, 3, "Keep going");
1014 pipeline
.front()->pullData();
1016 case STREAM_COMPLETE
:
1017 debugs(33, 3, "FTP reply data transfer successfully complete");
1018 userDataCompletionCheckpoint(226);
1020 case STREAM_UNPLANNED_COMPLETE
:
1021 debugs(33, 3, "FTP reply data transfer failed: STREAM_UNPLANNED_COMPLETE");
1022 userDataCompletionCheckpoint(451);
1025 userDataCompletionCheckpoint(451);
1026 debugs(33, 3, "FTP reply data transfer failed: STREAM_FAILED");
1029 fatal("unreachable code");
1034 Ftp::Server::handleUploadReply(const HttpReply
*reply
, StoreIOBuffer
)
1036 writeForwardedReply(reply
);
1037 // note that the client data connection may already be closed by now
1041 Ftp::Server::writeForwardedReply(const HttpReply
*reply
)
1045 if (waitingForOrigin
) {
1046 Must(delayedReply
== NULL
);
1047 delayedReply
= reply
;
1051 const HttpHeader
&header
= reply
->header
;
1052 // adaptation and forwarding errors lack Http::HdrType::FTP_STATUS
1053 if (!header
.has(Http::HdrType::FTP_STATUS
)) {
1054 writeForwardedForeign(reply
); // will get to Ftp::Server::wroteReply
1058 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
1059 AsyncCall::Pointer call
= JobCallback(33, 5, Dialer
, this, Ftp::Server::wroteReply
);
1060 writeForwardedReplyAndCall(reply
, call
);
1064 Ftp::Server::handleEprtReply(const HttpReply
*reply
, StoreIOBuffer
)
1066 if (pipeline
.front()->http
->request
->errType
!= ERR_NONE
) {
1067 writeCustomReply(502, "Server does not support PASV (converted from EPRT)", reply
);
1071 writeCustomReply(200, "EPRT successfully converted to PASV.");
1073 // and wait for RETR
1077 Ftp::Server::handleEpsvReply(const HttpReply
*reply
, StoreIOBuffer
)
1079 if (pipeline
.front()->http
->request
->errType
!= ERR_NONE
) {
1080 writeCustomReply(502, "Cannot connect to server", reply
);
1084 const unsigned short localPort
= listenForDataConnection();
1088 // In interception setups, we use a local port number and hope that data
1089 // traffic will be redirected to us.
1092 mb
.appendf("229 Entering Extended Passive Mode (|||%u|)\r\n", localPort
);
1094 debugs(9, 3, Raw("writing", mb
.buf
, mb
.size
));
1098 /// writes FTP error response with given status and reply-derived error details
1100 Ftp::Server::writeErrorReply(const HttpReply
*reply
, const int scode
)
1102 const HttpRequest
*request
= pipeline
.front()->http
->request
;
1108 if (request
->errType
!= ERR_NONE
)
1109 mb
.appendf("%i-%s\r\n", scode
, errorPageName(request
->errType
));
1111 if (request
->errDetail
> 0) {
1112 // XXX: > 0 may not always mean that this is an errno
1113 mb
.appendf("%i-Error: (%d) %s\r\n", scode
,
1115 strerror(request
->errDetail
));
1119 // XXX: Remove hard coded names. Use an error page template instead.
1120 const Adaptation::History::Pointer ah
= request
->adaptHistory();
1121 if (ah
!= NULL
) { // XXX: add adapt::<all_h but use lastMeta here
1122 const String info
= ah
->allMeta
.getByName("X-Response-Info");
1123 const String desc
= ah
->allMeta
.getByName("X-Response-Desc");
1125 mb
.appendf("%i-Information: %s\r\n", scode
, info
.termedBuf());
1127 mb
.appendf("%i-Description: %s\r\n", scode
, desc
.termedBuf());
1132 const char *reason
= reply
->header
.has(Http::HdrType::FTP_REASON
) ?
1133 reply
->header
.getStr(Http::HdrType::FTP_REASON
):
1134 reply
->sline
.reason();
1136 mb
.appendf("%i %s\r\n", scode
, reason
); // error terminating line
1138 // TODO: errorpage.cc should detect FTP client and use
1139 // configurable FTP-friendly error templates which we should
1140 // write to the client "as is" instead of hiding most of the info
1145 /// writes FTP response based on HTTP reply that is not an FTP-response wrapper
1146 /// for example, internally-generated Squid "errorpages" end up here (for now)
1148 Ftp::Server::writeForwardedForeign(const HttpReply
*reply
)
1150 changeState(fssConnected
, "foreign reply");
1151 closeDataConnection();
1152 // 451: We intend to keep the control connection open.
1153 writeErrorReply(reply
, 451);
1157 Ftp::Server::writeControlMsgAndCall(HttpReply
*reply
, AsyncCall::Pointer
&call
)
1159 // the caller guarantees that we are dealing with the current context only
1160 // the caller should also make sure reply->header.has(Http::HdrType::FTP_STATUS)
1161 writeForwardedReplyAndCall(reply
, call
);
1165 Ftp::Server::writeForwardedReplyAndCall(const HttpReply
*reply
, AsyncCall::Pointer
&call
)
1167 assert(reply
!= NULL
);
1168 const HttpHeader
&header
= reply
->header
;
1170 // without status, the caller must use the writeForwardedForeign() path
1171 Must(header
.has(Http::HdrType::FTP_STATUS
));
1172 Must(header
.has(Http::HdrType::FTP_REASON
));
1173 const int scode
= header
.getInt(Http::HdrType::FTP_STATUS
);
1174 debugs(33, 7, "scode: " << scode
);
1176 // Status 125 or 150 implies upload or data request, but we still check
1177 // the state in case the server is buggy.
1178 if ((scode
== 125 || scode
== 150) &&
1179 (master
->serverState
== fssHandleUploadRequest
||
1180 master
->serverState
== fssHandleDataRequest
)) {
1181 if (checkDataConnPost()) {
1182 // If the data connection is ready, start reading data (here)
1183 // and forward the response to client (further below).
1184 debugs(33, 7, "data connection established, start data transfer");
1185 if (master
->serverState
== fssHandleUploadRequest
)
1186 maybeReadUploadData();
1188 // If we are waiting to accept the data connection, keep waiting.
1189 if (Comm::IsConnOpen(dataListenConn
)) {
1190 debugs(33, 7, "wait for the client to establish a data connection");
1191 onDataAcceptCall
= call
;
1192 // TODO: Add connect timeout for passive connections listener?
1193 // TODO: Remember server response so that we can forward it?
1195 // Either the connection was establised and closed after the
1196 // data was transferred OR we failed to establish an active
1197 // data connection and already sent the error to the client.
1198 // In either case, there is nothing more to do.
1199 debugs(33, 7, "done with data OR active connection failed");
1207 Ftp::PrintReply(mb
, reply
);
1209 debugs(9, 2, "FTP Client " << clientConnection
);
1210 debugs(9, 2, "FTP Client REPLY:\n---------\n" << mb
.buf
<<
1213 Comm::Write(clientConnection
, &mb
, call
);
1217 Ftp::PrintReply(MemBuf
&mb
, const HttpReply
*reply
, const char *const)
1219 const HttpHeader
&header
= reply
->header
;
1221 HttpHeaderPos pos
= HttpHeaderInitPos
;
1222 while (const HttpHeaderEntry
*e
= header
.getEntry(&pos
)) {
1223 if (e
->id
== Http::HdrType::FTP_PRE
) {
1225 if (httpHeaderParseQuotedString(e
->value
.rawBuf(), e
->value
.size(), &raw
))
1226 mb
.appendf("%s\r\n", raw
.termedBuf());
1230 if (header
.has(Http::HdrType::FTP_STATUS
)) {
1231 const char *reason
= header
.getStr(Http::HdrType::FTP_REASON
);
1232 mb
.appendf("%i %s\r\n", header
.getInt(Http::HdrType::FTP_STATUS
),
1233 (reason
? reason
: 0));
1238 Ftp::Server::wroteEarlyReply(const CommIoCbParams
&io
)
1240 if (io
.flag
== Comm::ERR_CLOSING
)
1243 if (io
.flag
!= Comm::OK
) {
1244 debugs(33, 3, "FTP reply writing failed: " << xstrerr(io
.xerrno
));
1249 Http::StreamPointer context
= pipeline
.front();
1250 if (context
!= nullptr && context
->http
) {
1251 context
->http
->out
.size
+= io
.size
;
1252 context
->http
->out
.headers_sz
+= io
.size
;
1255 flags
.readMore
= true;
1260 Ftp::Server::wroteReply(const CommIoCbParams
&io
)
1262 if (io
.flag
== Comm::ERR_CLOSING
)
1265 if (io
.flag
!= Comm::OK
) {
1266 debugs(33, 3, "FTP reply writing failed: " << xstrerr(io
.xerrno
));
1271 Http::StreamPointer context
= pipeline
.front();
1272 assert(context
->http
);
1273 context
->http
->out
.size
+= io
.size
;
1274 context
->http
->out
.headers_sz
+= io
.size
;
1276 if (master
->serverState
== fssError
) {
1277 debugs(33, 5, "closing on FTP server error");
1282 const clientStream_status_t socketState
= context
->socketState();
1283 debugs(33, 5, "FTP client stream state " << socketState
);
1284 switch (socketState
) {
1285 case STREAM_UNPLANNED_COMPLETE
:
1291 case STREAM_COMPLETE
:
1292 flags
.readMore
= true;
1293 changeState(fssConnected
, "Ftp::Server::wroteReply");
1295 finishDechunkingRequest(false);
1296 context
->finished();
1303 Ftp::Server::handleRequest(HttpRequest
*request
)
1305 debugs(33, 9, request
);
1308 HttpHeader
&header
= request
->header
;
1309 Must(header
.has(Http::HdrType::FTP_COMMAND
));
1310 String
&cmd
= header
.findEntry(Http::HdrType::FTP_COMMAND
)->value
;
1311 Must(header
.has(Http::HdrType::FTP_ARGUMENTS
));
1312 String
¶ms
= header
.findEntry(Http::HdrType::FTP_ARGUMENTS
)->value
;
1314 if (Debug::Enabled(9, 2)) {
1319 debugs(9, 2, "FTP Client " << clientConnection
);
1320 debugs(9, 2, "FTP Client REQUEST:\n---------\n" << mb
.buf
<<
1324 // TODO: When HttpHeader uses SBuf, change keys to SBuf
1325 typedef std::map
<const std::string
, RequestHandler
> RequestHandlers
;
1326 static RequestHandlers handlers
;
1327 if (!handlers
.size()) {
1328 handlers
["LIST"] = &Ftp::Server::handleDataRequest
;
1329 handlers
["NLST"] = &Ftp::Server::handleDataRequest
;
1330 handlers
["MLSD"] = &Ftp::Server::handleDataRequest
;
1331 handlers
["FEAT"] = &Ftp::Server::handleFeatRequest
;
1332 handlers
["PASV"] = &Ftp::Server::handlePasvRequest
;
1333 handlers
["PORT"] = &Ftp::Server::handlePortRequest
;
1334 handlers
["RETR"] = &Ftp::Server::handleDataRequest
;
1335 handlers
["EPRT"] = &Ftp::Server::handleEprtRequest
;
1336 handlers
["EPSV"] = &Ftp::Server::handleEpsvRequest
;
1337 handlers
["CWD"] = &Ftp::Server::handleCwdRequest
;
1338 handlers
["PASS"] = &Ftp::Server::handlePassRequest
;
1339 handlers
["CDUP"] = &Ftp::Server::handleCdupRequest
;
1342 RequestHandler handler
= NULL
;
1343 if (request
->method
== Http::METHOD_PUT
)
1344 handler
= &Ftp::Server::handleUploadRequest
;
1346 const RequestHandlers::const_iterator hi
= handlers
.find(cmd
.termedBuf());
1347 if (hi
!= handlers
.end())
1348 handler
= hi
->second
;
1352 debugs(9, 7, "forwarding " << cmd
<< " as is, no post-processing");
1356 return (this->*handler
)(cmd
, params
);
1359 /// Called to parse USER command, which is required to create an HTTP request
1360 /// wrapper. W/o request, the errors are handled by returning earlyError().
1362 Ftp::Server::handleUserRequest(const SBuf
&, SBuf
¶ms
)
1364 if (params
.isEmpty())
1365 return earlyError(EarlyErrorKind::MissingUsername
);
1367 // find the [end of] user name
1368 const SBuf::size_type eou
= params
.rfind('@');
1369 if (eou
== SBuf::npos
|| eou
+ 1 >= params
.length())
1370 return earlyError(EarlyErrorKind::MissingHost
);
1372 // Determine the intended destination.
1373 host
= params
.substr(eou
+ 1, params
.length());
1374 // If we can parse it as raw IPv6 address, then surround with "[]".
1375 // Otherwise (domain, IPv4, [bracketed] IPv6, garbage, etc), use as is.
1376 if (host
.find(':') != SBuf::npos
) {
1377 const Ip::Address
ipa(host
.c_str());
1378 if (!ipa
.isAnyAddr()) {
1379 char ipBuf
[MAX_IPSTRLEN
];
1380 ipa
.toHostStr(ipBuf
, MAX_IPSTRLEN
);
1385 // const SBuf login = params.substr(0, eou);
1386 params
.chop(0, eou
); // leave just the login part for the peer
1389 if (master
->clientReadGreeting
)
1392 master
->workingDir
.clear();
1395 if (!master
->clientReadGreeting
) {
1396 debugs(9, 3, "set URI to " << uri
);
1397 } else if (oldUri
.caseCmp(uri
) == 0) {
1398 debugs(9, 5, "kept URI as " << oldUri
);
1400 debugs(9, 3, "reset URI from " << oldUri
<< " to " << uri
);
1401 closeDataConnection();
1402 unpinConnection(true); // close control connection to peer
1403 resetLogin("URI reset");
1406 return NULL
; // no early errors
1410 Ftp::Server::handleFeatRequest(String
&, String
&)
1412 changeState(fssHandleFeat
, "handleFeatRequest");
1417 Ftp::Server::handlePasvRequest(String
&, String
¶ms
)
1420 setReply(500, "Bad PASV command");
1424 if (params
.size() > 0) {
1425 setReply(501, "Unexpected parameter");
1429 changeState(fssHandlePasv
, "handlePasvRequest");
1430 // no need to fake PASV request via setDataCommand() in true PASV case
1434 /// [Re]initializes dataConn for active data transfers. Does not connect.
1436 Ftp::Server::createDataConnection(Ip::Address cltAddr
)
1438 assert(clientConnection
!= NULL
);
1439 assert(!clientConnection
->remote
.isAnyAddr());
1441 if (cltAddr
!= clientConnection
->remote
) {
1442 debugs(33, 2, "rogue PORT " << cltAddr
<< " request? ctrl: " << clientConnection
->remote
);
1443 // Closing the control connection would not help with attacks because
1444 // the client is evidently able to connect to us. Besides, closing
1445 // makes retrials easier for the client and more damaging to us.
1446 setReply(501, "Prohibited parameter value");
1450 closeDataConnection();
1452 Comm::ConnectionPointer conn
= new Comm::Connection();
1453 conn
->flags
|= COMM_DOBIND
;
1455 // Use local IP address of the control connection as the source address
1456 // of the active data connection, or some clients will refuse to accept.
1457 conn
->setAddrs(clientConnection
->local
, cltAddr
);
1458 // RFC 959 requires active FTP connections to originate from port 20
1459 // but that would preclude us from supporting concurrent transfers! (XXX?)
1460 conn
->local
.port(0);
1462 debugs(9, 3, "will actively connect from " << conn
->local
<< " to " <<
1466 uploadAvailSize
= 0;
1471 Ftp::Server::handlePortRequest(String
&, String
¶ms
)
1473 // TODO: Should PORT errors trigger closeDataConnection() cleanup?
1476 setReply(500, "Rejecting PORT after EPSV ALL");
1480 if (!params
.size()) {
1481 setReply(501, "Missing parameter");
1485 Ip::Address cltAddr
;
1486 if (!Ftp::ParseIpPort(params
.termedBuf(), NULL
, cltAddr
)) {
1487 setReply(501, "Invalid parameter");
1491 if (!createDataConnection(cltAddr
))
1494 changeState(fssHandlePort
, "handlePortRequest");
1496 return true; // forward our fake PASV request
1500 Ftp::Server::handleDataRequest(String
&, String
&)
1502 if (!checkDataConnPre())
1505 master
->userDataDone
= 0;
1506 originDataDownloadAbortedOnError
= false;
1508 changeState(fssHandleDataRequest
, "handleDataRequest");
1514 Ftp::Server::handleUploadRequest(String
&, String
&)
1516 if (!checkDataConnPre())
1519 if (Config
.accessList
.forceRequestBodyContinuation
) {
1520 ClientHttpRequest
*http
= pipeline
.front()->http
;
1521 HttpRequest
*request
= http
->request
;
1522 ACLFilledChecklist
bodyContinuationCheck(Config
.accessList
.forceRequestBodyContinuation
, request
, NULL
);
1523 if (bodyContinuationCheck
.fastCheck() == ACCESS_ALLOWED
) {
1524 request
->forcedBodyContinuation
= true;
1525 if (checkDataConnPost()) {
1526 // Write control Msg
1527 writeEarlyReply(150, "Data connection opened");
1528 maybeReadUploadData();
1530 // wait for acceptDataConnection but tell it to call wroteEarlyReply
1531 // after writing "150 Data connection opened"
1532 typedef CommCbMemFunT
<Server
, CommIoCbParams
> Dialer
;
1533 AsyncCall::Pointer call
= JobCallback(33, 5, Dialer
, this, Ftp::Server::wroteEarlyReply
);
1534 onDataAcceptCall
= call
;
1539 changeState(fssHandleUploadRequest
, "handleDataRequest");
1545 Ftp::Server::handleEprtRequest(String
&, String
¶ms
)
1547 debugs(9, 3, "Process an EPRT " << params
);
1550 setReply(500, "Rejecting EPRT after EPSV ALL");
1554 if (!params
.size()) {
1555 setReply(501, "Missing parameter");
1559 Ip::Address cltAddr
;
1560 if (!Ftp::ParseProtoIpPort(params
.termedBuf(), cltAddr
)) {
1561 setReply(501, "Invalid parameter");
1565 if (!createDataConnection(cltAddr
))
1568 changeState(fssHandleEprt
, "handleEprtRequest");
1570 return true; // forward our fake PASV request
1574 Ftp::Server::handleEpsvRequest(String
&, String
¶ms
)
1576 debugs(9, 3, "Process an EPSV command with params: " << params
);
1577 if (params
.size() <= 0) {
1578 // treat parameterless EPSV as "use the protocol of the ctrl conn"
1579 } else if (params
.caseCmp("ALL") == 0) {
1580 setReply(200, "EPSV ALL ok");
1583 } else if (params
.cmp("2") == 0) {
1584 if (!Ip::EnableIpv6
) {
1585 setReply(522, "Network protocol not supported, use (1)");
1588 } else if (params
.cmp("1") != 0) {
1589 setReply(501, "Unsupported EPSV parameter");
1593 changeState(fssHandleEpsv
, "handleEpsvRequest");
1595 return true; // forward our fake PASV request
1599 Ftp::Server::handleCwdRequest(String
&, String
&)
1601 changeState(fssHandleCwd
, "handleCwdRequest");
1606 Ftp::Server::handlePassRequest(String
&, String
&)
1608 changeState(fssHandlePass
, "handlePassRequest");
1613 Ftp::Server::handleCdupRequest(String
&, String
&)
1615 changeState(fssHandleCdup
, "handleCdupRequest");
1619 // Convert user PORT, EPRT, PASV, or EPSV data command to Squid PASV command.
1620 // Squid FTP client decides what data command to use with peers.
1622 Ftp::Server::setDataCommand()
1624 ClientHttpRequest
*const http
= pipeline
.front()->http
;
1625 assert(http
!= NULL
);
1626 HttpRequest
*const request
= http
->request
;
1627 assert(request
!= NULL
);
1628 HttpHeader
&header
= request
->header
;
1629 header
.delById(Http::HdrType::FTP_COMMAND
);
1630 header
.putStr(Http::HdrType::FTP_COMMAND
, "PASV");
1631 header
.delById(Http::HdrType::FTP_ARGUMENTS
);
1632 header
.putStr(Http::HdrType::FTP_ARGUMENTS
, "");
1633 debugs(9, 5, "client data command converted to fake PASV");
1636 /// check that client data connection is ready for future I/O or at least
1637 /// has a chance of becoming ready soon.
1639 Ftp::Server::checkDataConnPre()
1641 if (Comm::IsConnOpen(dataConn
))
1644 if (Comm::IsConnOpen(dataListenConn
)) {
1645 // We are still waiting for a client to connect to us after PASV.
1646 // Perhaps client's data conn handshake has not reached us yet.
1647 // After we talk to the server, checkDataConnPost() will recheck.
1648 debugs(33, 3, "expecting clt data conn " << dataListenConn
);
1652 if (!dataConn
|| dataConn
->remote
.isAnyAddr()) {
1653 debugs(33, 5, "missing " << dataConn
);
1654 // TODO: use client address and default port instead.
1655 setReply(425, "Use PORT or PASV first");
1659 // active transfer: open a data connection from Squid to client
1660 typedef CommCbMemFunT
<Server
, CommConnectCbParams
> Dialer
;
1661 connector
= JobCallback(17, 3, Dialer
, this, Ftp::Server::connectedForData
);
1662 Comm::ConnOpener
*cs
= new Comm::ConnOpener(dataConn
, connector
,
1663 Config
.Timeout
.connect
);
1664 AsyncJob::Start(cs
);
1665 return false; // ConnStateData::processFtpRequest waits handleConnectDone
1668 /// Check that client data connection is ready for immediate I/O.
1670 Ftp::Server::checkDataConnPost() const
1672 if (!Comm::IsConnOpen(dataConn
)) {
1673 debugs(33, 3, "missing client data conn: " << dataConn
);
1679 /// Done establishing a data connection to the user.
1681 Ftp::Server::connectedForData(const CommConnectCbParams
¶ms
)
1685 if (params
.flag
!= Comm::OK
) {
1686 /* it might have been a timeout with a partially open link */
1687 if (params
.conn
!= NULL
)
1688 params
.conn
->close();
1689 setReply(425, "Cannot open data connection.");
1690 Http::StreamPointer context
= pipeline
.front();
1691 Must(context
->http
);
1692 Must(context
->http
->storeEntry() != NULL
);
1694 Must(dataConn
== params
.conn
);
1695 Must(Comm::IsConnOpen(params
.conn
));
1696 fd_note(params
.conn
->fd
, "active client ftp data");
1703 Ftp::Server::setReply(const int code
, const char *msg
)
1705 Http::StreamPointer context
= pipeline
.front();
1706 ClientHttpRequest
*const http
= context
->http
;
1707 assert(http
!= NULL
);
1708 assert(http
->storeEntry() == NULL
);
1710 HttpReply
*const reply
= Ftp::HttpReplyWrapper(code
, msg
, Http::scNoContent
, 0);
1712 setLogUri(http
, urlCanonicalClean(http
->request
));
1714 clientStreamNode
*const node
= context
->getClientReplyContext();
1715 clientReplyContext
*const repContext
=
1716 dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
1717 assert(repContext
!= NULL
);
1719 RequestFlags reqFlags
;
1720 reqFlags
.cachable
= false; // force releaseRequest() in storeCreateEntry()
1721 reqFlags
.noCache
= true;
1722 repContext
->createStoreEntry(http
->request
->method
, reqFlags
);
1723 http
->storeEntry()->replaceHttpReply(reply
);
1727 Ftp::Server::callException(const std::exception
&e
)
1729 debugs(33, 2, "FTP::Server job caught: " << e
.what());
1730 closeDataConnection();
1731 unpinConnection(true);
1732 if (Comm::IsConnOpen(clientConnection
))
1733 clientConnection
->close();
1734 AsyncJob::callException(e
);
1738 Ftp::Server::startWaitingForOrigin()
1740 if (!isOpen()) // if we are closing, nothing to do
1743 debugs(33, 5, "waiting for Ftp::Client data transfer to end");
1744 waitingForOrigin
= true;
1748 Ftp::Server::stopWaitingForOrigin(int originStatus
)
1750 Must(waitingForOrigin
);
1751 waitingForOrigin
= false;
1753 if (!isOpen()) // if we are closing, nothing to do
1756 // if we have already decided how to respond, respond now
1758 HttpReply::Pointer reply
= delayedReply
;
1759 delayedReply
= nullptr;
1760 writeForwardedReply(reply
.getRaw());
1761 return; // do not completeDataDownload() after an earlier response
1764 if (master
->serverState
!= fssHandleDataRequest
)
1767 // completeDataDownload() could be waitingForOrigin in fssHandleDataRequest
1768 // Depending on which side has finished downloading first, either trust
1769 // master->userDataDone status or set originDataDownloadAbortedOnError:
1770 if (master
->userDataDone
) {
1771 // We finished downloading before Ftp::Client. Most likely, the
1772 // adaptation shortened the origin response or we hit an error.
1773 // Our status (stored in master->userDataDone) is more informative.
1774 // Use master->userDataDone; avoid originDataDownloadAbortedOnError.
1775 completeDataDownload();
1777 debugs(33, 5, "too early to write the response");
1778 // Ftp::Client naturally finished downloading before us. Set
1779 // originDataDownloadAbortedOnError to overwrite future
1780 // master->userDataDone and relay Ftp::Client error, if there was
1781 // any, to the user.
1782 originDataDownloadAbortedOnError
= (originStatus
>= 400);
1786 void Ftp::Server::userDataCompletionCheckpoint(int finalStatusCode
)
1788 Must(!master
->userDataDone
);
1789 master
->userDataDone
= finalStatusCode
;
1792 finishDechunkingRequest(false);
1794 if (waitingForOrigin
) {
1795 // The completeDataDownload() is not called here unconditionally
1796 // because we want to signal the FTP user that we are not fully
1797 // done processing its data stream, even though all data bytes
1798 // have been sent or received already.
1799 debugs(33, 5, "Transfering from FTP server is not complete");
1803 // Adjust our reply if the server aborted with an error before we are done.
1804 if (master
->userDataDone
== 226 && originDataDownloadAbortedOnError
) {
1805 debugs(33, 5, "Transfering from FTP server terminated with an error, adjust status code");
1806 master
->userDataDone
= 451;
1808 completeDataDownload();
1811 void Ftp::Server::completeDataDownload()
1813 writeCustomReply(master
->userDataDone
, master
->userDataDone
== 226 ? "Transfer complete" : "Server error; transfer aborted");
1814 closeDataConnection();
1817 /// Whether Squid FTP Relay supports a named feature (e.g., a command).
1819 Ftp::SupportedCommand(const SBuf
&name
)
1821 static std::set
<SBuf
> BlackList
;
1822 if (BlackList
.empty()) {
1823 /* Add FTP commands that Squid cannot relay correctly. */
1825 // We probably do not support AUTH TLS.* and AUTH SSL,
1826 // but let's disclaim all AUTH support to KISS, for now.
1827 BlackList
.insert(cmdAuth());
1830 // we claim support for all commands that we do not know about
1831 return BlackList
.find(name
) == BlackList
.end();