mod_win32.c revision 6c4a9c0851642315faf257b2d4842326aee29f2a
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq/* Copyright 2001-2004 Apache Software Foundation
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
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.
6335eb31f0f0ed54628a04ed32946360b8b77684minfrinextern OSVERSIONINFO osver; /* hiding in mpm_winnt.c */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * CGI Script stuff for Win32...
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowetypedef enum { eFileTypeUNKNOWN, eFileTypeBIN, eFileTypeEXE16, eFileTypeEXE32,
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowetypedef enum { INTERPRETER_SOURCE_UNSET, INTERPRETER_SOURCE_REGISTRY_STRICT,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding INTERPRETER_SOURCE_REGISTRY, INTERPRETER_SOURCE_SHEBANG
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingAP_DECLARE(file_type_e) ap_get_win32_interpreter(const request_rec *,
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq /* Where to find interpreter to run scripts */
a812b025d139f465a31c76fc02ed162ed5271b03ndstatic void *create_win32_dir_config(apr_pool_t *p, char *dir)
a812b025d139f465a31c76fc02ed162ed5271b03nd conf = (win32_dir_conf*)apr_palloc(p, sizeof(win32_dir_conf));
a812b025d139f465a31c76fc02ed162ed5271b03nd conf->script_interpreter_source = INTERPRETER_SOURCE_UNSET;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniqstatic void *merge_win32_dir_configs(apr_pool_t *p, void *basev, void *addv)
a812b025d139f465a31c76fc02ed162ed5271b03nd new = (win32_dir_conf *) apr_pcalloc(p, sizeof(win32_dir_conf));
a812b025d139f465a31c76fc02ed162ed5271b03nd new->script_interpreter_source = (add->script_interpreter_source
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowestatic const char *set_interpreter_source(cmd_parms *cmd, void *dv,
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe d->script_interpreter_source = INTERPRETER_SOURCE_REGISTRY;
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe d->script_interpreter_source = INTERPRETER_SOURCE_REGISTRY_STRICT;
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe d->script_interpreter_source = INTERPRETER_SOURCE_SHEBANG;
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe return apr_pstrcat(cmd->temp_pool, "ScriptInterpreterSource \"", arg,
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe "\" must be \"registry\", \"registry-strict\" or "
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.
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowestatic void prep_string(const char ** str, apr_pool_t *p)
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe while (*ch) {
38fd849bd99e2765ee633b6dc576b5f17acdc455wrowe while (*ch) {
9621e4c4056383e4a2b844b14687bae500b33a82wrowe /* sign extension won't hurt us here */
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???)
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowestatic char* get_interpreter_from_win32_registry(apr_pool_t *p,
1dac466bcc84f8ebf410016dcf2a4cd4312e8611wrowe const char* ext,
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
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);
4415d997ac73262e513c0a571bd5be4f609040bawrowe /* Retrieve the name of the script filetype extension */
4415d997ac73262e513c0a571bd5be4f609040bawrowe rv = ap_regkey_value_get(&type_name, type_key, "", p);
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe /* Open the key associated with the script filetype extension */
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe rv = ap_regkey_open(&name_key, AP_REGKEY_CLASSES_ROOT, type_name,
4415d997ac73262e513c0a571bd5be4f609040bawrowe /* Open the key for the script command path by:
4415d997ac73262e513c0a571bd5be4f609040bawrowe * 1) the 'named' filetype key for ExecCGI/Command
eee895b02dd7867622afd0a8a94f2efc7de9c618wrowe * 2) the extension's type key for ExecCGI/Command
4415d997ac73262e513c0a571bd5be4f609040bawrowe * and if the strict arg is false, then continue trying:
4415d997ac73262e513c0a571bd5be4f609040bawrowe * 3) the 'named' filetype key for Open/Command
4415d997ac73262e513c0a571bd5be4f609040bawrowe * 4) the extension's type key for Open/Command
4415d997ac73262e513c0a571bd5be4f609040bawrowe if ((rv = ap_regkey_open(&key, name_key, execcgi_path, APR_READ, p))
4415d997ac73262e513c0a571bd5be4f609040bawrowe if ((rv = ap_regkey_open(&key, type_key, execcgi_path, APR_READ, p))
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((rv = ap_regkey_open(&key, name_key, execopen_path, APR_READ, p))
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard if ((rv = ap_regkey_open(&key, type_key, execopen_path, APR_READ, p))
33cb45dc8c5106018b7c2f6ae42478b109423e0eniqstatic apr_array_header_t *split_argv(apr_pool_t *p, const char *interp,
a812b025d139f465a31c76fc02ed162ed5271b03nd apr_array_header_t *args = apr_array_make(p, 8, sizeof(char*));
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq const char **arg;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Skip on through Deep Space */
00211b036b78699ace57a6d800a52e6c2d57652fnd ++ch; continue;
00211b036b78699ace57a6d800a52e6c2d57652fnd /* One Arg */
00211b036b78699ace57a6d800a52e6c2d57652fnd if (((*ch == '$') || (*ch == '%')) && (*(ch + 1) == '*')) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (((*ch == '$') || (*ch == '%')) && (*(ch + 1) == '1')) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Todo: Make short name!!! */
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq /* Get 'em backslashes */
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq *d++ = *ch++;
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq /* last unmatched '\' + '"' sequence is a '"' */
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq /* '""' sequence within quotes is a '"' */
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq *d++ = *ch++; continue;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Flip quote state */
134330b92fbc39045b7e56654f4c252fb7c53803nd /* All other '"'s are Munched */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Anything else is, well, something else */
134330b92fbc39045b7e56654f4c252fb7c53803nd /* Term that arg, already pushed on args */
a812b025d139f465a31c76fc02ed162ed5271b03nd *d++ = '\0';
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic apr_status_t ap_cgi_build_command(const char **cmd, const char ***argv,
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;
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard d = (win32_dir_conf *)ap_get_module_config(r->per_dir_config,
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 if (r->args && r->args[0] && !ap_strchr_c(r->args, '=')) {
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 /* 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 if (ext && (!strcasecmp(ext,".exe") || !strcasecmp(ext,".com")
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe || !strcasecmp(ext,".bat") || !strcasecmp(ext,".cmd"))) {
8632261c895a84c88ae6ade6ea4c62b27bd22b3ebrianp /* Check the registry */
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe interpreter = get_interpreter_from_win32_registry(r->pool, ext,
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe if (interpreter && e_info->cmd_type != APR_SHELLCMD) {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe strict ? "No ExecCGI verb found for files of type '%s'."
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe : "No ExecCGI or Open verb found for files of type '%s'.",
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.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((rv = apr_file_open(&fh, *cmd, APR_READ | APR_BUFFERED,
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq if ((rv = apr_file_read(fh, buffer, &bytes)) != APR_SUCCESS) {
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.
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.
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.
33cb45dc8c5106018b7c2f6ae42478b109423e0eniq if ((bytes >= 3) && memcmp(buffer, "\xEF\xBB\xBF", 3) == 0) {
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 */
d7387fcd4969206172e3a2a8bbcd25a3d7011ac5rbb if (i < bytes) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Not a script, is it an executable? */
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard /* Ought to invoke this 16 bit exe by a stub, (cmd /c?) */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "%s is not executable; ensure interpreted scripts have "
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *argv = (const char **)(split_argv(p, interpreter, *cmd,
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.)
e2b2e15108eb7cb566b1d70ce4e479276d951de5minfrinstatic int win32_pre_config(apr_pool_t *pconf_, apr_pool_t *plog, apr_pool_t *ptemp)
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard win_nt = (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS);
e2b2e15108eb7cb566b1d70ce4e479276d951de5minfrin ap_hook_pre_config(win32_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddardAP_INIT_TAKE1("ScriptInterpreterSource", set_interpreter_source, NULL,
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard "Where to find interpreter to run Win32 scripts "
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard "(Registry or script shebang line)"),
e2b2e15108eb7cb566b1d70ce4e479276d951de5minfrin create_win32_dir_config, /* create per-dir config */
e6366481b8fe06a24337f0b30b7da66cf64d6062stoddard merge_win32_dir_configs, /* merge per-dir config */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif /* defined WIN32 */