/*
* Copyright (C) 1996-2023 The Squid Software Foundation and contributors
*
* Squid software is distributed under GPLv2+ license and includes
* contributions from numerous individuals and organizations.
* Please see the COPYING and CONTRIBUTORS files for details.
*/
/* DEBUG: section 04 Error Generation */
#include "squid.h"
#include "AccessLogEntry.h"
#include "cache_cf.h"
#include "clients/forward.h"
#include "comm/Connection.h"
#include "comm/Write.h"
#include "error/Detail.h"
#include "error/SysErrorDetail.h"
#include "errorpage.h"
#include "fde.h"
#include "format/Format.h"
#include "fs_io.h"
#include "html_quote.h"
#include "HttpHeaderTools.h"
#include "HttpReply.h"
#include "HttpRequest.h"
#include "MemBuf.h"
#include "MemObject.h"
#include "rfc1738.h"
#include "sbuf/Stream.h"
#include "SquidConfig.h"
#include "SquidTime.h"
#include "Store.h"
#include "tools.h"
#include "wordlist.h"
#if USE_AUTH
#include "auth/UserRequest.h"
#endif
#if USE_OPENSSL
#include "ssl/ErrorDetailManager.h"
#endif
/**
\defgroup ErrorPageInternal Error Page Internals
\ingroup ErrorPageAPI
*
\section ErrorPagesAbstract Abstract:
* These routines are used to generate error messages to be
* sent to clients. The error type is used to select between
* the various message formats. (formats are stored in the
* Config.errorDirectory)
*/
#if !defined(DEFAULT_SQUID_ERROR_DIR)
/** Where to look for errors if config path fails.
\note Please use ./configure --datadir=/path instead of patching
*/
#define DEFAULT_SQUID_ERROR_DIR DEFAULT_SQUID_DATA_DIR"/errors"
#endif
/// \ingroup ErrorPageInternal
CBDATA_CLASS_INIT(ErrorState);
const SBuf ErrorState::LogformatMagic("@Squid{");
/* local types */
/// an error page created from admin-configurable metadata (e.g. deny_info)
class ErrorDynamicPageInfo {
public:
ErrorDynamicPageInfo(const int anId, const char *aName, const SBuf &aCfgLocation);
~ErrorDynamicPageInfo() { xfree(page_name); }
/// error_text[] index for response body (unused in redirection responses)
int id;
/// Primary deny_info parameter:
/// * May start with an HTTP status code.
/// * Either a well-known error page name, a filename, or a redirect URL.
char *page_name;
/// admin-configured HTTP Location header value for redirection responses
const char *uri;
/// admin-configured name for the error page template (custom or standard)
const char *filename;
/// deny_info directive position in squid.conf (for reporting)
SBuf cfgLocation;
// XXX: Misnamed. Not just for redirects.
/// admin-configured HTTP status code
Http::StatusCode page_redirect;
private:
// no copying of any kind
ErrorDynamicPageInfo(ErrorDynamicPageInfo &&) = delete;
};
namespace ErrorPage {
/// state and parameters shared by several ErrorState::compile*() methods
class Build
{
public:
SBuf output; ///< compilation result
const char *input = nullptr; ///< template bytes that need to be compiled
bool building_deny_info_url = false; ///< whether we compile deny_info URI
bool allowRecursion = false; ///< whether top-level compile() calls are OK
};
/// pretty-prints error page/deny_info building error
class BuildErrorPrinter
{
public:
BuildErrorPrinter(const SBuf &anInputLocation, int aPage, const char *aMsg, const char *aNear): inputLocation(anInputLocation), page_id(aPage), msg(aMsg), near(aNear) {}
/// reports error details (for admin-visible exceptions and debugging)
std::ostream &print(std::ostream &) const;
/// print() helper to report where the error was found
std::ostream &printLocation(std::ostream &os) const;
/* saved constructor parameters */
const SBuf &inputLocation;
const int page_id;
const char *msg;
const char *near;
};
static inline std::ostream &
operator <<(std::ostream &os, const BuildErrorPrinter &context)
{
return context.print(os);
}
static const char *IsDenyInfoUri(const int page_id);
static void ImportStaticErrorText(const int page_id, const char *text, const SBuf &inputLocation);
static void ValidateStaticError(const int page_id, const SBuf &inputLocation);
} // namespace ErrorPage
/* local constant and vars */
/**
\ingroup ErrorPageInternal
*
\note hard coded error messages are not appended with %S
* automagically to give you more control on the format
*/
static const struct {
int type; /* and page_id */
const char *text;
}
/// error messages that cannot be configured/customized externally
error_hard_text[] = {
{
ERR_SQUID_SIGNATURE,
"\n
\n"
"