1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
21 /* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
22 /* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
23 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
24 /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
25 /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
26 /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
27 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
28 /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
29 /* THE POSSIBILITY OF SUCH DAMAGE. */
30 /******************************************************************************/
33 * Copyright © 2008 Florent Bruneau
41 #include "policy_tokens.h"
43 typedef struct strlist_config_t {
57 unsigned is_hostname :1;
59 unsigned match_sender :1;
60 unsigned match_recipient :1;
62 unsigned match_helo :1;
63 unsigned match_client :1;
64 unsigned match_reverse :1;
67 typedef struct strlist_async_data_t {
68 A(rbl_result_t) results;
72 } strlist_async_data_t;
74 static filter_type_t filter_type = FTK_UNKNOWN;
77 static strlist_config_t *strlist_config_new(void)
79 return p_new(strlist_config_t, 1);
82 static void strlist_config_delete(strlist_config_t **config)
85 array_deep_wipe((*config)->tries, trie_delete);
86 array_wipe((*config)->weights);
87 array_wipe((*config)->reverses);
88 array_wipe((*config)->partiales);
89 array_wipe((*config)->hosts);
90 array_wipe((*config)->host_offsets);
91 array_wipe((*config)->host_weights);
96 static inline void strlist_copy(char *dest, const char *str, ssize_t str_len,
101 for (const char *src = str + str_len - 1 ; src >= str ; --src) {
102 *dest = ascii_tolower(*src);
106 for (int i = 0 ; i < str_len ; ++i) {
107 *dest = ascii_tolower(str[i]);
116 static trie_t *strlist_create(const char *file, bool reverse, bool lock)
123 if (!file_map_open(&map, file, false)) {
128 while (end > p && end[-1] != '\n') {
131 if (end != map.end) {
132 warn("file %s miss a final \\n, ignoring last line",
137 while (p < end && p != NULL) {
138 const char *eol = (char *)memchr(p, '\n', end - p);
142 if (eol - p >= BUFSIZ) {
143 err("unreasonnable long line");
144 file_map_close(&map);
149 const char *eos = eol;
150 while (p < eos && isspace(*p)) {
153 while (p < eos && isspace(eos[-1])) {
157 strlist_copy(line, p, eos - p, reverse);
158 trie_insert(db, line);
163 file_map_close(&map);
164 trie_compile(db, lock);
168 static bool strlist_create_from_rhbl(const char *file, bool lock,
169 trie_t **phosts, trie_t **pdomains)
171 trie_t *hosts, *domains;
172 uint32_t host_count, domain_count;
177 if (!file_map_open(&map, file, false)) {
182 while (end > p && end[-1] != '\n') {
185 if (end != map.end) {
186 warn("file %s miss a final \\n, ignoring last line",
192 domains = trie_new();
194 while (p < end && p != NULL) {
195 const char *eol = (char *)memchr(p, '\n', end - p);
199 if (eol - p >= BUFSIZ) {
200 err("unreasonnable long line");
201 file_map_close(&map);
203 trie_delete(&domains);
207 const char *eos = eol;
208 while (p < eos && isspace(*p)) {
211 while (p < eos && isspace(eos[-1])) {
216 strlist_copy(line, p, eos - p, true);
217 trie_insert(hosts, line);
219 } else if (*p == '*') {
221 strlist_copy(line, p, eos - p, true);
222 trie_insert(domains, line);
229 file_map_close(&map);
230 if (host_count > 0) {
231 trie_compile(hosts, lock);
237 if (domain_count > 0) {
238 trie_compile(domains, lock);
241 trie_delete(&domains);
244 return hosts != NULL || domains != NULL;
249 static bool strlist_filter_constructor(filter_t *filter)
251 strlist_config_t *config = strlist_config_new();
253 #define PARSE_CHECK(Expr, Str, ...) \
255 err(Str, ##__VA_ARGS__); \
256 strlist_config_delete(&config); \
260 config->hard_threshold = 1;
261 config->soft_threshold = 1;
262 foreach (filter_param_t *param, filter->params) {
263 switch (param->type) {
264 /* file parameter is:
265 * [no]lock:(partial-)(prefix|suffix):weight:filename
267 * - lock: memlock the database in memory.
268 * - nolock: don't memlock the database in memory.
269 * - prefix: perform "prefix" compression on storage.
270 * - suffix perform "suffix" compression on storage.
271 * - \d+: a number describing the weight to give to the match
272 * the given list [mandatory]
273 * the file pointed by filename MUST be a valid string list (one string per
274 * line, empty lines and lines beginning with a '#' are ignored).
279 bool reverse = false;
280 bool partial = false;
282 const char *current = param->value;
283 const char *p = m_strchrnul(param->value, ':');
285 for (int i = 0 ; i < 4 ; ++i) {
286 PARSE_CHECK(i == 3 || *p,
287 "file parameter must contains a locking state "
288 "and a weight option");
291 if ((p - current) == 4 && strncmp(current, "lock", 4) == 0) {
293 } else if ((p - current) == 6 && strncmp(current, "nolock", 6) == 0) {
296 PARSE_CHECK(false, "illegal locking state %.*s",
297 (int)(p - current), current);
302 if (p - current > (ssize_t)strlen("partial-")
303 && strncmp(current, "partial-", strlen("partial-")) == 0) {
305 current += strlen("partial-");
307 if ((p - current) == 6 && strncmp(current, "suffix", 6) == 0) {
309 } else if ((p - current) == 6 && strncmp(current, "prefix", 6) == 0) {
312 PARSE_CHECK(false, "illegal character order value %.*s",
313 (int)(p - current), current);
318 weight = strtol(current, &next, 10);
319 PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
320 "illegal weight value %.*s",
321 (int)(p - current), current);
325 trie = strlist_create(current, reverse, lock);
326 PARSE_CHECK(trie != NULL,
327 "cannot load string list from %s", current);
328 array_add(config->tries, trie);
329 array_add(config->weights, weight);
330 array_add(config->reverses, reverse);
331 array_add(config->partiales, partial);
336 p = m_strchrnul(current, ':');
341 /* rbldns parameter is:
342 * [no]lock::weight:filename
344 * - lock: memlock the database in memory.
345 * - nolock: don't memlock the database in memory.
346 * - \d+: a number describing the weight to give to the match
347 * the given list [mandatory]
348 * directly import a file issued from a rhbl in rbldns format.
353 trie_t *trie_hosts = NULL;
354 trie_t *trie_domains = NULL;
355 const char *current = param->value;
356 const char *p = m_strchrnul(param->value, ':');
358 for (int i = 0 ; i < 3 ; ++i) {
359 PARSE_CHECK(i == 2 || *p,
360 "file parameter must contains a locking state "
361 "and a weight option");
364 if ((p - current) == 4 && strncmp(current, "lock", 4) == 0) {
366 } else if ((p - current) == 6 && strncmp(current, "nolock", 6) == 0) {
369 PARSE_CHECK(false, "illegal locking state %.*s",
370 (int)(p - current), current);
375 weight = strtol(current, &next, 10);
376 PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
377 "illegal weight value %.*s",
378 (int)(p - current), current);
382 PARSE_CHECK(strlist_create_from_rhbl(current, lock,
383 &trie_hosts, &trie_domains),
384 "cannot load string list from rhbl %s", current);
385 if (trie_hosts != NULL) {
386 array_add(config->tries, trie_hosts);
387 array_add(config->weights, weight);
388 array_add(config->reverses, true);
389 array_add(config->partiales, false);
391 if (trie_domains != NULL) {
392 array_add(config->tries, trie_domains);
393 array_add(config->weights, weight);
394 array_add(config->reverses, true);
395 array_add(config->partiales, true);
397 config->is_hostname = true;
402 p = m_strchrnul(current, ':');
409 * define a RBL to use through DNS resolution.
413 const char *current = param->value;
414 const char *p = m_strchrnul(param->value, ':');
416 for (int i = 0 ; i < 2 ; ++i) {
417 PARSE_CHECK(i == 1 || *p,
418 "host parameter must contains a weight option");
421 weight = strtol(current, &next, 10);
422 PARSE_CHECK(next == p && weight >= 0 && weight <= 1024,
423 "illegal weight value %.*s",
424 (int)(p - current), current);
428 array_add(config->host_offsets, array_len(config->hosts));
429 array_append(config->hosts, current, strlen(current) + 1);
430 array_add(config->host_weights, weight);
435 p = m_strchrnul(current, ':');
440 /* hard_threshold parameter is an integer.
441 * If the matching score is greater or equal than this threshold,
442 * the hook "hard_match" is called.
443 * hard_threshold = 1 means, that all matches are hard matches.
446 FILTER_PARAM_PARSE_INT(HARD_THRESHOLD, config->hard_threshold);
448 /* soft_threshold parameter is an integer.
449 * if the matching score is greater or equal than this threshold
450 * and smaller or equal than the hard_threshold, the hook "soft_match"
454 FILTER_PARAM_PARSE_INT(SOFT_THRESHOLD, config->soft_threshold);
456 /* fields to match againes:
457 * fields = field_name(,field_name)*
459 * - hostname: helo_name,client_name,reverse_client_name
460 * - email: sender,recipient
463 const char *current = param->value;
464 const char *p = m_strchrnul(param->value, ',');
466 postlicyd_token tok = policy_tokenize(current, p - current);
468 #define CASE(Up, Low, Type) \
470 config->match_ ## Low = true; \
471 config->is_ ## Type = true; \
473 CASE(HELO_NAME, helo, hostname);
474 CASE(CLIENT_NAME, client, hostname);
475 CASE(REVERSE_CLIENT_NAME, reverse, hostname);
476 CASE(SENDER_DOMAIN, sender, hostname);
477 CASE(RECIPIENT_DOMAIN, recipient, hostname);
478 CASE(SENDER, sender, email);
479 CASE(RECIPIENT, recipient, email);
482 PARSE_CHECK(false, "unknown field %.*s", (int)(p - current), current);
489 p = m_strchrnul(current, ',');
497 PARSE_CHECK(config->is_email != config->is_hostname,
498 "matched field MUST be emails XOR hostnames");
499 PARSE_CHECK(config->tries.len || config->host_offsets.len,
500 "no file parameter in the filter %s", filter->name);
501 filter->data = config;
505 static void strlist_filter_destructor(filter_t *filter)
507 strlist_config_t *config = filter->data;
508 strlist_config_delete(&config);
509 filter->data = config;
512 static void strlist_filter_async(rbl_result_t *result, void *arg)
514 filter_context_t *context = arg;
515 const filter_t *filter = context->current_filter;
516 const strlist_config_t *data = filter->data;
517 strlist_async_data_t *async = context->contexts[filter_type];
519 if (*result != RBL_ERROR) {
520 async->error = false;
524 debug("got asynchronous request result for filter %s, rbl %d, still awaiting %d answers",
525 filter->name, result - array_ptr(async->results, 0), async->awaited);
527 if (async->awaited == 0) {
528 filter_result_t res = HTK_FAIL;
533 #define DO_SUM(Field) \
534 if (data->match_ ## Field) { \
535 for (uint32_t i = 0 ; i < array_len(data->host_offsets) ; ++i) { \
536 int weight = array_elt(data->host_weights, i); \
538 switch (array_elt(async->results, j)) { \
540 crit("no more awaited answer but result is ASYNC"); \
543 async->sum += weight; \
557 debug("score is %d", async->sum);
558 if (async->sum >= (uint32_t)data->hard_threshold) {
559 res = HTK_HARD_MATCH;
560 } else if (async->sum >= (uint32_t)data->soft_threshold) {
561 res = HTK_SOFT_MATCH;
564 debug("answering to filter %s", filter->name);
565 filter_post_async_result(context, res);
570 static filter_result_t strlist_filter(const filter_t *filter, const query_t *query,
571 filter_context_t *context)
573 char reverse[BUFSIZ];
575 const strlist_config_t *config = filter->data;
576 strlist_async_data_t *async = context->contexts[filter_type];
580 array_ensure_exact_capacity(async->results, (config->match_client
581 + config->match_sender + config->match_helo
582 + config->match_recipient + config->match_reverse)
583 * array_len(config->host_offsets));
587 if (config->is_email &&
588 ((config->match_sender && query->state < SMTP_MAIL)
589 || (config->match_recipient && query->state != SMTP_RCPT))) {
590 warn("trying to match an email against a field that is not "
591 "available in current protocol state");
593 } else if (config->is_hostname && config->match_helo && query->state < SMTP_HELO) {
594 warn("trying to match hostname against helo before helo is received");
597 #define LOOKUP(Flag, Field) \
598 if (config->match_ ## Flag) { \
599 const int len = m_strlen(query->Field); \
600 strlist_copy(normal, query->Field, len, false); \
601 strlist_copy(reverse, query->Field, len, true); \
602 for (uint32_t i = 0 ; i < config->tries.len ; ++i) { \
603 const int weight = array_elt(config->weights, i); \
604 const trie_t *trie = array_elt(config->tries, i); \
605 const bool rev = array_elt(config->reverses, i); \
606 const bool part = array_elt(config->partiales, i); \
607 if ((!part && trie_lookup(trie, rev ? reverse : normal)) \
608 || (part && trie_prefix(trie, rev ? reverse : normal))) { \
609 async->sum += weight; \
610 if (async->sum >= (uint32_t)config->hard_threshold) { \
611 return HTK_HARD_MATCH; \
614 async->error = false; \
617 #define DNS(Flag, Field) \
618 if (config->match_ ## Flag) { \
619 const int len = m_strlen(query->Field); \
620 strlist_copy(normal, query->Field, len, false); \
621 for (uint32_t i = 0 ; len > 0 && i < config->host_offsets.len ; ++i) { \
622 const char *rbl = array_ptr(config->hosts, \
623 array_elt(config->host_offsets, i)); \
624 if (rhbl_check(normal, rbl, array_ptr(async->results, result_pos), \
625 strlist_filter_async, context)) { \
626 async->error = false; \
633 if (config->is_email) {
634 LOOKUP(sender, sender);
635 LOOKUP(recipient, recipient);
637 DNS(recipient, recipient);
638 } else if (config->is_hostname) {
639 LOOKUP(helo, helo_name);
640 LOOKUP(client, client_name);
641 LOOKUP(reverse, reverse_client_name);
642 LOOKUP(recipient, recipient_domain);
643 LOOKUP(sender, sender_domain);
644 DNS(helo, helo_name);
645 DNS(client, client_name);
646 DNS(reverse, reverse_client_name);
647 DNS(recipient, recipient_domain);
648 DNS(sender, sender_domain);
652 if (async->awaited > 0) {
656 err("filter %s: all the rbls returned an error", filter->name);
659 if (async->sum >= (uint32_t)config->hard_threshold) {
660 return HTK_HARD_MATCH;
661 } else if (async->sum >= (uint32_t)config->soft_threshold) {
662 return HTK_SOFT_MATCH;
668 static void *strlist_context_constructor(void)
670 return p_new(strlist_async_data_t, 1);
673 static void strlist_context_destructor(void *data)
675 strlist_async_data_t *ctx = data;
676 array_wipe(ctx->results);
680 static int strlist_init(void)
682 filter_type = filter_register("strlist", strlist_filter_constructor,
683 strlist_filter_destructor, strlist_filter,
684 strlist_context_constructor,
685 strlist_context_destructor);
688 (void)filter_hook_register(filter_type, "abort");
689 (void)filter_hook_register(filter_type, "error");
690 (void)filter_hook_register(filter_type, "fail");
691 (void)filter_hook_register(filter_type, "hard_match");
692 (void)filter_hook_register(filter_type, "soft_match");
696 (void)filter_param_register(filter_type, "file");
697 (void)filter_param_register(filter_type, "rbldns");
698 (void)filter_param_register(filter_type, "dns");
699 (void)filter_param_register(filter_type, "hard_threshold");
700 (void)filter_param_register(filter_type, "soft_threshold");
701 (void)filter_param_register(filter_type, "fields");
704 module_init(strlist_init);