]>
Commit | Line | Data |
---|---|---|
be335c22 | 1 | |
30a4f2a8 | 2 | /* |
702fb6b9 | 3 | * $Id: ftp.cc,v 1.306 2001/01/08 23:32:21 wessels Exp $ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 9 File Transfer Protocol (FTP) | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
42c04c16 | 8 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
e25c139f | 9 | * ---------------------------------------------------------- |
30a4f2a8 | 10 | * |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
e25c139f | 13 | * National Laboratory for Applied Network Research and funded by the |
14 | * National Science Foundation. Squid is Copyrighted (C) 1998 by | |
efd900cb | 15 | * the Regents of the University of California. Please see the |
16 | * COPYRIGHT file for full details. Squid incorporates software | |
17 | * developed and/or copyrighted by other sources. Please see the | |
18 | * CREDITS file for full details. | |
30a4f2a8 | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
24 | * | |
25 | * This program is distributed in the hope that it will be useful, | |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
29 | * | |
30 | * You should have received a copy of the GNU General Public License | |
31 | * along with this program; if not, write to the Free Software | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
30a4f2a8 | 34 | */ |
019dd986 | 35 | |
44a47c6e | 36 | #include "squid.h" |
090089c4 | 37 | |
3fdadc70 | 38 | static const char *const crlf = "\r\n"; |
39 | static char cbuf[1024]; | |
40 | ||
41 | typedef enum { | |
42 | BEGIN, | |
43 | SENT_USER, | |
44 | SENT_PASS, | |
45 | SENT_TYPE, | |
46 | SENT_MDTM, | |
47 | SENT_SIZE, | |
48 | SENT_PORT, | |
49 | SENT_PASV, | |
50 | SENT_CWD, | |
51 | SENT_LIST, | |
52 | SENT_NLST, | |
53 | SENT_REST, | |
54 | SENT_RETR, | |
54220df8 | 55 | SENT_STOR, |
3fdadc70 | 56 | SENT_QUIT, |
54220df8 | 57 | READING_DATA, |
58 | WRITING_DATA, | |
59 | SENT_MKDIR | |
3fdadc70 | 60 | } ftp_state_t; |
090089c4 | 61 | |
e55f0142 | 62 | struct _ftp_flags { |
1ecaa0a0 | 63 | unsigned int isdir:1; |
64 | unsigned int pasv_supported:1; | |
65 | unsigned int skip_whitespace:1; | |
66 | unsigned int rest_supported:1; | |
67 | unsigned int pasv_only:1; | |
68 | unsigned int authenticated:1; | |
69 | unsigned int http_header_sent:1; | |
70 | unsigned int tried_nlst:1; | |
71 | unsigned int use_base:1; | |
72 | unsigned int root_dir:1; | |
73 | unsigned int no_dotdot:1; | |
74 | unsigned int html_header_sent:1; | |
75 | unsigned int binary:1; | |
76 | unsigned int try_slash_hack:1; | |
77 | unsigned int put:1; | |
78 | unsigned int put_mkdir:1; | |
79 | unsigned int listformat_unknown:1; | |
80 | unsigned int datachannel_hack:1; | |
e55f0142 | 81 | }; |
82 | ||
090089c4 | 83 | typedef struct _Ftpdata { |
84 | StoreEntry *entry; | |
983061ed | 85 | request_t *request; |
77a30ebb | 86 | char user[MAX_URL]; |
87 | char password[MAX_URL]; | |
9bc73deb | 88 | int password_url; |
f0afe435 | 89 | char *reply_hdr; |
f0afe435 | 90 | int reply_hdr_state; |
3fdadc70 | 91 | char *title_url; |
9bc73deb | 92 | char *base_href; |
3fdadc70 | 93 | int conn_att; |
94 | int login_att; | |
95 | ftp_state_t state; | |
3fdadc70 | 96 | time_t mdtm; |
97 | int size; | |
3fdadc70 | 98 | wordlist *pathcomps; |
99 | char *filepath; | |
100 | int restart_offset; | |
cfbf5373 | 101 | int restarted_offset; |
3fdadc70 | 102 | int rest_att; |
103 | char *proxy_host; | |
104 | size_t list_width; | |
105 | wordlist *cwd_message; | |
969c39b9 | 106 | char *old_request; |
107 | char *old_reply; | |
0f169992 | 108 | char *old_filepath; |
9e242e02 | 109 | char typecode; |
3fdadc70 | 110 | struct { |
111 | int fd; | |
112 | char *buf; | |
113 | size_t size; | |
114 | off_t offset; | |
115 | FREE *freefunc; | |
116 | wordlist *message; | |
b9916917 | 117 | char *last_command; |
118 | char *last_reply; | |
3fdadc70 | 119 | int replycode; |
120 | } ctrl; | |
121 | struct { | |
122 | int fd; | |
123 | char *buf; | |
124 | size_t size; | |
125 | off_t offset; | |
126 | FREE *freefunc; | |
9b312a19 | 127 | char *host; |
128 | u_short port; | |
3fdadc70 | 129 | } data; |
e55f0142 | 130 | struct _ftp_flags flags; |
db1cd23c | 131 | FwdState *fwd; |
e5f6c5c2 | 132 | } FtpStateData; |
090089c4 | 133 | |
3fdadc70 | 134 | typedef struct { |
135 | char type; | |
136 | int size; | |
137 | char *date; | |
138 | char *name; | |
139 | char *showname; | |
140 | char *link; | |
141 | } ftpListParts; | |
142 | ||
ea3a2a69 | 143 | typedef void (FTPSM) (FtpStateData *); |
0a0bf5db | 144 | |
c68e9c6b | 145 | #define FTP_LOGIN_ESCAPED 1 |
146 | #define FTP_LOGIN_NOT_ESCAPED 0 | |
147 | ||
983061ed | 148 | /* Local functions */ |
3fdadc70 | 149 | static CNCB ftpPasvCallback; |
dbfed404 | 150 | static PF ftpDataRead; |
94439e4e | 151 | static PF ftpDataWrite; |
152 | static CWCB ftpDataWriteCallback; | |
9e4ad609 | 153 | static PF ftpStateFree; |
5c5783a2 | 154 | static PF ftpTimeout; |
3fdadc70 | 155 | static PF ftpReadControlReply; |
156 | static CWCB ftpWriteCommandCallback; | |
c68e9c6b | 157 | static void ftpLoginParser(const char *, FtpStateData *, int escaped); |
4f310655 | 158 | static wordlist *ftpParseControlReply(char *, size_t, int *, int *); |
cfbf5373 | 159 | static int ftpRestartable(FtpStateData * ftpState); |
f5b8bbc4 | 160 | static void ftpAppendSuccessHeader(FtpStateData * ftpState); |
ee1679df | 161 | static void ftpAuthRequired(HttpReply * reply, request_t * request, const char *realm); |
969c39b9 | 162 | static void ftpHackShortcut(FtpStateData * ftpState, FTPSM * nextState); |
0f169992 | 163 | static void ftpUnhack(FtpStateData * ftpState); |
05491dc4 | 164 | static void ftpScheduleReadControlReply(FtpStateData *, int); |
4f310655 | 165 | static void ftpHandleControlReply(FtpStateData *); |
ee16d4ab | 166 | static char *ftpHtmlifyListEntry(char *line, FtpStateData * ftpState); |
7e3ce7b9 | 167 | static void ftpFailed(FtpStateData *, err_type); |
168 | static void ftpFailedErrorMessage(FtpStateData *, err_type); | |
983061ed | 169 | |
7e3ce7b9 | 170 | /* |
171 | * State machine functions | |
969c39b9 | 172 | * send == state transition |
173 | * read == wait for response, and select next state transition | |
dbfed404 | 174 | * other == Transition logic |
969c39b9 | 175 | */ |
3fdadc70 | 176 | static FTPSM ftpReadWelcome; |
969c39b9 | 177 | static FTPSM ftpSendUser; |
3fdadc70 | 178 | static FTPSM ftpReadUser; |
969c39b9 | 179 | static FTPSM ftpSendPass; |
3fdadc70 | 180 | static FTPSM ftpReadPass; |
969c39b9 | 181 | static FTPSM ftpSendType; |
3fdadc70 | 182 | static FTPSM ftpReadType; |
969c39b9 | 183 | static FTPSM ftpSendMdtm; |
3fdadc70 | 184 | static FTPSM ftpReadMdtm; |
969c39b9 | 185 | static FTPSM ftpSendSize; |
3fdadc70 | 186 | static FTPSM ftpReadSize; |
969c39b9 | 187 | static FTPSM ftpSendPort; |
3fdadc70 | 188 | static FTPSM ftpReadPort; |
969c39b9 | 189 | static FTPSM ftpSendPasv; |
3fdadc70 | 190 | static FTPSM ftpReadPasv; |
dbfed404 | 191 | static FTPSM ftpTraverseDirectory; |
192 | static FTPSM ftpListDir; | |
193 | static FTPSM ftpGetFile; | |
969c39b9 | 194 | static FTPSM ftpSendCwd; |
3fdadc70 | 195 | static FTPSM ftpReadCwd; |
94439e4e | 196 | static FTPSM ftpRestOrList; |
969c39b9 | 197 | static FTPSM ftpSendList; |
198 | static FTPSM ftpSendNlst; | |
3fdadc70 | 199 | static FTPSM ftpReadList; |
969c39b9 | 200 | static FTPSM ftpSendRest; |
3fdadc70 | 201 | static FTPSM ftpReadRest; |
969c39b9 | 202 | static FTPSM ftpSendRetr; |
3fdadc70 | 203 | static FTPSM ftpReadRetr; |
204 | static FTPSM ftpReadTransferDone; | |
54220df8 | 205 | static FTPSM ftpSendStor; |
206 | static FTPSM ftpReadStor; | |
94439e4e | 207 | static FTPSM ftpWriteTransferDone; |
54220df8 | 208 | static FTPSM ftpSendReply; |
94439e4e | 209 | static FTPSM ftpSendMkdir; |
54220df8 | 210 | static FTPSM ftpReadMkdir; |
94439e4e | 211 | static FTPSM ftpFail; |
212 | static FTPSM ftpSendQuit; | |
213 | static FTPSM ftpReadQuit; | |
dbfed404 | 214 | /************************************************ |
215 | ** State Machine Description (excluding hacks) ** | |
216 | ************************************************* | |
217 | From To | |
218 | --------------------------------------- | |
219 | Welcome User | |
220 | User Pass | |
221 | Pass Type | |
222 | Type TraverseDirectory / GetFile | |
223 | TraverseDirectory Cwd / GetFile / ListDir | |
94439e4e | 224 | Cwd TraverseDirectory / Mkdir |
dbfed404 | 225 | GetFile Mdtm |
226 | Mdtm Size | |
227 | Size Pasv | |
228 | ListDir Pasv | |
94439e4e | 229 | Pasv FileOrList |
230 | FileOrList Rest / Retr / Nlst / List / Mkdir (PUT /xxx;type=d) | |
dbfed404 | 231 | Rest Retr |
94439e4e | 232 | Retr / Nlst / List DataRead* (on datachannel) |
233 | DataRead* ReadTransferDone | |
dbfed404 | 234 | ReadTransferDone DataTransferDone |
94439e4e | 235 | Stor DataWrite* (on datachannel) |
236 | DataWrite* RequestPutBody** (from client) | |
237 | RequestPutBody** DataWrite* / WriteTransferDone | |
238 | WriteTransferDone DataTransferDone | |
dbfed404 | 239 | DataTransferDone Quit |
240 | Quit - | |
241 | ************************************************/ | |
3fdadc70 | 242 | |
243 | FTPSM *FTP_SM_FUNCS[] = | |
244 | { | |
9bc73deb | 245 | ftpReadWelcome, /* BEGIN */ |
246 | ftpReadUser, /* SENT_USER */ | |
247 | ftpReadPass, /* SENT_PASS */ | |
248 | ftpReadType, /* SENT_TYPE */ | |
249 | ftpReadMdtm, /* SENT_MDTM */ | |
250 | ftpReadSize, /* SENT_SIZE */ | |
251 | ftpReadPort, /* SENT_PORT */ | |
252 | ftpReadPasv, /* SENT_PASV */ | |
253 | ftpReadCwd, /* SENT_CWD */ | |
3fdadc70 | 254 | ftpReadList, /* SENT_LIST */ |
255 | ftpReadList, /* SENT_NLST */ | |
9bc73deb | 256 | ftpReadRest, /* SENT_REST */ |
257 | ftpReadRetr, /* SENT_RETR */ | |
258 | ftpReadStor, /* SENT_STOR */ | |
259 | ftpReadQuit, /* SENT_QUIT */ | |
260 | ftpReadTransferDone, /* READING_DATA (RETR,LIST,NLST) */ | |
94439e4e | 261 | ftpWriteTransferDone, /* WRITING_DATA (STOR) */ |
262 | ftpSendReply, /* WRITTEN_DATA? (STOR) */ | |
9bc73deb | 263 | ftpReadMkdir /* SENT_MKDIR */ |
3fdadc70 | 264 | }; |
e381a13d | 265 | |
582b6456 | 266 | static void |
79d39a72 | 267 | ftpStateFree(int fdnotused, void *data) |
ba718c8f | 268 | { |
582b6456 | 269 | FtpStateData *ftpState = data; |
51fa90db | 270 | if (ftpState == NULL) |
582b6456 | 271 | return; |
c3b838d4 | 272 | debug(9, 3) ("ftpStateFree: %s\n", storeUrl(ftpState->entry)); |
bfcaf585 | 273 | storeUnregisterAbort(ftpState->entry); |
f88211e8 | 274 | storeUnlockObject(ftpState->entry); |
51fa90db | 275 | if (ftpState->reply_hdr) { |
db1cd23c | 276 | memFree(ftpState->reply_hdr, MEM_8K_BUF); |
1ecaa0a0 | 277 | /* this seems unnecessary, but people report SEGV's |
278 | * when freeing memory in this function */ | |
51fa90db | 279 | ftpState->reply_hdr = NULL; |
f0afe435 | 280 | } |
30a4f2a8 | 281 | requestUnlink(ftpState->request); |
1ecaa0a0 | 282 | if (ftpState->ctrl.buf) { |
3fdadc70 | 283 | ftpState->ctrl.freefunc(ftpState->ctrl.buf); |
1ecaa0a0 | 284 | /* this seems unnecessary, but people report SEGV's |
285 | * when freeing memory in this function */ | |
286 | ftpState->ctrl.buf = NULL; | |
287 | } | |
288 | if (ftpState->data.buf) { | |
3fdadc70 | 289 | ftpState->data.freefunc(ftpState->data.buf); |
1ecaa0a0 | 290 | /* this seems unnecessary, but people report SEGV's |
291 | * when freeing memory in this function */ | |
292 | ftpState->data.buf = NULL; | |
293 | } | |
3fdadc70 | 294 | if (ftpState->pathcomps) |
295 | wordlistDestroy(&ftpState->pathcomps); | |
296 | if (ftpState->ctrl.message) | |
297 | wordlistDestroy(&ftpState->ctrl.message); | |
298 | if (ftpState->cwd_message) | |
299 | wordlistDestroy(&ftpState->cwd_message); | |
b9916917 | 300 | safe_free(ftpState->ctrl.last_reply); |
301 | safe_free(ftpState->ctrl.last_command); | |
969c39b9 | 302 | safe_free(ftpState->old_request); |
303 | safe_free(ftpState->old_reply); | |
0f169992 | 304 | safe_free(ftpState->old_filepath); |
b5639035 | 305 | safe_free(ftpState->title_url); |
9bc73deb | 306 | safe_free(ftpState->base_href); |
b5639035 | 307 | safe_free(ftpState->filepath); |
9b312a19 | 308 | safe_free(ftpState->data.host); |
a0eff6be | 309 | if (ftpState->data.fd > -1) { |
d05283d9 | 310 | comm_close(ftpState->data.fd); |
311 | ftpState->data.fd = -1; | |
a0eff6be | 312 | } |
7dd44885 | 313 | cbdataFree(ftpState); |
ba718c8f | 314 | } |
315 | ||
b8d8561b | 316 | static void |
c68e9c6b | 317 | ftpLoginParser(const char *login, FtpStateData * ftpState, int escaped) |
090089c4 | 318 | { |
983061ed | 319 | char *s = NULL; |
582b6456 | 320 | xstrncpy(ftpState->user, login, MAX_URL); |
321 | if ((s = strchr(ftpState->user, ':'))) { | |
983061ed | 322 | *s = 0; |
582b6456 | 323 | xstrncpy(ftpState->password, s + 1, MAX_URL); |
c68e9c6b | 324 | if (escaped) |
325 | rfc1738_unescape(ftpState->password); | |
9bc73deb | 326 | ftpState->password_url = 1; |
983061ed | 327 | } else { |
582b6456 | 328 | xstrncpy(ftpState->password, null_string, MAX_URL); |
983061ed | 329 | } |
c68e9c6b | 330 | if (escaped) |
331 | rfc1738_unescape(ftpState->user); | |
582b6456 | 332 | if (ftpState->user[0] || ftpState->password[0]) |
429fdbec | 333 | return; |
582b6456 | 334 | xstrncpy(ftpState->user, "anonymous", MAX_URL); |
e34e0322 | 335 | xstrncpy(ftpState->password, Config.Ftp.anon_user, MAX_URL); |
090089c4 | 336 | } |
337 | ||
24382924 | 338 | static void |
5c5783a2 | 339 | ftpTimeout(int fd, void *data) |
090089c4 | 340 | { |
582b6456 | 341 | FtpStateData *ftpState = data; |
342 | StoreEntry *entry = ftpState->entry; | |
9fb13bb6 | 343 | debug(9, 4) ("ftpTimeout: FD %d: '%s'\n", fd, storeUrl(entry)); |
7e3ce7b9 | 344 | if (SENT_PASV == ftpState->state && fd == ftpState->data.fd) { |
345 | /* stupid ftp.netscape.com */ | |
346 | ftpState->fwd->flags.dont_retry = 0; | |
347 | ftpState->fwd->flags.ftp_pasv_failed = 1; | |
348 | debug(9, 1) ("ftpTimeout: timeout in SENT_PASV state\n"); | |
349 | } | |
9bc73deb | 350 | ftpFailed(ftpState, ERR_READ_TIMEOUT); |
351 | /* ftpFailed closes ctrl.fd and frees ftpState */ | |
090089c4 | 352 | } |
353 | ||
3fdadc70 | 354 | static void |
355 | ftpListingStart(FtpStateData * ftpState) | |
356 | { | |
357 | StoreEntry *e = ftpState->entry; | |
358 | wordlist *w; | |
ee16d4ab | 359 | char *dirup; |
6e5ae4a4 | 360 | int i, j, k; |
10270faa | 361 | char *title; |
438fc1e3 | 362 | storeBuffer(e); |
3fdadc70 | 363 | storeAppendPrintf(e, "<!-- HTML listing generated by Squid %s -->\n", |
364 | version_string); | |
365 | storeAppendPrintf(e, "<!-- %s -->\n", mkrfc1123(squid_curtime)); | |
366 | storeAppendPrintf(e, "<HTML><HEAD><TITLE>\n"); | |
367 | storeAppendPrintf(e, "FTP Directory: %s\n", | |
10270faa | 368 | html_quote(ftpState->title_url)); |
3fdadc70 | 369 | storeAppendPrintf(e, "</TITLE>\n"); |
e55f0142 | 370 | if (ftpState->flags.use_base) |
3fdadc70 | 371 | storeAppendPrintf(e, "<BASE HREF=\"%s\">\n", |
10270faa | 372 | html_quote(ftpState->base_href)); |
3fdadc70 | 373 | storeAppendPrintf(e, "</HEAD><BODY>\n"); |
374 | if (ftpState->cwd_message) { | |
375 | storeAppendPrintf(e, "<PRE>\n"); | |
376 | for (w = ftpState->cwd_message; w; w = w->next) | |
10270faa | 377 | storeAppendPrintf(e, "%s\n", html_quote(w->key)); |
3fdadc70 | 378 | storeAppendPrintf(e, "</PRE>\n"); |
379 | storeAppendPrintf(e, "<HR>\n"); | |
380 | wordlistDestroy(&ftpState->cwd_message); | |
381 | } | |
382 | storeAppendPrintf(e, "<H2>\n"); | |
6e5ae4a4 | 383 | storeAppendPrintf(e, "FTP Directory: "); |
384 | /* "ftp://" == 6 characters */ | |
385 | assert(strlen(ftpState->title_url) >= 6); | |
10270faa | 386 | title = html_quote(ftpState->title_url); |
387 | for (i = 6, j = 0; title[i]; j = i) { | |
6e5ae4a4 | 388 | storeAppendPrintf(e, "<A HREF=\""); |
10270faa | 389 | i += strcspn(&title[i], "/"); |
390 | if (title[i] == '/') | |
6e5ae4a4 | 391 | i++; |
392 | for (k = 0; k < i; k++) | |
10270faa | 393 | storeAppendPrintf(e, "%c", title[k]); |
6e5ae4a4 | 394 | storeAppendPrintf(e, "\">"); |
395 | for (k = j; k < i - 1; k++) | |
10270faa | 396 | storeAppendPrintf(e, "%c", title[k]); |
6e5ae4a4 | 397 | if (ftpState->title_url[k] != '/') |
10270faa | 398 | storeAppendPrintf(e, "%c", title[k++]); |
6e5ae4a4 | 399 | storeAppendPrintf(e, "</A>"); |
400 | if (k < i) | |
10270faa | 401 | storeAppendPrintf(e, "%c", title[k++]); |
6e5ae4a4 | 402 | if (i == j) { |
403 | /* Error guard, or "assert" */ | |
404 | storeAppendPrintf(e, "ERROR: Failed to parse URL: %s\n", | |
10270faa | 405 | html_quote(ftpState->title_url)); |
6e5ae4a4 | 406 | debug(9, 0) ("Failed to parse URL: %s\n", ftpState->title_url); |
407 | break; | |
408 | } | |
409 | } | |
3fdadc70 | 410 | storeAppendPrintf(e, "</H2>\n"); |
411 | storeAppendPrintf(e, "<PRE>\n"); | |
ee16d4ab | 412 | dirup = ftpHtmlifyListEntry("<internal-dirup>", ftpState); |
413 | storeAppend(e, dirup, strlen(dirup)); | |
438fc1e3 | 414 | storeBufferFlush(e); |
e55f0142 | 415 | ftpState->flags.html_header_sent = 1; |
3fdadc70 | 416 | } |
417 | ||
418 | static void | |
419 | ftpListingFinish(FtpStateData * ftpState) | |
420 | { | |
421 | StoreEntry *e = ftpState->entry; | |
438fc1e3 | 422 | storeBuffer(e); |
3fdadc70 | 423 | storeAppendPrintf(e, "</PRE>\n"); |
e55f0142 | 424 | if (ftpState->flags.listformat_unknown && !ftpState->flags.tried_nlst) { |
dbfed404 | 425 | storeAppendPrintf(e, "<A HREF=\"./;type=d\">[As plain directory]</A>\n"); |
0e473d70 | 426 | } else if (ftpState->typecode == 'D') { |
dbfed404 | 427 | storeAppendPrintf(e, "<A HREF=\"./\">[As extended directory]</A>\n"); |
428 | } | |
3fdadc70 | 429 | storeAppendPrintf(e, "<HR>\n"); |
430 | storeAppendPrintf(e, "<ADDRESS>\n"); | |
d20b1cd0 | 431 | storeAppendPrintf(e, "Generated %s by %s (%s)\n", |
3fdadc70 | 432 | mkrfc1123(squid_curtime), |
a15705d0 | 433 | getMyHostname(), |
d20b1cd0 | 434 | full_appname_string); |
3fdadc70 | 435 | storeAppendPrintf(e, "</ADDRESS></BODY></HTML>\n"); |
438fc1e3 | 436 | storeBufferFlush(e); |
3fdadc70 | 437 | } |
438 | ||
439 | static const char *Month[] = | |
440 | { | |
441 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
442 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
443 | }; | |
444 | ||
445 | static int | |
446 | is_month(const char *buf) | |
447 | { | |
448 | int i; | |
449 | for (i = 0; i < 12; i++) | |
450 | if (!strcasecmp(buf, Month[i])) | |
451 | return 1; | |
452 | return 0; | |
453 | } | |
454 | ||
455 | ||
456 | static void | |
457 | ftpListPartsFree(ftpListParts ** parts) | |
458 | { | |
459 | safe_free((*parts)->date); | |
460 | safe_free((*parts)->name); | |
461 | safe_free((*parts)->showname); | |
462 | safe_free((*parts)->link); | |
463 | safe_free(*parts); | |
464 | } | |
465 | ||
466 | #define MAX_TOKENS 64 | |
467 | ||
75e5a32e | 468 | #define SCAN_FTP1 "%[0123456789]" |
469 | #define SCAN_FTP2 "%[0123456789:]" | |
470 | #define SCAN_FTP3 "%[0123456789]-%[0123456789]-%[0123456789]" | |
471 | #define SCAN_FTP4 "%[0123456789]:%[0123456789]%[AaPp]%[Mm]" | |
472 | ||
3fdadc70 | 473 | static ftpListParts * |
e55f0142 | 474 | ftpListParseParts(const char *buf, struct _ftp_flags flags) |
3fdadc70 | 475 | { |
476 | ftpListParts *p = NULL; | |
477 | char *t = NULL; | |
478 | const char *ct = NULL; | |
479 | char *tokens[MAX_TOKENS]; | |
480 | int i; | |
481 | int n_tokens; | |
482 | static char sbuf[128]; | |
483 | char *xbuf = NULL; | |
484 | if (buf == NULL) | |
485 | return NULL; | |
486 | if (*buf == '\0') | |
487 | return NULL; | |
488 | p = xcalloc(1, sizeof(ftpListParts)); | |
489 | n_tokens = 0; | |
490 | for (i = 0; i < MAX_TOKENS; i++) | |
491 | tokens[i] = (char *) NULL; | |
492 | xbuf = xstrdup(buf); | |
e55f0142 | 493 | if (flags.tried_nlst) { |
dbfed404 | 494 | /* Machine readable format, one name per line */ |
0e473d70 | 495 | p->name = xbuf; |
496 | p->type = '\0'; | |
dbfed404 | 497 | return p; |
498 | } | |
3fdadc70 | 499 | for (t = strtok(xbuf, w_space); t && n_tokens < MAX_TOKENS; t = strtok(NULL, w_space)) |
500 | tokens[n_tokens++] = xstrdup(t); | |
501 | xfree(xbuf); | |
502 | /* locate the Month field */ | |
b6a2f15e | 503 | for (i = 3; i < n_tokens - 2; i++) { |
3fdadc70 | 504 | if (!is_month(tokens[i])) /* Month */ |
505 | continue; | |
75e5a32e | 506 | if (!sscanf(tokens[i - 1], SCAN_FTP1, sbuf)) /* Size */ |
3fdadc70 | 507 | continue; |
75e5a32e | 508 | if (!sscanf(tokens[i + 1], SCAN_FTP1, sbuf)) /* Day */ |
3fdadc70 | 509 | continue; |
75e5a32e | 510 | if (!sscanf(tokens[i + 2], SCAN_FTP2, sbuf)) /* Yr | hh:mm */ |
3fdadc70 | 511 | continue; |
512 | p->type = *tokens[0]; | |
513 | p->size = atoi(tokens[i - 1]); | |
042461c3 | 514 | snprintf(sbuf, 128, "%s %2s %5s", |
3fdadc70 | 515 | tokens[i], tokens[i + 1], tokens[i + 2]); |
516 | if (!strstr(buf, sbuf)) | |
042461c3 | 517 | snprintf(sbuf, 128, "%s %2s %-5s", |
3fdadc70 | 518 | tokens[i], tokens[i + 1], tokens[i + 2]); |
519 | if ((t = strstr(buf, sbuf))) { | |
520 | p->date = xstrdup(sbuf); | |
e55f0142 | 521 | if (flags.skip_whitespace) { |
3fdadc70 | 522 | t += strlen(sbuf); |
523 | while (strchr(w_space, *t)) | |
524 | t++; | |
525 | } else { | |
526 | /* XXX assumes a single space between date and filename | |
527 | * suggested by: Nathan.Bailey@cc.monash.edu.au and | |
528 | * Mike Battersby <mike@starbug.bofh.asn.au> */ | |
529 | t += strlen(sbuf) + 1; | |
530 | } | |
531 | p->name = xstrdup(t); | |
532 | if ((t = strstr(p->name, " -> "))) { | |
533 | *t = '\0'; | |
534 | p->link = xstrdup(t + 4); | |
535 | } | |
536 | } | |
537 | break; | |
538 | } | |
539 | /* try it as a DOS listing */ | |
540 | if (n_tokens > 3 && p->name == NULL && | |
75e5a32e | 541 | sscanf(tokens[0], SCAN_FTP3, sbuf, sbuf, sbuf) == 3 && |
3fdadc70 | 542 | /* 04-05-70 */ |
75e5a32e | 543 | sscanf(tokens[1], SCAN_FTP4, sbuf, sbuf, sbuf, sbuf) == 4) { |
3fdadc70 | 544 | /* 09:33PM */ |
545 | if (!strcasecmp(tokens[2], "<dir>")) { | |
546 | p->type = 'd'; | |
547 | } else { | |
548 | p->type = '-'; | |
549 | p->size = atoi(tokens[2]); | |
550 | } | |
042461c3 | 551 | snprintf(sbuf, 128, "%s %s", tokens[0], tokens[1]); |
3fdadc70 | 552 | p->date = xstrdup(sbuf); |
20f88067 | 553 | if (p->type == 'd') { |
554 | /* Directory.. name begins with first printable after <dir> */ | |
555 | ct = strstr(buf, tokens[2]); | |
556 | ct += strlen(tokens[2]); | |
b6a2f15e | 557 | while (xisspace(*ct)) |
20f88067 | 558 | ct++; |
c079f69d | 559 | if (!*ct) |
20f88067 | 560 | ct = NULL; |
561 | } else { | |
562 | /* A file. Name begins after size, with a space in between */ | |
563 | snprintf(sbuf, 128, " %s %s", tokens[2], tokens[3]); | |
564 | ct = strstr(buf, sbuf); | |
565 | if (ct) { | |
566 | ct += strlen(tokens[2]) + 2; | |
567 | } | |
568 | } | |
569 | p->name = xstrdup(ct ? ct : tokens[3]); | |
3fdadc70 | 570 | } |
571 | /* Try EPLF format; carson@lehman.com */ | |
572 | if (p->name == NULL && buf[0] == '+') { | |
573 | ct = buf + 1; | |
574 | p->type = 0; | |
575 | while (ct && *ct) { | |
efd900cb | 576 | long lt; |
577 | time_t t; | |
3fdadc70 | 578 | switch (*ct) { |
579 | case '\t': | |
580 | sscanf(ct + 1, "%[^,]", sbuf); | |
581 | p->name = xstrdup(sbuf); | |
582 | break; | |
583 | case 's': | |
584 | sscanf(ct + 1, "%d", &(p->size)); | |
585 | break; | |
586 | case 'm': | |
efd900cb | 587 | if (1 != sscanf(ct + 1, "%ld", <)) |
588 | break; | |
589 | t = lt; | |
590 | p->date = xstrdup(ctime(&t)); | |
3fdadc70 | 591 | *(strstr(p->date, "\n")) = '\0'; |
592 | break; | |
593 | case '/': | |
594 | p->type = 'd'; | |
595 | break; | |
596 | case 'r': | |
597 | p->type = '-'; | |
598 | break; | |
599 | case 'i': | |
600 | break; | |
601 | default: | |
602 | break; | |
603 | } | |
604 | ct = strstr(ct, ","); | |
605 | if (ct) { | |
606 | ct++; | |
607 | } | |
608 | } | |
609 | if (p->type == 0) { | |
610 | p->type = '-'; | |
611 | } | |
612 | } | |
613 | for (i = 0; i < n_tokens; i++) | |
614 | xfree(tokens[i]); | |
b5639035 | 615 | if (p->name == NULL) |
616 | ftpListPartsFree(&p); | |
3fdadc70 | 617 | return p; |
618 | } | |
619 | ||
620 | static const char * | |
621 | dots_fill(size_t len) | |
622 | { | |
623 | static char buf[256]; | |
624 | int i = 0; | |
625 | if (len > Config.Ftp.list_width) { | |
626 | memset(buf, ' ', 256); | |
627 | buf[0] = '\n'; | |
628 | buf[Config.Ftp.list_width + 4] = '\0'; | |
629 | return buf; | |
630 | } | |
631 | for (i = (int) len; i < Config.Ftp.list_width; i++) | |
632 | buf[i - len] = (i % 2) ? '.' : ' '; | |
633 | buf[i - len] = '\0'; | |
634 | return buf; | |
635 | } | |
636 | ||
637 | static char * | |
dbfed404 | 638 | ftpHtmlifyListEntry(char *line, FtpStateData * ftpState) |
3fdadc70 | 639 | { |
3fdadc70 | 640 | LOCAL_ARRAY(char, icon, 2048); |
2a1ca944 | 641 | LOCAL_ARRAY(char, href, 2048 + 40); |
642 | LOCAL_ARRAY(char, text, 2048); | |
643 | LOCAL_ARRAY(char, size, 2048); | |
644 | LOCAL_ARRAY(char, chdir, 2048 + 40); | |
645 | LOCAL_ARRAY(char, view, 2048 + 40); | |
646 | LOCAL_ARRAY(char, download, 2048 + 40); | |
647 | LOCAL_ARRAY(char, link, 2048 + 40); | |
3fdadc70 | 648 | LOCAL_ARRAY(char, html, 8192); |
3fdadc70 | 649 | size_t width = Config.Ftp.list_width; |
650 | ftpListParts *parts; | |
0cdcddb9 | 651 | *icon = *href = *text = *size = *chdir = *view = *download = *link = *html = '\0'; |
69e81830 | 652 | if ((int) strlen(line) > 1024) { |
042461c3 | 653 | snprintf(html, 8192, "%s\n", line); |
3fdadc70 | 654 | return html; |
655 | } | |
ee16d4ab | 656 | /* Handle builtin <dirup> */ |
10270faa | 657 | if (strcmp(line, "<internal-dirup>") == 0) { |
2a1ca944 | 658 | /* <A HREF="{href}">{icon}</A> <A HREF="{href}">{text}</A> {link} */ |
4162ee3b | 659 | snprintf(icon, 2048, "<IMG BORDER=0 SRC=\"%s\" ALT=\"%-6s\">", |
660 | mimeGetIconURL("internal-dirup"), | |
661 | "[DIRUP]"); | |
e55f0142 | 662 | if (!ftpState->flags.no_dotdot && !ftpState->flags.root_dir) { |
13e80e5b | 663 | /* Normal directory */ |
2a1ca944 | 664 | strcpy(href, "../"); |
665 | strcpy(text, "Parent Directory"); | |
e55f0142 | 666 | } else if (!ftpState->flags.no_dotdot && ftpState->flags.root_dir) { |
13e80e5b | 667 | /* "Top level" directory */ |
2a1ca944 | 668 | strcpy(href, "%2e%2e/"); |
669 | strcpy(text, "Parent Directory"); | |
670 | snprintf(link, 2048, "(<A HREF=\"%s\">%s</A>)", | |
13e80e5b | 671 | "%2f/", |
672 | "Root Directory"); | |
e55f0142 | 673 | } else if (ftpState->flags.no_dotdot && !ftpState->flags.root_dir) { |
13e80e5b | 674 | /* Normal directory where last component is / or .. */ |
2a1ca944 | 675 | strcpy(href, "%2e%2e/"); |
676 | strcpy(text, "Parent Directory"); | |
677 | snprintf(link, 2048, "(<A HREF=\"%s\">%s</A>)", | |
13e80e5b | 678 | "../", |
2a1ca944 | 679 | "Back"); |
13e80e5b | 680 | } else { /* NO_DOTDOT && ROOT_DIR */ |
681 | /* "UNIX Root" directory */ | |
2a1ca944 | 682 | strcpy(href, "../"); |
683 | strcpy(text, "Home Directory"); | |
13e80e5b | 684 | } |
2a1ca944 | 685 | snprintf(html, 8192, "<A HREF=\"%s\">%s</A> <A HREF=\"%s\">%s</A> %s\n", |
0cdcddb9 | 686 | href, icon, href, text, link); |
ee16d4ab | 687 | return html; |
688 | } | |
689 | if ((parts = ftpListParseParts(line, ftpState->flags)) == NULL) { | |
690 | char *p; | |
691 | snprintf(html, 8192, "%s\n", line); | |
b6a2f15e | 692 | for (p = line; *p && xisspace(*p); p++); |
693 | if (*p && !xisspace(*p)) | |
ee16d4ab | 694 | ftpState->flags.listformat_unknown = 1; |
13e80e5b | 695 | return html; |
696 | } | |
3fdadc70 | 697 | if (!strcmp(parts->name, ".") || !strcmp(parts->name, "..")) { |
698 | *html = '\0'; | |
b5639035 | 699 | ftpListPartsFree(&parts); |
3fdadc70 | 700 | return html; |
701 | } | |
702 | parts->size += 1023; | |
703 | parts->size >>= 10; | |
704 | parts->showname = xstrdup(parts->name); | |
705 | if (!Config.Ftp.list_wrap) { | |
706 | if (strlen(parts->showname) > width - 1) { | |
707 | *(parts->showname + width - 1) = '>'; | |
708 | *(parts->showname + width - 0) = '\0'; | |
709 | } | |
710 | } | |
2a1ca944 | 711 | /* {icon} {text} . . . {date}{size}{chdir}{view}{download}{link}\n */ |
9bc73deb | 712 | xstrncpy(href, rfc1738_escape_part(parts->name), 2048); |
2a1ca944 | 713 | xstrncpy(text, parts->showname, 2048); |
3fdadc70 | 714 | switch (parts->type) { |
715 | case 'd': | |
2a1ca944 | 716 | snprintf(icon, 2048, "<IMG BORDER=0 SRC=\"%s\" ALT=\"%-6s\">", |
4162ee3b | 717 | mimeGetIconURL("internal-dir"), |
3fdadc70 | 718 | "[DIR]"); |
2a1ca944 | 719 | strncat(href, "/", 2048); |
3fdadc70 | 720 | break; |
721 | case 'l': | |
2a1ca944 | 722 | snprintf(icon, 2048, "<IMG BORDER=0 SRC=\"%s\" ALT=\"%-6s\">", |
4162ee3b | 723 | mimeGetIconURL("internal-link"), |
3fdadc70 | 724 | "[LINK]"); |
7b63416b | 725 | /* sometimes there is an 'l' flag, but no "->" link */ |
10270faa | 726 | if (parts->link) { |
727 | char *link2 = xstrdup(html_quote(rfc1738_escape(parts->link))); | |
2a1ca944 | 728 | snprintf(link, 2048, " -> <A HREF=\"%s\">%s</A>", |
10270faa | 729 | link2, |
730 | html_quote(parts->link)); | |
731 | safe_free(link2); | |
732 | } | |
3fdadc70 | 733 | break; |
dbfed404 | 734 | case '\0': |
2a1ca944 | 735 | snprintf(icon, 2048, "<IMG BORDER=0 SRC=\"%s\" ALT=\"%-6s\">", |
4162ee3b | 736 | mimeGetIconURL(parts->name), |
dbfed404 | 737 | "[UNKNOWN]"); |
2a1ca944 | 738 | snprintf(chdir, 2048, " <A HREF=\"%s/;type=d\"><IMG BORDER=0 SRC=\"%s\" " |
0cdcddb9 | 739 | "ALT=\"[DIR]\"></A>", |
10270faa | 740 | rfc1738_escape_part(parts->name), |
2a1ca944 | 741 | mimeGetIconURL("internal-dir")); |
dbfed404 | 742 | break; |
3fdadc70 | 743 | case '-': |
744 | default: | |
2a1ca944 | 745 | snprintf(icon, 2048, "<IMG BORDER=0 SRC=\"%s\" ALT=\"%-6s\">", |
4162ee3b | 746 | mimeGetIconURL(parts->name), |
3fdadc70 | 747 | "[FILE]"); |
2a1ca944 | 748 | snprintf(size, 2048, " %6dk", parts->size); |
3fdadc70 | 749 | break; |
750 | } | |
2a1ca944 | 751 | if (parts->type != 'd') { |
752 | if (mimeGetViewOption(parts->name)) { | |
753 | snprintf(view, 2048, " <A HREF=\"%s;type=a\"><IMG BORDER=0 SRC=\"%s\" " | |
0cdcddb9 | 754 | "ALT=\"[VIEW]\"></A>", |
2a1ca944 | 755 | href, mimeGetIconURL("internal-view")); |
756 | } | |
757 | if (mimeGetDownloadOption(parts->name)) { | |
758 | snprintf(download, 2048, " <A HREF=\"%s;type=i\"><IMG BORDER=0 SRC=\"%s\" " | |
0cdcddb9 | 759 | "ALT=\"[DOWNLOAD]\"></A>", |
2a1ca944 | 760 | href, mimeGetIconURL("internal-download")); |
761 | } | |
762 | } | |
763 | /* <A HREF="{href}">{icon}</A> <A HREF="{href}">{text}</A> . . . {date}{size}{chdir}{view}{download}{link}\n */ | |
764 | if (parts->type != '\0') { | |
765 | snprintf(html, 8192, "<A HREF=\"%s\">%s</A> <A HREF=\"%s\">%s</A>%s " | |
0cdcddb9 | 766 | "%s%8s%s%s%s%s\n", |
10270faa | 767 | href, icon, href, html_quote(text), dots_fill(strlen(text)), |
2a1ca944 | 768 | parts->date, size, chdir, view, download, link); |
769 | } else { | |
770 | /* Plain listing. {icon} {text} ... {chdir}{view}{download} */ | |
771 | snprintf(html, 8192, "<A HREF=\"%s\">%s</A> <A HREF=\"%s\">%s</A>%s " | |
0cdcddb9 | 772 | "%s%s%s%s\n", |
10270faa | 773 | href, icon, href, html_quote(text), dots_fill(strlen(text)), |
2a1ca944 | 774 | chdir, view, download, link); |
775 | } | |
3fdadc70 | 776 | ftpListPartsFree(&parts); |
3fdadc70 | 777 | return html; |
778 | } | |
779 | ||
780 | static void | |
4f310655 | 781 | ftpParseListing(FtpStateData * ftpState) |
3fdadc70 | 782 | { |
783 | char *buf = ftpState->data.buf; | |
7131112f | 784 | char *sbuf; /* NULL-terminated copy of buf */ |
b5639035 | 785 | char *end; |
786 | char *line; | |
3fdadc70 | 787 | char *s; |
788 | char *t; | |
789 | size_t linelen; | |
b5639035 | 790 | size_t usable; |
3fdadc70 | 791 | StoreEntry *e = ftpState->entry; |
4f310655 | 792 | int len = ftpState->data.offset; |
7131112f | 793 | /* |
794 | * We need a NULL-terminated buffer for scanning, ick | |
795 | */ | |
796 | sbuf = xmalloc(len + 1); | |
797 | xstrncpy(sbuf, buf, len + 1); | |
798 | end = sbuf + len - 1; | |
799 | while (*end != '\r' && *end != '\n' && end > sbuf) | |
3fdadc70 | 800 | end--; |
7131112f | 801 | usable = end - sbuf; |
5999b776 | 802 | debug(9, 3) ("ftpParseListing: usable = %d\n", usable); |
b5639035 | 803 | if (usable == 0) { |
9fb13bb6 | 804 | debug(9, 3) ("ftpParseListing: didn't find end for %s\n", storeUrl(e)); |
7131112f | 805 | xfree(sbuf); |
3fdadc70 | 806 | return; |
807 | } | |
7131112f | 808 | debug(9, 3) ("ftpParseListing: %d bytes to play with\n", len); |
7021844c | 809 | line = memAllocate(MEM_4K_BUF); |
3fdadc70 | 810 | end++; |
438fc1e3 | 811 | storeBuffer(e); |
362be274 | 812 | s = sbuf; |
813 | s += strspn(s, crlf); | |
814 | for (; s < end; s += strcspn(s, crlf), s += strspn(s, crlf)) { | |
7131112f | 815 | debug(9, 3) ("ftpParseListing: s = {%s}\n", s); |
3fdadc70 | 816 | linelen = strcspn(s, crlf) + 1; |
fc09761c | 817 | if (linelen < 2) |
818 | break; | |
3fdadc70 | 819 | if (linelen > 4096) |
820 | linelen = 4096; | |
821 | xstrncpy(line, s, linelen); | |
7131112f | 822 | debug(9, 7) ("ftpParseListing: {%s}\n", line); |
3fdadc70 | 823 | if (!strncmp(line, "total", 5)) |
824 | continue; | |
dbfed404 | 825 | t = ftpHtmlifyListEntry(line, ftpState); |
3fdadc70 | 826 | assert(t != NULL); |
827 | storeAppend(e, t, strlen(t)); | |
828 | } | |
438fc1e3 | 829 | storeBufferFlush(e); |
b5639035 | 830 | assert(usable <= len); |
831 | if (usable < len) { | |
832 | /* must copy partial line to beginning of buf */ | |
e29f7586 | 833 | linelen = len - usable; |
b5639035 | 834 | if (linelen > 4096) |
835 | linelen = 4096; | |
836 | xstrncpy(line, end, linelen); | |
837 | xstrncpy(ftpState->data.buf, line, ftpState->data.size); | |
838 | ftpState->data.offset = strlen(ftpState->data.buf); | |
839 | } | |
db1cd23c | 840 | memFree(line, MEM_4K_BUF); |
4a2635aa | 841 | xfree(sbuf); |
3fdadc70 | 842 | } |
090089c4 | 843 | |
79a15e0a | 844 | static void |
94439e4e | 845 | ftpDataComplete(FtpStateData * ftpState) |
79a15e0a | 846 | { |
94439e4e | 847 | debug(9, 3) ("ftpDataComplete\n"); |
848 | /* Connection closed; transfer done. */ | |
9bc73deb | 849 | if (ftpState->data.fd > -1) { |
850 | /* | |
851 | * close data socket so it does not occupy resources while | |
852 | * we wait | |
853 | */ | |
854 | comm_close(ftpState->data.fd); | |
855 | ftpState->data.fd = -1; | |
54220df8 | 856 | } |
79a15e0a | 857 | /* expect the "transfer complete" message on the control socket */ |
05491dc4 | 858 | ftpScheduleReadControlReply(ftpState, 1); |
79a15e0a | 859 | } |
860 | ||
582b6456 | 861 | static void |
dbfed404 | 862 | ftpDataRead(int fd, void *data) |
090089c4 | 863 | { |
582b6456 | 864 | FtpStateData *ftpState = data; |
090089c4 | 865 | int len; |
a57512fa | 866 | int j; |
30a4f2a8 | 867 | int bin; |
bfcaf585 | 868 | StoreEntry *entry = ftpState->entry; |
447e176b | 869 | size_t read_sz; |
870 | #if DELAY_POOLS | |
94439e4e | 871 | MemObject *mem = entry->mem_obj; |
447e176b | 872 | delay_id delay_id = delayMostBytesAllowed(mem); |
873 | #endif | |
3fdadc70 | 874 | assert(fd == ftpState->data.fd); |
e92e4e44 | 875 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
08ce47ab | 876 | comm_close(ftpState->ctrl.fd); |
b8890359 | 877 | return; |
e92e4e44 | 878 | } |
1a6e21ac | 879 | errno = 0; |
447e176b | 880 | read_sz = ftpState->data.size - ftpState->data.offset; |
881 | #if DELAY_POOLS | |
56e64999 | 882 | read_sz = delayBytesWanted(delay_id, 1, read_sz); |
447e176b | 883 | #endif |
884 | memset(ftpState->data.buf + ftpState->data.offset, '\0', read_sz); | |
83704487 | 885 | statCounter.syscalls.sock.reads++; |
447e176b | 886 | len = read(fd, ftpState->data.buf + ftpState->data.offset, read_sz); |
ee1679df | 887 | if (len > 0) { |
888 | fd_bytes(fd, len, FD_READ); | |
447e176b | 889 | #if DELAY_POOLS |
890 | delayBytesIn(delay_id, len); | |
891 | #endif | |
83704487 | 892 | kb_incr(&statCounter.server.all.kbytes_in, len); |
893 | kb_incr(&statCounter.server.ftp.kbytes_in, len); | |
4f310655 | 894 | ftpState->data.offset += len; |
ee1679df | 895 | } |
dbfed404 | 896 | debug(9, 5) ("ftpDataRead: FD %d, Read %d bytes\n", fd, len); |
30a4f2a8 | 897 | if (len > 0) { |
4a63c85f | 898 | IOStats.Ftp.reads++; |
a57512fa | 899 | for (j = len - 1, bin = 0; j; bin++) |
900 | j >>= 1; | |
30a4f2a8 | 901 | IOStats.Ftp.read_hist[bin]++; |
902 | } | |
ee16d4ab | 903 | if (ftpState->flags.isdir && !ftpState->flags.html_header_sent && len >= 0) { |
904 | ftpListingStart(ftpState); | |
905 | } | |
ba718c8f | 906 | if (len < 0) { |
3b3a620c | 907 | debug(50, ignoreErrno(errno) ? 3 : 1) ("ftpDataRead: read error: %s\n", xstrerror()); |
b224ea98 | 908 | if (ignoreErrno(errno)) { |
a57512fa | 909 | commSetSelect(fd, |
910 | COMM_SELECT_READ, | |
dbfed404 | 911 | ftpDataRead, |
a57512fa | 912 | data, |
913 | Config.Timeout.read); | |
6fe6313d | 914 | } else { |
9bc73deb | 915 | ftpFailed(ftpState, ERR_READ_ERROR); |
916 | /* ftpFailed closes ctrl.fd and frees ftpState */ | |
917 | return; | |
6fe6313d | 918 | } |
090089c4 | 919 | } else if (len == 0) { |
94439e4e | 920 | ftpDataComplete(ftpState); |
090089c4 | 921 | } else { |
e55f0142 | 922 | if (ftpState->flags.isdir) { |
4f310655 | 923 | ftpParseListing(ftpState); |
b5639035 | 924 | } else { |
b5639035 | 925 | storeAppend(entry, ftpState->data.buf, len); |
4f310655 | 926 | ftpState->data.offset = 0; |
b5639035 | 927 | } |
94439e4e | 928 | commSetSelect(fd, |
929 | COMM_SELECT_READ, | |
930 | ftpDataRead, | |
931 | data, | |
932 | Config.Timeout.read); | |
090089c4 | 933 | } |
090089c4 | 934 | } |
935 | ||
429fdbec | 936 | /* |
937 | * ftpCheckAuth | |
938 | * | |
939 | * Return 1 if we have everything needed to complete this request. | |
940 | * Return 0 if something is missing. | |
941 | */ | |
942 | static int | |
5999b776 | 943 | ftpCheckAuth(FtpStateData * ftpState, const HttpHeader * req_hdr) |
429fdbec | 944 | { |
945 | char *orig_user; | |
63259c34 | 946 | const char *auth; |
c68e9c6b | 947 | ftpLoginParser(ftpState->request->login, ftpState, FTP_LOGIN_ESCAPED); |
9bc73deb | 948 | if (!ftpState->user[0]) |
949 | return 1; /* no name */ | |
950 | if (ftpState->password_url || ftpState->password[0]) | |
951 | return 1; /* passwd provided in URL */ | |
429fdbec | 952 | /* URL has name, but no passwd */ |
99edd1c3 | 953 | if (!(auth = httpHeaderGetAuth(req_hdr, HDR_AUTHORIZATION, "Basic"))) |
429fdbec | 954 | return 0; /* need auth header */ |
c68e9c6b | 955 | ftpState->flags.authenticated = 1; |
429fdbec | 956 | orig_user = xstrdup(ftpState->user); |
c68e9c6b | 957 | ftpLoginParser(auth, ftpState, FTP_LOGIN_NOT_ESCAPED); |
429fdbec | 958 | if (!strcmp(orig_user, ftpState->user)) { |
959 | xfree(orig_user); | |
960 | return 1; /* same username */ | |
961 | } | |
962 | strcpy(ftpState->user, orig_user); | |
963 | xfree(orig_user); | |
964 | return 0; /* different username */ | |
965 | } | |
966 | ||
13e80e5b | 967 | static void |
968 | ftpCheckUrlpath(FtpStateData * ftpState) | |
969 | { | |
970 | request_t *request = ftpState->request; | |
971 | int l; | |
02922e76 | 972 | const char *t; |
973 | if ((t = strRChr(request->urlpath, ';')) != NULL) { | |
dbfed404 | 974 | if (strncasecmp(t + 1, "type=", 5) == 0) { |
975 | ftpState->typecode = (char) toupper((int) *(t + 6)); | |
4d62b0af | 976 | strCutPtr(request->urlpath, t); |
dbfed404 | 977 | } |
978 | } | |
02922e76 | 979 | l = strLen(request->urlpath); |
e55f0142 | 980 | ftpState->flags.use_base = 1; |
13e80e5b | 981 | /* check for null path */ |
02922e76 | 982 | if (!l) { |
e55f0142 | 983 | ftpState->flags.isdir = 1; |
984 | ftpState->flags.root_dir = 1; | |
02922e76 | 985 | } else if (!strCmp(request->urlpath, "/%2f/")) { |
aac53724 | 986 | /* UNIX root directory */ |
987 | ftpState->flags.use_base = 0; | |
e55f0142 | 988 | ftpState->flags.isdir = 1; |
989 | ftpState->flags.root_dir = 1; | |
02922e76 | 990 | } else if ((l >= 1) && (*(strBuf(request->urlpath) + l - 1) == '/')) { |
aac53724 | 991 | /* Directory URL, ending in / */ |
e55f0142 | 992 | ftpState->flags.isdir = 1; |
993 | ftpState->flags.use_base = 0; | |
13e80e5b | 994 | if (l == 1) |
e55f0142 | 995 | ftpState->flags.root_dir = 1; |
13e80e5b | 996 | } |
997 | } | |
998 | ||
3fdadc70 | 999 | static void |
1000 | ftpBuildTitleUrl(FtpStateData * ftpState) | |
1001 | { | |
1002 | request_t *request = ftpState->request; | |
1003 | size_t len; | |
1004 | char *t; | |
1005 | len = 64 | |
1006 | + strlen(ftpState->user) | |
1007 | + strlen(ftpState->password) | |
1008 | + strlen(request->host) | |
02922e76 | 1009 | + strLen(request->urlpath); |
3fdadc70 | 1010 | t = ftpState->title_url = xcalloc(len, 1); |
1011 | strcat(t, "ftp://"); | |
1012 | if (strcmp(ftpState->user, "anonymous")) { | |
1013 | strcat(t, ftpState->user); | |
1014 | strcat(t, "@"); | |
1015 | } | |
1016 | strcat(t, request->host); | |
1017 | if (request->port != urlDefaultPort(PROTO_FTP)) | |
56878878 | 1018 | snprintf(&t[strlen(t)], len - strlen(t), ":%d", request->port); |
02922e76 | 1019 | strcat(t, strBuf(request->urlpath)); |
9bc73deb | 1020 | t = ftpState->base_href = xcalloc(len, 1); |
1021 | strcat(t, "ftp://"); | |
1022 | if (strcmp(ftpState->user, "anonymous")) { | |
1023 | strcat(t, rfc1738_escape_part(ftpState->user)); | |
1024 | if (ftpState->password_url) { | |
1025 | strcat(t, ":"); | |
1026 | strcat(t, rfc1738_escape_part(ftpState->password)); | |
1027 | } | |
1028 | strcat(t, "@"); | |
1029 | } | |
1030 | strcat(t, request->host); | |
1031 | if (request->port != urlDefaultPort(PROTO_FTP)) | |
1032 | snprintf(&t[strlen(t)], len - strlen(t), ":%d", request->port); | |
1033 | strcat(t, strBuf(request->urlpath)); | |
1034 | strcat(t, "/"); | |
3fdadc70 | 1035 | } |
1036 | ||
28c60158 | 1037 | CBDATA_TYPE(FtpStateData); |
770f051d | 1038 | void |
db1cd23c | 1039 | ftpStart(FwdState * fwd) |
0a0bf5db | 1040 | { |
db1cd23c | 1041 | request_t *request = fwd->request; |
1042 | StoreEntry *entry = fwd->entry; | |
1043 | int fd = fwd->server_fd; | |
0a0bf5db | 1044 | LOCAL_ARRAY(char, realm, 8192); |
9fb13bb6 | 1045 | const char *url = storeUrl(entry); |
28c60158 | 1046 | FtpStateData *ftpState; |
a0f32775 | 1047 | HttpReply *reply; |
28c60158 | 1048 | |
1049 | CBDATA_INIT_TYPE(FtpStateData); | |
1050 | ftpState = CBDATA_ALLOC(FtpStateData, NULL); | |
5fb59f68 | 1051 | debug(9, 3) ("ftpStart: '%s'\n", url); |
83704487 | 1052 | statCounter.server.all.requests++; |
1053 | statCounter.server.ftp.requests++; | |
770f051d | 1054 | storeLockObject(entry); |
5c5783a2 | 1055 | ftpState->entry = entry; |
5c5783a2 | 1056 | ftpState->request = requestLink(request); |
41462d93 | 1057 | ftpState->ctrl.fd = fd; |
3fdadc70 | 1058 | ftpState->data.fd = -1; |
a533dc81 | 1059 | ftpState->size = -1; |
6b8e7481 | 1060 | ftpState->mdtm = -1; |
d20b1cd0 | 1061 | if (!Config.Ftp.passive) |
1062 | ftpState->flags.rest_supported = 0; | |
1063 | else if (fwd->flags.ftp_pasv_failed) | |
1064 | ftpState->flags.pasv_supported = 0; | |
1065 | else | |
1066 | ftpState->flags.pasv_supported = 1; | |
e55f0142 | 1067 | ftpState->flags.rest_supported = 1; |
db1cd23c | 1068 | ftpState->fwd = fwd; |
54768eba | 1069 | comm_add_close_handler(fd, ftpStateFree, ftpState); |
54220df8 | 1070 | if (ftpState->request->method == METHOD_PUT) |
e55f0142 | 1071 | ftpState->flags.put = 1; |
99edd1c3 | 1072 | if (!ftpCheckAuth(ftpState, &request->header)) { |
429fdbec | 1073 | /* This request is not fully authenticated */ |
1074 | if (request->port == 21) { | |
56878878 | 1075 | snprintf(realm, 8192, "ftp %s", ftpState->user); |
429fdbec | 1076 | } else { |
56878878 | 1077 | snprintf(realm, 8192, "ftp %s port %d", |
5c5783a2 | 1078 | ftpState->user, request->port); |
e381a13d | 1079 | } |
63259c34 | 1080 | /* create reply */ |
a0f32775 | 1081 | reply = entry->mem_obj->reply; |
1082 | assert(reply != NULL); | |
1083 | /* create appropriate reply */ | |
1084 | ftpAuthRequired(reply, request, realm); | |
1085 | httpReplySwapOut(reply, entry); | |
db1cd23c | 1086 | fwdComplete(ftpState->fwd); |
54768eba | 1087 | comm_close(fd); |
429fdbec | 1088 | return; |
e381a13d | 1089 | } |
13e80e5b | 1090 | ftpCheckUrlpath(ftpState); |
3fdadc70 | 1091 | ftpBuildTitleUrl(ftpState); |
5fb59f68 | 1092 | debug(9, 5) ("ftpStart: host=%s, path=%s, user=%s, passwd=%s\n", |
02922e76 | 1093 | ftpState->request->host, strBuf(ftpState->request->urlpath), |
5c5783a2 | 1094 | ftpState->user, ftpState->password); |
41462d93 | 1095 | ftpState->state = BEGIN; |
54768eba | 1096 | ftpState->ctrl.last_command = xstrdup("Connect to server"); |
41462d93 | 1097 | ftpState->ctrl.buf = memAllocate(MEM_4K_BUF); |
1098 | ftpState->ctrl.freefunc = memFree4K; | |
1099 | ftpState->ctrl.size = 4096; | |
1100 | ftpState->ctrl.offset = 0; | |
1101 | ftpState->data.buf = xmalloc(SQUID_TCP_SO_RCVBUF); | |
1102 | ftpState->data.size = SQUID_TCP_SO_RCVBUF; | |
1103 | ftpState->data.freefunc = xfree; | |
05491dc4 | 1104 | ftpScheduleReadControlReply(ftpState, 0); |
090089c4 | 1105 | } |
1106 | ||
3fdadc70 | 1107 | /* ====================================================================== */ |
1108 | ||
b8d8561b | 1109 | static void |
3fdadc70 | 1110 | ftpWriteCommand(const char *buf, FtpStateData * ftpState) |
234967c9 | 1111 | { |
a3d5953d | 1112 | debug(9, 5) ("ftpWriteCommand: %s\n", buf); |
b9916917 | 1113 | safe_free(ftpState->ctrl.last_command); |
9bc73deb | 1114 | safe_free(ftpState->ctrl.last_reply); |
b9916917 | 1115 | ftpState->ctrl.last_command = xstrdup(buf); |
3fdadc70 | 1116 | comm_write(ftpState->ctrl.fd, |
1117 | xstrdup(buf), | |
1118 | strlen(buf), | |
1119 | ftpWriteCommandCallback, | |
1120 | ftpState, | |
1121 | xfree); | |
05491dc4 | 1122 | ftpScheduleReadControlReply(ftpState, 0); |
3fdadc70 | 1123 | } |
1124 | ||
1125 | static void | |
79a15e0a | 1126 | ftpWriteCommandCallback(int fd, char *bufnotused, size_t size, int errflag, void *data) |
3fdadc70 | 1127 | { |
1128 | FtpStateData *ftpState = data; | |
a3d5953d | 1129 | debug(9, 7) ("ftpWriteCommandCallback: wrote %d bytes\n", size); |
ee1679df | 1130 | if (size > 0) { |
1131 | fd_bytes(fd, size, FD_WRITE); | |
83704487 | 1132 | kb_incr(&statCounter.server.all.kbytes_out, size); |
1133 | kb_incr(&statCounter.server.ftp.kbytes_out, size); | |
ee1679df | 1134 | } |
96f1be5d | 1135 | if (errflag == COMM_ERR_CLOSING) |
1136 | return; | |
3fdadc70 | 1137 | if (errflag) { |
94439e4e | 1138 | debug(9, 1) ("ftpWriteCommandCallback: FD %d: %s\n", fd, xstrerror()); |
9bc73deb | 1139 | ftpFailed(ftpState, ERR_WRITE_ERROR); |
1140 | /* ftpFailed closes ctrl.fd and frees ftpState */ | |
1141 | return; | |
3fdadc70 | 1142 | } |
1143 | } | |
1144 | ||
1145 | static wordlist * | |
4f310655 | 1146 | ftpParseControlReply(char *buf, size_t len, int *codep, int *used) |
3fdadc70 | 1147 | { |
1148 | char *s; | |
4f310655 | 1149 | char *sbuf; |
1150 | char *end; | |
1151 | int usable; | |
3fdadc70 | 1152 | int complete = 0; |
51eeadc6 | 1153 | wordlist *head = NULL; |
3fdadc70 | 1154 | wordlist *list; |
1155 | wordlist **tail = &head; | |
1156 | off_t offset; | |
1157 | size_t linelen; | |
b5639035 | 1158 | int code = -1; |
a3d5953d | 1159 | debug(9, 5) ("ftpParseControlReply\n"); |
4f310655 | 1160 | /* |
1161 | * We need a NULL-terminated buffer for scanning, ick | |
1162 | */ | |
1163 | sbuf = xmalloc(len + 1); | |
1164 | xstrncpy(sbuf, buf, len + 1); | |
1165 | end = sbuf + len - 1; | |
1166 | while (*end != '\r' && *end != '\n' && end > sbuf) | |
1b28db46 | 1167 | end--; |
4f310655 | 1168 | usable = end - sbuf; |
1169 | debug(9, 3) ("ftpParseControlReply: usable = %d\n", usable); | |
1170 | if (usable == 0) { | |
1b28db46 | 1171 | debug(9, 3) ("ftpParseControlReply: didn't find end of line\n"); |
5fff72e3 | 1172 | safe_free(sbuf); |
1b28db46 | 1173 | return NULL; |
4f310655 | 1174 | } |
1175 | debug(9, 3) ("ftpParseControlReply: %d bytes to play with\n", len); | |
1176 | end++; | |
1177 | s = sbuf; | |
1178 | s += strspn(s, crlf); | |
1179 | for (; s < end; s += strcspn(s, crlf), s += strspn(s, crlf)) { | |
1180 | if (complete) | |
1181 | break; | |
1b28db46 | 1182 | debug(9, 3) ("ftpParseControlReply: s = {%s}\n", s); |
3fdadc70 | 1183 | linelen = strcspn(s, crlf) + 1; |
fc09761c | 1184 | if (linelen < 2) |
1185 | break; | |
3fdadc70 | 1186 | if (linelen > 3) |
1187 | complete = (*s >= '0' && *s <= '9' && *(s + 3) == ' '); | |
1188 | if (complete) | |
1189 | code = atoi(s); | |
1190 | offset = 0; | |
1191 | if (linelen > 3) | |
1192 | if (*s >= '0' && *s <= '9' && (*(s + 3) == '-' || *(s + 3) == ' ')) | |
1193 | offset = 4; | |
c68e9c6b | 1194 | list = memAllocate(MEM_WORDLIST); |
3fdadc70 | 1195 | list->key = xmalloc(linelen - offset); |
1196 | xstrncpy(list->key, s + offset, linelen - offset); | |
4939c5da | 1197 | debug(9, 7) ("%d %s\n", code, list->key); |
3fdadc70 | 1198 | *tail = list; |
1199 | tail = &list->next; | |
1200 | } | |
4f310655 | 1201 | *used = (int) (s - sbuf); |
1c34d125 | 1202 | safe_free(sbuf); |
3fdadc70 | 1203 | if (!complete) |
1204 | wordlistDestroy(&head); | |
1205 | if (codep) | |
1206 | *codep = code; | |
1207 | return head; | |
1208 | } | |
1209 | ||
4f310655 | 1210 | static void |
1b28db46 | 1211 | ftpScheduleReadControlReply(FtpStateData * ftpState, int buffered_ok) |
4f310655 | 1212 | { |
1b28db46 | 1213 | debug(9, 3) ("ftpScheduleReadControlReply: FD %d\n", ftpState->ctrl.fd); |
05491dc4 | 1214 | if (buffered_ok && ftpState->ctrl.offset > 0) { |
4f310655 | 1215 | /* We've already read some reply data */ |
1216 | ftpHandleControlReply(ftpState); | |
1217 | } else { | |
1b28db46 | 1218 | commSetSelect(ftpState->ctrl.fd, |
4f310655 | 1219 | COMM_SELECT_READ, |
1220 | ftpReadControlReply, | |
1221 | ftpState, | |
1222 | Config.Timeout.read); | |
7e3ce7b9 | 1223 | /* |
1224 | * Cancel the timeout on the Data socket (if any) and | |
1225 | * establish one on the control socket. | |
1226 | */ | |
1227 | if (ftpState->data.fd > -1) | |
1228 | commSetTimeout(ftpState->data.fd, -1, NULL, NULL); | |
1229 | commSetTimeout(ftpState->ctrl.fd, Config.Timeout.read, ftpTimeout, | |
1230 | ftpState); | |
4f310655 | 1231 | } |
1232 | } | |
1233 | ||
3fdadc70 | 1234 | static void |
1235 | ftpReadControlReply(int fd, void *data) | |
1236 | { | |
1237 | FtpStateData *ftpState = data; | |
1238 | StoreEntry *entry = ftpState->entry; | |
3fdadc70 | 1239 | int len; |
a3d5953d | 1240 | debug(9, 5) ("ftpReadControlReply\n"); |
9bc73deb | 1241 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
1242 | comm_close(ftpState->ctrl.fd); | |
1243 | return; | |
1244 | } | |
3fdadc70 | 1245 | assert(ftpState->ctrl.offset < ftpState->ctrl.size); |
83704487 | 1246 | statCounter.syscalls.sock.reads++; |
3fdadc70 | 1247 | len = read(fd, |
1248 | ftpState->ctrl.buf + ftpState->ctrl.offset, | |
1249 | ftpState->ctrl.size - ftpState->ctrl.offset); | |
ee1679df | 1250 | if (len > 0) { |
1251 | fd_bytes(fd, len, FD_READ); | |
83704487 | 1252 | kb_incr(&statCounter.server.all.kbytes_in, len); |
1253 | kb_incr(&statCounter.server.ftp.kbytes_in, len); | |
ee1679df | 1254 | } |
a3d5953d | 1255 | debug(9, 5) ("ftpReadControlReply: FD %d, Read %d bytes\n", fd, len); |
3fdadc70 | 1256 | if (len < 0) { |
3b3a620c | 1257 | debug(50, ignoreErrno(errno) ? 3 : 1) ("ftpReadControlReply: read error: %s\n", xstrerror()); |
b224ea98 | 1258 | if (ignoreErrno(errno)) { |
05491dc4 | 1259 | ftpScheduleReadControlReply(ftpState, 0); |
3fdadc70 | 1260 | } else { |
9bc73deb | 1261 | ftpFailed(ftpState, ERR_READ_ERROR); |
1262 | /* ftpFailed closes ctrl.fd and frees ftpState */ | |
1263 | return; | |
3fdadc70 | 1264 | } |
1265 | return; | |
1266 | } | |
1267 | if (len == 0) { | |
cc11e161 | 1268 | if (entry->store_status == STORE_PENDING) { |
9bc73deb | 1269 | ftpFailed(ftpState, ERR_FTP_FAILURE); |
1270 | /* ftpFailed closes ctrl.fd and frees ftpState */ | |
1271 | return; | |
9b312a19 | 1272 | } |
ff008d84 | 1273 | comm_close(ftpState->ctrl.fd); |
3fdadc70 | 1274 | return; |
1275 | } | |
b5639035 | 1276 | len += ftpState->ctrl.offset; |
1277 | ftpState->ctrl.offset = len; | |
3fdadc70 | 1278 | assert(len <= ftpState->ctrl.size); |
4f310655 | 1279 | ftpHandleControlReply(ftpState); |
1280 | } | |
1281 | ||
1282 | static void | |
1283 | ftpHandleControlReply(FtpStateData * ftpState) | |
1284 | { | |
1285 | char *oldbuf; | |
ec603b25 | 1286 | wordlist **W; |
4f310655 | 1287 | int bytes_used = 0; |
3fdadc70 | 1288 | wordlistDestroy(&ftpState->ctrl.message); |
4f310655 | 1289 | ftpState->ctrl.message = ftpParseControlReply(ftpState->ctrl.buf, |
1290 | ftpState->ctrl.offset, &ftpState->ctrl.replycode, &bytes_used); | |
05491dc4 | 1291 | if (ftpState->ctrl.message == NULL) { |
4f310655 | 1292 | /* didn't get complete reply yet */ |
4f310655 | 1293 | if (ftpState->ctrl.offset == ftpState->ctrl.size) { |
3fdadc70 | 1294 | oldbuf = ftpState->ctrl.buf; |
1295 | ftpState->ctrl.buf = xcalloc(ftpState->ctrl.size << 1, 1); | |
1296 | xmemcpy(ftpState->ctrl.buf, oldbuf, ftpState->ctrl.size); | |
1297 | ftpState->ctrl.size <<= 1; | |
1298 | ftpState->ctrl.freefunc(oldbuf); | |
1299 | ftpState->ctrl.freefunc = xfree; | |
1300 | } | |
05491dc4 | 1301 | ftpScheduleReadControlReply(ftpState, 0); |
3fdadc70 | 1302 | return; |
05491dc4 | 1303 | } else if (ftpState->ctrl.offset == bytes_used) { |
1304 | /* used it all up */ | |
1305 | ftpState->ctrl.offset = 0; | |
4f310655 | 1306 | } else { |
1307 | /* Got some data past the complete reply */ | |
1308 | assert(bytes_used < ftpState->ctrl.offset); | |
1309 | ftpState->ctrl.offset -= bytes_used; | |
1310 | xmemmove(ftpState->ctrl.buf, ftpState->ctrl.buf + bytes_used, | |
1311 | ftpState->ctrl.offset); | |
3fdadc70 | 1312 | } |
858783c9 | 1313 | /* Move the last line of the reply message to ctrl.last_reply */ |
ec603b25 | 1314 | for (W = &ftpState->ctrl.message; (*W)->next; W = &(*W)->next); |
b9916917 | 1315 | safe_free(ftpState->ctrl.last_reply); |
858783c9 | 1316 | ftpState->ctrl.last_reply = xstrdup((*W)->key); |
1317 | wordlistDestroy(W); | |
1318 | /* Copy the rest of the message to cwd_message to be printed in | |
1319 | * error messages | |
1320 | */ | |
1321 | wordlistAddWl(&ftpState->cwd_message, ftpState->ctrl.message); | |
4fc773f7 | 1322 | debug(9, 8) ("ftpHandleControlReply: state=%d, code=%d\n", ftpState->state, |
4f310655 | 1323 | ftpState->ctrl.replycode); |
3fdadc70 | 1324 | FTP_SM_FUNCS[ftpState->state] (ftpState); |
234967c9 | 1325 | } |
1326 | ||
3fdadc70 | 1327 | /* ====================================================================== */ |
1328 | ||
1329 | static void | |
1330 | ftpReadWelcome(FtpStateData * ftpState) | |
1331 | { | |
1332 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 1333 | debug(9, 3) ("ftpReadWelcome\n"); |
e55f0142 | 1334 | if (ftpState->flags.pasv_only) |
3fdadc70 | 1335 | ftpState->login_att++; |
9bc73deb | 1336 | /* Dont retry if the FTP server accepted the connection */ |
1337 | ftpState->fwd->flags.dont_retry = 1; | |
3fdadc70 | 1338 | if (code == 220) { |
4d62b0af | 1339 | if (ftpState->ctrl.message) { |
3fdadc70 | 1340 | if (strstr(ftpState->ctrl.message->key, "NetWare")) |
e55f0142 | 1341 | ftpState->flags.skip_whitespace = 1; |
4d62b0af | 1342 | } |
969c39b9 | 1343 | ftpSendUser(ftpState); |
cdc33f35 | 1344 | } else if (code == 120) { |
c3b838d4 | 1345 | if (NULL != ftpState->ctrl.message) |
1346 | debug(9, 3) ("FTP server is busy: %s\n", | |
1347 | ftpState->ctrl.message->key); | |
cdc33f35 | 1348 | return; |
3fdadc70 | 1349 | } else { |
1350 | ftpFail(ftpState); | |
1351 | } | |
1352 | } | |
1353 | ||
969c39b9 | 1354 | static void |
1355 | ftpSendUser(FtpStateData * ftpState) | |
1356 | { | |
1357 | if (ftpState->proxy_host != NULL) | |
1358 | snprintf(cbuf, 1024, "USER %s@%s\r\n", | |
1359 | ftpState->user, | |
1360 | ftpState->request->host); | |
1361 | else | |
1362 | snprintf(cbuf, 1024, "USER %s\r\n", ftpState->user); | |
1363 | ftpWriteCommand(cbuf, ftpState); | |
1364 | ftpState->state = SENT_USER; | |
1365 | } | |
1366 | ||
3fdadc70 | 1367 | static void |
1368 | ftpReadUser(FtpStateData * ftpState) | |
234967c9 | 1369 | { |
3fdadc70 | 1370 | int code = ftpState->ctrl.replycode; |
a3d5953d | 1371 | debug(9, 3) ("ftpReadUser\n"); |
3fdadc70 | 1372 | if (code == 230) { |
1373 | ftpReadPass(ftpState); | |
1374 | } else if (code == 331) { | |
969c39b9 | 1375 | ftpSendPass(ftpState); |
3fdadc70 | 1376 | } else { |
1377 | ftpFail(ftpState); | |
1378 | } | |
1379 | } | |
1380 | ||
969c39b9 | 1381 | static void |
1382 | ftpSendPass(FtpStateData * ftpState) | |
1383 | { | |
1384 | snprintf(cbuf, 1024, "PASS %s\r\n", ftpState->password); | |
1385 | ftpWriteCommand(cbuf, ftpState); | |
1386 | ftpState->state = SENT_PASS; | |
1387 | } | |
1388 | ||
3fdadc70 | 1389 | static void |
1390 | ftpReadPass(FtpStateData * ftpState) | |
1391 | { | |
1392 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 1393 | debug(9, 3) ("ftpReadPass\n"); |
3fdadc70 | 1394 | if (code == 230) { |
969c39b9 | 1395 | ftpSendType(ftpState); |
3fdadc70 | 1396 | } else { |
1397 | ftpFail(ftpState); | |
1398 | } | |
1399 | } | |
1400 | ||
969c39b9 | 1401 | static void |
1402 | ftpSendType(FtpStateData * ftpState) | |
1403 | { | |
02922e76 | 1404 | const char *t; |
1405 | const char *filename; | |
969c39b9 | 1406 | char mode; |
9e242e02 | 1407 | /* |
1408 | * Ref section 3.2.2 of RFC 1738 | |
1409 | */ | |
1410 | switch (mode = ftpState->typecode) { | |
1411 | case 'D': | |
1412 | mode = 'A'; | |
1413 | break; | |
1414 | case 'A': | |
1415 | case 'I': | |
1416 | break; | |
1417 | default: | |
e55f0142 | 1418 | if (ftpState->flags.isdir) { |
dbfed404 | 1419 | mode = 'A'; |
1420 | } else { | |
02922e76 | 1421 | t = strRChr(ftpState->request->urlpath, '/'); |
1422 | filename = t ? t + 1 : strBuf(ftpState->request->urlpath); | |
dbfed404 | 1423 | mode = mimeGetTransferMode(filename); |
1424 | } | |
9e242e02 | 1425 | break; |
1426 | } | |
969c39b9 | 1427 | if (mode == 'I') |
e55f0142 | 1428 | ftpState->flags.binary = 1; |
cfbf5373 | 1429 | else |
1430 | ftpState->flags.binary = 0; | |
969c39b9 | 1431 | snprintf(cbuf, 1024, "TYPE %c\r\n", mode); |
1432 | ftpWriteCommand(cbuf, ftpState); | |
1433 | ftpState->state = SENT_TYPE; | |
1434 | } | |
1435 | ||
3fdadc70 | 1436 | static void |
1437 | ftpReadType(FtpStateData * ftpState) | |
1438 | { | |
1439 | int code = ftpState->ctrl.replycode; | |
3fdadc70 | 1440 | char *path; |
6e5ae4a4 | 1441 | char *d, *p; |
a3d5953d | 1442 | debug(9, 3) ("This is ftpReadType\n"); |
3fdadc70 | 1443 | if (code == 200) { |
4c75f031 | 1444 | p = path = xstrdup(strBuf(ftpState->request->urlpath)); |
1445 | if (*p == '/') | |
1446 | p++; | |
1447 | while (*p) { | |
1448 | d = p; | |
6e5ae4a4 | 1449 | p += strcspn(p, "/"); |
1450 | if (*p) | |
1451 | *p++ = '\0'; | |
13e80e5b | 1452 | rfc1738_unescape(d); |
c68e9c6b | 1453 | wordlistAdd(&ftpState->pathcomps, d); |
3fdadc70 | 1454 | } |
13e80e5b | 1455 | xfree(path); |
969c39b9 | 1456 | if (ftpState->pathcomps) |
1457 | ftpTraverseDirectory(ftpState); | |
1458 | else | |
dbfed404 | 1459 | ftpListDir(ftpState); |
3fdadc70 | 1460 | } else { |
1461 | ftpFail(ftpState); | |
1462 | } | |
1463 | } | |
1464 | ||
1465 | static void | |
969c39b9 | 1466 | ftpTraverseDirectory(FtpStateData * ftpState) |
3fdadc70 | 1467 | { |
1468 | wordlist *w; | |
0f169992 | 1469 | debug(9, 4) ("ftpTraverseDirectory %s\n", |
1470 | ftpState->filepath ? ftpState->filepath : "<NULL>"); | |
969c39b9 | 1471 | |
1472 | safe_free(ftpState->filepath); | |
1473 | /* Done? */ | |
1474 | if (ftpState->pathcomps == NULL) { | |
a3d5953d | 1475 | debug(9, 3) ("the final component was a directory\n"); |
dbfed404 | 1476 | ftpListDir(ftpState); |
234967c9 | 1477 | return; |
3fdadc70 | 1478 | } |
969c39b9 | 1479 | /* Go to next path component */ |
1480 | w = ftpState->pathcomps; | |
1481 | ftpState->filepath = w->key; | |
1482 | ftpState->pathcomps = w->next; | |
3c20c6a5 | 1483 | memFree(w, MEM_WORDLIST); |
969c39b9 | 1484 | /* Check if we are to CWD or RETR */ |
e55f0142 | 1485 | if (ftpState->pathcomps != NULL || ftpState->flags.isdir) { |
969c39b9 | 1486 | ftpSendCwd(ftpState); |
1487 | } else { | |
1488 | debug(9, 3) ("final component is probably a file\n"); | |
dbfed404 | 1489 | ftpGetFile(ftpState); |
969c39b9 | 1490 | return; |
1491 | } | |
1492 | } | |
1493 | ||
1494 | static void | |
1495 | ftpSendCwd(FtpStateData * ftpState) | |
1496 | { | |
1497 | char *path = ftpState->filepath; | |
1498 | debug(9, 3) ("ftpSendCwd\n"); | |
1499 | if (!strcmp(path, "..") || !strcmp(path, "/")) { | |
e55f0142 | 1500 | ftpState->flags.no_dotdot = 1; |
13e80e5b | 1501 | } else { |
e55f0142 | 1502 | ftpState->flags.no_dotdot = 0; |
13e80e5b | 1503 | } |
6e5ae4a4 | 1504 | if (*path) |
1505 | snprintf(cbuf, 1024, "CWD %s\r\n", path); | |
1506 | else | |
1507 | snprintf(cbuf, 1024, "CWD\r\n"); | |
969c39b9 | 1508 | ftpWriteCommand(cbuf, ftpState); |
1509 | ftpState->state = SENT_CWD; | |
3fdadc70 | 1510 | } |
77a30ebb | 1511 | |
3fdadc70 | 1512 | static void |
1513 | ftpReadCwd(FtpStateData * ftpState) | |
1514 | { | |
1515 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 1516 | debug(9, 3) ("This is ftpReadCwd\n"); |
3fdadc70 | 1517 | if (code >= 200 && code < 300) { |
969c39b9 | 1518 | /* CWD OK */ |
0f169992 | 1519 | ftpUnhack(ftpState); |
858783c9 | 1520 | /* Reset cwd_message to only include the last message */ |
3fdadc70 | 1521 | if (ftpState->cwd_message) |
1522 | wordlistDestroy(&ftpState->cwd_message); | |
1523 | ftpState->cwd_message = ftpState->ctrl.message; | |
1524 | ftpState->ctrl.message = NULL; | |
969c39b9 | 1525 | /* Continue to traverse the path */ |
1526 | ftpTraverseDirectory(ftpState); | |
3fdadc70 | 1527 | } else { |
1528 | /* CWD FAILED */ | |
e55f0142 | 1529 | if (!ftpState->flags.put) |
4162ee3b | 1530 | ftpFail(ftpState); |
54220df8 | 1531 | else |
94439e4e | 1532 | ftpSendMkdir(ftpState); |
c021888f | 1533 | } |
3fdadc70 | 1534 | } |
1535 | ||
54220df8 | 1536 | static void |
94439e4e | 1537 | ftpSendMkdir(FtpStateData * ftpState) |
54220df8 | 1538 | { |
4162ee3b | 1539 | char *path = ftpState->filepath; |
94439e4e | 1540 | debug(9, 3) ("ftpSendMkdir: with path=%s\n", path); |
4162ee3b | 1541 | snprintf(cbuf, 1024, "MKD %s\r\n", path); |
1542 | ftpWriteCommand(cbuf, ftpState); | |
1543 | ftpState->state = SENT_MKDIR; | |
54220df8 | 1544 | } |
1545 | ||
1546 | static void | |
4162ee3b | 1547 | ftpReadMkdir(FtpStateData * ftpState) |
1548 | { | |
1549 | char *path = ftpState->filepath; | |
1550 | int code = ftpState->ctrl.replycode; | |
1551 | ||
c3b838d4 | 1552 | debug(9, 3) ("ftpReadMkdir: path %s, code %d\n", path, code); |
4162ee3b | 1553 | if (code == 257) { /* success */ |
1554 | ftpSendCwd(ftpState); | |
1555 | } else if (code == 550) { /* dir exists */ | |
e55f0142 | 1556 | if (ftpState->flags.put_mkdir) { |
1557 | ftpState->flags.put_mkdir = 1; | |
4162ee3b | 1558 | ftpSendCwd(ftpState); |
1559 | } else | |
1560 | ftpSendReply(ftpState); | |
1561 | } else | |
1562 | ftpSendReply(ftpState); | |
54220df8 | 1563 | } |
1564 | ||
dbfed404 | 1565 | static void |
1566 | ftpGetFile(FtpStateData * ftpState) | |
1567 | { | |
1568 | assert(*ftpState->filepath != '\0'); | |
e55f0142 | 1569 | ftpState->flags.isdir = 0; |
dbfed404 | 1570 | ftpSendMdtm(ftpState); |
1571 | } | |
1572 | ||
1573 | static void | |
1574 | ftpListDir(FtpStateData * ftpState) | |
1575 | { | |
e55f0142 | 1576 | if (!ftpState->flags.isdir) { |
dbfed404 | 1577 | debug(9, 3) ("Directory path did not end in /\n"); |
1578 | strcat(ftpState->title_url, "/"); | |
e55f0142 | 1579 | ftpState->flags.isdir = 1; |
1580 | ftpState->flags.use_base = 1; | |
dbfed404 | 1581 | } |
1582 | ftpSendPasv(ftpState); | |
1583 | } | |
1584 | ||
969c39b9 | 1585 | static void |
1586 | ftpSendMdtm(FtpStateData * ftpState) | |
1587 | { | |
1588 | assert(*ftpState->filepath != '\0'); | |
1589 | snprintf(cbuf, 1024, "MDTM %s\r\n", ftpState->filepath); | |
1590 | ftpWriteCommand(cbuf, ftpState); | |
1591 | ftpState->state = SENT_MDTM; | |
1592 | } | |
1593 | ||
3fdadc70 | 1594 | static void |
1595 | ftpReadMdtm(FtpStateData * ftpState) | |
1596 | { | |
1597 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 1598 | debug(9, 3) ("This is ftpReadMdtm\n"); |
3fdadc70 | 1599 | if (code == 213) { |
b9916917 | 1600 | ftpState->mdtm = parse_iso3307_time(ftpState->ctrl.last_reply); |
0f169992 | 1601 | ftpUnhack(ftpState); |
3fdadc70 | 1602 | } else if (code < 0) { |
1603 | ftpFail(ftpState); | |
77a30ebb | 1604 | } |
969c39b9 | 1605 | ftpSendSize(ftpState); |
1606 | } | |
1607 | ||
1608 | static void | |
1609 | ftpSendSize(FtpStateData * ftpState) | |
1610 | { | |
1611 | /* Only send SIZE for binary transfers. The returned size | |
1612 | * is useless on ASCII transfers */ | |
e55f0142 | 1613 | if (ftpState->flags.binary) { |
969c39b9 | 1614 | assert(ftpState->filepath != NULL); |
1615 | assert(*ftpState->filepath != '\0'); | |
1616 | snprintf(cbuf, 1024, "SIZE %s\r\n", ftpState->filepath); | |
1617 | ftpWriteCommand(cbuf, ftpState); | |
1618 | ftpState->state = SENT_SIZE; | |
1619 | } else | |
1620 | /* Skip to next state no non-binary transfers */ | |
1621 | ftpSendPasv(ftpState); | |
3fdadc70 | 1622 | } |
1623 | ||
1624 | static void | |
1625 | ftpReadSize(FtpStateData * ftpState) | |
1626 | { | |
1627 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 1628 | debug(9, 3) ("This is ftpReadSize\n"); |
3fdadc70 | 1629 | if (code == 213) { |
0f169992 | 1630 | ftpUnhack(ftpState); |
b9916917 | 1631 | ftpState->size = atoi(ftpState->ctrl.last_reply); |
bc2f604d | 1632 | if (ftpState->size == 0) { |
1633 | debug(9, 2) ("ftpReadSize: SIZE reported %s on %s\n", | |
1634 | ftpState->ctrl.last_reply, | |
1635 | ftpState->title_url); | |
1636 | ftpState->size = -1; | |
1637 | } | |
3fdadc70 | 1638 | } else if (code < 0) { |
1639 | ftpFail(ftpState); | |
1640 | } | |
4939c5da | 1641 | ftpSendPasv(ftpState); |
3fdadc70 | 1642 | } |
1643 | ||
1644 | static void | |
1645 | ftpSendPasv(FtpStateData * ftpState) | |
1646 | { | |
1647 | int fd; | |
cdc33f35 | 1648 | struct sockaddr_in addr; |
6637e3a5 | 1649 | socklen_t addr_len; |
7497ceef | 1650 | if (ftpState->request->method == METHOD_HEAD) { |
1651 | /* Terminate here for HEAD requests */ | |
1652 | ftpAppendSuccessHeader(ftpState); | |
1653 | storeTimestampsSet(ftpState->entry); | |
702fb6b9 | 1654 | /* |
1655 | * On rare occasions I'm seeing the entry get aborted after | |
1656 | * ftpReadControlReply() and before here, probably when | |
1657 | * trying to write to the client. | |
1658 | */ | |
1659 | if (!EBIT_TEST(ftpState->entry->flags, ENTRY_ABORTED)) | |
1660 | fwdComplete(ftpState->fwd); | |
7497ceef | 1661 | ftpSendQuit(ftpState); |
1662 | return; | |
1663 | } | |
a57512fa | 1664 | if (ftpState->data.fd >= 0) { |
e55f0142 | 1665 | if (!ftpState->flags.datachannel_hack) { |
0f169992 | 1666 | /* We are already connected, reuse this connection. */ |
1667 | ftpRestOrList(ftpState); | |
1668 | return; | |
1669 | } else { | |
1670 | /* Close old connection */ | |
1671 | comm_close(ftpState->data.fd); | |
1672 | ftpState->data.fd = -1; | |
1673 | } | |
a57512fa | 1674 | } |
e55f0142 | 1675 | if (!ftpState->flags.pasv_supported) { |
3fdadc70 | 1676 | ftpSendPort(ftpState); |
1677 | return; | |
30a4f2a8 | 1678 | } |
cdc33f35 | 1679 | addr_len = sizeof(addr); |
1680 | if (getsockname(ftpState->ctrl.fd, (struct sockaddr *) &addr, &addr_len)) { | |
1681 | debug(9, 0) ("ftpSendPasv: getsockname(%d,..): %s\n", | |
1682 | ftpState->ctrl.fd, xstrerror()); | |
1683 | addr.sin_addr = Config.Addrs.tcp_outgoing; | |
1684 | } | |
1685 | /* Open data channel with the same local address as control channel */ | |
3fdadc70 | 1686 | fd = comm_open(SOCK_STREAM, |
16b204c4 | 1687 | 0, |
cdc33f35 | 1688 | addr.sin_addr, |
30a4f2a8 | 1689 | 0, |
3fdadc70 | 1690 | COMM_NONBLOCKING, |
9fb13bb6 | 1691 | storeUrl(ftpState->entry)); |
05e11a8c | 1692 | debug(9, 3) ("ftpSendPasv: Unconnected data socket created on FD %d\n", fd); |
3fdadc70 | 1693 | if (fd < 0) { |
1694 | ftpFail(ftpState); | |
1695 | return; | |
1696 | } | |
ff008d84 | 1697 | /* |
1698 | * No comm_add_close_handler() here. If we have both ctrl and | |
1699 | * data FD's call ftpStateFree() upon close, then we have | |
1700 | * to delete the close handler which did NOT get called | |
1701 | * to prevent ftpStateFree() getting called twice. | |
1702 | * Instead we'll always call comm_close() on the ctrl FD. | |
1703 | */ | |
3fdadc70 | 1704 | ftpState->data.fd = fd; |
042461c3 | 1705 | snprintf(cbuf, 1024, "PASV\r\n"); |
3fdadc70 | 1706 | ftpWriteCommand(cbuf, ftpState); |
1707 | ftpState->state = SENT_PASV; | |
7e3ce7b9 | 1708 | /* |
1709 | * ugly hack for ftp servers like ftp.netscape.com that sometimes | |
1710 | * dont acknowledge PORT commands. | |
1711 | */ | |
1712 | commSetTimeout(ftpState->data.fd, 15, ftpTimeout, ftpState); | |
3fdadc70 | 1713 | } |
1714 | ||
1715 | static void | |
1716 | ftpReadPasv(FtpStateData * ftpState) | |
1717 | { | |
1718 | int code = ftpState->ctrl.replycode; | |
1719 | int h1, h2, h3, h4; | |
1720 | int p1, p2; | |
1721 | int n; | |
1722 | u_short port; | |
1723 | int fd = ftpState->data.fd; | |
b9916917 | 1724 | char *buf = ftpState->ctrl.last_reply; |
3fdadc70 | 1725 | LOCAL_ARRAY(char, junk, 1024); |
a3d5953d | 1726 | debug(9, 3) ("This is ftpReadPasv\n"); |
3fdadc70 | 1727 | if (code != 227) { |
a3d5953d | 1728 | debug(9, 3) ("PASV not supported by remote end\n"); |
3fdadc70 | 1729 | ftpSendPort(ftpState); |
1730 | return; | |
1731 | } | |
69e81830 | 1732 | if ((int) strlen(buf) > 1024) { |
2a1bc30a | 1733 | debug(9, 1) ("ftpReadPasv: Avoiding potential buffer overflow\n"); |
3fdadc70 | 1734 | ftpSendPort(ftpState); |
1735 | return; | |
1736 | } | |
1737 | /* 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */ | |
1738 | /* ANSI sez [^0-9] is undefined, it breaks on Watcom cc */ | |
a3d5953d | 1739 | debug(9, 5) ("scanning: %s\n", buf); |
3fdadc70 | 1740 | n = sscanf(buf, "%[^0123456789]%d,%d,%d,%d,%d,%d", |
1741 | junk, &h1, &h2, &h3, &h4, &p1, &p2); | |
1742 | if (n != 7 || p1 < 0 || p2 < 0 || p1 > 255 || p2 > 255) { | |
a3d5953d | 1743 | debug(9, 3) ("Bad 227 reply\n"); |
1744 | debug(9, 3) ("n=%d, p1=%d, p2=%d\n", n, p1, p2); | |
3fdadc70 | 1745 | ftpSendPort(ftpState); |
1746 | return; | |
1747 | } | |
56878878 | 1748 | snprintf(junk, 1024, "%d.%d.%d.%d", h1, h2, h3, h4); |
3fdadc70 | 1749 | if (!safe_inet_addr(junk, NULL)) { |
a3d5953d | 1750 | debug(9, 1) ("unsafe address (%s)\n", junk); |
3fdadc70 | 1751 | ftpSendPort(ftpState); |
1752 | return; | |
1753 | } | |
1754 | port = ((p1 << 8) + p2); | |
7f74df3a | 1755 | if (0 == port) { |
1afe05c5 | 1756 | debug(9, 1) ("ftpReadPasv: Invalid PASV reply: %s\n", buf); |
7f74df3a | 1757 | ftpSendPort(ftpState); |
1758 | return; | |
1759 | } | |
a3d5953d | 1760 | debug(9, 5) ("ftpReadPasv: connecting to %s, port %d\n", junk, port); |
9b312a19 | 1761 | ftpState->data.port = port; |
1762 | ftpState->data.host = xstrdup(junk); | |
9bc73deb | 1763 | safe_free(ftpState->ctrl.last_command); |
1764 | safe_free(ftpState->ctrl.last_reply); | |
1765 | ftpState->ctrl.last_command = xstrdup("Connect to server data port"); | |
3fdadc70 | 1766 | commConnectStart(fd, junk, port, ftpPasvCallback, ftpState); |
1767 | } | |
1768 | ||
1769 | static void | |
1770 | ftpPasvCallback(int fd, int status, void *data) | |
1771 | { | |
1772 | FtpStateData *ftpState = data; | |
a3d5953d | 1773 | debug(9, 3) ("ftpPasvCallback\n"); |
9b312a19 | 1774 | if (status != COMM_OK) { |
9bc73deb | 1775 | debug(9, 2) ("ftpPasvCallback: failed to connect. Retrying without PASV.\n"); |
1776 | ftpState->fwd->flags.dont_retry = 0; /* this is a retryable error */ | |
1777 | ftpState->fwd->flags.ftp_pasv_failed = 1; | |
1778 | ftpFailed(ftpState, ERR_NONE); | |
1779 | /* ftpFailed closes ctrl.fd and frees ftpState */ | |
3fdadc70 | 1780 | return; |
1781 | } | |
1782 | ftpRestOrList(ftpState); | |
1783 | } | |
1784 | ||
cdc33f35 | 1785 | static int |
1786 | ftpOpenListenSocket(FtpStateData * ftpState, int fallback) | |
1787 | { | |
1788 | int fd; | |
1789 | struct sockaddr_in addr; | |
6637e3a5 | 1790 | socklen_t addr_len; |
cdc33f35 | 1791 | int on = 1; |
1792 | u_short port = 0; | |
7e3ce7b9 | 1793 | /* |
a695e7ee | 1794 | * Tear down any old data connection if any. We are about to |
1795 | * establish a new one. | |
7e3ce7b9 | 1796 | */ |
1797 | if (ftpState->data.fd > 0) { | |
1798 | comm_close(ftpState->data.fd); | |
1799 | ftpState->data.fd = -1; | |
1800 | } | |
4f310655 | 1801 | /* |
1802 | * Set up a listen socket on the same local address as the | |
1803 | * control connection. | |
1804 | */ | |
cdc33f35 | 1805 | addr_len = sizeof(addr); |
1806 | if (getsockname(ftpState->ctrl.fd, (struct sockaddr *) &addr, &addr_len)) { | |
1807 | debug(9, 0) ("ftpOpenListenSocket: getsockname(%d,..): %s\n", | |
1808 | ftpState->ctrl.fd, xstrerror()); | |
1809 | return -1; | |
1810 | } | |
4f310655 | 1811 | /* |
1812 | * REUSEADDR is needed in fallback mode, since the same port is | |
1813 | * used for both control and data. | |
cdc33f35 | 1814 | */ |
1815 | if (fallback) { | |
1816 | setsockopt(ftpState->ctrl.fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)); | |
1817 | port = ntohs(addr.sin_port); | |
1818 | } | |
1819 | fd = comm_open(SOCK_STREAM, | |
1820 | 0, | |
1821 | addr.sin_addr, | |
1822 | port, | |
1823 | COMM_NONBLOCKING | (fallback ? COMM_REUSEADDR : 0), | |
1824 | storeUrl(ftpState->entry)); | |
05e11a8c | 1825 | debug(9, 3) ("ftpOpenListenSocket: Unconnected data socket created on FD %d\n", fd); |
cdc33f35 | 1826 | if (fd < 0) { |
1827 | debug(9, 0) ("ftpOpenListenSocket: comm_open failed\n"); | |
1828 | return -1; | |
1829 | } | |
1830 | if (comm_listen(fd) < 0) { | |
1831 | comm_close(fd); | |
1832 | return -1; | |
1833 | } | |
1834 | ftpState->data.fd = fd; | |
d20b1cd0 | 1835 | ftpState->data.port = comm_local_port(fd); |
cdc33f35 | 1836 | ftpState->data.host = NULL; |
1837 | return fd; | |
1838 | } | |
1839 | ||
3fdadc70 | 1840 | static void |
1841 | ftpSendPort(FtpStateData * ftpState) | |
1842 | { | |
cdc33f35 | 1843 | int fd; |
1844 | struct sockaddr_in addr; | |
6637e3a5 | 1845 | socklen_t addr_len; |
cdc33f35 | 1846 | unsigned char *addrptr; |
1847 | unsigned char *portptr; | |
a3d5953d | 1848 | debug(9, 3) ("This is ftpSendPort\n"); |
e55f0142 | 1849 | ftpState->flags.pasv_supported = 0; |
cdc33f35 | 1850 | fd = ftpOpenListenSocket(ftpState, 0); |
1851 | addr_len = sizeof(addr); | |
1852 | if (getsockname(fd, (struct sockaddr *) &addr, &addr_len)) { | |
1853 | debug(9, 0) ("ftpSendPort: getsockname(%d,..): %s\n", fd, xstrerror()); | |
1854 | /* XXX Need to set error message */ | |
1855 | ftpFail(ftpState); | |
1856 | return; | |
1857 | } | |
1858 | addrptr = (unsigned char *) &addr.sin_addr.s_addr; | |
1859 | portptr = (unsigned char *) &addr.sin_port; | |
1860 | snprintf(cbuf, 1024, "PORT %d,%d,%d,%d,%d,%d\r\n", | |
1861 | addrptr[0], addrptr[1], addrptr[2], addrptr[3], | |
1862 | portptr[0], portptr[1]); | |
1863 | ftpWriteCommand(cbuf, ftpState); | |
1864 | ftpState->state = SENT_PORT; | |
3fdadc70 | 1865 | } |
1866 | ||
1867 | static void | |
cdc33f35 | 1868 | ftpReadPort(FtpStateData * ftpState) |
3fdadc70 | 1869 | { |
cdc33f35 | 1870 | int code = ftpState->ctrl.replycode; |
a3d5953d | 1871 | debug(9, 3) ("This is ftpReadPort\n"); |
cdc33f35 | 1872 | if (code != 200) { |
1873 | /* Fall back on using the same port as the control connection */ | |
1874 | debug(9, 3) ("PORT not supported by remote end\n"); | |
cdc33f35 | 1875 | ftpOpenListenSocket(ftpState, 1); |
1876 | } | |
1877 | ftpRestOrList(ftpState); | |
1878 | } | |
1879 | ||
1880 | /* "read" handler to accept data connection */ | |
1881 | static void | |
1882 | ftpAcceptDataConnection(int fd, void *data) | |
1883 | { | |
1884 | FtpStateData *ftpState = data; | |
d20b1cd0 | 1885 | struct sockaddr_in my_peer, me; |
cdc33f35 | 1886 | debug(9, 3) ("ftpAcceptDataConnection\n"); |
1887 | ||
b044a7eb | 1888 | if (EBIT_TEST(ftpState->entry->flags, ENTRY_ABORTED)) { |
1889 | comm_close(ftpState->ctrl.fd); | |
1890 | return; | |
1891 | } | |
d20b1cd0 | 1892 | fd = comm_accept(fd, &my_peer, &me); |
cdc33f35 | 1893 | if (fd < 0) { |
1894 | debug(9, 1) ("ftpHandleDataAccept: comm_accept(%d): %s", fd, xstrerror()); | |
1895 | /* XXX Need to set error message */ | |
1896 | ftpFail(ftpState); | |
1897 | return; | |
1898 | } | |
c93a9f49 | 1899 | /* Replace the Listen socket with the accepted data socket */ |
1900 | comm_close(ftpState->data.fd); | |
05e11a8c | 1901 | debug(9, 3) ("ftpAcceptDataConnection: Connected data socket on FD %d\n", fd); |
cdc33f35 | 1902 | ftpState->data.fd = fd; |
d20b1cd0 | 1903 | ftpState->data.port = ntohs(my_peer.sin_port); |
1904 | ftpState->data.host = xstrdup(inet_ntoa(my_peer.sin_addr)); | |
58226b89 | 1905 | commSetTimeout(ftpState->ctrl.fd, -1, NULL, NULL); |
05e11a8c | 1906 | commSetTimeout(ftpState->data.fd, Config.Timeout.read, ftpTimeout, |
1907 | ftpState); | |
cdc33f35 | 1908 | /* XXX We should have a flag to track connect state... |
1909 | * host NULL -> not connected, port == local port | |
1910 | * host set -> connected, port == remote port | |
1911 | */ | |
1912 | /* Restart state (SENT_NLST/LIST/RETR) */ | |
1913 | FTP_SM_FUNCS[ftpState->state] (ftpState); | |
3fdadc70 | 1914 | } |
1915 | ||
1916 | static void | |
1917 | ftpRestOrList(FtpStateData * ftpState) | |
1918 | { | |
a3d5953d | 1919 | debug(9, 3) ("This is ftpRestOrList\n"); |
94439e4e | 1920 | if (ftpState->typecode == 'D') { |
e55f0142 | 1921 | ftpState->flags.isdir = 1; |
1922 | ftpState->flags.use_base = 1; | |
94439e4e | 1923 | if (ftpState->flags.put) { |
1924 | ftpSendMkdir(ftpState); /* PUT name;type=d */ | |
1925 | } else { | |
1926 | ftpSendNlst(ftpState); /* GET name;type=d sec 3.2.2 of RFC 1738 */ | |
1927 | } | |
1928 | } else if (ftpState->flags.put) { | |
1929 | debug(9, 3) ("ftpRestOrList: Sending STOR request...\n"); | |
1930 | ftpSendStor(ftpState); | |
e55f0142 | 1931 | } else if (ftpState->flags.isdir) |
969c39b9 | 1932 | ftpSendList(ftpState); |
cfbf5373 | 1933 | else if (ftpRestartable(ftpState)) |
969c39b9 | 1934 | ftpSendRest(ftpState); |
1935 | else | |
1936 | ftpSendRetr(ftpState); | |
1937 | } | |
1938 | ||
54220df8 | 1939 | static void |
1940 | ftpSendStor(FtpStateData * ftpState) | |
1941 | { | |
9bc73deb | 1942 | if (ftpState->filepath != NULL) { |
1943 | /* Plain file upload */ | |
1944 | snprintf(cbuf, 1024, "STOR %s\r\n", ftpState->filepath); | |
1945 | ftpWriteCommand(cbuf, ftpState); | |
1946 | ftpState->state = SENT_STOR; | |
1947 | } else if (httpHeaderGetInt(&ftpState->request->header, HDR_CONTENT_LENGTH) > 0) { | |
1948 | /* File upload without a filename. use STOU to generate one */ | |
1949 | snprintf(cbuf, 1024, "STOU\r\n"); | |
1950 | ftpWriteCommand(cbuf, ftpState); | |
1951 | ftpState->state = SENT_STOR; | |
1952 | } else { | |
1953 | /* No file to transfer. Only create directories if needed */ | |
1954 | ftpSendReply(ftpState); | |
1955 | } | |
54220df8 | 1956 | } |
1957 | ||
1958 | static void | |
1959 | ftpReadStor(FtpStateData * ftpState) | |
1960 | { | |
1961 | int code = ftpState->ctrl.replycode; | |
1962 | debug(9, 3) ("This is ftpReadStor\n"); | |
9bc73deb | 1963 | if (code == 125 || (code == 150 && ftpState->data.host)) { |
1964 | /* Begin data transfer */ | |
1965 | debug(9, 3) ("ftpReadStor: starting data transfer\n"); | |
94439e4e | 1966 | commSetSelect(ftpState->data.fd, |
1967 | COMM_SELECT_WRITE, | |
1968 | ftpDataWrite, | |
1969 | ftpState, | |
1970 | Config.Timeout.read); | |
acce49bf | 1971 | /* |
94439e4e | 1972 | * Cancel the timeout on the Control socket and |
acce49bf | 1973 | * establish one on the data socket. |
1974 | */ | |
1975 | commSetTimeout(ftpState->ctrl.fd, -1, NULL, NULL); | |
94439e4e | 1976 | commSetTimeout(ftpState->data.fd, Config.Timeout.read, ftpTimeout, |
1977 | ftpState); | |
acce49bf | 1978 | ftpState->state = WRITING_DATA; |
94439e4e | 1979 | debug(9, 3) ("ftpReadStor: writing data channel\n"); |
9bc73deb | 1980 | } else if (code == 150) { |
1981 | /* Accept data channel */ | |
1982 | debug(9, 3) ("ftpReadStor: accepting data channel\n"); | |
1983 | commSetSelect(ftpState->data.fd, | |
1984 | COMM_SELECT_READ, | |
1985 | ftpAcceptDataConnection, | |
1986 | ftpState, | |
1987 | 0); | |
54220df8 | 1988 | } else { |
9bc73deb | 1989 | debug(9, 3) ("ftpReadStor: Unexpected reply code %s\n", code); |
1990 | ftpFail(ftpState); | |
54220df8 | 1991 | } |
1992 | } | |
1993 | ||
969c39b9 | 1994 | static void |
1995 | ftpSendRest(FtpStateData * ftpState) | |
1996 | { | |
1997 | snprintf(cbuf, 1024, "REST %d\r\n", ftpState->restart_offset); | |
1998 | ftpWriteCommand(cbuf, ftpState); | |
1999 | ftpState->state = SENT_REST; | |
3fdadc70 | 2000 | } |
2001 | ||
cfbf5373 | 2002 | static int |
2003 | ftpRestartable(FtpStateData * ftpState) | |
2004 | { | |
2005 | if (ftpState->restart_offset > 0) | |
2006 | return 1; | |
2007 | if (!ftpState->request->range) | |
2008 | return 0; | |
2009 | if (!ftpState->flags.binary) | |
2010 | return 0; | |
2011 | if (ftpState->size <= 0) | |
2012 | return 0; | |
2013 | ||
2014 | ftpState->restart_offset = httpHdrRangeLowestOffset(ftpState->request->range, (size_t) ftpState->size); | |
2015 | if (ftpState->restart_offset <= 0) | |
2016 | return 0; | |
2017 | return 1; | |
2018 | } | |
2019 | ||
3fdadc70 | 2020 | static void |
2021 | ftpReadRest(FtpStateData * ftpState) | |
2022 | { | |
2023 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 2024 | debug(9, 3) ("This is ftpReadRest\n"); |
3fdadc70 | 2025 | assert(ftpState->restart_offset > 0); |
2026 | if (code == 350) { | |
cfbf5373 | 2027 | ftpState->restarted_offset = ftpState->restart_offset; |
969c39b9 | 2028 | ftpSendRetr(ftpState); |
3fdadc70 | 2029 | } else if (code > 0) { |
a3d5953d | 2030 | debug(9, 3) ("ftpReadRest: REST not supported\n"); |
e55f0142 | 2031 | ftpState->flags.rest_supported = 0; |
c7e0305b | 2032 | ftpSendRetr(ftpState); |
3fdadc70 | 2033 | } else { |
2034 | ftpFail(ftpState); | |
2035 | } | |
2036 | } | |
2037 | ||
969c39b9 | 2038 | static void |
2039 | ftpSendList(FtpStateData * ftpState) | |
2040 | { | |
dbfed404 | 2041 | if (ftpState->filepath) { |
e55f0142 | 2042 | ftpState->flags.use_base = 1; |
dbfed404 | 2043 | snprintf(cbuf, 1024, "LIST %s\r\n", ftpState->filepath); |
2044 | } else { | |
2045 | snprintf(cbuf, 1024, "LIST\r\n"); | |
2046 | } | |
969c39b9 | 2047 | ftpWriteCommand(cbuf, ftpState); |
2048 | ftpState->state = SENT_LIST; | |
2049 | } | |
2050 | ||
dbfed404 | 2051 | static void |
2052 | ftpSendNlst(FtpStateData * ftpState) | |
2053 | { | |
e55f0142 | 2054 | ftpState->flags.tried_nlst = 1; |
dbfed404 | 2055 | if (ftpState->filepath) { |
e55f0142 | 2056 | ftpState->flags.use_base = 1; |
dbfed404 | 2057 | snprintf(cbuf, 1024, "NLST %s\r\n", ftpState->filepath); |
2058 | } else { | |
2059 | snprintf(cbuf, 1024, "NLST\r\n"); | |
2060 | } | |
2061 | ftpWriteCommand(cbuf, ftpState); | |
2062 | ftpState->state = SENT_NLST; | |
2063 | } | |
2064 | ||
3fdadc70 | 2065 | static void |
2066 | ftpReadList(FtpStateData * ftpState) | |
2067 | { | |
2068 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 2069 | debug(9, 3) ("This is ftpReadList\n"); |
cdc33f35 | 2070 | if (code == 125 || (code == 150 && ftpState->data.host)) { |
2071 | /* Begin data transfer */ | |
3fdadc70 | 2072 | ftpAppendSuccessHeader(ftpState); |
2073 | commSetSelect(ftpState->data.fd, | |
30a4f2a8 | 2074 | COMM_SELECT_READ, |
dbfed404 | 2075 | ftpDataRead, |
3fdadc70 | 2076 | ftpState, |
2acf4be6 | 2077 | Config.Timeout.read); |
41462d93 | 2078 | commSetDefer(ftpState->data.fd, fwdCheckDeferRead, ftpState->entry); |
3fdadc70 | 2079 | ftpState->state = READING_DATA; |
05e11a8c | 2080 | /* |
2081 | * Cancel the timeout on the Control socket and establish one | |
2082 | * on the data socket | |
2083 | */ | |
a0eff6be | 2084 | commSetTimeout(ftpState->ctrl.fd, -1, NULL, NULL); |
2085 | commSetTimeout(ftpState->data.fd, Config.Timeout.read, ftpTimeout, ftpState); | |
3fdadc70 | 2086 | return; |
cdc33f35 | 2087 | } else if (code == 150) { |
2088 | /* Accept data channel */ | |
2089 | commSetSelect(ftpState->data.fd, | |
2090 | COMM_SELECT_READ, | |
2091 | ftpAcceptDataConnection, | |
2092 | ftpState, | |
05e11a8c | 2093 | 0); |
2094 | /* | |
2095 | * Cancel the timeout on the Control socket and establish one | |
2096 | * on the data socket | |
2097 | */ | |
2098 | commSetTimeout(ftpState->ctrl.fd, -1, NULL, NULL); | |
2099 | commSetTimeout(ftpState->data.fd, Config.Timeout.read, ftpTimeout, ftpState); | |
cdc33f35 | 2100 | return; |
e55f0142 | 2101 | } else if (!ftpState->flags.tried_nlst && code > 300) { |
969c39b9 | 2102 | ftpSendNlst(ftpState); |
3fdadc70 | 2103 | } else { |
2104 | ftpFail(ftpState); | |
2105 | return; | |
2106 | } | |
2107 | } | |
2108 | ||
969c39b9 | 2109 | static void |
2110 | ftpSendRetr(FtpStateData * ftpState) | |
2111 | { | |
2112 | assert(ftpState->filepath != NULL); | |
2113 | snprintf(cbuf, 1024, "RETR %s\r\n", ftpState->filepath); | |
2114 | ftpWriteCommand(cbuf, ftpState); | |
2115 | ftpState->state = SENT_RETR; | |
2116 | } | |
2117 | ||
3fdadc70 | 2118 | static void |
2119 | ftpReadRetr(FtpStateData * ftpState) | |
2120 | { | |
2121 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 2122 | debug(9, 3) ("This is ftpReadRetr\n"); |
cdc33f35 | 2123 | if (code == 125 || (code == 150 && ftpState->data.host)) { |
2124 | /* Begin data transfer */ | |
a57512fa | 2125 | debug(9, 3) ("ftpReadRetr: reading data channel\n"); |
3fdadc70 | 2126 | ftpAppendSuccessHeader(ftpState); |
2127 | commSetSelect(ftpState->data.fd, | |
2128 | COMM_SELECT_READ, | |
dbfed404 | 2129 | ftpDataRead, |
3fdadc70 | 2130 | ftpState, |
2acf4be6 | 2131 | Config.Timeout.read); |
41462d93 | 2132 | commSetDefer(ftpState->data.fd, fwdCheckDeferRead, ftpState->entry); |
3fdadc70 | 2133 | ftpState->state = READING_DATA; |
bd200c90 | 2134 | /* |
2135 | * Cancel the timeout on the Control socket and establish one | |
2136 | * on the data socket | |
2137 | */ | |
2acf4be6 | 2138 | commSetTimeout(ftpState->ctrl.fd, -1, NULL, NULL); |
bd200c90 | 2139 | commSetTimeout(ftpState->data.fd, Config.Timeout.read, ftpTimeout, |
2140 | ftpState); | |
cdc33f35 | 2141 | } else if (code == 150) { |
2142 | /* Accept data channel */ | |
2143 | commSetSelect(ftpState->data.fd, | |
2144 | COMM_SELECT_READ, | |
2145 | ftpAcceptDataConnection, | |
2146 | ftpState, | |
05e11a8c | 2147 | 0); |
bd200c90 | 2148 | /* |
2149 | * Cancel the timeout on the Control socket and establish one | |
2150 | * on the data socket | |
2151 | */ | |
2152 | commSetTimeout(ftpState->ctrl.fd, -1, NULL, NULL); | |
2153 | commSetTimeout(ftpState->data.fd, Config.Timeout.read, ftpTimeout, | |
2154 | ftpState); | |
cdc33f35 | 2155 | } else if (code >= 300) { |
e55f0142 | 2156 | if (!ftpState->flags.try_slash_hack) { |
969c39b9 | 2157 | /* Try this as a directory missing trailing slash... */ |
2158 | ftpHackShortcut(ftpState, ftpSendCwd); | |
2159 | } else { | |
2160 | ftpFail(ftpState); | |
2161 | } | |
cdc33f35 | 2162 | } else { |
2163 | ftpFail(ftpState); | |
3fdadc70 | 2164 | } |
2165 | } | |
2166 | ||
2167 | static void | |
2168 | ftpReadTransferDone(FtpStateData * ftpState) | |
2169 | { | |
2170 | int code = ftpState->ctrl.replycode; | |
a3d5953d | 2171 | debug(9, 3) ("This is ftpReadTransferDone\n"); |
9bc73deb | 2172 | if (code == 226) { |
2173 | /* Connection closed; retrieval done. */ | |
2174 | if (ftpState->flags.html_header_sent) | |
2175 | ftpListingFinish(ftpState); | |
94439e4e | 2176 | ftpSendQuit(ftpState); |
9bc73deb | 2177 | } else { /* != 226 */ |
c3b838d4 | 2178 | debug(9, 1) ("ftpReadTransferDone: Got code %d after reading data\n", |
2179 | code); | |
9bc73deb | 2180 | ftpFailed(ftpState, ERR_FTP_FAILURE); |
2181 | /* ftpFailed closes ctrl.fd and frees ftpState */ | |
2182 | return; | |
3fdadc70 | 2183 | } |
2184 | } | |
2185 | ||
94439e4e | 2186 | /* This will be called when there is data available to put */ |
3fdadc70 | 2187 | static void |
94439e4e | 2188 | ftpRequestBody(char *buf, size_t size, void *data) |
3fdadc70 | 2189 | { |
94439e4e | 2190 | FtpStateData *ftpState = (FtpStateData *) data; |
2191 | debug(9, 3) ("ftpRequestBody: buf=%p size=%d ftpState=%p\n", buf, size, data); | |
2192 | ftpState->data.offset = size; | |
2193 | if (size > 0) { | |
2194 | /* DataWrite */ | |
2195 | comm_write(ftpState->data.fd, buf, size, ftpDataWriteCallback, data, NULL); | |
2196 | } else if (size < 0) { | |
2197 | /* Error */ | |
2198 | debug(9, 1) ("ftpRequestBody: request aborted"); | |
2199 | ftpFailed(ftpState, ERR_READ_ERROR); | |
2200 | } else if (size == 0) { | |
2201 | /* End of transfer */ | |
2202 | ftpDataComplete(ftpState); | |
033fa114 | 2203 | } |
94439e4e | 2204 | } |
2205 | ||
2206 | /* This will be called when the put write is completed */ | |
2207 | static void | |
2208 | ftpDataWriteCallback(int fd, char *buf, size_t size, int err, void *data) | |
2209 | { | |
2210 | FtpStateData *ftpState = (FtpStateData *) data; | |
2211 | if (!err) { | |
2212 | /* Shedule the rest of the request */ | |
2213 | clientReadBody(ftpState->request, ftpState->data.buf, ftpState->data.size, ftpRequestBody, ftpState); | |
2214 | } else { | |
2215 | debug(9, 1) ("ftpDataWriteCallback: write error: %s\n", xstrerror()); | |
2216 | ftpFailed(ftpState, ERR_WRITE_ERROR); | |
2217 | } | |
2218 | } | |
2219 | ||
2220 | static void | |
2221 | ftpDataWrite(int ftp, void *data) | |
2222 | { | |
2223 | FtpStateData *ftpState = (FtpStateData *) data; | |
2224 | debug(9, 3) ("ftpDataWrite\n"); | |
2225 | /* This starts the body transfer */ | |
2226 | clientReadBody(ftpState->request, ftpState->data.buf, ftpState->data.size, ftpRequestBody, ftpState); | |
2227 | } | |
2228 | ||
2229 | static void | |
2230 | ftpWriteTransferDone(FtpStateData * ftpState) | |
2231 | { | |
2232 | int code = ftpState->ctrl.replycode; | |
2233 | debug(9, 3) ("This is ftpWriteTransferDone\n"); | |
2234 | if (code != 226) { | |
2235 | debug(9, 1) ("ftpReadTransferDone: Got code %d after sending data\n", | |
2236 | code); | |
2237 | ftpFailed(ftpState, ERR_FTP_PUT_ERROR); | |
2238 | return; | |
2239 | } | |
2240 | storeTimestampsSet(ftpState->entry); /* XXX Is this needed? */ | |
2241 | ftpSendReply(ftpState); | |
969c39b9 | 2242 | } |
2243 | ||
2244 | static void | |
2245 | ftpSendQuit(FtpStateData * ftpState) | |
2246 | { | |
033fa114 | 2247 | assert(ftpState->ctrl.fd > -1); |
56878878 | 2248 | snprintf(cbuf, 1024, "QUIT\r\n"); |
3fdadc70 | 2249 | ftpWriteCommand(cbuf, ftpState); |
2250 | ftpState->state = SENT_QUIT; | |
2251 | } | |
2252 | ||
2253 | static void | |
2254 | ftpReadQuit(FtpStateData * ftpState) | |
2255 | { | |
2256 | comm_close(ftpState->ctrl.fd); | |
2257 | } | |
2258 | ||
969c39b9 | 2259 | static void |
2260 | ftpTrySlashHack(FtpStateData * ftpState) | |
2261 | { | |
2262 | char *path; | |
e55f0142 | 2263 | ftpState->flags.try_slash_hack = 1; |
969c39b9 | 2264 | /* Free old paths */ |
2265 | if (ftpState->pathcomps) | |
2266 | wordlistDestroy(&ftpState->pathcomps); | |
2267 | safe_free(ftpState->filepath); | |
2268 | /* Build the new path (urlpath begins with /) */ | |
02922e76 | 2269 | path = xstrdup(strBuf(ftpState->request->urlpath)); |
969c39b9 | 2270 | rfc1738_unescape(path); |
2271 | ftpState->filepath = path; | |
2272 | /* And off we go */ | |
dbfed404 | 2273 | ftpGetFile(ftpState); |
969c39b9 | 2274 | } |
2275 | ||
0f169992 | 2276 | static void |
2277 | ftpTryDatachannelHack(FtpStateData * ftpState) | |
2278 | { | |
e55f0142 | 2279 | ftpState->flags.datachannel_hack = 1; |
0f169992 | 2280 | /* we have to undo some of the slash hack... */ |
2281 | if (ftpState->old_filepath != NULL) { | |
e55f0142 | 2282 | ftpState->flags.try_slash_hack = 0; |
0f169992 | 2283 | safe_free(ftpState->filepath); |
2284 | ftpState->filepath = ftpState->old_filepath; | |
2285 | ftpState->old_filepath = NULL; | |
2286 | } | |
e55f0142 | 2287 | ftpState->flags.tried_nlst = 0; |
0f169992 | 2288 | /* And off we go */ |
e55f0142 | 2289 | if (ftpState->flags.isdir) { |
0f169992 | 2290 | ftpListDir(ftpState); |
2291 | } else { | |
2292 | ftpGetFile(ftpState); | |
2293 | } | |
2294 | return; | |
2295 | } | |
2296 | ||
2297 | /* Forget hack status. Next error is shown to the user */ | |
2298 | static void | |
2299 | ftpUnhack(FtpStateData * ftpState) | |
2300 | { | |
2301 | if (ftpState->old_request != NULL) { | |
2302 | safe_free(ftpState->old_request); | |
2303 | safe_free(ftpState->old_reply); | |
2304 | } | |
2305 | } | |
2306 | ||
969c39b9 | 2307 | static void |
2308 | ftpHackShortcut(FtpStateData * ftpState, FTPSM * nextState) | |
2309 | { | |
c7e0305b | 2310 | /* Clear some unwanted state */ |
2311 | ftpState->restarted_offset = 0; | |
2312 | ftpState->restart_offset = 0; | |
0f169992 | 2313 | /* Save old error message & some state info */ |
2314 | if (ftpState->old_request == NULL) { | |
2315 | ftpState->old_request = ftpState->ctrl.last_command; | |
2316 | ftpState->ctrl.last_command = NULL; | |
2317 | ftpState->old_reply = ftpState->ctrl.last_reply; | |
2318 | ftpState->ctrl.last_reply = NULL; | |
2319 | if (ftpState->pathcomps == NULL && ftpState->filepath != NULL) | |
2320 | ftpState->old_filepath = xstrdup(ftpState->filepath); | |
2321 | } | |
969c39b9 | 2322 | /* Jump to the "hack" state */ |
2323 | nextState(ftpState); | |
2324 | } | |
2325 | ||
3fdadc70 | 2326 | static void |
2327 | ftpFail(FtpStateData * ftpState) | |
2328 | { | |
a3d5953d | 2329 | debug(9, 3) ("ftpFail\n"); |
dbfed404 | 2330 | /* Try the / hack to support "Netscape" FTP URL's for retreiving files */ |
0cdcddb9 | 2331 | if (!ftpState->flags.isdir && /* Not a directory */ |
2332 | !ftpState->flags.try_slash_hack && /* Not in slash hack */ | |
2a1ca944 | 2333 | ftpState->mdtm <= 0 && ftpState->size < 0 && /* Not known as a file */ |
c7e0305b | 2334 | strNCaseCmp(ftpState->request->urlpath, "/%2f", 4) != 0) { /* No slash encoded */ |
969c39b9 | 2335 | switch (ftpState->state) { |
2336 | case SENT_CWD: | |
2337 | case SENT_RETR: | |
2338 | /* Try the / hack */ | |
2339 | ftpHackShortcut(ftpState, ftpTrySlashHack); | |
2340 | return; | |
2341 | default: | |
2342 | break; | |
2343 | } | |
2344 | } | |
0f169992 | 2345 | /* Try to reopen datachannel */ |
e55f0142 | 2346 | if (!ftpState->flags.datachannel_hack && |
0f169992 | 2347 | ftpState->pathcomps == NULL) { |
2348 | switch (ftpState->state) { | |
2349 | case SENT_RETR: | |
2350 | case SENT_LIST: | |
2351 | case SENT_NLST: | |
2352 | /* Try to reopen datachannel */ | |
2353 | ftpHackShortcut(ftpState, ftpTryDatachannelHack); | |
2354 | return; | |
2355 | default: | |
2356 | break; | |
2357 | } | |
2358 | } | |
9bc73deb | 2359 | ftpFailed(ftpState, ERR_NONE); |
2360 | /* ftpFailed closes ctrl.fd and frees ftpState */ | |
2361 | } | |
2362 | ||
2363 | static void | |
2364 | ftpFailed(FtpStateData * ftpState, err_type error) | |
2365 | { | |
2366 | StoreEntry *entry = ftpState->entry; | |
2367 | if (entry->mem_obj->inmem_hi == 0) | |
2368 | ftpFailedErrorMessage(ftpState, error); | |
2369 | if (ftpState->data.fd > -1) { | |
2370 | comm_close(ftpState->data.fd); | |
2371 | ftpState->data.fd = -1; | |
2372 | } | |
2373 | comm_close(ftpState->ctrl.fd); | |
2374 | } | |
2375 | ||
2376 | static void | |
2377 | ftpFailedErrorMessage(FtpStateData * ftpState, err_type error) | |
2378 | { | |
2379 | ErrorState *err; | |
2380 | char *command, *reply; | |
b6a2f15e | 2381 | /* Translate FTP errors into HTTP errors */ |
2382 | err = NULL; | |
9bc73deb | 2383 | switch (error) { |
2384 | case ERR_NONE: | |
2385 | switch (ftpState->state) { | |
2386 | case SENT_USER: | |
2387 | case SENT_PASS: | |
2388 | if (ftpState->ctrl.replycode > 500) | |
2389 | err = errorCon(ERR_FTP_FORBIDDEN, HTTP_FORBIDDEN); | |
2390 | else if (ftpState->ctrl.replycode == 421) | |
2391 | err = errorCon(ERR_FTP_UNAVAILABLE, HTTP_SERVICE_UNAVAILABLE); | |
2392 | break; | |
2393 | case SENT_CWD: | |
2394 | case SENT_RETR: | |
2395 | if (ftpState->ctrl.replycode == 550) | |
2396 | err = errorCon(ERR_FTP_NOT_FOUND, HTTP_NOT_FOUND); | |
2397 | break; | |
2398 | default: | |
2399 | break; | |
2400 | } | |
b6a2f15e | 2401 | break; |
9bc73deb | 2402 | case ERR_READ_TIMEOUT: |
2403 | err = errorCon(error, HTTP_GATEWAY_TIMEOUT); | |
b6a2f15e | 2404 | break; |
2405 | default: | |
9bc73deb | 2406 | err = errorCon(error, HTTP_BAD_GATEWAY); |
b6a2f15e | 2407 | break; |
2408 | } | |
2409 | if (err == NULL) | |
2410 | err = errorCon(ERR_FTP_FAILURE, HTTP_BAD_GATEWAY); | |
9bc73deb | 2411 | err->xerrno = errno; |
b9916917 | 2412 | err->request = requestLink(ftpState->request); |
d20b1cd0 | 2413 | err->ftp.server_msg = ftpState->ctrl.message; |
2414 | ftpState->ctrl.message = NULL; | |
969c39b9 | 2415 | if (ftpState->old_request) |
9bc73deb | 2416 | command = ftpState->old_request; |
969c39b9 | 2417 | else |
9bc73deb | 2418 | command = ftpState->ctrl.last_command; |
2419 | if (command && strncmp(command, "PASS", 4) == 0) | |
2420 | command = "PASS <yourpassword>"; | |
969c39b9 | 2421 | if (ftpState->old_reply) |
9bc73deb | 2422 | reply = ftpState->old_reply; |
969c39b9 | 2423 | else |
9bc73deb | 2424 | reply = ftpState->ctrl.last_reply; |
2425 | if (command) | |
2426 | err->ftp.request = xstrdup(command); | |
2427 | if (reply) | |
2428 | err->ftp.reply = xstrdup(reply); | |
2429 | fwdFail(ftpState->fwd, err); | |
3fdadc70 | 2430 | } |
2431 | ||
54220df8 | 2432 | static void |
2433 | ftpSendReply(FtpStateData * ftpState) | |
2434 | { | |
2435 | ErrorState *err; | |
acce49bf | 2436 | int code = ftpState->ctrl.replycode; |
728da2ee | 2437 | http_status http_code; |
2438 | err_type err_code = ERR_NONE; | |
c3b838d4 | 2439 | debug(9, 5) ("ftpSendReply: %s, code %d\n", |
2440 | storeUrl(ftpState->entry), code); | |
54220df8 | 2441 | if (cbdataValid(ftpState)) |
acce49bf | 2442 | debug(9, 5) ("ftpSendReply: ftpState (%p) is valid!\n", ftpState); |
2443 | if (code == 226) { | |
2444 | err_code = (ftpState->mdtm > 0) ? ERR_FTP_PUT_MODIFIED : ERR_FTP_PUT_CREATED; | |
2445 | http_code = (ftpState->mdtm > 0) ? HTTP_ACCEPTED : HTTP_CREATED; | |
9bc73deb | 2446 | } else if (code == 227) { |
2447 | err_code = ERR_FTP_PUT_CREATED; | |
2448 | http_code = HTTP_CREATED; | |
54220df8 | 2449 | } else { |
2450 | err_code = ERR_FTP_PUT_ERROR; | |
acce49bf | 2451 | http_code = HTTP_INTERNAL_SERVER_ERROR; |
54220df8 | 2452 | } |
54220df8 | 2453 | err = errorCon(err_code, http_code); |
2454 | err->request = requestLink(ftpState->request); | |
2455 | if (ftpState->old_request) | |
9bc73deb | 2456 | err->ftp.request = xstrdup(ftpState->old_request); |
54220df8 | 2457 | else |
9bc73deb | 2458 | err->ftp.request = xstrdup(ftpState->ctrl.last_command); |
54220df8 | 2459 | if (ftpState->old_reply) |
9bc73deb | 2460 | err->ftp.reply = xstrdup(ftpState->old_reply); |
54220df8 | 2461 | else |
9bc73deb | 2462 | err->ftp.reply = xstrdup(ftpState->ctrl.last_reply); |
54220df8 | 2463 | errorAppendEntry(ftpState->entry, err); |
54220df8 | 2464 | storeBufferFlush(ftpState->entry); |
9bc73deb | 2465 | ftpSendQuit(ftpState); |
54220df8 | 2466 | } |
2467 | ||
3fdadc70 | 2468 | static void |
2469 | ftpAppendSuccessHeader(FtpStateData * ftpState) | |
2470 | { | |
2471 | char *mime_type = NULL; | |
2472 | char *mime_enc = NULL; | |
02922e76 | 2473 | String urlpath = ftpState->request->urlpath; |
2474 | const char *filename = NULL; | |
2475 | const char *t = NULL; | |
3fdadc70 | 2476 | StoreEntry *e = ftpState->entry; |
c68e9c6b | 2477 | StoreEntry *pe = NULL; |
9e975e4e | 2478 | http_reply *reply = e->mem_obj->reply; |
ccf44862 | 2479 | http_version_t version; |
e55f0142 | 2480 | if (ftpState->flags.http_header_sent) |
3fdadc70 | 2481 | return; |
e55f0142 | 2482 | ftpState->flags.http_header_sent = 1; |
8350fe9b | 2483 | assert(e->mem_obj->inmem_hi == 0); |
2749ab78 | 2484 | EBIT_CLR(e->flags, ENTRY_FWD_HDR_WAIT); |
02922e76 | 2485 | filename = (t = strRChr(urlpath, '/')) ? t + 1 : strBuf(urlpath); |
e55f0142 | 2486 | if (ftpState->flags.isdir) { |
3fdadc70 | 2487 | mime_type = "text/html"; |
2488 | } else { | |
31b4984a | 2489 | switch (ftpState->typecode) { |
2490 | case 'I': | |
2491 | mime_type = "application/octet-stream"; | |
2492 | mime_enc = mimeGetContentEncoding(filename); | |
2493 | break; | |
2494 | case 'A': | |
2495 | mime_type = "text/plain"; | |
2496 | break; | |
2497 | default: | |
2498 | mime_type = mimeGetContentType(filename); | |
2499 | mime_enc = mimeGetContentEncoding(filename); | |
2500 | break; | |
2501 | } | |
3fdadc70 | 2502 | } |
438fc1e3 | 2503 | storeBuffer(e); |
cb69b4c7 | 2504 | httpReplyReset(reply); |
2505 | /* set standard stuff */ | |
cfbf5373 | 2506 | if (ftpState->restarted_offset) { |
2507 | /* Partial reply */ | |
c2352aa2 | 2508 | HttpHdrRangeSpec range_spec; |
e3dd531e | 2509 | range_spec.offset = ftpState->restarted_offset; |
c2352aa2 | 2510 | range_spec.length = ftpState->size - ftpState->restarted_offset; |
9ee7f97b | 2511 | httpBuildVersion(&version, 1, 0); |
ccf44862 | 2512 | httpReplySetHeaders(reply, version, HTTP_PARTIAL_CONTENT, "Gatewaying", |
cfbf5373 | 2513 | mime_type, ftpState->size - ftpState->restarted_offset, ftpState->mdtm, -2); |
2514 | httpHeaderAddContRange(&reply->header, range_spec, ftpState->size); | |
2515 | } else { | |
2516 | /* Full reply */ | |
9ee7f97b | 2517 | httpBuildVersion(&version, 1, 0); |
ccf44862 | 2518 | httpReplySetHeaders(reply, version, HTTP_OK, "Gatewaying", |
cfbf5373 | 2519 | mime_type, ftpState->size, ftpState->mdtm, -2); |
2520 | } | |
cb69b4c7 | 2521 | /* additional info */ |
2522 | if (mime_enc) | |
d8b249ef | 2523 | httpHeaderPutStr(&reply->header, HDR_CONTENT_ENCODING, mime_enc); |
cb69b4c7 | 2524 | httpReplySwapOut(reply, e); |
438fc1e3 | 2525 | storeBufferFlush(e); |
2acf4be6 | 2526 | reply->hdr_sz = e->mem_obj->inmem_hi; |
e6b02cfc | 2527 | storeTimestampsSet(e); |
c68e9c6b | 2528 | if (ftpState->flags.authenticated) { |
2529 | /* | |
2530 | * Authenticated requests can't be cached. Eject any old cached | |
2531 | * object | |
2532 | */ | |
2533 | pe = storeGetPublic(e->mem_obj->url, e->mem_obj->method); | |
2534 | if (pe) | |
2535 | storeRelease(pe); | |
2536 | storeRelease(e); | |
cfbf5373 | 2537 | } else if (EBIT_TEST(e->flags, ENTRY_CACHABLE) && !ftpState->restarted_offset) { |
40ced4c0 | 2538 | storeSetPublicKey(e); |
c68e9c6b | 2539 | } else { |
2540 | storeRelease(e); | |
2541 | } | |
77a30ebb | 2542 | } |
bfcaf585 | 2543 | |
cb69b4c7 | 2544 | static void |
ee1679df | 2545 | ftpAuthRequired(HttpReply * old_reply, request_t * request, const char *realm) |
cb69b4c7 | 2546 | { |
2547 | ErrorState *err = errorCon(ERR_ACCESS_DENIED, HTTP_UNAUTHORIZED); | |
2548 | HttpReply *rep; | |
2549 | err->request = requestLink(request); | |
2550 | rep = errorBuildReply(err); | |
cb69b4c7 | 2551 | errorStateFree(err); |
63259c34 | 2552 | /* add Authenticate header */ |
d8b249ef | 2553 | httpHeaderPutAuth(&rep->header, "Basic", realm); |
63259c34 | 2554 | /* move new reply to the old one */ |
2555 | httpReplyAbsorb(old_reply, rep); | |
cb69b4c7 | 2556 | } |
8f872bb6 | 2557 | |
2558 | char * | |
2559 | ftpUrlWith2f(const request_t * request) | |
2560 | { | |
2561 | LOCAL_ARRAY(char, buf, MAX_URL); | |
2562 | LOCAL_ARRAY(char, loginbuf, MAX_LOGIN_SZ + 1); | |
2563 | LOCAL_ARRAY(char, portbuf, 32); | |
2564 | char *t; | |
2565 | portbuf[0] = '\0'; | |
23d92c64 | 2566 | if (request->protocol != PROTO_FTP) |
2567 | return NULL; | |
8f872bb6 | 2568 | if (request->port != urlDefaultPort(request->protocol)) |
2569 | snprintf(portbuf, 32, ":%d", request->port); | |
2570 | loginbuf[0] = '\0'; | |
69e81830 | 2571 | if ((int) strlen(request->login) > 0) { |
8f872bb6 | 2572 | strcpy(loginbuf, request->login); |
2573 | if ((t = strchr(loginbuf, ':'))) | |
2574 | *t = '\0'; | |
2575 | strcat(loginbuf, "@"); | |
2576 | } | |
2577 | snprintf(buf, MAX_URL, "%s://%s%s%s%s%s", | |
2578 | ProtocolStr[request->protocol], | |
2579 | loginbuf, | |
2580 | request->host, | |
2581 | portbuf, | |
2582 | "/%2f", | |
02922e76 | 2583 | strBuf(request->urlpath)); |
8f872bb6 | 2584 | if ((t = strchr(buf, '?'))) |
2585 | *t = '\0'; | |
2586 | return buf; | |
2587 | } |