main.c revision e156adefc1260d31a145df2f5e9b3c82050d4163
/* Copyright (c) 2005-2009 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 "safe-mkstemp.h"
#include "eacces-error.h"
#include "mkdir-parents.h"
#include "str.h"
#include "str-sanitize.h"
#include "strescape.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 "duplicate.h"
#include "mbox-from.h"
#include "lda-settings.h"
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <sysexits.h>
#ifndef EX_CONFIG
#endif
#define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON"
/* After buffer grows larger than this, create a temporary file to /tmp
where to read the mail. */
//#define MAIL_MAX_MEMORY_BUFFER (1024*128)
#define MAIL_MAX_MEMORY_BUFFER 10
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 there are non-atext chars, we need to return quoted-string */
for (p = local_part; *p != '\0'; p++) {
if (!IS_ATEXT(*p)) {
return t_strdup_printf("\"%s\"",
}
}
return local_part;
}
static const char *address_sanitize(const char *address)
{
struct message_address *addr;
else {
else
}
pool_unref(&pool);
return ret;
}
{
struct mail_namespace *ns;
const char *origin;
return 0;
dir));
return -1;
} else {
i_error("mkdir_parents_chown(%s, gid=%s) failed: %m",
return -1;
}
}
{
const char *dir, *p;
int fd;
if (p != NULL) {
return -1;
}
}
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_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;
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_CONFIG;
"and setuid-root. This allows root exploits. "
"See http://wiki.dovecot.org/LDA#multipleuids\n",
argv[0]);
return EX_CONFIG;
}
}
switch (c) {
case 'a':
/* destination address */
break;
case 'd':
/* destination user */
break;
case 'p':
/* input path */
if (*path != '/') {
/* expand relative paths before we chdir */
i_fatal("getcwd() failed: %m");
}
break;
case 'e':
break;
case 'm':
/* destination mailbox.
Ignore -m "". This allows doing -m ${extension}
in Postfix to handle user+mailbox */
i_fatal("Mailbox name not UTF-8: %s",
optarg);
}
} T_END;
break;
case 'f':
/* envelope sender address */
break;
default:
c, optarg)) {
print_help();
}
break;
}
}
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 */
"Couldn't lookup our username (uid=%s)",
}
} else {
"destination user parameter (-d user) not given");
}
#ifdef SIGXFSZ
#endif
/* create a separate mail user for the internal namespace */
"mail_full_filesystem_access=yes") < 0)
i_unreached();
} else {
}
if (mailbox_open(box) < 0) {
i_fatal("Can't open delivery mail as raw: %s",
}
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_close(&box);
return EX_OK;
}