CentOS 6.4 x64 postfix + dovecot + 虚拟用户认证


第一, 首先必须安装 apacache  mysql  php    


CentOS 直接使用 yum 安装


yum -y install httpd httpd-devel mysql php-mysql mysql-server mysql-devel  mysql-test mysql-connector-odbc64 php pam-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker



第二,安装Postfix邮件



tar zxvf postfix-2.10.0.tar.gz


cd postfix-2.10.0


groupadd  -g 1200 postdrop


groupadd  -g 25 postfix


useradd  -u 25 -g 25 -G postdrop -s /sbin/nologin -M postfix


make -f Makefile.init makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql' 'AUXLIBS=-L/usr/lib64/mysql -lmysqlclient -lz -lm'


make install


其间需要配置,我们按照默认方式既可,一直按按回车,既为默认模式安装


输出以下信息:

----------------------------------------------------------------------------------------------------------------------------

    Warning: you still need to edit myorigin/mydestination/mynetworks

    parameter settings in /etc/postfix/main.cf.


    See also http://www.postfix.org/STANDARD_CONFIGURATION_README.html

    for information about dialup sites or about sites inside a firewalled

    network.


    BTW: Check your /etc/aliases file and be sure to set up aliases

    that send mail for root and postmaster to a real person, then run

    /usr/bin/newaliases.


-----------------------------------------------------------------------------------------------------------------------------


这里先关闭系统自带的sendmail


chkconfig --level 2345 sendmail off


激活别名功能  


newaliases


使用postconf 可查看支持


postconf -m |grep mysql  查看是否支持mysql




第三,下面配置postfix


vim /etc/postfix/main.cf


修改 myhostname 为邮件域名


myhostname = mail.11111.com    改成自己的主机名


myorigin = $mydomain


inet_interfaces = all


mynetworks = 127.0.0.0/8  


home_mailbox = Maildir/


启动 postfix 


postfix start

---------------------------------------------------------------------

postfix/postfix-script: starting the Postfix mail system

----------------------------------------------------------------------






第四,安装配置 dovecot


1. 新建 dovecot 用户


useradd  -M -s /sbin/nologin dovecot


编译安装 dovecot


tar zxvf dovecot-1.1.4.tar.gz 


cd dovecot-1.1.4


./configure --sysconfdir=/etc/ --with-mysql


make -j 2 && make install



2. 配置 dovecot


cp /etc/dovecot-example.conf  /etc/dovecot.conf


vim /etc/dovecot.conf


protocols = imap imaps pop3 pop3s


disable_plaintext_auth = no


ssl_disable = yes


mail_location = maildir:~/opt/mailbox/%d/%n/Maildir



3. 启动dovecot  (-c指定配置文件)


/usr/local/sbin/dovecot -c /etc/dovecot.conf



ps aux|grep dovecot 


lsof -i:110 

lsof -i:143




第五,添加mysql 虚拟用户支持


tar zxvf extman-1.1.tar.gz


cd extman-1.1/docs/


1.导入数据库


mysql -uroot -p < extmail.sql


mysql -uroot -p < init.sql



2. 拷贝mysql认证配置文件到 postfix 目录


cp mysql_virtual_* /etc/postfix/



3. 建虚拟用户vuser


groupadd -g 1000 vuser


useradd -u 1000 -g 1000 -s /sbin/nologin -M vuser


mkdir /opt/mailbox


chown -R vuser:vuser /opt/mailbox


4. 修改main.cf 配置文件,增加以下内容支持mysql 认证查询


vim /etc/postfix/main.cf




# extmail config here

virtual_mailbox_base = /opt/mailbox

virtual_uid_maps = static:1000

virtual_gid_maps = static:1000

virtual_transport = virtual

virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf

virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf

virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf

virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_limit_maps.cf




第六,搭建extmail


1. 安装依赖包.


tar zxvf Unix-Syslog-1.1.tar.gz


cd Unix-Syslog-1.1


perl Makefile.PL && make && make install


cd ..



tar zxvf DBI-1.616.tar.gz


cd DBI-1.616


perl Makefile.PL && make && make install


cd ..



tar zxvf DBD-mysql-4.020.tar.gz 


cd DBD-mysql-4.020


perl Makefile.PL && make && make install


cd ..



2. 安装 extmail


tar zxvf extmail-1.2.tar.gz


cp -r extmail-1.2 /opt/htdocs/extmail


cd /opt/htdocs/extmail/


chown -R postfix:postfix cgi/


cp webmail.cf.default webmail.cf


vim webmail.cf


SYS_CONFIG = /opt/htdocs/extmail/

SYS_LANGDIR = /opt/htdocs/extmail/lang

SYS_TEMPLDIR = /opt/htdocs/extmail/html

SYS_MAILDIR_BASE = /opt/mailbox

SYS_MYSQL_USER = extmail

SYS_MYSQL_PASS = extmail

SYS_MYSQL_DB = extmail

SYS_MYSQL_HOST = localhost

SYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sock

SYS_G_ABOOK_FILE_PATH = /opt/htdcos/extmail/globabook.cf



3. 配置httpd 虚拟主机


vim /etc/httpd/conf/httpd.conf


User vuser

Group vuser


<VirtualHost *:80>

    ServerAdmin [email protected]

    DocumentRoot /var/www/html/extmail/html

    ServerName mail.moxian.com

    ErrorLog logs/error.log

    CustomLog logs/access.log common

ScriptAlias /extmail/cgi/ "/var/www/html/extmail/cgi/"

    Alias /extmail "/var/www/html/extmail/html/"

</VirtualHost>





第七,搭建extman


1. 安装依赖包.


yum install gd-devel -y



tar zxvf GD-2.46.tar.gz 


cd GD-2.46


perl Makefile.PL && make && make install


cd ..



tar zxvf File-Tail-0.99.3.tar.gz


cd File-Tail-0.99.3


perl Makefile.PL && make && make install


cd ..



2. 安装图形日志依赖包rrdtool,rrdtool-perl  (centos 6.0以上的可用 yum 安装   yum -y install rrdtool rrdtool-perl ) 


centos默认yum源中没有这两个包,所以需要手动添加yum源来安装这两个包


vim /etc/yum.repos.d/CentOS-Base.repo


---------------------------------------------------------------

[dag]

name=Dag RPM Repository for Red Hat Enterprise Linux

baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag

gpgcheck=1

gpgkey=http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt

enabled=1

-------------------------------------------------------------------


yum install rrdtool rrdtool-perl -y




3. 安装extman


tar zxvf extman-1.1.tar.gz


cp -r extman-1.1 /opt/htdocs/extman


cd /opt/htdocs/extman/


chown -R postfix:postfix cgi/


mkdir /tmp/extman


chown -R postfix:postfix /tmp/extman/


chmod -R 777 /tmp/extman/



4. 修改extman配置参数, 让其支持MYSQL 查询


cp webman.cf.default webman.cf


vim webman.cf


SYS_CONFIG = /opt/htdocs/extman/

SYS_LANGDIR = /opt/htdocs/extman/lang

SYS_TEMPLDIR = /opt/htdocs/extman/html

SYS_MAILDIR_BASE = /opt/mailbox

SYS_SESS_DIR = /tmp/extman/

SYS_DEFAULT_UID = 1000

SYS_DEFAULT_GID = 1000

SYS_MYSQL_USER = webman

SYS_MYSQL_PASS = webman

SYS_MYSQL_DB = extmail

SYS_MYSQL_HOST = localhost

SYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sock



5. 修改虚拟主机配置文件,使其支持extman


在上面虚拟机中添加

<VirtualHost *:80>

    .......

    ScriptAlias /extman/cgi "/opt/htdocs/extman/cgi/"

    Alias /extman "/opt/htdocs/extman/html/"</VirtualHost>



6. 启动extman 中自带的图形化支持脚本


/opt/htdocs/extman/daemon/cmdserver -d


cp -a /opt/htdocs/extman/addon/mailgraph_ext/ /usr/local/


/opt/htdocs/extman/addon/mailgraph_ext/mailgraph-init start


/opt/htdocs/extman/addon/mailgraph_ext/qmonitor.pl &




第八,虚拟用户发信认证


1. 安装courier-authlib认证软件包


