1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
* http_script: keeps all script-related ramblings together.
20
* Compliant to cgi/1.1 spec
22
* Adapted by rst from original NCSA code by Rob McCool
24
* Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
25
* custom error responses, and DOCUMENT_ROOT because we found it useful.
26
* It also adds SERVER_ADMIN - useful for scripts to know who to mail when
31
#include "apr_strings.h"
32
#include "apr_general.h"
33
#include "apr_file_io.h"
34
#include "apr_portable.h"
35
#include "apr_buckets.h"
36
#include "apr_optional.h"
37
#include "apr_signal.h"
39
#define APR_WANT_STRFUNC
42
#if APR_HAVE_SYS_SOCKET_H
43
#include <sys/socket.h>
48
#if APR_HAVE_SYS_TYPES_H
49
#include <sys/types.h>
54
#include "util_filter.h"
56
#include "http_config.h"
57
#include "http_request.h"
58
#include "http_core.h"
59
#include "http_protocol.h"
60
#include "http_main.h"
62
#include "util_script.h"
65
#include "mod_suexec.h"
66
#include "../filters/mod_include.h"
71
/* ### should be tossed in favor of APR */
73
#include <sys/un.h> /* for sockaddr_un */
76
module AP_MODULE_DECLARE_DATA cgid_module;
78
static int cgid_start(apr_pool_t *p, server_rec *main_server, apr_proc_t *procnew);
79
static int cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_server);
80
static int handle_exec(include_ctx_t *ctx, ap_filter_t *f, apr_bucket_brigade *bb);
82
static APR_OPTIONAL_FN_TYPE(ap_register_include_handler) *cgid_pfn_reg_with_ssi;
83
static APR_OPTIONAL_FN_TYPE(ap_ssi_get_tag_and_value) *cgid_pfn_gtv;
84
static APR_OPTIONAL_FN_TYPE(ap_ssi_parse_string) *cgid_pfn_ps;
86
static apr_pool_t *pcgi = NULL;
87
static int total_modules = 0;
88
static pid_t daemon_pid;
89
static int daemon_should_exit = 0;
90
static server_rec *root_server = NULL;
91
static apr_pool_t *root_pool = NULL;
92
static const char *sockname;
93
static pid_t parent_pid;
94
static ap_unix_identity_t empty_ugid = { (uid_t)-1, (gid_t)-1, -1 };
96
/* Read and discard the data in the brigade produced by a CGI script */
97
static void discard_script_output(apr_bucket_brigade *bb);
99
/* This doer will only ever be called when we are sure that we have
102
static ap_unix_identity_t *cgid_suexec_id_doer(const request_rec *r)
104
return (ap_unix_identity_t *)
105
ap_get_module_config(r->request_config, &cgid_module);
108
/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
109
* in ScriptAliased directories, which means we need to know if this
110
* request came through ScriptAlias or not... so the Alias module
111
* leaves a note for us.
114
static int is_scriptaliased(request_rec *r)
116
const char *t = apr_table_get(r->notes, "alias-forced-type");
117
return t && (!strcasecmp(t, "cgi-script"));
120
/* Configuration stuff */
122
#define DEFAULT_LOGBYTES 10385760
123
#define DEFAULT_BUFBYTES 1024
124
#define DEFAULT_SOCKET DEFAULT_REL_RUNTIMEDIR "/cgisock"
128
#define GETPID_REQ 3 /* get the pid of script created for prior request */
130
#define ERRFN_USERDATA_KEY "CGIDCHILDERRFN"
132
/* DEFAULT_CGID_LISTENBACKLOG controls the max depth on the unix socket's
133
* pending connection queue. If a bunch of cgi requests arrive at about
134
* the same time, connections from httpd threads/processes will back up
135
* in the queue while the cgid process slowly forks off a child to process
136
* each connection on the unix socket. If the queue is too short, the
137
* httpd process will get ECONNREFUSED when trying to connect.
139
#ifndef DEFAULT_CGID_LISTENBACKLOG
140
#define DEFAULT_CGID_LISTENBACKLOG 100
143
/* DEFAULT_CONNECT_ATTEMPTS controls how many times we'll try to connect
144
* to the cgi daemon from the thread/process handling the cgi request.
145
* Generally we want to retry when we get ECONNREFUSED since it is
146
* probably because the listen queue is full. We need to try harder so
147
* the client doesn't see it as a 503 error.
149
* Set this to 0 to continually retry until the connect works or Apache
152
#ifndef DEFAULT_CONNECT_ATTEMPTS
153
#define DEFAULT_CONNECT_ATTEMPTS 15
163
int req_type; /* request type (CGI_REQ, SSI_REQ, etc.) */
164
unsigned long conn_id; /* connection id; daemon uses this as a hash value
165
* to find the script pid when it is time for that
166
* process to be cleaned up
168
pid_t ppid; /* sanity check for config problems leading to
169
* wrong cgid socket use
171
int core_module_index;
173
ap_unix_identity_t ugid;
174
apr_size_t filename_len;
175
apr_size_t argv0_len;
178
int loglevel; /* to stuff in server_rec */
181
/* This routine is called to create the argument list to be passed
182
* to the CGI script. When suexec is enabled, the suexec path, user, and
183
* group are the first three arguments to be passed; if not, all three
184
* must be NULL. The query info is split into separate arguments, where
185
* "+" is the separator between keyword arguments.
187
* Do not process the args if they containing an '=' assignment.
189
static char **create_argv(apr_pool_t *p, char *path, char *user, char *group,
190
char *av0, const char *args)
197
if (ap_strchr_c(args, '=')) {
201
/* count the number of keywords */
203
for (x = 0, numwords = 1; args[x]; x++) {
204
if (args[x] == '+') {
210
if (numwords > APACHE_ARG_MAX - 5) {
211
numwords = APACHE_ARG_MAX - 5; /* Truncate args to prevent overrun */
213
av = (char **) apr_pcalloc(p, (numwords + 5) * sizeof(char *));
225
av[idx++] = apr_pstrdup(p, av0);
227
for (x = 1; x <= numwords; x++) {
228
w = ap_getword_nulls(p, &args, '+');
231
av[idx++] = ap_escape_shell_cmd(p, w);
238
#if APR_HAS_OTHER_CHILD
239
static void cgid_maint(int reason, void *data, apr_wait_t status)
241
apr_proc_t *proc = data;
246
case APR_OC_REASON_DEATH:
247
apr_proc_other_child_unregister(data);
248
/* If apache is not terminating or restarting,
249
* restart the cgid daemon
251
stopping = 1; /* if MPM doesn't support query,
252
* assume we shouldn't restart daemon
254
if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state) == APR_SUCCESS &&
255
mpm_state != AP_MPMQ_STOPPING) {
259
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
260
"cgid daemon process died, restarting");
261
cgid_start(root_pool, root_server, proc);
264
case APR_OC_REASON_RESTART:
265
/* don't do anything; server is stopping or restarting */
266
apr_proc_other_child_unregister(data);
268
case APR_OC_REASON_LOST:
269
/* Restart the child cgid daemon process */
270
apr_proc_other_child_unregister(data);
271
cgid_start(root_pool, root_server, proc);
273
case APR_OC_REASON_UNREGISTER:
274
/* we get here when pcgi is cleaned up; pcgi gets cleaned
275
* up when pconf gets cleaned up
277
kill(proc->pid, SIGHUP); /* send signal to daemon telling it to die */
279
/* Remove the cgi socket, we must do it here in order to try and
280
* guarantee the same permissions as when the socket was created.
282
if (unlink(sockname) < 0 && errno != ENOENT) {
283
ap_log_error(APLOG_MARK, APLOG_ERR, errno, NULL,
284
"Couldn't unlink unix domain socket %s",
292
/* deal with incomplete reads and signals
293
* assume you really have to read buf_size bytes
295
static apr_status_t sock_read(int fd, void *vbuf, size_t buf_size)
299
size_t bytes_read = 0;
303
rc = read(fd, buf + bytes_read, buf_size - bytes_read);
304
} while (rc < 0 && errno == EINTR);
308
case 0: /* unexpected */
313
} while (bytes_read < buf_size);
320
static apr_status_t sock_write(int fd, const void *buf, size_t buf_size)
325
rc = write(fd, buf, buf_size);
326
} while (rc < 0 && errno == EINTR);
334
static apr_status_t get_req(int fd, request_rec *r, char **argv0, char ***env,
339
core_request_config *temp_core;
343
r->server = apr_pcalloc(r->pool, sizeof(server_rec));
345
/* read the request header */
346
stat = sock_read(fd, req, sizeof(*req));
347
if (stat != APR_SUCCESS) {
350
r->server->loglevel = req->loglevel;
351
if (req->req_type == GETPID_REQ) {
352
/* no more data sent for this request */
356
/* handle module indexes and such */
357
rconf = (void **) apr_pcalloc(r->pool, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
359
temp_core = (core_request_config *)apr_palloc(r->pool, sizeof(core_module));
360
rconf[req->core_module_index] = (void *)temp_core;
361
r->request_config = (ap_conf_vector_t *)rconf;
362
ap_set_module_config(r->request_config, &cgid_module, (void *)&req->ugid);
364
/* Read the filename, argv0, uri, and args */
365
r->filename = apr_pcalloc(r->pool, req->filename_len + 1);
366
*argv0 = apr_pcalloc(r->pool, req->argv0_len + 1);
367
r->uri = apr_pcalloc(r->pool, req->uri_len + 1);
368
if ((stat = sock_read(fd, r->filename, req->filename_len)) != APR_SUCCESS ||
369
(stat = sock_read(fd, *argv0, req->argv0_len)) != APR_SUCCESS ||
370
(stat = sock_read(fd, r->uri, req->uri_len)) != APR_SUCCESS) {
374
r->args = apr_pcalloc(r->pool, req->args_len + 1); /* empty string if no args */
376
if ((stat = sock_read(fd, r->args, req->args_len)) != APR_SUCCESS) {
381
/* read the environment variables */
382
environ = apr_pcalloc(r->pool, (req->env_count + 2) *sizeof(char *));
383
for (i = 0; i < req->env_count; i++) {
386
if ((stat = sock_read(fd, &curlen, sizeof(curlen))) != APR_SUCCESS) {
389
environ[i] = apr_pcalloc(r->pool, curlen + 1);
390
if ((stat = sock_read(fd, environ[i], curlen)) != APR_SUCCESS) {
398
sock_read(fd, &j, sizeof(int));
400
temp_core->limit_cpu = (struct rlimit *)apr_palloc (sizeof(struct rlimit));
401
sock_read(fd, temp_core->limit_cpu, sizeof(struct rlimit));
404
temp_core->limit_cpu = NULL;
408
#if defined (RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
409
sock_read(fd, &j, sizeof(int));
411
temp_core->limit_mem = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
412
sock_read(fd, temp_core->limit_mem, sizeof(struct rlimit));
415
temp_core->limit_mem = NULL;
420
sock_read(fd, &j, sizeof(int));
422
temp_core->limit_nproc = (struct rlimit *)apr_palloc(r->pool, sizeof(struct rlimit));
423
sock_read(fd, temp_core->limit_nproc, sizeof(struct rlimit));
426
temp_core->limit_nproc = NULL;
434
static apr_status_t send_req(int fd, request_rec *r, char *argv0, char **env,
438
cgid_req_t req = {0};
440
ap_unix_identity_t * ugid = ap_run_get_suexec_identity(r);
443
req.ugid = empty_ugid;
445
memcpy(&req.ugid, ugid, sizeof(ap_unix_identity_t));
448
req.req_type = req_type;
449
req.ppid = parent_pid;
450
req.conn_id = r->connection->id;
451
req.core_module_index = core_module.module_index;
452
for (req.env_count = 0; env[req.env_count]; req.env_count++) {
455
req.filename_len = strlen(r->filename);
456
req.argv0_len = strlen(argv0);
457
req.uri_len = strlen(r->uri);
458
req.args_len = r->args ? strlen(r->args) : 0;
459
req.loglevel = r->server->loglevel;
461
/* Write the request header */
462
if ((stat = sock_write(fd, &req, sizeof(req))) != APR_SUCCESS) {
466
/* Write filename, argv0, uri, and args */
467
if ((stat = sock_write(fd, r->filename, req.filename_len)) != APR_SUCCESS ||
468
(stat = sock_write(fd, argv0, req.argv0_len)) != APR_SUCCESS ||
469
(stat = sock_write(fd, r->uri, req.uri_len)) != APR_SUCCESS) {
473
if ((stat = sock_write(fd, r->args, req.args_len)) != APR_SUCCESS) {
478
/* write the environment variables */
479
for (i = 0; i < req.env_count; i++) {
480
apr_size_t curlen = strlen(env[i]);
482
if ((stat = sock_write(fd, &curlen, sizeof(curlen))) != APR_SUCCESS) {
486
if ((stat = sock_write(fd, env[i], curlen)) != APR_SUCCESS) {
493
if (conf->limit_cpu) {
495
stat = sock_write(fd, &len, sizeof(int));
496
stat = sock_write(fd, conf->limit_cpu, sizeof(struct rlimit));
500
stat = sock_write(fd, &len, sizeof(int));
504
#if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
505
if (conf->limit_mem) {
507
stat = sock_write(fd, &len, sizeof(int));
508
stat = sock_write(fd, conf->limit_mem, sizeof(struct rlimit));
512
stat = sock_write(fd, &len, sizeof(int));
517
if (conf->limit_nproc) {
519
stat = sock_write(fd, &len, sizeof(int));
520
stat = sock_write(fd, conf->limit_nproc, sizeof(struct rlimit));
524
stat = sock_write(fd, &len, sizeof(int));
531
static void daemon_signal_handler(int sig)
534
++daemon_should_exit;
538
static void cgid_child_errfn(apr_pool_t *pool, apr_status_t err,
539
const char *description)
544
apr_pool_userdata_get(&vr, ERRFN_USERDATA_KEY, pool);
547
/* sure we got r, but don't call ap_log_rerror() because we don't
548
* have r->headers_in and possibly other storage referenced by
551
ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server, "%s", description);
554
static int cgid_server(void *data)
556
struct sockaddr_un unix_addr;
561
server_rec *main_server = data;
562
apr_hash_t *script_hash = apr_hash_make(pcgi);
564
apr_pool_create(&ptrans, pcgi);
566
apr_signal(SIGCHLD, SIG_IGN);
567
apr_signal(SIGHUP, daemon_signal_handler);
569
/* Close our copy of the listening sockets */
570
ap_close_listeners();
572
/* cgid should use its own suexec doer */
573
ap_hook_get_suexec_identity(cgid_suexec_id_doer, NULL, NULL,
574
APR_HOOK_REALLY_FIRST);
577
if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
578
ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
579
"Couldn't create unix domain socket");
583
memset(&unix_addr, 0, sizeof(unix_addr));
584
unix_addr.sun_family = AF_UNIX;
585
apr_cpystrn(unix_addr.sun_path, sockname, sizeof unix_addr.sun_path);
587
omask = umask(0077); /* so that only Apache can use socket */
588
rc = bind(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr));
589
umask(omask); /* can't fail, so can't clobber errno */
591
ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
592
"Couldn't bind unix domain socket %s",
597
if (listen(sd, DEFAULT_CGID_LISTENBACKLOG) < 0) {
598
ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
599
"Couldn't listen on unix domain socket");
604
if (chown(sockname, unixd_config.user_id, -1) < 0) {
605
ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
606
"Couldn't change owner of unix domain socket %s",
612
unixd_setup_child(); /* if running as root, switch to configured user/group */
614
while (!daemon_should_exit) {
615
int errfileno = STDERR_FILENO;
618
const char * const *argv;
620
apr_int32_t out_pipe;
621
apr_int32_t err_pipe;
622
apr_cmdtype_e cmd_type;
624
apr_procattr_t *procattr = NULL;
625
apr_proc_t *procnew = NULL;
630
apr_pool_clear(ptrans);
632
len = sizeof(unix_addr);
633
sd2 = accept(sd, (struct sockaddr *)&unix_addr, &len);
635
#if defined(ENETDOWN)
636
if (errno == ENETDOWN) {
637
/* The network has been shut down, no need to continue. Die gracefully */
638
++daemon_should_exit;
641
if (errno != EINTR) {
642
ap_log_error(APLOG_MARK, APLOG_ERR, errno,
644
"Error accepting on cgid socket");
649
r = apr_pcalloc(ptrans, sizeof(request_rec));
650
procnew = apr_pcalloc(ptrans, sizeof(*procnew));
652
stat = get_req(sd2, r, &argv0, &env, &cgid_req);
653
if (stat != APR_SUCCESS) {
654
ap_log_error(APLOG_MARK, APLOG_ERR, stat,
656
"Error reading request on cgid socket");
661
if (cgid_req.ppid != parent_pid) {
662
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, main_server,
663
"CGI request received from wrong server instance; "
664
"see ScriptSock directive");
669
if (cgid_req.req_type == GETPID_REQ) {
672
pid = (pid_t)((long)apr_hash_get(script_hash, &cgid_req.conn_id, sizeof(cgid_req.conn_id)));
673
if (write(sd2, &pid, sizeof(pid)) != sizeof(pid)) {
674
ap_log_error(APLOG_MARK, APLOG_ERR, 0,
676
"Error writing pid %" APR_PID_T_FMT " to handler", pid);
682
apr_os_file_put(&r->server->error_log, &errfileno, 0, r->pool);
683
apr_os_file_put(&inout, &sd2, 0, r->pool);
685
if (cgid_req.req_type == SSI_REQ) {
686
in_pipe = APR_NO_PIPE;
687
out_pipe = APR_FULL_BLOCK;
688
err_pipe = APR_NO_PIPE;
689
cmd_type = APR_SHELLCMD;
692
in_pipe = APR_CHILD_BLOCK;
693
out_pipe = APR_CHILD_BLOCK;
694
err_pipe = APR_CHILD_BLOCK;
695
cmd_type = APR_PROGRAM;
698
if (((rc = apr_procattr_create(&procattr, ptrans)) != APR_SUCCESS) ||
699
((cgid_req.req_type == CGI_REQ) &&
700
(((rc = apr_procattr_io_set(procattr,
703
err_pipe)) != APR_SUCCESS) ||
704
/* XXX apr_procattr_child_*_set() is creating an unnecessary
705
* pipe between this process and the child being created...
706
* It is cleaned up with the temporary pool for this request.
708
((rc = apr_procattr_child_err_set(procattr, r->server->error_log, NULL)) != APR_SUCCESS) ||
709
((rc = apr_procattr_child_in_set(procattr, inout, NULL)) != APR_SUCCESS))) ||
710
((rc = apr_procattr_child_out_set(procattr, inout, NULL)) != APR_SUCCESS) ||
711
((rc = apr_procattr_dir_set(procattr,
712
ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) ||
713
((rc = apr_procattr_cmdtype_set(procattr, cmd_type)) != APR_SUCCESS) ||
714
((rc = apr_procattr_child_errfn_set(procattr, cgid_child_errfn)) != APR_SUCCESS)) {
715
/* Something bad happened, tell the world.
716
* ap_log_rerror() won't work because the header table used by
717
* ap_log_rerror() hasn't been replicated in the phony r
719
ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
720
"couldn't set child process attributes: %s", r->filename);
723
apr_pool_userdata_set(r, ERRFN_USERDATA_KEY, apr_pool_cleanup_null, ptrans);
725
argv = (const char * const *)create_argv(r->pool, NULL, NULL, NULL, argv0, r->args);
727
/* We want to close sd2 for the new CGI process too.
728
* If it is left open it'll make ap_pass_brigade() block
729
* waiting for EOF if CGI forked something running long.
730
* close(sd2) here should be okay, as CGI channel
731
* is already dup()ed by apr_procattr_child_{in,out}_set()
736
if (memcmp(&empty_ugid, &cgid_req.ugid, sizeof(empty_ugid))) {
737
/* We have a valid identity, and can be sure that
738
* cgid_suexec_id_doer will return a valid ugid
740
rc = ap_os_create_privileged_process(r, procnew, argv0, argv,
741
(const char * const *)env,
744
rc = apr_proc_create(procnew, argv0, argv,
745
(const char * const *)env,
749
if (rc != APR_SUCCESS) {
750
/* Bad things happened. Everyone should have cleaned up.
751
* ap_log_rerror() won't work because the header table used by
752
* ap_log_rerror() hasn't been replicated in the phony r
754
ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
755
"couldn't create child process: %d: %s", rc,
756
apr_filepath_name_get(r->filename));
759
/* We don't want to leak storage for the key, so only allocate
760
* a key if the key doesn't exist yet in the hash; there are
761
* only a limited number of possible keys (one for each
762
* possible thread in the server), so we can allocate a copy
763
* of the key the first time a thread has a cgid request.
764
* Note that apr_hash_set() only uses the storage passed in
765
* for the key if it is adding the key to the hash for the
766
* first time; new key storage isn't needed for replacing the
767
* existing value of a key.
771
if (apr_hash_get(script_hash, &cgid_req.conn_id, sizeof(cgid_req.conn_id))) {
772
key = &cgid_req.conn_id;
775
key = apr_pcalloc(pcgi, sizeof(cgid_req.conn_id));
776
memcpy(key, &cgid_req.conn_id, sizeof(cgid_req.conn_id));
778
apr_hash_set(script_hash, key, sizeof(cgid_req.conn_id),
779
(void *)((long)procnew->pid));
786
static int cgid_start(apr_pool_t *p, server_rec *main_server,
790
daemon_should_exit = 0; /* clear setting from previous generation */
791
if ((daemon_pid = fork()) < 0) {
792
ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
793
"mod_cgid: Couldn't spawn cgid daemon process");
796
else if (daemon_pid == 0) {
798
apr_pool_create(&pcgi, p);
800
cgid_server(main_server);
803
procnew->pid = daemon_pid;
804
procnew->err = procnew->in = procnew->out = NULL;
805
apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
806
#if APR_HAS_OTHER_CHILD
807
apr_proc_other_child_register(procnew, cgid_maint, procnew, NULL, p);
812
static int cgid_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
815
sockname = ap_append_pid(pconf, DEFAULT_SOCKET, ".");
819
static int cgid_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp,
820
server_rec *main_server)
822
apr_proc_t *procnew = NULL;
824
const char *userdata_key = "cgid_init";
829
root_server = main_server;
832
apr_pool_userdata_get(&data, userdata_key, main_server->process->pool);
835
procnew = apr_pcalloc(main_server->process->pool, sizeof(*procnew));
837
procnew->err = procnew->in = procnew->out = NULL;
838
apr_pool_userdata_set((const void *)procnew, userdata_key,
839
apr_pool_cleanup_null, main_server->process->pool);
847
for (m = ap_preloaded_modules; *m != NULL; m++)
850
parent_pid = getpid();
851
sockname = ap_server_root_relative(p, sockname);
852
ret = cgid_start(p, main_server, procnew);
856
cgid_pfn_reg_with_ssi = APR_RETRIEVE_OPTIONAL_FN(ap_register_include_handler);
857
cgid_pfn_gtv = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_get_tag_and_value);
858
cgid_pfn_ps = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_parse_string);
860
if ((cgid_pfn_reg_with_ssi) && (cgid_pfn_gtv) && (cgid_pfn_ps)) {
861
/* Required by mod_include filter. This is how mod_cgid registers
862
* with mod_include to provide processing of the exec directive.
864
cgid_pfn_reg_with_ssi("exec", handle_exec);
870
static void *create_cgid_config(apr_pool_t *p, server_rec *s)
872
cgid_server_conf *c =
873
(cgid_server_conf *) apr_pcalloc(p, sizeof(cgid_server_conf));
876
c->logbytes = DEFAULT_LOGBYTES;
877
c->bufbytes = DEFAULT_BUFBYTES;
881
static void *merge_cgid_config(apr_pool_t *p, void *basev, void *overridesv)
883
cgid_server_conf *base = (cgid_server_conf *) basev, *overrides = (cgid_server_conf *) overridesv;
885
return overrides->logname ? overrides : base;
888
static const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
890
server_rec *s = cmd->server;
891
cgid_server_conf *conf = ap_get_module_config(s->module_config,
894
conf->logname = ap_server_root_relative(cmd->pool, arg);
896
if (!conf->logname) {
897
return apr_pstrcat(cmd->pool, "Invalid ScriptLog path ",
903
static const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, const char *arg)
905
server_rec *s = cmd->server;
906
cgid_server_conf *conf = ap_get_module_config(s->module_config,
909
conf->logbytes = atol(arg);
913
static const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, const char *arg)
915
server_rec *s = cmd->server;
916
cgid_server_conf *conf = ap_get_module_config(s->module_config,
919
conf->bufbytes = atoi(arg);
923
static const char *set_script_socket(cmd_parms *cmd, void *dummy, const char *arg)
925
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
930
/* Make sure the pid is appended to the sockname */
931
sockname = ap_append_pid(cmd->pool, arg, ".");
932
sockname = ap_server_root_relative(cmd->pool, sockname);
935
return apr_pstrcat(cmd->pool, "Invalid ScriptSock path",
942
static const command_rec cgid_cmds[] =
944
AP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
945
"the name of a log for script debugging info"),
946
AP_INIT_TAKE1("ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF,
947
"the maximum length (in bytes) of the script debug log"),
948
AP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
949
"the maximum size (in bytes) to record of a POST request"),
950
AP_INIT_TAKE1("ScriptSock", set_script_socket, NULL, RSRC_CONF,
951
"the name of the socket to use for communication with "
956
static int log_scripterror(request_rec *r, cgid_server_conf * conf, int ret,
957
apr_status_t rv, char *error)
959
apr_file_t *f = NULL;
961
char time_str[APR_CTIME_LEN];
962
int log_flags = rv ? APLOG_ERR : APLOG_ERR;
964
ap_log_rerror(APLOG_MARK, log_flags, rv, r,
965
"%s: %s", error, r->filename);
967
/* XXX Very expensive mainline case! Open, then getfileinfo! */
968
if (!conf->logname ||
969
((stat(conf->logname, &finfo) == 0)
970
&& (finfo.st_size > conf->logbytes)) ||
971
(apr_file_open(&f, conf->logname,
972
APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
976
/* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
977
apr_ctime(time_str, apr_time_now());
978
apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
979
r->args ? "?" : "", r->args ? r->args : "", r->protocol);
980
/* "%% 500 /usr/local/apache/cgid-bin */
981
apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);
983
apr_file_printf(f, "%%error\n%s\n", error);
989
static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
990
char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
991
apr_file_t *script_err)
993
const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
994
const apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
995
char argsbuffer[HUGE_STRING_LEN];
996
apr_file_t *f = NULL;
1004
char time_str[APR_CTIME_LEN];
1006
/* XXX Very expensive mainline case! Open, then getfileinfo! */
1007
if (!conf->logname ||
1008
((stat(conf->logname, &finfo) == 0)
1009
&& (finfo.st_size > conf->logbytes)) ||
1010
(apr_file_open(&f, conf->logname,
1011
APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
1012
/* Soak up script output */
1013
discard_script_output(bb);
1015
while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
1016
script_err) == APR_SUCCESS)
1022
/* "%% [Wed Jun 19 10:53:21 1996] GET /cgid-bin/printenv HTTP/1.0" */
1023
apr_ctime(time_str, apr_time_now());
1024
apr_file_printf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
1025
r->args ? "?" : "", r->args ? r->args : "", r->protocol);
1026
/* "%% 500 /usr/local/apache/cgid-bin" */
1027
apr_file_printf(f, "%%%% %d %s\n", ret, r->filename);
1029
apr_file_puts("%request\n", f);
1030
for (i = 0; i < hdrs_arr->nelts; ++i) {
1033
apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
1035
if ((r->method_number == M_POST || r->method_number == M_PUT)
1037
apr_file_printf(f, "\n%s\n", dbuf);
1040
apr_file_puts("%response\n", f);
1041
hdrs_arr = apr_table_elts(r->err_headers_out);
1042
hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
1044
for (i = 0; i < hdrs_arr->nelts; ++i) {
1047
apr_file_printf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
1051
apr_file_printf(f, "%s\n", sbuf);
1055
for (e = APR_BRIGADE_FIRST(bb);
1056
e != APR_BRIGADE_SENTINEL(bb);
1057
e = APR_BUCKET_NEXT(e))
1059
if (APR_BUCKET_IS_EOS(e)) {
1062
rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
1063
if (rv != APR_SUCCESS || (len == 0)) {
1067
apr_file_puts("%stdout\n", f);
1070
apr_file_write(f, buf, &len);
1071
apr_file_puts("\n", f);
1075
if (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
1076
script_err) == APR_SUCCESS) {
1077
apr_file_puts("%stderr\n", f);
1078
apr_file_puts(argsbuffer, f);
1079
while (apr_file_gets(argsbuffer, HUGE_STRING_LEN,
1080
script_err) == APR_SUCCESS)
1081
apr_file_puts(argsbuffer, f);
1082
apr_file_puts("\n", f);
1087
apr_file_close(script_err);
1094
static apr_status_t close_unix_socket(void *thefd)
1096
int fd = (int)((long)thefd);
1101
static int connect_to_daemon(int *sdptr, request_rec *r,
1102
cgid_server_conf *conf)
1104
struct sockaddr_un unix_addr;
1107
apr_interval_time_t sliding_timer;
1109
memset(&unix_addr, 0, sizeof(unix_addr));
1110
unix_addr.sun_family = AF_UNIX;
1111
apr_cpystrn(unix_addr.sun_path, sockname, sizeof unix_addr.sun_path);
1114
sliding_timer = 100000; /* 100 milliseconds */
1117
if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
1118
return log_scripterror(r, conf, HTTP_INTERNAL_SERVER_ERROR, errno,
1119
"unable to create socket to cgi daemon");
1121
if (connect(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) < 0) {
1122
if (errno == ECONNREFUSED && connect_tries < DEFAULT_CONNECT_ATTEMPTS) {
1123
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, errno, r,
1124
"connect #%d to cgi daemon failed, sleeping before retry",
1127
apr_sleep(sliding_timer);
1128
if (sliding_timer < apr_time_from_sec(2)) {
1134
return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, errno,
1135
"unable to connect to cgi daemon after multiple tries");
1139
apr_pool_cleanup_register(r->pool, (void *)((long)sd),
1140
close_unix_socket, apr_pool_cleanup_null);
1141
break; /* we got connected! */
1143
/* gotta try again, but make sure the cgid daemon is still around */
1144
if (kill(daemon_pid, 0) != 0) {
1145
return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, errno,
1146
"cgid daemon is gone; is Apache terminating?");
1153
static void discard_script_output(apr_bucket_brigade *bb)
1160
for (e = APR_BRIGADE_FIRST(bb);
1161
e != APR_BRIGADE_SENTINEL(bb);
1162
e = APR_BUCKET_NEXT(e))
1164
if (APR_BUCKET_IS_EOS(e)) {
1167
rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
1168
if (rv != APR_SUCCESS) {
1174
/****************************************************************
1176
* Actual cgid handling...
1179
struct cleanup_script_info {
1181
unsigned long conn_id;
1182
cgid_server_conf *conf;
1185
static apr_status_t dead_yet(pid_t pid, apr_interval_time_t max_wait)
1187
apr_interval_time_t interval = 10000; /* 10 ms */
1188
apr_interval_time_t total = 0;
1192
/* On AIX, for processes like mod_cgid's script children where
1193
* SIGCHLD is ignored, kill(pid,0) returns success for up to
1194
* one second after the script child exits, based on when a
1195
* daemon runs to clean up unnecessary process table entries.
1196
* getpgid() can report the proper info (-1/ESRCH) immediately.
1198
if (getpgid(pid) < 0) {
1200
if (kill(pid, 0) < 0) {
1204
apr_sleep(interval);
1205
total = total + interval;
1206
if (interval < 500000) {
1209
} while (total < max_wait);
1210
return APR_EGENERAL;
1213
static apr_status_t cleanup_nonchild_process(request_rec *r, pid_t pid)
1215
kill(pid, SIGTERM); /* in case it isn't dead yet */
1216
if (dead_yet(pid, apr_time_from_sec(3)) == APR_SUCCESS) {
1219
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1220
"CGI process %" APR_PID_T_FMT " didn't exit, sending SIGKILL",
1223
if (dead_yet(pid, apr_time_from_sec(3)) == APR_SUCCESS) {
1226
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1227
"CGI process %" APR_PID_T_FMT " didn't exit, sending SIGKILL again",
1231
return APR_EGENERAL;
1234
static apr_status_t cleanup_script(void *vptr)
1236
struct cleanup_script_info *info = vptr;
1239
cgid_req_t req = {0};
1243
rc = connect_to_daemon(&sd, info->r, info->conf);
1245
return APR_EGENERAL;
1248
/* we got a socket, and there is already a cleanup registered for it */
1250
req.req_type = GETPID_REQ;
1251
req.ppid = parent_pid;
1252
req.conn_id = info->r->connection->id;
1254
stat = sock_write(sd, &req, sizeof(req));
1255
if (stat != APR_SUCCESS) {
1259
/* wait for pid of script */
1260
stat = sock_read(sd, &pid, sizeof(pid));
1261
if (stat != APR_SUCCESS) {
1266
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, info->r,
1267
"daemon couldn't find CGI process for connection %lu",
1269
return APR_EGENERAL;
1271
return cleanup_nonchild_process(info->r, pid);
1274
static int cgid_handler(request_rec *r)
1276
conn_rec *c = r->connection;
1277
int retval, nph, dbpos = 0;
1278
char *argv0, *dbuf = NULL;
1279
apr_bucket_brigade *bb;
1281
cgid_server_conf *conf;
1283
int seen_eos, child_stopped_reading;
1286
apr_file_t *tempsock;
1287
struct cleanup_script_info *info;
1290
if (strcmp(r->handler,CGI_MAGIC_TYPE) && strcmp(r->handler,"cgi-script"))
1293
conf = ap_get_module_config(r->server->module_config, &cgid_module);
1294
is_included = !strcmp(r->protocol, "INCLUDED");
1296
if ((argv0 = strrchr(r->filename, '/')) != NULL)
1299
argv0 = r->filename;
1301
nph = !(strncmp(argv0, "nph-", 4));
1303
argv0 = r->filename;
1305
if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
1306
return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
1307
"Options ExecCGI is off in this directory");
1308
if (nph && is_included)
1309
return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
1310
"attempt to include NPH CGI script");
1312
#if defined(OS2) || defined(WIN32)
1313
#error mod_cgid does not work on this platform. If you teach it to, look
1314
#error at mod_cgi.c for required code in this path.
1316
if (r->finfo.filetype == 0)
1317
return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
1318
"script not found or unable to stat");
1320
if (r->finfo.filetype == APR_DIR)
1321
return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
1322
"attempt to invoke directory as script");
1324
if ((r->used_path_info == AP_REQ_REJECT_PATH_INFO) &&
1325
r->path_info && *r->path_info)
1327
/* default to accept */
1328
return log_scripterror(r, conf, HTTP_NOT_FOUND, 0,
1329
"AcceptPathInfo off disallows user's path");
1332
if (!ap_suexec_enabled) {
1333
if (!ap_can_exec(&r->finfo))
1334
return log_scripterror(r, conf, HTTP_FORBIDDEN, 0,
1335
"file permissions deny server execution");
1338
ap_add_common_vars(r);
1340
env = ap_create_environment(r->pool, r->subprocess_env);
1342
if ((retval = connect_to_daemon(&sd, r, conf)) != OK) {
1346
rv = send_req(sd, r, argv0, env, CGI_REQ);
1347
if (rv != APR_SUCCESS) {
1348
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1349
"write to cgi daemon process");
1352
info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
1354
info->conn_id = r->connection->id;
1356
apr_pool_cleanup_register(r->pool, info,
1358
apr_pool_cleanup_null);
1359
/* We are putting the socket discriptor into an apr_file_t so that we can
1360
* use a pipe bucket to send the data to the client. APR will create
1361
* a cleanup for the apr_file_t which will close the socket, so we'll
1362
* get rid of the cleanup we registered when we created the socket.
1365
apr_os_pipe_put_ex(&tempsock, &sd, 1, r->pool);
1366
apr_pool_cleanup_kill(r->pool, (void *)((long)sd), close_unix_socket);
1368
if ((argv0 = strrchr(r->filename, '/')) != NULL)
1371
argv0 = r->filename;
1373
/* Transfer any put/post args, CERN style...
1374
* Note that we already ignore SIGPIPE in the core server.
1376
bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1378
child_stopped_reading = 0;
1379
if (conf->logname) {
1380
dbuf = apr_palloc(r->pool, conf->bufbytes + 1);
1386
rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
1387
APR_BLOCK_READ, HUGE_STRING_LEN);
1389
if (rv != APR_SUCCESS) {
1393
for (bucket = APR_BRIGADE_FIRST(bb);
1394
bucket != APR_BRIGADE_SENTINEL(bb);
1395
bucket = APR_BUCKET_NEXT(bucket))
1400
if (APR_BUCKET_IS_EOS(bucket)) {
1405
/* We can't do much with this. */
1406
if (APR_BUCKET_IS_FLUSH(bucket)) {
1410
/* If the child stopped, we still must read to EOS. */
1411
if (child_stopped_reading) {
1416
apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
1418
if (conf->logname && dbpos < conf->bufbytes) {
1421
if ((dbpos + len) > conf->bufbytes) {
1422
cursize = conf->bufbytes - dbpos;
1427
memcpy(dbuf + dbpos, data, cursize);
1431
/* Keep writing data to the child until done or too much time
1432
* elapses with no progress or an error occurs.
1434
rv = apr_file_write_full(tempsock, data, len, NULL);
1436
if (rv != APR_SUCCESS) {
1437
/* silly script stopped reading, soak up remaining message */
1438
child_stopped_reading = 1;
1441
apr_brigade_cleanup(bb);
1445
if (conf->logname) {
1449
/* we're done writing, or maybe we didn't write at all;
1450
* force EOF on child's stdin so that the cgi detects end (or
1455
/* Handle script return... */
1457
const char *location;
1458
char sbuf[MAX_STRING_LEN];
1461
bb = apr_brigade_create(r->pool, c->bucket_alloc);
1462
b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
1463
APR_BRIGADE_INSERT_TAIL(bb, b);
1464
b = apr_bucket_eos_create(c->bucket_alloc);
1465
APR_BRIGADE_INSERT_TAIL(bb, b);
1467
if ((ret = ap_scan_script_header_err_brigade(r, bb, sbuf))) {
1468
return log_script(r, conf, ret, dbuf, sbuf, bb, NULL);
1471
location = apr_table_get(r->headers_out, "Location");
1473
if (location && location[0] == '/' && r->status == 200) {
1475
/* Soak up all the script output */
1476
discard_script_output(bb);
1477
apr_brigade_destroy(bb);
1478
/* This redirect needs to be a GET no matter what the original
1481
r->method = apr_pstrdup(r->pool, "GET");
1482
r->method_number = M_GET;
1484
/* We already read the message body (if any), so don't allow
1485
* the redirected request to think it has one. We can ignore
1486
* Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
1488
apr_table_unset(r->headers_in, "Content-Length");
1490
ap_internal_redirect_handler(location, r);
1493
else if (location && r->status == 200) {
1494
/* XX Note that if a script wants to produce its own Redirect
1495
* body, it now has to explicitly *say* "Status: 302"
1497
discard_script_output(bb);
1498
apr_brigade_destroy(bb);
1499
return HTTP_MOVED_TEMPORARILY;
1502
ap_pass_brigade(r->output_filters, bb);
1506
struct ap_filter_t *cur;
1508
/* get rid of all filters up through protocol... since we
1509
* haven't parsed off the headers, there is no way they can
1513
cur = r->proto_output_filters;
1514
while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
1517
r->output_filters = r->proto_output_filters = cur;
1519
bb = apr_brigade_create(r->pool, c->bucket_alloc);
1520
b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
1521
APR_BRIGADE_INSERT_TAIL(bb, b);
1522
b = apr_bucket_eos_create(c->bucket_alloc);
1523
APR_BRIGADE_INSERT_TAIL(bb, b);
1524
ap_pass_brigade(r->output_filters, bb);
1527
return OK; /* NOT r->status, even if it has changed. */
1533
/*============================================================================
1534
*============================================================================
1535
* This is the beginning of the cgi filter code moved from mod_include. This
1536
* is the code required to handle the "exec" SSI directive.
1537
*============================================================================
1538
*============================================================================*/
1539
static apr_status_t include_cgi(include_ctx_t *ctx, ap_filter_t *f,
1540
apr_bucket_brigade *bb, char *s)
1542
request_rec *r = f->r;
1543
request_rec *rr = ap_sub_req_lookup_uri(s, r, f->next);
1546
if (rr->status != HTTP_OK) {
1547
ap_destroy_sub_req(rr);
1548
return APR_EGENERAL;
1551
/* No hardwired path info or query allowed */
1552
if ((rr->path_info && rr->path_info[0]) || rr->args) {
1553
ap_destroy_sub_req(rr);
1554
return APR_EGENERAL;
1556
if (rr->finfo.filetype != APR_REG) {
1557
ap_destroy_sub_req(rr);
1558
return APR_EGENERAL;
1561
/* Script gets parameters of the *document*, for back compatibility */
1562
rr->path_info = r->path_info; /* hard to get right; see mod_cgi.c */
1565
/* Force sub_req to be treated as a CGI request, even if ordinary
1566
* typing rules would have called it something else.
1568
ap_set_content_type(rr, CGI_MAGIC_TYPE);
1571
rr_status = ap_run_sub_req(rr);
1572
if (ap_is_HTTP_REDIRECT(rr_status)) {
1573
const char *location = apr_table_get(rr->headers_out, "Location");
1578
location = ap_escape_html(rr->pool, location);
1579
buffer = apr_pstrcat(ctx->pool, "<a href=\"", location, "\">",
1580
location, "</a>", NULL);
1582
APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pool_create(buffer,
1583
strlen(buffer), ctx->pool,
1584
f->c->bucket_alloc));
1588
ap_destroy_sub_req(rr);
1593
/* This is the special environment used for running the "exec cmd="
1594
* variety of SSI directives.
1596
static void add_ssi_vars(request_rec *r)
1598
apr_table_t *e = r->subprocess_env;
1600
if (r->path_info && r->path_info[0] != '\0') {
1601
request_rec *pa_req;
1603
apr_table_setn(e, "PATH_INFO", ap_escape_shell_cmd(r->pool, r->path_info));
1605
pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r, NULL);
1606
if (pa_req->filename) {
1607
apr_table_setn(e, "PATH_TRANSLATED",
1608
apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info, NULL));
1610
ap_destroy_sub_req(pa_req);
1614
char *arg_copy = apr_pstrdup(r->pool, r->args);
1616
apr_table_setn(e, "QUERY_STRING", r->args);
1617
ap_unescape_url(arg_copy);
1618
apr_table_setn(e, "QUERY_STRING_UNESCAPED", ap_escape_shell_cmd(r->pool, arg_copy));
1622
static int include_cmd(include_ctx_t *ctx, ap_filter_t *f,
1623
apr_bucket_brigade *bb, char *command)
1628
apr_file_t *tempsock = NULL;
1629
request_rec *r = f->r;
1630
cgid_server_conf *conf = ap_get_module_config(r->server->module_config,
1632
struct cleanup_script_info *info;
1635
env = ap_create_environment(r->pool, r->subprocess_env);
1637
if ((retval = connect_to_daemon(&sd, r, conf)) != OK) {
1641
send_req(sd, r, command, env, SSI_REQ);
1643
info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
1645
info->conn_id = r->connection->id;
1647
/* for this type of request, the script is invoked through an
1648
* intermediate shell process... cleanup_script is only able
1649
* to knock out the shell process, not the actual script
1651
apr_pool_cleanup_register(r->pool, info,
1653
apr_pool_cleanup_null);
1655
/* We are putting the socket discriptor into an apr_file_t so that we can
1656
* use a pipe bucket to send the data to the client. APR will create
1657
* a cleanup for the apr_file_t which will close the socket, so we'll
1658
* get rid of the cleanup we registered when we created the socket.
1660
apr_os_pipe_put_ex(&tempsock, &sd, 1, r->pool);
1661
apr_pool_cleanup_kill(r->pool, (void *)((long)sd), close_unix_socket);
1663
APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pipe_create(tempsock,
1664
f->c->bucket_alloc));
1670
static apr_status_t handle_exec(include_ctx_t *ctx, ap_filter_t *f,
1671
apr_bucket_brigade *bb)
1674
char *tag_val = NULL;
1675
request_rec *r = f->r;
1676
char *file = r->filename;
1677
char parsed_string[MAX_STRING_LEN];
1680
ap_log_rerror(APLOG_MARK,
1681
(ctx->flags & SSI_FLAG_PRINTING)
1682
? APLOG_ERR : APLOG_WARNING,
1683
0, r, "missing argument for exec element in %s",
1687
if (!(ctx->flags & SSI_FLAG_PRINTING)) {
1692
SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1696
if (ctx->flags & SSI_FLAG_NO_EXEC) {
1697
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "exec used but not allowed "
1698
"in %s", r->filename);
1699
SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1704
cgid_pfn_gtv(ctx, &tag, &tag_val, SSI_VALUE_DECODED);
1705
if (!tag || !tag_val) {
1709
if (!strcmp(tag, "cmd")) {
1712
cgid_pfn_ps(ctx, tag_val, parsed_string, sizeof(parsed_string),
1713
SSI_EXPAND_LEAVE_NAME);
1715
rv = include_cmd(ctx, f, bb, parsed_string);
1716
if (rv != APR_SUCCESS) {
1717
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1718
"execution failure for parameter \"%s\" "
1719
"to tag exec in file %s", tag, r->filename);
1720
SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1724
else if (!strcmp(tag, "cgi")) {
1727
cgid_pfn_ps(ctx, tag_val, parsed_string, sizeof(parsed_string),
1728
SSI_EXPAND_DROP_NAME);
1730
rv = include_cgi(ctx, f, bb, parsed_string);
1731
if (rv != APR_SUCCESS) {
1732
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "invalid CGI ref "
1733
"\"%s\" in %s", tag_val, file);
1734
SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1739
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown parameter "
1740
"\"%s\" to tag exec in %s", tag, file);
1741
SSI_CREATE_ERROR_BUCKET(ctx, f, bb);
1748
/*============================================================================
1749
*============================================================================
1750
* This is the end of the cgi filter code moved from mod_include.
1751
*============================================================================
1752
*============================================================================*/
1755
static void register_hook(apr_pool_t *p)
1757
static const char * const aszPre[] = { "mod_include.c", NULL };
1759
ap_hook_pre_config(cgid_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1760
ap_hook_post_config(cgid_init, aszPre, NULL, APR_HOOK_MIDDLE);
1761
ap_hook_handler(cgid_handler, NULL, NULL, APR_HOOK_MIDDLE);
1764
module AP_MODULE_DECLARE_DATA cgid_module = {
1765
STANDARD20_MODULE_STUFF,
1766
NULL, /* dir config creater */
1767
NULL, /* dir merger --- default is to override */
1768
create_cgid_config, /* server config */
1769
merge_cgid_config, /* merge server config */
1770
cgid_cmds, /* command table */
1771
register_hook /* register_handlers */