📄 snmpelpt.cpp
字号:
Routine Description:
This routine will scan the insertion strings looking for occurances of %%n, where
n is a number indicating a substitution parameter value. If no occurances of %%n are found,
then the routine simply returns without making any modifications.
If any occurance of %%n is found in the buffer, secondary parameter substitution is then
required. FormatMessage is called, without any insertion strings. The event ID is the
value of n following the %%. The message module DLL is one of the following:
Registry
Machine
SYSTEM
CurrentControlSet
Services
EventLog
LogFile (Security, Application, System, etc.)
Source
ParameterMessageFile REG_EXPAND_SZ
- or -
PrimaryModule REG_SZ
If the ParameterMessageFile does not exist for the indicated source, then the PrimaryModule
value will be used for the LogFile key. If this value does not exist, or if any error occurs
when loading any of these DLL's, or if the parameter value cannot be found, the %%n value is
replaced with a NULL string and processing continues.
Arguments:
lpStringArray - Pointer to an array of insertion strings.
nNumStr - Number of insertion strings in lpStrArray.
nStringsSize - Pointer to total size of all insertion strings in lpStrArray.
lpszSrc - Pointer to the source name for this event.
lpszLog - Pointer to the registry event source name.
hPrimModule - Secondary parameter module DLL handle for this event.
Return Value:
None.
Notes:
--*/
{
LONG lastError; // return code from GetLastError
TCHAR szXParmModuleName[MAX_PATH+1]; // space for DLL message module
TCHAR szParmModuleName[MAX_PATH+1]; // space for expanded DLL message module
BOOL bExistParmModule; // says whether a ParmModuleName is specified or not
DWORD nFile = MAX_PATH+1; // max size for DLL message module name
DWORD dwType; // type of message module name
DWORD status; // status from registry calls
DWORD cbExpand; // byte count for REG_EXPAND_SZ parameters
HKEY hkResult; // handle to registry information
HINSTANCE hParmModule; // handle to message module DLL
UINT nBytes; // temporary field
UINT i; // temporary counter
LPTSTR lpParmBuffer;
LPTSTR lpszString, lpStartDigit, lpNew;
UINT nStrSize, nSubNo, nParmSize, nNewSize, nOffset;
PSourceHandleList lpsource; //pointer to source/handle list
WriteTrace(0x0a,"ScanParameters: Entering ScanParameters routine\n");
WriteTrace(0x00,"ScanParameters: Size of original insertion strings is %lu\n", *nStringsSize);
WriteTrace(0x0a,"ScanParameters: Opening registry for parameter module for %s\n", lpszLog);
if ( (status = RegOpenKeyEx( // open the registry to read the name
HKEY_LOCAL_MACHINE, // of the message module DLL
lpszLog, // registry key to open
0,
KEY_READ,
&hkResult) ) != ERROR_SUCCESS)
{
WriteTrace(0x14,"ScanParameters: Unable to open EventLog service registry key %s; RegOpenKeyEx returned %lu\n",
lpszLog, status); // write trace event record
WriteLog(SNMPELEA_CANT_OPEN_REGISTRY_PARM_DLL, lpszLog, status);
WriteTrace(0x0a,"ScanParameters: Exiting ScanParameters\n");
return; // return
}
if ( (status = RegQueryValueEx( // look up module name
hkResult, // handle to registry key
EXTENSION_PARM_MODULE, // key to look up
0, // ignored
&dwType, // address to return type value
(LPBYTE) szXParmModuleName, // where to return parameter module name
&nFile) ) != ERROR_SUCCESS) // size of parameter module name field
{
WriteTrace(0x14,"ScanParameters: No ParameterMessageFile registry key for %s; RegQueryValueEx returned %lu\n",
lpszLog, status); // write trace event record
bExistParmModule = FALSE;
}
else
{
WriteTrace(0x0a,"ScanParameters: ParameterMessageFile value read was %s\n", szXParmModuleName);
cbExpand = ExpandEnvironmentStrings( // expand the DLL name
szXParmModuleName, // unexpanded DLL name
szParmModuleName, // expanded DLL name
MAX_PATH+1); // max size of expanded DLL name
if (cbExpand > MAX_PATH+1) // if it didn't expand correctly
{
WriteTrace(0x14,"ScanParameters: Unable to expand parameter module %s; expanded size required is %lu bytes\n",
szXParmModuleName, cbExpand); // log error message
WriteLog(SNMPELEA_CANT_EXPAND_PARM_DLL, szXParmModuleName, cbExpand);
bExistParmModule = FALSE;
}
else
{
WriteTrace(0x0a,"ScanParameters: ParameterMessageFile expanded to %s\n", szParmModuleName);
bExistParmModule = TRUE;
}
}
// at this point either bExistParmModule = FALSE
// or we have the ';' separated list of ParmModules
// in szParmModuleName
WriteTrace(0x0a,"ScanParameters: Closing registry key for parameter module\n");
RegCloseKey(hkResult); // close the registry key
// for each insertion string
for (i = 0; i < nNumStr; i++)
{
WriteTrace(0x00,"ScanParameters: Scanning insertion string %lu: %s\n",
i, lpStringArray[i]);
nStrSize = strlen(lpStringArray[i]); // get size of insertion string
lpszString = lpStringArray[i]; // set initial pointer
// for each sub string identifier in the insertion string
while (nStrSize > 2)
{
if ( (lpStartDigit = strstr(lpszString, TEXT("%%"))) == NULL )
{
WriteTrace(0x00,"ScanParameters: No secondary substitution parameters found\n");
break;
}
nOffset = (UINT)(lpStartDigit - lpStringArray[i]); // calculate offset in buffer of %%
lpStartDigit += 2; // point to start of potential digit
lpszString = lpStartDigit; // set new string pointer
nStrSize = strlen(lpszString); // calculate new string length
if (nStrSize == 0)
{
WriteTrace(0x00,"ScanParameters: %% found, but remainder of string is null\n");
break;
}
nSubNo = atol(lpStartDigit); // convert to long integer
if (nSubNo == 0 && *lpStartDigit != '0')
{
WriteTrace(0x0a,"ScanParameters: %% found, but following characters were not numeric\n");
lpszString--; // back up 1 byte
// DBCS start
// not need
// if(WHATISCHAR(lpszString-1, 2) == CHAR_DBCS_TRAIL)
// lpszString--;
// DBCS end
nStrSize = strlen(lpszString); // recalculate length
continue; // continue parsing the string
}
// initialize nBytes to 0 to make clear no message formatting was done.
nBytes = 0;
lastError = 0;
// if there is a parameter file, look for into it for the secondary substitution strings
if (bExistParmModule)
{
LPTSTR pNextModule = szParmModuleName;
// for each module name in ParameterMessageFile list of modules
while (pNextModule != NULL)
{
// look for the next delimiter and change it with a string terminator
// in order to isolate the first module name - pointed by pNextModule
LPTSTR pDelim = _tcschr(pNextModule, _T(';'));
if (pDelim != NULL)
*pDelim = _T('\0');
WriteTrace(
0x0a,
"ScanParameters: Looking up secondary substitution string %lu in ParameterMessageFile %s\n",
nSubNo,
pNextModule);
// get the handle to the module (load the module now if need be)
hParmModule = FindSourceHandle(pNextModule);
if (!hParmModule)
hParmModule = AddSourceHandle(pNextModule);
// careful to restore the szParmModuleName string to its original content
// we need this as far as the scanning should be done for each of the insertion strings
if (pDelim != NULL)
*pDelim = _T(';');
// it's not clear whether FormatMessage() allocates any memory in lpParmBuffer when it fails
// so initialize the pointer here, and free it in case of failure. LocalFree is harmless on
// NULL pointer.
lpParmBuffer = NULL;
// if we have a valid parameter module handle at this point,
// format the insertion string using this module
if (hParmModule != NULL)
{
nBytes = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // let api build buffer
FORMAT_MESSAGE_IGNORE_INSERTS | // ignore inserted strings
FORMAT_MESSAGE_FROM_HMODULE, // look thru message DLL
(LPVOID) hParmModule, // use parameter file
nSubNo, // parameter number to get
(ULONG) NULL, // specify no language
(LPTSTR) &lpParmBuffer, // address for buffer pointer
80, // minimum space to allocate
NULL); // no inserted strings
lastError = GetLastError();
}
// if the formatting succeeds, break the loop (szParmModuleName should
// be at this point exactly as it was when the loop was entered)
if (nBytes != 0)
break;
LocalFree(lpParmBuffer);
// move on to the next module name
pNextModule = pDelim != NULL ? pDelim + 1 : NULL;
}
}
if (nBytes == 0)
{
WriteTrace(0x0a,"ScanParameters: ParameterMessageFile did not locate parameter - error %lu\n",
lastError);
// WriteLog(SNMPELEA_PARM_NOT_FOUND, nSubNo, lastError);
LocalFree(lpParmBuffer); // free storage
WriteTrace(0x0a,"ScanParameters: Searching PrimaryModule for parameter\n");
nBytes = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // let api build buffer
FORMAT_MESSAGE_IGNORE_INSERTS | // ignore inserted strings
FORMAT_MESSAGE_FROM_HMODULE, // look thru message DLL
(LPVOID) hPrimModule, // use parameter file
nSubNo, // parameter number to get
(ULONG) NULL, // specify no language
(LPTSTR) &lpParmBuffer, // address for buffer pointer
80, // minimum space to allocate
NULL); // no inserted strings
if (nBytes == 0)
{
lastError = GetLastError(); // get error code
WriteTrace(0x0a,"ScanParameters: PrimaryModule did not locate parameter - error %lu\n",
lastError);
WriteLog(SNMPELEA_PRIM_NOT_FOUND, nSubNo, lastError);
LocalFree(lpParmBuffer); // free storage
}
}
nParmSize = 2; // set initialize parameter size (%%)
while (strlen(lpszString))
{
if (!isdigit(*lpszString))
{
break; // exit if no more digits
}
nParmSize++; // increment parameter size
// DBCS start
if (IsDBCSLeadByte(*lpszString))
lpszString++;
// DBCS end
lpszString++; // point to next byte
}
nNewSize = strlen(lpStringArray[i])+nBytes-nParmSize+1; // calculate new length
nStrSize = strlen(lpStringArray[i])+1; // get original length
WriteTrace(0x00,"ScanParameters: Original string length is %lu, new string length is %lu\n",
nStrSize, nNewSize);
if (nNewSize > nStrSize)
{
lpNew = (TCHAR *) SNMP_realloc(lpStringArray[i], nNewSize);
if ( lpNew == NULL)
{
WriteTrace(0x14,"ScanParameters: Unable to reallocate storage for insertion strings. Scanning terminated.\n");
WriteLog(SNMPELEA_REALLOC_INSERTION_STRINGS_FAILED);
WriteTrace(0x00,"ScanParameters: Size of new insertion strings is %lu\n", *nStringsSize);
return; // return
}
WriteTrace(0x0a,"ScanParameters: Insertion string reallocated to %08X\n", lpNew);
lpStringArray[i] = lpNew; // set new pointer
lpStartDigit = lpStringArray[i] + nOffset; // point to new start of current %%
lpszString = lpStartDigit+nBytes; // set new start of scan spot
*nStringsSize += nBytes-nParmSize; // calculate new total size
WriteTrace(0x00,"ScanParameters: Old size of all insertion strings was %lu, new size is %lu\n",
*(nStringsSize)-nBytes+nParmSize, *nStringsSize);
}
else
{
WriteTrace(0x0a,"ScanParameters: New size of string is <= old size of string\n");
lpStartDigit -= 2; // now point to %%
lpszString = lpStartDigit; // set new start of scan spot
}
nStrSize = strlen(lpStartDigit)+1; // calculate length of remainder of string
if (nBytes)
{
memmove(lpStartDigit+nBytes-nParmSize, // destination address
lpStartDigit, // source address
nStrSize); // amount of data to move
memmove(lpStartDigit, // destination address
lpParmBuffer, // source address
nBytes); // amount of data to move
LocalFree(lpParmBuffer);
}
else
{
memmove(lpStartDigit, // destination address
lpStartDigit+nParmSize, // source address
nStrSize); // amount of data to move
}
WriteTrace(0x00,"ScanParameters: New insertion string is %s\n",
lpStringArray[i]);
nStrSize = strlen(lpszString); // get length of remainder of string
}
}
WriteTrace(0x00,"ScanParameters: Size of new insertion strings is %lu\n", *nStringsSize);
WriteTrace(0x0a,"ScanParameters: Exiting ScanParameters routine\n");
}
VOID
FreeArrays(
IN UINT nCount, // number of array entries to free
IN PUINT lpStrLenArray, // pointer to string length array
IN LPTSTR *lpStringArray, // pointer to string pointer array
IN BOOL DelStrs = TRUE
)
/*++
Routine Description:
This routine will free the allocated storage for strings in case of an error when
building the varbind entries.
Arguments:
nCount - This is a count of the number of entries to free
lpStrLenArray - This is a pointer to the string length array to be freed.
lpStringArray - This is a pointer to the string array to be freed.
DelStrs - Should the strings be deleted?
Return Value:
None.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -