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 CONTRIBUTORS ``AS IS'' AND ANY EXPRESS */
20 /* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY */
23 /* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL */
24 /* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS */
25 /* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
26 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, */
27 /* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN */
28 /* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
29 /* POSSIBILITY OF SUCH DAMAGE. */
31 /* Copyright (c) 2006-2008 the Authors */
32 /* see AUTHORS and source files for details */
33 /******************************************************************************/
36 * Copyright © 2007 Pierre Habouzit
43 #include "resources.h"
45 static const static_str_t static_cleanup = { "@@cleanup@@", 11 };
47 typedef struct greylist_config_t {
48 unsigned lookup_by_host : 1;
49 unsigned no_sender : 1;
50 unsigned no_recipient : 1;
63 #define GREYLIST_INIT { .lookup_by_host = false, \
65 .no_recipient = false, \
67 .retry_window = 2 * 24 * 3600, \
69 .max_age = 35 * 3600, \
70 .cleanup_period = 86400, \
71 .awlfilename = NULL, \
73 .objfilename = NULL, \
86 typedef struct greylist_resource_t {
88 } greylist_resource_t;
91 static void greylist_resource_wipe(greylist_resource_t *res)
100 static inline bool greylist_check_awlentry(const greylist_config_t *config,
101 struct awl_entry *aent, time_t now)
103 return !(config->max_age > 0 && now - aent->last > config->max_age);
106 static inline bool greylist_check_object(const greylist_config_t *config,
107 const struct obj_entry *oent, time_t now)
109 return !((config->max_age > 0 && now - oent->last > config->max_age)
110 || (oent->last - oent->first < config->delay
111 && now - oent->last > config->retry_window));
114 typedef bool (*db_entry_checker_t)(const greylist_config_t *, const void *, time_t);
116 static inline bool greylist_db_need_cleanup(const greylist_config_t *config, TCBDB *db)
119 time_t now = time(NULL);
120 const time_t *last_cleanup = tcbdbget3(db, static_cleanup.str, static_cleanup.len, &len);
121 if (last_cleanup == NULL) {
122 debug("No last cleanup time");
124 debug("Last cleanup time %u, (ie %us ago)",
125 (uint32_t)*last_cleanup, (uint32_t)(now - *last_cleanup));
127 return last_cleanup == NULL
128 || len != sizeof(*last_cleanup)
129 || (now - *last_cleanup) >= config->cleanup_period;
132 static TCBDB **greylist_db_get(const greylist_config_t *config, const char *path,
133 size_t entry_len, db_entry_checker_t check)
135 TCBDB *awl_db, *tmp_db;
136 time_t now = time(NULL);
138 greylist_resource_t *res = resource_get("greylist", path);
140 res = p_new(greylist_resource_t, 1);
141 resource_set("greylist", path, res, (resource_destructor_t)greylist_resource_wipe);
144 /* Open the database and check if cleanup is needed
148 if (awl_db == NULL) {
150 if (!tcbdbopen(awl_db, path, BDBOWRITER | BDBOCREAT)) {
151 err("can not open database: %s", tcbdberrmsg(tcbdbecode(awl_db)));
153 resource_release("greylist", path);
157 if (!greylist_db_need_cleanup(config, awl_db) || config->max_age <= 0) {
158 info("%s loaded: no cleanup needed", path);
166 /* Rebuild a new database after removing too old entries.
168 if (config->max_age > 0) {
169 uint32_t old_count = 0;
170 uint32_t new_count = 0;
171 bool replace = false;
172 bool trashable = false;
173 char tmppath[PATH_MAX];
174 snprintf(tmppath, PATH_MAX, "%s.tmp", path);
177 if (tcbdbopen(awl_db, path, BDBOREADER)) {
179 if (tcbdbopen(tmp_db, tmppath, BDBOWRITER | BDBOCREAT | BDBOTRUNC)) {
180 BDBCUR *cur = tcbdbcurnew(awl_db);
185 if (tcbdbcurfirst(cur)) {
190 (void)tcbdbcurrec(cur, key, value);
192 if ((size_t)tcxstrsize(value) == entry_len
193 && check(config, tcxstrptr(value), now)) {
194 tcbdbput(tmp_db, tcxstrptr(key), tcxstrsize(key),
195 tcxstrptr(value), entry_len);
199 } while (tcbdbcurnext(cur));
200 tcbdbput(tmp_db, static_cleanup.str, static_cleanup.len, &now, sizeof(now));
207 warn("cannot run database cleanup: can't open destination database: %s",
208 tcbdberrmsg(tcbdbecode(awl_db)));
212 int ecode = tcbdbecode(awl_db);
213 warn("can not open database: %s", tcbdberrmsg(ecode));
214 trashable = ecode != TCENOPERM && ecode != TCEOPEN && ecode != TCENOFILE && ecode != TCESUCCESS;
218 /** Cleanup successful, replace the old database with the new one.
221 info("%s cleanup: database was corrupted, create a new one", path);
223 } else if (replace) {
224 info("%s cleanup: done in %us, before %u, after %u entries",
225 path, (uint32_t)(time(0) - now), old_count, new_count);
227 if (rename(tmppath, path) != 0) {
229 resource_release("greylist", path);
234 info("%s cleanup: done in %us, nothing to do, %u entries",
235 path, (uint32_t)(time(0) - now), old_count);
239 /* Effectively open the database.
243 if (!tcbdbopen(awl_db, path, BDBOWRITER | BDBOCREAT)) {
244 err("can not open database: %s", tcbdberrmsg(tcbdbecode(awl_db)));
246 resource_release("greylist", path);
250 info("%s loaded", path);
256 static bool greylist_initialize(greylist_config_t *config,
257 const char *directory, const char *prefix)
261 if (config->client_awl) {
262 snprintf(path, sizeof(path), "%s/%swhitelist.db", directory, prefix);
263 config->awl_db = greylist_db_get(config, path,
264 sizeof(struct awl_entry),
265 (db_entry_checker_t)(greylist_check_awlentry));
266 if (config->awl_db == NULL) {
269 config->awlfilename = m_strdup(path);
272 snprintf(path, sizeof(path), "%s/%sgreylist.db", directory, prefix);
273 config->obj_db = greylist_db_get(config, path,
274 sizeof(struct obj_entry),
275 (db_entry_checker_t)(greylist_check_object));
276 if (config->obj_db == NULL) {
277 if (config->awlfilename) {
278 resource_release("greylist", config->awlfilename);
279 p_delete(&config->awlfilename);
283 config->objfilename = m_strdup(path);
288 static void greylist_shutdown(greylist_config_t *config)
290 if (config->awlfilename) {
291 resource_release("greylist", config->awlfilename);
292 p_delete(&config->awlfilename);
294 if (config->objfilename) {
295 resource_release("greylist", config->objfilename);
296 p_delete(&config->objfilename);
300 static const char *sender_normalize(const char *sender, char *buf, int len)
302 const char *at = strchr(sender, '@');
303 int rpos = 0, wpos = 0, userlen;
308 /* strip extension used for VERP or alike */
309 userlen = ((char *)memchr(sender, '+', at - sender) ?: at) - sender;
311 while (rpos < userlen) {
314 while (isdigit(sender[rpos + count]) && rpos + count < userlen)
316 if (count && !isalnum(sender[rpos + count])) {
317 /* replace \<\d+\> with '#' */
318 wpos += m_strputc(buf + wpos, len - wpos, '#');
322 while (isalnum(sender[rpos + count]) && rpos + count < userlen)
324 while (!isalnum(sender[rpos + count]) && rpos + count < userlen)
326 wpos += m_strncpy(buf + wpos, len - wpos, sender + rpos, count);
330 wpos += m_strputc(buf + wpos, len - wpos, '#');
331 wpos += m_strcpy(buf + wpos, len - wpos, at + 1);
335 static const char *c_net(const greylist_config_t *config,
336 const char *c_addr, const char *c_name,
337 char *cnet, int cnetlen)
342 if (config->lookup_by_host)
345 if (!(dot = strchr(c_addr, '.')))
347 if (!(dot = strchr(dot + 1, '.')))
351 if (!(dot = strchr(dot, '.')) || dot - p > 3)
353 m_strncpy(ip2, sizeof(ip2), p, dot - p);
356 if (!(dot = strchr(dot, '.')) || dot - p > 3)
358 m_strncpy(ip3, sizeof(ip3), p, dot - p);
360 /* skip if contains the last two ip numbers in the hostname,
361 we assume it's a pool of dialup of a provider */
362 if (strstr(c_name, ip2) && strstr(c_name, ip3))
365 m_strncpy(cnet, cnetlen, c_addr, dot - c_addr);
370 static bool try_greylist(const greylist_config_t *config,
371 const static_str_t *sender, const static_str_t *c_addr,
372 const static_str_t *c_name, const static_str_t *rcpt)
377 debug("whitelist entry for %.*s updated, count %d", \
378 (int)c_addr->len, c_addr->str, aent.count); \
379 tcbdbput(awl_db, c_addr->str, c_addr->len, &aent, sizeof(aent));
381 char sbuf[BUFSIZ], cnet[64], key[BUFSIZ];
384 time_t now = time(NULL);
385 struct obj_entry oent = { now, now };
386 struct awl_entry aent = { 0, 0 };
389 TCBDB * const awl_db = config->awl_db ? *(config->awl_db) : NULL;
390 TCBDB * const obj_db = config->obj_db ? *(config->obj_db) : NULL;
392 /* Auto whitelist clients.
394 if (config->client_awl) {
395 res = tcbdbget3(awl_db, c_addr->str, c_addr->len, &len);
396 if (res && len == sizeof(aent)) {
397 memcpy(&aent, res, len);
398 debug("client %.*s has a whitelist entry, count is %d",
399 (int)c_addr->len, c_addr->str, aent.count);
402 if (!greylist_check_awlentry(config, &aent, now)) {
405 debug("client %.*s whitelist entry too old",
406 (int)c_addr->len, c_addr->str);
409 /* Whitelist if count is enough.
411 if (aent.count >= config->client_awl) {
412 debug("client %.*s whitelisted", (int)c_addr->len, c_addr->str);
413 if (now < aent.last + 3600) {
425 klen = snprintf(key, sizeof(key), "%s/%s/%s",
426 c_net(config, c_addr->str, c_name->str, cnet, sizeof(cnet)),
427 config->no_sender ? "" : sender_normalize(sender->str, sbuf, sizeof(sbuf)),
428 config->no_recipient ? "" : rcpt->str);
429 klen = MIN(klen, ssizeof(key) - 1);
431 res = tcbdbget3(obj_db, key, klen, &len);
432 if (res && len == sizeof(oent)) {
433 memcpy(&oent, res, len);
434 debug("found a greylist entry for %.*s", klen, key);
437 /* Discard stored first-seen if it is the first retrial and
438 * it is beyong the retry window and too old entries.
440 if (!greylist_check_object(config, &oent, now)) {
442 debug("invalid retry for %.*s: %s", klen, key,
443 (config->max_age > 0 && now - oent.last > config->max_age) ?
445 : (oent.last - oent.first < config->delay ?
446 "retry too early" : "retry too late" ));
452 tcbdbput(obj_db, key, klen, &oent, sizeof(oent));
454 /* Auto whitelist clients:
456 * - on successful entry in the greylist db of a triplet:
457 * - client not whitelisted yet ? -> increase count
458 * -> withelist if count > limit
459 * - client whitelisted already ? -> update last-seen timestamp.
461 if (oent.first + config->delay < now) {
462 debug("valid retry for %.*s", klen, key);
463 if (config->client_awl) {
478 /* postlicyd filter declaration */
482 static greylist_config_t *greylist_config_new(void)
484 const greylist_config_t g = GREYLIST_INIT;
485 greylist_config_t *config = p_new(greylist_config_t, 1);
490 static void greylist_config_delete(greylist_config_t **config)
493 greylist_shutdown(*config);
498 static bool greylist_filter_constructor(filter_t *filter)
500 const char* path = NULL;
501 const char* prefix = NULL;
502 greylist_config_t *config = greylist_config_new();
504 #define PARSE_CHECK(Expr, Str, ...) \
506 err(Str, ##__VA_ARGS__); \
507 greylist_config_delete(&config); \
511 foreach (filter_param_t *param, filter->params) {
512 switch (param->type) {
513 FILTER_PARAM_PARSE_STRING(PATH, path, false);
514 FILTER_PARAM_PARSE_STRING(PREFIX, prefix, false);
515 FILTER_PARAM_PARSE_BOOLEAN(LOOKUP_BY_HOST, config->lookup_by_host);
516 FILTER_PARAM_PARSE_BOOLEAN(NO_SENDER, config->no_sender);
517 FILTER_PARAM_PARSE_BOOLEAN(NO_RECIPIENT, config->no_recipient);
518 FILTER_PARAM_PARSE_INT(RETRY_WINDOW, config->retry_window);
519 FILTER_PARAM_PARSE_INT(CLIENT_AWL, config->client_awl);
520 FILTER_PARAM_PARSE_INT(DELAY, config->delay);
521 FILTER_PARAM_PARSE_INT(MAX_AGE, config->max_age);
522 FILTER_PARAM_PARSE_INT(CLEANUP_PERIOD, config->cleanup_period);
528 PARSE_CHECK(path, "path to greylist db not given");
529 PARSE_CHECK(greylist_initialize(config, path, prefix ? prefix : ""),
530 "can not load greylist database");
532 filter->data = config;
536 static void greylist_filter_destructor(filter_t *filter)
538 greylist_config_t *data = filter->data;
539 greylist_config_delete(&data);
543 static filter_result_t greylist_filter(const filter_t *filter,
544 const query_t *query,
545 filter_context_t *context)
547 const greylist_config_t *config = filter->data;
548 if (!config->no_recipient && query->state != SMTP_RCPT) {
549 warn("greylisting on recipient only works as smtpd_recipient_restrictions");
552 if (!config->no_sender && query->state < SMTP_MAIL) {
553 warn("greylisting on sender must be performed after (or at) MAIL TO");
557 return try_greylist(config, &query->sender, &query->client_address,
558 &query->client_name, &query->recipient) ?
559 HTK_WHITELIST : HTK_GREYLIST;
562 static int greylist_init(void)
564 filter_type_t type = filter_register("greylist", greylist_filter_constructor,
565 greylist_filter_destructor,
566 greylist_filter, NULL, NULL);
569 (void)filter_hook_register(type, "abort");
570 (void)filter_hook_register(type, "error");
571 (void)filter_hook_register(type, "greylist");
572 (void)filter_hook_register(type, "whitelist");
576 (void)filter_param_register(type, "lookup_by_host");
577 (void)filter_param_register(type, "no_sender");
578 (void)filter_param_register(type, "no_recipient");
579 (void)filter_param_register(type, "delay");
580 (void)filter_param_register(type, "retry_window");
581 (void)filter_param_register(type, "client_awl");
582 (void)filter_param_register(type, "max_age");
583 (void)filter_param_register(type, "cleanup_period");
584 (void)filter_param_register(type, "path");
585 (void)filter_param_register(type, "prefix");
588 module_init(greylist_init)