home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / authfilt.c < prev    next >
Text File  |  1997-10-25  |  7KB  |  281 lines

  1. /*++
  2.  
  3. Copyright (c) 1996  Microsoft Corporation
  4.  
  5. This program is released into the public domain for any purpose.
  6.  
  7.  
  8. Module Name:
  9.  
  10.     authfilt.c
  11.  
  12. Abstract:
  13.  
  14.     This module is an example of an ISAPI Authentication Filter.
  15.  
  16.     It demonstrates how to do an authentication filter based on an external
  17.     datasource.  Though this sample uses a flat file, access to a database
  18.     could easily be plugged in.
  19.  
  20. --*/
  21.  
  22. #include <windows.h>
  23. #include <httpfilt.h>
  24. #include "authfilt.h"
  25.  
  26. //
  27. // Functions
  28. //
  29.  
  30. BOOL
  31. WINAPI
  32. DllMain(
  33.      IN HINSTANCE hinstDll,
  34.      IN DWORD     fdwReason,
  35.      IN LPVOID    lpvContext OPTIONAL
  36.      )
  37. /*++
  38.  
  39.  Routine Description:
  40.  
  41.    This function DllLibMain() is the main initialization function for
  42.     this DLL. It initializes local variables and prepares it to be invoked
  43.     subsequently.
  44.  
  45.  Arguments:
  46.  
  47.    hinstDll          Instance Handle of the DLL
  48.    fdwReason         Reason why NT called this DLL
  49.    lpvReserved       Reserved parameter for future use.
  50.  
  51.  Return Value:
  52.  
  53.     Returns TRUE is successful; otherwise FALSE is returned.
  54. --*/
  55. {
  56.     BOOL        fReturn = TRUE;
  57.  
  58.     switch (fdwReason )
  59.     {
  60.     case DLL_PROCESS_ATTACH:
  61.  
  62.         if ( !InitializeUserDatabase() ||
  63.              !InitializeCache() )
  64.         {
  65.             DbgWrite(( DEST,
  66.                        "[GetFilterVersion] Database or cache failed, error %d\n",
  67.                        GetLastError() ))
  68.  
  69.             return FALSE;
  70.         }
  71.  
  72.         //
  73.         //  We don't care about thread attach/detach notifications
  74.         //
  75.  
  76.         DisableThreadLibraryCalls( hinstDll );
  77.  
  78.         break;
  79.  
  80.     case DLL_PROCESS_DETACH:
  81.         {
  82.  
  83.             if ( lpvContext != NULL)
  84.             {
  85.                 TerminateCache();
  86.                 TerminateUserDatabase();
  87.             }
  88.  
  89.             break;
  90.         } /* case DLL_PROCESS_DETACH */
  91.  
  92.     default:
  93.         break;
  94.     }   /* switch */
  95.  
  96.     return ( fReturn);
  97. }  /* DllLibMain() */
  98.  
  99.  
  100.  
  101. BOOL
  102. WINAPI
  103. GetFilterVersion(
  104.     HTTP_FILTER_VERSION * pVer
  105.     )
  106. {
  107.     DbgWrite(( DEST,
  108.                "[GetFilterVersion] Server filter version is %d.%d\n",
  109.                HIWORD( pVer->dwServerFilterVersion ),
  110.                LOWORD( pVer->dwServerFilterVersion ) ));
  111.  
  112.     pVer->dwFilterVersion = HTTP_FILTER_REVISION;
  113.  
  114.     //
  115.     //  Specify the types and order of notification
  116.     //
  117.  
  118.     pVer->dwFlags = (SF_NOTIFY_SECURE_PORT        |
  119.                      SF_NOTIFY_NONSECURE_PORT     |
  120.  
  121.                      SF_NOTIFY_AUTHENTICATION     |
  122.                      SF_NOTIFY_LOG                |
  123.  
  124.                      SF_NOTIFY_ORDER_DEFAULT);
  125.  
  126.     strcpy( pVer->lpszFilterDesc, "Sample Authentication Filter, version 1.0" );
  127.  
  128.     return TRUE;
  129. }
  130.  
  131. DWORD
  132. WINAPI
  133. HttpFilterProc(
  134.     HTTP_FILTER_CONTEXT *      pfc,
  135.     DWORD                      NotificationType,
  136.     VOID *                     pvData
  137.     )
  138. /*++
  139.  
  140. Routine Description:
  141.  
  142.     Filter notification entry point
  143.  
  144. Arguments:
  145.  
  146.     pfc -              Filter context
  147.     NotificationType - Type of notification
  148.     pvData -           Notification specific data
  149.  
  150. Return Value:
  151.  
  152.     One of the SF_STATUS response codes
  153.  
  154. --*/
  155. {
  156.     BOOL                  fAllowed;
  157.     CHAR                  achUser[SF_MAX_USERNAME];
  158.     HTTP_FILTER_AUTHENT * pAuth;
  159.     HTTP_FILTER_LOG *     pLog;
  160.     CHAR *                pch;
  161.  
  162.     //
  163.     //  Handle this notification
  164.     //
  165.  
  166.     switch ( NotificationType )
  167.     {
  168.     case SF_NOTIFY_AUTHENTICATION:
  169.  
  170.         pAuth = (HTTP_FILTER_AUTHENT *) pvData;
  171.  
  172.         //
  173.         //  Ignore the anonymous user
  174.         //
  175.  
  176.         if ( !*pAuth->pszUser )
  177.         {
  178.             //
  179.             //  Tell the server to notify any subsequent notifications in the
  180.             //  chain
  181.             //
  182.  
  183.             return SF_STATUS_REQ_NEXT_NOTIFICATION;
  184.         }
  185.  
  186.         //
  187.         //  Save the unmapped username so we can log it later
  188.         //
  189.  
  190.         strcpy( achUser, pAuth->pszUser );
  191.  
  192.         //
  193.         //  Make sure this user is a valid user and map to the appropriate
  194.         //  Windows NT user
  195.         //
  196.  
  197.         if ( !ValidateUser( pAuth->pszUser,
  198.                             pAuth->pszPassword,
  199.                             &fAllowed ))
  200.         {
  201.             DbgWrite(( DEST,
  202.                        "[OnAuthentication] Error %d validating user %s\n",
  203.                        GetLastError(),
  204.                        pAuth->pszUser ));
  205.  
  206.             return SF_STATUS_REQ_ERROR;
  207.         }
  208.  
  209.         if ( !fAllowed )
  210.         {
  211.             //
  212.             //  This user isn't allowed access.  Indicate this to the server
  213.             //
  214.  
  215.             SetLastError( ERROR_ACCESS_DENIED );
  216.  
  217.             return SF_STATUS_REQ_ERROR;
  218.         }
  219.  
  220.         //
  221.         //  Save the unmapped user name so we can log it later on.  We allocate
  222.         //  enough space for two usernames so we can use this memory block
  223.         //  for logging.  Note we may have already allocated it from a previous
  224.         //  request on this TCP session
  225.         //
  226.  
  227.         if ( !pfc->pFilterContext )
  228.         {
  229.             pfc->pFilterContext = pfc->AllocMem( pfc, 2 * SF_MAX_USERNAME + 4, 0 );
  230.  
  231.             if ( !pfc->pFilterContext )
  232.             {
  233.                 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  234.                 return SF_STATUS_REQ_ERROR;
  235.             }
  236.         }
  237.  
  238.         strcpy( (CHAR *) pfc->pFilterContext, achUser );
  239.  
  240.         return SF_STATUS_REQ_HANDLED_NOTIFICATION;
  241.  
  242.     case SF_NOTIFY_LOG:
  243.  
  244.         //
  245.         //  The unmapped username is in pFilterContext if this filter
  246.         //  authenticated this user
  247.         //
  248.  
  249.         if ( pfc->pFilterContext )
  250.         {
  251.             pch  = pfc->pFilterContext;
  252.             pLog = (HTTP_FILTER_LOG *) pvData;
  253.  
  254.             //
  255.             //  Put both the original username and the NT mapped username
  256.             //  into the log in the form "Original User (NT User)"
  257.             //
  258.  
  259.             strcat( pch, " (" );
  260.             strcat( pch, pLog->pszClientUserName );
  261.             strcat( pch, ")" );
  262.  
  263.             pLog->pszClientUserName = pch;
  264.         }
  265.  
  266.         return SF_STATUS_REQ_NEXT_NOTIFICATION;
  267.  
  268.     default:
  269.         DbgWrite(( DEST,
  270.                 "[HttpFilterProc] Unknown notification type, %d\n",
  271.                 NotificationType ));
  272.  
  273.         break;
  274.     }
  275.  
  276.     return SF_STATUS_REQ_NEXT_NOTIFICATION;
  277. }
  278.  
  279.  
  280.  
  281.