Postfix + MySQL + Dovecot and FreeBSD

postfix_mailadmin

In this how-to I’ll guide you how to install postfix with mysql & dovecot and I’ll also introduce a new tool named (PostfixMailAdmin) for managing postfix mysql users.

We all knows Postfix is fast, easy to administer, and secure. The outside has a definite Sendmail-ish flavor, but the inside is completely different.

Installing Postfix

cd /usr/ports/mail/postfix
make install clean

(Be sure “SASL2”, “TLS“, “MySQL“, and “VDA” are selected in the menu).

Note: If you want use dovecot as backend for SASL then also select DOVECOT from option menu.

I have already mentioned that i will introduce a new software for managing postfix virtual users. The software name is postfixmailadmin.

The main feature of this software is new web interface, easy to use, optimized search and vacation module.

You can download postfixmailadmin from this url: http://sourceforge.net/projects/postfixmiladmin/

Now Install PostfixMailAdmin

cd /var/www/html
tar -xzvf postfixmailadmin-1.0.1.tar.gz

To configure postfixmailadmin we have to follow the instructions in doc/INSTALL.txt file

After installing postfixmailadmin you need to create mysql configuration files for postfix.

Let us create mysql configuration files now.

Create file mysql_virtual_alias_maps.cf for forwarding emails from one email address to another

vi /usr/local//etc/postfix/mysql_virtual_alias_maps.cf 
user = postfix
password = mail
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'

Create file mysql_virtual_domains_maps.cf, for the virtual domains mapping. Virtual domains are queried using information provided in this file.

vi /usr/local/etc/postfix/mysql_virtual_domains_maps.cf 
user = postfix
password = mail
hosts = localhost
dbname = postfix
query = SELECT domain FROM domain WHERE domain='%s' and backupmx='0' and active='1'

Create the file mysql_virtual_mailbox_maps.cf. This is usually the mapping of email addresses to the location of the user’s mailbox on your hard disk.

vi /usr/local/etc/postfix/mysql_virtual_mailbox_maps.cf 
user = postfix
password = mail
hosts = localhost
dbname = postfix
query = SELECT CONCAT(domain,'/',maildir) FROM mailbox WHERE username='%s' AND active = '1'

Create file mysql_virtual_mailbox_limit_maps.cf which will be used for mapping user’s mailboxes quota limit.

vi /usr/local/etc/postfix/mysql_virtual_mailbox_limit_maps.cf 
user = postfix
password = mail
hosts = localhost
dbname = postfix
query = SELECT quota FROM mailbox WHERE username='%s'

We will check sender from address after sasl authentication. So senders not use our mail server as open relay.

vi /usr/local/etc/postfix/mysql_sender_check.cf
user = postfix
password = mail
hosts = localhost
dbname = postfix
query = SELECT username FROM mailbox WHERE username='%s' and active=1

Next create mysql_virtual_alias_domains_maps.cf. for alias domains.

vi mysql_virtual_alias_domains_maps.cf
user = postfix
password = mail
hosts = localhost
dbname = postfix
query = SELECT concat('@', target_domain ) FROM alias_domain WHERE concat('@', alias_domain) = '%s' AND active = '1'

Lastly create file mysql_relay_domains_maps.cf which will be used for relay domains.

vi /usr/local/etc/postfix/mysql_relay_domains_maps.cf 
user = postfix
password = mail
hosts = localhost
dbname = postfix
query = SELECT domain FROM domain WHERE domain='%s' and backupmx = '1'

Set permission.

cd /usr/local/etc/postfix/
chmod 640 mysql_*
chgrp postfix mysql_*

In next step we will create a user (vmail) with home directory (/home/vmail). You can change this path as per your choice, because in this directory all virtual users emails will be saved.

useradd vmail -u 150 -g mail -d /home/vmail -m -s /sbin/nologin -c "Virtual mailbox"
chmod 770 /home/vmail/ 

SSL-Key Setup:

Create directories, then private key and lastly the certificate.

 mkdir -p /home/mySSLkey/
 cd /home/mySSLkey/
openssl genrsa 1024 > mail-key.pem
chmod 400 mail-key.pem
openssl req -new -x509 -nodes -sha1 -days 365 -key mail-key.pem > mail-cert.pem

Configuring Postfix main.cf

Now copy cp /etc/aliases* /etc/postfix/

Everything is installed and configured as per requirements. The next step is to configure /usr/local/etc/postfix/main.cf

The following command is showing you what i have done in my configuration file.

  grep -v "^#" /usr/loacl/etc/postfix/main.cf | sed -e '/^$/d' 

Here is the output of above command

soft_bounce = no
smtpd_banner = $myhostname
biff = no
append_dot_mydomain = no
inet_interfaces = all
myhostname = mail.example.com
myorigin = $myhostname
mydomain = example.com
mynetworks = 127.0.0.0/8
mynetworks_style = host
relayhost =
mydestination = $myhostname, localhost.$mydomain, localhost
alias_maps = hash:$config_directory/aliases, $virtual_alias_maps
local_recipient_maps = $alias_maps, $virtual_mailbox_maps
local_transport = local
transport_maps = proxy:mysql:$config_directory/mysql_transport.cf
debug_peer_level = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         ddd $daemon_directory/$process_name $process_id & sleep 5
html_directory = /usr/local/share/doc/postfix
disable_vrfy_command = no
mailbox_size_limit = 0
owner_request_special = no
recipient_delimiter = +
home_mailbox = Maildir/
mail_owner = postfix
command_directory = /usr/local/sbin
daemon_directory = /usr/local/libexec/postfix
data_directory = /var/db/postfix
queue_directory = /var/spool/postfix
sendmail_path = /usr/local/sbin/sendmail
newaliases_path = /usr/local/bin/newaliases
mailq_path = /usr/local/bin/mailq
mail_spool_directory = /var/spool/mail
manpage_directory = /usr/local/man
setgid_group = maildrop
unknown_local_recipient_reject_code = 450
########### Virtual Domains Users ######
virtual_transport = virtual
virtual_alias_maps =
        proxy:mysql:$config_directory/mysql_virtual_alias_maps.cf,
        proxy:mysql:$config_directory/mysql_virtual_alias_domains_maps.cf
virtual_mailbox_domains = proxy:mysql:$config_directory/mysql_virtual_domains_maps.cf
virtual_mailbox_maps = proxy:mysql:$config_directory/mysql_virtual_mailbox_maps.cf
virtual_mailbox_limit_maps = proxy:mysql:$config_directory/mysql_virtual_mailbox_limit_maps.cf
virtual_mailbox_base = /home/vmail
relay_domains = proxy:mysql:$config_directory/mysql_relay_domains_maps.cf
proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_mailbox_maps $virtual_mailbox_domains $relay_domains $virtual_mailbox_limit_maps
virtual_minimum_uid = 150
virtual_uid_maps = static:150
virtual_gid_maps = static:6
virtual_create_maildirsize = yes
virtual_mailbox_extended = yes
virtual_mailbox_limit_override = yes
virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn his disk space quota.
virtual_overquota_bounce = yes
###### SASL Authentication ######
smtpd_sasl_auth_enable = yes
smtpd_sasl_authenticated_header = yes
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous
  • mydomain = example.com is Domain name
  • inet_interfaces = all It Listens on all Interfaces
  • myhostname = howto.example.com Hostname of mail server
  • mynetworks = 127.0.0.0/8 It means only this machine can use postfix as smtp
  • virtual_minimum_uid = 150 is User ID of vmail user
  • virtual_gid_maps = static:6 is Group ID of mail group

Installing Dovecot:

cd /usr/ports/mail/dovecot
make install clean

(Be sure “MySQL“, “SSL” are selected in the menu)

 

Configuring Dovecot:

Let’s configure Dovecot for imap and pop3 services. You need to configure 2 files, one for dovecot main configuration file and 2nd is for mysql connectivity.

Let’s first configure dovecot-mysql.conf file

vi /usr/local/etc/dovecot-mysql.conf 
driver = mysql
connect = host=localhost user=mail password=mail dbname=mail
default_pass_scheme = MD5-CRYPT
password_query = SELECT password FROM mailbox WHERE username = '%u'
user_query = SELECT '/home/vmail/%d/%n/Maildir' as home, 'maildir:/home/vmail/%d/%n/Maildir' as mail, 150 AS uid, 6 AS gid, concat('dirsize:storage=',quota) AS quota FROM mailbox WHERE username ='%u' AND active ='1'
  • 150 AS uid is userID of vmail
  • 6 AS gid is groupID of mail group

Now configure the dovecot.conf file. Keep this thing in mind that normal Linux users can’t login to mail-server for MySQL user authentication, so We should set first_valid_uid and last_valid_uid to 150, which means only the user with uid 150 will be able to log in.

vi /usr/local/etc/dovecot.conf 
base_dir = /var/run/dovecot/
protocols = imap pop3 imaps pop3s
protocol imap {
  listen = *:143
  ssl_listen = *:993
}

protocol pop3 {
     listen = *:110
     ssl_listen = *:995
}

mail_location = maildir:~/Maildir
disable_plaintext_auth = no
syslog_facility = mail
mail_access_groups = mail
mail_debug = no
first_valid_uid = 150
last_valid_uid = 150
first_valid_gid = 6
last_valid_gid = 6
maildir_copy_with_hardlinks = yes
log_timestamp = "%Y-%m-%d %H:%M:%S"
log_path = /var/log/maillog

auth default {
  mechanisms = plain login
  passdb sql {
  args = /usr/local/etc/dovecot-mysql.conf
  }
  userdb sql {
   args = /usr/local/etc/dovecot-mysql.conf
  }
  socket listen {
    client {
     path = /var/spool/postfix/private/auth
     mode = 0660
     user = postfix
     group = postfix
    }
  }
}

Now start required services

/usr/local/etc/rc.d/postfix start
/usr/local/etc/rc.d/dovecot start
/usr/local/etc/rc.d/mysql start
/usr/local/etc/rc.d/saslauth start

If you want to start required services to run at boot time then edit file /etc/rc.conf

posyfix_enable="YES"
dovecot_enable="YES"
mysql_enable="YES"
sasl_enable="YES" 

To test email system first create domain and users using web interface of postfixmailadmin.http://example.com/postfixmailadmin/

Then using webmail client send emails to other users

I hope you’ll enjoy this howto

One comment

  1. [...] here Tags: bsd, freebsd, install postfixmailadmin, postfix with mysql, postfix with [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*