|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef WIN32
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
#include "apr_portable.h"
|
|
Packit |
90a5c9 |
#include "apr_buckets.h"
|
|
Packit |
90a5c9 |
#include "ap_config.h"
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
#include "http_core.h"
|
|
Packit |
90a5c9 |
#include "http_protocol.h"
|
|
Packit |
90a5c9 |
#include "http_request.h"
|
|
Packit |
90a5c9 |
#include "http_log.h"
|
|
Packit |
90a5c9 |
#include "util_script.h"
|
|
Packit |
90a5c9 |
#include "mod_core.h"
|
|
Packit |
90a5c9 |
#include "mod_cgi.h"
|
|
Packit |
90a5c9 |
#include "apr_lib.h"
|
|
Packit |
90a5c9 |
#include "ap_regkey.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* CGI Script stuff for Win32...
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
typedef enum { eFileTypeUNKNOWN, eFileTypeBIN, eFileTypeEXE16, eFileTypeEXE32,
|
|
Packit |
90a5c9 |
eFileTypeSCRIPT } file_type_e;
|
|
Packit |
90a5c9 |
typedef enum { INTERPRETER_SOURCE_UNSET, INTERPRETER_SOURCE_REGISTRY_STRICT,
|
|
Packit |
90a5c9 |
INTERPRETER_SOURCE_REGISTRY, INTERPRETER_SOURCE_SHEBANG
|
|
Packit |
90a5c9 |
} interpreter_source_e;
|
|
Packit |
90a5c9 |
AP_DECLARE(file_type_e) ap_get_win32_interpreter(const request_rec *,
|
|
Packit |
90a5c9 |
char **interpreter,
|
|
Packit |
90a5c9 |
char **arguments);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA win32_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
/* Where to find interpreter to run scripts */
|
|
Packit |
90a5c9 |
interpreter_source_e script_interpreter_source;
|
|
Packit |
90a5c9 |
} win32_dir_conf;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_win32_dir_config(apr_pool_t *p, char *dir)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
win32_dir_conf *conf;
|
|
Packit |
90a5c9 |
conf = (win32_dir_conf*)apr_palloc(p, sizeof(win32_dir_conf));
|
|
Packit |
90a5c9 |
conf->script_interpreter_source = INTERPRETER_SOURCE_UNSET;
|
|
Packit |
90a5c9 |
return conf;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *merge_win32_dir_configs(apr_pool_t *p, void *basev, void *addv)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
win32_dir_conf *new;
|
|
Packit |
90a5c9 |
win32_dir_conf *base = (win32_dir_conf *) basev;
|
|
Packit |
90a5c9 |
win32_dir_conf *add = (win32_dir_conf *) addv;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
new = (win32_dir_conf *) apr_pcalloc(p, sizeof(win32_dir_conf));
|
|
Packit |
90a5c9 |
new->script_interpreter_source = (add->script_interpreter_source
|
|
Packit |
90a5c9 |
!= INTERPRETER_SOURCE_UNSET)
|
|
Packit |
90a5c9 |
? add->script_interpreter_source
|
|
Packit |
90a5c9 |
: base->script_interpreter_source;
|
|
Packit |
90a5c9 |
return new;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *set_interpreter_source(cmd_parms *cmd, void *dv,
|
|
Packit |
90a5c9 |
char *arg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
win32_dir_conf *d = (win32_dir_conf *)dv;
|
|
Packit |
90a5c9 |
if (!strcasecmp(arg, "registry")) {
|
|
Packit |
90a5c9 |
d->script_interpreter_source = INTERPRETER_SOURCE_REGISTRY;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!strcasecmp(arg, "registry-strict")) {
|
|
Packit |
90a5c9 |
d->script_interpreter_source = INTERPRETER_SOURCE_REGISTRY_STRICT;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!strcasecmp(arg, "script")) {
|
|
Packit |
90a5c9 |
d->script_interpreter_source = INTERPRETER_SOURCE_SHEBANG;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
return apr_pstrcat(cmd->temp_pool, "ScriptInterpreterSource \"", arg,
|
|
Packit |
90a5c9 |
"\" must be \"registry\", \"registry-strict\" or "
|
|
Packit |
90a5c9 |
"\"script\"", NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* XXX: prep_string should translate the string into unicode,
|
|
Packit |
90a5c9 |
* such that it is compatible with whatever codepage the client
|
|
Packit |
90a5c9 |
* will read characters 80-ff. For the moment, use the unicode
|
|
Packit |
90a5c9 |
* values 0080-00ff. This isn't trivial, since the code page
|
|
Packit |
90a5c9 |
* varies between msdos and Windows applications.
|
|
Packit |
90a5c9 |
* For subsystem 2 [GUI] the default is the system Ansi CP.
|
|
Packit |
90a5c9 |
* For subsystem 3 [CLI] the default is the system OEM CP.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static void prep_string(const char ** str, apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const char *ch = *str;
|
|
Packit |
90a5c9 |
char *ch2;
|
|
Packit |
90a5c9 |
apr_size_t widen = 0;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ch) {
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
while (*ch) {
|
|
Packit |
90a5c9 |
if (*(ch++) & 0x80) {
|
|
Packit |
90a5c9 |
++widen;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (!widen) {
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
widen += (ch - *str) + 1;
|
|
Packit |
90a5c9 |
ch = *str;
|
|
Packit |
90a5c9 |
*str = ch2 = apr_palloc(p, widen);
|
|
Packit |
90a5c9 |
while (*ch) {
|
|
Packit |
90a5c9 |
if (*ch & 0x80) {
|
|
Packit |
90a5c9 |
/* sign extension won't hurt us here */
|
|
Packit |
90a5c9 |
*(ch2++) = 0xC0 | ((*ch >> 6) & 0x03);
|
|
Packit |
90a5c9 |
*(ch2++) = 0x80 | (*(ch++) & 0x3f);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
*(ch2++) = *(ch++);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
*(ch2++) = '\0';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Somewhat more exciting ... figure out where the registry has stashed the
|
|
Packit |
90a5c9 |
* ExecCGI or Open command - it may be nested one level deep (or more???)
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static char* get_interpreter_from_win32_registry(apr_pool_t *p,
|
|
Packit |
90a5c9 |
const char* ext,
|
|
Packit |
90a5c9 |
int strict)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
ap_regkey_t *name_key = NULL;
|
|
Packit |
90a5c9 |
ap_regkey_t *type_key;
|
|
Packit |
90a5c9 |
ap_regkey_t *key;
|
|
Packit |
90a5c9 |
char execcgi_path[] = "SHELL\\EXECCGI\\COMMAND";
|
|
Packit |
90a5c9 |
char execopen_path[] = "SHELL\\OPEN\\COMMAND";
|
|
Packit |
90a5c9 |
char *type_name;
|
|
Packit |
90a5c9 |
char *buffer;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ext) {
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Future optimization:
|
|
Packit |
90a5c9 |
* When the registry is successfully searched, store the strings for
|
|
Packit |
90a5c9 |
* interpreter and arguments in an ext hash to speed up subsequent look-ups
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Open the key associated with the script filetype extension */
|
|
Packit |
90a5c9 |
rv = ap_regkey_open(&type_key, AP_REGKEY_CLASSES_ROOT, ext, APR_READ, p);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Retrieve the name of the script filetype extension */
|
|
Packit |
90a5c9 |
rv = ap_regkey_value_get(&type_name, type_key, "", p);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv == APR_SUCCESS && type_name[0]) {
|
|
Packit |
90a5c9 |
/* Open the key associated with the script filetype extension */
|
|
Packit |
90a5c9 |
rv = ap_regkey_open(&name_key, AP_REGKEY_CLASSES_ROOT, type_name,
|
|
Packit |
90a5c9 |
APR_READ, p);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Open the key for the script command path by:
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* 1) the 'named' filetype key for ExecCGI/Command
|
|
Packit |
90a5c9 |
* 2) the extension's type key for ExecCGI/Command
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* and if the strict arg is false, then continue trying:
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* 3) the 'named' filetype key for Open/Command
|
|
Packit |
90a5c9 |
* 4) the extension's type key for Open/Command
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (name_key) {
|
|
Packit |
90a5c9 |
if ((rv = ap_regkey_open(&key, name_key, execcgi_path, APR_READ, p))
|
|
Packit |
90a5c9 |
== APR_SUCCESS) {
|
|
Packit |
90a5c9 |
rv = ap_regkey_value_get(&buffer, key, "", p);
|
|
Packit |
90a5c9 |
ap_regkey_close(name_key);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!name_key || (rv != APR_SUCCESS)) {
|
|
Packit |
90a5c9 |
if ((rv = ap_regkey_open(&key, type_key, execcgi_path, APR_READ, p))
|
|
Packit |
90a5c9 |
== APR_SUCCESS) {
|
|
Packit |
90a5c9 |
rv = ap_regkey_value_get(&buffer, key, "", p);
|
|
Packit |
90a5c9 |
ap_regkey_close(type_key);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!strict && name_key && (rv != APR_SUCCESS)) {
|
|
Packit |
90a5c9 |
if ((rv = ap_regkey_open(&key, name_key, execopen_path, APR_READ, p))
|
|
Packit |
90a5c9 |
== APR_SUCCESS) {
|
|
Packit |
90a5c9 |
rv = ap_regkey_value_get(&buffer, key, "", p);
|
|
Packit |
90a5c9 |
ap_regkey_close(name_key);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!strict && (rv != APR_SUCCESS)) {
|
|
Packit |
90a5c9 |
if ((rv = ap_regkey_open(&key, type_key, execopen_path, APR_READ, p))
|
|
Packit |
90a5c9 |
== APR_SUCCESS) {
|
|
Packit |
90a5c9 |
rv = ap_regkey_value_get(&buffer, key, "", p);
|
|
Packit |
90a5c9 |
ap_regkey_close(type_key);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (name_key) {
|
|
Packit |
90a5c9 |
ap_regkey_close(name_key);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_regkey_close(type_key);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS || !buffer[0]) {
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return buffer;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_array_header_t *split_argv(apr_pool_t *p, const char *interp,
|
|
Packit |
90a5c9 |
const char *cgiprg, const char *cgiargs)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_array_header_t *args = apr_array_make(p, 8, sizeof(char*));
|
|
Packit |
90a5c9 |
char *d = apr_palloc(p, strlen(interp)+1);
|
|
Packit |
90a5c9 |
const char *ch = interp;
|
|
Packit |
90a5c9 |
const char **arg;
|
|
Packit |
90a5c9 |
int prgtaken = 0;
|
|
Packit |
90a5c9 |
int argtaken = 0;
|
|
Packit |
90a5c9 |
int inquo;
|
|
Packit |
90a5c9 |
int sl;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (*ch) {
|
|
Packit |
90a5c9 |
/* Skip on through Deep Space */
|
|
Packit |
90a5c9 |
if (apr_isspace(*ch)) {
|
|
Packit |
90a5c9 |
++ch; continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* One Arg */
|
|
Packit |
90a5c9 |
if (((*ch == '$') || (*ch == '%')) && (*(ch + 1) == '*')) {
|
|
Packit |
90a5c9 |
const char *cgiarg = cgiargs;
|
|
Packit |
90a5c9 |
argtaken = 1;
|
|
Packit |
90a5c9 |
for (;;) {
|
|
Packit |
90a5c9 |
char *w = ap_getword_nulls(p, &cgiarg, '+');
|
|
Packit |
90a5c9 |
if (!*w) {
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_unescape_url(w);
|
|
Packit |
90a5c9 |
prep_string((const char**)&w, p);
|
|
Packit |
90a5c9 |
arg = (const char**)apr_array_push(args);
|
|
Packit |
90a5c9 |
*arg = ap_escape_shell_cmd(p, w);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ch += 2;
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (((*ch == '$') || (*ch == '%')) && (*(ch + 1) == '1')) {
|
|
Packit |
90a5c9 |
/* Todo: Make short name!!! */
|
|
Packit |
90a5c9 |
prgtaken = 1;
|
|
Packit |
90a5c9 |
arg = (const char**)apr_array_push(args);
|
|
Packit |
90a5c9 |
if (*ch == '%') {
|
|
Packit |
90a5c9 |
char *repl = apr_pstrdup(p, cgiprg);
|
|
Packit |
90a5c9 |
*arg = repl;
|
|
Packit |
90a5c9 |
while ((repl = strchr(repl, '/'))) {
|
|
Packit |
90a5c9 |
*repl++ = '\\';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
*arg = cgiprg;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ch += 2;
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if ((*ch == '\"') && ((*(ch + 1) == '$')
|
|
Packit |
90a5c9 |
|| (*(ch + 1) == '%')) && (*(ch + 2) == '1')
|
|
Packit |
90a5c9 |
&& (*(ch + 3) == '\"')) {
|
|
Packit |
90a5c9 |
prgtaken = 1;
|
|
Packit |
90a5c9 |
arg = (const char**)apr_array_push(args);
|
|
Packit |
90a5c9 |
if (*(ch + 1) == '%') {
|
|
Packit |
90a5c9 |
char *repl = apr_pstrdup(p, cgiprg);
|
|
Packit |
90a5c9 |
*arg = repl;
|
|
Packit |
90a5c9 |
while ((repl = strchr(repl, '/'))) {
|
|
Packit |
90a5c9 |
*repl++ = '\\';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
*arg = cgiprg;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ch += 4;
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
arg = (const char**)apr_array_push(args);
|
|
Packit |
90a5c9 |
*arg = d;
|
|
Packit |
90a5c9 |
inquo = 0;
|
|
Packit |
90a5c9 |
while (*ch) {
|
|
Packit |
90a5c9 |
if (apr_isspace(*ch) && !inquo) {
|
|
Packit |
90a5c9 |
++ch; break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Get 'em backslashes */
|
|
Packit |
90a5c9 |
for (sl = 0; *ch == '\\'; ++sl) {
|
|
Packit |
90a5c9 |
*d++ = *ch++;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (sl & 1) {
|
|
Packit |
90a5c9 |
/* last unmatched '\' + '"' sequence is a '"' */
|
|
Packit |
90a5c9 |
if (*ch == '\"') {
|
|
Packit |
90a5c9 |
*(d - 1) = *ch++;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (*ch == '\"') {
|
|
Packit |
90a5c9 |
/* '""' sequence within quotes is a '"' */
|
|
Packit |
90a5c9 |
if (*++ch == '\"' && inquo) {
|
|
Packit |
90a5c9 |
*d++ = *ch++; continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Flip quote state */
|
|
Packit |
90a5c9 |
inquo = !inquo;
|
|
Packit |
90a5c9 |
if (apr_isspace(*ch) && !inquo) {
|
|
Packit |
90a5c9 |
++ch; break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* All other '"'s are Munched */
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Anything else is, well, something else */
|
|
Packit |
90a5c9 |
*d++ = *ch++;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Term that arg, already pushed on args */
|
|
Packit |
90a5c9 |
*d++ = '\0';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!prgtaken) {
|
|
Packit |
90a5c9 |
arg = (const char**)apr_array_push(args);
|
|
Packit |
90a5c9 |
*arg = cgiprg;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!argtaken) {
|
|
Packit |
90a5c9 |
const char *cgiarg = cgiargs;
|
|
Packit |
90a5c9 |
for (;;) {
|
|
Packit |
90a5c9 |
char *w = ap_getword_nulls(p, &cgiarg, '+');
|
|
Packit |
90a5c9 |
if (!*w) {
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_unescape_url(w);
|
|
Packit |
90a5c9 |
prep_string((const char**)&w, p);
|
|
Packit |
90a5c9 |
arg = (const char**)apr_array_push(args);
|
|
Packit |
90a5c9 |
*arg = ap_escape_shell_cmd(p, w);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
arg = (const char**)apr_array_push(args);
|
|
Packit |
90a5c9 |
*arg = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return args;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t ap_cgi_build_command(const char **cmd, const char ***argv,
|
|
Packit |
90a5c9 |
request_rec *r, apr_pool_t *p,
|
|
Packit |
90a5c9 |
cgi_exec_info_t *e_info)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const apr_array_header_t *elts_arr = apr_table_elts(r->subprocess_env);
|
|
Packit |
90a5c9 |
const apr_table_entry_t *elts = (apr_table_entry_t *) elts_arr->elts;
|
|
Packit |
90a5c9 |
const char *ext = NULL;
|
|
Packit |
90a5c9 |
const char *interpreter = NULL;
|
|
Packit |
90a5c9 |
win32_dir_conf *d;
|
|
Packit |
90a5c9 |
apr_file_t *fh;
|
|
Packit |
90a5c9 |
const char *args = "";
|
|
Packit |
90a5c9 |
int i;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
d = (win32_dir_conf *)ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&win32_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (e_info->cmd_type) {
|
|
Packit |
90a5c9 |
/* We have to consider that the client gets any QUERY_ARGS
|
|
Packit |
90a5c9 |
* without any charset interpretation, use prep_string to
|
|
Packit |
90a5c9 |
* create a string of the literal QUERY_ARGS bytes.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
*cmd = r->filename;
|
|
Packit |
90a5c9 |
if (r->args && r->args[0] && !ap_strchr_c(r->args, '=')) {
|
|
Packit |
90a5c9 |
args = r->args;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Handle the complete file name, we DON'T want to follow suexec, since
|
|
Packit |
90a5c9 |
* an unrooted command is as predictable as shooting craps in Win32.
|
|
Packit |
90a5c9 |
* Notice that unlike most mime extension parsing, we have to use the
|
|
Packit |
90a5c9 |
* win32 parsing here, therefore the final extension is the only one
|
|
Packit |
90a5c9 |
* we will consider.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
ext = strrchr(apr_filepath_name_get(*cmd), '.');
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If the file has an extension and it is not .com and not .exe and
|
|
Packit |
90a5c9 |
* we've been instructed to search the registry, then do so.
|
|
Packit |
90a5c9 |
* Let apr_proc_create do all of the .bat/.cmd dirty work.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (ext && (!strcasecmp(ext,".exe") || !strcasecmp(ext,".com")
|
|
Packit |
90a5c9 |
|| !strcasecmp(ext,".bat") || !strcasecmp(ext,".cmd"))) {
|
|
Packit |
90a5c9 |
interpreter = "";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (!interpreter && ext
|
|
Packit |
90a5c9 |
&& (d->script_interpreter_source
|
|
Packit |
90a5c9 |
== INTERPRETER_SOURCE_REGISTRY
|
|
Packit |
90a5c9 |
|| d->script_interpreter_source
|
|
Packit |
90a5c9 |
== INTERPRETER_SOURCE_REGISTRY_STRICT)) {
|
|
Packit |
90a5c9 |
/* Check the registry */
|
|
Packit |
90a5c9 |
int strict = (d->script_interpreter_source
|
|
Packit |
90a5c9 |
== INTERPRETER_SOURCE_REGISTRY_STRICT);
|
|
Packit |
90a5c9 |
interpreter = get_interpreter_from_win32_registry(r->pool, ext,
|
|
Packit |
90a5c9 |
strict);
|
|
Packit |
90a5c9 |
if (interpreter && e_info->cmd_type != APR_SHELLCMD) {
|
|
Packit |
90a5c9 |
e_info->cmd_type = APR_PROGRAM_PATH;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
|
|
Packit |
90a5c9 |
strict ? APLOGNO(03180) "No ExecCGI verb found for files of type '%s'."
|
|
Packit |
90a5c9 |
: APLOGNO(03181) "No ExecCGI or Open verb found for files of type '%s'.",
|
|
Packit |
90a5c9 |
ext);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (!interpreter) {
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
char buffer[1024];
|
|
Packit |
90a5c9 |
apr_size_t bytes = sizeof(buffer);
|
|
Packit |
90a5c9 |
apr_size_t i;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Need to peek into the file figure out what it really is...
|
|
Packit |
90a5c9 |
* ### aught to go back and build a cache for this one of these days.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((rv = apr_file_open(&fh, *cmd, APR_READ | APR_BUFFERED,
|
|
Packit |
90a5c9 |
APR_OS_DEFAULT, r->pool)) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(02100)
|
|
Packit |
90a5c9 |
"Failed to open cgi file %s for testing", *cmd);
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if ((rv = apr_file_read(fh, buffer, &bytes)) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(02101)
|
|
Packit |
90a5c9 |
"Failed to read cgi file %s for testing", *cmd);
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_file_close(fh);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Some twisted character [no pun intended] at MS decided that a
|
|
Packit |
90a5c9 |
* zero width joiner as the lead wide character would be ideal for
|
|
Packit |
90a5c9 |
* describing Unicode text files. This was further convoluted to
|
|
Packit |
90a5c9 |
* another MSism that the same character mapped into utf-8, EF BB BF
|
|
Packit |
90a5c9 |
* would signify utf-8 text files.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Since MS configuration files are all protecting utf-8 encoded
|
|
Packit |
90a5c9 |
* Unicode path, file and resource names, we already have the correct
|
|
Packit |
90a5c9 |
* WinNT encoding. But at least eat the stupid three bytes up front.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* ### A more thorough check would also allow UNICODE text in buf, and
|
|
Packit |
90a5c9 |
* convert it to UTF-8 for invoking unicode scripts. Those are few
|
|
Packit |
90a5c9 |
* and far between, so leave that code an enterprising soul with a need.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((bytes >= 3) && memcmp(buffer, "\xEF\xBB\xBF", 3) == 0) {
|
|
Packit |
90a5c9 |
memmove(buffer, buffer + 3, bytes -= 3);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Script or executable, that is the question...
|
|
Packit |
90a5c9 |
* we check here also for '! so that .vbs scripts can work as CGI.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((bytes >= 2) && ((buffer[0] == '#') || (buffer[0] == '\''))
|
|
Packit |
90a5c9 |
&& (buffer[1] == '!')) {
|
|
Packit |
90a5c9 |
/* Assuming file is a script since it starts with a shebang */
|
|
Packit |
90a5c9 |
for (i = 2; i < bytes; i++) {
|
|
Packit |
90a5c9 |
if ((buffer[i] == '\r') || (buffer[i] == '\n')) {
|
|
Packit |
90a5c9 |
buffer[i] = '\0';
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (i < bytes) {
|
|
Packit |
90a5c9 |
interpreter = buffer + 2;
|
|
Packit |
90a5c9 |
while (apr_isspace(*interpreter)) {
|
|
Packit |
90a5c9 |
++interpreter;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (e_info->cmd_type != APR_SHELLCMD) {
|
|
Packit |
90a5c9 |
e_info->cmd_type = APR_PROGRAM_PATH;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (bytes >= sizeof(IMAGE_DOS_HEADER)) {
|
|
Packit |
90a5c9 |
/* Not a script, is it an executable? */
|
|
Packit |
90a5c9 |
IMAGE_DOS_HEADER *hdr = (IMAGE_DOS_HEADER*)buffer;
|
|
Packit |
90a5c9 |
if (hdr->e_magic == IMAGE_DOS_SIGNATURE) {
|
|
Packit |
90a5c9 |
if (hdr->e_lfarlc < 0x40) {
|
|
Packit |
90a5c9 |
/* Ought to invoke this 16 bit exe by a stub, (cmd /c?) */
|
|
Packit |
90a5c9 |
interpreter = "";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
interpreter = "";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (!interpreter) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02102)
|
|
Packit |
90a5c9 |
"%s is not executable; ensure interpreted scripts have "
|
|
Packit |
90a5c9 |
"\"#!\" or \"'!\" first line", *cmd);
|
|
Packit |
90a5c9 |
return APR_EBADF;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*argv = (const char **)(split_argv(p, interpreter, *cmd,
|
|
Packit |
90a5c9 |
args)->elts);
|
|
Packit |
90a5c9 |
*cmd = (*argv)[0];
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
e_info->detached = 1;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* XXX: Must fix r->subprocess_env to follow utf-8 conventions from
|
|
Packit |
90a5c9 |
* the client's octets so that win32 apr_proc_create is happy.
|
|
Packit |
90a5c9 |
* The -best- way is to determine if the .exe is unicode aware
|
|
Packit |
90a5c9 |
* (using 0x0080-0x00ff) or is linked as a command or windows
|
|
Packit |
90a5c9 |
* application (following the OEM or Ansi code page in effect.)
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
for (i = 0; i < elts_arr->nelts; ++i) {
|
|
Packit |
90a5c9 |
if (elts[i].key && *elts[i].key && *elts[i].val
|
|
Packit |
90a5c9 |
&& !(strncmp(elts[i].key, "REMOTE_", 7) == 0
|
|
Packit |
90a5c9 |
|| strcmp(elts[i].key, "GATEWAY_INTERFACE") == 0
|
|
Packit |
90a5c9 |
|| strcmp(elts[i].key, "REQUEST_METHOD") == 0
|
|
Packit |
90a5c9 |
|| strcmp(elts[i].key, "SERVER_ADDR") == 0
|
|
Packit |
90a5c9 |
|| strcmp(elts[i].key, "SERVER_PORT") == 0
|
|
Packit |
90a5c9 |
|| strcmp(elts[i].key, "SERVER_PROTOCOL") == 0)) {
|
|
Packit |
90a5c9 |
prep_string((const char**) &elts[i].val, r->pool);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
APR_REGISTER_OPTIONAL_FN(ap_cgi_build_command);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec win32_cmds[] = {
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("ScriptInterpreterSource", set_interpreter_source, NULL,
|
|
Packit |
90a5c9 |
OR_FILEINFO,
|
|
Packit |
90a5c9 |
"Where to find interpreter to run Win32 scripts "
|
|
Packit |
90a5c9 |
"(Registry or script shebang line)"),
|
|
Packit |
90a5c9 |
{ NULL }
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(win32) = {
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_win32_dir_config, /* create per-dir config */
|
|
Packit |
90a5c9 |
merge_win32_dir_configs, /* merge per-dir config */
|
|
Packit |
90a5c9 |
NULL, /* server config */
|
|
Packit |
90a5c9 |
NULL, /* merge server config */
|
|
Packit |
90a5c9 |
win32_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#endif /* defined WIN32 */
|