More dead code removal, this time Win32-isms in logging & sockets. Also start cleaning up OS macros in platform.h.

This commit is contained in:
Brian Cox 2016-06-23 14:51:17 -07:00
parent 3135aaea4f
commit 9be78c1d1c
7 changed files with 116 additions and 572 deletions

View File

@ -51,15 +51,6 @@
#include "corestrings.h" // for: STR_ERR2_ARCH_CRYPTO_ERR
#if FSEEK_TAKES_INT32
#define FSEEK(x, y, z) fseek((x), (int32)(y), (z))
#else
#define FSEEK(x, y, z) fseek((x), (y), (z))
#endif
//=============================================================================
// Utility Functions
//=============================================================================

View File

@ -89,17 +89,6 @@ void cDebug::Trace(int levelNum, const char* format, ...)
}
void cDebug::Trace(int levelNum, const wchar_t* format, ...)
{
if (levelNum > mDebugLevel)
return;
// create the output buffer
va_list args;
va_start(args, format);
DoTrace(format, args);
va_end(args);
}
///////////////////////////////////////////////////////////////////////////////
// DoTrace()
@ -146,56 +135,6 @@ void cDebug::DoTrace(const char *format, va_list &args)
logfile.flush();
}
}
void cDebug::DoTrace(const wchar_t *format, va_list &args)
{
#if IS_UNIX
// we don't support vswprintf on UNIX
ASSERT(false);
THROW_INTERNAL("debug.cpp");
#else
size_t guard1 = 0xBABABABA;
wchar_t out[2048];
size_t guard2 = 0xBABABABA;
vswprintf(out, format, args);
ASSERT(guard1 == 0xBABABABA && guard2 == 0xBABABABA); // string was too long
char nout[1024];
if (wcstombs(nout, out, 1024) == -1)
strcpy(nout, "XXX Unconvertable wide char detected in cDebug::DoTrace()\n");
std::ostringstream ostr;
ostr.setf(std::ios::left);
ostr.width(40);
ostr << mLabel;
ostr.width(0);
ostr << nout;
if ((mOutMask & OUT_STDOUT) != 0)
{
std::cout << ostr.str().c_str();
std::cout.flush();
}
//
//make it output to log file!
//
if ((mOutMask & OUT_FILE) != 0)
{
// the logfile is narrow chars only...
logfile.setf(std::ios::left);
logfile.width(40);
logfile << mLabel;
logfile.width(0);
logfile << out;
logfile.flush();
}
#endif // IS_UNIX
}
#ifdef DEBUG
//
@ -275,90 +214,11 @@ void cDebug::TraceNever(const char *format, ...)
va_end(args);
}
void cDebug::TraceAlways(const wchar_t *format, ...)
{
if (D_ALWAYS > mDebugLevel)
return;
// fill up arglist, and pass to printing routine
va_list args;
va_start(args, format);
DoTrace(format, args);
va_end(args);
}
void cDebug::TraceError(const wchar_t *format, ...)
{
if (D_ERROR > mDebugLevel)
return;
// fill up arglist, and pass to printing routine
va_list args;
va_start(args, format);
DoTrace(format, args);
va_end(args);
}
void cDebug::TraceWarning(const wchar_t *format, ...)
{
if (D_WARNING > mDebugLevel)
return;
// fill up arglist, and pass to printing routine
va_list args;
va_start(args, format);
DoTrace(format, args);
va_end(args);
}
void cDebug::TraceDebug(const wchar_t *format, ...)
{
if (D_DEBUG > mDebugLevel)
return;
// fill up arglist, and pass to printing routine
va_list args;
va_start(args, format);
DoTrace(format, args);
va_end(args);
}
void cDebug::TraceDetail(const wchar_t *format, ...)
{
if (D_DETAIL > mDebugLevel)
return;
// fill up arglist, and pass to printing routine
va_list args;
va_start(args, format);
DoTrace(format, args);
va_end(args);
}
void cDebug::TraceNever(const wchar_t *format, ...)
{
if (D_NEVER > mDebugLevel)
return;
// fill up arglist, and pass to printing routine
va_list args;
va_start(args, format);
DoTrace(format, args);
va_end(args);
}
void cDebug::TraceVaArgs( int iDebugLevel, const char *format, va_list &args )
{
if ( iDebugLevel <= mDebugLevel )
DoTrace( format, args);
}
void cDebug::TraceVaArgs( int iDebugLevel, const wchar_t *format, va_list &args )
{
if ( iDebugLevel <= mDebugLevel )
DoTrace( format, args );
}
#endif // DEBUG
///////////////////////////////////////////////////////////////////////////////
@ -447,55 +307,12 @@ void cDebug::DebugOut( const char* lpOutputString, ... )
vsprintf(buf, lpOutputString, args);
va_end(args);
#if !USE_OUTPUT_DEBUG_STRING
#ifdef _DEBUG
TCERR << buf;
#endif //_DEBUG
#else // USE_OUTPUT_DEBUG_STRING
::OutputDebugString(buf);
#endif // USE_OUTPUT_DEBUG_STRING
#ifdef _DEBUG
TCERR << buf;
#endif //_DEBUG
TCOUT.flush();
}
void cDebug::DebugOut( const wchar_t* lpOutputString, ... )
{
va_list args;
va_start(args, lpOutputString);
#if IS_UNIX
char mbformatbuf[1024];
char buf[1024];
// if (wcstombs(mbformatbuf, lpOutputString, wcslen(lpOutputString)) == -1)
// strcpy(mbformatbuf, "XXX Unconvertable wide char detected in cDebug::DebugOut()\n");
vsprintf(buf, mbformatbuf, args);
#else
wchar_t buf[1024];
vswprintf(buf, lpOutputString, args);
#endif
va_end(args);
char nbuf[1024];
#if IS_UNIX
strcpy(nbuf, buf);
#else
if (wcstombs(nbuf, buf, wcslen(buf)+1) == -1)
strcpy(nbuf, "XXX Unconvertable wide char detected in cDebug::DebugOut()\n");
#if !USE_OUTPUT_DEBUG_STRING
#ifdef _DEBUG
TCERR << nbuf;
#endif //_DEBUG
#else // USE_OUTPUT_DEBUG_STRING
::OutputDebugString(nbuf);
#endif // USE_OUTPUT_DEBUG_STRING
#endif
TCOUT.flush();
}
#endif // DEBUG
//////////////////////////////////////////////////////////////////////////////////

