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