-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmodperl_callback.c
More file actions
397 lines (344 loc) · 13 KB
/
Copy pathmodperl_callback.c
File metadata and controls
397 lines (344 loc) · 13 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* 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 "mod_perl.h"
int modperl_callback(pTHX_ modperl_handler_t *handler, apr_pool_t *p,
request_rec *r, server_rec *s, AV *args)
{
CV *cv = (CV *)NULL;
I32 flags = G_EVAL|G_SCALAR;
dSP;
int count, status = OK;
PERL_SET_CONTEXT(aTHX);
/* handler callbacks shouldn't affect each other's taintedness
* state, so start every callback with a clear tainted status
* before and after the callback one of the main problems we are
* trying to solve is that when modperl_croak called (which calls
* perl's croak((char *)NULL) to throw an error object) it leaves
* the interpreter in the tainted state which later affects other
* callbacks that call eval, etc., which triggers perl crash with:
* Insecure dependency in eval while running setgid. Callback
* called exit.
*/
TAINT_NOT;
if ((status = modperl_handler_resolve(aTHX_ &handler, p, s)) != OK) {
TAINT_NOT;
return status;
}
ENTER;SAVETMPS;
PUSHMARK(SP);
if (MpHandlerMETHOD(handler)) {
GV *gv;
if (!handler->mgv_obj) {
Perl_croak(aTHX_ "panic: %s method handler object is NULL!",
modperl_handler_name(handler));
}
gv = modperl_mgv_lookup(aTHX_ handler->mgv_obj);
XPUSHs(modperl_mgv_sv(gv));
}
if (args) {
I32 items = AvFILLp(args) + 1;
EXTEND(SP, items);
Copy(AvARRAY(args), SP + 1, items, SV*);
SP += items;
}
PUTBACK;
if (MpHandlerANON(handler)) {
#ifdef USE_ITHREADS
cv = modperl_handler_anon_get(aTHX_ handler->mgv_obj);
#else
cv = handler->cv;
#endif
}
else {
GV *gv = modperl_mgv_lookup_autoload(aTHX_ handler->mgv_cv, s, p);
if (gv) {
cv = modperl_mgv_cv(gv);
}
else {
const char *name;
modperl_mgv_t *symbol = handler->mgv_cv;
/* XXX: need to validate *symbol */
if (symbol && symbol->name) {
name = modperl_mgv_as_string(aTHX_ symbol, p, 0);
}
else {
name = handler->name;
}
MP_TRACE_h(MP_FUNC, "[%s] lookup of %s failed",
modperl_server_desc(s, p), name);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
"lookup of '%s' failed", name);
status = HTTP_INTERNAL_SERVER_ERROR;
}
}
if (status == OK) {
count = call_sv((SV*)cv, flags);
SPAGAIN;
if (count != 1) {
/* XXX can this really happen with G_EVAL|G_SCALAR? */
status = OK;
}
else {
SV *status_sv = POPs;
if (status_sv == &PL_sv_undef) {
/* ModPerl::Util::exit() and Perl_croak internally
* arrange to return PL_sv_undef with G_EVAL|G_SCALAR */
status = OK;
}
else {
status = SvIVx(status_sv);
}
}
PUTBACK;
}
FREETMPS;LEAVE;
if (SvTRUE(ERRSV)) {
MP_TRACE_h(MP_FUNC, "$@ = %s", SvPV_nolen(ERRSV));
status = HTTP_INTERNAL_SERVER_ERROR;
}
if (status == HTTP_INTERNAL_SERVER_ERROR) {
if (r && r->notes) {
apr_table_merge(r->notes, "error-notes", SvPV_nolen(ERRSV));
}
}
TAINT_NOT;
return status;
}
int modperl_callback_run_handlers(int idx, int type,
request_rec *r, conn_rec *c,
server_rec *s,
apr_pool_t *pconf,
apr_pool_t *plog,
apr_pool_t *ptemp,
modperl_hook_run_mode_e run_mode)
{
MP_dSCFG(s);
MP_dDCFG;
MP_dRCFG;
modperl_handler_t **handlers;
apr_pool_t *p = NULL;
MpAV *av, **avp;
int i, status = OK;
const char *desc = NULL;
AV *av_args = (AV *)NULL;
if (!MpSrvENABLE(scfg)) {
MP_TRACE_h(MP_FUNC, "PerlOff for server %s:%u",
s->server_hostname, s->port);
return DECLINED;
}
if (r || c) {
p = c ? c->pool : r->pool;
}
else {
p = pconf;
}
avp = modperl_handler_lookup_handlers(dcfg, scfg, rcfg, p,
type, idx, FALSE, &desc);
if (!(avp && (av = *avp))) {
MP_TRACE_h(MP_FUNC, "no %s handlers configured (%s)",
desc, r ? r->uri : "");
return DECLINED;
}
MP_dINTERPa(r, c, s);
switch (type) {
case MP_HANDLER_TYPE_PER_SRV:
modperl_handler_make_args(aTHX_ &av_args,
"Apache2::RequestRec", r, NULL);
/* per-server PerlSetEnv and PerlPassEnv - only once per-request */
if (! MpReqPERL_SET_ENV_SRV(rcfg)) {
modperl_env_configure_request_srv(aTHX_ r);
}
break;
case MP_HANDLER_TYPE_PER_DIR:
modperl_handler_make_args(aTHX_ &av_args,
"Apache2::RequestRec", r, NULL);
/* per-server PerlSetEnv and PerlPassEnv - only once per-request */
if (! MpReqPERL_SET_ENV_SRV(rcfg)) {
modperl_env_configure_request_srv(aTHX_ r);
}
/* per-directory PerlSetEnv - only once per-request */
if (! MpReqPERL_SET_ENV_DIR(rcfg)) {
modperl_env_configure_request_dir(aTHX_ r);
}
break;
case MP_HANDLER_TYPE_PRE_CONNECTION:
case MP_HANDLER_TYPE_CONNECTION:
modperl_handler_make_args(aTHX_ &av_args,
"Apache2::Connection", c, NULL);
break;
case MP_HANDLER_TYPE_FILES:
modperl_handler_make_args(aTHX_ &av_args,
"APR::Pool", pconf,
"APR::Pool", plog,
"APR::Pool", ptemp,
"Apache2::ServerRec", s, NULL);
break;
case MP_HANDLER_TYPE_PROCESS:
modperl_handler_make_args(aTHX_ &av_args,
"APR::Pool", pconf,
"Apache2::ServerRec", s, NULL);
break;
};
modperl_callback_current_callback_set(desc);
MP_TRACE_h(MP_FUNC, "running %d %s handlers", av->nelts, desc);
handlers = (modperl_handler_t **)av->elts;
for (i=0; i<av->nelts; i++) {
status = modperl_callback(aTHX_ handlers[i], p, r, s, av_args);
MP_TRACE_h(MP_FUNC, "callback '%s' returned %d",
modperl_handler_name(handlers[i]), status);
/* follow Apache's lead and let OK terminate the phase for
* MP_HOOK_RUN_FIRST handlers. MP_HOOK_RUN_ALL handlers keep
* going on OK. MP_HOOK_VOID handlers ignore all errors.
*/
if (run_mode == MP_HOOK_RUN_ALL) {
/* special case */
if (type == MP_HANDLER_TYPE_FILES && status != OK) {
/* open_logs and post_config require OK return code or
* the server aborts, so we need to log an error in
* case the handler didn't fail but returned something
* different from OK */
if (SvTRUE(ERRSV)) {
status = modperl_errsv(aTHX_ status, r, s);
}
else {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
"Callback '%s' returned %d, whereas "
"Apache2::Const::OK (%d) is the only valid "
"return value for %s handlers",
modperl_handler_name(handlers[i]),
status, OK, desc);
}
break;
}
/* the normal case:
* OK and DECLINED continue
* errors end the phase
*/
else if ((status != OK) && (status != DECLINED)) {
status = modperl_errsv(aTHX_ status, r, s);
#ifdef MP_TRACE
if (i+1 != av->nelts) {
MP_TRACE_h(MP_FUNC, "error status %d leaves %d "
"uncalled %s handlers",
status, av->nelts-i-1, desc);
}
#endif
break;
}
}
else if (run_mode == MP_HOOK_RUN_FIRST) {
/* the exceptional case:
* OK and errors end the phase
* DECLINED continues
*/
if (status == OK) {
#ifdef MP_TRACE
if (i+1 != av->nelts) {
MP_TRACE_h(MP_FUNC, "OK ends the %s stack, "
"leaving %d uncalled %s handlers",
desc, av->nelts-i-1, desc);
}
#endif
break;
}
if (status != DECLINED) {
status = modperl_errsv(aTHX_ status, r, s);
#ifdef MP_TRACE
if (i+1 != av->nelts) {
MP_TRACE_h(MP_FUNC, "error status %d leaves %d "
"uncalled %s handlers",
status, av->nelts-i-1, desc);
}
#endif
break;
}
}
else {
/* the rare case.
* MP_HOOK_VOID handlers completely ignore the return status
* Apache should handle whatever mod_perl returns,
* so there is no need to mess with the status
*/
}
/* it's possible that during the last callback a new handler
* was pushed onto the same phase it's running from. av needs
* to be updated.
*
* XXX: would be nice to somehow optimize that
*/
avp = modperl_handler_lookup_handlers(dcfg, scfg, rcfg, p,
type, idx, FALSE, NULL);
if (avp && (av = *avp)) {
handlers = (modperl_handler_t **)av->elts;
}
}
SvREFCNT_dec((SV*)av_args);
MP_INTERP_PUTBACK(interp, aTHX);
return status;
}
int modperl_callback_per_dir(int idx, request_rec *r,
modperl_hook_run_mode_e run_mode)
{
return modperl_callback_run_handlers(idx, MP_HANDLER_TYPE_PER_DIR,
r, NULL, r->server,
NULL, NULL, NULL, run_mode);
}
int modperl_callback_per_srv(int idx, request_rec *r,
modperl_hook_run_mode_e run_mode)
{
return modperl_callback_run_handlers(idx,
MP_HANDLER_TYPE_PER_SRV,
r, NULL, r->server,
NULL, NULL, NULL, run_mode);
}
int modperl_callback_connection(int idx, conn_rec *c,
modperl_hook_run_mode_e run_mode)
{
return modperl_callback_run_handlers(idx,
MP_HANDLER_TYPE_CONNECTION,
NULL, c, c->base_server,
NULL, NULL, NULL, run_mode);
}
int modperl_callback_pre_connection(int idx, conn_rec *c, void *csd,
modperl_hook_run_mode_e run_mode)
{
return modperl_callback_run_handlers(idx,
MP_HANDLER_TYPE_PRE_CONNECTION,
NULL, c, c->base_server,
NULL, NULL, NULL, run_mode);
}
void modperl_callback_process(int idx, apr_pool_t *p, server_rec *s,
modperl_hook_run_mode_e run_mode)
{
modperl_callback_run_handlers(idx, MP_HANDLER_TYPE_PROCESS,
NULL, NULL, s,
p, NULL, NULL, run_mode);
}
int modperl_callback_files(int idx,
apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s,
modperl_hook_run_mode_e run_mode)
{
return modperl_callback_run_handlers(idx, MP_HANDLER_TYPE_FILES,
NULL, NULL, s,
pconf, plog, ptemp, run_mode);
}
/*
* Local Variables:
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/