View File

@ -113,22 +113,15 @@ public:
void TraceDebug (const char *format, ...);
void TraceDetail (const char *format, ...);
void TraceNever (const char *format, ...);
void TraceAlways (const wchar_t *format, ...);
void TraceError (const wchar_t *format, ...);
void TraceWarning (const wchar_t *format, ...);
void TraceDebug (const wchar_t *format, ...);
void TraceDetail (const wchar_t *format, ...);
void TraceNever (const wchar_t *format, ...);
// these are of use if you are inside a function with a "..." as an argument
// and you want to trace those args
void TraceVaArgs (int iDebugLevel, const char *format, va_list &args);
void TraceVaArgs (int iDebugLevel, const wchar_t *format, va_list &args);
// ...but you can still choose to use this interface...
void Trace(int levelNum, const char* format, ...);
void Trace(int levelNum, const wchar_t* format, ...);
// Outputs based on levelnum. If levelnum <= global debug, print.
public:
@ -146,7 +139,6 @@ public:
// level will be output.
static void DebugOut ( const char* lpOutputString, ... );
static void DebugOut ( const wchar_t* lpOutputString, ... );
// Works just like TRACE
// note: there is an internal buffer size of 1024; traces larger
// than that will have unpredictable and probably bad results
@ -161,7 +153,6 @@ private:
// helper functions
void DoTrace(const char *format, va_list &args);
void DoTrace(const wchar_t *format, va_list &args);
#endif
};
@ -199,16 +190,8 @@ inline void cDebug::TraceWarning (const char *, ...) {}
inline void cDebug::TraceDebug (const char *, ...) {}
inline void cDebug::TraceDetail (const char *, ...) {}
inline void cDebug::TraceNever (const char *, ...) {}
inline void cDebug::TraceAlways (const wchar_t *, ...) {}
inline void cDebug::TraceError (const wchar_t *, ...) {}
inline void cDebug::TraceWarning (const wchar_t *, ...) {}
inline void cDebug::TraceDebug (const wchar_t *, ...) {}
inline void cDebug::TraceDetail (const wchar_t *, ...) {}
inline void cDebug::TraceNever (const wchar_t *, ...) {}
inline void cDebug::TraceVaArgs (int, const char *, va_list &) {}
inline void cDebug::TraceVaArgs (int, const wchar_t *, va_list &) {}
inline void cDebug::Trace (int, const char*, ...) {}
inline void cDebug::Trace (int, const wchar_t*, ...) {}
inline bool cDebug::AddOutTarget (OutTarget) { return false; }
inline bool cDebug::RemoveOutTarget (OutTarget) { return false; }
inline bool cDebug::HasOutTarget (OutTarget) { return false; }
@ -216,7 +199,6 @@ inline bool cDebug::SetOutputFile (const char*) { return false; }
inline void cDebug::SetDebugLevel (int) {}
inline int cDebug::GetDebugLevel (void) { return 0; }
inline void cDebug::DebugOut ( const char*, ... ) {}
inline void cDebug::DebugOut ( const wchar_t*, ... ) {}
#endif // DEBUG

