]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
262a0e14 | 2 | * $Id$ |
30a4f2a8 | 3 | * |
b510f3a1 | 4 | * DEBUG: section 09 File Transfer Protocol (FTP) |
30a4f2a8 | 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" | |
055421ee | 48 | #include "ip/tools.h" |
04f55905 | 49 | #include "MemBuf.h" |
056b24a1 | 50 | #include "rfc1738.h" |
04f55905 | 51 | #include "Server.h" |
056b24a1 | 52 | #include "SquidString.h" |
04f55905 AJ |
53 | #include "SquidTime.h" |
54 | #include "Store.h" | |
55 | #include "URLScheme.h" | |
56 | #include "wordlist.h" | |
57 | ||
b67e2c8c | 58 | #if DELAY_POOLS |
59 | #include "DelayPools.h" | |
86a2f789 | 60 | #include "MemObject.h" |
b67e2c8c | 61 | #endif |
253caccb | 62 | |
63be0a78 | 63 | /** |
64 | \defgroup ServerProtocolFTPInternal Server-Side FTP Internals | |
65 | \ingroup ServerProtocolFTPAPI | |
66 | */ | |
67 | ||
68 | /// \ingroup ServerProtocolFTPInternal | |
3fdadc70 | 69 | static const char *const crlf = "\r\n"; |
63be0a78 | 70 | |
71 | /// \ingroup ServerProtocolFTPInternal | |
3fdadc70 | 72 | static char cbuf[1024]; |
73 | ||
63be0a78 | 74 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 75 | typedef enum { |
76 | BEGIN, | |
77 | SENT_USER, | |
78 | SENT_PASS, | |
79 | SENT_TYPE, | |
80 | SENT_MDTM, | |
81 | SENT_SIZE, | |
cc192b50 | 82 | SENT_EPRT, |
3fdadc70 | 83 | SENT_PORT, |
a689bd4e | 84 | SENT_EPSV_ALL, |
85 | SENT_EPSV_1, | |
86 | SENT_EPSV_2, | |
3fdadc70 | 87 | SENT_PASV, |
88 | SENT_CWD, | |
89 | SENT_LIST, | |
90 | SENT_NLST, | |
91 | SENT_REST, | |
92 | SENT_RETR, | |
54220df8 | 93 | SENT_STOR, |
3fdadc70 | 94 | SENT_QUIT, |
54220df8 | 95 | READING_DATA, |
96 | WRITING_DATA, | |
97 | SENT_MKDIR | |
3fdadc70 | 98 | } ftp_state_t; |
090089c4 | 99 | |
63be0a78 | 100 | /// \ingroup ServerProtocolFTPInternal |
9e008dda | 101 | struct _ftp_flags { |
4e2da309 AJ |
102 | |
103 | /* passive mode */ | |
104 | bool pasv_supported; ///< PASV command is allowed | |
105 | bool epsv_all_sent; ///< EPSV ALL has been used. Must abort on failures. | |
106 | bool pasv_only; | |
107 | ||
108 | /* authentication */ | |
109 | bool authenticated; ///< authentication success | |
110 | bool tried_auth_anonymous; ///< auth has tried to use anonymous credentials already. | |
111 | bool tried_auth_nopass; ///< auth tried username with no password already. | |
112 | ||
113 | /* other */ | |
3977372c | 114 | bool isdir; |
3977372c | 115 | bool skip_whitespace; |
116 | bool rest_supported; | |
3977372c | 117 | bool http_header_sent; |
118 | bool tried_nlst; | |
119 | bool need_base_href; | |
120 | bool dir_slash; | |
121 | bool root_dir; | |
122 | bool no_dotdot; | |
3977372c | 123 | bool binary; |
124 | bool try_slash_hack; | |
125 | bool put; | |
126 | bool put_mkdir; | |
127 | bool listformat_unknown; | |
0477a072 | 128 | bool listing; |
5f8252d2 | 129 | bool completed_forwarding; |
e55f0142 | 130 | }; |
131 | ||
6f0aab86 | 132 | class FtpStateData; |
63be0a78 | 133 | |
134 | /// \ingroup ServerProtocolFTPInternal | |
6f0aab86 | 135 | typedef void (FTPSM) (FtpStateData *); |
136 | ||
94b88585 | 137 | /// common code for FTP control and data channels |
6e92b048 | 138 | // does not own the channel descriptor, which is managed by FtpStateData |
9e008dda AJ |
139 | class FtpChannel |
140 | { | |
94b88585 AR |
141 | public: |
142 | FtpChannel(): fd(-1) {} | |
143 | ||
144 | /// called after the socket is opened, sets up close handler | |
145 | void opened(int aFd, const AsyncCall::Pointer &aCloser); | |
146 | ||
04f55905 AJ |
147 | /** Handles all operations needed to properly close the active channel FD. |
148 | * clearing the close handler, clearing the listen socket properly, and calling comm_close | |
149 | */ | |
150 | void close(); | |
151 | ||
152 | void clear(); /// just resets fd and close handler. does not close active connections. | |
94b88585 AR |
153 | |
154 | int fd; /// channel descriptor; \todo: remove because the closer has it | |
155 | ||
04f55905 AJ |
156 | /** Current listening socket handler. delete on shutdown or abort. |
157 | * FTP stores a copy of the FD in the field fd above. | |
158 | * Use close() to properly close the channel. | |
159 | */ | |
160 | Comm::ListenStateData *listener; | |
161 | ||
94b88585 AR |
162 | private: |
163 | AsyncCall::Pointer closer; /// Comm close handler callback | |
164 | }; | |
165 | ||
63be0a78 | 166 | /// \ingroup ServerProtocolFTPInternal |
253caccb | 167 | class FtpStateData : public ServerStateData |
62e76326 | 168 | { |
0353e724 | 169 | |
170 | public: | |
171 | void *operator new (size_t); | |
172 | void operator delete (void *); | |
dc56a9b1 | 173 | void *toCbdata() { return this; } |
174 | ||
6f0aab86 | 175 | FtpStateData(FwdState *); |
0353e724 | 176 | ~FtpStateData(); |
77a30ebb | 177 | char user[MAX_URL]; |
178 | char password[MAX_URL]; | |
9bc73deb | 179 | int password_url; |
f0afe435 | 180 | char *reply_hdr; |
f0afe435 | 181 | int reply_hdr_state; |
30abd221 | 182 | String clean_url; |
183 | String title_url; | |
184 | String base_href; | |
3fdadc70 | 185 | int conn_att; |
186 | int login_att; | |
187 | ftp_state_t state; | |
3fdadc70 | 188 | time_t mdtm; |
47f6e231 | 189 | int64_t theSize; |
3fdadc70 | 190 | wordlist *pathcomps; |
191 | char *filepath; | |
5627a633 | 192 | char *dirpath; |
47f6e231 | 193 | int64_t restart_offset; |
3fdadc70 | 194 | char *proxy_host; |
195 | size_t list_width; | |
0477a072 | 196 | String cwd_message; |
969c39b9 | 197 | char *old_request; |
198 | char *old_reply; | |
0f169992 | 199 | char *old_filepath; |
9e242e02 | 200 | char typecode; |
0477a072 | 201 | MemBuf listing; ///< FTP directory listing in HTML format. |
62e76326 | 202 | |
94b88585 AR |
203 | // \todo: optimize ctrl and data structs member order, to minimize size |
204 | /// FTP control channel info; the channel is opened once per transaction | |
9e008dda | 205 | struct CtrlChannel: public FtpChannel { |
62e76326 | 206 | char *buf; |
207 | size_t size; | |
47f6e231 | 208 | size_t offset; |
62e76326 | 209 | wordlist *message; |
210 | char *last_command; | |
211 | char *last_reply; | |
212 | int replycode; | |
2fadd50d | 213 | } ctrl; |
62e76326 | 214 | |
94b88585 | 215 | /// FTP data channel info; the channel may be opened/closed a few times |
9e008dda | 216 | struct DataChannel: public FtpChannel { |
253caccb | 217 | MemBuf *readBuf; |
62e76326 | 218 | char *host; |
219 | u_short port; | |
253caccb | 220 | bool read_pending; |
2fadd50d | 221 | } data; |
62e76326 | 222 | |
e55f0142 | 223 | struct _ftp_flags flags; |
0353e724 | 224 | |
225 | private: | |
226 | CBDATA_CLASS(FtpStateData); | |
6f0aab86 | 227 | |
228 | public: | |
253caccb | 229 | // these should all be private |
6f0aab86 | 230 | void start(); |
231 | void loginParser(const char *, int escaped); | |
232 | int restartable(); | |
233 | void appendSuccessHeader(); | |
234 | void hackShortcut(FTPSM * nextState); | |
235 | void failed(err_type, int xerrno); | |
236 | void failedErrorMessage(err_type, int xerrno); | |
237 | void unhack(); | |
6f0aab86 | 238 | void scheduleReadControlReply(int); |
239 | void handleControlReply(); | |
5f8252d2 | 240 | void readStor(); |
f1a83a7d | 241 | void parseListing(); |
0477a072 AJ |
242 | MemBuf *htmlifyListEntry(const char *line); |
243 | void completedListing(void); | |
f1a83a7d | 244 | void dataComplete(); |
dc56a9b1 | 245 | void dataRead(const CommIoCbParams &io); |
f1a83a7d | 246 | int checkAuth(const HttpHeader * req_hdr); |
247 | void checkUrlpath(); | |
248 | void buildTitleUrl(); | |
47f6e231 | 249 | void writeReplyBody(const char *, size_t len); |
253caccb | 250 | void printfReplyBody(const char *fmt, ...); |
5f8252d2 | 251 | virtual int dataDescriptor() const; |
252 | virtual void maybeReadVirginBody(); | |
253 | virtual void closeServer(); | |
254 | virtual void completeForwarding(); | |
255 | virtual void abortTransaction(const char *reason); | |
063a47b5 | 256 | void processHeadResponse(); |
253caccb | 257 | void processReplyBody(); |
9317b41a | 258 | void writeCommand(const char *buf); |
9fa2e208 CT |
259 | void setCurrentOffset(int64_t offset) { currentOffset = offset; } |
260 | int64_t getCurrentOffset() const { return currentOffset; } | |
6f0aab86 | 261 | |
6f0aab86 | 262 | static CNCB ftpPasvCallback; |
6f0aab86 | 263 | static PF ftpDataWrite; |
dc56a9b1 | 264 | void ftpTimeout(const CommTimeoutCbParams &io); |
94b88585 AR |
265 | void ctrlClosed(const CommCloseCbParams &io); |
266 | void dataClosed(const CommCloseCbParams &io); | |
dc56a9b1 | 267 | void ftpReadControlReply(const CommIoCbParams &io); |
268 | void ftpWriteCommandCallback(const CommIoCbParams &io); | |
269 | void ftpAcceptDataConnection(const CommAcceptCbParams &io); | |
270 | ||
6f0aab86 | 271 | static HttpReply *ftpAuthRequired(HttpRequest * request, const char *realm); |
f381d699 AJ |
272 | const char *ftpRealm(void); |
273 | void loginFailed(void); | |
47f6e231 | 274 | static wordlist *ftpParseControlReply(char *, size_t, int *, size_t *); |
253caccb | 275 | |
5f8252d2 | 276 | // sending of the request body to the server |
dc56a9b1 | 277 | virtual void sentRequestBody(const CommIoCbParams&); |
5f8252d2 | 278 | virtual void doneSendingRequestBody(); |
279 | ||
063a47b5 | 280 | virtual void haveParsedReplyHeaders(); |
281 | ||
5f8252d2 | 282 | virtual bool doneWithServer() const; |
aa190430 | 283 | virtual bool haveControlChannel(const char *caller_name) const; |
94b88585 | 284 | AsyncCall::Pointer dataCloser(); /// creates a Comm close callback |
253caccb | 285 | |
5f8252d2 | 286 | private: |
287 | // BodyConsumer for HTTP: consume request body. | |
288 | virtual void handleRequestBodyProducerAborted(); | |
0353e724 | 289 | }; |
290 | ||
291 | CBDATA_CLASS_INIT(FtpStateData); | |
292 | ||
293 | void * | |
294 | FtpStateData::operator new (size_t) | |
295 | { | |
296 | CBDATA_INIT_TYPE(FtpStateData); | |
297 | FtpStateData *result = cbdataAlloc(FtpStateData); | |
298 | return result; | |
62e76326 | 299 | } |
090089c4 | 300 | |
0353e724 | 301 | void |
302 | FtpStateData::operator delete (void *address) | |
303 | { | |
304 | FtpStateData *t = static_cast<FtpStateData *>(address); | |
305 | cbdataFree(t); | |
306 | } | |
307 | ||
63be0a78 | 308 | /// \ingroup ServerProtocolFTPInternal |
9e008dda | 309 | typedef struct { |
3fdadc70 | 310 | char type; |
47f6e231 | 311 | int64_t size; |
3fdadc70 | 312 | char *date; |
313 | char *name; | |
314 | char *showname; | |
315 | char *link; | |
2fadd50d | 316 | } ftpListParts; |
3fdadc70 | 317 | |
63be0a78 | 318 | /// \ingroup ServerProtocolFTPInternal |
a689bd4e | 319 | #define FTP_LOGIN_ESCAPED 1 |
63be0a78 | 320 | |
321 | /// \ingroup ServerProtocolFTPInternal | |
a689bd4e | 322 | #define FTP_LOGIN_NOT_ESCAPED 0 |
c68e9c6b | 323 | |
7e3ce7b9 | 324 | /* |
325 | * State machine functions | |
969c39b9 | 326 | * send == state transition |
327 | * read == wait for response, and select next state transition | |
dbfed404 | 328 | * other == Transition logic |
969c39b9 | 329 | */ |
3fdadc70 | 330 | static FTPSM ftpReadWelcome; |
969c39b9 | 331 | static FTPSM ftpSendUser; |
3fdadc70 | 332 | static FTPSM ftpReadUser; |
969c39b9 | 333 | static FTPSM ftpSendPass; |
3fdadc70 | 334 | static FTPSM ftpReadPass; |
969c39b9 | 335 | static FTPSM ftpSendType; |
3fdadc70 | 336 | static FTPSM ftpReadType; |
969c39b9 | 337 | static FTPSM ftpSendMdtm; |
3fdadc70 | 338 | static FTPSM ftpReadMdtm; |
969c39b9 | 339 | static FTPSM ftpSendSize; |
3fdadc70 | 340 | static FTPSM ftpReadSize; |
cc192b50 | 341 | static FTPSM ftpSendEPRT; |
342 | static FTPSM ftpReadEPRT; | |
343 | static FTPSM ftpSendPORT; | |
344 | static FTPSM ftpReadPORT; | |
a689bd4e | 345 | static FTPSM ftpSendPassive; |
cc192b50 | 346 | static FTPSM ftpReadEPSV; |
3fdadc70 | 347 | static FTPSM ftpReadPasv; |
dbfed404 | 348 | static FTPSM ftpTraverseDirectory; |
349 | static FTPSM ftpListDir; | |
350 | static FTPSM ftpGetFile; | |
969c39b9 | 351 | static FTPSM ftpSendCwd; |
3fdadc70 | 352 | static FTPSM ftpReadCwd; |
94439e4e | 353 | static FTPSM ftpRestOrList; |
969c39b9 | 354 | static FTPSM ftpSendList; |
355 | static FTPSM ftpSendNlst; | |
3fdadc70 | 356 | static FTPSM ftpReadList; |
969c39b9 | 357 | static FTPSM ftpSendRest; |
3fdadc70 | 358 | static FTPSM ftpReadRest; |
969c39b9 | 359 | static FTPSM ftpSendRetr; |
3fdadc70 | 360 | static FTPSM ftpReadRetr; |
361 | static FTPSM ftpReadTransferDone; | |
54220df8 | 362 | static FTPSM ftpSendStor; |
363 | static FTPSM ftpReadStor; | |
94439e4e | 364 | static FTPSM ftpWriteTransferDone; |
54220df8 | 365 | static FTPSM ftpSendReply; |
94439e4e | 366 | static FTPSM ftpSendMkdir; |
54220df8 | 367 | static FTPSM ftpReadMkdir; |
94439e4e | 368 | static FTPSM ftpFail; |
369 | static FTPSM ftpSendQuit; | |
370 | static FTPSM ftpReadQuit; | |
a689bd4e | 371 | |
372 | /************************************************ | |
373 | ** Debugs Levels used here ** | |
374 | ************************************************* | |
375 | 0 CRITICAL Events | |
376 | 1 IMPORTANT Events | |
377 | Protocol and Transmission failures. | |
378 | 2 FTP Protocol Chatter | |
379 | 3 Logic Flows | |
380 | 4 Data Parsing Flows | |
381 | 5 Data Dumps | |
382 | 7 ?? | |
383 | ************************************************/ | |
384 | ||
dbfed404 | 385 | /************************************************ |
386 | ** State Machine Description (excluding hacks) ** | |
387 | ************************************************* | |
388 | From To | |
389 | --------------------------------------- | |
390 | Welcome User | |
391 | User Pass | |
392 | Pass Type | |
393 | Type TraverseDirectory / GetFile | |
394 | TraverseDirectory Cwd / GetFile / ListDir | |
94439e4e | 395 | Cwd TraverseDirectory / Mkdir |
dbfed404 | 396 | GetFile Mdtm |
397 | Mdtm Size | |
a689bd4e | 398 | Size Epsv |
399 | ListDir Epsv | |
400 | Epsv FileOrList | |
94439e4e | 401 | FileOrList Rest / Retr / Nlst / List / Mkdir (PUT /xxx;type=d) |
dbfed404 | 402 | Rest Retr |
94439e4e | 403 | Retr / Nlst / List DataRead* (on datachannel) |
404 | DataRead* ReadTransferDone | |
dbfed404 | 405 | ReadTransferDone DataTransferDone |
94439e4e | 406 | Stor DataWrite* (on datachannel) |
407 | DataWrite* RequestPutBody** (from client) | |
408 | RequestPutBody** DataWrite* / WriteTransferDone | |
409 | WriteTransferDone DataTransferDone | |
dbfed404 | 410 | DataTransferDone Quit |
411 | Quit - | |
412 | ************************************************/ | |
3fdadc70 | 413 | |
63be0a78 | 414 | /// \ingroup ServerProtocolFTPInternal |
9e008dda AJ |
415 | FTPSM *FTP_SM_FUNCS[] = { |
416 | ftpReadWelcome, /* BEGIN */ | |
417 | ftpReadUser, /* SENT_USER */ | |
418 | ftpReadPass, /* SENT_PASS */ | |
419 | ftpReadType, /* SENT_TYPE */ | |
420 | ftpReadMdtm, /* SENT_MDTM */ | |
421 | ftpReadSize, /* SENT_SIZE */ | |
422 | ftpReadEPRT, /* SENT_EPRT */ | |
423 | ftpReadPORT, /* SENT_PORT */ | |
424 | ftpReadEPSV, /* SENT_EPSV_ALL */ | |
425 | ftpReadEPSV, /* SENT_EPSV_1 */ | |
426 | ftpReadEPSV, /* SENT_EPSV_2 */ | |
427 | ftpReadPasv, /* SENT_PASV */ | |
428 | ftpReadCwd, /* SENT_CWD */ | |
429 | ftpReadList, /* SENT_LIST */ | |
430 | ftpReadList, /* SENT_NLST */ | |
431 | ftpReadRest, /* SENT_REST */ | |
432 | ftpReadRetr, /* SENT_RETR */ | |
433 | ftpReadStor, /* SENT_STOR */ | |
434 | ftpReadQuit, /* SENT_QUIT */ | |
435 | ftpReadTransferDone, /* READING_DATA (RETR,LIST,NLST) */ | |
436 | ftpWriteTransferDone, /* WRITING_DATA (STOR) */ | |
437 | ftpReadMkdir /* SENT_MKDIR */ | |
438 | }; | |
e381a13d | 439 | |
6e92b048 | 440 | /// handler called by Comm when FTP control channel is closed unexpectedly |
9e008dda | 441 | void |
94b88585 AR |
442 | FtpStateData::ctrlClosed(const CommCloseCbParams &io) |
443 | { | |
444 | ctrl.clear(); | |
445 | deleteThis("FtpStateData::ctrlClosed"); | |
446 | } | |
447 | ||
6e92b048 | 448 | /// handler called by Comm when FTP data channel is closed unexpectedly |
9e008dda | 449 | void |
94b88585 | 450 | FtpStateData::dataClosed(const CommCloseCbParams &io) |
518a192e | 451 | { |
04f55905 AJ |
452 | if (data.listener) { |
453 | delete data.listener; | |
454 | data.listener = NULL; | |
455 | data.fd = -1; | |
456 | } | |
94b88585 | 457 | data.clear(); |
5a5b4ce1 | 458 | failed(ERR_FTP_FAILURE, 0); |
6e92b048 | 459 | /* failed closes ctrl.fd and frees ftpState */ |
5a5b4ce1 AJ |
460 | |
461 | /* NP: failure recovery may be possible when its only a data.fd failure. | |
462 | * is the ctrl.fd is still fine, we can send ABOR down it and retry. | |
463 | * Just need to watch out for wider Squid states like shutting down or reconfigure. | |
464 | */ | |
6f0aab86 | 465 | } |
1be4874e | 466 | |
dc56a9b1 | 467 | FtpStateData::FtpStateData(FwdState *theFwdState) : AsyncJob("FtpStateData"), ServerStateData(theFwdState) |
6f0aab86 | 468 | { |
3900307b | 469 | const char *url = entry->url(); |
a689bd4e | 470 | debugs(9, 3, HERE << "'" << url << "'" ); |
6f0aab86 | 471 | statCounter.server.all.requests++; |
472 | statCounter.server.ftp.requests++; | |
47f6e231 | 473 | theSize = -1; |
fe6b2914 | 474 | mdtm = -1; |
6f0aab86 | 475 | |
fe6b2914 | 476 | if (Config.Ftp.passive && !theFwdState->ftpPasvFailed()) |
477 | flags.pasv_supported = 1; | |
6f0aab86 | 478 | |
fe6b2914 | 479 | flags.rest_supported = 1; |
6f0aab86 | 480 | |
dc56a9b1 | 481 | typedef CommCbMemFunT<FtpStateData, CommCloseCbParams> Dialer; |
4299f876 AR |
482 | AsyncCall::Pointer closer = JobCallback(9, 5, |
483 | Dialer, this, FtpStateData::ctrlClosed); | |
94b88585 | 484 | ctrl.opened(theFwdState->server_fd, closer); |
6f0aab86 | 485 | |
fe6b2914 | 486 | if (request->method == METHOD_PUT) |
487 | flags.put = 1; | |
518a192e | 488 | } |
489 | ||
0353e724 | 490 | FtpStateData::~FtpStateData() |
ba718c8f | 491 | { |
a689bd4e | 492 | debugs(9, 3, HERE << entry->url() ); |
62e76326 | 493 | |
6f0aab86 | 494 | if (reply_hdr) { |
495 | memFree(reply_hdr, MEM_8K_BUF); | |
496 | reply_hdr = NULL; | |
497 | } | |
62e76326 | 498 | |
94b88585 | 499 | data.close(); |
62e76326 | 500 | |
6e92b048 AR |
501 | if (ctrl.fd >= 0) { |
502 | debugs(9, DBG_IMPORTANT, HERE << "Internal bug: FtpStateData left " << | |
9e008dda | 503 | "control FD " << ctrl.fd << " open"); |
6e92b048 AR |
504 | } |
505 | ||
6f0aab86 | 506 | if (ctrl.buf) { |
507 | memFreeBuf(ctrl.size, ctrl.buf); | |
508 | ctrl.buf = NULL; | |
1ecaa0a0 | 509 | } |
62e76326 | 510 | |
968d691c | 511 | if (data.readBuf) { |
512 | if (!data.readBuf->isNull()) | |
9e008dda | 513 | data.readBuf->clean(); |
253caccb | 514 | |
968d691c | 515 | delete data.readBuf; |
516 | } | |
62e76326 | 517 | |
6f0aab86 | 518 | if (pathcomps) |
519 | wordlistDestroy(&pathcomps); | |
62e76326 | 520 | |
6f0aab86 | 521 | if (ctrl.message) |
522 | wordlistDestroy(&ctrl.message); | |
62e76326 | 523 | |
0477a072 | 524 | cwd_message.clean(); |
62e76326 | 525 | |
6f0aab86 | 526 | safe_free(ctrl.last_reply); |
62e76326 | 527 | |
6f0aab86 | 528 | safe_free(ctrl.last_command); |
62e76326 | 529 | |
6f0aab86 | 530 | safe_free(old_request); |
62e76326 | 531 | |
6f0aab86 | 532 | safe_free(old_reply); |
62e76326 | 533 | |
6f0aab86 | 534 | safe_free(old_filepath); |
62e76326 | 535 | |
30abd221 | 536 | title_url.clean(); |
62e76326 | 537 | |
30abd221 | 538 | base_href.clean(); |
62e76326 | 539 | |
6f0aab86 | 540 | safe_free(filepath); |
62e76326 | 541 | |
6d0440a2 | 542 | safe_free(dirpath); |
543 | ||
6f0aab86 | 544 | safe_free(data.host); |
5f8252d2 | 545 | |
b6b6f466 | 546 | fwd = NULL; // refcounted |
ba718c8f | 547 | } |
548 | ||
f381d699 AJ |
549 | /** |
550 | * Parse a possible login username:password pair. | |
cd0b63ba | 551 | * Produces filled member variables user, password, password_url if anything found. |
f381d699 | 552 | */ |
6f0aab86 | 553 | void |
cd0b63ba | 554 | FtpStateData::loginParser(const char *login, int escaped) |
090089c4 | 555 | { |
ccb355fa AJ |
556 | const char *u = NULL; // end of the username sub-string |
557 | int len; // length of the current sub-string to handle. | |
558 | ||
559 | int total_len = strlen(login); | |
d6a8572b | 560 | |
f381d699 AJ |
561 | debugs(9, 4, HERE << ": login='" << login << "', escaped=" << escaped); |
562 | debugs(9, 9, HERE << ": IN : login='" << login << "', escaped=" << escaped << ", user=" << user << ", password=" << password); | |
62e76326 | 563 | |
cd0b63ba | 564 | if ((u = strchr(login, ':'))) { |
62e76326 | 565 | |
f381d699 | 566 | /* if there was a username part */ |
cd0b63ba AJ |
567 | if (u > login) { |
568 | len = u - login; | |
ccb355fa | 569 | ++u; // jump off the delimiter. |
cd0b63ba | 570 | if (len > MAX_URL) |
ccb355fa AJ |
571 | len = MAX_URL-1; |
572 | xstrncpy(user, login, len +1); | |
573 | debugs(9, 9, HERE << ": found user='" << user << "'(" << len <<"), escaped=" << escaped); | |
f381d699 AJ |
574 | if (escaped) |
575 | rfc1738_unescape(user); | |
ccb355fa | 576 | debugs(9, 9, HERE << ": found user='" << user << "'(" << len <<") unescaped."); |
c4a2d5e1 | 577 | } |
62e76326 | 578 | |
f381d699 | 579 | /* if there was a password part */ |
ccb355fa AJ |
580 | len = login + total_len - u; |
581 | if ( len > 0) { | |
582 | if (len > MAX_URL) | |
583 | len = MAX_URL -1; | |
584 | xstrncpy(password, u, len +1); | |
585 | debugs(9, 9, HERE << ": found password='" << password << "'(" << len <<"), escaped=" << escaped); | |
f381d699 AJ |
586 | if (escaped) { |
587 | rfc1738_unescape(password); | |
588 | password_url = 1; | |
589 | } | |
ccb355fa | 590 | debugs(9, 9, HERE << ": found password='" << password << "'(" << len <<") unescaped."); |
f381d699 | 591 | } |
04f7fd38 | 592 | } else if (login[0]) { |
f381d699 | 593 | /* no password, just username */ |
ccb355fa AJ |
594 | if (total_len > MAX_URL) |
595 | total_len = MAX_URL -1; | |
596 | xstrncpy(user, login, total_len +1); | |
597 | debugs(9, 9, HERE << ": found user='" << user << "'(" << total_len <<"), escaped=" << escaped); | |
f381d699 AJ |
598 | if (escaped) |
599 | rfc1738_unescape(user); | |
ccb355fa | 600 | debugs(9, 9, HERE << ": found user='" << user << "'(" << total_len <<") unescaped."); |
f381d699 | 601 | } |
62e76326 | 602 | |
f381d699 | 603 | debugs(9, 9, HERE << ": OUT: login='" << login << "', escaped=" << escaped << ", user=" << user << ", password=" << password); |
090089c4 | 604 | } |
605 | ||
6f0aab86 | 606 | void |
dc56a9b1 | 607 | FtpStateData::ftpTimeout(const CommTimeoutCbParams &io) |
090089c4 | 608 | { |
dc56a9b1 | 609 | debugs(9, 4, "ftpTimeout: FD " << io.fd << ": '" << entry->url() << "'" ); |
62e76326 | 610 | |
dc56a9b1 | 611 | if (SENT_PASV == state && io.fd == data.fd) { |
62e76326 | 612 | /* stupid ftp.netscape.com */ |
dc56a9b1 | 613 | fwd->dontRetry(false); |
614 | fwd->ftpPasvFailed(true); | |
615 | debugs(9, DBG_IMPORTANT, "ftpTimeout: timeout in SENT_PASV state" ); | |
7e3ce7b9 | 616 | } |
62e76326 | 617 | |
dc56a9b1 | 618 | failed(ERR_READ_TIMEOUT, 0); |
6f0aab86 | 619 | /* failed() closes ctrl.fd and frees ftpState */ |
090089c4 | 620 | } |
621 | ||
0477a072 | 622 | #if DEAD_CODE // obsoleted by ERR_DIR_LISTING |
6f0aab86 | 623 | void |
624 | FtpStateData::listingFinish() | |
3fdadc70 | 625 | { |
0477a072 | 626 | // TODO: figure out what this means and how to show it ... |
62e76326 | 627 | |
6f0aab86 | 628 | if (flags.listformat_unknown && !flags.tried_nlst) { |
0477a072 | 629 | printfReplyBody("<a href=\"%s/;type=d\">[As plain directory]</a>\n", |
253caccb | 630 | flags.dir_slash ? rfc1738_escape_part(old_filepath) : "."); |
6f0aab86 | 631 | } else if (typecode == 'D') { |
632 | const char *path = flags.dir_slash ? filepath : "."; | |
0477a072 | 633 | printfReplyBody("<a href=\"%s/\">[As extended directory]</a>\n", rfc1738_escape_part(path)); |
dbfed404 | 634 | } |
3fdadc70 | 635 | } |
0477a072 | 636 | #endif /* DEAD_CODE */ |
3fdadc70 | 637 | |
63be0a78 | 638 | /// \ingroup ServerProtocolFTPInternal |
9e008dda AJ |
639 | static const char *Month[] = { |
640 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
641 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
642 | }; | |
3fdadc70 | 643 | |
63be0a78 | 644 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 645 | static int |
646 | is_month(const char *buf) | |
647 | { | |
648 | int i; | |
62e76326 | 649 | |
3fdadc70 | 650 | for (i = 0; i < 12; i++) |
62e76326 | 651 | if (!strcasecmp(buf, Month[i])) |
652 | return 1; | |
653 | ||
3fdadc70 | 654 | return 0; |
655 | } | |
656 | ||
63be0a78 | 657 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 658 | static void |
659 | ftpListPartsFree(ftpListParts ** parts) | |
660 | { | |
661 | safe_free((*parts)->date); | |
662 | safe_free((*parts)->name); | |
663 | safe_free((*parts)->showname); | |
664 | safe_free((*parts)->link); | |
665 | safe_free(*parts); | |
666 | } | |
667 | ||
63be0a78 | 668 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 669 | #define MAX_TOKENS 64 |
670 | ||
63be0a78 | 671 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 672 | static ftpListParts * |
e55f0142 | 673 | ftpListParseParts(const char *buf, struct _ftp_flags flags) |
3fdadc70 | 674 | { |
675 | ftpListParts *p = NULL; | |
676 | char *t = NULL; | |
677 | const char *ct = NULL; | |
678 | char *tokens[MAX_TOKENS]; | |
679 | int i; | |
680 | int n_tokens; | |
748f6a15 | 681 | static char tbuf[128]; |
3fdadc70 | 682 | char *xbuf = NULL; |
748f6a15 | 683 | static int scan_ftp_initialized = 0; |
684 | static regex_t scan_ftp_integer; | |
685 | static regex_t scan_ftp_time; | |
686 | static regex_t scan_ftp_dostime; | |
687 | static regex_t scan_ftp_dosdate; | |
688 | ||
9e008dda | 689 | if (!scan_ftp_initialized) { |
62e76326 | 690 | scan_ftp_initialized = 1; |
691 | regcomp(&scan_ftp_integer, "^[0123456789]+$", REG_EXTENDED | REG_NOSUB); | |
692 | regcomp(&scan_ftp_time, "^[0123456789:]+$", REG_EXTENDED | REG_NOSUB); | |
693 | regcomp(&scan_ftp_dosdate, "^[0123456789]+-[0123456789]+-[0123456789]+$", REG_EXTENDED | REG_NOSUB); | |
694 | regcomp(&scan_ftp_dostime, "^[0123456789]+:[0123456789]+[AP]M$", REG_EXTENDED | REG_NOSUB | REG_ICASE); | |
748f6a15 | 695 | } |
62e76326 | 696 | |
3fdadc70 | 697 | if (buf == NULL) |
62e76326 | 698 | return NULL; |
699 | ||
3fdadc70 | 700 | if (*buf == '\0') |
62e76326 | 701 | return NULL; |
702 | ||
e6ccf245 | 703 | p = (ftpListParts *)xcalloc(1, sizeof(ftpListParts)); |
62e76326 | 704 | |
3fdadc70 | 705 | n_tokens = 0; |
62e76326 | 706 | |
748f6a15 | 707 | memset(tokens, 0, sizeof(tokens)); |
62e76326 | 708 | |
3fdadc70 | 709 | xbuf = xstrdup(buf); |
62e76326 | 710 | |
9e008dda | 711 | if (flags.tried_nlst) { |
62e76326 | 712 | /* Machine readable format, one name per line */ |
713 | p->name = xbuf; | |
714 | p->type = '\0'; | |
715 | return p; | |
dbfed404 | 716 | } |
62e76326 | 717 | |
3fdadc70 | 718 | for (t = strtok(xbuf, w_space); t && n_tokens < MAX_TOKENS; t = strtok(NULL, w_space)) |
62e76326 | 719 | tokens[n_tokens++] = xstrdup(t); |
720 | ||
3fdadc70 | 721 | xfree(xbuf); |
62e76326 | 722 | |
3fdadc70 | 723 | /* locate the Month field */ |
9e008dda | 724 | for (i = 3; i < n_tokens - 2; i++) { |
62e76326 | 725 | char *size = tokens[i - 1]; |
726 | char *month = tokens[i]; | |
727 | char *day = tokens[i + 1]; | |
728 | char *year = tokens[i + 2]; | |
729 | ||
730 | if (!is_month(month)) | |
731 | continue; | |
732 | ||
733 | if (regexec(&scan_ftp_integer, size, 0, NULL, 0) != 0) | |
734 | continue; | |
735 | ||
736 | if (regexec(&scan_ftp_integer, day, 0, NULL, 0) != 0) | |
737 | continue; | |
738 | ||
7978e6ca | 739 | if (regexec(&scan_ftp_time, year, 0, NULL, 0) != 0) /* Yr | hh:mm */ |
62e76326 | 740 | continue; |
741 | ||
742 | snprintf(tbuf, 128, "%s %2s %5s", | |
743 | month, day, year); | |
744 | ||
745 | if (!strstr(buf, tbuf)) | |
746 | snprintf(tbuf, 128, "%s %2s %-5s", | |
747 | month, day, year); | |
748 | ||
65bfb9ab | 749 | char const *copyFrom = NULL; |
750 | ||
751 | if ((copyFrom = strstr(buf, tbuf))) { | |
62e76326 | 752 | p->type = *tokens[0]; |
47f6e231 | 753 | p->size = strtoll(size, NULL, 10); |
62e76326 | 754 | p->date = xstrdup(tbuf); |
755 | ||
756 | if (flags.skip_whitespace) { | |
65bfb9ab | 757 | copyFrom += strlen(tbuf); |
62e76326 | 758 | |
65bfb9ab | 759 | while (strchr(w_space, *copyFrom)) |
760 | copyFrom++; | |
62e76326 | 761 | } else { |
762 | /* XXX assumes a single space between date and filename | |
763 | * suggested by: Nathan.Bailey@cc.monash.edu.au and | |
764 | * Mike Battersby <mike@starbug.bofh.asn.au> */ | |
65bfb9ab | 765 | copyFrom += strlen(tbuf) + 1; |
62e76326 | 766 | } |
767 | ||
65bfb9ab | 768 | p->name = xstrdup(copyFrom); |
62e76326 | 769 | |
38ebaefc | 770 | if (p->type == 'l' && (t = strstr(p->name, " -> "))) { |
62e76326 | 771 | *t = '\0'; |
772 | p->link = xstrdup(t + 4); | |
773 | } | |
774 | ||
775 | goto found; | |
776 | } | |
777 | ||
778 | break; | |
3fdadc70 | 779 | } |
62e76326 | 780 | |
748f6a15 | 781 | /* try it as a DOS listing, 04-05-70 09:33PM ... */ |
782 | if (n_tokens > 3 && | |
62e76326 | 783 | regexec(&scan_ftp_dosdate, tokens[0], 0, NULL, 0) == 0 && |
9e008dda | 784 | regexec(&scan_ftp_dostime, tokens[1], 0, NULL, 0) == 0) { |
62e76326 | 785 | if (!strcasecmp(tokens[2], "<dir>")) { |
786 | p->type = 'd'; | |
787 | } else { | |
788 | p->type = '-'; | |
47f6e231 | 789 | p->size = strtoll(tokens[2], NULL, 10); |
62e76326 | 790 | } |
791 | ||
792 | snprintf(tbuf, 128, "%s %s", tokens[0], tokens[1]); | |
793 | p->date = xstrdup(tbuf); | |
794 | ||
795 | if (p->type == 'd') { | |
796 | /* Directory.. name begins with first printable after <dir> */ | |
797 | ct = strstr(buf, tokens[2]); | |
798 | ct += strlen(tokens[2]); | |
799 | ||
800 | while (xisspace(*ct)) | |
801 | ct++; | |
802 | ||
803 | if (!*ct) | |
804 | ct = NULL; | |
805 | } else { | |
806 | /* A file. Name begins after size, with a space in between */ | |
807 | snprintf(tbuf, 128, " %s %s", tokens[2], tokens[3]); | |
808 | ct = strstr(buf, tbuf); | |
809 | ||
810 | if (ct) { | |
811 | ct += strlen(tokens[2]) + 2; | |
812 | } | |
813 | } | |
814 | ||
815 | p->name = xstrdup(ct ? ct : tokens[3]); | |
816 | goto found; | |
3fdadc70 | 817 | } |
62e76326 | 818 | |
3fdadc70 | 819 | /* Try EPLF format; carson@lehman.com */ |
9e008dda | 820 | if (buf[0] == '+') { |
62e76326 | 821 | ct = buf + 1; |
822 | p->type = 0; | |
823 | ||
824 | while (ct && *ct) { | |
d5f8d05f | 825 | time_t tm; |
3f9d0b2d | 826 | int l = strcspn(ct, ","); |
62e76326 | 827 | char *tmp; |
828 | ||
829 | if (l < 1) | |
830 | goto blank; | |
831 | ||
832 | switch (*ct) { | |
833 | ||
834 | case '\t': | |
835 | p->name = xstrndup(ct + 1, l + 1); | |
836 | break; | |
837 | ||
838 | case 's': | |
839 | p->size = atoi(ct + 1); | |
840 | break; | |
841 | ||
842 | case 'm': | |
d5f8d05f | 843 | tm = (time_t) strtol(ct + 1, &tmp, 0); |
62e76326 | 844 | |
3f9d0b2d | 845 | if (tmp != ct + 1) |
62e76326 | 846 | break; /* not a valid integer */ |
847 | ||
d5f8d05f | 848 | p->date = xstrdup(ctime(&tm)); |
62e76326 | 849 | |
850 | *(strstr(p->date, "\n")) = '\0'; | |
851 | ||
852 | break; | |
853 | ||
854 | case '/': | |
855 | p->type = 'd'; | |
856 | ||
857 | break; | |
858 | ||
859 | case 'r': | |
860 | p->type = '-'; | |
861 | ||
862 | break; | |
863 | ||
864 | case 'i': | |
865 | break; | |
866 | ||
867 | default: | |
868 | break; | |
869 | } | |
870 | ||
871 | blank: | |
872 | ct = strstr(ct, ","); | |
873 | ||
874 | if (ct) { | |
875 | ct++; | |
876 | } | |
877 | } | |
878 | ||
879 | if (p->type == 0) { | |
880 | p->type = '-'; | |
881 | } | |
882 | ||
883 | if (p->name) | |
884 | goto found; | |
885 | else | |
886 | safe_free(p->date); | |
887 | } | |
888 | ||
889 | found: | |
890 | ||
3fdadc70 | 891 | for (i = 0; i < n_tokens; i++) |
62e76326 | 892 | xfree(tokens[i]); |
893 | ||
748f6a15 | 894 | if (!p->name) |
62e76326 | 895 | ftpListPartsFree(&p); /* cleanup */ |
896 | ||
3fdadc70 | 897 | return p; |
898 | } | |
899 | ||
0477a072 | 900 | MemBuf * |
6f0aab86 | 901 | FtpStateData::htmlifyListEntry(const char *line) |
3fdadc70 | 902 | { |
0477a072 AJ |
903 | char icon[2048]; |
904 | char href[2048 + 40]; | |
905 | char text[ 2048]; | |
906 | char size[ 2048]; | |
907 | char chdir[ 2048 + 40]; | |
908 | char view[ 2048 + 40]; | |
909 | char download[ 2048 + 40]; | |
910 | char link[ 2048 + 40]; | |
911 | MemBuf *html; | |
912 | char prefix[2048]; | |
3fdadc70 | 913 | ftpListParts *parts; |
0477a072 | 914 | *icon = *href = *text = *size = *chdir = *view = *download = *link = '\0'; |
62e76326 | 915 | |
0477a072 | 916 | debugs(9, 7, HERE << " line ={" << line << "}"); |
62e76326 | 917 | |
0477a072 AJ |
918 | if (strlen(line) > 1024) { |
919 | html = new MemBuf(); | |
920 | html->init(); | |
921 | html->Printf("<tr><td colspan=\"5\">%s</td></tr>\n", line); | |
62e76326 | 922 | return html; |
3fdadc70 | 923 | } |
62e76326 | 924 | |
45289935 | 925 | if (flags.dir_slash && dirpath && typecode != 'D') |
926 | snprintf(prefix, 2048, "%s/", rfc1738_escape_part(dirpath)); | |
5627a633 | 927 | else |
928 | prefix[0] = '\0'; | |
929 | ||
6f0aab86 | 930 | if ((parts = ftpListParseParts(line, flags)) == NULL) { |
62e76326 | 931 | const char *p; |
0477a072 AJ |
932 | |
933 | html = new MemBuf(); | |
934 | html->init(); | |
935 | html->Printf("<tr class=\"entry\"><td colspan=\"5\">%s</td></tr>\n", line); | |
62e76326 | 936 | |
3d0ac046 | 937 | for (p = line; *p && xisspace(*p); p++); |
62e76326 | 938 | if (*p && !xisspace(*p)) |
6f0aab86 | 939 | flags.listformat_unknown = 1; |
62e76326 | 940 | |
941 | return html; | |
13e80e5b | 942 | } |
62e76326 | 943 | |
3fdadc70 | 944 | if (!strcmp(parts->name, ".") || !strcmp(parts->name, "..")) { |
62e76326 | 945 | ftpListPartsFree(&parts); |
0477a072 | 946 | return NULL; |
3fdadc70 | 947 | } |
62e76326 | 948 | |
3fdadc70 | 949 | parts->size += 1023; |
950 | parts->size >>= 10; | |
951 | parts->showname = xstrdup(parts->name); | |
62e76326 | 952 | |
2a1ca944 | 953 | /* {icon} {text} . . . {date}{size}{chdir}{view}{download}{link}\n */ |
9bc73deb | 954 | xstrncpy(href, rfc1738_escape_part(parts->name), 2048); |
62e76326 | 955 | |
2a1ca944 | 956 | xstrncpy(text, parts->showname, 2048); |
62e76326 | 957 | |
3fdadc70 | 958 | switch (parts->type) { |
62e76326 | 959 | |
3fdadc70 | 960 | case 'd': |
0477a072 | 961 | snprintf(icon, 2048, "<img border=\"0\" src=\"%s\" alt=\"%-6s\">", |
62e76326 | 962 | mimeGetIconURL("internal-dir"), |
963 | "[DIR]"); | |
964 | strcat(href, "/"); /* margin is allocated above */ | |
965 | break; | |
966 | ||
3fdadc70 | 967 | case 'l': |
0477a072 | 968 | snprintf(icon, 2048, "<img border=\"0\" src=\"%s\" alt=\"%-6s\">", |
62e76326 | 969 | mimeGetIconURL("internal-link"), |
970 | "[LINK]"); | |
971 | /* sometimes there is an 'l' flag, but no "->" link */ | |
972 | ||
973 | if (parts->link) { | |
974 | char *link2 = xstrdup(html_quote(rfc1738_escape(parts->link))); | |
0477a072 | 975 | snprintf(link, 2048, " -> <a href=\"%s%s\">%s</a>", |
5627a633 | 976 | *link2 != '/' ? prefix : "", link2, |
62e76326 | 977 | html_quote(parts->link)); |
978 | safe_free(link2); | |
979 | } | |
980 | ||
981 | break; | |
982 | ||
dbfed404 | 983 | case '\0': |
0477a072 | 984 | snprintf(icon, 2048, "<img border=\"0\" src=\"%s\" alt=\"%-6s\">", |
62e76326 | 985 | mimeGetIconURL(parts->name), |
986 | "[UNKNOWN]"); | |
0477a072 AJ |
987 | snprintf(chdir, 2048, "<a href=\"%s/;type=d\"><img border=\"0\" src=\"%s\" " |
988 | "alt=\"[DIR]\"></a>", | |
62e76326 | 989 | rfc1738_escape_part(parts->name), |
990 | mimeGetIconURL("internal-dir")); | |
991 | break; | |
992 | ||
3fdadc70 | 993 | case '-': |
62e76326 | 994 | |
3fdadc70 | 995 | default: |
0477a072 | 996 | snprintf(icon, 2048, "<img border=\"0\" src=\"%s\" alt=\"%-6s\">", |
62e76326 | 997 | mimeGetIconURL(parts->name), |
998 | "[FILE]"); | |
47f6e231 | 999 | snprintf(size, 2048, " %6"PRId64"k", parts->size); |
62e76326 | 1000 | break; |
3fdadc70 | 1001 | } |
62e76326 | 1002 | |
2a1ca944 | 1003 | if (parts->type != 'd') { |
62e76326 | 1004 | if (mimeGetViewOption(parts->name)) { |
0477a072 AJ |
1005 | snprintf(view, 2048, "<a href=\"%s%s;type=a\"><img border=\"0\" src=\"%s\" " |
1006 | "alt=\"[VIEW]\"></a>", | |
5627a633 | 1007 | prefix, href, mimeGetIconURL("internal-view")); |
62e76326 | 1008 | } |
1009 | ||
1010 | if (mimeGetDownloadOption(parts->name)) { | |
0477a072 AJ |
1011 | snprintf(download, 2048, "<a href=\"%s%s;type=i\"><img border=\"0\" src=\"%s\" " |
1012 | "alt=\"[DOWNLOAD]\"></a>", | |
5627a633 | 1013 | prefix, href, mimeGetIconURL("internal-download")); |
62e76326 | 1014 | } |
2a1ca944 | 1015 | } |
62e76326 | 1016 | |
0477a072 AJ |
1017 | /* construct the table row from parts. */ |
1018 | html = new MemBuf(); | |
1019 | html->init(); | |
1020 | html->Printf("<tr class=\"entry\">" | |
05320519 A |
1021 | "<td class=\"icon\"><a href=\"%s%s\">%s</a></td>" |
1022 | "<td class=\"filename\"><a href=\"%s%s\">%s</a></td>" | |
1023 | "<td class=\"date\">%s</td>" | |
1024 | "<td class=\"size\">%s</td>" | |
1025 | "<td class=\"actions\">%s%s%s%s</td>" | |
1026 | "</tr>\n", | |
1027 | prefix, href, icon, | |
1028 | prefix, href, html_quote(text), | |
1029 | parts->date, | |
1030 | size, | |
62e76326 | 1031 | chdir, view, download, link); |
62e76326 | 1032 | |
3fdadc70 | 1033 | ftpListPartsFree(&parts); |
3fdadc70 | 1034 | return html; |
1035 | } | |
1036 | ||
f1a83a7d | 1037 | void |
1038 | FtpStateData::parseListing() | |
3fdadc70 | 1039 | { |
253caccb | 1040 | char *buf = data.readBuf->content(); |
a313a9ba | 1041 | char *sbuf; /* NULL-terminated copy of termedBuf */ |
b5639035 | 1042 | char *end; |
1043 | char *line; | |
3fdadc70 | 1044 | char *s; |
0477a072 | 1045 | MemBuf *t; |
3fdadc70 | 1046 | size_t linelen; |
b5639035 | 1047 | size_t usable; |
253caccb | 1048 | size_t len = data.readBuf->contentSize(); |
5f8252d2 | 1049 | |
1050 | if (!len) { | |
0477a072 | 1051 | debugs(9, 3, HERE << "no content to parse for " << entry->url() ); |
5f8252d2 | 1052 | return; |
1053 | } | |
1054 | ||
7131112f | 1055 | /* |
1056 | * We need a NULL-terminated buffer for scanning, ick | |
1057 | */ | |
e6ccf245 | 1058 | sbuf = (char *)xmalloc(len + 1); |
7131112f | 1059 | xstrncpy(sbuf, buf, len + 1); |
1060 | end = sbuf + len - 1; | |
62e76326 | 1061 | |
7131112f | 1062 | while (*end != '\r' && *end != '\n' && end > sbuf) |
62e76326 | 1063 | end--; |
1064 | ||
7131112f | 1065 | usable = end - sbuf; |
62e76326 | 1066 | |
a689bd4e | 1067 | debugs(9, 3, HERE << "usable = " << usable); |
62e76326 | 1068 | |
b5639035 | 1069 | if (usable == 0) { |
0477a072 | 1070 | debugs(9, 3, HERE << "didn't find end for " << entry->url() ); |
62e76326 | 1071 | xfree(sbuf); |
1072 | return; | |
3fdadc70 | 1073 | } |
62e76326 | 1074 | |
a689bd4e | 1075 | debugs(9, 3, HERE << (unsigned long int)len << " bytes to play with"); |
bf8fe701 | 1076 | |
e6ccf245 | 1077 | line = (char *)memAllocate(MEM_4K_BUF); |
3fdadc70 | 1078 | end++; |
362be274 | 1079 | s = sbuf; |
1080 | s += strspn(s, crlf); | |
62e76326 | 1081 | |
362be274 | 1082 | for (; s < end; s += strcspn(s, crlf), s += strspn(s, crlf)) { |
a689bd4e | 1083 | debugs(9, 7, HERE << "s = {" << s << "}"); |
62e76326 | 1084 | linelen = strcspn(s, crlf) + 1; |
1085 | ||
1086 | if (linelen < 2) | |
1087 | break; | |
1088 | ||
1089 | if (linelen > 4096) | |
1090 | linelen = 4096; | |
1091 | ||
1092 | xstrncpy(line, s, linelen); | |
1093 | ||
a689bd4e | 1094 | debugs(9, 7, HERE << "{" << line << "}"); |
62e76326 | 1095 | |
1096 | if (!strncmp(line, "total", 5)) | |
1097 | continue; | |
1098 | ||
f1a83a7d | 1099 | t = htmlifyListEntry(line); |
62e76326 | 1100 | |
05320519 | 1101 | if ( t != NULL) { |
0477a072 AJ |
1102 | debugs(9, 7, HERE << "listing append: t = {" << t->contentSize() << ", '" << t->content() << "'}"); |
1103 | listing.append(t->content(), t->contentSize()); | |
1104 | //leak? delete t; | |
1105 | } | |
b5639035 | 1106 | } |
62e76326 | 1107 | |
0477a072 | 1108 | debugs(9, 7, HERE << "Done."); |
253caccb | 1109 | data.readBuf->consume(usable); |
db1cd23c | 1110 | memFree(line, MEM_4K_BUF); |
4a2635aa | 1111 | xfree(sbuf); |
3fdadc70 | 1112 | } |
090089c4 | 1113 | |
5f8252d2 | 1114 | int |
9e008dda AJ |
1115 | FtpStateData::dataDescriptor() const |
1116 | { | |
5f8252d2 | 1117 | return data.fd; |
1118 | } | |
1119 | ||
f1a83a7d | 1120 | void |
1121 | FtpStateData::dataComplete() | |
79a15e0a | 1122 | { |
a689bd4e | 1123 | debugs(9, 3,HERE); |
62e76326 | 1124 | |
a689bd4e | 1125 | /* Connection closed; transfer done. */ |
94b88585 AR |
1126 | |
1127 | /// Close data channel, if any, to conserve resources while we wait. | |
1128 | data.close(); | |
62e76326 | 1129 | |
79a15e0a | 1130 | /* expect the "transfer complete" message on the control socket */ |
4e849646 | 1131 | /* |
1132 | * DPW 2007-04-23 | |
1133 | * Previously, this was the only place where we set the | |
1134 | * 'buffered_ok' flag when calling scheduleReadControlReply(). | |
1135 | * It caused some problems if the FTP server returns an unexpected | |
1136 | * status code after the data command. FtpStateData was being | |
1137 | * deleted in the middle of dataRead(). | |
1138 | */ | |
1139 | scheduleReadControlReply(0); | |
79a15e0a | 1140 | } |
1141 | ||
253caccb | 1142 | void |
5f8252d2 | 1143 | FtpStateData::maybeReadVirginBody() |
253caccb | 1144 | { |
1145 | if (data.fd < 0) | |
1146 | return; | |
1147 | ||
1148 | if (data.read_pending) | |
1149 | return; | |
1150 | ||
52edecde | 1151 | const int read_sz = replyBodySpace(*data.readBuf, 0); |
253caccb | 1152 | |
1153 | debugs(11,9, HERE << "FTP may read up to " << read_sz << " bytes"); | |
1154 | ||
1155 | if (read_sz < 2) // see http.cc | |
1156 | return; | |
1157 | ||
1158 | data.read_pending = true; | |
1159 | ||
dc56a9b1 | 1160 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
4299f876 AR |
1161 | AsyncCall::Pointer timeoutCall = JobCallback(9, 5, |
1162 | TimeoutDialer, this, FtpStateData::ftpTimeout); | |
dc56a9b1 | 1163 | commSetTimeout(data.fd, Config.Timeout.read, timeoutCall); |
928720a6 | 1164 | |
253caccb | 1165 | debugs(9,5,HERE << "queueing read on FD " << data.fd); |
1166 | ||
dc56a9b1 | 1167 | typedef CommCbMemFunT<FtpStateData, CommIoCbParams> Dialer; |
1168 | entry->delayAwareRead(data.fd, data.readBuf->space(), read_sz, | |
4299f876 | 1169 | JobCallback(9, 5, 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; | |
4299f876 AR |
1218 | AsyncCall::Pointer timeoutCall = JobCallback(9, 5, |
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 | |
a0297974 AR |
1524 | if (!canSend(ctrl.fd)) { |
1525 | debugs(9, 2, HERE << "cannot send to closing ctrl FD " << ctrl.fd); | |
1526 | // TODO: assert(ctrl.closer != NULL); | |
1527 | return; | |
1528 | } | |
1529 | ||
dc56a9b1 | 1530 | typedef CommCbMemFunT<FtpStateData, CommIoCbParams> Dialer; |
4299f876 AR |
1531 | AsyncCall::Pointer call = JobCallback(9, 5, |
1532 | Dialer, this, FtpStateData::ftpWriteCommandCallback); | |
2f47fadf | 1533 | comm_write(ctrl.fd, |
1534 | ctrl.last_command, | |
1535 | strlen(ctrl.last_command), | |
9e008dda | 1536 | call); |
dad0fe12 | 1537 | |
2f47fadf | 1538 | scheduleReadControlReply(0); |
3fdadc70 | 1539 | } |
1540 | ||
6f0aab86 | 1541 | void |
dc56a9b1 | 1542 | FtpStateData::ftpWriteCommandCallback(const CommIoCbParams &io) |
3fdadc70 | 1543 | { |
6800b5b5 | 1544 | |
dc56a9b1 | 1545 | debugs(9, 5, "ftpWriteCommandCallback: wrote " << io.size << " bytes"); |
62e76326 | 1546 | |
dc56a9b1 | 1547 | if (io.size > 0) { |
1548 | fd_bytes(io.fd, io.size, FD_WRITE); | |
1549 | kb_incr(&statCounter.server.all.kbytes_out, io.size); | |
1550 | kb_incr(&statCounter.server.ftp.kbytes_out, io.size); | |
ee1679df | 1551 | } |
62e76326 | 1552 | |
dc56a9b1 | 1553 | if (io.flag == COMM_ERR_CLOSING) |
62e76326 | 1554 | return; |
1555 | ||
dc56a9b1 | 1556 | if (io.flag) { |
1557 | debugs(9, DBG_IMPORTANT, "ftpWriteCommandCallback: FD " << io.fd << ": " << xstrerr(io.xerrno)); | |
1558 | failed(ERR_WRITE_ERROR, io.xerrno); | |
6f0aab86 | 1559 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 1560 | return; |
3fdadc70 | 1561 | } |
1562 | } | |
1563 | ||
6f0aab86 | 1564 | wordlist * |
47f6e231 | 1565 | FtpStateData::ftpParseControlReply(char *buf, size_t len, int *codep, size_t *used) |
3fdadc70 | 1566 | { |
1567 | char *s; | |
4f310655 | 1568 | char *sbuf; |
1569 | char *end; | |
1570 | int usable; | |
3fdadc70 | 1571 | int complete = 0; |
51eeadc6 | 1572 | wordlist *head = NULL; |
3fdadc70 | 1573 | wordlist *list; |
1574 | wordlist **tail = &head; | |
47f6e231 | 1575 | size_t offset; |
3fdadc70 | 1576 | size_t linelen; |
b5639035 | 1577 | int code = -1; |
a689bd4e | 1578 | debugs(9, 3, HERE); |
4f310655 | 1579 | /* |
1580 | * We need a NULL-terminated buffer for scanning, ick | |
1581 | */ | |
e6ccf245 | 1582 | sbuf = (char *)xmalloc(len + 1); |
4f310655 | 1583 | xstrncpy(sbuf, buf, len + 1); |
1584 | end = sbuf + len - 1; | |
62e76326 | 1585 | |
4f310655 | 1586 | while (*end != '\r' && *end != '\n' && end > sbuf) |
62e76326 | 1587 | end--; |
1588 | ||
4f310655 | 1589 | usable = end - sbuf; |
62e76326 | 1590 | |
a689bd4e | 1591 | debugs(9, 3, HERE << "usable = " << usable); |
62e76326 | 1592 | |
4f310655 | 1593 | if (usable == 0) { |
a689bd4e | 1594 | debugs(9, 3, HERE << "didn't find end of line"); |
62e76326 | 1595 | safe_free(sbuf); |
1596 | return NULL; | |
4f310655 | 1597 | } |
62e76326 | 1598 | |
a689bd4e | 1599 | debugs(9, 3, HERE << len << " bytes to play with"); |
4f310655 | 1600 | end++; |
1601 | s = sbuf; | |
1602 | s += strspn(s, crlf); | |
62e76326 | 1603 | |
4f310655 | 1604 | for (; s < end; s += strcspn(s, crlf), s += strspn(s, crlf)) { |
62e76326 | 1605 | if (complete) |
1606 | break; | |
1607 | ||
a689bd4e | 1608 | debugs(9, 5, HERE << "s = {" << s << "}"); |
62e76326 | 1609 | |
1610 | linelen = strcspn(s, crlf) + 1; | |
1611 | ||
1612 | if (linelen < 2) | |
1613 | break; | |
1614 | ||
1615 | if (linelen > 3) | |
1616 | complete = (*s >= '0' && *s <= '9' && *(s + 3) == ' '); | |
1617 | ||
1618 | if (complete) | |
1619 | code = atoi(s); | |
1620 | ||
1621 | offset = 0; | |
1622 | ||
1623 | if (linelen > 3) | |
1624 | if (*s >= '0' && *s <= '9' && (*(s + 3) == '-' || *(s + 3) == ' ')) | |
1625 | offset = 4; | |
1626 | ||
d295d770 | 1627 | list = new wordlist(); |
62e76326 | 1628 | |
1629 | list->key = (char *)xmalloc(linelen - offset); | |
1630 | ||
1631 | xstrncpy(list->key, s + offset, linelen - offset); | |
1632 | ||
a689bd4e | 1633 | /* trace the FTP communication chat at level 2 */ |
1634 | debugs(9, 2, "ftp>> " << code << " " << list->key); | |
62e76326 | 1635 | |
1636 | *tail = list; | |
1637 | ||
1638 | tail = &list->next; | |
3fdadc70 | 1639 | } |
62e76326 | 1640 | |
47f6e231 | 1641 | *used = (size_t) (s - sbuf); |
1c34d125 | 1642 | safe_free(sbuf); |
62e76326 | 1643 | |
3fdadc70 | 1644 | if (!complete) |
62e76326 | 1645 | wordlistDestroy(&head); |
1646 | ||
3fdadc70 | 1647 | if (codep) |
62e76326 | 1648 | *codep = code; |
1649 | ||
3fdadc70 | 1650 | return head; |
1651 | } | |
1652 | ||
63be0a78 | 1653 | /** |
4e849646 | 1654 | * DPW 2007-04-23 |
1655 | * Looks like there are no longer anymore callers that set | |
1656 | * buffered_ok=1. Perhaps it can be removed at some point. | |
1657 | */ | |
6f0aab86 | 1658 | void |
1659 | FtpStateData::scheduleReadControlReply(int buffered_ok) | |
4f310655 | 1660 | { |
a689bd4e | 1661 | debugs(9, 3, HERE << "FD " << ctrl.fd); |
62e76326 | 1662 | |
6f0aab86 | 1663 | if (buffered_ok && ctrl.offset > 0) { |
62e76326 | 1664 | /* We've already read some reply data */ |
6f0aab86 | 1665 | handleControlReply(); |
4f310655 | 1666 | } else { |
62e76326 | 1667 | /* XXX What about Config.Timeout.read? */ |
9e008dda | 1668 | typedef CommCbMemFunT<FtpStateData, CommIoCbParams> Dialer; |
4299f876 AR |
1669 | AsyncCall::Pointer reader = JobCallback(9, 5, |
1670 | Dialer, this, FtpStateData::ftpReadControlReply); | |
9e008dda | 1671 | comm_read(ctrl.fd, ctrl.buf + ctrl.offset, ctrl.size - ctrl.offset, reader); |
62e76326 | 1672 | /* |
1673 | * Cancel the timeout on the Data socket (if any) and | |
1674 | * establish one on the control socket. | |
1675 | */ | |
1676 | ||
9e008dda AJ |
1677 | if (data.fd > -1) { |
1678 | AsyncCall::Pointer nullCall = NULL; | |
dc56a9b1 | 1679 | commSetTimeout(data.fd, -1, nullCall); |
9e008dda | 1680 | } |
dc56a9b1 | 1681 | |
9e008dda | 1682 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
4299f876 AR |
1683 | AsyncCall::Pointer timeoutCall = JobCallback(9, 5, |
1684 | TimeoutDialer, this, FtpStateData::ftpTimeout); | |
62e76326 | 1685 | |
dc56a9b1 | 1686 | commSetTimeout(ctrl.fd, Config.Timeout.read, timeoutCall); |
4f310655 | 1687 | } |
1688 | } | |
1689 | ||
dc56a9b1 | 1690 | void FtpStateData::ftpReadControlReply(const CommIoCbParams &io) |
3fdadc70 | 1691 | { |
dc56a9b1 | 1692 | debugs(9, 3, "ftpReadControlReply: FD " << io.fd << ", Read " << io.size << " bytes"); |
c4b7a5a9 | 1693 | |
dc56a9b1 | 1694 | if (io.size > 0) { |
1695 | kb_incr(&statCounter.server.all.kbytes_in, io.size); | |
1696 | kb_incr(&statCounter.server.ftp.kbytes_in, io.size); | |
3c7568c7 | 1697 | } |
62e76326 | 1698 | |
dc56a9b1 | 1699 | if (io.flag == COMM_ERR_CLOSING) |
c4b7a5a9 | 1700 | return; |
62e76326 | 1701 | |
9bc73deb | 1702 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
dc56a9b1 | 1703 | abortTransaction("entry aborted during control reply read"); |
62e76326 | 1704 | return; |
9bc73deb | 1705 | } |
62e76326 | 1706 | |
dc56a9b1 | 1707 | assert(ctrl.offset < ctrl.size); |
62e76326 | 1708 | |
dc56a9b1 | 1709 | if (io.flag == COMM_OK && io.size > 0) { |
1710 | fd_bytes(io.fd, io.size, FD_READ); | |
ee1679df | 1711 | } |
62e76326 | 1712 | |
dc56a9b1 | 1713 | if (io.flag != COMM_OK || io.size < 0) { |
9e008dda AJ |
1714 | debugs(50, ignoreErrno(io.xerrno) ? 3 : DBG_IMPORTANT, |
1715 | "ftpReadControlReply: read error: " << xstrerr(io.xerrno)); | |
62e76326 | 1716 | |
dc56a9b1 | 1717 | if (ignoreErrno(io.xerrno)) { |
1718 | scheduleReadControlReply(0); | |
62e76326 | 1719 | } else { |
dc56a9b1 | 1720 | failed(ERR_READ_ERROR, io.xerrno); |
6f0aab86 | 1721 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 1722 | return; |
1723 | } | |
1724 | ||
1725 | return; | |
3fdadc70 | 1726 | } |
62e76326 | 1727 | |
dc56a9b1 | 1728 | if (io.size == 0) { |
62e76326 | 1729 | if (entry->store_status == STORE_PENDING) { |
dc56a9b1 | 1730 | failed(ERR_FTP_FAILURE, 0); |
6f0aab86 | 1731 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 1732 | return; |
1733 | } | |
1734 | ||
9e008dda | 1735 | /* XXX this may end up having to be serverComplete() .. */ |
dc56a9b1 | 1736 | abortTransaction("zero control reply read"); |
62e76326 | 1737 | return; |
3fdadc70 | 1738 | } |
62e76326 | 1739 | |
dc56a9b1 | 1740 | unsigned int len =io.size + ctrl.offset; |
1741 | ctrl.offset = len; | |
1742 | assert(len <= ctrl.size); | |
1743 | handleControlReply(); | |
4f310655 | 1744 | } |
1745 | ||
6f0aab86 | 1746 | void |
1747 | FtpStateData::handleControlReply() | |
4f310655 | 1748 | { |
ec603b25 | 1749 | wordlist **W; |
47f6e231 | 1750 | size_t bytes_used = 0; |
6f0aab86 | 1751 | wordlistDestroy(&ctrl.message); |
1752 | ctrl.message = ftpParseControlReply(ctrl.buf, | |
1753 | ctrl.offset, &ctrl.replycode, &bytes_used); | |
62e76326 | 1754 | |
6f0aab86 | 1755 | if (ctrl.message == NULL) { |
62e76326 | 1756 | /* didn't get complete reply yet */ |
1757 | ||
47f6e231 | 1758 | if (ctrl.offset == ctrl.size) { |
6f0aab86 | 1759 | ctrl.buf = (char *)memReallocBuf(ctrl.buf, ctrl.size << 1, &ctrl.size); |
62e76326 | 1760 | } |
1761 | ||
6f0aab86 | 1762 | scheduleReadControlReply(0); |
62e76326 | 1763 | return; |
6f0aab86 | 1764 | } else if (ctrl.offset == bytes_used) { |
62e76326 | 1765 | /* used it all up */ |
6f0aab86 | 1766 | ctrl.offset = 0; |
4f310655 | 1767 | } else { |
62e76326 | 1768 | /* Got some data past the complete reply */ |
6f0aab86 | 1769 | assert(bytes_used < ctrl.offset); |
1770 | ctrl.offset -= bytes_used; | |
1771 | xmemmove(ctrl.buf, ctrl.buf + bytes_used, | |
1772 | ctrl.offset); | |
3fdadc70 | 1773 | } |
62e76326 | 1774 | |
858783c9 | 1775 | /* Move the last line of the reply message to ctrl.last_reply */ |
3d0ac046 | 1776 | for (W = &ctrl.message; (*W)->next; W = &(*W)->next); |
6f0aab86 | 1777 | safe_free(ctrl.last_reply); |
62e76326 | 1778 | |
6f0aab86 | 1779 | ctrl.last_reply = xstrdup((*W)->key); |
62e76326 | 1780 | |
858783c9 | 1781 | wordlistDestroy(W); |
62e76326 | 1782 | |
858783c9 | 1783 | /* Copy the rest of the message to cwd_message to be printed in |
1784 | * error messages | |
1785 | */ | |
0477a072 AJ |
1786 | if (ctrl.message) { |
1787 | for (wordlist *w = ctrl.message; w; w = w->next) { | |
1788 | cwd_message.append('\n'); | |
1789 | cwd_message.append(w->key); | |
1790 | } | |
1791 | } | |
62e76326 | 1792 | |
a689bd4e | 1793 | debugs(9, 3, HERE << "state=" << state << ", code=" << ctrl.replycode); |
62e76326 | 1794 | |
6f0aab86 | 1795 | FTP_SM_FUNCS[state] (this); |
234967c9 | 1796 | } |
1797 | ||
3fdadc70 | 1798 | /* ====================================================================== */ |
1799 | ||
63be0a78 | 1800 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 1801 | static void |
1802 | ftpReadWelcome(FtpStateData * ftpState) | |
1803 | { | |
1804 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 1805 | debugs(9, 3, HERE); |
62e76326 | 1806 | |
e55f0142 | 1807 | if (ftpState->flags.pasv_only) |
62e76326 | 1808 | ftpState->login_att++; |
1809 | ||
9bc73deb | 1810 | /* Dont retry if the FTP server accepted the connection */ |
b6b6f466 | 1811 | ftpState->fwd->dontRetry(true); |
62e76326 | 1812 | |
3fdadc70 | 1813 | if (code == 220) { |
62e76326 | 1814 | if (ftpState->ctrl.message) { |
1815 | if (strstr(ftpState->ctrl.message->key, "NetWare")) | |
1816 | ftpState->flags.skip_whitespace = 1; | |
1817 | } | |
1818 | ||
1819 | ftpSendUser(ftpState); | |
cdc33f35 | 1820 | } else if (code == 120) { |
62e76326 | 1821 | if (NULL != ftpState->ctrl.message) |
a689bd4e | 1822 | debugs(9, DBG_IMPORTANT, "FTP server is busy: " << ftpState->ctrl.message->key); |
62e76326 | 1823 | |
1824 | return; | |
3fdadc70 | 1825 | } else { |
62e76326 | 1826 | ftpFail(ftpState); |
3fdadc70 | 1827 | } |
1828 | } | |
1829 | ||
f381d699 AJ |
1830 | /** |
1831 | * Translate FTP login failure into HTTP error | |
1832 | * this is an attmpt to get the 407 message to show up outside Squid. | |
1833 | * its NOT a general failure. But a correct FTP response type. | |
1834 | */ | |
1835 | void | |
1836 | FtpStateData::loginFailed() | |
1837 | { | |
1838 | ErrorState *err = NULL; | |
1839 | const char *command, *reply; | |
1840 | ||
1841 | if (state == SENT_USER || state == SENT_PASS) { | |
1842 | if (ctrl.replycode > 500) { | |
1843 | if (password_url) | |
1844 | err = errorCon(ERR_FTP_FORBIDDEN, HTTP_FORBIDDEN, fwd->request); | |
1845 | else | |
1846 | err = errorCon(ERR_FTP_FORBIDDEN, HTTP_UNAUTHORIZED, fwd->request); | |
1847 | } else if (ctrl.replycode == 421) { | |
1848 | err = errorCon(ERR_FTP_UNAVAILABLE, HTTP_SERVICE_UNAVAILABLE, fwd->request); | |
1849 | } | |
1850 | } else { | |
1851 | ftpFail(this); | |
1852 | return; | |
1853 | } | |
1854 | ||
1855 | err->ftp.server_msg = ctrl.message; | |
1856 | ||
1857 | ctrl.message = NULL; | |
1858 | ||
1859 | if (old_request) | |
1860 | command = old_request; | |
1861 | else | |
1862 | command = ctrl.last_command; | |
1863 | ||
1864 | if (command && strncmp(command, "PASS", 4) == 0) | |
1865 | command = "PASS <yourpassword>"; | |
1866 | ||
1867 | if (old_reply) | |
1868 | reply = old_reply; | |
1869 | else | |
1870 | reply = ctrl.last_reply; | |
1871 | ||
1872 | if (command) | |
1873 | err->ftp.request = xstrdup(command); | |
1874 | ||
1875 | if (reply) | |
1876 | err->ftp.reply = xstrdup(reply); | |
1877 | ||
1878 | ||
1879 | HttpReply *newrep = err->BuildHttpReply(); | |
1880 | errorStateFree(err); | |
1881 | /* add Authenticate header */ | |
1882 | newrep->header.putAuth("Basic", ftpRealm()); | |
1883 | ||
1884 | // add it to the store entry for response.... | |
1885 | entry->replaceHttpReply(newrep); | |
1886 | serverComplete(); | |
1887 | } | |
1888 | ||
1889 | const char * | |
1890 | FtpStateData::ftpRealm() | |
1891 | { | |
1892 | static char realm[8192]; | |
1893 | ||
1894 | /* This request is not fully authenticated */ | |
1895 | if (!request) { | |
1896 | snprintf(realm, 8192, "FTP %s unknown", user); | |
1897 | } else if (request->port == 21) { | |
1898 | snprintf(realm, 8192, "FTP %s %s", user, request->GetHost()); | |
1899 | } else { | |
1900 | snprintf(realm, 8192, "FTP %s %s port %d", user, request->GetHost(), request->port); | |
1901 | } | |
1902 | return realm; | |
1903 | } | |
1904 | ||
63be0a78 | 1905 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 1906 | static void |
1907 | ftpSendUser(FtpStateData * ftpState) | |
1908 | { | |
a11382aa | 1909 | /* check the server control channel is still available */ |
9e008dda | 1910 | if (!ftpState || !ftpState->haveControlChannel("ftpSendUser")) |
a11382aa | 1911 | return; |
1912 | ||
969c39b9 | 1913 | if (ftpState->proxy_host != NULL) |
62e76326 | 1914 | snprintf(cbuf, 1024, "USER %s@%s\r\n", |
1915 | ftpState->user, | |
cc192b50 | 1916 | ftpState->request->GetHost()); |
969c39b9 | 1917 | else |
62e76326 | 1918 | snprintf(cbuf, 1024, "USER %s\r\n", ftpState->user); |
1919 | ||
2f47fadf | 1920 | ftpState->writeCommand(cbuf); |
62e76326 | 1921 | |
969c39b9 | 1922 | ftpState->state = SENT_USER; |
1923 | } | |
1924 | ||
63be0a78 | 1925 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 1926 | static void |
1927 | ftpReadUser(FtpStateData * ftpState) | |
234967c9 | 1928 | { |
3fdadc70 | 1929 | int code = ftpState->ctrl.replycode; |
a689bd4e | 1930 | debugs(9, 3, HERE); |
62e76326 | 1931 | |
3fdadc70 | 1932 | if (code == 230) { |
62e76326 | 1933 | ftpReadPass(ftpState); |
3fdadc70 | 1934 | } else if (code == 331) { |
62e76326 | 1935 | ftpSendPass(ftpState); |
3fdadc70 | 1936 | } else { |
f381d699 | 1937 | ftpState->loginFailed(); |
3fdadc70 | 1938 | } |
1939 | } | |
1940 | ||
63be0a78 | 1941 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 1942 | static void |
1943 | ftpSendPass(FtpStateData * ftpState) | |
1944 | { | |
a11382aa | 1945 | /* check the server control channel is still available */ |
9e008dda | 1946 | if (!ftpState || !ftpState->haveControlChannel("ftpSendPass")) |
a11382aa | 1947 | return; |
1948 | ||
969c39b9 | 1949 | snprintf(cbuf, 1024, "PASS %s\r\n", ftpState->password); |
2f47fadf | 1950 | ftpState->writeCommand(cbuf); |
969c39b9 | 1951 | ftpState->state = SENT_PASS; |
1952 | } | |
1953 | ||
63be0a78 | 1954 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 1955 | static void |
1956 | ftpReadPass(FtpStateData * ftpState) | |
1957 | { | |
1958 | int code = ftpState->ctrl.replycode; | |
4d6c56a6 | 1959 | debugs(9, 3, HERE << "code=" << code); |
62e76326 | 1960 | |
3fdadc70 | 1961 | if (code == 230) { |
62e76326 | 1962 | ftpSendType(ftpState); |
3fdadc70 | 1963 | } else { |
f381d699 | 1964 | ftpState->loginFailed(); |
3fdadc70 | 1965 | } |
1966 | } | |
1967 | ||
63be0a78 | 1968 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 1969 | static void |
1970 | ftpSendType(FtpStateData * ftpState) | |
1971 | { | |
02922e76 | 1972 | const char *t; |
1973 | const char *filename; | |
969c39b9 | 1974 | char mode; |
a11382aa | 1975 | |
1976 | /* check the server control channel is still available */ | |
9e008dda | 1977 | if (!ftpState || !ftpState->haveControlChannel("ftpSendType")) |
a11382aa | 1978 | return; |
1979 | ||
9e242e02 | 1980 | /* |
1981 | * Ref section 3.2.2 of RFC 1738 | |
1982 | */ | |
b02bfc2d | 1983 | mode = ftpState->typecode; |
62e76326 | 1984 | |
b02bfc2d | 1985 | switch (mode) { |
62e76326 | 1986 | |
9e242e02 | 1987 | case 'D': |
62e76326 | 1988 | mode = 'A'; |
1989 | break; | |
1990 | ||
9e242e02 | 1991 | case 'A': |
62e76326 | 1992 | |
9e242e02 | 1993 | case 'I': |
62e76326 | 1994 | break; |
1995 | ||
9e242e02 | 1996 | default: |
62e76326 | 1997 | |
1998 | if (ftpState->flags.isdir) { | |
1999 | mode = 'A'; | |
2000 | } else { | |
650c4b88 | 2001 | t = ftpState->request->urlpath.rpos('/'); |
d53b3f6d | 2002 | filename = t ? t + 1 : ftpState->request->urlpath.termedBuf(); |
62e76326 | 2003 | mode = mimeGetTransferMode(filename); |
2004 | } | |
2005 | ||
2006 | break; | |
9e242e02 | 2007 | } |
62e76326 | 2008 | |
969c39b9 | 2009 | if (mode == 'I') |
62e76326 | 2010 | ftpState->flags.binary = 1; |
cfbf5373 | 2011 | else |
62e76326 | 2012 | ftpState->flags.binary = 0; |
2013 | ||
969c39b9 | 2014 | snprintf(cbuf, 1024, "TYPE %c\r\n", mode); |
62e76326 | 2015 | |
2f47fadf | 2016 | ftpState->writeCommand(cbuf); |
62e76326 | 2017 | |
969c39b9 | 2018 | ftpState->state = SENT_TYPE; |
2019 | } | |
2020 | ||
63be0a78 | 2021 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2022 | static void |
2023 | ftpReadType(FtpStateData * ftpState) | |
2024 | { | |
2025 | int code = ftpState->ctrl.replycode; | |
3fdadc70 | 2026 | char *path; |
6e5ae4a4 | 2027 | char *d, *p; |
a689bd4e | 2028 | debugs(9, 3, HERE); |
62e76326 | 2029 | |
3fdadc70 | 2030 | if (code == 200) { |
d53b3f6d | 2031 | p = path = xstrdup(ftpState->request->urlpath.termedBuf()); |
62e76326 | 2032 | |
2033 | if (*p == '/') | |
2034 | p++; | |
2035 | ||
2036 | while (*p) { | |
2037 | d = p; | |
2038 | p += strcspn(p, "/"); | |
2039 | ||
2040 | if (*p) | |
2041 | *p++ = '\0'; | |
2042 | ||
2043 | rfc1738_unescape(d); | |
2044 | ||
80dc929d | 2045 | if (*d) |
2046 | wordlistAdd(&ftpState->pathcomps, d); | |
62e76326 | 2047 | } |
2048 | ||
2049 | xfree(path); | |
2050 | ||
2051 | if (ftpState->pathcomps) | |
2052 | ftpTraverseDirectory(ftpState); | |
2053 | else | |
2054 | ftpListDir(ftpState); | |
3fdadc70 | 2055 | } else { |
62e76326 | 2056 | ftpFail(ftpState); |
3fdadc70 | 2057 | } |
2058 | } | |
2059 | ||
63be0a78 | 2060 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2061 | static void |
969c39b9 | 2062 | ftpTraverseDirectory(FtpStateData * ftpState) |
3fdadc70 | 2063 | { |
2064 | wordlist *w; | |
a689bd4e | 2065 | debugs(9, 4, HERE << (ftpState->filepath ? ftpState->filepath : "<NULL>")); |
969c39b9 | 2066 | |
5627a633 | 2067 | safe_free(ftpState->dirpath); |
2068 | ftpState->dirpath = ftpState->filepath; | |
2069 | ftpState->filepath = NULL; | |
2070 | ||
969c39b9 | 2071 | /* Done? */ |
62e76326 | 2072 | |
969c39b9 | 2073 | if (ftpState->pathcomps == NULL) { |
a689bd4e | 2074 | debugs(9, 3, HERE << "the final component was a directory"); |
62e76326 | 2075 | ftpListDir(ftpState); |
2076 | return; | |
3fdadc70 | 2077 | } |
62e76326 | 2078 | |
969c39b9 | 2079 | /* Go to next path component */ |
2080 | w = ftpState->pathcomps; | |
62e76326 | 2081 | |
969c39b9 | 2082 | ftpState->filepath = w->key; |
62e76326 | 2083 | |
969c39b9 | 2084 | ftpState->pathcomps = w->next; |
62e76326 | 2085 | |
d295d770 | 2086 | delete w; |
62e76326 | 2087 | |
969c39b9 | 2088 | /* Check if we are to CWD or RETR */ |
e55f0142 | 2089 | if (ftpState->pathcomps != NULL || ftpState->flags.isdir) { |
62e76326 | 2090 | ftpSendCwd(ftpState); |
969c39b9 | 2091 | } else { |
a689bd4e | 2092 | debugs(9, 3, HERE << "final component is probably a file"); |
62e76326 | 2093 | ftpGetFile(ftpState); |
2094 | return; | |
969c39b9 | 2095 | } |
2096 | } | |
2097 | ||
63be0a78 | 2098 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 2099 | static void |
2100 | ftpSendCwd(FtpStateData * ftpState) | |
2101 | { | |
9ededfe2 | 2102 | char *path = NULL; |
a11382aa | 2103 | |
2104 | /* check the server control channel is still available */ | |
9e008dda | 2105 | if (!ftpState || !ftpState->haveControlChannel("ftpSendCwd")) |
a11382aa | 2106 | return; |
2107 | ||
a689bd4e | 2108 | debugs(9, 3, HERE); |
62e76326 | 2109 | |
9ededfe2 | 2110 | path = ftpState->filepath; |
2111 | ||
969c39b9 | 2112 | if (!strcmp(path, "..") || !strcmp(path, "/")) { |
62e76326 | 2113 | ftpState->flags.no_dotdot = 1; |
13e80e5b | 2114 | } else { |
62e76326 | 2115 | ftpState->flags.no_dotdot = 0; |
13e80e5b | 2116 | } |
62e76326 | 2117 | |
80dc929d | 2118 | snprintf(cbuf, 1024, "CWD %s\r\n", path); |
62e76326 | 2119 | |
2f47fadf | 2120 | ftpState->writeCommand(cbuf); |
62e76326 | 2121 | |
969c39b9 | 2122 | ftpState->state = SENT_CWD; |
3fdadc70 | 2123 | } |
77a30ebb | 2124 | |
63be0a78 | 2125 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2126 | static void |
2127 | ftpReadCwd(FtpStateData * ftpState) | |
2128 | { | |
2129 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 2130 | debugs(9, 3, HERE); |
62e76326 | 2131 | |
3fdadc70 | 2132 | if (code >= 200 && code < 300) { |
62e76326 | 2133 | /* CWD OK */ |
6f0aab86 | 2134 | ftpState->unhack(); |
62e76326 | 2135 | |
0477a072 AJ |
2136 | /* Reset cwd_message to only include the last message */ |
2137 | ftpState->cwd_message.reset(""); | |
2138 | for (wordlist *w = ftpState->ctrl.message; w; w = w->next) { | |
2139 | ftpState->cwd_message.append(' '); | |
2140 | ftpState->cwd_message.append(w->key); | |
2141 | } | |
62e76326 | 2142 | ftpState->ctrl.message = NULL; |
2143 | ||
2144 | /* Continue to traverse the path */ | |
2145 | ftpTraverseDirectory(ftpState); | |
3fdadc70 | 2146 | } else { |
62e76326 | 2147 | /* CWD FAILED */ |
2148 | ||
2149 | if (!ftpState->flags.put) | |
2150 | ftpFail(ftpState); | |
2151 | else | |
2152 | ftpSendMkdir(ftpState); | |
c021888f | 2153 | } |
3fdadc70 | 2154 | } |
2155 | ||
63be0a78 | 2156 | /// \ingroup ServerProtocolFTPInternal |
54220df8 | 2157 | static void |
94439e4e | 2158 | ftpSendMkdir(FtpStateData * ftpState) |
54220df8 | 2159 | { |
9ededfe2 | 2160 | char *path = NULL; |
a11382aa | 2161 | |
2162 | /* check the server control channel is still available */ | |
9e008dda | 2163 | if (!ftpState || !ftpState->haveControlChannel("ftpSendMkdir")) |
a11382aa | 2164 | return; |
2165 | ||
9ededfe2 | 2166 | path = ftpState->filepath; |
a689bd4e | 2167 | debugs(9, 3, HERE << "with path=" << path); |
4162ee3b | 2168 | snprintf(cbuf, 1024, "MKD %s\r\n", path); |
2f47fadf | 2169 | ftpState->writeCommand(cbuf); |
4162ee3b | 2170 | ftpState->state = SENT_MKDIR; |
54220df8 | 2171 | } |
2172 | ||
63be0a78 | 2173 | /// \ingroup ServerProtocolFTPInternal |
54220df8 | 2174 | static void |
4162ee3b | 2175 | ftpReadMkdir(FtpStateData * ftpState) |
2176 | { | |
2177 | char *path = ftpState->filepath; | |
2178 | int code = ftpState->ctrl.replycode; | |
2179 | ||
a689bd4e | 2180 | debugs(9, 3, HERE << "path " << path << ", code " << code); |
62e76326 | 2181 | |
4162ee3b | 2182 | if (code == 257) { /* success */ |
62e76326 | 2183 | ftpSendCwd(ftpState); |
4162ee3b | 2184 | } else if (code == 550) { /* dir exists */ |
62e76326 | 2185 | |
2186 | if (ftpState->flags.put_mkdir) { | |
2187 | ftpState->flags.put_mkdir = 1; | |
2188 | ftpSendCwd(ftpState); | |
2189 | } else | |
2190 | ftpSendReply(ftpState); | |
4162ee3b | 2191 | } else |
62e76326 | 2192 | ftpSendReply(ftpState); |
54220df8 | 2193 | } |
2194 | ||
63be0a78 | 2195 | /// \ingroup ServerProtocolFTPInternal |
dbfed404 | 2196 | static void |
2197 | ftpGetFile(FtpStateData * ftpState) | |
2198 | { | |
2199 | assert(*ftpState->filepath != '\0'); | |
e55f0142 | 2200 | ftpState->flags.isdir = 0; |
dbfed404 | 2201 | ftpSendMdtm(ftpState); |
2202 | } | |
2203 | ||
63be0a78 | 2204 | /// \ingroup ServerProtocolFTPInternal |
dbfed404 | 2205 | static void |
2206 | ftpListDir(FtpStateData * ftpState) | |
2207 | { | |
d5f80edc | 2208 | if (ftpState->flags.dir_slash) { |
a689bd4e | 2209 | debugs(9, 3, HERE << "Directory path did not end in /"); |
62e76326 | 2210 | ftpState->title_url.append("/"); |
2211 | ftpState->flags.isdir = 1; | |
dbfed404 | 2212 | } |
62e76326 | 2213 | |
a689bd4e | 2214 | ftpSendPassive(ftpState); |
dbfed404 | 2215 | } |
2216 | ||
63be0a78 | 2217 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 2218 | static void |
2219 | ftpSendMdtm(FtpStateData * ftpState) | |
2220 | { | |
a11382aa | 2221 | /* check the server control channel is still available */ |
9e008dda | 2222 | if (!ftpState || !ftpState->haveControlChannel("ftpSendMdtm")) |
a11382aa | 2223 | return; |
2224 | ||
969c39b9 | 2225 | assert(*ftpState->filepath != '\0'); |
2226 | snprintf(cbuf, 1024, "MDTM %s\r\n", ftpState->filepath); | |
2f47fadf | 2227 | ftpState->writeCommand(cbuf); |
969c39b9 | 2228 | ftpState->state = SENT_MDTM; |
2229 | } | |
2230 | ||
63be0a78 | 2231 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2232 | static void |
2233 | ftpReadMdtm(FtpStateData * ftpState) | |
2234 | { | |
2235 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 2236 | debugs(9, 3, HERE); |
62e76326 | 2237 | |
3fdadc70 | 2238 | if (code == 213) { |
62e76326 | 2239 | ftpState->mdtm = parse_iso3307_time(ftpState->ctrl.last_reply); |
6f0aab86 | 2240 | ftpState->unhack(); |
3fdadc70 | 2241 | } else if (code < 0) { |
62e76326 | 2242 | ftpFail(ftpState); |
9e008dda | 2243 | return; |
77a30ebb | 2244 | } |
62e76326 | 2245 | |
969c39b9 | 2246 | ftpSendSize(ftpState); |
2247 | } | |
2248 | ||
63be0a78 | 2249 | /// \ingroup ServerProtocolFTPInternal |
969c39b9 | 2250 | static void |
2251 | ftpSendSize(FtpStateData * ftpState) | |
2252 | { | |
a11382aa | 2253 | /* check the server control channel is still available */ |
9e008dda | 2254 | if (!ftpState || !ftpState->haveControlChannel("ftpSendSize")) |
a11382aa | 2255 | return; |
2256 | ||
969c39b9 | 2257 | /* Only send SIZE for binary transfers. The returned size |
2258 | * is useless on ASCII transfers */ | |
62e76326 | 2259 | |
e55f0142 | 2260 | if (ftpState->flags.binary) { |
62e76326 | 2261 | assert(ftpState->filepath != NULL); |
2262 | assert(*ftpState->filepath != '\0'); | |
2263 | snprintf(cbuf, 1024, "SIZE %s\r\n", ftpState->filepath); | |
2f47fadf | 2264 | ftpState->writeCommand(cbuf); |
62e76326 | 2265 | ftpState->state = SENT_SIZE; |
969c39b9 | 2266 | } else |
62e76326 | 2267 | /* Skip to next state no non-binary transfers */ |
a689bd4e | 2268 | ftpSendPassive(ftpState); |
3fdadc70 | 2269 | } |
2270 | ||
63be0a78 | 2271 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2272 | static void |
2273 | ftpReadSize(FtpStateData * ftpState) | |
2274 | { | |
2275 | int code = ftpState->ctrl.replycode; | |
a689bd4e | 2276 | debugs(9, 3, HERE); |
62e76326 | 2277 | |
3fdadc70 | 2278 | if (code == 213) { |
6f0aab86 | 2279 | ftpState->unhack(); |
47f6e231 | 2280 | ftpState->theSize = strtoll(ftpState->ctrl.last_reply, NULL, 10); |
62e76326 | 2281 | |
47f6e231 | 2282 | if (ftpState->theSize == 0) { |
a689bd4e | 2283 | debugs(9, 2, "SIZE reported " << |
9e008dda | 2284 | ftpState->ctrl.last_reply << " on " << |
d53b3f6d | 2285 | ftpState->title_url); |
47f6e231 | 2286 | ftpState->theSize = -1; |
62e76326 | 2287 | } |
3fdadc70 | 2288 | } else if (code < 0) { |
62e76326 | 2289 | ftpFail(ftpState); |
9e008dda | 2290 | return; |
3fdadc70 | 2291 | } |
62e76326 | 2292 | |
a689bd4e | 2293 | ftpSendPassive(ftpState); |
3fdadc70 | 2294 | } |
2295 | ||
63be0a78 | 2296 | /** |
2297 | \ingroup ServerProtocolFTPInternal | |
2298 | */ | |
cc192b50 | 2299 | static void |
a689bd4e | 2300 | ftpReadEPSV(FtpStateData* ftpState) |
cc192b50 | 2301 | { |
a689bd4e | 2302 | int code = ftpState->ctrl.replycode; |
2303 | char h1, h2, h3, h4; | |
2304 | int n; | |
2305 | u_short port; | |
b7ac5457 | 2306 | Ip::Address ipa_remote; |
a689bd4e | 2307 | int fd = ftpState->data.fd; |
2308 | char *buf; | |
2309 | debugs(9, 3, HERE); | |
2310 | ||
2311 | if (code != 229 && code != 522) { | |
9e008dda | 2312 | if (code == 200) { |
a689bd4e | 2313 | /* handle broken servers (RFC 2428 says OK code for EPSV MUST be 229 not 200) */ |
2314 | /* vsftpd for one send '200 EPSV ALL ok.' without even port info. | |
2315 | * Its okay to re-send EPSV 1/2 but nothing else. */ | |
2316 | debugs(9, DBG_IMPORTANT, "Broken FTP Server at " << fd_table[ftpState->ctrl.fd].ipaddr << ". Wrong accept code for EPSV"); | |
9e008dda | 2317 | } else { |
a689bd4e | 2318 | debugs(9, 2, "EPSV not supported by remote end"); |
2319 | ftpState->state = SENT_EPSV_1; /* simulate having failed EPSV 1 (last EPSV to try before shifting to PASV) */ | |
2320 | } | |
2321 | ftpSendPassive(ftpState); | |
2322 | return; | |
2323 | } | |
cc192b50 | 2324 | |
9e008dda | 2325 | if (code == 522) { |
a689bd4e | 2326 | /* server response with list of supported methods */ |
2327 | /* 522 Network protocol not supported, use (1) */ | |
2328 | /* 522 Network protocol not supported, use (1,2) */ | |
055421ee | 2329 | /* 522 Network protocol not supported, use (2) */ |
e1381638 | 2330 | /* TODO: handle the (1,2) case. We might get it back after EPSV ALL |
0f738a43 | 2331 | * which means close data + control without self-destructing and re-open from scratch. */ |
a689bd4e | 2332 | debugs(9, 5, HERE << "scanning: " << ftpState->ctrl.last_reply); |
0f738a43 AJ |
2333 | buf = ftpState->ctrl.last_reply; |
2334 | while (buf != NULL && *buf != '\0' && *buf != '\n' && *buf != '(') ++buf; | |
2335 | if (buf != NULL && *buf == '\n') ++buf; | |
cc192b50 | 2336 | |
9e008dda | 2337 | if (buf == NULL || *buf == '\0') { |
a689bd4e | 2338 | /* handle broken server (RFC 2428 says MUST specify supported protocols in 522) */ |
2339 | debugs(9, DBG_IMPORTANT, "Broken FTP Server at " << fd_table[ftpState->ctrl.fd].ipaddr << ". 522 error missing protocol negotiation hints"); | |
2340 | ftpSendPassive(ftpState); | |
9e008dda | 2341 | } else if (strcmp(buf, "(1)") == 0) { |
a689bd4e | 2342 | ftpState->state = SENT_EPSV_2; /* simulate having sent and failed EPSV 2 */ |
2343 | ftpSendPassive(ftpState); | |
9e008dda | 2344 | } else if (strcmp(buf, "(2)") == 0) { |
055421ee AJ |
2345 | if (Ip::EnableIpv6) { |
2346 | /* If server only supports EPSV 2 and we have already tried that. Go straight to EPRT */ | |
2347 | if (ftpState->state == SENT_EPSV_2) { | |
2348 | ftpSendEPRT(ftpState); | |
2349 | } else { | |
2350 | /* or try the next Passive mode down the chain. */ | |
2351 | ftpSendPassive(ftpState); | |
2352 | } | |
9e008dda | 2353 | } else { |
055421ee AJ |
2354 | /* Server only accept EPSV in IPv6 traffic. */ |
2355 | ftpState->state = SENT_EPSV_1; /* simulate having sent and failed EPSV 1 */ | |
a689bd4e | 2356 | ftpSendPassive(ftpState); |
2357 | } | |
e1381638 | 2358 | } else { |
0f738a43 AJ |
2359 | /* handle broken server (RFC 2428 says MUST specify supported protocols in 522) */ |
2360 | debugs(9, DBG_IMPORTANT, "WARNING: Server at " << fd_table[ftpState->ctrl.fd].ipaddr << " sent unknown protocol negotiation hint: " << buf); | |
2361 | ftpSendPassive(ftpState); | |
2362 | } | |
a689bd4e | 2363 | return; |
2364 | } | |
2365 | ||
2366 | /* 229 Entering Extended Passive Mode (|||port|) */ | |
2367 | /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */ | |
2368 | debugs(9, 5, "scanning: " << ftpState->ctrl.last_reply); | |
2369 | ||
2370 | buf = ftpState->ctrl.last_reply + strcspn(ftpState->ctrl.last_reply, "("); | |
2371 | ||
2372 | n = sscanf(buf, "(%c%c%c%hu%c)", &h1, &h2, &h3, &port, &h4); | |
2373 | ||
2374 | if (h1 != h2 || h1 != h3 || h1 != h4) { | |
2375 | debugs(9, DBG_IMPORTANT, "Invalid EPSV reply from " << | |
2376 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << | |
2377 | ftpState->ctrl.last_reply); | |
2378 | ||
2379 | ftpSendPassive(ftpState); | |
2380 | return; | |
2381 | } | |
2382 | ||
2383 | if (0 == port) { | |
2384 | debugs(9, DBG_IMPORTANT, "Unsafe EPSV reply from " << | |
2385 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << | |
2386 | ftpState->ctrl.last_reply); | |
2387 | ||
2388 | ftpSendPassive(ftpState); | |
2389 | return; | |
2390 | } | |
2391 | ||
2392 | if (Config.Ftp.sanitycheck) { | |
2393 | if (port < 1024) { | |
2394 | debugs(9, DBG_IMPORTANT, "Unsafe EPSV reply from " << | |
2395 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << | |
2396 | ftpState->ctrl.last_reply); | |
2397 | ||
2398 | ftpSendPassive(ftpState); | |
2399 | return; | |
2400 | } | |
2401 | } | |
2402 | ||
2403 | ftpState->data.port = port; | |
2404 | ||
2405 | ftpState->data.host = xstrdup(fd_table[ftpState->ctrl.fd].ipaddr); | |
2406 | ||
2407 | safe_free(ftpState->ctrl.last_command); | |
2408 | ||
2409 | safe_free(ftpState->ctrl.last_reply); | |
2410 | ||
2411 | ftpState->ctrl.last_command = xstrdup("Connect to server data port"); | |
2412 | ||
2413 | debugs(9, 3, HERE << "connecting to " << ftpState->data.host << ", port " << ftpState->data.port); | |
cc192b50 | 2414 | |
a689bd4e | 2415 | commConnectStart(fd, ftpState->data.host, port, FtpStateData::ftpPasvCallback, ftpState); |
2416 | } | |
cc192b50 | 2417 | |
63be0a78 | 2418 | /** \ingroup ServerProtocolFTPInternal |
2419 | * | |
a689bd4e | 2420 | * Send Passive connection request. |
2421 | * Default method is to use modern EPSV request. | |
2422 | * The failover mechanism should check for previous state and re-call with alternates on failure. | |
2423 | */ | |
3fdadc70 | 2424 | static void |
a689bd4e | 2425 | ftpSendPassive(FtpStateData * ftpState) |
3fdadc70 | 2426 | { |
b7ac5457 | 2427 | Ip::Address addr; |
cc192b50 | 2428 | struct addrinfo *AI = NULL; |
62e76326 | 2429 | |
a689bd4e | 2430 | /** Checks the server control channel is still available before running. */ |
9e008dda | 2431 | if (!ftpState || !ftpState->haveControlChannel("ftpSendPassive")) |
a11382aa | 2432 | return; |
2433 | ||
a689bd4e | 2434 | debugs(9, 3, HERE); |
2435 | ||
2436 | /** \par | |
2437 | * Checks for EPSV ALL special conditions: | |
2438 | * If enabled to be sent, squid MUST NOT request any other connect methods. | |
2439 | * If 'ALL' is sent and fails the entire FTP Session fails. | |
2440 | * NP: By my reading exact EPSV protocols maybe attempted, but only EPSV method. */ | |
9e008dda | 2441 | if (Config.Ftp.epsv_all && ftpState->flags.epsv_all_sent && ftpState->state == SENT_EPSV_1 ) { |
a689bd4e | 2442 | debugs(9, DBG_IMPORTANT, "FTP does not allow PASV method after 'EPSV ALL' has been sent."); |
2443 | ftpFail(ftpState); | |
2444 | return; | |
2445 | } | |
ee65546f | 2446 | |
a689bd4e | 2447 | /** \par |
2448 | * Checks for 'HEAD' method request and passes off for special handling by FtpStateData::processHeadResponse(). */ | |
47f6e231 | 2449 | if (ftpState->request->method == METHOD_HEAD && (ftpState->flags.isdir || ftpState->theSize != -1)) { |
063a47b5 | 2450 | ftpState->processHeadResponse(); // may call serverComplete |
62e76326 | 2451 | return; |
7497ceef | 2452 | } |
62e76326 | 2453 | |
94b88585 AR |
2454 | /// Closes any old FTP-Data connection which may exist. */ |
2455 | ftpState->data.close(); | |
62e76326 | 2456 | |
a689bd4e | 2457 | /** \par |
2458 | * Checks for previous EPSV/PASV failures on this server/session. | |
2459 | * Diverts to EPRT immediately if they are not working. */ | |
e55f0142 | 2460 | if (!ftpState->flags.pasv_supported) { |
cc192b50 | 2461 | ftpSendEPRT(ftpState); |
62e76326 | 2462 | return; |
30a4f2a8 | 2463 | } |
62e76326 | 2464 | |
a689bd4e | 2465 | /** \par |
2466 | * Locates the Address of the remote server. */ | |
cc192b50 | 2467 | addr.InitAddrInfo(AI); |
62e76326 | 2468 | |
cc192b50 | 2469 | if (getsockname(ftpState->ctrl.fd, AI->ai_addr, &AI->ai_addrlen)) { |
a689bd4e | 2470 | /** If it cannot be located the FTP Session is killed. */ |
cc192b50 | 2471 | addr.FreeAddrInfo(AI); |
a689bd4e | 2472 | debugs(9, DBG_CRITICAL, HERE << "getsockname(" << ftpState->ctrl.fd << ",'" << addr << "',...): " << xstrerror()); |
62e76326 | 2473 | ftpFail(ftpState); |
2474 | return; | |
cdc33f35 | 2475 | } |
62e76326 | 2476 | |
cc192b50 | 2477 | addr = *AI; |
cc192b50 | 2478 | addr.FreeAddrInfo(AI); |
2479 | ||
a689bd4e | 2480 | /** \par |
2481 | * Send EPSV (ALL,2,1) or PASV on the control channel. | |
2482 | * | |
d85b8894 | 2483 | * - EPSV ALL is used if enabled. |
9adb5bc5 AJ |
2484 | * - EPSV 2 is used if ALL is disabled and IPv6 is available and ctrl channel is IPv6. |
2485 | * - EPSV 1 is used if EPSV 2 (IPv6) fails or is not available or ctrl channel is IPv4. | |
d85b8894 | 2486 | * - PASV is used if EPSV 1 fails. |
a689bd4e | 2487 | */ |
9e008dda | 2488 | switch (ftpState->state) { |
9adb5bc5 AJ |
2489 | case SENT_EPSV_ALL: /* EPSV ALL resulted in a bad response. Try ther EPSV methods. */ |
2490 | ftpState->flags.epsv_all_sent = true; | |
2491 | if (addr.IsIPv6()) { | |
fe2d668b | 2492 | debugs(9, 5, HERE << "FTP Channel is IPv6 (" << addr << ") attempting EPSV 2 after EPSV ALL has failed."); |
9adb5bc5 AJ |
2493 | snprintf(cbuf, 1024, "EPSV 2\r\n"); |
2494 | ftpState->state = SENT_EPSV_2; | |
2495 | break; | |
2496 | } | |
2497 | // else fall through to skip EPSV 2 | |
62e76326 | 2498 | |
a689bd4e | 2499 | case SENT_EPSV_2: /* EPSV IPv6 failed. Try EPSV IPv4 */ |
9adb5bc5 | 2500 | if (addr.IsIPv4()) { |
fe2d668b | 2501 | debugs(9, 5, HERE << "FTP Channel is IPv4 (" << addr << ") attempting EPSV 1 after EPSV ALL has failed."); |
9adb5bc5 AJ |
2502 | snprintf(cbuf, 1024, "EPSV 1\r\n"); |
2503 | ftpState->state = SENT_EPSV_1; | |
d06d0e1e | 2504 | break; |
e1381638 | 2505 | } else if (ftpState->flags.epsv_all_sent) { |
9adb5bc5 AJ |
2506 | debugs(9, DBG_IMPORTANT, "FTP does not allow PASV method after 'EPSV ALL' has been sent."); |
2507 | ftpFail(ftpState); | |
2508 | return; | |
2509 | } | |
2510 | // else fall through to skip EPSV 1 | |
a689bd4e | 2511 | |
9adb5bc5 | 2512 | case SENT_EPSV_1: /* EPSV options exhausted. Try PASV now. */ |
fe2d668b | 2513 | debugs(9, 5, HERE << "FTP Channel (" << addr << ") rejects EPSV connection attempts. Trying PASV instead."); |
9adb5bc5 AJ |
2514 | snprintf(cbuf, 1024, "PASV\r\n"); |
2515 | ftpState->state = SENT_PASV; | |
a689bd4e | 2516 | break; |
2517 | ||
2518 | default: | |
51ee534d | 2519 | if (!Config.Ftp.epsv) { |
fe2d668b | 2520 | debugs(9, 5, HERE << "EPSV support manually disabled. Sending PASV for FTP Channel (" << addr <<")"); |
51ee534d AJ |
2521 | snprintf(cbuf, 1024, "PASV\r\n"); |
2522 | ftpState->state = SENT_PASV; | |
2523 | } else if (Config.Ftp.epsv_all) { | |
fe2d668b | 2524 | debugs(9, 5, HERE << "EPSV ALL manually enabled. Attempting with FTP Channel (" << addr <<")"); |
a689bd4e | 2525 | snprintf(cbuf, 1024, "EPSV ALL\r\n"); |
2526 | ftpState->state = SENT_EPSV_ALL; | |
2527 | /* block other non-EPSV connections being attempted */ | |
2528 | ftpState->flags.epsv_all_sent = true; | |
9e008dda | 2529 | } else { |
fe2d668b AJ |
2530 | if (addr.IsIPv6()) { |
2531 | debugs(9, 5, HERE << "FTP Channel (" << addr << "). Sending default EPSV 2"); | |
2532 | snprintf(cbuf, 1024, "EPSV 2\r\n"); | |
2533 | ftpState->state = SENT_EPSV_2; | |
2534 | } | |
fe2d668b AJ |
2535 | if (addr.IsIPv4()) { |
2536 | debugs(9, 5, HERE << "Channel (" << addr <<"). Sending default EPSV 1"); | |
2537 | snprintf(cbuf, 1024, "EPSV 1\r\n"); | |
2538 | ftpState->state = SENT_EPSV_1; | |
2539 | } | |
a689bd4e | 2540 | } |
2541 | break; | |
2542 | } | |
62e76326 | 2543 | |
9adb5bc5 AJ |
2544 | /** Otherwise, Open data channel with the same local address as control channel (on a new random port!) */ |
2545 | addr.SetPort(0); | |
2546 | int fd = comm_open(SOCK_STREAM, | |
2547 | IPPROTO_TCP, | |
2548 | addr, | |
2549 | COMM_NONBLOCKING, | |
2550 | ftpState->entry->url()); | |
2551 | ||
fe2d668b | 2552 | debugs(9, 3, HERE << "Unconnected data socket created on FD " << fd << " from " << addr); |
9adb5bc5 AJ |
2553 | |
2554 | if (fd < 0) { | |
2555 | ftpFail(ftpState); | |
2556 | return; | |
2557 | } | |
2558 | ||
2559 | ftpState->data.opened(fd, ftpState->dataCloser()); | |
a689bd4e | 2560 | ftpState->writeCommand(cbuf); |
62e76326 | 2561 | |
7e3ce7b9 | 2562 | /* |
2563 | * ugly hack for ftp servers like ftp.netscape.com that sometimes | |
1be4874e | 2564 | * dont acknowledge PASV commands. |
7e3ce7b9 | 2565 | */ |
dc56a9b1 | 2566 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
4299f876 AR |
2567 | AsyncCall::Pointer timeoutCall = JobCallback(9, 5, |
2568 | TimeoutDialer, ftpState, FtpStateData::ftpTimeout); | |
dc56a9b1 | 2569 | |
2570 | commSetTimeout(ftpState->data.fd, 15, timeoutCall); | |
3fdadc70 | 2571 | } |
2572 | ||
063a47b5 | 2573 | void |
2574 | FtpStateData::processHeadResponse() | |
2575 | { | |
2576 | debugs(9, 5, HERE << "handling HEAD response"); | |
2577 | ftpSendQuit(this); | |
2578 | appendSuccessHeader(); | |
2579 | ||
2580 | /* | |
2581 | * On rare occasions I'm seeing the entry get aborted after | |
2582 | * ftpReadControlReply() and before here, probably when | |
2583 | * trying to write to the client. | |
2584 | */ | |
2585 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { | |
2586 | abortTransaction("entry aborted while processing HEAD"); | |
2587 | return; | |
2588 | } | |
2589 | ||
a83c6ed6 AR |
2590 | #if USE_ADAPTATION |
2591 | if (adaptationAccessCheckPending) { | |
2592 | debugs(9,3, HERE << "returning due to adaptationAccessCheckPending"); | |
063a47b5 | 2593 | return; |
2594 | } | |
2595 | #endif | |
2596 | ||
2597 | // processReplyBody calls serverComplete() since there is no body | |
9e008dda | 2598 | processReplyBody(); |
063a47b5 | 2599 | } |
2600 | ||
63be0a78 | 2601 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2602 | static void |
2603 | ftpReadPasv(FtpStateData * ftpState) | |
2604 | { | |
2605 | int code = ftpState->ctrl.replycode; | |
2606 | int h1, h2, h3, h4; | |
2607 | int p1, p2; | |
2608 | int n; | |
2609 | u_short port; | |
b7ac5457 | 2610 | Ip::Address ipa_remote; |
3fdadc70 | 2611 | int fd = ftpState->data.fd; |
748f6a15 | 2612 | char *buf; |
00c5afca | 2613 | LOCAL_ARRAY(char, ipaddr, 1024); |
a689bd4e | 2614 | debugs(9, 3, HERE); |
62e76326 | 2615 | |
3fdadc70 | 2616 | if (code != 227) { |
a689bd4e | 2617 | debugs(9, 2, "PASV not supported by remote end"); |
cc192b50 | 2618 | ftpSendEPRT(ftpState); |
62e76326 | 2619 | return; |
3fdadc70 | 2620 | } |
62e76326 | 2621 | |
748f6a15 | 2622 | /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */ |
2623 | /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */ | |
a689bd4e | 2624 | debugs(9, 5, HERE << "scanning: " << ftpState->ctrl.last_reply); |
62e76326 | 2625 | |
3f9d0b2d | 2626 | buf = ftpState->ctrl.last_reply + strcspn(ftpState->ctrl.last_reply, "0123456789"); |
62e76326 | 2627 | |
748f6a15 | 2628 | n = sscanf(buf, "%d,%d,%d,%d,%d,%d", &h1, &h2, &h3, &h4, &p1, &p2); |
62e76326 | 2629 | |
748f6a15 | 2630 | if (n != 6 || p1 < 0 || p2 < 0 || p1 > 255 || p2 > 255) { |
a689bd4e | 2631 | debugs(9, DBG_IMPORTANT, "Unsafe PASV reply from " << |
bf8fe701 | 2632 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << |
2633 | ftpState->ctrl.last_reply); | |
2634 | ||
cc192b50 | 2635 | ftpSendEPRT(ftpState); |
62e76326 | 2636 | return; |
3fdadc70 | 2637 | } |
62e76326 | 2638 | |
00c5afca | 2639 | snprintf(ipaddr, 1024, "%d.%d.%d.%d", h1, h2, h3, h4); |
62e76326 | 2640 | |
cc192b50 | 2641 | ipa_remote = ipaddr; |
2642 | ||
9e008dda | 2643 | if ( ipa_remote.IsAnyAddr() ) { |
a689bd4e | 2644 | debugs(9, DBG_IMPORTANT, "Unsafe PASV reply from " << |
bf8fe701 | 2645 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << |
2646 | ftpState->ctrl.last_reply); | |
2647 | ||
cc192b50 | 2648 | ftpSendEPRT(ftpState); |
62e76326 | 2649 | return; |
3fdadc70 | 2650 | } |
62e76326 | 2651 | |
3fdadc70 | 2652 | port = ((p1 << 8) + p2); |
62e76326 | 2653 | |
7f74df3a | 2654 | if (0 == port) { |
a689bd4e | 2655 | debugs(9, DBG_IMPORTANT, "Unsafe PASV reply from " << |
bf8fe701 | 2656 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << |
2657 | ftpState->ctrl.last_reply); | |
2658 | ||
cc192b50 | 2659 | ftpSendEPRT(ftpState); |
62e76326 | 2660 | return; |
7f74df3a | 2661 | } |
62e76326 | 2662 | |
00c5afca | 2663 | if (Config.Ftp.sanitycheck) { |
62e76326 | 2664 | if (port < 1024) { |
a689bd4e | 2665 | debugs(9, DBG_IMPORTANT, "Unsafe PASV reply from " << |
bf8fe701 | 2666 | fd_table[ftpState->ctrl.fd].ipaddr << ": " << |
2667 | ftpState->ctrl.last_reply); | |
2668 | ||
cc192b50 | 2669 | ftpSendEPRT(ftpState); |
62e76326 | 2670 | return; |
2671 | } | |
00c5afca | 2672 | } |
62e76326 | 2673 | |
9b312a19 | 2674 | ftpState->data.port = port; |
3f9d0b2d | 2675 | |
2676 | if (Config.Ftp.sanitycheck) | |
2677 | ftpState->data.host = xstrdup(fd_table[ftpState->ctrl.fd].ipaddr); | |
2678 | else | |
2679 | ftpState->data.host = xstrdup(ipaddr); | |
2680 | ||
9bc73deb | 2681 | safe_free(ftpState->ctrl.last_command); |
3f9d0b2d | 2682 | |
9bc73deb | 2683 | safe_free(ftpState->ctrl.last_reply); |
3f9d0b2d | 2684 | |
9bc73deb | 2685 | ftpState->ctrl.last_command = xstrdup("Connect to server data port"); |
3f9d0b2d | 2686 | |
a689bd4e | 2687 | debugs(9, 3, HERE << "connecting to " << ftpState->data.host << ", port " << ftpState->data.port); |
3f9d0b2d | 2688 | |
6f0aab86 | 2689 | commConnectStart(fd, ipaddr, port, FtpStateData::ftpPasvCallback, ftpState); |
3fdadc70 | 2690 | } |
2691 | ||
6f0aab86 | 2692 | void |
3ff65596 | 2693 | FtpStateData::ftpPasvCallback(int fd, const DnsLookupDetails &dns, comm_err_t status, int xerrno, void *data) |
3fdadc70 | 2694 | { |
e6ccf245 | 2695 | FtpStateData *ftpState = (FtpStateData *)data; |
a689bd4e | 2696 | debugs(9, 3, HERE); |
3ff65596 | 2697 | ftpState->request->recordLookup(dns); |
62e76326 | 2698 | |
9b312a19 | 2699 | if (status != COMM_OK) { |
a689bd4e | 2700 | debugs(9, 2, HERE << "Failed to connect. Retrying without PASV."); |
b6b6f466 | 2701 | ftpState->fwd->dontRetry(false); /* this is a retryable error */ |
2702 | ftpState->fwd->ftpPasvFailed(true); | |
8043ea13 | 2703 | ftpState->failed(ERR_NONE, 0); |
6f0aab86 | 2704 | /* failed closes ctrl.fd and frees ftpState */ |
62e76326 | 2705 | return; |
3fdadc70 | 2706 | } |
62e76326 | 2707 | |
3fdadc70 | 2708 | ftpRestOrList(ftpState); |
2709 | } | |
2710 | ||
63be0a78 | 2711 | /// \ingroup ServerProtocolFTPInternal |
cdc33f35 | 2712 | static int |
2713 | ftpOpenListenSocket(FtpStateData * ftpState, int fallback) | |
2714 | { | |
2715 | int fd; | |
b7ac5457 | 2716 | Ip::Address addr; |
cc192b50 | 2717 | struct addrinfo *AI = NULL; |
cdc33f35 | 2718 | int on = 1; |
cc192b50 | 2719 | int x = 0; |
62e76326 | 2720 | |
04f55905 | 2721 | /// Close old data channels, if any. We may open a new one below. |
94b88585 | 2722 | ftpState->data.close(); |
62e76326 | 2723 | |
4f310655 | 2724 | /* |
2725 | * Set up a listen socket on the same local address as the | |
2726 | * control connection. | |
2727 | */ | |
62e76326 | 2728 | |
cc192b50 | 2729 | addr.InitAddrInfo(AI); |
2730 | ||
2731 | x = getsockname(ftpState->ctrl.fd, AI->ai_addr, &AI->ai_addrlen); | |
2732 | ||
2733 | addr = *AI; | |
2734 | ||
2735 | addr.FreeAddrInfo(AI); | |
2736 | ||
9e008dda | 2737 | if (x) { |
a689bd4e | 2738 | debugs(9, DBG_CRITICAL, HERE << "getsockname(" << ftpState->ctrl.fd << ",..): " << xstrerror()); |
62e76326 | 2739 | return -1; |
cdc33f35 | 2740 | } |
62e76326 | 2741 | |
4f310655 | 2742 | /* |
2743 | * REUSEADDR is needed in fallback mode, since the same port is | |
2744 | * used for both control and data. | |
cdc33f35 | 2745 | */ |
2746 | if (fallback) { | |
62e76326 | 2747 | setsockopt(ftpState->ctrl.fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)); |
9e008dda | 2748 | } else { |
a689bd4e | 2749 | /* if not running in fallback mode a new port needs to be retrieved */ |
2750 | addr.SetPort(0); | |
2751 | } | |
62e76326 | 2752 | |
cdc33f35 | 2753 | fd = comm_open(SOCK_STREAM, |
bdb741f4 | 2754 | IPPROTO_TCP, |
cc192b50 | 2755 | addr, |
62e76326 | 2756 | COMM_NONBLOCKING | (fallback ? COMM_REUSEADDR : 0), |
3900307b | 2757 | ftpState->entry->url()); |
a689bd4e | 2758 | debugs(9, 3, HERE << "Unconnected data socket created on FD " << fd ); |
62e76326 | 2759 | |
cdc33f35 | 2760 | if (fd < 0) { |
a689bd4e | 2761 | debugs(9, DBG_CRITICAL, HERE << "comm_open failed"); |
62e76326 | 2762 | return -1; |
cdc33f35 | 2763 | } |
62e76326 | 2764 | |
04f55905 | 2765 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; |
4299f876 AR |
2766 | AsyncCall::Pointer acceptCall = JobCallback(11, 5, |
2767 | acceptDialer, ftpState, FtpStateData::ftpAcceptDataConnection); | |
04f55905 AJ |
2768 | ftpState->data.listener = new Comm::ListenStateData(fd, acceptCall, false); |
2769 | ||
1fc32b95 | 2770 | if (!ftpState->data.listener || ftpState->data.listener->errcode != 0) { |
62e76326 | 2771 | comm_close(fd); |
2772 | return -1; | |
cdc33f35 | 2773 | } |
62e76326 | 2774 | |
94b88585 | 2775 | ftpState->data.opened(fd, ftpState->dataCloser()); |
d20b1cd0 | 2776 | ftpState->data.port = comm_local_port(fd); |
cdc33f35 | 2777 | ftpState->data.host = NULL; |
2778 | return fd; | |
2779 | } | |
2780 | ||
63be0a78 | 2781 | /// \ingroup ServerProtocolFTPInternal |
3fdadc70 | 2782 | static void |
cc192b50 | 2783 | ftpSendPORT(FtpStateData * ftpState) |
3fdadc70 | 2784 | { |
cdc33f35 | 2785 | int fd; |
b7ac5457 | 2786 | Ip::Address ipa; |
cc192b50 | 2787 | struct addrinfo *AI = NULL; |
cdc33f35 | 2788 | unsigned char *addrptr; |
2789 | unsigned char *portptr; | |
a11382aa | 2790 | |
2791 | /* check the server control channel is still available */ | |
9e008dda | 2792 | if (!ftpState || !ftpState->haveControlChannel("ftpSendPort")) |
a11382aa | 2793 | return; |
2794 | ||
9e008dda | 2795 | if (Config.Ftp.epsv_all && ftpState->flags.epsv_all_sent) { |
a689bd4e | 2796 | debugs(9, DBG_IMPORTANT, "FTP does not allow PORT method after 'EPSV ALL' has been sent."); |
2797 | return; | |
2798 | } | |
2799 | ||
2800 | debugs(9, 3, HERE); | |
e55f0142 | 2801 | ftpState->flags.pasv_supported = 0; |
cdc33f35 | 2802 | fd = ftpOpenListenSocket(ftpState, 0); |
cc192b50 | 2803 | ipa.InitAddrInfo(AI); |
62e76326 | 2804 | |
cc192b50 | 2805 | if (getsockname(fd, AI->ai_addr, &AI->ai_addrlen)) { |
2806 | ipa.FreeAddrInfo(AI); | |
a689bd4e | 2807 | debugs(9, DBG_CRITICAL, HERE << "getsockname(" << fd << ",..): " << xstrerror()); |
bf8fe701 | 2808 | |
62e76326 | 2809 | /* XXX Need to set error message */ |
2810 | ftpFail(ftpState); | |
2811 | return; | |
cdc33f35 | 2812 | } |
62e76326 | 2813 | |
9e008dda | 2814 | if ( AI->ai_addrlen != sizeof(struct sockaddr_in) ) { |
cc192b50 | 2815 | ipa.FreeAddrInfo(AI); |
9e008dda AJ |
2816 | /* IPv6 CANNOT send PORT command. */ |
2817 | /* we got here by attempting and failing an EPRT */ | |
2818 | /* using the same reply code should simulate a PORT failure */ | |
2819 | ftpReadPORT(ftpState); | |
2820 | return; | |
cc192b50 | 2821 | } |
cc192b50 | 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; | |
b7ac5457 | 2855 | Ip::Address 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 | ||
b7ac5457 | 2868 | Ip::Address::InitAddrInfo(AI); |
cc192b50 | 2869 | |
2870 | if (getsockname(fd, AI->ai_addr, &AI->ai_addrlen)) { | |
b7ac5457 | 2871 | Ip::Address::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 | ||
b7ac5457 | 2891 | Ip::Address::FreeAddrInfo(AI); |
cc192b50 | 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 | 2948 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; |
4299f876 AR |
2949 | AsyncCall::Pointer acceptCall = JobCallback(11, 5, |
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; |
4299f876 AR |
2980 | AsyncCall::Pointer timeoutCall = JobCallback(9, 5, |
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 | 3072 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
4299f876 AR |
3073 | AsyncCall::Pointer timeoutCall = JobCallback(9, 5, |
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; | |
4299f876 AR |
3085 | AsyncCall::Pointer acceptCall = JobCallback(11, 5, |
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 | 3220 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; |
4299f876 AR |
3221 | AsyncCall::Pointer acceptCall = JobCallback(11, 5, |
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; | |
4299f876 AR |
3233 | AsyncCall::Pointer timeoutCall = JobCallback(9, 5, |
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 | 3282 | typedef CommCbMemFunT<FtpStateData, CommAcceptCbParams> acceptDialer; |
4299f876 AR |
3283 | AsyncCall::Pointer acceptCall = JobCallback(11, 5, |
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 | 3293 | typedef CommCbMemFunT<FtpStateData, CommTimeoutCbParams> TimeoutDialer; |
4299f876 AR |
3294 | AsyncCall::Pointer timeoutCall = JobCallback(9, 5, |
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; | |
f0324b7f | 3320 | ferr->ftp.cwd_msg = xstrdup(cwd_message.size()? cwd_message.termedBuf() : ""); |
0477a072 AJ |
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; | |
4299f876 | 3929 | return JobCallback(9, 5, Dialer, this, FtpStateData::dataClosed); |
94b88585 AR |
3930 | } |
3931 | ||
3932 | /// configures the channel with a descriptor and registers a close handler | |
3933 | void | |
3934 | FtpChannel::opened(int aFd, const AsyncCall::Pointer &aCloser) | |
3935 | { | |
3936 | assert(fd < 0); | |
3937 | assert(closer == NULL); | |
3938 | ||
3939 | assert(aFd >= 0); | |
3940 | assert(aCloser != NULL); | |
3941 | ||
3942 | fd = aFd; | |
3943 | closer = aCloser; | |
3944 | comm_add_close_handler(fd, closer); | |
3945 | } | |
3946 | ||
3947 | /// planned close: removes the close handler and calls comm_close | |
3948 | void | |
3949 | FtpChannel::close() | |
3950 | { | |
04f55905 AJ |
3951 | // channels with active listeners will be closed when the listener handler dies. |
3952 | if (listener) { | |
3953 | delete listener; | |
3954 | listener = NULL; | |
3955 | comm_remove_close_handler(fd, closer); | |
3956 | closer = NULL; | |
3957 | fd = -1; | |
97b8ac39 | 3958 | } else if (fd >= 0) { |
94b88585 AR |
3959 | comm_remove_close_handler(fd, closer); |
3960 | closer = NULL; | |
3961 | comm_close(fd); // we do not expect to be called back | |
3962 | fd = -1; | |
3963 | } | |
3964 | } | |
3965 | ||
3966 | /// just resets fd and close handler | |
3967 | void | |
3968 | FtpChannel::clear() | |
3969 | { | |
3970 | fd = -1; | |
3971 | closer = NULL; | |
3972 | } |