xdm_syslog.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) 2006, Novell, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are 
00007  * met:
00008  * 
00009  *     * Redistributions of source code must retain the above copyright 
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright 
00012  *       notice, this list of conditions and the following disclaimer in the 
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Novell nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this 
00016  *       software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *--------------------------------------------------------------------------*/
00030 
00047 #include <stddef.h>
00048 
00049 #ifdef _WIN32
00050 # define WIN32_LEAN_AND_MEAN
00051 # include <windows.h>
00052 # include <tchar.h>
00053 # include <stdlib.h>
00054 # include <string.h>
00056 static HANDLE s_elhandle = 0;
00057 # define XDMAPI __cdecl
00058 # define XDMEXP __declspec(dllexport)
00059 #else
00060 # include <syslog.h>
00061 # define XDMAPI
00062 # define XDMEXP
00063 #endif
00064 
00066 static void (*s_fplogmsg)(int level, const char * msg, ... ) = 0;
00067 
00068 #ifdef _WIN32
00069 
00071 static void EnsureRegistryKeys(
00072       char * (*getcnfstr)(const char *, char *, size_t *))
00073 {
00074    char msgpath[5 * MAX_PATH];
00075    wchar_t wmsgpath[MAX_PATH] = {0};
00076    wchar_t * keypath = _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\OpenXDAS");
00077    DWORD dwData, dwDisp;
00078    size_t msgpathsz;
00079    HKEY hk; 
00080 
00081 /* HKEY_LOCAL_MACHINE
00082       SYSTEM
00083          CurrentControlSet
00084             Services
00085                EventLog
00086                   Application
00087                      OpenXDAS */
00088 
00089    // Create the event source as a subkey of the log. 
00090    if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, keypath, 0, 0, 
00091          REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hk, &dwDisp) != ERROR_SUCCESS)
00092    {
00093       (*s_fplogmsg)(0, "syslog: Unable to create registry keys.\n");
00094       return;
00095    }
00096    if (dwDisp == REG_OPENED_EXISTING_KEY)
00097    {
00098       // key already existed - no need to recreate everything.
00099       (*s_fplogmsg)(0, "syslog: Windows Event Service keys already in place.\n");
00100       RegCloseKey(hk);
00101       return;
00102    }
00103 
00104    (*s_fplogmsg)(0, "syslog: First run. Adding system event log registry keys.\n");
00105 
00106    // Get the syslog logger module's path name for use as the message file
00107    msgpathsz = sizeof(msgpath);
00108    if ((*getcnfstr)("xdasd.loggers", msgpath, &msgpathsz) != 0)
00109    {
00110       char * cp;
00111       if ((cp = strstr(msgpath, "xdm_syslog.dll")) != 0)
00112       {
00113          cp[14] = 0; // terminate string after module name
00114          if ((cp = strrchr(cp, ',')) == 0)
00115             cp = msgpath - 1;
00116          cp++; // increment to start of module full path
00117       }
00118       if (cp && *cp) // convert to wide character string for Windows
00119          mbstowcs(wmsgpath, msgpath, sizeof(wmsgpath)/sizeof(wchar_t));
00120    }
00121    if (!*wmsgpath)
00122    {
00123       wcscpy(wmsgpath, _T("C:\\Program Files\\OpenXDAS\\Loggers\\xdm_syslog.dll"));
00124       (*s_fplogmsg)(0, "syslog: Unable to determine module path. Assuming default.\n");
00125    }
00126 
00127    // Set message file name - messages are stored in xdm_syslog.dll
00128    if (RegSetValueEx(hk, _T("EventMessageFile"), 0, REG_EXPAND_SZ, 
00129          (LPBYTE)wmsgpath, (DWORD)(wcslen(wmsgpath) + 1) * sizeof(wchar_t)) != ERROR_SUCCESS)
00130       (*s_fplogmsg)(0, "syslog: Unable to set event message file name value.\n");
00131    else
00132    {
00133       // Set the supported event types - only INFORMATION right now. 
00134       dwData = EVENTLOG_AUDIT_SUCCESS | EVENTLOG_AUDIT_FAILURE;
00135       if (RegSetValueEx(hk, _T("TypesSupported"), 0, REG_DWORD, 
00136             (LPBYTE)&dwData, sizeof(DWORD)) != ERROR_SUCCESS)
00137          (*s_fplogmsg)(0, "syslog: Unable to set the supported types.\n"); 
00138    }
00139    RegCloseKey(hk); 
00140 }
00141 #endif
00142 
00153 XDMEXP int XDMAPI xdm_append(const char ** msgflds)
00154 {
00155 #ifdef _WIN32
00156    // msgflds[9] is the outcome code in hexadecimal
00157    unsigned outcome = strtoul(msgflds[9], 0, 16);
00158    WORD type = (outcome & 0xFF)? EVENTLOG_AUDIT_FAILURE : EVENTLOG_AUDIT_SUCCESS;
00159    if (!ReportEventA(s_elhandle, type, 0, 0x40000001, 0, 1, 
00160          0, (char **)msgflds, 0))
00161       return -1;
00162 #else
00163    syslog(LOG_INFO, "%.*s", msgflds[33] - msgflds[0] - 2, msgflds[0]);
00164 #endif
00165    return 0;
00166 }
00167 
00174 XDMEXP int XDMAPI xdm_init(void (*logmsg)(int level, const char * msg, ... ),
00175       char * (*getcnfstr)(const char *, char *, size_t *))
00176 {
00177    (void)getcnfstr;     /* no configuration data to be read */
00178 
00179    s_fplogmsg = logmsg; /* save logmsg function pointer */
00180 
00181 #ifdef _WIN32
00182    EnsureRegistryKeys(getcnfstr);
00183    if ((s_elhandle = RegisterEventSource(0, _T("OpenXDAS"))) == 0)
00184       return -1;
00185 #else
00186    openlog("xdas", 0, LOG_AUTHPRIV);
00187 #endif
00188    return 0;
00189 }
00190 
00195 XDMEXP void XDMAPI xdm_exit(void)
00196 {
00197 #ifdef _WIN32
00198    DeregisterEventSource(s_elhandle);
00199 #else
00200    closelog();
00201 #endif
00202 }
00203 

Generated on Thu Aug 20 22:33:05 2009 for OpenXDAS by  doxygen 1.5.6