📄 command.cpp
字号:
//****************************************************************************
//
// Copyright (c) 1996, Microsoft Corporation
//
// File: COMMAND.CPP
//
// Implementation of the translation command classes and their containers.
//
// Author: Nadir Ahmed (nadira@microsoft.com)
//
// History:
//
// nadira 03/20/96 Created.
//
//****************************************************************************
// Public Includes
// ===============
// Private includes
// ================
#include "stdafx.h"
#include <eventcmd.h>
#include "resource.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
//============================================================================
// TrapCommandItem::TrapCommandItem
//
// This is the TrapCommandItem class's only constructor. This class is derived
// from the CommandItem class. It is used to store and process individual trap
// destination configuration requests. The CommandItem constructor is called
// count, time and eventid default values of 0. The eventlog gets the value of
// the community name and the eventsource gets the value of the address.
//
//
// Parameters:
//
// CommandType comtype The requested command type. Currently either
// AddTrap or DeleteTrap are only supported.
//
// CString * comm The community name for the trap destination.
//
// CString * addr The network address for the trap destination.
//
// HKEY key The registry key HKEY_LOCAL_MACHINE for the
// machine that is beig configured.
//
// Returns:
//
// none
//
//============================================================================
TrapCommandItem::TrapCommandItem(CommandType comtype, CString * comm, CString * addr, HKEY key)
:CommandItem(comtype, comm, addr, 0, 0, 0, key)
{
// Store the community name and address for the request
// ====================================================
community = comm;
address = addr;
}
//============================================================================
// TrapCommandItem::Process
//
// This public method is called to process the TrapCommandItem. It modifies
// the image of the SNMP trap destination registry held in memory. The success
// of the operation is indicated by setting the second parameter. It returns
// a message contained in a CString which is to be written to the log.
//
//
// Parameters:
//
// UniqueList * CommList The image of the SNMP trap config held in
// memory. This is a list of community names. Each
// community name having a list of destination
// addresses. (Therefore a list of lists!)
//
// ReturnVal * success An indication of the success of the operation.
// Currently values reported are:
// RET_OK - success
// RET_BAD - failure
// RET_NOT_FOUND - Duplicate not added OR
// - Item to be deleted not found
//
//
// Returns:
//
// CString * A pointer to a CString containing a message
// to be written in the log. If this is non NULL
// the CString object being pointed to should be
// deleted by the calling function after use.
//
//============================================================================
CString * TrapCommandItem::Process(UniqueList * CommList, ReturnVal * success)
{
CString * retchar = NULL;
CString * commname = new CString(*community);
CommListItem * tmp = new CommListItem(commname);
CString * addr = new CString(*address);
ListItem * addr_item = new ListItem(addr);
*success = RET_OK;
if (GetCommand() == AddTrap)
{
tmp->addresses.Add(addr_item);
CommListItem * tmp2 = (CommListItem *)CommList->Add(tmp);
if (tmp2) //duplicate commname just add the address to it
{
addr_item = tmp->addresses.Pop(); //get the address
delete tmp; //delete the duplicate
if(tmp2->addresses.Add(addr_item)) //add the address
{
retchar = WriteToBuff(IDS_MSG1);
delete addr_item; //duplicate address delete it
*success = RET_NOTFOUND; //indicate duplicate not added
}
if(!retchar)
retchar = WriteToBuff(IDS_MSG2);
}
if(!retchar)
retchar = WriteToBuff(IDS_MSG3);
}
else //DeleteTrap
{
// Is the item in the current image?
// =================================
CommListItem * tmp2 = (CommListItem *)CommList->FindItem(tmp, 0);
if(tmp2) //if it is in the image it will be removed.
{
POSITION p = tmp2->addresses.FindItem(addr_item);
BOOL removed = FALSE;
if (p)
{
ListItem * del = tmp2->addresses.FindItem(addr_item, 0);
tmp2->addresses.RemoveAt(p); //remove from the list
delete del; //delete the item from memory
retchar = WriteToBuff(IDS_MSG4);
removed = TRUE;
}
else
{
retchar = WriteToBuff(IDS_MSG5);
*success = RET_NOTFOUND;
}
if (tmp2->addresses.IsEmpty())
{
POSITION p2 = CommList->FindItem(tmp);
if (p2)
{
CommListItem * del2 = (CommListItem *)CommList->FindItem(tmp, 0);
CommList->RemoveAt(p2); //remove from the list
delete del2; //delete the item from memory
if (retchar)
{
delete retchar;
retchar = WriteToBuff(IDS_MSG6);
}
else
retchar = WriteToBuff(IDS_MSG7);
}
}
}
else //not in the image, say so and indicate not found
{
retchar = WriteToBuff(IDS_MSG8);
*success = RET_NOTFOUND;
}
delete tmp;
delete addr_item;
}
return retchar;
}
//============================================================================
// CommandItem::CommandItem
//
// This is the CommandItem class's only constructor. This class is derived
// from the CObject class. This is so the MFC template storage classes can be
// used. It is used to store and process event to SNMP trap translation
// configuration requests.
//
//
// Parameters:
//
// CommandType comtype The requested command type. Currently either
// Add or Delete are only supported.
//
// CString * log The event log name.
//
// CString * src The event source name.
//
// DWORD cnt The number of times the event occurs before a
// trap should be generated.
//
// DWORD tm The time interval for the count to be reached.
//
// HKEY key The registry key HKEY_LOCAL_MACHINE for the
// machine that is being configured.
//
// Returns:
//
// none
//
//============================================================================
CommandItem::CommandItem(CommandType com, CString * log,
CString * src, DWORD id,
DWORD cnt, DWORD tm, HKEY key)
{
// Set the configuration values to be set in the registry
// ======================================================
command = com;
eventLog = log;
eventSource = src;
eventID = id;
hkey_machine = key;
// If the count is 0 make it 1, a valid value
// ==========================================
if(cnt)
count = cnt;
else
count = 1;
time = tm;
}
//============================================================================
// CommandItem::ModifyRegistry
//
// This public method is called to process the CommandItem. It modifies the
// the registry either bt editing, deleting or creating a key. The success
// of the operation is indicated by setting the second parameter. It returns
// a message contained in a CString which is to be written to the log.
//
//
// Parameters:
//
// SourceItem * srcItem A pointer to a structure containing additional
// information about this command. This needed to
// process the item.
//
//
// ReturnVal * success An indication of the success of the operation.
// Currently values reported are:
// RET_OK - success
// RET_BAD - failure
// RET_NOT_FOUND - Item to be deleted not found
//
// Returns:
//
// CString * A pointer to a CString containing a message
// to be written in the log. If this is non NULL
// the CString object being pointed to should be
// deleted after use.
//
//============================================================================
CString * CommandItem::ModifyRegistry(SourceItem * srcItem, ReturnVal * success)
{
CString * retchar = NULL;
*success = RET_OK;
// Get the command type and process the config request accordingly
// ===============================================================
switch(command)
{
case Add: //Do the add process
{
retchar = DoAdd(srcItem, success); //modifies the success parameter
break;
}
case Delete: //Do the delete process
{
retchar = DoDelete(srcItem, success); //modifies the success parameter
break;
}
default: //Should never get here!
break;
}
return retchar; //value returned by DoAdd or DoDelete
}
//============================================================================
// CommandItem::DoDelete
//
// This protected method is called to process the CommandItem of type "delete".
// It modifies the registry by deleting a key. This method should only be
// called by ModifyRegistry. The success of the operation is indicated by
// setting the second parameter. It returnsa message contained in a CString
// which is to be written to the log.
//
//
// Parameters:
//
// SourceItem * srcItem A pointer to a structure containing additional
// information about this command. This needed to
// process the item.
//
//
// ReturnVal * success An indication of the success of the operation.
// Currently values reported are:
// RET_OK - success
// RET_BAD - failure
// RET_NOT_FOUND - Item to be deleted not found
//
// Returns:
//
// CString * A pointer to a CString containing a message
// to be written in the log. This should never be
// NULL and the CString object being pointed to
// should be deleted after use.
//
//============================================================================
CString * CommandItem::DoDelete(SourceItem * srcItem, ReturnVal * success)
{
CString * retchar = NULL;
HKEY hkey;
CString keyname;
char evidstr[34];
_ultoa(eventID, evidstr, 10);
keyname.LoadString(IDS_SNMP_AGENT);
CString DirSep;
DirSep.LoadString(IDS_DIRSEP);
keyname += DirSep;
keyname += *eventSource;
keyname += DirSep;
keyname += evidstr;
// Open the key we need to delete so we can delete sub-keys
// ========================================================
LONG result = RegOpenKeyEx(hkey_machine, keyname,
0, KEY_ALL_ACCESS, &hkey);
if(result != ERROR_SUCCESS)
{
// Failed to open the key either it's not there or we have an error
// ================================================================
if(result == ERROR_FILE_NOT_FOUND)
*success = RET_NOTFOUND;
else
*success = RET_BAD;
return(WriteToBuff(IDS_MSG9));
}
// Enumerate and delete all subkeys, then close and delete this key
// ================================================================
retchar = DeleteSubKeys(&hkey);
RegCloseKey(hkey);
if(!retchar) //Deleting all sub-keys worked.
{
result = RegDeleteKey(hkey_machine, keyname);
if(result != ERROR_SUCCESS)
{
*success = RET_BAD;
return(WriteToBuff(IDS_MSG10));
}
retchar = WriteToBuff(IDS_MSG11);
// If the last entry for this source is deleted, delete the source
// ===============================================================
CString keyN;
keyN.LoadString(IDS_SNMP_AGENT);
keyN += DirSep;
keyN += *eventSource;
result = RegOpenKeyEx(hkey_machine, keyN,
0, KEY_ALL_ACCESS, &hkey);
if(result == ERROR_SUCCESS) //this should always be true
{
char Buffer[1024 + 1];
DWORD dwLength = 1024 +1;
result = RegEnumKeyEx(hkey, 0, Buffer, &dwLength, NULL,
NULL, NULL, NULL);
RegCloseKey(hkey);
if (result == ERROR_NO_MORE_ITEMS) //we can delete it!
{
RegDeleteKey(hkey_machine, keyN);
}
}
}
else //Deleteing all sub-keys failed
{
*success = RET_BAD;
}
return retchar;
}
//============================================================================
// CommandItem::DeleteSubKeys
//
// This protected method is called to delete all subkeys under the registry
// key passed as the only parameter. It returns a apointer to a CString which
// contains a message, if there was an error, which is to be written to the
// log. This method is recursive...it calls DeleteKey which in turn calls
// this method.
//
//
// Parameters:
//
// HKEY * hKey A pointer to the registry key whose subkeys are to be
// deleted.
//
// Returns:
//
// CString * A pointer to a CString containing an error message to
// be written in the log. This should be NULL if there
// were no errors. The CString object being pointed to
// should be deleted after use.
//
//============================================================================
CString * CommandItem::DeleteSubKeys(HKEY * hKey)
{
CString * retchar = NULL;
char Buffer[1024 + 1];
DWORD dwLength;
int i = 0;
LONG Result = ERROR_SUCCESS;
// Enumerate all subkeys and delete them
// =====================================
while (TRUE)
{
dwLength = 1024 + 1;
Result = RegEnumKeyEx(*hKey, i, Buffer, &dwLength, NULL,
NULL, NULL, NULL);
if (Result != ERROR_SUCCESS)
break;
if (dwLength > 0)
{
CString CBuffer(Buffer);
// This will delete the key
// ========================
retchar = DeleteKey(hKey, &CBuffer);
if (retchar) //there was an error
return retchar;
}
i++;
}
// Did we find a normal end condition?
// ===================================
if (Result == ERROR_NO_MORE_ITEMS)
return retchar; //no errors
return (WriteToBuff(IDS_MSG12)); //log an error
}
//============================================================================
// CommandItem::DeleteKey
//
// This protected method is called to delete the registry key indicateded by
// the second parameter. The first parameter is an open registry key, the
// second parameter is the keyname (full path relative to the first parameter).
// This method returns a pointer to a CString which contains a message, if
// there was an error, which is to be written to the log.
//
//
// Parameters:
//
// HKEY * hKey An open registry key.
//
// CString * key The name of the key to be deleted (full path relative
// to the first parameter).
//
// Returns:
//
// CString * A pointer to a CString containing an error message to
// be written in the log. This should be NULL if there
// were no errors. The CString object being pointed to
// should be deleted after use.
//
//============================================================================
CString * CommandItem::DeleteKey(HKEY * hKey, CString * key)
{
CString * retchar = NULL;
HKEY hk;
// Open the key to be deleted so we can tell if it has any subkeys
// ===============================================================
LONG result = RegOpenKeyEx(*hKey, *key,
0, KEY_ALL_ACCESS, &hk);
if(result != ERROR_SUCCESS)
{
return(WriteToBuff(IDS_MSG13));
}
// Enumerate and delete all subkeys, then close and delete this key
// ================================================================
retchar = DeleteSubKeys(&hk);
RegCloseKey(hk);
if(!retchar)
{
result = RegDeleteKey(*hKey, *key); //delete the key
if(result != ERROR_SUCCESS)
{
return(WriteToBuff(IDS_MSG14));
}
}
return retchar;
}
//============================================================================
// CommandItem::DoDelete
//
// This protected method is called to process the CommandItem of type "add".
// It modifies the registry by editing or creating a key. This method should
// only be called by ModifyRegistry. The success of the operation is indicated
// by setting the second parameter. It returns a pointer to a CString which
// contains a message to be written to the log.
//
//
// Parameters:
//
// SourceItem * srcItem A pointer to a structure containing additional
// information about this command. This needed to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -