Axeorcat.com

Would you prefer to be an axe or a cat? That is the question. // I Code. I Sysadmin. I Beer.
January 19, 2010

Create linux email account without shell account – Postfix/Dovecot virtual user setup

We want a virtual mail server setup i.e. we don’t want to have to create linux accounts on our server for every mailbox (for security and maintenance reasons).

Tell Postfix to use virtual mailboxes under a single vmail linux user/group with mail stored in maildir format under /home/vmail (Refs: http://www.postfix.org/VIRTUAL_README.html and ubuntu.

In main.cf comment out the mailbox_command line and setup the following:

virtual_mailbox_base = /home/vmail
virtual_mailbox_domains = /etc/postfix/vdomains
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_minimum_uid = 1000
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
virtual_alias_maps = hash:/etc/postfix/valiasmaps
home_mailbox = Maildir/
#mailbox_command = procmail -a "$EXTENSION"

Then we will create these files:

  • /etc/postfix/vdomains – contains each domain we want to process email for
  • /etc/postfix/valiasmaps – contains emails we want to forward to local or remote accounts (on change run “postmap valiasmaps”)
  • /etc/postfix/vmailbox – contains emails to be stored on this machine, defining the filesystem path for them (on change run “postmap vmailbox “)

Ok so now Postfix will store/forward incoming mail, but we need to tell dovecot to authenticate against plain files in dovecot.conf.

mail_location = maildir:/home/vmail/%d/%n
disable_plaintext_auth = no
passdb passwd-file {
  args = /etc/dovecot/passwd
}
userdb passwd-file {
  args = /etc/dovecot/users
}

Maintenance:

  • Each forwarded email address should be added to valiasmaps (+ run postmap)
  • Each locally managed email address must be added to vmailbox (+ run postmap), and to dovecot’s passwd & users file

The Server Files

/etc/dovecot/users

contact@mydomain.com::5000:5000::/home/vmail/mydomain.com/:/bin/false::

/etc/dovecot/passwd

contact@mydomain.com:$1$sPOUe4qI$v1ai6yA/RPiiW9AlArdoa0

/etc/postfix/vmailbox

# Define locally stored virtual email accounts here, one email account per line
# * First column is the public email address
# * Second column is the relative storage folder
# The folder format should match what you setup in the dovecot conf

contact@mydomain.com  ansdb.com/contact/

/etc/postfix/vdomains

# List all domains that this server will accept mail for
mydomain.com

/etc/postfix/valiasmaps

# Define virtual address to account.
# * First column is the public email address
# * Second column is a local unix account or another email address to forward to

postmaster@mydomain.com postmaster
contact@mydomain.com  bob@example.org

Little script to help out.

# save this as "adddovecotuser.sh"
echo "$1" > /tmp/user
user=`cat /tmp/user | cut -f1 -d "@"`
domain=`cat /tmp/user | cut -f2 -d "@"`
echo "$user@$domain::5000:5000::/home/vmail/$domain/:/bin/false::" >> /etc/dovecot/users

You can create Maildir directories like this by hand

/usr/bin/maildirmake.dovecot /home/vmail/$domain/$user 5000:5000
# save this as "mkdovecotpasswd.sh"
mkpasswd --hash=md5 $2  > /tmp/hash
echo "$1:`cat /tmp/hash`" >> /etc/dovecot/passwd

Leave a Comment