一、简介

      基于虚拟账号的邮件系统采用的技术如下图所示:

利用Postfix等搭建基于虚拟账号的邮件系统_第1张图片

      Postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软

件。Postfix是Wietse Venema想要为使用最广泛的sendmail提供替代品的一个尝

试。在Internet世界中,大部分的电子邮件都是通过sendmail来投递的,大约有100

万用户使用sendmail,每天投递上亿封邮件。这真是一个让人吃惊的数字。Postfix试

图更快、更容易管理、更安全,同时还与sendmail保持足够的兼容性。

      Postfix 是一种电子邮件服务器,它是由任职于IBM华生研究中心(T.J. Watson

Research Center)的荷兰籍研究员Wietse Venema为了改良sendmail邮件服务器

而产生的。最早在1990年代晚期出现,是一个开放源代码的软件。

Postfix特点

1. postfix是免费的;

2. 更快;

3. 兼容性好;

4. 更健壮;

5. 更灵活;

6. 安全性。

二、案例

首先搭建虚拟机环境:

地址设为:192.168.145.100

[root@localhost ~]# vim /etc/resolv.conf

编辑结果:

nameserver 192.168.145.100

[root@localhost ~]# vim /etc/sysconfig/network

编辑结果:

HOSTNAME=mail.a.org

[root@localhost ~]# vim /etc/hosts

编辑结果:

127.0.0.1               localhost.localdomain localhost

重启虚拟机,环境搭建完成。

挂载光盘:

[root@mail ~]# mkdir /mnt/cdrom

[root@mail ~]# mount /dev/cdrom /mnt/cdrom

[root@mail ~]# cd /mnt/cdrom/Server

编辑yum服务器:

[root@mail Server]# vim /etc/yum.repos.d/rhel-debuginfo.repo

编辑结果:

[rhel-server]
name=Red Hat Enterprise Linux serverbaseurl=file:///mnt/cdrom/Server
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release

搭建DNS服务器:

[root@mail Server]# yum install bind

[root@mail Server]# yum install bind-chroot

[root@mail Server]# yum install caching-nameserver

[root@mail Server]# cd /var/named/chroot/etc

[root@mail etc]# cp -p named.caching-nameserver.conf  named.conf

[root@mail etc]# vim named.conf

编辑结果:

options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";

        // Those options should be used carefully because they disable port
        // randomization
        // query-source    port 53;    
        // query-source-v6 port 53;

        allow-query     { any; };
        allow-query-cache { any; };
};

view localhost_resolver {
        match-clients      { any; };
        match-destinations { any; };
        recursion yes;
        include "/etc/named.rfc1912.zones";
};

区域声明:

[root@mail etc]# vim named.rfc1912.zones

编辑结果:

27 zone "a.org" IN {
28         type master;
29         file "a.org.zone";
30         allow-update { none; };
31 };

[root@mail etc]# cd ../var/named

[root@mail named]# cp -p localhost.zone a.org.zone

[root@mail named]# vim a.org.zone

编辑结果:

1 $TTL    86400
  2 @               IN SOA  ns.a.org.       root (
  3                                         42              ; serial (d. ada    ms)                                
  4                                         3H              ; refresh
  5                                         15M             ; retry
  6                                         1W              ; expiry
  7                                         1D )            ; minimum
  8            
  9                    IN NS           ns.a.org.
10 ns               IN A            192.168.145.100
11 mail             IN A            192.168.145.100
12 pop3           IN CNAME        mail
13 smtp           IN CNAME        mail
14 @               IN MX 10        mail

[root@mail named]# service named start
启动 named:                                               [确定]

[root@mail named]# rndc reload
server reload successful

[root@mail named]# chkconfig named on

DNS服务器搭建完成。

关闭sendmail:

[root@mail named]# service sendmail stop
关闭 sm-client:                                           [确定]
关闭 sendmail:                                            [确定]

[root@mail named]# chkconfig sendmail off

安装相关软件:

[root@mail named]# yum install httpd  php  php-mysql  mysql  mysql-server  mysql-devel  openssl-devel  dovecot  perl-DBD-MySQL  tcl  tcl-devel  libart_lgpl  libart_lgpl-devel libtool-ltdl  libtool-ltdl-devel  expect

启动mysql:

[root@mail named]# service mysqld start

[root@mail named]# chkconfig mysqld on

为mysql用户设置权限:

[root@mail named]# mysql

本地用户:

mysql> SET  PASSWORD FOR root@'localhost'=PASSWORD('redhat');
Query OK, 0 rows affected (0.00 sec)

