From 1c094155986f54a1a6faf3032e9d94b14dde8dd4 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 3 Jun 2009 17:43:53 +0200 Subject: fixed sha1 calc, removed tmpdir, create spam and ham dirs fixed the calc of the sha1 hash (many of by 1s) include -lssl in the cflags removed unused tmpdir create spamdir and hamdir before writing mails to it --- Makefile | 3 ++ trainstore.c | 101 +++++++++++++++++++++++++++-------------------------------- 2 files changed, 49 insertions(+), 55 deletions(-) diff --git a/Makefile b/Makefile index a16deed..bcffd34 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,9 @@ endif ifeq ("$(BACKEND)", "crm114-exec") objs += signature.o endif +ifeq ("$(BACKEND)", "trainstore") +CFLAGS += -lssl +endif # main make rules CFLAGS += -fPIC -shared -Wall -Wextra -DPLUGINNAME=$(PLUGINNAME) diff --git a/trainstore.c b/trainstore.c index 5cb7cf5..055828d 100644 --- a/trainstore.c +++ b/trainstore.c @@ -23,6 +23,9 @@ #include #include #include +#include +#include +#include #include "lib.h" #include "dict.h" @@ -34,75 +37,69 @@ static const char *spamdir = NULL; static const char *hamdir = NULL; -static const char *tmpdir = NULL; struct antispam_transaction_context { int count; - char *tmpdir; - int tmplen; + const char *spamdir; + const char *hamdir; }; -struct antispam_transaction_context * -backend_start(struct mailbox *box __attr_unused__) -{ - struct antispam_transaction_context *ast; - char *tmp; +int mkdir_rec(const char *dir, mode_t mask) { + char *parent; + struct stat sb; + int status; - ast = i_new(struct antispam_transaction_context, 1); - ast->count = 0; + if (stat(dir, &sb) == 0) { + if (! S_ISDIR(sb.st_mode)) { + return -1; + } + + return 0; + } - tmp = i_strconcat(tmp, "/antispam-mail-XXXXXX", NULL); + t_push(); - ast->tmpdir = mkdtemp(tmp); - if (!ast->tmpdir) - i_free(tmp); - else - ast->tmplen = strlen(ast->tmpdir); + parent = t_malloc(strlen(dir) + 1); + strcpy(parent, dir); - return ast; + if ((status = mkdir_rec(dirname(parent), mask)) == 0) { + mkdir(dir, mask); + + status = 0; + } + + t_pop(); + + return status; } -static void clear_tmpdir(struct antispam_transaction_context *ast) +struct antispam_transaction_context * +backend_start(struct mailbox *box __attr_unused__) { - char *buf; + struct antispam_transaction_context *ast; - t_push(); + ast = i_new(struct antispam_transaction_context, 1); + ast->count = 0; - buf = t_malloc(20 + ast->tmplen); + /* Create spam and hamdir */ + mkdir_rec(spamdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + mkdir_rec(hamdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - while (ast->count > 0) { - ast->count--; - i_snprintf(buf, 20 + ast->tmplen - 1, "%s/%d", - ast->tmpdir, ast->count); - unlink(buf); - } - rmdir(ast->tmpdir); + ast->spamdir = spamdir; + ast->hamdir = hamdir; - t_pop(); + return ast; } void backend_rollback(struct antispam_transaction_context *ast) { - if (ast->tmpdir) { - /* clear it! */ - clear_tmpdir(ast); - i_free(ast->tmpdir); - } - i_free(ast); } int backend_commit(struct mailbox_transaction_context *ctx __attr_unused__, struct antispam_transaction_context *ast) { - if (!ast->tmpdir) { - i_free(ast); - return 0; - } - - i_free(ast->tmpdir); i_free(ast); - return 0; } @@ -114,14 +111,14 @@ static char *create_unique_mailname(const char *date, const char *mail_id, char t_push(); unique_string = t_malloc(strlen(date) + strlen(mail_id) + 1); - i_snprintf(unique_string, strlen(date) + strlen(mail_id), "%s%s", date, mail_id); + i_snprintf(unique_string, strlen(date) + strlen(mail_id) + 1, "%s%s", date, mail_id); sha_bytes = t_malloc(20); sha_bytes = SHA1((unsigned char*)unique_string, strlen(date) + strlen(mail_id), sha_bytes); int i; for (i = 0; i < 20; i++) { - i_snprintf(sha1+(i*2), 2, "%hhx", sha_bytes[i]); + i_snprintf(sha1+(i*2), 3, "%02hhx", sha_bytes[i]); } sha1[40] = 0; @@ -145,7 +142,7 @@ int backend_handle_mail(struct mailbox_transaction_context *t, const char *date = NULL, *mail_id = NULL, *dest = NULL, *other = NULL; struct stat sb; - if (!hamdir || !spamdir) { + if (!ast->hamdir || !ast->spamdir) { mail_storage_set_error(t->box->storage, ME(NOTPOSSIBLE) "antispam plugin not configured"); @@ -189,12 +186,12 @@ int backend_handle_mail(struct mailbox_transaction_context *t, /* switch weather this should be ham or spam */ switch (wanted) { case CLASS_SPAM: - dest = spamdir; - other = hamdir; + dest = ast->spamdir; + other = ast->hamdir; break; case CLASS_NOTSPAM: - dest = hamdir; - other = spamdir; + dest = ast->hamdir; + other = ast->spamdir; break; } @@ -312,12 +309,6 @@ void backend_init(pool_t pool __attr_unused__) hamdir = tmp; debug("mail backend not-spam directory %s\n", tmp); } - - tmp = get_setting("DIR_TMP"); - if (tmp) { - tmpdir = tmp; - debug("mail backend temporary directory %s\n", tmp); - } } void backend_exit(void) -- cgit v1.2.3