Apache HTTPD
mod_dumpio.c
Go to the documentation of this file.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/*
18 * Originally written @ Covalent by Jim Jagielski
19 */
20
21/*
22 * mod_dumpio.c:
23 * Think of this as a filter sniffer for Apache 2.x. It logs
24 * all filter data right before and after it goes out on the
25 * wire (BUT right before SSL encoded or after SSL decoded).
26 * It can produce a *huge* amount of data.
27 */
28
29
30#include "httpd.h"
31#include "http_connection.h"
32#include "http_config.h"
33#include "http_core.h"
34#include "http_log.h"
35#include "apr_strings.h"
36
37module AP_MODULE_DECLARE_DATA dumpio_module ;
38
43
44/* consider up to 80 additional characters, and factor the longest
45 * line length of all \xNN sequences; log_error cannot record more
46 * than MAX_STRING_LEN characters.
47 */
48#define dumpio_MAX_STRING_LEN (MAX_STRING_LEN / 4 - 80)
49
50/*
51 * Workhorse function: simply log to the current error_log
52 * info about the data in the bucket as well as the data itself
53 */
55{
56 conn_rec *c = f->c;
57
59 "mod_dumpio: %s (%s-%s): %" APR_SIZE_T_FMT " bytes",
60 f->frec->name,
61 (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
62 b->type->name,
63 b->length) ;
64
66 {
67#if APR_CHARSET_EBCDIC
69#endif
70 const char *buf;
73
74 if (rv == APR_SUCCESS)
75 {
76 while (nbytes)
77 {
78 apr_size_t logbytes = nbytes;
79 if (logbytes > dumpio_MAX_STRING_LEN)
80 logbytes = dumpio_MAX_STRING_LEN;
81 nbytes -= logbytes;
82
83#if APR_CHARSET_EBCDIC
84 memcpy(xlatebuf, buf, logbytes);
86 xlatebuf[logbytes] = '\0';
88 "mod_dumpio: %s (%s-%s): %s", f->frec->name,
89 (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
90 b->type->name, xlatebuf);
91#else
92 /* XXX: Seriously flawed; we do not pay attention to embedded
93 * \0's in the request body, these should be escaped; however,
94 * the logging function already performs a significant amount
95 * of escaping, and so any escaping would be double-escaped.
96 * The coding solution is to throw away the current logic
97 * within ap_log_error, and introduce new vformatter %-escapes
98 * for escaping text, and for binary text (fixed len strings).
99 */
101 "mod_dumpio: %s (%s-%s): %.*s", f->frec->name,
102 (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
103 b->type->name, (int)logbytes, buf);
104#endif
105 buf += logbytes;
106 }
107 }
108 else {
110 "mod_dumpio: %s (%s-%s): %s", f->frec->name,
111 (APR_BUCKET_IS_METADATA(b)) ? "metadata" : "data",
112 b->type->name, "error reading data");
113 }
114 }
115}
116
117#define whichmode( mode ) \
118 ( (( mode ) == AP_MODE_READBYTES) ? "readbytes" : \
119 (( mode ) == AP_MODE_GETLINE) ? "getline" : \
120 (( mode ) == AP_MODE_EATCRLF) ? "eatcrlf" : \
121 (( mode ) == AP_MODE_SPECULATIVE) ? "speculative" : \
122 (( mode ) == AP_MODE_EXHAUSTIVE) ? "exhaustive" : \
123 (( mode ) == AP_MODE_INIT) ? "init" : "unknown" \
124 )
125
128{
129
130 apr_bucket *b;
132 conn_rec *c = f->c;
133 dumpio_conf_t *ptr = f->ctx;
134
136 "mod_dumpio: %s [%s-%s] %" APR_OFF_T_FMT " readbytes",
137 f->frec->name,
139 ((block) == APR_BLOCK_READ) ? "blocking" : "nonblocking",
140 readbytes);
141
142 ret = ap_get_brigade(f->next, bb, mode, block, readbytes);
143
144 if (ret == APR_SUCCESS) {
146 dumpit(f, b, ptr);
147 }
148 }
149 else {
151 "mod_dumpio: %s - %d", f->frec->name, ret) ;
152 return ret;
153 }
154
155 return APR_SUCCESS ;
156}
157
159{
160 apr_bucket *b;
161 conn_rec *c = f->c;
162 dumpio_conf_t *ptr = f->ctx;
163
164 ap_log_cerror(APLOG_MARK, APLOG_TRACE7, 0, c, "mod_dumpio: %s", f->frec->name);
165
167 /*
168 * If we ever see an EOS, make sure to FLUSH.
169 */
170 if (APR_BUCKET_IS_EOS(b)) {
171 apr_bucket *flush = apr_bucket_flush_create(f->c->bucket_alloc);
173 }
174 dumpit(f, b, ptr);
175 }
176
177 return ap_pass_brigade(f->next, bb) ;
178}
179
180static int dumpio_pre_conn(conn_rec *c, void *csd)
181{
182 dumpio_conf_t *ptr;
183
184 if (!APLOGctrace7(c)) {
185 /* Nothing to do below TRACE7 */
186 return DECLINED;
187 }
188
189 ptr = (dumpio_conf_t *) ap_get_module_config(c->base_server->module_config,
190 &dumpio_module);
191
192 if (ptr->enable_input)
193 ap_add_input_filter("DUMPIO_IN", ptr, NULL, c);
194 if (ptr->enable_output)
195 ap_add_output_filter("DUMPIO_OUT", ptr, NULL, c);
196 return OK;
197}
198
200{
201/*
202 * We know that SSL is CONNECTION + 5
203 */
206
209
211}
212
214{
215 dumpio_conf_t *ptr = apr_pcalloc(p, sizeof *ptr);
216 ptr->enable_input = 0;
217 ptr->enable_output = 0;
218 return ptr;
219}
220
221static const char *dumpio_enable_input(cmd_parms *cmd, void *dummy, int arg)
222{
223 dumpio_conf_t *ptr = ap_get_module_config(cmd->server->module_config,
224 &dumpio_module);
225
226 ptr->enable_input = arg;
227 return NULL;
228}
229
230static const char *dumpio_enable_output(cmd_parms *cmd, void *dummy, int arg)
231{
232 dumpio_conf_t *ptr = ap_get_module_config(cmd->server->module_config,
233 &dumpio_module);
234
235 ptr->enable_output = arg;
236 return NULL;
237}
238
239static const command_rec dumpio_cmds[] = {
240 AP_INIT_FLAG("DumpIOInput", dumpio_enable_input, NULL,
241 RSRC_CONF, "Enable I/O Dump on Input Data"),
242 AP_INIT_FLAG("DumpIOOutput", dumpio_enable_output, NULL,
243 RSRC_CONF, "Enable I/O Dump on Output Data"),
244 { NULL }
245};
246
249 NULL, /* create per-dir config structures */
250 NULL, /* merge per-dir config structures */
251 dumpio_create_sconfig, /* create per-server config structures */
252 NULL, /* merge per-server config structures */
253 dumpio_cmds, /* table of config file commands */
254 dumpio_register_hooks /* register hooks */
255};
APR Strings library.
void ap_hook_pre_connection(ap_HOOK_pre_connection_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition connection.c:43
#define ap_get_module_config(v, m)
#define AP_DECLARE_MODULE(foo)
#define AP_INIT_FLAG(directive, func, mconfig, where, help)
void * csd
int flush
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
#define ap_xlate_proto_from_ascii(x, y)
Definition util_ebcdic.h:81
apr_status_t ap_pass_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket)
ap_filter_t * ap_add_input_filter(const char *name, void *ctx, request_rec *r, conn_rec *c)
ap_filter_rec_t * ap_register_output_filter(const char *name, ap_out_filter_func filter_func, ap_init_filter_func filter_init, ap_filter_type ftype)
ap_filter_t * ap_add_output_filter(const char *name, void *ctx, request_rec *r, conn_rec *c)
apr_status_t ap_filter_rec_t * ap_register_input_filter(const char *name, ap_in_filter_func filter_func, ap_init_filter_func filter_init, ap_filter_type ftype)
apr_status_t ap_get_brigade(ap_filter_t *filter, apr_bucket_brigade *bucket, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
@ AP_FTYPE_CONNECTION
#define ap_log_cerror
Definition http_log.h:498
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_TRACE7
Definition http_log.h:78
#define APLOGctrace7(c)
Definition http_log.h:263
const unsigned char * buf
Definition util_md5.h:50
void * dummy
Definition http_vhost.h:62
void const char * arg
Definition http_vhost.h:63
apr_file_t * f
#define APR_BUCKET_IS_METADATA(e)
#define APR_BUCKET_NEXT(e)
apr_read_type_e
Definition apr_buckets.h:57
#define APR_BRIGADE_SENTINEL(b)
#define APR_BUCKET_IS_EOS(e)
#define APR_BRIGADE_FIRST(b)
#define APR_BUCKET_INSERT_BEFORE(a, b)
#define apr_bucket_read(e, str, len, block)
@ APR_BLOCK_READ
Definition apr_buckets.h:58
apr_dbd_transaction_t int mode
Definition apr_dbd.h:261
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define RSRC_CONF
#define STANDARD20_MODULE_STUFF
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void apr_size_t * nbytes
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_pool_t * b
Definition apr_pools.h:529
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const char * s
Definition apr_strings.h:95
apr_cmdtype_e cmd
Apache Configuration.
Apache connection library.
CORE HTTP Daemon.
Apache Logging library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
#define whichmode(mode)
Definition mod_dumpio.c:117
static int dumpio_pre_conn(conn_rec *c, void *csd)
Definition mod_dumpio.c:180
#define dumpio_MAX_STRING_LEN
Definition mod_dumpio.c:48
static int dumpio_input_filter(ap_filter_t *f, apr_bucket_brigade *bb, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
Definition mod_dumpio.c:126
static void dumpit(ap_filter_t *f, apr_bucket *b, dumpio_conf_t *ptr)
Definition mod_dumpio.c:54
static const char * dumpio_enable_output(cmd_parms *cmd, void *dummy, int arg)
Definition mod_dumpio.c:230
static const command_rec dumpio_cmds[]
Definition mod_dumpio.c:239
static int dumpio_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
Definition mod_dumpio.c:158
static void dumpio_register_hooks(apr_pool_t *p)
Definition mod_dumpio.c:199
static const char * dumpio_enable_input(cmd_parms *cmd, void *dummy, int arg)
Definition mod_dumpio.c:221
static void * dumpio_create_sconfig(apr_pool_t *p, server_rec *s)
Definition mod_dumpio.c:213
return NULL
Definition mod_so.c:359
The representation of a filter chain.
Structure to store things which are per connection.
Definition httpd.h:1152
A structure to store information for each virtual server.
Definition httpd.h:1322
ap_input_mode_t
input filtering modes
Definition util_filter.h:41
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray