-
Notifications
You must be signed in to change notification settings - Fork 1
/
apache-http-server-facade.c
567 lines (469 loc) · 15.9 KB
/
apache-http-server-facade.c
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* Copyright (C) 2020 Ping Identity Corporation
* All rights reserved.
*
* The contents of this file are the property of Ping Identity Corporation.
* You may not copy or use this file, in either source code or executable
* form, except in compliance with terms set by Ping Identity Corporation.
* For further information please contact:
*
* Ping Identity Corporation
* 1001 17th St Suite 100
* Denver, CO 80202
* 303.468.2900
* https://www.pingidentity.com
*/
/**
* PAA HTTP server facade implementation for Apache
*
* @file apache-http-server-facade.c
*/
#include "util_filter.h"
#include "http_protocol.h"
#include "paa-log.h"
#include "paa-util.h"
#include "apache-http-server-facade.h"
/* Equivalent of ASCII "AP24" */
#if (MODULE_MAGIC_COOKIE == 0x41503234UL)
#define APACHE24
/* Equivalent of ASCII "AP22" */
#elif (MODULE_MAGIC_COOKIE == 0x41503232UL)
#define APACHE22
#endif
const char * const APACHE_MSGID = "PAA_APACHE";
static const size_t READ_FAILURE = (size_t)-1;
static
size_t read_cb(unsigned char *dst, size_t size, void *data)
{
apache_client_req *req_wrapper;
apr_bucket_brigade *brigade;
size_t size_written, write_buffer_len;
request_rec *rec;
req_wrapper = (apache_client_req *)data;
rec = req_wrapper->rec;
if (req_wrapper->eos_reached) {
// This handles the case where end-of-stream was reached, but data was written during
// that callback invocation
return 0;
}
write_buffer_len = size;
size_written = 0;
do {
apr_status_t result;
apr_bucket *bucket;
apr_size_t left_to_write = write_buffer_len - size_written;
brigade = apr_brigade_create(rec->pool, rec->connection->bucket_alloc);
if (brigade == NULL) {
paa_log(rec->pool, APACHE_MSGID, PAA_ERROR,
"failed to create brigade to read client request body");
size_written = READ_FAILURE;
break;
}
result = ap_get_brigade(rec->input_filters, brigade, AP_MODE_READBYTES,
APR_BLOCK_READ, left_to_write);
if (result != APR_SUCCESS) {
paa_log_error(rec->pool, APACHE_MSGID, PAA_ERROR, result,
"failed to obtain input brigade to read client request body");
size_written = READ_FAILURE;
apr_brigade_destroy(brigade);
break;
}
for (bucket = APR_BRIGADE_FIRST(brigade);
bucket != APR_BRIGADE_SENTINEL(brigade);
bucket = APR_BUCKET_NEXT(bucket))
{
if (APR_BUCKET_IS_EOS(bucket)) {
req_wrapper->eos_reached = 1;
break;
}
// Need to ignore metadata
if (APR_BUCKET_IS_METADATA(bucket)) {
continue;
}
const char *bucket_buf = NULL;
size_t actual_read = 0;
result = apr_bucket_read(bucket, &bucket_buf, &actual_read, APR_BLOCK_READ);
if (result != APR_SUCCESS) {
paa_log_error(rec->pool, APACHE_MSGID, PAA_ERROR, result,
"failed to read client request body data");
size_written = READ_FAILURE;
break;
}
// Sanity check: even though a specific amount was requested in ap_get_brigade,
// make sure that more data wasn't allocated than requested
if (actual_read > write_buffer_len) {
paa_log(rec->pool, APACHE_MSGID, PAA_ERROR, "more data available than requested");
size_written = READ_FAILURE;
break;
}else{
memcpy(dst, bucket_buf, actual_read);
}
size_written += actual_read;
}
// Cleanup the data used by the current brigade
apr_brigade_destroy(brigade);
}while(0);
return size_written;
}
static
size_t agent_resp_apache_write_cb(const unsigned char *src, size_t size, void *userdata)
{
request_rec *r = (request_rec *)userdata;
size_t total_to_read = size;
size_t size_read;
if (total_to_read > 0) {
do {
apr_bucket_brigade *bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
if (bb == NULL) {
paa_log(r->pool, APACHE_MSGID, PAA_ERROR,
"failed to create brigade to write agent response");
size_read = 0;
break;
}
apr_status_t result = ap_fwrite(r->output_filters, bb, (const char *)src,
total_to_read);
if (result != APR_SUCCESS) {
paa_log_error(r->pool, APACHE_MSGID, PAA_ERROR, result,
"failed to write agent response data");
size_read = 0;
break;
}
result = ap_fflush(r->output_filters, bb);
if (result != APR_SUCCESS) {
paa_log_error(r->pool, APACHE_MSGID, PAA_ERROR, result,
"failed to flush agent response data");
size_read = 0;
break;
}
size_read = total_to_read;
}while(0);
}else{
size_read = 0;
}
return size_read;
}
static
inline
apache_client_req *req_impl(const paa_client_request *req)
{
return ((apache_client_req *)req->impl);
}
static
inline
apache_client_resp *resp_impl(const paa_client_response *resp)
{
return ((apache_client_resp *)resp->impl);
}
static
paa_client_response_write_cb client_response_get_write_cb(const paa_client_response *resp)
{
USE(resp);
return agent_resp_apache_write_cb;
}
static
void *client_response_get_write_data(const paa_client_response *resp)
{
return resp_impl(resp)->rec;
}
static
paa_client_request_read_cb client_request_get_read_cb(const paa_client_request *req)
{
USE(req);
return read_cb;
}
static
void *client_request_get_read_data(const paa_client_request *req)
{
return req_impl(req);
}
static
const char *client_request_get_method(const paa_client_request *req)
{
return req_impl(req)->rec->method;
}
static
const char *client_request_get_normalized_uri(const paa_client_request *req)
{
return req_impl(req)->full_normalized_uri;
}
static
const char *client_request_get_raw_uri(const paa_client_request *req)
{
return req_impl(req)->rec->unparsed_uri;
}
static
const char *client_request_get_remote_ip(const paa_client_request *req)
{
char *ip =
#ifdef APACHE24
req_impl(req)->rec->connection->client_ip;
#else
req_impl(req)->rec->connection->remote_ip;
#endif
return ip;
}
static
const char *client_request_get_scheme(const paa_client_request *req)
{
return ap_http_scheme(req_impl(req)->rec);
}
static
const char *client_request_get_scope(const paa_client_request *req)
{
USE(req);
return "";
}
struct client_req_iter_data
{
paa_header_cb cb;
void *userdata;
apr_status_t result;
};
static
int client_request_header_iter(void *reqdata, const char *key, const char *value)
{
apr_status_t result;
struct client_req_iter_data *iter_data;
iter_data = (struct client_req_iter_data *)reqdata;
result = iter_data->cb(key, value, iter_data->userdata);
if (result != APR_EAGAIN) {
iter_data->result = result;
return 0;
}
iter_data->result = APR_SUCCESS;
return 1;
}
static
apr_status_t client_request_enumerate_headers(const paa_client_request *req, paa_header_cb cb,
void *userdata)
{
struct client_req_iter_data iter_data;
iter_data.cb = cb;
iter_data.userdata = userdata;
iter_data.result = APR_SUCCESS;
int iter_result = apr_table_do(client_request_header_iter, &iter_data,
req_impl(req)->rec->headers_in, NULL);
if (iter_result == 0) {
paa_log(req_impl(req)->rec->pool, APACHE_MSGID,
PAA_DEBUG, "client request header callback returned early");
}
return iter_data.result;
}
// Note:
// The data passed to these functions is guaranteed to have the same lifetime as the request.
// This allows the apr_table_*n functions to be applied instead of their counterparts that copy
// the data.
static
apr_status_t client_request_get_header_values(const paa_client_request *req,
const char *name,
paa_header_cb cb,
void *userdata)
{
struct client_req_iter_data iter_data;
iter_data.cb = cb;
iter_data.userdata = userdata;
iter_data.result = APR_SUCCESS;
int iter_result = apr_table_do(client_request_header_iter, &iter_data,
req_impl(req)->rec->headers_in, name, NULL);
if (iter_result == 0) {
paa_log(req_impl(req)->rec->pool, APACHE_MSGID,
PAA_TRACE, "client request header callback returned early");
}
return iter_data.result;
}
static
apr_status_t client_request_set_header(const paa_client_request *req, const char *name,
const char *value)
{
if (strcasecmp(name, HTTP_CONTENT_TYPE) == 0) {
ap_set_content_type(req_impl(req)->rec, value);
}
apr_table_setn(req_impl(req)->rec->headers_in, name, value);
return APR_SUCCESS;
}
static
apr_status_t client_request_add_header(const paa_client_request *req, const char *name,
const char *value)
{
if (strcasecmp(name, HTTP_CONTENT_TYPE) == 0) {
// append does not make sense for Content-Type since an HTTP message can have only one
// Content-Type -- appending will likely confuse Apache in unknown ways
ap_set_content_type(req_impl(req)->rec, value);
apr_table_setn(req_impl(req)->rec->headers_in, name, value);
}else{
apr_table_addn(req_impl(req)->rec->headers_in, name, value);
if (strcasecmp(HTTP_COOKIE, name) == 0) {
// Collapse the cookie headers into a single header field
const char *cookie_value = paa_util_apr_table_getm(req_impl(req)->rec->pool,
req_impl(req)->rec->headers_in,
name,
';');
apr_table_setn(req_impl(req)->rec->headers_in, HTTP_COOKIE, cookie_value);
}
}
return APR_SUCCESS;
}
static
apr_status_t client_request_set_req_var(const paa_client_request *req, const char *name,
const char *value)
{
apr_table_setn(req_impl(req)->rec->subprocess_env, name, value);
return APR_SUCCESS;
}
static
apr_status_t client_request_remove_header(const paa_client_request *req, const char *name)
{
apr_table_unset(req_impl(req)->rec->headers_in, name);
return APR_SUCCESS;
}
static
apr_status_t client_request_remove_req_var(const paa_client_request *req, const char *name)
{
apr_table_unset(req_impl(req)->rec->subprocess_env, name);
return APR_SUCCESS;
}
static
apr_status_t client_request_set_user(const paa_client_request *req, const char *name)
{
req_impl(req)->rec->user = (char *)name;
return APR_SUCCESS;
}
static
const char *client_request_get_user(const paa_client_request *req)
{
return req_impl(req)->rec->user;
}
static
apr_status_t client_response_replacing(const paa_client_response *resp)
{
apr_table_clear(resp_impl(resp)->rec->headers_out);
apr_table_clear(resp_impl(resp)->rec->err_headers_out);
return APR_SUCCESS;
}
static
apr_status_t client_response_set_header(const paa_client_response *resp, const char *name,
const char *value)
{
if (strcasecmp(name, HTTP_CONTENT_TYPE) == 0) {
ap_set_content_type(resp_impl(resp)->rec, value);
}
apr_table_setn(resp_impl(resp)->rec->headers_out, name, value);
return APR_SUCCESS;
}
static
apr_status_t client_response_add_header(const paa_client_response *resp, const char *name,
const char *value)
{
if (strcasecmp(name, HTTP_CONTENT_TYPE) == 0) {
// append does not make sense for Content-Type since a message can have only one
// Content-Type -- appending will likely confuse Apache in unknown ways
ap_set_content_type(resp_impl(resp)->rec, value);
apr_table_setn(resp_impl(resp)->rec->headers_out, name, value);
}else{
apr_table_addn(resp_impl(resp)->rec->headers_out, name, value);
}
return APR_SUCCESS;
}
static
apr_status_t client_response_remove_header(const paa_client_response *resp, const char *name)
{
if (strcasecmp(name, HTTP_CONTENT_TYPE) == 0) {
ap_set_content_type(resp_impl(resp)->rec, NULL);
}
apr_table_unset(resp_impl(resp)->rec->headers_out, name);
return APR_SUCCESS;
}
static
apr_status_t client_response_set_status(const paa_client_response *resp, int status,
const char *reason)
{
USE(reason);
// Apache doesn't require that the reason string be set -- if unset, it will set it to the
// standard value which is acceptable (and desired)
resp_impl(resp)->rec->status = status;
return APR_SUCCESS;
}
struct client_response_iter_data
{
paa_header_cb cb;
void *userdata;
apr_status_t result;
};
static
int client_response_header_iter(void *reqdata, const char *key, const char *value)
{
apr_status_t result;
struct client_response_iter_data *iter_data;
iter_data = (struct client_response_iter_data *)reqdata;
result = iter_data->cb(key, value, iter_data->userdata);
if (result != APR_EAGAIN) {
iter_data->result = result;
return 0;
}
iter_data->result = APR_SUCCESS;
return 1;
}
static
apr_status_t
client_response_enumerate_headers(const paa_client_response *resp, paa_header_cb cb,
void *userdata)
{
struct client_response_iter_data iter_data;
iter_data.cb = cb;
iter_data.userdata = userdata;
iter_data.result = APR_SUCCESS;
int iter_result = apr_table_do(client_response_header_iter, &iter_data,
resp_impl(resp)->rec->headers_out, NULL);
if (iter_result == 0) {
paa_log(resp_impl(resp)->rec->pool, APACHE_MSGID, PAA_DEBUG,
"client response header callback returned early for standard headers");
}
iter_result = apr_table_do(client_response_header_iter, &iter_data,
resp_impl(resp)->rec->err_headers_out, NULL);
if (iter_result == 0) {
paa_log(resp_impl(resp)->rec->pool, APACHE_MSGID, PAA_DEBUG,
"client response header callback returned early for error headers");
}
return iter_data.result;
}
static
int client_response_get_status(const paa_client_response *resp)
{
return resp_impl(resp)->rec->status;
}
void apache_client_req_init(paa_client_request *intf,
apache_client_req *impl)
{
intf->impl = (void *)impl;
intf->get_method = client_request_get_method;
intf->get_normalized_uri = client_request_get_normalized_uri;
intf->get_raw_uri = client_request_get_raw_uri;
intf->get_remote_ip = client_request_get_remote_ip;
intf->get_scheme = client_request_get_scheme;
intf->get_scope = client_request_get_scope;
intf->enumerate_headers = client_request_enumerate_headers;
intf->get_header_values = client_request_get_header_values;
intf->set_header = client_request_set_header;
intf->add_header = client_request_add_header;
intf->remove_header = client_request_remove_header;
intf->set_req_var = client_request_set_req_var;
intf->remove_req_var = client_request_remove_req_var;
intf->set_user = client_request_set_user;
intf->get_user = client_request_get_user;
intf->get_read_cb = client_request_get_read_cb;
intf->get_read_data = client_request_get_read_data;
}
void apache_client_resp_init(paa_client_response *intf,
apache_client_resp *impl)
{
intf->impl = impl;
intf->get_status = client_response_get_status;
intf->set_header = client_response_set_header;
intf->add_header = client_response_add_header;
intf->remove_header = client_response_remove_header;
intf->enumerate_headers = client_response_enumerate_headers;
intf->replacing = client_response_replacing;
intf->set_status = client_response_set_status;
intf->get_write_cb = client_response_get_write_cb;
intf->get_write_data = client_response_get_write_data;
}