ptnet.c

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C语言 代码 · 共 1,751 行 · 第 1/3 页

C
1,751
字号
/*__________________________________________________________________________
 Copyright (C) 2002 PGP Corporation
 All rights reserved.
 
 $Id: PTnet.c,v 1.35 2002/08/06 20:10:33 dallen Exp $
__________________________________________________________________________*/
#include "pgpPFLConfig.h"
#include "precomp.h"

#include "pgpWin32IPC.h"
#include "PTnet.h"
#include "pgpClientPrefs.h"
#include "pgpNetPrefs.h"
#include "pgpNetAttack.h"
#include "pgpNetAttackStrings.h"
#include <winsock.h>

#define IPC_TIMEOUT				2500
#define FLASH_TIMER_MS			1000
#define KILLDELAYMS				3000
#define NUM_HOSTS_FOR_POPUP		16
#define	NUM_SA_FLASHES			30

#define NORMALINDENT			4
#define CHILDINDENT				16
#define TEXTINDENT				24
#define NOHOSTSINDENT			14
#define DOTOFFSET				20
#define BITMAPHEIGHT			16
#define BITMAPWIDTH				16
#define HALFBITMAPWIDTH			(BITMAPWIDTH>>1)

#define MENU_HEADER				1
#define MENU_FOOTER				2
#define MENU_NOHOSTS			3
#define HEADER_HEIGHT			34
#define FOOTER_HEIGHT			6
#define EDGE_WIDTH				2
#define HEADER_BITMAP_WIDTH		106
#define HEADER_BITMAP_HEIGHT	30

#define NETTRAYDEBUGLOG			PGP_DEBUG && FALSE

typedef struct _SORTEDHOSTLIST {
	struct _SORTEDHOSTLIST*	pNext;
	struct _SORTEDHOSTLIST*	pChild;
	INT						iIndex;
	BOOL					bChild;
} SORTEDHOSTLIST, *PSORTEDHOSTLIST;

static BOOL			s_bGUIEnabled		= TRUE;
static PGPBoolean	s_bNetEnabled		= FALSE;
static PGPBoolean	s_bIconEnabled		= TRUE; // wjb from FALSE
static INT			s_iFlashCount		= 0;
static UINT			s_uAttackType		= kPGPnetAttack_None;
static DWORD		s_dwAttackerIP		= 0;
static UINT			s_uServiceStatus	= PGPNET_SERVICENOTAVAILABLE;
static HIMAGELIST	s_himlIcons			= NULL;
static HFONT		s_hfontMenu			= NULL;
static UINT			s_uMenuHeight		= 0;
static UINT			s_uBitmapOffset		= 0;
static BOOL			s_bMenuStructsValid = FALSE;

static PGPNetPrefHostEntry*	s_pHostList		= NULL;
static UINT					s_uHostCount	= 0;
static PGPipsecSA*			s_pSAList		= NULL;
static UINT					s_uSACount		= 0;
static PSORTEDHOSTLIST		s_pSortedList	= NULL;
static HIMAGELIST			s_himlPGPnet	= NULL;

#if NETTRAYDEBUGLOG
#include <time.h>

sLogText (LPSTR psz)
{
	CHAR	sz[128];
	time_t	tt;
	FILE*	fp;

	time (&tt);
	lstrcpy (sz, ctime (&tt));

	lstrcat (sz, ":");
	lstrcat (sz, psz);
	lstrcat (sz, "\n");

	fp = fopen ("\\PGPtrayDebug.txt", "a+t");
	fwrite (sz, sizeof(char), lstrlen(sz), fp);
	fclose (fp);
}
#endif	// NETTRAYDEBUGLOG

//	________________________
//
//	destroy sorted list

VOID
sFreeSortedList (
	PSORTEDHOSTLIST		p)
{
	PSORTEDHOSTLIST		pNext;

	while (p)
	{
		if (p->pChild)
			sFreeSortedList (p->pChild);

		pNext = p->pNext;
		free (p);
		p = pNext;
	}
}

//	________________________
//
//	create sorted linked-list of hosts

VOID
sInsertHostIntoList (
		PSORTEDHOSTLIST			pThis,
		LPSTR					psz,
		PSORTEDHOSTLIST*		pp)
{
	while (*pp)
	{
		if (lstrcmpi (psz, s_pHostList[(*pp)->iIndex].hostName) <= 0)
			break;

		pp = &((*pp)->pNext);
	}
	pThis->pNext = *pp;
	*pp = pThis;
}

