deliver.c revision 2b312f0183750c8cb9049510df4b6cd2027f9f43
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (C) 2005-2006 Timo Sirainen */
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "lib.h"
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen#include "lib-signals.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "ioloop.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "hostpid.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "home-expand.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "env-util.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "fd-set-nonblock.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "istream.h"
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen#include "istream-seekable.h"
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen#include "module-dir.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "str.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "var-expand.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "message-address.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "dict-client.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "mbox-from.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "auth-client.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "mail-send.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "duplicate.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include "deliver.h"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#include <stdio.h>
395682d473b161c86165e7b9323ce4e45afb94bdTimo Sirainen#include <stdlib.h>
395682d473b161c86165e7b9323ce4e45afb94bdTimo Sirainen#include <unistd.h>
072f06b60d69fe9456c3fffe20e72a7d09f2825dTimo Sirainen#include <fcntl.h>
395682d473b161c86165e7b9323ce4e45afb94bdTimo Sirainen#include <pwd.h>
395682d473b161c86165e7b9323ce4e45afb94bdTimo Sirainen#include <syslog.h>
395682d473b161c86165e7b9323ce4e45afb94bdTimo Sirainen
395682d473b161c86165e7b9323ce4e45afb94bdTimo Sirainen#define DEFAULT_CONFIG_FILE SYSCONFDIR"/dovecot.conf"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#define DEFAULT_AUTH_SOCKET_PATH PKG_RUNDIR"/auth-master"
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#define DEFAULT_SENDMAIL_PATH "/usr/lib/sendmail"
395682d473b161c86165e7b9323ce4e45afb94bdTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen/* After buffer grows larger than this, create a temporary file to /tmp
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen where to read the mail. */
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen#define MAIL_MAX_MEMORY_BUFFER (1024*128)
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainenstruct deliver_settings *deliver_set;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainendeliver_mail_func_t *deliver_mail = NULL;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainenvoid (*hook_mail_storage_created)(struct mail_storage *storage) = NULL;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainenstatic struct module *modules;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainenstatic struct ioloop *ioloop;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainenstatic void sig_die(int signo, void *context __attr_unused__)
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen{
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen /* warn about being killed because of some signal, except SIGINT (^C)
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen which is too common at least while testing :) */
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen if (signo != SIGINT)
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen i_warning("Killed with signal %d", signo);
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen io_loop_stop(ioloop);
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen}
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainenstatic int sync_quick(struct mailbox *box)
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen{
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen struct mailbox_sync_context *ctx;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen struct mailbox_sync_rec sync_rec;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen struct mailbox_status status;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen
31a574fda352ef4f71dbff9c30e15e4744e132c0Timo Sirainen ctx = mailbox_sync_init(box, 0);
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen while (mailbox_sync_next(ctx, &sync_rec) > 0)
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen ;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen return mailbox_sync_deinit(&ctx, &status);
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen}
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainenstatic struct mailbox *
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainenmailbox_open_or_create_synced(struct mail_storage *storage, const char *name)
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen{
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen struct mailbox *box;
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen bool syntax, temp;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen box = mailbox_open(storage, name, NULL, MAILBOX_OPEN_FAST |
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen MAILBOX_OPEN_KEEP_RECENT);
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen if (box != NULL)
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen return box;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen (void)mail_storage_get_last_error(storage, &syntax, &temp);
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen if (syntax || temp)
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen return NULL;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen /* probably the mailbox just doesn't exist. try creating it. */
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen if (mail_storage_mailbox_create(storage, name, FALSE) < 0)
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen return NULL;
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen /* and try opening again */
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen box = mailbox_open(storage, name, NULL, MAILBOX_OPEN_FAST |
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen MAILBOX_OPEN_KEEP_RECENT);
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen if (box == NULL)
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen return NULL;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen if (sync_quick(box) < 0) {
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen mailbox_close(&box);
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen return NULL;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen }
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen return box;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen}
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainenint deliver_save(struct mail_storage *storage, const char *mailbox,
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen struct mail *mail, enum mail_flags flags,
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen const char *const *keywords)
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen{
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen struct mailbox *box;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen struct mailbox_transaction_context *t;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen struct mail_keywords *kw;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen int ret = 0;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen box = mailbox_open_or_create_synced(storage, mailbox);
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen if (box == NULL)
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen return -1;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen kw = strarray_length(keywords) == 0 ? NULL :
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen mailbox_keywords_create(t, keywords);
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen if (mailbox_copy(t, mail, flags, kw, NULL) < 0)
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen ret = -1;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen mailbox_keywords_free(t, &kw);
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen if (ret < 0)
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen mailbox_transaction_rollback(&t);
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen else
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen ret = mailbox_transaction_commit(&t, 0);
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen mailbox_close(&box);
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen return ret;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen}
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainenconst char *deliver_get_return_address(struct mail *mail)
363929157786b549c80630bda3c3575f5115c6c5Timo Sirainen{
4182d8cd818e76856a5a1e25b343fe5ddf69fd8eTimo Sirainen struct message_address *addr;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen const char *str;
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen str = mail_get_first_header(mail, "Return-Path");
c115c742f730e312d6b6ab5064595cd0d8b4e26eTimo Sirainen addr = str == NULL ? NULL :
message_address_parse(pool_datastack_create(),
(const unsigned char *)str,
strlen(str), 1);
return addr == NULL || addr->mailbox == NULL || addr->domain == NULL ?
NULL : t_strconcat(addr->mailbox, "@", addr->domain, NULL);
}
const char *deliver_get_new_message_id(void)
{
static int count = 0;
return t_strdup_printf("<dovecot-%s-%s-%d@%s>",
dec2str(ioloop_timeval.tv_sec),
dec2str(ioloop_timeval.tv_usec),
count++, deliver_set->hostname);
}
#define IS_WHITE(c) ((c) == ' ' || (c) == '\t')
static void config_file_init(const char *path)
{
struct istream *input;
const char *key, *value;
char *line, *p, quote;
int fd, sections = 0, lda_section = FALSE;
size_t len;
fd = open(path, O_RDONLY);
if (fd < 0)
i_fatal_status(EX_CONFIG, "open(%s) failed: %m", path);
t_push();
input = i_stream_create_file(fd, default_pool, 1024, TRUE);
while ((line = i_stream_read_next_line(input)) != NULL) {
/* @UNSAFE: line is modified */
/* skip whitespace */
while (IS_WHITE(*line))
line++;
/* ignore comments or empty lines */
if (*line == '#' || *line == '\0')
continue;
/* strip away comments. pretty kludgy way really.. */
for (p = line; *p != '\0'; p++) {
if (*p == '\'' || *p == '"') {
quote = *p;
for (p++; *p != quote && *p != '\0'; p++) {
if (*p == '\\' && p[1] != '\0')
p++;
}
if (*p == '\0')
break;
} else if (*p == '#') {
*p = '\0';
break;
}
}
/* remove whitespace from end of line */
len = strlen(line);
while (IS_WHITE(line[len-1]))
len--;
line[len] = '\0';
value = p = strchr(line, '=');
if (value == NULL) {
if (strchr(line, '{') != NULL) {
if (strcmp(line, "protocol lda {") == 0 ||
strcmp(line, "plugin {") == 0)
lda_section = TRUE;
sections++;
}
if (*line == '}') {
sections--;
lda_section = FALSE;
}
continue;
}
if (sections > 0 && !lda_section)
continue;
while (p > line && p[-1] == ' ') p--;
key = t_strdup_until(line, p);
do {
value++;
} while (*value == ' ');
env_put(t_strconcat(t_str_ucase(key), "=", value, NULL));
}
i_stream_unref(&input);
t_pop();
}
static const struct var_expand_table *
get_var_expand_table(const char *user, const char *home)
{
static struct var_expand_table static_tab[] = {
{ 'u', NULL },
{ 'n', NULL },
{ 'd', NULL },
{ 's', NULL },
{ 'h', NULL },
{ 'l', NULL },
{ 'r', NULL },
{ 'p', NULL },
{ '\0', NULL }
};
struct var_expand_table *tab;
tab = t_malloc(sizeof(static_tab));
memcpy(tab, static_tab, sizeof(static_tab));
tab[0].value = user;
tab[1].value = t_strcut(user, '@');
tab[2].value = strchr(user, '@');
if (tab[2].value != NULL) tab[2].value++;
tab[3].value = "DELIVER";
tab[4].value = home;
tab[5].value = NULL;
tab[6].value = NULL;
tab[7].value = my_pid;
return tab;
}
static const char *
expand_mail_env(const char *env, const struct var_expand_table *table)
{
string_t *str;
const char *p;
str = t_str_new(256);
/* it's either type:data or just data */
p = strchr(env, ':');
if (p != NULL) {
while (env != p) {
str_append_c(str, *env);
env++;
}
str_append_c(str, *env++);
}
if (env[0] == '~' && env[1] == '/') {
/* expand home */
env = t_strconcat("%h", env+1, NULL);
}
/* expand %vars */
var_expand(str, env, table);
return str_c(str);
}
static struct istream *create_mbox_stream(int fd)
{
const char *mbox_hdr;
struct istream *input_list[4], *input;
fd_set_nonblock(fd, FALSE);
mbox_hdr = mbox_from_create("dovecot.deliver", ioloop_time);
input_list[0] = i_stream_create_from_data(default_pool, mbox_hdr,
strlen(mbox_hdr));
input_list[1] = i_stream_create_file(fd, default_pool, 4096, FALSE);
input_list[2] = i_stream_create_from_data(default_pool, "\n", 1);
input_list[3] = NULL;
input = i_stream_create_seekable(input_list, default_pool,
MAIL_MAX_MEMORY_BUFFER,
"/tmp/dovecot.deliver.");
i_stream_unref(&input_list[0]);
i_stream_unref(&input_list[1]);
i_stream_unref(&input_list[2]);
return input;
}
static void open_logfile(const char *username)
{
const char *prefix;
prefix = t_strdup_printf("deliver(%s)", username);
if (getenv("LOG_PATH") == NULL) {
const char *env = getenv("SYSLOG_FACILITY");
i_set_failure_syslog(prefix, LOG_NDELAY,
env == NULL ? LOG_MAIL : atoi(env));
} else {
/* log to file or stderr */
i_set_failure_file(getenv("LOG_PATH"), prefix);
}
if (getenv("INFO_LOG_PATH") != NULL)
i_set_info_file(getenv("INFO_LOG_PATH"));
i_set_failure_timestamp_format(getenv("LOG_TIMESTAMP"));
}
static void print_help(void)
{
printf("Usage: deliver [-c <config file>] [-d <destination user>] [-m <mailbox>]\n");
}
int main(int argc, char *argv[])
{
const char *config_path = DEFAULT_CONFIG_FILE;
const char *mailbox = "INBOX";
const char *auth_socket, *env_tz;
const char *home, *destination, *user, *mail_env;
const struct var_expand_table *table;
enum mail_storage_flags flags;
enum mail_storage_lock_method lock_method;
struct mail_storage *storage, *mbox_storage;
struct mailbox *box;
struct istream *input;
struct mailbox_transaction_context *t;
struct mail *mail;
uid_t process_euid;
int i, ret;
lib_init();
ioloop = io_loop_create(default_pool);
lib_signals_init();
lib_signals_set_handler(SIGINT, TRUE, sig_die, NULL);
lib_signals_set_handler(SIGTERM, TRUE, sig_die, NULL);
lib_signals_ignore(SIGPIPE);
lib_signals_set_handler(SIGALRM, FALSE, NULL, NULL);
#ifdef SIGXFSZ
lib_signals_set_handler(SIGXFSZ, FALSE, NULL, NULL);
#endif
/* Clean up environment. */
env_tz = getenv("TZ");
home = getenv("HOME");
env_clean();
if (env_tz != NULL)
env_put(t_strconcat("TZ=", env_tz, NULL));
if (home != NULL)
env_put(t_strconcat("HOME=", home, NULL));
destination = NULL;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-d") == 0) {
/* destination user */
i++;
if (i == argc) {
i_fatal_status(EX_USAGE,
"Missing destination argument");
}
destination = argv[i];
} else if (strcmp(argv[i], "-c") == 0) {
/* config file path */
i++;
if (i == argc) {
i_fatal_status(EX_USAGE,
"Missing config file path argument");
}
config_path = argv[i];
} else if (strcmp(argv[i], "-m") == 0) {
/* destination mailbox */
i++;
if (i == argc) {
i_fatal_status(EX_USAGE,
"Missing mailbox argument");
}
mailbox = argv[i];
} else {
print_help();
i_fatal_status(EX_USAGE,
"Unknown argument: %s", argv[1]);
}
}
process_euid = geteuid();
if (destination != NULL)
user = destination;
else if (process_euid != 0) {
/* we're non-root. get our username. */
struct passwd *pw;
pw = getpwuid(process_euid);
if (pw != NULL)
user = t_strdup(pw->pw_name);
else {
i_fatal("Couldn't lookup our username (uid=%s)",
dec2str(process_euid));
}
} else {
i_fatal_status(EX_USAGE,
"destination user parameter (-d user) not given");
}
config_file_init(config_path);
open_logfile(user);
if (destination != NULL) {
auth_socket = getenv("AUTH_SOCKET_PATH");
if (auth_socket == NULL)
auth_socket = DEFAULT_AUTH_SOCKET_PATH;
ret = auth_client_put_user_env(ioloop, auth_socket,
destination, process_euid);
if (ret != 0)
return ret;
home = getenv("HOME");
if (home != NULL) {
/* If possible chdir to home directory so core file
could be written. If it fails, don't worry. */
(void)chdir(home);
}
} else {
destination = user;
}
deliver_set = i_new(struct deliver_settings, 1);
deliver_set->hostname = getenv("HOSTNAME");
if (deliver_set->hostname == NULL)
deliver_set->hostname = my_hostname;
deliver_set->postmaster_address = getenv("POSTMASTER_ADDRESS");
if (deliver_set->postmaster_address == NULL) {
i_fatal_status(EX_CONFIG,
"postmaster_address setting not given");
}
deliver_set->sendmail_path = getenv("SENDMAIL_PATH");
if (deliver_set->sendmail_path == NULL)
deliver_set->sendmail_path = DEFAULT_SENDMAIL_PATH;
dict_client_register();
duplicate_init();
mail_storage_init();
mail_storage_register_all();
/* MAIL comes from userdb, DEFAULT_MAIL_ENV from dovecot.conf */
mail_env = getenv("MAIL");
if (mail_env == NULL)
mail_env = getenv("DEFAULT_MAIL_ENV");
if (mail_env != NULL) {
table = get_var_expand_table(destination, getenv("HOME"));
mail_env = expand_mail_env(mail_env, table);
}
if (getenv("MAIL_PLUGINS") == NULL)
modules = NULL;
else {
const char *plugin_dir = getenv("MAIL_PLUGIN_DIR");
if (plugin_dir == NULL)
plugin_dir = MODULEDIR"/lda";
modules = module_dir_load(plugin_dir, getenv("MAIL_PLUGINS"),
TRUE);
}
/* FIXME: how should we handle namespaces? */
mail_storage_parse_env(&flags, &lock_method);
storage = mail_storage_create_with_data(mail_env, destination,
flags, lock_method);
if (storage == NULL) {
i_fatal_status(EX_CONFIG,
"Failed to create storage for '%s' with mail '%s'",
destination, mail_env == NULL ? "(null)" : mail_env);
}
if (hook_mail_storage_created != NULL)
hook_mail_storage_created(storage);
mbox_storage = mail_storage_create("mbox", "/tmp", destination, 0,
MAIL_STORAGE_LOCK_FCNTL);
input = create_mbox_stream(0);
box = mailbox_open(mbox_storage, "Dovecot Delivery Mail", input,
MAILBOX_OPEN_NO_INDEX_FILES);
if (box == NULL)
i_fatal("Can't open delivery mail as mbox");
if (sync_quick(box) < 0)
i_fatal("Can't sync delivery mail");
t = mailbox_transaction_begin(box, 0);
mail = mail_alloc(t, 0, NULL);
if (mail_set_seq(mail, 1) < 0)
i_fatal("mail_set_seq() failed");
ret = deliver_mail == NULL ? 0 :
deliver_mail(storage, mail, destination, mailbox);
if (ret <= 0) {
/* plugins didn't handle this. save into INBOX. */
i_stream_seek(input, 0);
if (deliver_save(storage, mailbox, mail, 0, NULL) < 0) {
const char *error;
bool syntax, temporary_error;
int ret;
error = mail_storage_get_last_error(storage, &syntax,
&temporary_error);
if (temporary_error)
return EX_TEMPFAIL;
/* we'll have to reply with permanent failure */
ret = mail_send_rejection(mail, destination, error);
if (ret != 0)
return ret < 0 ? EX_TEMPFAIL : ret;
/* ok, rejection sent */
}
}
i_stream_unref(&input);
mail_free(&mail);
mailbox_transaction_rollback(&t);
mailbox_close(&box);
mail_storage_destroy(&mbox_storage);
mail_storage_destroy(&storage);
module_dir_unload(&modules);
mail_storage_deinit();
duplicate_deinit();
dict_client_unregister();
lib_signals_deinit();
io_loop_destroy(&ioloop);
lib_deinit();
return EX_OK;
}