aboutsummaryrefslogtreecommitdiffstats
path: root/pipe.c
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2011-05-03 19:26:38 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2011-05-03 19:26:38 +0200
commita1527ce7177df11a8ee1b46a0e7b64f0f0df234b (patch)
tree41f724c2a04b3582878d40dc451930e71a7b86aa /pipe.c
parentc8a9a6550b84412f919b772a9cb8db1e3ef79d19 (diff)
parent808eaa49efe2dc2438ef98ddf4bac4cfd2898a3d (diff)
downloaddovecot-antispam-master.tar.gz
dovecot-antispam-master.tar.xz
dovecot-antispam-master.zip
merge with base/masterHEADmaster
Diffstat (limited to '')
-rw-r--r--pipe.c (renamed from mailtrain.c)95
1 files changed, 58 insertions, 37 deletions
diff --git a/mailtrain.c b/pipe.c
index 41071bc..c0853d6 100644
--- a/mailtrain.c
+++ b/pipe.c
@@ -30,14 +30,14 @@
#include "antispam-plugin.h"
-static const char *spamaddr = NULL;
-static const char *hamaddr = NULL;
-static const char *sendmail_binary = "/usr/sbin/sendmail";
+static const char *spam_arg = NULL;
+static const char *ham_arg = NULL;
+static const char *pipe_binary = "/usr/sbin/sendmail";
static const char *tmpdir = "/tmp";
static char **extra_args = NULL;
static int extra_args_num = 0;
-static int run_sendmail(int mailfd, enum classification wanted)
+static int run_pipe(int mailfd, enum classification wanted)
{
const char *dest;
pid_t pid;
@@ -45,10 +45,10 @@ static int run_sendmail(int mailfd, enum classification wanted)
switch (wanted) {
case CLASS_SPAM:
- dest = spamaddr;
+ dest = spam_arg;
break;
case CLASS_NOTSPAM:
- dest = hamaddr;
+ dest = ham_arg;
break;
}
@@ -60,7 +60,7 @@ static int run_sendmail(int mailfd, enum classification wanted)
if (pid == -1)
return -1;
- debug("running mailtrain backend program %s", sendmail_binary);
+ debug("running mailtrain backend program %s", pipe_binary);
if (pid) {
if (waitpid(pid, &status, 0) == -1)
@@ -71,12 +71,12 @@ static int run_sendmail(int mailfd, enum classification wanted)
} else {
char **argv;
int sz = sizeof(char *) * (2 + extra_args_num + 1);
- int i;
+ int i, fd;
argv = i_malloc(sz);
memset(argv, 0, sz);
- argv[0] = (char *) sendmail_binary;
+ argv[0] = (char *) pipe_binary;
for (i = 0; i < extra_args_num; i++)
argv[i + 1] = (char *) extra_args[i];
@@ -84,9 +84,11 @@ static int run_sendmail(int mailfd, enum classification wanted)
argv[i + 1] = (char *) dest;
dup2(mailfd, 0);
- close(1);
- close(2);
- execv(sendmail_binary, argv);
+ fd = open("/dev/null", O_WRONLY);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ close(fd);
+ execv(pipe_binary, argv);
_exit(1);
/* not reached */
return -1;
@@ -99,7 +101,7 @@ struct antispam_transaction_context {
int tmplen;
};
-struct antispam_transaction_context *
+static struct antispam_transaction_context *
backend_start(struct mailbox *box __attr_unused__)
{
struct antispam_transaction_context *ast;
@@ -141,7 +143,7 @@ static int process_tmpdir(struct mailbox_transaction_context *ctx,
fd = open(buf, O_RDONLY);
read(fd, &wanted, sizeof(wanted));
- if ((rc = run_sendmail(fd, wanted))) {
+ if ((rc = run_pipe(fd, wanted))) {
mail_storage_set_error(ctx->box->storage,
ME(TEMP)
"failed to send mail");
@@ -176,7 +178,7 @@ static void clear_tmpdir(struct antispam_transaction_context *ast)
t_pop();
}
-void backend_rollback(struct antispam_transaction_context *ast)
+static void backend_rollback(struct antispam_transaction_context *ast)
{
if (ast->tmpdir) {
/* clear it! */
@@ -187,8 +189,8 @@ void backend_rollback(struct antispam_transaction_context *ast)
i_free(ast);
}
-int backend_commit(struct mailbox_transaction_context *ctx,
- struct antispam_transaction_context *ast)
+static int backend_commit(struct mailbox_transaction_context *ctx,
+ struct antispam_transaction_context *ast)
{
int ret;
@@ -207,9 +209,9 @@ int backend_commit(struct mailbox_transaction_context *ctx,
return ret;
}
-int backend_handle_mail(struct mailbox_transaction_context *t,
- struct antispam_transaction_context *ast,
- struct mail *mail, enum classification wanted)
+static int backend_handle_mail(struct mailbox_transaction_context *t,
+ struct antispam_transaction_context *ast,
+ struct mail *mail, enum classification wanted)
{
struct istream *mailstream;
struct ostream *outstream;
@@ -226,7 +228,7 @@ int backend_handle_mail(struct mailbox_transaction_context *t,
return -1;
}
- if (!hamaddr || !spamaddr) {
+ if (!ham_arg || !spam_arg) {
mail_storage_set_error(t->box->storage,
ME(NOTPOSSIBLE)
"antispam plugin not configured");
@@ -308,45 +310,64 @@ int backend_handle_mail(struct mailbox_transaction_context *t,
return ret;
}
-void backend_init(pool_t pool __attr_unused__)
+static void backend_init(pool_t pool __attr_unused__)
{
const char *tmp;
int i;
- tmp = get_setting("MAIL_SPAM");
+ tmp = get_setting("PIPE_PROGRAM_SPAM_ARG");
+ if (!tmp)
+ tmp = get_setting("MAIL_SPAM");
if (tmp) {
- spamaddr = tmp;
- debug("mail backend spam address %s\n", tmp);
+ spam_arg = tmp;
+ debug("pipe backend spam argument = %s\n", tmp);
}
- tmp = get_setting("MAIL_NOTSPAM");
+ tmp = get_setting("PIPE_PROGRAM_NOTSPAM_ARG");
+ if (!tmp)
+ tmp = get_setting("MAIL_NOTSPAM");
if (tmp) {
- hamaddr = tmp;
- debug("mail backend not-spam address %s\n", tmp);
+ ham_arg = tmp;
+ debug("pipe backend not-spam argument = %s\n", tmp);
}
- tmp = get_setting("MAIL_SENDMAIL");
+ tmp = get_setting("PIPE_PROGRAM");
+ if (!tmp)
+ tmp = get_setting("MAIL_SENDMAIL");
if (tmp) {
- sendmail_binary = tmp;
- debug("mail backend sendmail %s\n", tmp);
+ pipe_binary = tmp;
+ debug("pipe backend program = %s\n", tmp);
}
- tmp = get_setting("MAIL_SENDMAIL_ARGS");
+ tmp = get_setting("PIPE_PROGRAM_ARGS");
+ if (!tmp)
+ tmp = get_setting("MAIL_SENDMAIL_ARGS");
if (tmp) {
extra_args = p_strsplit(pool, tmp, ";");
extra_args_num = str_array_length(
(const char *const *)extra_args);
for (i = 0; i < extra_args_num; i++)
- debug("mail backend sendmail arg %s\n",
- extra_args[i]);
+ debug("pipe backend program arg[%d] = %s\n",
+ i, extra_args[i]);
}
- tmp = get_setting("MAIL_TMPDIR");
+ tmp = get_setting("PIPE_TMPDIR");
+ if (!tmp)
+ tmp = get_setting("MAIL_TMPDIR");
if (tmp)
tmpdir = tmp;
- debug("mail backend tmpdir %s\n", tmpdir);
+ debug("pipe backend tmpdir %s\n", tmpdir);
}
-void backend_exit(void)
+static void backend_exit(void)
{
}
+
+struct backend pipe_backend = {
+ .init = backend_init,
+ .exit = backend_exit,
+ .handle_mail = backend_handle_mail,
+ .start = backend_start,
+ .rollback = backend_rollback,
+ .commit = backend_commit,
+};