1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_buckets.h"
#include "apr_lib.h"
#include "apr_poll.h"
#define APR_WANT_STRFUNC
#define APR_WANT_MEMFUNC
#include "apr_want.h"
#include "httpd.h"
#include "util_filter.h"
/* A CGI bucket type is needed to catch any output to stderr from the
* script; see PR 22030. */
static const apr_bucket_type_t bucket_type_cgi;
struct cgi_bucket_data {
apr_pollset_t *pollset;
request_rec *r;
apr_interval_time_t timeout;
};
/* Create a CGI bucket using pipes from script stdout 'out'
* and stderr 'err', for request 'r'. */
static apr_bucket *cgi_bucket_create(request_rec *r,
apr_interval_time_t timeout,
apr_file_t *out, apr_file_t *err,
apr_bucket_alloc_t *list)
{
apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
apr_status_t rv;
apr_pollfd_t fd;
struct cgi_bucket_data *data = apr_palloc(r->pool, sizeof *data);
APR_BUCKET_INIT(b);
b->free = apr_bucket_free;
b->list = list;
b->type = &bucket_type_cgi;
b->length = (apr_size_t)(-1);
b->start = -1;
/* Create the pollset */
rv = apr_pollset_create(&data->pollset, 2, r->pool, 0);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01217)
"apr_pollset_create(); check system or user limits");
return NULL;
}
fd.desc_type = APR_POLL_FILE;
fd.reqevents = APR_POLLIN;
fd.p = r->pool;
fd.desc.f = out; /* script's stdout */
fd.client_data = (void *)1;
rv = apr_pollset_add(data->pollset, &fd);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01218)
"apr_pollset_add(); check system or user limits");
return NULL;
}
fd.desc.f = err; /* script's stderr */
fd.client_data = (void *)2;
rv = apr_pollset_add(data->pollset, &fd);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01219)
"apr_pollset_add(); check system or user limits");
return NULL;
}
data->r = r;
data->timeout = timeout;
b->data = data;
return b;
}
/* Create a duplicate CGI bucket using given bucket data */
static apr_bucket *cgi_bucket_dup(struct cgi_bucket_data *data,
apr_bucket_alloc_t *list)
{
apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
APR_BUCKET_INIT(b);
b->free = apr_bucket_free;
b->list = list;
b->type = &bucket_type_cgi;
b->length = (apr_size_t)(-1);
b->start = -1;
b->data = data;
return b;
}
/* Handle stdout from CGI child. Duplicate of logic from the _read
* method of the real APR pipe bucket implementation. */
static apr_status_t cgi_read_stdout(apr_bucket *a, apr_file_t *out,
const char **str, apr_size_t *len)
{
char *buf;
apr_status_t rv;
*str = NULL;
*len = APR_BUCKET_BUFF_SIZE;
buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
rv = apr_file_read(out, buf, len);
if (rv != APR_SUCCESS && rv != APR_EOF) {
apr_bucket_free(buf);
return rv;
}
if (*len > 0) {
struct cgi_bucket_data *data = a->data;
apr_bucket_heap *h;
/* Change the current bucket to refer to what we read */
a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
h = a->data;
h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
*str = buf;
APR_BUCKET_INSERT_AFTER(a, cgi_bucket_dup(data, a->list));
}
else {
apr_bucket_free(buf);
a = apr_bucket_immortal_make(a, "", 0);
*str = a->data;
}
return rv;
}
/* Read method of CGI bucket: polls on stderr and stdout of the child,
* sending any stderr output immediately away to the error log. */
static apr_status_t cgi_bucket_read(apr_bucket *b, const char **str,
apr_size_t *len, apr_read_type_e block)
{
struct cgi_bucket_data *data = b->data;
apr_interval_time_t timeout = 0;
apr_status_t rv;
int gotdata = 0;
if (block != APR_NONBLOCK_READ) {
timeout = data->timeout > 0 ? data->timeout : data->r->server->timeout;
}
do {
const apr_pollfd_t *results;
apr_int32_t num;
rv = apr_pollset_poll(data->pollset, timeout, &num, &results);
if (APR_STATUS_IS_TIMEUP(rv)) {
if (timeout) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, data->r, APLOGNO(01220)
"Timeout waiting for output from CGI script %s",
data->r->filename);
return rv;
}
else {
return APR_EAGAIN;
}
}
else if (APR_STATUS_IS_EINTR(rv)) {
continue;
}
else if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, data->r, APLOGNO(01221)
"poll failed waiting for CGI child");
return rv;
}
for (; num; num--, results++) {
if (results[0].client_data == (void *)1) {
/* stdout */
rv = cgi_read_stdout(b, results[0].desc.f, str, len);
if (APR_STATUS_IS_EOF(rv)) {
rv = APR_SUCCESS;
}
gotdata = 1;
} else {
/* stderr */
apr_status_t rv2 = log_script_err(data->r, results[0].desc.f);
if (APR_STATUS_IS_EOF(rv2)) {
apr_pollset_remove(data->pollset, &results[0]);
}
}
}
} while (!gotdata);
return rv;
}
static const apr_bucket_type_t bucket_type_cgi = {
"CGI", 5, APR_BUCKET_DATA,
apr_bucket_destroy_noop,
cgi_bucket_read,
apr_bucket_setaside_notimpl,
apr_bucket_split_notimpl,
apr_bucket_copy_notimpl
};
|