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