main.c revision 0992011130e9d0a498ca860ddbe4028398a530c5
/* Copyright (c) 2005-2011 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "lib-signals.h"
#include "env-util.h"
#include "fd-set-nonblock.h"
#include "close-keep-errno.h"
#include "istream.h"
#include "istream-seekable.h"
#include "abspath.h"
#include "safe-mkstemp.h"
#include "eacces-error.h"
#include "ipwd.h"
#include "mkdir-parents.h"
#include "str.h"
#include "str-sanitize.h"
#include "strescape.h"
#include "unichar.h"
#include "rfc822-parser.h"
#include "message-address.h"
#include "imap-utf7.h"
#include "settings-parser.h"
#include "master-service.h"
#include "master-service-settings.h"
#include "mail-storage-service.h"
#include "mail-namespace.h"
#include "raw-storage.h"
#include "mail-deliver.h"
#include "mail-send.h"
#include "mbox-from.h"
#include "lda-settings.h"
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON"
/* After buffer grows larger than this, create a temporary file to /tmp
where to read the mail. */
static const char *wanted_headers[] = {
"From", "To", "Message-ID", "Subject", "Return-Path",
};
static const char *escape_local_part(const char *local_part)
{
const char *p;
/* if local_part isn't dot-atom-text, we need to return quoted-string
dot-atom-text = 1*atext *("." 1*atext) */
for (p = local_part; *p != '\0'; p++) {
if (!IS_ATEXT(*p) && *p != '.')
break;
}
return local_part;
}
static const char *address_sanitize(const char *address)
{
struct message_address *addr;
else {
else
}
pool_unref(&pool);
return ret;
}
{
int fd;
if (fd == -1) {
return -1;
}
/* we just want the fd, unlink it */
/* shouldn't happen.. */
return -1;
}
return fd;
}
static struct istream *
{
const unsigned char *data;
/* If input begins with a From-line, drop it */
/* skip until the first LF */
for (i = 0; i < size; i++) {
if (data[i] == '\n')
break;
}
if (i != size) {
&sender);
break;
}
}
}
/* use the envelope sender from From_-line, but only if it
hasn't been specified with -f already. */
}
} else {
}
return input;
}
static void failure_exit_callback(int *status)
{
/* we want all our exit codes to be sysexits.h compatible.
if we failed because of a logging related error, we most likely
aren't writing to stderr, so try writing there to give some kind of
a clue what's wrong. FATAL_LOGOPEN failure already wrote to
stderr, so don't duplicate it. */
switch (*status) {
case FATAL_LOGWRITE:
break;
case FATAL_LOGERROR:
break;
case FATAL_LOGOPEN:
case FATAL_OUTOFMEM:
case FATAL_EXEC:
case FATAL_DEFAULT:
break;
default:
return;
}
*status = EX_TEMPFAIL;
}
static void print_help(void)
{
"Usage: dovecot-lda [-c <config file>] [-a <address>] [-d <username>] [-p <path>]\n"
" [-f <envelope sender>] [-m <mailbox>] [-e] [-k]\n");
}
{
const struct setting_parser_info *set_roots[] = {
};
struct mail_deliver_context ctx;
enum mail_storage_service_flags service_flags = 0;
struct mail_storage_service_ctx *storage_service;
struct mail_storage_service_user *service_user;
struct mail_user *raw_mail_user;
struct mail_namespace *raw_ns;
struct mail_namespace_settings raw_ns_set;
struct mail_storage *storage;
struct raw_mailbox *raw_box;
struct mailbox_transaction_context *t;
struct mailbox_header_lookup_ctx *headers_ctx;
const char *user_source = "";
void **sets;
bool stderr_rejection = FALSE;
int ret, c;
enum mail_error error;
/* running setuid - don't allow this if the binary is
executable by anyone */
return EX_TEMPFAIL;
"and setuid-root. This allows root exploits. "
"See http://wiki2.dovecot.org/LDA#multipleuids\n",
argv[0]);
return EX_TEMPFAIL;
}
}
while ((c = master_getopt(master_service)) > 0) {
switch (c) {
case 'a':
/* original recipient address */
break;
case 'd':
/* destination user */
break;
case 'e':
break;
case 'f':
/* envelope sender address */
break;
case 'm':
/* destination mailbox.
Ignore -m "". This allows doing -m ${extension}
in Postfix to handle user+mailbox */
if (!uni_utf8_str_is_valid(optarg)) {
i_fatal("Mailbox name not UTF-8: %s",
optarg);
}
} T_END;
break;
case 'p':
/* input path */
break;
case 'r':
/* final recipient address */
break;
default:
print_help();
return EX_USAGE;
}
}
print_help();
}
process_euid = geteuid();
if ((service_flags & MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP) != 0)
;
else if (process_euid != 0) {
/* we're non-root. get our username and possibly our home. */
const char *home;
/* no need for a pw lookup */
user_source = "USER environment";
user_source = "passwd lookup for process euid";
} else if (ret < 0) {
/* temporary failure */
i_fatal("getpwuid() failed: %m");
"Couldn't lookup our username (uid=%s)",
}
} else {
"destination user parameter (-d user) not given");
}
&errstr);
if (ret <= 0) {
if (ret < 0)
return EX_NOUSER;
}
#ifdef SIGXFSZ
#endif
i_debug("userdb lookup skipped, username taken from %s",
}
/* create a separate mail user for the internal namespace */
i_fatal("Can't open delivery mail as raw: %s",
}
} else {
if (mailbox_open(box) < 0) {
i_fatal("Can't open delivery mail as raw: %s",
}
}
if (mailbox_sync(box, 0) < 0) {
i_fatal("Can't sync delivery mail: %s",
}
t = mailbox_transaction_begin(box, 0);
}
}
/* This shouldn't happen */
i_error("BUG: Saving failed to unknown storage");
return EX_TEMPFAIL;
}
if (stderr_rejection) {
/* write to stderr also for tempfails so that MTA
can log the reason if it wants to. */
}
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;
}
/* we'll have to reply with permanent failure */
if (stderr_rejection)
return EX_NOPERM;
if (ret != 0)
/* ok, rejection sent */
}
mailbox_free(&box);
return EX_OK;
}