View File

@ -40,6 +40,9 @@
#ifndef __PLATFORM_H
#define __PLATFORM_H
//NOTE: Autoconf is strongly preferred as the Right Way to detect platform-specific features/behaviors.
// These macros should really only be used when autoconf can't get the job done.
//=============================================================================
// Enumerations
//
@ -47,13 +50,42 @@
// variation. We group similar items together, such as OS_REDHAT and OS_SLACKWARE
#define OS_UNKNOWN 0
#define OS_WIN32 0x0101
#define OS_CYGWIN 0x0102
#define OS_DOS_DJGPP 0x0103
#define OS_LINUX 0x0201
#define OS_ANDROID 0x0202
#define OS_FREEBSD 0x0301
#define OS_NETBSD 0x0302
#define OS_OPENBSD 0x0303
#define OS_DARWIN 0x0304
#define OS_DRAGONFLYBSD 0x0305
#define OS_MIDNIGHTBSD 0x0306
#define OS_HARDENEDBSD 0x0307
#define OS_SOLARIS 0x0400
#define OS_AIX 0x0401
#define OS_HPUX 0x0501
#define OS_IRIX 0x0601
#define OS_OSF1 0x0701
#define OS_HPUX 0x0402
#define OS_IRIX 0x0403
#define OS_OSF1 0x0404
#define OS_MINIX 0x0501
#define OS_HURD 0x0502
#define OS_HAIKU 0x0503
#define OS_SYLLABLE 0x0504
#define OS_SKYOS 0x0505
#define OS_SORTIX 0x0506
#define OS_MINT 0x0507
#define OS_AROS 0x0506
#define COMP_UNKNOWN 0
#define COMP_GCC 0x0001
#define COMP_CLANG 0x0002
#define COMP_MSVC 0x0101
#define COMP_KAI_GCC 0x0201
#define COMP_KAI_SUNPRO 0x0202
@ -81,34 +113,91 @@
#if defined(_WIN32)
#define OS OS_WIN32
#define IS_WIN32 1
#elif defined(__CYGWIN__)
#define OS OS_CYGWIN
#define IS_CYGWIN 1
#if defined(_MSC_VER)
#define COMP COMP_MSVC
#else
#error _MSC_VER not defined. MSVC is currently the only supported compiler
#endif
#elif defined(__DJGPP__)
#define OS OS_DOS_DJGPP
#define IS_DOS_DJGPP 1
#elif defined(__ANDROID_API__)
#define OS OS_ANDROID
#define IS_ANDROID 1
#elif defined(_LINUX)
#define OS OS_LINUX
#define IS_LINUX 1
#elif defined(__FreeBSD__)
#define OS OS_FREEBSD
#define IS_FREEBSD 1
#elif defined(_IRIX)
#define OS OS_IRIX
#define COMP COMP_KAI_IRIX
#elif defined(_ALPHA)
#define OS OS_OSF1
#define COMP COMP_KAI_OSF1ALPHA
// add NetBSD, OpenBSD, other BSDs
#elif defined(_OSX)
#define OS OS_DARWIN
#define IS_DARWIN 1
#elif defined(_SOLARIS)
#define OS OS_SOLARIS
#define IS_SOLARIS 1
#elif defined(_AIX)
#define OS OS_AIX
#define IS_AIX 1
#elif defined (_HPUX)
#define OS OS_HPUX
#define COMP COMP_KAI_HPANSIC
#define IS_HPUX 1
#elif defined(_IRIX)
#define OS OS_IRIX
#define IS_IRIX 1
#elif defined(TRU64) || defined(__OSF1__)
#define OS OS_OSF1
#define IS_OSF1 1
// need Minix, Haiku
#elif defined(__gnu_hurd__)
#define OS OS_HURD
#define IS_HURD 1
#elif defined(__SYLLABLE__)
#define OS OS_SYLLABLE
#define IS_SYLLABLE 1
#elif defined(SKYOS)
#define OS OS_SKYOS
#define IS_SKYOS 1
#elif defined(_SORTIX_SOURCE)
#define OS OS_SORTIX
#define IS_SORTIX 1
#elif defined(__MINT__)
#define OS OS_MINT
#define IS_MINT 1
#elif defined(__AROS__)
#define OS OS_AROS
#define IS_AROS 1
#else
// OK for OS not to resolve, it's being phased out.
// #error Unknown OS
#endif
#if !defined(OS)
// OK for OS not to resolve, it's being phased out.
// #error OS definition did not resolve. Check "platform.h".
#endif
/* XXX: COMP may now not resolve, because autoconf may
* detect GCC. This is done in the hopes that all
* COMP detections, and indeed both OS & COMP detechtions
@ -150,13 +239,6 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// OS detection
// Note: Avoid using these if possible (see above)
#define IS_WIN32 (OS == OS_WIN32)
#define IS_AIX (OS == OS_AIX)
#define IS_HPUX (OS == OS_HPUX)
#define IS_IRIX (OS == OS_IRIX)
#define IS_OSF1 (OS == OS_OSF1)
// complier detection
#define IS_KAI (COMP == COMP_KAI_GCC || COMP == COMP_KAI_SUNPRO || COMP == COMP_KAI_GLIBC || COMP == COMP_KAI_VISUALAGE || COMP == COMP_KAI_HPANSIC || COMP == COMP_KAI_IRIX || COMP == COMP_KAI_OSF1ALPHA)
@ -183,17 +265,12 @@
#define SUPPORTS_POSIX_THREADS (!SUPPORTS_WIN32_THREADS)
// Miscellaneous
#define FSEEK_TAKES_INT32 IS_UNIX // True if fseek takes 32-bit offsets
#define USE_OUTPUT_DEBUG_STRING IS_WIN32 // Use the Win32 OutputDebugString() for debug messages.
#define SUPPORTS_MAPI 0
#define WCHAR_IS_16_BITS IS_WIN32
#define WCHAR_IS_32_BITS IS_UNIX
#define WCHAR_REP_IS_UCS2 IS_WIN32
#define USES_MPOPEN IS_UNIX
#define USES_WINSOCK IS_WIN32
#define SUPPORTS_WCHART IS_WIN32 // TODO: Remove after getting new ver of KAI
#define USES_GLIBC ((COMP == COMP_KAI_GLIBC) || HAVE_GCC)
#define SUPPORTS_EVENTLOG IS_WIN32
#define SUPPORTS_MEMBER_TEMPLATES ( ! IS_SUNPRO )
#define SUPPORTS_EXPLICIT_TEMPLATE_FUNC_INST ( ! IS_SUNPRO )

View File

@ -228,60 +228,6 @@ private:
// socket related member variables
SOCKET mSocket;
#if USES_WINSOCK
//
// Types for the functions in winsock.dll
//
// These function prototypes and the pointers that use them allow us to use
// winsock without requiring the DLL to be on the end system for the application
// to run. It seems unacceptable for tripwire to fail to run at all if wsock32.dll
// is not present on the system.
//
HINSTANCE mHlibWinsock;
typedef int (PASCAL * WSASTARTUPPROC) (WORD wVersionRequired, LPWSADATA lpWSAData);
typedef SOCKET (PASCAL * SOCKETPROC) (int af, int type, int protocol);
typedef unsigned long (PASCAL * INETADDRPROC) (const char FAR * cp);
typedef int (PASCAL FAR * GETHOSTNAMEPROC) (char FAR * name, int namelen);
typedef struct hostent FAR * (PASCAL * GETHOSTBYNAMEPROC)(const char FAR * name);
typedef int (PASCAL * CONNECTPROC) (SOCKET s, const struct sockaddr FAR *name, int namelen);
typedef int (PASCAL * CLOSESOCKETPROC) (SOCKET s);
typedef int (PASCAL * SENDPROC) (SOCKET s, const char FAR * buf, int len, int flags);
typedef int (PASCAL * RECVPROC) (SOCKET s, char FAR * buf, int len, int flags);
typedef int (PASCAL * SELECTPROC) (int nfds, fd_set FAR * readfds, fd_set FAR * writefds, fd_set FAR * exceptfds, const struct timeval FAR * timeout);
typedef u_long (PASCAL * LONGPROC) (u_long netlong);
typedef u_short (PASCAL * SHORTPROC) (u_short netlong);
// pointers to the functions in wsock32.dll
// Berkeley functions
SOCKETPROC mPfnSocket;
INETADDRPROC mPfnInetAddr;
GETHOSTNAMEPROC mPfnGethostname;
GETHOSTBYNAMEPROC mPfnGethostbyname;
CONNECTPROC mPfnConnect;
CLOSESOCKETPROC mPfnCloseSocket;
SENDPROC mPfnSend;
RECVPROC mPfnRecv;
SELECTPROC mPfnSelect;
// winsock functions
FARPROC mPfnWSAGetLastError;
WSASTARTUPPROC mPfnWSAStartup;
FARPROC mPfnWSACleanup;
// Endian convertion functions
LONGPROC mPfnNtohl;
LONGPROC mPfnHtonl;
SHORTPROC mPfnNtohs;
SHORTPROC mPfnHtons;
// Methods to set the pointers to functions.
bool LoadDll();
bool UnloadDll();
#endif
void SendString( const std::string& str );
// methods common to windows and unix:

View File

@ -132,29 +132,6 @@ cSMTPMailMessage::cSMTPMailMessage(TSTRING strServerName, unsigned short portNum
mstrServerName = strServerName;
mPortNumber = portNumber;
mSocket = INVALID_SOCKET;
#if USES_WINSOCK
mHlibWinsock = NULL;
mPfnSocket = NULL;
mPfnInetAddr = NULL;
mPfnGethostname = NULL;
mPfnGethostbyname = NULL;
mPfnConnect = NULL;
mPfnCloseSocket = NULL;
mPfnSend = NULL;
mPfnRecv = NULL;
mPfnSelect = NULL;
mPfnWSAStartup = NULL;
mPfnWSACleanup = NULL;
mPfnWSAGetLastError = NULL;
mPfnNtohl = NULL;
mPfnHtonl = NULL;
mPfnNtohs = NULL;
mPfnHtons = NULL;
#endif // USES_WINSOCK
}
@ -167,8 +144,6 @@ cSMTPMailMessage::~cSMTPMailMessage()
}
///////////////////////////////////////////////////////////////////////////////
//
// Get the IP address from the the server string. It's OK to have
@ -585,88 +560,6 @@ void cSMTPMailMessage::DecodeError()
{
#if defined(_DEBUG)
#if USES_WINSOCK
//
// decode an error condition encountered by Windows sockets.
// TODO - write something less technical to explain what when wrong. Windows
// message names are not good error strings for a shipping product.
//
int error = mPfnWSAGetLastError();
fprintf(stderr, "Encountered winsock error message %d", error);
struct ErrorStruct
{
const char *msg;
int error;
}
static const errors[52] =
{
{ "WSAEINTR", (WSABASEERR+4) },
{ "WSAEBADF", (WSABASEERR+9) },
{ "WSAEACCES", (WSABASEERR+13) },
{ "WSAEFAULT", (WSABASEERR+14) },
{ "WSAEINVAL", (WSABASEERR+22) },
{ "WSAEMFILE", (WSABASEERR+24) },
{ "WSAEWOULDBLOCK", (WSABASEERR+35) },
{ "WSAEINPROGRESS", (WSABASEERR+36) },
{ "WSAEALREADY", (WSABASEERR+37) },
{ "WSAENOTSOCK", (WSABASEERR+38) },
{ "WSAEDESTADDRREQ", (WSABASEERR+39) },
{ "WSAEMSGSIZE", (WSABASEERR+40) },
{ "WSAEPROTOTYPE", (WSABASEERR+41) },
{ "WSAENOPROTOOPT", (WSABASEERR+42) },
{ "WSAEPROTONOSUPPORT", (WSABASEERR+43) },
{ "WSAESOCKTNOSUPPORT", (WSABASEERR+44) },
{ "WSAEOPNOTSUPP", (WSABASEERR+45) },
{ "WSAEPFNOSUPPORT", (WSABASEERR+46) },
{ "WSAEAFNOSUPPORT", (WSABASEERR+47) },
{ "WSAEADDRINUSE", (WSABASEERR+48) },
{ "WSAEADDRNOTAVAIL", (WSABASEERR+49) },
{ "WSAENETDOWN", (WSABASEERR+50) },
{ "WSAENETUNREACH", (WSABASEERR+51) },
{ "WSAENETRESET", (WSABASEERR+52) },
{ "WSAECONNABORTED", (WSABASEERR+53) },
{ "WSAECONNRESET", (WSABASEERR+54) },
{ "WSAENOBUFS", (WSABASEERR+55) },
{ "WSAEISCONN", (WSABASEERR+56) },
{ "WSAENOTCONN", (WSABASEERR+57) },
{ "WSAESHUTDOWN", (WSABASEERR+58) },
{ "WSAETOOMANYREFS", (WSABASEERR+59) },
{ "WSAETIMEDOUT", (WSABASEERR+60) },
{ "WSAECONNREFUSED", (WSABASEERR+61) },
{ "WSAELOOP", (WSABASEERR+62) },
{ "WSAENAMETOOLONG", (WSABASEERR+63) },
{ "WSAEHOSTDOWN", (WSABASEERR+64) },
{ "WSAEHOSTUNREACH", (WSABASEERR+65) },
{ "WSAENOTEMPTY", (WSABASEERR+66) },
{ "WSAEPROCLIM", (WSABASEERR+67) },
{ "WSAEUSERS", (WSABASEERR+68) },
{ "WSAEDQUOT", (WSABASEERR+69) },
{ "WSAESTALE", (WSABASEERR+70) },
{ "WSAEREMOTE", (WSABASEERR+71) },
{ "WSAEDISCON", (WSABASEERR+101) },
{ "WSASYSNOTREADY", (WSABASEERR+91) },
{ "WSAVERNOTSUPPORTED", (WSABASEERR+92) },
{ "WSANOTINITIALISED", (WSABASEERR+93) },
{ "WSAHOST_NOT_FOUND", (WSABASEERR+1001) },
{ "WSATRY_AGAIN", (WSABASEERR+1002) },
{ "WSANO_RECOVERY", (WSABASEERR+1003) },
{ "WSANO_DATA", (WSABASEERR+1004) },
{ NULL, 0 }
};
int i = 0;
while (errors[i].msg)
{
if (errors[i].error == error)
{
fprintf(stderr, ": %s", errors[i].msg);
break;
}
i++;
}
fprintf(stderr, "\n");
#endif // USES_WINSOCK
#if IS_UNIX
//

View File

@ -44,11 +44,6 @@
#include <sys/syslog.h>
#endif
#if SUPPORTS_EVENTLOG
#include "tripwiremsg.h"
#include "tw/systeminfo.h"
#endif
// next three includes are for error reporting
#include "tw/twutil.h"
#include "tw/twerrors.h"
@ -63,175 +58,18 @@
///////////////////////////////////////////////////////////////////////////////
// Syslog
///////////////////////////////////////////////////////////////////////////////
#if SUPPORTS_EVENTLOG
static void InitEventLog();
#endif
void cSyslog::Log(const TCHAR* programName, cSyslog::LogType logType, const TCHAR* message)
{
#if HAVE_SYSLOG_H
#if HAVE_SYSLOG_H && !defined(SKYOS) // Handle an oddball OS that has syslog.h but doesn't implement the calls.
(void)logType; // logType is not used for Unix syslog
ASSERT(sizeof(TCHAR) == sizeof(char));
const char* ident = programName;
const char* msg = message;
#ifndef SKYOS // Handle an oddball OS that has syslog.h but doesn't implement the calls.
openlog(ident, LOG_PID, LOG_USER);
syslog(LOG_NOTICE, "%s", msg);
closelog();
#endif
#elif SUPPORTS_EVENTLOG
static bool eventLogInitialized = false;
if (!eventLogInitialized)
{
InitEventLog();
eventLogInitialized = true;
}
HANDLE h = RegisterEventSource(NULL, _T("Tripwire"));
if (h != NULL)
{
LPCTSTR stringArray[1];
stringArray[0] = message;
WORD type;
DWORD id;
switch (logType)
{
default:
ASSERT(false);
case LOG_SUCCESS:
type = EVENTLOG_INFORMATION_TYPE;
id = MSG_TRIPWIRE_GENERIC_SUCCESS;
break;
case LOG_INFORMATION:
type = EVENTLOG_INFORMATION_TYPE;
id = MSG_TRIPWIRE_GENERIC_INFO;
break;
case LOG_WARNING:
type = EVENTLOG_WARNING_TYPE;
id = MSG_TRIPWIRE_GENERIC_WARNING;
break;
case LOG_ERROR:
type = EVENTLOG_ERROR_TYPE;
id = MSG_TRIPWIRE_GENERIC_ERROR;
break;
}
BOOL ret = ReportEvent(
h,
type, // event type
0, // catagory
id, // event id
0, // user sid
1, // num strings
0, // raw binary data size
stringArray, // array of strings
0 // raw binrary data
);
if (!ret)
{
eTWSyslog e( TSS_GetString(cTW, tw::STR_REPORTEVENT_FAILED).c_str(), eError::NON_FATAL );
cTWUtil::PrintErrorMsg(e);
}
DeregisterEventSource(h);
}
#else
// No support for syslog like functionality
//ASSERT(false);
#endif
}
#if SUPPORTS_EVENTLOG
static void InitEventLog()
{
// To initialize the event log, we need to verify that Tripwire
// is a valid event source.
// To do this we look up the value for the Tripwire event source. If it
// exists, the the dest is a file called "tripwire.exe" (or this executable) and the
// file exists, then we assume that the event source is set correctly.
HKEY hKey;
DWORD disposition;
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\Tripwire"), 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &disposition) == ERROR_SUCCESS &&
disposition == REG_OPENED_EXISTING_KEY)
{
DWORD type, count;
// We need to insure we have back slashes in our exepath
TSTRING exepath = cSystemInfo::GetExePath();
TSTRING::iterator i;
for (i = exepath.begin(); i != exepath.end(); ++i)
if (*i == _T('/'))
*i = _T('\\');
if (RegQueryValueEx(hKey, _T("TypesSupported"), 0, &type, 0, &count) == ERROR_SUCCESS &&
type == REG_DWORD &&
RegQueryValueEx(hKey, _T("EventMessageFile"), 0, &type, 0, &count) == ERROR_SUCCESS &&
type == REG_EXPAND_SZ)
{
TSTRING data;
data.resize(count);
if (RegQueryValueEx(hKey, _T("EventMessageFile"), 0, &type, (LPBYTE)data.data(), &count) == ERROR_SUCCESS &&
type == REG_EXPAND_SZ)
{
TSTRING::size_type lastDelimitor;
lastDelimitor = data.find_last_of(_T('\\'));
if (lastDelimitor == TSTRING::npos)
lastDelimitor = (TSTRING::size_type)-1;
if (_tcscmp(exepath.c_str(), data.c_str()) == 0)
{
RegCloseKey(hKey);
return;
}
if (_tcsicmp(_T("tripwire.exe"), data.substr(lastDelimitor + 1).c_str()) == 0)
{
HINSTANCE hInst = LoadLibraryEx(data.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES|LOAD_LIBRARY_AS_DATAFILE);
if (hInst != NULL)
{
FreeLibrary(hInst);
RegCloseKey(hKey);
return;
}
}
}
}
// If we got here then the event source is not set up correctly
// Add the name to the EventMessageFile subkey.
RegSetValueEx(hKey, // subkey handle
_T("EventMessageFile"), // value name
0, // must be zero
REG_EXPAND_SZ, // value type
(LPBYTE) exepath.c_str(), // pointer to value data
sizeof(TCHAR)*(exepath.length() + 1)); // length of value data
// Set the supported event types in the TypesSupported subkey.
DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
RegSetValueEx(hKey, // subkey handle
_T("TypesSupported"), // value name
0, // must be zero
REG_DWORD, // value type
(LPBYTE) &dwData, // pointer to value data
sizeof(DWORD)); // length of value data
RegCloseKey(hKey);
}
}
#endif