Hostnamefilter_src

csucdl 2009-02-05 07:13:02
// DB.CPP - This module implements the database routines for the authentication filter.

#include "stdafx.h"
#include "HostnameFilter.h"

/*
Routine Description:
Looks up the hostname and confirms the host is allowed access to the
server

Arguments:
pszHostname - The hostname to validate
pfValid - Set to TRUE if the host should be allowed

Return Value:
TRUE on success, FALSE on failure
*/
BOOL CHostnameFilter::ValidateHost(const CHAR* pszHostname, OUT BOOL* pfValid)
{
// Assume we're going to fail validation
*pfValid = FALSE;

BOOL fFound;

// Lookup the host in the cache, if that fails, get the host from the
// database and add the retrieved host to the cache
if ( !LookupHostInCache( pszHostname, &fFound ))
return FALSE;

if ( !fFound )
{
if ( !LookupHostInDb( pszHostname, &fFound ))
return FALSE;

if ( fFound )
AddHostToCache( pszHostname );
}

if ( !fFound )
{
ISAPITRACE1("[ValidateHost] Failed to find host %s\n", pszHostname );
return TRUE;
}

*pfValid = TRUE;

return TRUE;
}

/*
Routine Description:
Retrieves the hostlist from the file. If the hosts were coming from a
database, this routine would connect to the database.

Return Value:
TRUE on success, FALSE on failure

*/
BOOL CHostnameFilter::InitializeHostDatabase()
{
DWORD cbRead;

// Open and read the file. The System account must have access to the file.
HANDLE hFile = CreateFile( HOST_LIST_FILE, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if ( hFile == INVALID_HANDLE_VALUE )
{
ISAPITRACE2("[InitializeHostDatabase] Error %d openning %s\n", GetLastError(), HOST_LIST_FILE );
return FALSE;
}

DWORD cbFile = GetFileSize( hFile, NULL );
if ( cbFile == (DWORD) -1 )
{
CloseHandle( hFile );
return FALSE;
}

m_pszHostFile = (CHAR *)LocalAlloc( LPTR, cbFile + 1 );
if ( !m_pszHostFile )
{
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
CloseHandle( hFile );
return FALSE;
}

if ( !ReadFile( hFile, m_pszHostFile, cbFile, &cbRead, NULL ))
{
CloseHandle( hFile );
LocalFree( m_pszHostFile );
return FALSE;
}

CloseHandle( hFile );

// Zero terminate the file data
m_pszHostFile[cbRead] = '\0';

return TRUE;
}

/*
Routine Description:
Looks up the hostname in the database
The file data is not sorted to simulate the cost of an external database
lookup.

Arguments:
pszHostname - The hostname to find in the database (case insensitive)
pfFound - Set to TRUE if the specified host name was found in the
database

Return Value:
TRUE on success, FALSE on failure
*/
BOOL CHostnameFilter::LookupHostInDb(const CHAR* pszHostname, OUT BOOL* pfFound)
{
*pfFound = FALSE;

// Find the external hostname. We're expecting one host per line
CHAR* pch = m_pszHostFile;
CHAR* pszDomain = strchr( pszHostname, '.' );
if (pszDomain)
pszDomain++;
DWORD cchHost = strlen( pszHostname );

while ( pch && *pch )
{
while ( ISWHITE( *pch ) )
pch++;

if (*pch == '*')
{
if (pch[1] == '\r' || pch[1] == '\n') // * alone in a line?
goto Found;

pch += 2; // * and .
char* pDom = pszDomain;
while (pDom)
{
DWORD cchDomain = strlen( pDom );
if ( toupper( *pch ) == toupper( *pDom ) &&
!strnicmp( pDom, pch, cchDomain ) &&
(pch[cchDomain] == '\r' || pch[cchDomain] == '\n') )
{
goto Found;
}

pDom = strchr(pDom, '.');
if (pDom)
pDom++;
}
}
else
if ( toupper( *pch ) == toupper( *pszHostname ) &&
!strnicmp( pszHostname, pch, cchHost ) &&
(pch[cchHost] == '\r' || pch[cchHost] == '\n') )
{
goto Found;
}

pch = strchr( pch+1, '\n' );
}

// Not found
return TRUE;

Found:
*pfFound = TRUE;

return TRUE;
}

/*
Routine Description:
Shutsdown the host database.
*/
VOID CHostnameFilter::TerminateHostDatabase()
{
if ( m_pszHostFile )
LocalFree(m_pszHostFile );
}

// HOSTNAMEFILTER.CPP - Implementation file for your Internet Server
// Hostname Sample Filter

#include "stdafx.h"
#include "HostnameFilter.h"

#include <winsock2.h>

///////////////////////////////////////////////////////////////////////
// The one and only CWinApp object
// NOTE: You may remove this object if you alter your project to no
// longer use MFC in a DLL.

CWinApp theApp;



///////////////////////////////////////////////////////////////////////
// The one and only CHostnameFilter object

CHostnameFilter theFilter;

///////////////////////////////////////////////////////////////////////
// CHostnameFilter static members

DWORD CHostnameFilter::m_cCacheItems = 0;
CHAR* CHostnameFilter::m_pszHostFile = NULL;
LIST_ENTRY CHostnameFilter::m_CacheListHead;
CRITICAL_SECTION CHostnameFilter::m_csCacheLock;


///////////////////////////////////////////////////////////////////////
// CHostnameFilter implementation

CHostnameFilter::CHostnameFilter()
{
m_fCacheInitialized = FALSE;
}

CHostnameFilter::~CHostnameFilter()
{
TerminateCache();
TerminateHostDatabase();
WSACleanup();
}

BOOL CHostnameFilter::Initialize()
{
WORD wVersionRequested = MAKEWORD( 2, 0 );
WSADATA wsaData;
if ( WSAStartup( wVersionRequested, &wsaData ))
return FALSE;
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
return FALSE;

return (InitializeHostDatabase() && InitializeCache());
}

BOOL CHostnameFilter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
{
ISAPITRACE2( "[GetFilterVersion] Server filter version is %d.%d\n",
HIWORD( pVer->dwServerFilterVersion ),
LOWORD( pVer->dwServerFilterVersion ) );

if ( !Initialize() )
{
ISAPITRACE1("[GetFilterVersion] Database or cache failed, error %d\n", GetLastError() );
return FALSE;
}

// Call default implementation for initialization
CHttpFilter::GetFilterVersion(pVer);

// Set the flags we are interested in
pVer->dwFlags = SF_NOTIFY_ORDER_DEFAULT |
SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT |
SF_NOTIFY_URL_MAP;

// Load description string
TCHAR sz[SF_MAX_FILTER_DESC_LEN+1];
ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
IDS_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
_tcscpy(pVer->lpszFilterDesc, sz);
return TRUE;
}

DWORD CHostnameFilter::OnUrlMap(CHttpFilterContext* pCtxt, PHTTP_FILTER_URL_MAP pMapInfo)
{
char szAddress[80];
DWORD dwSize = sizeof(szAddress);
if (pCtxt->GetServerVariable("REMOTE_ADDR", szAddress, &dwSize))
{
char nAddr[4];
nAddr[0] = nAddr[1] = nAddr[2] = nAddr[3] = 0;
int nIndex = 0;
char *pstr = szAddress;
while (*pstr != '\0' && nIndex < 4)
{
if (*pstr == '.')
nIndex++;
else
{
nAddr[nIndex] *= 10;
nAddr[nIndex] += (*pstr - '0');
}
pstr++;
}
// perform DNS reverse lookup using winsock
HOSTENT* pResult;
pResult = gethostbyaddr((const char *)&nAddr, 4, PF_INET);
if (pResult == NULL)
strncpy(pMapInfo->pszPhysicalPath, NO_NAME, pMapInfo->cbPathBuff-1);
else
{
// Make sure this host is a valid host
BOOL fAllowed;
if ( !ValidateHost(pResult->h_name, &fAllowed ))
{
ISAPITRACE2("[OnUrlMap] Error %d validating host %s\n", GetLastError(), pResult->h_name );
return SF_STATUS_REQ_ERROR;
}

if ( !fAllowed )
{
// This host isn't allowed access. Map to information URL
strncpy(pMapInfo->pszPhysicalPath, NO_ACCESS, pMapInfo->cbPathBuff-1);
}
}
}

return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CHostnameFilter, CHttpFilter)
//{{AFX_MSG_MAP(CHostnameFilter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0


...全文
69 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