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