tar jxvf courier-authlib-0.62.4.tar.bz2


cd courier-authlib-0.62.4


./configure --prefix=/usr/local/courier-authlib --without-stdheaderdir --with-authmysql --with-mysql-libs=/usr/lib64/mysql/ --with-mysql-includes=/usr/include/mysql/  


make -j 8 && make install && make install-configure




2. 把courier-authlib链接库添加进系统默认支持库


vim /etc/ld.so.conf.d/courier-authlib.conf


/usr/local/courier-authlib/lib/courier-authlib




3. 编辑authdaemonrc,使其支持mysql认证


cd /usr/local/courier-authlib/etc/authlib/


vim authdaemonrc


authmodulelist="authmysql"

authmodulelistorig="authmysql"



chmod -R 755 /usr/local/courier-authlib/var/spool/authdaemon/




4. 编辑authmysqlrc设置和mysql的连接参数.


cd /usr/local/courier-authlib/etc/authlib/


vim authmysqlrc


MYSQL_SERVER            localhost

MYSQL_USERNAME          extmail

MYSQL_PASSWORD          extmail

MYSQL_SOCKET            /var/lib/mysql/mysql.sock

MYSQL_PORT              3306

MYSQL_DATABASE          extmail

MYSQL_USER_TABLE        mailbox

MYSQL_CRYPT_PWFIELD     password

MYSQL_UID_FIELD         1000

MYSQL_GID_FIELD         1000

MYSQL_LOGIN_FIELD       username

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

MYSQL_NAME_FIELD        name

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




5. 将脚本文件添加进默认环境变量中,并添加开机启动程序


cp /opt/software/courier-authlib-0.62.4/courier-authlib.sysvinit /usr/local/bin/courier-authlib 


chmod 755  /usr/local/bin/courier-authlib


courier-authlib start




6. 新建smtpd.conf文件,使postfix支持认证。


vim /usr/lib64/sasl2/smtpd.conf


----------------------------------------------------------------------------

pwcheck_method: authdaemond

mech_list:PLAIN LOGIN

authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket

-----------------------------------------------------------------------------


cp -a /usr/local/courier-authlib/sbin/authtest /usr/local/sbin/





第九,虚拟用户收信认证


1. 编辑dovecot.conf 文件 打开mysql认证,关闭默认pam认证


vim /etc/dovecot.conf

--------------------------------------------------------------------------------------------------


protocols = imap imaps pop3 pop3s  <---默认没有打开pop3,需要手动添加

disable_plaintext_auth = no       <------允许plain认证

ssl_disable = yes<------禁用ssl加密

mail_location = maildir:~/opt/mailbox/%d/%n/Maildir

mechanisms = plain

 #passdb pam {<--默认是打开的,需要注释掉

 #}

 passdb sql {

        args = /etc/dovecot-mysql.conf

    }

  userdb sql {

        args = /etc/dovecot-mysql.conf    

}


---------------------------------------------------------------------------------------------------



2. 创建dovecot-mysql.conf,设置和mysql连接及查询参数


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'

--------------------------------------------------------------------------------------------------------------------




3. 重启 dovecot  


pkill dovecot


dovecot -c /etc/dovecot.conf





第十,虚拟用户SASL身份认证


1. 重新编译 postfix


postfix stop


make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl  -DUSE_TLS ' 'AUXLIBS=-L/usr/lib64/mysql -lmysqlclient -lz -lm -L/usr/lib64/sasl2 -lsasl2   -lssl -lcrypto'


make -j 8


2. 升级postfix  (升级)


make upgrade


重新启动 postfix 


postfix start


postconf  -a  查看是否支持 cyrus



3. 修改main.cf配置文件,配置sasl认证参数


vim /etc/postfix/main.cf


在最后面增加 sasl认证


#sasl认证

smtpd_sasl_auth_enable = yes

broken_sasl_auth_clients = yes

smtpd_sasl_security_options = noanonymous

smtpd_recipient_restrictions =

        permit_mynetworks,

        permit_sasl_authenticated,

        reject_non_fqdn_hostname,

        reject_unknown_sender_domain,

        reject_non_fqdn_sender,

        reject_non_fqdn_recipient,

        reject_unauth_destination,

        reject_unauth_pipelining,

        reject_invalid_hostname