//	________________________
//
//	create sorted linked-list of hosts

VOID
sSortHosts (void)
{
	PSORTEDHOSTLIST		pThis;
	PSORTEDHOSTLIST		p;
	UINT				u;

	sFreeSortedList (s_pSortedList);
	s_pSortedList = NULL;

	for (u=0; u<s_uHostCount; u++)
	{
		pThis = malloc (sizeof (SORTEDHOSTLIST));
		if (pThis)
		{
			pThis->pNext = NULL;
			pThis->pChild = NULL;
			pThis->iIndex = u;
			pThis->bChild = FALSE;

			if (s_pSortedList == NULL)
			{
				s_pSortedList = pThis;
				continue;
			}

			if (s_pHostList[u].childOf == -1)
			{
				sInsertHostIntoList (pThis, 
						s_pHostList[u].hostName, &s_pSortedList);
			}
			else
			{
				pThis->bChild = TRUE;
				p = s_pSortedList;

				while (p)
				{
					if (p->iIndex == s_pHostList[u].childOf)
					{
						if (p->pChild == NULL)
							p->pChild = pThis;
						else
						{
							sInsertHostIntoList (pThis,
									s_pHostList[u].hostName, 
									&(p->pChild));
						}
						break;
					}
					p = p->pNext;
				}
			}
		}
	}
}

//	________________________
//
//	Launch PGPnet.exe, specifying the start page

VOID
PTNetLaunch (
		HWND	hwnd,
		UINT	uStartPage)
{
	PGPError	err					= kPGPError_OutputBufferTooSmall;
	CHAR		szFile[MAX_PATH];
	CHAR		sz[16];

	err = PGPclGetPath (kPGPclInstallFolder, szFile, sizeof(szFile));

	if (IsntPGPError (err)) 
	{
		if ((lstrlen (szFile) + lstrlen (PGP_NETAPPNAME)) <= sizeof(szFile))
		{
			lstrcat (szFile, PGP_NETAPPNAME);

			if ((lstrlen (szFile) +4) <= sizeof(szFile))
			{
				wsprintf (sz, " /%i", uStartPage);
				lstrcat (szFile, sz);

				if (!PGPclExecute (szFile, SW_SHOW))
					err = kPGPError_FileNotFound;
				else
					err = kPGPError_NoErr;
			}
		}
	}

	if (IsPGPError (err))
	{
		PTMessageBox (hwnd, IDS_CAPTION, IDS_CANNOTLAUNCHPGPNET,
							MB_OK|MB_ICONSTOP);
	}
}

//	________________________
//
//	query PGPnet App to see if we should flash or not

static BOOL 
sShouldFlash (VOID)
{
	HWND	hwnd;
	DWORD	dwResult;

	hwnd = FindWindow (PGPNET_APPWINDOWCLASS, 
									PGPNET_APPWINDOWTITLE);
	if (hwnd)
	{
		if (SendMessageTimeout (hwnd, PGPNET_M_APPMESSAGE, PGPNET_FLASHICON,
					TRUE, SMTO_NORMAL, IPC_TIMEOUT, &dwResult))
		{
			return (BOOL)dwResult;
		}
		else
			return TRUE;
	}
	else
		return TRUE;
}

//	________________________
//
//	Send message to service, if running

static BOOL 
sSendServiceMessage (
		UINT	uMsg,
		WPARAM	wParam,
		LPARAM	lParam)
{
	HWND	hwnd;
	DWORD	dwResult;

	hwnd = FindWindow (PGPNET_SERVICECOMMWINDOWNAME, 
									PGPNET_SERVICECOMMWINDOWNAME);
	if (hwnd)
	{
		if (SendMessageTimeout (hwnd, uMsg, wParam, lParam, 
							SMTO_NORMAL, IPC_TIMEOUT, &dwResult))
		{
			return TRUE;
		}
		else
			return FALSE;
	}
	else
		return FALSE;
}

//	________________________
//
//	Send logon, logoff, or query message to service

BOOL
PTSendLogOnOffMessage (
		HWND	hwnd,
		UINT	uMessage)
{
	BOOL	bReturn		= FALSE;

	switch (uMessage) {
	case QUERYSERVICE :
		bReturn = TRUE;
		break;

	case REPORTPROCESSID :
		bReturn = sSendServiceMessage (
				PGPNET_M_APPMESSAGE, PGPNET_USERPROCESSID, 
				(LPARAM)GetCurrentProcessId ());
		break;
	}

	return bReturn;
}


