Add prefix and suffix matching for strlist filter.
[apps/pfixtools.git] / postlicyd / greylist.c
index e80a9df..53bcf33 100644 (file)
@@ -97,6 +97,7 @@ static TCBDB *greylist_db_get(const greylist_config_t *config,
         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);
 
@@ -104,9 +105,10 @@ static TCBDB *greylist_db_get(const greylist_config_t *config,
         awl_db = tcbdbnew();
         if (tcbdbopen(awl_db, path, BDBOREADER)) {
             tmp_db = tcbdbnew();
-            if (tcbdbopen(tmp_db, tmppath, BDBOWRITER | BDBOTRUNC)) {
+            if (tcbdbopen(tmp_db, tmppath, BDBOWRITER | BDBOCREAT | BDBOTRUNC)) {
                 BDBCUR *cur = tcbdbcurnew(awl_db);
                 TCXSTR *key, *value;
+
                 key = tcxstrnew();
                 value = tcxstrnew();
                 if (tcbdbcurfirst(cur)) {
@@ -129,29 +131,41 @@ static TCBDB *greylist_db_get(const greylist_config_t *config,
                 tcxstrdel(value);
                 tcbdbcurdel(cur);
                 tcbdbsync(tmp_db);
+            } else {
+                syslog(LOG_ERR, "cannot run database cleanup: can't open destination database: %s",
+                       tcbdberrmsg(tcbdbecode(awl_db)));
             }
             tcbdbdel(tmp_db);
+        } else {
+            int ecode = tcbdbecode(awl_db);
+            syslog(LOG_ERR, "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 (replace) {
+        if (trashable) {
+            syslog(LOG_INFO, "database cleanup finished: database was corrupted, create a new one");
+            unlink(path);
+        } else if (replace) {
+            syslog(LOG_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;
             }
-            syslog(LOG_INFO, "database cleanup stat: before %u entries, after %d entries",
-                   old_count, new_count);
+        } else {
+            syslog(LOG_INFO, "database cleanup finished: nothing to do, %u entries", new_count);
         }
-        syslog(LOG_INFO, "database cleanup finished");
     }
 
     /* Effectively open the database.
      */
     awl_db = tcbdbnew();
     if (!tcbdbopen(awl_db, path, BDBOWRITER | BDBOCREAT)) {
+        syslog(LOG_ERR, "can not open database: %s", tcbdberrmsg(tcbdbecode(awl_db)));
         tcbdbdel(awl_db);
         return NULL;
     }
@@ -166,6 +180,7 @@ static bool greylist_initialize(greylist_config_t *config,
 
     if (config->client_awl) {
         snprintf(path, sizeof(path), "%s/%swhitelist.db", directory, prefix);
+        syslog(LOG_INFO, "loading auto-whitelist database");
         config->awl_db = greylist_db_get(config, path, true,
                                          sizeof(struct awl_entry),
                                          (db_entry_checker_t)(greylist_check_awlentry));
@@ -175,6 +190,7 @@ static bool greylist_initialize(greylist_config_t *config,
     }
 
     snprintf(path, sizeof(path), "%s/%sgreylist.db", directory, prefix);
+    syslog(LOG_INFO, "loading greylist database");
     config->obj_db = greylist_db_get(config, path, true,
                                      sizeof(struct obj_entry),
                                      (db_entry_checker_t)(greylist_check_object));