+static inline bool greylist_check_awlentry(const greylist_config_t *config,
+ struct awl_entry *aent, time_t now)
+{
+ return !(config->max_age > 0 && now - aent->last > config->max_age);
+}
+
+static inline bool greylist_check_object(const greylist_config_t *config,
+ const struct obj_entry *oent, time_t now)
+{
+ return !((config->max_age > 0 && now - oent->last > config->max_age)
+ || (oent->last - oent->first < config->delay
+ && now - oent->last > config->retry_window));
+}
+
+typedef bool (*db_entry_checker_t)(const greylist_config_t *, const void *, time_t);
+
+static TCBDB *greylist_db_get(const greylist_config_t *config,
+ const char *path, bool cleanup,
+ size_t entry_len, db_entry_checker_t check)
+{
+ TCBDB *awl_db, *tmp_db;
+ time_t now = time(NULL);
+
+ /* Rebuild a new database after removing too old entries.
+ */
+ if (cleanup && config->max_age > 0) {
+ uint32_t old_count = 0;
+ uint32_t new_count = 0;
+ bool replace = false;
+ bool trashable = false;
+ char tmppath[PATH_MAX];
+ snprintf(tmppath, PATH_MAX, "%s.tmp", path);
+
+ info("database cleanup started");
+ awl_db = tcbdbnew();
+ if (tcbdbopen(awl_db, path, BDBOREADER)) {
+ tmp_db = tcbdbnew();
+ if (tcbdbopen(tmp_db, tmppath, BDBOWRITER | BDBOCREAT | BDBOTRUNC)) {
+ BDBCUR *cur = tcbdbcurnew(awl_db);
+ TCXSTR *key, *value;
+
+ key = tcxstrnew();
+ value = tcxstrnew();
+ if (tcbdbcurfirst(cur)) {
+ replace = true;
+ do {
+ tcxstrclear(key);
+ tcxstrclear(value);
+ (void)tcbdbcurrec(cur, key, value);
+
+ if ((size_t)tcxstrsize(value) == entry_len
+ && check(config, tcxstrptr(value), now)) {
+ tcbdbput(tmp_db, tcxstrptr(key), tcxstrsize(key),
+ tcxstrptr(value), entry_len);
+ ++new_count;
+ }
+ ++old_count;
+ } while (tcbdbcurnext(cur));
+ }
+ tcxstrdel(key);
+ tcxstrdel(value);
+ tcbdbcurdel(cur);
+ tcbdbsync(tmp_db);
+ } else {
+ warn("cannot run database cleanup: can't open destination database: %s",
+ tcbdberrmsg(tcbdbecode(awl_db)));
+ }
+ tcbdbdel(tmp_db);
+ } else {
+ int ecode = tcbdbecode(awl_db);
+ warn("can not open database: %s", tcbdberrmsg(ecode));
+ trashable = ecode != TCENOPERM && ecode != TCEOPEN && ecode != TCENOFILE && ecode != TCESUCCESS;
+ }
+ tcbdbdel(awl_db);
+
+ /** Cleanup successful, replace the old database with the new one.
+ */
+ if (trashable) {
+ info("database cleanup finished: database was corrupted, create a new one");
+ unlink(path);
+ } else if (replace) {
+ info("database cleanup finished: before %u entries, after %d entries",
+ old_count, new_count);
+ unlink(path);
+ if (rename(tmppath, path) != 0) {
+ UNIXERR("rename");
+ return NULL;
+ }
+ } else {
+ info("database cleanup finished: nothing to do, %u entries", new_count);
+ }
+ }
+
+ /* Effectively open the database.
+ */
+ awl_db = tcbdbnew();
+ if (!tcbdbopen(awl_db, path, BDBOWRITER | BDBOCREAT)) {
+ err("can not open database: %s", tcbdberrmsg(tcbdbecode(awl_db)));
+ tcbdbdel(awl_db);
+ return NULL;
+ }
+ return awl_db;
+}
+
+
+static bool greylist_initialize(greylist_config_t *config,
+ const char *directory, const char *prefix)