最近一直忙着找工作,很久没有写博客了,这不最近老大说为公司建个邮件服务器吧,统一一下同事们的邮箱,笔者第一反应就是:postfix+mysql+extmail 解决方案,解决方案有了咱就开始着手干吧~
借鉴网上的一幅图片当用户通过extmail登陆时,首先extmail先去通过mysql进行虚拟用户认证,当认证通过时,postfix通过Cyrus-SASL这个函数库,这个认证框架,结合Courier-authlib对发信人进行认证,通过检索Mysql数据库中的数据来进行认证,以此来判定收件人所在的域是否为自己所负责的域,并且判断是否为其中继邮件。
需求:为公司建立邮件服务器,公司全部员工使用统一的域名 内网IP为:192.168.0.115 操作系统:RHEL5所用软件列表:mysql-5.5.20 postfix-2.9.1 courier-authlib-0.62.4.tar.bz2 extmail-1.2.tar.gz extman-1.1.tar.gz Unix-Syslog-1.1.tar.gz perl-GD-2.35-1.el5.rf.i386.rpm rrdtool-1.2.23 Time-HiRes-1.9725.tar.gz File-Tail-0.99.3.tar.gz |
四、为postfix开启基于cyrus-sasl认证功能 1、检查postfix是否支持cyrus-sasl认证postconf -a
2、配置postfix的主配置文件vim /etc/postfix/main.cf 在最后添加以下内容############################CYRUS-SASL############################ broken_sasl_auth_clients = yes 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_recipient_domain,reject_unauth_pipelining,reject_unauth_destination smtpd_sasl_auth_enable = yes smtpd_sasl_local_domain = $myhostname smtpd_sasl_security_options = noanonymous smtpd_sasl_application_name = smtpd smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!
说明:broken_sasl_auth_clients = yes:开启基于基于sasl认证客户端smtpd_recipient_restrictions:根据收件人地址进行过滤控制 permit_mynetworks:如果主机是mynetworks中定义的则允许 permit_sasl_authenticated:允许通过sasl认证的主机 reject_invalid_hostname:如果SMTP helo/ehlo命令中的主机名语法无效则拒绝 reject_non_fqdn_hostname:如果主机地址不是正规的FQDN格式则拒绝 reject_unknown_sender_domain:如果DNS无法解析发件人则拒绝 reject_unknown_recipient_domain:如果DNS无法解析收件人则拒绝 reject_non_fqdn_sender:如果发件人的地址不是正规的FQDN格式则拒绝 reject_non_fqdn_recipient:如果收件人的收件地址不是正规的FQDN格式则拒绝 reject_unauth_pipelining:拒绝验证未通过的 reject_unauth_destination:如果目标地址在relay_domains或my_destinations中则拒绝smtpd_sasl_auth_enable = yes:开启smtp基于sasl认证 smtpd_sasl_local_domain = $myhostname:sasl认证时$myhostname为本地域 smtpd_sasl_security_options = noanonymous:sasl认证不允许匿名用户 smtpd_sasl_application_name = smtpd:sasl应用程序名是smtpd smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!:smtpdbanner信息朋友们根据个人需要对配置进行修改vim /usr/lib/sasl2/smtpd.conf 添加如下内容:pwcheck_method: saslauthd mech_list: PLAIN LOGIN 重新载入postfix 3、验证此刻postfix是否支持认证五、安装courier-authlib 1、courier-authlib courier-authlib是Courier组件中的认证库,它是courier组件中一个独立的子项目,用于为Courier的其它组件提供认证服务。其认证功能通常包括验正登录时的帐号和密码、获取一个帐号相关的家目录或邮件目录等信息、改变帐号的密码等。而其认证的实现方式也包括基于PAM通过/etc/passwd和/etc/shadow进行认证,基于GDBM或DB进行认证,基于LDAP/MySQL/PostgreSQL进行认证等。因此,courier-authlib也常用来与courier之外的其它邮件组件(如postfix)整合为其提供认证服务。
2、安装courier-authlibtar jxvf courier-authlib-0.62.4.tar.bz2 cd courier-authlib-0.62.4 ./configure --prefix=/usr/local/courier-authlib --sysconfdir=/etc --without-authpam --without-authshadow --without-authvchkpw --without-authpgsql --with-authmysql --with-mysql-libs=/usr/local/mysql/lib --with-mysql-includes=/usr/local/mysql/include --with-redhat --with-authmysqlrc=/etc/authmysqlrc --with-authdaemonrc=/etc/authdaemonrc --with-ltdl-lib=/usr/lib --with-ltdl-include=/usr/include说明:此处请根据个人环境修改mysql的头文件以及库文件的路径make make install3、提供修改配置文件cp /etc/authdaemonrc.dist /etc/authdaemonrc cp /etc/authmysqlrc.dist /etc/authmysqlrc 修改/etc/authdaemonrc 文件authmodulelist="authmysql" authmodulelistorig="authmysql" daemons=10修改/etc/authmysqlrc
MYSQL_SERVER mysql.example.com MYSQL_USERNAME admin MYSQL_PASSWORD admin MYSQL_PORT 0 MYSQL_DATABASE mysql MYSQL_USER_TABLE passwd MYSQL_CRYPT_PWFIELD crypt MYSQL_UID_FIELD uid MYSQL_GID_FIELD gid MYSQL_LOGIN_FIELD id MYSQL_HOME_FIELD home MYSQL_NAME_FIELD name # MYSQL_MAILDIR_FIELD maildir |
MYSQL_SERVER localhost MYSQL_USERNAME extmail MYSQL_PASSWORD extmail MYSQL_PORT 3306 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) |
4、提供Sysv风格的服务脚本 在courier-authlib的源码解压目录下将courier-authlib.sysvinit复制到/etc/rc.d/init.d下cp courier-authlib.sysvinit /etc/rc.d/init.d/courier-authlib chmod a+x /etc/rc.d/init.d/courier-authlib 使用functions文件中的success failure函数对该脚本进行修改,修改过的脚本大家可以在附件中下载
echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf ldconfig chkconfig --add courier-authlib chkconfig courier-authlib on
新建虚拟用户邮箱所在目录mkdir –pv /var/mailbox chown –R postfix /var/mailbox 重新配置SMTP认证,编辑/usr/lib/sasl2/smtpd.conf修改内容为pwcheck_method: authdaemond log_level: 3 mech_list:PLAIN LOGIN authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket 六、让postfix支持虚拟域和虚拟用户 1、编辑/etc/postfix/main.cf,添加如下内容:########################Virtual Mailbox Settings######################## virtual_mailbox_base = /var/mailbox virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf virtual_alias_domains = virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf virtual_uid_maps = static:2525 virtual_gid_maps = static:2525 virtual_transport = virtual maildrop_destination_recipient_limit = 1 maildrop_destination_concurrency_limit = 1 ##########################QUOTA Settings######################## message_size_limit = 14336000 virtual_mailbox_limit = 20971520 virtual_create_maildirsize = yes virtual_mailbox_extended = yes virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf virtual_mailbox_limit_override = yes virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn his diskspace quota, please Tidy your mailbox and try again later. virtual_overquota_bounce = yes |
2、取消中心域 启用虚拟域以后,需要取消中心域,即注释掉myhostname, mydestination, mydomain, myorigin几个指令;当然,你也可以把mydestionation的值改为你自己需要的。 重启postfixservice postfix restart
七、安装配置dovecotyum install -y dovecot vim /etc/dovecot.conf 将对应部分内容修改为auth default { mechanisms = plain passdb sql { args = /etc/dovecot-mysql.conf } userdb sql { args = /etc/dovecot-mysql.conf } 编辑/etc/dovecot-mysql添加如下内容driver = mysql connect = host=/tmp/mysql.sock 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' 启动dovecotservice dovecot start八、安装extman 1、使用extman源码目录下docs目录中的extmail.sql和init.sql建立数据库由于extman版本较早,其sql脚本是针对mysql-5.0的,对于现在mysql-5.5的语法稍有不适用 因此编辑extmaul.sql执行 1,$s@TYPE=MyISAM@ENGINE=MyISAM@g 对sql脚本进行修改之后使用该脚本建立数据库mysql -uroot -p < extmail.sql mysql -uroot -p < init.sql cp mysql_virtual_* /etc/postfix/ 2、创建访问extmail数据库的用户并且赋予权限mysql> GRANT all privileges on extmail.* TO extmail@localhost IDENTIFIED BY 'extmail'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT all privileges on extmail.* TO extmail@127.0.0.1 IDENTIFIED BY 'extmail'; Query OK, 0 rows affected (0.00 sec) 3、安装配置apacheyum install httpd -y vim /etc/httpd/conf/httpd.conf 注释掉中心主机,启用虚拟主机 添加以下内容:<VirtualHost *:80> ServerName mail.huatuo.com DocumentRoot /var/www/extmail/html/ ScriptAlias /extmail/cgi /var/www/extmail/cgi Alias /extmail /var/www/extmail/html SuexecUserGroup postfix postfix ScriptAlias /extman/cgi /var/www/extman/cgi Alias /extman /var/www/extman/html </VirtualHost>4、提供extman网页并配置 将解压出来的extman移动到/var/www下mv extman-1.1 /var/www/extman cp /var/www/extman/webman.cf.default /var/www/extman/webman.cf vim /var/www/extman/webman.cf部分修改选项的说明: SYS_CONFIG = /var/www/extman/ SYS_LANGDIR = /var/www/extman/lang SYS_TEMPLDIR = /var/www/extman/html SYS_MAILDIR_BASE = /home/domains 此处即为您在前文所设置的用户邮件的存放目录,可改作: SYS_MAILDIR_BASE = /var/mailbox SYS_DEFAULT_UID = 1000 SYS_DEFAULT_GID = 1000 此两处后面设定的ID号需更改为前而创建的postfix用户和postfix组的id号,本文使用的是2525,因此,上述两项需要修改为: SYS_DEFAULT_UID = 2525 SYS_DEFAULT_GID = 2525 SYS_MYSQL_USER = webman SYS_MYSQL_PASS = webman 修改为: SYS_MYSQL_USER = extmail SYS_MYSQL_PASS = extmail SYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sock 修改为: SYS_MYSQL_SOCKET = /tmp/mysql.sock |
而后修改cgi目录的属主:chown -R postfix.postfix /var/www/extman/cgi/ 在apache的主配置文件中Extmail的虚拟主机部分,添加如下两行:ScriptAlias /extman/cgi /var/www/extman/cgi Alias /extman /var/www/extman/html 创建其运行时所需的临时目录,并修改其相应的权限:mkdir -pv /tmp/extman chown postfix.postfix /tmp/extman
5、提供extmail网页并配置 将解压出来的extman移动到/var/www下tar xvf extmail-1.2.tar.gz mv extmail-1.2 /var/www/extmail cd /var/www/extmail/ cp webmail.cf.default webmail.cf vim /var/www/extmail/webmail.cf部分修改说明: SYS_CONFIG = /var/www/extmail/ SYS_LANGDIR = /var/www/extmail/lang SYS_TEMPLDIR = /var/www/extmail/html SYS_G_ABOOK_FILE_PATH = /var/www/extmail/globabook.cf SYS_MESSAGE_SIZE_LIMIT = 5242880 用户可以发送的最大邮件 SYS_USER_LANG = en_US 语言选项,可改作: SYS_USER_LANG = zh_CN SYS_MAILDIR_BASE = /home/domains 此处即为您在前文所设置的用户邮件的存放目录,可改作: SYS_MAILDIR_BASE = /var/mailbox SYS_MYSQL_USER = db_user SYS_MYSQL_PASS = db_pass 以上两句句用来设置连接数据库服务器所使用用户名、密码和邮件服务器用到的数据库,这里修改为: SYS_MYSQL_USER = extmail SYS_MYSQL_PASS = extmail SYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sock 修改为: SYS_MYSQL_SOCKET = /tmp/mysql.sock SYS_MYSQL_HOST = localhost 指明数据库服务器主机名,这里默认即可 SYS_MYSQL_TABLE = mailbox SYS_MYSQL_ATTR_USERNAME = username SYS_MYSQL_ATTR_DOMAIN = domain SYS_MYSQL_ATTR_PASSWD = password 以上用来指定验正用户登录里所用到的表,以及用户名、域名和用户密码分别对应的表中列的名称;这里默认即可 SYS_AUTHLIB_SOCKET = /var/spool/authdaemon/socket 此句用来指明authdaemo socket文件的位置,这里修改为: SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket |
6、解决依赖关系 extmail将会用到perl的Unix::syslogd功能tar zxvf Unix-Syslog-1.1.tar.gz cd Unix-Syslog-1.1 perl Makefile.PL make make install 正常使用校验码 安装perl-GD模块可以解决rpm -ivh perl-GD-2.35-1.el5.rf.i386.rpm 安装连接mysql的驱动模块yum install -y perl-DBD-MySQL 启动apacheservice httpd start 九、验证 在浏览器中输入192.168.0.105
十、配置Mailgraph_ext,使用Extman的图形日志:
接下来安装图形日志的运行所需要的软件包Time::HiRes、File::Tail和rrdtool 首先在http://search.cpan.org 搜索:Time::HiRes和File::Tail:并下载 http://oss.oetiker.ch/rrdtool/pub/?M=D 下载rrdtool-1.2.23.tar.gz1、安装Time::HiRestar zxvf Time-HiRes-1.9707.tar.gz cd Time-HiRes-1.9707 perl Makefile.PL make make test make install2、安装File::Tailtar zxvf File-Tail-0.99.3.tar.gz cd File-Tail-0.99.3 perl Makefile.PL make make test make install3、安装rrdtool-1.2.23 yum install -y libpng-devel freetype tar zxvf rrdtool-1.2.23.tar.gz cd rrdtool-1.2.23 ./configure --prefix=/usr/local/rrdtool make make installcp -r /var/www/extsuite/extman/addon/mailgraph_ext /usr/local /usr/local/mailgraph_ext/mailgraph-init start 启动报错:/usr/local/mailgraph_ext/mailgraph-init start Starting mail statistics grapher: mailgraph_ext Can't locate RRDs.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /usr/local/mailgraph_ext/mailgraph_ext.pl line 292. BEGIN failed--compilation aborted at /usr/local/mailgraph_ext/mailgraph_ext.pl line 292. Starting queue statistics grapher: qmonitor Can't locate RRDs.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /usr/local/mailgraph_ext/qmonitor.pl line 8. BEGIN failed--compilation aborted at /usr/local/mailgraph_ext/qmonitor.pl line 8. |
解决方法:
ln -sv /usr/local/rrdtool/lib/perl/5.8.8/i386-linux-thread-multi/RRDs.pm /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/ ln -sv /usr/local/rrdtool/lib/perl/5.8.8/i386-linux-thread-multi/auto/RRDs/RRDs.so /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/(如果碰到少别的模块都可以用查找链接这种方法解决,找不到就是没安装,一个是模块,一个是库)/usr/local/mailgraph_ext/qmonitor-init start //启动ok/var/www/extman/daemon/cmdserver -d //打开系统信息 加入开机启动脚本中,达到开机启动的目的echo "/usr/local/mailgraph_ext/mailgraph-init start" >> /etc/rc.localecho “/var/www/extman/daemon/cmdserver -d” >> /etc/rc.d/rc.local 查看系统信息打开图形日志:
可以看出图形日志中已经有图形了。至此
Postfix+courier-authlib+dovecot+mysql+extmail构建企业邮件应用已经完成。 剩下只需要为服务器的公网IP添加为MX记录就OK了。