FtpServer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 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_SERVERS_FTP_SERVER_H
12 #define SQUID_SERVERS_FTP_SERVER_H
13 
14 #include "base/Lock.h"
15 #include "client_side.h"
16 
17 namespace Ftp
18 {
19 
20 typedef enum {
34 } ServerState;
35 
36 // TODO: This should become a part of MasterXaction when we start sending
37 // master transactions to the clients/ code.
40 {
41 public:
43 
45 
50  int userDataDone;
52 };
53 
55 class Server: public ConnStateData
56 {
58 
59 public:
60  explicit Server(const MasterXaction::Pointer &xact);
61  virtual ~Server() override;
62 
63  /* AsyncJob API */
64  virtual void callException(const std::exception &e) override;
65 
68  void startWaitingForOrigin();
69 
73  void stopWaitingForOrigin(int status);
74 
75  // This is a pointer in hope to minimize future changes when MasterState
76  // becomes a part of MasterXaction. Guaranteed not to be nil.
78 
79 protected:
80  friend void StartListening();
81 
82  // errors detected before it is possible to create an HTTP request wrapper
83  enum class EarlyErrorKind {
89  InvalidUri,
91  };
92 
93  /* ConnStateData API */
94  virtual Http::Stream *parseOneRequest() override;
95  virtual void processParsedRequest(Http::StreamPointer &context) override;
96  virtual void notePeerConnection(Comm::ConnectionPointer conn) override;
97  virtual void clientPinnedConnectionClosed(const CommCloseCbParams &io) override;
98  virtual void handleReply(HttpReply *header, StoreIOBuffer receivedData) override;
99  virtual int pipelinePrefetchMax() const override;
100  virtual bool writeControlMsgAndCall(HttpReply *rep, AsyncCall::Pointer &call) override;
101  virtual time_t idleTimeout() const override;
102 
103  /* BodyPipe API */
104  virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer) override;
105  virtual void noteBodyConsumerAborted(BodyPipe::Pointer ptr) override;
106 
107  /* AsyncJob API */
108  virtual void start() override;
109 
110  /* Comm callbacks */
111  static void AcceptCtrlConnection(const CommAcceptCbParams &params);
112  void acceptDataConnection(const CommAcceptCbParams &params);
113  void readUploadData(const CommIoCbParams &io);
114  void wroteEarlyReply(const CommIoCbParams &io);
115  void wroteReply(const CommIoCbParams &io);
116  void wroteReplyData(const CommIoCbParams &io);
117  void connectedForData(const CommConnectCbParams &params);
118 
119  unsigned int listenForDataConnection();
120  bool createDataConnection(Ip::Address cltAddr);
121  void closeDataConnection();
122 
125  void userDataCompletionCheckpoint(int finalStatusCode);
126 
129  void completeDataDownload();
130 
131  void calcUri(const SBuf *file);
132  void changeState(const Ftp::ServerState newState, const char *reason);
133  Http::Stream *handleUserRequest(const SBuf &cmd, SBuf &params);
134  bool checkDataConnPost() const;
136  void maybeReadUploadData();
137 
138  void setReply(const int code, const char *msg);
139  void writeCustomReply(const int code, const char *msg, const HttpReply *reply = NULL);
140  void writeEarlyReply(const int code, const char *msg);
141  void writeErrorReply(const HttpReply *reply, const int status);
142  void writeForwardedForeign(const HttpReply *reply);
143  void writeForwardedReply(const HttpReply *reply);
144  void writeForwardedReplyAndCall(const HttpReply *reply, AsyncCall::Pointer &call);
145  void writeReply(MemBuf &mb);
146 
148  bool handleRequest(HttpRequest *);
149  void setDataCommand();
150  bool checkDataConnPre();
151 
153  typedef bool (Ftp::Server::*RequestHandler)(String &cmd, String &params);
154  bool handleFeatRequest(String &cmd, String &params);
155  bool handlePasvRequest(String &cmd, String &params);
156  bool handlePortRequest(String &cmd, String &params);
157  bool handleDataRequest(String &cmd, String &params);
158  bool handleUploadRequest(String &cmd, String &params);
159  bool handleEprtRequest(String &cmd, String &params);
160  bool handleEpsvRequest(String &cmd, String &params);
161  bool handleCwdRequest(String &cmd, String &params);
162  bool handlePassRequest(String &cmd, String &params);
163  bool handleCdupRequest(String &cmd, String &params);
164 
166  typedef void (Ftp::Server::*ReplyHandler)(const HttpReply *reply, StoreIOBuffer data);
167  void handleFeatReply(const HttpReply *header, StoreIOBuffer receivedData);
168  void handlePasvReply(const HttpReply *header, StoreIOBuffer receivedData);
169  void handlePortReply(const HttpReply *header, StoreIOBuffer receivedData);
170  void handleErrorReply(const HttpReply *header, StoreIOBuffer receivedData);
171  void handleDataReply(const HttpReply *header, StoreIOBuffer receivedData);
172  void handleUploadReply(const HttpReply *header, StoreIOBuffer receivedData);
173  void handleEprtReply(const HttpReply *header, StoreIOBuffer receivedData);
174  void handleEpsvReply(const HttpReply *header, StoreIOBuffer receivedData);
175 
176 private:
177  void doProcessRequest();
178  void shovelUploadData();
179  void resetLogin(const char *reason);
180 
183  bool gotEpsvAll;
189 
193 
198 
201 };
202 
203 } // namespace Ftp
204 
205 #endif /* SQUID_SERVERS_FTP_SERVER_H */
206 
bool gotEpsvAll
restrict data conn setup commands to just EPSV
Definition: FtpServer.h:183
bool handleEprtRequest(String &cmd, String &params)
Definition: FtpServer.cc:1565
@ fssHandleCdup
Definition: FtpServer.h:32
SBuf host
intended dest. of a transparently intercepted FTP conn
Definition: FtpServer.h:182
void wroteEarlyReply(const CommIoCbParams &io)
Definition: FtpServer.cc:1232
Http::Stream * handleUserRequest(const SBuf &cmd, SBuf &params)
Definition: FtpServer.cc:1356
void completeDataDownload()
Definition: FtpServer.cc:1829
CBDATA_CHILD(Server)
Comm::ConnectionPointer dataListenConn
data connection listening socket
Definition: FtpServer.h:185
AsyncCall::Pointer listener
set when we are passively listening
Definition: FtpServer.h:190
void handleDataReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:939
void(Ftp::Server::* ReplyHandler)(const HttpReply *reply, StoreIOBuffer data)
a method handling an FTP response; selected by handleReply()
Definition: FtpServer.h:166
int userDataDone
Squid will send or has sent this final status code to the FTP client.
Definition: FtpServer.h:51
Transaction information shared among our FTP client and server jobs.
Definition: FtpServer.h:39
@ fssHandleUploadRequest
Definition: FtpServer.h:27
static void AcceptCtrlConnection(const CommAcceptCbParams &params)
accept a new FTP control connection and hand it to a dedicated Server
Definition: FtpServer.cc:241
@ fssBegin
Definition: FtpServer.h:21
virtual int pipelinePrefetchMax() const override
returning N allows a pipeline of 1+N requests (see pipeline_prefetch)
Definition: FtpServer.cc:79
@ fssHandleCwd
Definition: FtpServer.h:30
Definition: SBuf.h:86
bool handlePortRequest(String &cmd, String &params)
Definition: FtpServer.cc:1489
void closeDataConnection()
Definition: FtpServer.cc:440
void writeForwardedForeign(const HttpReply *reply)
Definition: FtpServer.cc:1141
bool handleCwdRequest(String &cmd, String &params)
Definition: FtpServer.cc:1619
bool handleFeatRequest(String &cmd, String &params)
Definition: FtpServer.cc:1404
void wroteReplyData(const CommIoCbParams &io)
called when we are done writing a chunk of the response data
Definition: FtpServer.cc:982
void doProcessRequest()
react to the freshly parsed request
Definition: FtpServer.cc:127
void handleFeatReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:808
bool clientReadGreeting
Definition: FtpServer.h:49
void writeForwardedReply(const HttpReply *reply)
Definition: FtpServer.cc:1032
friend void StartListening()
accept connections on all configured ftp_ports
char uploadBuf[CLIENT_REQ_BUF_SZ]
data connection input buffer
Definition: FtpServer.h:187
ServerState
Definition: FtpServer.h:20
bool handlePasvRequest(String &cmd, String &params)
Definition: FtpServer.cc:1411
size_t uploadAvailSize
number of yet unused uploadBuf bytes
Definition: FtpServer.h:188
void handleEprtReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1055
void wroteReply(const CommIoCbParams &io)
Definition: FtpServer.cc:1254
void shovelUploadData()
shovel upload data from the internal buffer to the body pipe if possible
Definition: FtpServer.cc:201
SBuf workingDir
estimated current working directory for URI formation
Definition: FtpServer.h:47
Definition: forward.h:27
virtual void start() override
called by AsyncStart; do not call directly
Definition: FtpServer.cc:91
bool createDataConnection(Ip::Address cltAddr)
[Re]initializes dataConn for active data transfers. Does not connect.
Definition: FtpServer.cc:1430
Server(const MasterXaction::Pointer &xact)
Definition: FtpServer.cc:52
bool checkDataConnPre()
Definition: FtpServer.cc:1659
virtual time_t idleTimeout() const override
timeout to use when waiting for the next request
Definition: FtpServer.cc:85
MasterState::Pointer master
info shared among our FTP client and server jobs
Definition: FtpServer.h:77
void setReply(const int code, const char *msg)
Definition: FtpServer.cc:1723
bool handleDataRequest(String &cmd, String &params)
Definition: FtpServer.cc:1518
void replyDataWritingCheckpoint()
ClientStream checks after (actual or skipped) reply data writing.
Definition: FtpServer.cc:1000
void writeErrorReply(const HttpReply *reply, const int status)
writes FTP error response with given status and reply-derived error details
Definition: FtpServer.cc:1091
@ fssConnected
Definition: FtpServer.h:22
void setDataCommand()
Definition: FtpServer.cc:1642
#define NULL
Definition: types.h:166
AsyncCall::Pointer onDataAcceptCall
who to call upon data conn acceptance
Definition: FtpServer.h:184
void calcUri(const SBuf *file)
computes uri member from host and, if tracked, working dir with file name
Definition: FtpServer.cc:340
void writeReply(MemBuf &mb)
Definition: FtpServer.cc:490
virtual void notePeerConnection(Comm::ConnectionPointer conn) override
called just before a FwdState-dispatched job starts using connection
Definition: FtpServer.cc:297
#define CLIENT_REQ_BUF_SZ
Definition: defines.h:130
virtual void handleReply(HttpReply *header, StoreIOBuffer receivedData) override
Definition: FtpServer.cc:774
Definition: MemBuf.h:23
void acceptDataConnection(const CommAcceptCbParams &params)
Definition: FtpServer.cc:396
bool handleCdupRequest(String &cmd, String &params)
Definition: FtpServer.cc:1633
unsigned char code
Definition: html_quote.c:20
@ fssHandlePort
Definition: FtpServer.h:25
void maybeReadUploadData()
schedules another data connection read if needed
Definition: FtpServer.cc:108
void const char HLPCB void * data
Definition: stub_helper.cc:16
void handleUploadReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1025
Http::Stream * earlyError(const EarlyErrorKind eek)
creates a context filled with an error message for a given early error
Definition: FtpServer.cc:565
void writeCustomReply(const int code, const char *msg, const HttpReply *reply=NULL)
Definition: FtpServer.cc:502
virtual void noteBodyConsumerAborted(BodyPipe::Pointer ptr) override
Definition: FtpServer.cc:230
void stopWaitingForOrigin(int status)
Definition: FtpServer.cc:1766
void userDataCompletionCheckpoint(int finalStatusCode)
Definition: FtpServer.cc:1804
int conn
the current server connection FD
Definition: Transport.cc:26
virtual void callException(const std::exception &e) override
called when the job throws during an async call
Definition: FtpServer.cc:1745
@ fssHandlePasv
Definition: FtpServer.h:24
Comm::ConnectionPointer dataConn
data connection
Definition: FtpServer.h:186
Manages a control connection from an FTP client.
Definition: FtpServer.h:55
virtual ~Server() override
Definition: FtpServer.cc:73
virtual const char * status() const
internal cleanup; do not call directly
Definition: AsyncJob.cc:159
@ fssHandleEpsv
Definition: FtpServer.h:29
@ fssHandlePass
Definition: FtpServer.h:31
unsigned int listenForDataConnection()
Definition: FtpServer.cc:365
@ fssError
Definition: FtpServer.h:33
void handlePasvReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:877
RefCount< MasterState > Pointer
Definition: FtpServer.h:42
bool handlePassRequest(String &cmd, String &params)
Definition: FtpServer.cc:1626
void readUploadData(const CommIoCbParams &io)
imports more upload data from the data connection
Definition: FtpServer.cc:167
AsyncCall::Pointer reader
set when we are reading FTP data
Definition: FtpServer.h:192
Ip::Address clientDataAddr
address of our FTP client data connection
Definition: FtpServer.h:46
ServerState serverState
what our FTP server is doing
Definition: FtpServer.h:48
@ fssHandleEprt
Definition: FtpServer.h:28
bool checkDataConnPost() const
Check that client data connection is ready for immediate I/O.
Definition: FtpServer.cc:1690
void writeEarlyReply(const int code, const char *msg)
Definition: FtpServer.cc:471
virtual bool writeControlMsgAndCall(HttpReply *rep, AsyncCall::Pointer &call) override
handle a control message received by context from a peer and call back
Definition: FtpServer.cc:1150
virtual void processParsedRequest(Http::StreamPointer &context) override
start processing a freshly parsed request
Definition: FtpServer.cc:155
void writeForwardedReplyAndCall(const HttpReply *reply, AsyncCall::Pointer &call)
Definition: FtpServer.cc:1159
void handleErrorReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:930
void handleEpsvReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1068
@ fssHandleDataRequest
Definition: FtpServer.h:26
virtual void clientPinnedConnectionClosed(const CommCloseCbParams &io) override
Our close handler called by Comm when the pinned connection is closed.
Definition: FtpServer.cc:311
bool handleUploadRequest(String &cmd, String &params)
Definition: FtpServer.cc:1532
void changeState(const Ftp::ServerState newState, const char *reason)
Definition: FtpServer.cc:524
virtual Http::Stream * parseOneRequest() override
Definition: FtpServer.cc:635
bool originDataDownloadAbortedOnError
whether the origin data transfer aborted
Definition: FtpServer.h:197
#define false
Definition: GnuRegex.c:233
void connectedForData(const CommConnectCbParams &params)
Done establishing a data connection to the user.
Definition: FtpServer.cc:1701
virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer) override
Definition: FtpServer.cc:221
bool waitingForOrigin
whether we wait for the origin data transfer to end
Definition: FtpServer.h:195
void resetLogin(const char *reason)
clear client and server login-related state after the old login is gone
Definition: FtpServer.cc:331
void startWaitingForOrigin()
Definition: FtpServer.cc:1756
bool handleEpsvRequest(String &cmd, String &params)
Definition: FtpServer.cc:1594
bool(Ftp::Server::* RequestHandler)(String &cmd, String &params)
a method handling an FTP command; selected by handleRequest()
Definition: FtpServer.h:153
@ fssHandleFeat
Definition: FtpServer.h:23
SBuf uri
a URI reconstructed from various FTP message details
Definition: FtpServer.h:181
void handlePortReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:917
HttpReply::Pointer delayedReply
a response which writing was postponed until stopWaitingForOrigin()
Definition: FtpServer.h:200
bool handleRequest(HttpRequest *)
Definition: FtpServer.cc:1297
AsyncCall::Pointer connector
set when we are actively connecting
Definition: FtpServer.h:191

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors