mod_cgid.c revision e0fe4de2016336428729a620ac0034cd1198ad7a
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* ====================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * The Apache Software License, Version 1.1
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * reserved.
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek *
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * Redistribution and use in source and binary forms, with or without
06c1952db1ab5598e3d68132f9c846bc59c94ef7Jakub Hrozek * modification, are permitted provided that the following conditions
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * are met:
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek *
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek * 1. Redistributions of source code must retain the above copyright
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek * notice, this list of conditions and the following disclaimer.
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek *
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.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
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 *
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 *
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.
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek *
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 *
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 * <http://www.apache.org/>.
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek *
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 */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * http_script: keeps all script-related ramblings together.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Compliant to cgi/1.1 spec
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Adapted by rst from original NCSA code by Rob McCool
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
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 * they fail.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_lib.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_strings.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_general.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_file_io.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_portable.h"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek#include "apr_buckets.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_optional.h"
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#include "apr_signal.h"
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#define APR_WANT_STRFUNC
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#include "apr_want.h"
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#if APR_HAVE_SYS_SOCKET_H
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#include <sys/socket.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#if APR_HAVE_UNISTD_H
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <unistd.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#if APR_HAVE_SYS_TYPES_H
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/types.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define CORE_PRIVATE
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek#include "util_filter.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "httpd.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "http_config.h"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek#include "http_request.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "http_core.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "http_protocol.h"
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#include "http_main.h"
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek#include "http_log.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "util_script.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "ap_mpm.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "unixd.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "mod_suexec.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "../filters/mod_include.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "mod_core.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* ### should be tossed in favor of APR */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/stat.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/un.h> /* for sockaddr_un */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghermodule AP_MODULE_DECLARE_DATA cgid_module;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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 Gallagher
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
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic apr_pool_t *pcgi;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int total_modules = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic pid_t daemon_pid;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int daemon_should_exit = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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
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 */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekstatic int is_scriptaliased(request_rec *r)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *t = apr_table_get(r->notes, "alias-forced-type");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return t && (!strcasecmp(t, "cgi-script"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* Configuration stuff */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define DEFAULT_LOGBYTES 10385760
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#define DEFAULT_BUFBYTES 1024
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#define DEFAULT_SOCKET DEFAULT_REL_RUNTIMEDIR "/cgisock"
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define CGI_REQ 1
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define SSI_REQ 2
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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 */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#ifndef DEFAULT_CGID_LISTENBACKLOG
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek#define DEFAULT_CGID_LISTENBACKLOG 100
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
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 *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Set this to 0 to continually retry until the connect works or Apache
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * terminates.
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek */
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek#ifndef DEFAULT_CONNECT_ATTEMPTS
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek#define DEFAULT_CONNECT_ATTEMPTS 15
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek#endif
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghertypedef struct {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *sockname;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek const char *logname;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek long logbytes;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek int bufbytes;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek} cgid_server_conf;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekstatic char **create_argv(apr_pool_t *p, char *path, char *user, char *group,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek char *av0, const char *args)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek{
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek int x, numwords;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek char **av;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *w;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int idx = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* count the number of keywords */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (x = 0, numwords = 1; args[x]; x++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (args[x] == '+') {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ++numwords;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (numwords > APACHE_ARG_MAX - 5) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher av = (char **) apr_pcalloc(p, (numwords + 5) * sizeof(char *));
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (path) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek av[idx++] = path;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (user) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher av[idx++] = user;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (group) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher av[idx++] = group;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher av[idx++] = apr_pstrdup(p, av0);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek for (x = 1; x <= numwords; x++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher w = ap_getword_nulls(p, &args, '+');
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (strcmp(w, "")) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_unescape_url(w);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher av[idx++] = ap_escape_shell_cmd(p, w);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher av[idx] = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return av;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek}
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#if APR_HAS_OTHER_CHILD
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void cgid_maint(int reason, void *data, apr_wait_t status)
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher{
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek pid_t *sd = data;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek switch (reason) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek case APR_OC_REASON_DEATH:
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek case APR_OC_REASON_RESTART:
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* don't do anything; server is stopping or restarting */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_proc_other_child_unregister(data);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek break;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek case APR_OC_REASON_LOST:
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 */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek kill(getpid(), AP_SIG_GRACEFUL);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek break;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek case APR_OC_REASON_UNREGISTER:
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* we get here when pcgi is cleaned up; pcgi gets cleaned
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * up when pconf gets cleaned up
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek kill(*sd, SIGHUP); /* send signal to daemon telling it to die */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek break;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek}
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek#endif
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozekstatic int get_req(int fd, request_rec *r, char **argv0, char ***env, int *req_type)
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek{
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek int i, len, j, rc;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek unsigned char *data;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek char **environ;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek core_dir_config *temp_core;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek void **dconf;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek module *suexec_mod = ap_find_linked_module("mod_suexec.c");
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek r->server = apr_pcalloc(r->pool, sizeof(server_rec));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek rc = read(fd, req_type, sizeof(int));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (rc != sizeof(int)) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return 1;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek rc = read(fd, &j, sizeof(int));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (rc != sizeof(int)) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek return 1;
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek rc = read(fd, &len, sizeof(int));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (rc != sizeof(int)) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return 1;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek data = apr_pcalloc(r->pool, len + 1); /* get a cleared byte for final '\0' */
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek rc = read(fd, data, len);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (rc != len) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek return 1;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek r->filename = ap_getword(r->pool, (const char **)&data, '\n');
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek *argv0 = ap_getword(r->pool, (const char **)&data, '\n');
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek r->uri = ap_getword(r->pool, (const char **)&data, '\n');
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek environ = apr_pcalloc(r->pool, (j + 2) *sizeof(char *));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek i = 0;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek for (i = 0; i < j; i++) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek environ[i] = ap_getword(r->pool, (const char **)&data, '\n');
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek *env = environ;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek r->args = ap_getword(r->pool, (const char **)&data, '\n');
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek rc = read(fd, &i, sizeof(int));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (rc != sizeof(int)) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return 1;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* add 1, so that if i == 0, we still malloc something. */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek dconf = (void **) apr_pcalloc(r->pool, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek temp_core = (core_dir_config *)apr_palloc(r->pool, sizeof(core_module));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek dconf[i] = (void *)temp_core;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (suexec_mod) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek suexec_config_t *suexec_cfg = apr_pcalloc(r->pool, sizeof(*suexec_cfg));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek rc = read(fd, &i, sizeof(int));
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (rc != sizeof(int)) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek return 1;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek }
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek rc = read(fd, suexec_cfg, sizeof(*suexec_cfg));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (rc != sizeof(*suexec_cfg)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return 1;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek dconf[i] = (void *)suexec_cfg;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek r->per_dir_config = (ap_conf_vector_t *)dconf;
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek#if 0
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek#ifdef RLIMIT_CPU
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek read(fd, &j, sizeof(int));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (j) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek temp_core->limit_cpu = (struct rlimit *)apr_palloc (sizeof(struct rlimit));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek read(fd, temp_core->limit_cpu, sizeof(struct rlimit));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek else {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek temp_core->limit_cpu = NULL;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek#endif
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek#if defined (RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek read(fd, &j, sizeof(int));
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (j) {
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));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek else {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek temp_core->limit_mem = NULL;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#endif
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#ifdef RLIMIT_NPROC
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek read(fd, &j, sizeof(int));
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek if (j) {
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));
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek else {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek temp_core->limit_nproc = NULL;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek#endif
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek#endif
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* basic notes table to avoid segfaults */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek r->notes = apr_table_make(r->pool, 1);
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* mod_userdir requires the mod_userdir_user note */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek rc = read(fd, &len, sizeof(len));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if ((rc == sizeof(len)) && len) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek data = apr_pcalloc(r->pool, len + 1); /* last byte is '\0' */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek rc = read(fd, data, len);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (rc != len) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek return 1;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek apr_table_set(r->notes,"mod_userdir_user", (const char *)data);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek return 0;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek}
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic void send_req(int fd, request_rec *r, char *argv0, char **env, int req_type)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek{
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek int len, r_type = req_type;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek int i = 0;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek char *data;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek module *suexec_mod = ap_find_linked_module("mod_suexec.c");
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek data = apr_pstrcat(r->pool, r->filename, "\n", argv0, "\n", r->uri, "\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i =0; env[i]; i++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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 }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Write the number of entries in the environment. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (write(fd, &i, sizeof(int)) < 0) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "write to cgi daemon process");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = 0; env[i]; i++) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek data = apr_pstrcat(r->pool, data, env[i], "\n", NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher data = apr_pstrcat(r->pool, data, r->args, NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher len = strlen(data);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Write the length of the concatenated env string. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (write(fd, &len, sizeof(int)) < 0) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "write to cgi daemon process");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* Write the concatted env string. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (write(fd, data, len) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "write to cgi daemon process");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
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");
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (suexec_mod) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher suexec_config_t *suexec_cfg = ap_get_module_config(r->per_dir_config,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher suexec_mod);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagher write(fd, &suexec_mod->module_index, sizeof(int));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher write(fd, suexec_cfg, sizeof(*suexec_cfg));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek#if 0
056302a92862fda16351d7192600746746f38e5dStephen Gallagher#ifdef RLIMIT_CPU
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (conf->limit_cpu) {
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek len = 1;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek write(fd, &len, sizeof(int));
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek write(fd, conf->limit_cpu, sizeof(struct rlimit));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek len = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher write(fd, &len, sizeof(int));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (conf->limit_mem) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher len = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher write(fd, &len, sizeof(int));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher write(fd, conf->limit_mem, sizeof(struct rlimit));
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek else {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek len = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher write(fd, &len, sizeof(int));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#ifdef RLIMIT_NPROC
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (conf->limit_nproc) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher len = 1;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek write(fd, &len, sizeof(int));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher write(fd, conf->limit_nproc, sizeof(struct rlimit));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher len = 0;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek write(fd, &len, sizeof(int));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* send a minimal notes table */
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek data = (char *) apr_table_get(r->notes, "mod_userdir_user");
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek if (data != NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher len = strlen(data);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek write(fd, &len, sizeof(len));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek write(fd, data, len);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek else {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek len = 0;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek write(fd, &len, sizeof(len));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher}
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void daemon_signal_handler(int sig)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (sig == SIGHUP) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ++daemon_should_exit;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagherstatic int cgid_server(void *data)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek struct sockaddr_un unix_addr;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek int sd, sd2, rc, req_type;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mode_t omask;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_socklen_t len;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_t *ptrans;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher server_rec *main_server = data;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cgid_server_conf *sconf = ap_get_module_config(main_server->module_config,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek &cgid_module);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_pool_create(&ptrans, pcgi);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_signal(SIGCHLD, SIG_IGN);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_signal(SIGHUP, daemon_signal_handler);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
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 sconf->sockname);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* just a warning; don't bail out */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
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");
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return errno;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek memset(&unix_addr, 0, sizeof(unix_addr));
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek unix_addr.sun_family = AF_UNIX;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek strcpy(unix_addr.sun_path, sconf->sockname);
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek
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 */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (rc < 0) {
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek "Couldn't bind unix domain socket %s",
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek sconf->sockname);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return errno;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek
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");
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return errno;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!geteuid()) {
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 sconf->sockname);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return errno;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek unixd_setup_child(); /* if running as root, switch to configured user/group */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek while (!daemon_should_exit) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek int errfileno = STDERR_FILENO;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek char *argv0;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek char **env;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek const char * const *argv;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek apr_int32_t in_pipe;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek apr_int32_t out_pipe;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek apr_int32_t err_pipe;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek apr_cmdtype_e cmd_type;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek request_rec *r;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek apr_procattr_t *procattr = NULL;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_proc_t *procnew = NULL;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek apr_file_t *inout;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek apr_pool_clear(ptrans);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek len = sizeof(unix_addr);
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek sd2 = accept(sd, (struct sockaddr *)&unix_addr, &len);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (sd2 < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (errno != EINTR) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek ap_log_error(APLOG_MARK, APLOG_ERR, errno,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher (server_rec *)data,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher "Error accepting on cgid socket");
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher continue;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher r = apr_pcalloc(ptrans, sizeof(request_rec));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher procnew = apr_pcalloc(ptrans, sizeof(*procnew));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek r->pool = ptrans;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher rc = get_req(sd2, r, &argv0, &env, &req_type);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (rc) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher main_server,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek "Error reading request on cgid socket");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(sd2);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
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);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (req_type == SSI_REQ) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek in_pipe = APR_NO_PIPE;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek out_pipe = APR_FULL_BLOCK;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek err_pipe = APR_NO_PIPE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cmd_type = APR_SHELLCMD;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher in_pipe = APR_CHILD_BLOCK;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher out_pipe = APR_CHILD_BLOCK;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher err_pipe = APR_CHILD_BLOCK;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cmd_type = APR_PROGRAM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (((rc = apr_procattr_create(&procattr, ptrans)) != APR_SUCCESS) ||
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ((req_type == CGI_REQ) &&
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher (((rc = apr_procattr_io_set(procattr,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek in_pipe,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher out_pipe,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher err_pipe)) != 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.
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek */
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 ((rc = apr_procattr_dir_set(procattr,
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);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher argv = (const char * const *)create_argv(r->pool, NULL, NULL, NULL, argv0, r->args);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
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()
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * above.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(sd2);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek rc = ap_os_create_privileged_process(r, procnew, argv0, argv,
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek (const char * const *)env,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher procattr, ptrans);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (rc != APR_SUCCESS) {
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);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozekstatic int cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher server_rec *main_server)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_proc_t *procnew;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher void *data;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek int first_time = 0;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek const char *userdata_key = "cgid_init";
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher module **m;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_userdata_get(&data, userdata_key, main_server->process->pool);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (!data) {
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek first_time = 1;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek apr_pool_userdata_set((const void *)1, userdata_key,
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek apr_pool_cleanup_null, main_server->process->pool);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (!first_time) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek total_modules = 0;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek for (m = ap_preloaded_modules; *m != NULL; m++)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek total_modules++;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek daemon_should_exit = 0; /* clear setting from previous generation */
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if ((daemon_pid = fork()) < 0) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek "mod_cgid: Couldn't spawn cgid daemon process");
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek return DECLINED;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek }
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek else if (daemon_pid == 0) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_create(&pcgi, p);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cgid_server(main_server);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek exit(-1);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek procnew = apr_pcalloc(p, sizeof(*procnew));
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek procnew->pid = daemon_pid;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek procnew->err = procnew->in = procnew->out = NULL;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek#if APR_HAS_OTHER_CHILD
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek apr_proc_other_child_register(procnew, cgid_maint, &procnew->pid, NULL, p);
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek#endif
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek
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
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.
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek */
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cgid_pfn_reg_with_ssi("exec", handle_exec);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek return OK;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek}
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozekstatic void *create_cgid_config(apr_pool_t *p, server_rec *s)
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek{
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cgid_server_conf *c =
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek (cgid_server_conf *) apr_pcalloc(p, sizeof(cgid_server_conf));
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek c->logname = NULL;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek c->logbytes = DEFAULT_LOGBYTES;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek c->bufbytes = DEFAULT_BUFBYTES;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek c->sockname = ap_server_root_relative(p, DEFAULT_SOCKET);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek return c;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek}
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozekstatic void *merge_cgid_config(apr_pool_t *p, void *basev, void *overridesv)
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek{
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek cgid_server_conf *base = (cgid_server_conf *) basev, *overrides = (cgid_server_conf *) overridesv;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek return overrides->logname ? overrides : base;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek}
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozekstatic const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek{
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek server_rec *s = cmd->server;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(s->module_config,
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek &cgid_module);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek conf->logname = ap_server_root_relative(cmd->pool, arg);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (!conf->logname) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek return apr_pstrcat(cmd->pool, "Invalid ScriptLog path ",
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek arg, NULL);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek return NULL;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek}
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, const char *arg)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek server_rec *s = cmd->server;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(s->module_config,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek &cgid_module);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek conf->logbytes = atol(arg);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return NULL;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek}
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozekstatic const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, const char *arg)
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek{
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek server_rec *s = cmd->server;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(s->module_config,
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek &cgid_module);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek conf->bufbytes = atoi(arg);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek return NULL;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek}
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic const char *set_script_socket(cmd_parms *cmd, void *dummy, const char *arg)
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek{
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek server_rec *s = cmd->server;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(s->module_config,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek &cgid_module);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek conf->sockname = ap_server_root_relative(cmd->pool, arg);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (!conf->sockname) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return apr_pstrcat(cmd->pool, "Invalid Scriptsock path ",
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek arg, NULL);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return NULL;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek}
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic const command_rec cgid_cmds[] =
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
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 Hrozek {NULL}
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek};
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic int log_scripterror(request_rec *r, cgid_server_conf * conf, int ret,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_status_t rv, char *error)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_t *f = NULL;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct stat finfo;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek char time_str[APR_CTIME_LEN];
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek int log_flags = rv ? APLOG_ERR : APLOG_ERR;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek ap_log_rerror(APLOG_MARK, log_flags, rv, r,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "%s: %s", error, r->filename);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* XXX Very expensive mainline case! Open, then getfileinfo! */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!conf->logname ||
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ((stat(conf->logname, &finfo) == 0)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek && (finfo.st_size > conf->logbytes)) ||
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek (apr_file_open(&f, conf->logname,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_ctime(time_str, apr_time_now());
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 Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_printf(f, "%%error\n%s\n", error);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_file_close(f);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek}
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
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,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_t *script_err)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
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 char argsbuffer[HUGE_STRING_LEN];
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_file_t *f = NULL;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_bucket *e;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *buf;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_size_t len;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_status_t rv;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek int first;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek int i;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek struct stat finfo;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek char time_str[APR_CTIME_LEN];
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* XXX Very expensive mainline case! Open, then getfileinfo! */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!conf->logname ||
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek ((stat(conf->logname, &finfo) == 0)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek && (finfo.st_size > conf->logbytes)) ||
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek (apr_file_open(&f, conf->logname,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* Soak up script output */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek discard_script_output(bb);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (script_err) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek script_err) == APR_SUCCESS)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek continue;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return ret;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek /* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_ctime(time_str, apr_time_now());
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
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_puts("%request\n", f);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek for (i = 0; i < hdrs_arr->nelts; ++i) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (!hdrs[i].key)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek continue;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if ((r->method_number == M_POST || r->method_number == M_PUT)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek && *dbuf) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_printf(f, "\n%s\n", dbuf);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_puts("%response\n", f);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek hdrs_arr = apr_table_elts(r->err_headers_out);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek for (i = 0; i < hdrs_arr->nelts; ++i) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (!hdrs[i].key)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek continue;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (sbuf && *sbuf)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_printf(f, "%s\n", sbuf);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher first = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BRIGADE_FOREACH(e, bb) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (APR_BUCKET_IS_EOS(e)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (!APR_STATUS_IS_SUCCESS(rv) || (len == 0)) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek break;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (first) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_file_puts("%stdout\n", f);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek first = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_write(f, buf, &len);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_puts("\n", f);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (script_err) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek script_err) == APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_puts("%stderr\n", f);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_puts(argsbuffer, f);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher script_err) == APR_SUCCESS)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_puts(argsbuffer, f);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_puts("\n", f);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (script_err) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_close(script_err);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_close(f);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozekstatic apr_status_t close_unix_socket(void *thefd)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int fd = (int)thefd;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return close(fd);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekstatic int connect_to_daemon(int *sdptr, request_rec *r,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher cgid_server_conf *conf)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek struct sockaddr_un unix_addr;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int sd;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int connect_tries;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher apr_interval_time_t sliding_timer;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher memset(&unix_addr, 0, sizeof(unix_addr));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher unix_addr.sun_family = AF_UNIX;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher strcpy(unix_addr.sun_path, conf->sockname);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek connect_tries = 0;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek sliding_timer = 100000; /* 100 milliseconds */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek while (1) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ++connect_tries;
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");
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
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 connect_tries);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek close(sd);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher apr_sleep(sliding_timer);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (sliding_timer < apr_time_from_sec(2)) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek sliding_timer *= 2;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher else {
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek close(sd);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, errno,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek "unable to connect to cgi daemon after multiple tries");
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek else {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_pool_cleanup_register(r->pool, (void *)sd, close_unix_socket,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_pool_cleanup_null);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek break; /* we got connected! */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek /* gotta try again, but make sure the cgid daemon is still around */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (kill(daemon_pid, 0) != 0) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, errno,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek "cgid daemon is gone; is Apache terminating?");
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek *sdptr = sd;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek return OK;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek}
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozekstatic void discard_script_output(apr_bucket_brigade *bb)
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek{
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek apr_bucket *e;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek const char *buf;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek apr_size_t len;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek apr_status_t rv;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek APR_BRIGADE_FOREACH(e, bb) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (APR_BUCKET_IS_EOS(e)) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek break;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (!APR_STATUS_IS_SUCCESS(rv)) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek break;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek}
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek/****************************************************************
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek *
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * Actual cgid handling...
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek */
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozekstatic int cgid_handler(request_rec *r)
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek{
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek conn_rec *c = r->connection;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek int retval, nph, dbpos = 0;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek char *argv0, *dbuf = NULL;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek apr_bucket_brigade *bb;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek apr_bucket *b;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek cgid_server_conf *conf;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek int is_included;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek int seen_eos, child_stopped_reading;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int sd;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek char **env;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_t *tempsock;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (strcmp(r->handler,CGI_MAGIC_TYPE) && strcmp(r->handler,"cgi-script"))
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return DECLINED;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (r->method_number == M_OPTIONS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* 99 out of 100 cgid scripts, this is all they support */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher r->allowed |= (AP_METHOD_BIT << M_GET);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher r->allowed |= (AP_METHOD_BIT << M_POST);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return DECLINED;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek conf = ap_get_module_config(r->server->module_config, &cgid_module);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher is_included = !strcmp(r->protocol, "INCLUDED");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((argv0 = strrchr(r->filename, '/')) != NULL)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher argv0++;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek else
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher argv0 = r->filename;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher nph = !(strncmp(argv0, "nph-", 4));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if ((argv0 = strrchr(r->filename, '/')) != NULL)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek argv0++;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher argv0 = r->filename;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
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");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (nph && is_included)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "attempt to include NPH CGI script");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#if defined(OS2) || defined(WIN32)
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#else
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (r->finfo.filetype == 0)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "script not found or unable to stat");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (r->finfo.filetype == APR_DIR)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "attempt to invoke directory as script");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((r->used_path_info == AP_REQ_REJECT_PATH_INFO) &&
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek r->path_info && *r->path_info)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher {
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 }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
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");
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek*/
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ap_add_common_vars(r);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_add_cgi_vars(r);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher env = ap_create_environment(r->pool, r->subprocess_env);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((retval = connect_to_daemon(&sd, r, conf)) != OK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return retval;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher send_req(sd, r, argv0, env, CGI_REQ);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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 */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_os_file_put(&tempsock, &sd, 0, r->pool);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((argv0 = strrchr(r->filename, '/')) != NULL)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher argv0++;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher argv0 = r->filename;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Transfer any put/post args, CERN style...
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Note that we already ignore SIGPIPE in the core server.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek seen_eos = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher child_stopped_reading = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (conf->logname) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbuf = apr_palloc(r->pool, conf->bufbytes + 1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbpos = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher do {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_bucket *bucket;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_status_t rv;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek APR_BLOCK_READ, HUGE_STRING_LEN);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (rv != APR_SUCCESS) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return rv;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek APR_BRIGADE_FOREACH(bucket, bb) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *data;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_size_t len;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (APR_BUCKET_IS_EOS(bucket)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek seen_eos = 1;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek break;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* We can't do much with this. */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (APR_BUCKET_IS_FLUSH(bucket)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek continue;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* If the child stopped, we still must read to EOS. */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (child_stopped_reading) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek continue;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* read */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (conf->logname && dbpos < conf->bufbytes) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek int cursize;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if ((dbpos + len) > conf->bufbytes) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek cursize = conf->bufbytes - dbpos;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cursize = len;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher memcpy(dbuf + dbpos, data, cursize);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbpos += cursize;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
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 */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek rv = apr_file_write_full(tempsock, data, len, NULL);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (rv != APR_SUCCESS) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* silly script stopped reading, soak up remaining message */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek child_stopped_reading = 1;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_brigade_cleanup(bb);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek while (!seen_eos);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (conf->logname) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek dbuf[dbpos] = '\0';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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 */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek shutdown(sd, 1);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek /* Handle script return... */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!nph) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *location;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek char sbuf[MAX_STRING_LEN];
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bb = apr_brigade_create(r->pool, c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek APR_BRIGADE_INSERT_TAIL(bb, b);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher b = apr_bucket_eos_create(c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BRIGADE_INSERT_TAIL(bb, b);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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 }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher location = apr_table_get(r->headers_out, "Location");
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (location && location[0] == '/' && r->status == 200) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek /* Soak up all the script output */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek discard_script_output(bb);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek apr_brigade_destroy(bb);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek /* This redirect needs to be a GET no matter what the original
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * method was.
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek r->method = apr_pstrdup(r->pool, "GET");
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek r->method_number = M_GET;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
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.
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek apr_table_unset(r->headers_in, "Content-Length");
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek ap_internal_redirect_handler(location, r);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return OK;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else if (location && r->status == 200) {
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"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek discard_script_output(bb);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_brigade_destroy(bb);
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek return HTTP_MOVED_TEMPORARILY;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek if (!r->header_only) {
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.
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek */
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek apr_pool_cleanup_kill(r->pool, (void *)sd, close_unix_socket);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek ap_pass_brigade(r->output_filters, bb);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if (nph) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek struct ap_filter_t *cur;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
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 */
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher apr_pool_cleanup_kill(r->pool, (void *)sd, close_unix_socket);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
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
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek * work
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek cur = r->proto_output_filters;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek cur = cur->next;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek r->output_filters = r->proto_output_filters = cur;
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek bb = apr_brigade_create(r->pool, c->bucket_alloc);
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek APR_BRIGADE_INSERT_TAIL(bb, b);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek b = apr_bucket_eos_create(c->bucket_alloc);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek APR_BRIGADE_INSERT_TAIL(bb, b);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek ap_pass_brigade(r->output_filters, bb);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek }
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek return OK; /* NOT r->status, even if it has changed. */
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek}
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek request_rec *rr = ap_sub_req_lookup_uri(s, r, next);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int rr_status;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_bucket *tmp_buck, *tmp2_buck;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if (rr->status != HTTP_OK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_destroy_sub_req(rr);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return -1;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* No hardwired path info or query allowed */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((rr->path_info && rr->path_info[0]) || rr->args) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ap_destroy_sub_req(rr);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek return -1;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (rr->finfo.filetype != APR_REG) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_destroy_sub_req(rr);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Script gets parameters of the *document*, for back compatibility */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher rr->path_info = r->path_info; /* hard to get right; see mod_cgi.c */
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek rr->args = r->args;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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 */
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek ap_set_content_type(rr, CGI_MAGIC_TYPE);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek /* Run it. */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek rr_status = ap_run_sub_req(rr);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (ap_is_HTTP_REDIRECT(rr_status)) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek apr_size_t len_loc;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek const char *location = apr_table_get(rr->headers_out, "Location");
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek conn_rec *c = r->connection;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek location = ap_escape_html(rr->pool, location);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek len_loc = strlen(location);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp_buck = apr_bucket_immortal_create("<A HREF=\"",
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek sizeof("<A HREF=\"") - 1,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp_buck);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp2_buck = apr_bucket_heap_create(location, len_loc, NULL,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher tmp2_buck = apr_bucket_immortal_create("\">", sizeof("\">") - 1,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp2_buck = apr_bucket_heap_create(location, len_loc, NULL,
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek c->bucket_alloc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp2_buck = apr_bucket_immortal_create("</A>", sizeof("</A>") - 1,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher c->bucket_alloc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher APR_BUCKET_INSERT_BEFORE(head_ptr, tmp2_buck);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (*inserted_head == NULL) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek *inserted_head = tmp_buck;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ap_destroy_sub_req(rr);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* This is the special environment used for running the "exec cmd="
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * variety of SSI directives.
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void add_ssi_vars(request_rec *r, ap_filter_t *next)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_table_t *e = r->subprocess_env;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (r->path_info && r->path_info[0] != '\0') {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher request_rec *pa_req;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_table_setn(e, "PATH_INFO", ap_escape_shell_cmd(r->pool, r->path_info));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r, next);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (pa_req->filename) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_table_setn(e, "PATH_TRANSLATED",
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info, NULL));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ap_destroy_sub_req(pa_req);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (r->args) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher char *arg_copy = apr_pstrdup(r->pool, r->args);
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher apr_table_setn(e, "QUERY_STRING", r->args);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek ap_unescape_url(arg_copy);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_table_setn(e, "QUERY_STRING_UNESCAPED", ap_escape_shell_cmd(r->pool, arg_copy));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher}
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagherstatic int include_cmd(include_ctx_t *ctx, apr_bucket_brigade **bb, char *command,
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek request_rec *r, ap_filter_t *f)
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek{
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek char **env;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher const char *location;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek int sd;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_status_t rc = APR_SUCCESS;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher int retval;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher apr_bucket_brigade *bcgi;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher apr_bucket *b;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher apr_file_t *tempsock = NULL;
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek cgid_server_conf *conf = ap_get_module_config(r->server->module_config,
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek &cgid_module);
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher add_ssi_vars(r, f->next);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher env = ap_create_environment(r->pool, r->subprocess_env);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if ((retval = connect_to_daemon(&sd, r, conf)) != OK) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return retval;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek SPLIT_AND_PASS_PRETAG_BUCKETS(*bb, ctx, f->next, rc);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (rc != APR_SUCCESS) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return rc;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek send_req(sd, r, command, env, SSI_REQ);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
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.
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek */
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher apr_os_file_put(&tempsock, &sd, 0, r->pool);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if ((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return retval;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek location = apr_table_get(r->headers_out, "Location");
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (location && location[0] == '/' && r->status == 200) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher char argsbuffer[HUGE_STRING_LEN];
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher /* Soak up all the script output */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek tempsock) == APR_SUCCESS) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher continue;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek /* This redirect needs to be a GET no matter what the original
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher * method was.
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher */
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher r->method = apr_pstrdup(r->pool, "GET");
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek r->method_number = M_GET;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
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 */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_table_unset(r->headers_in, "Content-Length");
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek ap_internal_redirect_handler(location, r);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek return OK;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek }
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek else if (location && r->status == 200) {
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 */
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek return HTTP_MOVED_TEMPORARILY;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek }
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if (!r->header_only) {
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 */
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek apr_pool_cleanup_kill(r->pool, (void *)sd, close_unix_socket);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
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 Hrozek APR_BRIGADE_INSERT_TAIL(bcgi, b);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek ap_pass_brigade(f->next, bcgi);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek }
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return 0;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek}
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
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)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *tag = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *tag_val = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *file = r->filename;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_bucket *tmp_buck;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek char parsed_string[MAX_STRING_LEN];
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *inserted_head = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ctx->flags & FLAG_PRINTING) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ctx->flags & FLAG_NO_EXEC) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
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 }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher while (1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cgid_pfn_gtv(ctx, &tag, &tag_val, 1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tag_val == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tag == NULL) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return (0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!strcmp(tag, "cmd")) {
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 }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* just in case some stooge changed directories */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else if (!strcmp(tag, "cgi")) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_status_t retval = APR_SUCCESS;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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 (retval != APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return retval;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
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);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else {
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);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return 0;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek}
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek/*============================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *============================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * This is the end of the cgi filter code moved from mod_include.
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek *============================================================================
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *============================================================================*/
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagherstatic void register_hook(apr_pool_t *p)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek static const char * const aszPre[] = { "mod_include.c", NULL };
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
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);
d6d50c17e94dc0d3000345e8a933311c14bbb828Jakub Hrozek}
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghermodule AP_MODULE_DECLARE_DATA cgid_module = {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher STANDARD20_MODULE_STUFF,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher NULL, /* dir config creater */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher NULL, /* dir merger --- default is to override */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek create_cgid_config, /* server config */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher merge_cgid_config, /* merge server config */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cgid_cmds, /* command table */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher register_hook /* register_handlers */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher