/
filter.c
509 lines (458 loc) · 16.4 KB
/
filter.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
/****************************************************************************/
/* pfixtools: a collection of postfix related tools */
/* ~~~~~~~~~ */
/* ______________________________________________________________________ */
/* */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions */
/* are met: */
/* */
/* 1. Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* 2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the */
/* distribution. */
/* 3. The names of its contributors may not be used to endorse or promote */
/* products derived from this software without specific prior written */
/* permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY */
/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE */
/* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
/* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
/* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, */
/* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE */
/* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
/* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/* Copyright (c) 2006-2014 the Authors */
/* see AUTHORS and source files for details */
/****************************************************************************/
#include "str.h"
#include "buffer.h"
#include "filter.h"
struct filter_description_t {
/* Name of the description
*/
filter_token id;
const char* name;
/* Filter execution function
*/
filter_runner_f runner;
/* Construction/destruction
*/
filter_constructor_f constructor;
filter_destructor_f destructor;
/* Filter context construction/destruction
*/
filter_context_constructor_f ctx_constructor;
filter_context_destructor_f ctx_destructor;
/* Valid hooks
*/
bool hooks[HTK_count];
filter_result_t forward[HTK_count];
/* Valid parameters
*/
bool params[ATK_count];
};
static struct {
filter_description_t filter_descriptions[FTK_count];
filter_async_handler_f async_handler;
const filter_hook_t default_hook;
const filter_hook_t async_hook;
bool init_done;
} filter_internal_g = {
#define _G filter_internal_g
.default_hook = {
.type = 0,
.value = (char*)"DUNNO",
.counter = -1,
.cost = 0,
.postfix = true,
.async = false,
.filter_id = 0
},
.async_hook = {
.type = 0,
.value = NULL,
.counter = -1,
.cost = 0,
.postfix = false,
.async = true,
.filter_id = 0
}
};
uint32_t filter_running_g = 0;
#define filter_declare(filter) \
filter_constructor_prototype(filter); \
module_init(filter ## _init_filter);
filter_declare(iplist)
filter_declare(greylist)
filter_declare(strlist)
filter_declare(match)
filter_declare(counter)
filter_declare(spf)
filter_declare(hang)
filter_declare(rate)
filter_declare(srs)
static int filter_module_init(void)
{
if (_G.init_done) {
return 0;
}
_G.init_done = true;
for (int i = 0 ; i < FTK_count ; ++i) {
_G.filter_descriptions[i].id = (filter_token)i;
_G.filter_descriptions[i].name = ftokens[i];
for (int j = 0 ; j < HTK_count ; ++j) {
_G.filter_descriptions[i].forward[j] = HTK_UNKNOWN;
}
}
return 0;
}
module_init(filter_module_init);
filter_type_t
filter_register(const char *type,
filter_constructor_f constructor,
filter_destructor_f destructor,
filter_runner_f runner,
filter_context_constructor_f context_constructor,
filter_context_destructor_f context_destructor)
{
filter_token tok = filter_tokenize(type, m_strlen(type));
CHECK_FILTER(tok);
filter_description_t *description = &_G.filter_descriptions[tok];
description->runner = runner;
description->constructor = constructor;
description->destructor = destructor;
description->ctx_constructor = context_constructor;
description->ctx_destructor = context_destructor;
return description;
}
filter_result_t filter_hook_register(filter_type_t filter,
const char *name)
{
filter_result_t tok = hook_tokenize(name, m_strlen(name));
CHECK_FILTER(filter->id);
CHECK_HOOK(tok);
((filter_description_t*)filter)->hooks[tok] = true;
return tok;
}
void filter_hook_forward_register(filter_type_t filter,
filter_result_t source,
filter_result_t target)
{
filter_module_init();
CHECK_FILTER(filter->id);
CHECK_HOOK(source);
CHECK_HOOK(target);
assert(target != HTK_ASYNC && target != HTK_ABORT
&& "Cannot forward async or abort");
assert(target != HTK_ASYNC && "Cannot forward result to async");
((filter_description_t*)filter)->forward[source] = target;
}
filter_param_id_t filter_param_register(filter_type_t filter,
const char *name)
{
filter_param_id_t tok = param_tokenize(name, m_strlen(name));
CHECK_FILTER(filter->id);
CHECK_PARAM(tok);
((filter_description_t*)filter)->params[tok] = true;
return tok;
}
void filter_async_handler_register(filter_async_handler_f handler)
{
_G.async_handler = handler;
}
bool filter_build(filter_t *filter)
{
bool ret = true;
if (filter->type == NULL || filter->name == NULL) {
return false;
}
if (filter->hooks.len > 0) {
# define QSORT_TYPE filter_hook_t
# define QSORT_BASE filter->hooks.data
# define QSORT_NELT filter->hooks.len
# define QSORT_LT(a,b) a->type < b->type
# include "qsort.c"
}
if (filter->type->constructor) {
ret = filter->type->constructor(filter);
}
array_deep_wipe(filter->params, filter_params_wipe);
return ret;
}
bool filter_update_references(filter_t *filter, A(filter_t) *filter_list)
{
foreach (hook, filter->hooks) {
if (!hook->postfix) {
hook->filter_id = filter_find_with_name(filter_list, hook->value);
if (hook->filter_id == -1) {
err("invalid filter name %s for hook %s",
hook->value, htokens[hook->type]);
return false;
}
p_delete(&hook->value);
}
}
return true;
}
static inline bool filter_check_loop(filter_t *filter, A(filter_t) *array,
int level)
{
if (filter->last_seen == level) {
return true;
}
filter->last_seen = level;
foreach (hook, filter->hooks) {
if (hook->postfix) {
continue;
}
if (hook->filter_id == level) {
return false;
}
if (!filter_check_loop(array_ptr(*array, hook->filter_id),
array, level)) {
return false;
}
}
return true;
}
bool filter_check_safety(A(filter_t) *array)
{
foreach (filter, *array) {
int pos = filter - array->data;
if (!filter_check_loop(filter, array, pos)) {
err("the filter tree contains a loop");
return false;
}
}
return true;
}
void filter_wipe(filter_t *filter)
{
if (filter->type->destructor) {
filter->type->destructor(filter);
}
array_deep_wipe(filter->hooks, filter_hook_wipe);
array_deep_wipe(filter->params, filter_params_wipe);
p_delete(&filter->name);
}
static inline
const filter_hook_t *filter_hook_for_result(const filter_t *filter,
filter_result_t res)
{
int start = 0;
int end = filter->hooks.len;
if (res == HTK_ABORT) {
return NULL;
}
if (res == HTK_ASYNC) {
return &_G.async_hook;
}
while (start < end) {
int mid = (start + end) / 2;
filter_hook_t *hook = array_ptr(filter->hooks, mid);
if (hook->type == res) {
debug("return hook of type %s, value %s",
htokens[hook->type], hook->value);
return hook;
} else if (res < hook->type) {
end = mid;
} else {
start = mid + 1;
}
}
if (filter->type->forward[res] != HTK_UNKNOWN) {
debug("no hook for result %s, forwarding to %s", htokens[res],
htokens[filter->type->forward[res]]);
return filter_hook_for_result(filter, filter->type->forward[res]);
} else {
warn("missing hook %s for filter %s", htokens[res], filter->name);
return &_G.default_hook;
}
}
const filter_hook_t *filter_run(const filter_t *filter, const query_t *query,
filter_context_t *context)
{
debug("running filter %s (%s)", filter->name, filter->type->name);
filter_running_g++;
filter_result_t res = filter->type->runner(filter, query, context);
if (res == HTK_ASYNC) {
context->current_filter = filter;
} else {
filter_running_g--;
context->current_filter = NULL;
}
debug("filter run, result is %s", htokens[res]);
return filter_hook_for_result(filter, res);
}
bool filter_test(const filter_t *filter, const query_t *query,
filter_context_t *context, filter_result_t result)
{
context->explanation.str = 0;
context->explanation.len = 0;
return !!(filter->type->runner(filter, query, context) == result);
}
void filter_set_name(filter_t *filter, const char *name, int len)
{
filter->name = p_dupstr(name, len);
}
bool filter_set_type(filter_t *filter, const char *type, int len)
{
filter_token tok = filter_tokenize(type, len);
if (tok == FTK_UNKNOWN) {
return false;
}
filter->type = &_G.filter_descriptions[tok];
return true;
}
bool filter_add_param(filter_t *filter, const char *name, int name_len,
const char *value, int value_len)
{
filter_param_t param;
param.type = param_tokenize(name, name_len);
if (param.type == ATK_UNKNOWN) {
err("unknown parameter %.*s", name_len, name);
return false;
}
if (!filter->type->params[param.type]) {
err("hook %s is not valid for filter %s",
atokens[param.type], filter->type->name);
return false;
}
param.value = p_dupstr(value, value_len);
param.value_len = value_len;
array_add(filter->params, param);
return true;
}
bool filter_add_hook(filter_t *filter, const char *name, int name_len,
const char *value, int value_len)
{
#define PARSE_CHECK(Cond, Message, ...) \
if (!(Cond)) { \
err("hook %s, " Message, htokens[hook.type], ##__VA_ARGS__); \
filter_hook_wipe(&hook); \
return false; \
}
filter_hook_t hook;
hook.filter_id = -1;
hook.value = NULL;
hook.warn = NULL;
hook.counter = -1;
hook.cost = 0;
hook.type = hook_tokenize(name, name_len);
if (hook.type == HTK_UNKNOWN) {
err("unknown hook type %.*s", name_len, name);
return false;
}
PARSE_CHECK(filter->type->hooks[hook.type] && hook.type != HTK_ABORT,
"not is valid for filter %s", filter->type->name);
hook.async = false;
/* Value format is (counter:id:incr)?(postfix:reply|filter_name)
*/
while (true) {
if (strncmp(value, "counter:", 8) == 0) {
PARSE_CHECK(hook.counter == -1,
"cannot specify more than one counter");
char *end = NULL;
value += 8;
hook.counter = strtol(value, &end, 10);
PARSE_CHECK(end != value && *end == ':',
"cannot read counter id");
PARSE_CHECK(hook.counter >= 0 && hook.counter < MAX_COUNTERS,
"invalid counter id %d", hook.counter);
value = end + 1;
hook.cost = strtol(value, &end, 10);
PARSE_CHECK(end != value && *end == ':',
"cannot read counter increment");
PARSE_CHECK(hook.cost >= 0,
"invalid counter increment value %d", hook.cost);
value = end + 1;
} else if (strncmp(value, "warn:", 5) == 0) {
PARSE_CHECK(hook.warn == NULL,
"cannot specify more than one warning message");
value += 5;
const char* end = strchr(value, ':');
PARSE_CHECK(end != NULL, "invalid unterminated warning message");
PARSE_CHECK(end != value, "empty warning message");
hook.warn = p_dupstr(value, end - value);
PARSE_CHECK(query_format_check(hook.warn),
"invalid message format: \"%s\"", hook.warn);
value = end + 1;
} else {
break;
}
}
hook.postfix = (strncmp(value, "postfix:", 8) == 0);
if (hook.postfix) {
value += 8;
}
PARSE_CHECK(!hook.postfix || query_format_check(value),
"invalid postfix reply format: \"%s\"", value);
hook.value = m_strdup(value);
array_add(filter->hooks, hook);
#undef PARSE_CHECK
return true;
}
void filter_context_prepare(filter_context_t *context, void *qctx)
{
for (int i = 0 ; i < FTK_count ; ++i) {
if (_G.filter_descriptions[i].ctx_constructor != NULL) {
context->contexts[i]
= _G.filter_descriptions[i].ctx_constructor();
}
}
context->current_filter = NULL;
context->explanation.str = NULL;
context->explanation.len = 0;
context->data = qctx;
}
void filter_context_wipe(filter_context_t *context)
{
for (int i = 0 ; i < FTK_count ; ++i) {
if (_G.filter_descriptions[i].ctx_destructor != NULL) {
_G.filter_descriptions[i].ctx_destructor(context->contexts[i]);
}
}
}
void* filter_context(const filter_t *filter, filter_context_t *context)
{
return context->contexts[filter->type->id];
}
void filter_context_clean(filter_context_t *context)
{
p_clear(&context->counters, 1);
context->instance[0] = '\0';
}
void filter_post_async_result(filter_context_t *context,
filter_result_t result)
{
const filter_t *filter = context->current_filter;
const filter_hook_t *hook = NULL;
if (result == HTK_ASYNC) {
return;
}
filter_running_g--;
hook = filter_hook_for_result(filter, result);
_G.async_handler(context, hook);
}
void filter_set_explanation(filter_context_t *context, const char* str,
ssize_t len)
{
context->explanation.str = str;
context->explanation.len = len >= 0 ? len : m_strlen(str);
}
void filter_post_async_result_with_explanation(filter_context_t *context,
filter_result_t result,
const char* str, ssize_t len)
{
filter_set_explanation(context, str, len);
filter_post_async_result(context, result);
}
/* vim:set et sw=4 sts=4 sws=4: */