summaryrefslogtreecommitdiffstats
path: root/bin/named/log.c
blob: 3aa25e9a95b38fa95d9eb82abe5fc5103dfb9150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * 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 http://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*! \file */

#include <config.h>

#include <isc/result.h>

#include <isccfg/log.h>

#include <named/log.h>

#ifndef ISC_FACILITY
#define ISC_FACILITY LOG_DAEMON
#endif

/*%
 * 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.
 */
static isc_logcategory_t categories[] = {
	{ "",		 		0 },
	{ "client",	 		0 },
	{ "network",	 		0 },
	{ "update",	 		0 },
	{ "queries",	 		0 },
	{ "unmatched",	 		0 },
	{ "update-security",		0 },
	{ "query-errors",		0 },
	{ "trust-anchor-telemetry",	0 },
	{ NULL, 			0 }
};

/*%
 * When adding a new module, be sure to add the appropriate
 * \#define to <dns/log.h>.
 */
static isc_logmodule_t modules[] = {
	{ "main",	 		0 },
	{ "client",	 		0 },
	{ "server",		 	0 },
	{ "query",		 	0 },
	{ "interfacemgr",	 	0 },
	{ "update",	 		0 },
	{ "xfer-in",	 		0 },
	{ "xfer-out",	 		0 },
	{ "notify",	 		0 },
	{ "control",	 		0 },
	{ "lwresd",	 		0 },
	{ NULL, 			0 }
};

isc_result_t
ns_log_init(bool safe) {
	isc_result_t result;
	isc_logconfig_t *lcfg = NULL;

	ns_g_categories = categories;
	ns_g_modules = modules;

	/*
	 * Setup a logging context.
	 */
	result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
	if (result != ISC_R_SUCCESS)
		return (result);

	/*
	 * named-checktool.c:setup_logging() needs to be kept in sync.
	 */
	isc_log_registercategories(ns_g_lctx, ns_g_categories);
	isc_log_registermodules(ns_g_lctx, ns_g_modules);
	isc_log_setcontext(ns_g_lctx);
	dns_log_init(ns_g_lctx);
	dns_log_setcontext(ns_g_lctx);
	cfg_log_init(ns_g_lctx);

	if (safe)
		result = ns_log_setsafechannels(lcfg);
	else
		result = ns_log_setdefaultchannels(lcfg);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	result = ns_log_setdefaultcategory(lcfg);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	return (ISC_R_SUCCESS);

 cleanup:
	isc_log_destroy(&ns_g_lctx);
	isc_log_setcontext(NULL);
	dns_log_setcontext(NULL);

	return (result);
}

isc_result_t
ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
	isc_result_t result;
	isc_logdestination_t destination;

	/*
	 * By default, the logging library makes "default_debug" log to
	 * stderr.  In BIND, we want to override this and log to named.run
	 * instead, unless the -g option was given.
	 */
	if (! ns_g_logstderr) {
		destination.file.stream = NULL;
		destination.file.name = "named.run";
		destination.file.versions = ISC_LOG_ROLLNEVER;
		destination.file.maximum_size = 0;
		result = isc_log_createchannel(lcfg, "default_debug",
					       ISC_LOG_TOFILE,
					       ISC_LOG_DYNAMIC,
					       &destination,
					       ISC_LOG_PRINTTIME|
					       ISC_LOG_DEBUGONLY);
		if (result != ISC_R_SUCCESS)
			goto cleanup;
	}

	if (ns_g_logfile != NULL) {
		destination.file.stream = NULL;
		destination.file.name = ns_g_logfile;
		destination.file.versions = ISC_LOG_ROLLNEVER;
		destination.file.maximum_size = 0;
		result = isc_log_createchannel(lcfg, "default_logfile",
					       ISC_LOG_TOFILE,
					       ISC_LOG_DYNAMIC,
					       &destination,
					       ISC_LOG_PRINTTIME|
					       ISC_LOG_PRINTCATEGORY|
					       ISC_LOG_PRINTLEVEL);
		if (result != ISC_R_SUCCESS)
			goto cleanup;
	}

#if ISC_FACILITY != LOG_DAEMON
	destination.facility = ISC_FACILITY;
	result = isc_log_createchannel(lcfg, "default_syslog",
				       ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
				       &destination, 0);
	if (result != ISC_R_SUCCESS)
		goto cleanup;
#endif

	/*
	 * Set the initial debug level.
	 */
	isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);

	result = ISC_R_SUCCESS;

 cleanup:
	return (result);
}

isc_result_t
ns_log_setsafechannels(isc_logconfig_t *lcfg) {
	isc_result_t result;
	isc_logdestination_t destination;

	if (! ns_g_logstderr) {
		result = isc_log_createchannel(lcfg, "default_debug",
					       ISC_LOG_TONULL,
					       ISC_LOG_DYNAMIC,
					       NULL, 0);
		if (result != ISC_R_SUCCESS)
			goto cleanup;

		/*
		 * Setting the debug level to zero should get the output
		 * discarded a bit faster.
		 */
		isc_log_setdebuglevel(ns_g_lctx, 0);
	} else {
		isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
	}

	if (ns_g_logfile != NULL) {
		destination.file.stream = NULL;
		destination.file.name = ns_g_logfile;
		destination.file.versions = ISC_LOG_ROLLNEVER;
		destination.file.maximum_size = 0;
		result = isc_log_createchannel(lcfg, "default_logfile",
					       ISC_LOG_TOFILE,
					       ISC_LOG_DYNAMIC,
					       &destination,
					       ISC_LOG_PRINTTIME|
					       ISC_LOG_PRINTCATEGORY|
					       ISC_LOG_PRINTLEVEL);
		if (result != ISC_R_SUCCESS)
			goto cleanup;
	}

#if ISC_FACILITY != LOG_DAEMON
	destination.facility = ISC_FACILITY;
	result = isc_log_createchannel(lcfg, "default_syslog",
				       ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
				       &destination, 0);
	if (result != ISC_R_SUCCESS)
		goto cleanup;
#endif

	result = ISC_R_SUCCESS;

 cleanup:
	return (result);
}

isc_result_t
ns_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)
		goto cleanup;

	if (! ns_g_logstderr) {
		if (ns_g_logfile != NULL)
			result = isc_log_usechannel(lcfg, "default_logfile",
						    ISC_LOGCATEGORY_DEFAULT,
						    NULL);
		else if (! ns_g_nosyslog)
			result = isc_log_usechannel(lcfg, "default_syslog",
						    ISC_LOGCATEGORY_DEFAULT,
						    NULL);
	}

 cleanup:
	return (result);
}

isc_result_t
ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
	isc_result_t result;

	result = isc_log_usechannel(lcfg, "null",
				    NS_LOGCATEGORY_UNMATCHED, NULL);
	return (result);
}

void
ns_log_shutdown(void) {
	isc_log_destroy(&ns_g_lctx);
	isc_log_setcontext(NULL);
	dns_log_setcontext(NULL);
}