//	________________________
//
//	if intrusion has been flagged, stop blinking and start timer

BOOL
PTIntrusionInProgress (VOID)
{
	if (s_uAttackType != kPGPnetAttack_None)
	{
		if (s_iFlashCount < 0)
			s_iFlashCount = 1;

		return TRUE;
	}
	return FALSE;
}


//	________________________
//
//	Query service for status

static UINT 
sGetServiceStatus (
		HWND	hwnd)
{
	HWND	hwndService;
	DWORD	dwResult;

	hwndService = FindWindow (PGPNET_SERVICECOMMWINDOWNAME, 
									PGPNET_SERVICECOMMWINDOWNAME);
	if (hwndService)
	{
		if (SendMessageTimeout (hwndService, PGPNET_M_APPMESSAGE, 
				(WPARAM)PGPNET_QUERYDRIVERSTATUS, (LPARAM)hwnd,
				SMTO_NORMAL, IPC_TIMEOUT, &dwResult))
		{
			return dwResult;
		}
		else 
			return PGPNET_SERVICENOTRESPONDING;
	}
	else
		return PGPNET_SERVICENOTAVAILABLE;
}


//	________________________
//
//	create string description of attack

static VOID 
sGetAttackDescription (
		LPSTR		psz,
		INT			iLen,
		UINT		uAttackType,
		DWORD		dwIP)
{
	CHAR			szIP[16];
	CHAR			szAttack[32];
	CHAR			szFormat[64];
	struct in_addr	ia;

	ia.S_un.S_addr = s_dwAttackerIP;
	lstrcpy (szIP, inet_ntoa (ia));

	if (uAttackType >= kPGPnetAttack_FirstFirewallRule)
	{
		wsprintf (szAttack, "%i", 
				uAttackType-kPGPnetAttack_FirstFirewallRule+1);
		LoadString (g_hinst, 
				IDS_FIREWALLVIOLATIONFORMAT, szFormat, sizeof(szFormat));
	}
	else
	{
		PGPGetNetAttackString (uAttackType, sizeof(szAttack), szAttack);
		LoadString (g_hinst, IDS_ATTACKFORMAT, szFormat, sizeof(szFormat));
	}

	if ((lstrlen (szAttack) + lstrlen (szIP) + lstrlen(szFormat)) < iLen)	
		wsprintf (psz, szFormat, szAttack, szIP);
}


//	________________________
//
//	Get icon and tooltip text based on driver/service status

