1/* 2 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (C) 2001-2003 Internet Software Consortium. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 * PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18/* $Id: syslog.c,v 1.10 2007/06/19 23:47:19 tbox Exp $ */ 19 20#include <config.h> 21 22#include <stdio.h> 23#include <windows.h> 24#include <string.h> 25#include <stdlib.h> 26#include <syslog.h> 27 28#include <isc/bindevt.h> 29#include <isc/result.h> 30#include <isc/syslog.h> 31#include <isc/util.h> 32 33static HANDLE hAppLog = NULL; 34static FILE *log_stream; 35static int debug_level = 0; 36 37static struct dsn_c_pvt_sfnt { 38 int val; 39 const char *strval; 40} facilities[] = { 41 { LOG_KERN, "kern" }, 42 { LOG_USER, "user" }, 43 { LOG_MAIL, "mail" }, 44 { LOG_DAEMON, "daemon" }, 45 { LOG_AUTH, "auth" }, 46 { LOG_SYSLOG, "syslog" }, 47 { LOG_LPR, "lpr" }, 48#ifdef LOG_NEWS 49 { LOG_NEWS, "news" }, 50#endif 51#ifdef LOG_UUCP 52 { LOG_UUCP, "uucp" }, 53#endif 54#ifdef LOG_CRON 55 { LOG_CRON, "cron" }, 56#endif 57#ifdef LOG_AUTHPRIV 58 { LOG_AUTHPRIV, "authpriv" }, 59#endif 60#ifdef LOG_FTP 61 { LOG_FTP, "ftp" }, 62#endif 63 { LOG_LOCAL0, "local0"}, 64 { LOG_LOCAL1, "local1"}, 65 { LOG_LOCAL2, "local2"}, 66 { LOG_LOCAL3, "local3"}, 67 { LOG_LOCAL4, "local4"}, 68 { LOG_LOCAL5, "local5"}, 69 { LOG_LOCAL6, "local6"}, 70 { LOG_LOCAL7, "local7"}, 71 { 0, NULL } 72}; 73 74isc_result_t 75isc_syslog_facilityfromstring(const char *str, int *facilityp) { 76 int i; 77 78 REQUIRE(str != NULL); 79 REQUIRE(facilityp != NULL); 80 81 for (i = 0; facilities[i].strval != NULL; i++) { 82 if (strcasecmp(facilities[i].strval, str) == 0) { 83 *facilityp = facilities[i].val; 84 return (ISC_R_SUCCESS); 85 } 86 } 87 return (ISC_R_NOTFOUND); 88} 89 90/* 91 * Log to the NT Event Log 92 */ 93void 94syslog(int level, const char *fmt, ...) { 95 va_list ap; 96 char buf[1024]; 97 char *str[1]; 98 99 str[0] = buf; 100 101 va_start(ap, fmt); 102 vsprintf(buf, fmt, ap); 103 va_end(ap); 104 105 /* Make sure that the channel is open to write the event */ 106 if (hAppLog != NULL) { 107 switch (level) { 108 case LOG_INFO: 109 case LOG_NOTICE: 110 case LOG_DEBUG: 111 ReportEvent(hAppLog, EVENTLOG_INFORMATION_TYPE, 0, 112 BIND_INFO_MSG, NULL, 1, 0, str, NULL); 113 break; 114 case LOG_WARNING: 115 ReportEvent(hAppLog, EVENTLOG_WARNING_TYPE, 0, 116 BIND_WARN_MSG, NULL, 1, 0, str, NULL); 117 break; 118 default: 119 ReportEvent(hAppLog, EVENTLOG_ERROR_TYPE, 0, 120 BIND_ERR_MSG, NULL, 1, 0, str, NULL); 121 break; 122 } 123 } 124} 125 126/* 127 * Initialize event logging 128 */ 129void 130openlog(const char *name, int flags, ...) { 131 /* Get a handle to the Application event log */ 132 hAppLog = RegisterEventSource(NULL, name); 133} 134 135/* 136 * Close the Handle to the application Event Log 137 * We don't care whether or not we succeeded so ignore return values 138 * In fact if we failed then we would have nowhere to put the message 139 */ 140void 141closelog() { 142 DeregisterEventSource(hAppLog); 143} 144 145/* 146 * Keep event logging synced with the current debug level 147 */ 148void 149ModifyLogLevel(int level) { 150 debug_level = level; 151} 152 153/* 154 * Initialize logging for the port section of libbind. 155 * Piggyback onto stream given. 156 */ 157void 158InitNTLogging(FILE *stream, int debug) { 159 log_stream = stream; 160 ModifyLogLevel(debug); 161} 162/* 163 * This function is for reporting errors to the application 164 * event log in case the regular syslog is not available 165 * mainly during startup. It should not be used under normal 166 * circumstances. 167 */ 168void 169NTReportError(const char *name, const char *str) { 170 HANDLE hNTAppLog = NULL; 171 const char *buf[1]; 172 173 buf[0] = str; 174 175 hNTAppLog = RegisterEventSource(NULL, name); 176 177 ReportEvent(hNTAppLog, EVENTLOG_ERROR_TYPE, 0, 178 BIND_ERR_MSG, NULL, 1, 0, buf, NULL); 179 180 DeregisterEventSource(hNTAppLog); 181} 182