mod_cgid.c revision 445997e06464e7625c7f0e22917f8f2d9876cfff
/* ====================================================================
* 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 "apr_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"
#include "mod_suexec.h"
#include "apr_optional.h"
#include "../filters/mod_include.h"
#ifdef HAVE_SYS_SOCKET_H
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
static apr_pool_t *pcgi;
static int total_modules = 0;
/* 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"
#define CGI_REQ 1
#define SSI_REQ 2
/* 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++) {
if (strcmp(w, "")) {
ap_unescape_url(w);
}
}
return av;
}
{
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
}
static void get_req(int fd, request_rec *r, char **filename, char **argv0, char ***env, int *req_type)
{
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 (suexec_mod) {
dconf[i] = (void *)suexec_cfg;
}
r->per_dir_config = dconf;
#if 0
#ifdef RLIMIT_CPU
if (j) {
}
else {
}
#endif
if (j) {
}
else {
}
#endif
#ifdef RLIMIT_NPROC
if (j) {
}
else {
}
#endif
#endif
/* For right now, just make the notes table. At some point we will need
* to actually fill this out, but for now we just don't want suexec to
* seg fault.
*/
}
{
int i = 0;
char *data;
NULL);
for (i =0; env[i]; i++) {
continue;
}
/* Write the request type (SSI "exec cmd" or cgi). */
"write to cgi daemon process");
}
/* Write the number of entries in the environment. */
"write to cgi daemon process");
}
for (i = 0; env[i]; i++) {
}
/* Write the length of the concatenated env string. */
"write to cgi daemon process");
}
/* Write the concatted env string. */
"write to cgi daemon process");
}
/* Write module_index id value. */
"write to cgi daemon process");
}
if (suexec_mod) {
}
#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(void *data)
{
struct sockaddr_un unix_addr;
"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) {
int errfileno = STDERR_FILENO;
char *argv0;
char *filename;
char **env;
const char * const *argv;
apr_pool_t *p;
request_rec *r;
if (sd2 < 0) {
(server_rec *)data,
"Error accepting on cgid socket.");
}
continue;
}
apr_create_pool(&p, pcgi);
r = apr_pcalloc(p, sizeof(request_rec));
r->pool = p;
}
err_pipe)) != APR_SUCCESS) ||
/* Something bad happened, tell the world. */
"couldn't set child process attributes: %s", r->filename);
}
else {
(const char * const *)env,
procattr, p);
if (rc != APR_SUCCESS) {
/* Bad things happened. Everyone should have cleaned up. */
}
}
}
return -1;
}
{
void *data;
int first_time = 0;
const char *userdata_key = "cgid_init";
module **m;
if (!data) {
first_time = 1;
}
if (!first_time) {
apr_create_pool(&pcgi, p);
total_modules = 0;
for (m = ap_preloaded_modules; *m != NULL; m++)
"Couldn't spawn cgid daemon process");
}
else if (pid == 0) {
exit(-1);
}
#endif
/* Required by mod_include filter. This is how mod_cgid registers
* with mod_include to provide processing of the exec directive.
*/
}
}
}
{
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}
};
{
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)
{
apr_bucket *b;
char argsbuffer[HUGE_STRING_LEN];
void *sconf;
int is_included;
int sd;
char **env;
struct sockaddr_un unix_addr;
return DECLINED;
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");
#else
"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, 0,
"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) {
b = apr_bucket_create_eos();
}
}
if (nph) {
b = apr_bucket_create_eos();
}
return OK; /* NOT r->status, even if it has changed. */
}
/*============================================================================
*============================================================================
* This is the beginning of the cgi filter code moved from mod_include. This
* is the code required to handle the "exec" SSI directive.
*============================================================================
*============================================================================*/
{
int rr_status;
return -1;
}
/* No hardwired path info or query allowed */
return -1;
}
return -1;
}
/* Script gets parameters of the *document*, for back compatibility */
/* Force sub_req to be treated as a CGI request, even if ordinary
* typing rules would have called it something else.
*/
/* Run it. */
if (ap_is_HTTP_REDIRECT(rr_status)) {
if (*inserted_head == NULL) {
}
}
return 0;
}
/* This is the special environment used for running the "exec cmd="
* variety of SSI directives.
*/
{
apr_table_t *e = r->subprocess_env;
apr_table_setn(e, "PATH_TRANSLATED",
}
}
if (r->args) {
}
}
request_rec *r, ap_filter_t *f)
{
char **env;
const char *location;
int sd;
int retval;
apr_bucket *b;
struct sockaddr_un unix_addr;
add_ssi_vars(r, f->next);
"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;
char argsbuffer[HUGE_STRING_LEN];
/* 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) {
}
return 0;
}
{
char parsed_string[MAX_STRING_LEN];
*inserted_head = NULL;
"exec used but not allowed in %s", r->filename);
}
else {
while (1) {
return (0);
}
else {
return 1;
}
}
"execution failure for parameter \"%s\" "
}
/* just in case some stooge changed directories */
}
}
}
else {
}
}
}
}
return 0;
}
/*============================================================================
*============================================================================
* This is the end of the cgi filter code moved from mod_include.
*============================================================================
*============================================================================*/
static void register_hook(apr_pool_t *p)
{
}
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 */
register_hook /* register_handlers */
};