2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00
bind/bin/named/log.c

251 lines
6.6 KiB
C
Raw Normal View History

1999-10-22 19:33:40 +00:00
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
1999-10-22 19:33:40 +00:00
*/
/*! \file */
2000-06-22 22:00:42 +00:00
Set up default logging for SSLKEYLOGFILE A customary method of exporting TLS pre-master secrets used by a piece of software (for debugging purposes, e.g. to examine decrypted traffic in a packet sniffer) is to set the SSLKEYLOGFILE environment variable to the path to the file in which this data should be logged. In order to enable writing any data to a file using the logging framework provided by libisc, a logging channel needs to be defined and the relevant logging category needs to be associated with it. Since the SSLKEYLOGFILE variable is only expected to contain a path, some defaults for the logging channel need to be assumed. Add a new function, named_log_setdefaultsslkeylogfile(), for setting up those implicit defaults, which are equivalent to the following logging configuration: channel default_sslkeylogfile { file "${SSLKEYLOGFILE}" versions 10 size 100m suffix timestamp; }; category sslkeylog { default_sslkeylogfile; }; This ensures TLS pre-master secrets do not use up more than about 1 GB of disk space, which should be enough to hold debugging data for the most recent 1 million TLS connections. As these values are arguably not universally appropriate for all deployment environments, a way for overriding them needs to exist. Suppress creation of the default logging channel for TLS pre-master secrets when the SSLKEYLOGFILE variable is set to the string "config". This enables providing custom logging configuration for the relevant category via the "logging" stanza. (Note that it would have been simpler to only skip setting up the default logging channel for TLS pre-master secrets if the SSLKEYLOGFILE environment variable is not set at all. However, libisc only logs pre-master secrets if that variable is set. Detecting a "magic" string enables the SSLKEYLOGFILE environment variable to serve as a single control for both enabling TLS pre-master secret collection and potentially also indicating where and how they should be exported.)
2021-12-22 18:17:26 +01:00
#include <stdlib.h>
2000-08-25 01:08:20 +00:00
#include <isc/result.h>
Set up default logging for SSLKEYLOGFILE A customary method of exporting TLS pre-master secrets used by a piece of software (for debugging purposes, e.g. to examine decrypted traffic in a packet sniffer) is to set the SSLKEYLOGFILE environment variable to the path to the file in which this data should be logged. In order to enable writing any data to a file using the logging framework provided by libisc, a logging channel needs to be defined and the relevant logging category needs to be associated with it. Since the SSLKEYLOGFILE variable is only expected to contain a path, some defaults for the logging channel need to be assumed. Add a new function, named_log_setdefaultsslkeylogfile(), for setting up those implicit defaults, which are equivalent to the following logging configuration: channel default_sslkeylogfile { file "${SSLKEYLOGFILE}" versions 10 size 100m suffix timestamp; }; category sslkeylog { default_sslkeylogfile; }; This ensures TLS pre-master secrets do not use up more than about 1 GB of disk space, which should be enough to hold debugging data for the most recent 1 million TLS connections. As these values are arguably not universally appropriate for all deployment environments, a way for overriding them needs to exist. Suppress creation of the default logging channel for TLS pre-master secrets when the SSLKEYLOGFILE variable is set to the string "config". This enables providing custom logging configuration for the relevant category via the "logging" stanza. (Note that it would have been simpler to only skip setting up the default logging channel for TLS pre-master secrets if the SSLKEYLOGFILE environment variable is not set at all. However, libisc only logs pre-master secrets if that variable is set. Detecting a "magic" string enables the SSLKEYLOGFILE environment variable to serve as a single control for both enabling TLS pre-master secret collection and potentially also indicating where and how they should be exported.)
2021-12-22 18:17:26 +01:00
#include <isc/util.h>
2000-08-25 01:08:20 +00:00
#include <dns/log.h>
#include <isccfg/log.h>
#include <ns/log.h>
1999-10-22 19:33:40 +00:00
#include <named/log.h>
#ifndef ISC_FACILITY
#define ISC_FACILITY LOG_DAEMON
#endif /* ifndef ISC_FACILITY */
/*%
1999-10-22 19:33:40 +00:00
* When adding a new category, be sure to add the appropriate
* \#define to <named/log.h> and to update the list in
* bin/check/check-tool.c.
1999-10-22 19:33:40 +00:00
*/
static isc_logcategory_t categories[] = { { "", 0 },
{ "unmatched", 0 },
{ NULL, 0 } };
1999-10-22 19:33:40 +00:00
/*%
1999-10-22 19:33:40 +00:00
* When adding a new module, be sure to add the appropriate
* \#define to <dns/log.h>.
1999-10-22 19:33:40 +00:00
*/
static isc_logmodule_t modules[] = {
{ "main", 0 }, { "server", 0 }, { "control", 0 }, { NULL, 0 }
1999-10-22 19:33:40 +00:00
};
isc_result_t
2020-02-13 14:44:37 -08:00
named_log_init(bool safe) {
isc_result_t result;
2000-11-25 01:33:11 +00:00
isc_logconfig_t *lcfg = NULL;
1999-10-22 19:33:40 +00:00
named_g_categories = categories;
named_g_modules = modules;
1999-10-22 19:33:40 +00:00
1999-10-23 00:32:48 +00:00
/*
* Setup a logging context.
*/
isc_log_create(named_g_mctx, &named_g_lctx, &lcfg);
/*
* named-checktool.c:setup_logging() needs to be kept in sync.
*/
isc_log_registercategories(named_g_lctx, named_g_categories);
isc_log_registermodules(named_g_lctx, named_g_modules);
isc_log_setcontext(named_g_lctx);
dns_log_init(named_g_lctx);
dns_log_setcontext(named_g_lctx);
cfg_log_init(named_g_lctx);
ns_log_init(named_g_lctx);
ns_log_setcontext(named_g_lctx);
1999-10-23 00:32:48 +00:00
if (safe) {
named_log_setsafechannels(lcfg);
} else {
named_log_setdefaultchannels(lcfg);
}
result = named_log_setdefaultcategory(lcfg);
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
Set up default logging for SSLKEYLOGFILE A customary method of exporting TLS pre-master secrets used by a piece of software (for debugging purposes, e.g. to examine decrypted traffic in a packet sniffer) is to set the SSLKEYLOGFILE environment variable to the path to the file in which this data should be logged. In order to enable writing any data to a file using the logging framework provided by libisc, a logging channel needs to be defined and the relevant logging category needs to be associated with it. Since the SSLKEYLOGFILE variable is only expected to contain a path, some defaults for the logging channel need to be assumed. Add a new function, named_log_setdefaultsslkeylogfile(), for setting up those implicit defaults, which are equivalent to the following logging configuration: channel default_sslkeylogfile { file "${SSLKEYLOGFILE}" versions 10 size 100m suffix timestamp; }; category sslkeylog { default_sslkeylogfile; }; This ensures TLS pre-master secrets do not use up more than about 1 GB of disk space, which should be enough to hold debugging data for the most recent 1 million TLS connections. As these values are arguably not universally appropriate for all deployment environments, a way for overriding them needs to exist. Suppress creation of the default logging channel for TLS pre-master secrets when the SSLKEYLOGFILE variable is set to the string "config". This enables providing custom logging configuration for the relevant category via the "logging" stanza. (Note that it would have been simpler to only skip setting up the default logging channel for TLS pre-master secrets if the SSLKEYLOGFILE environment variable is not set at all. However, libisc only logs pre-master secrets if that variable is set. Detecting a "magic" string enables the SSLKEYLOGFILE environment variable to serve as a single control for both enabling TLS pre-master secret collection and potentially also indicating where and how they should be exported.)
2021-12-22 18:17:26 +01:00
named_log_setdefaultsslkeylogfile(lcfg);
return (ISC_R_SUCCESS);
cleanup:
isc_log_destroy(&named_g_lctx);
isc_log_setcontext(NULL);
dns_log_setcontext(NULL);
return (result);
}
void
2020-02-13 14:44:37 -08:00
named_log_setdefaultchannels(isc_logconfig_t *lcfg) {
isc_logdestination_t destination;
1999-10-23 00:32:48 +00:00
/*
* By default, the logging library makes "default_debug" log to
* stderr. In BIND, we want to override this and log to named.run
2009-01-05 23:20:22 +00:00
* instead, unless the -g option was given.
1999-10-23 00:32:48 +00:00
*/
if (!named_g_logstderr) {
destination.file.stream = NULL;
destination.file.name = "named.run";
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
isc_log_createchannel(lcfg, "default_debug", ISC_LOG_TOFILE,
ISC_LOG_DYNAMIC, &destination,
ISC_LOG_PRINTTIME | ISC_LOG_DEBUGONLY);
}
if (named_g_logfile != NULL) {
destination.file.stream = NULL;
destination.file.name = named_g_logfile;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
isc_log_createchannel(lcfg, "default_logfile", ISC_LOG_TOFILE,
ISC_LOG_DYNAMIC, &destination,
ISC_LOG_PRINTTIME |
ISC_LOG_PRINTCATEGORY |
ISC_LOG_PRINTLEVEL);
}
#if ISC_FACILITY != LOG_DAEMON
destination.facility = ISC_FACILITY;
isc_log_createchannel(lcfg, "default_syslog", ISC_LOG_TOSYSLOG,
ISC_LOG_INFO, &destination, 0);
#endif /* if ISC_FACILITY != LOG_DAEMON */
/*
* Set the initial debug level.
*/
isc_log_setdebuglevel(named_g_lctx, named_g_debuglevel);
}
void
2020-02-13 14:44:37 -08:00
named_log_setsafechannels(isc_logconfig_t *lcfg) {
isc_logdestination_t destination;
if (!named_g_logstderr) {
isc_log_createchannel(lcfg, "default_debug", ISC_LOG_TONULL,
ISC_LOG_DYNAMIC, NULL, 0);
/*
* Setting the debug level to zero should get the output
* discarded a bit faster.
*/
isc_log_setdebuglevel(named_g_lctx, 0);
} else {
isc_log_setdebuglevel(named_g_lctx, named_g_debuglevel);
}
if (named_g_logfile != NULL) {
destination.file.stream = NULL;
destination.file.name = named_g_logfile;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
isc_log_createchannel(lcfg, "default_logfile", ISC_LOG_TOFILE,
ISC_LOG_DYNAMIC, &destination,
ISC_LOG_PRINTTIME |
ISC_LOG_PRINTCATEGORY |
ISC_LOG_PRINTLEVEL);
}
#if ISC_FACILITY != LOG_DAEMON
destination.facility = ISC_FACILITY;
isc_log_createchannel(lcfg, "default_syslog", ISC_LOG_TOSYSLOG,
ISC_LOG_INFO, &destination, 0);
#endif /* if ISC_FACILITY != LOG_DAEMON */
}
Set up default logging for SSLKEYLOGFILE A customary method of exporting TLS pre-master secrets used by a piece of software (for debugging purposes, e.g. to examine decrypted traffic in a packet sniffer) is to set the SSLKEYLOGFILE environment variable to the path to the file in which this data should be logged. In order to enable writing any data to a file using the logging framework provided by libisc, a logging channel needs to be defined and the relevant logging category needs to be associated with it. Since the SSLKEYLOGFILE variable is only expected to contain a path, some defaults for the logging channel need to be assumed. Add a new function, named_log_setdefaultsslkeylogfile(), for setting up those implicit defaults, which are equivalent to the following logging configuration: channel default_sslkeylogfile { file "${SSLKEYLOGFILE}" versions 10 size 100m suffix timestamp; }; category sslkeylog { default_sslkeylogfile; }; This ensures TLS pre-master secrets do not use up more than about 1 GB of disk space, which should be enough to hold debugging data for the most recent 1 million TLS connections. As these values are arguably not universally appropriate for all deployment environments, a way for overriding them needs to exist. Suppress creation of the default logging channel for TLS pre-master secrets when the SSLKEYLOGFILE variable is set to the string "config". This enables providing custom logging configuration for the relevant category via the "logging" stanza. (Note that it would have been simpler to only skip setting up the default logging channel for TLS pre-master secrets if the SSLKEYLOGFILE environment variable is not set at all. However, libisc only logs pre-master secrets if that variable is set. Detecting a "magic" string enables the SSLKEYLOGFILE environment variable to serve as a single control for both enabling TLS pre-master secret collection and potentially also indicating where and how they should be exported.)
2021-12-22 18:17:26 +01:00
/*
* If the SSLKEYLOGFILE environment variable is set, TLS pre-master secrets are
* logged (for debugging purposes) to the file whose path is provided in that
* variable. Set up a default logging channel which maintains up to 10 files
* containing TLS pre-master secrets, each up to 100 MB in size. If the
* SSLKEYLOGFILE environment variable is set to the string "config", suppress
* creation of the default channel, allowing custom logging channel
* configuration for TLS pre-master secrets to be provided via the "logging"
* stanza in the configuration file.
*/
void
named_log_setdefaultsslkeylogfile(isc_logconfig_t *lcfg) {
const char *sslkeylogfile_path = getenv("SSLKEYLOGFILE");
isc_logdestination_t destination = {
.file = {
.name = sslkeylogfile_path,
.versions = 10,
.suffix = isc_log_rollsuffix_timestamp,
.maximum_size = 100 * 1024 * 1024,
},
};
isc_result_t result;
if (sslkeylogfile_path == NULL ||
strcmp(sslkeylogfile_path, "config") == 0) {
return;
}
isc_log_createchannel(lcfg, "default_sslkeylogfile", ISC_LOG_TOFILE,
ISC_LOG_INFO, &destination, 0);
result = isc_log_usechannel(lcfg, "default_sslkeylogfile",
ISC_LOGCATEGORY_SSLKEYLOG, NULL);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
}
isc_result_t
2020-02-13 14:44:37 -08:00
named_log_setdefaultcategory(isc_logconfig_t *lcfg) {
isc_result_t result = ISC_R_SUCCESS;
result = isc_log_usechannel(lcfg, "default_debug",
ISC_LOGCATEGORY_DEFAULT, NULL);
if (result != ISC_R_SUCCESS) {
1999-10-22 19:33:40 +00:00
goto cleanup;
}
1999-10-23 00:32:48 +00:00
if (!named_g_logstderr) {
if (named_g_logfile != NULL) {
result = isc_log_usechannel(lcfg, "default_logfile",
ISC_LOGCATEGORY_DEFAULT,
NULL);
} else if (!named_g_nosyslog) {
result = isc_log_usechannel(lcfg, "default_syslog",
ISC_LOGCATEGORY_DEFAULT,
NULL);
}
}
1999-10-22 19:33:40 +00:00
cleanup:
1999-10-22 19:33:40 +00:00
return (result);
}
isc_result_t
2020-02-13 14:44:37 -08:00
named_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
isc_result_t result;
result = isc_log_usechannel(lcfg, "null", NAMED_LOGCATEGORY_UNMATCHED,
NULL);
return (result);
}
1999-10-22 19:33:40 +00:00
void
2020-02-13 14:44:37 -08:00
named_log_shutdown(void) {
isc_log_destroy(&named_g_lctx);
isc_log_setcontext(NULL);
dns_log_setcontext(NULL);
1999-10-22 19:33:40 +00:00
}