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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

363 lines
9.5 KiB
C
Raw Permalink Normal View History

2000-02-28 18:38:44 +00:00
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
2000-02-28 18:38:44 +00:00
*
* SPDX-License-Identifier: MPL-2.0
*
2000-02-28 18:38:44 +00:00
* 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/.
*
2000-02-28 18:38:44 +00:00
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/*! \file */
2000-06-22 22:00:42 +00:00
#include <inttypes.h>
#include <stdbool.h>
2011-03-05 03:03:57 +00:00
#include <isc/file.h>
2000-08-25 01:08:20 +00:00
#include <isc/result.h>
#include <isc/stdio.h>
#include <isc/string.h>
#include <isc/syslog.h>
#include <isc/util.h>
#include <isccfg/cfg.h>
2000-02-28 18:38:44 +00:00
#include <named/log.h>
#include <named/logconf.h>
#define CHECK(op) \
do { \
result = (op); \
if (result != ISC_R_SUCCESS) \
goto cleanup; \
} while (0)
/*%
2000-02-28 18:38:44 +00:00
* Set up a logging category according to the named.conf data
* in 'ccat' and add it to 'logconfig'.
2000-02-28 18:38:44 +00:00
*/
static isc_result_t
category_fromconf(const cfg_obj_t *ccat, isc_logconfig_t *logconfig) {
2000-02-28 18:38:44 +00:00
isc_result_t result;
const char *catname;
isc_logcategory_t category;
const cfg_obj_t *destinations = NULL;
2000-02-28 18:38:44 +00:00
catname = cfg_obj_asstring(cfg_tuple_get(ccat, "name"));
category = isc_log_categorybyname(catname);
if (category == ISC_LOGCATEGORY_INVALID) {
cfg_obj_log(ccat, ISC_LOG_ERROR,
"unknown logging category '%s' ignored", catname);
/*
* Allow further processing by returning success.
*/
return ISC_R_SUCCESS;
}
if (logconfig == NULL) {
return ISC_R_SUCCESS;
}
destinations = cfg_tuple_get(ccat, "destinations");
CFG_LIST_FOREACH(destinations, element) {
const cfg_obj_t *channel = cfg_listelt_value(element);
const char *channelname = cfg_obj_asstring(channel);
result = isc_log_usechannel(logconfig, channelname, category,
ISC_LOGMODULE_DEFAULT);
2000-03-01 20:40:33 +00:00
if (result != ISC_R_SUCCESS) {
isc_log_write(CFG_LOGCATEGORY_CONFIG,
NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,
"logging channel '%s': %s", channelname,
2000-03-01 20:40:33 +00:00
isc_result_totext(result));
2000-02-28 18:38:44 +00:00
return result;
2000-03-01 20:40:33 +00:00
}
2000-02-28 18:38:44 +00:00
}
return ISC_R_SUCCESS;
}
/*%
2000-02-28 18:38:44 +00:00
* Set up a logging channel according to the named.conf data
* in 'cchan' and add it to 'logconfig'.
2000-02-28 18:38:44 +00:00
*/
static isc_result_t
channel_fromconf(const cfg_obj_t *channel, isc_logconfig_t *logconfig) {
isc_result_t result = ISC_R_SUCCESS;
2000-02-28 18:38:44 +00:00
isc_logdestination_t dest;
unsigned int type;
unsigned int flags = 0;
2000-02-28 18:38:44 +00:00
int level;
const char *channelname;
const cfg_obj_t *fileobj = NULL;
const cfg_obj_t *syslogobj = NULL;
const cfg_obj_t *nullobj = NULL;
const cfg_obj_t *stderrobj = NULL;
const cfg_obj_t *severity = NULL;
int i;
channelname = cfg_obj_asstring(cfg_map_getname(channel));
(void)cfg_map_get(channel, "file", &fileobj);
(void)cfg_map_get(channel, "syslog", &syslogobj);
(void)cfg_map_get(channel, "null", &nullobj);
(void)cfg_map_get(channel, "stderr", &stderrobj);
i = 0;
if (fileobj != NULL) {
i++;
}
if (syslogobj != NULL) {
i++;
}
if (nullobj != NULL) {
i++;
}
if (stderrobj != NULL) {
i++;
}
if (i != 1) {
cfg_obj_log(channel, ISC_LOG_ERROR,
"channel '%s': exactly one of file, syslog, "
"null, and stderr must be present",
channelname);
return ISC_R_FAILURE;
}
2000-02-28 19:18:13 +00:00
type = ISC_LOG_TONULL;
2011-03-05 23:52:31 +00:00
if (fileobj != NULL) {
const cfg_obj_t *pathobj = cfg_tuple_get(fileobj, "file");
const cfg_obj_t *sizeobj = cfg_tuple_get(fileobj, "size");
const cfg_obj_t *versionsobj = cfg_tuple_get(fileobj,
"versions");
const cfg_obj_t *suffixobj = cfg_tuple_get(fileobj, "suffix");
int32_t versions = ISC_LOG_ROLLNEVER;
isc_log_rollsuffix_t suffix = isc_log_rollsuffix_increment;
off_t size = 0;
uint64_t maxoffset;
2016-07-13 13:54:12 +05:30
/*
* off_t is a signed integer type, so the maximum
2016-07-13 13:54:12 +05:30
* value is all 1s except for the MSB.
*/
switch (sizeof(off_t)) {
2016-07-13 13:54:12 +05:30
case 4:
maxoffset = 0x7fffffffULL;
2016-07-13 13:54:12 +05:30
break;
case 8:
maxoffset = 0x7fffffffffffffffULL;
2016-07-13 13:54:12 +05:30
break;
default:
UNREACHABLE();
2016-07-13 13:54:12 +05:30
}
2000-02-28 18:38:44 +00:00
type = ISC_LOG_TOFILE;
2011-03-05 23:52:31 +00:00
if (versionsobj != NULL && cfg_obj_isuint32(versionsobj)) {
versions = cfg_obj_asuint32(versionsobj);
} else if (versionsobj != NULL &&
cfg_obj_isstring(versionsobj) &&
strcasecmp(cfg_obj_asstring(versionsobj),
"unlimited") == 0)
{
versions = ISC_LOG_ROLLINFINITE;
}
if (sizeobj != NULL && cfg_obj_isuint64(sizeobj) &&
2016-07-13 13:54:12 +05:30
cfg_obj_asuint64(sizeobj) < maxoffset)
{
size = (off_t)cfg_obj_asuint64(sizeobj);
}
if (suffixobj != NULL && cfg_obj_isstring(suffixobj) &&
strcasecmp(cfg_obj_asstring(suffixobj), "timestamp") == 0)
{
suffix = isc_log_rollsuffix_timestamp;
}
dest.file.stream = NULL;
dest.file.name = cfg_obj_asstring(pathobj);
dest.file.versions = versions;
dest.file.suffix = suffix;
dest.file.maximum_size = size;
} else if (syslogobj != NULL) {
int facility = LOG_DAEMON;
2000-02-28 18:38:44 +00:00
type = ISC_LOG_TOSYSLOG;
if (cfg_obj_isstring(syslogobj)) {
const char *facilitystr = cfg_obj_asstring(syslogobj);
(void)isc_syslog_facilityfromstring(facilitystr,
&facility);
2000-06-22 22:30:12 +00:00
}
dest.facility = facility;
} else if (stderrobj != NULL) {
type = ISC_LOG_TOFILEDESC;
dest.file.stream = stderr;
dest.file.name = NULL;
dest.file.versions = ISC_LOG_ROLLNEVER;
dest.file.suffix = isc_log_rollsuffix_increment;
dest.file.maximum_size = 0;
2000-02-28 18:38:44 +00:00
}
/*
* Munge flags.
*/
{
const cfg_obj_t *printcat = NULL;
const cfg_obj_t *printsev = NULL;
const cfg_obj_t *printtime = NULL;
const cfg_obj_t *buffered = NULL;
2000-02-28 18:38:44 +00:00
(void)cfg_map_get(channel, "print-category", &printcat);
(void)cfg_map_get(channel, "print-severity", &printsev);
(void)cfg_map_get(channel, "print-time", &printtime);
(void)cfg_map_get(channel, "buffered", &buffered);
2000-02-28 18:38:44 +00:00
if (printcat != NULL && cfg_obj_asboolean(printcat)) {
2000-02-28 18:38:44 +00:00
flags |= ISC_LOG_PRINTCATEGORY;
}
if (printsev != NULL && cfg_obj_asboolean(printsev)) {
2000-02-28 18:38:44 +00:00
flags |= ISC_LOG_PRINTLEVEL;
}
if (buffered != NULL && cfg_obj_asboolean(buffered)) {
flags |= ISC_LOG_BUFFERED;
}
if (printtime != NULL && cfg_obj_isboolean(printtime)) {
if (cfg_obj_asboolean(printtime)) {
flags |= ISC_LOG_PRINTTIME;
}
} else if (printtime != NULL) { /* local/iso8601/iso8601-utc */
const char *s = cfg_obj_asstring(printtime);
flags |= ISC_LOG_PRINTTIME;
if (strcasecmp(s, "iso8601") == 0) {
flags |= ISC_LOG_ISO8601;
} else if (strcasecmp(s, "iso8601-utc") == 0) {
flags |= ISC_LOG_ISO8601 | ISC_LOG_UTC;
} else if (strcasecmp(s, "iso8601-tzinfo") == 0) {
flags |= ISC_LOG_ISO8601 | ISC_LOG_TZINFO;
}
}
2000-02-28 18:38:44 +00:00
}
2000-02-28 18:38:44 +00:00
level = ISC_LOG_INFO;
if (cfg_map_get(channel, "severity", &severity) == ISC_R_SUCCESS) {
if (cfg_obj_isstring(severity)) {
const char *str = cfg_obj_asstring(severity);
if (strcasecmp(str, "critical") == 0) {
level = ISC_LOG_CRITICAL;
} else if (strcasecmp(str, "error") == 0) {
level = ISC_LOG_ERROR;
} else if (strcasecmp(str, "warning") == 0) {
level = ISC_LOG_WARNING;
} else if (strcasecmp(str, "notice") == 0) {
level = ISC_LOG_NOTICE;
} else if (strcasecmp(str, "info") == 0) {
level = ISC_LOG_INFO;
} else if (strcasecmp(str, "dynamic") == 0) {
level = ISC_LOG_DYNAMIC;
}
} else {
/* debug */
level = cfg_obj_asuint32(severity);
}
}
if (logconfig != NULL) {
isc_log_createchannel(logconfig, channelname, type, level,
&dest, flags);
}
if (type == ISC_LOG_TOFILE) {
FILE *fp;
2011-03-05 23:52:31 +00:00
/*
* Test to make sure that file is a plain file.
* Fix defect #22771
*/
2011-03-05 23:52:31 +00:00
result = isc_file_isplainfile(dest.file.name);
if (result == ISC_R_SUCCESS || result == ISC_R_FILENOTFOUND) {
/*
* Test that the file can be opened, since
2011-03-05 23:52:31 +00:00
* isc_log_open() can't effectively report
* failures when called in isc_log_doit().
*/
result = isc_stdio_open(dest.file.name, "a", &fp);
if (result != ISC_R_SUCCESS) {
if (logconfig != NULL && !named_g_nosyslog) {
syslog(LOG_ERR,
"isc_stdio_open '%s' failed: "
"%s",
dest.file.name,
isc_result_totext(result));
}
} else {
(void)isc_stdio_close(fp);
}
goto done;
}
if (logconfig != NULL && !named_g_nosyslog) {
syslog(LOG_ERR, "isc_file_isplainfile '%s' failed: %s",
dest.file.name, isc_result_totext(result));
}
}
done:
2000-02-28 18:38:44 +00:00
return result;
}
isc_result_t
named_logconfig(isc_logconfig_t *logconfig, const cfg_obj_t *logstmt) {
2000-02-28 18:38:44 +00:00
isc_result_t result;
const cfg_obj_t *channels = NULL;
const cfg_obj_t *categories = NULL;
bool default_set = false;
bool unmatched_set = false;
const cfg_obj_t *catname = NULL;
2000-02-28 18:38:44 +00:00
if (logconfig != NULL) {
named_log_setdefaultchannels(logconfig);
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(logconfig);
}
(void)cfg_map_get(logstmt, "channel", &channels);
CFG_LIST_FOREACH(channels, element) {
const cfg_obj_t *channel = cfg_listelt_value(element);
CHECK(channel_fromconf(channel, logconfig));
2000-02-28 18:38:44 +00:00
}
(void)cfg_map_get(logstmt, "category", &categories);
CFG_LIST_FOREACH(categories, element) {
const cfg_obj_t *category = cfg_listelt_value(element);
CHECK(category_fromconf(category, logconfig));
if (!default_set) {
catname = cfg_tuple_get(category, "name");
if (strcmp(cfg_obj_asstring(catname), "default") == 0) {
default_set = true;
}
}
if (!unmatched_set) {
catname = cfg_tuple_get(category, "name");
if (strcmp(cfg_obj_asstring(catname), "unmatched") == 0)
{
unmatched_set = true;
}
}
2000-02-28 18:38:44 +00:00
}
if (logconfig != NULL && !default_set) {
CHECK(named_log_setdefaultcategory(logconfig));
}
if (logconfig != NULL && !unmatched_set) {
CHECK(named_log_setunmatchedcategory(logconfig));
}
2000-02-28 18:38:44 +00:00
return ISC_R_SUCCESS;
cleanup:
return result;
}