mod_cgid.c revision 446b877d2ab69d9bbcd9152a2d54814f05f1c00b
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. 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. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" 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 name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``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 SOFTWARE FOUNDATION 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 Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* http_script: keeps all script-related ramblings together.
*
* Compliant to cgi/1.1 spec
*
* Adapted by rst from original NCSA code by Rob McCool
*
* Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
* custom error responses, and DOCUMENT_ROOT because we found it useful.
* It also adds SERVER_ADMIN - useful for scripts to know who to mail when
* they fail.
*/
#define CORE_PRIVATE
#include "apr_lib.h"
#include "apr_strings.h"
#include "apr_general.h"
#include "apr_file_io.h"
#include "apr_portable.h"
#include "ap_buckets.h"
#include "util_filter.h"
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_main.h"
#include "http_log.h"
#include "util_script.h"
#include "http_conf_globals.h"
#include "ap_mpm.h"
#include "unixd.h"
#ifdef HAVE_SYS_SOCKET_H
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
static int once_through = 0;
static apr_pool_t *pcgi;
/* KLUDGE --- for back-combatibility, we don't have to check Execcgid
* in ScriptAliased directories, which means we need to know if this
* request came through ScriptAlias or not... so the Alias module
* leaves a note for us.
*/
static int is_scriptaliased(request_rec *r)
{
return t && (!strcasecmp(t, "cgi-script"));
}
/* Configuration stuff */
#define DEFAULT_LOGBYTES 10385760
#define DEFAULT_BUFBYTES 1024
#define DEFAULT_SOCKET "logs/cgisock"
#define SHELL_PATH "/bin/sh"
/* DEFAULT_CGID_LISTENBACKLOG controls the max depth on the unix socket's
* pending connection queue. If a bunch of cgi requests arrive at about
* in the queue while the cgid process slowly forks off a child to process
* each connection on the unix socket. If the queue is too short, the
* httpd process will get ECONNREFUSED when trying to connect.
*/
#ifndef DEFAULT_CGID_LISTENBACKLOG
#define DEFAULT_CGID_LISTENBACKLOG 100
#endif
typedef struct {
const char *sockname;
const char *logname;
long logbytes;
int bufbytes;
/* 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;
}
{
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.
*/
if (shellcmd) {
}
}
else {
env);
}
return (pid);
}
{
switch (reason) {
case APR_OC_REASON_DEATH:
case APR_OC_REASON_LOST:
/* stop gap to make sure everything else works. In the end,
* we'll just restart the cgid server. */
break;
case APR_OC_REASON_RESTART:
case APR_OC_REASON_UNREGISTER:
break;
}
#endif
}
{
int i, len, j;
unsigned char *data;
char **environ;
void **dconf;
i = 0;
for (i = 0; i < j; i++) {
}
/* add 1, so that if i == 0, we still malloc something. */
#if 0
#ifdef RLIMIT_CPU
if (j) {
}
else {
}
#endif
if (j) {
}
else {
}
#endif
#ifdef RLIMIT_NPROC
if (j) {
}
else {
}
#endif
#endif
r->per_dir_config = dconf;
}
{
int len;
int i = 0;
char *data;
NULL);
for (i =0; env[i]; i++) {
continue;
}
"write to cgi daemon process");
}
for (i = 0; env[i]; i++) {
}
"write to cgi daemon process");
}
"write to cgi daemon process");
}
"write to cgi daemon process");
}
#if 0
#ifdef RLIMIT_CPU
len = 1;
}
else {
len = 0;
}
#endif
len = 1;
}
else {
len = 0;
}
#endif
#ifdef RLIMIT_NPROC
if (conf->limit_nproc) {
len = 1;
}
else {
len = 0;
}
#endif
#endif
}
static int cgid_server_child(int sd)
{
char *argv0;
char *filename;
char **env;
apr_pool_t *p;
request_rec *r;
apr_create_pool(&p, pcgi);
r = apr_pcalloc(p, sizeof(request_rec));
r->pool = p;
}
static int cgid_server(void *data)
{
struct sockaddr_un unix_addr;
int pid;
int errfile;
"Couldn't unlink unix domain socket %s",
/* just a warning; don't bail out */
}
"Couldn't create unix domain socket");
return errno;
}
if (rc < 0) {
"Couldn't bind unix domain socket %s",
return errno;
}
"Couldn't listen on unix domain socket");
return errno;
}
if (!geteuid()) {
"Couldn't change owner of unix domain socket %s",
return errno;
}
}
unixd_setup_child(); /* if running as root, switch to configured user/group */
while (1) {
if (sd2 < 0) {
(server_rec *)data,
"Error accepting on cgid socket.");
}
continue;
}
}
else if (pid == 0) {
/* setup the STDERR here, because I have all the info
* for it. I'll do the STDIN and STDOUT later, but I can't
* do STDERR as easily.
*/
}
else {
}
}
else {
"Couldn't fork cgi script");
}
}
return -1;
}
{
if (once_through > 0) {
apr_create_pool(&pcgi, p);
"Couldn't spawn cgid daemon process");
}
else if (pid == 0) {
exit(-1);
}
#endif
}
else once_through++;
}
{
cgid_server_conf *c =
c->logbytes = DEFAULT_LOGBYTES;
c->bufbytes = DEFAULT_BUFBYTES;
return c;
}
{
}
{
return NULL;
}
{
return NULL;
}
{
return NULL;
}
{
return NULL;
}
static const command_rec cgid_cmds[] =
{
"the name of a log for script debugging info"),
"the maximum length (in bytes) of the script debug log"),
"the maximum size (in bytes) to record of a POST request"),
"the name of the socket to use for communication with "
"the cgi daemon."),
{NULL}
};
int show_errno, char *error)
{
apr_file_t *f = NULL;
char time_str[APR_CTIME_LEN];
return ret;
}
apr_close(f);
return ret;
}
{
char argsbuffer[HUGE_STRING_LEN];
apr_file_t *f = NULL;
int i;
char time_str[APR_CTIME_LEN];
/* Soak up script output */
continue;
if (script_err) {
continue;
}
return ret;
}
apr_puts("%request\n", f);
continue;
}
&& *dbuf) {
}
apr_puts("%response\n", f);
continue;
}
apr_puts("%stdout\n", f);
apr_puts(argsbuffer, f);
apr_puts(argsbuffer, f);
apr_puts("\n", f);
}
if (script_err) {
apr_puts("%stderr\n", f);
apr_puts(argsbuffer, f);
apr_puts(argsbuffer, f);
apr_puts("\n", f);
}
}
if (script_err) {
}
apr_close(f);
return ret;
}
/****************************************************************
*
* Actual cgid handling...
*/
static int cgid_handler(request_rec *r)
{
ap_bucket *b;
char argsbuffer[HUGE_STRING_LEN];
int sd;
char **env;
struct sockaddr_un unix_addr;
if (r->method_number == M_OPTIONS) {
/* 99 out of 100 cgid scripts, this is all they support */
return DECLINED;
}
argv0++;
else
argv0++;
else
"Options ExecCGI is off in this directory");
if (nph && is_included)
"attempt to include NPH CGI script");
/* Allow for cgid files without the .EXE extension on them under OS/2 */
char *newfile;
"script not found or unable to stat");
} else {
}
}
#else
if (r->finfo.protection == 0)
"script not found or unable to stat");
#endif
"attempt to invoke directory as script");
/*
if (!ap_suexec_enabled) {
if (!ap_can_exec(&r->finfo))
return log_scripterror(r, conf, HTTP_FORBIDDEN, APLOG_NOERRNO,
"file permissions deny server execution");
}
*/
ap_add_cgi_vars(r);
"unable to create socket to cgi daemon");
}
"unable to connect to cgi daemon");
}
/* We are putting the tempsock variable into a file so that we can use
* a pipe bucket to send the data to the client.
*/
return retval;
argv0++;
else
* Note that we already ignore SIGPIPE in the core server.
*/
if (ap_should_client_block(r)) {
dbpos = 0;
}
while ((len_read =
}
else {
}
}
/* silly script stopped reading, soak up remaining message */
/* dump it */
}
break;
}
}
}
/* Handle script return... */
if (!nph) {
const char *location;
char sbuf[MAX_STRING_LEN];
int ret;
}
/* Soak up all the script output */
continue;
}
/* This redirect needs to be a GET no matter what the original
* method was.
*/
r->method_number = M_GET;
/* We already read the message body (if any), so don't allow
* the redirected request to think it has one. We can ignore
* Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
*/
return OK;
}
/* XX Note that if a script wants to produce its own Redirect
* body, it now has to explicitly *say* "Status: 302"
*/
return HTTP_MOVED_TEMPORARILY;
}
if (!r->header_only) {
AP_BRIGADE_INSERT_TAIL(bb, b);
b = ap_bucket_create_eos();
AP_BRIGADE_INSERT_TAIL(bb, b);
}
}
if (nph) {
AP_BRIGADE_INSERT_TAIL(bb, b);
b = ap_bucket_create_eos();
AP_BRIGADE_INSERT_TAIL(bb, b);
}
return OK; /* NOT r->status, even if it has changed. */
}
static const handler_rec cgid_handlers[] =
{
{"cgi-script", cgid_handler},
{NULL}
};
static void register_hook(void)
{
}
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
create_cgid_config, /* server config */
merge_cgid_config, /* merge server config */
cgid_cmds, /* command table */
cgid_handlers, /* handlers */
register_hook /* register_handlers */
};