00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
00082
00083
00084
00085
00086
00087
00088
00089
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
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
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;
00114 if ((cp = strrchr(cp, ',')) == 0)
00115 cp = msgpath - 1;
00116 cp++;
00117 }
00118 if (cp && *cp)
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
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
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
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;
00178
00179 s_fplogmsg = logmsg;
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