virtual mailbox (postfix + dovecot + mysql)

最後更新: 2015-10-08

介紹

 


Step 0: 安裝有關 Package

yum install cyrus-sasl-plain

yum install dovecot dovecot-mysql dovecot-pigeonhole

yum install postfix

yum install mariadb-server

Step 1: 建立 system user

groupadd -g 2000 vmail

useradd -u 2000 -g 2000 -d /var/vmail vmail -m

Step 2: Create mysql tables

Table: domain

CREATE TABLE `domain` (
  `domain` varchar(255) NOT NULL,
  `description` varchar(255) CHARACTER SET utf8 NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`domain`),
  KEY `active` (`active`)
) ENGINE=MyISAM DEFAULT CHARSET='utf8';

Table: mailbox

CREATE TABLE `mailbox` (
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `maildir` varchar(255) NOT NULL,
  `domain` varchar(255) NOT NULL,
  `enablesmtp` tinyint(1) NOT NULL DEFAULT '1',
  `enablesmtpsecured` tinyint(1) NOT NULL DEFAULT '1',
  `enablepop3` tinyint(1) NOT NULL DEFAULT '1',
  `enablepop3secured` tinyint(1) NOT NULL DEFAULT '1',
  `enableimap` tinyint(1) NOT NULL DEFAULT '1',
  `enableimapsecured` tinyint(1) NOT NULL DEFAULT '1',
  `enabledeliver` tinyint(1) NOT NULL DEFAULT '1',
  `enablelda` tinyint(1) NOT NULL DEFAULT '1',
  `enablemanagesieve` tinyint(1) NOT NULL DEFAULT '1',
  `enablemanagesievesecured` tinyint(1) NOT NULL DEFAULT '1',
  `enablesieve` tinyint(1) NOT NULL DEFAULT '1',
  `enablesievesecured` tinyint(1) NOT NULL DEFAULT '1',
  `enableinternal` tinyint(1) NOT NULL DEFAULT '1',
  `enabledoveadm` tinyint(1) NOT NULL DEFAULT '1',
  `enablelib-storage` tinyint(1) NOT NULL DEFAULT '1',
  `enabledsync` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT 'dsync',
  `active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`username`),
  KEY `domain` (`domain`),
  KEY `enablesmtp` (`enablesmtp`),
  KEY `enablesmtpsecured` (`enablesmtpsecured`),
  KEY `enablepop3` (`enablepop3`),
  KEY `enablepop3secured` (`enablepop3secured`),
  KEY `enableimap` (`enableimap`),
  KEY `enableimapsecured` (`enableimapsecured`),
  KEY `enabledeliver` (`enabledeliver`),
  KEY `enablelda` (`enablelda`),
  KEY `enablemanagesieve` (`enablemanagesieve`),
  KEY `enablemanagesievesecured` (`enablemanagesievesecured`),
  KEY `enablesieve` (`enablesieve`),
  KEY `enablesievesecured` (`enablesievesecured`),
  KEY `enableinternal` (`enableinternal`),
  KEY `enabledoveadm` (`enabledoveadm`),
  KEY `enablelib-storage` (`enablelib-storage`),
  KEY `active` (`active`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

Step 3: postfix

main.cf

inet_interfaces = ipv4

proxy_read_maps = $virtual_mailbox_domains $virtual_mailbox_maps

message_size_limit = 15728640

virtual_minimum_uid = 2000
virtual_uid_maps = static:2000
virtual_gid_maps = static:2000

virtual_mailbox_base = /var/vmail
home_mailbox = Maildir/

virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql/virtual_mailbox_domains.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql/virtual_mailbox_maps.cf

smtpd_recipient_restrictions = reject_unknown_sender_domain,
 reject_unknown_recipient_domain,
 reject_non_fqdn_sender,
 reject_non_fqdn_recipient,
 reject_unlisted_recipient,
 permit_mynetworks,
 reject_unauth_destination

# 轉發 E-Mail 的設定
mailbox_command = /usr/libexec/dovecot/deliver
virtual_transport = dovecot
dovecot_destination_recipient_limit = 1

# sasl 設定
smtpd_sasl_type = dovecot
smtpd_sasl_path = ./dovecot-auth

smtp_helo_name = mail.lifebuddies.hk

default_process_limit = 20

bounce_queue_lifetime = 4h
maximal_queue_lifetime = 1d

# disable local user
local_recipient_maps =

postconf myhostname=X.X

master.cf

# 轉發 E-Mail 的設定
dovecot unix    -       n       n       -       -      pipe
    flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${user}@${domain} -m ${extension}

postfix to mysql

mkdir /etc/postfix/mysql

/etc/postfix/mysql/virtual_mailbox_domains.cf

user        = vmail
password    = ????
hosts       = 127.0.0.1
port        = 3306
dbname      = vmail
query       = SELECT domain FROM domain WHERE domain='%s' AND active=1

/etc/postfix/mysql/virtual_mailbox_maps.cf

user        = vmail
password    = ????
hosts       = 127.0.0.1
port        = 3306
dbname      = vmail
query       = SELECT CONCAT('/var/vmail/', mailbox.domain, '/', mailbox.maildir, '/Maildir/') FROM mailbox,domain
 WHERE mailbox.username='%s' AND
 mailbox.active=1 AND
 mailbox.enabledeliver=1 AND
 domain.domain = mailbox.domain AND
 domain.active=1

chroot 用到的 file

mkdir /var/spool/postfix/etc

cd /etc

cp hosts localtime resolv.conf services /var/spool/postfix/etc

建立 alias

newaliases

P.S.

/etc/postfix/aliases -> /etc/aliases

Step 4: dovecot

dovecot.conf

# 如果想 Disable SSL, 就要在這裡, 在 local.conf 是不行的
ssl = no

protocols = imap pop3 lmtp

dict {
}

!include_try /etc/dovecot/local.conf

local.conf

# User/group who owns the message files:
mail_uid = 2000
mail_gid = 2000

# Assign uid to virtual users.
first_valid_uid = 2000
last_valid_uid = 2000

# Mail location and mailbox format.
mail_location = maildir:/%Lh/Maildir/:INDEX=/%Lh/Maildir/


# Virtual mail accounts.
userdb {
    args = /etc/dovecot/dovecot-mysql.conf
    driver = sql
}
passdb {
    args = /etc/dovecot/dovecot-mysql.conf
    driver = sql
}

service auth {
    unix_listener /var/spool/postfix/dovecot-auth {
        user = postfix
        group = postfix
        mode = 0666
    }
    unix_listener auth-userdb {
        user = vmail
        group = vmail
        mode = 0660
    }
}

/etc/dovecot/dovecot-mysql.conf

driver = mysql
default_pass_scheme = CRYPT
connect = host=127.0.0.1 dbname=vmail user=vmail password=??????
# Required by 'doveadm mailbox ...'.
iterate_query = SELECT username AS user FROM mailbox
password_query = SELECT password FROM mailbox WHERE username='%u' AND active='1'
user_query = SELECT \
    '%u' AS master_user, \
    CONCAT(mailbox.storagebasedirectory, '/', mailbox.storagenode, '/', mailbox.maildir) AS home, \
    CONCAT('*:bytes=', mailbox.quota*1048576) AS quota_rule \
FROM mailbox,domain \
WHERE mailbox.username='%u' \
    AND mailbox.domain='%d' \
    AND mailbox.`enable%Ls%Lc`=1 \
    AND mailbox.domain=domain.domain \
    AND domain.active=1 \
    AND mailbox.active=1

Data 的 Location

ln -s /home/vmail /var/vmail

 

Dovecot 有關的 log File

cd /var/log/

touch dovecot-sieve.log dovecot-lmtp.log dovecot.log

chown dovecot.vmail /var/log/dovecot*log

chmod 660 /var/log/dovecot*log

/etc/logrotate.d/dovecot

/var/log/dovecot.log
/var/log/dovecot-lmtp.log
/var/log/dovecot-sieve.log {
    compress
    weekly
    rotate 10
    create 0660 dovecot vmail
    missingok

    # Use bzip2 for compress.
    #compresscmd bzip2
    #uncompresscmd bunzip2
    #compressoptions -9
    #compressext .bz2

    postrotate
        doveadm log reopen
    endscript
}

Auto Startup

systemctl enable dovecot

systemctl enable postfix

Step 5: Configure file permission

postfix

chmod 640 /etc/postfix/mysql/virtual_mailbox_domains.cf

chgrp postfix /etc/postfix/mysql/virtual_mailbox_domains.cf

chmod 640 /etc/postfix/mysql/virtual_mailbox_maps.cf

chgrp postfix /etc/postfix/mysql/virtual_mailbox_maps.cf

dovecot

chmod 640 /etc/dovecot/dovecot-mysql.conf

chgrp dovecot /etc/dovecot/dovecot-mysql.conf
 

Step 6: firewall rule

# e-mail
firewall-cmd --add-port=143/tcp --permanent

firewall-cmd --add-port=110/tcp --permanent

firewall-cmd --add-port=1025/tcp --permanent

firewall-cmd --add-port=25/tcp --permanent

firewall-cmd --add-port=80/tcp --permanent

 



用 Configure

 

cd /etc/postfix/mysql

sed -i 's/OLDPW/NEWPW/g' *

cd /etc/dovecot/

sed -i 's/OLDPW/NEWPW/g' *

 

附加檔案大小
setting.tar_.gz36.09 KB