⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgpservice.cpp

📁 vc环境下的pgp源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________
	Copyright (c) 1998 Network Associates, Inc. and its Affiliated Companies
	All rights reserved.

	Implementation of CService. This is a modified MS sample.

	$Id: pgpService.cpp,v 1.2.6.1 1999/06/14 03:22:39 elowe Exp $
____________________________________________________________________________*/

#include <windows.h>
#include <stdio.h>
#include <string>

#include "pgpNetIPC.h"
#include "pgpNetDebugLog.h"
#include "pgpNetLogUtils.h"
#include "pgpService.h"

// externals
extern DWORD g_platformID;

// static variables
CService*	CService::m_pThis			= NULL;
std::string CService::m_szServiceName	= "PGPnetIKE";
std::string CService::m_szValueName		= "PGPnetIKE.EXE";
std::string CService::m_szDisplayName	= PGPNET_SERVICEWINDOWNAME;
PGPInt32	CService::m_iMajorVersion	= 1;
PGPInt32	CService::m_iMinorVersion	= 0;
HANDLE		CService::m_hEventSource	= 0;

typedef int (CALLBACK *REGPROC)(DWORD,DWORD);

CService::CService(const char* szServiceName, 
				   const char *szDisplayName)
{
	// copy the address of the current object so we can access it from
	// the static member callback functions. 
	// WARNING: This limits the application to only one CService object. 
	m_pThis = this;

	// Set the default service name and version
	m_szServiceName = szServiceName;
	m_szDisplayName = szDisplayName;

	m_iMajorVersion = 1;
	m_iMinorVersion = 0;
	m_hEventSource = NULL;

	// set up the initial service status 
	m_hServiceStatus = NULL;
	m_Status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
	m_Status.dwCurrentState = SERVICE_STOPPED;
	m_Status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
	m_Status.dwWin32ExitCode = 0;
	m_Status.dwServiceSpecificExitCode = 0;
	m_Status.dwCheckPoint = 0;
	m_Status.dwWaitHint = 0;
	m_commandLine = FALSE;
	
	(void) PGPCondCreate(&m_StopEvent, 0);	// XXX error checking
}

CService::~CService()
{
	if (m_hEventSource) {
		DeregisterEventSource(m_hEventSource);
	}
	PGPCondDestroy(&m_StopEvent); // XXX error checking
}

//
// Default command line argument parsing
//
// Returns TRUE if it found an arg it recognised, FALSE if not
// Note: processing some arguments causes output to stdout to be generated.
//
PGPBoolean CService::ParseStandardArgs(int argc, char* argv[])
{
	// See if we have any command line args we recognise
	if (argc <= 1)
		return FALSE;
	
	if (_stricmp(argv[1], "-v") == 0) {
		return TRUE; // say we processed the argument
	} else if (_stricmp(argv[1], "-i") == 0) {
		// Request to install.
		if (!IsInstalled()) {
			// Try and install the copy that's running
			Install();
		}
		return TRUE; // say we processed the argument
	} else if (_stricmp(argv[1], "-u") == 0) {
		// Request to uninstall.
		if (IsInstalled()) {
			// Try and remove the copy that's installed
			Uninstall();
		}
		return TRUE; // say we processed the argument
	}
	// Don't recognise the args
	return FALSE;
}

//
// Install/uninstall routines
//
// Test if the service is currently installed
//
PGPBoolean CService::IsInstalled()
{
	PGPBoolean bResult = FALSE;

	if (g_platformID == VER_PLATFORM_WIN32_WINDOWS) {
		HKEY hKey = NULL;
		DWORD type = 0, size = 0;
		LONG ret;
		std::string szKey = 
			"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunServices";
	
		// check for existence of key
		ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
			szKey.c_str(),
			0,
			KEY_ALL_ACCESS,
			&hKey);

		if (ret != ERROR_SUCCESS)
			return FALSE;
	
		// query value for our name
		ret = RegQueryValueEx(hKey,
			m_szValueName.c_str(),
			0,
			&type,
			0,
			&size);

		if (ret != ERROR_SUCCESS)
			return FALSE;
	
		// make sure that there is some data there
		if (size > 0)
			return TRUE;

		return FALSE;
	} else if (g_platformID == VER_PLATFORM_WIN32_NT) {
		// Open the Service Control Manager
		SC_HANDLE hSCM = OpenSCManager(NULL,	// local machine
								   NULL,	// ServicesActive database
								   SC_MANAGER_ALL_ACCESS);	// full access
		if (hSCM) {
			// Try to open the service
			SC_HANDLE hService = OpenService(hSCM,
										 m_szServiceName.c_str(),
										 SERVICE_QUERY_CONFIG);
			if (hService) {
				bResult = TRUE;
				CloseServiceHandle(hService);
			}
			CloseServiceHandle(hSCM);
		}
	}
	return bResult;
}

