forked from danghvu/mod_dumpost
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_dumpost.c
249 lines (215 loc) · 9.42 KB
/
mod_dumpost.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
/*******************************************************************************
* Copyright (c) 2012 Hoang-Vu Dang <[email protected]>
* This file is part of mod_dumpost
*
* mod_dumpost is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mod_dumpost is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mod_dumpost. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "httpd.h"
#include "http_connection.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_request.h"
#include "apr_strings.h"
#include "mod_dumpost.h"
#define DEBUG(request, format, ...) ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, format, __VA_ARGS__);
module AP_MODULE_DECLARE_DATA dumpost_module;
static void dumpit(request_rec *r, apr_bucket *b, char *buf, apr_size_t *current_size) {
dumpost_cfg_t *cfg =
(dumpost_cfg_t *) ap_get_module_config(r->per_dir_config, &dumpost_module);
if (*current_size < cfg->max_size && !(APR_BUCKET_IS_METADATA(b))) {
const char * ibuf;
apr_size_t nbytes;
if (apr_bucket_read(b, &ibuf, &nbytes, APR_BLOCK_READ) == APR_SUCCESS) {
if (nbytes) {
DEBUG(r, "%ld bytes read from bucket for request %s", nbytes, r->the_request);
nbytes = min(nbytes, cfg->max_size - *current_size);
strncpy(buf, ibuf, nbytes);
*current_size += nbytes;
}
} else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"mod_dumpost: error reading data");
}
}
else {
if (APR_BUCKET_IS_EOS(b)) {
DEBUG(r, "EOS bucket detected for request %s", r->the_request);
}
}
}
apr_status_t logit(ap_filter_t *f) {
request_state *state = f->ctx;
request_rec *r = f->r;
if (state == NULL || state->log_size == 0) return -1;
if (state->fd == NULL) {
// no file to write to, write to error log
// data is truncated to MAX_STRING_LEN ~ 8192 in apache
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
"\"%s\" %s", r->the_request, state->buffer);
} else {
// need to manually get the time and ip address -- too lazy to make these cusomizable
char *time = apr_palloc(r->pool, 50);
apr_ctime(time, r->request_time);
// condition taken from mod_security
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER > 2
char *ip = r->connection->client_ip;
#else
char *ip = r->connection->remote_ip;
#endif
apr_size_t nbytes_written;
char *text = apr_psprintf(r->pool, "[%s] %s \"%s\" %s\n",time, ip, r->the_request, state->buffer);
apr_status_t rc = apr_file_write_full(state->fd, text, strlen(text), &nbytes_written);
if (rc != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "mod_dumpost: error while writing to log");
return rc;
}
apr_file_close(state->fd);
}
return APR_SUCCESS;
}
apr_status_t dumpost_input_filter (ap_filter_t *f, apr_bucket_brigade *bb,
ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes) {
dumpost_cfg_t *cfg =
(dumpost_cfg_t *) ap_get_module_config(f->r->per_dir_config, &dumpost_module);
apr_bucket *b;
apr_status_t ret;
/* restoring state */
request_state *state = f->ctx;
if (state == NULL) {
/* create state if not yet */
apr_pool_t *mp;
if ((ret = apr_pool_create(&mp, f->r->pool)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r, "mod_dumpost: unable to create memory pool");
return ret;
}
f->ctx = state = (request_state *) apr_palloc(mp, sizeof *state);
state->mp = mp;
state->log_size = 0;
state->header_printed = 0;
state->buffer = apr_palloc(state->mp, cfg->max_size + 1); //1 byte more because string buffer is null terminated
state->fd = NULL;
if (cfg->file != 0) {
apr_status_t rc = apr_file_open(&state->fd, cfg->file,
APR_FOPEN_CREATE | APR_FOPEN_APPEND | APR_FOPEN_WRITE
, APR_OS_DEFAULT, state->mp);
if (rc != APR_SUCCESS) {
char buferr[50];
apr_strerror(rc, buferr, 50);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, f->r, "mod_dumpost: unable to open the log file: %s %s", cfg->file, buferr);
}
}
//This doesn't work for oldest apr versions but i can obtain same result in a cleaner way using macro APR_BUCKET_IS_EOS() in dumpit function to detect when data stream ends
apr_pool_pre_cleanup_register(state->mp, f, (apr_status_t (*)(void *))logit);
}
char *buf = state->buffer;
apr_size_t buf_len = state->log_size;
char **headers = (cfg->headers->nelts > 0)?(char **) cfg->headers->elts : NULL;
/* dump header if config */
if (state->log_size != LOG_IS_FULL && headers!=NULL && !state->header_printed) {
int i;
for (i = 0; i < cfg->headers->nelts; ++i) {
const char *s = apr_table_get(f->r->headers_in, headers[i]);
if (s == NULL) continue;
// Append header name.
int len = strlen(headers[i]);
if (buf_len + len + 5 >= cfg->max_size) {
// Header name and static text won't even fit, skip whole header.
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r, "mod_dumpost: skipped header %s, body limit reach", headers[i]);
continue;
}
buf[buf_len++] = '"';
strncpy(buf + buf_len, headers[i], len);
buf_len += len;
buf[buf_len++] = ':';
buf[buf_len++] = ' ';
// Append header contents (trimmed as necessary).
len = strlen(s);
len = min(len, cfg->max_size - buf_len - 2);
strncpy(buf + buf_len, s, len);
buf_len += len;
buf[buf_len++] = '"';
buf[buf_len++] = ' ';
// Continue loop even if more headers won't fit to ensure that every skipped header is logged.
}
state->header_printed = 1;
}
if ((ret = ap_get_brigade(f->next, bb, mode, block, readbytes)) != APR_SUCCESS)
return ret;
/* dump body */
DEBUG(f->r, "Start brigade for request: %s", f->r->the_request)
for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b))
if (state->log_size != LOG_IS_FULL && buf_len < cfg->max_size)
dumpit(f->r, b, buf + buf_len, &buf_len);
DEBUG(f->r, "End brigade for request: %s, buffer: %ld bytes", f->r->the_request, buf_len)
if (buf_len && state->log_size != LOG_IS_FULL) {
buf_len = min(buf_len, cfg->max_size);
state->log_size = buf_len;
if (state->log_size == cfg->max_size){
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r, "mod_dumpost: body limit reach");
state->log_size = LOG_IS_FULL;
}
}
// Ensure ending NUL. Take special care of buffer marker.
buf[buf_len != LOG_IS_FULL ? buf_len : cfg->max_size] = 0x00;
return APR_SUCCESS;
}
static void dumpost_insert_filter( request_rec *req) {
ap_add_input_filter("DUMPOST_IN", NULL, req, req->connection);
}
static void dumpost_register_hooks(apr_pool_t *p) {
ap_hook_insert_filter(dumpost_insert_filter, NULL, NULL, APR_HOOK_FIRST);
ap_register_input_filter("DUMPOST_IN", dumpost_input_filter,
NULL, AP_FTYPE_CONTENT_SET);
}
static void *dumpost_create_dconfig(apr_pool_t *mp, char *path) {
dumpost_cfg_t *cfg = apr_pcalloc(mp, sizeof(dumpost_cfg_t));
cfg->max_size = DEFAULT_MAX_SIZE;
cfg->headers = apr_array_make(mp, 0, sizeof(char *));
cfg->pool = mp;
cfg->file = 0;
return cfg;
}
static const char *dumpost_set_max_size(cmd_parms *cmd, void *_cfg, const char *arg) {
dumpost_cfg_t *cfg = (dumpost_cfg_t *) _cfg; //ap_get_module_config(cmd->server->module_config, &dumpost_module);
cfg->max_size = atoi(arg);
if (cfg->max_size == 0)
cfg->max_size = DEFAULT_MAX_SIZE;
return NULL;
}
static const char *dumpost_add_header(cmd_parms *cmd, void *_cfg, const char *arg) {
dumpost_cfg_t *cfg = (dumpost_cfg_t *) _cfg;
*(const char**) apr_array_push(cfg->headers) = arg;
return NULL;
}
static const char *dumpost_log_file(cmd_parms *cmd, void *_cfg, const char *arg ){
dumpost_cfg_t *cfg = (dumpost_cfg_t *) _cfg;
cfg->file = (char *) arg;
return NULL;
}
static const command_rec dumpost_cmds[] = {
AP_INIT_TAKE1("DumpPostMaxSize", dumpost_set_max_size, NULL, RSRC_CONF, "Set maximum data size"),
AP_INIT_ITERATE("DumpPostHeaderAdd", dumpost_add_header, NULL, RSRC_CONF, "Add header to log"),
AP_INIT_TAKE1("DumpPostLogFile", dumpost_log_file, NULL, RSRC_CONF, "A custom file to log to"),
{ NULL }
};
module AP_MODULE_DECLARE_DATA dumpost_module = {
STANDARD20_MODULE_STUFF,
dumpost_create_dconfig,
NULL,
NULL, //dumpost_create_sconfig,
NULL,
dumpost_cmds,
dumpost_register_hooks
};