mysql> SET  PASSWORD FOR root@'127.0.0.1'=PASSWORD('redhat');
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

远程用户:

mysql> GRANT  ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'redhat';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> \quit
Bye

安装配置postfix:

[root@mail ~]# tar -zxvf postfix-2.8.2.tar.gz -C /usr/local/src

[root@mail ~]# cd /usr/local/src

[root@mail src]# ls
postfix-2.8.2

[root@mail src]# cd postfix-2.8.2/

添加组和账号:

[root@mail postfix-2.8.2]# groupadd -g 2525 postfix

[root@mail postfix-2.8.2]# useradd -g postfix -u 2525 -s /sbin/nologin –M

postfix

[root@mail postfix-2.8.2]# groupadd -g 2526 postdrop

[root@mail postfix-2.8.2]# useradd -g postdrop -u 2526 -s /bin/false –M

postdrop

生成makefile文件:

[root@mail postfix-2.8.2]# make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl  -DUSE_TLS ' 'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2   -lssl –lcrypto'

编译:

[root@mail postfix-2.8.2]# make

安装:

[root@mail postfix-2.8.2]# make install

install_root: [/]

tempdir: [/usr/local/src/postfix-2.8.2] /tmp

config_directory: [/etc/postfix]

command_directory: [/usr/sbin]

daemon_directory: [/usr/libexec/postfix]

data_directory: [/var/lib/postfix]

html_directory: [no]

mail_owner: [postfix]

mailq_path: [/usr/bin/mailq]

manpage_directory: [/usr/local/man]

newaliases_path: [/usr/bin/newaliases]

queue_directory: [/var/spool/postfix]

readme_directory: [no]

sendmail_path: [/usr/sbin/sendmail]

setgid_group: [postdrop]

生成别名二进制文件:

[root@mail postfix-2.8.2]# newaliases

[root@mail postfix-2.8.2]# postfix start

[root@mail postfix-2.8.2]# vim /etc/postfix/main.cf

编辑结果:

75 myhostname = mail.a.org

83 mydomain = a.org

99 myorigin = $mydomain

113 inet_interfaces = all

161 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

260 mynetworks = 127.0.0.0/8

[root@mail postfix-2.8.2]# postfix stop
postfix/postfix-script: stopping the Postfix mail system

[root@mail postfix-2.8.2]# postfix start
postfix/postfix-script: starting the Postfix mail system

为postfix开启基于cyrus-sasl的认证功能:

[root@mail postfix-2.8.2]# vim /etc/postfix/main.cf

编辑结果:

653 ############################CYRUS-SASL############################
654 broken_sasl_auth_clients = yes
655 smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated    ,reject_invalid_hostname,reject_non_fqdn_hostname,reject_unknown_sender_    domain,reject_non_fqdn_sender,reject_non_fqdn_recipient,reject_unknown_r    ecipient_domain,reject_unauth_pipelining,reject_unauth_destination
656 smtpd_sasl_auth_enable = yes
657 smtpd_sasl_local_domain = $myhostname
658 smtpd_sasl_security_options = noanonymous
659 smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Ava    ilable!

[root@mail postfix-2.8.2]# cd /usr/lib/sasl2

[root@mail sasl2]# cp -p Sendmail.conf smtpd.conf

[root@mail sasl2]# vim smtpd.conf

编辑结果:

pwcheck_method:saslauthd
mech_list:LOGIN PLAIN

[root@mail sasl2]# service saslauthd start
启动 saslauthd:                                           [确定]

[root@mail sasl2]# chkconfig saslauthd on

安装Courier authentication library:

[root@mail ~]# tar -jxvf courier-authlib-0.63.1.20111230.tar.bz2 -C /usr/local/src

[root@mail ~]# cd /usr/local/src

[root@mail src]# ls
courier-authlib-0.63.1.20111230  postfix-2.8.2

[root@mail src]# cd courier-authlib-0.63.1.20111230/

[root@mail courier-authlib-0.63.1.20111230]# ./configure --prefix=/usr/local/courier-authlib --sysconfdir=/etc --with-authmysql --with-mysql-libs=/usr/lib/mysql --with-mysql-includes=/usr/include/mysql --with-redhat --with-authmysqlrc=/etc/authmysqlrc --with-authdaemonrc=/etc/authdaemonrc --with-ltdl-lib=/usr/lib --with-ltdl-include=/usr/include

[root@mail courier-authlib-0.63.1.20111230]# make

[root@mail courier-authlib-0.63.1.20111230]# make install

[root@mail courier-authlib-0.63.1.20111230]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon

[root@mail courier-authlib-0.63.1.20111230]# cp /etc/authdaemonrc.dist  /etc/authdaemonrc

