FtpGateway.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 09 File Transfer Protocol (FTP) */
10 
11 #include "squid.h"
12 #include "acl/FilledChecklist.h"
13 #include "base/PackableStream.h"
14 #include "clients/forward.h"
15 #include "clients/FtpClient.h"
16 #include "comm.h"
17 #include "comm/ConnOpener.h"
18 #include "comm/Read.h"
19 #include "comm/TcpAcceptor.h"
20 #include "CommCalls.h"
21 #include "compat/socket.h"
22 #include "compat/strtoll.h"
23 #include "errorpage.h"
24 #include "fd.h"
25 #include "fde.h"
26 #include "FwdState.h"
27 #include "html/Quoting.h"
28 #include "HttpHdrContRange.h"
29 #include "HttpHeader.h"
30 #include "HttpHeaderRange.h"
31 #include "HttpReply.h"
32 #include "ip/tools.h"
33 #include "MemBuf.h"
34 #include "mime.h"
35 #include "rfc1738.h"
36 #include "SquidConfig.h"
37 #include "SquidString.h"
38 #include "StatCounters.h"
39 #include "Store.h"
40 #include "tools.h"
41 #include "util.h"
42 #include "wordlist.h"
43 
44 #if USE_DELAY_POOLS
45 #include "DelayPools.h"
46 #include "MemObject.h"
47 #endif
48 
49 #include <cerrno>
50 #if HAVE_REGEX_H
51 #include <regex.h>
52 #endif
53 
54 namespace Ftp
55 {
56 
57 struct GatewayFlags {
58 
59  /* passive mode */
62  bool pasv_only;
63  bool pasv_failed; // was FwdState::flags.ftp_pasv_failed
64 
65  /* authentication */
69 
70  /* other */
71  bool isdir;
75  bool tried_nlst;
77  bool dir_slash;
78  bool root_dir;
79  bool no_dotdot;
80  bool binary;
82  bool put;
83  bool put_mkdir;
85  bool listing;
87 };
88 
89 class Gateway;
90 typedef void (StateMethod)(Ftp::Gateway *);
91 
95 class Gateway : public Ftp::Client
96 {
98 
99 public:
100  Gateway(FwdState *);
101  ~Gateway() override;
102  char user[MAX_URL];
105  char *reply_hdr;
110  int conn_att;
112  time_t mdtm;
113  int64_t theSize;
115  char *filepath;
116  char *dirpath;
117  int64_t restart_offset;
118  char *proxy_host;
119  size_t list_width;
122  char typecode;
124 
126 
127 public:
128  // these should all be private
129  void start() override;
131  int restartable();
132  void appendSuccessHeader();
133  void hackShortcut(StateMethod *nextState);
134  void unhack();
135  void readStor();
136  void parseListing();
137  bool htmlifyListEntry(const char *line, PackableStream &);
138  void completedListing(void);
139 
142 
143  int checkAuth(const HttpHeader * req_hdr);
144  void checkUrlpath();
145  void buildTitleUrl();
146  void writeReplyBody(const char *, size_t len);
147  void completeForwarding() override;
148  void processHeadResponse();
149  void processReplyBody() override;
150  void setCurrentOffset(int64_t offset) { currentOffset = offset; }
151  int64_t getCurrentOffset() const { return currentOffset; }
152 
153  void dataChannelConnected(const CommConnectCbParams &io) override;
154  static PF ftpDataWrite;
155  void timeout(const CommTimeoutCbParams &io) override;
157 
159  SBuf ftpRealm();
160  void loginFailed(void);
161 
162  void haveParsedReplyHeaders() override;
163 
164  virtual bool haveControlChannel(const char *caller_name) const;
165 
166 protected:
167  void handleControlReply() override;
168  void dataClosed(const CommCloseCbParams &io) override;
169 
170 private:
171  bool mayReadVirginReplyBody() const override;
172  // BodyConsumer for HTTP: consume request body.
173  void handleRequestBodyProducerAborted() override;
174 
175  void loginParser(const SBuf &login, bool escaped);
176 };
177 
178 } // namespace Ftp
179 
180 typedef Ftp::StateMethod FTPSM; // to avoid lots of non-changes
181 
183 
184 typedef struct {
185  char type;
186  int64_t size;
187  char *date;
188  char *name;
189  char *showname;
190  char *link;
191 } ftpListParts;
192 
193 #define CTRL_BUFLEN 16*1024
194 static char cbuf[CTRL_BUFLEN];
195 
196 /*
197  * State machine functions
198  * send == state transition
199  * read == wait for response, and select next state transition
200  * other == Transition logic
201  */
239 static FTPSM ftpFail;
242 
243 /************************************************
244 ** Debugs Levels used here **
245 *************************************************
246 0 CRITICAL Events
247 1 IMPORTANT Events
248  Protocol and Transmission failures.
249 2 FTP Protocol Chatter
250 3 Logic Flows
251 4 Data Parsing Flows
252 5 Data Dumps
253 7 ??
254 ************************************************/
255 
256 /************************************************
257 ** State Machine Description (excluding hacks) **
258 *************************************************
259 From To
260 ---------------------------------------
261 Welcome User
262 User Pass
263 Pass Type
264 Type TraverseDirectory / GetFile
265 TraverseDirectory Cwd / GetFile / ListDir
266 Cwd TraverseDirectory / Mkdir
267 GetFile Mdtm
268 Mdtm Size
269 Size Epsv
270 ListDir Epsv
271 Epsv FileOrList
272 FileOrList Rest / Retr / Nlst / List / Mkdir (PUT /xxx;type=d)
273 Rest Retr
274 Retr / Nlst / List DataRead* (on datachannel)
275 DataRead* ReadTransferDone
276 ReadTransferDone DataTransferDone
277 Stor DataWrite* (on datachannel)
278 DataWrite* RequestPutBody** (from client)
279 RequestPutBody** DataWrite* / WriteTransferDone
280 WriteTransferDone DataTransferDone
281 DataTransferDone Quit
282 Quit -
283 ************************************************/
284 
286  ftpReadWelcome, /* BEGIN */
287  ftpReadUser, /* SENT_USER */
288  ftpReadPass, /* SENT_PASS */
289  ftpReadType, /* SENT_TYPE */
290  ftpReadMdtm, /* SENT_MDTM */
291  ftpReadSize, /* SENT_SIZE */
292  ftpReadEPRT, /* SENT_EPRT */
293  ftpReadPORT, /* SENT_PORT */
294  ftpReadEPSV, /* SENT_EPSV_ALL */
295  ftpReadEPSV, /* SENT_EPSV_1 */
296  ftpReadEPSV, /* SENT_EPSV_2 */
297  ftpReadPasv, /* SENT_PASV */
298  ftpReadCwd, /* SENT_CWD */
299  ftpReadList, /* SENT_LIST */
300  ftpReadList, /* SENT_NLST */
301  ftpReadRest, /* SENT_REST */
302  ftpReadRetr, /* SENT_RETR */
303  ftpReadStor, /* SENT_STOR */
304  ftpReadQuit, /* SENT_QUIT */
305  ftpReadTransferDone, /* READING_DATA (RETR,LIST,NLST) */
306  ftpWriteTransferDone, /* WRITING_DATA (STOR) */
307  ftpReadMkdir, /* SENT_MKDIR */
308  nullptr, /* SENT_FEAT */
309  nullptr, /* SENT_PWD */
310  nullptr, /* SENT_CDUP*/
311  nullptr, /* SENT_DATA_REQUEST */
312  nullptr /* SENT_COMMAND */
313 };
314 
316 void
318 {
321  /* failed closes ctrl.conn and frees ftpState */
322 
323  /* NP: failure recovery may be possible when its only a data.conn failure.
324  * if the ctrl.conn is still fine, we can send ABOR down it and retry.
325  * Just need to watch out for wider Squid states like shutting down or reconfigure.
326  */
327 }
328 
330  AsyncJob("FtpStateData"),
331  Ftp::Client(fwdState),
332  password_url(0),
333  reply_hdr(nullptr),
334  reply_hdr_state(0),
335  conn_att(0),
336  login_att(0),
337  mdtm(-1),
338  theSize(-1),
339  pathcomps(nullptr),
340  filepath(nullptr),
341  dirpath(nullptr),
342  restart_offset(0),
343  proxy_host(nullptr),
344  list_width(0),
345  old_filepath(nullptr),
346  typecode('\0')
347 {
348  debugs(9, 3, entry->url());
349 
350  *user = 0;
351  *password = 0;
352  memset(&flags, 0, sizeof(flags));
353 
355  flags.pasv_supported = 1;
356 
357  flags.rest_supported = 1;
358 
360  flags.put = 1;
361 
362  initReadBuf();
363 }
364 
366 {
367  debugs(9, 3, entry->url());
368 
369  if (Comm::IsConnOpen(ctrl.conn)) {
370  debugs(9, DBG_IMPORTANT, "ERROR: Squid BUG: FTP Gateway left open " <<
371  "control channel " << ctrl.conn);
372  }
373 
374  if (reply_hdr) {
375  memFree(reply_hdr, MEM_8K_BUF);
376  reply_hdr = nullptr;
377  }
378 
379  if (pathcomps)
380  wordlistDestroy(&pathcomps);
381 
382  cwd_message.clean();
383  xfree(old_filepath);
384  title_url.clean();
385  base_href.clean();
386  xfree(filepath);
387  xfree(dirpath);
388 }
389 
397 void
398 Ftp::Gateway::loginParser(const SBuf &login, bool escaped)
399 {
400  debugs(9, 4, "login=" << login << ", escaped=" << escaped);
401  debugs(9, 9, "IN : login=" << login << ", escaped=" << escaped << ", user=" << user << ", password=" << password);
402 
403  if (login.isEmpty())
404  return;
405 
406  if (!login[0]) {
407  debugs(9, 2, "WARNING: Ignoring FTP credentials that start with a NUL character");
408  // TODO: Either support credentials with NUL characters (in any position) or ban all of them.
409  return;
410  }
411 
412  const SBuf::size_type colonPos = login.find(':');
413 
414  /* If there was a username part with at least one character use it.
415  * Ignore 0-length username portion, retain what we have already.
416  */
417  if (colonPos == SBuf::npos || colonPos > 0) {
418  const SBuf userName = login.substr(0, colonPos);
419  SBuf::size_type upto = userName.copy(user, sizeof(user)-1);
420  user[upto]='\0';
421  debugs(9, 9, "found user=" << userName << ' ' <<
422  (upto != userName.length() ? ", truncated-to=" : ", length=") << upto <<
423  ", escaped=" << escaped);
424  if (escaped)
425  rfc1738_unescape(user);
426  debugs(9, 9, "found user=" << user << " (" << strlen(user) << ") unescaped.");
427  }
428 
429  /* If there was a password part.
430  * For 0-length password clobber what we have already, this means explicitly none
431  */
432  if (colonPos != SBuf::npos) {
433  const SBuf pass = login.substr(colonPos+1, SBuf::npos);
434  SBuf::size_type upto = pass.copy(password, sizeof(password)-1);
435  password[upto]='\0';
436  debugs(9, 9, "found password=" << pass << " " <<
437  (upto != pass.length() ? ", truncated-to=" : ", length=") << upto <<
438  ", escaped=" << escaped);
439  if (escaped) {
440  rfc1738_unescape(password);
441  password_url = 1;
442  }
443  debugs(9, 9, "found password=" << password << " (" << strlen(password) << ") unescaped.");
444  }
445 
446  debugs(9, 9, "OUT: login=" << login << ", escaped=" << escaped << ", user=" << user << ", password=" << password);
447 }
448 
449 void
451 {
452  if (!Comm::IsConnOpen(ctrl.conn)) {
453  debugs(9, 5, "The control connection to the remote end is closed");
454  return;
455  }
456 
457  assert(!Comm::IsConnOpen(data.conn));
458 
459  typedef CommCbMemFunT<Gateway, CommAcceptCbParams> AcceptDialer;
460  typedef AsyncCallT<AcceptDialer> AcceptCall;
461  const auto call = JobCallback(11, 5, AcceptDialer, this, Ftp::Gateway::ftpAcceptDataConnection);
463  const char *note = entry->url();
464 
465  /* open the conn if its not already open */
466  if (!Comm::IsConnOpen(conn)) {
467  conn->fd = comm_open_listener(SOCK_STREAM, IPPROTO_TCP, conn->local, conn->flags, note);
468  if (!Comm::IsConnOpen(conn)) {
469  debugs(5, DBG_CRITICAL, "ERROR: comm_open_listener failed:" << conn->local << " error: " << errno);
470  return;
471  }
472  debugs(9, 3, "Unconnected data socket created on " << conn);
473  }
474 
475  conn->tos = ctrl.conn->tos;
476  conn->nfmark = ctrl.conn->nfmark;
477 
478  assert(Comm::IsConnOpen(conn));
479  AsyncJob::Start(new Comm::TcpAcceptor(conn, note, sub));
480 
481  // Ensure we have a copy of the FD opened for listening and a close handler on it.
482  data.opened(conn, dataCloser());
483  switchTimeoutToDataChannel();
484 }
485 
486 void
488 {
489  if (SENT_PASV == state) {
490  /* stupid ftp.netscape.com, of FTP server behind stupid firewall rules */
491  flags.pasv_supported = false;
492  debugs(9, DBG_IMPORTANT, "FTP Gateway timeout in SENT_PASV state");
493 
494  // cancel the data connection setup, if any
495  dataConnWait.cancel("timeout");
496 
497  data.close();
498  }
499 
501 }
502 
503 static const char *Month[] = {
504  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
505  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
506 };
507 
508 static int
509 is_month(const char *buf)
510 {
511  int i;
512 
513  for (i = 0; i < 12; ++i)
514  if (!strcasecmp(buf, Month[i]))
515  return 1;
516 
517  return 0;
518 }
519 
520 static void
522 {
523  safe_free((*parts)->date);
524  safe_free((*parts)->name);
525  safe_free((*parts)->showname);
526  safe_free((*parts)->link);
527  safe_free(*parts);
528 }
529 
530 #define MAX_TOKENS 64
531 
532 static ftpListParts *
533 ftpListParseParts(const char *buf, struct Ftp::GatewayFlags flags)
534 {
535  ftpListParts *p = nullptr;
536  char *t = nullptr;
537  struct FtpLineToken {
538  char *token = nullptr;
539  size_t pos = 0;
540  } tokens[MAX_TOKENS];
541  int i;
542  int n_tokens;
543  static char tbuf[128];
544  char *xbuf = nullptr;
545  static int scan_ftp_initialized = 0;
546  static regex_t scan_ftp_integer;
547  static regex_t scan_ftp_time;
548  static regex_t scan_ftp_dostime;
549  static regex_t scan_ftp_dosdate;
550 
551  if (!scan_ftp_initialized) {
552  scan_ftp_initialized = 1;
553  regcomp(&scan_ftp_integer, "^[0123456789]+$", REG_EXTENDED | REG_NOSUB);
554  regcomp(&scan_ftp_time, "^[0123456789:]+$", REG_EXTENDED | REG_NOSUB);
555  regcomp(&scan_ftp_dosdate, "^[0123456789]+-[0123456789]+-[0123456789]+$", REG_EXTENDED | REG_NOSUB);
556  regcomp(&scan_ftp_dostime, "^[0123456789]+:[0123456789]+[AP]M$", REG_EXTENDED | REG_NOSUB | REG_ICASE);
557  }
558 
559  if (buf == nullptr)
560  return nullptr;
561 
562  if (*buf == '\0')
563  return nullptr;
564 
565  p = (ftpListParts *)xcalloc(1, sizeof(ftpListParts));
566 
567  n_tokens = 0;
568 
569  xbuf = xstrdup(buf);
570 
571  if (flags.tried_nlst) {
572  /* Machine readable format, one name per line */
573  p->name = xbuf;
574  p->type = '\0';
575  return p;
576  }
577 
578  for (t = strtok(xbuf, w_space); t && n_tokens < MAX_TOKENS; t = strtok(nullptr, w_space)) {
579  tokens[n_tokens].token = xstrdup(t);
580  tokens[n_tokens].pos = t - xbuf;
581  ++n_tokens;
582  }
583 
584  xfree(xbuf);
585 
586  /* locate the Month field */
587  for (i = 3; i < n_tokens - 2; ++i) {
588  const auto size = tokens[i - 1].token;
589  char *month = tokens[i].token;
590  char *day = tokens[i + 1].token;
591  char *year = tokens[i + 2].token;
592 
593  if (!is_month(month))
594  continue;
595 
596  if (regexec(&scan_ftp_integer, size, 0, nullptr, 0) != 0)
597  continue;
598 
599  if (regexec(&scan_ftp_integer, day, 0, nullptr, 0) != 0)
600  continue;
601 
602  if (regexec(&scan_ftp_time, year, 0, nullptr, 0) != 0) /* Yr | hh:mm */
603  continue;
604 
605  const auto *copyFrom = buf + tokens[i].pos;
606 
607  // "MMM DD [ YYYY|hh:mm]" with at most two spaces between DD and YYYY
608  auto dateSize = snprintf(tbuf, sizeof(tbuf), "%s %2s %5s", month, day, year);
609  bool isTypeA = (dateSize == 12) && (strncmp(copyFrom, tbuf, dateSize) == 0);
610 
611  // "MMM DD [YYYY|hh:mm]" with one space between DD and YYYY
612  dateSize = snprintf(tbuf, sizeof(tbuf), "%s %2s %-5s", month, day, year);
613  bool isTypeB = (dateSize == 12 || dateSize == 11) && (strncmp(copyFrom, tbuf, dateSize) == 0);
614 
615  // TODO: replace isTypeA and isTypeB with a regex.
616  if (isTypeA || isTypeB) {
617  p->type = *tokens[0].token;
618  p->size = strtoll(size, nullptr, 10);
619  const auto finalDateSize = snprintf(tbuf, sizeof(tbuf), "%s %2s %5s", month, day, year);
620  assert(finalDateSize >= 0);
621  p->date = xstrdup(tbuf);
622 
623  // point after tokens[i+2] :
624  copyFrom = buf + tokens[i + 2].pos + strlen(tokens[i + 2].token);
625  if (flags.skip_whitespace) {
626  while (strchr(w_space, *copyFrom))
627  ++copyFrom;
628  } else {
629  /* Handle the following four formats:
630  * "MMM DD YYYY Name"
631  * "MMM DD YYYYName"
632  * "MMM DD YYYY Name"
633  * "MMM DD YYYY Name"
634  * Assuming a single space between date and filename
635  * suggested by: Nathan.Bailey@cc.monash.edu.au and
636  * Mike Battersby <mike@starbug.bofh.asn.au> */
637  if (strchr(w_space, *copyFrom))
638  ++copyFrom;
639  }
640 
641  p->name = xstrdup(copyFrom);
642 
643  if (p->type == 'l' && (t = strstr(p->name, " -> "))) {
644  *t = '\0';
645  p->link = xstrdup(t + 4);
646  }
647 
648  goto found;
649  }
650 
651  break;
652  }
653 
654  /* try it as a DOS listing, 04-05-70 09:33PM ... */
655  if (n_tokens > 3 &&
656  regexec(&scan_ftp_dosdate, tokens[0].token, 0, nullptr, 0) == 0 &&
657  regexec(&scan_ftp_dostime, tokens[1].token, 0, nullptr, 0) == 0) {
658  if (!strcasecmp(tokens[2].token, "<dir>")) {
659  p->type = 'd';
660  } else {
661  p->type = '-';
662  p->size = strtoll(tokens[2].token, nullptr, 10);
663  }
664 
665  snprintf(tbuf, sizeof(tbuf), "%s %s", tokens[0].token, tokens[1].token);
666  p->date = xstrdup(tbuf);
667 
668  if (p->type == 'd') {
669  // Directory.. name begins with first printable after <dir>
670  // Because of the "n_tokens > 3", the next printable after <dir>
671  // is stored at token[3]. No need for more checks here.
672  } else {
673  // A file. Name begins after size, with a space in between.
674  // Also a space should exist before size.
675  // But there is not needed to be very strict with spaces.
676  // The name is stored at token[3], take it from here.
677  }
678 
679  p->name = xstrdup(tokens[3].token);
680  goto found;
681  }
682 
683  /* Try EPLF format; carson@lehman.com */
684  if (buf[0] == '+') {
685  const char *ct = buf + 1;
686  p->type = 0;
687 
688  while (ct && *ct) {
689  time_t tm;
690  int l = strcspn(ct, ",");
691  char *tmp;
692 
693  if (l < 1)
694  goto blank;
695 
696  switch (*ct) {
697 
698  case '\t':
699  safe_free(p->name); // TODO: properly handle multiple p->name occurrences
700  p->name = xstrndup(ct + 1, l + 1);
701  break;
702 
703  case 's':
704  p->size = atoi(ct + 1);
705  break;
706 
707  case 'm':
708  tm = (time_t) strtol(ct + 1, &tmp, 0);
709 
710  if (tmp != ct + 1)
711  break; /* not a valid integer */
712 
713  safe_free(p->date); // TODO: properly handle multiple p->name occurrences
714  p->date = xstrdup(ctime(&tm));
715 
716  *(strstr(p->date, "\n")) = '\0';
717 
718  break;
719 
720  case '/':
721  p->type = 'd';
722 
723  break;
724 
725  case 'r':
726  p->type = '-';
727 
728  break;
729 
730  case 'i':
731  break;
732 
733  default:
734  break;
735  }
736 
737 blank:
738  ct = strstr(ct, ",");
739 
740  if (ct) {
741  ++ct;
742  }
743  }
744 
745  if (p->type == 0) {
746  p->type = '-';
747  }
748 
749  if (p->name)
750  goto found;
751  else
752  safe_free(p->date);
753  }
754 
755 found:
756 
757  for (i = 0; i < n_tokens; ++i)
758  xfree(tokens[i].token);
759 
760  if (!p->name)
761  ftpListPartsFree(&p); /* cleanup */
762 
763  return p;
764 }
765 
766 bool
768 {
769  debugs(9, 7, "line={" << line << "}");
770 
771  if (strlen(line) > 1024) {
772  html << "<tr><td colspan=\"5\">" << line << "</td></tr>\n";
773  return true;
774  }
775 
776  SBuf prefix;
777  if (flags.dir_slash && dirpath && typecode != 'D') {
778  prefix.append(rfc1738_escape_part(dirpath));
779  prefix.append("/", 1);
780  }
781 
782  ftpListParts *parts = ftpListParseParts(line, flags);
783  if (!parts) {
784  html << "<tr class=\"entry\"><td colspan=\"5\">" << line << "</td></tr>\n";
785 
786  const char *p;
787  for (p = line; *p && xisspace(*p); ++p);
788  if (*p && !xisspace(*p))
789  flags.listformat_unknown = 1;
790 
791  return true;
792  }
793 
794  if (!strcmp(parts->name, ".") || !strcmp(parts->name, "..")) {
795  ftpListPartsFree(&parts);
796  return false;
797  }
798 
799  parts->size += 1023;
800  parts->size >>= 10;
801  parts->showname = xstrdup(parts->name);
802 
803  /* {icon} {text} . . . {date}{size}{chdir}{view}{download}{link}\n */
804  SBuf href(prefix);
805  href.append(rfc1738_escape_part(parts->name));
806 
807  SBuf text(parts->showname);
808 
809  SBuf icon, size, chdir, link;
810  switch (parts->type) {
811 
812  case 'd':
813  icon.appendf("<img border=\"0\" src=\"%s\" alt=\"%-6s\">",
814  mimeGetIconURL("internal-dir"),
815  "[DIR]");
816  href.append("/", 1); /* margin is allocated above */
817  break;
818 
819  case 'l':
820  icon.appendf("<img border=\"0\" src=\"%s\" alt=\"%-6s\">",
821  mimeGetIconURL("internal-link"),
822  "[LINK]");
823  /* sometimes there is an 'l' flag, but no "->" link */
824 
825  if (parts->link) {
826  SBuf link2(html_quote(rfc1738_escape(parts->link)));
827  link.appendf(" -&gt; <a href=\"%s" SQUIDSBUFPH "\">%s</a>",
828  link2[0] != '/' ? prefix.c_str() : "", SQUIDSBUFPRINT(link2),
829  html_quote(parts->link));
830  }
831 
832  break;
833 
834  case '\0':
835  icon.appendf("<img border=\"0\" src=\"%s\" alt=\"%-6s\">",
836  mimeGetIconURL(parts->name),
837  "[UNKNOWN]");
838  chdir.appendf("<a href=\"%s/;type=d\"><img border=\"0\" src=\"%s\" "
839  "alt=\"[DIR]\"></a>",
840  rfc1738_escape_part(parts->name),
841  mimeGetIconURL("internal-dir"));
842  break;
843 
844  case '-':
845 
846  default:
847  icon.appendf("<img border=\"0\" src=\"%s\" alt=\"%-6s\">",
848  mimeGetIconURL(parts->name),
849  "[FILE]");
850  size.appendf(" %6" PRId64 "k", parts->size);
851  break;
852  }
853 
854  SBuf view, download;
855  if (parts->type != 'd') {
856  if (mimeGetViewOption(parts->name)) {
857  view.appendf("<a href=\"" SQUIDSBUFPH ";type=a\"><img border=\"0\" src=\"%s\" "
858  "alt=\"[VIEW]\"></a>",
859  SQUIDSBUFPRINT(href), mimeGetIconURL("internal-view"));
860  }
861 
862  if (mimeGetDownloadOption(parts->name)) {
863  download.appendf("<a href=\"" SQUIDSBUFPH ";type=i\"><img border=\"0\" src=\"%s\" "
864  "alt=\"[DOWNLOAD]\"></a>",
865  SQUIDSBUFPRINT(href), mimeGetIconURL("internal-download"));
866  }
867  }
868 
869  /* construct the table row from parts. */
870  html << "<tr class=\"entry\">"
871  "<td class=\"icon\"><a href=\"" << href << "\">" << icon << "</a></td>"
872  "<td class=\"filename\"><a href=\"" << href << "\">" << html_quote(text.c_str()) << "</a></td>"
873  "<td class=\"date\">" << parts->date << "</td>"
874  "<td class=\"size\">" << size << "</td>"
875  "<td class=\"actions\">" << chdir << view << download << link << "</td>"
876  "</tr>\n";
877 
878  ftpListPartsFree(&parts);
879  return true;
880 }
881 
882 void
884 {
885  char *buf = data.readBuf->content();
886  char *sbuf; /* NULL-terminated copy of termedBuf */
887  char *end;
888  char *line;
889  char *s;
890  size_t linelen;
891  size_t usable;
892  size_t len = data.readBuf->contentSize();
893 
894  if (!len) {
895  debugs(9, 3, "no content to parse for " << entry->url() );
896  return;
897  }
898 
899  /*
900  * We need a NULL-terminated buffer for scanning, ick
901  */
902  sbuf = (char *)xmalloc(len + 1);
903  xstrncpy(sbuf, buf, len + 1);
904  end = sbuf + len - 1;
905 
906  while (*end != '\r' && *end != '\n' && end > sbuf)
907  --end;
908 
909  usable = end - sbuf;
910 
911  debugs(9, 3, "usable = " << usable << " of " << len << " bytes.");
912 
913  if (usable == 0) {
914  if (buf[0] == '\0' && len == 1) {
915  debugs(9, 3, "NIL ends data from " << entry->url() << " transfer problem?");
916  data.readBuf->consume(len);
917  } else {
918  debugs(9, 3, "didn't find end for " << entry->url());
919  debugs(9, 3, "buffer remains (" << len << " bytes) '" << rfc1738_do_escape(buf,0) << "'");
920  }
921  xfree(sbuf);
922  return;
923  }
924 
925  debugs(9, 3, (unsigned long int)len << " bytes to play with");
926 
927  line = (char *)memAllocate(MEM_4K_BUF);
928  ++end;
929  s = sbuf;
930  s += strspn(s, crlf);
931 
932  for (; s < end; s += strcspn(s, crlf), s += strspn(s, crlf)) {
933  debugs(9, 7, "s = {" << s << "}");
934  linelen = strcspn(s, crlf) + 1;
935 
936  if (linelen < 2)
937  break;
938 
939  if (linelen > 4096)
940  linelen = 4096;
941 
942  xstrncpy(line, s, linelen);
943 
944  debugs(9, 7, "{" << line << "}");
945 
946  if (!strncmp(line, "total", 5))
947  continue;
948 
949  MemBuf htmlPage;
950  htmlPage.init();
951  PackableStream html(htmlPage);
952 
953  if (htmlifyListEntry(line, html)) {
954  html.flush();
955  debugs(9, 7, "listing append: t = {" << htmlPage.contentSize() << ", '" << htmlPage.content() << "'}");
956  listing.append(htmlPage.content(), htmlPage.contentSize());
957  }
958  }
959 
960  debugs(9, 7, "Done.");
961  data.readBuf->consume(usable);
962  memFree(line, MEM_4K_BUF);
963  xfree(sbuf);
964 }
965 
966 void
968 {
969  debugs(9, 3, status());
970 
971  if (request->method == Http::METHOD_HEAD && (flags.isdir || theSize != -1)) {
972  serverComplete();
973  return;
974  }
975 
976  /* Directory listings are special. They write ther own headers via the error objects */
977  if (!flags.http_header_sent && data.readBuf->contentSize() >= 0 && !flags.isdir)
978  appendSuccessHeader();
979 
980  if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
981  /*
982  * probably was aborted because content length exceeds one
983  * of the maximum size limits.
984  */
985  abortAll("entry aborted after calling appendSuccessHeader()");
986  return;
987  }
988 
989 #if USE_ADAPTATION
990 
991  if (adaptationAccessCheckPending) {
992  debugs(9, 3, "returning from Ftp::Gateway::processReplyBody due to adaptationAccessCheckPending");
993  return;
994  }
995 
996 #endif
997 
998  if (flags.isdir) {
999  if (!flags.listing) {
1000  flags.listing = 1;
1001  listing.reset();
1002  }
1003  parseListing();
1004  maybeReadVirginBody();
1005  return;
1006  } else if (const auto csize = data.readBuf->contentSize()) {
1007  writeReplyBody(data.readBuf->content(), csize);
1008  debugs(9, 5, "consuming " << csize << " bytes of readBuf");
1009  data.readBuf->consume(csize);
1010  }
1011 
1012  entry->flush();
1013 
1014  maybeReadVirginBody();
1015 }
1016 
1033 int
1035 {
1036  /* default username */
1037  xstrncpy(user, "anonymous", MAX_URL);
1038 
1039 #if HAVE_AUTH_MODULE_BASIC
1040  /* Check HTTP Authorization: headers (better than defaults, but less than URL) */
1041  const auto auth(req_hdr->getAuthToken(Http::HdrType::AUTHORIZATION, "Basic"));
1042  if (!auth.isEmpty()) {
1043  flags.authenticated = 1;
1044  loginParser(auth, false);
1045  }
1046  /* we fail with authorization-required error later IFF the FTP server requests it */
1047 #else
1048  (void)req_hdr;
1049 #endif
1050 
1051  /* Test URL login syntax. Overrides any headers received. */
1052  loginParser(request->url.userInfo(), true);
1053 
1054  // XXX: We we keep default "anonymous" instead of properly supporting empty usernames.
1055  Assure(user[0]);
1056 
1057  /* name + password == success */
1058  if (password[0])
1059  return 1;
1060 
1061  /* Setup default FTP password settings */
1062  /* this has to be done last so that we can have a no-password case above. */
1063  if (!password[0]) {
1064  if (strcmp(user, "anonymous") == 0 && !flags.tried_auth_anonymous) {
1065  xstrncpy(password, Config.Ftp.anon_user, MAX_URL);
1066  flags.tried_auth_anonymous=1;
1067  return 1;
1068  } else if (!flags.tried_auth_nopass) {
1069  xstrncpy(password, null_string, MAX_URL);
1070  flags.tried_auth_nopass=1;
1071  return 1;
1072  }
1073  }
1074 
1075  return 0; /* different username */
1076 }
1077 
1078 void
1080 {
1081  // TODO: parse FTP URL syntax properly in AnyP::Uri::parse()
1082 
1083  // If typecode was specified, extract it and leave just the filename in
1084  // url.path. Tolerate trailing garbage or missing typecode value. Roughly:
1085  // [filename] ;type=[typecode char] [trailing garbage]
1086  static const SBuf middle(";type=");
1087  const auto typeSpecStart = request->url.path().find(middle);
1088  if (typeSpecStart != SBuf::npos) {
1089  const auto fullPath = request->url.path();
1090  const auto typecodePos = typeSpecStart + middle.length();
1091  typecode = (typecodePos < fullPath.length()) ?
1092  static_cast<char>(xtoupper(fullPath[typecodePos])) : '\0';
1093  request->url.path(fullPath.substr(0, typeSpecStart));
1094  }
1095 
1096  int l = request->url.path().length();
1097  /* check for null path */
1098 
1099  if (!l) {
1100  flags.isdir = 1;
1101  flags.root_dir = 1;
1102  flags.need_base_href = 1; /* Work around broken browsers */
1103  } else if (!request->url.path().cmp("/%2f/")) {
1104  /* UNIX root directory */
1105  flags.isdir = 1;
1106  flags.root_dir = 1;
1107  } else if ((l >= 1) && (request->url.path()[l-1] == '/')) {
1108  /* Directory URL, ending in / */
1109  flags.isdir = 1;
1110 
1111  if (l == 1)
1112  flags.root_dir = 1;
1113  } else {
1114  flags.dir_slash = 1;
1115  }
1116 }
1117 
1118 void
1120 {
1121  title_url = "ftp://";
1122 
1123  if (strcmp(user, "anonymous")) {
1124  title_url.append(user);
1125  title_url.append("@");
1126  }
1127 
1128  SBuf authority = request->url.authority(request->url.getScheme() != AnyP::PROTO_FTP);
1129 
1130  title_url.append(authority);
1131  title_url.append(request->url.absolutePath());
1132 
1133  base_href = "ftp://";
1134 
1135  if (strcmp(user, "anonymous") != 0) {
1136  base_href.append(rfc1738_escape_part(user));
1137 
1138  if (password_url) {
1139  base_href.append(":");
1140  base_href.append(rfc1738_escape_part(password));
1141  }
1142 
1143  base_href.append("@");
1144  }
1145 
1146  base_href.append(authority);
1147  base_href.append(request->url.path());
1148  base_href.append("/");
1149 }
1150 
1151 void
1153 {
1154  if (!checkAuth(&request->header)) {
1155  /* create appropriate reply */
1156  SBuf realm(ftpRealm()); // local copy so SBuf will not disappear too early
1157  const auto reply = ftpAuthRequired(request.getRaw(), realm, fwd->al);
1158  entry->replaceHttpReply(reply);
1159  serverComplete();
1160  return;
1161  }
1162 
1163  checkUrlpath();
1164  buildTitleUrl();
1165  debugs(9, 5, "FD " << (ctrl.conn ? ctrl.conn->fd : -1) << " : host=" << request->url.host() <<
1166  ", path=" << request->url.absolutePath() << ", user=" << user << ", passwd=" << password);
1167  state = BEGIN;
1169 }
1170 
1171 /* ====================================================================== */
1172 
1173 void
1175 {
1177  if (ctrl.message == nullptr)
1178  return; // didn't get complete reply yet
1179 
1180  /* Copy the message except for the last line to cwd_message to be
1181  * printed in error messages.
1182  */
1183  for (wordlist *w = ctrl.message; w && w->next; w = w->next) {
1184  cwd_message.append('\n');
1185  cwd_message.append(w->key);
1186  }
1187 
1188  FTP_SM_FUNCS[state] (this);
1189 }
1190 
1191 /* ====================================================================== */
1192 
1193 static void
1195 {
1196  int code = ftpState->ctrl.replycode;
1197  debugs(9, 3, MYNAME);
1198 
1199  if (ftpState->flags.pasv_only)
1200  ++ ftpState->login_att;
1201 
1202  if (code == 220) {
1203  if (ftpState->ctrl.message) {
1204  if (strstr(ftpState->ctrl.message->key, "NetWare"))
1205  ftpState->flags.skip_whitespace = 1;
1206  }
1207 
1208  ftpSendUser(ftpState);
1209  } else if (code == 120) {
1210  if (nullptr != ftpState->ctrl.message)
1211  debugs(9, DBG_IMPORTANT, "FTP server is busy: " << ftpState->ctrl.message->key);
1212 
1213  return;
1214  } else {
1215  ftpFail(ftpState);
1216  }
1217 }
1218 
1224 void
1226 {
1227  ErrorState *err = nullptr;
1228 
1229  if ((state == SENT_USER || state == SENT_PASS) && ctrl.replycode >= 400) {
1230  if (ctrl.replycode == 421 || ctrl.replycode == 426) {
1231  // 421/426 - Service Overload - retry permitted.
1232  err = new ErrorState(ERR_FTP_UNAVAILABLE, Http::scServiceUnavailable, fwd->request, fwd->al);
1233  } else if (ctrl.replycode >= 430 && ctrl.replycode <= 439) {
1234  // 43x - Invalid or Credential Error - retry challenge required.
1235  err = new ErrorState(ERR_FTP_FORBIDDEN, Http::scUnauthorized, fwd->request, fwd->al);
1236  } else if (ctrl.replycode >= 530 && ctrl.replycode <= 539) {
1237  // 53x - Credentials Missing - retry challenge required
1238  if (password_url) // but they were in the URI! major fail.
1239  err = new ErrorState(ERR_FTP_FORBIDDEN, Http::scForbidden, fwd->request, fwd->al);
1240  else
1241  err = new ErrorState(ERR_FTP_FORBIDDEN, Http::scUnauthorized, fwd->request, fwd->al);
1242  }
1243  }
1244 
1245  if (!err) {
1246  ftpFail(this);
1247  return;
1248  }
1249 
1250  failed(ERR_NONE, ctrl.replycode, err);
1251  // any other problems are general failures.
1252 
1253  HttpReply *newrep = err->BuildHttpReply();
1254  delete err;
1255 
1256 #if HAVE_AUTH_MODULE_BASIC
1257  /* add Authenticate header */
1258  // XXX: performance regression. c_str() may reallocate
1259  SBuf realm(ftpRealm()); // local copy so SBuf will not disappear too early
1260  newrep->header.putAuth("Basic", realm.c_str());
1261 #endif
1262 
1263  // add it to the store entry for response....
1264  entry->replaceHttpReply(newrep);
1265  serverComplete();
1266 }
1267 
1268 SBuf
1270 {
1271  SBuf realm;
1272 
1273  /* This request is not fully authenticated */
1274  realm.appendf("FTP %s ", user);
1275  if (!request)
1276  realm.append("unknown", 7);
1277  else {
1278  realm.append(request->url.host());
1279  const auto &rport = request->url.port();
1280  if (rport && *rport != 21)
1281  realm.appendf(" port %hu", *rport);
1282  }
1283  return realm;
1284 }
1285 
1286 static void
1288 {
1289  /* check the server control channel is still available */
1290  if (!ftpState || !ftpState->haveControlChannel("ftpSendUser"))
1291  return;
1292 
1293  if (ftpState->proxy_host != nullptr)
1294  snprintf(cbuf, CTRL_BUFLEN, "USER %s@%s\r\n", ftpState->user, ftpState->request->url.host());
1295  else
1296  snprintf(cbuf, CTRL_BUFLEN, "USER %s\r\n", ftpState->user);
1297 
1298  ftpState->writeCommand(cbuf);
1299 
1300  ftpState->state = Ftp::Client::SENT_USER;
1301 }
1302 
1303 static void
1305 {
1306  int code = ftpState->ctrl.replycode;
1307  debugs(9, 3, MYNAME);
1308 
1309  if (code == 230) {
1310  ftpReadPass(ftpState);
1311  } else if (code == 331) {
1312  ftpSendPass(ftpState);
1313  } else {
1314  ftpState->loginFailed();
1315  }
1316 }
1317 
1318 static void
1320 {
1321  /* check the server control channel is still available */
1322  if (!ftpState || !ftpState->haveControlChannel("ftpSendPass"))
1323  return;
1324 
1325  snprintf(cbuf, CTRL_BUFLEN, "PASS %s\r\n", ftpState->password);
1326  ftpState->writeCommand(cbuf);
1327  ftpState->state = Ftp::Client::SENT_PASS;
1328 }
1329 
1330 static void
1332 {
1333  int code = ftpState->ctrl.replycode;
1334  debugs(9, 3, "code=" << code);
1335 
1336  if (code == 230) {
1337  ftpSendType(ftpState);
1338  } else {
1339  ftpState->loginFailed();
1340  }
1341 }
1342 
1343 static void
1345 {
1346  /* check the server control channel is still available */
1347  if (!ftpState || !ftpState->haveControlChannel("ftpSendType"))
1348  return;
1349 
1350  /*
1351  * Ref section 3.2.2 of RFC 1738
1352  */
1353  char mode = ftpState->typecode;
1354 
1355  switch (mode) {
1356 
1357  case 'D':
1358  mode = 'A';
1359  break;
1360 
1361  case 'A':
1362 
1363  case 'I':
1364  break;
1365 
1366  default:
1367 
1368  if (ftpState->flags.isdir) {
1369  mode = 'A';
1370  } else {
1371  auto t = ftpState->request->url.path().rfind('/');
1372  // XXX: performance regression, c_str() may reallocate
1373  SBuf filename = ftpState->request->url.path().substr(t != SBuf::npos ? t + 1 : 0);
1374  mode = mimeGetTransferMode(filename.c_str());
1375  }
1376 
1377  break;
1378  }
1379 
1380  if (mode == 'I')
1381  ftpState->flags.binary = 1;
1382  else
1383  ftpState->flags.binary = 0;
1384 
1385  snprintf(cbuf, CTRL_BUFLEN, "TYPE %c\r\n", mode);
1386 
1387  ftpState->writeCommand(cbuf);
1388 
1389  ftpState->state = Ftp::Client::SENT_TYPE;
1390 }
1391 
1392 static void
1394 {
1395  int code = ftpState->ctrl.replycode;
1396  char *path;
1397  char *d, *p;
1398  debugs(9, 3, "code=" << code);
1399 
1400  if (code == 200) {
1401  p = path = SBufToCstring(ftpState->request->url.path());
1402 
1403  if (*p == '/')
1404  ++p;
1405 
1406  while (*p) {
1407  d = p;
1408  p += strcspn(p, "/");
1409 
1410  if (*p) {
1411  *p = '\0';
1412  ++p;
1413  }
1414 
1415  rfc1738_unescape(d);
1416 
1417  if (*d)
1418  wordlistAdd(&ftpState->pathcomps, d);
1419  }
1420 
1421  xfree(path);
1422 
1423  if (ftpState->pathcomps)
1424  ftpTraverseDirectory(ftpState);
1425  else
1426  ftpListDir(ftpState);
1427  } else {
1428  ftpFail(ftpState);
1429  }
1430 }
1431 
1432 static void
1434 {
1435  debugs(9, 4, (ftpState->filepath ? ftpState->filepath : "<NULL>"));
1436 
1437  safe_free(ftpState->dirpath);
1438  ftpState->dirpath = ftpState->filepath;
1439  ftpState->filepath = nullptr;
1440 
1441  /* Done? */
1442 
1443  if (ftpState->pathcomps == nullptr) {
1444  debugs(9, 3, "the final component was a directory");
1445  ftpListDir(ftpState);
1446  return;
1447  }
1448 
1449  /* Go to next path component */
1450  ftpState->filepath = wordlistChopHead(& ftpState->pathcomps);
1451 
1452  /* Check if we are to CWD or RETR */
1453  if (ftpState->pathcomps != nullptr || ftpState->flags.isdir) {
1454  ftpSendCwd(ftpState);
1455  } else {
1456  debugs(9, 3, "final component is probably a file");
1457  ftpGetFile(ftpState);
1458  return;
1459  }
1460 }
1461 
1462 static void
1464 {
1465  char *path = nullptr;
1466 
1467  /* check the server control channel is still available */
1468  if (!ftpState || !ftpState->haveControlChannel("ftpSendCwd"))
1469  return;
1470 
1471  debugs(9, 3, MYNAME);
1472 
1473  path = ftpState->filepath;
1474 
1475  if (!strcmp(path, "..") || !strcmp(path, "/")) {
1476  ftpState->flags.no_dotdot = 1;
1477  } else {
1478  ftpState->flags.no_dotdot = 0;
1479  }
1480 
1481  snprintf(cbuf, CTRL_BUFLEN, "CWD %s\r\n", path);
1482 
1483  ftpState->writeCommand(cbuf);
1484 
1485  ftpState->state = Ftp::Client::SENT_CWD;
1486 }
1487 
1488 static void
1490 {
1491  int code = ftpState->ctrl.replycode;
1492  debugs(9, 3, MYNAME);
1493 
1494  if (code >= 200 && code < 300) {
1495  /* CWD OK */
1496  ftpState->unhack();
1497 
1498  /* Reset cwd_message to only include the last message */
1499  ftpState->cwd_message.reset("");
1500  for (wordlist *w = ftpState->ctrl.message; w; w = w->next) {
1501  ftpState->cwd_message.append('\n');
1502  ftpState->cwd_message.append(w->key);
1503  }
1504  ftpState->ctrl.message = nullptr;
1505 
1506  /* Continue to traverse the path */
1507  ftpTraverseDirectory(ftpState);
1508  } else {
1509  /* CWD FAILED */
1510 
1511  if (!ftpState->flags.put)
1512  ftpFail(ftpState);
1513  else
1514  ftpSendMkdir(ftpState);
1515  }
1516 }
1517 
1518 static void
1520 {
1521  char *path = nullptr;
1522 
1523  /* check the server control channel is still available */
1524  if (!ftpState || !ftpState->haveControlChannel("ftpSendMkdir"))
1525  return;
1526 
1527  path = ftpState->filepath;
1528  debugs(9, 3, "with path=" << path);
1529  snprintf(cbuf, CTRL_BUFLEN, "MKD %s\r\n", path);
1530  ftpState->writeCommand(cbuf);
1531  ftpState->state = Ftp::Client::SENT_MKDIR;
1532 }
1533 
1534 static void
1536 {
1537  char *path = ftpState->filepath;
1538  int code = ftpState->ctrl.replycode;
1539 
1540  debugs(9, 3, "path " << path << ", code " << code);
1541 
1542  if (code == 257) { /* success */
1543  ftpSendCwd(ftpState);
1544  } else if (code == 550) { /* dir exists */
1545 
1546  if (ftpState->flags.put_mkdir) {
1547  ftpState->flags.put_mkdir = 1;
1548  ftpSendCwd(ftpState);
1549  } else
1550  ftpSendReply(ftpState);
1551  } else
1552  ftpSendReply(ftpState);
1553 }
1554 
1555 static void
1557 {
1558  assert(*ftpState->filepath != '\0');
1559  ftpState->flags.isdir = 0;
1560  ftpSendMdtm(ftpState);
1561 }
1562 
1563 static void
1565 {
1566  if (ftpState->flags.dir_slash) {
1567  debugs(9, 3, "Directory path did not end in /");
1568  ftpState->title_url.append("/");
1569  ftpState->flags.isdir = 1;
1570  }
1571 
1572  ftpSendPassive(ftpState);
1573 }
1574 
1575 static void
1577 {
1578  /* check the server control channel is still available */
1579  if (!ftpState || !ftpState->haveControlChannel("ftpSendMdtm"))
1580  return;
1581 
1582  assert(*ftpState->filepath != '\0');
1583  snprintf(cbuf, CTRL_BUFLEN, "MDTM %s\r\n", ftpState->filepath);
1584  ftpState->writeCommand(cbuf);
1585  ftpState->state = Ftp::Client::SENT_MDTM;
1586 }
1587 
1588 static void
1590 {
1591  int code = ftpState->ctrl.replycode;
1592  debugs(9, 3, MYNAME);
1593 
1594  if (code == 213) {
1595  ftpState->mdtm = Time::ParseIso3307(ftpState->ctrl.last_reply);
1596  ftpState->unhack();
1597  } else if (code < 0) {
1598  ftpFail(ftpState);
1599  return;
1600  }
1601 
1602  ftpSendSize(ftpState);
1603 }
1604 
1605 static void
1607 {
1608  /* check the server control channel is still available */
1609  if (!ftpState || !ftpState->haveControlChannel("ftpSendSize"))
1610  return;
1611 
1612  /* Only send SIZE for binary transfers. The returned size
1613  * is useless on ASCII transfers */
1614 
1615  if (ftpState->flags.binary) {
1616  assert(ftpState->filepath != nullptr);
1617  assert(*ftpState->filepath != '\0');
1618  snprintf(cbuf, CTRL_BUFLEN, "SIZE %s\r\n", ftpState->filepath);
1619  ftpState->writeCommand(cbuf);
1620  ftpState->state = Ftp::Client::SENT_SIZE;
1621  } else
1622  /* Skip to next state no non-binary transfers */
1623  ftpSendPassive(ftpState);
1624 }
1625 
1626 static void
1628 {
1629  int code = ftpState->ctrl.replycode;
1630  debugs(9, 3, MYNAME);
1631 
1632  if (code == 213) {
1633  ftpState->unhack();
1634  ftpState->theSize = strtoll(ftpState->ctrl.last_reply, nullptr, 10);
1635 
1636  if (ftpState->theSize == 0) {
1637  debugs(9, 2, "SIZE reported " <<
1638  ftpState->ctrl.last_reply << " on " <<
1639  ftpState->title_url);
1640  ftpState->theSize = -1;
1641  }
1642  } else if (code < 0) {
1643  ftpFail(ftpState);
1644  return;
1645  }
1646 
1647  ftpSendPassive(ftpState);
1648 }
1649 
1650 static void
1652 {
1653  Ip::Address srvAddr; // unused
1654  if (ftpState->handleEpsvReply(srvAddr)) {
1655  if (ftpState->ctrl.message == nullptr)
1656  return; // didn't get complete reply yet
1657 
1658  ftpState->connectDataChannel();
1659  }
1660 }
1661 
1666 static void
1668 {
1670  if (!ftpState || !ftpState->haveControlChannel("ftpSendPassive"))
1671  return;
1672 
1673  debugs(9, 3, MYNAME);
1674 
1677  if (ftpState->request->method == Http::METHOD_HEAD && (ftpState->flags.isdir || ftpState->theSize != -1)) {
1678  ftpState->processHeadResponse(); // may call serverComplete
1679  return;
1680  }
1681 
1682  if (ftpState->sendPassive()) {
1683  // SENT_EPSV_ALL blocks other non-EPSV connections being attempted
1684  if (ftpState->state == Ftp::Client::SENT_EPSV_ALL)
1685  ftpState->flags.epsv_all_sent = true;
1686  }
1687 }
1688 
1689 void
1691 {
1692  debugs(9, 5, "handling HEAD response");
1693  ftpSendQuit(this);
1694  appendSuccessHeader();
1695 
1696  /*
1697  * On rare occasions I'm seeing the entry get aborted after
1698  * readControlReply() and before here, probably when
1699  * trying to write to the client.
1700  */
1701  if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1702  abortAll("entry aborted while processing HEAD");
1703  return;
1704  }
1705 
1706 #if USE_ADAPTATION
1707  if (adaptationAccessCheckPending) {
1708  debugs(9,3, "returning due to adaptationAccessCheckPending");
1709  return;
1710  }
1711 #endif
1712 
1713  // processReplyBody calls serverComplete() since there is no body
1714  processReplyBody();
1715 }
1716 
1717 static void
1719 {
1720  Ip::Address srvAddr; // unused
1721  if (ftpState->handlePasvReply(srvAddr))
1722  ftpState->connectDataChannel();
1723  else {
1724  ftpFail(ftpState);
1725  // Currently disabled, does not work correctly:
1726  // ftpSendEPRT(ftpState);
1727  return;
1728  }
1729 }
1730 
1731 void
1733 {
1734  debugs(9, 3, MYNAME);
1735  dataConnWait.finish();
1736 
1737  if (io.flag != Comm::OK) {
1738  debugs(9, 2, "Failed to connect. Retrying via another method.");
1739 
1740  // ABORT on timeouts. server may be waiting on a broken TCP link.
1741  if (io.xerrno == Comm::TIMEOUT)
1742  writeCommand("ABOR\r\n");
1743 
1744  // try another connection attempt with some other method
1745  ftpSendPassive(this);
1746  return;
1747  }
1748 
1749  data.opened(io.conn, dataCloser());
1750  ftpRestOrList(this);
1751 }
1752 
1753 static void
1754 ftpOpenListenSocket(Ftp::Gateway * ftpState, int fallback)
1755 {
1757  if (ftpState->data.conn != nullptr) {
1758  if ((ftpState->data.conn->flags & COMM_REUSEADDR))
1759  // NP: in fact it points to the control channel. just clear it.
1760  ftpState->data.clear();
1761  else
1762  ftpState->data.close();
1763  }
1764  safe_free(ftpState->data.host);
1765 
1766  if (!Comm::IsConnOpen(ftpState->ctrl.conn)) {
1767  debugs(9, 5, "The control connection to the remote end is closed");
1768  return;
1769  }
1770 
1771  /*
1772  * Set up a listen socket on the same local address as the
1773  * control connection.
1774  */
1776  temp->local = ftpState->ctrl.conn->local;
1777 
1778  /*
1779  * REUSEADDR is needed in fallback mode, since the same port is
1780  * used for both control and data.
1781  */
1782  if (fallback) {
1783  int on = 1;
1784  errno = 0;
1785  if (xsetsockopt(ftpState->ctrl.conn->fd, SOL_SOCKET, SO_REUSEADDR,
1786  &on, sizeof(on)) == -1) {
1787  int xerrno = errno;
1788  // SO_REUSEADDR is only an optimization, no need to be verbose about error
1789  debugs(9, 4, "setsockopt failed: " << xstrerr(xerrno));
1790  }
1791  ftpState->ctrl.conn->flags |= COMM_REUSEADDR;
1792  temp->flags |= COMM_REUSEADDR;
1793  } else {
1794  /* if not running in fallback mode a new port needs to be retrieved */
1795  temp->local.port(0);
1796  }
1797 
1798  ftpState->listenForDataChannel(temp);
1799 }
1800 
1801 static void
1803 {
1804  /* check the server control channel is still available */
1805  if (!ftpState || !ftpState->haveControlChannel("ftpSendPort"))
1806  return;
1807 
1808  if (Config.Ftp.epsv_all && ftpState->flags.epsv_all_sent) {
1809  debugs(9, DBG_IMPORTANT, "FTP does not allow PORT method after 'EPSV ALL' has been sent.");
1810  return;
1811  }
1812 
1813  debugs(9, 3, MYNAME);
1814  ftpState->flags.pasv_supported = 0;
1815  ftpOpenListenSocket(ftpState, 0);
1816 
1817  if (!Comm::IsConnOpen(ftpState->data.listenConn)) {
1818  if ( ftpState->data.listenConn != nullptr && !ftpState->data.listenConn->local.isIPv4() ) {
1819  /* non-IPv4 CANNOT send PORT command. */
1820  /* we got here by attempting and failing an EPRT */
1821  /* using the same reply code should simulate a PORT failure */
1822  ftpReadPORT(ftpState);
1823  return;
1824  }
1825 
1826  /* XXX Need to set error message */
1827  ftpFail(ftpState);
1828  return;
1829  }
1830 
1831  // pull out the internal IP address bytes to send in PORT command...
1832  // source them from the listen_conn->local
1833 
1834  struct addrinfo *AI = nullptr;
1835  ftpState->data.listenConn->local.getAddrInfo(AI, AF_INET);
1836  unsigned char *addrptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_addr;
1837  unsigned char *portptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_port;
1838  snprintf(cbuf, CTRL_BUFLEN, "PORT %d,%d,%d,%d,%d,%d\r\n",
1839  addrptr[0], addrptr[1], addrptr[2], addrptr[3],
1840  portptr[0], portptr[1]);
1841  ftpState->writeCommand(cbuf);
1842  ftpState->state = Ftp::Client::SENT_PORT;
1843 
1845 }
1846 
1847 static void
1849 {
1850  int code = ftpState->ctrl.replycode;
1851  debugs(9, 3, MYNAME);
1852 
1853  if (code != 200) {
1854  /* Fall back on using the same port as the control connection */
1855  debugs(9, 3, "PORT not supported by remote end");
1856  ftpOpenListenSocket(ftpState, 1);
1857  }
1858 
1859  ftpRestOrList(ftpState);
1860 }
1861 
1862 static void
1864 {
1865  int code = ftpState->ctrl.replycode;
1866  debugs(9, 3, MYNAME);
1867 
1868  if (code != 200) {
1869  /* Failover to attempting old PORT command. */
1870  debugs(9, 3, "EPRT not supported by remote end");
1871  ftpSendPORT(ftpState);
1872  return;
1873  }
1874 
1875  ftpRestOrList(ftpState);
1876 }
1877 
1882 void
1884 {
1885  debugs(9, 3, MYNAME);
1886 
1887  if (!Comm::IsConnOpen(ctrl.conn)) { /*Close handlers will cleanup*/
1888  debugs(9, 5, "The control connection to the remote end is closed");
1889  return;
1890  }
1891 
1892  if (io.flag != Comm::OK) {
1893  data.listenConn->close();
1894  data.listenConn = nullptr;
1895  debugs(9, DBG_IMPORTANT, "FTP AcceptDataConnection: " << io.conn << ": " << xstrerr(io.xerrno));
1896  // TODO: need to send error message on control channel
1897  ftpFail(this);
1898  return;
1899  }
1900 
1901  if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1902  abortAll("entry aborted when accepting data conn");
1903  data.listenConn->close();
1904  data.listenConn = nullptr;
1905  io.conn->close();
1906  return;
1907  }
1908 
1909  /* data listening conn is no longer even open. abort. */
1910  if (!Comm::IsConnOpen(data.listenConn)) {
1911  data.listenConn = nullptr; // ensure that it's cleared and not just closed.
1912  return;
1913  }
1914 
1915  /* data listening conn is no longer even open. abort. */
1916  if (!Comm::IsConnOpen(data.conn)) {
1917  data.clear(); // ensure that it's cleared and not just closed.
1918  return;
1919  }
1920 
1927  if (Config.Ftp.sanitycheck) {
1928  // accept if either our data or ctrl connection is talking to this remote peer.
1929  if (data.conn->remote != io.conn->remote && ctrl.conn->remote != io.conn->remote) {
1930  debugs(9, DBG_IMPORTANT,
1931  "ERROR: FTP data connection from unexpected server (" <<
1932  io.conn->remote << "), expecting " <<
1933  data.conn->remote << " or " << ctrl.conn->remote);
1934 
1935  /* close the bad sources connection down ASAP. */
1936  io.conn->close();
1937 
1938  /* drop the bad connection (io) by ignoring the attempt. */
1939  return;
1940  }
1941  }
1942 
1944  data.close();
1945  data.opened(io.conn, dataCloser());
1946  data.addr(io.conn->remote);
1947 
1948  debugs(9, 3, "Connected data socket on " <<
1949  io.conn << ". FD table says: " <<
1950  "ctrl-peer= " << fd_table[ctrl.conn->fd].ipaddr << ", " <<
1951  "data-peer= " << fd_table[data.conn->fd].ipaddr);
1952 
1953  assert(haveControlChannel("ftpAcceptDataConnection"));
1954  assert(ctrl.message == nullptr);
1955 
1956  // Ctrl channel operations will determine what happens to this data connection
1957 }
1958 
1959 static void
1961 {
1962  debugs(9, 3, MYNAME);
1963 
1964  if (ftpState->typecode == 'D') {
1965  ftpState->flags.isdir = 1;
1966 
1967  if (ftpState->flags.put) {
1968  ftpSendMkdir(ftpState); /* PUT name;type=d */
1969  } else {
1970  ftpSendNlst(ftpState); /* GET name;type=d sec 3.2.2 of RFC 1738 */
1971  }
1972  } else if (ftpState->flags.put) {
1973  ftpSendStor(ftpState);
1974  } else if (ftpState->flags.isdir)
1975  ftpSendList(ftpState);
1976  else if (ftpState->restartable())
1977  ftpSendRest(ftpState);
1978  else
1979  ftpSendRetr(ftpState);
1980 }
1981 
1982 static void
1984 {
1985  /* check the server control channel is still available */
1986  if (!ftpState || !ftpState->haveControlChannel("ftpSendStor"))
1987  return;
1988 
1989  debugs(9, 3, MYNAME);
1990 
1991  if (ftpState->filepath != nullptr) {
1992  /* Plain file upload */
1993  snprintf(cbuf, CTRL_BUFLEN, "STOR %s\r\n", ftpState->filepath);
1994  ftpState->writeCommand(cbuf);
1995  ftpState->state = Ftp::Client::SENT_STOR;
1996  } else if (ftpState->request->header.getInt64(Http::HdrType::CONTENT_LENGTH) > 0) {
1997  /* File upload without a filename. use STOU to generate one */
1998  snprintf(cbuf, CTRL_BUFLEN, "STOU\r\n");
1999  ftpState->writeCommand(cbuf);
2000  ftpState->state = Ftp::Client::SENT_STOR;
2001  } else {
2002  /* No file to transfer. Only create directories if needed */
2003  ftpSendReply(ftpState);
2004  }
2005 }
2006 
2008 static void
2010 {
2011  ftpState->readStor();
2012 }
2013 
2015 {
2016  int code = ctrl.replycode;
2017  debugs(9, 3, MYNAME);
2018 
2019  if (code == 125 || (code == 150 && Comm::IsConnOpen(data.conn))) {
2020  if (!originalRequest()->body_pipe) {
2021  debugs(9, 3, "zero-size STOR?");
2022  state = WRITING_DATA; // make ftpWriteTransferDone() responsible
2023  dataComplete(); // XXX: keep in sync with doneSendingRequestBody()
2024  return;
2025  }
2026 
2027  if (!startRequestBodyFlow()) { // register to receive body data
2028  ftpFail(this);
2029  return;
2030  }
2031 
2032  /* When client status is 125, or 150 and the data connection is open, Begin data transfer. */
2033  debugs(9, 3, "starting data transfer");
2034  switchTimeoutToDataChannel();
2035  sendMoreRequestBody();
2036  fwd->dontRetry(true); // do not permit re-trying if the body was sent.
2037  state = WRITING_DATA;
2038  debugs(9, 3, "writing data channel");
2039  } else if (code == 150) {
2040  /* When client code is 150 with no data channel, Accept data channel. */
2041  debugs(9, 3, "ftpReadStor: accepting data channel");
2042  listenForDataChannel(data.conn);
2043  } else {
2044  debugs(9, DBG_IMPORTANT, "ERROR: Unexpected reply code "<< std::setfill('0') << std::setw(3) << code);
2045  ftpFail(this);
2046  }
2047 }
2048 
2049 static void
2051 {
2052  /* check the server control channel is still available */
2053  if (!ftpState || !ftpState->haveControlChannel("ftpSendRest"))
2054  return;
2055 
2056  debugs(9, 3, MYNAME);
2057 
2058  snprintf(cbuf, CTRL_BUFLEN, "REST %" PRId64 "\r\n", ftpState->restart_offset);
2059  ftpState->writeCommand(cbuf);
2060  ftpState->state = Ftp::Client::SENT_REST;
2061 }
2062 
2063 int
2065 {
2066  if (restart_offset > 0)
2067  return 1;
2068 
2069  if (!request->range)
2070  return 0;
2071 
2072  if (!flags.binary)
2073  return 0;
2074 
2075  if (theSize <= 0)
2076  return 0;
2077 
2078  int64_t desired_offset = request->range->lowestOffset(theSize);
2079 
2080  if (desired_offset <= 0)
2081  return 0;
2082 
2083  if (desired_offset >= theSize)
2084  return 0;
2085 
2086  restart_offset = desired_offset;
2087  return 1;
2088 }
2089 
2090 static void
2092 {
2093  int code = ftpState->ctrl.replycode;
2094  debugs(9, 3, MYNAME);
2095  assert(ftpState->restart_offset > 0);
2096 
2097  if (code == 350) {
2098  ftpState->setCurrentOffset(ftpState->restart_offset);
2099  ftpSendRetr(ftpState);
2100  } else if (code > 0) {
2101  debugs(9, 3, "REST not supported");
2102  ftpState->flags.rest_supported = 0;
2103  ftpSendRetr(ftpState);
2104  } else {
2105  ftpFail(ftpState);
2106  }
2107 }
2108 
2109 static void
2111 {
2112  /* check the server control channel is still available */
2113  if (!ftpState || !ftpState->haveControlChannel("ftpSendList"))
2114  return;
2115 
2116  debugs(9, 3, MYNAME);
2117 
2118  if (ftpState->filepath) {
2119  snprintf(cbuf, CTRL_BUFLEN, "LIST %s\r\n", ftpState->filepath);
2120  } else {
2121  snprintf(cbuf, CTRL_BUFLEN, "LIST\r\n");
2122  }
2123 
2124  ftpState->writeCommand(cbuf);
2125  ftpState->state = Ftp::Client::SENT_LIST;
2126 }
2127 
2128 static void
2130 {
2131  /* check the server control channel is still available */
2132  if (!ftpState || !ftpState->haveControlChannel("ftpSendNlst"))
2133  return;
2134 
2135  debugs(9, 3, MYNAME);
2136 
2137  ftpState->flags.tried_nlst = 1;
2138 
2139  if (ftpState->filepath) {
2140  snprintf(cbuf, CTRL_BUFLEN, "NLST %s\r\n", ftpState->filepath);
2141  } else {
2142  snprintf(cbuf, CTRL_BUFLEN, "NLST\r\n");
2143  }
2144 
2145  ftpState->writeCommand(cbuf);
2146  ftpState->state = Ftp::Client::SENT_NLST;
2147 }
2148 
2149 static void
2151 {
2152  int code = ftpState->ctrl.replycode;
2153  debugs(9, 3, MYNAME);
2154 
2155  if (code == 125 || (code == 150 && Comm::IsConnOpen(ftpState->data.conn))) {
2156  /* Begin data transfer */
2157  debugs(9, 3, "begin data transfer from " << ftpState->data.conn->remote << " (" << ftpState->data.conn->local << ")");
2158  ftpState->switchTimeoutToDataChannel();
2159  ftpState->maybeReadVirginBody();
2160  ftpState->state = Ftp::Client::READING_DATA;
2161  return;
2162  } else if (code == 150) {
2163  /* Accept data channel */
2164  debugs(9, 3, "accept data channel from " << ftpState->data.conn->remote << " (" << ftpState->data.conn->local << ")");
2165  ftpState->listenForDataChannel(ftpState->data.conn);
2166  return;
2167  } else if (!ftpState->flags.tried_nlst && code > 300) {
2168  ftpSendNlst(ftpState);
2169  } else {
2170  ftpFail(ftpState);
2171  return;
2172  }
2173 }
2174 
2175 static void
2177 {
2178  /* check the server control channel is still available */
2179  if (!ftpState || !ftpState->haveControlChannel("ftpSendRetr"))
2180  return;
2181 
2182  debugs(9, 3, MYNAME);
2183 
2184  assert(ftpState->filepath != nullptr);
2185  snprintf(cbuf, CTRL_BUFLEN, "RETR %s\r\n", ftpState->filepath);
2186  ftpState->writeCommand(cbuf);
2187  ftpState->state = Ftp::Client::SENT_RETR;
2188 }
2189 
2190 static void
2192 {
2193  int code = ftpState->ctrl.replycode;
2194  debugs(9, 3, MYNAME);
2195 
2196  if (code == 125 || (code == 150 && Comm::IsConnOpen(ftpState->data.conn))) {
2197  /* Begin data transfer */
2198  debugs(9, 3, "begin data transfer from " << ftpState->data.conn->remote << " (" << ftpState->data.conn->local << ")");
2199  ftpState->switchTimeoutToDataChannel();
2200  ftpState->maybeReadVirginBody();
2201  ftpState->state = Ftp::Client::READING_DATA;
2202  } else if (code == 150) {
2203  /* Accept data channel */
2204  ftpState->listenForDataChannel(ftpState->data.conn);
2205  } else if (code >= 300) {
2206  if (!ftpState->flags.try_slash_hack) {
2207  /* Try this as a directory missing trailing slash... */
2208  ftpState->hackShortcut(ftpSendCwd);
2209  } else {
2210  ftpFail(ftpState);
2211  }
2212  } else {
2213  ftpFail(ftpState);
2214  }
2215 }
2216 
2221 void
2223 {
2224  assert(entry);
2225  entry->lock("Ftp::Gateway");
2226  ErrorState ferr(ERR_DIR_LISTING, Http::scOkay, request.getRaw(), fwd->al);
2227  ferr.ftp.listing = &listing;
2228  safe_free(ferr.ftp.cwd_msg);
2229  ferr.ftp.cwd_msg = xstrdup(cwd_message.size()? cwd_message.termedBuf() : "");
2230  ferr.ftp.server_msg = ctrl.message;
2231  ctrl.message = nullptr;
2232  entry->replaceHttpReply(ferr.BuildHttpReply());
2233  entry->flush();
2234  entry->unlock("Ftp::Gateway");
2235 }
2236 
2237 static void
2239 {
2240  int code = ftpState->ctrl.replycode;
2241  debugs(9, 3, MYNAME);
2242 
2243  if (code == 226 || code == 250) {
2244  /* Connection closed; retrieval done. */
2245  if (ftpState->flags.listing) {
2246  ftpState->completedListing();
2247  /* QUIT operation handles sending the reply to client */
2248  }
2249  ftpState->markParsedVirginReplyAsWhole("ftpReadTransferDone code 226 or 250");
2250  ftpSendQuit(ftpState);
2251  } else { /* != 226 */
2252  debugs(9, DBG_IMPORTANT, "Got code " << code << " after reading data");
2253  ftpState->failed(ERR_FTP_FAILURE, 0);
2254  /* failed closes ctrl.conn and frees ftpState */
2255  return;
2256  }
2257 }
2258 
2259 // premature end of the request body
2260 void
2262 {
2264  debugs(9, 3, "ftpState=" << this);
2265  failed(ERR_READ_ERROR, 0);
2266 }
2267 
2268 static void
2270 {
2271  int code = ftpState->ctrl.replycode;
2272  debugs(9, 3, MYNAME);
2273 
2274  if (!(code == 226 || code == 250)) {
2275  debugs(9, DBG_IMPORTANT, "Got code " << code << " after sending data");
2276  ftpState->failed(ERR_FTP_PUT_ERROR, 0);
2277  return;
2278  }
2279 
2280  ftpState->entry->timestampsSet(); /* XXX Is this needed? */
2281  ftpState->markParsedVirginReplyAsWhole("ftpWriteTransferDone code 226 or 250");
2282  ftpSendReply(ftpState);
2283 }
2284 
2285 static void
2287 {
2288  /* check the server control channel is still available */
2289  if (!ftpState || !ftpState->haveControlChannel("ftpSendQuit"))
2290  return;
2291 
2292  snprintf(cbuf, CTRL_BUFLEN, "QUIT\r\n");
2293  ftpState->writeCommand(cbuf);
2294  ftpState->state = Ftp::Client::SENT_QUIT;
2295 }
2296 
2300 static void
2302 {
2303  ftpState->serverComplete();
2304 }
2305 
2306 static void
2308 {
2309  ftpState->flags.try_slash_hack = 1;
2310  /* Free old paths */
2311 
2312  debugs(9, 3, MYNAME);
2313 
2314  if (ftpState->pathcomps)
2315  wordlistDestroy(&ftpState->pathcomps);
2316 
2317  /* Build the new path */
2318  safe_free(ftpState->filepath);
2319  ftpState->filepath = SBufToCstring(AnyP::Uri::Decode(ftpState->request->url.absolutePath()));
2320 
2321  /* And off we go */
2322  ftpGetFile(ftpState);
2323 }
2324 
2328 void
2330 {
2331  debugs(9, 3, MYNAME);
2332 
2333  if (old_request != nullptr) {
2334  safe_free(old_request);
2335  safe_free(old_reply);
2336  }
2337 }
2338 
2339 void
2341 {
2342  /* Clear some unwanted state */
2343  setCurrentOffset(0);
2344  restart_offset = 0;
2345  /* Save old error message & some state info */
2346 
2347  debugs(9, 3, MYNAME);
2348 
2349  if (old_request == nullptr) {
2350  old_request = ctrl.last_command;
2351  ctrl.last_command = nullptr;
2352  old_reply = ctrl.last_reply;
2353  ctrl.last_reply = nullptr;
2354 
2355  if (pathcomps == nullptr && filepath != nullptr)
2356  old_filepath = xstrdup(filepath);
2357  }
2358 
2359  /* Jump to the "hack" state */
2360  nextState(this);
2361 }
2362 
2363 static void
2365 {
2366  const bool slashHack = ftpState->request->url.path().caseCmp("/%2f", 4)==0;
2367  int code = ftpState->ctrl.replycode;
2368  err_type error_code = ERR_NONE;
2369 
2370  debugs(9, 6, "state " << ftpState->state <<
2371  " reply code " << code << "flags(" <<
2372  (ftpState->flags.isdir?"IS_DIR,":"") <<
2373  (ftpState->flags.try_slash_hack?"TRY_SLASH_HACK":"") << "), " <<
2374  "mdtm=" << ftpState->mdtm << ", size=" << ftpState->theSize <<
2375  "slashhack=" << (slashHack? "T":"F"));
2376 
2377  /* Try the / hack to support "Netscape" FTP URL's for retrieving files */
2378  if (!ftpState->flags.isdir && /* Not a directory */
2379  !ftpState->flags.try_slash_hack && !slashHack && /* Not doing slash hack */
2380  ftpState->mdtm <= 0 && ftpState->theSize < 0) { /* Not known as a file */
2381 
2382  switch (ftpState->state) {
2383 
2384  case Ftp::Client::SENT_CWD:
2385 
2387  /* Try the / hack */
2388  ftpState->hackShortcut(ftpTrySlashHack);
2389  return;
2390 
2391  default:
2392  break;
2393  }
2394  }
2395 
2396  Http::StatusCode sc = ftpState->failedHttpStatus(error_code);
2397  const auto ftperr = new ErrorState(error_code, sc, ftpState->fwd->request, ftpState->fwd->al);
2398  ftpState->failed(error_code, 0, ftperr);
2399  ftperr->detailError(new Ftp::ErrorDetail(code));
2400  HttpReply *newrep = ftperr->BuildHttpReply();
2401  delete ftperr;
2402 
2403  ftpState->entry->replaceHttpReply(newrep);
2404  ftpSendQuit(ftpState);
2405 }
2406 
2409 {
2410  if (error == ERR_NONE) {
2411  switch (state) {
2412 
2413  case SENT_USER:
2414 
2415  case SENT_PASS:
2416 
2417  if (ctrl.replycode > 500) {
2419  return password_url ? Http::scForbidden : Http::scUnauthorized;
2420  } else if (ctrl.replycode == 421) {
2423  }
2424  break;
2425 
2426  case SENT_CWD:
2427 
2428  case SENT_RETR:
2429  if (ctrl.replycode == 550) {
2431  return Http::scNotFound;
2432  }
2433  break;
2434 
2435  default:
2436  break;
2437  }
2438  }
2440 }
2441 
2442 static void
2444 {
2445  int code = ftpState->ctrl.replycode;
2446  Http::StatusCode http_code;
2447  err_type err_code = ERR_NONE;
2448 
2449  debugs(9, 3, ftpState->entry->url() << ", code " << code);
2450 
2451  if (cbdataReferenceValid(ftpState))
2452  debugs(9, 5, "ftpState (" << ftpState << ") is valid!");
2453 
2454  if (code == 226 || code == 250) {
2455  err_code = (ftpState->mdtm > 0) ? ERR_FTP_PUT_MODIFIED : ERR_FTP_PUT_CREATED;
2456  http_code = (ftpState->mdtm > 0) ? Http::scAccepted : Http::scCreated;
2457  } else if (code == 227) {
2458  err_code = ERR_FTP_PUT_CREATED;
2459  http_code = Http::scCreated;
2460  } else {
2461  err_code = ERR_FTP_PUT_ERROR;
2462  http_code = Http::scInternalServerError;
2463  }
2464 
2465  ErrorState err(err_code, http_code, ftpState->request.getRaw(), ftpState->fwd->al);
2466 
2467  if (ftpState->old_request)
2468  err.ftp.request = xstrdup(ftpState->old_request);
2469  else
2470  err.ftp.request = xstrdup(ftpState->ctrl.last_command);
2471 
2472  if (ftpState->old_reply)
2473  err.ftp.reply = xstrdup(ftpState->old_reply);
2474  else if (ftpState->ctrl.last_reply)
2475  err.ftp.reply = xstrdup(ftpState->ctrl.last_reply);
2476  else
2477  err.ftp.reply = xstrdup("");
2478 
2479  err.detailError(new Ftp::ErrorDetail(code));
2480 
2481  ftpState->entry->replaceHttpReply(err.BuildHttpReply());
2482 
2483  ftpSendQuit(ftpState);
2484 }
2485 
2486 void
2488 {
2489  debugs(9, 3, MYNAME);
2490 
2491  if (flags.http_header_sent)
2492  return;
2493 
2494  HttpReply *reply = new HttpReply;
2495 
2496  flags.http_header_sent = 1;
2497 
2498  assert(entry->isEmpty());
2499 
2500  entry->buffer(); /* released when done processing current data payload */
2501 
2502  SBuf urlPath = request->url.path();
2503  auto t = urlPath.rfind('/');
2504  SBuf filename = urlPath.substr(t != SBuf::npos ? t : 0);
2505 
2506  const char *mime_type = nullptr;
2507  const char *mime_enc = nullptr;
2508 
2509  if (flags.isdir) {
2510  mime_type = "text/html";
2511  } else {
2512  switch (typecode) {
2513 
2514  case 'I':
2515  mime_type = "application/octet-stream";
2516  // XXX: performance regression, c_str() may reallocate
2517  mime_enc = mimeGetContentEncoding(filename.c_str());
2518  break;
2519 
2520  case 'A':
2521  mime_type = "text/plain";
2522  break;
2523 
2524  default:
2525  // XXX: performance regression, c_str() may reallocate
2526  mime_type = mimeGetContentType(filename.c_str());
2527  mime_enc = mimeGetContentEncoding(filename.c_str());
2528  break;
2529  }
2530  }
2531 
2532  /* set standard stuff */
2533 
2534  if (0 == getCurrentOffset()) {
2535  /* Full reply */
2536  reply->setHeaders(Http::scOkay, "Gatewaying", mime_type, theSize, mdtm, -2);
2537  } else if (theSize < getCurrentOffset()) {
2538  /*
2539  * DPW 2007-05-04
2540  * offset should not be larger than theSize. We should
2541  * not be seeing this condition any more because we'll only
2542  * send REST if we know the theSize and if it is less than theSize.
2543  */
2544  debugs(0, DBG_CRITICAL, "ERROR: " <<
2545  " current offset=" << getCurrentOffset() <<
2546  ", but theSize=" << theSize <<
2547  ". assuming full content response");
2548  reply->setHeaders(Http::scOkay, "Gatewaying", mime_type, theSize, mdtm, -2);
2549  } else {
2550  /* Partial reply */
2551  HttpHdrRangeSpec range_spec;
2552  range_spec.offset = getCurrentOffset();
2553  range_spec.length = theSize - getCurrentOffset();
2554  reply->setHeaders(Http::scPartialContent, "Gatewaying", mime_type, theSize - getCurrentOffset(), mdtm, -2);
2555  httpHeaderAddContRange(&reply->header, range_spec, theSize);
2556  }
2557 
2558  /* additional info */
2559  if (mime_enc)
2560  reply->header.putStr(Http::HdrType::CONTENT_ENCODING, mime_enc);
2561 
2562  reply->sources |= Http::Message::srcFtp;
2563  setVirginReply(reply);
2564  adaptOrFinalizeReply();
2565 }
2566 
2567 void
2569 {
2571 
2572  StoreEntry *e = entry;
2573 
2574  e->timestampsSet();
2575 
2576  // makePublic() if allowed/possible or release() otherwise
2577  if (flags.authenticated || // authenticated requests can't be cached
2578  getCurrentOffset() ||
2579  !e->makePublic()) {
2580  e->release();
2581  }
2582 }
2583 
2584 HttpReply *
2586 {
2588  HttpReply *newrep = err.BuildHttpReply();
2589 #if HAVE_AUTH_MODULE_BASIC
2590  /* add Authenticate header */
2591  // XXX: performance regression. c_str() may reallocate
2592  newrep->header.putAuth("Basic", realm.c_str());
2593 #else
2594  (void)realm;
2595 #endif
2596  return newrep;
2597 }
2598 
2599 const SBuf &
2601 {
2602  SBuf newbuf("%2f");
2603 
2604  if (request->url.getScheme() != AnyP::PROTO_FTP) {
2605  static const SBuf nil;
2606  return nil;
2607  }
2608 
2609  if (request->url.path().startsWith(AnyP::Uri::SlashPath())) {
2610  newbuf.append(request->url.path());
2611  request->url.path(newbuf);
2612  } else if (!request->url.path().startsWith(newbuf)) {
2613  newbuf.append(request->url.path().substr(1));
2614  request->url.path(newbuf);
2615  }
2616 
2617  return request->effectiveRequestUri();
2618 }
2619 
2624 void
2625 Ftp::Gateway::writeReplyBody(const char *dataToWrite, size_t dataLength)
2626 {
2627  debugs(9, 5, "writing " << dataLength << " bytes to the reply");
2628  addVirginReplyBody(dataToWrite, dataLength);
2629 }
2630 
2637 void
2639 {
2640  if (fwd == nullptr || flags.completed_forwarding) {
2641  debugs(9, 3, "avoid double-complete on FD " <<
2642  (ctrl.conn ? ctrl.conn->fd : -1) << ", Data FD " << (data.conn ? data.conn->fd : -1) <<
2643  ", this " << this << ", fwd " << fwd);
2644  return;
2645  }
2646 
2647  flags.completed_forwarding = true;
2649 }
2650 
2657 bool
2658 Ftp::Gateway::haveControlChannel(const char *caller_name) const
2659 {
2660  if (doneWithServer())
2661  return false;
2662 
2663  /* doneWithServer() only checks BOTH channels are closed. */
2664  if (!Comm::IsConnOpen(ctrl.conn)) {
2665  debugs(9, DBG_IMPORTANT, "WARNING: FTP Server Control channel is closed, but Data channel still active.");
2666  debugs(9, 2, caller_name << ": attempted on a closed FTP channel.");
2667  return false;
2668  }
2669 
2670  return true;
2671 }
2672 
2673 bool
2675 {
2676  // TODO: Can we do what Ftp::Relay::mayReadVirginReplyBody() does instead?
2677  return !doneWithServer();
2678 }
2679 
2680 void
2681 Ftp::StartGateway(FwdState *const fwdState)
2682 {
2683  AsyncJob::Start(new Ftp::Gateway(fwdState));
2684 }
2685 
static void ftpTrySlashHack(Ftp::Gateway *ftpState)
Definition: FtpGateway.cc:2307
const char * xstrerr(int error)
Definition: xstrerror.cc:83
static FTPSM ftpSendCwd
Definition: FtpGateway.cc:222
void processReplyBody() override
Definition: FtpGateway.cc:967
static FTPSM ftpReadQuit
Definition: FtpGateway.cc:241
CBDATA_CHILD(Gateway)
void handleRequestBodyProducerAborted() override
Definition: FtpGateway.cc:2261
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:71
StoreEntry * entry
Definition: Client.h:177
size_type find(char c, size_type startPos=0) const
Definition: SBuf.cc:584
void wordlistDestroy(wordlist **list)
destroy a wordlist
Definition: wordlist.cc:16
Gateway(FwdState *)
Definition: FtpGateway.cc:329
@ scAccepted
Definition: StatusCode.h:29
char * cwd_msg
Definition: errorpage.h:192
@ scUnauthorized
Definition: StatusCode.h:46
@ METHOD_HEAD
Definition: MethodType.h:28
#define DBG_CRITICAL
Definition: Stream.h:37
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
@ ERR_READ_ERROR
Definition: forward.h:28
#define xmalloc
~Gateway() override
Definition: FtpGateway.cc:365
@ ERR_FTP_FORBIDDEN
Definition: forward.h:56
static PF ftpDataWrite
Definition: FtpGateway.cc:154
MemBuf * listing
Definition: errorpage.h:193
SBuf ftpRealm()
Definition: FtpGateway.cc:1269
bool makePublic(const KeyScope keyScope=ksDefault)
Definition: store.cc:167
HttpHeader header
Definition: Message.h:74
char * old_filepath
Definition: FtpGateway.cc:121
static FTPSM ftpReadTransferDone
Definition: FtpGateway.cc:232
bool isEmpty() const
Definition: SBuf.h:435
@ ERR_FTP_PUT_ERROR
Definition: forward.h:54
void maybeReadVirginBody() override
read response data from the network
Definition: FtpClient.cc:918
char * reply
Definition: errorpage.h:191
const char * url() const
Definition: store.cc:1566
void connectDataChannel()
Definition: FtpClient.cc:763
const char *const crlf
Definition: FtpClient.cc:40
void writeCommand(const char *buf)
Definition: FtpClient.cc:824
bool pasv_supported
PASV command is allowed.
Definition: FtpGateway.cc:60
static void ftpOpenListenSocket(Ftp::Gateway *ftpState, int fallback)
Definition: FtpGateway.cc:1754
@ ENTRY_ABORTED
Definition: enums.h:110
HttpReply * BuildHttpReply(void)
Definition: errorpage.cc:1316
virtual void handleRequestBodyProducerAborted()=0
Definition: Client.cc:355
void error(char *format,...)
AccessLogEntryPointer al
info for the future access.log entry
Definition: FwdState.h:204
static FTPSM ftpSendRest
Definition: FtpGateway.cc:228
@ ERR_CACHE_ACCESS_DENIED
Definition: forward.h:19
bool tried_auth_nopass
auth tried username with no password already.
Definition: FtpGateway.cc:68
void dataClosed(const CommCloseCbParams &io) override
handler called by Comm when FTP data channel is closed unexpectedly
Definition: FtpGateway.cc:317
struct ErrorState::@47 ftp
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:93
Definition: SBuf.h:93
static HttpReply * ftpAuthRequired(HttpRequest *request, SBuf &realm, AccessLogEntry::Pointer &)
Definition: FtpGateway.cc:2585
bool sendPassive()
Definition: FtpClient.cc:654
bool htmlifyListEntry(const char *line, PackableStream &)
Definition: FtpGateway.cc:767
static FTPSM ftpReadSize
Definition: FtpGateway.cc:212
void SBufToCstring(char *d, const SBuf &s)
Definition: SBuf.h:756
#define xtoupper(x)
Definition: xis.h:16
#define xstrdup
void handleControlReply() override
Definition: FtpGateway.cc:1174
@ TIMEOUT
Definition: Flag.h:18
static FTPSM ftpSendRetr
Definition: FtpGateway.cc:230
@ CONTENT_ENCODING
int checkAuth(const HttpHeader *req_hdr)
Definition: FtpGateway.cc:1034
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:698
C * getRaw() const
Definition: RefCount.h:89
void initReadBuf()
Definition: FtpClient.cc:222
void loginParser(const SBuf &login, bool escaped)
Definition: FtpGateway.cc:398
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
void detailError(const ErrorDetail::Pointer &dCode)
set error type-specific detail code
Definition: errorpage.h:111
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:270
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
@ OK
Definition: Flag.h:16
static FTPSM ftpSendUser
Definition: FtpGateway.cc:203
#define rfc1738_escape(x)
Definition: rfc1738.h:52
virtual void failed(err_type error=ERR_NONE, int xerrno=0, ErrorState *ftperr=nullptr)
handle a fatal transaction error, closing the control connection
Definition: FtpClient.cc:263
static FTPSM ftpReadRest
Definition: FtpGateway.cc:229
static FTPSM ftpReadList
Definition: FtpGateway.cc:227
@ ERR_NONE
Definition: forward.h:15
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition: store.cc:1705
void * memAllocate(mem_type)
Allocate one element from the typed pool.
Definition: old_api.cc:122
static FTPSM ftpSendNlst
Definition: FtpGateway.cc:226
bool isIPv4() const
Definition: Address.cc:178
StatusCode
Definition: StatusCode.h:20
static FTPSM ftpSendMkdir
Definition: FtpGateway.cc:237
err_type
Definition: forward.h:14
static FTPSM ftpRestOrList
Definition: FtpGateway.cc:224
virtual void haveParsedReplyHeaders()
called when we have final (possibly adapted) reply headers; kids extend
Definition: Client.cc:541
int64_t currentOffset
Definition: Client.h:173
static FTPSM ftpSendType
Definition: FtpGateway.cc:207
char * wordlistChopHead(wordlist **wl)
Definition: wordlist.cc:42
#define w_space
int xerrno
The last errno to occur. non-zero if flag is Comm::COMM_ERROR.
Definition: CommCalls.h:83
int64_t theSize
Definition: FtpGateway.cc:113
SBuf substr(size_type pos, size_type n=npos) const
Definition: SBuf.cc:576
void serverComplete()
Definition: Client.cc:167
FTP client functionality shared among FTP Gateway and Relay clients.
Definition: FtpClient.h:110
@ CONTENT_LENGTH
Definition: forward.h:23
virtual void completeForwarding()
Definition: Client.cc:216
void() StateMethod(Ftp::Gateway *)
Definition: FtpGateway.cc:90
CBDATA_NAMESPACED_CLASS_INIT(Ftp, Gateway)
static FTPSM ftpListDir
Definition: FtpGateway.cc:220
char * showname
Definition: FtpGateway.cc:189
void comm_open_listener(int sock_type, int proto, Comm::ConnectionPointer &conn, const char *note)
Definition: comm.cc:259
virtual bool haveControlChannel(const char *caller_name) const
Definition: FtpGateway.cc:2658
size_type rfind(char c, size_type endPos=npos) const
Definition: SBuf.cc:692
#define MAX_URL
Definition: defines.h:76
bool handlePasvReply(Ip::Address &remoteAddr)
Definition: FtpClient.cc:456
static FTPSM ftpSendQuit
Definition: FtpGateway.cc:240
struct tok tokens[]
Definition: parse.c:168
static FTPSM ftpReadCwd
Definition: FtpGateway.cc:223
static FTPSM ftpReadUser
Definition: FtpGateway.cc:204
char * proxy_host
Definition: FtpGateway.cc:118
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
wordlist * server_msg
Definition: errorpage.h:189
int size
Definition: ModDevPoll.cc:70
wordlist * message
Definition: FtpClient.h:82
static FTPSM ftpReadMkdir
Definition: FtpGateway.cc:238
void start() override
called by AsyncStart; do not call directly
Definition: FtpClient.cc:216
static char cbuf[CTRL_BUFLEN]
Definition: FtpGateway.cc:194
bool completed_forwarding
Definition: FtpGateway.cc:86
String base_href
Definition: FtpGateway.cc:109
static FTPSM ftpSendSize
Definition: FtpGateway.cc:211
@ ERR_FTP_PUT_MODIFIED
Definition: forward.h:58
void rfc1738_unescape(char *url)
Definition: rfc1738.c:146
@ scForbidden
Definition: StatusCode.h:48
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
virtual void handleControlReply()
Definition: FtpClient.cc:420
char * last_command
Definition: FtpClient.h:83
static FTPSM ftpSendPass
Definition: FtpGateway.cc:205
virtual void dataClosed(const CommCloseCbParams &io)
handler called by Comm when FTP data channel is closed unexpectedly
Definition: FtpClient.cc:811
void buildTitleUrl()
Definition: FtpGateway.cc:1119
MemBlob::size_type size_type
Definition: SBuf.h:96
#define COMM_REUSEADDR
Definition: Connection.h:48
void append(char const *buf, int len)
Definition: String.cc:131
static FTPSM ftpReadWelcome
Definition: FtpGateway.cc:202
String clean_url
Definition: FtpGateway.cc:107
int64_t size
Definition: FtpGateway.cc:186
HttpRequestPointer request
Definition: errorpage.h:177
void putAuth(const char *auth_scheme, const char *realm)
Definition: HttpHeader.cc:1137
char * html_quote(const char *string)
Definition: Quoting.cc:42
void appendSuccessHeader()
Definition: FtpGateway.cc:2487
Definition: MemBuf.h:23
Ip::Address local
Definition: Connection.h:149
SBuf text("GET http://resource.com/path HTTP/1.1\r\n" "Host: resource.com\r\n" "Cookie: laijkpk3422r j1noin \r\n" "\r\n")
#define EBIT_TEST(flag, bit)
Definition: defines.h:67
bool handleEpsvReply(Ip::Address &remoteAddr)
Definition: FtpClient.cc:492
String cwd_message
Definition: FtpGateway.cc:120
@ scPartialContent
Definition: StatusCode.h:33
size_t list_width
Definition: FtpGateway.cc:119
static FTPSM ftpReadEPRT
Definition: FtpGateway.cc:213
int xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
POSIX setsockopt(2) equivalent.
Definition: socket.h:122
FTPSM * FTP_SM_FUNCS[]
Definition: FtpGateway.cc:285
void timeout(const CommTimeoutCbParams &io) override
read timeout handler
Definition: FtpGateway.cc:487
Comm::ConnectionPointer conn
Definition: CommCalls.h:80
@ scCreated
Definition: StatusCode.h:28
int64_t getInt64(Http::HdrType id) const
Definition: HttpHeader.cc:1266
#define safe_free(x)
Definition: xalloc.h:73
@ srcFtp
ftp_port or FTP server
Definition: Message.h:40
Ip::Address remote
Definition: Connection.h:152
void close()
planned close: removes the close handler and calls comm_close
Definition: FtpClient.cc:107
void ftpAcceptDataConnection(const CommAcceptCbParams &io)
Definition: FtpGateway.cc:1883
SBuf getAuthToken(Http::HdrType id, const char *auth_scheme) const
Definition: HttpHeader.cc:1408
#define assert(EX)
Definition: assert.h:17
bool tried_auth_anonymous
auth has tried to use anonymous credentials already.
Definition: FtpGateway.cc:67
bool authenticated
authentication success
Definition: FtpGateway.cc:66
SSL Connection
Definition: Session.h:49
FwdState::Pointer fwd
Definition: Client.h:178
bool mayReadVirginReplyBody() const override
whether we may receive more virgin response body bytes
Definition: FtpGateway.cc:2674
static FTPSM ftpSendList
Definition: FtpGateway.cc:225
@ METHOD_PUT
Definition: MethodType.h:27
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:82
void parseListing()
Definition: FtpGateway.cc:883
static FTPSM ftpTraverseDirectory
Definition: FtpGateway.cc:219
String title_url
Definition: FtpGateway.cc:108
@ scServiceUnavailable
Definition: StatusCode.h:76
const AnyP::UriScheme & getScheme() const
Definition: Uri.h:58
static FTPSM ftpReadEPSV
Definition: FtpGateway.cc:217
#define Assure(condition)
Definition: Assure.h:35
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:70
char * filepath
Definition: FtpGateway.cc:115
@ scInternalServerError
Definition: StatusCode.h:73
Comm::ConnectionPointer conn
channel descriptor
Definition: FtpClient.h:58
wordlist * pathcomps
Definition: FtpGateway.cc:114
const char * null_string
const char * c_str()
Definition: SBuf.cc:516
int64_t strtoll(const char *nptr, char **endptr, int base)
Definition: strtoll.c:61
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:419
char * xstrndup(const char *s, size_t n)
Definition: xstring.cc:56
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
int reply_hdr_state
Definition: FtpGateway.cc:106
#define xfree
void completeForwarding() override
Definition: FtpGateway.cc:2638
char * anon_user
Definition: SquidConfig.h:413
char * reply_hdr
Definition: FtpGateway.cc:105
DataChannel data
FTP data channel state.
Definition: FtpClient.h:143
static FTPSM ftpSendPassive
Definition: FtpGateway.cc:216
wordlist * next
Definition: wordlist.h:60
bool mimeGetViewOption(const char *fn)
Definition: mime.cc:223
static const size_type npos
Definition: SBuf.h:100
void markParsedVirginReplyAsWhole(const char *reasonWeAreSure)
Definition: Client.cc:158
uint32_t sources
The message sources.
Definition: Message.h:99
void writeReplyBody(const char *, size_t len)
Definition: FtpGateway.cc:2625
static void ftpListPartsFree(ftpListParts **parts)
Definition: FtpGateway.cc:521
static const char * Month[]
Definition: FtpGateway.cc:503
void hackShortcut(StateMethod *nextState)
Definition: FtpGateway.cc:2340
#define fd_table
Definition: fde.h:189
static FTPSM ftpReadPasv
Definition: FtpGateway.cc:218
void clear()
remove the close handler, leave connection open
Definition: FtpClient.cc:128
static const SBuf & SlashPath()
the static '/' default URL-path
Definition: Uri.cc:135
char user[MAX_URL]
Definition: FtpGateway.cc:102
@ MEM_4K_BUF
Definition: forward.h:49
void checkUrlpath()
Definition: FtpGateway.cc:1079
HttpRequestMethod method
Definition: HttpRequest.h:114
void path(const char *p)
Definition: Uri.h:96
static FTPSM ftpReadType
Definition: FtpGateway.cc:208
Comm::ConnectionPointer listenConn
Definition: FtpClient.h:65
@ scNotFound
Definition: StatusCode.h:49
const char * mimeGetContentType(const char *fn)
Definition: mime.cc:181
virtual Http::StatusCode failedHttpStatus(err_type &error)
Definition: FtpClient.cc:312
MemBuf listing
FTP directory listing in HTML format.
Definition: FtpGateway.cc:123
void dataChannelConnected(const CommConnectCbParams &io) override
Definition: FtpGateway.cc:1732
@ PROTO_FTP
Definition: ProtocolType.h:26
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:619
char * key
Definition: wordlist.h:59
@ MEM_8K_BUF
Definition: forward.h:50
char * last_reply
Definition: FtpClient.h:84
const SBuf & UrlWith2f(HttpRequest *)
Definition: FtpGateway.cc:2600
static ftpListParts * ftpListParseParts(const char *buf, struct Ftp::GatewayFlags flags)
Definition: FtpGateway.cc:533
int64_t restart_offset
Definition: FtpGateway.cc:117
static FTPSM ftpReadMdtm
Definition: FtpGateway.cc:210
static SBuf Decode(const SBuf &)
%-decode the given buffer
Definition: Uri.cc:105
void processHeadResponse()
Definition: FtpGateway.cc:1690
void httpHeaderAddContRange(HttpHeader *, HttpHdrRangeSpec, int64_t)
bool timestampsSet()
Definition: store.cc:1387
GatewayFlags flags
Definition: FtpGateway.cc:125
char * content()
start of the added data
Definition: MemBuf.h:41
static int is_month(const char *buf)
Definition: FtpGateway.cc:509
void putStr(Http::HdrType id, const char *str)
Definition: HttpHeader.cc:1128
static FTPSM ftpFail
Definition: FtpGateway.cc:239
Ftp::StateMethod FTPSM
Definition: FtpGateway.cc:180
static FTPSM ftpReadStor
Definition: FtpGateway.cc:234
void memFree(void *, int type)
Free a element allocated by memAllocate()
Definition: minimal.cc:61
void readStor()
Definition: FtpGateway.cc:2014
char * old_request
Definition: FtpClient.h:177
void loginFailed(void)
Definition: FtpGateway.cc:1225
void StartGateway(FwdState *const fwdState)
A new FTP Gateway job.
Definition: FtpGateway.cc:2681
static FTPSM ftpSendMdtm
Definition: FtpGateway.cc:209
const char * mimeGetContentEncoding(const char *fn)
Definition: mime.cc:195
void reset(char const *str)
Definition: String.cc:123
struct SquidConfig::@92 Ftp
#define DBG_IMPORTANT
Definition: Stream.h:38
void listenForDataChannel(const Comm::ConnectionPointer &conn)
create a data channel acceptor and start listening.
Definition: FtpGateway.cc:450
char password[MAX_URL]
Definition: FtpGateway.cc:103
int restartable()
Definition: FtpGateway.cc:2064
Http::StatusCode failedHttpStatus(err_type &error) override
Definition: FtpGateway.cc:2408
#define MYNAME
Definition: Stream.h:219
void completedListing(void)
Definition: FtpGateway.cc:2222
void release(const bool shareable=false)
Definition: store.cc:1146
#define PRId64
Definition: types.h:104
void haveParsedReplyHeaders() override
called when we have final (possibly adapted) reply headers; kids extend
Definition: FtpGateway.cc:2568
time_t ParseIso3307(const char *)
Convert from ISO 3307 style time: YYYYMMDDHHMMSS or YYYYMMDDHHMMSS.xxx.
Definition: iso3307.cc:18
char * rfc1738_do_escape(const char *url, int flags)
Definition: rfc1738.c:56
int64_t getCurrentOffset() const
Definition: FtpGateway.cc:151
CtrlChannel ctrl
FTP control channel state.
Definition: FtpClient.h:142
static FTPSM ftpReadPORT
Definition: FtpGateway.cc:215
SBuf & absolutePath() const
RFC 3986 section 4.2 relative reference called 'absolute-path'.
Definition: Uri.cc:775
int token
Definition: parse.c:163
@ ERR_FTP_FAILURE
Definition: forward.h:53
char * dirpath
Definition: FtpGateway.cc:116
const char * mimeGetIconURL(const char *fn)
Definition: mime.cc:162
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:170
#define xisspace(x)
Definition: xis.h:15
static FTPSM ftpSendPORT
Definition: FtpGateway.cc:214
#define CTRL_BUFLEN
Definition: FtpGateway.cc:193
@ scOkay
Definition: StatusCode.h:27
bool mimeGetDownloadOption(const char *fn)
Definition: mime.cc:216
SBuf & appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Definition: SBuf.cc:229
#define rfc1738_escape_part(x)
Definition: rfc1738.h:55
const char * wordlistAdd(wordlist **list, const char *key)
Definition: wordlist.cc:25
static FTPSM ftpSendStor
Definition: FtpGateway.cc:233
const SBuf & effectiveRequestUri() const
RFC 7230 section 5.5 - Effective Request URI.
Definition: HttpRequest.cc:741
virtual void timeout(const CommTimeoutCbParams &io)
read timeout handler
Definition: FtpClient.cc:891
HttpRequest * request
Definition: FwdState.h:203
void host(const char *src)
Definition: Uri.cc:142
@ ERR_FTP_UNAVAILABLE
Definition: forward.h:52
#define MAX_TOKENS
Definition: FtpGateway.cc:530
static FTPSM ftpSendReply
Definition: FtpGateway.cc:236
HttpRequestPointer request
Definition: Client.h:179
@ ERR_FTP_NOT_FOUND
Definition: forward.h:55
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
@ ERR_FTP_PUT_CREATED
Definition: forward.h:57
static FTPSM ftpReadRetr
Definition: FtpGateway.cc:231
char * old_reply
Definition: FtpClient.h:178
@ ERR_DIR_LISTING
Definition: forward.h:70
static FTPSM ftpReadPass
Definition: FtpGateway.cc:206
void setCurrentOffset(int64_t offset)
Definition: FtpGateway.cc:150
void start() override
called by AsyncStart; do not call directly
Definition: FtpGateway.cc:1152
void switchTimeoutToDataChannel()
Definition: FtpClient.cc:1070
size_type copy(char *dest, size_type n) const
Definition: SBuf.cc:500
nfmark_t nfmark
Definition: Connection.h:166
#define SQUIDSBUFPH
Definition: SBuf.h:31
char mimeGetTransferMode(const char *fn)
Definition: mime.cc:209
void PF(int, void *)
Definition: forward.h:18
static FTPSM ftpWriteTransferDone
Definition: FtpGateway.cc:235
class SquidConfig Config
Definition: SquidConfig.cc:12
static FTPSM ftpGetFile
Definition: FtpGateway.cc:221
bool epsv_all_sent
EPSV ALL has been used. Must abort on failures.
Definition: FtpGateway.cc:61
static void Start(const Pointer &job)
Definition: AsyncJob.cc:37

 

Introduction

Documentation

Support

Miscellaneous