📄 eventcmt.cpp
字号:
// Is SNMP installed?
// ==================
LONG Result = RegOpenKeyEx(hkey_machine, keyName,
0, KEY_EXECUTE, &hkeyOpen);
if (Result != ERROR_SUCCESS)
{
SetError(EVCMT_NO_SNMP);
SNMPinstalled = FALSE;
StatusMif(IDS_NO_SNMP_INSTALLED, FALSE);
}
else
SNMPinstalled = TRUE;
RegCloseKey(hkeyOpen);
keyName.Empty();
keyName.LoadString(IDS_SNMP_AGENT);
// Is the SNMP sub-agent SNMPELEA.DLL installed?
// =============================================
Result = RegOpenKeyEx(hkey_machine, keyName,
0, KEY_EXECUTE, &hkeyOpen);
if (Result != ERROR_SUCCESS)
{
SetError(EVCMT_NO_SNMP_XN);
return FALSE;
}
RegCloseKey(hkeyOpen);
// Is somebody else editing our piece of the registry
// ==================================================
keyName.Empty();
keyName.LoadString(IDS_LOCK_REG);
Result = RegCreateKeyEx(hkey_machine, keyName, 0,
NULL, REG_OPTION_VOLATILE,
KEY_ALL_ACCESS, NULL, &hkeyOpen, &createtype);
if (Result != ERROR_SUCCESS)
{
StatusMif(IDS_NOT_LOCKED, FALSE);
SetError(EVCMT_LOCK_FAIL);
return FALSE;
}
RegCloseKey(hkeyOpen);
if (createtype == REG_OPENED_EXISTING_KEY)
{
StatusMif(IDS_REG_LOCKED, FALSE);
SetError(EVCMT_LOCK_OUT);
return FALSE;
}
keyName.Empty();
keyName.LoadString(IDS_BASE_KEY);
Result = RegOpenKeyEx(hkey_machine, keyName,
0, KEY_ALL_ACCESS, &hkeyOpen);
if (Result != ERROR_SUCCESS)
{
SetError(EVCMT_REG_FAIL);
return FALSE;
}
// Check and if necessary SET the configuration mode
// =================================================
DWORD mode;
if (SetCustom)
mode = EVENTCMT_CUSTOM_MODE;
else
mode = EVENTCMT_SYSTEM_MODE;
// /DEFAULT was NOT a command line switch
// ======================================
if (MandatoryMode)
{
// Set the mode required
// =====================
keyName.Empty();
keyName.LoadString(IDS_CONF_TYPE);
Result = RegSetValueEx(hkeyOpen, keyName, 0, REG_DWORD,
(CONST BYTE *)&mode, 4);
if (Result != ERROR_SUCCESS)
{
RegCloseKey(hkeyOpen);
keyName.Empty();
keyName.LoadString(IDS_LOCK_REG);
RegDeleteKey(hkey_machine, keyName);
SetError(EVCMT_REG_FAIL);
StatusMif(IDS_MODE_FAIL, FALSE);
return FALSE;
}
}
else // /DEFAULT specified, only run if allowed
{
TCHAR val[1024 + 1];
DWORD valsz;
DWORD type;
CString Conftype;
Conftype.LoadString(IDS_CONF_TYPE);
BOOL gotmode = FALSE;
// Get the current mode
// ====================
valsz = 1024 + 1;
Result = RegQueryValueEx(hkeyOpen, Conftype, NULL,
&type, (unsigned char*)val, &valsz);
if (Result != ERROR_SUCCESS) //didn't find the key for the mode
{
if (Result == ERROR_FILE_NOT_FOUND) //it's not there!
{
gotmode = FALSE;
}
else
{
RegCloseKey(hkeyOpen);
keyName.Empty();
keyName.LoadString(IDS_LOCK_REG);
RegDeleteKey(hkey_machine, keyName);
SetError(EVCMT_REG_FAIL);
return FALSE;
}
}
else //got the key now what's the current mode
{
if (valsz > 0)
{
if (type == REG_DWORD)
{
mode = * ((DWORD *)val);
gotmode = TRUE;
}
}
}
if (!gotmode || (mode == EVENTCMT_REQUST_MODE))
{
// No current mode or default requested
// ====================================
if (SetCustom) //Set to Custom mode requested
{
mode = EVENTCMT_CUSTOM_MODE;
}
else //Set to default mode
{
mode = EVENTCMT_SYSTEM_MODE;
}
keyName.Empty();
keyName.LoadString(IDS_CONF_TYPE);
Result = RegSetValueEx(hkeyOpen, keyName, 0, REG_DWORD,
(CONST BYTE *)&mode, 4);
if (Result != ERROR_SUCCESS)
{
RegCloseKey(hkeyOpen);
keyName.Empty();
keyName.LoadString(IDS_LOCK_REG);
RegDeleteKey(hkey_machine, keyName);
SetError(EVCMT_REG_FAIL);
StatusMif(IDS_MODE_FAIL, FALSE);
return FALSE;
}
}
else if (gotmode && (mode == EVENTCMT_CUSTOM_MODE))
{
// The current mode is Custom
// ==========================
RegCloseKey(hkeyOpen);
keyName.Empty();
keyName.LoadString(IDS_LOCK_REG);
RegDeleteKey(hkey_machine, keyName);
SetError(EVCMT_CUSTOM_SET);
StatusMif(IDS_DFLT_CONFLICT, FALSE);
return FALSE;
}
else if (gotmode && (mode == EVENTCMT_SYSTEM_MODE) && SetCustom)
{
// The current mode is the default and /SETCUSTOM specified
// ========================================================
mode = EVENTCMT_CUSTOM_MODE;
keyName.Empty();
keyName.LoadString(IDS_CONF_TYPE);
Result = RegSetValueEx(hkeyOpen, keyName, 0, REG_DWORD,
(CONST BYTE *)&mode, 4);
if (Result != ERROR_SUCCESS)
{
RegCloseKey(hkeyOpen);
keyName.Empty();
keyName.LoadString(IDS_LOCK_REG);
RegDeleteKey(hkey_machine, keyName);
SetError(EVCMT_REG_FAIL);
StatusMif(IDS_MODE_FAIL, FALSE);
return FALSE;
}
}
}
RegCloseKey(hkeyOpen);
return TRUE;
}
//============================================================================
// EventConfigModifier::ProcessTrapQ
//
// This private method is called to process the list of trap destinations.
//
//
// Parameters:
//
// none
//
//
// Returns:
//
// none
//
//
//============================================================================
void EventConfigModifier::ProcessTrapQ()
{
// Get the first item from the TrapQ
// =================================
TrapCommandItem* TCmnd = (TrapCommandItem*)TrapQ.Pop();
if (!TCmnd)
{
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG51);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
return;
}
// Get the current SNMP trap destination settings, we'll edit this
// in memory and then write the changes back once we have finished
// ===============================================================
if (!ReadSNMPRegistry())
{
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG36);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
StatusMif(IDS_REGREAD,FALSE);
SetError(EVCMT_REG_FAIL);
return;
}
// Loop through the TrapQ adding to and deleting from
// CommNames as you go to build a new registry image.
// ==================================================
while (TCmnd)
{
// Process the current configuration request
// =========================================
ReturnVal retval;
CString * msg = TCmnd->Process(&CommNames, &retval);
if(retval != RET_OK) //No change was made...
{
if (retval == RET_BAD) //An error occurred
{
StatusMif(IDS_SYNTAX_ERROR, FALSE);
SetError(EVCMT_INVALID_COMMAND);
}
else if (TCmnd->GetCommand() != AddTrap) //RET_NOT_FOUND && DeleteTrap
{
StatusMif(IDS_DEL_MISS_ENTRY, TRUE);
}
}
else //Successful modification
{
SNMPModified = TRUE;
}
if (msg)
{
WriteToLog(msg);
delete msg;
}
// Delete this configuration request nd get the next one...
// ========================================================
delete TCmnd;
TCmnd = (TrapCommandItem*)TrapQ.Pop();
}
// Finished processing the TrapQ. If we made changes, write
// all changes to the registry and stop the SNMP service
// ========================================================
if (SNMPModified)
{
if (WriteSNMPRegistry())
{
StopSNMP();
}
}
else
{
CString sstr;
sstr += NL;
sstr += NL;
CString messg;
messg.LoadString(IDS_MSG51);
sstr += messg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
}
// Start the SNMP service regardless of if we stopped it
// =====================================================
StartSNMP();
}
//============================================================================
// EventConfigModifier::StopSNMP
//
// This private method is called to stop the SNMP service.
//
//
// Parameters:
//
// none
//
//
// Returns:
//
// none
//
//
//============================================================================
void EventConfigModifier::StopSNMP()
{
// Check that /NOSTOPSTART was NOT specified
// =========================================
if (!SNMPStopStart)
{
SetError(EVCMT_SNMP_STOPSTART);
return;
}
// Get a handle to the service manager so we can get the SNMP service
// ==================================================================
SC_HANDLE services;
if (Machine.GetLength())
{
services = OpenSCManager(Machine, //the remote machine
"ServicesActive", //active services
GENERIC_EXECUTE); //want to interrogate, start and stop a service
}
else
{
services = OpenSCManager(NULL, //NULL for local machine
"ServicesActive", //active services
GENERIC_EXECUTE); //want to interrogate, start and stop a service
}
if (!services) //didn't connect to the service control manager, log it
{
DWORD its_broke = GetLastError();
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG37);
sstr += msg;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG38);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
StatusMif(IDS_STARTSTOPSNMP, FALSE);
SetError(EVCMT_SNMP_STOPSTART);
SNMPStopStart = FALSE; //don't try start.
return;
}
// Get a handle to the SNMP service
// ================================
SC_HANDLE snmp_service = OpenService(services, "SNMP", SERVICE_CONTROL_INTERROGATE |
SERVICE_START | SERVICE_STOP);
if (!snmp_service) //didn't get a handle to the SNMP service, log it
{
DWORD its_broke = GetLastError();
CloseServiceHandle(services);
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG37);
sstr += msg;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG38);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
StatusMif(IDS_STARTSTOPSNMP, FALSE);
SetError(EVCMT_SNMP_STOPSTART);
SNMPStopStart = FALSE; //don't try start.
return;
}
SERVICE_STATUS stat;
// Send the message to stop the service
// ====================================
if (!ControlService(snmp_service, SERVICE_CONTROL_STOP, &stat))
{
DWORD its_broke = GetLastError();
BOOL flag_error = FALSE;
if (stat.dwCurrentState == SERVICE_STOP_PENDING)
{
Sleep(10000); //ten second wait
if (ControlService(snmp_service, SERVICE_CONTROL_INTERROGATE, &stat))
{
if (stat.dwCurrentState != SERVICE_STOPPED) //has it stopped?
{
flag_error = TRUE; //raise an error
}
}
else
{
flag_error = TRUE; //couldn't contact service? raise error!
}
}
else if (stat.dwCurrentState != SERVICE_STOPPED && its_broke != ERROR_SERVICE_NOT_ACTIVE)
{
flag_error = TRUE; //raise an error if the service is not stopped.
}
if (flag_error)
{
CloseServiceHandle(snmp_service);
CloseServiceHandle(services);
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG37);
sstr += msg;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG38);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
StatusMif(IDS_STARTSTOPSNMP, FALSE);
SetError(EVCMT_SNMP_STOPSTART);
SNMPStopStart = FALSE; //don't try start.
return;
}
}
CloseServiceHandle(snmp_service);
CloseServiceHandle(services);
}
//============================================================================
// EventConfigModifier::StartSNMP
//
// This private method is called to start the SNMP service.
//
//
// Parameters:
//
// none
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -