Dovecot LDA
===========

The Dovecot LDA, called *'deliver'*, is a <local delivery agent> [MDA.txt]
which takes mail from an<MTA.txt> and delivers it to a user's mailbox, while
keeping Dovecot index files up to date.

This page describes the common settings required to make deliver work. You
should read it first, and then the MTA specific pages:

 * <LDA.Postfix.txt>
 * <LDA.Exim.txt>
 * <LDA.Sendmail.txt>
 * <LDA.Qmail.txt>

Main features of Dovecot LDA
----------------------------

 * <Mailbox indexing during mail delivery> [LDA.Indexing.txt], providing faster
   mailbox access later
 * <Quota enforcing by a plugin> [Quota.txt]
 * <Sieve language support by a plugin> [LDA.Sieve.txt]
    * Mail filtering
    * Mail forwarding
    * Vacation auto-reply

Common configuration
--------------------

The configuration is done in the /protocol lda/ section in 'dovecot.conf'. The
important settings are:

 * 'postmaster_address' is used as the From: header address in bounce mails
 * 'hostname' is used in generated Message-IDs and in Reporting-UA: header in
   bounce mails
 * 'sendmail_path' is used to send mails. Note that the default is
   '/usr/lib/sendmail', which doesn't necessarily work the same as
   '/usr/sbin/sendmail'.
 * 'auth_socket_path' specifies the UNIX socket to dovecot-auth where deliver
   can lookup userdb information when '-d' parameter is used.

Note that 'dovecot.conf' file must be world readable to enable deliver process
read it, while running with user priviledges.

Parameters
----------

Parameters accepted by deliver:

 * '-d <username>': Destination username. If given, the user information is
   looked up from dovecot-auth.
 * '-a <address>': Destination address (e.g. user+ext@domain). Default is the
   same as username. (v1.1+ only)
 * '-f <address>': Envelope sender address. Currently this is used only for
   writing mbox's From_-header. The default is "MAILER-DAEMON".
 * '-c <path>': Alternative configuration file path.
 * '-m <mailbox>': Destination mailbox (default is INBOX). If the mailbox
   doesn't exist, it's created (unless -n is used). If message couldn't be
   saved to the mailbox for any reason, it's delivered to INBOX instead.
    * If Sieve plugin is used, this mailbox is used as the "keep" action's
      mailbox. It's also used if there is no Sieve script or if the script
      fails for some reason.
    * v1.1: Deliveries to namespace prefix will result in saving the mail to
      INBOX instead. For example if you have "Mail/" namespace, this allows you
      to specify 'deliver -n -m Mail/$mailbox' where mail is stored to
      Mail/$mailbox or to INBOX if $mailbox is empty.
 * '-n': If the destination mailbox doesn't exist, don't create it. This
   affects both '-m' parameter and 'fileinto' action in Sieve scripts. The
   fallback is to deliver mail to INBOX.
 * '-s': Subscribe to mailboxes that are automatically created (via '-m'
   parameter or 'fileinto' Sieve action). (v1.1.3+)
 * '-e': If mail gets rejected, write the rejection reason to stderr and exit
   with EX_NOPERM. The default is to send a rejection mail ourself (v1.0.1+).
 * '-k': Don't clear all environment at startup (v1.1+).
 * '-p <path>': Path to the mail to be delivered instead of reading from stdin.
   If using maildir the file is hard linked to the destination if possible.
   This allows a single mail to be delivered to multiple users using hard
   links, but currently it also prevents deliver from updating cache file so it
   shouldn't be used unless really necessary. (v1.1+)

System users
------------

You can use deliver with a few selected system users (ie. user is found from
'/etc/passwd' / NSS) by calling deliver in the user's '~/.forward' file:

---%<-------------------------------------------------------------------------
| "/usr/local/libexec/dovecot/deliver"
---%<-------------------------------------------------------------------------

This should work with any MTA which supports per-user '.forward' files.  For
qmail's per-user setup, see<LDA.Qmail.txt>.

This method doesn't require the authentication socket explained below since
it's executed as the user itself.

Virtual users
-------------

With a lookup
-------------

Give the destination username to deliver with '-d' parameter, for example:

---%<-------------------------------------------------------------------------
deliver -f $FROM_ENVELOPE -d $DEST_USERNAME
---%<-------------------------------------------------------------------------

You'll need to set up a master authentication socket for deliver so it knows
where to find mailboxes for the users:

---%<-------------------------------------------------------------------------
protocol lda {
..
  # UNIX socket path to master authentication server to find users.
  #auth_socket_path = /var/run/dovecot/auth-master
}
auth default {
..
  socket listen {
    # Note that we're setting a master socket. SMTP AUTH for Postfix and Exim
uses client sockets.
    master {
      path = /var/run/dovecot/auth-master
      # Auth master socket can be used to look up userdb information for
      # given usernames. This probably isn't very sensitive information
      # for most systems, but still try to restrict the socket access if
possible.
      mode = 0600
      user = vmail # User running deliver
      #group = mail # Or alternatively mode 0660 + deliver user in this group
    }
  }
..
}
---%<-------------------------------------------------------------------------

The master socket can be used to do <userdb> [UserDatabase.txt] lookups for
given usernames. Typically the result will contain the user's UID, GID and home
directory, but depending on your configuration it may return other information
as well. So the information is similar to what can be found from
eg.'/etc/passwd' for system users. This means that it's probably not a problem
to use mode=0666 for the socket, but you should try to restrict it more just to
be safe.

Without a lookup
----------------

If you have already looked up the user's home directory and you don't need a
userdb lookup for any other reason either (such as overriding settings for
specific users), you can run deliver similar to how it's run for system users:

---%<-------------------------------------------------------------------------
HOME=/path/to/user/homedir deliver -f $FROM_ENVELOPE
---%<-------------------------------------------------------------------------

This way you don't need to have a master listener socket. Note that you should
verify the user's existence prior to running deliver, otherwise you'll end up
having mail delivered to non-existing users as well.

You must have set the proper UID (and GID) before running deliver. It's not
possible to run deliver as root without '-d' parameter.

Multiple UIDs
-------------

If you're using more than one UID for users, you're going to have problems
running deliver. Most MTAs won't let you run deliver as root, so for now you'll
need to make it setuid root. However *it's insecure to make deliver
setuid-root*, especially if you have untrusted users in your
system.*Setuid-root deliver can be used to gain root privileges*. You should
take extra steps to make sure that untrusted users can't run it and potentially
gain root privileges. You can do this by making sure only your MTA has
execution access to it. For example:

---%<-------------------------------------------------------------------------
# chgrp secmail /usr/local/libexec/dovecot/deliver
# chmod 04750 /usr/local/libexec/dovecot/deliver
# ls -l /usr/local/libexec/dovecot/deliver
-rwsr-x--- 1 root secmail 4023932 2009-01-15 16:23 deliver
---%<-------------------------------------------------------------------------

Then start deliver as a user that belongs to secmail group.

Problems with deliver
---------------------

 * <Namespaces.txt> are supported with v1.1 and later. With v1.0 and older
   versions mails can be delivered only to mailboxes specified by
   the<mail_location> [MailLocation.txt] setting.
 * If you are using <prefetch userdb> [UserDatabase.Prefetch.txt], keep in mind
   that 'deliver' does not make a password query and thus will not work if '-d'
   parameter is used. The<UserDatabase.Prefetch.txt> page explains how to fix
   this.
    * See <Checkpassword> [PasswordDatabase.CheckPassword.txt] for how to make
      deliver work with checkpassword.

Logging
-------

 * Normally Dovecot logs everything through its master process, which is
   running as root. Deliver doesn't, which means that you might need some
   special configuration for it to log anything at all.
 * If deliver fails to write to log files it exits with temporary failure.
 * If you have trouble finding where Dovecot logs by default, see
   <Logging.txt>.
 * Note that Postfix's 'mailbox_size_limit' setting applies to all files that
   are written to. So if you have a limit of 50 MB, deliver can't write to log
   files larger than 50 MB and you'll start getting temporary failures.

If you want deliver to keep using Dovecot's the default log files:

 * If you're logging to syslog, make sure the syslog socket (usually
   '/dev/log') has enough write permissions for deliver. For example set it
   world-read/writable:'chmod a+rw /dev/log'.
 * If you're logging to Dovecot's default log files again you'll need to give
   enough write permissions to the log files for deliver.

You can also specify different log files for deliver. This way you don't have
to give any extra write permissions to other log files or the syslog socket.
You can do this by overriding the 'log_path' and 'info_log_path' settings:

---%<-------------------------------------------------------------------------
protocol lda {
 ..
  # remember to give proper permissions for these files as well
  log_path = /var/log/dovecot-deliver.log
  info_log_path = /var/log/dovecot-deliver.log
}
---%<-------------------------------------------------------------------------

For using syslog with deliver, set the paths empty:

---%<-------------------------------------------------------------------------
protocol lda {
 ..
  log_path =
  info_log_path =
  # You can also override the default syslog_facility:
  #syslog_facility = mail
}
---%<-------------------------------------------------------------------------

Plugins
-------

 * Most of the <Dovecot plugins> [Plugins.txt] work with deliver.
 * Virtual quota can be enforced using <Quota plugin> [Quota.txt].
 * Sieve language support can be added with <Sieve plugin> [LDA.Sieve.txt]. 

(This file was created from the wiki on 2009-02-04 04:42)
