]> git.ipfire.org Git - thirdparty/squid.git/blob - src/servers/FtpServer.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / servers / FtpServer.h
1 /*
2 * Copyright (C) 1996-2016 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 {
21 fssBegin,
22 fssConnected,
23 fssHandleFeat,
24 fssHandlePasv,
25 fssHandlePort,
26 fssHandleDataRequest,
27 fssHandleUploadRequest,
28 fssHandleEprt,
29 fssHandleEpsv,
30 fssHandleCwd,
31 fssHandlePass,
32 fssHandleCdup,
33 fssError
34 } ServerState;
35
36 // TODO: This should become a part of MasterXaction when we start sending
37 // master transactions to the clients/ code.
38 /// Transaction information shared among our FTP client and server jobs.
39 class MasterState: public RefCountable
40 {
41 public:
42 typedef RefCount<MasterState> Pointer;
43
44 MasterState(): serverState(fssBegin), clientReadGreeting(false) {}
45
46 Ip::Address clientDataAddr; ///< address of our FTP client data connection
47 SBuf workingDir; ///< estimated current working directory for URI formation
48 ServerState serverState; ///< what our FTP server is doing
49 bool clientReadGreeting; ///< whether our FTP client read their FTP server greeting
50 };
51
52 /// Manages a control connection from an FTP client.
53 class Server: public ConnStateData
54 {
55 CBDATA_CLASS(Server);
56 // XXX CBDATA_CLASS expands to nonvirtual toCbdata, AsyncJob::toCbdata
57 // is pure virtual. breaks build on clang if override is used
58
59 public:
60 explicit Server(const MasterXaction::Pointer &xact);
61 virtual ~Server();
62 /* AsyncJob API */
63 virtual void callException(const std::exception &e);
64
65 // This is a pointer in hope to minimize future changes when MasterState
66 // becomes a part of MasterXaction. Guaranteed not to be nil.
67 MasterState::Pointer master; ///< info shared among our FTP client and server jobs
68
69 protected:
70 friend void StartListening();
71
72 // errors detected before it is possible to create an HTTP request wrapper
73 enum class EarlyErrorKind {
74 HugeRequest,
75 MissingLogin,
76 MissingUsername,
77 MissingHost,
78 UnsupportedCommand,
79 InvalidUri,
80 MalformedCommand
81 };
82
83 /* ConnStateData API */
84 virtual ClientSocketContext *parseOneRequest();
85 virtual void processParsedRequest(ClientSocketContext *context);
86 virtual void notePeerConnection(Comm::ConnectionPointer conn);
87 virtual void clientPinnedConnectionClosed(const CommCloseCbParams &io);
88 virtual void handleReply(HttpReply *header, StoreIOBuffer receivedData);
89 virtual int pipelinePrefetchMax() const;
90 virtual void writeControlMsgAndCall(HttpReply *rep, AsyncCall::Pointer &call);
91 virtual time_t idleTimeout() const;
92
93 /* BodyPipe API */
94 virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer);
95 virtual void noteBodyConsumerAborted(BodyPipe::Pointer ptr);
96
97 /* AsyncJob API */
98 virtual void start();
99
100 /* Comm callbacks */
101 static void AcceptCtrlConnection(const CommAcceptCbParams &params);
102 void acceptDataConnection(const CommAcceptCbParams &params);
103 void readUploadData(const CommIoCbParams &io);
104 void wroteEarlyReply(const CommIoCbParams &io);
105 void wroteReply(const CommIoCbParams &io);
106 void wroteReplyData(const CommIoCbParams &io);
107 void connectedForData(const CommConnectCbParams &params);
108
109 unsigned int listenForDataConnection();
110 bool createDataConnection(Ip::Address cltAddr);
111 void closeDataConnection();
112
113 void calcUri(const SBuf *file);
114 void changeState(const Ftp::ServerState newState, const char *reason);
115 ClientSocketContext *handleUserRequest(const SBuf &cmd, SBuf &params);
116 bool checkDataConnPost() const;
117 void replyDataWritingCheckpoint();
118 void maybeReadUploadData();
119
120 void setReply(const int code, const char *msg);
121 void writeCustomReply(const int code, const char *msg, const HttpReply *reply = NULL);
122 void writeEarlyReply(const int code, const char *msg);
123 void writeErrorReply(const HttpReply *reply, const int status);
124 void writeForwardedForeign(const HttpReply *reply);
125 void writeForwardedReply(const HttpReply *reply);
126 void writeForwardedReplyAndCall(const HttpReply *reply, AsyncCall::Pointer &call);
127 void writeReply(MemBuf &mb);
128
129 ClientSocketContext *earlyError(const EarlyErrorKind eek);
130 bool handleRequest(HttpRequest *);
131 void setDataCommand();
132 bool checkDataConnPre();
133
134 /// a method handling an FTP command; selected by handleRequest()
135 typedef bool (Ftp::Server::*RequestHandler)(String &cmd, String &params);
136 bool handleFeatRequest(String &cmd, String &params);
137 bool handlePasvRequest(String &cmd, String &params);
138 bool handlePortRequest(String &cmd, String &params);
139 bool handleDataRequest(String &cmd, String &params);
140 bool handleUploadRequest(String &cmd, String &params);
141 bool handleEprtRequest(String &cmd, String &params);
142 bool handleEpsvRequest(String &cmd, String &params);
143 bool handleCwdRequest(String &cmd, String &params);
144 bool handlePassRequest(String &cmd, String &params);
145 bool handleCdupRequest(String &cmd, String &params);
146
147 /// a method handling an FTP response; selected by handleReply()
148 typedef void (Ftp::Server::*ReplyHandler)(const HttpReply *reply, StoreIOBuffer data);
149 void handleFeatReply(const HttpReply *header, StoreIOBuffer receivedData);
150 void handlePasvReply(const HttpReply *header, StoreIOBuffer receivedData);
151 void handlePortReply(const HttpReply *header, StoreIOBuffer receivedData);
152 void handleErrorReply(const HttpReply *header, StoreIOBuffer receivedData);
153 void handleDataReply(const HttpReply *header, StoreIOBuffer receivedData);
154 void handleUploadReply(const HttpReply *header, StoreIOBuffer receivedData);
155 void handleEprtReply(const HttpReply *header, StoreIOBuffer receivedData);
156 void handleEpsvReply(const HttpReply *header, StoreIOBuffer receivedData);
157
158 private:
159 void doProcessRequest();
160 void shovelUploadData();
161 void resetLogin(const char *reason);
162
163 SBuf uri; ///< a URI reconstructed from various FTP message details
164 SBuf host; ///< intended dest. of a transparently intercepted FTP conn
165 bool gotEpsvAll; ///< restrict data conn setup commands to just EPSV
166 AsyncCall::Pointer onDataAcceptCall; ///< who to call upon data conn acceptance
167 Comm::ConnectionPointer dataListenConn; ///< data connection listening socket
168 Comm::ConnectionPointer dataConn; ///< data connection
169 char uploadBuf[CLIENT_REQ_BUF_SZ]; ///< data connection input buffer
170 size_t uploadAvailSize; ///< number of yet unused uploadBuf bytes
171
172 AsyncCall::Pointer listener; ///< set when we are passively listening
173 AsyncCall::Pointer connector; ///< set when we are actively connecting
174 AsyncCall::Pointer reader; ///< set when we are reading FTP data
175 };
176
177 } // namespace Ftp
178
179 #endif /* SQUID_SERVERS_FTP_SERVER_H */
180