FtpServer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /* DEBUG: section 33 Client-side Routines */
10 
11 #ifndef SQUID_SRC_SERVERS_FTPSERVER_H
12 #define SQUID_SRC_SERVERS_FTPSERVER_H
13 
14 #include "base/JobWait.h"
15 #include "base/Lock.h"
16 #include "client_side.h"
17 #include "comm/forward.h"
18 #include "http/forward.h"
19 
20 namespace Ftp
21 {
22 
23 typedef enum {
37 } ServerState;
38 
39 // TODO: This should become a part of MasterXaction when we start sending
40 // master transactions to the clients/ code.
43 {
44 public:
46 
48 
53  int userDataDone;
55 };
56 
58 class Server: public ConnStateData
59 {
61 
62 public:
63  explicit Server(const MasterXaction::Pointer &xact);
64  ~Server() override;
65 
66  /* AsyncJob API */
67  void callException(const std::exception &e) override;
68 
71  void startWaitingForOrigin();
72 
76  void stopWaitingForOrigin(int status);
77 
78  // This is a pointer in hope to minimize future changes when MasterState
79  // becomes a part of MasterXaction. Guaranteed not to be nil.
81 
82 protected:
83  friend void StartListening();
84 
85  // errors detected before it is possible to create an HTTP request wrapper
86  enum class EarlyErrorKind {
92  InvalidUri,
94  };
95 
96  /* ConnStateData API */
97  Http::Stream *parseOneRequest() override;
98  void processParsedRequest(Http::StreamPointer &context) override;
99  void notePeerConnection(Comm::ConnectionPointer conn) override;
100  void clientPinnedConnectionClosed(const CommCloseCbParams &io) override;
101  void handleReply(HttpReply *header, StoreIOBuffer receivedData) override;
102  int pipelinePrefetchMax() const override;
103  bool writeControlMsgAndCall(HttpReply *rep, AsyncCall::Pointer &call) override;
104  time_t idleTimeout() const override;
105 
106  /* BodyPipe API */
108  void noteBodyConsumerAborted(BodyPipe::Pointer ptr) override;
109 
110  /* AsyncJob API */
111  void start() override;
112 
113  /* Comm callbacks */
114  static void AcceptCtrlConnection(const CommAcceptCbParams &params);
115  void acceptDataConnection(const CommAcceptCbParams &params);
116  void readUploadData(const CommIoCbParams &io);
117  void wroteEarlyReply(const CommIoCbParams &io);
118  void wroteReply(const CommIoCbParams &io);
119  void wroteReplyData(const CommIoCbParams &io);
120  void connectedForData(const CommConnectCbParams &params);
121 
122  unsigned int listenForDataConnection();
123  bool createDataConnection(Ip::Address cltAddr);
124  void closeDataConnection();
125 
128  void userDataCompletionCheckpoint(int finalStatusCode);
129 
132  void completeDataDownload();
133 
134  void calcUri(const SBuf *file);
135  void changeState(const Ftp::ServerState newState, const char *reason);
136  Http::Stream *handleUserRequest(const SBuf &cmd, SBuf &params);
137  bool checkDataConnPost() const;
139  void maybeReadUploadData();
140 
141  void setReply(const int code, const char *msg);
142  void writeCustomReply(const int code, const char *msg, const HttpReply *reply = nullptr);
143  void writeEarlyReply(const int code, const char *msg);
144  void writeErrorReply(const HttpReply *reply, const int status);
145  void writeForwardedForeign(const HttpReply *reply);
146  void writeForwardedReply(const HttpReply *reply);
147  void writeForwardedReplyAndCall(const HttpReply *reply, AsyncCall::Pointer &call);
148  void writeReply(MemBuf &mb);
149 
151  bool handleRequest(HttpRequest *);
152  void setDataCommand();
153  bool checkDataConnPre();
154 
156  typedef bool (Ftp::Server::*RequestHandler)(String &cmd, String &params);
157  bool handleFeatRequest(String &cmd, String &params);
158  bool handlePasvRequest(String &cmd, String &params);
159  bool handlePortRequest(String &cmd, String &params);
160  bool handleDataRequest(String &cmd, String &params);
161  bool handleUploadRequest(String &cmd, String &params);
162  bool handleEprtRequest(String &cmd, String &params);
163  bool handleEpsvRequest(String &cmd, String &params);
164  bool handleCwdRequest(String &cmd, String &params);
165  bool handlePassRequest(String &cmd, String &params);
166  bool handleCdupRequest(String &cmd, String &params);
167 
169  typedef void (Ftp::Server::*ReplyHandler)(const HttpReply *reply, StoreIOBuffer data);
170  void handleFeatReply(const HttpReply *header, StoreIOBuffer receivedData);
171  void handlePasvReply(const HttpReply *header, StoreIOBuffer receivedData);
172  void handlePortReply(const HttpReply *header, StoreIOBuffer receivedData);
173  void handleErrorReply(const HttpReply *header, StoreIOBuffer receivedData);
174  void handleDataReply(const HttpReply *header, StoreIOBuffer receivedData);
175  void handleUploadReply(const HttpReply *header, StoreIOBuffer receivedData);
176  void handleEprtReply(const HttpReply *header, StoreIOBuffer receivedData);
177  void handleEpsvReply(const HttpReply *header, StoreIOBuffer receivedData);
178 
179 private:
180  void doProcessRequest();
181  void shovelUploadData();
182  void resetLogin(const char *reason);
183 
186  bool gotEpsvAll;
192 
194 
198 
200 
205 
208 };
209 
210 } // namespace Ftp
211 
212 #endif /* SQUID_SRC_SERVERS_FTPSERVER_H */
213 
bool gotEpsvAll
restrict data conn setup commands to just EPSV
Definition: FtpServer.h:186
bool handleEprtRequest(String &cmd, String &params)
Definition: FtpServer.cc:1567
@ fssHandleCdup
Definition: FtpServer.h:35
SBuf host
intended dest. of a transparently intercepted FTP conn
Definition: FtpServer.h:185
void wroteEarlyReply(const CommIoCbParams &io)
Definition: FtpServer.cc:1234
Http::Stream * handleUserRequest(const SBuf &cmd, SBuf &params)
Definition: FtpServer.cc:1358
void completeDataDownload()
Definition: FtpServer.cc:1834
CBDATA_CHILD(Server)
Comm::ConnectionPointer dataListenConn
data connection listening socket
Definition: FtpServer.h:188
AsyncCall::Pointer listener
set when we are passively listening
Definition: FtpServer.h:193
void handleDataReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:943
void(Ftp::Server::* ReplyHandler)(const HttpReply *reply, StoreIOBuffer data)
a method handling an FTP response; selected by handleReply()
Definition: FtpServer.h:169
int userDataDone
Squid will send or has sent this final status code to the FTP client.
Definition: FtpServer.h:54
Transaction information shared among our FTP client and server jobs.
Definition: FtpServer.h:42
@ fssHandleUploadRequest
Definition: FtpServer.h:30
static void AcceptCtrlConnection(const CommAcceptCbParams &params)
accept a new FTP control connection and hand it to a dedicated Server
Definition: FtpServer.cc:242
@ fssBegin
Definition: FtpServer.h:24
int pipelinePrefetchMax() const override
returning N allows a pipeline of 1+N requests (see pipeline_prefetch)
Definition: FtpServer.cc:80
@ fssHandleCwd
Definition: FtpServer.h:33
Definition: SBuf.h:93
bool handlePortRequest(String &cmd, String &params)
Definition: FtpServer.cc:1491
void closeDataConnection()
Definition: FtpServer.cc:445
void writeForwardedForeign(const HttpReply *reply)
Definition: FtpServer.cc:1143
bool handleCwdRequest(String &cmd, String &params)
Definition: FtpServer.cc:1621
JobWait< Comm::ConnOpener > dataConnWait
Definition: FtpServer.h:197
bool handleFeatRequest(String &cmd, String &params)
Definition: FtpServer.cc:1406
void wroteReplyData(const CommIoCbParams &io)
called when we are done writing a chunk of the response data
Definition: FtpServer.cc:986
void doProcessRequest()
react to the freshly parsed request
Definition: FtpServer.cc:128
void handleFeatReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:812
bool clientReadGreeting
Definition: FtpServer.h:52
void writeForwardedReply(const HttpReply *reply)
Definition: FtpServer.cc:1036
friend void StartListening()
accept connections on all configured ftp_ports
char uploadBuf[CLIENT_REQ_BUF_SZ]
data connection input buffer
Definition: FtpServer.h:190
ServerState
Definition: FtpServer.h:23
bool handlePasvRequest(String &cmd, String &params)
Definition: FtpServer.cc:1413
size_t uploadAvailSize
number of yet unused uploadBuf bytes
Definition: FtpServer.h:191
void handleEprtReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1059
void wroteReply(const CommIoCbParams &io)
Definition: FtpServer.cc:1256
void shovelUploadData()
shovel upload data from the internal buffer to the body pipe if possible
Definition: FtpServer.cc:202
SBuf workingDir
estimated current working directory for URI formation
Definition: FtpServer.h:50
Definition: forward.h:23
void start() override
called by AsyncStart; do not call directly
Definition: FtpServer.cc:92
bool createDataConnection(Ip::Address cltAddr)
[Re]initializes dataConn for active data transfers. Does not connect.
Definition: FtpServer.cc:1432
Server(const MasterXaction::Pointer &xact)
Definition: FtpServer.cc:53
bool checkDataConnPre()
Definition: FtpServer.cc:1661
time_t idleTimeout() const override
timeout to use when waiting for the next request
Definition: FtpServer.cc:86
MasterState::Pointer master
info shared among our FTP client and server jobs
Definition: FtpServer.h:80
void setReply(const int code, const char *msg)
Definition: FtpServer.cc:1729
bool handleDataRequest(String &cmd, String &params)
Definition: FtpServer.cc:1520
void replyDataWritingCheckpoint()
ClientStream checks after (actual or skipped) reply data writing.
Definition: FtpServer.cc:1004
void writeErrorReply(const HttpReply *reply, const int status)
writes FTP error response with given status and reply-derived error details
Definition: FtpServer.cc:1095
@ fssConnected
Definition: FtpServer.h:25
void setDataCommand()
Definition: FtpServer.cc:1644
AsyncCall::Pointer onDataAcceptCall
who to call upon data conn acceptance
Definition: FtpServer.h:187
void calcUri(const SBuf *file)
computes uri member from host and, if tracked, working dir with file name
Definition: FtpServer.cc:345
void writeReply(MemBuf &mb)
Definition: FtpServer.cc:495
void notePeerConnection(Comm::ConnectionPointer conn) override
called just before a FwdState-dispatched job starts using connection
Definition: FtpServer.cc:302
#define CLIENT_REQ_BUF_SZ
Definition: defines.h:86
void handleReply(HttpReply *header, StoreIOBuffer receivedData) override
Definition: FtpServer.cc:778
Definition: MemBuf.h:23
void acceptDataConnection(const CommAcceptCbParams &params)
Definition: FtpServer.cc:401
bool handleCdupRequest(String &cmd, String &params)
Definition: FtpServer.cc:1635
@ fssHandlePort
Definition: FtpServer.h:28
void maybeReadUploadData()
schedules another data connection read if needed
Definition: FtpServer.cc:109
void handleUploadReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1029
Http::Stream * earlyError(const EarlyErrorKind eek)
creates a context filled with an error message for a given early error
Definition: FtpServer.cc:570
void noteBodyConsumerAborted(BodyPipe::Pointer ptr) override
Definition: FtpServer.cc:231
void stopWaitingForOrigin(int status)
Definition: FtpServer.cc:1771
void userDataCompletionCheckpoint(int finalStatusCode)
Definition: FtpServer.cc:1809
void callException(const std::exception &e) override
called when the job throws during an async call
Definition: FtpServer.cc:1750
@ fssHandlePasv
Definition: FtpServer.h:27
Comm::ConnectionPointer dataConn
data connection
Definition: FtpServer.h:189
Manages a control connection from an FTP client.
Definition: FtpServer.h:58
~Server() override
Definition: FtpServer.cc:74
int code
Definition: smb-errors.c:145
virtual const char * status() const
internal cleanup; do not call directly
Definition: AsyncJob.cc:182
@ fssHandleEpsv
Definition: FtpServer.h:32
@ fssHandlePass
Definition: FtpServer.h:34
unsigned int listenForDataConnection()
Definition: FtpServer.cc:370
@ fssError
Definition: FtpServer.h:36
void handlePasvReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:881
RefCount< MasterState > Pointer
Definition: FtpServer.h:45
bool handlePassRequest(String &cmd, String &params)
Definition: FtpServer.cc:1628
void readUploadData(const CommIoCbParams &io)
imports more upload data from the data connection
Definition: FtpServer.cc:168
AsyncCall::Pointer reader
set when we are reading FTP data
Definition: FtpServer.h:199
Ip::Address clientDataAddr
address of our FTP client data connection
Definition: FtpServer.h:49
ServerState serverState
what our FTP server is doing
Definition: FtpServer.h:51
@ fssHandleEprt
Definition: FtpServer.h:31
bool checkDataConnPost() const
Check that client data connection is ready for immediate I/O.
Definition: FtpServer.cc:1692
void writeEarlyReply(const int code, const char *msg)
Definition: FtpServer.cc:476
bool writeControlMsgAndCall(HttpReply *rep, AsyncCall::Pointer &call) override
handle a control message received by context from a peer and call back
Definition: FtpServer.cc:1152
void processParsedRequest(Http::StreamPointer &context) override
start processing a freshly parsed request
Definition: FtpServer.cc:156
void writeForwardedReplyAndCall(const HttpReply *reply, AsyncCall::Pointer &call)
Definition: FtpServer.cc:1161
void handleErrorReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:934
void handleEpsvReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1072
@ fssHandleDataRequest
Definition: FtpServer.h:29
void writeCustomReply(const int code, const char *msg, const HttpReply *reply=nullptr)
Definition: FtpServer.cc:507
void clientPinnedConnectionClosed(const CommCloseCbParams &io) override
Our close handler called by Comm when the pinned connection is closed.
Definition: FtpServer.cc:316
bool handleUploadRequest(String &cmd, String &params)
Definition: FtpServer.cc:1534
void changeState(const Ftp::ServerState newState, const char *reason)
Definition: FtpServer.cc:529
Http::Stream * parseOneRequest() override
Definition: FtpServer.cc:640
bool originDataDownloadAbortedOnError
whether the origin data transfer aborted
Definition: FtpServer.h:204
void connectedForData(const CommConnectCbParams &params)
Done establishing a data connection to the user.
Definition: FtpServer.cc:1703
void noteMoreBodySpaceAvailable(BodyPipe::Pointer) override
Definition: FtpServer.cc:222
bool waitingForOrigin
whether we wait for the origin data transfer to end
Definition: FtpServer.h:202
void resetLogin(const char *reason)
clear client and server login-related state after the old login is gone
Definition: FtpServer.cc:336
void startWaitingForOrigin()
Definition: FtpServer.cc:1761
bool handleEpsvRequest(String &cmd, String &params)
Definition: FtpServer.cc:1596
bool(Ftp::Server::* RequestHandler)(String &cmd, String &params)
a method handling an FTP command; selected by handleRequest()
Definition: FtpServer.h:156
@ fssHandleFeat
Definition: FtpServer.h:26
SBuf uri
a URI reconstructed from various FTP message details
Definition: FtpServer.h:184
void handlePortReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:921
HttpReply::Pointer delayedReply
a response which writing was postponed until stopWaitingForOrigin()
Definition: FtpServer.h:207
bool handleRequest(HttpRequest *)
Definition: FtpServer.cc:1299

 

Introduction

Documentation

Support

Miscellaneous