PGPBoolean
CService::Install()
{
	if (g_platformID == VER_PLATFORM_WIN32_WINDOWS) {
		// Get the executable file path
		char szFilePath[_MAX_PATH];
		GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));

		HKEY hKey = NULL;
		std::string szKey = 
			"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunServices";
		DWORD disp;
	
		if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
						 szKey.c_str(),
						 0, 
						 0, 
						 REG_OPTION_NON_VOLATILE, 
						 KEY_ALL_ACCESS, 
						 0, 
						 &hKey, 
						 &disp) != ERROR_SUCCESS)
			return FALSE;
	
		int err = RegSetValueEx(hKey,
			m_szValueName.c_str(),
			0,
			REG_EXPAND_SZ,
			(CONST BYTE*)szFilePath,
			strlen(szFilePath) + 1);     
	
		RegCloseKey(hKey);
	
		if (err != ERROR_SUCCESS)
			return FALSE;
	
		return TRUE;
	} else if (g_platformID == VER_PLATFORM_WIN32_NT) {
	// Open the Service Control Manager
	SC_HANDLE hSCM = OpenSCManager(NULL,	// local machine
								   NULL,	// ServicesActive database
								   SC_MANAGER_ALL_ACCESS);	// full access
	if (!hSCM)
		return FALSE;

	// Get the executable file path
	char szFilePath[_MAX_PATH];
	GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));

	// Create the service
	SC_HANDLE hService = CreateService(
		hSCM,
		m_szServiceName.c_str(),
		m_szDisplayName.c_str(),
		SERVICE_ALL_ACCESS,
		SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
		SERVICE_AUTO_START, 
		SERVICE_ERROR_NORMAL,
		szFilePath,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL);
	if (!hService) {
		CloseServiceHandle(hSCM);
		return FALSE;
	}

	// make registry entries to support logging messages
	// Add the source name as a subkey under the Application
	// key in the EventLog service portion of the registry.
	std::string szKey;
	HKEY hKey = NULL;
	szKey = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
	szKey += m_szServiceName;

	if (RegCreateKey(HKEY_LOCAL_MACHINE, szKey.c_str(), &hKey) != ERROR_SUCCESS) {
		CloseServiceHandle(hService);
		CloseServiceHandle(hSCM);
		return FALSE;
	}
	
	// Add the Event ID message-file name to the 'EventMessageFile' subkey.
	RegSetValueEx(hKey,
				  "EventMessageFile",
				  0,
				  REG_EXPAND_SZ, 
				  (CONST BYTE*)szFilePath,
				  strlen(szFilePath) + 1);     

	// Set the supported types flags.
	DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
		EVENTLOG_INFORMATION_TYPE;
	RegSetValueEx(hKey,
				  "TypesSupported",
				  0,
				  REG_DWORD,
				  (CONST BYTE*)&dwData,
				  sizeof(DWORD));
	RegCloseKey(hKey);
	
	LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_INSTALLED, m_szServiceName.c_str());
	
	// tidy up
	CloseServiceHandle(hService);
	CloseServiceHandle(hSCM);
	return TRUE;
	}
	return FALSE;
}

PGPBoolean
CService::Uninstall()
{
	PGPBoolean bResult = FALSE;

	if (g_platformID == VER_PLATFORM_WIN32_WINDOWS) {
		PGPBoolean err = TRUE;
		HKEY hKey = NULL;
		std::string szKey =
			"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunServices";
	
		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey.c_str(), 0, KEY_ALL_ACCESS, &hKey)
			!= ERROR_SUCCESS)
			return FALSE;
	
		if (RegDeleteValue(hKey, m_szValueName.c_str()) != ERROR_SUCCESS)
			err = FALSE;
	
		RegCloseKey(hKey);

		return err;
	} else if (g_platformID == VER_PLATFORM_WIN32_NT) {
		// Open the Service Control Manager
		SC_HANDLE hSCM = OpenSCManager(NULL,	// local machine
								   NULL,	// ServicesActive database
								   SC_MANAGER_ALL_ACCESS);	// full access
		if (!hSCM) return FALSE;
	
		SC_HANDLE hService = OpenService(hSCM,
									 m_szServiceName.c_str(),
									 DELETE);
		if (hService) {
			if (DeleteService(hService)) {
				LogEvent(EVENTLOG_INFORMATION_TYPE,
						EVMSG_REMOVED,
						m_szServiceName.c_str());
				bResult = TRUE;
			} else {
				LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_NOTREMOVED, m_szServiceName.c_str());
			}
			CloseServiceHandle(hService);
		}
	
		CloseServiceHandle(hSCM);
	}
	return bResult;
}

//
// Logging functions
//
// This function makes an entry into the application event log
//
void
CService::LogEvent(WORD wType, DWORD dwID,
					 const char* pszS1,
					 const char* pszS2,
					 const char* pszS3)
{
	if (g_platformID == VER_PLATFORM_WIN32_NT) {
		const char* ps[3];
		ps[0] = pszS1;
		ps[1] = pszS2;
		ps[2] = pszS3;

		int iStr = 0;
		for (int i = 0; i < 3; i++) {
			if (ps[i] != NULL) iStr++;
		}
	
		// Check the event source has been registered and if
		// not then register it now
		if (!m_hEventSource) {
			m_hEventSource = RegisterEventSource(NULL,	 // local machine
											 m_szServiceName.c_str()); // source name
		}
	
		if (m_hEventSource) {
			ReportEvent(m_hEventSource,
						wType,
						0,
						dwID,
						NULL, // sid
						iStr,
						0,
						ps,
						NULL);
		}
	}
}

//
// Service startup and registration
//
PGPBoolean
CService::StartService()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -