|
1 | 1 |
|
2 | | -#include "apache_http_modsecurity.h" |
3 | 2 | #include "msc_filters.h" |
4 | 3 |
|
5 | | -static void OutputFilter(request_rec *r) |
6 | | -{ |
7 | | - FilterConfig *pConfig = ap_get_module_config(r->server->module_config, |
8 | | - &security3_module); |
9 | 4 |
|
10 | | - if (!pConfig->oEnabled) |
11 | | - { |
12 | | - return; |
13 | | - } |
14 | | - |
15 | | - ap_add_output_filter("OUT", NULL, r, r->connection); |
16 | | -} |
17 | | - |
18 | | -static void InputFilter(request_rec *r) |
19 | | -{ |
20 | | - FilterConfig *pConfig = ap_get_module_config(r->server->module_config, |
21 | | - &security3_module); |
22 | | - if (!pConfig->iEnabled) |
23 | | - { |
24 | | - return; |
25 | | - } |
26 | | - |
27 | | - ap_add_input_filter("IN", NULL, r, r->connection); |
28 | | -} |
29 | | - |
30 | | -static int modsec_handler(request_rec *r) |
31 | | -{ |
32 | | - |
33 | | - |
34 | | - if (!r->handler || strcmp(r->handler, "security3_module")) |
35 | | - { |
36 | | - return (DECLINED); |
37 | | - } |
38 | | - |
39 | | - ap_rputs("Welcome to ModSec!<br/>", r); |
40 | | - fprintf(stderr, "Welcome to ModSec!\n"); |
41 | | - return OK; |
42 | | -} |
43 | | - |
44 | | - |
45 | | - |
46 | | -static void *FilterOutCreateServerConfig(apr_pool_t *p, server_rec *s) |
47 | | -{ |
48 | | - FilterConfig *pConfig = apr_pcalloc(p,sizeof *pConfig); |
49 | | - |
50 | | - pConfig->oEnabled = 1; |
51 | | - |
52 | | - return pConfig; |
53 | | -} |
54 | | - |
55 | | -static void *FilterInCreateServerConfig(apr_pool_t *p, server_rec *s) |
56 | | -{ |
57 | | - FilterConfig *pConfig = apr_pcalloc(p, sizeof *pConfig); |
58 | | - |
59 | | - pConfig->iEnabled = 1; |
60 | | - |
61 | | - return pConfig; |
62 | | -} |
63 | | - |
64 | | -static int input_filter(ap_filter_t *f, apr_bucket_brigade *pbbOut, |
65 | | - ap_input_mode_t eMode, apr_read_type_e eBlock, apr_off_t nBytes) |
66 | | -{ |
67 | | - |
68 | | - request_rec *r = f->r; |
69 | | - conn_rec *c = r->connection; |
70 | | - FilterContext *pCtx; |
71 | | - apr_status_t ret; |
72 | | - |
73 | | - apache_http_modsecurity_main_conf_t *md = ap_get_module_config(r->server->module_config, |
74 | | - &security3_module); |
75 | | - apache_http_modsecurity_loc_conf_t *cf = ap_get_module_config(r->server->module_config, |
76 | | - &security3_module); |
77 | | - |
78 | | - md->transaction = msc_new_transaction(md->modsec, cf->rules_set, NULL); |
79 | | - |
80 | | - if (!(pCtx = f->ctx)) |
81 | | - { |
82 | | - f->ctx = pCtx = apr_palloc(r->pool, sizeof *pCtx); |
83 | | - pCtx->pbbTmp = apr_brigade_create(r->pool, c->bucket_alloc); |
84 | | - } |
85 | | - |
86 | | - if (APR_BRIGADE_EMPTY(pCtx->pbbTmp)) |
87 | | - { |
88 | | - ret = ap_get_brigade(f->next, pCtx->pbbTmp, eMode, eBlock, nBytes); |
89 | | - |
90 | | - if (eMode == AP_MODE_EATCRLF || ret != APR_SUCCESS) |
91 | | - return ret; |
92 | | - } |
93 | | - |
94 | | - while (!APR_BRIGADE_EMPTY(pCtx->pbbTmp)) |
95 | | - { |
96 | | - apr_bucket *pbktIn = APR_BRIGADE_FIRST(pCtx->pbbTmp); |
97 | | - apr_bucket *pbktOut; |
98 | | - const char *data; |
99 | | - apr_size_t len; |
100 | | - unsigned char *buf; |
101 | | - apr_size_t n; |
102 | | - |
103 | | - if (APR_BUCKET_IS_EOS(pbktIn)) |
104 | | - { |
105 | | - APR_BUCKET_REMOVE(pbktIn); |
106 | | - APR_BRIGADE_INSERT_TAIL(pbbOut, pbktIn); |
107 | | - break; |
108 | | - } |
109 | | - |
110 | | - ret=apr_bucket_read(pbktIn, &data, &len, eBlock); |
111 | | - if (ret != APR_SUCCESS) |
112 | | - { |
113 | | - return ret; |
114 | | - } |
115 | | - |
116 | | - buf = (unsigned char *) malloc(len); |
117 | | - for (n=0 ; n < len ; ++n) |
118 | | - { |
119 | | - buf[n] = data[n]; |
120 | | - } |
121 | | - |
122 | | - msc_append_request_body(md->transaction, buf, len); |
123 | | - fprintf(stderr, "req app\n"); |
124 | | - |
125 | | - |
126 | | - pbktOut = apr_bucket_heap_create(buf, len, 0, c->bucket_alloc); |
127 | | - APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut); |
128 | | - apr_bucket_delete(pbktIn); |
129 | | - } |
130 | | - msc_process_request_body(md->transaction); |
131 | | - msc_process_logging(md->transaction); |
132 | | - fprintf(stderr, "req \n"); |
133 | | - |
134 | | - |
135 | | - return APR_SUCCESS; |
136 | | -} |
137 | | - |
138 | | -static int output_filter(ap_filter_t *f, apr_bucket_brigade *pbbIn) |
139 | | -{ |
140 | | - |
141 | | - request_rec *r = f->r; |
142 | | - conn_rec *c = r->connection; |
143 | | - apr_bucket *pbktIn; |
144 | | - apr_bucket_brigade *pbbOut; |
145 | | - |
146 | | - pbbOut = apr_brigade_create(r->pool, c->bucket_alloc); |
147 | | - |
148 | | - |
149 | | - apache_http_modsecurity_main_conf_t *md = ap_get_module_config(r->server->module_config, |
150 | | - &security3_module); |
151 | | - apache_http_modsecurity_loc_conf_t *cf = ap_get_module_config(r->server->module_config, |
152 | | - &security3_module); |
153 | | - |
154 | | - md->transaction = msc_new_transaction(md->modsec, cf->rules_set, NULL); |
155 | | - |
156 | | - for (pbktIn = APR_BRIGADE_FIRST(pbbIn); |
157 | | - pbktIn != APR_BRIGADE_SENTINEL(pbbIn); |
158 | | - pbktIn = APR_BUCKET_NEXT(pbktIn)) |
159 | | - { |
160 | | - const char *data; |
161 | | - apr_size_t len; |
162 | | - unsigned char *buf; |
163 | | - apr_size_t n; |
164 | | - apr_bucket *pbktOut; |
165 | | - |
166 | | - if (APR_BUCKET_IS_EOS(pbktIn)) |
167 | | - { |
168 | | - apr_bucket *pbktEOS = apr_bucket_eos_create(c->bucket_alloc); |
169 | | - APR_BRIGADE_INSERT_TAIL(pbbOut, pbktEOS); |
170 | | - continue; |
171 | | - } |
172 | | - |
173 | | - apr_bucket_read(pbktIn, &data, &len, APR_BLOCK_READ); |
174 | | - |
175 | | - buf = apr_bucket_alloc(len, c->bucket_alloc); |
176 | | - for (n=0 ; n < len ; ++n) |
177 | | - { |
178 | | - buf[n] = data[n]; |
179 | | - } |
180 | | - |
181 | | - msc_append_response_body(md->transaction, buf, len); |
182 | | - fprintf(stderr, "res app\n"); |
183 | | - |
184 | | - pbktOut = apr_bucket_heap_create(buf, len, apr_bucket_free, |
185 | | - c->bucket_alloc); |
186 | | - APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut); |
187 | | - } |
188 | | - msc_process_response_body(md->transaction); |
189 | | - msc_process_logging(md->transaction); |
190 | | - fprintf(stderr, "res \n"); |
191 | | - fprintf(stderr, "WMI '%s' \n",msc_who_am_i (md->modsec )); |
192 | | - |
193 | | - |
194 | | - apr_brigade_cleanup(pbbIn); |
195 | | - return ap_pass_brigade(f->next, pbbOut); |
196 | | -} |
0 commit comments