1258945Sroberto/*
2258945Sroberto * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 2001-2003  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18258945Sroberto/* $Id: syslog.c,v 1.10 2007/06/19 23:47:19 tbox Exp $ */
19258945Sroberto
20258945Sroberto#include <config.h>
21258945Sroberto
22258945Sroberto#include <stdio.h>
23258945Sroberto#include <windows.h>
24258945Sroberto#include <string.h>
25258945Sroberto#include <stdlib.h>
26258945Sroberto#include <syslog.h>
27258945Sroberto
28258945Sroberto#include <isc/bindevt.h>
29258945Sroberto#include <isc/result.h>
30258945Sroberto#include <isc/syslog.h>
31258945Sroberto#include <isc/util.h>
32258945Sroberto
33258945Srobertostatic HANDLE hAppLog = NULL;
34258945Srobertostatic FILE *log_stream;
35258945Srobertostatic int debug_level = 0;
36258945Sroberto
37258945Srobertostatic struct dsn_c_pvt_sfnt {
38258945Sroberto	int val;
39258945Sroberto	const char *strval;
40258945Sroberto} facilities[] = {
41258945Sroberto	{ LOG_KERN,		"kern" },
42258945Sroberto	{ LOG_USER,		"user" },
43258945Sroberto	{ LOG_MAIL,		"mail" },
44258945Sroberto	{ LOG_DAEMON,		"daemon" },
45258945Sroberto	{ LOG_AUTH,		"auth" },
46258945Sroberto	{ LOG_SYSLOG,		"syslog" },
47258945Sroberto	{ LOG_LPR,		"lpr" },
48258945Sroberto#ifdef LOG_NEWS
49258945Sroberto	{ LOG_NEWS,		"news" },
50258945Sroberto#endif
51258945Sroberto#ifdef LOG_UUCP
52258945Sroberto	{ LOG_UUCP,		"uucp" },
53258945Sroberto#endif
54258945Sroberto#ifdef LOG_CRON
55258945Sroberto	{ LOG_CRON,		"cron" },
56258945Sroberto#endif
57258945Sroberto#ifdef LOG_AUTHPRIV
58258945Sroberto	{ LOG_AUTHPRIV,		"authpriv" },
59258945Sroberto#endif
60258945Sroberto#ifdef LOG_FTP
61258945Sroberto	{ LOG_FTP,		"ftp" },
62258945Sroberto#endif
63258945Sroberto	{ LOG_LOCAL0,		"local0"},
64258945Sroberto	{ LOG_LOCAL1,		"local1"},
65258945Sroberto	{ LOG_LOCAL2,		"local2"},
66258945Sroberto	{ LOG_LOCAL3,		"local3"},
67258945Sroberto	{ LOG_LOCAL4,		"local4"},
68258945Sroberto	{ LOG_LOCAL5,		"local5"},
69258945Sroberto	{ LOG_LOCAL6,		"local6"},
70258945Sroberto	{ LOG_LOCAL7,		"local7"},
71258945Sroberto	{ 0,			NULL }
72258945Sroberto};
73258945Sroberto
74258945Srobertoisc_result_t
75258945Srobertoisc_syslog_facilityfromstring(const char *str, int *facilityp) {
76258945Sroberto	int i;
77258945Sroberto
78258945Sroberto	REQUIRE(str != NULL);
79258945Sroberto	REQUIRE(facilityp != NULL);
80258945Sroberto
81258945Sroberto	for (i = 0; facilities[i].strval != NULL; i++) {
82258945Sroberto		if (strcasecmp(facilities[i].strval, str) == 0) {
83258945Sroberto			*facilityp = facilities[i].val;
84258945Sroberto			return (ISC_R_SUCCESS);
85258945Sroberto		}
86258945Sroberto	}
87258945Sroberto	return (ISC_R_NOTFOUND);
88258945Sroberto}
89258945Sroberto
90258945Sroberto/*
91258945Sroberto * Log to the NT Event Log
92258945Sroberto */
93258945Srobertovoid
94258945Srobertosyslog(int level, const char *fmt, ...) {
95258945Sroberto	va_list ap;
96258945Sroberto	char buf[1024];
97258945Sroberto	char *str[1];
98258945Sroberto
99258945Sroberto	str[0] = buf;
100258945Sroberto
101258945Sroberto	va_start(ap, fmt);
102258945Sroberto	vsprintf(buf, fmt, ap);
103258945Sroberto	va_end(ap);
104258945Sroberto
105258945Sroberto	/* Make sure that the channel is open to write the event */
106258945Sroberto	if (hAppLog != NULL) {
107258945Sroberto		switch (level) {
108258945Sroberto		case LOG_INFO:
109258945Sroberto		case LOG_NOTICE:
110258945Sroberto		case LOG_DEBUG:
111258945Sroberto			ReportEvent(hAppLog, EVENTLOG_INFORMATION_TYPE, 0,
112258945Sroberto				    BIND_INFO_MSG, NULL, 1, 0, str, NULL);
113258945Sroberto			break;
114258945Sroberto		case LOG_WARNING:
115258945Sroberto			ReportEvent(hAppLog, EVENTLOG_WARNING_TYPE, 0,
116258945Sroberto				    BIND_WARN_MSG, NULL, 1, 0, str, NULL);
117258945Sroberto			break;
118258945Sroberto		default:
119258945Sroberto			ReportEvent(hAppLog, EVENTLOG_ERROR_TYPE, 0,
120258945Sroberto				    BIND_ERR_MSG, NULL, 1, 0, str, NULL);
121258945Sroberto			break;
122258945Sroberto		}
123258945Sroberto	}
124258945Sroberto}
125258945Sroberto
126258945Sroberto/*
127258945Sroberto * Initialize event logging
128258945Sroberto */
129258945Srobertovoid
130258945Srobertoopenlog(const char *name, int flags, ...) {
131258945Sroberto	/* Get a handle to the Application event log */
132258945Sroberto	hAppLog = RegisterEventSource(NULL, name);
133258945Sroberto}
134258945Sroberto
135258945Sroberto/*
136258945Sroberto * Close the Handle to the application Event Log
137258945Sroberto * We don't care whether or not we succeeded so ignore return values
138258945Sroberto * In fact if we failed then we would have nowhere to put the message
139258945Sroberto */
140258945Srobertovoid
141258945Srobertocloselog() {
142258945Sroberto	DeregisterEventSource(hAppLog);
143258945Sroberto}
144258945Sroberto
145258945Sroberto/*
146258945Sroberto * Keep event logging synced with the current debug level
147258945Sroberto */
148258945Srobertovoid
149258945SrobertoModifyLogLevel(int level) {
150258945Sroberto	debug_level = level;
151258945Sroberto}
152258945Sroberto
153258945Sroberto/*
154258945Sroberto * Initialize logging for the port section of libbind.
155258945Sroberto * Piggyback onto stream given.
156258945Sroberto */
157258945Srobertovoid
158258945SrobertoInitNTLogging(FILE *stream, int debug) {
159258945Sroberto	log_stream = stream;
160258945Sroberto	ModifyLogLevel(debug);
161258945Sroberto}
162258945Sroberto/*
163258945Sroberto * This function is for reporting errors to the application
164258945Sroberto * event log in case the regular syslog is not available
165258945Sroberto * mainly during startup. It should not be used under normal
166258945Sroberto * circumstances.
167258945Sroberto */
168258945Srobertovoid
169258945SrobertoNTReportError(const char *name, const char *str) {
170258945Sroberto	HANDLE hNTAppLog = NULL;
171258945Sroberto	const char *buf[1];
172258945Sroberto
173258945Sroberto	buf[0] = str;
174258945Sroberto
175258945Sroberto	hNTAppLog = RegisterEventSource(NULL, name);
176258945Sroberto
177258945Sroberto	ReportEvent(hNTAppLog, EVENTLOG_ERROR_TYPE, 0,
178258945Sroberto		    BIND_ERR_MSG, NULL, 1, 0, buf, NULL);
179258945Sroberto
180258945Sroberto	DeregisterEventSource(hNTAppLog);
181258945Sroberto}
182