]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
262a0e14 | 2 | * $Id$ |
30a4f2a8 | 3 | * |
4 | * DEBUG: section 9 File Transfer Protocol (FTP) | |
5 | * AUTHOR: Harvest Derived | |
6 | * | |
2b6662ba | 7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 8 | * ---------------------------------------------------------- |
30a4f2a8 | 9 | * |
2b6662ba | 10 | * Squid is the result of efforts by numerous individuals from |
11 | * the Internet community; see the CONTRIBUTORS file for full | |
12 | * details. Many organizations have provided support for Squid's | |
13 | * development; see the SPONSORS file for full details. Squid is | |
14 | * Copyrighted (C) 2001 by the Regents of the University of | |
15 | * California; see the COPYRIGHT file for full details. Squid | |
16 | * incorporates software developed and/or copyrighted by other | |
17 | * sources; see the CREDITS file for full details. | |
30a4f2a8 | 18 | * |
19 | * This program is free software; you can redistribute it and/or modify | |
20 | * it under the terms of the GNU General Public License as published by | |
21 | * the Free Software Foundation; either version 2 of the License, or | |
22 | * (at your option) any later version. | |
9e008dda | 23 | * |
30a4f2a8 | 24 | * This program is distributed in the hope that it will be useful, |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
9e008dda | 28 | * |
30a4f2a8 | 29 | * You should have received a copy of the GNU General Public License |
30 | * along with this program; if not, write to the Free Software | |
cbdec147 | 31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 32 | * |
30a4f2a8 | 33 | */ |
019dd986 | 34 | |
44a47c6e | 35 | #include "squid.h" |
04f55905 AJ |
36 | #include "comm.h" |
37 | #include "comm/ListenStateData.h" | |
27bc2077 | 38 | #include "compat/strtoll.h" |
04f55905 | 39 | #include "ConnectionDetail.h" |
aa839030 | 40 | #include "errorpage.h" |
528b2c61 | 41 | #include "fde.h" |
04f55905 | 42 | #include "forward.h" |
528b2c61 | 43 | #include "HttpHdrContRange.h" |
04f55905 | 44 | #include "HttpHeaderRange.h" |
528b2c61 | 45 | #include "HttpHeader.h" |
04f55905 AJ |
46 | #include "HttpRequest.h" |
47 | #include "HttpReply.h" | |
48 | #include "MemBuf.h" | |
056b24a1 | 49 | #include "rfc1738.h" |
04f55905 | 50 | #include "Server.h" |
056b24a1 | 51 | #include "SquidString.h" |
04f55905 AJ |
52 | #include "SquidTime.h" |
53 | #include "Store.h" | |
54 | #include "URLScheme.h" | |
55 | #include "wordlist.h" | |
56 | ||
b67e2c8c | 57 | #if DELAY_POOLS |
58 | #include "DelayPools.h" | |
86a2f789 | 59 | #include "MemObject.h" |
b67e2c8c | 60 | #endif |
253caccb | 61 | |
63be0a78 | 62 | /** |
63 | \defgroup ServerProtocolFTPInternal Server-Side FTP Internals | |
64 | \ingroup ServerProtocolFTPAPI | |
65 | */ | |
66 | ||
67 | /// \ingroup ServerProtocolFTPInternal | |
3fdadc70 | 68 | static const char *const crlf = "\r\n"; |
63be0a78 | 69 | |
70 | /// \ingroup ServerProtocolFTPInternal | |
3fdadc70 | 71 | static char cbuf[1024]; |
72 | ||
63be0a78 | 73 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 74 | typedef enum { |
75 | BEGIN, | |
76 | SENT_USER, | |
77 | SENT_PASS, | |
78 | SENT_TYPE, | |
79 | SENT_MDTM, | |
80 | SENT_SIZE, | |
cc192b50 | 81 | SENT_EPRT, |
3fdadc70 | 82 | SENT_PORT, |
a689bd4e | 83 | SENT_EPSV_ALL, |
84 | SENT_EPSV_1, | |
85 | SENT_EPSV_2, | |
3fdadc70 | 86 | SENT_PASV, |
87 | SENT_CWD, | |
88 | SENT_LIST, | |
89 | SENT_NLST, | |
90 | SENT_REST, | |
91 | SENT_RETR, | |
54220df8 | 92 | SENT_STOR, |
3fdadc70 | 93 | SENT_QUIT, |
54220df8 | 94 | READING_DATA, |
95 | WRITING_DATA, | |
96 | SENT_MKDIR | |
3fdadc70 | 97 | } ftp_state_t; |
090089c4 | 98 | |
63be0a78 | 99 | /// \ingroup ServerProtocolFTPInternal |
9e008dda | 100 | struct _ftp_flags { |
4e2da309 AJ |
101 | |
102 | /* passive mode */ | |
103 | bool pasv_supported; ///< PASV command is allowed | |
104 | bool epsv_all_sent; ///< EPSV ALL has been used. Must abort on failures. | |
105 | bool pasv_only; | |
106 | ||
107 | /* authentication */ | |
108 | bool authenticated; ///< authentication success | |
109 | bool tried_auth_anonymous; ///< auth has tried to use anonymous credentials already. | |
110 | bool tried_auth_nopass; ///< auth tried username with no password already. | |
111 | ||
112 | /* other */ | |
3977372c | 113 | bool isdir; |
3977372c | 114 | bool skip_whitespace; |
115 | bool rest_supported; | |
3977372c | 116 | bool http_header_sent; |
117 | bool tried_nlst; | |
118 | bool need_base_href; | |
119 | bool dir_slash; | |
120 | bool root_dir; | |
121 | bool no_dotdot; | |
3977372c | 122 | bool binary; |
123 | bool try_slash_hack; | |
124 | bool put; | |
125 | bool put_mkdir; | |
126 | bool listformat_unknown; | |
0477a072 | 127 | bool listing; |
5f8252d2 | 128 | bool completed_forwarding; |
e55f0142 | 129 | }; |
130 | ||
6f0aab86 | 131 | class FtpStateData; |
63be0a78 | 132 | |
133 | /// \ingroup ServerProtocolFTPInternal | |
6f0aab86 | 134 | typedef void (FTPSM) (FtpStateData *); |
135 | ||
94b88585 | 136 | /// common code for FTP control and data channels |
6e92b048 | 137 | // does not own the channel descriptor, which is managed by FtpStateData |
9e008dda AJ |
138 | class FtpChannel |
139 | { | |
94b88585 AR |
140 | public: |
141 | FtpChannel(): fd(-1) {} | |
142 | ||
143 | /// called after the socket is opened, sets up close handler | |
144 | void opened(int aFd, const AsyncCall::Pointer &aCloser); | |
145 | ||
04f55905 AJ |
146 | /** Handles all operations needed to properly close the active channel FD. |
147 | * clearing the close handler, clearing the listen socket properly, and calling comm_close | |
148 | */ | |
149 | void close(); | |
150 | ||
151 | void clear(); /// just resets fd and close handler. does not close active connections. | |
94b88585 AR |
152 | |
153 | int fd; /// channel descriptor; \todo: remove because the closer has it | |
154 | ||
04f55905 AJ |
155 | /** Current listening socket handler. delete on shutdown or abort. |
156 | * FTP stores a copy of the FD in the field fd above. | |
157 | * Use close() to properly close the channel. | |
158 | */ | |
159 | Comm::ListenStateData *listener; | |
160 | ||
94b88585 AR |
161 | private: |
162 | AsyncCall::Pointer closer; /// Comm close handler callback | |
163 | }; | |
164 | ||
63be0a78 | 165 | /// \ingroup ServerProtocolFTPInternal |
253caccb | 166 | class FtpStateData : public ServerStateData |
62e76326 | 167 | { |
0353e724 | 168 | |
169 | public: | |
170 | void *operator new (size_t); | |
171 | void operator delete (void *); | |
dc56a9b1 | 172 | void *toCbdata() { return this; } |
173 | ||
6f0aab86 | 174 | FtpStateData(FwdState *); |
0353e724 | 175 | ~FtpStateData(); |
77a30ebb | 176 | char user[MAX_URL]; |
177 | char password[MAX_URL]; | |
9bc73deb | 178 | int password_url; |
f0afe435 | 179 | char *reply_hdr; |
f0afe435 | 180 | int reply_hdr_state; |
30abd221 | 181 | String clean_url; |
182 | String title_url; | |
183 | String base_href; | |
3fdadc70 | 184 | int conn_att; |
185 | int login_att; | |
186 | ftp_state_t state; | |
3fdadc70 | 187 | time_t mdtm; |
47f6e231 | 188 | int64_t theSize; |
3fdadc70 | 189 | wordlist *pathcomps; |
190 | char *filepath; | |
5627a633 | 191 | char *dirpath; |
47f6e231 | 192 | int64_t restart_offset; |
3fdadc70 | 193 | char *proxy_host; |
194 | size_t list_width; | |
0477a072 | 195 | String cwd_message; |
969c39b9 | 196 | char *old_request; |
197 | char *old_reply; | |
0f169992 | 198 | char *old_filepath; |
9e242e02 | 199 | char typecode; |
0477a072 | 200 | MemBuf listing; ///< FTP directory listing in HTML format. |
62e76326 | 201 | |
94b88585 AR |
202 | // \todo: optimize ctrl and data structs member order, to minimize size |
203 | /// FTP control channel info; the channel is opened once per transaction | |
9e008dda | 204 | struct CtrlChannel: public FtpChannel { |
62e76326 | 205 | char *buf; |
206 | size_t size; | |
47f6e231 | 207 | size_t offset; |
62e76326 | 208 | wordlist *message; |
209 | char *last_command; | |
210 | char *last_reply; | |
211 | int replycode; | |
2fadd50d | 212 | } ctrl; |
62e76326 | 213 | |
94b88585 | 214 | /// FTP data channel info; the channel may be opened/closed a few times |
9e008dda | 215 | struct DataChannel: public FtpChannel { |
253caccb | 216 | MemBuf *readBuf; |
62e76326 | 217 | char *host; |
218 | u_short port; | |
253caccb | 219 | bool read_pending; |
2fadd50d | 220 | } data; |
62e76326 | 221 | |
e55f0142 | 222 | struct _ftp_flags flags; |
0353e724 | 223 | |
224 | private: | |
225 | CBDATA_CLASS(FtpStateData); | |
6f0aab86 | 226 | |
227 | public: | |
253caccb | 228 | // these should all be private |
6f0aab86 | 229 | void start(); |
230 | void loginParser(const char *, int escaped); | |
231 | int restartable(); | |
232 | void appendSuccessHeader(); | |
233 | void hackShortcut(FTPSM * nextState); | |
234 | void failed(err_type, int xerrno); | |
235 | void failedErrorMessage(err_type, int xerrno); | |
236 | void unhack(); | |
6f0aab86 | 237 | void scheduleReadControlReply(int); |
238 | void handleControlReply(); | |
5f8252d2 | 239 | void readStor(); |
f1a83a7d | 240 | void parseListing(); |
0477a072 AJ |
241 | MemBuf *htmlifyListEntry(const char *line); |
242 | void completedListing(void); | |
f1a83a7d | 243 | void dataComplete(); |
dc56a9b1 | 244 | void dataRead(const CommIoCbParams &io); |
f1a83a7d | 245 | int checkAuth(const HttpHeader * req_hdr); |
246 | void checkUrlpath(); | |
247 | void buildTitleUrl(); | |
47f6e231 | 248 | void writeReplyBody(const char *, size_t len); |
253caccb | 249 | void printfReplyBody(const char *fmt, ...); |
5f8252d2 | 250 | virtual int dataDescriptor() const; |
251 | virtual void maybeReadVirginBody(); | |
252 | virtual void closeServer(); | |
253 | virtual void completeForwarding(); | |
254 | virtual void abortTransaction(const char *reason); | |
063a47b5 | 255 | void processHeadResponse(); |
253caccb | 256 | void processReplyBody(); |
9317b41a | 257 | void writeCommand(const char *buf); |
9fa2e208 CT |
258 | void setCurrentOffset(int64_t offset) { currentOffset = offset; } |
259 | int64_t getCurrentOffset() const { return currentOffset; } | |
6f0aab86 | 260 | |
6f0aab86 | 261 | static CNCB ftpPasvCallback; |
6f0aab86 | 262 | static PF ftpDataWrite; |
dc56a9b1 | 263 | void ftpTimeout(const CommTimeoutCbParams &io); |
94b88585 AR |
264 | void ctrlClosed(const CommCloseCbParams &io); |
265 | void dataClosed(const CommCloseCbParams &io); | |
dc56a9b1 | 266 | void ftpReadControlReply(const CommIoCbParams &io); |
267 | void ftpWriteCommandCallback(const CommIoCbParams &io); | |
268 | void ftpAcceptDataConnection(const CommAcceptCbParams &io); | |
269 | ||
6f0aab86 | 270 | static HttpReply *ftpAuthRequired(HttpRequest * request, const char *realm); |
f381d699 AJ |
271 | const char *ftpRealm(void); |
272 | void loginFailed(void); | |
47f6e231 | 273 | static wordlist *ftpParseControlReply(char *, size_t, int *, size_t *); |
253caccb | 274 | |
5f8252d2 | 275 | // sending of the request body to the server |
dc56a9b1 | 276 | virtual void sentRequestBody(const CommIoCbParams&); |
5f8252d2 | 277 | virtual void doneSendingRequestBody(); |
278 | ||
063a47b5 | 279 | virtual void haveParsedReplyHeaders(); |
280 | ||
5f8252d2 | 281 | virtual bool doneWithServer() const; |
aa190430 | 282 | virtual bool haveControlChannel(const char *caller_name) const; |
94b88585 | 283 | AsyncCall::Pointer dataCloser(); /// creates a Comm close callback |
253caccb | 284 | |
5f8252d2 | 285 | private: |
286 | // BodyConsumer for HTTP: consume request body. | |
287 | virtual void handleRequestBodyProducerAborted(); | |
0353e724 | 288 | }; |
289 | ||
290 | CBDATA_CLASS_INIT(FtpStateData); | |
291 | ||
292 | void * | |
293 | FtpStateData::operator new (size_t) | |
294 | { | |
295 | CBDATA_INIT_TYPE(FtpStateData); | |
296 | FtpStateData *result = cbdataAlloc(FtpStateData); | |
297 | return result; | |
62e76326 | 298 | } |
090089c4 | 299 | |
0353e724 | 300 | void |
301 | FtpStateData::operator delete (void *address) | |
302 | { | |
303 | FtpStateData *t = static_cast<FtpStateData *>(address); | |
304 | cbdataFree(t); | |
305 | } | |
306 | ||
63be0a78 | 307 | /// \ingroup ServerProtocolFTPInternal |
9e008dda | 308 | typedef struct { |
3fdadc70 | 309 | char type; |
47f6e231 | 310 | int64_t size; |
3fdadc70 | 311 | char *date; |
312 | char *name; | |
313 | char *showname; | |
314 | char *link; | |
2fadd50d | 315 | } ftpListParts; |
3fdadc70 | 316 | |
63be0a78 | 317 | /// \ingroup ServerProtocolFTPInternal |
a689bd4e | 318 | #define FTP_LOGIN_ESCAPED 1 |
63be0a78 | 319 | |
320 | /// \ingroup ServerProtocolFTPInternal | |
a689bd4e | 321 | #define FTP_LOGIN_NOT_ESCAPED 0 |
c68e9c6b | 322 | |
7e3ce7b9 | 323 | /* |
324 | * State machine functions | |
969c39b9 | 325 | * send == state transition |
326 | * read == wait for response, and select next state transition | |
dbfed404 | 327 | * other == Transition logic |
969c39b9 | 328 | */ |
3fdadc70 | 329 | static FTPSM ftpReadWelcome; |
969c39b9 | 330 | static FTPSM ftpSendUser; |
3fdadc70 | 331 | static FTPSM ftpReadUser; |
969c39b9 | 332 | static FTPSM ftpSendPass; |
3fdadc70 | 333 | static FTPSM ftpReadPass; |
969c39b9 | 334 | static FTPSM ftpSendType; |
3fdadc70 | 335 | static FTPSM ftpReadType; |
969c39b9 | 336 | static FTPSM ftpSendMdtm; |
3fdadc70 | 337 | static FTPSM ftpReadMdtm; |
969c39b9 | 338 | static FTPSM ftpSendSize; |
3fdadc70 | 339 | static FTPSM ftpReadSize; |
cc192b50 | 340 | static FTPSM ftpSendEPRT; |
341 | static FTPSM ftpReadEPRT; | |
342 | static FTPSM ftpSendPORT; | |
343 | static FTPSM ftpReadPORT; | |
a689bd4e | 344 | static FTPSM ftpSendPassive; |
cc192b50 | 345 | static FTPSM ftpReadEPSV; |
3fdadc70 | 346 | static FTPSM ftpReadPasv; |
dbfed404 | 347 | static FTPSM ftpTraverseDirectory; |
348 | static FTPSM ftpListDir; | |
349 | static FTPSM ftpGetFile; | |
969c39b9 | 350 | static FTPSM ftpSendCwd; |
3fdadc70 | 351 | static FTPSM ftpReadCwd; |
94439e4e | 352 | static FTPSM ftpRestOrList; |
969c39b9 | 353 | static FTPSM ftpSendList; |
354 | static FTPSM ftpSendNlst; | |
3fdadc70 | 355 | static FTPSM ftpReadList; |
969c39b9 | 356 | static FTPSM ftpSendRest; |
3fdadc70 | 357 | static FTPSM ftpReadRest; |
969c39b9 | 358 | static FTPSM ftpSendRetr; |
3fdadc70 | 359 | static FTPSM ftpReadRetr; |
360 | static FTPSM ftpReadTransferDone; | |
54220df8 | 361 | static FTPSM ftpSendStor; |
362 | static FTPSM ftpReadStor; | |
94439e4e | 363 | static FTPSM ftpWriteTransferDone; |
54220df8 | 364 | static FTPSM ftpSendReply; |
94439e4e | 365 | static FTPSM ftpSendMkdir; |
54220df8 | 366 | static FTPSM ftpReadMkdir; |
94439e4e | 367 | static FTPSM ftpFail; |
368 | static FTPSM ftpSendQuit; | |
369 | static FTPSM ftpReadQuit; | |
a689bd4e | 370 | |
371 | /************************************************ | |
372 | ** Debugs Levels used here ** | |
373 | ************************************************* | |
374 | 0 CRITICAL Events | |
375 | 1 IMPORTANT Events | |
376 | Protocol and Transmission failures. | |
377 | 2 FTP Protocol Chatter | |
378 | 3 Logic Flows | |
379 | 4 Data Parsing Flows | |
380 | 5 Data Dumps | |
381 | 7 ?? | |
382 | ************************************************/ | |
383 | ||
dbfed404 | 384 | /************************************************ |
385 | ** State Machine Description (excluding hacks) ** | |
386 | ************************************************* | |
387 | From To | |
388 | --------------------------------------- | |
389 | Welcome User | |
390 | User Pass | |
391 | Pass Type | |
392 | Type TraverseDirectory / GetFile | |
393 | TraverseDirectory Cwd / GetFile / ListDir | |
94439e4e | 394 | Cwd TraverseDirectory / Mkdir |
dbfed404 | 395 | GetFile Mdtm |
396 | Mdtm Size | |
a689bd4e | 397 | Size Epsv |
398 | ListDir Epsv | |
399 | Epsv FileOrList | |
94439e4e | 400 | FileOrList Rest / Retr / Nlst / List / Mkdir (PUT /xxx;type=d) |
dbfed404 | 401 | Rest Retr |
94439e4e | 402 | Retr / Nlst / List DataRead* (on datachannel) |
403 | DataRead* ReadTransferDone | |
dbfed404 | 404 | ReadTransferDone DataTransferDone |
94439e4e | 405 | Stor DataWrite* (on datachannel) |
406 | DataWrite* RequestPutBody** (from client) | |
407 | RequestPutBody** DataWrite* / WriteTransferDone | |
408 | WriteTransferDone DataTransferDone | |
dbfed404 | 409 | DataTransferDone Quit |
410 | Quit - | |
411 | ************************************************/ | |
3fdadc70 | 412 | |
63be0a78 | 413 | /// \ingroup ServerProtocolFTPInternal |
9e008dda AJ |
414 | FTPSM *FTP_SM_FUNCS[] = { |
415 | ftpReadWelcome, /* BEGIN */ | |
416 | ftpReadUser, /* SENT_USER */ | |
417 | ftpReadPass, /* SENT_PASS */ | |
418 | ftpReadType, /* SENT_TYPE */ | |
419 | ftpReadMdtm, /* SENT_MDTM */ | |
420 | ftpReadSize, /* SENT_SIZE */ | |
421 | ftpReadEPRT, /* SENT_EPRT */ | |
422 | ftpReadPORT, /* SENT_PORT */ | |
423 | ftpReadEPSV, /* SENT_EPSV_ALL */ | |
424 | ftpReadEPSV, /* SENT_EPSV_1 */ | |
425 | ftpReadEPSV, /* SENT_EPSV_2 */ | |
426 | ftpReadPasv, /* SENT_PASV */ | |
427 | ftpReadCwd, /* SENT_CWD */ | |
428 | ftpReadList, /* SENT_LIST */ | |
429 | ftpReadList, /* SENT_NLST */ | |
430 | ftpReadRest, /* SENT_REST */ | |
431 | ftpReadRetr, /* SENT_RETR */ | |
432 | ftpReadStor, /* SENT_STOR */ | |
433 | ftpReadQuit, /* SENT_QUIT */ | |
434 | ftpReadTransferDone, /* READING_DATA (RETR,LIST,NLST) */ | |
435 | ftpWriteTransferDone, /* WRITING_DATA (STOR) */ | |
436 | ftpReadMkdir /* SENT_MKDIR */ | |
437 | }; | |
e381a13d | 438 | |
6e92b048 | 439 | /// handler called by Comm when FTP control channel is closed unexpectedly |
9e008dda | 440 | void |
94b88585 AR |
441 | FtpStateData::ctrlClosed(const CommCloseCbParams &io) |
442 | { | |
443 | ctrl.clear(); | |
444 | deleteThis("FtpStateData::ctrlClosed"); | |
445 | } | |
446 | ||
6e92b048 | 447 | /// handler called by Comm when FTP data channel is closed unexpectedly |
9e008dda | 448 | void |
94b88585 | 449 | FtpStateData::dataClosed(const CommCloseCbParams &io) |
518a192e | 450 | { |
04f55905 AJ |
451 | if (data.listener) { |
452 | delete data.listener; | |
453 | data.listener = NULL; | |
454 | data.fd = -1; | |
455 | } | |
94b88585 | 456 | data.clear(); |
5a5b4ce1 | 457 | failed(ERR_FTP_FAILURE, 0); |
6e92b048 | 458 | /* failed closes ctrl.fd and frees ftpState */ |
5a5b4ce1 AJ |
459 | |
460 | /* NP: failure recovery may be possible when its only a data.fd failure. | |
461 | * is the ctrl.fd is still fine, we can send ABOR down it and retry. | |
462 | * Just need to watch out for wider Squid states like shutting down or reconfigure. | |
463 | */ | |
6f0aab86 | 464 | } |
1be4874e | 465 | |
dc56a9b1 | 466 | FtpStateData::FtpStateData(FwdState *theFwdState) : AsyncJob("FtpStateData"), ServerStateData(theFwdState) |
6f0aab86 | 467 | { |
3900307b | 468 | const char *url = entry->url(); |
a689bd4e | 469 | debugs(9, 3, HERE << "'" << url << "'" ); |
6f0aab86 | 470 | statCounter.server.all.requests++; |
471 | statCounter.server.ftp.requests++; | |
47f6e231 | 472 | theSize = -1; |
fe6b2914 | 473 | mdtm = -1; |
6f0aab86 | 474 | |
fe6b2914 | 475 | if (Config.Ftp.passive && !theFwdState->ftpPasvFailed()) |
476 | flags.pasv_supported = 1; | |
6f0aab86 | 477 | |
fe6b2914 | 478 | flags.rest_supported = 1; |
6f0aab86 | 479 | |
dc56a9b1 | 480 | typedef CommCbMemFunT<FtpStateData, CommCloseCbParams> Dialer; |
94b88585 | 481 | AsyncCall::Pointer closer = asyncCall(9, 5, "FtpStateData::ctrlClosed", |
9e008dda | 482 | Dialer(this, &FtpStateData::ctrlClosed)); |
94b88585 | 483 | ctrl.opened(theFwdState->server_fd, closer); |
6f0aab86 | 484 | |
fe6b2914 | 485 | if (request->method == METHOD_PUT) |
486 | flags.put = 1; | |
518a192e | 487 | } |
488 | ||
0353e724 | 489 | FtpStateData::~FtpStateData() |
ba718c8f | 490 | { |
a689bd4e | 491 | debugs(9, 3, HERE << entry->url() ); |
62e76326 | 492 | |
6f0aab86 | 493 | if (reply_hdr) { |
494 | memFree(reply_hdr, MEM_8K_BUF); | |
495 | reply_hdr = NULL; | |
496 | } | |
62e76326 | 497 | |
94b88585 | 498 | data.close(); |
62e76326 | 499 | |
6e92b048 AR |
500 | if (ctrl.fd >= 0) { |
501 | debugs(9, DBG_IMPORTANT, HERE << "Internal bug: FtpStateData left " << | |
9e008dda | 502 | "control FD " << ctrl.fd << " open"); |
6e92b048 AR |
503 | } |
504 | ||
6f0aab86 | 505 | if (ctrl.buf) { |
506 | memFreeBuf(ctrl.size, ctrl.buf); | |
507 | ctrl.buf = NULL; | |
1ecaa0a0 | 508 | } |
62e76326 | 509 | |
968d691c | 510 | if (data.readBuf) { |
511 | if (!data.readBuf->isNull()) | |
9e008dda | 512 | data.readBuf->clean(); |
253caccb | 513 | |
968d691c | 514 | delete data.readBuf; |
515 | } | |
62e76326 | 516 | |
6f0aab86 | 517 | if (pathcomps) |
518 | wordlistDestroy(&pathcomps); | |
62e76326 | 519 | |
6f0aab86 | 520 | if (ctrl.message) |
521 | wordlistDestroy(&ctrl.message); | |
62e76326 | 522 | |
0477a072 | 523 | cwd_message.clean(); |
62e76326 | 524 | |
6f0aab86 | 525 | safe_free(ctrl.last_reply); |
62e76326 | 526 | |
6f0aab86 | 527 | safe_free(ctrl.last_command); |
62e76326 | 528 | |
6f0aab86 | 529 | safe_free(old_request); |
62e76326 | 530 | |
6f0aab86 | 531 | safe_free(old_reply); |
62e76326 | 532 | |
6f0aab86 | 533 | safe_free(old_filepath); |
62e76326 | 534 | |
30abd221 | 535 | title_url.clean(); |
62e76326 | 536 | |
30abd221 | 537 | base_href.clean(); |
62e76326 | 538 | |
6f0aab86 | 539 | safe_free(filepath); |
62e76326 | 540 | |
6d0440a2 | 541 | safe_free(dirpath); |
542 | ||
6f0aab86 | 543 | safe_free(data.host); |
5f8252d2 | 544 | |
b6b6f466 | 545 | fwd = NULL; // refcounted |
ba718c8f | 546 | } |
547 | ||
f381d699 AJ |
548 | /** |
549 | * Parse a possible login username:password pair. | |
cd0b63ba | 550 | * Produces filled member variables user, password, password_url if anything found. |
f381d699 | 551 | */ |
6f0aab86 | 552 | void |
cd0b63ba | 553 | FtpStateData::loginParser(const char *login, int escaped) |
090089c4 | 554 | { |
ccb355fa AJ |
555 | const char *u = NULL; // end of the username sub-string |
556 | int len; // length of the current sub-string to handle. | |
557 | ||
558 | int total_len = strlen(login); | |
d6a8572b | 559 | |
f381d699 AJ |
560 | debugs(9, 4, HERE << ": login='" << login << "', escaped=" << escaped); |
561 | debugs(9, 9, HERE << ": IN : login='" << login << "', escaped=" << escaped << ", user=" << user << ", password=" << password); | |
62e76326 | 562 | |
cd0b63ba | 563 | if ((u = strchr(login, ':'))) { |
62e76326 | 564 | |
f381d699 | 565 | /* if there was a username part */ |
cd0b63ba AJ |
566 | if (u > login) { |
567 | len = u - login; | |
ccb355fa | 568 | ++u; // jump off the delimiter. |
cd0b63ba | 569 | if (len > MAX_URL) |
ccb355fa AJ |
570 | len = MAX_URL-1; |
571 | xstrncpy(user, login, len +1); | |
572 | debugs(9, 9, HERE << ": found user='" << user << "'(" << len <<"), escaped=" << escaped); | |
f381d699 AJ |
573 | if (escaped) |
574 | rfc1738_unescape(user); | |
ccb355fa | 575 | debugs(9, 9, HERE << ": found user='" << user << "'(" << len <<") unescaped."); |
c4a2d5e1 | 576 | } |
62e76326 | 577 | |
f381d699 | 578 | /* if there was a password part */ |
ccb355fa AJ |
579 | len = login + total_len - u; |
580 | if ( len > 0) { | |
581 | if (len > MAX_URL) | |
582 | len = MAX_URL -1; | |
583 | xstrncpy(password, u, len +1); | |
584 | debugs(9, 9, HERE << ": found password='" << password << "'(" << len <<"), escaped=" << escaped); | |
f381d699 AJ |
585 | if (escaped) { |
586 | rfc1738_unescape(password); | |
587 | password_url = 1; | |
588 | } | |
ccb355fa | 589 | debugs(9, 9, HERE << ": found password='" << password << "'(" << len <<") unescaped."); |
f381d699 | 590 | } |
04f7fd38 | 591 | } else if (login[0]) { |
f381d699 | 592 | /* no password, just username */ |
ccb355fa AJ |
593 | if (total_len > MAX_URL) |
594 | total_len = MAX_URL -1; | |
595 | xstrncpy(user, login, total_len +1); | |
596 | debugs(9, 9, HERE << ": found user='" << user << "'(" << total_len <<"), escaped=" << escaped); | |
f381d699 AJ |
597 | if (escaped) |
598 | rfc1738_unescape(user); | |
ccb355fa | 599 | debugs(9, 9, HERE << ": found user='" << user << "'(" << total_len <<") unescaped."); |
f381d699 | 600 | } |
62e76326 | 601 | |
f381d699 | 602 | debugs(9, 9, HERE << ": OUT: login='" << login << "', escaped=" << escaped << ", user=" << user << ", password=" << password); |
090089c4 | 603 | } |
604 | ||
6f0aab86 | 605 | void |
dc56a9b1 | 606 | FtpStateData::ftpTimeout(const CommTimeoutCbParams &io) |
090089c4 | 607 | { |
dc56a9b1 | 608 | debugs(9, 4, "ftpTimeout: FD " << io.fd << ": '" << entry->url() << "'" ); |
62e76326 | 609 | |
dc56a9b1 | 610 | if (SENT_PASV == state && io.fd == data.fd) { |
62e76326 | 611 | /* stupid ftp.netscape.com */ |
dc56a9b1 | 612 | fwd->dontRetry(false); |
613 | fwd->ftpPasvFailed(true); | |
614 | debugs(9, DBG_IMPORTANT, "ftpTimeout: timeout in SENT_PASV state" ); | |
7e3ce7b9 | 615 | } |
62e76326 | 616 | |
dc56a9b1 | 617 | failed(ERR_READ_TIMEOUT, 0); |
6f0aab86 | 618 | /* failed() closes ctrl.fd and frees ftpState */ |
090089c4 | 619 | } |
620 | ||
0477a072 | 621 | #if DEAD_CODE // obsoleted by ERR_DIR_LISTING |
6f0aab86 | 622 | void |
623 | FtpStateData::listingFinish() | |
3fdadc70 | 624 | { |
0477a072 | 625 | // TODO: figure out what this means and how to show it ... |
62e76326 | 626 | |
6f0aab86 | 627 | if (flags.listformat_unknown && !flags.tried_nlst) { |
0477a072 | 628 | printfReplyBody("<a href=\"%s/;type=d\">[As plain directory]</a>\n", |
253caccb | 629 | flags.dir_slash ? rfc1738_escape_part(old_filepath) : "."); |
6f0aab86 | 630 | } else if (typecode == 'D') { |
631 | const char *path = flags.dir_slash ? filepath : "."; | |
0477a072 | 632 | printfReplyBody("<a href=\"%s/\">[As extended directory]</a>\n", rfc1738_escape_part(path)); |
dbfed404 | 633 | } |
3fdadc70 | 634 | } |
0477a072 | 635 | #endif /* DEAD_CODE */ |
3fdadc70 | 636 | |
63be0a78 | 637 | /// \ingroup ServerProtocolFTPInternal |
9e008dda AJ |
638 | static const char *Month[] = { |
639 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
640 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
641 | }; | |
3fdadc70 | 642 | |
63be0a78 | 643 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 644 | static int |
645 | is_month(const char *buf) | |
646 | { | |
647 | int i; | |
62e76326 | 648 | |
3fdadc70 | 649 | for (i = 0; i < 12; i++) |
62e76326 | 650 | if (!strcasecmp(buf, Month[i])) |
651 | return 1; | |
652 | ||
3fdadc70 | 653 | return 0; |
654 | } | |
655 | ||
63be0a78 | 656 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 657 | static void |
658 | ftpListPartsFree(ftpListParts ** parts) | |
659 | { | |
660 | safe_free((*parts)->date); | |
661 | safe_free((*parts)->name); | |
662 | safe_free((*parts)->showname); | |
663 | safe_free((*parts)->link); | |
664 | safe_free(*parts); | |
665 | } | |
666 | ||
63be0a78 | 667 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 668 | #define MAX_TOKENS 64 |
669 | ||
63be0a78 | 670 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 671 | static ftpListParts * |
e55f0142 | 672 | ftpListParseParts(const char *buf, struct _ftp_flags flags) |
3fdadc70 | 673 | { |
674 | ftpListParts *p = NULL; | |
675 | char *t = NULL; | |
676 | const char *ct = NULL; | |
677 | char *tokens[MAX_TOKENS]; | |
678 | int i; | |
679 | int n_tokens; | |
748f6a15 | 680 | static char tbuf[128]; |
3fdadc70 | 681 | char *xbuf = NULL; |
748f6a15 | 682 | static int scan_ftp_initialized = 0; |
683 | static regex_t scan_ftp_integer; | |
684 | static regex_t scan_ftp_time; | |
685 | static regex_t scan_ftp_dostime; | |
686 | static regex_t scan_ftp_dosdate; | |
687 | ||
9e008dda | 688 | if (!scan_ftp_initialized) { |
62e76326 | 689 | scan_ftp_initialized = 1; |
690 | regcomp(&scan_ftp_integer, "^[0123456789]+$", REG_EXTENDED | REG_NOSUB); | |
691 | regcomp(&scan_ftp_time, "^[0123456789:]+$", REG_EXTENDED | REG_NOSUB); | |
692 | regcomp(&scan_ftp_dosdate, "^[0123456789]+-[0123456789]+-[0123456789]+$", REG_EXTENDED | REG_NOSUB); | |
693 | regcomp(&scan_ftp_dostime, "^[0123456789]+:[0123456789]+[AP]M$", REG_EXTENDED | REG_NOSUB | REG_ICASE); | |
748f6a15 | 694 | } |
62e76326 | 695 | |
3fdadc70 | 696 | if (buf == NULL) |
62e76326 | 697 | return NULL; |
698 | ||
3fdadc70 | 699 | if (*buf == '\0') |
62e76326 | 700 | return NULL; |
701 | ||
e6ccf245 | 702 | p = (ftpListParts *)xcalloc(1, sizeof(ftpListParts)); |
62e76326 | 703 | |
3fdadc70 | 704 | n_tokens = 0; |
62e76326 | 705 | |
748f6a15 | 706 | memset(tokens, 0, sizeof(tokens)); |
62e76326 | 707 | |
3fdadc70 | 708 | xbuf = xstrdup(buf); |
62e76326 | 709 | |
9e008dda | 710 | if (flags.tried_nlst) { |
62e76326 | 711 | /* Machine readable format, one name per line */ |
712 | p->name = xbuf; | |
713 | p->type = '\0'; | |
714 | return p; | |
dbfed404 | 715 | } |
62e76326 | 716 | |
3fdadc70 | 717 | for (t = strtok(xbuf, w_space); t && n_tokens < MAX_TOKENS; t = strtok(NULL, w_space)) |
62e76326 | 718 | tokens[n_tokens++] = xstrdup(t); |
719 | ||
3fdadc70 | 720 | xfree(xbuf); |
62e76326 | 721 | |
3fdadc70 | 722 | /* locate the Month field */ |
9e008dda | 723 | for (i = 3; i < n_tokens - 2; i++) { |
62e76326 | 724 | char *size = tokens[i - 1]; |
725 | char *month = tokens[i]; | |
726 | char *day = tokens[i + 1]; | |
727 | char *year = tokens[i + 2]; | |
728 | ||
729 | if (!is_month(month)) | |
730 | continue; | |
731 | ||
732 | if (regexec(&scan_ftp_integer, size, 0, NULL, 0) != 0) | |
733 | continue; | |
734 | ||
735 | if (regexec(&scan_ftp_integer, day, 0, NULL, 0) != 0) | |
736 | continue; | |
737 | ||
7978e6ca | 738 | if (regexec(&scan_ftp_time, year, 0, NULL, 0) != 0) /* Yr | hh:mm */ |
62e76326 | 739 | continue; |
740 | ||
741 | snprintf(tbuf, 128, "%s %2s %5s", | |
742 | month, day, year); | |
743 | ||
744 | if (!strstr(buf, tbuf)) | |
745 | snprintf(tbuf, 128, "%s %2s %-5s", | |
746 | month, day, year); | |
747 | ||
65bfb9ab | 748 | char const *copyFrom = NULL; |
749 | ||
750 | if ((copyFrom = strstr(buf, tbuf))) { | |
62e76326 | 751 | p->type = *tokens[0]; |
47f6e231 | 752 | p->size = strtoll(size, NULL, 10); |
62e76326 | 753 | p->date = xstrdup(tbuf); |
754 | ||
755 | if (flags.skip_whitespace) { | |
65bfb9ab | 756 | copyFrom += strlen(tbuf); |
62e76326 | 757 | |
65bfb9ab | 758 | while (strchr(w_space, *copyFrom)) |
759 | copyFrom++; | |
62e76326 | 760 | } else { |
761 | /* XXX assumes a single space between date and filename | |
762 | * suggested by: Nathan.Bailey@cc.monash.edu.au and | |
763 | * Mike Battersby <mike@starbug.bofh.asn.au> */ | |
65bfb9ab | 764 | copyFrom += strlen(tbuf) + 1; |
62e76326 | 765 | } |
766 | ||
65bfb9ab | 767 | p->name = xstrdup(copyFrom); |
62e76326 | 768 | |
38ebaefc | 769 | if (p->type == 'l' && (t = strstr(p->name, " -> "))) { |
62e76326 | 770 | *t = '\0'; |
771 | p->link = xstrdup(t + 4); | |
772 | } | |
773 | ||
774 | goto found; | |
775 | } | |
776 | ||
777 | break; | |
3fdadc70 | 778 | } |
62e76326 | 779 | |
748f6a15 | 780 | /* try it as a DOS listing, 04-05-70 09:33PM ... */ |
781 | if (n_tokens > 3 && | |
62e76326 | 782 | regexec(&scan_ftp_dosdate, tokens[0], 0, NULL, 0) == 0 && |
9e008dda | 783 | regexec(&scan_ftp_dostime, tokens[1], 0, NULL, 0) == 0) { |
62e76326 | 784 | if (!strcasecmp(tokens[2], "<dir>")) { |
785 | p->type = 'd'; | |
786 | } else { | |
787 | p->type = '-'; | |
47f6e231 | 788 | p->size = strtoll(tokens[2], NULL, 10); |
62e76326 | 789 | } |
790 | ||
791 | snprintf(tbuf, 128, "%s %s", tokens[0], tokens[1]); | |
792 | p->date = xstrdup(tbuf); | |
793 | ||
794 | if (p->type == 'd') { | |
795 | /* Directory.. name begins with first printable after <dir> */ | |
796 | ct = strstr(buf, tokens[2]); | |
797 | ct += strlen(tokens[2]); | |
798 | ||
799 | while (xisspace(*ct)) | |
800 | ct++; | |
801 | ||
802 | if (!*ct) | |
803 | ct = NULL; | |
804 | } else { | |
805 | /* A file. Name begins after size, with a space in between */ | |
806 | snprintf(tbuf, 128, " %s %s", tokens[2], tokens[3]); | |
807 | ct = strstr(buf, tbuf); | |
808 | ||
809 | if (ct) { | |
810 | ct += strlen(tokens[2]) + 2; | |
811 | } | |
812 | } | |
813 | ||
814 | p->name = xstrdup(ct ? ct : tokens[3]); | |
815 | goto found; | |
3fdadc70 | 816 | } |
62e76326 | 817 | |
3fdadc70 | 818 | /* Try EPLF format; carson@lehman.com */ |
9e008dda | 819 | if (buf[0] == '+') { |
62e76326 | 820 | ct = buf + 1; |
821 | p->type = 0; | |
822 | ||
823 | while (ct && *ct) { | |
d5f8d05f | 824 | time_t tm; |
3f9d0b2d | 825 | int l = strcspn(ct, ","); |
62e76326 | 826 | char *tmp; |
827 | ||
828 | if (l < 1) | |
829 | goto blank; | |
830 | ||
831 | switch (*ct) { | |
832 | ||
833 | case '\t': | |
834 | p->name = xstrndup(ct + 1, l + 1); | |
835 | break; | |
836 | ||
837 | case 's': | |
838 | p->size = atoi(ct + 1); | |
839 | break; | |
840 | ||
841 | case 'm': | |
d5f8d05f | 842 | tm = (time_t) strtol(ct + 1, &tmp, 0); |
62e76326 | 843 | |
3f9d0b2d | 844 | if (tmp != ct + 1) |
62e76326 | 845 | break; /* not a valid integer */ |
846 | ||
d5f8d05f | 847 | p->date = xstrdup(ctime(&tm)); |
62e76326 | 848 | |
849 | *(strstr(p->date, "\n")) = '\0'; | |
850 | ||
851 | break; | |
852 | ||
853 | case '/': | |
854 | p->type = 'd'; | |
855 | ||
856 | break; | |
857 | ||
858 | case 'r': | |
859 | p->type = '-'; | |
860 | ||
861 | break; | |
862 | ||
863 | case 'i': | |
864 | break; | |
865 | ||
866 | default: | |
867 | break; | |
868 | } | |
869 | ||
870 | blank: | |
871 | ct = strstr(ct, ","); | |
872 | ||
873 | if (ct) { | |
874 | ct++; | |
875 | } | |
876 | } | |
877 | ||
878 | if (p->type == 0) { | |
879 | p->type = '-'; | |
880 | } | |
881 | ||
882 | if (p->name) | |
883 | goto found; | |
884 | else | |
885 | safe_free(p->date); | |
886 | } | |
887 | ||
888 | found: | |
889 | ||
3fdadc70 | 890 | for (i = 0; i < n_tokens; i++) |
62e76326 | 891 | xfree(tokens[i]); |
892 | ||
748f6a15 | 893 | if (!p->name) |
62e76326 | 894 | ftpListPartsFree(&p); /* cleanup */ |
895 | ||
3fdadc70 | 896 | return p; |
897 | } | |
898 | ||
0477a072 | 899 | MemBuf * |
6f0aab86 | 900 | FtpStateData::htmlifyListEntry(const char *line) |
3fdadc70 | 901 | { |
0477a072 AJ |
902 | char icon[2048]; |
903 | char href[2048 + 40]; | |
904 | char text[ 2048]; | |
905 | char size[ 2048]; | |
906 | char chdir[ 2048 + 40]; | |
907 | char view[ 2048 + 40]; | |
908 | char download[ 2048 + 40]; | |
909 | char link[ 2048 + 40]; | |
910 | MemBuf *html; | |
911 | char prefix[2048]; | |
3fdadc70 | 912 | ftpListParts *parts; |
0477a072 | 913 | *icon = *href = *text = *size = *chdir = *view = *download = *link = '\0'; |
62e76326 | 914 | |
0477a072 | 915 | debugs(9, 7, HERE << " line ={" << line << "}"); |
62e76326 | 916 | |
0477a072 AJ |
917 | if (strlen(line) > 1024) { |
918 | html = new MemBuf(); | |
919 | html->init(); | |
920 | html->Printf("<tr><td colspan=\"5\">%s</td></tr>\n", line); | |
62e76326 | 921 | return html; |
3fdadc70 | 922 | } |
62e76326 | 923 | |
45289935 | 924 | if (flags.dir_slash && dirpath && typecode != 'D') |
925 | snprintf(prefix, 2048, "%s/", rfc1738_escape_part(dirpath)); | |
5627a633 | 926 | else |
927 | prefix[0] = '\0'; | |
928 | ||
6f0aab86 | 929 | if ((parts = ftpListParseParts(line, flags)) == NULL) { |
62e76326 | 930 | const char *p; |
0477a072 AJ |
931 | |
932 | html = new MemBuf(); | |
933 | html->init(); | |
934 | html->Printf("<tr class=\"entry\"><td colspan=\"5\">%s</td></tr>\n", line); | |
62e76326 | 935 | |
3d0ac046 | 936 | for (p = line; *p && xisspace(*p); p++); |
62e76326 | 937 | if (*p && !xisspace(*p)) |
6f0aab86 | 938 | flags.listformat_unknown = 1; |
62e76326 | 939 | |
940 | return html; | |
13e80e5b | 941 | } |
62e76326 | 942 | |
3fdadc70 | 943 | if (!strcmp(parts->name, ".") || !strcmp(parts->name, "..")) { |
62e76326 | 944 | ftpListPartsFree(&parts); |
0477a072 | 945 | return NULL; |
3fdadc70 | 946 | } |
62e76326 | 947 | |
3fdadc70 | 948 | parts->size += 1023; |
949 | parts->size >>= 10; | |
950 | parts->showname = xstrdup(parts->name); | |
62e76326 | 951 | |
2a1ca944 | 952 | /* {icon} {text} . . . {date}{size}{chdir}{view}{download}{link}\n */ |
9bc73deb | 953 | xstrncpy(href, rfc1738_escape_part(parts->name), 2048); |
62e76326 | 954 | |
2a1ca944 | 955 | xstrncpy(text, parts->showname, 2048); |
62e76326 | 956 | |
3fdadc70 | 957 | switch (parts->type) { |
62e76326 | 958 | |
3fdadc70 | 959 | case 'd': |
0477a072 | 960 | snprintf(icon, 2048, "<img border=\"0\" src=\"%s\" alt=\"%-6s\">", |
62e76326 | 961 | mimeGetIconURL("internal-dir"), |
962 | "[DIR]"); | |
963 | strcat(href, "/"); /* margin is allocated above */ | |
964 | break; | |
965 | ||
3fdadc70 | 966 | case 'l': |
0477a072 | 967 | snprintf(icon, 2048, "<img border=\"0\" src=\"%s\" alt=\"%-6s\">", |
62e76326 | 968 | mimeGetIconURL("internal-link"), |
969 | "[LINK]"); | |
970 | /* sometimes there is an 'l' flag, but no "->" link */ | |
971 | ||
972 | if (parts->link) { | |
973 | char *link2 = xstrdup(html_quote(rfc1738_escape(parts->link))); | |
0477a072 | 974 | snprintf(link, 2048, " -> <a href=\"%s%s\">%s</a>", |
5627a633 | 975 | *link2 != '/' ? prefix : "", link2, |
62e76326 | 976 | html_quote(parts->link)); |
977 | safe_free(link2); | |
978 | } | |
979 | ||
980 | break; | |
981 | ||
dbfed404 | 982 | case '\0': |
0477a072 | 983 | snprintf(icon, 2048, "<img border=\"0\" src=\"%s\" alt=\"%-6s\">", |
62e76326 | 984 | mimeGetIconURL(parts->name), |
985 | "[UNKNOWN]"); | |
0477a072 AJ |
986 | snprintf(chdir, 2048, "<a href=\"%s/;type=d\"><img border=\"0\" src=\"%s\" " |
987 | "alt=\"[DIR]\"></a>", | |
62e76326 | 988 | rfc1738_escape_part(parts->name), |
989 | mimeGetIconURL("internal-dir")); | |
990 | break; | |
991 | ||
3fdadc70 | 992 | case '-': |
62e76326 | 993 | |
3fdadc70 | 994 | default: |
0477a072 | 995 | snprintf(icon, 2048, "<img border=\"0\" src=\"%s\" alt=\"%-6s\">", |
62e76326 | 996 | mimeGetIconURL(parts->name), |
997 | "[FILE]"); | |
47f6e231 | 998 | snprintf(size, 2048, " %6"PRId64"k", parts->size); |
62e76326 | 999 | break; |
3fdadc70 | 1000 | } |
62e76326 | 1001 | |
2a1ca944 | 1002 | if (parts->type != 'd') { |
62e76326 | 1003 | if (mimeGetViewOption(parts->name)) { |
0477a072 AJ |
1004 | snprintf(view, 2048, "<a href=\"%s%s;type=a\"><img border=\"0\" src=\"%s\" " |
1005 | "alt=\"[VIEW]\"></a>", | |
5627a633 | 1006 | prefix, href, mimeGetIconURL("internal-view")); |
62e76326 | 1007 | } |
1008 | ||
1009 | if (mimeGetDownloadOption(parts->name)) { | |
0477a072 AJ |
1010 | snprintf(download, 2048, "<a href=\"%s%s;type=i\"><img border=\"0\" src=\"%s\" " |
1011 | "alt=\"[DOWNLOAD]\"></a>", | |
5627a633 | 1012 | prefix, href, mimeGetIconURL("internal-download")); |
62e76326 | 1013 | } |
2a1ca944 | 1014 | } |
62e76326 | 1015 | |
0477a072 AJ |
1016 | /* construct the table row from parts. */ |
1017 | html = new MemBuf(); | |
1018 | html->init(); | |
1019 | html->Printf("<tr class=\"entry\">" | |
05320519 A |
1020 | "<td class=\"icon\"><a href=\"%s%s\">%s</a></td>" |
1021 | "<td class=\"filename\"><a href=\"%s%s\">%s</a></td>" | |
1022 | "<td class=\"date\">%s</td>" | |
1023 | "<td class=\"size\">%s</td>" | |
1024 | "<td class=\"actions\">%s%s%s%s</td>" | |
1025 | "</tr>\n", | |
1026 | prefix, href, icon, | |
1027 | prefix, href, html_quote(text), | |
1028 | parts->date, | |
1029 | size, | |
62e76326 | 1030 | chdir, view, download, link); |
62e76326 | 1031 | |
3fdadc70 | 1032 | ftpListPartsFree(&parts); |
3fdadc70 | 1033 | return html; |
1034 | } | |
1035 | ||
f1a83a7d | 1036 | void |
1037 | FtpStateData::parseListing() | |
3fdadc70 | 1038 | { |
253caccb | 1039 | char *buf = data.readBuf->content(); |
a313a9ba | 1040 | char *sbuf; /* NULL-terminated copy of termedBuf */ |
b5639035 | 1041 | char *end; |
1042 | char *line; | |
3fdadc70 | 1043 | char *s; |
0477a072 | 1044 | MemBuf *t; |
3fdadc70 | 1045 | size_t linelen; |
b5639035 | 1046 | size_t usable; |
253caccb | 1047 | size_t len = data.readBuf->contentSize(); |
5f8252d2 | 1048 | |
1049 | if (!len) { | |
0477a072 | 1050 | debugs(9, 3, HERE << "no content to parse for " << entry->url() ); |
5f8252d2 | 1051 | return; |
1052 | } | |
1053 | ||
7131112f | 1054 | /* |
1055 | * We need a NULL-terminated buffer for scanning, ick | |
1056 | */ | |
e6ccf245 | 1057 | sbuf = (char *)xmalloc(len + 1); |
7131112f | 1058 | xstrncpy(sbuf, buf, len + 1); |
1059 | end = sbuf + len - 1; | |
62e76326 | 1060 | |
7131112f | 1061 | while (*end != '\r' && *end != '\n' && end > sbuf) |
62e76326 | 1062 | end--; |
1063 | ||
7131112f | 1064 | usable = end - sbuf; |
62e76326 | 1065 | |
a689bd4e | 1066 | debugs(9, 3, HERE << "usable = " << usable); |
62e76326 | 1067 | |
b5639035 | 1068 | if (usable == 0) { |
0477a072 | 1069 | debugs(9, 3, HERE << "didn't find end for " << entry->url() ); |
62e76326 | 1070 | xfree(sbuf); |
1071 | return; | |
3fdadc70 | 1072 | } |
62e76326 | 1073 | |
a689bd4e | 1074 | debugs(9, 3, HERE << (unsigned long int)len << " bytes to play with"); |
bf8fe701 | 1075 | |
e6ccf245 | 1076 | line = (char *)memAllocate(MEM_4K_BUF); |
3fdadc70 | 1077 | end++; |
362be274 | 1078 | s = sbuf; |
1079 | s += strspn(s, crlf); | |
62e76326 | 1080 | |
362be274 | 1081 | for (; s < end; s += strcspn(s, crlf), s += strspn(s, crlf)) { |
a689bd4e | 1082 | debugs(9, 7, HERE << "s = {" << s << "}"); |
62e76326 | 1083 | linelen = strcspn(s, crlf) + 1; |
1084 | ||
1085 | if (linelen < 2) | |
1086 | break; | |
1087 | ||
1088 | if (linelen > 4096) | |
1089 | linelen = 4096; | |
1090 | ||
1091 | xstrncpy(line, s, linelen); | |
1092 | ||
a689bd4e | 1093 | debugs(9, 7, HERE << "{" << line << "}"); |
62e76326 | 1094 | |
1095 | if (!strncmp(line, "total", 5)) | |
1096 | continue; | |
1097 | ||
f1a83a7d | 1098 | t = htmlifyListEntry(line); |
62e76326 | 1099 | |
05320519 | 1100 | if ( t != NULL) { |
0477a072 AJ |
1101 | debugs(9, 7, HERE << "listing append: t = {" << t->contentSize() << ", '" << t->content() << "'}"); |
1102 | listing.append(t->content(), t->contentSize()); | |
1103 | //leak? delete t; | |
1104 | } | |
b5639035 | 1105 | } |
62e76326 | 1106 | |
0477a072 | 1107 | debugs(9, 7, HERE << "Done."); |
253caccb | 1108 | data.readBuf->consume(usable); |
db1cd23c | 1109 | memFree(line, MEM_4K_BUF); |
4a2635aa | 1110 | xfree(sbuf); |
3fdadc70 | 1111 | } |
090089c4 | 1112 | |
5f8252d2 | 1113 | int |
9e008dda AJ |
1114 | FtpStateData::dataDescriptor() const |
1115 | { | |
5f8252d2 | 1116 | return data.fd; |
1117 | } | |
1118 | ||
f1a83a7d | 1119 | void |
1120 | FtpStateData::dataComplete() | |
79a15e0a | 1121 | { |
a689bd4e | 1122 | debugs(9, 3,HERE); |
62e76326 | 1123 | |
a689bd4e | 1124 | /* Connection closed; transfer done. */ |
94b88585 AR |
1125 | |
1126 | /// Close data channel, if any, to conserve resources while we wait. | |
1127 | data.close(); | |
62e76326 | 1128 | |
79a15e0a | 1129 | /* expect the "transfer complete" message on the control socket */ |
4e849646 | 1130 | /* |
1131 | * DPW 2007-04-23 | |
1132 | * Previously, this was the only place where we set the | |
1133 | * 'buffered_ok' flag when calling scheduleReadControlReply(). | |
1134 | * It caused some problems if the FTP server returns an unexpected | |
1135 | * status code after the data command. FtpStateData was being | |
1136 | * deleted in the middle of dataRead(). | |
1137 | */ | |
1138 | scheduleReadControlReply(0); | |
79a15e0a | 1139 | } |
1140 | ||
253caccb | 1141 | void |
5f8252d2 | 1142 | FtpStateData::maybeReadVirginBody() |
253caccb | 1143 | { |
1144 | if (data.fd < 0) | |
1145 | return; | |
1146 | ||
1147 | if (data.read_pending) | |
1148 | return; | |
1149 | ||
52edecde | 1150 | const int read_sz = replyBodySpace(*data.readBuf, 0); |
253caccb | 1151 | |
1152 | debugs(11,9, HERE << "FTP may read up to " << read_sz << " bytes"); | |
1153 | ||
1154 | if (read_sz < 2) // see http.cc | |
1155 | return; | |
1156 | ||
1157 | data.read_pending = true; | |
1158 | ||
dc56a9b1 | 1159 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
1160 | AsyncCall::Pointer timeoutCall = asyncCall(9, 5, "FtpStateData::ftpTimeout", | |
9e008dda | 1161 | TimeoutDialer(this,&FtpStateData::ftpTimeout)); |
dc56a9b1 | 1162 | commSetTimeout(data.fd, Config.Timeout.read, timeoutCall); |
928720a6 | 1163 | |
253caccb | 1164 | debugs(9,5,HERE << "queueing read on FD " << data.fd); |
1165 | ||
dc56a9b1 | 1166 | typedef CommCbMemFunT<FtpStateData, CommIoCbParams> Dialer; |
1167 | entry->delayAwareRead(data.fd, data.readBuf->space(), read_sz, | |
9e008dda AJ |
1168 | asyncCall(9, 5, "FtpStateData::dataRead", |
1169 | Dialer(this, &FtpStateData::dataRead))); | |
253caccb | 1170 | } |
1171 | ||
f1a83a7d | 1172 | void |
dc56a9b1 | 1173 | FtpStateData::dataRead(const CommIoCbParams &io) |
f1a83a7d | 1174 | { |
a57512fa | 1175 | int j; |
30a4f2a8 | 1176 | int bin; |
62e76326 | 1177 | |
dc56a9b1 | 1178 | data.read_pending = false; |
1179 | ||
1180 | debugs(9, 3, HERE << "ftpDataRead: FD " << io.fd << " Read " << io.size << " bytes"); | |
62e76326 | 1181 | |
dc56a9b1 | 1182 | if (io.size > 0) { |
1183 | kb_incr(&statCounter.server.all.kbytes_in, io.size); | |
1184 | kb_incr(&statCounter.server.ftp.kbytes_in, io.size); | |
3c7568c7 | 1185 | } |
62e76326 | 1186 | |
dc56a9b1 | 1187 | if (io.flag == COMM_ERR_CLOSING) |
62e76326 | 1188 | return; |
3c7568c7 | 1189 | |
dc56a9b1 | 1190 | assert(io.fd == data.fd); |
3c7568c7 | 1191 | |
e92e4e44 | 1192 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
5f8252d2 | 1193 | abortTransaction("entry aborted during dataRead"); |
62e76326 | 1194 | return; |
e92e4e44 | 1195 | } |
c4b7a5a9 | 1196 | |
dc56a9b1 | 1197 | if (io.flag == COMM_OK && io.size > 0) { |
1198 | debugs(9,5,HERE << "appended " << io.size << " bytes to readBuf"); | |
1199 | data.readBuf->appended(io.size); | |
253caccb | 1200 | #if DELAY_POOLS |
253caccb | 1201 | DelayId delayId = entry->mem_obj->mostBytesAllowed(); |
dc56a9b1 | 1202 | delayId.bytesIn(io.size); |
253caccb | 1203 | #endif |
62e76326 | 1204 | IOStats.Ftp.reads++; |
1205 | ||
dc56a9b1 | 1206 | for (j = io.size - 1, bin = 0; j; bin++) |
62e76326 | 1207 | j >>= 1; |
1208 | ||
1209 | IOStats.Ftp.read_hist[bin]++; | |
30a4f2a8 | 1210 | } |
62e76326 | 1211 | |
dc56a9b1 | 1212 | if (io.flag != COMM_OK || io.size < 0) { |
9e008dda AJ |
1213 | debugs(50, ignoreErrno(io.xerrno) ? 3 : DBG_IMPORTANT, |
1214 | "ftpDataRead: read error: " << xstrerr(io.xerrno)); | |
dc56a9b1 | 1215 | |
1216 | if (ignoreErrno(io.xerrno)) { | |
1217 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; | |
1218 | AsyncCall::Pointer timeoutCall = asyncCall(9, 5, "FtpStateData::ftpTimeout", | |
9e008dda | 1219 | TimeoutDialer(this,&FtpStateData::ftpTimeout)); |
dc56a9b1 | 1220 | commSetTimeout(io.fd, Config.Timeout.read, timeoutCall); |
62e76326 | 1221 | |
5f8252d2 | 1222 | maybeReadVirginBody(); |
62e76326 | 1223 | } else { |
0477a072 | 1224 | if (!flags.http_header_sent && !fwd->ftpPasvFailed() && flags.pasv_supported && !flags.listing) { |
f1a83a7d | 1225 | fwd->dontRetry(false); /* this is a retryable error */ |
1226 | fwd->ftpPasvFailed(true); | |
18ed7c61 | 1227 | } |
1228 | ||
f1a83a7d | 1229 | failed(ERR_READ_ERROR, 0); |
6f0aab86 | 1230 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 1231 | return; |
1232 | } | |
dc56a9b1 | 1233 | } else if (io.size == 0) { |
1234 | debugs(9,3, HERE << "Calling dataComplete() because io.size == 0"); | |
9e008dda AJ |
1235 | /* |
1236 | * DPW 2007-04-23 | |
1237 | * Dangerous curves ahead. This call to dataComplete was | |
1238 | * calling scheduleReadControlReply, handleControlReply, | |
1239 | * and then ftpReadTransferDone. If ftpReadTransferDone | |
1240 | * gets unexpected status code, it closes down the control | |
1241 | * socket and our FtpStateData object gets destroyed. As | |
1242 | * a workaround we no longer set the 'buffered_ok' flag in | |
1243 | * the scheduleReadControlReply call. | |
1244 | */ | |
f1a83a7d | 1245 | dataComplete(); |
253caccb | 1246 | } |
62e76326 | 1247 | |
253caccb | 1248 | processReplyBody(); |
1249 | } | |
b66315e4 | 1250 | |
253caccb | 1251 | void |
1252 | FtpStateData::processReplyBody() | |
1253 | { | |
dc56a9b1 | 1254 | debugs(9, 3, HERE << "FtpStateData::processReplyBody starting."); |
063a47b5 | 1255 | |
47f6e231 | 1256 | if (request->method == METHOD_HEAD && (flags.isdir || theSize != -1)) { |
063a47b5 | 1257 | serverComplete(); |
1258 | return; | |
1259 | } | |
1260 | ||
0477a072 AJ |
1261 | /* Directory listings are special. They write ther own headers via the error objects */ |
1262 | if (!flags.http_header_sent && data.readBuf->contentSize() >= 0 && !flags.isdir) | |
253caccb | 1263 | appendSuccessHeader(); |
62e76326 | 1264 | |
35f26998 | 1265 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
9e008dda AJ |
1266 | /* |
1267 | * probably was aborted because content length exceeds one | |
1268 | * of the maximum size limits. | |
1269 | */ | |
35f26998 | 1270 | abortTransaction("entry aborted after calling appendSuccessHeader()"); |
1271 | return; | |
1272 | } | |
1273 | ||
a83c6ed6 | 1274 | #if USE_ADAPTATION |
62e76326 | 1275 | |
a83c6ed6 AR |
1276 | if (adaptationAccessCheckPending) { |
1277 | debugs(9,3, HERE << "returning from FtpStateData::processReplyBody due to adaptationAccessCheckPending"); | |
253caccb | 1278 | return; |
1279 | } | |
62e76326 | 1280 | |
c4b7a5a9 | 1281 | #endif |
62e76326 | 1282 | |
253caccb | 1283 | if (flags.isdir) { |
0477a072 AJ |
1284 | if (!flags.listing) { |
1285 | flags.listing = 1; | |
1286 | listing.reset(); | |
1287 | } | |
253caccb | 1288 | parseListing(); |
0477a072 AJ |
1289 | maybeReadVirginBody(); |
1290 | return; | |
e1381638 AJ |
1291 | } else if (const int csize = data.readBuf->contentSize()) { |
1292 | writeReplyBody(data.readBuf->content(), csize); | |
1293 | debugs(9, 5, HERE << "consuming " << csize << " bytes of readBuf"); | |
1294 | data.readBuf->consume(csize); | |
1295 | } | |
253caccb | 1296 | |
3900307b | 1297 | entry->flush(); |
253caccb | 1298 | |
5f8252d2 | 1299 | maybeReadVirginBody(); |
090089c4 | 1300 | } |
1301 | ||
63be0a78 | 1302 | /** |
f381d699 AJ |
1303 | * Locates the FTP user:password login. |
1304 | * | |
1305 | * Highest to lowest priority: | |
1306 | * - Checks URL (ftp://user:pass@domain) | |
1307 | * - Authorization: Basic header | |
1308 | * - squid.conf anonymous-FTP settings (default: anonymous:Squid@). | |
1309 | * | |
1310 | * Special Case: A username-only may be provided in the URL and password in the HTTP headers. | |
1311 | * | |
4e2da309 AJ |
1312 | * TODO: we might be able to do something about locating username from other sources: |
1313 | * ie, external ACL user=* tag or ident lookup | |
1314 | * | |
63be0a78 | 1315 | \retval 1 if we have everything needed to complete this request. |
1316 | \retval 0 if something is missing. | |
429fdbec | 1317 | */ |
f1a83a7d | 1318 | int |
1319 | FtpStateData::checkAuth(const HttpHeader * req_hdr) | |
429fdbec | 1320 | { |
63259c34 | 1321 | const char *auth; |
62e76326 | 1322 | |
f381d699 AJ |
1323 | /* default username */ |
1324 | xstrncpy(user, "anonymous", MAX_URL); | |
62e76326 | 1325 | |
f381d699 AJ |
1326 | /* Check HTTP Authorization: headers (better than defaults, but less than URL) */ |
1327 | if ( (auth = req_hdr->getAuth(HDR_AUTHORIZATION, "Basic")) ) { | |
1328 | flags.authenticated = 1; | |
1329 | loginParser(auth, FTP_LOGIN_NOT_ESCAPED); | |
1330 | } | |
1331 | /* we fail with authorization-required error later IFF the FTP server requests it */ | |
62e76326 | 1332 | |
f381d699 AJ |
1333 | /* Test URL login syntax. Overrides any headers received. */ |
1334 | loginParser(request->login, FTP_LOGIN_ESCAPED); | |
62e76326 | 1335 | |
f381d699 AJ |
1336 | /* name is missing. thats fatal. */ |
1337 | if (!user[0]) | |
1338 | fatal("FTP login parsing destroyed username info"); | |
62e76326 | 1339 | |
f381d699 AJ |
1340 | /* name + password == success */ |
1341 | if (password[0]) | |
1342 | return 1; | |
62e76326 | 1343 | |
f381d699 AJ |
1344 | /* Setup default FTP password settings */ |
1345 | /* this has to be done last so that we can have a no-password case above. */ | |
1346 | if (!password[0]) { | |
4e2da309 | 1347 | if (strcmp(user, "anonymous") == 0 && !flags.tried_auth_anonymous) { |
f381d699 | 1348 | xstrncpy(password, Config.Ftp.anon_user, MAX_URL); |
4e2da309 AJ |
1349 | flags.tried_auth_anonymous=1; |
1350 | return 1; | |
e1381638 | 1351 | } else if (!flags.tried_auth_nopass) { |
f381d699 | 1352 | xstrncpy(password, null_string, MAX_URL); |
4e2da309 AJ |
1353 | flags.tried_auth_nopass=1; |
1354 | return 1; | |
1355 | } | |
429fdbec | 1356 | } |
62e76326 | 1357 | |
429fdbec | 1358 | return 0; /* different username */ |
1359 | } | |
1360 | ||
811d914c | 1361 | static String str_type_eq; |
f1a83a7d | 1362 | void |
1363 | FtpStateData::checkUrlpath() | |
13e80e5b | 1364 | { |
13e80e5b | 1365 | int l; |
9b558d8a | 1366 | size_t t; |
62e76326 | 1367 | |
811d914c FC |
1368 | if (str_type_eq.undefined()) //hack. String doesn't support global-static |
1369 | str_type_eq="type="; | |
1370 | ||
2c1fd837 | 1371 | if ((t = request->urlpath.rfind(';')) != String::npos) { |
9b558d8a FC |
1372 | if (request->urlpath.substr(t+1,t+1+str_type_eq.size())==str_type_eq) { |
1373 | typecode = (char)xtoupper(request->urlpath[t+str_type_eq.size()+1]); | |
1374 | request->urlpath.cut(t); | |
62e76326 | 1375 | } |
dbfed404 | 1376 | } |
62e76326 | 1377 | |
528b2c61 | 1378 | l = request->urlpath.size(); |
13e80e5b | 1379 | /* check for null path */ |
62e76326 | 1380 | |
02922e76 | 1381 | if (!l) { |
f1a83a7d | 1382 | flags.isdir = 1; |
1383 | flags.root_dir = 1; | |
1384 | flags.need_base_href = 1; /* Work around broken browsers */ | |
30abd221 | 1385 | } else if (!request->urlpath.cmp("/%2f/")) { |
62e76326 | 1386 | /* UNIX root directory */ |
f1a83a7d | 1387 | flags.isdir = 1; |
1388 | flags.root_dir = 1; | |
a313a9ba | 1389 | } else if ((l >= 1) && (request->urlpath[l - 1] == '/')) { |
62e76326 | 1390 | /* Directory URL, ending in / */ |
f1a83a7d | 1391 | flags.isdir = 1; |
62e76326 | 1392 | |
1393 | if (l == 1) | |
f1a83a7d | 1394 | flags.root_dir = 1; |
d5f80edc | 1395 | } else { |
f1a83a7d | 1396 | flags.dir_slash = 1; |
13e80e5b | 1397 | } |
1398 | } | |
1399 | ||
f1a83a7d | 1400 | void |
1401 | FtpStateData::buildTitleUrl() | |
3fdadc70 | 1402 | { |
f1a83a7d | 1403 | title_url = "ftp://"; |
62e76326 | 1404 | |
f1a83a7d | 1405 | if (strcmp(user, "anonymous")) { |
1406 | title_url.append(user); | |
1407 | title_url.append("@"); | |
06a5d871 | 1408 | } |
62e76326 | 1409 | |
cc192b50 | 1410 | title_url.append(request->GetHost()); |
62e76326 | 1411 | |
06a5d871 | 1412 | if (request->port != urlDefaultPort(PROTO_FTP)) { |
f1a83a7d | 1413 | title_url.append(":"); |
1414 | title_url.append(xitoa(request->port)); | |
3fdadc70 | 1415 | } |
62e76326 | 1416 | |
f1a83a7d | 1417 | title_url.append (request->urlpath); |
06a5d871 | 1418 | |
f1a83a7d | 1419 | base_href = "ftp://"; |
62e76326 | 1420 | |
f1a83a7d | 1421 | if (strcmp(user, "anonymous") != 0) { |
1422 | base_href.append(rfc1738_escape_part(user)); | |
62e76326 | 1423 | |
f1a83a7d | 1424 | if (password_url) { |
1425 | base_href.append (":"); | |
1426 | base_href.append(rfc1738_escape_part(password)); | |
62e76326 | 1427 | } |
1428 | ||
f1a83a7d | 1429 | base_href.append("@"); |
06a5d871 | 1430 | } |
62e76326 | 1431 | |
cc192b50 | 1432 | base_href.append(request->GetHost()); |
62e76326 | 1433 | |
06a5d871 | 1434 | if (request->port != urlDefaultPort(PROTO_FTP)) { |
f1a83a7d | 1435 | base_href.append(":"); |
1436 | base_href.append(xitoa(request->port)); | |
9bc73deb | 1437 | } |
62e76326 | 1438 | |
f1a83a7d | 1439 | base_href.append(request->urlpath); |
1440 | base_href.append("/"); | |
3fdadc70 | 1441 | } |
1442 | ||
63be0a78 | 1443 | /// \ingroup ServerProtocolFTPAPI |
770f051d | 1444 | void |
db1cd23c | 1445 | ftpStart(FwdState * fwd) |
0a0bf5db | 1446 | { |
6f0aab86 | 1447 | FtpStateData *ftpState = new FtpStateData(fwd); |
1448 | ftpState->start(); | |
1449 | } | |
62e76326 | 1450 | |
6f0aab86 | 1451 | void |
1452 | FtpStateData::start() | |
1453 | { | |
f1a83a7d | 1454 | if (!checkAuth(&request->header)) { |
62e76326 | 1455 | /* create appropriate reply */ |
f381d699 | 1456 | HttpReply *reply = ftpAuthRequired(request, ftpRealm()); |
db237875 | 1457 | entry->replaceHttpReply(reply); |
5f8252d2 | 1458 | serverComplete(); |
62e76326 | 1459 | return; |
e381a13d | 1460 | } |
62e76326 | 1461 | |
f1a83a7d | 1462 | checkUrlpath(); |
1463 | buildTitleUrl(); | |
a689bd4e | 1464 | debugs(9, 5, HERE << "host=" << request->GetHost() << ", path=" << |
d53b3f6d | 1465 | request->urlpath << ", user=" << user << ", passwd=" << |
bf8fe701 | 1466 | password); |
1467 | ||
6f0aab86 | 1468 | state = BEGIN; |
1469 | ctrl.last_command = xstrdup("Connect to server"); | |
1470 | ctrl.buf = (char *)memAllocBuf(4096, &ctrl.size); | |
1471 | ctrl.offset = 0; | |
253caccb | 1472 | data.readBuf = new MemBuf; |
1473 | data.readBuf->init(4096, SQUID_TCP_SO_RCVBUF); | |
6f0aab86 | 1474 | scheduleReadControlReply(0); |
090089c4 | 1475 | } |
1476 | ||
3fdadc70 | 1477 | /* ====================================================================== */ |
1478 | ||
63be0a78 | 1479 | /// \ingroup ServerProtocolFTPInternal |
dad0fe12 | 1480 | static char * |
1481 | escapeIAC(const char *buf) | |
1482 | { | |
1483 | int n; | |
1484 | char *ret; | |
1485 | unsigned const char *p; | |
1486 | unsigned char *r; | |
1487 | ||
1488 | for (p = (unsigned const char *)buf, n = 1; *p; n++, p++) | |
1489 | if (*p == 255) | |
1490 | n++; | |
1491 | ||
1492 | ret = (char *)xmalloc(n); | |
1493 | ||
1494 | for (p = (unsigned const char *)buf, r=(unsigned char *)ret; *p; p++) { | |
1495 | *r++ = *p; | |
1496 | ||
1497 | if (*p == 255) | |
1498 | *r++ = 255; | |
1499 | } | |
1500 | ||
1501 | *r++ = '\0'; | |
1502 | assert((r - (unsigned char *)ret) == n ); | |
1503 | return ret; | |
1504 | } | |
1505 | ||
2f47fadf | 1506 | void |
1507 | FtpStateData::writeCommand(const char *buf) | |
234967c9 | 1508 | { |
dad0fe12 | 1509 | char *ebuf; |
a689bd4e | 1510 | /* trace FTP protocol communications at level 2 */ |
1511 | debugs(9, 2, "ftp<< " << buf); | |
dad0fe12 | 1512 | |
1513 | if (Config.Ftp.telnet) | |
1514 | ebuf = escapeIAC(buf); | |
1515 | else | |
1516 | ebuf = xstrdup(buf); | |
1517 | ||
2f47fadf | 1518 | safe_free(ctrl.last_command); |
dad0fe12 | 1519 | |
2f47fadf | 1520 | safe_free(ctrl.last_reply); |
dad0fe12 | 1521 | |
2f47fadf | 1522 | ctrl.last_command = ebuf; |
dad0fe12 | 1523 | |
dc56a9b1 | 1524 | typedef CommCbMemFunT<FtpStateData, CommIoCbParams> Dialer; |
1525 | AsyncCall::Pointer call = asyncCall(9, 5, "FtpStateData::ftpWriteCommandCallback", | |
9e008dda | 1526 | Dialer(this, &FtpStateData::ftpWriteCommandCallback)); |
2f47fadf | 1527 | comm_write(ctrl.fd, |
1528 | ctrl.last_command, | |
1529 | strlen(ctrl.last_command), | |
9e008dda | 1530 | call); |
dad0fe12 | 1531 | |
2f47fadf | 1532 | scheduleReadControlReply(0); |
3fdadc70 | 1533 | } |
1534 | ||
6f0aab86 | 1535 | void |
dc56a9b1 | 1536 | FtpStateData::ftpWriteCommandCallback(const CommIoCbParams &io) |
3fdadc70 | 1537 | { |
6800b5b5 | 1538 | |
dc56a9b1 | 1539 | debugs(9, 5, "ftpWriteCommandCallback: wrote " << io.size << " bytes"); |
62e76326 | 1540 | |
dc56a9b1 | 1541 | if (io.size > 0) { |
1542 | fd_bytes(io.fd, io.size, FD_WRITE); | |
1543 | kb_incr(&statCounter.server.all.kbytes_out, io.size); | |
1544 | kb_incr(&statCounter.server.ftp.kbytes_out, io.size); | |
ee1679df | 1545 | } |
62e76326 | 1546 | |
dc56a9b1 | 1547 | if (io.flag == COMM_ERR_CLOSING) |
62e76326 | 1548 | return; |
1549 | ||
dc56a9b1 | 1550 | if (io.flag) { |
1551 | debugs(9, DBG_IMPORTANT, "ftpWriteCommandCallback: FD " << io.fd << ": " << xstrerr(io.xerrno)); | |
1552 | failed(ERR_WRITE_ERROR, io.xerrno); | |
6f0aab86 | 1553 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 1554 | return; |
3fdadc70 | 1555 | } |
1556 | } | |
1557 | ||
6f0aab86 | 1558 | wordlist * |
47f6e231 | 1559 | FtpStateData::ftpParseControlReply(char *buf, size_t len, int *codep, size_t *used) |
3fdadc70 | 1560 | { |
1561 | char *s; | |
4f310655 | 1562 | char *sbuf; |
1563 | char *end; | |
1564 | int usable; | |
3fdadc70 | 1565 | int complete = 0; |
51eeadc6 | 1566 | wordlist *head = NULL; |
3fdadc70 | 1567 | wordlist *list; |
1568 | wordlist **tail = &head; | |
47f6e231 | 1569 | size_t offset; |
3fdadc70 | 1570 | size_t linelen; |
b5639035 | 1571 | int code = -1; |
a689bd4e | 1572 | debugs(9, 3, HERE); |
4f310655 | 1573 | /* |
1574 | * We need a NULL-terminated buffer for scanning, ick | |
1575 | */ | |
e6ccf245 | 1576 | sbuf = (char *)xmalloc(len + 1); |
4f310655 | 1577 | xstrncpy(sbuf, buf, len + 1); |
1578 | end = sbuf + len - 1; | |
62e76326 | 1579 | |
4f310655 | 1580 | while (*end != '\r' && *end != '\n' && end > sbuf) |
62e76326 | 1581 | end--; |
1582 | ||
4f310655 | 1583 | usable = end - sbuf; |
62e76326 | 1584 | |
a689bd4e | 1585 | debugs(9, 3, HERE << "usable = " << usable); |
62e76326 | 1586 | |
4f310655 | 1587 | if (usable == 0) { |
a689bd4e | 1588 | debugs(9, 3, HERE << "didn't find end of line"); |
62e76326 | 1589 | safe_free(sbuf); |
1590 | return NULL; | |
4f310655 | 1591 | } |
62e76326 | 1592 | |
a689bd4e | 1593 | debugs(9, 3, HERE << len << " bytes to play with"); |
4f310655 | 1594 | end++; |
1595 | s = sbuf; | |
1596 | s += strspn(s, crlf); | |
62e76326 | 1597 | |
4f310655 | 1598 | for (; s < end; s += strcspn(s, crlf), s += strspn(s, crlf)) { |
62e76326 | 1599 | if (complete) |
1600 | break; | |
1601 | ||
a689bd4e | 1602 | debugs(9, 5, HERE << "s = {" << s << "}"); |
62e76326 | 1603 | |
1604 | linelen = strcspn(s, crlf) + 1; | |
1605 | ||
1606 | if (linelen < 2) | |
1607 | break; | |
1608 | ||
1609 | if (linelen > 3) | |
1610 | complete = (*s >= '0' && *s <= '9' && *(s + 3) == ' '); | |
1611 | ||
1612 | if (complete) | |
1613 | code = atoi(s); | |
1614 | ||
1615 | offset = 0; | |
1616 | ||
1617 | if (linelen > 3) | |
1618 | if (*s >= '0' && *s <= '9' && (*(s + 3) == '-' || *(s + 3) == ' ')) | |
1619 | offset = 4; | |
1620 | ||
d295d770 | 1621 | list = new wordlist(); |
62e76326 | 1622 | |
1623 | list->key = (char *)xmalloc(linelen - offset); | |
1624 | ||
1625 | xstrncpy(list->key, s + offset, linelen - offset); | |
1626 | ||
a689bd4e | 1627 | /* trace the FTP communication chat at level 2 */ |
1628 | debugs(9, 2, "ftp>> " << code << " " << list->key); | |
62e76326 | 1629 | |
1630 | *tail = list; | |
1631 | ||
1632 | tail = &list->next; | |
3fdadc70 | 1633 | } |
62e76326 | 1634 | |
47f6e231 | 1635 | *used = (size_t) (s - sbuf); |
1c34d125 | 1636 | safe_free(sbuf); |
62e76326 | 1637 | |
3fdadc70 | 1638 | if (!complete) |
62e76326 | 1639 | wordlistDestroy(&head); |
1640 | ||
3fdadc70 | 1641 | if (codep) |
62e76326 | 1642 | *codep = code; |
1643 | ||
3fdadc70 | 1644 | return head; |
1645 | } | |
1646 | ||
63be0a78 | 1647 | /** |
4e849646 | 1648 | * DPW 2007-04-23 |
1649 | * Looks like there are no longer anymore callers that set | |
1650 | * buffered_ok=1. Perhaps it can be removed at some point. | |
1651 | */ | |
6f0aab86 | 1652 | void |
1653 | FtpStateData::scheduleReadControlReply(int buffered_ok) | |
4f310655 | 1654 | { |
a689bd4e | 1655 | debugs(9, 3, HERE << "FD " << ctrl.fd); |
62e76326 | 1656 | |
6f0aab86 | 1657 | if (buffered_ok && ctrl.offset > 0) { |
62e76326 | 1658 | /* We've already read some reply data */ |
6f0aab86 | 1659 | handleControlReply(); |
4f310655 | 1660 | } else { |
62e76326 | 1661 | /* XXX What about Config.Timeout.read? */ |
9e008dda AJ |
1662 | typedef CommCbMemFunT<FtpStateData, CommIoCbParams> Dialer; |
1663 | AsyncCall::Pointer reader=asyncCall(9, 5, "FtpStateData::ftpReadControlReply", | |
1664 | Dialer(this, &FtpStateData::ftpReadControlReply)); | |
1665 | comm_read(ctrl.fd, ctrl.buf + ctrl.offset, ctrl.size - ctrl.offset, reader); | |
62e76326 | 1666 | /* |
1667 | * Cancel the timeout on the Data socket (if any) and | |
1668 | * establish one on the control socket. | |
1669 | */ | |
1670 | ||
9e008dda AJ |
1671 | if (data.fd > -1) { |
1672 | AsyncCall::Pointer nullCall = NULL; | |
dc56a9b1 | 1673 | commSetTimeout(data.fd, -1, nullCall); |
9e008dda | 1674 | } |
dc56a9b1 | 1675 | |
9e008dda AJ |
1676 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
1677 | AsyncCall::Pointer timeoutCall = asyncCall(9, 5, "FtpStateData::ftpTimeout", | |
1678 | TimeoutDialer(this,&FtpStateData::ftpTimeout)); | |
62e76326 | 1679 | |
dc56a9b1 | 1680 | commSetTimeout(ctrl.fd, Config.Timeout.read, timeoutCall); |
4f310655 | 1681 | } |
1682 | } | |
1683 | ||
dc56a9b1 | 1684 | void FtpStateData::ftpReadControlReply(const CommIoCbParams &io) |
3fdadc70 | 1685 | { |
dc56a9b1 | 1686 | debugs(9, 3, "ftpReadControlReply: FD " << io.fd << ", Read " << io.size << " bytes"); |
c4b7a5a9 | 1687 | |
dc56a9b1 | 1688 | if (io.size > 0) { |
1689 | kb_incr(&statCounter.server.all.kbytes_in, io.size); | |
1690 | kb_incr(&statCounter.server.ftp.kbytes_in, io.size); | |
3c7568c7 | 1691 | } |
62e76326 | 1692 | |
dc56a9b1 | 1693 | if (io.flag == COMM_ERR_CLOSING) |
c4b7a5a9 | 1694 | return; |
62e76326 | 1695 | |
9bc73deb | 1696 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
dc56a9b1 | 1697 | abortTransaction("entry aborted during control reply read"); |
62e76326 | 1698 | return; |
9bc73deb | 1699 | } |
62e76326 | 1700 | |
dc56a9b1 | 1701 | assert(ctrl.offset < ctrl.size); |
62e76326 | 1702 | |
dc56a9b1 | 1703 | if (io.flag == COMM_OK && io.size > 0) { |
1704 | fd_bytes(io.fd, io.size, FD_READ); | |
ee1679df | 1705 | } |
62e76326 | 1706 | |
dc56a9b1 | 1707 | if (io.flag != COMM_OK || io.size < 0) { |
9e008dda AJ |
1708 | debugs(50, ignoreErrno(io.xerrno) ? 3 : DBG_IMPORTANT, |
1709 | "ftpReadControlReply: read error: " << xstrerr(io.xerrno)); | |
62e76326 | 1710 | |
dc56a9b1 | 1711 | if (ignoreErrno(io.xerrno)) { |
1712 | scheduleReadControlReply(0); | |
62e76326 | 1713 | } else { |
dc56a9b1 | 1714 | failed(ERR_READ_ERROR, io.xerrno); |
6f0aab86 | 1715 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 1716 | return; |
1717 | } | |
1718 | ||
1719 | return; | |
3fdadc70 | 1720 | } |
62e76326 | 1721 | |
dc56a9b1 | 1722 | if (io.size == 0) { |
62e76326 | 1723 | if (entry->store_status == STORE_PENDING) { |
dc56a9b1 | 1724 | failed(ERR_FTP_FAILURE, 0); |
6f0aab86 | 1725 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 1726 | return; |
1727 | } | |
1728 | ||
9e008dda | 1729 | /* XXX this may end up having to be serverComplete() .. */ |
dc56a9b1 | 1730 | abortTransaction("zero control reply read"); |
62e76326 | 1731 | return; |
3fdadc70 | 1732 | } |
62e76326 | 1733 | |
dc56a9b1 | 1734 | unsigned int len =io.size + ctrl.offset; |
1735 | ctrl.offset = len; | |
1736 | assert(len <= ctrl.size); | |
1737 | handleControlReply(); | |
4f310655 | 1738 | } |
1739 | ||
6f0aab86 | 1740 | void |
1741 | FtpStateData::handleControlReply() | |
4f310655 | 1742 | { |
ec603b25 | 1743 | wordlist **W; |
47f6e231 | 1744 | size_t bytes_used = 0; |
6f0aab86 | 1745 | wordlistDestroy(&ctrl.message); |
1746 | ctrl.message = ftpParseControlReply(ctrl.buf, | |
1747 | ctrl.offset, &ctrl.replycode, &bytes_used); | |
62e76326 | 1748 | |
6f0aab86 | 1749 | if (ctrl.message == NULL) { |
62e76326 | 1750 | /* didn't get complete reply yet */ |
1751 | ||
47f6e231 | 1752 | if (ctrl.offset == ctrl.size) { |
6f0aab86 | 1753 | ctrl.buf = (char *)memReallocBuf(ctrl.buf, ctrl.size << 1, &ctrl.size); |
62e76326 | 1754 | } |
1755 | ||
6f0aab86 | 1756 | scheduleReadControlReply(0); |
62e76326 | 1757 | return; |
6f0aab86 | 1758 | } else if (ctrl.offset == bytes_used) { |
62e76326 | 1759 | /* used it all up */ |
6f0aab86 | 1760 | ctrl.offset = 0; |
4f310655 | 1761 | } else { |
62e76326 | 1762 | /* Got some data past the complete reply */ |
6f0aab86 | 1763 | assert(bytes_used < ctrl.offset); |
1764 | ctrl.offset -= bytes_used; | |
1765 | xmemmove(ctrl.buf, ctrl.buf + bytes_used, | |
1766 | ctrl.offset); | |
3fdadc70 | 1767 | } |
62e76326 | 1768 | |
858783c9 | 1769 | /* Move the last line of the reply message to ctrl.last_reply */ |
3d0ac046 | 1770 | for (W = &ctrl.message; (*W)->next; W = &(*W)->next); |
6f0aab86 | 1771 | safe_free(ctrl.last_reply); |
62e76326 | 1772 | |
6f0aab86 | 1773 | ctrl.last_reply = xstrdup((*W)->key); |
62e76326 | 1774 | |
858783c9 | 1775 | wordlistDestroy(W); |
62e76326 | 1776 | |
858783c9 | 1777 | /* Copy the rest of the message to cwd_message to be printed in |
1778 | * error messages | |
1779 | */ | |
0477a072 AJ |
1780 | if (ctrl.message) { |
1781 | for (wordlist *w = ctrl.message; w; w = w->next) { | |
1782 | cwd_message.append('\n'); | |
1783 | cwd_message.append(w->key); | |
1784 | } | |
1785 | } | |
62e76326 | 1786 | |
a689bd4e | 1787 | debugs(9, 3, HERE << "state=" << state << ", code=" << ctrl.replycode); |
62e76326 | 1788 | |
6f0aab86 | 1789 | FTP_SM_FUNCS[state] (this); |
234967c9 | 1790 | } |
1791 | ||
3fdadc70 | 1792 | /* ====================================================================== */ |
1793 | ||
63be0a78 | 1794 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 1795 | static void |
1796 | ftpReadWelcome(FtpStateData * ftpState) | |
1797 | { | |
1798 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 1799 | debugs(9, 3, HERE); |
62e76326 | 1800 | |
e55f0142 | 1801 | if (ftpState->flags.pasv_only) |
62e76326 | 1802 | ftpState->login_att++; |
1803 | ||
9bc73deb | 1804 | /* Dont retry if the FTP server accepted the connection */ |
b6b6f466 | 1805 | ftpState->fwd->dontRetry(true); |
62e76326 | 1806 | |
3fdadc70 | 1807 | if (code == 220) { |
62e76326 | 1808 | if (ftpState->ctrl.message) { |
1809 | if (strstr(ftpState->ctrl.message->key, "NetWare")) | |
1810 | ftpState->flags.skip_whitespace = 1; | |
1811 | } | |
1812 | ||
1813 | ftpSendUser(ftpState); | |
cdc33f35 | 1814 | } else if (code == 120) { |
62e76326 | 1815 | if (NULL != ftpState->ctrl.message) |
a689bd4e | 1816 | debugs(9, DBG_IMPORTANT, "FTP server is busy: " << ftpState->ctrl.message->key); |
62e76326 | 1817 | |
1818 | return; | |
3fdadc70 | 1819 | } else { |
62e76326 | 1820 | ftpFail(ftpState); |
3fdadc70 | 1821 | } |
1822 | } | |
1823 | ||
f381d699 AJ |
1824 | /** |
1825 | * Translate FTP login failure into HTTP error | |
1826 | * this is an attmpt to get the 407 message to show up outside Squid. | |
1827 | * its NOT a general failure. But a correct FTP response type. | |
1828 | */ | |
1829 | void | |
1830 | FtpStateData::loginFailed() | |
1831 | { | |
1832 | ErrorState *err = NULL; | |
1833 | const char *command, *reply; | |
1834 | ||
1835 | if (state == SENT_USER || state == SENT_PASS) { | |
1836 | if (ctrl.replycode > 500) { | |
1837 | if (password_url) | |
1838 | err = errorCon(ERR_FTP_FORBIDDEN, HTTP_FORBIDDEN, fwd->request); | |
1839 | else | |
1840 | err = errorCon(ERR_FTP_FORBIDDEN, HTTP_UNAUTHORIZED, fwd->request); | |
1841 | } else if (ctrl.replycode == 421) { | |
1842 | err = errorCon(ERR_FTP_UNAVAILABLE, HTTP_SERVICE_UNAVAILABLE, fwd->request); | |
1843 | } | |
1844 | } else { | |
1845 | ftpFail(this); | |
1846 | return; | |
1847 | } | |
1848 | ||
1849 | err->ftp.server_msg = ctrl.message; | |
1850 | ||
1851 | ctrl.message = NULL; | |
1852 | ||
1853 | if (old_request) | |
1854 | command = old_request; | |
1855 | else | |
1856 | command = ctrl.last_command; | |
1857 | ||
1858 | if (command && strncmp(command, "PASS", 4) == 0) | |
1859 | command = "PASS <yourpassword>"; | |
1860 | ||
1861 | if (old_reply) | |
1862 | reply = old_reply; | |
1863 | else | |
1864 | reply = ctrl.last_reply; | |
1865 | ||
1866 | if (command) | |
1867 | err->ftp.request = xstrdup(command); | |
1868 | ||
1869 | if (reply) | |
1870 | err->ftp.reply = xstrdup(reply); | |
1871 | ||
1872 | ||
1873 | HttpReply *newrep = err->BuildHttpReply(); | |
1874 | errorStateFree(err); | |
1875 | /* add Authenticate header */ | |
1876 | newrep->header.putAuth("Basic", ftpRealm()); | |
1877 | ||
1878 | // add it to the store entry for response.... | |
1879 | entry->replaceHttpReply(newrep); | |
1880 | serverComplete(); | |
1881 | } | |
1882 | ||
1883 | const char * | |
1884 | FtpStateData::ftpRealm() | |
1885 | { | |
1886 | static char realm[8192]; | |
1887 | ||
1888 | /* This request is not fully authenticated */ | |
1889 | if (!request) { | |
1890 | snprintf(realm, 8192, "FTP %s unknown", user); | |
1891 | } else if (request->port == 21) { | |
1892 | snprintf(realm, 8192, "FTP %s %s", user, request->GetHost()); | |
1893 | } else { | |
1894 | snprintf(realm, 8192, "FTP %s %s port %d", user, request->GetHost(), request->port); | |
1895 | } | |
1896 | return realm; | |
1897 | } | |
1898 | ||
63be0a78 | 1899 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 1900 | static void |
1901 | ftpSendUser(FtpStateData * ftpState) | |
1902 | { | |
a11382aa | 1903 | /* check the server control channel is still available */ |
9e008dda | 1904 | if (!ftpState || !ftpState->haveControlChannel("ftpSendUser")) |
a11382aa | 1905 | return; |
1906 | ||
969c39b9 | 1907 | if (ftpState->proxy_host != NULL) |
62e76326 | 1908 | snprintf(cbuf, 1024, "USER %s@%s\r\n", |
1909 | ftpState->user, | |
cc192b50 | 1910 | ftpState->request->GetHost()); |
969c39b9 | 1911 | else |
62e76326 | 1912 | snprintf(cbuf, 1024, "USER %s\r\n", ftpState->user); |
1913 | ||
2f47fadf | 1914 | ftpState->writeCommand(cbuf); |
62e76326 | 1915 | |
969c39b9 | 1916 | ftpState->state = SENT_USER; |
1917 | } | |
1918 | ||
63be0a78 | 1919 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 1920 | static void |
1921 | ftpReadUser(FtpStateData * ftpState) | |
234967c9 | 1922 | { |
3fdadc70 | 1923 | int code = ftpState->ctrl.replycode; |
a689bd4e | 1924 | debugs(9, 3, HERE); |
62e76326 | 1925 | |
3fdadc70 | 1926 | if (code == 230) { |
62e76326 | 1927 | ftpReadPass(ftpState); |
3fdadc70 | 1928 | } else if (code == 331) { |
62e76326 | 1929 | ftpSendPass(ftpState); |
3fdadc70 | 1930 | } else { |
f381d699 | 1931 | ftpState->loginFailed(); |
3fdadc70 | 1932 | } |
1933 | } | |
1934 | ||
63be0a78 | 1935 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 1936 | static void |
1937 | ftpSendPass(FtpStateData * ftpState) | |
1938 | { | |
a11382aa | 1939 | /* check the server control channel is still available */ |
9e008dda | 1940 | if (!ftpState || !ftpState->haveControlChannel("ftpSendPass")) |
a11382aa | 1941 | return; |
1942 | ||
969c39b9 | 1943 | snprintf(cbuf, 1024, "PASS %s\r\n", ftpState->password); |
2f47fadf | 1944 | ftpState->writeCommand(cbuf); |
969c39b9 | 1945 | ftpState->state = SENT_PASS; |
1946 | } | |
1947 | ||
63be0a78 | 1948 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 1949 | static void |
1950 | ftpReadPass(FtpStateData * ftpState) | |
1951 | { | |
1952 | int code = ftpState->ctrl.replycode; | |
4d6c56a6 | 1953 | debugs(9, 3, HERE << "code=" << code); |
62e76326 | 1954 | |
3fdadc70 | 1955 | if (code == 230) { |
62e76326 | 1956 | ftpSendType(ftpState); |
3fdadc70 | 1957 | } else { |
f381d699 | 1958 | ftpState->loginFailed(); |
3fdadc70 | 1959 | } |
1960 | } | |
1961 | ||
63be0a78 | 1962 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 1963 | static void |
1964 | ftpSendType(FtpStateData * ftpState) | |
1965 | { | |
02922e76 | 1966 | const char *t; |
1967 | const char *filename; | |
969c39b9 | 1968 | char mode; |
a11382aa | 1969 | |
1970 | /* check the server control channel is still available */ | |
9e008dda | 1971 | if (!ftpState || !ftpState->haveControlChannel("ftpSendType")) |
a11382aa | 1972 | return; |
1973 | ||
9e242e02 | 1974 | /* |
1975 | * Ref section 3.2.2 of RFC 1738 | |
1976 | */ | |
b02bfc2d | 1977 | mode = ftpState->typecode; |
62e76326 | 1978 | |
b02bfc2d | 1979 | switch (mode) { |
62e76326 | 1980 | |
9e242e02 | 1981 | case 'D': |
62e76326 | 1982 | mode = 'A'; |
1983 | break; | |
1984 | ||
9e242e02 | 1985 | case 'A': |
62e76326 | 1986 | |
9e242e02 | 1987 | case 'I': |
62e76326 | 1988 | break; |
1989 | ||
9e242e02 | 1990 | default: |
62e76326 | 1991 | |
1992 | if (ftpState->flags.isdir) { | |
1993 | mode = 'A'; | |
1994 | } else { | |
650c4b88 | 1995 | t = ftpState->request->urlpath.rpos('/'); |
d53b3f6d | 1996 | filename = t ? t + 1 : ftpState->request->urlpath.termedBuf(); |
62e76326 | 1997 | mode = mimeGetTransferMode(filename); |
1998 | } | |
1999 | ||
2000 | break; | |
9e242e02 | 2001 | } |
62e76326 | 2002 | |
969c39b9 | 2003 | if (mode == 'I') |
62e76326 | 2004 | ftpState->flags.binary = 1; |
cfbf5373 | 2005 | else |
62e76326 | 2006 | ftpState->flags.binary = 0; |
2007 | ||
969c39b9 | 2008 | snprintf(cbuf, 1024, "TYPE %c\r\n", mode); |
62e76326 | 2009 | |
2f47fadf | 2010 | ftpState->writeCommand(cbuf); |
62e76326 | 2011 | |
969c39b9 | 2012 | ftpState->state = SENT_TYPE; |
2013 | } | |
2014 | ||
63be0a78 | 2015 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2016 | static void |
2017 | ftpReadType(FtpStateData * ftpState) | |
2018 | { | |
2019 | int code = ftpState->ctrl.replycode; | |
3fdadc70 | 2020 | char *path; |
6e5ae4a4 | 2021 | char *d, *p; |
a689bd4e | 2022 | debugs(9, 3, HERE); |
62e76326 | 2023 | |
3fdadc70 | 2024 | if (code == 200) { |
d53b3f6d | 2025 | p = path = xstrdup(ftpState->request->urlpath.termedBuf()); |
62e76326 | 2026 | |
2027 | if (*p == '/') | |
2028 | p++; | |
2029 | ||
2030 | while (*p) { | |
2031 | d = p; | |
2032 | p += strcspn(p, "/"); | |
2033 | ||
2034 | if (*p) | |
2035 | *p++ = '\0'; | |
2036 | ||
2037 | rfc1738_unescape(d); | |
2038 | ||
80dc929d | 2039 | if (*d) |
2040 | wordlistAdd(&ftpState->pathcomps, d); | |
62e76326 | 2041 | } |
2042 | ||
2043 | xfree(path); | |
2044 | ||
2045 | if (ftpState->pathcomps) | |
2046 | ftpTraverseDirectory(ftpState); | |
2047 | else | |
2048 | ftpListDir(ftpState); | |
3fdadc70 | 2049 | } else { |
62e76326 | 2050 | ftpFail(ftpState); |
3fdadc70 | 2051 | } |
2052 | } | |
2053 | ||
63be0a78 | 2054 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2055 | static void |
969c39b9 | 2056 | ftpTraverseDirectory(FtpStateData * ftpState) |
3fdadc70 | 2057 | { |
2058 | wordlist *w; | |
a689bd4e | 2059 | debugs(9, 4, HERE << (ftpState->filepath ? ftpState->filepath : "<NULL>")); |
969c39b9 | 2060 | |
5627a633 | 2061 | safe_free(ftpState->dirpath); |
2062 | ftpState->dirpath = ftpState->filepath; | |
2063 | ftpState->filepath = NULL; | |
2064 | ||
969c39b9 | 2065 | /* Done? */ |
62e76326 | 2066 | |
969c39b9 | 2067 | if (ftpState->pathcomps == NULL) { |
a689bd4e | 2068 | debugs(9, 3, HERE << "the final component was a directory"); |
62e76326 | 2069 | ftpListDir(ftpState); |
2070 | return; | |
3fdadc70 | 2071 | } |
62e76326 | 2072 | |
969c39b9 | 2073 | /* Go to next path component */ |
2074 | w = ftpState->pathcomps; | |
62e76326 | 2075 | |
969c39b9 | 2076 | ftpState->filepath = w->key; |
62e76326 | 2077 | |
969c39b9 | 2078 | ftpState->pathcomps = w->next; |
62e76326 | 2079 | |
d295d770 | 2080 | delete w; |
62e76326 | 2081 | |
969c39b9 | 2082 | /* Check if we are to CWD or RETR */ |
e55f0142 | 2083 | if (ftpState->pathcomps != NULL || ftpState->flags.isdir) { |
62e76326 | 2084 | ftpSendCwd(ftpState); |
969c39b9 | 2085 | } else { |
a689bd4e | 2086 | debugs(9, 3, HERE << "final component is probably a file"); |
62e76326 | 2087 | ftpGetFile(ftpState); |
2088 | return; | |
969c39b9 | 2089 | } |
2090 | } | |
2091 | ||
63be0a78 | 2092 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 2093 | static void |
2094 | ftpSendCwd(FtpStateData * ftpState) | |
2095 | { | |
9ededfe2 | 2096 | char *path = NULL; |
a11382aa | 2097 | |
2098 | /* check the server control channel is still available */ | |
9e008dda | 2099 | if (!ftpState || !ftpState->haveControlChannel("ftpSendCwd")) |
a11382aa | 2100 | return; |
2101 | ||
a689bd4e | 2102 | debugs(9, 3, HERE); |
62e76326 | 2103 | |
9ededfe2 | 2104 | path = ftpState->filepath; |
2105 | ||
969c39b9 | 2106 | if (!strcmp(path, "..") || !strcmp(path, "/")) { |
62e76326 | 2107 | ftpState->flags.no_dotdot = 1; |
13e80e5b | 2108 | } else { |
62e76326 | 2109 | ftpState->flags.no_dotdot = 0; |
13e80e5b | 2110 | } |
62e76326 | 2111 | |
80dc929d | 2112 | snprintf(cbuf, 1024, "CWD %s\r\n", path); |
62e76326 | 2113 | |
2f47fadf | 2114 | ftpState->writeCommand(cbuf); |
62e76326 | 2115 | |
969c39b9 | 2116 | ftpState->state = SENT_CWD; |
3fdadc70 | 2117 | } |
77a30ebb | 2118 | |
63be0a78 | 2119 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2120 | static void |
2121 | ftpReadCwd(FtpStateData * ftpState) | |
2122 | { | |
2123 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 2124 | debugs(9, 3, HERE); |
62e76326 | 2125 | |
3fdadc70 | 2126 | if (code >= 200 && code < 300) { |
62e76326 | 2127 | /* CWD OK */ |
6f0aab86 | 2128 | ftpState->unhack(); |
62e76326 | 2129 | |
0477a072 AJ |
2130 | /* Reset cwd_message to only include the last message */ |
2131 | ftpState->cwd_message.reset(""); | |
2132 | for (wordlist *w = ftpState->ctrl.message; w; w = w->next) { | |
2133 | ftpState->cwd_message.append(' '); | |
2134 | ftpState->cwd_message.append(w->key); | |
2135 | } | |
62e76326 | 2136 | ftpState->ctrl.message = NULL; |
2137 | ||
2138 | /* Continue to traverse the path */ | |
2139 | ftpTraverseDirectory(ftpState); | |
3fdadc70 | 2140 | } else { |
62e76326 | 2141 | /* CWD FAILED */ |
2142 | ||
2143 | if (!ftpState->flags.put) | |
2144 | ftpFail(ftpState); | |
2145 | else | |
2146 | ftpSendMkdir(ftpState); | |
c021888f | 2147 | } |
3fdadc70 | 2148 | } |
2149 | ||
63be0a78 | 2150 | /// \ingroup ServerProtocolFTPInternal |
54220df8 | 2151 | static void |
94439e4e | 2152 | ftpSendMkdir(FtpStateData * ftpState) |
54220df8 | 2153 | { |
9ededfe2 | 2154 | char *path = NULL; |
a11382aa | 2155 | |
2156 | /* check the server control channel is still available */ | |
9e008dda | 2157 | if (!ftpState || !ftpState->haveControlChannel("ftpSendMkdir")) |
a11382aa | 2158 | return; |
2159 | ||
9ededfe2 | 2160 | path = ftpState->filepath; |
a689bd4e | 2161 | debugs(9, 3, HERE << "with path=" << path); |
4162ee3b | 2162 | snprintf(cbuf, 1024, "MKD %s\r\n", path); |
2f47fadf | 2163 | ftpState->writeCommand(cbuf); |
4162ee3b | 2164 | ftpState->state = SENT_MKDIR; |
54220df8 | 2165 | } |
2166 | ||
63be0a78 | 2167 | /// \ingroup ServerProtocolFTPInternal |
54220df8 | 2168 | static void |
4162ee3b | 2169 | ftpReadMkdir(FtpStateData * ftpState) |
2170 | { | |
2171 | char *path = ftpState->filepath; | |
2172 | int code = ftpState->ctrl.replycode; | |
2173 | ||
a689bd4e | 2174 | debugs(9, 3, HERE << "path " << path << ", code " << code); |
62e76326 | 2175 | |
4162ee3b | 2176 | if (code == 257) { /* success */ |
62e76326 | 2177 | ftpSendCwd(ftpState); |
4162ee3b | 2178 | } else if (code == 550) { /* dir exists */ |
62e76326 | 2179 | |
2180 | if (ftpState->flags.put_mkdir) { | |
2181 | ftpState->flags.put_mkdir = 1; | |
2182 | ftpSendCwd(ftpState); | |
2183 | } else | |
2184 | ftpSendReply(ftpState); | |
4162ee3b | 2185 | } else |
62e76326 | 2186 | ftpSendReply(ftpState); |
54220df8 | 2187 | } |
2188 | ||
63be0a78 | 2189 | /// \ingroup ServerProtocolFTPInternal |
dbfed404 | 2190 | static void |
2191 | ftpGetFile(FtpStateData * ftpState) | |
2192 | { | |
2193 | assert(*ftpState->filepath != '\0'); | |
e55f0142 | 2194 | ftpState->flags.isdir = 0; |
dbfed404 | 2195 | ftpSendMdtm(ftpState); |
2196 | } | |
2197 | ||
63be0a78 | 2198 | /// \ingroup ServerProtocolFTPInternal |
dbfed404 | 2199 | static void |
2200 | ftpListDir(FtpStateData * ftpState) | |
2201 | { | |
d5f80edc | 2202 | if (ftpState->flags.dir_slash) { |
a689bd4e | 2203 | debugs(9, 3, HERE << "Directory path did not end in /"); |
62e76326 | 2204 | ftpState->title_url.append("/"); |
2205 | ftpState->flags.isdir = 1; | |
dbfed404 | 2206 | } |
62e76326 | 2207 | |
a689bd4e | 2208 | ftpSendPassive(ftpState); |
dbfed404 | 2209 | } |
2210 | ||
63be0a78 | 2211 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 2212 | static void |
2213 | ftpSendMdtm(FtpStateData * ftpState) | |
2214 | { | |
a11382aa | 2215 | /* check the server control channel is still available */ |
9e008dda | 2216 | if (!ftpState || !ftpState->haveControlChannel("ftpSendMdtm")) |
a11382aa | 2217 | return; |
2218 | ||
969c39b9 | 2219 | assert(*ftpState->filepath != '\0'); |
2220 | snprintf(cbuf, 1024, "MDTM %s\r\n", ftpState->filepath); | |
2f47fadf | 2221 | ftpState->writeCommand(cbuf); |
969c39b9 | 2222 | ftpState->state = SENT_MDTM; |
2223 | } | |
2224 | ||
63be0a78 | 2225 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2226 | static void |
2227 | ftpReadMdtm(FtpStateData * ftpState) | |
2228 | { | |
2229 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 2230 | debugs(9, 3, HERE); |
62e76326 | 2231 | |
3fdadc70 | 2232 | if (code == 213) { |
62e76326 | 2233 | ftpState->mdtm = parse_iso3307_time(ftpState->ctrl.last_reply); |
6f0aab86 | 2234 | ftpState->unhack(); |
3fdadc70 | 2235 | } else if (code < 0) { |
62e76326 | 2236 | ftpFail(ftpState); |
9e008dda | 2237 | return; |
77a30ebb | 2238 | } |
62e76326 | 2239 | |
969c39b9 | 2240 | ftpSendSize(ftpState); |
2241 | } | |
2242 | ||
63be0a78 | 2243 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 2244 | static void |
2245 | ftpSendSize(FtpStateData * ftpState) | |
2246 | { | |
a11382aa | 2247 | /* check the server control channel is still available */ |
9e008dda | 2248 | if (!ftpState || !ftpState->haveControlChannel("ftpSendSize")) |
a11382aa | 2249 | return; |
2250 | ||
969c39b9 | 2251 | /* Only send SIZE for binary transfers. The returned size |
2252 | * is useless on ASCII transfers */ | |
62e76326 | 2253 | |
e55f0142 | 2254 | if (ftpState->flags.binary) { |
62e76326 | 2255 | assert(ftpState->filepath != NULL); |
2256 | assert(*ftpState->filepath != '\0'); | |
2257 | snprintf(cbuf, 1024, "SIZE %s\r\n", ftpState->filepath); | |
2f47fadf | 2258 | ftpState->writeCommand(cbuf); |
62e76326 | 2259 | ftpState->state = SENT_SIZE; |
969c39b9 | 2260 | } else |
62e76326 | 2261 | /* Skip to next state no non-binary transfers */ |
a689bd4e | 2262 | ftpSendPassive(ftpState); |
3fdadc70 | 2263 | } |
2264 | ||
63be0a78 | 2265 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2266 | static void |
2267 | ftpReadSize(FtpStateData * ftpState) | |
2268 | { | |
2269 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 2270 | debugs(9, 3, HERE); |
62e76326 | 2271 | |
3fdadc70 | 2272 | if (code == 213) { |
6f0aab86 | 2273 | ftpState->unhack(); |
47f6e231 | 2274 | ftpState->theSize = strtoll(ftpState->ctrl.last_reply, NULL, 10); |
62e76326 | 2275 | |
47f6e231 | 2276 | if (ftpState->theSize == 0) { |
a689bd4e | 2277 | debugs(9, 2, "SIZE reported " << |
9e008dda | 2278 | ftpState->ctrl.last_reply << " on " << |
d53b3f6d | 2279 | ftpState->title_url); |
47f6e231 | 2280 | ftpState->theSize = -1; |
62e76326 | 2281 | } |
3fdadc70 | 2282 | } else if (code < 0) { |
62e76326 | 2283 | ftpFail(ftpState); |
9e008dda | 2284 | return; |
3fdadc70 | 2285 | } |
62e76326 | 2286 | |
a689bd4e | 2287 | ftpSendPassive(ftpState); |
3fdadc70 | 2288 | } |
2289 | ||
63be0a78 | 2290 | /** |
2291 | \ingroup ServerProtocolFTPInternal | |
2292 | */ | |
cc192b50 | 2293 | static void |
a689bd4e | 2294 | ftpReadEPSV(FtpStateData* ftpState) |
cc192b50 | 2295 | { |
a689bd4e | 2296 | int code = ftpState->ctrl.replycode; |
2297 | char h1, h2, h3, h4; | |
2298 | int n; | |
2299 | u_short port; | |
ad61a2b4 | 2300 | IpAddress ipa_remote; |
a689bd4e | 2301 | int fd = ftpState->data.fd; |
2302 | char *buf; | |
2303 | debugs(9, 3, HERE); | |
2304 | ||
2305 | if (code != 229 && code != 522) { | |
9e008dda | 2306 | if (code == 200) { |
a689bd4e | 2307 | /* handle broken servers (RFC 2428 says OK code for EPSV MUST be 229 not 200) */ |
2308 | /* vsftpd for one send '200 EPSV ALL ok.' without even port info. | |
2309 | * Its okay to re-send EPSV 1/2 but nothing else. */ | |
2310 | debugs(9, DBG_IMPORTANT, "Broken FTP Server at " << fd_table[ftpState->ctrl.fd].ipaddr << ". Wrong accept code for EPSV"); | |
9e008dda | 2311 | } else { |
a689bd4e | 2312 | debugs(9, 2, "EPSV not supported by remote end"); |
2313 | ftpState->state = SENT_EPSV_1; /* simulate having failed EPSV 1 (last EPSV to try before shifting to PASV) */ | |
2314 | } | |
2315 | ftpSendPassive(ftpState); | |
2316 | return; | |
2317 | } | |
cc192b50 | 2318 | |
9e008dda | 2319 | if (code == 522) { |
a689bd4e | 2320 | /* server response with list of supported methods */ |
2321 | /* 522 Network protocol not supported, use (1) */ | |
2322 | /* 522 Network protocol not supported, use (1,2) */ | |
e1381638 | 2323 | /* TODO: handle the (1,2) case. We might get it back after EPSV ALL |
0f738a43 | 2324 | * which means close data + control without self-destructing and re-open from scratch. */ |
a689bd4e | 2325 | debugs(9, 5, HERE << "scanning: " << ftpState->ctrl.last_reply); |
0f738a43 AJ |
2326 | buf = ftpState->ctrl.last_reply; |
2327 | while (buf != NULL && *buf != '\0' && *buf != '\n' && *buf != '(') ++buf; | |
2328 | if (buf != NULL && *buf == '\n') ++buf; | |
cc192b50 | 2329 | |
9e008dda | 2330 | if (buf == NULL || *buf == '\0') { |
a689bd4e | 2331 | /* handle broken server (RFC 2428 says MUST specify supported protocols in 522) */ |
2332 | debugs(9, DBG_IMPORTANT, "Broken FTP Server at " << fd_table[ftpState->ctrl.fd].ipaddr << ". 522 error missing protocol negotiation hints"); | |
2333 | ftpSendPassive(ftpState); | |
9e008dda | 2334 | } else if (strcmp(buf, "(1)") == 0) { |
a689bd4e | 2335 | ftpState->state = SENT_EPSV_2; /* simulate having sent and failed EPSV 2 */ |
2336 | ftpSendPassive(ftpState); | |
9e008dda | 2337 | } else if (strcmp(buf, "(2)") == 0) { |
a689bd4e | 2338 | #if USE_IPV6 |
2339 | /* If server only supports EPSV 2 and we have already tried that. Go straight to EPRT */ | |
9e008dda | 2340 | if (ftpState->state == SENT_EPSV_2) { |
a689bd4e | 2341 | ftpSendEPRT(ftpState); |
9e008dda | 2342 | } else { |
a689bd4e | 2343 | /* or try the next Passive mode down the chain. */ |
2344 | ftpSendPassive(ftpState); | |
2345 | } | |
2346 | #else | |
2347 | /* We do not support IPv6. Remote server requires it. | |
2348 | So we must simulate having failed all EPSV methods. */ | |
2349 | ftpState->state = SENT_EPSV_1; | |
2350 | ftpSendPassive(ftpState); | |
2351 | #endif | |
e1381638 | 2352 | } else { |
0f738a43 AJ |
2353 | /* handle broken server (RFC 2428 says MUST specify supported protocols in 522) */ |
2354 | debugs(9, DBG_IMPORTANT, "WARNING: Server at " << fd_table[ftpState->ctrl.fd].ipaddr << " sent unknown protocol negotiation hint: " << buf); | |
2355 | ftpSendPassive(ftpState); | |
2356 | } | |
a689bd4e | 2357 | return; |
2358 | } | |
2359 | ||
2360 | /* 229 Entering Extended Passive Mode (|||port|) */ | |
2361 | /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */ | |
2362 | debugs(9, 5, "scanning: " << ftpState->ctrl.last_reply); | |
2363 | ||
2364 | buf = ftpState->ctrl.last_reply + strcspn(ftpState->ctrl.last_reply, "("); | |
2365 | ||
2366 | n = sscanf(buf, "(%c%c%c%hu%c)", &h1, &h2, &h3, &port, &h4); | |
2367 | ||
2368 | if (h1 != h2 || h1 != h3 || h1 != h4) { | |
2369 | debugs(9, DBG_IMPORTANT, "Invalid EPSV reply from " << | |
2370 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << | |
2371 | ftpState->ctrl.last_reply); | |
2372 | ||
2373 | ftpSendPassive(ftpState); | |
2374 | return; | |
2375 | } | |
2376 | ||
2377 | if (0 == port) { | |
2378 | debugs(9, DBG_IMPORTANT, "Unsafe EPSV reply from " << | |
2379 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << | |
2380 | ftpState->ctrl.last_reply); | |
2381 | ||
2382 | ftpSendPassive(ftpState); | |
2383 | return; | |
2384 | } | |
2385 | ||
2386 | if (Config.Ftp.sanitycheck) { | |
2387 | if (port < 1024) { | |
2388 | debugs(9, DBG_IMPORTANT, "Unsafe EPSV reply from " << | |
2389 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << | |
2390 | ftpState->ctrl.last_reply); | |
2391 | ||
2392 | ftpSendPassive(ftpState); | |
2393 | return; | |
2394 | } | |
2395 | } | |
2396 | ||
2397 | ftpState->data.port = port; | |
2398 | ||
2399 | ftpState->data.host = xstrdup(fd_table[ftpState->ctrl.fd].ipaddr); | |
2400 | ||
2401 | safe_free(ftpState->ctrl.last_command); | |
2402 | ||
2403 | safe_free(ftpState->ctrl.last_reply); | |
2404 | ||
2405 | ftpState->ctrl.last_command = xstrdup("Connect to server data port"); | |
2406 | ||
2407 | debugs(9, 3, HERE << "connecting to " << ftpState->data.host << ", port " << ftpState->data.port); | |
cc192b50 | 2408 | |
a689bd4e | 2409 | commConnectStart(fd, ftpState->data.host, port, FtpStateData::ftpPasvCallback, ftpState); |
2410 | } | |
cc192b50 | 2411 | |
63be0a78 | 2412 | /** \ingroup ServerProtocolFTPInternal |
2413 | * | |
a689bd4e | 2414 | * Send Passive connection request. |
2415 | * Default method is to use modern EPSV request. | |
2416 | * The failover mechanism should check for previous state and re-call with alternates on failure. | |
2417 | */ | |
3fdadc70 | 2418 | static void |
a689bd4e | 2419 | ftpSendPassive(FtpStateData * ftpState) |
3fdadc70 | 2420 | { |
ad61a2b4 | 2421 | IpAddress addr; |
cc192b50 | 2422 | struct addrinfo *AI = NULL; |
62e76326 | 2423 | |
a689bd4e | 2424 | /** Checks the server control channel is still available before running. */ |
9e008dda | 2425 | if (!ftpState || !ftpState->haveControlChannel("ftpSendPassive")) |
a11382aa | 2426 | return; |
2427 | ||
a689bd4e | 2428 | debugs(9, 3, HERE); |
2429 | ||
2430 | /** \par | |
2431 | * Checks for EPSV ALL special conditions: | |
2432 | * If enabled to be sent, squid MUST NOT request any other connect methods. | |
2433 | * If 'ALL' is sent and fails the entire FTP Session fails. | |
2434 | * NP: By my reading exact EPSV protocols maybe attempted, but only EPSV method. */ | |
9e008dda | 2435 | if (Config.Ftp.epsv_all && ftpState->flags.epsv_all_sent && ftpState->state == SENT_EPSV_1 ) { |
a689bd4e | 2436 | debugs(9, DBG_IMPORTANT, "FTP does not allow PASV method after 'EPSV ALL' has been sent."); |
2437 | ftpFail(ftpState); | |
2438 | return; | |
2439 | } | |
ee65546f | 2440 | |
a689bd4e | 2441 | /** \par |
2442 | * Checks for 'HEAD' method request and passes off for special handling by FtpStateData::processHeadResponse(). */ | |
47f6e231 | 2443 | if (ftpState->request->method == METHOD_HEAD && (ftpState->flags.isdir || ftpState->theSize != -1)) { |
063a47b5 | 2444 | ftpState->processHeadResponse(); // may call serverComplete |
62e76326 | 2445 | return; |
7497ceef | 2446 | } |
62e76326 | 2447 | |
94b88585 AR |
2448 | /// Closes any old FTP-Data connection which may exist. */ |
2449 | ftpState->data.close(); | |
62e76326 | 2450 | |
a689bd4e | 2451 | /** \par |
2452 | * Checks for previous EPSV/PASV failures on this server/session. | |
2453 | * Diverts to EPRT immediately if they are not working. */ | |
e55f0142 | 2454 | if (!ftpState->flags.pasv_supported) { |
cc192b50 | 2455 | ftpSendEPRT(ftpState); |
62e76326 | 2456 | return; |
30a4f2a8 | 2457 | } |
62e76326 | 2458 | |
a689bd4e | 2459 | /** \par |
2460 | * Locates the Address of the remote server. */ | |
cc192b50 | 2461 | addr.InitAddrInfo(AI); |
62e76326 | 2462 | |
cc192b50 | 2463 | if (getsockname(ftpState->ctrl.fd, AI->ai_addr, &AI->ai_addrlen)) { |
a689bd4e | 2464 | /** If it cannot be located the FTP Session is killed. */ |
cc192b50 | 2465 | addr.FreeAddrInfo(AI); |
a689bd4e | 2466 | debugs(9, DBG_CRITICAL, HERE << "getsockname(" << ftpState->ctrl.fd << ",'" << addr << "',...): " << xstrerror()); |
62e76326 | 2467 | ftpFail(ftpState); |
2468 | return; | |
cdc33f35 | 2469 | } |
62e76326 | 2470 | |
cc192b50 | 2471 | addr = *AI; |
cc192b50 | 2472 | addr.FreeAddrInfo(AI); |
2473 | ||
a689bd4e | 2474 | /** \par |
2475 | * Send EPSV (ALL,2,1) or PASV on the control channel. | |
2476 | * | |
d85b8894 | 2477 | * - EPSV ALL is used if enabled. |
9adb5bc5 AJ |
2478 | * - EPSV 2 is used if ALL is disabled and IPv6 is available and ctrl channel is IPv6. |
2479 | * - EPSV 1 is used if EPSV 2 (IPv6) fails or is not available or ctrl channel is IPv4. | |
d85b8894 | 2480 | * - PASV is used if EPSV 1 fails. |
a689bd4e | 2481 | */ |
9e008dda | 2482 | switch (ftpState->state) { |
9adb5bc5 AJ |
2483 | case SENT_EPSV_ALL: /* EPSV ALL resulted in a bad response. Try ther EPSV methods. */ |
2484 | ftpState->flags.epsv_all_sent = true; | |
2485 | if (addr.IsIPv6()) { | |
fe2d668b | 2486 | debugs(9, 5, HERE << "FTP Channel is IPv6 (" << addr << ") attempting EPSV 2 after EPSV ALL has failed."); |
9adb5bc5 AJ |
2487 | snprintf(cbuf, 1024, "EPSV 2\r\n"); |
2488 | ftpState->state = SENT_EPSV_2; | |
2489 | break; | |
2490 | } | |
2491 | // else fall through to skip EPSV 2 | |
62e76326 | 2492 | |
a689bd4e | 2493 | case SENT_EPSV_2: /* EPSV IPv6 failed. Try EPSV IPv4 */ |
9adb5bc5 | 2494 | if (addr.IsIPv4()) { |
fe2d668b | 2495 | debugs(9, 5, HERE << "FTP Channel is IPv4 (" << addr << ") attempting EPSV 1 after EPSV ALL has failed."); |
9adb5bc5 AJ |
2496 | snprintf(cbuf, 1024, "EPSV 1\r\n"); |
2497 | ftpState->state = SENT_EPSV_1; | |
d06d0e1e | 2498 | break; |
e1381638 | 2499 | } else if (ftpState->flags.epsv_all_sent) { |
9adb5bc5 AJ |
2500 | debugs(9, DBG_IMPORTANT, "FTP does not allow PASV method after 'EPSV ALL' has been sent."); |
2501 | ftpFail(ftpState); | |
2502 | return; | |
2503 | } | |
2504 | // else fall through to skip EPSV 1 | |
a689bd4e | 2505 | |
9adb5bc5 | 2506 | case SENT_EPSV_1: /* EPSV options exhausted. Try PASV now. */ |
fe2d668b | 2507 | debugs(9, 5, HERE << "FTP Channel (" << addr << ") rejects EPSV connection attempts. Trying PASV instead."); |
9adb5bc5 AJ |
2508 | snprintf(cbuf, 1024, "PASV\r\n"); |
2509 | ftpState->state = SENT_PASV; | |
a689bd4e | 2510 | break; |
2511 | ||
2512 | default: | |
51ee534d | 2513 | if (!Config.Ftp.epsv) { |
fe2d668b | 2514 | debugs(9, 5, HERE << "EPSV support manually disabled. Sending PASV for FTP Channel (" << addr <<")"); |
51ee534d AJ |
2515 | snprintf(cbuf, 1024, "PASV\r\n"); |
2516 | ftpState->state = SENT_PASV; | |
2517 | } else if (Config.Ftp.epsv_all) { | |
fe2d668b | 2518 | debugs(9, 5, HERE << "EPSV ALL manually enabled. Attempting with FTP Channel (" << addr <<")"); |
a689bd4e | 2519 | snprintf(cbuf, 1024, "EPSV ALL\r\n"); |
2520 | ftpState->state = SENT_EPSV_ALL; | |
2521 | /* block other non-EPSV connections being attempted */ | |
2522 | ftpState->flags.epsv_all_sent = true; | |
9e008dda | 2523 | } else { |
a689bd4e | 2524 | #if USE_IPV6 |
fe2d668b AJ |
2525 | if (addr.IsIPv6()) { |
2526 | debugs(9, 5, HERE << "FTP Channel (" << addr << "). Sending default EPSV 2"); | |
2527 | snprintf(cbuf, 1024, "EPSV 2\r\n"); | |
2528 | ftpState->state = SENT_EPSV_2; | |
2529 | } | |
a689bd4e | 2530 | #endif |
fe2d668b AJ |
2531 | if (addr.IsIPv4()) { |
2532 | debugs(9, 5, HERE << "Channel (" << addr <<"). Sending default EPSV 1"); | |
2533 | snprintf(cbuf, 1024, "EPSV 1\r\n"); | |
2534 | ftpState->state = SENT_EPSV_1; | |
2535 | } | |
a689bd4e | 2536 | } |
2537 | break; | |
2538 | } | |
62e76326 | 2539 | |
9adb5bc5 AJ |
2540 | /** Otherwise, Open data channel with the same local address as control channel (on a new random port!) */ |
2541 | addr.SetPort(0); | |
2542 | int fd = comm_open(SOCK_STREAM, | |
2543 | IPPROTO_TCP, | |
2544 | addr, | |
2545 | COMM_NONBLOCKING, | |
2546 | ftpState->entry->url()); | |
2547 | ||
fe2d668b | 2548 | debugs(9, 3, HERE << "Unconnected data socket created on FD " << fd << " from " << addr); |
9adb5bc5 AJ |
2549 | |
2550 | if (fd < 0) { | |
2551 | ftpFail(ftpState); | |
2552 | return; | |
2553 | } | |
2554 | ||
2555 | ftpState->data.opened(fd, ftpState->dataCloser()); | |
a689bd4e | 2556 | ftpState->writeCommand(cbuf); |
62e76326 | 2557 | |
7e3ce7b9 | 2558 | /* |
2559 | * ugly hack for ftp servers like ftp.netscape.com that sometimes | |
1be4874e | 2560 | * dont acknowledge PASV commands. |
7e3ce7b9 | 2561 | */ |
dc56a9b1 | 2562 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
2563 | AsyncCall::Pointer timeoutCall = asyncCall(9, 5, "FtpStateData::ftpTimeout", | |
9e008dda | 2564 | TimeoutDialer(ftpState,&FtpStateData::ftpTimeout)); |
dc56a9b1 | 2565 | |
2566 | commSetTimeout(ftpState->data.fd, 15, timeoutCall); | |
3fdadc70 | 2567 | } |
2568 | ||
063a47b5 | 2569 | void |
2570 | FtpStateData::processHeadResponse() | |
2571 | { | |
2572 | debugs(9, 5, HERE << "handling HEAD response"); | |
2573 | ftpSendQuit(this); | |
2574 | appendSuccessHeader(); | |
2575 | ||
2576 | /* | |
2577 | * On rare occasions I'm seeing the entry get aborted after | |
2578 | * ftpReadControlReply() and before here, probably when | |
2579 | * trying to write to the client. | |
2580 | */ | |
2581 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { | |
2582 | abortTransaction("entry aborted while processing HEAD"); | |
2583 | return; | |
2584 | } | |
2585 | ||
a83c6ed6 AR |
2586 | #if USE_ADAPTATION |
2587 | if (adaptationAccessCheckPending) { | |
2588 | debugs(9,3, HERE << "returning due to adaptationAccessCheckPending"); | |
063a47b5 | 2589 | return; |
2590 | } | |
2591 | #endif | |
2592 | ||
2593 | // processReplyBody calls serverComplete() since there is no body | |
9e008dda | 2594 | processReplyBody(); |
063a47b5 | 2595 | } |
2596 | ||
63be0a78 | 2597 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2598 | static void |
2599 | ftpReadPasv(FtpStateData * ftpState) | |
2600 | { | |
2601 | int code = ftpState->ctrl.replycode; | |
2602 | int h1, h2, h3, h4; | |
2603 | int p1, p2; | |
2604 | int n; | |
2605 | u_short port; | |
ad61a2b4 | 2606 | IpAddress ipa_remote; |
3fdadc70 | 2607 | int fd = ftpState->data.fd; |
748f6a15 | 2608 | char *buf; |
00c5afca | 2609 | LOCAL_ARRAY(char, ipaddr, 1024); |
a689bd4e | 2610 | debugs(9, 3, HERE); |
62e76326 | 2611 | |
3fdadc70 | 2612 | if (code != 227) { |
a689bd4e | 2613 | debugs(9, 2, "PASV not supported by remote end"); |
cc192b50 | 2614 | ftpSendEPRT(ftpState); |
62e76326 | 2615 | return; |
3fdadc70 | 2616 | } |
62e76326 | 2617 | |
748f6a15 | 2618 | /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */ |
2619 | /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */ | |
a689bd4e | 2620 | debugs(9, 5, HERE << "scanning: " << ftpState->ctrl.last_reply); |
62e76326 | 2621 | |
3f9d0b2d | 2622 | buf = ftpState->ctrl.last_reply + strcspn(ftpState->ctrl.last_reply, "0123456789"); |
62e76326 | 2623 | |
748f6a15 | 2624 | n = sscanf(buf, "%d,%d,%d,%d,%d,%d", &h1, &h2, &h3, &h4, &p1, &p2); |
62e76326 | 2625 | |
748f6a15 | 2626 | if (n != 6 || p1 < 0 || p2 < 0 || p1 > 255 || p2 > 255) { |
a689bd4e | 2627 | debugs(9, DBG_IMPORTANT, "Unsafe PASV reply from " << |
bf8fe701 | 2628 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << |
2629 | ftpState->ctrl.last_reply); | |
2630 | ||
cc192b50 | 2631 | ftpSendEPRT(ftpState); |
62e76326 | 2632 | return; |
3fdadc70 | 2633 | } |
62e76326 | 2634 | |
00c5afca | 2635 | snprintf(ipaddr, 1024, "%d.%d.%d.%d", h1, h2, h3, h4); |
62e76326 | 2636 | |
cc192b50 | 2637 | ipa_remote = ipaddr; |
2638 | ||
9e008dda | 2639 | if ( ipa_remote.IsAnyAddr() ) { |
a689bd4e | 2640 | debugs(9, DBG_IMPORTANT, "Unsafe PASV reply from " << |
bf8fe701 | 2641 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << |
2642 | ftpState->ctrl.last_reply); | |
2643 | ||
cc192b50 | 2644 | ftpSendEPRT(ftpState); |
62e76326 | 2645 | return; |
3fdadc70 | 2646 | } |
62e76326 | 2647 | |
3fdadc70 | 2648 | port = ((p1 << 8) + p2); |
62e76326 | 2649 | |
7f74df3a | 2650 | if (0 == port) { |
a689bd4e | 2651 | debugs(9, DBG_IMPORTANT, "Unsafe PASV reply from " << |
bf8fe701 | 2652 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << |
2653 | ftpState->ctrl.last_reply); | |
2654 | ||
cc192b50 | 2655 | ftpSendEPRT(ftpState); |
62e76326 | 2656 | return; |
7f74df3a | 2657 | } |
62e76326 | 2658 | |
00c5afca | 2659 | if (Config.Ftp.sanitycheck) { |
62e76326 | 2660 | if (port < 1024) { |
a689bd4e | 2661 | debugs(9, DBG_IMPORTANT, "Unsafe PASV reply from " << |
bf8fe701 | 2662 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << |
2663 | ftpState->ctrl.last_reply); | |
2664 | ||
cc192b50 | 2665 | ftpSendEPRT(ftpState); |
62e76326 | 2666 | return; |
2667 | } | |
00c5afca | 2668 | } |
62e76326 | 2669 | |
9b312a19 | 2670 | ftpState->data.port = port; |
3f9d0b2d | 2671 | |
2672 | if (Config.Ftp.sanitycheck) | |
2673 | ftpState->data.host = xstrdup(fd_table[ftpState->ctrl.fd].ipaddr); | |
2674 | else | |
2675 | ftpState->data.host = xstrdup(ipaddr); | |
2676 | ||
9bc73deb | 2677 | safe_free(ftpState->ctrl.last_command); |
3f9d0b2d | 2678 | |
9bc73deb | 2679 | safe_free(ftpState->ctrl.last_reply); |
3f9d0b2d | 2680 | |
9bc73deb | 2681 | ftpState->ctrl.last_command = xstrdup("Connect to server data port"); |
3f9d0b2d | 2682 | |
a689bd4e | 2683 | debugs(9, 3, HERE << "connecting to " << ftpState->data.host << ", port " << ftpState->data.port); |
3f9d0b2d | 2684 | |
6f0aab86 | 2685 | commConnectStart(fd, ipaddr, port, FtpStateData::ftpPasvCallback, ftpState); |
3fdadc70 | 2686 | } |
2687 | ||
6f0aab86 | 2688 | void |
3ff65596 | 2689 | FtpStateData::ftpPasvCallback(int fd, const DnsLookupDetails &dns, comm_err_t status, int xerrno, void *data) |
3fdadc70 | 2690 | { |
e6ccf245 | 2691 | FtpStateData *ftpState = (FtpStateData *)data; |
a689bd4e | 2692 | debugs(9, 3, HERE); |
3ff65596 | 2693 | ftpState->request->recordLookup(dns); |
62e76326 | 2694 | |
9b312a19 | 2695 | if (status != COMM_OK) { |
a689bd4e | 2696 | debugs(9, 2, HERE << "Failed to connect. Retrying without PASV."); |
b6b6f466 | 2697 | ftpState->fwd->dontRetry(false); /* this is a retryable error */ |
2698 | ftpState->fwd->ftpPasvFailed(true); | |
8043ea13 | 2699 | ftpState->failed(ERR_NONE, 0); |
6f0aab86 | 2700 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 2701 | return; |
3fdadc70 | 2702 | } |
62e76326 | 2703 | |
3fdadc70 | 2704 | ftpRestOrList(ftpState); |
2705 | } | |
2706 | ||
63be0a78 | 2707 | /// \ingroup ServerProtocolFTPInternal |
cdc33f35 | 2708 | static int |
2709 | ftpOpenListenSocket(FtpStateData * ftpState, int fallback) | |
2710 | { | |
2711 | int fd; | |
62e76326 | 2712 | |
ad61a2b4 | 2713 | IpAddress addr; |
cc192b50 | 2714 | struct addrinfo *AI = NULL; |
cdc33f35 | 2715 | int on = 1; |
cc192b50 | 2716 | int x = 0; |
62e76326 | 2717 | |
04f55905 | 2718 | /// Close old data channels, if any. We may open a new one below. |
94b88585 | 2719 | ftpState->data.close(); |
62e76326 | 2720 | |
4f310655 | 2721 | /* |
2722 | * Set up a listen socket on the same local address as the | |
2723 | * control connection. | |
2724 | */ | |
62e76326 | 2725 | |
cc192b50 | 2726 | addr.InitAddrInfo(AI); |
2727 | ||
2728 | x = getsockname(ftpState->ctrl.fd, AI->ai_addr, &AI->ai_addrlen); | |
2729 | ||
2730 | addr = *AI; | |
2731 | ||
2732 | addr.FreeAddrInfo(AI); | |
2733 | ||
9e008dda | 2734 | if (x) { |
a689bd4e | 2735 | debugs(9, DBG_CRITICAL, HERE << "getsockname(" << ftpState->ctrl.fd << ",..): " << xstrerror()); |
62e76326 | 2736 | return -1; |
cdc33f35 | 2737 | } |
62e76326 | 2738 | |
4f310655 | 2739 | /* |
2740 | * REUSEADDR is needed in fallback mode, since the same port is | |
2741 | * used for both control and data. | |
cdc33f35 | 2742 | */ |
2743 | if (fallback) { | |
62e76326 | 2744 | setsockopt(ftpState->ctrl.fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)); |
9e008dda | 2745 | } else { |
a689bd4e | 2746 | /* if not running in fallback mode a new port needs to be retrieved */ |
2747 | addr.SetPort(0); | |
2748 | } | |
62e76326 | 2749 | |
cdc33f35 | 2750 | fd = comm_open(SOCK_STREAM, |
bdb741f4 | 2751 | IPPROTO_TCP, |
cc192b50 | 2752 | addr, |
62e76326 | 2753 | COMM_NONBLOCKING | (fallback ? COMM_REUSEADDR : 0), |
3900307b | 2754 | ftpState->entry->url()); |
a689bd4e | 2755 | debugs(9, 3, HERE << "Unconnected data socket created on FD " << fd ); |
62e76326 | 2756 | |
cdc33f35 | 2757 | if (fd < 0) { |
a689bd4e | 2758 | debugs(9, DBG_CRITICAL, HERE << "comm_open failed"); |
62e76326 | 2759 | return -1; |
cdc33f35 | 2760 | } |
62e76326 | 2761 | |
04f55905 AJ |
2762 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; |
2763 | AsyncCall::Pointer acceptCall = asyncCall(11, 5, "FtpStateData::ftpAcceptDataConnection", | |
2764 | acceptDialer(ftpState, &FtpStateData::ftpAcceptDataConnection)); | |
2765 | ftpState->data.listener = new Comm::ListenStateData(fd, acceptCall, false); | |
2766 | ||
2767 | if (!ftpState->data.listener || ftpState->data.listener->errcode < 0) { | |
62e76326 | 2768 | comm_close(fd); |
2769 | return -1; | |
cdc33f35 | 2770 | } |
62e76326 | 2771 | |
94b88585 | 2772 | ftpState->data.opened(fd, ftpState->dataCloser()); |
d20b1cd0 | 2773 | ftpState->data.port = comm_local_port(fd); |
cdc33f35 | 2774 | ftpState->data.host = NULL; |
2775 | return fd; | |
2776 | } | |
2777 | ||
63be0a78 | 2778 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2779 | static void |
cc192b50 | 2780 | ftpSendPORT(FtpStateData * ftpState) |
3fdadc70 | 2781 | { |
cdc33f35 | 2782 | int fd; |
62e76326 | 2783 | |
ad61a2b4 | 2784 | IpAddress ipa; |
cc192b50 | 2785 | struct addrinfo *AI = NULL; |
cdc33f35 | 2786 | unsigned char *addrptr; |
2787 | unsigned char *portptr; | |
a11382aa | 2788 | |
2789 | /* check the server control channel is still available */ | |
9e008dda | 2790 | if (!ftpState || !ftpState->haveControlChannel("ftpSendPort")) |
a11382aa | 2791 | return; |
2792 | ||
9e008dda | 2793 | if (Config.Ftp.epsv_all && ftpState->flags.epsv_all_sent) { |
a689bd4e | 2794 | debugs(9, DBG_IMPORTANT, "FTP does not allow PORT method after 'EPSV ALL' has been sent."); |
2795 | return; | |
2796 | } | |
2797 | ||
2798 | debugs(9, 3, HERE); | |
e55f0142 | 2799 | ftpState->flags.pasv_supported = 0; |
cdc33f35 | 2800 | fd = ftpOpenListenSocket(ftpState, 0); |
cc192b50 | 2801 | ipa.InitAddrInfo(AI); |
62e76326 | 2802 | |
cc192b50 | 2803 | if (getsockname(fd, AI->ai_addr, &AI->ai_addrlen)) { |
2804 | ipa.FreeAddrInfo(AI); | |
a689bd4e | 2805 | debugs(9, DBG_CRITICAL, HERE << "getsockname(" << fd << ",..): " << xstrerror()); |
bf8fe701 | 2806 | |
62e76326 | 2807 | /* XXX Need to set error message */ |
2808 | ftpFail(ftpState); | |
2809 | return; | |
cdc33f35 | 2810 | } |
62e76326 | 2811 | |
cc192b50 | 2812 | #if USE_IPV6 |
9e008dda | 2813 | if ( AI->ai_addrlen != sizeof(struct sockaddr_in) ) { |
cc192b50 | 2814 | ipa.FreeAddrInfo(AI); |
9e008dda AJ |
2815 | /* IPv6 CANNOT send PORT command. */ |
2816 | /* we got here by attempting and failing an EPRT */ | |
2817 | /* using the same reply code should simulate a PORT failure */ | |
2818 | ftpReadPORT(ftpState); | |
2819 | return; | |
cc192b50 | 2820 | } |
2821 | #endif | |
2822 | ||
2823 | addrptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_addr; | |
2824 | portptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_port; | |
cdc33f35 | 2825 | snprintf(cbuf, 1024, "PORT %d,%d,%d,%d,%d,%d\r\n", |
62e76326 | 2826 | addrptr[0], addrptr[1], addrptr[2], addrptr[3], |
2827 | portptr[0], portptr[1]); | |
2f47fadf | 2828 | ftpState->writeCommand(cbuf); |
cdc33f35 | 2829 | ftpState->state = SENT_PORT; |
cc192b50 | 2830 | |
2831 | ipa.FreeAddrInfo(AI); | |
3fdadc70 | 2832 | } |
2833 | ||
63be0a78 | 2834 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2835 | static void |
cc192b50 | 2836 | ftpReadPORT(FtpStateData * ftpState) |
3fdadc70 | 2837 | { |
cdc33f35 | 2838 | int code = ftpState->ctrl.replycode; |
a689bd4e | 2839 | debugs(9, 3, HERE); |
62e76326 | 2840 | |
cdc33f35 | 2841 | if (code != 200) { |
62e76326 | 2842 | /* Fall back on using the same port as the control connection */ |
bf8fe701 | 2843 | debugs(9, 3, "PORT not supported by remote end"); |
62e76326 | 2844 | ftpOpenListenSocket(ftpState, 1); |
cdc33f35 | 2845 | } |
62e76326 | 2846 | |
cdc33f35 | 2847 | ftpRestOrList(ftpState); |
2848 | } | |
2849 | ||
63be0a78 | 2850 | /// \ingroup ServerProtocolFTPInternal |
cc192b50 | 2851 | static void |
2852 | ftpSendEPRT(FtpStateData * ftpState) | |
2853 | { | |
2854 | int fd; | |
ad61a2b4 | 2855 | IpAddress addr; |
cc192b50 | 2856 | struct addrinfo *AI = NULL; |
2857 | char buf[MAX_IPSTRLEN]; | |
2858 | ||
9e008dda | 2859 | if (Config.Ftp.epsv_all && ftpState->flags.epsv_all_sent) { |
a689bd4e | 2860 | debugs(9, DBG_IMPORTANT, "FTP does not allow EPRT method after 'EPSV ALL' has been sent."); |
2861 | return; | |
2862 | } | |
2863 | ||
2864 | debugs(9, 3, HERE); | |
cc192b50 | 2865 | ftpState->flags.pasv_supported = 0; |
2866 | fd = ftpOpenListenSocket(ftpState, 0); | |
2867 | ||
2868 | addr.InitAddrInfo(AI); | |
2869 | ||
2870 | if (getsockname(fd, AI->ai_addr, &AI->ai_addrlen)) { | |
2871 | addr.FreeAddrInfo(AI); | |
a689bd4e | 2872 | debugs(9, DBG_CRITICAL, HERE << "getsockname(" << fd << ",..): " << xstrerror()); |
cc192b50 | 2873 | |
2874 | /* XXX Need to set error message */ | |
2875 | ftpFail(ftpState); | |
2876 | return; | |
2877 | } | |
2878 | ||
2879 | addr = *AI; | |
2880 | ||
2881 | /* RFC 2428 defines EPRT as IPv6 equivalent to IPv4 PORT command. */ | |
2882 | /* Which can be used by EITHER protocol. */ | |
2883 | snprintf(cbuf, 1024, "EPRT |%d|%s|%d|\r\n", | |
a689bd4e | 2884 | ( addr.IsIPv6() ? 2 : 1 ), |
cc192b50 | 2885 | addr.NtoA(buf,MAX_IPSTRLEN), |
2886 | addr.GetPort() ); | |
2887 | ||
2888 | ftpState->writeCommand(cbuf); | |
2889 | ftpState->state = SENT_EPRT; | |
2890 | ||
2891 | addr.FreeAddrInfo(AI); | |
2892 | } | |
2893 | ||
2894 | static void | |
2895 | ftpReadEPRT(FtpStateData * ftpState) | |
2896 | { | |
2897 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 2898 | debugs(9, 3, HERE); |
cc192b50 | 2899 | |
2900 | if (code != 200) { | |
2901 | /* Failover to attempting old PORT command. */ | |
2902 | debugs(9, 3, "EPRT not supported by remote end"); | |
2903 | ftpSendPORT(ftpState); | |
2904 | return; | |
2905 | } | |
2906 | ||
2907 | ftpRestOrList(ftpState); | |
2908 | } | |
2909 | ||
a689bd4e | 2910 | /** |
63be0a78 | 2911 | \ingroup ServerProtocolFTPInternal |
a689bd4e | 2912 | \par |
2913 | * "read" handler to accept FTP data connections. | |
2914 | * | |
63be0a78 | 2915 | \param io comm accept(2) callback parameters |
a689bd4e | 2916 | */ |
dc56a9b1 | 2917 | void FtpStateData::ftpAcceptDataConnection(const CommAcceptCbParams &io) |
cdc33f35 | 2918 | { |
cc192b50 | 2919 | char ntoapeer[MAX_IPSTRLEN]; |
dc56a9b1 | 2920 | debugs(9, 3, "ftpAcceptDataConnection"); |
cdc33f35 | 2921 | |
04f55905 AJ |
2922 | // one connection accepted. the handler has stopped listening. drop our local pointer to it. |
2923 | data.listener = NULL; | |
02d1422b | 2924 | |
dc56a9b1 | 2925 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
2926 | abortTransaction("entry aborted when accepting data conn"); | |
62e76326 | 2927 | return; |
b044a7eb | 2928 | } |
c4b7a5a9 | 2929 | |
a689bd4e | 2930 | /** \par |
2931 | * When squid.conf ftp_sanitycheck is enabled, check the new connection is actually being | |
2932 | * made by the remote client which is connected to the FTP control socket. | |
2933 | * This prevents third-party hacks, but also third-party load balancing handshakes. | |
2934 | */ | |
00c5afca | 2935 | if (Config.Ftp.sanitycheck) { |
dc56a9b1 | 2936 | io.details.peer.NtoA(ntoapeer,MAX_IPSTRLEN); |
2937 | ||
2938 | if (strcmp(fd_table[ctrl.fd].ipaddr, ntoapeer) != 0) { | |
9e008dda AJ |
2939 | debugs(9, DBG_IMPORTANT, |
2940 | "FTP data connection from unexpected server (" << | |
2941 | io.details.peer << "), expecting " << | |
2942 | fd_table[ctrl.fd].ipaddr); | |
dc56a9b1 | 2943 | |
04f55905 | 2944 | /* close the bad soures connection down ASAP. */ |
dc56a9b1 | 2945 | comm_close(io.nfd); |
04f55905 AJ |
2946 | |
2947 | /* we are ony accepting once, so need to re-open the listener socket. */ | |
9e008dda AJ |
2948 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; |
2949 | AsyncCall::Pointer acceptCall = asyncCall(11, 5, "FtpStateData::ftpAcceptDataConnection", | |
2950 | acceptDialer(this, &FtpStateData::ftpAcceptDataConnection)); | |
04f55905 | 2951 | data.listener = new Comm::ListenStateData(data.fd, acceptCall, false); |
62e76326 | 2952 | return; |
2953 | } | |
00c5afca | 2954 | } |
62e76326 | 2955 | |
dc56a9b1 | 2956 | if (io.flag != COMM_OK) { |
04f55905 | 2957 | debugs(9, DBG_IMPORTANT, "ftpHandleDataAccept: FD " << io.nfd << ": " << xstrerr(io.xerrno)); |
a689bd4e | 2958 | /** \todo XXX Need to set error message */ |
dc56a9b1 | 2959 | ftpFail(this); |
62e76326 | 2960 | return; |
cdc33f35 | 2961 | } |
62e76326 | 2962 | |
a689bd4e | 2963 | /**\par |
2964 | * Replace the Listen socket with the accepted data socket */ | |
94b88585 AR |
2965 | data.close(); |
2966 | data.opened(io.nfd, dataCloser()); | |
dc56a9b1 | 2967 | data.port = io.details.peer.GetPort(); |
2968 | io.details.peer.NtoA(data.host,SQUIDHOSTNAMELEN); | |
a689bd4e | 2969 | |
dc56a9b1 | 2970 | debugs(9, 3, "ftpAcceptDataConnection: Connected data socket on " << |
9e008dda AJ |
2971 | "FD " << io.nfd << " to " << io.details.peer << " FD table says: " << |
2972 | "ctrl-peer= " << fd_table[ctrl.fd].ipaddr << ", " << | |
2973 | "data-peer= " << fd_table[data.fd].ipaddr); | |
a689bd4e | 2974 | |
a689bd4e | 2975 | |
dc56a9b1 | 2976 | AsyncCall::Pointer nullCall = NULL; |
2977 | commSetTimeout(ctrl.fd, -1, nullCall); | |
62e76326 | 2978 | |
dc56a9b1 | 2979 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
2980 | AsyncCall::Pointer timeoutCall = asyncCall(9, 5, "FtpStateData::ftpTimeout", | |
9e008dda | 2981 | TimeoutDialer(this,&FtpStateData::ftpTimeout)); |
dc56a9b1 | 2982 | commSetTimeout(data.fd, Config.Timeout.read, timeoutCall); |
62e76326 | 2983 | |
a689bd4e | 2984 | /*\todo XXX We should have a flag to track connect state... |
cdc33f35 | 2985 | * host NULL -> not connected, port == local port |
2986 | * host set -> connected, port == remote port | |
2987 | */ | |
2988 | /* Restart state (SENT_NLST/LIST/RETR) */ | |
dc56a9b1 | 2989 | FTP_SM_FUNCS[state] (this); |
3fdadc70 | 2990 | } |
2991 | ||
63be0a78 | 2992 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2993 | static void |
2994 | ftpRestOrList(FtpStateData * ftpState) | |
2995 | { | |
a689bd4e | 2996 | debugs(9, 3, HERE); |
62e76326 | 2997 | |
94439e4e | 2998 | if (ftpState->typecode == 'D') { |
62e76326 | 2999 | ftpState->flags.isdir = 1; |
62e76326 | 3000 | |
3001 | if (ftpState->flags.put) { | |
3002 | ftpSendMkdir(ftpState); /* PUT name;type=d */ | |
3003 | } else { | |
3004 | ftpSendNlst(ftpState); /* GET name;type=d sec 3.2.2 of RFC 1738 */ | |
3005 | } | |
94439e4e | 3006 | } else if (ftpState->flags.put) { |
62e76326 | 3007 | ftpSendStor(ftpState); |
e55f0142 | 3008 | } else if (ftpState->flags.isdir) |
62e76326 | 3009 | ftpSendList(ftpState); |
6f0aab86 | 3010 | else if (ftpState->restartable()) |
62e76326 | 3011 | ftpSendRest(ftpState); |
969c39b9 | 3012 | else |
62e76326 | 3013 | ftpSendRetr(ftpState); |
969c39b9 | 3014 | } |
3015 | ||
63be0a78 | 3016 | /// \ingroup ServerProtocolFTPInternal |
54220df8 | 3017 | static void |
3018 | ftpSendStor(FtpStateData * ftpState) | |
3019 | { | |
a11382aa | 3020 | /* check the server control channel is still available */ |
9e008dda | 3021 | if (!ftpState || !ftpState->haveControlChannel("ftpSendStor")) |
a11382aa | 3022 | return; |
3023 | ||
a689bd4e | 3024 | debugs(9, 3, HERE); |
3025 | ||
9bc73deb | 3026 | if (ftpState->filepath != NULL) { |
62e76326 | 3027 | /* Plain file upload */ |
3028 | snprintf(cbuf, 1024, "STOR %s\r\n", ftpState->filepath); | |
2f47fadf | 3029 | ftpState->writeCommand(cbuf); |
62e76326 | 3030 | ftpState->state = SENT_STOR; |
47f6e231 | 3031 | } else if (ftpState->request->header.getInt64(HDR_CONTENT_LENGTH) > 0) { |
62e76326 | 3032 | /* File upload without a filename. use STOU to generate one */ |
3033 | snprintf(cbuf, 1024, "STOU\r\n"); | |
2f47fadf | 3034 | ftpState->writeCommand(cbuf); |
62e76326 | 3035 | ftpState->state = SENT_STOR; |
9bc73deb | 3036 | } else { |
62e76326 | 3037 | /* No file to transfer. Only create directories if needed */ |
3038 | ftpSendReply(ftpState); | |
9bc73deb | 3039 | } |
54220df8 | 3040 | } |
3041 | ||
63be0a78 | 3042 | /// \ingroup ServerProtocolFTPInternal |
3043 | /// \deprecated use ftpState->readStor() instead. | |
54220df8 | 3044 | static void |
3045 | ftpReadStor(FtpStateData * ftpState) | |
3046 | { | |
5f8252d2 | 3047 | ftpState->readStor(); |
3048 | } | |
3049 | ||
9e008dda AJ |
3050 | void FtpStateData::readStor() |
3051 | { | |
5f8252d2 | 3052 | int code = ctrl.replycode; |
a689bd4e | 3053 | debugs(9, 3, HERE); |
62e76326 | 3054 | |
5f8252d2 | 3055 | if (code == 125 || (code == 150 && data.host)) { |
123ec4de | 3056 | if (!startRequestBodyFlow()) { // register to receive body data |
5f8252d2 | 3057 | ftpFail(this); |
3058 | return; | |
3059 | } | |
3060 | ||
a689bd4e | 3061 | /*\par |
3062 | * When client status is 125, or 150 without a hostname, Begin data transfer. */ | |
3063 | debugs(9, 3, HERE << "starting data transfer"); | |
5f8252d2 | 3064 | sendMoreRequestBody(); |
a689bd4e | 3065 | /** \par |
62e76326 | 3066 | * Cancel the timeout on the Control socket and |
3067 | * establish one on the data socket. | |
3068 | */ | |
9e008dda | 3069 | AsyncCall::Pointer nullCall = NULL; |
dc56a9b1 | 3070 | commSetTimeout(ctrl.fd, -1, nullCall); |
3071 | ||
9e008dda AJ |
3072 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
3073 | AsyncCall::Pointer timeoutCall = asyncCall(9, 5, "FtpStateData::ftpTimeout", | |
3074 | TimeoutDialer(this,&FtpStateData::ftpTimeout)); | |
dc56a9b1 | 3075 | |
3076 | commSetTimeout(data.fd, Config.Timeout.read, timeoutCall); | |
5f8252d2 | 3077 | |
3078 | state = WRITING_DATA; | |
a689bd4e | 3079 | debugs(9, 3, HERE << "writing data channel"); |
9bc73deb | 3080 | } else if (code == 150) { |
a689bd4e | 3081 | /*\par |
3082 | * When client code is 150 with a hostname, Accept data channel. */ | |
dc56a9b1 | 3083 | debugs(9, 3, "ftpReadStor: accepting data channel"); |
3084 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; | |
3085 | AsyncCall::Pointer acceptCall = asyncCall(11, 5, "FtpStateData::ftpAcceptDataConnection", | |
9e008dda | 3086 | acceptDialer(this, &FtpStateData::ftpAcceptDataConnection)); |
dc56a9b1 | 3087 | |
04f55905 | 3088 | data.listener = new Comm::ListenStateData(data.fd, acceptCall, false); |
54220df8 | 3089 | } else { |
a689bd4e | 3090 | debugs(9, DBG_IMPORTANT, HERE << "Unexpected reply code "<< std::setfill('0') << std::setw(3) << code); |
5f8252d2 | 3091 | ftpFail(this); |
54220df8 | 3092 | } |
3093 | } | |
3094 | ||
63be0a78 | 3095 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 3096 | static void |
3097 | ftpSendRest(FtpStateData * ftpState) | |
3098 | { | |
a11382aa | 3099 | /* check the server control channel is still available */ |
9e008dda | 3100 | if (!ftpState || !ftpState->haveControlChannel("ftpSendRest")) |
a11382aa | 3101 | return; |
3102 | ||
a689bd4e | 3103 | debugs(9, 3, HERE); |
3104 | ||
47f6e231 | 3105 | snprintf(cbuf, 1024, "REST %"PRId64"\r\n", ftpState->restart_offset); |
2f47fadf | 3106 | ftpState->writeCommand(cbuf); |
969c39b9 | 3107 | ftpState->state = SENT_REST; |
3fdadc70 | 3108 | } |
3109 | ||
6f0aab86 | 3110 | int |
3111 | FtpStateData::restartable() | |
cfbf5373 | 3112 | { |
6f0aab86 | 3113 | if (restart_offset > 0) |
62e76326 | 3114 | return 1; |
3115 | ||
6f0aab86 | 3116 | if (!request->range) |
62e76326 | 3117 | return 0; |
3118 | ||
6f0aab86 | 3119 | if (!flags.binary) |
62e76326 | 3120 | return 0; |
3121 | ||
47f6e231 | 3122 | if (theSize <= 0) |
62e76326 | 3123 | return 0; |
cfbf5373 | 3124 | |
47f6e231 | 3125 | int64_t desired_offset = request->range->lowestOffset(theSize); |
62e76326 | 3126 | |
b0e52cb9 | 3127 | if (desired_offset <= 0) |
62e76326 | 3128 | return 0; |
3129 | ||
47f6e231 | 3130 | if (desired_offset >= theSize) |
9e008dda | 3131 | return 0; |
b0e52cb9 | 3132 | |
3133 | restart_offset = desired_offset; | |
cfbf5373 | 3134 | return 1; |
3135 | } | |
3136 | ||
63be0a78 | 3137 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 3138 | static void |
3139 | ftpReadRest(FtpStateData * ftpState) | |
3140 | { | |
3141 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 3142 | debugs(9, 3, HERE); |
3fdadc70 | 3143 | assert(ftpState->restart_offset > 0); |
62e76326 | 3144 | |
3fdadc70 | 3145 | if (code == 350) { |
04f7fd38 | 3146 | ftpState->setCurrentOffset(ftpState->restart_offset); |
62e76326 | 3147 | ftpSendRetr(ftpState); |
3fdadc70 | 3148 | } else if (code > 0) { |
a689bd4e | 3149 | debugs(9, 3, HERE << "REST not supported"); |
62e76326 | 3150 | ftpState->flags.rest_supported = 0; |
3151 | ftpSendRetr(ftpState); | |
3fdadc70 | 3152 | } else { |
62e76326 | 3153 | ftpFail(ftpState); |
3fdadc70 | 3154 | } |
3155 | } | |
3156 | ||
63be0a78 | 3157 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 3158 | static void |
3159 | ftpSendList(FtpStateData * ftpState) | |
3160 | { | |
a11382aa | 3161 | /* check the server control channel is still available */ |
9e008dda | 3162 | if (!ftpState || !ftpState->haveControlChannel("ftpSendList")) |
a11382aa | 3163 | return; |
3164 | ||
a689bd4e | 3165 | debugs(9, 3, HERE); |
3166 | ||
dbfed404 | 3167 | if (ftpState->filepath) { |
62e76326 | 3168 | snprintf(cbuf, 1024, "LIST %s\r\n", ftpState->filepath); |
dbfed404 | 3169 | } else { |
62e76326 | 3170 | snprintf(cbuf, 1024, "LIST\r\n"); |
dbfed404 | 3171 | } |
62e76326 | 3172 | |
2f47fadf | 3173 | ftpState->writeCommand(cbuf); |
969c39b9 | 3174 | ftpState->state = SENT_LIST; |
3175 | } | |
3176 | ||
63be0a78 | 3177 | /// \ingroup ServerProtocolFTPInternal |
dbfed404 | 3178 | static void |
3179 | ftpSendNlst(FtpStateData * ftpState) | |
3180 | { | |
a11382aa | 3181 | /* check the server control channel is still available */ |
9e008dda | 3182 | if (!ftpState || !ftpState->haveControlChannel("ftpSendNlst")) |
a11382aa | 3183 | return; |
3184 | ||
a689bd4e | 3185 | debugs(9, 3, HERE); |
3186 | ||
e55f0142 | 3187 | ftpState->flags.tried_nlst = 1; |
62e76326 | 3188 | |
dbfed404 | 3189 | if (ftpState->filepath) { |
62e76326 | 3190 | snprintf(cbuf, 1024, "NLST %s\r\n", ftpState->filepath); |
dbfed404 | 3191 | } else { |
62e76326 | 3192 | snprintf(cbuf, 1024, "NLST\r\n"); |
dbfed404 | 3193 | } |
62e76326 | 3194 | |
2f47fadf | 3195 | ftpState->writeCommand(cbuf); |
dbfed404 | 3196 | ftpState->state = SENT_NLST; |
3197 | } | |
3198 | ||
63be0a78 | 3199 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 3200 | static void |
3201 | ftpReadList(FtpStateData * ftpState) | |
3202 | { | |
3203 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 3204 | debugs(9, 3, HERE); |
62e76326 | 3205 | |
cdc33f35 | 3206 | if (code == 125 || (code == 150 && ftpState->data.host)) { |
62e76326 | 3207 | /* Begin data transfer */ |
62e76326 | 3208 | /* XXX what about Config.Timeout.read? */ |
5f8252d2 | 3209 | ftpState->maybeReadVirginBody(); |
62e76326 | 3210 | ftpState->state = READING_DATA; |
3211 | /* | |
3212 | * Cancel the timeout on the Control socket and establish one | |
3213 | * on the data socket | |
3214 | */ | |
9e008dda | 3215 | AsyncCall::Pointer nullCall = NULL; |
dc56a9b1 | 3216 | commSetTimeout(ftpState->ctrl.fd, -1, nullCall); |
62e76326 | 3217 | return; |
cdc33f35 | 3218 | } else if (code == 150) { |
62e76326 | 3219 | /* Accept data channel */ |
9e008dda AJ |
3220 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; |
3221 | AsyncCall::Pointer acceptCall = asyncCall(11, 5, "FtpStateData::ftpAcceptDataConnection", | |
3222 | acceptDialer(ftpState, &FtpStateData::ftpAcceptDataConnection)); | |
dc56a9b1 | 3223 | |
04f55905 | 3224 | ftpState->data.listener = new Comm::ListenStateData(ftpState->data.fd, acceptCall, false); |
62e76326 | 3225 | /* |
3226 | * Cancel the timeout on the Control socket and establish one | |
3227 | * on the data socket | |
3228 | */ | |
9e008dda | 3229 | AsyncCall::Pointer nullCall = NULL; |
dc56a9b1 | 3230 | commSetTimeout(ftpState->ctrl.fd, -1, nullCall); |
9e008dda AJ |
3231 | |
3232 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; | |
3233 | AsyncCall::Pointer timeoutCall = asyncCall(9, 5, "FtpStateData::ftpTimeout", | |
3234 | TimeoutDialer(ftpState,&FtpStateData::ftpTimeout)); | |
dc56a9b1 | 3235 | commSetTimeout(ftpState->data.fd, Config.Timeout.read, timeoutCall); |
62e76326 | 3236 | return; |
e55f0142 | 3237 | } else if (!ftpState->flags.tried_nlst && code > 300) { |
62e76326 | 3238 | ftpSendNlst(ftpState); |
3fdadc70 | 3239 | } else { |
62e76326 | 3240 | ftpFail(ftpState); |
3241 | return; | |
3fdadc70 | 3242 | } |
3243 | } | |
3244 | ||
63be0a78 | 3245 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 3246 | static void |
3247 | ftpSendRetr(FtpStateData * ftpState) | |
3248 | { | |
a11382aa | 3249 | /* check the server control channel is still available */ |
9e008dda | 3250 | if (!ftpState || !ftpState->haveControlChannel("ftpSendRetr")) |
a11382aa | 3251 | return; |
3252 | ||
a689bd4e | 3253 | debugs(9, 3, HERE); |
3254 | ||
969c39b9 | 3255 | assert(ftpState->filepath != NULL); |
3256 | snprintf(cbuf, 1024, "RETR %s\r\n", ftpState->filepath); | |
2f47fadf | 3257 | ftpState->writeCommand(cbuf); |
969c39b9 | 3258 | ftpState->state = SENT_RETR; |
3259 | } | |
3260 | ||
63be0a78 | 3261 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 3262 | static void |
3263 | ftpReadRetr(FtpStateData * ftpState) | |
3264 | { | |
3265 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 3266 | debugs(9, 3, HERE); |
62e76326 | 3267 | |
cdc33f35 | 3268 | if (code == 125 || (code == 150 && ftpState->data.host)) { |
62e76326 | 3269 | /* Begin data transfer */ |
a689bd4e | 3270 | debugs(9, 3, HERE << "reading data channel"); |
62e76326 | 3271 | /* XXX what about Config.Timeout.read? */ |
5f8252d2 | 3272 | ftpState->maybeReadVirginBody(); |
62e76326 | 3273 | ftpState->state = READING_DATA; |
3274 | /* | |
3275 | * Cancel the timeout on the Control socket and establish one | |
3276 | * on the data socket | |
3277 | */ | |
9e008dda | 3278 | AsyncCall::Pointer nullCall = NULL; |
dc56a9b1 | 3279 | commSetTimeout(ftpState->ctrl.fd, -1, nullCall); |
cdc33f35 | 3280 | } else if (code == 150) { |
62e76326 | 3281 | /* Accept data channel */ |
9e008dda AJ |
3282 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; |
3283 | AsyncCall::Pointer acceptCall = asyncCall(11, 5, "FtpStateData::ftpAcceptDataConnection", | |
3284 | acceptDialer(ftpState, &FtpStateData::ftpAcceptDataConnection)); | |
04f55905 | 3285 | ftpState->data.listener = new Comm::ListenStateData(ftpState->data.fd, acceptCall, false); |
62e76326 | 3286 | /* |
3287 | * Cancel the timeout on the Control socket and establish one | |
3288 | * on the data socket | |
3289 | */ | |
9e008dda | 3290 | AsyncCall::Pointer nullCall = NULL; |
dc56a9b1 | 3291 | commSetTimeout(ftpState->ctrl.fd, -1, nullCall); |
3292 | ||
9e008dda AJ |
3293 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
3294 | AsyncCall::Pointer timeoutCall = asyncCall(9, 5, "FtpStateData::ftpTimeout", | |
3295 | TimeoutDialer(ftpState,&FtpStateData::ftpTimeout)); | |
dc56a9b1 | 3296 | commSetTimeout(ftpState->data.fd, Config.Timeout.read, timeoutCall); |
cdc33f35 | 3297 | } else if (code >= 300) { |
62e76326 | 3298 | if (!ftpState->flags.try_slash_hack) { |
3299 | /* Try this as a directory missing trailing slash... */ | |
6f0aab86 | 3300 | ftpState->hackShortcut(ftpSendCwd); |
62e76326 | 3301 | } else { |
3302 | ftpFail(ftpState); | |
3303 | } | |
cdc33f35 | 3304 | } else { |
62e76326 | 3305 | ftpFail(ftpState); |
3fdadc70 | 3306 | } |
3307 | } | |
3308 | ||
0477a072 AJ |
3309 | /** |
3310 | * Generate the HTTP headers and template fluff around an FTP | |
3311 | * directory listing display. | |
3312 | */ | |
3313 | void | |
3314 | FtpStateData::completedListing() | |
3315 | { | |
3316 | assert(entry); | |
3317 | entry->lock(); | |
3318 | ErrorState *ferr = errorCon(ERR_DIR_LISTING, HTTP_OK, request); | |
3319 | ferr->ftp.listing = &listing; | |
3320 | ferr->ftp.cwd_msg = xstrdup(cwd_message.termedBuf()); | |
3321 | ferr->ftp.server_msg = ctrl.message; | |
3322 | ctrl.message = NULL; | |
3323 | entry->replaceHttpReply( ferr->BuildHttpReply() ); | |
3324 | errorStateFree(ferr); | |
3325 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); | |
3326 | entry->flush(); | |
3327 | entry->unlock(); | |
3328 | } | |
3329 | ||
3330 | ||
63be0a78 | 3331 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 3332 | static void |
3333 | ftpReadTransferDone(FtpStateData * ftpState) | |
3334 | { | |
3335 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 3336 | debugs(9, 3, HERE); |
62e76326 | 3337 | |
bbf07a94 | 3338 | if (code == 226 || code == 250) { |
62e76326 | 3339 | /* Connection closed; retrieval done. */ |
0477a072 AJ |
3340 | if (ftpState->flags.listing) { |
3341 | ftpState->completedListing(); | |
3342 | /* QUIT operation handles sending the reply to client */ | |
3343 | } | |
c0d75e74 | 3344 | ftpSendQuit(ftpState); |
9bc73deb | 3345 | } else { /* != 226 */ |
a689bd4e | 3346 | debugs(9, DBG_IMPORTANT, HERE << "Got code " << code << " after reading data"); |
8043ea13 | 3347 | ftpState->failed(ERR_FTP_FAILURE, 0); |
6f0aab86 | 3348 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 3349 | return; |
3fdadc70 | 3350 | } |
3351 | } | |
3352 | ||
5f8252d2 | 3353 | // premature end of the request body |
6f0aab86 | 3354 | void |
5f8252d2 | 3355 | FtpStateData::handleRequestBodyProducerAborted() |
3fdadc70 | 3356 | { |
5f8252d2 | 3357 | ServerStateData::handleRequestBodyProducerAborted(); |
a689bd4e | 3358 | debugs(9, 3, HERE << "ftpState=" << this); |
5f8252d2 | 3359 | failed(ERR_READ_ERROR, 0); |
94439e4e | 3360 | } |
3361 | ||
63be0a78 | 3362 | /** |
3363 | * This will be called when the put write is completed | |
3364 | */ | |
6f0aab86 | 3365 | void |
dc56a9b1 | 3366 | FtpStateData::sentRequestBody(const CommIoCbParams &io) |
94439e4e | 3367 | { |
dc56a9b1 | 3368 | if (io.size > 0) |
3369 | kb_incr(&statCounter.server.ftp.kbytes_out, io.size); | |
3370 | ServerStateData::sentRequestBody(io); | |
94439e4e | 3371 | } |
3372 | ||
63be0a78 | 3373 | /// \ingroup ServerProtocolFTPInternal |
94439e4e | 3374 | static void |
3375 | ftpWriteTransferDone(FtpStateData * ftpState) | |
3376 | { | |
3377 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 3378 | debugs(9, 3, HERE); |
62e76326 | 3379 | |
bbf07a94 | 3380 | if (!(code == 226 || code == 250)) { |
a689bd4e | 3381 | debugs(9, DBG_IMPORTANT, HERE << "Got code " << code << " after sending data"); |
8043ea13 | 3382 | ftpState->failed(ERR_FTP_PUT_ERROR, 0); |
62e76326 | 3383 | return; |
94439e4e | 3384 | } |
62e76326 | 3385 | |
3900307b | 3386 | ftpState->entry->timestampsSet(); /* XXX Is this needed? */ |
94439e4e | 3387 | ftpSendReply(ftpState); |
969c39b9 | 3388 | } |
3389 | ||
63be0a78 | 3390 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 3391 | static void |
3392 | ftpSendQuit(FtpStateData * ftpState) | |
3393 | { | |
a11382aa | 3394 | /* check the server control channel is still available */ |
9e008dda | 3395 | if (!ftpState || !ftpState->haveControlChannel("ftpSendQuit")) |
a11382aa | 3396 | return; |
3397 | ||
56878878 | 3398 | snprintf(cbuf, 1024, "QUIT\r\n"); |
2f47fadf | 3399 | ftpState->writeCommand(cbuf); |
3fdadc70 | 3400 | ftpState->state = SENT_QUIT; |
3401 | } | |
3402 | ||
0477a072 AJ |
3403 | /** |
3404 | * \ingroup ServerProtocolFTPInternal | |
3405 | * | |
3406 | * This completes a client FTP operation with success or other page | |
3407 | * generated and stored in the entry field by the code issuing QUIT. | |
3408 | */ | |
3fdadc70 | 3409 | static void |
3410 | ftpReadQuit(FtpStateData * ftpState) | |
3411 | { | |
5f8252d2 | 3412 | ftpState->serverComplete(); |
3fdadc70 | 3413 | } |
3414 | ||
63be0a78 | 3415 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 3416 | static void |
3417 | ftpTrySlashHack(FtpStateData * ftpState) | |
3418 | { | |
3419 | char *path; | |
e55f0142 | 3420 | ftpState->flags.try_slash_hack = 1; |
969c39b9 | 3421 | /* Free old paths */ |
62e76326 | 3422 | |
a689bd4e | 3423 | debugs(9, 3, HERE); |
3424 | ||
969c39b9 | 3425 | if (ftpState->pathcomps) |
62e76326 | 3426 | wordlistDestroy(&ftpState->pathcomps); |
3427 | ||
969c39b9 | 3428 | safe_free(ftpState->filepath); |
62e76326 | 3429 | |
969c39b9 | 3430 | /* Build the new path (urlpath begins with /) */ |
d53b3f6d | 3431 | path = xstrdup(ftpState->request->urlpath.termedBuf()); |
62e76326 | 3432 | |
969c39b9 | 3433 | rfc1738_unescape(path); |
62e76326 | 3434 | |
969c39b9 | 3435 | ftpState->filepath = path; |
62e76326 | 3436 | |
969c39b9 | 3437 | /* And off we go */ |
dbfed404 | 3438 | ftpGetFile(ftpState); |
969c39b9 | 3439 | } |
3440 | ||
63be0a78 | 3441 | /** |
3442 | * Forget hack status. Next error is shown to the user | |
3443 | */ | |
6f0aab86 | 3444 | void |
3445 | FtpStateData::unhack() | |
0f169992 | 3446 | { |
a689bd4e | 3447 | debugs(9, 3, HERE); |
3448 | ||
6f0aab86 | 3449 | if (old_request != NULL) { |
3450 | safe_free(old_request); | |
3451 | safe_free(old_reply); | |
0f169992 | 3452 | } |
3453 | } | |
3454 | ||
6f0aab86 | 3455 | void |
3456 | FtpStateData::hackShortcut(FTPSM * nextState) | |
969c39b9 | 3457 | { |
c7e0305b | 3458 | /* Clear some unwanted state */ |
9fa2e208 | 3459 | setCurrentOffset(0); |
6f0aab86 | 3460 | restart_offset = 0; |
0f169992 | 3461 | /* Save old error message & some state info */ |
62e76326 | 3462 | |
a689bd4e | 3463 | debugs(9, 3, HERE); |
3464 | ||
6f0aab86 | 3465 | if (old_request == NULL) { |
3466 | old_request = ctrl.last_command; | |
3467 | ctrl.last_command = NULL; | |
3468 | old_reply = ctrl.last_reply; | |
3469 | ctrl.last_reply = NULL; | |
62e76326 | 3470 | |
6f0aab86 | 3471 | if (pathcomps == NULL && filepath != NULL) |
3472 | old_filepath = xstrdup(filepath); | |
0f169992 | 3473 | } |
62e76326 | 3474 | |
969c39b9 | 3475 | /* Jump to the "hack" state */ |
6f0aab86 | 3476 | nextState(this); |
969c39b9 | 3477 | } |
3478 | ||
63be0a78 | 3479 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 3480 | static void |
6f0aab86 | 3481 | ftpFail(FtpStateData *ftpState) |
3fdadc70 | 3482 | { |
4d6c56a6 | 3483 | debugs(9, 6, HERE << "flags(" << |
9e008dda AJ |
3484 | (ftpState->flags.isdir?"IS_DIR,":"") << |
3485 | (ftpState->flags.try_slash_hack?"TRY_SLASH_HACK":"") << "), " << | |
3486 | "mdtm=" << ftpState->mdtm << ", size=" << ftpState->theSize << | |
3487 | "slashhack=" << (ftpState->request->urlpath.caseCmp("/%2f", 4)==0? "T":"F") ); | |
62e76326 | 3488 | |
a689bd4e | 3489 | /* Try the / hack to support "Netscape" FTP URL's for retreiving files */ |
0cdcddb9 | 3490 | if (!ftpState->flags.isdir && /* Not a directory */ |
62e76326 | 3491 | !ftpState->flags.try_slash_hack && /* Not in slash hack */ |
47f6e231 | 3492 | ftpState->mdtm <= 0 && ftpState->theSize < 0 && /* Not known as a file */ |
30abd221 | 3493 | ftpState->request->urlpath.caseCmp("/%2f", 4) != 0) { /* No slash encoded */ |
62e76326 | 3494 | |
3495 | switch (ftpState->state) { | |
3496 | ||
3497 | case SENT_CWD: | |
3498 | ||
3499 | case SENT_RETR: | |
3500 | /* Try the / hack */ | |
6f0aab86 | 3501 | ftpState->hackShortcut(ftpTrySlashHack); |
62e76326 | 3502 | return; |
3503 | ||
3504 | default: | |
3505 | break; | |
3506 | } | |
969c39b9 | 3507 | } |
62e76326 | 3508 | |
6f0aab86 | 3509 | ftpState->failed(ERR_NONE, 0); |
3510 | /* failed() closes ctrl.fd and frees this */ | |
9bc73deb | 3511 | } |
3512 | ||
6f0aab86 | 3513 | void |
3514 | FtpStateData::failed(err_type error, int xerrno) | |
9bc73deb | 3515 | { |
4d6c56a6 | 3516 | debugs(9,3,HERE << "entry-null=" << (entry?entry->isEmpty():0) << ", entry=" << entry); |
528b2c61 | 3517 | if (entry->isEmpty()) |
6f0aab86 | 3518 | failedErrorMessage(error, xerrno); |
62e76326 | 3519 | |
5f8252d2 | 3520 | serverComplete(); |
9bc73deb | 3521 | } |
3522 | ||
6f0aab86 | 3523 | void |
3524 | FtpStateData::failedErrorMessage(err_type error, int xerrno) | |
9bc73deb | 3525 | { |
c3669e32 | 3526 | ErrorState *ftperr; |
a2c963ae | 3527 | const char *command, *reply; |
c3669e32 | 3528 | |
b6a2f15e | 3529 | /* Translate FTP errors into HTTP errors */ |
c3669e32 | 3530 | ftperr = NULL; |
62e76326 | 3531 | |
9bc73deb | 3532 | switch (error) { |
62e76326 | 3533 | |
9bc73deb | 3534 | case ERR_NONE: |
62e76326 | 3535 | |
6f0aab86 | 3536 | switch (state) { |
62e76326 | 3537 | |
3538 | case SENT_USER: | |
3539 | ||
3540 | case SENT_PASS: | |
3541 | ||
6f0aab86 | 3542 | if (ctrl.replycode > 500) |
3543 | if (password_url) | |
c3669e32 | 3544 | ftperr = errorCon(ERR_FTP_FORBIDDEN, HTTP_FORBIDDEN, fwd->request); |
c4a2d5e1 | 3545 | else |
c3669e32 | 3546 | ftperr = errorCon(ERR_FTP_FORBIDDEN, HTTP_UNAUTHORIZED, fwd->request); |
c4a2d5e1 | 3547 | |
6f0aab86 | 3548 | else if (ctrl.replycode == 421) |
c3669e32 | 3549 | ftperr = errorCon(ERR_FTP_UNAVAILABLE, HTTP_SERVICE_UNAVAILABLE, fwd->request); |
62e76326 | 3550 | |
3551 | break; | |
3552 | ||
3553 | case SENT_CWD: | |
3554 | ||
3555 | case SENT_RETR: | |
6f0aab86 | 3556 | if (ctrl.replycode == 550) |
c3669e32 | 3557 | ftperr = errorCon(ERR_FTP_NOT_FOUND, HTTP_NOT_FOUND, fwd->request); |
62e76326 | 3558 | |
3559 | break; | |
3560 | ||
3561 | default: | |
3562 | break; | |
3563 | } | |
3564 | ||
3565 | break; | |
3566 | ||
9bc73deb | 3567 | case ERR_READ_TIMEOUT: |
c3669e32 | 3568 | ftperr = errorCon(error, HTTP_GATEWAY_TIMEOUT, fwd->request); |
62e76326 | 3569 | break; |
3570 | ||
b6a2f15e | 3571 | default: |
c3669e32 | 3572 | ftperr = errorCon(error, HTTP_BAD_GATEWAY, fwd->request); |
62e76326 | 3573 | break; |
b6a2f15e | 3574 | } |
62e76326 | 3575 | |
c3669e32 AJ |
3576 | if (ftperr == NULL) |
3577 | ftperr = errorCon(ERR_FTP_FAILURE, HTTP_BAD_GATEWAY, fwd->request); | |
62e76326 | 3578 | |
c3669e32 | 3579 | ftperr->xerrno = xerrno; |
62e76326 | 3580 | |
c3669e32 | 3581 | ftperr->ftp.server_msg = ctrl.message; |
6f0aab86 | 3582 | ctrl.message = NULL; |
62e76326 | 3583 | |
6f0aab86 | 3584 | if (old_request) |
3585 | command = old_request; | |
969c39b9 | 3586 | else |
6f0aab86 | 3587 | command = ctrl.last_command; |
62e76326 | 3588 | |
9bc73deb | 3589 | if (command && strncmp(command, "PASS", 4) == 0) |
62e76326 | 3590 | command = "PASS <yourpassword>"; |
3591 | ||
6f0aab86 | 3592 | if (old_reply) |
3593 | reply = old_reply; | |
969c39b9 | 3594 | else |
6f0aab86 | 3595 | reply = ctrl.last_reply; |
62e76326 | 3596 | |
9bc73deb | 3597 | if (command) |
c3669e32 | 3598 | ftperr->ftp.request = xstrdup(command); |
62e76326 | 3599 | |
9bc73deb | 3600 | if (reply) |
c3669e32 | 3601 | ftperr->ftp.reply = xstrdup(reply); |
62e76326 | 3602 | |
c3669e32 AJ |
3603 | entry->replaceHttpReply( ftperr->BuildHttpReply() ); |
3604 | errorStateFree(ftperr); | |
3fdadc70 | 3605 | } |
3606 | ||
63be0a78 | 3607 | /// \ingroup ServerProtocolFTPInternal |
54220df8 | 3608 | static void |
3609 | ftpSendReply(FtpStateData * ftpState) | |
3610 | { | |
3611 | ErrorState *err; | |
acce49bf | 3612 | int code = ftpState->ctrl.replycode; |
728da2ee | 3613 | http_status http_code; |
3614 | err_type err_code = ERR_NONE; | |
a11382aa | 3615 | |
a689bd4e | 3616 | debugs(9, 3, HERE << ftpState->entry->url() << ", code " << code); |
62e76326 | 3617 | |
fa80a8ef | 3618 | if (cbdataReferenceValid(ftpState)) |
a689bd4e | 3619 | debugs(9, 5, HERE << "ftpState (" << ftpState << ") is valid!"); |
62e76326 | 3620 | |
bbf07a94 | 3621 | if (code == 226 || code == 250) { |
62e76326 | 3622 | err_code = (ftpState->mdtm > 0) ? ERR_FTP_PUT_MODIFIED : ERR_FTP_PUT_CREATED; |
3623 | http_code = (ftpState->mdtm > 0) ? HTTP_ACCEPTED : HTTP_CREATED; | |
9bc73deb | 3624 | } else if (code == 227) { |
62e76326 | 3625 | err_code = ERR_FTP_PUT_CREATED; |
3626 | http_code = HTTP_CREATED; | |
54220df8 | 3627 | } else { |
62e76326 | 3628 | err_code = ERR_FTP_PUT_ERROR; |
3629 | http_code = HTTP_INTERNAL_SERVER_ERROR; | |
54220df8 | 3630 | } |
62e76326 | 3631 | |
2cc81f1f | 3632 | err = errorCon(err_code, http_code, ftpState->request); |
62e76326 | 3633 | |
54220df8 | 3634 | if (ftpState->old_request) |
62e76326 | 3635 | err->ftp.request = xstrdup(ftpState->old_request); |
54220df8 | 3636 | else |
62e76326 | 3637 | err->ftp.request = xstrdup(ftpState->ctrl.last_command); |
3638 | ||
54220df8 | 3639 | if (ftpState->old_reply) |
62e76326 | 3640 | err->ftp.reply = xstrdup(ftpState->old_reply); |
0dfb298f | 3641 | else if (ftpState->ctrl.last_reply) |
62e76326 | 3642 | err->ftp.reply = xstrdup(ftpState->ctrl.last_reply); |
0dfb298f | 3643 | else |
62e76326 | 3644 | err->ftp.reply = xstrdup(""); |
3645 | ||
c3669e32 AJ |
3646 | ftpState->entry->replaceHttpReply( err->BuildHttpReply() ); |
3647 | errorStateFree(err); | |
62e76326 | 3648 | |
9bc73deb | 3649 | ftpSendQuit(ftpState); |
54220df8 | 3650 | } |
3651 | ||
6f0aab86 | 3652 | void |
3653 | FtpStateData::appendSuccessHeader() | |
3fdadc70 | 3654 | { |
a2c963ae | 3655 | const char *mime_type = NULL; |
3656 | const char *mime_enc = NULL; | |
30abd221 | 3657 | String urlpath = request->urlpath; |
02922e76 | 3658 | const char *filename = NULL; |
3659 | const char *t = NULL; | |
253caccb | 3660 | |
a689bd4e | 3661 | debugs(9, 3, HERE); |
ee65546f | 3662 | |
6f0aab86 | 3663 | if (flags.http_header_sent) |
62e76326 | 3664 | return; |
3665 | ||
585ab260 | 3666 | HttpReply *reply = new HttpReply; |
3667 | ||
6f0aab86 | 3668 | flags.http_header_sent = 1; |
62e76326 | 3669 | |
0477a072 | 3670 | assert(entry->isEmpty()); |
62e76326 | 3671 | |
0477a072 | 3672 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); |
62e76326 | 3673 | |
0477a072 | 3674 | entry->buffer(); /* released when done processing current data payload */ |
b66315e4 | 3675 | |
d53b3f6d | 3676 | filename = (t = urlpath.rpos('/')) ? t + 1 : urlpath.termedBuf(); |
62e76326 | 3677 | |
6f0aab86 | 3678 | if (flags.isdir) { |
62e76326 | 3679 | mime_type = "text/html"; |
3fdadc70 | 3680 | } else { |
6f0aab86 | 3681 | switch (typecode) { |
62e76326 | 3682 | |
3683 | case 'I': | |
3684 | mime_type = "application/octet-stream"; | |
3685 | mime_enc = mimeGetContentEncoding(filename); | |
3686 | break; | |
3687 | ||
3688 | case 'A': | |
3689 | mime_type = "text/plain"; | |
3690 | break; | |
3691 | ||
3692 | default: | |
3693 | mime_type = mimeGetContentType(filename); | |
3694 | mime_enc = mimeGetContentEncoding(filename); | |
3695 | break; | |
3696 | } | |
3fdadc70 | 3697 | } |
62e76326 | 3698 | |
cb69b4c7 | 3699 | /* set standard stuff */ |
62e76326 | 3700 | |
9fa2e208 | 3701 | if (0 == getCurrentOffset()) { |
690b22c6 | 3702 | /* Full reply */ |
11992b6f | 3703 | reply->setHeaders(HTTP_OK, "Gatewaying", mime_type, theSize, mdtm, -2); |
9fa2e208 | 3704 | } else if (theSize < getCurrentOffset()) { |
9e008dda AJ |
3705 | /* |
3706 | * DPW 2007-05-04 | |
3707 | * offset should not be larger than theSize. We should | |
3708 | * not be seeing this condition any more because we'll only | |
3709 | * send REST if we know the theSize and if it is less than theSize. | |
3710 | */ | |
3711 | debugs(0,DBG_CRITICAL,HERE << "Whoops! " << | |
9fa2e208 | 3712 | " current offset=" << getCurrentOffset() << |
9e008dda AJ |
3713 | ", but theSize=" << theSize << |
3714 | ". assuming full content response"); | |
11992b6f | 3715 | reply->setHeaders(HTTP_OK, "Gatewaying", mime_type, theSize, mdtm, -2); |
690b22c6 | 3716 | } else { |
62e76326 | 3717 | /* Partial reply */ |
3718 | HttpHdrRangeSpec range_spec; | |
9fa2e208 CT |
3719 | range_spec.offset = getCurrentOffset(); |
3720 | range_spec.length = theSize - getCurrentOffset(); | |
11992b6f | 3721 | reply->setHeaders(HTTP_PARTIAL_CONTENT, "Gatewaying", mime_type, theSize - getCurrentOffset(), mdtm, -2); |
47f6e231 | 3722 | httpHeaderAddContRange(&reply->header, range_spec, theSize); |
cfbf5373 | 3723 | } |
62e76326 | 3724 | |
cb69b4c7 | 3725 | /* additional info */ |
3726 | if (mime_enc) | |
a9925b40 | 3727 | reply->header.putStr(HDR_CONTENT_ENCODING, mime_enc); |
62e76326 | 3728 | |
585ab260 | 3729 | setVirginReply(reply); |
3730 | adaptOrFinalizeReply(); | |
063a47b5 | 3731 | } |
3732 | ||
3733 | void | |
3734 | FtpStateData::haveParsedReplyHeaders() | |
3735 | { | |
c1520b67 AJ |
3736 | ServerStateData::haveParsedReplyHeaders(); |
3737 | ||
063a47b5 | 3738 | StoreEntry *e = entry; |
62e76326 | 3739 | |
3900307b | 3740 | e->timestampsSet(); |
62e76326 | 3741 | |
6f0aab86 | 3742 | if (flags.authenticated) { |
62e76326 | 3743 | /* |
3744 | * Authenticated requests can't be cached. | |
3745 | */ | |
5f33b71d | 3746 | e->release(); |
9fa2e208 | 3747 | } else if (EBIT_TEST(e->flags, ENTRY_CACHABLE) && !getCurrentOffset()) { |
d88e3c49 | 3748 | e->setPublicKey(); |
c68e9c6b | 3749 | } else { |
5f33b71d | 3750 | e->release(); |
c68e9c6b | 3751 | } |
77a30ebb | 3752 | } |
bfcaf585 | 3753 | |
6f0aab86 | 3754 | HttpReply * |
3755 | FtpStateData::ftpAuthRequired(HttpRequest * request, const char *realm) | |
cb69b4c7 | 3756 | { |
2cc81f1f | 3757 | ErrorState *err = errorCon(ERR_CACHE_ACCESS_DENIED, HTTP_UNAUTHORIZED, request); |
c70281f8 | 3758 | HttpReply *newrep = err->BuildHttpReply(); |
cb69b4c7 | 3759 | errorStateFree(err); |
63259c34 | 3760 | /* add Authenticate header */ |
a9925b40 | 3761 | newrep->header.putAuth("Basic", realm); |
4a56ee8d | 3762 | return newrep; |
cb69b4c7 | 3763 | } |
8f872bb6 | 3764 | |
cc192b50 | 3765 | /** |
63be0a78 | 3766 | \ingroup ServerProtocolFTPAPI |
3767 | \todo Should be a URL class API call. | |
3768 | * | |
cc192b50 | 3769 | * Construct an URI with leading / in PATH portion for use by CWD command |
3770 | * possibly others. FTP encodes absolute paths as beginning with '/' | |
3771 | * after the initial URI path delimiter, which happens to be / itself. | |
3772 | * This makes FTP absolute URI appear as: ftp:host:port//root/path | |
3773 | * To encompass older software which compacts multiple // to / in transit | |
9e008dda | 3774 | * We use standard URI-encoding on the second / making it |
cc192b50 | 3775 | * ftp:host:port/%2froot/path AKA 'the FTP %2f hack'. |
3776 | */ | |
3777 | const char * | |
3778 | ftpUrlWith2f(HttpRequest * request) | |
8f872bb6 | 3779 | { |
cc192b50 | 3780 | String newbuf = "%2f"; |
62e76326 | 3781 | |
23d92c64 | 3782 | if (request->protocol != PROTO_FTP) |
62e76326 | 3783 | return NULL; |
3784 | ||
d53b3f6d | 3785 | if ( request->urlpath[0]=='/' ) { |
cc192b50 | 3786 | newbuf.append(request->urlpath); |
3787 | request->urlpath.absorb(newbuf); | |
3788 | safe_free(request->canonical); | |
d53b3f6d | 3789 | } else if ( !strncmp(request->urlpath.termedBuf(), "%2f", 3) ) { |
826a1fed | 3790 | newbuf.append(request->urlpath.substr(1,request->urlpath.size())); |
cc192b50 | 3791 | request->urlpath.absorb(newbuf); |
3792 | safe_free(request->canonical); | |
8f872bb6 | 3793 | } |
62e76326 | 3794 | |
cc192b50 | 3795 | return urlCanonical(request); |
8f872bb6 | 3796 | } |
253caccb | 3797 | |
3798 | void | |
3799 | FtpStateData::printfReplyBody(const char *fmt, ...) | |
3800 | { | |
3801 | va_list args; | |
3802 | va_start (args, fmt); | |
3803 | static char buf[4096]; | |
3804 | buf[0] = '\0'; | |
3805 | vsnprintf(buf, 4096, fmt, args); | |
3806 | writeReplyBody(buf, strlen(buf)); | |
3807 | } | |
3808 | ||
63be0a78 | 3809 | /** |
253caccb | 3810 | * Call this when there is data from the origin server |
3811 | * which should be sent to either StoreEntry, or to ICAP... | |
3812 | */ | |
3813 | void | |
e053c141 | 3814 | FtpStateData::writeReplyBody(const char *dataToWrite, size_t dataLength) |
253caccb | 3815 | { |
e053c141 FC |
3816 | debugs(9, 5, HERE << "writing " << dataLength << " bytes to the reply"); |
3817 | addVirginReplyBody(dataToWrite, dataLength); | |
253caccb | 3818 | } |
3819 | ||
63be0a78 | 3820 | /** |
3821 | * called after we wrote the last byte of the request body | |
3822 | */ | |
16846d00 | 3823 | void |
5f8252d2 | 3824 | FtpStateData::doneSendingRequestBody() |
16846d00 | 3825 | { |
a689bd4e | 3826 | debugs(9,3, HERE); |
f9ac241b | 3827 | dataComplete(); |
9e008dda AJ |
3828 | /* NP: RFC 959 3.3. DATA CONNECTION MANAGEMENT |
3829 | * if transfer type is 'stream' call dataComplete() | |
3830 | * otherwise leave open. (reschedule control channel read?) | |
3831 | */ | |
16846d00 | 3832 | } |
3833 | ||
63be0a78 | 3834 | /** |
3835 | * A hack to ensure we do not double-complete on the forward entry. | |
3836 | * | |
9e008dda | 3837 | \todo FtpStateData logic should probably be rewritten to avoid |
63be0a78 | 3838 | * double-completion or FwdState should be rewritten to allow it. |
3839 | */ | |
16846d00 | 3840 | void |
5f8252d2 | 3841 | FtpStateData::completeForwarding() |
16846d00 | 3842 | { |
5f8252d2 | 3843 | if (fwd == NULL || flags.completed_forwarding) { |
a689bd4e | 3844 | debugs(9, 3, HERE << "completeForwarding avoids " << |
9e008dda AJ |
3845 | "double-complete on FD " << ctrl.fd << ", Data FD " << data.fd << |
3846 | ", this " << this << ", fwd " << fwd); | |
5f8252d2 | 3847 | return; |
3848 | } | |
16846d00 | 3849 | |
5f8252d2 | 3850 | flags.completed_forwarding = true; |
3851 | ServerStateData::completeForwarding(); | |
16846d00 | 3852 | } |
3853 | ||
63be0a78 | 3854 | /** |
3855 | * Close the FTP server connection(s). Used by serverComplete(). | |
3856 | */ | |
253caccb | 3857 | void |
5f8252d2 | 3858 | FtpStateData::closeServer() |
253caccb | 3859 | { |
a689bd4e | 3860 | debugs(9,3, HERE << "closing FTP server FD " << ctrl.fd << ", Data FD " << data.fd << ", this " << this); |
253caccb | 3861 | |
c0d75e74 | 3862 | if (ctrl.fd > -1) { |
3863 | fwd->unregister(ctrl.fd); | |
94b88585 | 3864 | ctrl.close(); |
c0d75e74 | 3865 | } |
253caccb | 3866 | |
94b88585 | 3867 | data.close(); |
5f8252d2 | 3868 | } |
253caccb | 3869 | |
63be0a78 | 3870 | /** |
3871 | * Did we close all FTP server connection(s)? | |
3872 | * | |
04f55905 | 3873 | \retval true Both server control and data channels are closed. And not waitigng for a new data connection to open. |
63be0a78 | 3874 | \retval false Either control channel or data is still active. |
3875 | */ | |
5f8252d2 | 3876 | bool |
3877 | FtpStateData::doneWithServer() const | |
3878 | { | |
3879 | return ctrl.fd < 0 && data.fd < 0; | |
3880 | } | |
c0d75e74 | 3881 | |
63be0a78 | 3882 | /** |
3883 | * Have we lost the FTP server control channel? | |
3884 | * | |
3885 | \retval true The server control channel is available. | |
3886 | \retval false The server control channel is not available. | |
3887 | */ | |
a11382aa | 3888 | bool |
4784c403 | 3889 | FtpStateData::haveControlChannel(const char *caller_name) const |
a11382aa | 3890 | { |
9e008dda | 3891 | if (doneWithServer()) |
33ed5343 | 3892 | return false; |
a11382aa | 3893 | |
33ed5343 | 3894 | /* doneWithServer() only checks BOTH channels are closed. */ |
9e008dda | 3895 | if (ctrl.fd < 0) { |
a689bd4e | 3896 | debugs(9, DBG_IMPORTANT, "WARNING! FTP Server Control channel is closed, but Data channel still active."); |
33ed5343 | 3897 | debugs(9, 2, caller_name << ": attempted on a closed FTP channel."); |
3898 | return false; | |
3899 | } | |
3900 | ||
3901 | return true; | |
a11382aa | 3902 | } |
3903 | ||
63be0a78 | 3904 | /** |
3905 | * Quickly abort the transaction | |
3906 | * | |
3907 | \todo destruction should be sufficient as the destructor should cleanup, | |
3908 | * including canceling close handlers | |
3909 | */ | |
5f8252d2 | 3910 | void |
3911 | FtpStateData::abortTransaction(const char *reason) | |
3912 | { | |
a689bd4e | 3913 | debugs(9, 3, HERE << "aborting transaction for " << reason << |
9e008dda | 3914 | "; FD " << ctrl.fd << ", Data FD " << data.fd << ", this " << this); |
3e8c047e | 3915 | if (ctrl.fd >= 0) { |
5f8252d2 | 3916 | comm_close(ctrl.fd); |
3e8c047e | 3917 | return; |
3918 | } | |
5a5b4ce1 | 3919 | |
3e8c047e | 3920 | fwd->handleUnregisteredServerEnd(); |
dc56a9b1 | 3921 | deleteThis("FtpStateData::abortTransaction"); |
253caccb | 3922 | } |
94b88585 AR |
3923 | |
3924 | /// creates a data channel Comm close callback | |
3925 | AsyncCall::Pointer | |
3926 | FtpStateData::dataCloser() | |
3927 | { | |
3928 | typedef CommCbMemFunT<FtpStateData, CommCloseCbParams> Dialer; | |
3929 | return asyncCall(9, 5, "FtpStateData::dataClosed", | |
9e008dda | 3930 | Dialer(this, &FtpStateData::dataClosed)); |
94b88585 AR |
3931 | } |
3932 | ||
3933 | /// configures the channel with a descriptor and registers a close handler | |
3934 | void | |
3935 | FtpChannel::opened(int aFd, const AsyncCall::Pointer &aCloser) | |
3936 | { | |
3937 | assert(fd < 0); | |
3938 | assert(closer == NULL); | |
3939 | ||
3940 | assert(aFd >= 0); | |
3941 | assert(aCloser != NULL); | |
3942 | ||
3943 | fd = aFd; | |
3944 | closer = aCloser; | |
3945 | comm_add_close_handler(fd, closer); | |
3946 | } | |
3947 | ||
3948 | /// planned close: removes the close handler and calls comm_close | |
3949 | void | |
3950 | FtpChannel::close() | |
3951 | { | |
04f55905 AJ |
3952 | // channels with active listeners will be closed when the listener handler dies. |
3953 | if (listener) { | |
3954 | delete listener; | |
3955 | listener = NULL; | |
3956 | comm_remove_close_handler(fd, closer); | |
3957 | closer = NULL; | |
3958 | fd = -1; | |
97b8ac39 | 3959 | } else if (fd >= 0) { |
94b88585 AR |
3960 | comm_remove_close_handler(fd, closer); |
3961 | closer = NULL; | |
3962 | comm_close(fd); // we do not expect to be called back | |
3963 | fd = -1; | |
3964 | } | |
3965 | } | |
3966 | ||
3967 | /// just resets fd and close handler | |
3968 | void | |
3969 | FtpChannel::clear() | |
3970 | { | |
3971 | fd = -1; | |
3972 | closer = NULL; | |
3973 | } |