00001
00010
00011
00012 #include "EventLog.h"
00013 #include "PKIFLog.h"
00014 #include "PKIFException.h"
00015 #include "components.h"
00016 #include "PKIFCommonErrors.h"
00017
00018 #include "boost/numeric/conversion/cast.hpp"
00019
00020 using boost::numeric_cast;
00021 using boost::bad_numeric_cast;
00022
00030 CEventLog::CEventLog()
00031 {
00032 m_bSourceAdded = false;
00033 }
00041 CEventLog::~CEventLog()
00042 {
00043 }
00044
00045 bool CEventLog::m_bSourceAdded = false;
00046
00047 #ifdef _DEBUG
00048 char* CEventLog::m_cstrEventSource = "PKIFd";
00049 #else
00050 char* CEventLog::m_cstrEventSource = "PKIF";
00051 #endif
00052
00059 bool CEventLog::AddEventSource()
00060 {
00061
00062
00063 HKEY hk = NULL;
00064 DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
00065
00066
00067 char regKeyName[MAX_PATH] = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
00068
00069
00070 strcat(regKeyName,m_cstrEventSource);
00071 if (RegCreateKey(HKEY_LOCAL_MACHINE, regKeyName, &hk))
00072 {
00073 OutputDebugString( "\nCould not create the logging registry key.");
00074 return false;
00075 }
00076
00077
00078 #ifdef _DEBUG
00079 HINSTANCE hInstance = GetModuleHandle("PKIFd");
00080 if(!hInstance)
00081 {
00082 OutputDebugString( "\nCould not get module handle");
00083 return false;
00084 }
00085 #else
00086 HINSTANCE hInstance = GetModuleHandle("PKIF");
00087 if(!hInstance)
00088 {
00089 OutputDebugString( "\nCould not get module handle");
00090 return false;
00091 }
00092 #endif
00093 char szBuffer[MAX_PATH];
00094 if (!GetModuleFileName(hInstance,szBuffer,sizeof(szBuffer)))
00095 {
00096 OutputDebugString( "\nCould not get module filename");
00097 return false;
00098 }
00099 std::string cstrBuf = szBuffer;
00100
00101 const char* pCstrBuf = cstrBuf.c_str();
00102
00103
00104
00105 DWORD pCstrBufLen = 0;
00106 try
00107 {
00108 pCstrBufLen = numeric_cast<DWORD>(cstrBuf.size());
00109 }
00110 catch(bad_numeric_cast &)
00111 {
00112 throw CPKIFException(TOOLKIT_PATH, COMMON_INVALID_INPUT, "Buffer size is an impossibly long number.");
00113 }
00114
00115
00116 if (RegSetValueEx(hk,
00117 "EventMessageFile",
00118 0,
00119 REG_EXPAND_SZ,
00120 (BYTE*)pCstrBuf,
00121 pCstrBufLen + 1))
00122 {
00123
00124 OutputDebugString( "\nCould not set the event message file.");
00125 return false;
00126 }
00127
00128 if (RegSetValueEx(hk,
00129 "TypesSupported",
00130 0,
00131 REG_DWORD,
00132 (LPBYTE) &dwData,
00133 sizeof(DWORD)))
00134 {
00135 OutputDebugString( "\nCould not set the supported types.");
00136 return false;
00137 }
00138
00139 dwData = 7;
00140 if (RegSetValueEx(hk,
00141 "CategoryCount",
00142 0,
00143 REG_DWORD,
00144 (LPBYTE) &dwData,
00145 sizeof(DWORD)))
00146 {
00147 OutputDebugString( "\nCould not set the supported types.");
00148 return false;
00149 }
00150
00151 if (RegSetValueEx(hk,
00152 "CategoryMessageFile",
00153 0,
00154 REG_EXPAND_SZ,
00155 (BYTE*)pCstrBuf,
00156 pCstrBufLen + 1))
00157 {
00158
00159 OutputDebugString( "\nCould not set the event message file.");
00160 return false;
00161 }
00162
00163 RegCloseKey(hk);
00164 m_bSourceAdded = true;
00165 return true;
00166
00167 m_bSourceAdded = false;
00168 return false;
00169 }
00177 DWORD GetUserSid(PSID& sid)
00178 {
00179 int sts;
00180 DWORD len, sidLen;
00181 TCHAR *refDomain, *userName;
00182 SID_NAME_USE sidNameUse;
00183 unsigned long userLen = 128;
00184
00185
00186
00187
00188
00189 userName = ( TCHAR*) new TCHAR[userLen];
00190 if(!GetUserName(userName,&userLen)) {
00191 delete [] userName;
00192 return GetLastError();
00193 }
00194
00195
00196 sidLen = len = 0;
00197 sts = LookupAccountName(NULL,
00198 userName,
00199 NULL,
00200 &sidLen,
00201 NULL,
00202 &len,
00203 &sidNameUse);
00204
00205
00206 sid = (void*) new char[sidLen];
00207 refDomain = (TCHAR*) new TCHAR[len];
00208
00209
00210 sts = LookupAccountName(NULL,
00211 userName,
00212 sid,
00213 &sidLen,
00214 refDomain,
00215 &len,
00216 &sidNameUse);
00217
00218 if (!sts) {
00219 int error = GetLastError();
00220 if (!error) error = ERROR_NO_NETWORK;
00221 delete [] sid;
00222 delete [] refDomain;
00223 delete [] userName;
00224 return error;
00225 }
00226
00227 delete [] refDomain;
00228 delete [] userName;
00229 return ERROR_SUCCESS;
00230 }
00238 void CEventLog::Write(
00240 int intEventType,
00242 int intEventCat,
00244 int eventID,
00246 const char* cstrMessage)
00247 {
00248 HANDLE h;
00249 if( m_bSourceAdded == false )
00250 CEventLog::AddEventSource();
00251
00252 h = RegisterEventSource(NULL,
00253 m_cstrEventSource);
00254 if (h == NULL)
00255 {
00256 OutputDebugString( "\nCould not register the event source.");
00257 return;
00258 }
00259
00260 PSID sid = NULL;
00261 if(PKIF_UNLOAD != eventID && PKIF_LOAD != eventID)
00262 {
00263 GetUserSid(sid);
00264 }
00265
00266
00267 int err = 0;
00268
00269 if (!ReportEvent(h,
00270 intEventType,
00271 intEventCat,
00272 eventID,
00273 sid,
00274 1,
00275 0,
00276 &cstrMessage,
00277 NULL))
00278 {
00279 OutputDebugString("\nCould not report the event.");
00280 err = GetLastError();
00281
00282 }
00283 if(sid) delete[] sid;
00284 DeregisterEventSource(h);
00285 }