mod_cgid.c revision 286129112dc6bfee53f0db4f945d3352d4d5bed7
967e5f3c25249c779575864692935627004d3f9eChristian Maeder/* ====================================================================
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * The Apache Software License, Version 1.1
75a6279dbae159d018ef812185416cf6df386c10Till Mossakowski * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * Redistribution and use in source and binary forms, with or without
89054b2b95a3f92e78324dc852f3d34704e2ca49Christian Maeder * modification, are permitted provided that the following conditions
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * 1. Redistributions of source code must retain the above copyright
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * notice, this list of conditions and the following disclaimer.
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * 2. Redistributions in binary form must reproduce the above copyright
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * notice, this list of conditions and the following disclaimer in
997c56f3bc74a703043010978e5013fdb074d659Christian Maeder * the documentation and/or other materials provided with the
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder * distribution.
34c05dd06c937d85e7f552e4ff0d36ca0393daeaChristian Maeder * 3. The end-user documentation included with the redistribution,
997c56f3bc74a703043010978e5013fdb074d659Christian Maeder * if any, must include the following acknowledgment:
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder * "This product includes software developed by the
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder * Apache Software Foundation (http://www.apache.org/)."
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * Alternately, this acknowledgment may appear in the software itself,
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder * if and wherever such third-party acknowledgments normally appear.
997c56f3bc74a703043010978e5013fdb074d659Christian Maeder * 4. The names "Apache" and "Apache Software Foundation" must
967e5f3c25249c779575864692935627004d3f9eChristian Maeder * not be used to endorse or promote products derived from this
9744c7d9fa61d255d5e73beec7edc3499522e9e2Till Mossakowski * software without prior written permission. For written
9744c7d9fa61d255d5e73beec7edc3499522e9e2Till Mossakowski * permission, please contact apache@apache.org.
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder * 5. Products derived from this software may not be called "Apache",
997c56f3bc74a703043010978e5013fdb074d659Christian Maeder * nor may "Apache" appear in their name, without prior written
89054b2b95a3f92e78324dc852f3d34704e2ca49Christian Maeder * permission of the Apache Software Foundation.
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * SUCH DAMAGE.
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * ====================================================================
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * This software consists of voluntary contributions made by many
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * individuals on behalf of the Apache Software Foundation. For more
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * information on the Apache Software Foundation, please see
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * Portions of this software are based upon public domain software
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * originally written at the National Center for Supercomputing Applications,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * University of Illinois, Urbana-Champaign.
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder * http_script: keeps all script-related ramblings together.
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder * Compliant to cgi/1.1 spec
997c56f3bc74a703043010978e5013fdb074d659Christian Maeder * Adapted by rst from original NCSA code by Rob McCool
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
997c56f3bc74a703043010978e5013fdb074d659Christian Maeder * custom error responses, and DOCUMENT_ROOT because we found it useful.
997c56f3bc74a703043010978e5013fdb074d659Christian Maeder * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
997c56f3bc74a703043010978e5013fdb074d659Christian Maeder/* ### should be tossed in favor of APR */
997c56f3bc74a703043010978e5013fdb074d659Christian Maederstatic void cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_server);
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maederstatic int handle_exec(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head);
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maederstatic APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *cgid_pfn_reg_with_ssi;
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maederstatic APR_OPTIONAL_FN_TYPE(ap_ssi_get_tag_and_value) *cgid_pfn_gtv;
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maederstatic APR_OPTIONAL_FN_TYPE(ap_ssi_parse_string) *cgid_pfn_ps;
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder/* KLUDGE --- for back-combatibility, we don't have to check Execcgid
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * in ScriptAliased directories, which means we need to know if this
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * request came through ScriptAlias or not... so the Alias module
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * leaves a note for us.
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder const char *t = apr_table_get(r->notes, "alias-forced-type");
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder return t && (!strcasecmp(t, "cgi-script"));
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder/* Configuration stuff */
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder/* DEFAULT_CGID_LISTENBACKLOG controls the max depth on the unix socket's
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * pending connection queue. If a bunch of cgi requests arrive at about
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * the same time, connections from httpd threads/processes will back up
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * in the queue while the cgid process slowly forks off a child to process
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * each connection on the unix socket. If the queue is too short, the
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * httpd process will get ECONNREFUSED when trying to connect.
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maedertypedef struct {
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder/* If a request includes query info in the URL (stuff after "?"), and
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * the query info does not contain "=" (indicative of a FORM submission),
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * then this routine is called to create the argument list to be passed
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * to the CGI script. When suexec is enabled, the suexec path, user, and
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * group are the first three arguments to be passed; if not, all three
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * must be NULL. The query info is split into separate arguments, where
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * "+" is the separator between keyword arguments.
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * XXXX: note that the WIN32 code uses one of the suexec strings
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * to pass an interpreter name. Remember this if changing the way they
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder * are handled in create_argv.
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maederstatic char **create_argv(apr_pool_t *p, char *path, char *user, char *group,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder /* count the number of keywords */
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder av = (char **) apr_pcalloc(p, (numwords + 5) * sizeof(char *));
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maederstatic void cgid_maint(int reason, void *data, apr_wait_t status)
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder /* stop gap to make sure everything else works. In the end,
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder * we'll just restart the cgid server. */
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder kill(getpid(), SIGWINCH); /* yes, to ourself */
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder /* we get here when pcgi is cleaned up; pcgi gets cleaned
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder * up when pconf gets cleaned up
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maederstatic void get_req(int fd, request_rec *r, char **argv0, char ***env, int *req_type)
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder unsigned char *data;
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder module *suexec_mod = ap_find_linked_module("mod_suexec.c");
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder r->server = apr_pcalloc(r->pool, sizeof(server_rec));
34c05dd06c937d85e7f552e4ff0d36ca0393daeaChristian Maeder data = apr_pcalloc(r->pool, len + 1); /* get a cleared byte for final '\0' */
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder r->filename = ap_getword(r->pool, (const char **)&data, '\n');
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder *argv0 = ap_getword(r->pool, (const char **)&data, '\n');
09eef8548cd62d787cf3a6535f9eae10592eec89Christian Maeder r->uri = ap_getword(r->pool, (const char **)&data, '\n');
09eef8548cd62d787cf3a6535f9eae10592eec89Christian Maeder environ = apr_pcalloc(r->pool, (j + 2) *sizeof(char *));
adee28c3eb7bb0b9bb045d26ee6d35e19cf39053Christian Maeder for (i = 0; i < j; i++) {
adee28c3eb7bb0b9bb045d26ee6d35e19cf39053Christian Maeder environ[i] = ap_getword(r->pool, (const char **)&data, '\n');
adee28c3eb7bb0b9bb045d26ee6d35e19cf39053Christian Maeder r->args = ap_getword(r->pool, (const char **)&data, '\n');
adee28c3eb7bb0b9bb045d26ee6d35e19cf39053Christian Maeder /* add 1, so that if i == 0, we still malloc something. */
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder dconf = (void **) apr_pcalloc(r->pool, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder temp_core = (core_dir_config *)apr_palloc(r->pool, sizeof(core_module));
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder suexec_config_t *suexec_cfg = apr_pcalloc(r->pool, sizeof(*suexec_cfg));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder read(fd, &suexec_cfg->ugid.uid, sizeof(uid_t));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder read(fd, &suexec_cfg->ugid.gid, sizeof(gid_t));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder read(fd, &suexec_cfg->active, sizeof(int));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder r->per_dir_config = (ap_conf_vector_t *)dconf;
967e5f3c25249c779575864692935627004d3f9eChristian Maeder temp_core->limit_cpu = (struct rlimit *)apr_palloc (sizeof(struct rlimit));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder read(fd, temp_core->limit_cpu, sizeof(struct rlimit));
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder#if defined (RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder temp_core->limit_mem = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder read(fd, temp_core->limit_mem, sizeof(struct rlimit));
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder temp_core->limit_nproc = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder read(fd, temp_core->limit_nproc, sizeof(struct rlimit));
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder /* For right now, just make the notes table. At some point we will need
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder * to actually fill this out, but for now we just don't want suexec to
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maederstatic void send_req(int fd, request_rec *r, char *argv0, char **env, int req_type)
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder module *suexec_mod = ap_find_linked_module("mod_suexec.c");
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder data = apr_pstrcat(r->pool, r->filename, "\n", argv0, "\n", r->uri, "\n",
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder for (i =0; env[i]; i++) {
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder /* Write the request type (SSI "exec cmd" or cgi). */
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder "write to cgi daemon process");
967e5f3c25249c779575864692935627004d3f9eChristian Maeder /* Write the number of entries in the environment. */
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder "write to cgi daemon process");
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder for (i = 0; env[i]; i++) {
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder data = apr_pstrcat(r->pool, data, env[i], "\n", NULL);
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder data = apr_pstrcat(r->pool, data, r->args, NULL);
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder /* Write the length of the concatenated env string. */
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
967e5f3c25249c779575864692935627004d3f9eChristian Maeder "write to cgi daemon process");
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder /* Write the concatted env string. */
09eef8548cd62d787cf3a6535f9eae10592eec89Christian Maeder ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder "write to cgi daemon process");
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder /* Write module_index id value. */
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder if (write(fd, &core_module.module_index, sizeof(int)) < 0) {
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder "write to cgi daemon process");
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder suexec_config_t *suexec_cfg = ap_get_module_config(r->per_dir_config,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder write(fd, &suexec_mod->module_index, sizeof(int));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder write(fd, &suexec_cfg->ugid.uid, sizeof(uid_t));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder write(fd, &suexec_cfg->ugid.gid, sizeof(gid_t));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder write(fd, &suexec_cfg->active, sizeof(int));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder write(fd, conf->limit_cpu, sizeof(struct rlimit));
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder#if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder write(fd, conf->limit_mem, sizeof(struct rlimit));
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder write(fd, conf->limit_nproc, sizeof(struct rlimit));
af621d0066770895fd79562728e93099c8c52060Christian Maeder cgid_server_conf *sconf = ap_get_module_config(main_server->module_config,
af621d0066770895fd79562728e93099c8c52060Christian Maeder if (unlink(sconf->sockname) < 0 && errno != ENOENT) {
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder "Couldn't unlink unix domain socket %s",
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder /* just a warning; don't bail out */
return errno;
if (rc < 0) {
return errno;
return errno;
if (!geteuid()) {
return errno;
char *argv0;
char **env;
const char * const *argv;
request_rec *r;
if (sd2 < 0) {
(const char * const *)env,
void *data;
int first_time = 0;
module **m;
if (!data) {
if (!first_time) {
total_modules = 0;
else if (pid == 0) {
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;
apr_bucket *b;
int is_included;
int sd;
char **env;
return DECLINED;
return DECLINED;
argv0++;
argv0++;
ap_add_cgi_vars(r);
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) {
b = apr_bucket_eos_create();
if (nph) {
b = apr_bucket_eos_create();
int rr_status;
if (r->args) {
char **env;
const char *location;
int sd;
int retval;
apr_bucket *b;
&cgid_module);
return retval;
return OK;
return HTTP_MOVED_TEMPORARILY;
if (!r->header_only) {