util_script.c revision ebc2d0583ce25c4d7894da8392ca57ccc608ce49
/* ====================================================================
* Copyright (c) 1995-1999 The Apache Group. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/
#define CORE_PRIVATE
#include "httpd.h"
#include "http_config.h"
#include "http_main.h"
#include "http_log.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h" /* for sub_req_lookup_uri() */
#include "util_script.h"
#include "util_date.h" /* For parseHTTPdate() */
#ifdef OS2
#define INCL_DOS
#include <os2.h>
#endif
/*
* Various utility functions which are common to a whole lot of
* script-type extensions mechanisms, and might as well be gathered
* in one place (if only to avoid creating inter-module dependancies
* where there don't have to be).
*/
#define MALFORMED_MESSAGE "malformed header from script. Bad header="
#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
/* If a request includes query info in the URL (stuff after "?"), and
* the query info does not contain "=" (indicative of a FORM submission),
* then this routine is called to create the argument list to be passed
* to the CGI script. When suexec is enabled, the suexec path, user, and
* group are the first three arguments to be passed; if not, all three
* must be NULL. The query info is split into separate arguments, where
* "+" is the separator between keyword arguments.
*
* XXXX: note that the WIN32 code uses one of the suexec strings
* to pass an interpreter name. Remember this if changing the way they
* are handled in create_argv.
*
*/
{
int x, numwords;
char **av;
char *w;
int idx = 0;
/* count the number of keywords */
if (args[x] == '+') {
++numwords;
}
}
}
if (path) {
}
if (user) {
}
if (group) {
}
for (x = 1; x <= numwords; x++) {
ap_unescape_url(w);
}
return av;
}
#endif /* defined(OS2) || defined(WIN32) */
static char *http2env(ap_context_t *a, char *w)
{
while (*++cp) {
*cp = '_';
}
else {
}
}
return res;
}
{
int i, j;
char *tz;
char *whack;
j = 0;
if (!ap_table_get(t, "TZ")) {
}
}
continue;
}
if (ap_isdigit(*whack)) {
*whack++ = '_';
}
while (*whack != '=') {
*whack = '_';
}
++whack;
}
++j;
}
return env;
}
{
ap_table_t *e;
server_rec *s = r->server;
conn_rec *c = r->connection;
const char *rem_logname;
char *env_path;
#ifdef WIN32
char *env_temp;
#endif
const char *host;
int i;
/* use a temporary ap_table_t which we'll overlap onto
* r->subprocess_env later
*/
/* First, add environment vars from headers... this is as per
* CGI specs, though other sorts of scripting interfaces see
* the same vars...
*/
continue;
}
/* A few headers are special cased --- Authorization to prevent
* rogue scripts from capturing passwords; content-type and -length
* for no particular reason.
*/
}
}
/*
* You really don't want to disable this check, since it leaves you
* wide open to CGIs stealing passwords and people viewing them
* in the environment with "ps -e". But, if you must...
*/
#ifndef SECURITY_HOLE_PASS_AUTHORIZATION
continue;
}
#endif
else {
}
}
}
#ifdef WIN32
}
}
}
#endif
ap_table_addn(e, "SERVER_PORT",
if (host) {
}
ap_table_addn(e, "REMOTE_PORT",
if (r->user) {
}
if (r->ap_auth_type) {
}
if (rem_logname) {
}
/* Apache custom error responses. If we have redirected set two new vars */
if (r->prev) {
}
}
}
}
/* This "cute" little function comes about because the path info on
* filenames and URLs aren't always the same. So we take the two,
* and find as much of the two that match as possible.
*/
{
if (lu == -1) {
lu = 0;
}
lu++;
}
return lu;
}
/* Obtain the Request-URI from the original request-line, returning
* a new string from the request pool containing the URI or "".
*/
static char *original_uri(request_rec *r)
{
if (r->the_request == NULL) {
}
++first; /* skip over the method */
}
while (ap_isspace(*first)) {
++first; /* and the space(s) */
}
++last; /* end at next whitespace */
}
}
{
ap_table_t *e = r->subprocess_env;
/* Note that the code below special-cases scripts run from includes,
* because it "knows" that the sub_request has been hacked to have the
* args and path_info of the original request, and not any that may have
* come with the script URI in the include command. Ugh.
*/
}
}
}
else {
ap_table_setn(e, "SCRIPT_NAME",
}
/*
* To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
* Need to re-escape it for this, since the entire URI was
* un-escaped before we determined where the PATH_INFO began.
*/
#ifdef WIN32
char buffer[HUGE_STRING_LEN];
#endif
NULL);
#ifdef WIN32
/* We need to make this a real Windows path name */
#else
#endif
}
}
}
{
return 1;
}
int (*getsfunc) (char *, int, void *),
void *getsfunc_data)
{
char x[MAX_STRING_LEN];
char *w, *l;
int p;
int cgi_status = HTTP_OK;
if (buffer) {
*buffer = '\0';
}
/* temporary place to hold headers to merge in later */
/* The HTTP specification says that it is legal to merge duplicate
* headers into one. Some browsers that support Cookies don't like
* merged headers and prefer that each Set-Cookie header is sent
* separately. Lets humour those browsers by not merging.
* Oh what a pain it is.
*/
while (1) {
"Premature end of script headers: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Delete terminal (CR?)LF */
p = strlen(w);
if (p > 0 && w[p - 1] == '\n') {
if (p > 1 && w[p - 2] == '\015') {
w[p - 2] = '\0';
}
else {
w[p - 1] = '\0';
}
}
/*
* If we've finished reading the headers, check to make sure any
* HTTP/1.1 conditions are met. If so, we're done; normal processing
* will handle the script's output. If not, just return the error.
* The appropriate thing to do would be to send the script process a
* SIGPIPE to let it know we're ignoring it, close the channel to the
* script process, and *then* return the failed-to-meet-condition
* error. Otherwise we'd be waiting for the script to finish
* blithering before telling the client the output was no good.
* However, we don't have the information to do that, so we have to
* leave it to an upper layer.
*/
if (w[0] == '\0') {
int cond_status = OK;
}
if (!ap_is_empty_table(cookie_table)) {
/* the cookies have already been copied to the cookie_table */
r->err_headers_out, cookie_table);
}
return cond_status;
}
/* if we see a bogus header don't ignore it. Shout and scream */
#ifdef CHARSET_EBCDIC
/* Chances are that we received an ASCII header text instead of
* the expected EBCDIC header lines. Try to auto-detect:
*/
if (!(l = strchr(w, ':'))) {
int maybeASCII = 0, maybeEBCDIC = 0;
char *cp;
++maybeEBCDIC;
++maybeASCII;
}
if (maybeASCII > maybeEBCDIC) {
"CGI Interface Error: Script headers apparently ASCII: (CGI = %s)", r->filename);
ascii2ebcdic(w, w, cp - w);
}
}
#endif
if (!(l = strchr(w, ':'))) {
if (!buffer) {
/* Soak up all the script output - may save an outright kill */
continue;
}
}
return HTTP_INTERNAL_SERVER_ERROR;
}
*l++ = '\0';
while (*l && ap_isspace(*l)) {
++l;
}
if (!strcasecmp(w, "Content-type")) {
char *tmp;
/* Nuke trailing whitespace */
*endp-- = '\0';
}
r->content_type = tmp;
}
/*
* If the script returned a specific status, that's what
* we'll use - otherwise we assume 200 OK.
*/
else if (!strcasecmp(w, "Status")) {
}
else if (!strcasecmp(w, "Location")) {
ap_table_set(r->headers_out, w, l);
}
else if (!strcasecmp(w, "Content-Length")) {
ap_table_set(r->headers_out, w, l);
}
else if (!strcasecmp(w, "Transfer-Encoding")) {
ap_table_set(r->headers_out, w, l);
}
/*
* If the script gave us a Last-Modified header, we can't just
* pass it on blindly because of restrictions on future values.
*/
else if (!strcasecmp(w, "Last-Modified")) {
ap_update_mtime(r, mtime);
}
else if (!strcasecmp(w, "Set-Cookie")) {
ap_table_add(cookie_table, w, l);
}
else {
ap_table_add(merge, w, l);
}
}
}
{
}
char *buffer)
{
}
{
}
char *buffer)
{
}
{
/* XXX: this -1 thing is a gross hack */
ap_rputs(" -", r);
}
else if (!size) {
ap_rputs(" 0k", r);
}
else if (size < 1024) {
ap_rputs(" 1k", r);
}
else if (size < 1048576) {
}
else if (size < 103809024) {
}
else {
}
}
{
register int x, n;
char **av;
char *w;
for (x = 0, n = 2; args[x]; x++) {
if (args[x] == '+') {
++n;
}
}
/* Add extra strings to array. */
n = n + 2;
/* Now insert the extra strings we made room for above. */
for (x = (1 + 2); x < n; x++) {
ap_unescape_url(w);
av[x] = ap_escape_shell_cmd(p, w);
}
return av;
}
#endif
/* ZZZ need to look at this in more depth and convert to an AP func so we
can get rid of OS specific code.
*/
#if 0
{
int pid = 0;
int errfileno = STDERR_FILENO;
/* the fd on r->server->error_log is closed, but we need somewhere to
* put the error messages from the log_* functions. So, we use stderr,
* since that is better than allowing errors to go unnoticed. Don't do
* this on Win32, though, since we haven't fork()'d.
*/
#endif
/* TODO: all that RLimit stuff should become part of the spawning API,
* and not something in the core_dir_config... define a special rlimit
* structure and pass it in here.
*/
#ifdef OS2
{
/* Additions by Alec Kloss, to allow exec'ing of scripts under OS/2 */
int is_script = 0;
char error_object[260];
int cmdlen;
int env_len, e;
char *env_block, *env_block_pos;
if (!program) {
r->filename);
return (pid);
}
is_script = 1;
char buffer[300];
} else {
}
}
}
}
if (is_script) {
}
/* Special case to allow use of REXX commands as scripts. */
os2pathname(r->filename);
}
else {
}
*args_end = 0;
}
/* +4 = 1 space between progname and args, 2 for double null at end, 2 for possible quote on first arg */
while (*cmdline_pos) {
cmdline_pos++;
}
char *arg;
*(cmdline_pos++) = ' ';
}
*(++cmdline_pos) = 0; /* Add required second terminator */
if (args) {
*args = 0;
args++;
}
/* Create environment block from list of envariables */
for (e=0; env[e]; e++) {
}
*env_block_pos = 0; /* environment block is terminated by a double null */
rc = DosExecPgm(error_object, sizeof(error_object), EXEC_ASYNC, cmdline, env_block, &rescodes, cmdline);
if (rc) {
return -1;
}
return rescodes.codeTerminate;
}
{
/* Adapted from Alec Kloss' work for OS/2 */
char *interpreter = NULL;
char *s = NULL;
char *quoted_filename;
char *pCommand;
int i;
int iEnvBlockLen;
pid = -1;
if (!shellcmd) {
if (fileType == eFileTypeUNKNOWN) {
"%s is not executable; ensure interpreted scripts have "
"\"#!\" first line",
r->filename);
return (pid);
}
/*
* Look at the arguments...
*/
arguments = "";
/* If we are in this leg, there are some other arguments
* that we must include in the execution of the CGI.
* Because CreateProcess is the way it is, we have to
* create a command line like format for the execution
* of the CGI. This means we need to create on long
* string with the executable and arguments.
*
* The arguments string comes in the request structure,
* and each argument is separated by a '+'. We'll replace
* these pluses with spaces.
*/
int iStringSize = 0;
int x;
/*
* Duplicate the request structure string so we don't change it.
*/
/*
* Change the '+' to ' '
*/
for (x=0; arguments[x]; x++) {
if ('+' == arguments[x]) {
arguments[x] = ' ';
}
}
/*
* We need to unescape any characters that are
* in the arguments list.
*/
}
/*
* We have the interpreter (if there is one) and we have
* the arguments (if there are any).
* Build the command string to pass to CreateProcess.
*/
if (interpreter && *interpreter) {
}
else {
}
} else {
char *shell_cmd = "CMD.EXE /C ";
/*
* Use CMD.EXE for NT, COMMAND.COM for WIN95
*/
if (GetVersionEx(&osver)) {
shell_cmd = "COMMAND.COM /C ";
}
}
}
/*
* Make child process use hPipeOutputWrite as standard out,
* and make sure it does not show on screen.
*/
/*
* Win32's CreateProcess call requires that the environment
* be passed in an environment block, a null terminated block of
* null terminated strings.
*/
i = 0;
iEnvBlockLen = 1;
while (env[i]) {
i++;
}
i = 0;
while (env[i]) {
i++;
}
if (fileType == eFileTypeEXE16) {
/* Hack to get 16-bit CGI's working. It works for all the
* standard modules shipped with Apache. pi.dwProcessId is 0
* for 16-bit CGIs and all the Unix specific code that calls
* ap_call_exec interprets this as a failure case. And we can't
* use -1 either because it is mapped to 0 by the caller.
*/
pid = -2;
}
else {
/*
* We must close the handles to the new process and its main thread
* to prevent handle and memory leaks.
*/
}
}
return (pid);
}
#else
/* TODO: reimplement suexec */
#if 0
if (pos) {
*pos = '\0';
}
"getpwnam: invalid username %s", username);
return (pid);
}
return (pid);
}
else {
}
}
else {
}
}
else {
"getpwuid: invalid userid %ld",
(long) r->server->server_uid);
return (pid);
}
"getgrgid: invalid groupid %ld",
(long) r->server->server_gid);
return (pid);
}
}
if (shellcmd) {
}
}
else {
env);
}
}
else {
#endif
if (shellcmd) {
}
}
else {
env);
}
#if 0
}
#endif
return (pid);
#endif
}
#endif