EventLog.cpp

Go to the documentation of this file.
00001 
00010 //#include "atlbase.h"
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     //USES_CONVERSION;
00062 
00063     HKEY hk = NULL; 
00064     DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE; 
00065 
00066     // Add your source name as a subkey under the Application key in the EventLog registry key. 
00067     char regKeyName[MAX_PATH] = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
00068 
00069     //reviewed 4/25 - buffer large enough to accomodate string literal plus PKIF or PKIFd
00070     strcat(regKeyName,m_cstrEventSource);
00071     if (RegCreateKey(HKEY_LOCAL_MACHINE, regKeyName/*A2T(regKeyName)*/, &hk)) 
00072     {
00073         OutputDebugString( "\nCould not create the logging registry key."); 
00074         return false;
00075     }
00076 
00077     // Set the name of the message file.
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     //reviewed 4/25 - size limited to above string literal plus m_cstrEventSource (which is either PKIF or PKIFd)
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     // Add the name to the EventMessageFile subkey. 
00116     if (RegSetValueEx(hk,             // subkey handle 
00117         "EventMessageFile",       // value name 
00118         0,                        // must be zero 
00119         REG_EXPAND_SZ,            // value type 
00120         (BYTE*)pCstrBuf,           // pointer to value data 
00121         pCstrBufLen + 1))           // length of value data 
00122     {
00123 
00124         OutputDebugString( "\nCould not set the event message file."); 
00125         return false;
00126     }
00127 
00128     if (RegSetValueEx(hk,      // subkey handle 
00129         "TypesSupported",  // value name 
00130         0, // must be zero 
00131         REG_DWORD,         // value type 
00132         (LPBYTE) &dwData,  // pointer to value data 
00133         sizeof(DWORD)))    // length of value data 
00134     {
00135         OutputDebugString( "\nCould not set the supported types."); 
00136         return false;
00137     }
00138 
00139     dwData = 7;
00140     if (RegSetValueEx(hk,      // subkey handle 
00141         "CategoryCount",  // value name 
00142         0,  
00143         REG_DWORD,         // value type 
00144         (LPBYTE) &dwData,  // pointer to value data 
00145         sizeof(DWORD)))    // length of value data 
00146     {
00147         OutputDebugString( "\nCould not set the supported types."); 
00148         return false;
00149     }
00150 
00151     if (RegSetValueEx(hk,             // subkey handle 
00152         "CategoryMessageFile",       // value name 
00153         0,                        // must be zero 
00154         REG_EXPAND_SZ,            // value type 
00155         (BYTE*)pCstrBuf,           // pointer to value data 
00156         pCstrBufLen + 1))           // length of value data 
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     // Fetch user name
00186     // if(!m TNetAvail.m bNetAvail)
00187     //  return ERROR NO NETWORK;
00188 
00189     userName = ( TCHAR*) new  TCHAR[userLen];
00190     if(!GetUserName(userName,&userLen)) { 
00191         delete [] userName;
00192         return GetLastError(); 
00193     }
00194 
00195     // Fetch length of SID a user account first.
00196     sidLen = len = 0;
00197     sts = LookupAccountName(NULL,      // Look at local machine
00198         userName,      // User name
00199         NULL,      // Point to SID
00200         &sidLen,     // Length of SID
00201         NULL,      // Ignore Referenced Domain name
00202         &len,      // Ignore Referenced Domain name length
00203         &sidNameUse);    // SID name use
00204 
00205     // Allocate required storage
00206     sid = (void*) new char[sidLen]; 
00207     refDomain = (TCHAR*) new TCHAR[len];
00208 
00209     // Now, with buffers in hand fetch SID for a user account
00210     sts = LookupAccountName(NULL,      // Look at local machine
00211         userName,      // User name
00212         sid,      // Point to SID
00213         &sidLen,     // Length of SID
00214         refDomain,     // Ignore Referenced Domain name
00215         &len,      // Ignore Referenced Domain name length
00216         &sidNameUse);    // SID name use            
00217 
00218     if (!sts) {
00219         int error = GetLastError();
00220         if (!error) error = ERROR_NO_NETWORK; // cover up a Microsoft bug
00221         delete [] sid;
00222         delete [] refDomain;
00223         delete [] userName;
00224         return error;
00225     }
00226 //  delete [] sid;  - moved to CEventLog::Write 11/9/2003
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,  // uses local computer 
00253         m_cstrEventSource);    // source name
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     //  USES_CONVERSION;
00267     int err = 0;
00268     //  LPCSTR lp = A2T( const_cast<char*>(cstrMessage));
00269     if (!ReportEvent(h,           // event log handle 
00270         intEventType,  // event type (info, success, failure, etc)
00271         intEventCat,                    // category 
00272         eventID,        // event identifier 
00273         sid,                 // no user security identifier 
00274         1,                    // one substitution string 
00275         0,                    // no data 
00276         &cstrMessage,         // pointer to string array 
00277         NULL))                // pointer to data 
00278     {
00279         OutputDebugString("\nCould not report the event.");
00280         err = GetLastError();
00281         //      return;
00282     }
00283     if(sid) delete[] sid; //moved to here 11/9/2003
00284     DeregisterEventSource(h); 
00285 }

Generated on Mon Nov 15 11:15:50 2010 for PublicKeyInfrastructureFramework(PKIF) by  doxygen 1.5.6