1 /* ====================================================================
2 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
60 #include "http_config.h"
61 #include "http_main.h"
63 #include "http_core.h"
64 #include "http_protocol.h"
65 #include "http_request.h" /* for sub_req_lookup_uri() */
66 #include "util_script.h"
67 #include "util_date.h" /* For parseHTTPdate() */
75 * Various utility functions which are common to a whole lot of
76 * script-type extensions mechanisms, and might as well be gathered
77 * in one place (if only to avoid creating inter-module dependancies
78 * where there don't have to be).
81 #define MALFORMED_MESSAGE "malformed header from script. Bad header="
82 #define MALFORMED_HEADER_LENGTH_TO_SHOW 30
84 #if defined(OS2) || defined(WIN32)
85 /* If a request includes query info in the URL (stuff after "?"), and
86 * the query info does not contain "=" (indicative of a FORM submission),
87 * then this routine is called to create the argument list to be passed
88 * to the CGI script. When suexec is enabled, the suexec path, user, and
89 * group are the first three arguments to be passed; if not, all three
90 * must be NULL. The query info is split into separate arguments, where
91 * "+" is the separator between keyword arguments.
93 * XXXX: note that the WIN32 code uses one of the suexec strings
94 * to pass an interpreter name. Remember this if changing the way they
95 * are handled in create_argv.
98 static char **create_argv(ap_context_t *p, char *path, char *user, char *group,
99 char *av0, const char *args)
106 /* count the number of keywords */
108 for (x = 0, numwords = 1; args[x]; x++) {
109 if (args[x] == '+') {
114 if (numwords > APACHE_ARG_MAX - 5) {
115 numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
117 av = (char **) ap_palloc(p, (numwords + 5) * sizeof(char *));
131 for (x = 1; x <= numwords; x++) {
132 w = ap_getword_nulls(p, &args, '+');
134 av[idx++] = ap_escape_shell_cmd(p, w);
139 #endif /* defined(OS2) || defined(WIN32) */
141 static char *http2env(ap_context_t *a, char *w)
143 char *res = ap_pstrcat(a, "HTTP_", w, NULL);
147 if (!ap_isalnum(*cp) && *cp != '_') {
151 *cp = ap_toupper(*cp);
158 API_EXPORT(char **) ap_create_environment(ap_context_t *p, ap_table_t *t)
160 ap_array_header_t *env_arr = ap_table_elts(t);
161 ap_table_entry_t *elts = (ap_table_entry_t *) env_arr->elts;
162 char **env = (char **) ap_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
168 if (!ap_table_get(t, "TZ")) {
171 env[j++] = ap_pstrcat(p, "TZ=", tz, NULL);
174 for (i = 0; i < env_arr->nelts; ++i) {
178 env[j] = ap_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
180 if (ap_isdigit(*whack)) {
183 while (*whack != '=') {
184 if (!ap_isalnum(*whack) && *whack != '_') {
196 API_EXPORT(void) ap_add_common_vars(request_rec *r)
199 server_rec *s = r->server;
200 conn_rec *c = r->connection;
201 const char *rem_logname;
207 ap_array_header_t *hdrs_arr = ap_table_elts(r->headers_in);
208 ap_table_entry_t *hdrs = (ap_table_entry_t *) hdrs_arr->elts;
211 /* use a temporary ap_table_t which we'll overlap onto
212 * r->subprocess_env later
214 e = ap_make_table(r->pool, 25 + hdrs_arr->nelts);
216 /* First, add environment vars from headers... this is as per
217 * CGI specs, though other sorts of scripting interfaces see
221 for (i = 0; i < hdrs_arr->nelts; ++i) {
226 /* A few headers are special cased --- Authorization to prevent
227 * rogue scripts from capturing passwords; content-type and -length
228 * for no particular reason.
231 if (!strcasecmp(hdrs[i].key, "Content-type")) {
232 ap_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
234 else if (!strcasecmp(hdrs[i].key, "Content-length")) {
235 ap_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
238 * You really don't want to disable this check, since it leaves you
239 * wide open to CGIs stealing passwords and people viewing them
240 * in the environment with "ps -e". But, if you must...
242 #ifndef SECURITY_HOLE_PASS_AUTHORIZATION
243 else if (!strcasecmp(hdrs[i].key, "Authorization")
244 || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
249 ap_table_addn(e, http2env(r->pool, hdrs[i].key), hdrs[i].val);
253 if (!(env_path = getenv("PATH"))) {
254 env_path = DEFAULT_PATH;
258 if (env_temp = getenv("SystemRoot")) {
259 ap_table_addn(e, "SystemRoot", env_temp);
261 if (env_temp = getenv("COMSPEC")) {
262 ap_table_addn(e, "COMSPEC", env_temp);
264 if (env_temp = getenv("WINDIR")) {
265 ap_table_addn(e, "WINDIR", env_temp);
269 ap_table_addn(e, "PATH", env_path);
270 ap_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
271 ap_table_addn(e, "SERVER_SOFTWARE", ap_get_server_version());
272 ap_table_addn(e, "SERVER_NAME", ap_get_server_name(r));
273 ap_table_addn(e, "SERVER_ADDR", r->connection->local_ip); /* Apache */
274 ap_table_addn(e, "SERVER_PORT",
275 ap_psprintf(r->pool, "%u", ap_get_server_port(r)));
276 host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST);
278 ap_table_addn(e, "REMOTE_HOST", host);
280 ap_table_addn(e, "REMOTE_ADDR", c->remote_ip);
281 ap_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r)); /* Apache */
282 ap_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
283 ap_table_addn(e, "SCRIPT_FILENAME", r->filename); /* Apache */
285 ap_table_addn(e, "REMOTE_PORT",
286 ap_psprintf(r->pool, "%d", ntohs(c->remote_addr.sin_port)));
289 ap_table_addn(e, "REMOTE_USER", r->user);
291 if (r->ap_auth_type) {
292 ap_table_addn(e, "AUTH_TYPE", r->ap_auth_type);
294 rem_logname = ap_get_remote_logname(r);
296 ap_table_addn(e, "REMOTE_IDENT", ap_pstrdup(r->pool, rem_logname));
299 /* Apache custom error responses. If we have redirected set two new vars */
303 ap_table_addn(e, "REDIRECT_QUERY_STRING", r->prev->args);
306 ap_table_addn(e, "REDIRECT_URL", r->prev->uri);
310 ap_overlap_tables(r->subprocess_env, e, AP_OVERLAP_TABLES_SET);
313 /* This "cute" little function comes about because the path info on
314 * filenames and URLs aren't always the same. So we take the two,
315 * and find as much of the two that match as possible.
318 API_EXPORT(int) ap_find_path_info(const char *uri, const char *path_info)
320 int lu = strlen(uri);
321 int lp = strlen(path_info);
323 while (lu-- && lp-- && uri[lu] == path_info[lp]);
329 while (uri[lu] != '\0' && uri[lu] != '/') {
335 /* Obtain the Request-URI from the original request-line, returning
336 * a new string from the request pool containing the URI or "".
338 static char *original_uri(request_rec *r)
342 if (r->the_request == NULL) {
343 return (char *) ap_pcalloc(r->pool, 1);
346 first = r->the_request; /* use the request-line */
348 while (*first && !ap_isspace(*first)) {
349 ++first; /* skip over the method */
351 while (ap_isspace(*first)) {
352 ++first; /* and the space(s) */
356 while (*last && !ap_isspace(*last)) {
357 ++last; /* end at next whitespace */
360 return ap_pstrndup(r->pool, first, last - first);
363 API_EXPORT(void) ap_add_cgi_vars(request_rec *r)
365 ap_table_t *e = r->subprocess_env;
367 ap_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
368 ap_table_setn(e, "SERVER_PROTOCOL", r->protocol);
369 ap_table_setn(e, "REQUEST_METHOD", r->method);
370 ap_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
371 ap_table_setn(e, "REQUEST_URI", original_uri(r));
373 /* Note that the code below special-cases scripts run from includes,
374 * because it "knows" that the sub_request has been hacked to have the
375 * args and path_info of the original request, and not any that may have
376 * come with the script URI in the include command. Ugh.
379 if (!strcmp(r->protocol, "INCLUDED")) {
380 ap_table_setn(e, "SCRIPT_NAME", r->uri);
381 if (r->path_info && *r->path_info) {
382 ap_table_setn(e, "PATH_INFO", r->path_info);
385 else if (!r->path_info || !*r->path_info) {
386 ap_table_setn(e, "SCRIPT_NAME", r->uri);
389 int path_info_start = ap_find_path_info(r->uri, r->path_info);
391 ap_table_setn(e, "SCRIPT_NAME",
392 ap_pstrndup(r->pool, r->uri, path_info_start));
394 ap_table_setn(e, "PATH_INFO", r->path_info);
397 if (r->path_info && r->path_info[0]) {
399 * To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
400 * Need to re-escape it for this, since the entire URI was
401 * un-escaped before we determined where the PATH_INFO began.
405 pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r);
407 if (pa_req->filename) {
409 char buffer[HUGE_STRING_LEN];
411 char *pt = ap_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
414 /* We need to make this a real Windows path name */
415 GetFullPathName(pt, HUGE_STRING_LEN, buffer, NULL);
416 ap_table_setn(e, "PATH_TRANSLATED", ap_pstrdup(r->pool, buffer));
418 ap_table_setn(e, "PATH_TRANSLATED", pt);
421 ap_destroy_sub_req(pa_req);
426 static int set_cookie_doo_doo(void *v, const char *key, const char *val)
428 ap_table_addn(v, key, val);
432 API_EXPORT(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
433 int (*getsfunc) (char *, int, void *),
436 char x[MAX_STRING_LEN];
439 int cgi_status = HTTP_OK;
441 ap_table_t *cookie_table;
446 w = buffer ? buffer : x;
448 /* temporary place to hold headers to merge in later */
449 merge = ap_make_table(r->pool, 10);
451 /* The HTTP specification says that it is legal to merge duplicate
452 * headers into one. Some browsers that support Cookies don't like
453 * merged headers and prefer that each Set-Cookie header is sent
454 * separately. Lets humour those browsers by not merging.
455 * Oh what a pain it is.
457 cookie_table = ap_make_table(r->pool, 2);
458 ap_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
462 if ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data) == 0) {
463 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
464 "Premature end of script headers: %s", r->filename);
465 return HTTP_INTERNAL_SERVER_ERROR;
468 /* Delete terminal (CR?)LF */
471 if (p > 0 && w[p - 1] == '\n') {
472 if (p > 1 && w[p - 2] == '\015') {
481 * If we've finished reading the headers, check to make sure any
482 * HTTP/1.1 conditions are met. If so, we're done; normal processing
483 * will handle the script's output. If not, just return the error.
484 * The appropriate thing to do would be to send the script process a
485 * SIGPIPE to let it know we're ignoring it, close the channel to the
486 * script process, and *then* return the failed-to-meet-condition
487 * error. Otherwise we'd be waiting for the script to finish
488 * blithering before telling the client the output was no good.
489 * However, we don't have the information to do that, so we have to
490 * leave it to an upper layer.
493 int cond_status = OK;
495 if ((cgi_status == HTTP_OK) && (r->method_number == M_GET)) {
496 cond_status = ap_meets_conditions(r);
498 ap_overlap_tables(r->err_headers_out, merge,
499 AP_OVERLAP_TABLES_MERGE);
500 if (!ap_is_empty_table(cookie_table)) {
501 /* the cookies have already been copied to the cookie_table */
502 ap_table_unset(r->err_headers_out, "Set-Cookie");
503 r->err_headers_out = ap_overlay_tables(r->pool,
504 r->err_headers_out, cookie_table);
509 /* if we see a bogus header don't ignore it. Shout and scream */
511 #ifdef CHARSET_EBCDIC
512 /* Chances are that we received an ASCII header text instead of
513 * the expected EBCDIC header lines. Try to auto-detect:
515 if (!(l = strchr(w, ':'))) {
516 int maybeASCII = 0, maybeEBCDIC = 0;
519 for (cp = w; *cp != '\0'; ++cp) {
520 if (isprint(*cp) && !isprint(os_toebcdic[*cp]))
522 if (!isprint(*cp) && isprint(os_toebcdic[*cp]))
525 if (maybeASCII > maybeEBCDIC) {
526 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server,
527 "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)", r->filename);
528 ascii2ebcdic(w, w, cp - w);
532 if (!(l = strchr(w, ':'))) {
533 char malformed[(sizeof MALFORMED_MESSAGE) + 1
534 + MALFORMED_HEADER_LENGTH_TO_SHOW];
536 strcpy(malformed, MALFORMED_MESSAGE);
537 strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
540 /* Soak up all the script output - may save an outright kill */
541 while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
546 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
547 "%s: %s", malformed, r->filename);
548 return HTTP_INTERNAL_SERVER_ERROR;
552 while (*l && ap_isspace(*l)) {
556 if (!strcasecmp(w, "Content-type")) {
559 /* Nuke trailing whitespace */
561 char *endp = l + strlen(l) - 1;
562 while (endp > l && ap_isspace(*endp)) {
566 tmp = ap_pstrdup(r->pool, l);
567 ap_content_type_tolower(tmp);
568 r->content_type = tmp;
571 * If the script returned a specific status, that's what
572 * we'll use - otherwise we assume 200 OK.
574 else if (!strcasecmp(w, "Status")) {
575 r->status = cgi_status = atoi(l);
576 r->status_line = ap_pstrdup(r->pool, l);
578 else if (!strcasecmp(w, "Location")) {
579 ap_table_set(r->headers_out, w, l);
581 else if (!strcasecmp(w, "Content-Length")) {
582 ap_table_set(r->headers_out, w, l);
584 else if (!strcasecmp(w, "Transfer-Encoding")) {
585 ap_table_set(r->headers_out, w, l);
588 * If the script gave us a Last-Modified header, we can't just
589 * pass it on blindly because of restrictions on future values.
591 else if (!strcasecmp(w, "Last-Modified")) {
592 time_t mtime = ap_parseHTTPdate(l);
594 ap_update_mtime(r, mtime);
595 ap_set_last_modified(r);
597 else if (!strcasecmp(w, "Set-Cookie")) {
598 ap_table_add(cookie_table, w, l);
601 ap_table_add(merge, w, l);
606 static int getsfunc_FILE(char *buf, int len, void *f)
608 return fgets(buf, len, (FILE *) f) != NULL;
611 API_EXPORT(int) ap_scan_script_header_err(request_rec *r, FILE *f,
614 return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
617 static int getsfunc_BUFF(char *w, int len, void *fb)
619 return ap_bgets(w, len, (BUFF *) fb) > 0;
622 API_EXPORT(int) ap_scan_script_header_err_buff(request_rec *r, BUFF *fb,
625 return ap_scan_script_header_err_core(r, buffer, getsfunc_BUFF, fb);
629 API_EXPORT(void) ap_send_size(size_t size, request_rec *r)
631 /* XXX: this -1 thing is a gross hack */
632 if (size == (size_t)-1) {
638 else if (size < 1024) {
641 else if (size < 1048576) {
642 ap_rprintf(r, "%4dk", (size + 512) / 1024);
644 else if (size < 103809024) {
645 ap_rprintf(r, "%4.1fM", size / 1048576.0);
648 ap_rprintf(r, "%4dM", (size + 524288) / 1048576);
652 #if defined(OS2) || defined(WIN32)
653 static char **create_argv_cmd(ap_context_t *p, char *av0, const char *args, char *path)
659 for (x = 0, n = 2; args[x]; x++) {
660 if (args[x] == '+') {
665 /* Add extra strings to array. */
668 av = (char **) ap_palloc(p, (n + 1) * sizeof(char *));
671 /* Now insert the extra strings we made room for above. */
672 av[1] = strdup("/C");
673 av[2] = strdup(path);
675 for (x = (1 + 2); x < n; x++) {
676 w = ap_getword(p, &args, '+');
678 av[x] = ap_escape_shell_cmd(p, w);
685 /* ZZZ need to look at this in more depth and convert to an AP func so we
686 can get rid of OS specific code.
689 API_EXPORT(int) ap_call_exec(request_rec *r, ap_child_info_t *pinfo, char *argv0,
690 char **env, int shellcmd)
693 int errfileno = STDERR_FILENO;
695 #if !defined(WIN32) && !defined(OS2)
696 /* the fd on r->server->error_log is closed, but we need somewhere to
697 * put the error messages from the log_* functions. So, we use stderr,
698 * since that is better than allowing errors to go unnoticed. Don't do
699 * this on Win32, though, since we haven't fork()'d.
701 ap_put_os_file(&r->server->error_log, &errfileno, r->pool);
704 /* TODO: all that RLimit stuff should become part of the spawning API,
705 * and not something in the core_dir_config... define a special rlimit
706 * structure and pass it in here.
711 /* Additions by Alec Kloss, to allow exec'ing of scripts under OS/2 */
713 char interpreter[2048]; /* hope it's enough for the interpreter path */
714 char error_object[260];
716 char *cmdline = r->filename, *cmdline_pos;
718 char *args = "", *args_end;
720 RESULTCODES rescodes;
722 char *env_block, *env_block_pos;
724 if (r->args && r->args[0] && !strchr(r->args, '='))
727 program = fopen(r->filename, "rt");
730 ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "fopen(%s) failed",
735 fgets(interpreter, sizeof(interpreter), program);
738 if (!strncmp(interpreter, "#!", 2)) {
740 interpreter[strlen(interpreter) - 1] = '\0';
741 if (interpreter[2] != '/' && interpreter[2] != '\\' && interpreter[3] != ':') {
743 if (DosSearchPath(SEARCH_ENVIRONMENT, "PATH", interpreter+2, buffer, sizeof(buffer)) == 0) {
744 strcpy(interpreter+2, buffer);
746 strcat(interpreter, ".exe");
747 if (DosSearchPath(SEARCH_ENVIRONMENT, "PATH", interpreter+2, buffer, sizeof(buffer)) == 0) {
748 strcpy(interpreter+2, buffer);
755 cmdline = ap_pstrcat(r->pool, interpreter+2, " ", r->filename, NULL);
757 else if (strstr(strupr(r->filename), ".CMD") > 0) {
758 /* Special case to allow use of REXX commands as scripts. */
759 os2pathname(r->filename);
760 cmdline = ap_pstrcat(r->pool, SHELL_PATH, " /C ", r->filename, NULL);
763 cmdline = r->filename;
766 args = ap_pstrdup(r->pool, args);
767 ap_unescape_url(args);
768 args = ap_double_quotes(r->pool, args);
769 args_end = args + strlen(args);
771 if (args_end - args > 4000) { /* cmd.exe won't handle lines longer than 4k */
772 args_end = args + 4000;
776 /* +4 = 1 space between progname and args, 2 for double null at end, 2 for possible quote on first arg */
777 cmdlen = strlen(cmdline) + strlen(args) + 4;
778 cmdline_pos = cmdline;
780 while (*cmdline_pos) {
781 cmdlen += 2 * (*cmdline_pos == '+'); /* Allow space for each arg to be quoted */
785 cmdline = ap_pstrndup(r->pool, cmdline, cmdlen);
786 cmdline_pos = cmdline + strlen(cmdline);
788 while (args < args_end) {
791 arg = ap_getword_nc(r->pool, &args, '+');
793 if (strpbrk(arg, "&|<> "))
794 arg = ap_pstrcat(r->pool, "\"", arg, "\"", NULL);
796 *(cmdline_pos++) = ' ';
797 strcpy(cmdline_pos, arg);
798 cmdline_pos += strlen(cmdline_pos);
801 *(++cmdline_pos) = 0; /* Add required second terminator */
802 args = strchr(cmdline, ' ');
809 /* Create environment block from list of envariables */
810 for (env_len=1, e=0; env[e]; e++)
811 env_len += strlen(env[e]) + 1;
813 env_block = ap_palloc(r->pool, env_len);
814 env_block_pos = env_block;
816 for (e=0; env[e]; e++) {
817 strcpy(env_block_pos, env[e]);
818 env_block_pos += strlen(env_block_pos) + 1;
821 *env_block_pos = 0; /* environment block is terminated by a double null */
823 rc = DosExecPgm(error_object, sizeof(error_object), EXEC_ASYNC, cmdline, env_block, &rescodes, cmdline);
826 ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "DosExecPgm(%s %s) failed, %s - %s",
827 cmdline, args ? args : "", ap_os_error_message(rc), error_object );
831 return rescodes.codeTerminate;
835 /* Adapted from Alec Kloss' work for OS/2 */
836 char *interpreter = NULL;
837 char *arguments = NULL;
839 char *exename = NULL;
841 char *quoted_filename;
843 char *pEnvBlock, *pNext;
848 file_type_e fileType;
851 PROCESS_INFORMATION pi;
853 memset(&si, 0, sizeof(si));
854 memset(&pi, 0, sizeof(pi));
860 fileType = ap_get_win32_interpreter(r, &interpreter);
862 if (fileType == eFileTypeUNKNOWN) {
863 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, r,
864 "%s is not executable; ensure interpreted scripts have "
871 * Look at the arguments...
874 if ((r->args) && (r->args[0]) && !strchr(r->args, '=')) {
875 /* If we are in this leg, there are some other arguments
876 * that we must include in the execution of the CGI.
877 * Because CreateProcess is the way it is, we have to
878 * create a command line like format for the execution
879 * of the CGI. This means we need to create on long
880 * string with the executable and arguments.
882 * The arguments string comes in the request structure,
883 * and each argument is separated by a '+'. We'll replace
884 * these pluses with spaces.
891 * Duplicate the request structure string so we don't change it.
893 arguments = ap_pstrdup(r->pool, r->args);
896 * Change the '+' to ' '
898 for (x=0; arguments[x]; x++) {
899 if ('+' == arguments[x]) {
905 * We need to unescape any characters that are
906 * in the arguments list.
908 ap_unescape_url(arguments);
909 arguments = ap_escape_shell_cmd(r->pool, arguments);
913 * We have the interpreter (if there is one) and we have
914 * the arguments (if there are any).
915 * Build the command string to pass to CreateProcess.
917 quoted_filename = ap_pstrcat(r->pool, "\"", r->filename, "\"", NULL);
918 if (interpreter && *interpreter) {
919 pCommand = ap_pstrcat(r->pool, interpreter, " ",
920 quoted_filename, " ", arguments, NULL);
923 pCommand = ap_pstrcat(r->pool, quoted_filename, " ", arguments, NULL);
928 char *shell_cmd = "CMD.EXE /C ";
930 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
933 * Use CMD.EXE for NT, COMMAND.COM for WIN95
935 if (GetVersionEx(&osver)) {
936 if (osver.dwPlatformId != VER_PLATFORM_WIN32_NT) {
937 shell_cmd = "COMMAND.COM /C ";
940 pCommand = ap_pstrcat(r->pool, shell_cmd, argv0, NULL);
944 * Make child process use hPipeOutputWrite as standard out,
945 * and make sure it does not show on screen.
948 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
949 si.wShowWindow = SW_HIDE;
950 si.hStdInput = pinfo->hPipeInputRead;
951 si.hStdOutput = pinfo->hPipeOutputWrite;
952 si.hStdError = pinfo->hPipeErrorWrite;
955 * Win32's CreateProcess call requires that the environment
956 * be passed in an environment block, a null terminated block of
957 * null terminated strings.
962 iEnvBlockLen += strlen(env[i]) + 1;
966 pEnvBlock = (char *)ap_pcalloc(r->pool,iEnvBlockLen);
971 strcpy(pNext, env[i]);
972 pNext = pNext + strlen(pNext) + 1;
976 if (CreateProcess(NULL, pCommand, NULL, NULL, TRUE, DETACHED_PROCESS, pEnvBlock,
977 ap_make_dirstr_parent(r->pool, r->filename),
979 if (fileType == eFileTypeEXE16) {
980 /* Hack to get 16-bit CGI's working. It works for all the
981 * standard modules shipped with Apache. pi.dwProcessId is 0
982 * for 16-bit CGIs and all the Unix specific code that calls
983 * ap_call_exec interprets this as a failure case. And we can't
984 * use -1 either because it is mapped to 0 by the caller.
989 pid = pi.dwProcessId;
991 * We must close the handles to the new process and its main thread
992 * to prevent handle and memory leaks.
994 CloseHandle(pi.hProcess);
995 CloseHandle(pi.hThread);
1002 /* TODO: reimplement suexec */
1004 if (ap_suexec_enabled
1005 && ((r->server->server_uid != ap_user_id)
1006 || (r->server->server_gid != ap_group_id)
1007 || (!strncmp("/~", r->uri, 2)))) {
1009 char *execuser, *grpname;
1013 if (!strncmp("/~", r->uri, 2)) {
1015 char *username = ap_pstrdup(r->pool, r->uri + 2);
1016 char *pos = strchr(username, '/');
1022 if ((pw = getpwnam(username)) == NULL) {
1023 ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
1024 "getpwnam: invalid username %s", username);
1027 execuser = ap_pstrcat(r->pool, "~", pw->pw_name, NULL);
1028 user_gid = pw->pw_gid;
1030 if ((gr = getgrgid(user_gid)) == NULL) {
1031 if ((grpname = ap_palloc(r->pool, 16)) == NULL) {
1035 ap_snprintf(grpname, 16, "%ld", (long) user_gid);
1039 grpname = gr->gr_name;
1043 if ((pw = getpwuid(r->server->server_uid)) == NULL) {
1044 ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
1045 "getpwuid: invalid userid %ld",
1046 (long) r->server->server_uid);
1049 execuser = ap_pstrdup(r->pool, pw->pw_name);
1051 if ((gr = getgrgid(r->server->server_gid)) == NULL) {
1052 ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
1053 "getgrgid: invalid groupid %ld",
1054 (long) r->server->server_gid);
1057 grpname = gr->gr_name;
1061 execle(SUEXEC_BIN, SUEXEC_BIN, execuser, grpname, argv0,
1065 else if ((!r->args) || (!r->args[0]) || strchr(r->args, '=')) {
1066 execle(SUEXEC_BIN, SUEXEC_BIN, execuser, grpname, argv0,
1072 create_argv(r->pool, SUEXEC_BIN, execuser, grpname,
1080 execle(SHELL_PATH, SHELL_PATH, "-c", argv0, NULL, env);
1083 else if ((!r->args) || (!r->args[0]) || strchr(r->args, '=')) {
1084 execle(r->filename, argv0, NULL, env);
1089 create_argv(r->pool, NULL, NULL, NULL, argv0, r->args),