home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c14 / lab03 / ex02 / emponly.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.6 KB  |  128 lines

  1. // EMPONLY.CPP - Implementation file for your Internet Server
  2. //    EmpOnly Filter
  3.  
  4. #include "stdafx.h"
  5. #include "EmpOnly.h"
  6.  
  7. // Do not edit the following lines, which are needed by ClassWizard.
  8. #if 0
  9. BEGIN_MESSAGE_MAP(CEmpOnlyFilter, CHttpFilter)
  10.     //{{AFX_MSG_MAP(CEmpOnlyFilter)
  11.     //}}AFX_MSG_MAP
  12. END_MESSAGE_MAP()
  13. #endif    // 0
  14.  
  15. ///////////////////////////////////////////////////////////////////////
  16. // The one and only CWinApp object
  17. CWinApp theApp;
  18.  
  19. ///////////////////////////////////////////////////////////////////////
  20. // The one and only CEmpOnlyFilter object
  21. CEmpOnlyFilter theFilter;
  22.  
  23. ///////////////////////////////////////////////////////////////////////
  24. // CEmpOnlyFilter implementation
  25. CEmpOnlyFilter::CEmpOnlyFilter()
  26. {
  27.     // Read in employee "database" text file - Employ.dat.  
  28.     // Employ.dat must be located in Scripts subdirectory.
  29.     // Since ctor is only called when EmpOnly.dll is loaded into memory, 
  30.     // any changes to database demands that web service be restarted.
  31.  
  32.     CString buf;
  33.  
  34.     // This next path is specific to the web server installation!
  35.     // CStdioFile infile("c:\\Winnt351\\System32\\InetSrv\\Scripts\\Employ.dat",
  36.     //                     CFile::modeRead);
  37.     
  38.     CStdioFile infile("c:\\webshare\\Scripts\\Employ.dat",
  39.                         CFile::modeRead);
  40.     
  41.     while (infile.ReadString(buf))
  42.     {
  43.         strAuthUsers += buf + "; ";
  44.         buf.Empty();
  45.     }    
  46. }
  47.  
  48. CEmpOnlyFilter::~CEmpOnlyFilter()
  49. {
  50. }
  51.  
  52. BOOL CEmpOnlyFilter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
  53. {
  54.     // Call default implementation for initialization
  55.     CHttpFilter::GetFilterVersion(pVer);
  56.  
  57.     // Clear the flags set by base class
  58.     pVer->dwFlags &= ~SF_NOTIFY_ORDER_MASK;
  59.  
  60.     // Set the flags we are interested in
  61.     pVer->dwFlags |= SF_NOTIFY_ORDER_MEDIUM | SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_URL_MAP;
  62.  
  63.     // Load description string
  64.     TCHAR sz[SF_MAX_FILTER_DESC_LEN+1];
  65.     ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
  66.             IDS_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
  67.     _tcscpy(pVer->lpszFilterDesc, sz);
  68.     return TRUE;
  69. }
  70.  
  71. DWORD CEmpOnlyFilter::OnUrlMap(CHttpFilterContext* pCtxt,
  72.     PHTTP_FILTER_URL_MAP pMapInfo)
  73. {
  74.     char pstrName[100], pstrIP[20], *pstrSearch;
  75.     DWORD dwSize;
  76.     BOOL bIsAuth = FALSE;
  77.  
  78.     //Check to see if client is requesting Employees Only page.
  79.     //Use of _strlwr is possible because NT does not differentiate on case.
  80.     _strlwr(pMapInfo->pszPhysicalPath);
  81.     pstrSearch = strstr(pMapInfo->pszPhysicalPath, "emponly.htm");
  82.     if (pstrSearch == NULL) //not asking for Employees Only page
  83.         return SF_STATUS_REQ_NEXT_NOTIFICATION;
  84.         
  85.     dwSize = sizeof(pstrName);
  86.     pCtxt->GetServerVariable("REMOTE_USER", pstrName, &dwSize);
  87.     if (dwSize > 5)  //check that is valid user (not anonymous)
  88.         bIsAuth = IsAuthUserName(pstrName);
  89.         
  90.     if (bIsAuth == FALSE) //if not authorized name, then check IP
  91.     {
  92.         dwSize = sizeof(pstrIP);
  93.         pCtxt->GetServerVariable("REMOTE_ADDR", pstrIP, &dwSize);
  94.         if(dwSize > 6) //should always have client's IP, but...
  95.             bIsAuth = IsAuthUserIP(pstrIP);
  96.     }
  97.  
  98.     if (bIsAuth == TRUE)  //if user is an authorized employee
  99.     {
  100.         return SF_STATUS_REQ_NEXT_NOTIFICATION;
  101.     }
  102.     else  //if not, then redirect to consolation page
  103.     {
  104.         strcpy(pstrSearch, "Consoltn.htm");
  105.         //preclude any other filter processing on this event
  106.         return SF_STATUS_REQ_HANDLED_NOTIFICATION;
  107.     }
  108. }
  109.  
  110.  
  111. BOOL CEmpOnlyFilter::IsAuthUserName(LPCSTR lpszUserName)
  112. {
  113.     int hit = strAuthUsers.Find(lpszUserName);
  114.     if (hit == -1)
  115.         return FALSE;
  116.     else
  117.         return TRUE;
  118. }
  119.  
  120. BOOL CEmpOnlyFilter::IsAuthUserIP(LPCSTR lpszIPAddress)
  121. {
  122.     int hit = strAuthUsers.Find(lpszIPAddress);
  123.     if (hit == -1)
  124.         return FALSE;
  125.     else
  126.         return TRUE;
  127. }
  128.