From: Pierre Habouzit Date: Fri, 30 Nov 2007 23:31:46 +0000 (+0100) Subject: greylist initializer. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=26fccb93b1256eb435e16e5c4646ec8ed4116b71;p=apps%2Fpfixtools.git greylist initializer. Signed-off-by: Pierre Habouzit --- diff --git a/greylist.c b/greylist.c index 9f4895a..7008105 100644 --- a/greylist.c +++ b/greylist.c @@ -35,16 +35,17 @@ #include +#include "common.h" #include "greylist.h" #include "str.h" static struct { - int do_awl; - int awl_count; + bool do_awl; bool lookup_by_host; + int awl_limit; int delay; - int retry_window; + int window; TCBDB *awl_db, *obj_db; } cfg; @@ -59,6 +60,50 @@ struct obj_entry { time_t last; }; +int greylist_initialize(const char *directory, const char *prefix) +{ + char path[PATH_MAX]; + + if (cfg.do_awl) { + snprintf(path, sizeof(path), "%s/%swhitelist.db", directory, prefix); + cfg.awl_db = tcbdbnew(); + if (!tcbdbopen(cfg.awl_db, path, BDBOWRITER | BDBOCREAT)) { + tcbdbdel(cfg.awl_db); + cfg.awl_db = NULL; + } + return -1; + } + + snprintf(path, sizeof(path), "%s/%sgreylist.db", directory, prefix); + cfg.obj_db = tcbdbnew(); + if (!tcbdbopen(cfg.obj_db, path, BDBOWRITER | BDBOCREAT)) { + tcbdbdel(cfg.obj_db); + cfg.obj_db = NULL; + if (cfg.awl_db) { + tcbdbdel(cfg.awl_db); + cfg.awl_db = NULL; + } + return -1; + } + + return 0; +} + +static void greylist_shutdown(void) +{ + if (cfg.awl_db) { + tcbdbsync(cfg.awl_db); + tcbdbdel(cfg.awl_db); + cfg.awl_db = NULL; + } + if (cfg.obj_db) { + tcbdbsync(cfg.obj_db); + tcbdbdel(cfg.obj_db); + cfg.obj_db = NULL; + } +} +module_exit(greylist_shutdown); + const char *sender_normalize(const char *sender, char *buf, int len) { const char *at = strchr(sender, '@'); @@ -145,7 +190,7 @@ bool try_greylist(const char *sender, const char *c_addr, if (res && len == sizeof(aent)) { memcpy(&aent, res, len); } - if (aent.count > cfg.awl_count) { + if (aent.count > cfg.awl_limit) { if (now < aent.last + 3600) goto incr_aent; return true; @@ -162,9 +207,7 @@ bool try_greylist(const char *sender, const char *c_addr, memcpy(&oent, res, len); } - if (oent.last - oent.first < cfg.delay - && now - oent.first > cfg.retry_window) - { + if (oent.last - oent.first < cfg.delay && now - oent.first > cfg.window) { oent.first = now; } oent.last = now; diff --git a/greylist.h b/greylist.h index 7086fd6..ac58a97 100644 --- a/greylist.h +++ b/greylist.h @@ -33,5 +33,7 @@ * Copyright © 2007 Pierre Habouzit */ +int greylist_initialize(const char *directory, const char *prefix); + bool try_greylist(const char *sender, const char *c_addr, const char *c_name, const char *rcpt);