FtpServer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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/JobWait.h"
15 #include "base/Lock.h"
16 #include "client_side.h"
17 #include "comm/forward.h"
18 
19 namespace Ftp
20 {
21 
22 typedef enum {
35  fssError
37 
38 // TODO: This should become a part of MasterXaction when we start sending
39 // master transactions to the clients/ code.
42 {
43 public:
45 
47 
52  int userDataDone;
54 };
55 
57 class Server: public ConnStateData
58 {
60 
61 public:
62  explicit Server(const MasterXaction::Pointer &xact);
63  virtual ~Server() override;
64 
65  /* AsyncJob API */
66  virtual void callException(const std::exception &e) override;
67 
70  void startWaitingForOrigin();
71 
75  void stopWaitingForOrigin(int status);
76 
77  // This is a pointer in hope to minimize future changes when MasterState
78  // becomes a part of MasterXaction. Guaranteed not to be nil.
80 
81 protected:
82  friend void StartListening();
83 
84  // errors detected before it is possible to create an HTTP request wrapper
85  enum class EarlyErrorKind {
91  InvalidUri,
93  };
94 
95  /* ConnStateData API */
96  virtual Http::Stream *parseOneRequest() override;
97  virtual void processParsedRequest(Http::StreamPointer &context) override;
98  virtual void notePeerConnection(Comm::ConnectionPointer conn) override;
99  virtual void clientPinnedConnectionClosed(const CommCloseCbParams &io) override;
100  virtual void handleReply(HttpReply *header, StoreIOBuffer receivedData) override;
101  virtual int pipelinePrefetchMax() const override;
102  virtual bool writeControlMsgAndCall(HttpReply *rep, AsyncCall::Pointer &call) override;
103  virtual time_t idleTimeout() const override;
104 
105  /* BodyPipe API */
106  virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer) override;
107  virtual void noteBodyConsumerAborted(BodyPipe::Pointer ptr) override;
108 
109  /* AsyncJob API */
110  virtual void start() override;
111 
112  /* Comm callbacks */
113  static void AcceptCtrlConnection(const CommAcceptCbParams &params);
114  void acceptDataConnection(const CommAcceptCbParams &params);
115  void readUploadData(const CommIoCbParams &io);
116  void wroteEarlyReply(const CommIoCbParams &io);
117  void wroteReply(const CommIoCbParams &io);
118  void wroteReplyData(const CommIoCbParams &io);
119  void connectedForData(const CommConnectCbParams &params);
120 
121  unsigned int listenForDataConnection();
122  bool createDataConnection(Ip::Address cltAddr);
123  void closeDataConnection();
124 
127  void userDataCompletionCheckpoint(int finalStatusCode);
128 
131  void completeDataDownload();
132 
133  void calcUri(const SBuf *file);
134  void changeState(const Ftp::ServerState newState, const char *reason);
135  Http::Stream *handleUserRequest(const SBuf &cmd, SBuf &params);
136  bool checkDataConnPost() const;
138  void maybeReadUploadData();
139 
140  void setReply(const int code, const char *msg);
141  void writeCustomReply(const int code, const char *msg, const HttpReply *reply = NULL);
142  void writeEarlyReply(const int code, const char *msg);
143  void writeErrorReply(const HttpReply *reply, const int status);
144  void writeForwardedForeign(const HttpReply *reply);
145  void writeForwardedReply(const HttpReply *reply);
146  void writeForwardedReplyAndCall(const HttpReply *reply, AsyncCall::Pointer &call);
147  void writeReply(MemBuf &mb);
148 
150  bool handleRequest(HttpRequest *);
151  void setDataCommand();
152  bool checkDataConnPre();
153 
155  typedef bool (Ftp::Server::*RequestHandler)(String &cmd, String &params);
156  bool handleFeatRequest(String &cmd, String &params);
157  bool handlePasvRequest(String &cmd, String &params);
158  bool handlePortRequest(String &cmd, String &params);
159  bool handleDataRequest(String &cmd, String &params);
160  bool handleUploadRequest(String &cmd, String &params);
161  bool handleEprtRequest(String &cmd, String &params);
162  bool handleEpsvRequest(String &cmd, String &params);
163  bool handleCwdRequest(String &cmd, String &params);
164  bool handlePassRequest(String &cmd, String &params);
165  bool handleCdupRequest(String &cmd, String &params);
166 
168  typedef void (Ftp::Server::*ReplyHandler)(const HttpReply *reply, StoreIOBuffer data);
169  void handleFeatReply(const HttpReply *header, StoreIOBuffer receivedData);
170  void handlePasvReply(const HttpReply *header, StoreIOBuffer receivedData);
171  void handlePortReply(const HttpReply *header, StoreIOBuffer receivedData);
172  void handleErrorReply(const HttpReply *header, StoreIOBuffer receivedData);
173  void handleDataReply(const HttpReply *header, StoreIOBuffer receivedData);
174  void handleUploadReply(const HttpReply *header, StoreIOBuffer receivedData);
175  void handleEprtReply(const HttpReply *header, StoreIOBuffer receivedData);
176  void handleEpsvReply(const HttpReply *header, StoreIOBuffer receivedData);
177 
178 private:
179  void doProcessRequest();
180  void shovelUploadData();
181  void resetLogin(const char *reason);
182 
185  bool gotEpsvAll;
191 
193 
197 
199 
204 
207 };
208 
209 } // namespace Ftp
210 
211 #endif /* SQUID_SERVERS_FTP_SERVER_H */
212 
bool gotEpsvAll
restrict data conn setup commands to just EPSV
Definition: FtpServer.h:185
bool handleEprtRequest(String &cmd, String &params)
Definition: FtpServer.cc:1562
@ fssHandleCdup
Definition: FtpServer.h:34
SBuf host
intended dest. of a transparently intercepted FTP conn
Definition: FtpServer.h:184
void wroteEarlyReply(const CommIoCbParams &io)
Definition: FtpServer.cc:1229
Http::Stream * handleUserRequest(const SBuf &cmd, SBuf &params)
Definition: FtpServer.cc:1353
void completeDataDownload()
Definition: FtpServer.cc:1830
CBDATA_CHILD(Server)
Comm::ConnectionPointer dataListenConn
data connection listening socket
Definition: FtpServer.h:187
AsyncCall::Pointer listener
set when we are passively listening
Definition: FtpServer.h:192
void handleDataReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:938
void(Ftp::Server::* ReplyHandler)(const HttpReply *reply, StoreIOBuffer data)
a method handling an FTP response; selected by handleReply()
Definition: FtpServer.h:168
int userDataDone
Squid will send or has sent this final status code to the FTP client.
Definition: FtpServer.h:53
Transaction information shared among our FTP client and server jobs.
Definition: FtpServer.h:42
@ fssHandleUploadRequest
Definition: FtpServer.h:29
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:23
virtual int pipelinePrefetchMax() const override
returning N allows a pipeline of 1+N requests (see pipeline_prefetch)
Definition: FtpServer.cc:80
@ fssHandleCwd
Definition: FtpServer.h:32
Definition: SBuf.h:94
bool handlePortRequest(String &cmd, String &params)
Definition: FtpServer.cc:1486
void closeDataConnection()
Definition: FtpServer.cc:439
void writeForwardedForeign(const HttpReply *reply)
Definition: FtpServer.cc:1138
bool handleCwdRequest(String &cmd, String &params)
Definition: FtpServer.cc:1616
JobWait< Comm::ConnOpener > dataConnWait
Definition: FtpServer.h:196
bool handleFeatRequest(String &cmd, String &params)
Definition: FtpServer.cc:1401
void wroteReplyData(const CommIoCbParams &io)
called when we are done writing a chunk of the response data
Definition: FtpServer.cc:981
void doProcessRequest()
react to the freshly parsed request
Definition: FtpServer.cc:128
void handleFeatReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:807
bool clientReadGreeting
Definition: FtpServer.h:51
void writeForwardedReply(const HttpReply *reply)
Definition: FtpServer.cc:1031
friend void StartListening()
accept connections on all configured ftp_ports
char uploadBuf[CLIENT_REQ_BUF_SZ]
data connection input buffer
Definition: FtpServer.h:189
ServerState
Definition: FtpServer.h:22
bool handlePasvRequest(String &cmd, String &params)
Definition: FtpServer.cc:1408
size_t uploadAvailSize
number of yet unused uploadBuf bytes
Definition: FtpServer.h:190
void handleEprtReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1054
void wroteReply(const CommIoCbParams &io)
Definition: FtpServer.cc:1251
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:49
Definition: forward.h:28
virtual 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:1427
Server(const MasterXaction::Pointer &xact)
Definition: FtpServer.cc:53
bool checkDataConnPre()
Definition: FtpServer.cc:1656
virtual 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:79
void setReply(const int code, const char *msg)
Definition: FtpServer.cc:1724
bool handleDataRequest(String &cmd, String &params)
Definition: FtpServer.cc:1515
void replyDataWritingCheckpoint()
ClientStream checks after (actual or skipped) reply data writing.
Definition: FtpServer.cc:999
void writeErrorReply(const HttpReply *reply, const int status)
writes FTP error response with given status and reply-derived error details
Definition: FtpServer.cc:1090
@ fssConnected
Definition: FtpServer.h:24
void setDataCommand()
Definition: FtpServer.cc:1639
#define NULL
Definition: types.h:166
AsyncCall::Pointer onDataAcceptCall
who to call upon data conn acceptance
Definition: FtpServer.h:186
void calcUri(const SBuf *file)
computes uri member from host and, if tracked, working dir with file name
Definition: FtpServer.cc:339
void writeReply(MemBuf &mb)
Definition: FtpServer.cc:489
virtual void notePeerConnection(Comm::ConnectionPointer conn) override
called just before a FwdState-dispatched job starts using connection
Definition: FtpServer.cc:296
#define CLIENT_REQ_BUF_SZ
Definition: defines.h:88
virtual void handleReply(HttpReply *header, StoreIOBuffer receivedData) override
Definition: FtpServer.cc:773
Definition: MemBuf.h:24
void acceptDataConnection(const CommAcceptCbParams &params)
Definition: FtpServer.cc:395
bool handleCdupRequest(String &cmd, String &params)
Definition: FtpServer.cc:1630
unsigned char code
Definition: html_quote.c:20
@ fssHandlePort
Definition: FtpServer.h:27
void maybeReadUploadData()
schedules another data connection read if needed
Definition: FtpServer.cc:109
void handleUploadReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1024
Http::Stream * earlyError(const EarlyErrorKind eek)
creates a context filled with an error message for a given early error
Definition: FtpServer.cc:564
void writeCustomReply(const int code, const char *msg, const HttpReply *reply=NULL)
Definition: FtpServer.cc:501
virtual void noteBodyConsumerAborted(BodyPipe::Pointer ptr) override
Definition: FtpServer.cc:231
void stopWaitingForOrigin(int status)
Definition: FtpServer.cc:1767
void userDataCompletionCheckpoint(int finalStatusCode)
Definition: FtpServer.cc:1805
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:1746
@ fssHandlePasv
Definition: FtpServer.h:26
Comm::ConnectionPointer dataConn
data connection
Definition: FtpServer.h:188
Manages a control connection from an FTP client.
Definition: FtpServer.h:58
virtual ~Server() override
Definition: FtpServer.cc:74
virtual const char * status() const
internal cleanup; do not call directly
Definition: AsyncJob.cc:167
@ fssHandleEpsv
Definition: FtpServer.h:31
@ fssHandlePass
Definition: FtpServer.h:33
unsigned int listenForDataConnection()
Definition: FtpServer.cc:364
@ fssError
Definition: FtpServer.h:35
void handlePasvReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:876
RefCount< MasterState > Pointer
Definition: FtpServer.h:44
bool handlePassRequest(String &cmd, String &params)
Definition: FtpServer.cc:1623
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:198
Ip::Address clientDataAddr
address of our FTP client data connection
Definition: FtpServer.h:48
ServerState serverState
what our FTP server is doing
Definition: FtpServer.h:50
@ fssHandleEprt
Definition: FtpServer.h:30
bool checkDataConnPost() const
Check that client data connection is ready for immediate I/O.
Definition: FtpServer.cc:1687
void writeEarlyReply(const int code, const char *msg)
Definition: FtpServer.cc:470
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:1147
virtual 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:1156
void handleErrorReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:929
void handleEpsvReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:1067
@ fssHandleDataRequest
Definition: FtpServer.h:28
virtual void clientPinnedConnectionClosed(const CommCloseCbParams &io) override
Our close handler called by Comm when the pinned connection is closed.
Definition: FtpServer.cc:310
bool handleUploadRequest(String &cmd, String &params)
Definition: FtpServer.cc:1529
void changeState(const Ftp::ServerState newState, const char *reason)
Definition: FtpServer.cc:523
virtual Http::Stream * parseOneRequest() override
Definition: FtpServer.cc:634
bool originDataDownloadAbortedOnError
whether the origin data transfer aborted
Definition: FtpServer.h:203
#define false
Definition: GnuRegex.c:233
void connectedForData(const CommConnectCbParams &params)
Done establishing a data connection to the user.
Definition: FtpServer.cc:1698
virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer) override
Definition: FtpServer.cc:222
bool waitingForOrigin
whether we wait for the origin data transfer to end
Definition: FtpServer.h:201
void resetLogin(const char *reason)
clear client and server login-related state after the old login is gone
Definition: FtpServer.cc:330
void startWaitingForOrigin()
Definition: FtpServer.cc:1757
bool handleEpsvRequest(String &cmd, String &params)
Definition: FtpServer.cc:1591
bool(Ftp::Server::* RequestHandler)(String &cmd, String &params)
a method handling an FTP command; selected by handleRequest()
Definition: FtpServer.h:155
@ fssHandleFeat
Definition: FtpServer.h:25
SBuf uri
a URI reconstructed from various FTP message details
Definition: FtpServer.h:183
void handlePortReply(const HttpReply *header, StoreIOBuffer receivedData)
Definition: FtpServer.cc:916
HttpReply::Pointer delayedReply
a response which writing was postponed until stopWaitingForOrigin()
Definition: FtpServer.h:206
bool handleRequest(HttpRequest *)
Definition: FtpServer.cc:1294

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors