基于虚拟账号的邮件系统(RPM篇)

小编:前几天给大家给大家介绍了古老的邮件系统SendMail的简单应用,今天来给大家谈一下在SenMail基础上异军突起的邮件系统Postfix,并且结合Extmail的Web方式进行邮件的收发,由于它的界面非常友好而深受企业用户的亲睐。今天就在RPM的基础上结合源码,来对Postfix进行一个比较深刻的了解,并且实现基于虚拟账号的邮件系统,下一次的博客中,我们将会基于LAMP环境,来搭建基于虚拟账号的邮件系统.

系统环境

Red Hat Enterprise Linux Server release 5.4

内核版本(2.6.18-164.e15)

准备工作

下载Postfix最新源码包:http://www.postfix.org/

(postfix-2.10.0.tar.gz)

下载Extmail最新源码包:http://www.extmail.org/

(extmail-1.2.tar.gz)

下载Extman最新源码包:http://wiki.extmail.org/extman/

(extman-1.1.tar.gz)

下载Courier-Authlib最新源码包:http://www.courier-mta.org/authlib/

(courier-authlib-0.62.4.tar)

下载Unix-Syslog最新源码包:http://search.cpan.org/dist/Unix-Syslog/

(Unix-Syslog-1.1.tar.gz)

实验目的与流程分析图

基本环境搭建

Step1:搭建DNS服务器,添加新的区域163.com

①配置本地Yum客户端.

mkdir  /mnt/cdrom

mount  /dev/cdrom  /mnt/cdrom

cd  /mnt/cdrom/Server

vim  /etc/yum.repos.d/rhel-debuginfo.repo

②yum安装DNS需要的安装包.

yum  install -y  bind  bind-chroot caching-nameserver

③配置DNS服务器,添加新区域.

cd  /var/named/chroot/etc/

cp  -p  named.caching-nameserver.conf  named.conf

vim  named.conf

vim  named.rfc1912.zones

cd  ../var/named/

cp  -p  localhost.zone  163.com.zone

vim  163.com.zone

③修改系统的主机名.

vim  /etc/sysconfig/network

④修改系统hosts文件.

vim  /etc/hosts

hostname  mail.163.com

⑤修改系统DNS指向文件.

vim  /etc/resolv.conf

⑥启动DNS服务器,并进行解析测试

service  named  start

Step2:RPM包搭建基本的Web环境.

①yum安装Web环境所需的rpm包.

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

②关闭SendMail服务,并修改开机启动级别.

service  sendmail  stop

chkconfig  sendmail  off

③启动mysql数据库,并给mysql的root用户设置管理密码.

service  mysqld  start

chkconfig  mysqld  on

mysqladmin  -u  root  password  '123456'

下面的方法也是可以的:

1>授权本地用户

SET  PASSWORD FOR root@'localhost'=PASSWORD('123456');

SET  PASSWORD FOR root@'127.0.0.1'=PASSWORD('123456');

FLUSH PRIVILEGES;

2>授权远程用户

GRANT  ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '123456';

FLUSH PRIVILEGES;

邮件服务器环境搭建

Step1:源码安装和配置postfix.

①实现创建postfix服务的所属组和nologin账号.

groupadd -g 2525 postfix

useradd -g postfix -u 2525 -s /sbin/nologin -M postfix

groupadd -g 2526 postdrop

useradd -g postdrop -u 2526 -s /bin/false -M postdrop

②拆解源码包到指定路径下,并进行源码安装.

tar  zxvf  postfix-2.10.0.tar.gz  -C  /usr/local/src

cd   /usr/local/src/postfix-2.10.0

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'

make

make install

按照以下的提示输入相关的路径

([]号中的是缺省值,”]”后的是输入值,省略的表示采用默认值)

install_root: [/]  /

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

config_directory: [/etc/postfix]  /etc/postfix

daemon_directory: [/usr/libexec/postfix]

command_directory: [/usr/sbin]

queue_directory: [/var/spool/postfix]

sendmail_path: [/usr/sbin/sendmail]

newaliases_path: [/usr/bin/newaliases]

mailq_path: [/usr/bin/mailq]

mail_owner: [postfix]

setgid_group: [postdrop]  

html_directory: [no]  /var/www/postfix_html

manpages: [/usr/local/man]

readme_directory: [no]

③生成别名二进制文件,这个步骤如果忽略,会造成postfix效率极低.

newaliases

④修改Postfix的主配置文件,进行一些基本配置.测试启动postfix并进行发信

vim  /etc/postfix/main.cf

修改以下几项配置参数.

myhostname = mail.163.com

(指定运行postfix邮件系统的主机的主机名,默认情况下,其值被设定为本地机器名)

myorigin = 163.com (用来指明发件人所在的域名)

mydomain = $mydomain

(指定域名,默认情况下,postfix将myhostname的第一部分删除而作为mydomain的值)

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

(指定postfix接收邮件时收件人的域名,即postfix系统要接收到哪个域名的邮件)

mynetworks = 127.0.0.0/8

(指定你所在的网络的网络地址,postfix系统根据其值来区别用户是远程的还是本地的,如果是本地网络用户则允许其访问)

⑤为了实现postfix的service管理,拷贝生成服务的启动控制脚本.

mkdir  /tmp/abc

cd  /mnt/cdrom/Server

cp   postfix-2.3.3-2.1.e15_2.i386.rpm  /tmp/abc

cd  /tmp/abc

rpm2cpio  postfix-2.3.3-2.1.e15_2.i386.rpm  | cpio -id

cp  -p  etc/rc.d/init.d/postfix   /etc/init.d/

⑥启动postfix服务.

service  postfix  start

chkconfig  postfix  on

⑦Telnet连接postfix,验证服务启动是否正常.

telnet localhost 25

Trying 127.0.0.1...

Connected to localhost.localdomain (127.0.0.1).

Escape character is '^]'.

220 mail.163.com ESMTP Postfix

ehlo mail.163.com

250-mail.163.com

250-PIPELINING

250-SIZE 10240000

250-VRFY

250-ETRN

250-ENHANCEDSTATUSCODES

250-8BITMIME

250 DSN

mail from:[email protected]

250 2.1.0 Ok

rcpt to:[email protected]

250 2.1.5 Ok

data

354 End data with <CR><LF>.<CR><LF>

subject:111

2222

.

250 2.0.0 Ok: queued as AB94A1A561

quit

221 2.0.0 Bye

Connection closed by foreign host.

⑧切换到user1用户进行收信测试.

su - user1

mail

Mail version 8.1 6/6/93.  Type ? for help.

"/var/spool/mail/user1": 1 message 1 new

>N  1 [email protected]        Wed Sep  5 10:59  15/488   "111"

&

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

①验证postfix是否支持cyrus风格的sasl认证

postconf  -a 的结果如下:

cyrus

Dovecot

②修改postfix的主配置文件,添加cyrus-sasl认证参数.

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_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!

③修改postfix的主配置文件,添加cyrus-sasl认证参数.

vim  /usr/lib/sasl2/smtpd.conf

添加如下内容:

pwcheck_method: saslauthd

mech_list: PLAIN LOGIN

④启动sasl认证服务,并修改服务的启动级别,并且重启postfix服务.

service  saslauthd  start

chkconfig  saslauthd  on

service  postfix  restart

⑤Telnet连接postfix,验证验证服务启动是否正常.

# telnet localhost 25

Trying 127.0.0.1...

Connected to localhost.localdomain (127.0.0.1).

Escape character is '^]'.

220 Welcome to our mail.163.com ESMTP,Warning: Version not Available!

ehlo mail.163.com

250-mail.163.com

250-PIPELINING

250-SIZE 10240000

250-VRFY

250-ETRN

250-AUTH PLAIN LOGIN   (出现本行和下行证明postfix结合认证成功)

250-AUTH=PLAIN LOGIN     

250-ENHANCEDSTATUSCODES

250-8BITMIME

250 DSN

Step3:安装Courier-Authlib,并进行链接数据库的相关配置.

①源码安装courier-authlib.

tar  xvf  courier-authlib-0.62.4.tar

cp  courier-authlib-0.62.4   /usr/local/src/courier-authlib

cd  /usr/local/src/courier-authlib

./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

make

make install

②修改courier-authlib服务的目录的权限,使具有执行权限.

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

③拷贝生成服务的两个模块的配置脚本,并进行修改.

cp /etc/authdaemonrc.dist  /etc/authdaemonrc

cp /etc/authmysqlrc.dist  /etc/authmysqlrc

vim  /etc/authdaemonrc (改动如下)

authmodulelist="authmysql"

authmodulelistorig="authmysql"

daemons=10

vim  /etc/authmysqlrc(改动如下)

MYSQL_SERVER  localhost

MYSQL_PORT  3306   (指定mysql监听的端口,这里使用默认的3306)

MYSQL_USERNAME  extmail    (这时后面要用的数据库的所有者的用户名)

MYSQL_PASSWORD  extmail    (这时后面要用的数据库的所有者的密码)

MYSQL_SOCKET  /var/lib/mysql/mysql.sock(源码安装的mysql在/tmp下)

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)

④拷贝生成courier-authlib的服务启动脚本,并修改权限.

cp  courier-authlib.sysvinit  /etc/init.d/courier-authlib

chmod  755  /etc/init.d/courier-authlib

⑤将courier-authlib的库文件加入到系统库中.

echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf

ldconfig

⑥启动courier-authlib服务.

service courier-authlib  start

chkconfig  courier-authlib  on

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

mkdir -pv  /var/mailbox

chown �CR  postfix  /var/mailbox

⑧编辑/usr/lib/sasl2/smtpd.conf

确保具有以下内容:

vim  /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的主配置,让postfix支持虚拟域和虚拟用户.

vim  /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

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

tar  zxvf  extman-1.1.tar.gz

cd  extman-1.1/docs

mysql -u root -p <extmail.sql

mysql -u root -p <init.sql

Mysql -u root -p

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

mysql> GRANT all privileges on extmail.* TO extmail@localhost IDENTIFIED BY 'extmail';

mysql> GRANT all privileges on extmail.* TO [email protected] IDENTIFIED BY 'extmail';

mysql>FLUSH PRIVILEGES; 

cp  mysql_virtual_*  /etc/postfix/

⑿编辑postfix的主配置文件,禁用以下行.

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

⒀重启postfix服务.

service  postfix  restart

Step4:进行dovecot的相关配置.

①编辑dovecot的主配置文件.

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

}

}把userdb的其他相关禁用

②编辑postfix的主配置文件.

vim  /etc/postfix/main.cf (修改以下行)

home_mailbox = Maildir/

③生成dovecot链接数据库的配置文件.

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'

④启动dovecot服务并修改开机启动级别.

service dovecot start

chkconfig dovecot on

Step5:进行Extmail的相关配置.

①解压缩源码包.

tar  zxvf  extmail-1.2.tar.gz

mkdir -pv  /var/www/extsuite

mv extmail-1.2 /var/www/extsuite/extmail

mv extman-1.1 /var/www/extsuite/extman

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

②修改主配置文件

vim  /var/www/extsuite/extmail/webmail.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_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

SYS_LOG_ON = 0

③进行apache相关配置.

<VirtualHost 192.168.145.100:80>

ServerName mail.163.com

DocumentRoot  /var/www/extsuite/extmail/html/

ScriptAlias /extmail/cgi /var/www/extsuite/extmail/cgi

Alias /extmail /var/www/extsuite/extmail/html

</VirtualHost>

User postfix

Group postfix

④修改 cgi执行文件运行身份为postfix.

chown -R  postfix.postfix /var/www/extsuite/extmail/cgi/

⑤解决依赖关系.

extmail会用到perl的Unix-syslogd功能,所以需要打个补丁.

tar  zxvf  Unix-Syslog-1.1.tar.gz  -C  /usr/local/src

cd  /usr/local/src/Unix-Syslog-1.1

perl Makefile.PL

make

make install

⑥启动apache服务.

service  httpd  start

chkconfig  httpd  on

Step6:进行Extman的相关配置.

①解压缩源码包.

tar zxvf  extman-1.1.tar.gz

mv  extman-1.1  /var/www/extsuite/extman

②拷贝生成extman配置文件,编辑配置.

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

vim  /var/www/extsuite/extman/webman.cf(修改以下参数)

SYS_MAILDIR_BASE = /home/domains

改为:SYS_MAILDIR_BASE = /var/mailbox

SYS_CAPTCHA_ON = 1

改为:SYS_CAPTCHA_ON = 0

③修改 cgi执行文件运行身份为postfix.

chown -R  postfix.postfix /var/www/extsuite/extman/cgi/

④进行apache相关配置.

<VirtualHost 192.168.145.100:80>

ServerName mail.163.com

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

</VirtualHost>

service  httpd  restart

⑤创建其运行时所需的临时目录,并修改其相应的权限.

mkdir  -pv  /tmp/extman

chown  postfix.postfix  /tmp/extman

登陆测试

http://192.168.145.100

默认管理帐号为:[email protected]  密码为:extmail*123*

以默认管理帐号和密码登陆到后台,添加域163.com和虚拟域126.com

163.com域内添加测试用户[email protected]

126.com域内添加测试用户[email protected]

user1和user2间的收发邮件测试.

下一篇博客,我们将会基于LAMP环境来搭建基于虚拟账号的邮件系统!

敬请期待。。。。。

你可能感兴趣的:(博客,color,账号,邮件系统)