deliver.c revision 29e82a14501731bef8c41a1b27fc415d634fa01d
/* Copyright (c) 2005-2007 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "lib-signals.h"
#include "file-lock.h"
#include "array.h"
#include "ioloop.h"
#include "hostpid.h"
#include "home-expand.h"
#include "env-util.h"
#include "fd-set-nonblock.h"
#include "istream.h"
#include "istream-seekable.h"
#include "module-dir.h"
#include "str.h"
#include "str-sanitize.h"
#include "strescape.h"
#include "var-expand.h"
#include "message-address.h"
#include "istream-header-filter.h"
#include "mbox-storage.h"
#include "mail-namespace.h"
#include "dict-client.h"
#include "mbox-from.h"
#include "auth-client.h"
#include "mail-send.h"
#include "duplicate.h"
#include "../master/syslog-util.h"
#include "../master/syslog-util.c" /* ugly, ugly.. */
#include "deliver.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <syslog.h>
#define DEFAULT_SENDMAIL_PATH "/usr/lib/sendmail"
#define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON"
/* After buffer grows larger than this, create a temporary file to /tmp
where to read the mail. */
struct deliver_settings *deliver_set;
/* FIXME: these two should be in some context struct instead of as globals.. */
static const char *default_mailbox_name = NULL;
static bool saved_mail = FALSE;
static bool tried_default_save = FALSE;
static bool no_mailbox_autocreate = FALSE;
static pool_t plugin_pool;
static ARRAY_DEFINE(plugin_envs, const char *);
{
/* warn about being killed because of some signal, except SIGINT (^C)
which is too common at least while testing :) */
}
{
struct mailbox_sync_context *ctx;
struct mailbox_sync_rec sync_rec;
;
}
static struct mailbox *
{
struct mail_namespace *ns;
enum mail_error error;
return NULL;
}
return box;
if (error != MAIL_ERROR_NOTFOUND)
return NULL;
/* try creating it. */
return NULL;
/* and try opening again */
return NULL;
if (sync_quick(box) < 0) {
mailbox_close(&box);
return NULL;
}
return box;
}
const char *const *keywords)
{
struct mailbox_transaction_context *t;
struct mail_keywords *kw;
const char *msgid, *mailbox_name;
int ret = 0;
return -1;
ret = -1;
if (ret < 0)
else
ret = mailbox_transaction_commit(&t, 0);
msgid = "";
else
if (ret == 0) {
saved_mail = TRUE;
} else {
enum mail_error error;
}
mailbox_close(&box);
return ret;
}
{
struct message_address *addr;
const char *str;
return NULL;
(const unsigned char *)str,
}
const char *deliver_get_new_message_id(void)
{
static int count = 0;
return t_strdup_printf("<dovecot-%s-%s-%d@%s>",
}
#include "settings.h"
#include "../master/master-settings.h"
#include "../master/master-settings-defs.c"
static bool setting_is_bool(const char *name)
{
const struct setting_def *def;
}
return FALSE;
}
static void config_file_init(const char *path)
{
if (fd < 0)
t_push();
/* @UNSAFE: line is modified */
/* skip whitespace */
line++;
/* ignore comments or empty lines */
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--;
lda_section = TRUE;
lda_section = TRUE;
pop3_section = TRUE;
sections++;
}
if (*line == '}') {
sections--;
lda_section = FALSE;
}
continue;
}
if (sections > 0 && !lda_section) {
if (!pop3_section ||
continue;
}
do {
value++;
if (len > 0 &&
}
continue;
if (!plugin_section) {
} else {
/* %variables need to be expanded.
store these for later. */
}
}
t_pop();
}
static const struct var_expand_table *
{
static struct var_expand_table static_tab[] = {
{ 'u', NULL },
{ 'n', NULL },
{ 'd', NULL },
{ 's', NULL },
{ 'h', NULL },
{ 'l', NULL },
{ 'r', NULL },
{ 'p', NULL },
{ 'i', NULL },
{ '\0', NULL }
};
struct var_expand_table *tab;
"/HOME_DIRECTORY_USED_BUT_NOT_GIVEN_BY_USERDB";
return tab;
}
static const char *
{
const char *p;
/* it's either type:data or just data */
if (p != NULL) {
while (env != p) {
env++;
}
}
/* expand home */
}
/* expand %vars */
}
static const char *address_sanitize(const char *address)
{
struct message_address *addr;
const char *ret;
else
pool_unref(&pool);
return ret;
}
{
const char *mbox_hdr;
NULL);
"/tmp/dovecot.deliver.");
i_stream_unref(&input_list[0]);
return input;
}
static void failure_exit_callback(int *status)
{
/* we want all our exit codes to be sysexits.h compatible */
switch (*status) {
case FATAL_LOGOPEN:
case FATAL_LOGWRITE:
case FATAL_LOGERROR:
case FATAL_OUTOFMEM:
case FATAL_EXEC:
case FATAL_DEFAULT:
*status = EX_TEMPFAIL;
break;
}
}
static void open_logfile(const char *username)
{
int facility;
} else {
/* log to file or stderr */
}
}
static void print_help(void)
{
"Usage: deliver [-c <config file>] [-d <destination user>] [-m <mailbox>]\n"
" [-n] [-e] [-f <envelope sender>]\n");
}
void deliver_env_clean(void)
{
/* Note that if the original environment was set with env_put(), the
environment strings will be invalid after env_clean(). That's why
we t_strconcat() them above. */
env_clean();
}
static void expand_envs(const char *destination)
{
const struct var_expand_table *table;
unsigned int i, count;
for (i = 0; i < count; i++) {
str_truncate(str, 0);
}
/* get the table again in case plugin envs provided the home
directory (yea, kludgy) */
}
}
{
char **fields;
const char *key, *p;
unsigned int i, count;
for (i = 0; i < count; i++) {
if (p == NULL)
else {
}
}
}
{
const char *config_path = DEFAULT_CONFIG_FILE;
const char *envelope_sender = DEFAULT_ENVELOPE_SENDER;
const char *mailbox = "INBOX";
const char *auth_socket;
struct mail_storage *storage;
struct mailbox_transaction_context *t;
bool stderr_rejection = FALSE;
bool keep_environment = FALSE;
int i, ret;
lib_init();
ioloop = io_loop_create();
#ifdef SIGXFSZ
#endif
destination = NULL;
for (i = 1; i < argc; i++) {
/* destination user */
i++;
if (i == argc) {
"Missing destination argument");
}
destination = argv[i];
/* config file path */
i++;
if (i == argc) {
"Missing config file path argument");
}
config_path = argv[i];
/* destination mailbox */
i++;
if (i == argc) {
"Missing mailbox argument");
}
/* Ignore -m "". This allows doing -m ${extension}
in Postfix to handle user+mailbox */
if (*argv[i] != '\0')
/* destination mailbox */
/* envelope sender address */
i++;
if (i == argc) {
"Missing envelope argument");
}
envelope_sender = argv[i];
} else if (argv[i][0] != '\0') {
print_help();
"Unknown argument: %s", argv[i]);
}
}
if (!keep_environment)
process_euid = geteuid();
if (destination != NULL)
user = destination;
else if (process_euid != 0) {
/* we're non-root. get our username and possibly our home. */
} else {
i_fatal("Couldn't lookup our username (uid=%s)",
}
} else {
"destination user parameter (-d user) not given");
}
env_put("DEBUG=1");
else {
const char *version;
if (plugin_dir == NULL)
}
if (destination != NULL) {
if (auth_socket == NULL)
&extra_fields);
if (ret != 0)
return ret;
} else {
destination = user;
}
/* If possible chdir to home directory, so that core file
could be written in case we crash. */
}
}
i = 0077;
(void)umask(i);
"postmaster_address setting not given");
}
}
0, FILE_LOCK_METHOD_FCNTL, &error) < 0)
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);
if (deliver_mail == NULL)
ret = -1;
else {
destination, mailbox) <= 0) {
/* if message was saved, don't bounce it even though
the script failed later. */
} else {
/* success. message may or may not have been saved. */
ret = 0;
}
}
if (ret < 0 && !tried_default_save) {
/* plugins didn't handle this. save into the default mailbox. */
i_stream_seek(input, 0);
}
/* still didn't work. try once more to save it
to INBOX. */
i_stream_seek(input, 0);
}
if (ret < 0 ) {
const char *error_string, *msgid;
enum mail_error error;
int ret;
/* This shouldn't happen */
i_error("BUG: Saving failed for unknown storage");
return EX_TEMPFAIL;
}
if (error != MAIL_ERROR_NOSPACE ||
/* Saving to INBOX should always work unless
we're over quota. If it didn't, it's probably a
configuration problem. */
return EX_TEMPFAIL;
}
msgid = "";
/* we'll have to reply with permanent failure */
if (stderr_rejection) {
return EX_NOPERM;
}
if (ret != 0)
/* ok, rejection sent */
}
mailbox_close(&box);
lib_deinit();
return EX_OK;
}