1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*
* mailing backend for dovecot antispam plugin
*
* Copyright (C) 2007 Johannes Berg <johannes@sipsolutions.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "lib.h"
#include "dict.h"
#include "mail-storage-private.h"
#include "ostream.h"
#include "istream.h"
#include "antispam-plugin.h"
static int run_pipe(const struct antispam_config *cfg,
int mailfd, enum classification wanted)
{
char **dest;
int dest_num;
pid_t pid;
int status;
switch (wanted) {
case CLASS_SPAM:
dest = cfg->pipe.spam_args;
dest_num = cfg->pipe.spam_args_num;
break;
case CLASS_NOTSPAM:
dest = cfg->pipe.ham_args;
dest_num = cfg->pipe.ham_args_num;
break;
}
if (!dest)
return -1;
pid = fork();
if (pid == -1)
return -1;
debug(&cfg->dbgcfg, "running mailtrain backend program %s", cfg->pipe.pipe_binary);
if (pid) {
if (waitpid(pid, &status, 0) == -1)
return -1;
if (!WIFEXITED(status))
return -1;
return WEXITSTATUS(status);
} else {
char **argv;
int sz = sizeof(char *) * (2 + cfg->pipe.extra_args_num + dest_num + 1);
int i, j, fd;
argv = i_malloc(sz);
memset(argv, 0, sz);
argv[0] = (char *) cfg->pipe.pipe_binary;
for (i = 0; i < cfg->pipe.extra_args_num; i++) {
argv[i + 1] = (char *) cfg->pipe.extra_args[i];
debug(&cfg->dbgcfg, "running mailtrain backend program parameter %d %s", i + 1, argv[i + 1]);
}
for (j = 0; j < dest_num; j++) {
argv[i + 1 + j] = (char *) dest[j];
debug(&cfg->dbgcfg, "running mailtrain backend program parameter %d %s", i + 1 + j, argv[i + 1 + j]);
}
dup2(mailfd, 0);
fd = open("/dev/null", O_WRONLY);
dup2(fd, 1);
dup2(fd, 2);
close(fd);
execv(cfg->pipe.pipe_binary, argv);
_exit(1);
/* not reached */
return -1;
}
}
struct antispam_transaction_context {
char *tmpdir;
int count;
int tmplen;
};
static struct antispam_transaction_context *
backend_start(const struct antispam_config *cfg ATTR_UNUSED,
struct mailbox *box ATTR_UNUSED)
{
struct antispam_transaction_context *ast;
char *tmp;
ast = i_new(struct antispam_transaction_context, 1);
ast->count = 0;
tmp = i_strconcat(cfg->pipe.tmpdir, "/antispam-mail-XXXXXX", NULL);
ast->tmpdir = mkdtemp(tmp);
if (!ast->tmpdir)
i_free(tmp);
else
ast->tmplen = strlen(ast->tmpdir);
return ast;
}
static int process_tmpdir(const struct antispam_config *cfg,
struct mailbox_transaction_context *ctx,
struct antispam_transaction_context *ast)
{
int cnt = ast->count;
int fd;
char *buf;
enum classification wanted;
int rc = 0;
t_push();
buf = t_malloc(20 + ast->tmplen);
while (rc == 0 && cnt > 0) {
cnt--;
i_snprintf(buf, 20 + ast->tmplen - 1, "%s/%d",
ast->tmpdir, cnt);
fd = open(buf, O_RDONLY);
read(fd, &wanted, sizeof(wanted));
if ((rc = run_pipe(cfg, fd, wanted))) {
mail_storage_set_error(ctx->box->storage,
ME(TEMP)
"failed to send mail");
debug(&cfg->dbgcfg, "run program failed with exit code %d\n", rc);
rc = -1;
}
close(fd);
}
t_pop();
return rc;
}
static void clear_tmpdir(struct antispam_transaction_context *ast)
{
char *buf;
t_push();
buf = t_malloc(20 + ast->tmplen);
while (ast->count > 0) {
ast->count--;
i_snprintf(buf, 20 + ast->tmplen - 1, "%s/%d",
ast->tmpdir, ast->count);
unlink(buf);
}
rmdir(ast->tmpdir);
t_pop();
}
static void backend_rollback(const struct antispam_config *cfg ATTR_UNUSED,
struct antispam_transaction_context *ast)
{
if (ast->tmpdir) {
/* clear it! */
clear_tmpdir(ast);
i_free(ast->tmpdir);
}
i_free(ast);
}
static int backend_commit(const struct antispam_config *cfg,
struct mailbox_transaction_context *ctx,
struct antispam_transaction_context *ast)
{
int ret;
if (!ast->tmpdir) {
i_free(ast);
return 0;
}
ret = process_tmpdir(cfg, ctx, ast);
clear_tmpdir(ast);
i_free(ast->tmpdir);
i_free(ast);
return ret;
}
static int backend_handle_mail(const struct antispam_config *cfg,
struct mailbox_transaction_context *t,
struct antispam_transaction_context *ast,
struct mail *mail, enum classification wanted)
{
struct istream *mailstream;
struct ostream *outstream;
int ret;
char *buf;
const unsigned char *beginning;
size_t size;
int fd;
if (!ast->tmpdir) {
mail_storage_set_error(t->box->storage,
ME(NOTPOSSIBLE)
"Failed to initialise temporary dir");
return -1;
}
if (!cfg->pipe.ham_args || !cfg->pipe.spam_args) {
mail_storage_set_error(t->box->storage,
ME(NOTPOSSIBLE)
"antispam plugin not configured");
return -1;
}
if (mail_get_stream(mail, NULL, NULL, &mailstream) < 0) {
mail_storage_set_error(t->box->storage,
ME(EXPUNGED)
"Failed to get mail contents");
return -1;
}
t_push();
buf = t_malloc(20 + ast->tmplen);
i_snprintf(buf, 20 + ast->tmplen - 1, "%s/%d", ast->tmpdir, ast->count);
fd = creat(buf, 0600);
if (fd < 0) {
mail_storage_set_error(t->box->storage,
ME(NOTPOSSIBLE)
"Failed to create temporary file");
ret = -1;
goto out;
}
ast->count++;
outstream = o_stream_create_from_fd(fd, t->box->pool);
if (!outstream) {
ret = -1;
mail_storage_set_error(t->box->storage,
ME(NOTPOSSIBLE)
"Failed to stream temporary file");
goto out_close;
}
if (o_stream_send(outstream, &wanted, sizeof(wanted))
!= sizeof(wanted)) {
ret = -1;
mail_storage_set_error(t->box->storage,
ME(NOTPOSSIBLE)
"Failed to write marker to temp file");
goto failed_to_copy;
}
if (i_stream_read_data(mailstream, &beginning, &size, 5) < 0 ||
size < 5) {
ret = -1;
mail_storage_set_error(t->box->storage,
ME(NOTPOSSIBLE)
"Failed to read mail beginning");
goto failed_to_copy;
}
/* "From "? skip line */
if (memcmp("From ", beginning, 5) == 0)
i_stream_read_next_line(mailstream);
if (o_stream_send_istream(outstream, mailstream) < 0) {
ret = -1;
mail_storage_set_error(t->box->storage,
ME(NOTPOSSIBLE)
"Failed to copy to temporary file");
goto failed_to_copy;
}
ret = 0;
failed_to_copy:
o_stream_destroy(&outstream);
out_close:
close(fd);
out:
t_pop();
return ret;
}
static void backend_init(struct antispam_config *cfg,
const char *(getenv)(const char *env, void *data),
void *getenv_data)
{
const char *tmp;
int i;
tmp = getenv("PIPE_PROGRAM_SPAM_ARGS", getenv_data);
if (tmp) {
cfg->pipe.spam_args = p_strsplit(cfg->mem_pool, tmp, ";");
cfg->pipe.spam_args_num = str_array_length(
(const char *const *)cfg->pipe.spam_args);
for (i = 0; i < cfg->pipe.spam_args_num; i++)
debug(&cfg->dbgcfg, "pipe backend spam arg[%d] = %s\n",
i, cfg->pipe.spam_args[i]);
} else {
tmp = getenv("PIPE_PROGRAM_SPAM_ARG", getenv_data);
if (!tmp)
tmp = getenv("MAIL_SPAM", getenv_data);
if (tmp) {
/* bit of a hack */
cfg->pipe.spam_args =
p_strsplit(cfg->mem_pool, tmp, "\x01");
cfg->pipe.spam_args_num = 1;
debug(&cfg->dbgcfg,
"pipe backend spam argument = %s\n", tmp);
tmp = NULL;
}
}
tmp = getenv("PIPE_PROGRAM_NOTSPAM_ARGS", getenv_data);
if (tmp) {
cfg->pipe.ham_args = p_strsplit(cfg->mem_pool, tmp, ";");
cfg->pipe.ham_args_num = str_array_length(
(const char *const *)cfg->pipe.ham_args);
for (i = 0; i < cfg->pipe.ham_args_num; i++)
debug(&cfg->dbgcfg, "pipe backend ham arg[%d] = %s\n",
i, cfg->pipe.ham_args[i]);
} else {
tmp = getenv("PIPE_PROGRAM_NOTSPAM_ARG", getenv_data);
if (!tmp)
tmp = getenv("MAIL_NOTSPAM", getenv_data);
if (tmp) {
/* bit of a hack */
cfg->pipe.ham_args =
p_strsplit(cfg->mem_pool, tmp, "\x01");
cfg->pipe.ham_args_num = 1;
debug(&cfg->dbgcfg,
"pipe backend not-spam argument = %s\n", tmp);
tmp = NULL;
}
}
tmp = getenv("PIPE_PROGRAM", getenv_data);
if (!tmp)
tmp = getenv("MAIL_SENDMAIL", getenv_data);
if (tmp) {
cfg->pipe.pipe_binary = tmp;
debug(&cfg->dbgcfg, "pipe backend program = %s\n", tmp);
} else
cfg->pipe.pipe_binary = "/usr/sbin/sendmail";
tmp = getenv("PIPE_PROGRAM_ARGS", getenv_data);
if (!tmp)
tmp = getenv("MAIL_SENDMAIL_ARGS", getenv_data);
if (tmp) {
cfg->pipe.extra_args = p_strsplit(cfg->mem_pool, tmp, ";");
cfg->pipe.extra_args_num = str_array_length(
(const char *const *)cfg->pipe.extra_args);
for (i = 0; i < cfg->pipe.extra_args_num; i++)
debug(&cfg->dbgcfg, "pipe backend program arg[%d] = %s\n",
i, cfg->pipe.extra_args[i]);
}
tmp = getenv("PIPE_TMPDIR", getenv_data);
if (!tmp)
tmp = getenv("MAIL_TMPDIR", getenv_data);
if (tmp)
cfg->pipe.tmpdir = tmp;
else
cfg->pipe.tmpdir = "/tmp";
debug(&cfg->dbgcfg, "pipe backend tmpdir %s\n", cfg->pipe.tmpdir);
}
struct backend pipe_backend = {
.init = backend_init,
.handle_mail = backend_handle_mail,
.start = backend_start,
.rollback = backend_rollback,
.commit = backend_commit,
};
|