static HICON
sGetIconAndText (
		HWND	hwnd,
		LPSTR	psz,
		UINT	uLen)
{
	HICON	hicon	= NULL;
	CHAR	sz[64];

	// if default icon is loaded up, that means that PGPnet is not installed,
	// so just return the default icon
	if (g_hIconTrayDefault)
	{
		LoadString (g_hinst, IDS_SZAPP, psz, uLen);
		return g_hIconTrayDefault;
	}

	s_uServiceStatus = sGetServiceStatus (hwnd);
	switch (s_uServiceStatus) {
	case PGPNET_DRIVERNOTAVAILABLE :
		hicon = LoadImage (g_hinst, MAKEINTRESOURCE(IDI_BROKENNETTRAYICON),
				IMAGE_ICON, g_uTrayIconSelector, g_uTrayIconSelector, 0);
		LoadString (g_hinst, IDS_DRIVERNOTAVAIL, psz, uLen);
		break;

	case PGPNET_DRIVERENABLED :
		if (s_uSACount == 0)
		{
			hicon = LoadImage (g_hinst, MAKEINTRESOURCE(IDI_NETTRAYICON),
					IMAGE_ICON, g_uTrayIconSelector, g_uTrayIconSelector, 0);
		}
		else
		{
			hicon = LoadImage (g_hinst, MAKEINTRESOURCE(IDI_SANETTRAYICON),
					IMAGE_ICON, g_uTrayIconSelector, g_uTrayIconSelector, 0);
		}

		// Only display SA's if VPN is installed 
		// (for instances of fire but not vpn)
		if(PGPclIsComponentInstalled (kPGPclVPN))
		{
			LoadString (g_hinst, IDS_DRIVERENABLED, sz, uLen);
			wsprintf (psz, sz, s_uSACount);
		}
		else
		{
			LoadString (g_hinst, IDS_SZAPP, psz, uLen);
		}
		break;

	case PGPNET_DRIVERDISABLED :
		hicon = LoadImage (g_hinst, MAKEINTRESOURCE(IDI_TRAYICON),
				IMAGE_ICON, g_uTrayIconSelector, g_uTrayIconSelector, 0);
		LoadString (g_hinst, IDS_DRIVERDISABLED, psz, uLen);
		break;

	case PGPNET_SERVICENOTRESPONDING :
		s_bGUIEnabled = TRUE;
		hicon = LoadImage (g_hinst, MAKEINTRESOURCE(IDI_BROKENNETTRAYICON),
				IMAGE_ICON, g_uTrayIconSelector, g_uTrayIconSelector, 0);
		LoadString (g_hinst, IDS_SERVICENOTRESPOND, psz, uLen);
		break;

	case PGPNET_SERVICENOTAVAILABLE :
	default :
		s_bGUIEnabled = TRUE;
		hicon = LoadImage (g_hinst, MAKEINTRESOURCE(IDI_BROKENNETTRAYICON),
				IMAGE_ICON, g_uTrayIconSelector, g_uTrayIconSelector, 0);
		LoadString (g_hinst, IDS_SERVICENOTAVAIL, psz, uLen);
		break;
	}

	if (s_iFlashCount != 0)
	{
		if (s_uAttackType == kPGPnetAttack_None)
		{
			if (s_iFlashCount & 0x1)
			{
				hicon = LoadImage (g_hinst, MAKEINTRESOURCE(IDI_SA2NETTRAYICON),
						IMAGE_ICON, g_uTrayIconSelector, g_uTrayIconSelector, 0);
			}
			else
			{
				hicon = LoadImage (g_hinst, MAKEINTRESOURCE(IDI_SANETTRAYICON),
						IMAGE_ICON, g_uTrayIconSelector, g_uTrayIconSelector, 0);
			}
		}
		else
		{
			if (s_iFlashCount & 0x1)
			{
				hicon = LoadIcon (
						g_hinst, MAKEINTRESOURCE (IDI_INTRUDERTRAYICON));
			}
			sGetAttackDescription (psz, 
					uLen, s_uAttackType, s_dwAttackerIP);
		}
	}

	return hicon;
}


//_________________________________________
//
//	update system tray icon

BOOL 
PTUpdateTrayIconAndText (
		HWND	hwnd,
		BOOL	bUpdateText,
		BOOL	bForceAddIcon)
{
	BOOL			result;
	NOTIFYICONDATA	nid;
	CHAR			sz[128];
	static BOOL		bIconIsVisible = FALSE;

	memset (&nid, 0x00, sizeof(NOTIFYICONDATA));
	nid.cbSize				= sizeof(NOTIFYICONDATA);
	nid.hWnd				= hwnd;
	nid.uID					= TASKBAR_TRAY_ID;
	nid.uFlags				= NIF_ICON;
	if (bUpdateText)
		nid.uFlags |= NIF_TIP;

	if (s_bIconEnabled)
	{
		nid.hIcon = sGetIconAndText (hwnd, sz, sizeof(sz));
		lstrcpy (nid.szTip, sz);

		if (bIconIsVisible && !bForceAddIcon)
			result = Shell_NotifyIcon (NIM_MODIFY, &nid);
		else
		{
			nid.uCallbackMessage = WM_TASKAREA_MESSAGE;
			nid.uFlags |= NIF_MESSAGE;
			result = Shell_NotifyIcon (NIM_ADD, &nid);
			if (result)
				bIconIsVisible = TRUE;
		}

		DestroyIcon(nid.hIcon);
	}
	else
	{
		if (bIconIsVisible)
		{
			result = Shell_NotifyIcon (NIM_DELETE, &nid);
			if (result)
				bIconIsVisible = FALSE;
		}

		if (s_iFlashCount < 0)
			s_iFlashCount = 1;
	}

	return result;
}


//	________________________
//
//	process the intruder alert message

static VOID
sIntruderAlert (
		HWND						hwnd,
		PGPIntruderAlertStructure*	ppias)
{
	#if NETTRAYDEBUGLOG
	{
		CHAR	sz[64];
		wsprintf (sz, "got PGPNET_INTRUDERALERT; attackType = %li",
						ppias->attackType);
		sLogText (sz);
	}
	#endif	// NETTRAYDEBUGLOG

⌨️ 快捷键说明

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