mod_win32.c revision 1fbf6ba0f5207e6637b49f9a9dfcc779bbe952a9
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 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.
*/
#ifdef WIN32
#include "apr_strings.h"
#include "apr_portable.h"
#include "apr_buckets.h"
#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 "mod_core.h"
#include "mod_cgi.h"
#include "apr_lib.h"
#include "ap_regkey.h"
static int win_nt;
/*
* CGI Script stuff for Win32...
*/
typedef enum { INTERPRETER_SOURCE_UNSET, INTERPRETER_SOURCE_REGISTRY_STRICT,
char **interpreter,
char **arguments);
typedef struct {
/* Where to find interpreter to run scripts */
{
return conf;
}
{
return new;
}
char *arg)
{
}
}
}
else {
"\" must be \"registry\", \"registry-strict\" or "
"\"script\"", NULL);
}
return NULL;
}
/* XXX: prep_string should translate the string into unicode,
* such that it is compatible with whatever codepage the client
* will read characters 80-ff. For the moment, use the unicode
* values 0080-00ff. This isn't trivial, since the code page
* varies between msdos and Windows applications.
* For subsystem 2 [GUI] the default is the system Ansi CP.
* For subsystem 3 [CLI] the default is the system OEM CP.
*/
{
char *ch2;
int widen = 0;
if (!ch) {
return;
}
while (*ch) {
if (*(ch++) & 0x80) {
++widen;
}
}
if (!widen) {
return;
}
while (*ch) {
if (*ch & 0x80) {
/* sign extension won't hurt us here */
}
else {
}
}
*(ch2++) = '\0';
}
/* Somewhat more exciting ... figure out where the registry has stashed the
* ExecCGI or Open command - it may be nested one level deep (or more???)
*/
static char* get_interpreter_from_win32_registry(apr_pool_t *p,
const char* ext,
int strict)
{
char execcgi_path[] = "SHELL\\EXECCGI\\COMMAND";
char execopen_path[] = "SHELL\\OPEN\\COMMAND";
char *type_name;
char *buffer;
if (!ext) {
return NULL;
}
/*
* Future optimization:
* When the registry is successfully searched, store the strings for
* interpreter and arguments in an ext hash to speed up subsequent look-ups
*/
/* Open the key associated with the script filetype extension */
if (rv != APR_SUCCESS) {
return NULL;
}
/* Retrieve the name of the script filetype extension */
/* Open the key associated with the script filetype extension */
APR_READ, p);
}
/* Open the key for the script command path by:
*
*
* and if the strict arg is false, then continue trying:
*
*/
if (name_key) {
== APR_SUCCESS) {
}
}
== APR_SUCCESS) {
}
}
== APR_SUCCESS) {
}
}
== APR_SUCCESS) {
}
}
if (name_key) {
}
return NULL;
}
return buffer;
}
{
const char **arg;
int prgtaken = 0;
int argtaken = 0;
int inquo;
int sl;
while (*ch) {
/* Skip on through Deep Space */
if (apr_isspace(*ch)) {
++ch; continue;
}
/* One Arg */
argtaken = 1;
for (;;) {
if (!*w) {
break;
}
ap_unescape_url(w);
if (win_nt) {
prep_string(&w, p);
}
*arg = ap_escape_shell_cmd(p, w);
}
ch += 2;
continue;
}
/* Todo: Make short name!!! */
prgtaken = 1;
if (*ch == '%') {
*repl++ = '\\';
}
}
else {
}
ch += 2;
continue;
}
prgtaken = 1;
*repl++ = '\\';
}
}
else {
}
ch += 4;
continue;
}
*arg = d;
inquo = 0;
while (*ch) {
++ch; break;
}
/* Get 'em backslashes */
*d++ = *ch++;
}
if (sl & 1) {
/* last unmatched '\' + '"' sequence is a '"' */
if (*ch == '\"') {
*(d - 1) = *ch++;
}
continue;
}
if (*ch == '\"') {
/* '""' sequence within quotes is a '"' */
*d++ = *ch++; continue;
}
/* Flip quote state */
++ch; break;
}
/* All other '"'s are Munched */
continue;
}
/* Anything else is, well, something else */
*d++ = *ch++;
}
/* Term that arg, already pushed on args */
*d++ = '\0';
}
if (!prgtaken) {
}
if (!argtaken) {
for (;;) {
if (!*w) {
break;
}
ap_unescape_url(w);
if (win_nt) {
prep_string(&w, p);
}
*arg = ap_escape_shell_cmd(p, w);
}
}
return args;
}
request_rec *r, apr_pool_t *p,
{
const char *interpreter = NULL;
win32_dir_conf *d;
apr_file_t *fh;
const char *args = "";
int i;
&win32_module);
/* We have to consider that the client gets any QUERY_ARGS
* without any charset interpretation, use prep_string to
* create a string of the literal QUERY_ARGS bytes.
*/
}
}
/* Handle the complete file name, we DON'T want to follow suexec, since
* an unrooted command is as predictable as shooting craps in Win32.
* Notice that unlike most mime extension parsing, we have to use the
* win32 parsing here, therefore the final extension is the only one
* we will consider.
*/
/* If the file has an extension and it is not .com and not .exe and
* we've been instructed to search the registry, then do so.
* Let apr_proc_create do all of the .bat/.cmd dirty work.
*/
interpreter = "";
}
if (!interpreter && ext
&& (d->script_interpreter_source
/* Check the registry */
int strict = (d->script_interpreter_source
strict);
}
else {
strict ? "No ExecCGI verb found for files of type '%s'."
: "No ExecCGI or Open verb found for files of type '%s'.",
ext);
}
}
if (!interpreter) {
char buffer[1024];
apr_size_t i;
/* Need to peek into the file figure out what it really is...
* ### aught to go back and build a cache for this one of these days.
*/
"Failed to open cgi file %s for testing", *cmd);
return rv;
}
"Failed to read cgi file %s for testing", *cmd);
return rv;
}
/* Some twisted character [no pun intended] at MS decided that a
* zero width joiner as the lead wide character would be ideal for
* describing Unicode text files. This was further convoluted to
* another MSism that the same character mapped into utf-8, EF BB BF
* would signify utf-8 text files.
*
* Since MS configuration files are all protecting utf-8 encoded
* Unicode path, file and resource names, we already have the correct
* WinNT encoding. But at least eat the stupid three bytes up front.
*
* ### A more thorough check would also allow UNICODE text in buf, and
* convert it to UTF-8 for invoking unicode scripts. Those are few
* and far between, so leave that code an enterprising soul with a need.
*/
}
/* Script or executable, that is the question... */
/* Assuming file is a script since it starts with a shebang */
for (i = 2; i < bytes; i++) {
buffer[i] = '\0';
break;
}
}
if (i < bytes) {
while (apr_isspace(*interpreter)) {
++interpreter;
}
}
}
}
else if (bytes >= sizeof(IMAGE_DOS_HEADER)) {
/* Not a script, is it an executable? */
/* Ought to invoke this 16 bit exe by a stub, (cmd /c?) */
interpreter = "";
}
else {
interpreter = "";
}
}
}
}
if (!interpreter) {
"%s is not executable; ensure interpreted scripts have "
"\"#!\" first line", *cmd);
return APR_EBADF;
}
/* XXX: Must fix r->subprocess_env to follow utf-8 conventions from
* the client's octets so that win32 apr_proc_create is happy.
* The -best- way is to determine if the .exe is unicode aware
* (using 0x0080-0x00ff) or is linked as a command or windows
* application (following the OEM or Ansi code page in effect.)
*/
}
}
return APR_SUCCESS;
}
{
return OK;
}
static void register_hooks(apr_pool_t *p)
{
}
static const command_rec win32_cmds[] = {
"Where to find interpreter to run Win32 scripts "
"(Registry or script shebang line)"),
{ NULL }
};
create_win32_dir_config, /* create per-dir config */
merge_win32_dir_configs, /* merge per-dir config */
NULL, /* server config */
NULL, /* merge server config */
win32_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};
#endif /* defined WIN32 */