]> git.ipfire.org Git - thirdparty/squid.git/blob - src/errorpage.cc
88a0b62ce67dd335e2cb878b483db1da0796ef77
[thirdparty/squid.git] / src / errorpage.cc
1 /*
2 * Copyright (C) 1996-2016 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 "cache_cf.h"
13 #include "clients/forward.h"
14 #include "comm/Connection.h"
15 #include "comm/Write.h"
16 #include "err_detail_type.h"
17 #include "errorpage.h"
18 #include "fde.h"
19 #include "fs_io.h"
20 #include "html_quote.h"
21 #include "HttpHeaderTools.h"
22 #include "HttpReply.h"
23 #include "HttpRequest.h"
24 #include "MemBuf.h"
25 #include "MemObject.h"
26 #include "rfc1738.h"
27 #include "SquidConfig.h"
28 #include "Store.h"
29 #include "tools.h"
30 #include "URL.h"
31 #include "wordlist.h"
32 #if USE_AUTH
33 #include "auth/UserRequest.h"
34 #endif
35 #include "SquidTime.h"
36 #if USE_OPENSSL
37 #include "ssl/ErrorDetailManager.h"
38 #endif
39
40 /**
41 \defgroup ErrorPageInternal Error Page Internals
42 \ingroup ErrorPageAPI
43 *
44 \section Abstract Abstract:
45 * These routines are used to generate error messages to be
46 * sent to clients. The error type is used to select between
47 * the various message formats. (formats are stored in the
48 * Config.errorDirectory)
49 */
50
51 #if !defined(DEFAULT_SQUID_ERROR_DIR)
52 /** Where to look for errors if config path fails.
53 \note Please use ./configure --datadir=/path instead of patching
54 */
55 #define DEFAULT_SQUID_ERROR_DIR DEFAULT_SQUID_DATA_DIR"/errors"
56 #endif
57
58 /// \ingroup ErrorPageInternal
59 CBDATA_CLASS_INIT(ErrorState);
60
61 /* local types */
62
63 /// \ingroup ErrorPageInternal
64 typedef struct {
65 int id;
66 char *page_name;
67 Http::StatusCode page_redirect;
68 } ErrorDynamicPageInfo;
69
70 /* local constant and vars */
71
72 /**
73 \ingroup ErrorPageInternal
74 *
75 \note hard coded error messages are not appended with %S
76 * automagically to give you more control on the format
77 */
78 static const struct {
79 int type; /* and page_id */
80 const char *text;
81 }
82
83 error_hard_text[] = {
84
85 {
86 ERR_SQUID_SIGNATURE,
87 "\n<br>\n"
88 "<hr>\n"
89 "<div id=\"footer\">\n"
90 "Generated %T by %h (%s)\n"
91 "</div>\n"
92 "</body></html>\n"
93 },
94 {
95 TCP_RESET,
96 "reset"
97 }
98 };
99
100 /// \ingroup ErrorPageInternal
101 static std::vector<ErrorDynamicPageInfo *> ErrorDynamicPages;
102
103 /* local prototypes */
104
105 /// \ingroup ErrorPageInternal
106 static const int error_hard_text_count = sizeof(error_hard_text) / sizeof(*error_hard_text);
107
108 /// \ingroup ErrorPageInternal
109 static char **error_text = NULL;
110
111 /// \ingroup ErrorPageInternal
112 static int error_page_count = 0;
113
114 /// \ingroup ErrorPageInternal
115 static MemBuf error_stylesheet;
116
117 static const char *errorFindHardText(err_type type);
118 static ErrorDynamicPageInfo *errorDynamicPageInfoCreate(int id, const char *page_name);
119 static void errorDynamicPageInfoDestroy(ErrorDynamicPageInfo * info);
120 static IOCB errorSendComplete;
121
122 /// \ingroup ErrorPageInternal
123 /// manages an error page template
124 class ErrorPageFile: public TemplateFile
125 {
126 public:
127 ErrorPageFile(const char *name, const err_type code) : TemplateFile(name,code) {textBuf.init();}
128
129 /// The template text data read from disk
130 const char *text() { return textBuf.content(); }
131
132 private:
133 /// stores the data read from disk to a local buffer
134 virtual bool parse(const char *buf, int len, bool) {
135 if (len)
136 textBuf.append(buf, len);
137 return true;
138 }
139
140 MemBuf textBuf; ///< A buffer to store the error page
141 };
142
143 /// \ingroup ErrorPageInternal
144 err_type &operator++ (err_type &anErr)
145 {
146 int tmp = (int)anErr;
147 anErr = (err_type)(++tmp);
148 return anErr;
149 }
150
151 /// \ingroup ErrorPageInternal
152 int operator - (err_type const &anErr, err_type const &anErr2)
153 {
154 return (int)anErr - (int)anErr2;
155 }
156
157 void
158 errorInitialize(void)
159 {
160 err_type i;
161 const char *text;
162 error_page_count = ERR_MAX + ErrorDynamicPages.size();
163 error_text = static_cast<char **>(xcalloc(error_page_count, sizeof(char *)));
164
165 for (i = ERR_NONE, ++i; i < error_page_count; ++i) {
166 safe_free(error_text[i]);
167
168 if ((text = errorFindHardText(i))) {
169 /**\par
170 * Index any hard-coded error text into defaults.
171 */
172 error_text[i] = xstrdup(text);
173
174 } else if (i < ERR_MAX) {
175 /**\par
176 * Index precompiled fixed template files from one of two sources:
177 * (a) default language translation directory (error_default_language)
178 * (b) admin specified custom directory (error_directory)
179 */
180 ErrorPageFile errTmpl(err_type_str[i], i);
181 error_text[i] = errTmpl.loadDefault() ? xstrdup(errTmpl.text()) : NULL;
182 } else {
183 /** \par
184 * Index any unknown file names used by deny_info.
185 */
186 ErrorDynamicPageInfo *info = ErrorDynamicPages.at(i - ERR_MAX);
187 assert(info && info->id == i && info->page_name);
188
189 const char *pg = info->page_name;
190 if (info->page_redirect != Http::scNone)
191 pg = info->page_name +4;
192
193 if (strchr(pg, ':') == NULL) {
194 /** But only if they are not redirection URL. */
195 ErrorPageFile errTmpl(pg, ERR_MAX);
196 error_text[i] = errTmpl.loadDefault() ? xstrdup(errTmpl.text()) : NULL;
197 }
198 }
199 }
200
201 error_stylesheet.reset();
202
203 // look for and load stylesheet into global MemBuf for it.
204 if (Config.errorStylesheet) {
205 ErrorPageFile tmpl("StylesSheet", ERR_MAX);
206 tmpl.loadFromFile(Config.errorStylesheet);
207 error_stylesheet.appendf("%s",tmpl.text());
208 }
209
210 #if USE_OPENSSL
211 Ssl::errorDetailInitialize();
212 #endif
213 }
214
215 void
216 errorClean(void)
217 {
218 if (error_text) {
219 int i;
220
221 for (i = ERR_NONE + 1; i < error_page_count; ++i)
222 safe_free(error_text[i]);
223
224 safe_free(error_text);
225 }
226
227 while (!ErrorDynamicPages.empty()) {
228 errorDynamicPageInfoDestroy(ErrorDynamicPages.back());
229 ErrorDynamicPages.pop_back();
230 }
231
232 error_page_count = 0;
233
234 #if USE_OPENSSL
235 Ssl::errorDetailClean();
236 #endif
237 }
238
239 /// \ingroup ErrorPageInternal
240 static const char *
241 errorFindHardText(err_type type)
242 {
243 int i;
244
245 for (i = 0; i < error_hard_text_count; ++i)
246 if (error_hard_text[i].type == type)
247 return error_hard_text[i].text;
248
249 return NULL;
250 }
251
252 TemplateFile::TemplateFile(const char *name, const err_type code): silent(false), wasLoaded(false), templateName(name), templateCode(code)
253 {
254 assert(name);
255 }
256
257 bool
258 TemplateFile::loadDefault()
259 {
260 if (loaded()) // already loaded?
261 return true;
262
263 /** test error_directory configured location */
264 if (Config.errorDirectory) {
265 char path[MAXPATHLEN];
266 snprintf(path, sizeof(path), "%s/%s", Config.errorDirectory, templateName.termedBuf());
267 loadFromFile(path);
268 }
269
270 #if USE_ERR_LOCALES
271 /** test error_default_language location */
272 if (!loaded() && Config.errorDefaultLanguage) {
273 if (!tryLoadTemplate(Config.errorDefaultLanguage)) {
274 debugs(1, (templateCode < TCP_RESET ? DBG_CRITICAL : 3), "Unable to load default error language files. Reset to backups.");
275 }
276 }
277 #endif
278
279 /* test default location if failed (templates == English translation base templates) */
280 if (!loaded()) {
281 tryLoadTemplate("templates");
282 }
283
284 /* giving up if failed */
285 if (!loaded()) {
286 debugs(1, (templateCode < TCP_RESET ? DBG_CRITICAL : 3), "WARNING: failed to find or read error text file " << templateName);
287 parse("Internal Error: Missing Template ", 33, '\0');
288 parse(templateName.termedBuf(), templateName.size(), '\0');
289 }
290
291 return true;
292 }
293
294 bool
295 TemplateFile::tryLoadTemplate(const char *lang)
296 {
297 assert(lang);
298
299 char path[MAXPATHLEN];
300 /* TODO: prep the directory path string to prevent snprintf ... */
301 snprintf(path, sizeof(path), "%s/%s/%s",
302 DEFAULT_SQUID_ERROR_DIR, lang, templateName.termedBuf());
303 path[MAXPATHLEN-1] = '\0';
304
305 if (loadFromFile(path))
306 return true;
307
308 #if HAVE_GLOB
309 if ( strlen(lang) == 2) {
310 /* TODO glob the error directory for sub-dirs matching: <tag> '-*' */
311 /* use first result. */
312 debugs(4,2, HERE << "wildcard fallback errors not coded yet.");
313 }
314 #endif
315
316 return false;
317 }
318
319 bool
320 TemplateFile::loadFromFile(const char *path)
321 {
322 int fd;
323 char buf[4096];
324 ssize_t len;
325
326 if (loaded()) // already loaded?
327 return true;
328
329 fd = file_open(path, O_RDONLY | O_TEXT);
330
331 if (fd < 0) {
332 /* with dynamic locale negotiation we may see some failures before a success. */
333 if (!silent && templateCode < TCP_RESET) {
334 int xerrno = errno;
335 debugs(4, DBG_CRITICAL, MYNAME << "'" << path << "': " << xstrerr(xerrno));
336 }
337 wasLoaded = false;
338 return wasLoaded;
339 }
340
341 while ((len = FD_READ_METHOD(fd, buf, sizeof(buf))) > 0) {
342 if (!parse(buf, len, false)) {
343 debugs(4, DBG_CRITICAL, MYNAME << "parse error while reading template file: " << path);
344 wasLoaded = false;
345 return wasLoaded;
346 }
347 }
348 parse(buf, 0, true);
349
350 if (len < 0) {
351 int xerrno = errno;
352 debugs(4, DBG_CRITICAL, MYNAME << "ERROR: failed to fully read: '" << path << "': " << xstrerr(xerrno));
353 }
354
355 file_close(fd);
356
357 wasLoaded = true;
358 return wasLoaded;
359 }
360
361 bool strHdrAcptLangGetItem(const String &hdr, char *lang, int langLen, size_t &pos)
362 {
363 while (pos < hdr.size()) {
364
365 /* skip any initial whitespace. */
366 while (pos < hdr.size() && xisspace(hdr[pos]))
367 ++pos;
368
369 /*
370 * Header value format:
371 * - sequence of whitespace delimited tags
372 * - each tag may suffix with ';'.* which we can ignore.
373 * - IFF a tag contains only two characters we can wildcard ANY translations matching: <it> '-'? .*
374 * with preference given to an exact match.
375 */
376 bool invalid_byte = false;
377 char *dt = lang;
378 while (pos < hdr.size() && hdr[pos] != ';' && hdr[pos] != ',' && !xisspace(hdr[pos]) && dt < (lang + (langLen -1)) ) {
379 if (!invalid_byte) {
380 #if USE_HTTP_VIOLATIONS
381 // if accepting violations we may as well accept some broken browsers
382 // which may send us the right code, wrong ISO formatting.
383 if (hdr[pos] == '_')
384 *dt = '-';
385 else
386 #endif
387 *dt = xtolower(hdr[pos]);
388 // valid codes only contain A-Z, hyphen (-) and *
389 if (*dt != '-' && *dt != '*' && (*dt < 'a' || *dt > 'z') )
390 invalid_byte = true;
391 else
392 ++dt; // move to next destination byte.
393 }
394 ++pos;
395 }
396 *dt = '\0'; // nul-terminated the filename content string before system use.
397
398 // if we terminated the tag on garbage or ';' we need to skip to the next ',' or end of header.
399 while (pos < hdr.size() && hdr[pos] != ',')
400 ++pos;
401
402 if (pos < hdr.size() && hdr[pos] == ',')
403 ++pos;
404
405 debugs(4, 9, "STATE: lang=" << lang << ", pos=" << pos << ", buf='" << ((pos < hdr.size()) ? hdr.substr(pos,hdr.size()) : "") << "'");
406
407 /* if we found anything we might use, try it. */
408 if (*lang != '\0' && !invalid_byte)
409 return true;
410 }
411 return false;
412 }
413
414 bool
415 TemplateFile::loadFor(const HttpRequest *request)
416 {
417 String hdr;
418
419 #if USE_ERR_LOCALES
420 if (loaded()) // already loaded?
421 return true;
422
423 if (!request || !request->header.getList(Http::HdrType::ACCEPT_LANGUAGE, &hdr) )
424 return false;
425
426 char lang[256];
427 size_t pos = 0; // current parsing position in header string
428
429 debugs(4, 6, HERE << "Testing Header: '" << hdr << "'");
430
431 while ( strHdrAcptLangGetItem(hdr, lang, 256, pos) ) {
432
433 /* wildcard uses the configured default language */
434 if (lang[0] == '*' && lang[1] == '\0') {
435 debugs(4, 6, HERE << "Found language '" << lang << "'. Using configured default.");
436 return false;
437 }
438
439 debugs(4, 6, HERE << "Found language '" << lang << "', testing for available template");
440
441 if (tryLoadTemplate(lang)) {
442 /* store the language we found for the Content-Language reply header */
443 errLanguage = lang;
444 break;
445 } else if (Config.errorLogMissingLanguages) {
446 debugs(4, DBG_IMPORTANT, "WARNING: Error Pages Missing Language: " << lang);
447 }
448 }
449 #endif
450
451 return loaded();
452 }
453
454 /// \ingroup ErrorPageInternal
455 static ErrorDynamicPageInfo *
456 errorDynamicPageInfoCreate(int id, const char *page_name)
457 {
458 ErrorDynamicPageInfo *info = new ErrorDynamicPageInfo;
459 info->id = id;
460 info->page_name = xstrdup(page_name);
461 info->page_redirect = static_cast<Http::StatusCode>(atoi(page_name));
462
463 /* WARNING on redirection status:
464 * 2xx are permitted, but not documented officially.
465 * - might be useful for serving static files (PAC etc) in special cases
466 * 3xx require a URL suitable for Location: header.
467 * - the current design does not allow for a Location: URI as well as a local file template
468 * although this possibility is explicitly permitted in the specs.
469 * 4xx-5xx require a local file template.
470 * - sending Location: on these codes with no body is invalid by the specs.
471 * - current result is Squid crashing or XSS problems as dynamic deny_info load random disk files.
472 * - a future redesign of the file loading may result in loading remote objects sent inline as local body.
473 */
474 if (info->page_redirect == Http::scNone)
475 ; // special case okay.
476 else if (info->page_redirect < 200 || info->page_redirect > 599) {
477 // out of range
478 debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " is not valid on '" << page_name << "'");
479 self_destruct();
480 } else if ( /* >= 200 && */ info->page_redirect < 300 && strchr(&(page_name[4]), ':')) {
481 // 2xx require a local template file
482 debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a template on '" << page_name << "'");
483 self_destruct();
484 } else if (info->page_redirect >= 300 && info->page_redirect <= 399 && !strchr(&(page_name[4]), ':')) {
485 // 3xx require an absolute URL
486 debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a URL on '" << page_name << "'");
487 self_destruct();
488 } else if (info->page_redirect >= 400 /* && <= 599 */ && strchr(&(page_name[4]), ':')) {
489 // 4xx/5xx require a local template file
490 debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a template on '" << page_name << "'");
491 self_destruct();
492 }
493 // else okay.
494
495 return info;
496 }
497
498 /// \ingroup ErrorPageInternal
499 static void
500 errorDynamicPageInfoDestroy(ErrorDynamicPageInfo * info)
501 {
502 assert(info);
503 safe_free(info->page_name);
504 delete info;
505 }
506
507 /// \ingroup ErrorPageInternal
508 static int
509 errorPageId(const char *page_name)
510 {
511 for (int i = 0; i < ERR_MAX; ++i) {
512 if (strcmp(err_type_str[i], page_name) == 0)
513 return i;
514 }
515
516 for (size_t j = 0; j < ErrorDynamicPages.size(); ++j) {
517 if (strcmp(ErrorDynamicPages[j]->page_name, page_name) == 0)
518 return j + ERR_MAX;
519 }
520
521 return ERR_NONE;
522 }
523
524 err_type
525 errorReservePageId(const char *page_name)
526 {
527 ErrorDynamicPageInfo *info;
528 int id = errorPageId(page_name);
529
530 if (id == ERR_NONE) {
531 info = errorDynamicPageInfoCreate(ERR_MAX + ErrorDynamicPages.size(), page_name);
532 ErrorDynamicPages.push_back(info);
533 id = info->id;
534 }
535
536 return (err_type)id;
537 }
538
539 /// \ingroup ErrorPageInternal
540 const char *
541 errorPageName(int pageId)
542 {
543 if (pageId >= ERR_NONE && pageId < ERR_MAX) /* common case */
544 return err_type_str[pageId];
545
546 if (pageId >= ERR_MAX && pageId - ERR_MAX < (ssize_t)ErrorDynamicPages.size())
547 return ErrorDynamicPages[pageId - ERR_MAX]->page_name;
548
549 return "ERR_UNKNOWN"; /* should not happen */
550 }
551
552 ErrorState *
553 ErrorState::NewForwarding(err_type type, HttpRequest *request)
554 {
555 assert(request);
556 const Http::StatusCode status = request->flags.needValidation ?
557 Http::scGatewayTimeout : Http::scServiceUnavailable;
558 return new ErrorState(type, status, request);
559 }
560
561 ErrorState::ErrorState(err_type t, Http::StatusCode status, HttpRequest * req) :
562 type(t),
563 page_id(t),
564 err_language(NULL),
565 httpStatus(status),
566 #if USE_AUTH
567 auth_user_request (NULL),
568 #endif
569 request(NULL),
570 url(NULL),
571 xerrno(0),
572 port(0),
573 dnsError(),
574 ttl(0),
575 src_addr(),
576 redirect_url(NULL),
577 callback(NULL),
578 callback_data(NULL),
579 request_hdrs(NULL),
580 err_msg(NULL),
581 #if USE_OPENSSL
582 detail(NULL),
583 #endif
584 detailCode(ERR_DETAIL_NONE)
585 {
586 memset(&ftp, 0, sizeof(ftp));
587
588 if (page_id >= ERR_MAX && ErrorDynamicPages[page_id - ERR_MAX]->page_redirect != Http::scNone)
589 httpStatus = ErrorDynamicPages[page_id - ERR_MAX]->page_redirect;
590
591 if (req != NULL) {
592 request = req;
593 HTTPMSGLOCK(request);
594 src_addr = req->client_addr;
595 }
596 }
597
598 void
599 errorAppendEntry(StoreEntry * entry, ErrorState * err)
600 {
601 assert(entry->mem_obj != NULL);
602 assert (entry->isEmpty());
603 debugs(4, 4, "Creating an error page for entry " << entry <<
604 " with errorstate " << err <<
605 " page id " << err->page_id);
606
607 if (entry->store_status != STORE_PENDING) {
608 debugs(4, 2, "Skipping error page due to store_status: " << entry->store_status);
609 /*
610 * If the entry is not STORE_PENDING, then no clients
611 * care about it, and we don't need to generate an
612 * error message
613 */
614 assert(EBIT_TEST(entry->flags, ENTRY_ABORTED));
615 assert(entry->mem_obj->nclients == 0);
616 delete err;
617 return;
618 }
619
620 if (err->page_id == TCP_RESET) {
621 if (err->request) {
622 debugs(4, 2, "RSTing this reply");
623 err->request->flags.resetTcp = true;
624 }
625 }
626
627 entry->storeErrorResponse(err->BuildHttpReply());
628 delete err;
629 }
630
631 void
632 errorSend(const Comm::ConnectionPointer &conn, ErrorState * err)
633 {
634 HttpReply *rep;
635 debugs(4, 3, HERE << conn << ", err=" << err);
636 assert(Comm::IsConnOpen(conn));
637
638 rep = err->BuildHttpReply();
639
640 MemBuf *mb = rep->pack();
641 AsyncCall::Pointer call = commCbCall(78, 5, "errorSendComplete",
642 CommIoCbPtrFun(&errorSendComplete, err));
643 Comm::Write(conn, mb, call);
644 delete mb;
645
646 delete rep;
647 }
648
649 /**
650 \ingroup ErrorPageAPI
651 *
652 * Called by commHandleWrite() after data has been written
653 * to the client socket.
654 *
655 \note If there is a callback, the callback is responsible for
656 * closing the FD, otherwise we do it ourselves.
657 */
658 static void
659 errorSendComplete(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag errflag, int, void *data)
660 {
661 ErrorState *err = static_cast<ErrorState *>(data);
662 debugs(4, 3, HERE << conn << ", size=" << size);
663
664 if (errflag != Comm::ERR_CLOSING) {
665 if (err->callback) {
666 debugs(4, 3, "errorSendComplete: callback");
667 err->callback(conn->fd, err->callback_data, size);
668 } else {
669 debugs(4, 3, "errorSendComplete: comm_close");
670 conn->close();
671 }
672 }
673
674 delete err;
675 }
676
677 ErrorState::~ErrorState()
678 {
679 HTTPMSGUNLOCK(request);
680 safe_free(redirect_url);
681 safe_free(url);
682 safe_free(request_hdrs);
683 wordlistDestroy(&ftp.server_msg);
684 safe_free(ftp.request);
685 safe_free(ftp.reply);
686 #if USE_AUTH
687 auth_user_request = NULL;
688 #endif
689 safe_free(err_msg);
690 #if USE_ERR_LOCALES
691 if (err_language != Config.errorDefaultLanguage)
692 #endif
693 safe_free(err_language);
694 #if USE_OPENSSL
695 delete detail;
696 #endif
697 }
698
699 int
700 ErrorState::Dump(MemBuf * mb)
701 {
702 MemBuf str;
703 char ntoabuf[MAX_IPSTRLEN];
704
705 str.reset();
706 /* email subject line */
707 str.appendf("CacheErrorInfo - %s", errorPageName(type));
708 mb->appendf("?subject=%s", rfc1738_escape_part(str.buf));
709 str.reset();
710 /* email body */
711 str.appendf("CacheHost: %s\r\n", getMyHostname());
712 /* - Err Msgs */
713 str.appendf("ErrPage: %s\r\n", errorPageName(type));
714
715 if (xerrno) {
716 str.appendf("Err: (%d) %s\r\n", xerrno, strerror(xerrno));
717 } else {
718 str.append("Err: [none]\r\n", 13);
719 }
720 #if USE_AUTH
721 if (auth_user_request.getRaw() && auth_user_request->denyMessage())
722 str.appendf("Auth ErrMsg: %s\r\n", auth_user_request->denyMessage());
723 #endif
724 if (dnsError.size() > 0)
725 str.appendf("DNS ErrMsg: %s\r\n", dnsError.termedBuf());
726
727 /* - TimeStamp */
728 str.appendf("TimeStamp: %s\r\n\r\n", mkrfc1123(squid_curtime));
729
730 /* - IP stuff */
731 str.appendf("ClientIP: %s\r\n", src_addr.toStr(ntoabuf,MAX_IPSTRLEN));
732
733 if (request && request->hier.host[0] != '\0') {
734 str.appendf("ServerIP: %s\r\n", request->hier.host);
735 }
736
737 str.append("\r\n", 2);
738 /* - HTTP stuff */
739 str.append("HTTP Request:\r\n", 15);
740 if (request) {
741 str.appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\n",
742 SQUIDSBUFPRINT(request->method.image()),
743 SQUIDSBUFPRINT(request->url.path()),
744 AnyP::ProtocolType_str[request->http_ver.protocol],
745 request->http_ver.major, request->http_ver.minor);
746 request->header.packInto(&str);
747 }
748
749 str.append("\r\n", 2);
750 /* - FTP stuff */
751
752 if (ftp.request) {
753 str.appendf("FTP Request: %s\r\n", ftp.request);
754 str.appendf("FTP Reply: %s\r\n", (ftp.reply? ftp.reply:"[none]"));
755 str.append("FTP Msg: ", 9);
756 wordlistCat(ftp.server_msg, &str);
757 str.append("\r\n", 2);
758 }
759
760 str.append("\r\n", 2);
761 mb->appendf("&body=%s", rfc1738_escape_part(str.buf));
762 str.clean();
763 return 0;
764 }
765
766 /// \ingroup ErrorPageInternal
767 #define CVT_BUF_SZ 512
768
769 const char *
770 ErrorState::Convert(char token, bool building_deny_info_url, bool allowRecursion)
771 {
772 static MemBuf mb;
773 const char *p = NULL; /* takes priority over mb if set */
774 int do_quote = 1;
775 int no_urlescape = 0; /* if true then item is NOT to be further URL-encoded */
776 char ntoabuf[MAX_IPSTRLEN];
777
778 mb.reset();
779
780 switch (token) {
781
782 case 'a':
783 #if USE_AUTH
784 if (request && request->auth_user_request != NULL)
785 p = request->auth_user_request->username();
786 if (!p)
787 #endif
788 p = "-";
789 break;
790
791 case 'b':
792 mb.appendf("%u", getMyPort());
793 break;
794
795 case 'B':
796 if (building_deny_info_url) break;
797 if (request) {
798 const SBuf &tmp = Ftp::UrlWith2f(request);
799 mb.append(tmp.rawContent(), tmp.length());
800 } else
801 p = "[no URL]";
802 break;
803
804 case 'c':
805 if (building_deny_info_url) break;
806 p = errorPageName(type);
807 break;
808
809 case 'D':
810 if (!allowRecursion)
811 p = "%D"; // if recursion is not allowed, do not convert
812 #if USE_OPENSSL
813 // currently only SSL error details implemented
814 else if (detail) {
815 detail->useRequest(request);
816 const String &errDetail = detail->toString();
817 if (errDetail.size() > 0) {
818 MemBuf *detail_mb = ConvertText(errDetail.termedBuf(), false);
819 mb.append(detail_mb->content(), detail_mb->contentSize());
820 delete detail_mb;
821 do_quote = 0;
822 }
823 }
824 #endif
825 if (!mb.contentSize())
826 mb.append("[No Error Detail]", 17);
827 break;
828
829 case 'e':
830 mb.appendf("%d", xerrno);
831 break;
832
833 case 'E':
834 if (xerrno)
835 mb.appendf("(%d) %s", xerrno, strerror(xerrno));
836 else
837 mb.append("[No Error]", 10);
838 break;
839
840 case 'f':
841 if (building_deny_info_url) break;
842 /* FTP REQUEST LINE */
843 if (ftp.request)
844 p = ftp.request;
845 else
846 p = "nothing";
847 break;
848
849 case 'F':
850 if (building_deny_info_url) break;
851 /* FTP REPLY LINE */
852 if (ftp.reply)
853 p = ftp.reply;
854 else
855 p = "nothing";
856 break;
857
858 case 'g':
859 if (building_deny_info_url) break;
860 /* FTP SERVER RESPONSE */
861 if (ftp.listing) {
862 mb.append(ftp.listing->content(), ftp.listing->contentSize());
863 do_quote = 0;
864 } else if (ftp.server_msg) {
865 wordlistCat(ftp.server_msg, &mb);
866 }
867 break;
868
869 case 'h':
870 mb.appendf("%s", getMyHostname());
871 break;
872
873 case 'H':
874 if (request) {
875 if (request->hier.host[0] != '\0') // if non-empty string.
876 p = request->hier.host;
877 else
878 p = request->url.host();
879 } else if (!building_deny_info_url)
880 p = "[unknown host]";
881 break;
882
883 case 'i':
884 mb.appendf("%s", src_addr.toStr(ntoabuf,MAX_IPSTRLEN));
885 break;
886
887 case 'I':
888 if (request && request->hier.tcpServer != NULL)
889 p = request->hier.tcpServer->remote.toStr(ntoabuf,MAX_IPSTRLEN);
890 else if (!building_deny_info_url)
891 p = "[unknown]";
892 break;
893
894 case 'l':
895 if (building_deny_info_url) break;
896 mb.append(error_stylesheet.content(), error_stylesheet.contentSize());
897 do_quote = 0;
898 break;
899
900 case 'L':
901 if (building_deny_info_url) break;
902 if (Config.errHtmlText) {
903 mb.appendf("%s", Config.errHtmlText);
904 do_quote = 0;
905 } else
906 p = "[not available]";
907 break;
908
909 case 'm':
910 if (building_deny_info_url) break;
911 #if USE_AUTH
912 if (auth_user_request.getRaw())
913 p = auth_user_request->denyMessage("[not available]");
914 else
915 p = "[not available]";
916 #else
917 p = "-";
918 #endif
919 break;
920
921 case 'M':
922 if (request) {
923 const SBuf &m = request->method.image();
924 mb.append(m.rawContent(), m.length());
925 } else if (!building_deny_info_url)
926 p = "[unknown method]";
927 break;
928
929 case 'O':
930 if (!building_deny_info_url)
931 do_quote = 0;
932 case 'o':
933 p = request ? request->extacl_message.termedBuf() : external_acl_message;
934 if (!p && !building_deny_info_url)
935 p = "[not available]";
936 break;
937
938 case 'p':
939 if (request) {
940 mb.appendf("%u", request->url.port());
941 } else if (!building_deny_info_url) {
942 p = "[unknown port]";
943 }
944 break;
945
946 case 'P':
947 if (request) {
948 const SBuf &m = request->url.getScheme().image();
949 mb.append(m.rawContent(), m.length());
950 } else if (!building_deny_info_url) {
951 p = "[unknown protocol]";
952 }
953 break;
954
955 case 'R':
956 if (building_deny_info_url) {
957 if (request != NULL) {
958 SBuf tmp = request->url.path();
959 p = tmp.c_str();
960 no_urlescape = 1;
961 } else
962 p = "[no request]";
963 break;
964 }
965 if (request != NULL) {
966 mb.appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\n",
967 SQUIDSBUFPRINT(request->method.image()),
968 SQUIDSBUFPRINT(request->url.path()),
969 AnyP::ProtocolType_str[request->http_ver.protocol],
970 request->http_ver.major, request->http_ver.minor);
971 request->header.packInto(&mb, true); //hide authorization data
972 } else if (request_hdrs) {
973 p = request_hdrs;
974 } else {
975 p = "[no request]";
976 }
977 break;
978
979 case 's':
980 /* for backward compat we make %s show the full URL. Drop this in some future release. */
981 if (building_deny_info_url) {
982 if (request) {
983 const SBuf &tmp = request->effectiveRequestUri();
984 mb.append(tmp.rawContent(), tmp.length());
985 } else
986 p = url;
987 debugs(0, DBG_CRITICAL, "WARNING: deny_info now accepts coded tags. Use %u to get the full URL instead of %s");
988 } else
989 p = visible_appname_string;
990 break;
991
992 case 'S':
993 if (building_deny_info_url) {
994 p = visible_appname_string;
995 break;
996 }
997 /* signature may contain %-escapes, recursion */
998 if (page_id != ERR_SQUID_SIGNATURE) {
999 const int saved_id = page_id;
1000 page_id = ERR_SQUID_SIGNATURE;
1001 MemBuf *sign_mb = BuildContent();
1002 mb.append(sign_mb->content(), sign_mb->contentSize());
1003 sign_mb->clean();
1004 delete sign_mb;
1005 page_id = saved_id;
1006 do_quote = 0;
1007 } else {
1008 /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */
1009 p = "[%S]";
1010 }
1011 break;
1012
1013 case 't':
1014 mb.appendf("%s", Time::FormatHttpd(squid_curtime));
1015 break;
1016
1017 case 'T':
1018 mb.appendf("%s", mkrfc1123(squid_curtime));
1019 break;
1020
1021 case 'U':
1022 /* Using the fake-https version of absolute-URI so error pages see https:// */
1023 /* even when the url-path cannot be shown as more than '*' */
1024 if (request)
1025 p = urlCanonicalFakeHttps(request);
1026 else if (url)
1027 p = url;
1028 else if (!building_deny_info_url)
1029 p = "[no URL]";
1030 break;
1031
1032 case 'u':
1033 if (request) {
1034 const SBuf &tmp = request->effectiveRequestUri();
1035 mb.append(tmp.rawContent(), tmp.length());
1036 } else if (url)
1037 p = url;
1038 else if (!building_deny_info_url)
1039 p = "[no URL]";
1040 break;
1041
1042 case 'w':
1043 if (Config.adminEmail)
1044 mb.appendf("%s", Config.adminEmail);
1045 else if (!building_deny_info_url)
1046 p = "[unknown]";
1047 break;
1048
1049 case 'W':
1050 if (building_deny_info_url) break;
1051 if (Config.adminEmail && Config.onoff.emailErrData)
1052 Dump(&mb);
1053 no_urlescape = 1;
1054 break;
1055
1056 case 'x':
1057 #if USE_OPENSSL
1058 if (detail)
1059 mb.appendf("%s", detail->errorName());
1060 else
1061 #endif
1062 if (!building_deny_info_url)
1063 p = "[Unknown Error Code]";
1064 break;
1065
1066 case 'z':
1067 if (building_deny_info_url) break;
1068 if (dnsError.size() > 0)
1069 p = dnsError.termedBuf();
1070 else if (ftp.cwd_msg)
1071 p = ftp.cwd_msg;
1072 else
1073 p = "[unknown]";
1074 break;
1075
1076 case 'Z':
1077 if (building_deny_info_url) break;
1078 if (err_msg)
1079 p = err_msg;
1080 else
1081 p = "[unknown]";
1082 break;
1083
1084 case '%':
1085 p = "%";
1086 break;
1087
1088 default:
1089 mb.appendf("%%%c", token);
1090 do_quote = 0;
1091 break;
1092 }
1093
1094 if (!p)
1095 p = mb.buf; /* do not use mb after this assignment! */
1096
1097 assert(p);
1098
1099 debugs(4, 3, "errorConvert: %%" << token << " --> '" << p << "'" );
1100
1101 if (do_quote)
1102 p = html_quote(p);
1103
1104 if (building_deny_info_url && !no_urlescape)
1105 p = rfc1738_escape_part(p);
1106
1107 return p;
1108 }
1109
1110 void
1111 ErrorState::DenyInfoLocation(const char *name, HttpRequest *, MemBuf &result)
1112 {
1113 char const *m = name;
1114 char const *p = m;
1115 char const *t;
1116
1117 if (m[0] == '3')
1118 m += 4; // skip "3xx:"
1119
1120 while ((p = strchr(m, '%'))) {
1121 result.append(m, p - m); /* copy */
1122 t = Convert(*++p, true, true); /* convert */
1123 result.appendf("%s", t); /* copy */
1124 m = p + 1; /* advance */
1125 }
1126
1127 if (*m)
1128 result.appendf("%s", m); /* copy tail */
1129
1130 assert((size_t)result.contentSize() == strlen(result.content()));
1131 }
1132
1133 HttpReply *
1134 ErrorState::BuildHttpReply()
1135 {
1136 HttpReply *rep = new HttpReply;
1137 const char *name = errorPageName(page_id);
1138 /* no LMT for error pages; error pages expire immediately */
1139
1140 if (name[0] == '3' || (name[0] != '2' && name[0] != '4' && name[0] != '5' && strchr(name, ':'))) {
1141 /* Redirection */
1142 Http::StatusCode status = Http::scFound;
1143 // Use configured 3xx reply status if set.
1144 if (name[0] == '3')
1145 status = httpStatus;
1146 else {
1147 // Use 307 for HTTP/1.1 non-GET/HEAD requests.
1148 if (request != NULL && request->method != Http::METHOD_GET && request->method != Http::METHOD_HEAD && request->http_ver >= Http::ProtocolVersion(1,1))
1149 status = Http::scTemporaryRedirect;
1150 }
1151
1152 rep->setHeaders(status, NULL, "text/html;charset=utf-8", 0, 0, -1);
1153
1154 if (request) {
1155 MemBuf redirect_location;
1156 redirect_location.init();
1157 DenyInfoLocation(name, request, redirect_location);
1158 httpHeaderPutStrf(&rep->header, Http::HdrType::LOCATION, "%s", redirect_location.content() );
1159 }
1160
1161 httpHeaderPutStrf(&rep->header, Http::HdrType::X_SQUID_ERROR, "%d %s", httpStatus, "Access Denied");
1162 } else {
1163 MemBuf *content = BuildContent();
1164 rep->setHeaders(httpStatus, NULL, "text/html;charset=utf-8", content->contentSize(), 0, -1);
1165 /*
1166 * include some information for downstream caches. Implicit
1167 * replaceable content. This isn't quite sufficient. xerrno is not
1168 * necessarily meaningful to another system, so we really should
1169 * expand it. Additionally, we should identify ourselves. Someone
1170 * might want to know. Someone _will_ want to know OTOH, the first
1171 * X-CACHE-MISS entry should tell us who.
1172 */
1173 httpHeaderPutStrf(&rep->header, Http::HdrType::X_SQUID_ERROR, "%s %d", name, xerrno);
1174
1175 #if USE_ERR_LOCALES
1176 /*
1177 * If error page auto-negotiate is enabled in any way, send the Vary.
1178 * RFC 2616 section 13.6 and 14.44 says MAY and SHOULD do this.
1179 * We have even better reasons though:
1180 * see http://wiki.squid-cache.org/KnowledgeBase/VaryNotCaching
1181 */
1182 if (!Config.errorDirectory) {
1183 /* We 'negotiated' this ONLY from the Accept-Language. */
1184 rep->header.delById(Http::HdrType::VARY);
1185 rep->header.putStr(Http::HdrType::VARY, "Accept-Language");
1186 }
1187
1188 /* add the Content-Language header according to RFC section 14.12 */
1189 if (err_language) {
1190 rep->header.putStr(Http::HdrType::CONTENT_LANGUAGE, err_language);
1191 } else
1192 #endif /* USE_ERROR_LOCALES */
1193 {
1194 /* default templates are in English */
1195 /* language is known unless error_directory override used */
1196 if (!Config.errorDirectory)
1197 rep->header.putStr(Http::HdrType::CONTENT_LANGUAGE, "en");
1198 }
1199
1200 rep->body.setMb(content);
1201 /* do not memBufClean() or delete the content, it was absorbed by httpBody */
1202 }
1203
1204 // Make sure error codes get back to the client side for logging and
1205 // error tracking.
1206 if (request) {
1207 int edc = ERR_DETAIL_NONE; // error detail code
1208 #if USE_OPENSSL
1209 if (detail)
1210 edc = detail->errorNo();
1211 else
1212 #endif
1213 if (detailCode)
1214 edc = detailCode;
1215 else
1216 edc = xerrno;
1217 request->detailError(type, edc);
1218 }
1219
1220 return rep;
1221 }
1222
1223 MemBuf *
1224 ErrorState::BuildContent()
1225 {
1226 const char *m = NULL;
1227
1228 assert(page_id > ERR_NONE && page_id < error_page_count);
1229
1230 #if USE_ERR_LOCALES
1231 ErrorPageFile *localeTmpl = NULL;
1232
1233 /** error_directory option in squid.conf overrides translations.
1234 * Custom errors are always found either in error_directory or the templates directory.
1235 * Otherwise locate the Accept-Language header
1236 */
1237 if (!Config.errorDirectory && page_id < ERR_MAX) {
1238 if (err_language && err_language != Config.errorDefaultLanguage)
1239 safe_free(err_language);
1240
1241 localeTmpl = new ErrorPageFile(err_type_str[page_id], static_cast<err_type>(page_id));
1242 if (localeTmpl->loadFor(request)) {
1243 m = localeTmpl->text();
1244 assert(localeTmpl->language());
1245 err_language = xstrdup(localeTmpl->language());
1246 }
1247 }
1248 #endif /* USE_ERR_LOCALES */
1249
1250 /** \par
1251 * If client-specific error templates are not enabled or available.
1252 * fall back to the old style squid.conf settings.
1253 */
1254 if (!m) {
1255 m = error_text[page_id];
1256 #if USE_ERR_LOCALES
1257 if (!Config.errorDirectory)
1258 err_language = Config.errorDefaultLanguage;
1259 #endif
1260 debugs(4, 2, HERE << "No existing error page language negotiated for " << errorPageName(page_id) << ". Using default error file.");
1261 }
1262
1263 MemBuf *result = ConvertText(m, true);
1264 #if USE_ERR_LOCALES
1265 if (localeTmpl)
1266 delete localeTmpl;
1267 #endif
1268 return result;
1269 }
1270
1271 MemBuf *ErrorState::ConvertText(const char *text, bool allowRecursion)
1272 {
1273 MemBuf *content = new MemBuf;
1274 const char *p;
1275 const char *m = text;
1276 assert(m);
1277 content->init();
1278
1279 while ((p = strchr(m, '%'))) {
1280 content->append(m, p - m); /* copy */
1281 const char *t = Convert(*++p, false, allowRecursion); /* convert */
1282 content->appendf("%s", t); /* copy */
1283 m = p + 1; /* advance */
1284 }
1285
1286 if (*m)
1287 content->appendf("%s", m); /* copy tail */
1288
1289 content->terminate();
1290
1291 assert((size_t)content->contentSize() == strlen(content->content()));
1292
1293 return content;
1294 }
1295