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