smtpd_sender_login_maps =

        mysql:/etc/postfix/mysql_virtual_sender_maps.cf,

        mysql:/etc/postfix/mysql_virtual_alias_maps.cf





邮件过滤 dkim-milter

--------------------------------------------------------------------------------------------------

rpm -ivh http://download4.fedora.redhat.com/pub/epel/5/`uname -i`/dkim-milter-2.8.3-8.el5.`uname -i`.rpm --nodeps
wget -O dkim-milter.tar.gz http://sourceforge.net/projects/dkim-milter/files/latest/download?source=files
tar zxvf dkim-milter.tar.gz
cd  dkim-milter-*
mkdir -p /etc/dkim-milter/keys/
cp ./dkim-filter/dkim-genkey.sh /etc/dkim-milter/keys/
cd /etc/dkim-milter/keys/
chmod +x ./dkim-genkey.sh
chown dkim-milter /etc/dkim-milter
chmod 700 /etc/dkim-milter
chgrp postfix /var/run/dkim-milter
chmod 770 /var/run/dkim-milter
./dkim-genkey.sh -r -d `hostname -f`
chown dkim-milter /etc/dkim-milter/keys/default.private
mv /etc/dkim-milter/keys/default.private /etc/dkim-milter/keys/default.key.pem

#会生成default.txt 和 default.private
#根据default.txt 里面的内容,将DNS的TXT记录添加好
#default.private已安置好,并改名为default.key.pem,可以使用了
#下面的命令,大家根据实际微调

echo "*@moxianmail.com:moxianmail.com:/etc/dkim-milter/keys/default.key.pem" >> /etc/mail/dkim-milter/keys/keylist

echo 'AutoRestart yes' >> /etc/mail/dkim-milter/dkim-filter.conf
echo 'AutoRestartRate 10/1h' >> /etc/mail/dkim-milter/dkim-filter.conf
echo 'Domain kn007.net' >> /etc/mail/dkim-milter/dkim-filter.conf
echo 'SubDomains yes' >> /etc/mail/dkim-milter/dkim-filter.conf
echo 'Selector default' >> /etc/mail/dkim-milter/dkim-filter.conf
echo 'KeyFile /etc/dkim-milter/keys/default.key.pem' >> /etc/mail/dkim-milter/dkim-filter.conf
echo 'Background yes' >> /etc/mail/dkim-milter/dkim-filter.conf
echo 'Socket inet:8891' >> /etc/mail/dkim-milter/dkim-filter.conf
echo 'Userid dkim-milter' >> /etc/mail/dkim-milter/dkim-filter.conf

echo 'smtpd_milters = inet:8891' >> /etc/postfix/main.cf
echo 'non_smtpd_milters = inet:8891' >> /etc/postfix/main.cf
echo 'milter_protocol = 2' >> /etc/postfix/main.cf
echo 'milter_default_action = accept' >> /etc/postfix/main.cf
chkconfig --level 345 dkim-milter on
service dkim-milter start
service postfix restart
cat /var/log/maillog  





 






错误 1


Can't locate CPAN.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).

BEGIN failed--compilation aborted.


yum -y install perl-CPAN



然后


perl -MCPAN -eshell


出现  cpan[1]>  


输入


install CGI






错误 2


warning: dict_nis_init: NIS domain name not set - NIS lookups disabled

postfix/smtpd[7256]: connect from unknown[113.116.237.125]

postfix/smtpd[7256]: warning: xsasl_cyrus_server_get_mechanism_list: no applicable SASL mechanisms

postfix/smtpd[7256]: fatal: no SASL authentication mechanisms

postfix/master[7247]: warning: process /usr/libexec/postfix/smtpd pid 7256 exit status 1

postfix/master[7247]: warning: /usr/libexec/postfix/smtpd: bad command startup -- throttling





ln -s /usr/local/lib/sasl2 /usr/lib/sasl2



yum -y install cyrus-sasl-plain cyrus-sasl-sql





=====================================================================================


常用命令维护:


查询邮件队列:postqueue -p


清除所有邮件队列:postsuper -d ALL

你可能感兴趣的:(postfix)