errorpage.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 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 04 Error Generation */
10 
11 #include "squid.h"
12 #include "AccessLogEntry.h"
13 #include "cache_cf.h"
14 #include "clients/forward.h"
15 #include "comm/Connection.h"
16 #include "comm/Write.h"
17 #include "error/Detail.h"
18 #include "error/SysErrorDetail.h"
19 #include "errorpage.h"
20 #include "fde.h"
21 #include "format/Format.h"
22 #include "fs_io.h"
23 #include "html_quote.h"
24 #include "HttpHeaderTools.h"
25 #include "HttpReply.h"
26 #include "HttpRequest.h"
27 #include "MemBuf.h"
28 #include "MemObject.h"
29 #include "rfc1738.h"
30 #include "sbuf/Stream.h"
31 #include "SquidConfig.h"
32 #include "SquidTime.h"
33 #include "Store.h"
34 #include "tools.h"
35 #include "wordlist.h"
36 #if USE_AUTH
37 #include "auth/UserRequest.h"
38 #endif
39 #if USE_OPENSSL
40 #include "ssl/ErrorDetailManager.h"
41 #endif
42 
54 #if !defined(DEFAULT_SQUID_ERROR_DIR)
55 
58 #define DEFAULT_SQUID_ERROR_DIR DEFAULT_SQUID_DATA_DIR"/errors"
59 #endif
60 
63 
64 const SBuf ErrorState::LogformatMagic("@Squid{");
65 
66 /* local types */
67 
70 public:
71  ErrorDynamicPageInfo(const int anId, const char *aName, const SBuf &aCfgLocation);
73 
75  int id;
76 
80  char *page_name;
81 
83  const char *uri;
84 
86  const char *filename;
87 
90 
91  // XXX: Misnamed. Not just for redirects.
94 
95 private:
96  // no copying of any kind
98 };
99 
100 namespace ErrorPage {
101 
103 class Build
104 {
105 public:
107  const char *input = nullptr;
108  bool building_deny_info_url = false;
109  bool allowRecursion = false;
110 };
111 
114 {
115 public:
116  BuildErrorPrinter(const SBuf &anInputLocation, int aPage, const char *aMsg, const char *aNear): inputLocation(anInputLocation), page_id(aPage), msg(aMsg), near(aNear) {}
117 
119  std::ostream &print(std::ostream &) const;
120 
122  std::ostream &printLocation(std::ostream &os) const;
123 
124  /* saved constructor parameters */
126  const int page_id;
127  const char *msg;
128  const char *near;
129 };
130 
131 static inline std::ostream &
132 operator <<(std::ostream &os, const BuildErrorPrinter &context)
133 {
134  return context.print(os);
135 }
136 
137 static const char *IsDenyInfoUri(const int page_id);
138 
139 static void ImportStaticErrorText(const int page_id, const char *text, const SBuf &inputLocation);
140 static void ValidateStaticError(const int page_id, const SBuf &inputLocation);
141 
142 } // namespace ErrorPage
143 
144 /* local constant and vars */
145 
152 static const struct {
153  int type; /* and page_id */
154  const char *text;
155 }
156 
157 error_hard_text[] = {
158 
159  {
161  "\n<br>\n"
162  "<hr>\n"
163  "<div id=\"footer\">\n"
164  "Generated %T by %h (%s)\n"
165  "</div>\n"
166  "</body></html>\n"
167  },
168  {
169  TCP_RESET,
170  "reset"
171  },
172  {
174  "unexpected client disconnect"
175  },
176  {
178  "secure accept fail"
179  },
180  {
182  "request start timedout"
183  },
184  {
185  MGR_INDEX,
186  "mgr_index"
187  }
188 };
189 
191 static std::vector<ErrorDynamicPageInfo *> ErrorDynamicPages;
192 
193 /* local prototypes */
194 
196 static const int error_hard_text_count = sizeof(error_hard_text) / sizeof(*error_hard_text);
197 
199 static char **error_text = NULL;
200 
202 static int error_page_count = 0;
203 
206 
207 static const char *errorFindHardText(err_type type);
209 
213 {
214 public:
215  ErrorPageFile(const char *name, const err_type code) : TemplateFile(name, code) {}
216 
218  const char *text() { return template_.c_str(); }
219 
220 protected:
221  virtual void setDefault() override {
222  template_ = "Internal Error: Missing Template ";
223  template_.append(templateName.termedBuf());
224  }
225 };
226 
229 {
230  int tmp = (int)anErr;
231  anErr = (err_type)(++tmp);
232  return anErr;
233 }
234 
236 int operator - (err_type const &anErr, err_type const &anErr2)
237 {
238  return (int)anErr - (int)anErr2;
239 }
240 
243 static const char *
244 ErrorPage::IsDenyInfoUri(const int page_id)
245 {
246  if (ERR_MAX <= page_id && page_id < error_page_count)
247  return ErrorDynamicPages.at(page_id - ERR_MAX)->uri; // may be nil
248  return nullptr;
249 }
250 
251 void
253 {
255 
256  err_type i;
257  const char *text;
259  error_text = static_cast<char **>(xcalloc(error_page_count, sizeof(char *)));
260 
261  for (i = ERR_NONE, ++i; i < error_page_count; ++i) {
262  safe_free(error_text[i]);
263 
264  if ((text = errorFindHardText(i))) {
268  static const SBuf builtIn("built-in");
270 
271  } else if (i < ERR_MAX) {
277  ErrorPageFile errTmpl(err_type_str[i], i);
278  errTmpl.loadDefault();
279  ImportStaticErrorText(i, errTmpl.text(), errTmpl.filename);
280  } else {
285  assert(info && info->id == i && info->page_name);
286 
287  if (info->filename) {
289  ErrorPageFile errTmpl(info->filename, ERR_MAX);
290  errTmpl.loadDefault();
291  ImportStaticErrorText(i, errTmpl.text(), errTmpl.filename);
292  } else {
293  assert(info->uri);
295  }
296  }
297  }
298 
300 
301  // look for and load stylesheet into global MemBuf for it.
302  if (Config.errorStylesheet) {
303  ErrorPageFile tmpl("StylesSheet", ERR_MAX);
304  tmpl.loadFromFile(Config.errorStylesheet);
305  error_stylesheet.appendf("%s",tmpl.text());
306  }
307 
308 #if USE_OPENSSL
310 #endif
311 }
312 
313 void
315 {
316  if (error_text) {
317  int i;
318 
319  for (i = ERR_NONE + 1; i < error_page_count; ++i)
320  safe_free(error_text[i]);
321 
323  }
324 
325  while (!ErrorDynamicPages.empty()) {
326  delete ErrorDynamicPages.back();
327  ErrorDynamicPages.pop_back();
328  }
329 
330  error_page_count = 0;
331 
332 #if USE_OPENSSL
334 #endif
335 }
336 
338 static const char *
340 {
341  int i;
342 
343  for (i = 0; i < error_hard_text_count; ++i)
344  if (error_hard_text[i].type == type)
345  return error_hard_text[i].text;
346 
347  return NULL;
348 }
349 
350 TemplateFile::TemplateFile(const char *name, const err_type code): silent(false), wasLoaded(false), templateName(name), templateCode(code)
351 {
352  assert(name);
353 }
354 
355 void
357 {
358  if (loaded()) // already loaded?
359  return;
360 
362  if (Config.errorDirectory) {
363  char path[MAXPATHLEN];
364  snprintf(path, sizeof(path), "%s/%s", Config.errorDirectory, templateName.termedBuf());
365  loadFromFile(path);
366  }
367 
368 #if USE_ERR_LOCALES
369 
370  if (!loaded() && Config.errorDefaultLanguage) {
372  debugs(1, (templateCode < TCP_RESET ? DBG_CRITICAL : 3), "Unable to load default error language files. Reset to backups.");
373  }
374  }
375 #endif
376 
377  /* test default location if failed (templates == English translation base templates) */
378  if (!loaded()) {
379  tryLoadTemplate("templates");
380  }
381 
382  /* giving up if failed */
383  if (!loaded()) {
384  debugs(1, (templateCode < TCP_RESET ? DBG_CRITICAL : 3), "WARNING: failed to find or read error text file " << templateName);
385  template_.clear();
386  setDefault();
387  wasLoaded = true;
388  }
389 }
390 
391 bool
393 {
394  assert(lang);
395 
396  char path[MAXPATHLEN];
397  /* TODO: prep the directory path string to prevent snprintf ... */
398  snprintf(path, sizeof(path), "%s/%s/%s",
400  path[MAXPATHLEN-1] = '\0';
401 
402  if (loadFromFile(path))
403  return true;
404 
405 #if HAVE_GLOB
406  if ( strlen(lang) == 2) {
407  /* TODO glob the error directory for sub-dirs matching: <tag> '-*' */
408  /* use first result. */
409  debugs(4,2, HERE << "wildcard fallback errors not coded yet.");
410  }
411 #endif
412 
413  return false;
414 }
415 
416 bool
417 TemplateFile::loadFromFile(const char *path)
418 {
419  int fd;
420  char buf[4096];
421  ssize_t len;
422 
423  if (loaded()) // already loaded?
424  return true;
425 
426  fd = file_open(path, O_RDONLY | O_TEXT);
427 
428  if (fd < 0) {
429  /* with dynamic locale negotiation we may see some failures before a success. */
430  if (!silent && templateCode < TCP_RESET) {
431  int xerrno = errno;
432  debugs(4, DBG_CRITICAL, "ERROR: loading file '" << path << "': " << xstrerr(xerrno));
433  }
434  wasLoaded = false;
435  return wasLoaded;
436  }
437 
438  template_.clear();
439  while ((len = FD_READ_METHOD(fd, buf, sizeof(buf))) > 0) {
440  template_.append(buf, len);
441  }
442 
443  if (len < 0) {
444  int xerrno = errno;
445  file_close(fd);
446  debugs(4, DBG_CRITICAL, MYNAME << "ERROR: failed to fully read: '" << path << "': " << xstrerr(xerrno));
447  wasLoaded = false;
448  return false;
449  }
450 
451  file_close(fd);
452 
453  filename = SBuf(path);
454 
455  if (!parse()) {
456  debugs(4, DBG_CRITICAL, "ERROR: parsing error in template file: " << path);
457  wasLoaded = false;
458  return false;
459  }
460 
461  wasLoaded = true;
462  return wasLoaded;
463 }
464 
465 bool strHdrAcptLangGetItem(const String &hdr, char *lang, int langLen, size_t &pos)
466 {
467  while (pos < hdr.size()) {
468 
469  /* skip any initial whitespace. */
470  while (pos < hdr.size() && xisspace(hdr[pos]))
471  ++pos;
472 
473  /*
474  * Header value format:
475  * - sequence of whitespace delimited tags
476  * - each tag may suffix with ';'.* which we can ignore.
477  * - IFF a tag contains only two characters we can wildcard ANY translations matching: <it> '-'? .*
478  * with preference given to an exact match.
479  */
480  bool invalid_byte = false;
481  char *dt = lang;
482  while (pos < hdr.size() && hdr[pos] != ';' && hdr[pos] != ',' && !xisspace(hdr[pos]) && dt < (lang + (langLen -1)) ) {
483  if (!invalid_byte) {
484 #if USE_HTTP_VIOLATIONS
485  // if accepting violations we may as well accept some broken browsers
486  // which may send us the right code, wrong ISO formatting.
487  if (hdr[pos] == '_')
488  *dt = '-';
489  else
490 #endif
491  *dt = xtolower(hdr[pos]);
492  // valid codes only contain A-Z, hyphen (-) and *
493  if (*dt != '-' && *dt != '*' && (*dt < 'a' || *dt > 'z') )
494  invalid_byte = true;
495  else
496  ++dt; // move to next destination byte.
497  }
498  ++pos;
499  }
500  *dt = '\0'; // nul-terminated the filename content string before system use.
501 
502  // if we terminated the tag on garbage or ';' we need to skip to the next ',' or end of header.
503  while (pos < hdr.size() && hdr[pos] != ',')
504  ++pos;
505 
506  if (pos < hdr.size() && hdr[pos] == ',')
507  ++pos;
508 
509  debugs(4, 9, "STATE: lang=" << lang << ", pos=" << pos << ", buf='" << ((pos < hdr.size()) ? hdr.substr(pos,hdr.size()) : "") << "'");
510 
511  /* if we found anything we might use, try it. */
512  if (*lang != '\0' && !invalid_byte)
513  return true;
514  }
515  return false;
516 }
517 
518 bool
520 {
521  String hdr;
522 
523 #if USE_ERR_LOCALES
524  if (loaded()) // already loaded?
525  return true;
526 
527  if (!request || !request->header.getList(Http::HdrType::ACCEPT_LANGUAGE, &hdr))
528  return false;
529 
530  char lang[256];
531  size_t pos = 0; // current parsing position in header string
532 
533  debugs(4, 6, HERE << "Testing Header: '" << hdr << "'");
534 
535  while ( strHdrAcptLangGetItem(hdr, lang, 256, pos) ) {
536 
537  /* wildcard uses the configured default language */
538  if (lang[0] == '*' && lang[1] == '\0') {
539  debugs(4, 6, HERE << "Found language '" << lang << "'. Using configured default.");
540  return false;
541  }
542 
543  debugs(4, 6, HERE << "Found language '" << lang << "', testing for available template");
544 
545  if (tryLoadTemplate(lang)) {
546  /* store the language we found for the Content-Language reply header */
547  errLanguage = lang;
548  break;
549  } else if (Config.errorLogMissingLanguages) {
550  debugs(4, DBG_IMPORTANT, "WARNING: Error Pages Missing Language: " << lang);
551  }
552  }
553 #endif
554 
555  return loaded();
556 }
557 
558 ErrorDynamicPageInfo::ErrorDynamicPageInfo(const int anId, const char *aName, const SBuf &aCfgLocation):
559  id(anId),
560  page_name(xstrdup(aName)),
561  uri(nullptr),
562  filename(nullptr),
563  cfgLocation(aCfgLocation),
564  page_redirect(static_cast<Http::StatusCode>(atoi(page_name)))
565 {
566  const char *filenameOrUri = nullptr;
567  if (xisdigit(*page_name)) {
568  if (const char *statusCodeEnd = strchr(page_name, ':'))
569  filenameOrUri = statusCodeEnd + 1;
570  } else {
572  filenameOrUri = page_name;
573  }
574 
575  // Guessed uri, filename, or both values may be nil or malformed.
576  // They are validated later.
577  if (!page_redirect) {
578  if (filenameOrUri && strchr(filenameOrUri, ':')) // looks like a URL
579  uri = filenameOrUri;
580  else
581  filename = filenameOrUri;
582  }
583  else if (page_redirect/100 == 3) {
584  // redirects imply a URL
585  uri = filenameOrUri;
586  } else {
587  // non-redirects imply an error page name
588  filename = filenameOrUri;
589  }
590 
591  const auto info = this; // source code change reduction hack
592  // TODO: Move and refactor to avoid self_destruct()s in reconfigure.
593 
594  /* WARNING on redirection status:
595  * 2xx are permitted, but not documented officially.
596  * - might be useful for serving static files (PAC etc) in special cases
597  * 3xx require a URL suitable for Location: header.
598  * - the current design does not allow for a Location: URI as well as a local file template
599  * although this possibility is explicitly permitted in the specs.
600  * 4xx-5xx require a local file template.
601  * - sending Location: on these codes with no body is invalid by the specs.
602  * - current result is Squid crashing or XSS problems as dynamic deny_info load random disk files.
603  * - a future redesign of the file loading may result in loading remote objects sent inline as local body.
604  */
605  if (info->page_redirect == Http::scNone)
606  ; // special case okay.
607  else if (info->page_redirect < 200 || info->page_redirect > 599) {
608  // out of range
609  debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " is not valid on '" << page_name << "'");
610  self_destruct();
611  } else if ( /* >= 200 && */ info->page_redirect < 300 && strchr(&(page_name[4]), ':')) {
612  // 2xx require a local template file
613  debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a template on '" << page_name << "'");
614  self_destruct();
615  } else if (info->page_redirect >= 300 && info->page_redirect <= 399 && !strchr(&(page_name[4]), ':')) {
616  // 3xx require an absolute URL
617  debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a URL on '" << page_name << "'");
618  self_destruct();
619  } else if (info->page_redirect >= 400 /* && <= 599 */ && strchr(&(page_name[4]), ':')) {
620  // 4xx/5xx require a local template file
621  debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a template on '" << page_name << "'");
622  self_destruct();
623  }
624  // else okay.
625 }
626 
628 static int
629 errorPageId(const char *page_name)
630 {
631  for (int i = 0; i < ERR_MAX; ++i) {
632  if (strcmp(err_type_str[i], page_name) == 0)
633  return i;
634  }
635 
636  for (size_t j = 0; j < ErrorDynamicPages.size(); ++j) {
637  if (strcmp(ErrorDynamicPages[j]->page_name, page_name) == 0)
638  return j + ERR_MAX;
639  }
640 
641  return ERR_NONE;
642 }
643 
644 err_type
645 errorReservePageId(const char *page_name, const SBuf &cfgLocation)
646 {
647  int id = errorPageId(page_name);
648 
649  if (id == ERR_NONE) {
650  id = ERR_MAX + ErrorDynamicPages.size();
651  const auto info = new ErrorDynamicPageInfo(id, page_name, cfgLocation);
652  ErrorDynamicPages.push_back(info);
653  }
654 
655  return (err_type)id;
656 }
657 
659 const char *
660 errorPageName(int pageId)
661 {
662  if (pageId >= ERR_NONE && pageId < ERR_MAX) /* common case */
663  return err_type_str[pageId];
664 
665  if (pageId >= ERR_MAX && pageId - ERR_MAX < (ssize_t)ErrorDynamicPages.size())
666  return ErrorDynamicPages[pageId - ERR_MAX]->page_name;
667 
668  return "ERR_UNKNOWN"; /* should not happen */
669 }
670 
671 ErrorState *
673 {
674  const Http::StatusCode status = (request && request->flags.needValidation) ?
676  return new ErrorState(type, status, request.getRaw(), ale);
677 }
678 
680  type(t),
681  page_id(t),
682  callback(nullptr)
683 {
684 }
685 
687  ErrorState(t)
688 {
689  if (page_id >= ERR_MAX && ErrorDynamicPages[page_id - ERR_MAX]->page_redirect != Http::scNone)
690  httpStatus = ErrorDynamicPages[page_id - ERR_MAX]->page_redirect;
691  else
692  httpStatus = status;
693 
694  if (req) {
695  request = req;
696  src_addr = req->client_addr;
697  }
698 
699  ale = anAle;
700 }
701 
704 {
705  Must(errorReply);
706  response_ = errorReply;
707  httpStatus = errorReply->sline.status();
708 
709  if (req) {
710  request = req;
711  src_addr = req->client_addr;
712  }
713 }
714 
715 void
717 {
718  assert(entry->mem_obj != NULL);
719  assert (entry->isEmpty());
720  debugs(4, 4, "Creating an error page for entry " << entry <<
721  " with errorstate " << err <<
722  " page id " << err->page_id);
723 
724  if (entry->store_status != STORE_PENDING) {
725  debugs(4, 2, "Skipping error page due to store_status: " << entry->store_status);
726  /*
727  * If the entry is not STORE_PENDING, then no clients
728  * care about it, and we don't need to generate an
729  * error message
730  */
732  assert(entry->mem_obj->nclients == 0);
733  delete err;
734  return;
735  }
736 
737  if (err->page_id == TCP_RESET) {
738  if (err->request) {
739  debugs(4, 2, "RSTing this reply");
740  err->request->flags.resetTcp = true;
741  }
742  }
743 
744  entry->storeErrorResponse(err->BuildHttpReply());
745  delete err;
746 }
747 
748 void
750 {
751  debugs(4, 3, conn << ", err=" << err);
753 
754  HttpReplyPointer rep(err->BuildHttpReply());
755 
756  MemBuf *mb = rep->pack();
757  AsyncCall::Pointer call = commCbCall(78, 5, "errorSendComplete",
759  Comm::Write(conn, mb, call);
760  delete mb;
761 }
762 
772 static void
773 errorSendComplete(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag errflag, int, void *data)
774 {
775  ErrorState *err = static_cast<ErrorState *>(data);
776  debugs(4, 3, HERE << conn << ", size=" << size);
777 
778  if (errflag != Comm::ERR_CLOSING) {
779  if (err->callback) {
780  debugs(4, 3, "errorSendComplete: callback");
781  err->callback(conn->fd, err->callback_data, size);
782  } else {
783  debugs(4, 3, "errorSendComplete: comm_close");
784  conn->close();
785  }
786  }
787 
788  delete err;
789 }
790 
792 {
794  safe_free(url);
796  wordlistDestroy(&ftp.server_msg);
797  safe_free(ftp.request);
798  safe_free(ftp.reply);
800 #if USE_ERR_LOCALES
802 #endif
804 }
805 
806 int
808 {
809  MemBuf str;
810  char ntoabuf[MAX_IPSTRLEN];
811 
812  str.reset();
813  /* email subject line */
814  str.appendf("CacheErrorInfo - %s", errorPageName(type));
815  mb->appendf("?subject=%s", rfc1738_escape_part(str.buf));
816  str.reset();
817  /* email body */
818  str.appendf("CacheHost: %s\r\n", getMyHostname());
819  /* - Err Msgs */
820  str.appendf("ErrPage: %s\r\n", errorPageName(type));
821 
822  if (xerrno) {
823  str.appendf("Err: (%d) %s\r\n", xerrno, strerror(xerrno));
824  } else {
825  str.append("Err: [none]\r\n", 13);
826  }
827 #if USE_AUTH
829  str.appendf("Auth ErrMsg: %s\r\n", auth_user_request->denyMessage());
830 #endif
831  if (dnsError.size() > 0)
832  str.appendf("DNS ErrMsg: %s\r\n", dnsError.termedBuf());
833 
834  /* - TimeStamp */
835  str.appendf("TimeStamp: %s\r\n\r\n", mkrfc1123(squid_curtime));
836 
837  /* - IP stuff */
838  str.appendf("ClientIP: %s\r\n", src_addr.toStr(ntoabuf,MAX_IPSTRLEN));
839 
840  if (request && request->hier.host[0] != '\0') {
841  str.appendf("ServerIP: %s\r\n", request->hier.host);
842  }
843 
844  str.append("\r\n", 2);
845  /* - HTTP stuff */
846  str.append("HTTP Request:\r\n", 15);
847  if (request) {
848  str.appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\n",
853  request->header.packInto(&str);
854  }
855 
856  str.append("\r\n", 2);
857  /* - FTP stuff */
858 
859  if (ftp.request) {
860  str.appendf("FTP Request: %s\r\n", ftp.request);
861  str.appendf("FTP Reply: %s\r\n", (ftp.reply? ftp.reply:"[none]"));
862  str.append("FTP Msg: ", 9);
863  wordlistCat(ftp.server_msg, &str);
864  str.append("\r\n", 2);
865  }
866 
867  str.append("\r\n", 2);
868  mb->appendf("&body=%s", rfc1738_escape_part(str.buf));
869  str.clean();
870  return 0;
871 }
872 
874 #define CVT_BUF_SZ 512
875 
876 void
878 {
880 
881  try {
882  const auto logformat = build.input + LogformatMagic.length();
883 
884  // Logformat supports undocumented "external" encoding specifications
885  // like [%>h] or "%<a". To preserve the possibility of extending
886  // @Squid{} syntax to non-logformat sequences, we require logformat
887  // sequences to start with '%'. This restriction does not limit
888  // logformat quoting abilities. TODO: Deprecate "external" encoding?
889  if (*logformat != '%')
890  throw TexcHere("logformat expressions that do not start with % are not supported");
891 
892  static MemBuf result;
893  result.reset();
894  const auto logformatLen = Format::AssembleOne(logformat, result, ale);
895  assert(logformatLen > 0);
896  const auto closure = logformat + logformatLen;
897  if (*closure != '}')
898  throw TexcHere("Missing closing brace (})");
899  build.output.append(result.content(), result.contentSize());
900  build.input = closure + 1;
901  return;
902  } catch (...) {
903  noteBuildError("Bad @Squid{logformat} sequence", build.input);
904  }
905 
906  // we cannot recover reliably so stop interpreting the rest of input
907  const auto remainingSize = strlen(build.input);
908  build.output.append(build.input, remainingSize);
909  build.input += remainingSize;
910 }
911 
912 void
914 {
915  static MemBuf mb;
916  const char *p = NULL; /* takes priority over mb if set */
917  int do_quote = 1;
918  int no_urlescape = 0; /* if true then item is NOT to be further URL-encoded */
919  char ntoabuf[MAX_IPSTRLEN];
920 
921  mb.reset();
922 
923  const auto &building_deny_info_url = build.building_deny_info_url; // a change reduction hack
924 
925  const auto letter = build.input[1];
926 
927  switch (letter) {
928 
929  case 'a':
930 #if USE_AUTH
933  if (!p)
934 #endif
935  p = "-";
936  break;
937 
938  case 'A':
939  // TODO: When/if we get ALE here, pass it as well
940  if (const auto addr = FindListeningPortAddress(request.getRaw(), nullptr))
941  mb.appendf("%s", addr->toStr(ntoabuf, MAX_IPSTRLEN));
942  else
943  p = "-";
944  break;
945 
946  case 'b':
947  mb.appendf("%u", getMyPort());
948  break;
949 
950  case 'B':
951  if (building_deny_info_url) break;
952  if (request) {
953  const SBuf &tmp = Ftp::UrlWith2f(request.getRaw());
954  mb.append(tmp.rawContent(), tmp.length());
955  } else
956  p = "[no URL]";
957  break;
958 
959  case 'c':
960  if (building_deny_info_url) break;
961  p = errorPageName(type);
962  break;
963 
964  case 'D':
965  if (!build.allowRecursion)
966  p = "%D"; // if recursion is not allowed, do not convert
967  else if (detail) {
968  auto rawDetail = detail->verbose(request);
969  // XXX: Performance regression. c_str() reallocates
970  const auto compiledDetail = compileBody(rawDetail.c_str(), false);
971  mb.append(compiledDetail.rawContent(), compiledDetail.length());
972  do_quote = 0;
973  }
974  if (!mb.contentSize())
975  mb.append("[No Error Detail]", 17);
976  break;
977 
978  case 'e':
979  mb.appendf("%d", xerrno);
980  break;
981 
982  case 'E':
983  if (xerrno)
984  mb.appendf("(%d) %s", xerrno, strerror(xerrno));
985  else
986  mb.append("[No Error]", 10);
987  break;
988 
989  case 'f':
990  if (building_deny_info_url) break;
991  /* FTP REQUEST LINE */
992  if (ftp.request)
993  p = ftp.request;
994  else
995  p = "nothing";
996  break;
997 
998  case 'F':
999  if (building_deny_info_url) break;
1000  /* FTP REPLY LINE */
1001  if (ftp.reply)
1002  p = ftp.reply;
1003  else
1004  p = "nothing";
1005  break;
1006 
1007  case 'g':
1008  if (building_deny_info_url) break;
1009  /* FTP SERVER RESPONSE */
1010  if (ftp.listing) {
1011  mb.append(ftp.listing->content(), ftp.listing->contentSize());
1012  do_quote = 0;
1013  } else if (ftp.server_msg) {
1014  wordlistCat(ftp.server_msg, &mb);
1015  }
1016  break;
1017 
1018  case 'h':
1019  mb.appendf("%s", getMyHostname());
1020  break;
1021 
1022  case 'H':
1023  if (request) {
1024  if (request->hier.host[0] != '\0') // if non-empty string.
1025  p = request->hier.host;
1026  else
1027  p = request->url.host();
1028  } else if (!building_deny_info_url)
1029  p = "[unknown host]";
1030  break;
1031 
1032  case 'i':
1033  mb.appendf("%s", src_addr.toStr(ntoabuf,MAX_IPSTRLEN));
1034  break;
1035 
1036  case 'I':
1037  if (request && request->hier.tcpServer)
1038  p = request->hier.tcpServer->remote.toStr(ntoabuf,MAX_IPSTRLEN);
1039  else if (!building_deny_info_url)
1040  p = "[unknown]";
1041  break;
1042 
1043  case 'l':
1044  if (building_deny_info_url) break;
1046  do_quote = 0;
1047  break;
1048 
1049  case 'L':
1050  if (building_deny_info_url) break;
1051  if (Config.errHtmlText) {
1052  mb.appendf("%s", Config.errHtmlText);
1053  do_quote = 0;
1054  } else
1055  p = "[not available]";
1056  break;
1057 
1058  case 'm':
1059  if (building_deny_info_url) break;
1060 #if USE_AUTH
1061  if (auth_user_request.getRaw())
1062  p = auth_user_request->denyMessage("[not available]");
1063  else
1064  p = "[not available]";
1065 #else
1066  p = "-";
1067 #endif
1068  break;
1069 
1070  case 'M':
1071  if (request) {
1072  const SBuf &m = request->method.image();
1073  mb.append(m.rawContent(), m.length());
1074  } else if (!building_deny_info_url)
1075  p = "[unknown method]";
1076  break;
1077 
1078  case 'O':
1079  if (!building_deny_info_url)
1080  do_quote = 0;
1081  case 'o':
1083  if (!p && !building_deny_info_url)
1084  p = "[not available]";
1085  break;
1086 
1087  case 'p':
1088  if (request) {
1089  mb.appendf("%u", request->url.port());
1090  } else if (!building_deny_info_url) {
1091  p = "[unknown port]";
1092  }
1093  break;
1094 
1095  case 'P':
1096  if (request) {
1097  const SBuf &m = request->url.getScheme().image();
1098  mb.append(m.rawContent(), m.length());
1099  } else if (!building_deny_info_url) {
1100  p = "[unknown protocol]";
1101  }
1102  break;
1103 
1104  case 'R':
1105  if (building_deny_info_url) {
1106  if (request != NULL) {
1107  const SBuf &tmp = request->url.path();
1108  mb.append(tmp.rawContent(), tmp.length());
1109  no_urlescape = 1;
1110  } else
1111  p = "[no request]";
1112  break;
1113  }
1114  if (request) {
1115  mb.appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\n",
1120  request->header.packInto(&mb, true); //hide authorization data
1121  } else if (request_hdrs) {
1122  p = request_hdrs;
1123  } else {
1124  p = "[no request]";
1125  }
1126  break;
1127 
1128  case 's':
1129  /* for backward compat we make %s show the full URL. Drop this in some future release. */
1130  if (building_deny_info_url) {
1131  if (request) {
1132  const SBuf &tmp = request->effectiveRequestUri();
1133  mb.append(tmp.rawContent(), tmp.length());
1134  } else
1135  p = url;
1136  debugs(0, DBG_CRITICAL, "WARNING: deny_info now accepts coded tags. Use %u to get the full URL instead of %s");
1137  } else
1139  break;
1140 
1141  case 'S':
1142  if (building_deny_info_url) {
1144  break;
1145  }
1146  /* signature may contain %-escapes, recursion */
1147  if (page_id != ERR_SQUID_SIGNATURE) {
1148  const int saved_id = page_id;
1150  const auto signature = buildBody();
1151  mb.append(signature.rawContent(), signature.length());
1152  page_id = saved_id;
1153  do_quote = 0;
1154  } else {
1155  /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */
1156  p = "[%S]";
1157  }
1158  break;
1159 
1160  case 't':
1162  break;
1163 
1164  case 'T':
1165  mb.appendf("%s", mkrfc1123(squid_curtime));
1166  break;
1167 
1168  case 'U':
1169  /* Using the fake-https version of absolute-URI so error pages see https:// */
1170  /* even when the url-path cannot be shown as more than '*' */
1171  if (request)
1173  else if (url)
1174  p = url;
1175  else if (!building_deny_info_url)
1176  p = "[no URL]";
1177  break;
1178 
1179  case 'u':
1180  if (request) {
1181  const SBuf &tmp = request->effectiveRequestUri();
1182  mb.append(tmp.rawContent(), tmp.length());
1183  } else if (url)
1184  p = url;
1185  else if (!building_deny_info_url)
1186  p = "[no URL]";
1187  break;
1188 
1189  case 'w':
1190  if (Config.adminEmail)
1191  mb.appendf("%s", Config.adminEmail);
1192  else if (!building_deny_info_url)
1193  p = "[unknown]";
1194  break;
1195 
1196  case 'W':
1197  if (building_deny_info_url) break;
1199  Dump(&mb);
1200  no_urlescape = 1;
1201  break;
1202 
1203  case 'x':
1204  if (detail) {
1205  const auto brief = detail->brief();
1206  mb.append(brief.rawContent(), brief.length());
1207  } else if (!building_deny_info_url) {
1208  p = "[Unknown Error Code]";
1209  }
1210  break;
1211 
1212  case 'z':
1213  if (building_deny_info_url) break;
1214  if (dnsError.size() > 0)
1215  p = dnsError.termedBuf();
1216  else if (ftp.cwd_msg)
1217  p = ftp.cwd_msg;
1218  else
1219  p = "[unknown]";
1220  break;
1221 
1222  case 'Z':
1223  if (building_deny_info_url) break;
1224  if (err_msg)
1225  p = err_msg;
1226  else
1227  p = "[unknown]";
1228  break;
1229 
1230  case '%':
1231  p = "%";
1232  break;
1233 
1234  default:
1235  if (building_deny_info_url)
1236  bypassBuildErrorXXX("Unsupported deny_info %code", build.input);
1237  else if (letter != ';')
1238  bypassBuildErrorXXX("Unsupported error page %code", build.input);
1239  // else too many "font-size: 100%;" template errors to report
1240 
1241  mb.append(build.input, 2);
1242  do_quote = 0;
1243  break;
1244  }
1245 
1246  if (!p)
1247  p = mb.buf; /* do not use mb after this assignment! */
1248 
1249  assert(p);
1250 
1251  debugs(4, 3, "%" << letter << " --> '" << p << "'" );
1252 
1253  if (do_quote)
1254  p = html_quote(p);
1255 
1256  if (building_deny_info_url && !no_urlescape)
1257  p = rfc1738_escape_part(p);
1258 
1259  // TODO: Optimize by replacing mb with direct build.output usage.
1260  build.output.append(p, strlen(p));
1261  build.input += 2;
1262 }
1263 
1264 void
1266 {
1267  if (const auto urlTemplate = ErrorPage::IsDenyInfoUri(page_id)) {
1268  (void)compile(urlTemplate, true, true);
1269  } else {
1270  assert(page_id > ERR_NONE);
1272  (void)compileBody(error_text[page_id], true);
1273  }
1274 }
1275 
1276 HttpReply *
1278 {
1279  if (response_)
1280  return response_.getRaw();
1281 
1282  HttpReply *rep = new HttpReply;
1283  const char *name = errorPageName(page_id);
1284  /* no LMT for error pages; error pages expire immediately */
1285 
1286  if (const auto urlTemplate = ErrorPage::IsDenyInfoUri(page_id)) {
1287  /* Redirection */
1289  // Use configured 3xx reply status if set.
1290  if (name[0] == '3')
1291  status = httpStatus;
1292  else {
1293  // Use 307 for HTTP/1.1 non-GET/HEAD requests.
1295  status = Http::scTemporaryRedirect;
1296  }
1297 
1298  rep->setHeaders(status, NULL, "text/html;charset=utf-8", 0, 0, -1);
1299 
1300  if (request) {
1301  auto location = compile(urlTemplate, true, true);
1302  rep->header.putStr(Http::HdrType::LOCATION, location.c_str());
1303  }
1304 
1305  httpHeaderPutStrf(&rep->header, Http::HdrType::X_SQUID_ERROR, "%d %s", httpStatus, "Access Denied");
1306  } else {
1307  const auto body = buildBody();
1308  rep->setHeaders(httpStatus, NULL, "text/html;charset=utf-8", body.length(), 0, -1);
1309  /*
1310  * include some information for downstream caches. Implicit
1311  * replaceable content. This isn't quite sufficient. xerrno is not
1312  * necessarily meaningful to another system, so we really should
1313  * expand it. Additionally, we should identify ourselves. Someone
1314  * might want to know. Someone _will_ want to know OTOH, the first
1315  * X-CACHE-MISS entry should tell us who.
1316  */
1318 
1319 #if USE_ERR_LOCALES
1320  /*
1321  * If error page auto-negotiate is enabled in any way, send the Vary.
1322  * RFC 2616 section 13.6 and 14.44 says MAY and SHOULD do this.
1323  * We have even better reasons though:
1324  * see http://wiki.squid-cache.org/KnowledgeBase/VaryNotCaching
1325  */
1326  if (!Config.errorDirectory) {
1327  /* We 'negotiated' this ONLY from the Accept-Language. */
1329  rep->header.putStr(Http::HdrType::VARY, "Accept-Language");
1330  }
1331 
1332  /* add the Content-Language header according to RFC section 14.12 */
1333  if (err_language) {
1335  } else
1336 #endif /* USE_ERROR_LOCALES */
1337  {
1338  /* default templates are in English */
1339  /* language is known unless error_directory override used */
1340  if (!Config.errorDirectory)
1342  }
1343 
1344  rep->body.set(body);
1345  }
1346 
1347  // Make sure error codes get back to the client side for logging and
1348  // error tracking.
1349  if (request) {
1350  if (detail)
1352  else
1354  }
1355 
1356  return rep;
1357 }
1358 
1359 SBuf
1361 {
1363 
1364 #if USE_ERR_LOCALES
1365 
1369  if (!Config.errorDirectory && page_id < ERR_MAX) {
1372 
1373  ErrorPageFile localeTmpl(err_type_str[page_id], static_cast<err_type>(page_id));
1374  if (localeTmpl.loadFor(request.getRaw())) {
1375  inputLocation = localeTmpl.filename;
1376  assert(localeTmpl.language());
1377  err_language = xstrdup(localeTmpl.language());
1378  return compileBody(localeTmpl.text(), true);
1379  }
1380  }
1381 #endif /* USE_ERR_LOCALES */
1382 
1387 #if USE_ERR_LOCALES
1388  if (!Config.errorDirectory)
1390 #endif
1391  debugs(4, 2, "No existing error page language negotiated for " << errorPageName(page_id) << ". Using default error file.");
1392  return compileBody(error_text[page_id], true);
1393 }
1394 
1395 SBuf
1396 ErrorState::compileBody(const char *input, bool allowRecursion)
1397 {
1398  return compile(input, false, allowRecursion);
1399 }
1400 
1401 SBuf
1402 ErrorState::compile(const char *input, bool building_deny_info_url, bool allowRecursion)
1403 {
1404  assert(input);
1405 
1406  Build build;
1407  build.building_deny_info_url = building_deny_info_url;
1408  build.allowRecursion = allowRecursion;
1409  build.input = input;
1410 
1411  auto blockStart = build.input;
1412  while (const auto letter = *build.input) {
1413  if (letter == '%') {
1414  build.output.append(blockStart, build.input - blockStart);
1415  compileLegacyCode(build);
1416  blockStart = build.input;
1417  }
1418  else if (letter == '@' && LogformatMagic.cmp(build.input, LogformatMagic.length()) == 0) {
1419  build.output.append(blockStart, build.input - blockStart);
1420  compileLogformatCode(build);
1421  blockStart = build.input;
1422  } else {
1423  ++build.input;
1424  }
1425  }
1426  build.output.append(blockStart, build.input - blockStart);
1427  return build.output;
1428 }
1429 
1437 void
1438 ErrorState::noteBuildError_(const char *msg, const char *near, const bool forceBypass)
1439 {
1441  const auto runtime = !starting_up;
1442  if (runtime || forceBypass) {
1443  // swallow this problem because the admin may not be (and/or the page
1444  // building code is not) ready to handle throwing consequences
1445 
1446  static unsigned int seenErrors = 0;
1447  ++seenErrors;
1448 
1449  const auto debugLevel =
1450  (seenErrors > 100) ? DBG_DATA:
1452  3; // most other errors have been reported as configuration errors
1453 
1454  // Error fatality depends on the error context: Reconfiguration errors
1455  // are, like startup ones, DBG_CRITICAL but will never become FATAL.
1456  if (starting_up && seenErrors <= 10)
1457  debugs(4, debugLevel, "WARNING: The following configuration error will be fatal in future Squid versions");
1458 
1459  debugs(4, debugLevel, "ERROR: " << BuildErrorPrinter(inputLocation, page_id, msg, near));
1460  } else {
1461  throw TexcHere(ToSBuf(BuildErrorPrinter(inputLocation, page_id, msg, near)));
1462  }
1463 }
1464 
1465 /* ErrorPage::BuildErrorPrinter */
1466 
1467 std::ostream &
1469  if (!inputLocation.isEmpty())
1470  return os << inputLocation;
1471 
1472  if (page_id < ERR_NONE || page_id >= error_page_count)
1473  return os << "[error page " << page_id << "]"; // should not happen
1474 
1475  if (page_id < ERR_MAX)
1476  return os << err_type_str[page_id];
1477 
1478  return os << "deny_info " << ErrorDynamicPages.at(page_id - ERR_MAX)->page_name;
1479 }
1480 
1481 std::ostream &
1482 ErrorPage::BuildErrorPrinter::print(std::ostream &os) const {
1483  printLocation(os) << ": " << msg << " near ";
1484 
1485  // TODO: Add support for prefix printing to Raw
1486  const size_t maxContextLength = 15; // plus "..."
1487  if (strlen(near) > maxContextLength) {
1488  os.write(near, maxContextLength);
1489  os << "...";
1490  } else {
1491  os << near;
1492  }
1493 
1494  // XXX: We should not be converting (inner) exception to text if we are
1495  // going to throw again. See "add arbitrary (re)thrower-supplied details"
1496  // TODO in TextException.h for a long-term in-catcher solution.
1497  if (std::current_exception())
1498  os << "\n additional info: " << CurrentException;
1499 
1500  return os;
1501 }
1502 
1504 static void
1505 ErrorPage::ImportStaticErrorText(const int page_id, const char *text, const SBuf &inputLocation)
1506 {
1507  assert(!error_text[page_id]);
1508  error_text[page_id] = xstrdup(text);
1509  ValidateStaticError(page_id, inputLocation);
1510 }
1511 
1513 static void
1514 ErrorPage::ValidateStaticError(const int page_id, const SBuf &inputLocation)
1515 {
1516  // Supplying nil ALE pointer limits validation to logformat %code
1517  // recognition by Format::Token::parse(). This is probably desirable
1518  // because actual %code assembly is slow and should not affect validation
1519  // when our ALE cannot have any real data (this code is not associated
1520  // with any real transaction).
1521  ErrorState anErr(err_type(page_id), Http::scNone, nullptr, nullptr);
1522  anErr.inputLocation = inputLocation;
1523  anErr.validate();
1524 }
1525 
char * errorDirectory
Definition: SquidConfig.h:433
const char * xstrerr(int error)
Definition: xstrerror.cc:83
an error page created from admin-configurable metadata (e.g. deny_info)
Definition: errorpage.cc:69
static IOCB errorSendComplete
Definition: errorpage.cc:208
std::ostream & printLocation(std::ostream &os) const
print() helper to report where the error was found
Definition: errorpage.cc:1468
char * buf
Definition: MemBuf.h:134
#define MYNAME
Definition: Debug.h:170
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:72
unsigned int major
major version number
void wordlistDestroy(wordlist **list)
destroy a wordlist
Definition: wordlist.cc:16
int getMyPort(void)
Definition: tools.cc:1003
AnyP::ProtocolVersion http_ver
Definition: Message.h:73
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
@ METHOD_HEAD
Definition: MethodType.h:28
void port(unsigned short p)
Definition: Uri.h:94
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
static char ** error_text
Definition: errorpage.cc:199
static std::ostream & operator<<(std::ostream &os, const BuildErrorPrinter &context)
Definition: errorpage.cc:132
err_type errorReservePageId(const char *page_name, const SBuf &cfgLocation)
allocates a new slot for the error page
Definition: errorpage.cc:645
char const * denyMessage(char const *const default_message=NULL) const
Definition: UserRequest.cc:127
ERCB * callback
Definition: errorpage.h:184
@ scNone
Definition: StatusCode.h:21
bool needValidation
Definition: RequestFlags.h:46
String templateName
The name of the template.
Definition: errorpage.h:335
HttpHeader header
Definition: Message.h:75
static std::vector< ErrorDynamicPageInfo * > ErrorDynamicPages
Definition: errorpage.cc:191
bool loadFromFile(const char *path)
Definition: errorpage.cc:417
void errorAppendEntry(StoreEntry *entry, ErrorState *err)
Definition: errorpage.cc:716
bool isEmpty() const
Definition: SBuf.h:420
err_type & operator++(err_type &anErr)
Definition: errorpage.cc:228
MemObject * mem_obj
Definition: Store.h:213
RequestFlags flags
Definition: HttpRequest.h:141
@ TCP_RESET
Definition: forward.h:77
bool loaded() const
return true if the data loaded from disk without any problem
Definition: errorpage.h:286
int Dump(MemBuf *mb)
Definition: errorpage.cc:807
static const int error_hard_text_count
Definition: errorpage.cc:196
bool wasLoaded
True if the template data read from disk without any problem.
Definition: errorpage.h:333
const char * external_acl_message
Http::StatusCode page_redirect
admin-configured HTTP status code
Definition: errorpage.cc:93
HttpReplyPointer response_
Definition: errorpage.h:206
#define O_TEXT
Definition: defines.h:201
HttpReply * BuildHttpReply(void)
Definition: errorpage.cc:1277
unsigned int minor
minor version number
const char * ProtocolType_str[]
Definition: SBuf.h:87
int const char int
Definition: stub_libmem.cc:75
AccessLogEntryPointer ale
transaction details (or nil)
Definition: errorpage.h:198
#define xstrdup
String errLanguage
The error language of the template.
Definition: errorpage.h:334
int page_id
Definition: errorpage.h:169
const char * errorPageName(int pageId)
error ID to string
Definition: errorpage.cc:660
char * html_quote(const char *)
Definition: html_quote.c:53
const char * filename
admin-configured name for the error page template (custom or standard)
Definition: errorpage.cc:86
@ CONTENT_LANGUAGE
const char * err_type_str[]
Definition: stub_liberror.cc:8
Auth::UserRequest::Pointer auth_user_request
Definition: HttpRequest.h:127
int type
Definition: errorpage.cc:153
C * getRaw() const
Definition: RefCount.h:80
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:216
uint16_t flags
Definition: Store.h:224
const SBuf & image() const
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:26
char * errorDefaultLanguage
Definition: SquidConfig.h:435
Http::StatusLine sline
Definition: HttpReply.h:60
char * errHtmlText
Definition: SquidConfig.h:227
@ ERR_NONE
Definition: forward.h:15
#define DBG_CRITICAL
Definition: Debug.h:45
StatusCode
Definition: StatusCode.h:20
err_type
Definition: forward.h:14
Definition: forward.h:22
err_type templateCode
The internal code for this template.
Definition: errorpage.h:336
void file_close(int fd)
Definition: fs_io.cc:76
#define xtolower(x)
Definition: xis.h:19
void detailError(const err_type c, const ErrorDetail::Pointer &d)
sets error detail if no earlier detail was available
Definition: HttpRequest.h:101
@ ERR_CLOSING
Definition: Flag.h:25
int errorLogMissingLanguages
Definition: SquidConfig.h:436
#define DBG_IMPORTANT
Definition: Debug.h:46
void httpHeaderPutStrf(HttpHeader *hdr, Http::HdrType id, const char *fmt,...)
ProtocolType protocol
which protocol this version is for
void bypassBuildErrorXXX(const char *msg, const char *near)
Definition: errorpage.h:156
void self_destruct(void)
Definition: cache_cf.cc:266
const char * text()
The template text data read from disk.
Definition: errorpage.cc:218
const char * input
template bytes that need to be compiled
Definition: errorpage.cc:107
static void ValidateStaticError(const int page_id, const SBuf &inputLocation)
validate static error page
Definition: errorpage.cc:1514
size_t AssembleOne(const char *start, MemBuf &buf, const AccessLogEntryPointer &ale)
Definition: Format.cc:100
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:802
AnyP::UriScheme const & getScheme() const
Definition: Uri.h:67
#define TexcHere(msg)
legacy convenience macro; it is not difficult to type Here() now
Definition: TextException.h:58
SBuf output
compilation result
Definition: errorpage.cc:106
char * strerror(int ern)
Definition: strerror.c:22
bool building_deny_info_url
whether we compile deny_info URI
Definition: errorpage.cc:108
void errorDetailInitialize()
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:23
ErrorPageFile(const char *name, const err_type code)
Definition: errorpage.cc:215
void compileLogformatCode(Build &build)
compile @Squid{code} sequence containing a single logformat code
Definition: errorpage.cc:877
@ scGatewayTimeout
Definition: StatusCode.h:75
TemplateFile(const char *name, const err_type code)
Definition: errorpage.cc:350
void noteBuildError_(const char *msg, const char *near, const bool forceBypass)
Definition: errorpage.cc:1438
HttpBody body
Definition: HttpReply.h:62
bool silent
Whether to print error messages on cache.log file or not. It is user defined.
Definition: errorpage.h:316
struct SquidConfig::@113 onoff
@ ERR_REQUEST_START_TIMEOUT
Definition: forward.h:81
void set(const SBuf &newContent)
Definition: HttpBody.h:26
virtual void setDefault()
recover from loadDefault() failure to load or parse() a template
Definition: errorpage.h:323
void loadDefault()
Definition: errorpage.cc:356
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
@ scTemporaryRedirect
Definition: StatusCode.h:42
bool allowRecursion
whether top-level compile() calls are OK
Definition: errorpage.cc:109
int size
Definition: ModDevPoll.cc:77
void errorSend(const Comm::ConnectionPointer &conn, ErrorState *err)
Definition: errorpage.cc:749
#define NULL
Definition: types.h:166
const char * rawContent() const
Definition: SBuf.cc:516
ErrorState()=delete
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
struct ErrorState::@60 ftp
BuildErrorPrinter(const SBuf &anInputLocation, int aPage, const char *aMsg, const char *aNear)
Definition: errorpage.cc:116
Http::StatusCode status() const
retrieve the status code for this status line
Definition: StatusLine.h:45
void errorDetailClean()
char const * visible_appname_string
static ErrorDetail::Pointer NewIfAny(const int errorNo)
@ ERR_SECURE_ACCEPT_FAIL
Definition: forward.h:80
@ ACCEPT_LANGUAGE
err_type type
Definition: errorpage.h:168
const char * text
Definition: errorpage.cc:154
HttpRequestPointer request
Definition: errorpage.h:175
bool strHdrAcptLangGetItem(const String &hdr, char *lang, int langLen, size_t &pos)
Definition: errorpage.cc:465
char * request_hdrs
Definition: errorpage.h:195
Definition: MemBuf.h:24
#define EBIT_TEST(flag, bit)
Definition: defines.h:107
void clean()
Definition: MemBuf.cc:113
unsigned char code
Definition: html_quote.c:20
bool tryLoadTemplate(const char *lang)
Definition: errorpage.cc:392
SBuf cfgLocation
deny_info directive position in squid.conf (for reporting)
Definition: errorpage.cc:89
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:157
int starting_up
int delById(Http::HdrType id)
Definition: HttpHeader.cc:711
pretty-prints error page/deny_info building error
Definition: errorpage.cc:114
virtual bool parse()
post-process the loaded template
Definition: errorpage.h:320
void const char HLPCB void * data
Definition: stub_helper.cc:16
void IOCB(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int xerrno, void *data)
Definition: CommCalls.h:36
char * adminEmail
Definition: SquidConfig.h:189
void validate()
ensures that a future BuildHttpReply() is likely to succeed
Definition: errorpage.cc:1265
int reconfiguring
int emailErrData
Definition: SquidConfig.h:326
void wordlistCat(const wordlist *w, MemBuf *mb)
Definition: wordlist.cc:35
static const struct @59 error_hard_text[]
Ip::Address src_addr
Definition: errorpage.h:182
#define safe_free(x)
Definition: xalloc.h:73
Ip::Address remote
Definition: Connection.h:159
store_status_t store_status
Definition: Store.h:236
state and parameters shared by several ErrorState::compile*() methods
Definition: errorpage.cc:104
char * err_msg
Definition: errorpage.h:196
CommCbFunPtrCallT< Dialer > * commCbCall(int debugSection, int debugLevel, const char *callName, const Dialer &dialer)
Definition: CommCalls.h:342
virtual SBuf brief() const =0
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:19
char const * termedBuf() const
Definition: SquidString.h:91
SBuf compileBody(const char *text, bool allowRecursion)
Definition: errorpage.cc:1396
SBuf image() const
Definition: UriScheme.h:50
HierarchyLogEntry hier
Definition: HttpRequest.h:157
std::ostream & CurrentException(std::ostream &os)
prints active (i.e., thrown but not yet handled) exception
#define xisdigit(x)
Definition: xis.h:20
@ scServiceUnavailable
Definition: StatusCode.h:74
void packInto(Packable *p, bool mask_sensitive_info=false) const
Definition: HttpHeader.cc:584
char * err_language
Definition: errorpage.h:170
ErrorDetail::Pointer detail
Definition: errorpage.h:204
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:35
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:318
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:404
String dnsError
DNS lookup error message.
Definition: errorpage.h:179
time_t squid_curtime
Definition: stub_time.cc:17
@ MGR_INDEX
Definition: forward.h:86
SBuf & append(const SBuf &S)
Definition: SBuf.cc:192
@ ERR_MAX
Definition: forward.h:88
#define xfree
ErrorDynamicPageInfo(const int anId, const char *aName, const SBuf &aCfgLocation)
Definition: errorpage.cc:558
int xerrno
Definition: errorpage.h:177
const char * mkrfc1123(time_t)
Definition: rfc1123.c:202
const char * uri
admin-configured HTTP Location header value for redirection responses
Definition: errorpage.cc:83
Flag
Definition: Flag.h:15
int FD_READ_METHOD(int fd, char *buf, int len)
Definition: fde.h:194
const Ip::Address * FindListeningPortAddress(const HttpRequest *callerRequest, const AccessLogEntry *ale)
Definition: HttpRequest.cc:786
virtual void setDefault() override
recover from loadDefault() failure to load or parse() a template
Definition: errorpage.cc:221
const char * urlCanonicalFakeHttps(const HttpRequest *request)
Definition: Uri.cc:657
void compileLegacyCode(Build &build)
compile a single-letter code like D
Definition: errorpage.cc:913
const char * language()
The language used for the template.
Definition: errorpage.h:312
Comm::ConnectionPointer tcpServer
TCP/IP level details of the last peer/server connection.
char * redirect_url
Definition: errorpage.h:183
@ scFound
Definition: StatusCode.h:38
SBuf inputLocation
the source of the error template (for reporting purposes)
Definition: errorpage.h:115
HttpRequestMethod method
Definition: HttpRequest.h:114
void path(const char *p)
Definition: Uri.h:99
@ ENTRY_ABORTED
Definition: enums.h:115
ErrorDynamicPageInfo(ErrorDynamicPageInfo &&)=delete
SBuf filename
where the template was loaded from
Definition: errorpage.h:314
char * url
Definition: errorpage.h:176
const SBuf & UrlWith2f(HttpRequest *)
Definition: FtpGateway.cc:2636
int cmp(const SBuf &S, const size_type n) const
shorthand version for compare()
Definition: SBuf.h:264
@ ERR_SQUID_SIGNATURE
Definition: forward.h:71
static ErrorState * NewForwarding(err_type, HttpRequestPointer &, const AccessLogEntryPointer &)
Creates a general request forwarding error with the right http_status.
Definition: errorpage.cc:672
char * content()
start of the added data
Definition: MemBuf.h:41
void putStr(Http::HdrType id, const char *str)
Definition: HttpHeader.cc:1068
#define DEFAULT_SQUID_ERROR_DIR
Definition: errorpage.cc:58
@ ERR_CLIENT_GONE
Definition: forward.h:79
size_type size() const
Definition: SquidString.h:72
const char * getMyHostname(void)
Definition: tools.cc:412
bool loadFor(const HttpRequest *request)
Definition: errorpage.cc:519
bool isEmpty() const
Definition: Store.h:67
char const * username() const
Definition: UserRequest.cc:32
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:124
static MemBuf error_stylesheet
Definition: errorpage.cc:205
char * errorStylesheet
Definition: SquidConfig.h:438
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:72
void const char HLPCB * callback
Definition: stub_helper.cc:16
SBuf buildBody()
locates the right error page template for this error and compiles it
Definition: errorpage.cc:1360
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
void reset()
Definition: MemBuf.cc:132
String extacl_message
Definition: HttpRequest.h:184
int operator-(err_type const &anErr, err_type const &anErr2)
Definition: errorpage.cc:236
char host[SQUIDHOSTNAMELEN]
const char * FormatHttpd(time_t t)
Definition: time.cc:111
String substr(size_type from, size_type to) const
Definition: String.cc:223
int id
error_text[] index for response body (unused in redirection responses)
Definition: errorpage.cc:75
static void ImportStaticErrorText(const int page_id, const char *text, const SBuf &inputLocation)
add error page template to the global index
Definition: errorpage.cc:1505
std::ostream & print(std::ostream &) const
reports error details (for admin-visible exceptions and debugging)
Definition: errorpage.cc:1482
@ ERR_RELAY_REMOTE
Definition: forward.h:83
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:168
#define xisspace(x)
Definition: xis.h:17
#define MAXPATHLEN
Definition: stdio.h:62
static const char * IsDenyInfoUri(const int page_id)
Definition: errorpage.cc:244
SBuf template_
raw template contents
Definition: errorpage.h:332
int file_open(const char *path, int mode)
Definition: fs_io.cc:46
static const char * errorFindHardText(err_type type)
Definition: errorpage.cc:339
virtual SBuf verbose(const HttpRequestPointer &) const =0
#define rfc1738_escape_part(x)
Definition: rfc1738.h:55
@ METHOD_GET
Definition: MethodType.h:25
void * callback_data
Definition: errorpage.h:185
#define false
Definition: GnuRegex.c:233
const SBuf & effectiveRequestUri() const
RFC 7230 section 5.5 - Effective Request URI.
Definition: HttpRequest.cc:688
void errorClean(void)
Definition: errorpage.cc:314
SBuf compile(const char *input, bool building_deny_info_url, bool allowRecursion)
Definition: errorpage.cc:1402
Ip::Address client_addr
Definition: HttpRequest.h:149
void host(const char *src)
Definition: Uri.cc:98
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
static const SBuf LogformatMagic
marks each embedded logformat entry
Definition: errorpage.h:211
int nclients
Definition: MemObject.h:146
void const char * buf
Definition: stub_helper.cc:16
static int errorPageId(const char *page_name)
Definition: errorpage.cc:629
Http::StatusCode httpStatus
Definition: errorpage.h:171
void storeErrorResponse(HttpReply *reply)
Store a prepared error response. MemObject locks the reply object.
Definition: store.cc:1744
Auth::UserRequest::Pointer auth_user_request
Definition: errorpage.h:173
void errorInitialize(void)
Definition: errorpage.cc:252
#define SQUIDSBUFPH
Definition: SBuf.h:31
class SquidConfig Config
Definition: SquidConfig.cc:12
void noteBuildError(const char *msg, const char *near)
Definition: errorpage.h:147
static int error_page_count
Definition: errorpage.cc:202
AnyP::ProtocolVersion ProtocolVersion(unsigned int aMajor, unsigned int aMinor)
HTTP version label information.
#define DBG_DATA
Definition: Debug.h:48
@ STORE_PENDING
Definition: enums.h:51

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors