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