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