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