mod_cgid.c revision e0fe4de2016336428729a620ac0034cd1198ad7a
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* ====================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * The Apache Software License, Version 1.1
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * Redistribution and use in source and binary forms, with or without
06c1952db1ab5598e3d68132f9c846bc59c94ef7Jakub Hrozek * modification, are permitted provided that the following conditions
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek * 1. Redistributions of source code must retain the above copyright
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * notice, this list of conditions and the following disclaimer.
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * 2. Redistributions in binary form must reproduce the above copyright
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * notice, this list of conditions and the following disclaimer in
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * the documentation and/or other materials provided with the
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek * distribution.
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * 3. The end-user documentation included with the redistribution,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * if any, must include the following acknowledgment:
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * "This product includes software developed by the
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * Apache Software Foundation (http://www.apache.org/)."
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * Alternately, this acknowledgment may appear in the software itself,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * if and wherever such third-party acknowledgments normally appear.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * 4. The names "Apache" and "Apache Software Foundation" must
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * not be used to endorse or promote products derived from this
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek * software without prior written permission. For written
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * permission, please contact apache@apache.org.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * 5. Products derived from this software may not be called "Apache",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * nor may "Apache" appear in their name, without prior written
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * permission of the Apache Software Foundation.
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * SUCH DAMAGE.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * ====================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * This software consists of voluntary contributions made by many
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * individuals on behalf of the Apache Software Foundation. For more
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * information on the Apache Software Foundation, please see
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * Portions of this software are based upon public domain software
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * originally written at the National Center for Supercomputing Applications,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * University of Illinois, Urbana-Champaign.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * http_script: keeps all script-related ramblings together.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Compliant to cgi/1.1 spec
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Adapted by rst from original NCSA code by Rob McCool
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * custom error responses, and DOCUMENT_ROOT because we found it useful.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* ### should be tossed in favor of APR */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_server);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int handle_exec(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *cgid_pfn_reg_with_ssi;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic APR_OPTIONAL_FN_TYPE(ap_ssi_get_tag_and_value) *cgid_pfn_gtv;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic APR_OPTIONAL_FN_TYPE(ap_ssi_parse_string) *cgid_pfn_ps;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* Read and discard the data in the brigade produced by a CGI script */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void discard_script_output(apr_bucket_brigade *bb);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* KLUDGE --- for back-combatibility, we don't have to check Execcgid
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * in ScriptAliased directories, which means we need to know if this
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * request came through ScriptAlias or not... so the Alias module
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * leaves a note for us.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *t = apr_table_get(r->notes, "alias-forced-type");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return t && (!strcasecmp(t, "cgi-script"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* Configuration stuff */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#define DEFAULT_SOCKET DEFAULT_REL_RUNTIMEDIR "/cgisock"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* DEFAULT_CGID_LISTENBACKLOG controls the max depth on the unix socket's
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * pending connection queue. If a bunch of cgi requests arrive at about
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * the same time, connections from httpd threads/processes will back up
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * in the queue while the cgid process slowly forks off a child to process
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * each connection on the unix socket. If the queue is too short, the
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * httpd process will get ECONNREFUSED when trying to connect.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* DEFAULT_CONNECT_ATTEMPTS controls how many times we'll try to connect
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * to the cgi daemon from the thread/process handling the cgi request.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Generally we want to retry when we get ECONNREFUSED since it is
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * probably because the listen queue is full. We need to try harder so
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * the client doesn't see it as a 503 error.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Set this to 0 to continually retry until the connect works or Apache
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghertypedef struct {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* If a request includes query info in the URL (stuff after "?"), and
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * the query info does not contain "=" (indicative of a FORM submission),
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * then this routine is called to create the argument list to be passed
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * to the CGI script. When suexec is enabled, the suexec path, user, and
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * group are the first three arguments to be passed; if not, all three
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * must be NULL. The query info is split into separate arguments, where
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * "+" is the separator between keyword arguments.
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekstatic char **create_argv(apr_pool_t *p, char *path, char *user, char *group,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* count the number of keywords */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher av = (char **) apr_pcalloc(p, (numwords + 5) * sizeof(char *));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void cgid_maint(int reason, void *data, apr_wait_t status)
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* don't do anything; server is stopping or restarting */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* it would be better to restart just the cgid child
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * process but for now we'll gracefully restart the entire
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * server by sending AP_SIG_GRACEFUL to ourself, the httpd
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * parent process
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* we get here when pcgi is cleaned up; pcgi gets cleaned
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * up when pconf gets cleaned up
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek kill(*sd, SIGHUP); /* send signal to daemon telling it to die */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozekstatic int get_req(int fd, request_rec *r, char **argv0, char ***env, int *req_type)
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek unsigned char *data;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek module *suexec_mod = ap_find_linked_module("mod_suexec.c");
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek r->server = apr_pcalloc(r->pool, sizeof(server_rec));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (rc != sizeof(int)) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (rc != sizeof(int)) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (rc != sizeof(int)) {
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek data = apr_pcalloc(r->pool, len + 1); /* get a cleared byte for final '\0' */
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek r->filename = ap_getword(r->pool, (const char **)&data, '\n');
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek *argv0 = ap_getword(r->pool, (const char **)&data, '\n');
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek r->uri = ap_getword(r->pool, (const char **)&data, '\n');
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek environ = apr_pcalloc(r->pool, (j + 2) *sizeof(char *));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek for (i = 0; i < j; i++) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek environ[i] = ap_getword(r->pool, (const char **)&data, '\n');
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek r->args = ap_getword(r->pool, (const char **)&data, '\n');
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (rc != sizeof(int)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* add 1, so that if i == 0, we still malloc something. */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek dconf = (void **) apr_pcalloc(r->pool, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek temp_core = (core_dir_config *)apr_palloc(r->pool, sizeof(core_module));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek suexec_config_t *suexec_cfg = apr_pcalloc(r->pool, sizeof(*suexec_cfg));
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (rc != sizeof(int)) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek rc = read(fd, suexec_cfg, sizeof(*suexec_cfg));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek temp_core->limit_cpu = (struct rlimit *)apr_palloc (sizeof(struct rlimit));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek read(fd, temp_core->limit_cpu, sizeof(struct rlimit));
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek#if defined (RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek temp_core->limit_mem = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek read(fd, temp_core->limit_mem, sizeof(struct rlimit));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek temp_core->limit_nproc = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek read(fd, temp_core->limit_nproc, sizeof(struct rlimit));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* basic notes table to avoid segfaults */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* mod_userdir requires the mod_userdir_user note */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek data = apr_pcalloc(r->pool, len + 1); /* last byte is '\0' */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek apr_table_set(r->notes,"mod_userdir_user", (const char *)data);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic void send_req(int fd, request_rec *r, char *argv0, char **env, int req_type)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek module *suexec_mod = ap_find_linked_module("mod_suexec.c");
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek data = apr_pstrcat(r->pool, r->filename, "\n", argv0, "\n", r->uri, "\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i =0; env[i]; i++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Write the request type (SSI "exec cmd" or cgi). */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (write(fd, &r_type, sizeof(int)) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek "write to cgi daemon process");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Write the number of entries in the environment. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "write to cgi daemon process");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = 0; env[i]; i++) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek data = apr_pstrcat(r->pool, data, env[i], "\n", NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher data = apr_pstrcat(r->pool, data, r->args, NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Write the length of the concatenated env string. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "write to cgi daemon process");
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* Write the concatted env string. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "write to cgi daemon process");
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* Write module_index id value. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (write(fd, &core_module.module_index, sizeof(int)) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "write to cgi daemon process");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher suexec_config_t *suexec_cfg = ap_get_module_config(r->per_dir_config,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher write(fd, &suexec_mod->module_index, sizeof(int));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher write(fd, suexec_cfg, sizeof(*suexec_cfg));
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek write(fd, conf->limit_cpu, sizeof(struct rlimit));
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher write(fd, conf->limit_mem, sizeof(struct rlimit));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher write(fd, conf->limit_nproc, sizeof(struct rlimit));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* send a minimal notes table */
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek data = (char *) apr_table_get(r->notes, "mod_userdir_user");
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cgid_server_conf *sconf = ap_get_module_config(main_server->module_config,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (unlink(sconf->sockname) < 0 && errno != ENOENT) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek "Couldn't unlink unix domain socket %s",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* just a warning; don't bail out */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek "Couldn't create unix domain socket");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher omask = umask(0077); /* so that only Apache can use socket */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek rc = bind(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr));
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek umask(omask); /* can't fail, so can't clobber errno */
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek "Couldn't bind unix domain socket %s",
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (listen(sd, DEFAULT_CGID_LISTENBACKLOG) < 0) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek "Couldn't listen on unix domain socket");
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (chown(sconf->sockname, unixd_config.user_id, -1) < 0) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek "Couldn't change owner of unix domain socket %s",
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek unixd_setup_child(); /* if running as root, switch to configured user/group */
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek const char * const *argv;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek sd2 = accept(sd, (struct sockaddr *)&unix_addr, &len);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher "Error accepting on cgid socket");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher r = apr_pcalloc(ptrans, sizeof(request_rec));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher procnew = apr_pcalloc(ptrans, sizeof(*procnew));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher rc = get_req(sd2, r, &argv0, &env, &req_type);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek "Error reading request on cgid socket");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_os_file_put(&r->server->error_log, &errfileno, 0, r->pool);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_os_file_put(&inout, &sd2, 0, r->pool);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (((rc = apr_procattr_create(&procattr, ptrans)) != APR_SUCCESS) ||
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* XXX apr_procattr_child_*_set() is creating an unnecessary
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * pipe between this process and the child being created...
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * It is cleaned up with the temporary pool for this request.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ((rc = apr_procattr_child_err_set(procattr, r->server->error_log, NULL)) != APR_SUCCESS) ||
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ((rc = apr_procattr_child_in_set(procattr, inout, NULL)) != APR_SUCCESS))) ||
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek ((rc = apr_procattr_child_out_set(procattr, inout, NULL)) != APR_SUCCESS) ||
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) ||
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ((rc = apr_procattr_cmdtype_set(procattr, cmd_type)) != APR_SUCCESS)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Something bad happened, tell the world. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek "couldn't set child process attributes: %s", r->filename);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher argv = (const char * const *)create_argv(r->pool, NULL, NULL, NULL, argv0, r->args);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* We want to close sd2 for the new CGI process too.
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * If it is left open it'll make ap_pass_brigade() block
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * waiting for EOF if CGI forked something running long.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * close(sd2) here should be okay, as CGI channel
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * is already dup()ed by apr_procattr_child_{in,out}_set()
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek rc = ap_os_create_privileged_process(r, procnew, argv0, argv,
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek (const char * const *)env,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Bad things happened. Everyone should have cleaned up. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "couldn't create child process: %d: %s", rc, r->filename);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozekstatic int cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_userdata_get(&data, userdata_key, main_server->process->pool);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek apr_pool_userdata_set((const void *)1, userdata_key,
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek apr_pool_cleanup_null, main_server->process->pool);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek for (m = ap_preloaded_modules; *m != NULL; m++)
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek daemon_should_exit = 0; /* clear setting from previous generation */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek "mod_cgid: Couldn't spawn cgid daemon process");
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek else if (daemon_pid == 0) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek procnew->err = procnew->in = procnew->out = NULL;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek apr_proc_other_child_register(procnew, cgid_maint, &procnew->pid, NULL, p);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cgid_pfn_reg_with_ssi = APR_RETRIEVE_OPTIONAL_FN(ap_register_include_handler);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek cgid_pfn_gtv = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_get_tag_and_value);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek cgid_pfn_ps = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_parse_string);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if ((cgid_pfn_reg_with_ssi) && (cgid_pfn_gtv) && (cgid_pfn_ps)) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek /* Required by mod_include filter. This is how mod_cgid registers
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * with mod_include to provide processing of the exec directive.
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozekstatic void *create_cgid_config(apr_pool_t *p, server_rec *s)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek (cgid_server_conf *) apr_pcalloc(p, sizeof(cgid_server_conf));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek c->sockname = ap_server_root_relative(p, DEFAULT_SOCKET);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozekstatic void *merge_cgid_config(apr_pool_t *p, void *basev, void *overridesv)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek cgid_server_conf *base = (cgid_server_conf *) basev, *overrides = (cgid_server_conf *) overridesv;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozekstatic const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(s->module_config,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek conf->logname = ap_server_root_relative(cmd->pool, arg);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek return apr_pstrcat(cmd->pool, "Invalid ScriptLog path ",
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, const char *arg)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(s->module_config,
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozekstatic const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, const char *arg)
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(s->module_config,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic const char *set_script_socket(cmd_parms *cmd, void *dummy, const char *arg)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(s->module_config,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek conf->sockname = ap_server_root_relative(cmd->pool, arg);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return apr_pstrcat(cmd->pool, "Invalid Scriptsock path ",
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek AP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "the name of a log for script debugging info"),
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek AP_INIT_TAKE1("ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek "the maximum length (in bytes) of the script debug log"),
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek AP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "the maximum size (in bytes) to record of a POST request"),
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek AP_INIT_TAKE1("Scriptsock", set_script_socket, NULL, RSRC_CONF,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "the name of the socket to use for communication with "
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek "the cgi daemon."),
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic int log_scripterror(request_rec *r, cgid_server_conf * conf, int ret,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* XXX Very expensive mainline case! Open, then getfileinfo! */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek r->args ? "?" : "", r->args ? r->args : "", r->protocol);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* "%% 500 /usr/local/apache/cgid-bin */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic int log_script(request_rec *r, cgid_server_conf * conf, int ret,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *buf;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* XXX Very expensive mainline case! Open, then getfileinfo! */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* Soak up script output */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek /* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek r->args ? "?" : "", r->args ? r->args : "", r->protocol);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek /* "%% 500 /usr/local/apache/cgid-bin" */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if ((r->method_number == M_POST || r->method_number == M_PUT)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (!APR_STATUS_IS_SUCCESS(rv) || (len == 0)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozekstatic apr_status_t close_unix_socket(void *thefd)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekstatic int connect_to_daemon(int *sdptr, request_rec *r,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher strcpy(unix_addr.sun_path, conf->sockname);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return log_scripterror(r, conf, HTTP_INTERNAL_SERVER_ERROR, errno,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek "unable to create socket to cgi daemon");
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek if (connect(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) < 0) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (errno == ECONNREFUSED && connect_tries < DEFAULT_CONNECT_ATTEMPTS) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_DEBUG, errno, r,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek "connect #%d to cgi daemon failed, sleeping before retry",
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (sliding_timer < apr_time_from_sec(2)) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, errno,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek "unable to connect to cgi daemon after multiple tries");
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_pool_cleanup_register(r->pool, (void *)sd, close_unix_socket,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek break; /* we got connected! */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek /* gotta try again, but make sure the cgid daemon is still around */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, errno,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "cgid daemon is gone; is Apache terminating?");
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozekstatic void discard_script_output(apr_bucket_brigade *bb)
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek const char *buf;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek/****************************************************************
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * Actual cgid handling...
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (strcmp(r->handler,CGI_MAGIC_TYPE) && strcmp(r->handler,"cgi-script"))
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* 99 out of 100 cgid scripts, this is all they support */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek conf = ap_get_module_config(r->server->module_config, &cgid_module);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher is_included = !strcmp(r->protocol, "INCLUDED");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((argv0 = strrchr(r->filename, '/')) != NULL)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if ((argv0 = strrchr(r->filename, '/')) != NULL)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "Options ExecCGI is off in this directory");
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "attempt to include NPH CGI script");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#error mod_cgid does not work on this platform. If you teach it to, look
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#error at mod_cgi.c for required code in this path.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "script not found or unable to stat");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "attempt to invoke directory as script");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((r->used_path_info == AP_REQ_REJECT_PATH_INFO) &&
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* default to accept */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "AcceptPathInfo off disallows user's path");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!ap_suexec_enabled) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (!ap_can_exec(&r->finfo))
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek "file permissions deny server execution");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher env = ap_create_environment(r->pool, r->subprocess_env);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((retval = connect_to_daemon(&sd, r, conf)) != OK) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek /* We are putting the socket discriptor into an apr_file_t so that we can
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * use a pipe bucket to send the data to the client.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Note that this does not register a cleanup for the socket. We did
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * that explicitly right after we created the socket.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_os_file_put(&tempsock, &sd, 0, r->pool);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((argv0 = strrchr(r->filename, '/')) != NULL)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Transfer any put/post args, CERN style...
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Note that we already ignore SIGPIPE in the core server.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbuf = apr_palloc(r->pool, conf->bufbytes + 1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *data;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* We can't do much with this. */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* If the child stopped, we still must read to EOS. */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* Keep writing data to the child until done or too much time
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * elapses with no progress or an error occurs.
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek rv = apr_file_write_full(tempsock, data, len, NULL);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* silly script stopped reading, soak up remaining message */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek /* we're done writing, or maybe we didn't write at all;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * force EOF on child's stdin so that the cgi detects end (or
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * absence) of data
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* Handle script return... */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bb = apr_brigade_create(r->pool, c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher b = apr_bucket_eos_create(c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((ret = ap_scan_script_header_err_brigade(r, bb, sbuf))) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return log_script(r, conf, ret, dbuf, sbuf, bb, NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher location = apr_table_get(r->headers_out, "Location");
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (location && location[0] == '/' && r->status == 200) {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek /* Soak up all the script output */
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek /* This redirect needs to be a GET no matter what the original
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * method was.
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek /* We already read the message body (if any), so don't allow
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * the redirected request to think it has one. We can ignore
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_table_unset(r->headers_in, "Content-Length");
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek /* XX Note that if a script wants to produce its own Redirect
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek * body, it now has to explicitly *say* "Status: 302"
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek /* Passing our socket down the filter chain in a pipe bucket
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * gives up the responsibility of closing the socket, so
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * get rid of the cleanup.
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek apr_pool_cleanup_kill(r->pool, (void *)sd, close_unix_socket);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* Passing our socket down the filter chain in a pipe bucket
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek * gives up the responsibility of closing the socket, so
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek * get rid of the cleanup.
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher apr_pool_cleanup_kill(r->pool, (void *)sd, close_unix_socket);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek /* get rid of all filters up through protocol... since we
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * haven't parsed off the headers, there is no way they can
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek r->output_filters = r->proto_output_filters = cur;
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek bb = apr_brigade_create(r->pool, c->bucket_alloc);
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek return OK; /* NOT r->status, even if it has changed. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*============================================================================
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek *============================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * This is the beginning of the cgi filter code moved from mod_include. This
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * is the code required to handle the "exec" SSI directive.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *============================================================================
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek *============================================================================*/
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozekstatic int include_cgi(char *s, request_rec *r, ap_filter_t *next,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_bucket *head_ptr, apr_bucket **inserted_head)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek request_rec *rr = ap_sub_req_lookup_uri(s, r, next);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* No hardwired path info or query allowed */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((rr->path_info && rr->path_info[0]) || rr->args) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Script gets parameters of the *document*, for back compatibility */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher rr->path_info = r->path_info; /* hard to get right; see mod_cgi.c */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Force sub_req to be treated as a CGI request, even if ordinary
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * typing rules would have called it something else.
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek /* Run it. */
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek const char *location = apr_table_get(rr->headers_out, "Location");
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* XXX: if most of this stuff is going to get copied anyway,
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek * it'd be more efficient to pstrcat it into a single pool buffer
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek * and a single pool bucket */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp_buck = apr_bucket_immortal_create("<A HREF=\"",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp2_buck = apr_bucket_heap_create(location, len_loc, NULL,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher tmp2_buck = apr_bucket_immortal_create("\">", sizeof("\">") - 1,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp2_buck = apr_bucket_heap_create(location, len_loc, NULL,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp2_buck = apr_bucket_immortal_create("</A>", sizeof("</A>") - 1,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* This is the special environment used for running the "exec cmd="
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * variety of SSI directives.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void add_ssi_vars(request_rec *r, ap_filter_t *next)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_table_setn(e, "PATH_INFO", ap_escape_shell_cmd(r->pool, r->path_info));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r, next);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info, NULL));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher char *arg_copy = apr_pstrdup(r->pool, r->args);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher apr_table_setn(e, "QUERY_STRING", r->args);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_table_setn(e, "QUERY_STRING_UNESCAPED", ap_escape_shell_cmd(r->pool, arg_copy));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagherstatic int include_cmd(include_ctx_t *ctx, apr_bucket_brigade **bb, char *command,
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(r->server->module_config,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher env = ap_create_environment(r->pool, r->subprocess_env);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if ((retval = connect_to_daemon(&sd, r, conf)) != OK) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek SPLIT_AND_PASS_PRETAG_BUCKETS(*bb, ctx, f->next, rc);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* We are putting the socket discriptor into an apr_file_t so that we can
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher * use a pipe bucket to send the data to the client.
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * Note that this does not register a cleanup for the socket. We did
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher * that explicitly right after we created the socket.
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher apr_os_file_put(&tempsock, &sd, 0, r->pool);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if ((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek location = apr_table_get(r->headers_out, "Location");
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (location && location[0] == '/' && r->status == 200) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher /* Soak up all the script output */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek /* This redirect needs to be a GET no matter what the original
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* We already read the message body (if any), so don't allow
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * the redirected request to think it has one. We can ignore
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_table_unset(r->headers_in, "Content-Length");
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek /* XX Note that if a script wants to produce its own Redirect
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * body, it now has to explicitly *say* "Status: 302"
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek /* Passing our socket down the filter chain in a pipe bucket
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek * gives up the responsibility of closing the socket, so
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek * get rid of the cleanup.
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek apr_pool_cleanup_kill(r->pool, (void *)sd, close_unix_socket);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek bcgi = apr_brigade_create(r->pool, r->connection->bucket_alloc);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek b = apr_bucket_pipe_create(tempsock, r->connection->bucket_alloc);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozekstatic int handle_exec(include_ctx_t *ctx, apr_bucket_brigade **bb, request_rec *r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_filter_t *f, apr_bucket *head_ptr, apr_bucket **inserted_head)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek "exec used but not allowed in %s", r->filename);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cgid_pfn_ps(r, ctx, tag_val, parsed_string, sizeof(parsed_string), 1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (include_cmd(ctx, bb, parsed_string, r, f) == -1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek "execution failure for parameter \"%s\" "
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "to tag exec in file %s", tag, r->filename);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* just in case some stooge changed directories */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cgid_pfn_ps(r, ctx, tag_val, parsed_string, sizeof(parsed_string), 0);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek SPLIT_AND_PASS_PRETAG_BUCKETS(*bb, ctx, f->next, retval);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (include_cgi(parsed_string, r, f->next, head_ptr, inserted_head) == -1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "invalid CGI ref \"%s\" in %s", tag_val, file);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "unknown parameter \"%s\" to tag exec in %s", tag, file);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek CREATE_ERROR_BUCKET(ctx, tmp_buck, head_ptr, *inserted_head);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek/*============================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *============================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * This is the end of the cgi filter code moved from mod_include.
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek *============================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *============================================================================*/
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek static const char * const aszPre[] = { "mod_include.c", NULL };
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ap_hook_post_config(cgid_init, aszPre, NULL, APR_HOOK_MIDDLE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_hook_handler(cgid_handler, NULL, NULL, APR_HOOK_MIDDLE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghermodule AP_MODULE_DECLARE_DATA cgid_module = {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher NULL, /* dir merger --- default is to override */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher merge_cgid_config, /* merge server config */