mod_cgid.c revision 2fc50921b88defeb7127985dfe4b4130175e069e
2d2eda71267231c2526be701fe655db125852c1ffielding/* ====================================================================
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * The Apache Software License, Version 1.1
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * reserved.
2d2eda71267231c2526be701fe655db125852c1ffielding * Redistribution and use in source and binary forms, with or without
2d2eda71267231c2526be701fe655db125852c1ffielding * modification, are permitted provided that the following conditions
2d2eda71267231c2526be701fe655db125852c1ffielding * 1. Redistributions of source code must retain the above copyright
2d2eda71267231c2526be701fe655db125852c1ffielding * notice, this list of conditions and the following disclaimer.
2d2eda71267231c2526be701fe655db125852c1ffielding * 2. Redistributions in binary form must reproduce the above copyright
2d2eda71267231c2526be701fe655db125852c1ffielding * notice, this list of conditions and the following disclaimer in
2d2eda71267231c2526be701fe655db125852c1ffielding * the documentation and/or other materials provided with the
2d2eda71267231c2526be701fe655db125852c1ffielding * distribution.
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * 3. The end-user documentation included with the redistribution,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * if any, must include the following acknowledgment:
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * "This product includes software developed by the
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * Apache Software Foundation (http://www.apache.org/)."
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * Alternately, this acknowledgment may appear in the software itself,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * if and wherever such third-party acknowledgments normally appear.
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * 4. The names "Apache" and "Apache Software Foundation" must
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * not be used to endorse or promote products derived from this
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * software without prior written permission. For written
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * permission, please contact apache@apache.org.
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * 5. Products derived from this software may not be called "Apache",
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * nor may "Apache" appear in their name, without prior written
64185f9824e42f21ca7b9ae6c004484215c031a7rbb * permission of the Apache Software Foundation.
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
2d2eda71267231c2526be701fe655db125852c1ffielding * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * SUCH DAMAGE.
2d2eda71267231c2526be701fe655db125852c1ffielding * ====================================================================
2d2eda71267231c2526be701fe655db125852c1ffielding * This software consists of voluntary contributions made by many
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * individuals on behalf of the Apache Software Foundation. For more
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * information on the Apache Software Foundation, please see
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * Portions of this software are based upon public domain software
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * originally written at the National Center for Supercomputing Applications,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding * University of Illinois, Urbana-Champaign.
2d2eda71267231c2526be701fe655db125852c1ffielding * http_script: keeps all script-related ramblings together.
fd0edaa8e3d4dd67d0604ccef2e96b071db96643fielding * Compliant to cgi/1.1 spec
92f3af936ce61f25358a3ee4f28df2f6d62040dfdreid * Adapted by rst from original NCSA code by Rob McCool
2d2eda71267231c2526be701fe655db125852c1ffielding * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
2d2eda71267231c2526be701fe655db125852c1ffielding * custom error responses, and DOCUMENT_ROOT because we found it useful.
2d2eda71267231c2526be701fe655db125852c1ffielding * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
2d2eda71267231c2526be701fe655db125852c1ffielding * they fail.
2d2eda71267231c2526be701fe655db125852c1ffielding/* ### should be tossed in favor of APR */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic int cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_server);
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic int handle_exec(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *cgid_pfn_reg_with_ssi;
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic APR_OPTIONAL_FN_TYPE(ap_ssi_get_tag_and_value) *cgid_pfn_gtv;
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic APR_OPTIONAL_FN_TYPE(ap_ssi_parse_string) *cgid_pfn_ps;
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic int total_modules = 0;
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic int daemon_should_exit = 0;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* KLUDGE --- for back-combatibility, we don't have to check Execcgid
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * in ScriptAliased directories, which means we need to know if this
2d2eda71267231c2526be701fe655db125852c1ffielding * request came through ScriptAlias or not... so the Alias module
4c3d3eaf42bd7757edb3721cb586f2ef2bcbf671stoddard * leaves a note for us.
000b67449410515eac43e76ef6667915bfd4d2abgstein const char *t = apr_table_get(r->notes, "alias-forced-type");
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* Configuration stuff */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* DEFAULT_CGID_LISTENBACKLOG controls the max depth on the unix socket's
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * pending connection queue. If a bunch of cgi requests arrive at about
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * the same time, connections from httpd threads/processes will back up
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * in the queue while the cgid process slowly forks off a child to process
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * each connection on the unix socket. If the queue is too short, the
2d2eda71267231c2526be701fe655db125852c1ffielding * httpd process will get ECONNREFUSED when trying to connect.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* DEFAULT_CONNECT_ATTEMPTS controls how many times we'll try to connect
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * to the cgi daemon from the thread/process handling the cgi request.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Generally we want to retry when we get ECONNREFUSED since it is
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * probably because the listen queue is full. We need to try harder so
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * the client doesn't see it as a 503 error.
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * Set this to 0 to continually retry until the connect works or Apache
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * terminates.
2d2eda71267231c2526be701fe655db125852c1ffieldingtypedef struct {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb const char *sockname;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb const char *logname;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb/* If a request includes query info in the URL (stuff after "?"), and
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * the query info does not contain "=" (indicative of a FORM submission),
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * then this routine is called to create the argument list to be passed
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * to the CGI script. When suexec is enabled, the suexec path, user, and
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * group are the first three arguments to be passed; if not, all three
2d2eda71267231c2526be701fe655db125852c1ffielding * must be NULL. The query info is split into separate arguments, where
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * "+" is the separator between keyword arguments.
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic char **create_argv(apr_pool_t *p, char *path, char *user, char *group,
2d2eda71267231c2526be701fe655db125852c1ffielding /* count the number of keywords */
2d2eda71267231c2526be701fe655db125852c1ffielding numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb av = (char **) apr_pcalloc(p, (numwords + 5) * sizeof(char *));
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic void cgid_maint(int reason, void *data, apr_wait_t status)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* don't do anything; server is stopping or restarting */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* it would be better to restart just the cgid child
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * process but for now we'll gracefully restart the entire
2d2eda71267231c2526be701fe655db125852c1ffielding * server by sending AP_SIG_GRACEFUL to ourself, the httpd
2d2eda71267231c2526be701fe655db125852c1ffielding * parent process
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* we get here when pcgi is cleaned up; pcgi gets cleaned
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * up when pconf gets cleaned up
61fd0cab072a05b855cbef9c585702401ac5ae29rbb kill(*sd, SIGHUP); /* send signal to daemon telling it to die */
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic int get_req(int fd, request_rec *r, char **argv0, char ***env, int *req_type)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb unsigned char *data;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb module *suexec_mod = ap_find_linked_module("mod_suexec.c");
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (rc != sizeof(int)) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (rc != sizeof(int)) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (rc != sizeof(int)) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb data = apr_pcalloc(r->pool, len + 1); /* get a cleared byte for final '\0' */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb r->filename = ap_getword(r->pool, (const char **)&data, '\n');
61fd0cab072a05b855cbef9c585702401ac5ae29rbb *argv0 = ap_getword(r->pool, (const char **)&data, '\n');
61fd0cab072a05b855cbef9c585702401ac5ae29rbb r->uri = ap_getword(r->pool, (const char **)&data, '\n');
61fd0cab072a05b855cbef9c585702401ac5ae29rbb environ = apr_pcalloc(r->pool, (j + 2) *sizeof(char *));
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (i = 0; i < j; i++) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb environ[i] = ap_getword(r->pool, (const char **)&data, '\n');
2d2eda71267231c2526be701fe655db125852c1ffielding r->args = ap_getword(r->pool, (const char **)&data, '\n');
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (rc != sizeof(int)) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* add 1, so that if i == 0, we still malloc something. */
2d2eda71267231c2526be701fe655db125852c1ffielding dconf = (void **) apr_pcalloc(r->pool, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
2d2eda71267231c2526be701fe655db125852c1ffielding temp_core = (core_dir_config *)apr_palloc(r->pool, sizeof(core_module));
2d2eda71267231c2526be701fe655db125852c1ffielding suexec_config_t *suexec_cfg = apr_pcalloc(r->pool, sizeof(*suexec_cfg));
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (rc != sizeof(int)) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (rc != sizeof(int)) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb temp_core->limit_cpu = (struct rlimit *)apr_palloc (sizeof(struct rlimit));
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#if defined (RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb temp_core->limit_mem = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
2d2eda71267231c2526be701fe655db125852c1ffielding read(fd, temp_core->limit_mem, sizeof(struct rlimit));
61fd0cab072a05b855cbef9c585702401ac5ae29rbb temp_core->limit_nproc = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
2d2eda71267231c2526be701fe655db125852c1ffielding read(fd, temp_core->limit_nproc, sizeof(struct rlimit));
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* For right now, just make the notes table. At some point we will need
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * to actually fill this out, but for now we just don't want suexec to
61fd0cab072a05b855cbef9c585702401ac5ae29rbb * seg fault.
61fd0cab072a05b855cbef9c585702401ac5ae29rbbstatic void send_req(int fd, request_rec *r, char *argv0, char **env, int req_type)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb int i = 0;
61fd0cab072a05b855cbef9c585702401ac5ae29rbb module *suexec_mod = ap_find_linked_module("mod_suexec.c");
61fd0cab072a05b855cbef9c585702401ac5ae29rbb data = apr_pstrcat(r->pool, r->filename, "\n", argv0, "\n", r->uri, "\n",
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (i =0; env[i]; i++) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Write the request type (SSI "exec cmd" or cgi). */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "write to cgi daemon process");
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Write the number of entries in the environment. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "write to cgi daemon process");
61fd0cab072a05b855cbef9c585702401ac5ae29rbb for (i = 0; env[i]; i++) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Write the length of the concatenated env string. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "write to cgi daemon process");
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Write the concatted env string. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "write to cgi daemon process");
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* Write module_index id value. */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb if (write(fd, &core_module.module_index, sizeof(int)) < 0) {
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "write to cgi daemon process");
61fd0cab072a05b855cbef9c585702401ac5ae29rbb suexec_config_t *suexec_cfg = ap_get_module_config(r->per_dir_config,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb#if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
61fd0cab072a05b855cbef9c585702401ac5ae29rbb cgid_server_conf *sconf = ap_get_module_config(main_server->module_config,
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "Couldn't unlink unix domain socket %s",
61fd0cab072a05b855cbef9c585702401ac5ae29rbb /* just a warning; don't bail out */
61fd0cab072a05b855cbef9c585702401ac5ae29rbb "Couldn't create unix domain socket");
if (rc < 0) {
return errno;
return errno;
if (!geteuid()) {
return errno;
while (!daemon_should_exit) {
char *argv0;
char **env;
const char * const *argv;
request_rec *r;
if (sd2 < 0) {
if (rc) {
(const char * const *)env,
void *data;
int first_time = 0;
module **m;
if (!data) {
if (!first_time) {
total_modules = 0;
else if (daemon_pid == 0) {
return OK;
cgid_server_conf *c =
&cgid_module);
return NULL;
&cgid_module);
return NULL;
&cgid_module);
return NULL;
&cgid_module);
return NULL;
{NULL}
return ret;
apr_file_close(f);
return ret;
if (script_err) {
return ret;
&& *dbuf) {
if (script_err) {
if (script_err) {
apr_file_close(f);
return ret;
int sd;
int connect_tries;
connect_tries = 0;
return OK;
apr_bucket *b;
int is_included;
int sd;
char **env;
return DECLINED;
return DECLINED;
argv0++;
argv0++;
ap_add_cgi_vars(r);
return retval;
return retval;
argv0++;
if (ap_should_client_block(r)) {
dbpos = 0;
while ((len_read =
if (!nph) {
const char *location;
int ret;
return OK;
return HTTP_MOVED_TEMPORARILY;
if (!r->header_only) {
if (nph) {
int rr_status;
c->bucket_alloc);
c->bucket_alloc);
c->bucket_alloc);
c->bucket_alloc);
c->bucket_alloc);
if (r->args) {
char **env;
const char *location;
int sd;
int retval;
apr_bucket *b;
&cgid_module);
return retval;
return rc;
return retval;
return OK;
return HTTP_MOVED_TEMPORARILY;
if (!r->header_only) {
return retval;