[root@mail courier-authlib-0.63.1.20111230]# cp /etc/authmysqlrc.dist  /etc/authmysqlrc

[root@mail courier-authlib-0.63.1.20111230]# vim /etc/authdaemonrc

编辑结果:

authmodulelist="authmysql"
authmodulelistorig="authmysql"
daemons=10

[root@mail courier-authlib-0.63.1.20111230]# vim /etc/authmysqlrc

编辑结果:

MYSQL_SERVER            localhost
MYSQL_USERNAME          extmail
MYSQL_PASSWORD          extmail

MYSQL_PORT              3306

MYSQL_SOCKET            /var/lib/mysql/mysql.sock

MYSQL_DATABASE          extmail

MYSQL_USER_TABLE        mailbox

MYSQL_CRYPT_PWFIELD     password

MYSQL_UID_FIELD         2525

MYSQL_GID_FIELD         2525

MYSQL_LOGIN_FIELD       username

MYSQL_HOME_FIELD        concat('/var/mailbox/',homedir)

MYSQL_NAME_FIELD        name

MYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)

[root@mail courier-authlib-0.63.1.20111230]# cp courier-authlib.sysvinit /etc/init.d/courier-authlib

[root@mail courier-authlib-0.63.1.20111230]# chmod 755 /etc/init.d/courier-authlib

[root@mail courier-authlib-0.63.1.20111230]# chkconfig --add courier-authlib

[root@mail courier-authlib-0.63.1.20111230]# chkconfig --level 2345 courier-authlib on

[root@mail courier-authlib-0.63.1.20111230]# echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf

[root@mail courier-authlib-0.63.1.20111230]# ldconfig –v

[root@mail courier-authlib-0.63.1.20111230]# service courier-authlib start
Starting Courier authentication services: authdaemond

新建虚拟用户邮箱所在的目录,并将其权限赋予postfix用户:

[root@mail courier-authlib-0.63.1.20111230]# mkdir -pv /var/mailbox

[root@mail courier-authlib-0.63.1.20111230]# chown  -R postfix /var/mailbox

重新配置SMTP 认证:

[root@mail courier-authlib-0.63.1.20111230]# cd /usr/lib/sasl2

[root@mail sasl2]# vim smtpd.conf

编辑结果:

pwcheck_method:authdaemond
mech_list:LOGIN PLAIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket

让postfix支持虚拟域和虚拟用户

[root@mail ~]# vim /etc/postfix/main.cf

编辑结果:

661 ########################Virtual Mailbox Settings########################
662 virtual_mailbox_base = /var/mailbox
663 virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
664 virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.    cf
665 virtual_alias_domains =
666 virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
667 virtual_uid_maps = static:2525
668 virtual_gid_maps = static:2525
669 virtual_transport = virtual
670 maildrop_destination_recipient_limit = 1
671 maildrop_destination_concurrency_limit = 1

672 ##########################QUOTA Settings########################
673 message_size_limit = 14336000
674 virtual_mailbox_limit = 20971520
675 virtual_create_maildirsize = yes
676 virtual_mailbox_extended = yes
677 virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_li    mit_maps.cf
678 virtual_mailbox_limit_override = yes
679 virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn     his diskspace quota, please Tidy your mailbox and try again later.
680 virtual_overquota_bounce = yes

使用extman源码目录下docs目录中的extmail.sql和init.sql建立数据库:

[root@mail ~]# tar -zxvf extman-1.1.tar.gz -C /usr/local/src

[root@mail ~]# cd /usr/local/src

[root@mail src]# ls
courier-authlib-0.63.1.20111230  extman-1.1  postfix-2.8.2

[root@mail src]# cd extman-1.1/docs

[root@mail docs]# mysql -u root -p Enter password:

[root@mail docs]# mysql -u root -p <init.sql
Enter password:

[root@mail docs]# cp mysql*  /etc/postfix/

授予用户extmail访问extmail数据库的权限:

[root@mail docs]# mysql -u root -p
Enter password:

mysql> GRANT all privileges on extmail.* TO extmail@localhost IDENTIFIED BY 'extmail';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT all privileges on extmail.* TO [email protected] IDENTIFIED BY 'extmail';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES; 
Query OK, 0 rows affected (0.00 sec)

mysql> \quit
Bye

[root@mail docs]# cp mysql_virtual_* /etc/postfix/

[root@mail docs]# postfix stop
postfix/postfix-script: stopping the Postfix mail system

[root@mail docs]# postfix start
postfix/postfix-script: starting the Postfix mail system

说明:启用虚拟域以后,需要取消中心域,即注释掉myhostname, mydestination, mydomain, myorigin几个指令;当然,你也可以把mydestionation的值改为你自己需要的。

 

配置dovecot

[root@mail ~]# vim /etc/dovecot.conf

编辑结果:

mail_location = maildir:/var/mailbox/%d/%n/Maildir

auth default {
    mechanisms = plain
    passdb sql {
        args = /etc/dovecot-mysql.conf
    }
    userdb sql {
        args = /etc/dovecot-mysql.conf
    }

[root@mail ~]# vim  /etc/postfix/main.cf

编辑结果:

415 home_mailbox = Maildir/

[root@mail ~]# vim /etc/dovecot-mysql.conf  

编辑结果:

driver = mysql
connect = host=localhost dbname=extmail user=extmail password=extmail
default_pass_scheme = CRYPT
password_query = SELECT username AS user,password AS password FROM mailbox WHERE username = '%u'                           
user_query = SELECT maildir, uidnumber AS uid, gidnumber AS gid FROM mailbox WHERE username = '%u'

[root@mail ~]# service dovecot start
启动 Dovecot Imap:                                        [确定]

[root@mail ~]# chkconfig dovecot on

安装Extmail-1.2(先安装httpd)

[root@mail ~]# tar -zxvf extmail-1.2.tar.gz  -C /usr/local/src

[root@mail ~]# cd /usr/local/src

[root@mail src]# ls
courier-authlib-0.63.1.20111230  extmail-1.2  extman-1.1  postfix-2.8.2

[root@mail src]# cd extmail-1.2/

[root@mail extmail-1.2]# mkdir -pv /var/www/extsuite

[root@mail src]# mv extmail-1.2 /var/www/extsuite/extmail

[root@mail src]# cp /var/www/extsuite/extmail/webmail.cf.default 

/var/www/extsuite/extmail/webmail.cf

[root@mail src]# vim /var/www/extsuite/extmail/webmail.cf

编辑结果:

127 SYS_MAILDIR_BASE = /var/mailbox

139 SYS_MYSQL_USER = extmail
140 SYS_MYSQL_PASS = extmail

197 SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket

[root@mail src]# vim /etc/httpd/conf/httpd.conf

编辑结果:

231 User postfix
232 Group postfix

993
994 ServerName mail.a.org
995 DocumentRoot /var/www/extsuite/extmail/html/
996 ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
997 Alias /extmail /var/www/extsuite/extmail/html
998 SuexecUserGroup postfix postfix
999

依赖关系的解决:

[root@mail ~]# tar -zxvf Unix-Syslog-1.1.tar.gz -C /usr/local/src

[root@mail ~]# cd /usr/local/src

[root@mail src]# ls
courier-authlib-0.63.1.20111230  extman-1.1  postfix-2.8.2  Unix-Syslog-1.1

[root@mail src]# cd Unix-Syslog-1.1/

[root@mail Unix-Syslog-1.1]# perl Makefile.PL

[root@mail Unix-Syslog-1.1]# make

[root@mail Unix-Syslog-1.1]# service httpd start
启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using mail.a.org for ServerName                         [确定]

[root@mail Unix-Syslog-1.1]# chkconfig httpd on

安装Extman-1.1

[root@mail ~]# tar -zxvf extman-1.1.tar.gz -C /usr/local/src

[root@mail ~]# cd /usr/local/src

[root@mail src]# mv extman-1.1 /var/www/extsuite/extman

[root@mail src]#cp /var/www/extsuite/extman/webman.cf.default 

/var/www/extsuite/extman/webman.cf

[root@mail src]# vi /var/www/extsuite/extman/webman.cf

编辑结果:

12 SYS_MAILDIR_BASE = /var/mailbox

21 SYS_CAPTCHA_ON = 0

[root@mail src]# chown -R postfix.postfix /var/www/extsuite/extman/cgi/

[root@mail src]# vim /etc/httpd/conf/httpd.conf

编辑结果:


ServerName mail.a.org
DocumentRoot /var/www/extsuite/extmail/html/
ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi
Alias /extmail /var/www/extsuite/extmail/html
ScriptAlias /extman/cgi /var/www/extsuite/extman/cgi
Alias /extman /var/www/extsuite/extman/html
SuexecUserGroup postfix postfix

[root@mail src]# mkdir  -pv  /tmp/extman

[root@mail src]# chown postfix.postfix  /tmp/extman

[root@mail src]# service httpd restart
停止 httpd:                                               [确定]
启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using mail.a.org for ServerName                          [确定]

安装完成。