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