Fix build on 64bits.
[apps/pfixtools.git] / postlicyd / greylist.c
index 7d4f282..6bc65db 100644 (file)
@@ -39,6 +39,7 @@
 #include "str.h"
 #include "resources.h"
 
+static const static_str_t static_cleanup = { "@@cleanup@@", 11 };
 
 typedef struct greylist_config_t {
     unsigned lookup_by_host : 1;
@@ -113,7 +114,7 @@ static inline bool greylist_db_need_cleanup(const greylist_config_t *config, TCB
 {
     int len = 0;
     time_t now = time(NULL);
-    const time_t *last_cleanup = tcbdbget3(db, "@@cleanup@@", strlen("@@cleanup@@"), &len);
+    const time_t *last_cleanup = tcbdbget3(db, static_cleanup.str, static_cleanup.len, &len);
     if (last_cleanup == NULL) {
         debug("No last cleanup time");
     } else {
@@ -151,7 +152,7 @@ static TCBDB **greylist_db_get(const greylist_config_t *config, const char *path
         }
     }
     if (!greylist_db_need_cleanup(config, awl_db) || config->max_age <= 0) {
-        info("no cleanup needed");
+        info("%s loaded: no cleanup needed", path);
         res->db = awl_db;
         return &res->db;
     } else {
@@ -193,8 +194,7 @@ static TCBDB **greylist_db_get(const greylist_config_t *config, const char *path
                         }
                         ++old_count;
                     } while (tcbdbcurnext(cur));
-                    now = time(0);
-                    tcbdbput(tmp_db, "@@cleanup@@", strlen("@@cleanup@@"), &now, sizeof(now));
+                    tcbdbput(tmp_db, static_cleanup.str, static_cleanup.len, &now, sizeof(now));
                 }
                 tcxstrdel(key);
                 tcxstrdel(value);
@@ -215,11 +215,11 @@ static TCBDB **greylist_db_get(const greylist_config_t *config, const char *path
         /** Cleanup successful, replace the old database with the new one.
          */
         if (trashable) {
-            info("database cleanup finished: database was corrupted, create a new one");
+            info("%s cleanup: database was corrupted, create a new one", path);
             unlink(path);
         } else if (replace) {
-            info("database cleanup finished: before %u entries, after %d entries",
-                   old_count, new_count);
+            info("%s cleanup: done in %us, before %u, after %u entries",
+                 path, (uint32_t)(time(0) - now), old_count, new_count);
             unlink(path);
             if (rename(tmppath, path) != 0) {
                 UNIXERR("rename");
@@ -228,7 +228,8 @@ static TCBDB **greylist_db_get(const greylist_config_t *config, const char *path
             }
         } else {
             unlink(tmppath);
-            info("database cleanup finished: nothing to do, %u entries", new_count);
+            info("%s cleanup: done in %us, nothing to do, %u entries",
+                 path, (uint32_t)(time(0) - now), old_count);
         }
     }
 
@@ -243,6 +244,7 @@ static TCBDB **greylist_db_get(const greylist_config_t *config, const char *path
         return NULL;
     }
 
+    info("%s loaded", path);
     res->db = awl_db;
     return &res->db;
 }
@@ -255,7 +257,6 @@ static bool greylist_initialize(greylist_config_t *config,
 
     if (config->client_awl) {
         snprintf(path, sizeof(path), "%s/%swhitelist.db", directory, prefix);
-        info("loading auto-whitelist database");
         config->awl_db = greylist_db_get(config, path,
                                          sizeof(struct awl_entry),
                                          (db_entry_checker_t)(greylist_check_awlentry));
@@ -266,7 +267,6 @@ static bool greylist_initialize(greylist_config_t *config,
     }
 
     snprintf(path, sizeof(path), "%s/%sgreylist.db", directory, prefix);
-    info("loading greylist database");
     config->obj_db = greylist_db_get(config, path,
                                      sizeof(struct obj_entry),
                                      (db_entry_checker_t)(greylist_check_object));
@@ -365,15 +365,15 @@ static const char *c_net(const greylist_config_t *config,
 
 
 static bool try_greylist(const greylist_config_t *config,
-                         const char *sender, const char *c_addr,
-                         const char *c_name, const char *rcpt)
+                         const static_str_t *sender, const static_str_t *c_addr,
+                         const static_str_t *c_name, const static_str_t *rcpt)
 {
 #define INCR_AWL                                              \
     aent.count++;                                             \
     aent.last = now;                                          \
     debug("whitelist entry for %.*s updated, count %d",       \
-          c_addrlen, c_addr, aent.count);                     \
-    tcbdbput(awl_db, c_addr, c_addrlen, &aent, sizeof(aent));
+          (int)c_addr->len, c_addr->str, aent.count);         \
+    tcbdbput(awl_db, c_addr->str, c_addr->len, &aent, sizeof(aent));
 
     char sbuf[BUFSIZ], cnet[64], key[BUFSIZ];
     const void *res;
@@ -382,31 +382,31 @@ static bool try_greylist(const greylist_config_t *config,
     struct obj_entry oent = { now, now };
     struct awl_entry aent = { 0, 0 };
 
-    int len, klen, c_addrlen = strlen(c_addr);
+    int len, klen;
     TCBDB * const awl_db = config->awl_db ? *(config->awl_db) : NULL;
     TCBDB * const obj_db = config->obj_db ? *(config->obj_db) : NULL;
 
     /* Auto whitelist clients.
      */
     if (config->client_awl) {
-        res = tcbdbget3(awl_db, c_addr, c_addrlen, &len);
+        res = tcbdbget3(awl_db, c_addr->str, c_addr->len, &len);
         if (res && len == sizeof(aent)) {
             memcpy(&aent, res, len);
             debug("client %.*s has a whitelist entry, count is %d",
-                  c_addrlen, c_addr, aent.count);
+                  (int)c_addr->len, c_addr->str, aent.count);
         }
 
         if (!greylist_check_awlentry(config, &aent, now)) {
             aent.count = 0;
             aent.last  = 0;
             debug("client %.*s whitelist entry too old",
-                  c_addrlen, c_addr);
+                  (int)c_addr->len, c_addr->str);
         }
 
         /* Whitelist if count is enough.
          */
         if (aent.count >= config->client_awl) {
-            debug("client %.*s whitelisted", c_addrlen, c_addr);
+            debug("client %.*s whitelisted", (int)c_addr->len, c_addr->str);
             if (now < aent.last + 3600) {
                 INCR_AWL
             }
@@ -420,9 +420,9 @@ static bool try_greylist(const greylist_config_t *config,
     /* Lookup.
      */
     klen = snprintf(key, sizeof(key), "%s/%s/%s",
-                    c_net(config, c_addr, c_name, cnet, sizeof(cnet)),
-                    config->no_sender ? "" : sender_normalize(sender, sbuf, sizeof(sbuf)),
-                    config->no_recipient ? "" : rcpt);
+                    c_net(config, c_addr->str, c_name->str, cnet, sizeof(cnet)),
+                    config->no_sender ? "" : sender_normalize(sender->str, sbuf, sizeof(sbuf)),
+                    config->no_recipient ? "" : rcpt->str);
     klen = MIN(klen, ssizeof(key) - 1);
 
     res = tcbdbget3(obj_db, key, klen, &len);
@@ -507,8 +507,8 @@ static bool greylist_filter_constructor(filter_t *filter)
 
     foreach (filter_param_t *param, filter->params) {
         switch (param->type) {
-          FILTER_PARAM_PARSE_STRING(PATH,   path);
-          FILTER_PARAM_PARSE_STRING(PREFIX, prefix);
+          FILTER_PARAM_PARSE_STRING(PATH,   path, false);
+          FILTER_PARAM_PARSE_STRING(PREFIX, prefix, false);
           FILTER_PARAM_PARSE_BOOLEAN(LOOKUP_BY_HOST, config->lookup_by_host);
           FILTER_PARAM_PARSE_BOOLEAN(NO_SENDER, config->no_sender);
           FILTER_PARAM_PARSE_BOOLEAN(NO_RECIPIENT, config->no_recipient);
@@ -551,8 +551,8 @@ static filter_result_t greylist_filter(const filter_t *filter,
         return HTK_ABORT;
     }
 
-    return try_greylist(config, query->sender, query->client_address,
-                        query->client_name, query->recipient) ?
+    return try_greylist(config, &query->sender, &query->client_address,
+                        &query->client_name, &query->recipient) ?
            HTK_WHITELIST : HTK_GREYLIST;
 }