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