X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=greylist.c;h=7008105dcda6e70fb36ab8ce744005daa3e1d2c2;hb=26fccb93b1256eb435e16e5c4646ec8ed4116b71;hp=9f4895aa485bb14e0e14dcf95cc65396f620973c;hpb=cf48fad99532b7590b8bfea416532a7a84910bdf;p=apps%2Fpfixtools.git 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;