mod_isapi.c revision 597d160db0361084da89b9786a2f1b213385cc24
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* mod_isapi.c - Internet Server Application (ISA) module for Apache
* by Alexei Kosut <akosut@apache.org>
*
* This module implements Microsoft's ISAPI, allowing Apache (when running
* under Windows) to load Internet Server Applications (ISAPI extensions).
* It implements all of the ISAPI 2.0 specification, except for the
* "Microsoft-only" extensions dealing with asynchronous I/O. All ISAPI
* extensions that use only synchronous I/O and are compatible with the
* ISAPI 2.0 specification should work (most ISAPI 1.0 extensions should
* function as well).
*
* To load, simply place the ISA in a location in the document tree.
* Then add an "AddHandler isapi-isa dll" into your config file.
* You should now be able to load ISAPI DLLs just be reffering to their
* URLs. Make sure the ExecCGI option is active in the directory
* the ISA is in.
*/
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_log.h"
#include "util_script.h"
#include "apr_portable.h"
#include "apr_strings.h"
/* We use the exact same header file as the original */
#include <HttpExt.h>
/* TODO: Unknown errors that must be researched for correct codes */
#define TODO_ERROR 1
/* Seems IIS does not enforce the requirement for \r\n termination on HSE_REQ_SEND_RESPONSE_HEADER,
define this to conform */
#define RELAX_HEADER_RULE
static int LogNotSupported = -1;
static int AppendLogToErrors = 0;
static int AppendLogToQuery = 0;
/* Declare the ISAPI functions */
/*
The optimiser blows it totally here. What happens is that autos are addressed relative to the
stack pointer, which, of course, moves around. The optimiser seems to lose track of it somewhere
between setting HttpExtensionProc's address and calling through it. We work around the problem by
forcing it to use frame pointers.
The revisions below may eliminate this artifact.
*/
/* Our loaded isapi module description structure */
typedef struct {
int refcount;
} isapi_loaded;
/* Our "Connection ID" structure */
typedef struct {
request_rec *r;
} isapi_cid;
{
apr_table_t *e = r->subprocess_env;
char *fspec;
char *p;
int res;
/* Use similar restrictions as CGIs
*
* If this fails, it's pointless to load the isapi dll.
*/
if (!(ap_allow_options(r) & OPT_EXECCGI))
return HTTP_FORBIDDEN;
return HTTP_NOT_FOUND;
return HTTP_FORBIDDEN;
/* Load the module...
* per PR2555, the LoadLibraryEx function is very picky about slashes.
* Debugging on NT 4 SP 6a reveals First Chance Exception within NTDLL.
* LoadLibrary in the MS PSDK also reveals that it -explicitly- states
* that backslashes must be used.
*
* Transpose '\' for '/' in the filename.
*/
while (*p) {
if (*p == '/')
*p = '\\';
++p;
}
/* Load the module
*
* TODO: Critical section
*
* Warning: cid should not be allocated from request pool if we
* cache the isapi process in-memory.
*
* This code could use cacheing... everything that follows
* should only be performed on the first isapi dll invocation,
* not with every HttpExtensionProc()
*/
/* TODO: These may need to become overrideable, so that we
* assure a given isapi can be fooled into behaving well.
*/
"ISAPI %s failed to load", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (!(isa->GetExtensionVersion =
"ISAPI %s is missing GetExtensionVersion()",
r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (!(isa->HttpExtensionProc =
"ISAPI %s is missing HttpExtensionProc()",
r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* TerminateExtension() is an optional interface */
/* Run GetExtensionVersion() */
/* ### euh... we're passing the wrong type of error code here */
"ISAPI %s call GetExtensionVersion() failed",
r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Load of this module completed, this is the point at which *isa
* could be cached for later invocation.
*
* on to invoking this request...
*/
/* Set up variables */
ap_add_cgi_vars(r);
/* Set up connection structure and ecb */
/* TODO: Critical section */
cid->r = r;
// TODO: are copies really needed here?
(char*) apr_table_get(e, "QUERY_STRING"));
(char*) apr_table_get(e, "PATH_INFO"));
(char*) apr_table_get(e, "PATH_TRANSLATED"));
(char*) apr_table_get(e, "CONTENT_TYPE"));
/* Set up the callbacks */
/* Set up client input */
if (isa->TerminateExtension) {
}
}
if (ap_should_client_block(r)) {
/* Time to start reading the appropriate amount of data,
* and allow the administrator to tweak the number
* TODO: add the httpd.conf option for ReadAheadBuffer.
*/
if (r->remaining) {
else
}
else
{
}
read = 0;
}
if (res < 0) {
goto contentfailure;
}
/* Although its not to spec, IIS seems to null-terminate
* its lpdData string. So we will too.
*/
if (res == 0)
else
}
else {
}
/* All right... try and run the sucker */
/* Set the status (for logging) */
}
/* Check for a log message - and log it */
case HSE_STATUS_SUCCESS:
/* Ignore the keepalive stuff; Apache handles it just fine without
* the ISA's "advice".
* Per Microsoft: "In IIS versions 4.0 and later, the return
* values HSE_STATUS_SUCCESS and HSE_STATUS_SUCCESS_AND_KEEP_CONN
* are functionally identical: Keep-Alive connections are
* maintained, if supported by the client."
* ... so we were pat all this time
*/
break;
case HSE_STATUS_PENDING:
/* emulating async behavior...
*
* Create a cid->completed event and wait on it for some timeout
* so that the app thinks is it running async.
*
* All async ServerSupportFunction calls will be handled through
* the registered IO_COMPLETION hook.
*/
if (LogNotSupported)
{
"ISAPI %s asynch I/O request refused",
r->filename);
}
}
else {
== WAIT_TIMEOUT) {
/* TODO: Now what... if this hung, then do we kill our own
* thread to force it's death? For now leave timeout = -1
*/
}
}
break;
case HSE_STATUS_ERROR:
/* end response if we have yet to do so.
*/
break;
default:
/* TODO: log unrecognized retval for debugging
*/
break;
}
/* All done with the DLL... get rid of it...
*
* If optionally cached, pass HSE_TERM_ADVISORY_UNLOAD,
* and if it returns TRUE, unload, otherwise, cache it.
*/
if (isa->TerminateExtension) {
}
/* TODO: Crit section */
}
{
apr_table_t *e = r->subprocess_env;
const char *result;
/* Mostly, we just grab it from the environment, but there are
* a couple of special cases
*/
/* We don't support NT users, so this is always the same as
* REMOTE_USER
*/
}
/* Apache doesn't support secure requests inherently, so
* we have no way of knowing. We'll be conservative, and say
* all requests are insecure.
*/
result = "0";
}
}
else {
}
if (result) {
return FALSE;
}
return TRUE;
}
/* Didn't find it */
return FALSE;
}
{
int writ; /* written, actually, but why shouldn't I make up words? */
/* We only support synchronous writing */
if (LogNotSupported)
"ISAPI %s asynch I/O request refused",
r->filename);
return FALSE;
}
return FALSE;
}
*lpwdwBytes = writ;
return TRUE;
}
{
int res;
if (res < 0) {
*lpdwSize = 0;
if (!GetLastError())
return FALSE;
}
}
return TRUE;
}
{
int termarg;
char *termch;
stat = "Status: 200 OK";
}
else {
char *newstat;
if (statlen == 0)
/* Whoops... not NULL terminated */
}
head = "\r\n";
}
/* Whoops... not NULL terminated */
}
/* Parse them out, or die trying */
/* All the headers should be set now */
ap_send_http_header(cid->r);
/* Any data left should now be sent directly,
* it may be raw if headlen was provided.
*/
}
return FALSE;
return TRUE;
}
/* XXX: Is there is still an O(n^2) attack possible here? Please detail. */
{
request_rec *r = cid->r;
switch (dwHSERequest) {
case 1: /* HSE_REQ_SEND_URL_REDIRECT_RESP */
/* Set the status to be returned when the HttpExtensionProc()
* is done.
* WARNING: Microsoft now advertises HSE_REQ_SEND_URL_REDIRECT_RESP
* and HSE_REQ_SEND_URL as equivalant per the Jan 2000 SDK.
* They most definately are not, even in their own samples.
*/
return TRUE;
case 2: /* HSE_REQ_SEND_URL */
/* Soak up remaining input */
if (r->remaining > 0) {
char argsbuffer[HUGE_STRING_LEN];
}
/* Reset the method to GET */
r->method_number = M_GET;
/* Don't let anyone think there's still data */
/* AV fault per PR3598 - redirected path is lost! */
ap_internal_redirect((char*)lpvBuffer, r);
return TRUE;
case 3: /* HSE_REQ_SEND_RESPONSE_HEADER */
/* Parse them out, or die trying */
/* Signal to resume the thread completing this request
*/
return TRUE;
case 1001: /* HSE_REQ_MAP_URL_TO_PATH */
{
/* Map a URL to a filename */
/* IIS puts a trailing slash on directories, Apache doesn't */
if (l < *lpdwSize - 1) {
file[l] = '\\';
}
}
return TRUE;
}
case 1002: /* HSE_REQ_GET_SSPI_INFO */
if (LogNotSupported)
"ISAPI ServerSupportFunction HSE_REQ_GET_SSPI_INFO "
"is not supported: %s", r->filename);
return FALSE;
case 1003: /* HSE_APPEND_LOG_PARAMETER */
/* Log lpvBuffer, of lpdwSize bytes, in the URI Query (cs-uri-query) field
*/
if (AppendLogToQuery) {
if (r->args)
else
}
if (AppendLogToErrors)
(char*) lpvBuffer);
return TRUE;
case 1005: /* HSE_REQ_IO_COMPLETION */
/* TODO: Emulate a completion port, if we can...
* Record the callback address and user defined argument...
* we will call this after any async request (e.g. transmitfile)
* as if the request had completed async execution.
* Per MS docs... HSE_REQ_IO_COMPLETION replaces any prior call
* to HSE_REQ_IO_COMPLETION, and lpvBuffer may be set to NULL.
*/
return FALSE;
return TRUE;
case 1006: /* HSE_REQ_TRANSMIT_FILE */
/* Use TransmitFile... nothing wrong with that :)
* Just not quite ready yet...
*/
if (LogNotSupported)
"ISAPI asynchronous I/O not supported: %s",
r->filename);
return FALSE;
case 1007: /* HSE_REQ_REFRESH_ISAPI_ACL */
if (LogNotSupported)
"ISAPI ServerSupportFunction "
"HSE_REQ_REFRESH_ISAPI_ACL "
"is not supported: %s", r->filename);
return FALSE;
case 1008: /* HSE_REQ_IS_KEEP_CONN */
return TRUE;
case 1010: /* HSE_REQ_ASYNC_READ_CLIENT */
if (LogNotSupported)
"ISAPI asynchronous I/O not supported: %s",
r->filename);
return FALSE;
case 1011: /* HSE_REQ_GET_IMPERSONATION_TOKEN Added in ISAPI 4.0 */
if (LogNotSupported)
"ISAPI ServerSupportFunction "
"HSE_REQ_GET_IMPERSONATION_TOKEN "
"is not supported: %s", r->filename);
return FALSE;
case 1012: /* HSE_REQ_MAP_URL_TO_PATH_EX */
{
/* Map a URL to a filename */
/* Mapping started with assuming both strings matched.
* Now roll on the path_info as a mismatch and handle
* terminating slashes for directory matches.
*/
/* roll forward over path_info's first slash */
++info->cchMatchingPath;
++info->cchMatchingURL;
}
}
/* Add a trailing slash for directory */
}
/* If the matched isn't a file, roll match back to the prior slash */
break;
--info->cchMatchingPath;
--info->cchMatchingURL;
}
}
/* Paths returned with back slashes */
if (*test_uri == '/')
*test_uri = '\\';
/* is a combination of:
* HSE_URL_FLAGS_READ 0x001 Allow read
* HSE_URL_FLAGS_WRITE 0x002 Allow write
* HSE_URL_FLAGS_EXECUTE 0x004 Allow execute
* HSE_URL_FLAGS_SSL 0x008 Require SSL
* HSE_URL_FLAGS_DONT_CACHE 0x010 Don't cache (VRoot only)
* HSE_URL_FLAGS_NEGO_CERT 0x020 Allow client SSL cert
* HSE_URL_FLAGS_REQUIRE_CERT 0x040 Require client SSL cert
* HSE_URL_FLAGS_MAP_CERT 0x080 Map client SSL cert to account
* HSE_URL_FLAGS_SSL128 0x100 Require 128-bit SSL cert
* HSE_URL_FLAGS_SCRIPT 0x200 Allow script execution
*
* XxX: As everywhere, EXEC flags could use some work...
* and this could go further with more flags, as desired.
*/
return TRUE;
}
case 1014: /* HSE_REQ_ABORTIVE_CLOSE */
if (LogNotSupported)
"ISAPI ServerSupportFunction HSE_REQ_ABORTIVE_CLOSE"
" is not supported: %s", r->filename);
return FALSE;
case 1015: /* HSE_REQ_GET_CERT_INFO_EX Added in ISAPI 4.0 */
if (LogNotSupported)
"ISAPI ServerSupportFunction "
"HSE_REQ_GET_CERT_INFO_EX "
"is not supported: %s", r->filename);
return FALSE;
case 1016: /* HSE_REQ_SEND_RESPONSE_HEADER_EX Added in ISAPI 4.0 */
{
/* XXX: ignore shi->fKeepConn? We shouldn't need the advise */
/* r->connection->keepalive = shi->fKeepConn; */
}
case 1017: /* HSE_REQ_CLOSE_CONNECTION Added after ISAPI 4.0 */
if (LogNotSupported)
"ISAPI ServerSupportFunction "
"HSE_REQ_CLOSE_CONNECTION "
"is not supported: %s", r->filename);
return FALSE;
case 1018: /* HSE_REQ_IS_CONNECTED Added after ISAPI 4.0 */
/* Returns True if client is connected c.f. MSKB Q188346
* XXX: That statement is very ambigious... assuming the
* identical return mechanism as HSE_REQ_IS_KEEP_CONN.
*/
return TRUE;
case 1020: /* HSE_REQ_EXTENSION_TRIGGER Added after ISAPI 4.0 */
/* Undocumented - defined by the Microsoft Jan '00 Platform SDK
*/
if (LogNotSupported)
"ISAPI ServerSupportFunction "
"HSE_REQ_EXTENSION_TRIGGER "
"is not supported: %s", r->filename);
return FALSE;
default:
if (LogNotSupported)
"ISAPI ServerSupportFunction (%d) not supported: "
return FALSE;
}
}
/*
* Command handler for the ISAPIReadAheadBuffer directive, which is TAKE1
*/
char *arg)
{
long val;
return err;
}
return "ISAPIReadAheadBuffer must be a legitimate value.";
return NULL;
}
/*
* Command handler for the ISAPIReadAheadBuffer directive, which is TAKE1
*/
char *arg)
{
return err;
}
LogNotSupported = -1;
}
LogNotSupported = 0;
}
else {
return "ISAPILogNotSupported must be on or off";
}
return NULL;
}
char *arg)
{
return err;
}
AppendLogToErrors = -1;
}
AppendLogToErrors = 0;
}
else {
return "ISAPIAppendLogToErrors must be on or off";
}
return NULL;
}
char *arg)
{
return err;
}
AppendLogToQuery = -1;
}
AppendLogToQuery = 0;
}
else {
return "ISAPIAppendLogToQuery must be on or off";
}
return NULL;
}
static const command_rec isapi_cmds[] = {
"Maximum bytes to initially pass to the ISAPI handler" },
"Log requests not supported by the ISAPI server" },
"Send all Append Log requests to the error log" },
"Append Log requests are concatinated to the query args" },
{ NULL }
};
handler_rec isapi_handlers[] = {
{ "isapi-isa", isapi_handler },
{ NULL}
};
module isapi_module = {
NULL, /* create per-dir config */
NULL, /* merge per-dir config */
NULL, /* server config */
NULL, /* merge server config */
isapi_cmds, /* command apr_table_t */
isapi_handlers, /* handlers */
NULL /* register hooks */
};