PRB: ISAPI Filter AuthFilt Sample Causes Access Violation (239738)
The information in this article applies to:
- Microsoft Internet Information Server 4.0
- Microsoft Internet Information Services 5.0
- Microsoft Internet Information Services version 5.1
This article was previously published under Q239738 SYMPTOMS
The Microsoft Internet Information Server SDK contains an Internet Server Application Programming Interface (ISAPI) AuthFilt filter sample in the \InterPub\Iissamples\Sdk\Isapi\Filters\AuthFilt folder. When you use the AuthFilt sample directly, an access violation occurs.
CAUSE
The AuthFilt sample allocates a buffer on the SF_NOTIFY_AUTHENTICATION notification with AllocMem:
pfc->pFilterContext = pfc->AllocMem( pfc, 2 * SF_MAX_USERNAME + 4, 0 );
Then, the AuthFilt sample has string concatenations within the SF_NOTIFY_LOG event:
pch = pfc->pFilterContext;
pLog = (HTTP_FILTER_LOG *) pvData;
strcat( pch, " (" );
strcat( pch, pLog->pszClientUserName );
strcat( pch, ")" );
NOTE: Multiple requests occur on the same network session if the connection is keep-alive (authentication occurs only once per session). The filter is called multiple times in the SF_NOTIFY_LOG event within the same session. Because the pFilterContext buffer is maintained for the entire session, having a fixed size of 2 * SF_MAX_USERNAME + 4, an access violation occurs.
RESOLUTION
To work around this behavior, perform the following:
- Define a struct:
typedef struct
{
int iLength;
CHAR szUsername[ 2 * SF_MAX_USERNAME + 4 ];
} USERID;
- Define a new pointer at the beginning of the HttpFilterProc function:
USERID * pUser;
- Allocate a buffer to store the unmapped username and its length in SF_NOTIFY_AUTHENTICATION:
if ( !pfc->pFilterContext )
{
pfc->pFilterContext = pfc->AllocMem( pfc, sizeof ( USERID ), 0 );
if ( !pfc->pFilterContext )
{
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return SF_STATUS_REQ_ERROR;
}
}
pUser = pfc->pFilterContext;
strcpy(pUser->szUsername, achUser );
pUser->iLength = strlen ( achUser );
return SF_STATUS_REQ_HANDLED_NOTIFICATION;
- In the SF_NOTIFY_LOG event, set a NULL char at the end of the unmapped username. (This allows the strcat to append the mapped username to the unmapped username.)
if ( pfc->pFilterContext )
{
pUser = pfc->pFilterContext;
pUser->szUsername [ pUser->iLength ] = 0;
pch = pUser->szUsername;
pLog = ( HTTP_FILTER_LOG* ) pvData;
strcat( pch, " (" );
strcat( pch, pLog->pszClientUserName );
strcat( pch, ")" );
pLog->pszClientUserName = pch;
}
Modification Type: | Major | Last Reviewed: | 6/29/2004 |
---|
Keywords: | kbFilter kbprb KB239738 |
---|
|