]>
Commit | Line | Data |
---|---|---|
434a79b0 | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 The Squid Software Foundation and contributors |
434a79b0 | 3 | * |
bbc27441 AJ |
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. | |
434a79b0 DK |
7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 09 File Transfer Protocol (FTP) */ |
10 | ||
5517260a AR |
11 | #ifndef SQUID_FTP_CLIENT_H |
12 | #define SQUID_FTP_CLIENT_H | |
434a79b0 DK |
13 | |
14 | #include "Server.h" | |
15 | ||
54a6c0cd | 16 | class String; |
27c841f6 AR |
17 | namespace Ftp |
18 | { | |
434a79b0 DK |
19 | |
20 | extern const char *const crlf; | |
21 | ||
e7ce227f AR |
22 | /// Common code for FTP server control and data channels. |
23 | /// Does not own the channel descriptor, which is managed by Ftp::Client. | |
5517260a | 24 | class Channel |
434a79b0 DK |
25 | { |
26 | public: | |
27 | /// called after the socket is opened, sets up close handler | |
28 | void opened(const Comm::ConnectionPointer &conn, const AsyncCall::Pointer &aCloser); | |
29 | ||
30 | /** Handles all operations needed to properly close the active channel FD. | |
31 | * clearing the close handler, clearing the listen socket properly, and calling comm_close | |
32 | */ | |
33 | void close(); | |
34 | ||
35 | void forget(); /// remove the close handler, leave connection open | |
36 | ||
37 | void clear(); ///< just drops conn and close handler. does not close active connections. | |
38 | ||
39 | Comm::ConnectionPointer conn; ///< channel descriptor | |
40 | ||
41 | /** A temporary handle to the connection being listened on. | |
42 | * Closing this will also close the waiting Data channel acceptor. | |
43 | * If a data connection has already been accepted but is still waiting in the event queue | |
44 | * the callback will still happen and needs to be handled (usually dropped). | |
45 | */ | |
46 | Comm::ConnectionPointer listenConn; | |
47 | ||
48 | AsyncCall::Pointer opener; ///< Comm opener handler callback. | |
49 | private: | |
50 | AsyncCall::Pointer closer; ///< Comm close handler callback | |
51 | }; | |
52 | ||
e7ce227f AR |
53 | /// FTP channel for control commands. |
54 | /// This channel is opened once per transaction. | |
55 | class CtrlChannel: public Ftp::Channel | |
56 | { | |
57 | public: | |
58 | CtrlChannel(); | |
59 | ~CtrlChannel(); | |
60 | ||
61 | char *buf; | |
62 | size_t size; | |
63 | size_t offset; | |
64 | wordlist *message; | |
65 | char *last_command; | |
66 | char *last_reply; | |
67 | int replycode; | |
68 | ||
69 | private: | |
70 | CtrlChannel(const CtrlChannel &); // not implemented | |
71 | CtrlChannel &operator =(const CtrlChannel &); // not implemented | |
72 | }; | |
73 | ||
74 | /// FTP channel for data exchanges. | |
75 | /// This channel may be opened/closed a few times. | |
76 | class DataChannel: public Ftp::Channel | |
77 | { | |
78 | public: | |
79 | DataChannel(); | |
80 | ~DataChannel(); | |
81 | ||
82 | void addr(const Ip::Address &addr); ///< import host and port | |
83 | ||
84 | public: | |
85 | MemBuf *readBuf; | |
86 | char *host; | |
87 | unsigned short port; | |
88 | bool read_pending; | |
89 | }; | |
90 | ||
43446566 | 91 | /// FTP client functionality shared among FTP Gateway and Relay clients. |
5517260a | 92 | class Client: public ::ServerStateData |
434a79b0 DK |
93 | { |
94 | public: | |
5517260a AR |
95 | explicit Client(FwdState *fwdState); |
96 | virtual ~Client(); | |
434a79b0 | 97 | |
5517260a | 98 | /// handle a fatal transaction error, closing the control connection |
434a79b0 | 99 | virtual void failed(err_type error = ERR_NONE, int xerrno = 0); |
5517260a AR |
100 | |
101 | /// read timeout handler | |
434a79b0 | 102 | virtual void timeout(const CommTimeoutCbParams &io); |
5517260a AR |
103 | |
104 | /* ServerStateData API */ | |
105 | virtual void maybeReadVirginBody(); | |
106 | ||
434a79b0 | 107 | void writeCommand(const char *buf); |
73950ceb AR |
108 | |
109 | /// extracts remoteAddr from PASV response, validates it, | |
110 | /// sets data address details, and returns true on success | |
111 | bool handlePasvReply(Ip::Address &remoteAddr); | |
000e664b AR |
112 | bool handleEpsvReply(Ip::Address &remoteAddr); |
113 | ||
114 | bool sendEprt(); | |
115 | bool sendPort(); | |
116 | bool sendPassive(); | |
434a79b0 | 117 | void connectDataChannel(); |
000e664b | 118 | bool openListenSocket(); |
434a79b0 DK |
119 | void switchTimeoutToDataChannel(); |
120 | ||
e7ce227f AR |
121 | CtrlChannel ctrl; ///< FTP control channel state |
122 | DataChannel data; ///< FTP data channel state | |
434a79b0 | 123 | |
000e664b AR |
124 | enum { |
125 | BEGIN, | |
126 | SENT_USER, | |
127 | SENT_PASS, | |
128 | SENT_TYPE, | |
129 | SENT_MDTM, | |
130 | SENT_SIZE, | |
131 | SENT_EPRT, | |
132 | SENT_PORT, | |
133 | SENT_EPSV_ALL, | |
134 | SENT_EPSV_1, | |
135 | SENT_EPSV_2, | |
136 | SENT_PASV, | |
137 | SENT_CWD, | |
138 | SENT_LIST, | |
139 | SENT_NLST, | |
140 | SENT_REST, | |
141 | SENT_RETR, | |
142 | SENT_STOR, | |
143 | SENT_QUIT, | |
144 | READING_DATA, | |
145 | WRITING_DATA, | |
146 | SENT_MKDIR, | |
147 | SENT_FEAT, | |
54a6c0cd CT |
148 | SENT_PWD, |
149 | SENT_CDUP, | |
000e664b AR |
150 | SENT_DATA_REQUEST, // LIST, NLST or RETR requests.. |
151 | SENT_COMMAND, // General command | |
152 | END | |
153 | } ftp_state_t; | |
154 | ||
434a79b0 DK |
155 | int state; |
156 | char *old_request; | |
157 | char *old_reply; | |
158 | ||
159 | protected: | |
5517260a | 160 | /* AsyncJob API */ |
434a79b0 DK |
161 | virtual void start(); |
162 | ||
5517260a | 163 | /* ServerStateData API */ |
434a79b0 DK |
164 | virtual void closeServer(); |
165 | virtual bool doneWithServer() const; | |
5517260a AR |
166 | virtual const Comm::ConnectionPointer & dataConnection() const; |
167 | virtual void abortTransaction(const char *reason); | |
168 | ||
434a79b0 DK |
169 | virtual Http::StatusCode failedHttpStatus(err_type &error); |
170 | void ctrlClosed(const CommCloseCbParams &io); | |
171 | void scheduleReadControlReply(int buffered_ok); | |
172 | void readControlReply(const CommIoCbParams &io); | |
173 | virtual void handleControlReply(); | |
174 | void writeCommandCallback(const CommIoCbParams &io); | |
43446566 | 175 | virtual void dataChannelConnected(const CommConnectCbParams &io) = 0; |
434a79b0 DK |
176 | void dataRead(const CommIoCbParams &io); |
177 | void dataComplete(); | |
178 | AsyncCall::Pointer dataCloser(); | |
179 | virtual void dataClosed(const CommCloseCbParams &io); | |
5517260a | 180 | void initReadBuf(); |
434a79b0 DK |
181 | |
182 | // sending of the request body to the server | |
183 | virtual void sentRequestBody(const CommIoCbParams &io); | |
184 | virtual void doneSendingRequestBody(); | |
185 | ||
186 | private: | |
a2c7f09a | 187 | bool parseControlReply(size_t &bytesUsed); |
434a79b0 | 188 | |
e7ce227f AR |
189 | /// XXX: An old hack for FTP servers like ftp.netscape.com that may not |
190 | /// respond to PASV. Use faster connect timeout instead of read timeout. | |
191 | bool shortenReadTimeout; | |
192 | ||
5517260a | 193 | CBDATA_CLASS2(Client); |
434a79b0 DK |
194 | }; |
195 | ||
92ae4c86 | 196 | } // namespace Ftp |
434a79b0 | 197 | |
5517260a | 198 | #endif /* SQUID_FTP_CLIENT_H */ |