]> git.ipfire.org Git - thirdparty/squid.git/blame - src/servers/FtpServer.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / servers / FtpServer.h
CommitLineData
92ae4c86 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
bbc27441
AJ
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.
92ae4c86
AR
7 */
8
bbc27441
AJ
9/* DEBUG: section 33 Client-side Routines */
10
92ae4c86
AR
11#ifndef SQUID_SERVERS_FTP_SERVER_H
12#define SQUID_SERVERS_FTP_SERVER_H
13
2b6b1bcb 14#include "base/JobWait.h"
aea65fec 15#include "base/Lock.h"
8939f9c9 16#include "client_side.h"
2b6b1bcb 17#include "comm/forward.h"
92ae4c86 18
27c841f6
AR
19namespace Ftp
20{
92ae4c86
AR
21
22typedef enum {
23 fssBegin,
24 fssConnected,
25 fssHandleFeat,
26 fssHandlePasv,
27 fssHandlePort,
28 fssHandleDataRequest,
29 fssHandleUploadRequest,
30 fssHandleEprt,
31 fssHandleEpsv,
32 fssHandleCwd,
33 fssHandlePass,
34 fssHandleCdup,
35 fssError
36} ServerState;
37
38// TODO: This should become a part of MasterXaction when we start sending
39// master transactions to the clients/ code.
40/// Transaction information shared among our FTP client and server jobs.
aea65fec 41class MasterState: public RefCountable
92ae4c86
AR
42{
43public:
aea65fec
AR
44 typedef RefCount<MasterState> Pointer;
45
a8d1312d 46 MasterState(): serverState(fssBegin), clientReadGreeting(false), userDataDone(0) {}
aea65fec 47
92ae4c86 48 Ip::Address clientDataAddr; ///< address of our FTP client data connection
43446566 49 SBuf workingDir; ///< estimated current working directory for URI formation
92ae4c86
AR
50 ServerState serverState; ///< what our FTP server is doing
51 bool clientReadGreeting; ///< whether our FTP client read their FTP server greeting
92cfc72f
CT
52 /// Squid will send or has sent this final status code to the FTP client
53 int userDataDone;
92ae4c86
AR
54};
55
56/// Manages a control connection from an FTP client.
57class Server: public ConnStateData
58{
d836b06f 59 CBDATA_CHILD(Server);
5c2f68b7 60
92ae4c86
AR
61public:
62 explicit Server(const MasterXaction::Pointer &xact);
337b9aa4 63 ~Server() override;
d836b06f 64
0d253dfa 65 /* AsyncJob API */
337b9aa4 66 void callException(const std::exception &e) override;
92ae4c86 67
3238b9b6
CT
68 /// Called by Ftp::Client class when it is start receiving or
69 /// sending data.
70 void startWaitingForOrigin();
71
92cfc72f
CT
72 /// Called by Ftp::Client class when it is done receiving or
73 /// sending data. Waits for both agents to be done before
74 /// responding to the FTP client and closing the data connection.
3238b9b6 75 void stopWaitingForOrigin(int status);
92cfc72f 76
aea65fec
AR
77 // This is a pointer in hope to minimize future changes when MasterState
78 // becomes a part of MasterXaction. Guaranteed not to be nil.
79 MasterState::Pointer master; ///< info shared among our FTP client and server jobs
92ae4c86
AR
80
81protected:
82 friend void StartListening();
83
eacfca83 84 // errors detected before it is possible to create an HTTP request wrapper
3aab028c
FC
85 enum class EarlyErrorKind {
86 HugeRequest,
87 MissingLogin,
88 MissingUsername,
89 MissingHost,
90 UnsupportedCommand,
91 InvalidUri,
92 MalformedCommand
93 };
eacfca83 94
92ae4c86 95 /* ConnStateData API */
337b9aa4
AR
96 Http::Stream *parseOneRequest() override;
97 void processParsedRequest(Http::StreamPointer &context) override;
98 void notePeerConnection(Comm::ConnectionPointer conn) override;
99 void clientPinnedConnectionClosed(const CommCloseCbParams &io) override;
100 void handleReply(HttpReply *header, StoreIOBuffer receivedData) override;
101 int pipelinePrefetchMax() const override;
102 bool writeControlMsgAndCall(HttpReply *rep, AsyncCall::Pointer &call) override;
103 time_t idleTimeout() const override;
92ae4c86
AR
104
105 /* BodyPipe API */
337b9aa4
AR
106 void noteMoreBodySpaceAvailable(BodyPipe::Pointer) override;
107 void noteBodyConsumerAborted(BodyPipe::Pointer ptr) override;
92ae4c86
AR
108
109 /* AsyncJob API */
337b9aa4 110 void start() override;
92ae4c86
AR
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
2f8abb64 125 /// Called after data transfer on client-to-squid data connection is
92cfc72f
CT
126 /// finished.
127 void userDataCompletionCheckpoint(int finalStatusCode);
128
129 /// Writes the data-transfer status reply to the FTP client and
130 /// closes the data connection.
3238b9b6 131 void completeDataDownload();
92cfc72f 132
1ab04517 133 void calcUri(const SBuf *file);
92ae4c86 134 void changeState(const Ftp::ServerState newState, const char *reason);
d3dddfb5 135 Http::Stream *handleUserRequest(const SBuf &cmd, SBuf &params);
92ae4c86
AR
136 bool checkDataConnPost() const;
137 void replyDataWritingCheckpoint();
138 void maybeReadUploadData();
139
140 void setReply(const int code, const char *msg);
aee3523a 141 void writeCustomReply(const int code, const char *msg, const HttpReply *reply = nullptr);
92ae4c86
AR
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
d3dddfb5 149 Http::Stream *earlyError(const EarlyErrorKind eek);
eacfca83 150 bool handleRequest(HttpRequest *);
92ae4c86
AR
151 void setDataCommand();
152 bool checkDataConnPre();
153
154 /// a method handling an FTP command; selected by handleRequest()
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
167 /// a method handling an FTP response; selected by handleReply()
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
178private:
179 void doProcessRequest();
180 void shovelUploadData();
e7ce227f 181 void resetLogin(const char *reason);
92ae4c86 182
1ab04517
AR
183 SBuf uri; ///< a URI reconstructed from various FTP message details
184 SBuf host; ///< intended dest. of a transparently intercepted FTP conn
92ae4c86
AR
185 bool gotEpsvAll; ///< restrict data conn setup commands to just EPSV
186 AsyncCall::Pointer onDataAcceptCall; ///< who to call upon data conn acceptance
187 Comm::ConnectionPointer dataListenConn; ///< data connection listening socket
188 Comm::ConnectionPointer dataConn; ///< data connection
189 char uploadBuf[CLIENT_REQ_BUF_SZ]; ///< data connection input buffer
190 size_t uploadAvailSize; ///< number of yet unused uploadBuf bytes
191
192 AsyncCall::Pointer listener; ///< set when we are passively listening
2b6b1bcb
AR
193
194 /// Waits for an FTP data connection to the client to be established/opened.
195 /// This wait only happens in FTP active mode (via PORT or EPRT).
196 JobWait<Comm::ConnOpener> dataConnWait;
197
92ae4c86 198 AsyncCall::Pointer reader; ///< set when we are reading FTP data
3238b9b6
CT
199
200 /// whether we wait for the origin data transfer to end
201 bool waitingForOrigin;
202 /// whether the origin data transfer aborted
203 bool originDataDownloadAbortedOnError;
a8d1312d 204
3238b9b6
CT
205 /// a response which writing was postponed until stopWaitingForOrigin()
206 HttpReply::Pointer delayedReply;
92ae4c86
AR
207};
208
209} // namespace Ftp
210
211#endif /* SQUID_SERVERS_FTP_SERVER_H */
f53969cc 212