Total Pageviews

Thursday 11 October 2012

postfix+web页面+身份验证

使用web方式实现对postfix邮件接收发送的管理,且需要经过身份验证
一:修改主机名:
[root@lyt ~]# vim /etc/sysconfig/network
image
[root@lyt ~]# vim /etc/hosts
image
[root@lyt ~]# service sendmail stop
[root@lyt ~]# chkconfig sendmail off       #关闭sendmail
[root@lyt ~]# init 6     #重启计算机
二:搭建dns服务器:
[root@mail ~]# mkdir /mnt/cdrom       #新建挂载点
[root@mail ~]# mount /dev/cdrom /mnt/cdrom/      #挂载光盘
[root@mail ~]# cd /mnt/cdrom/Server/
[root@mail Server]# rpm -ivh bind-9.3.6-4.P1.el5.i386.rpm      #安装dns主程序
[root@mail Server]# rpm -ivh bind-chroot-9.3.6-4.P1.el5.i386.rpm       #安装与dns安全相关的软件包
[root@mail Server]# rpm -ivh caching-nameserver-9.3.6-4.P1.el5.i386.rpm       #安装与缓存相关的软件包
[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         #编辑dns的主配置文件
image
image
image
[root@mail etc]# vim named.rfc1912.zones        #编辑dns区域声明文件
image
[root@mail etc]# cd ../var/named/
[root@mail named]# cp -p localhost.zone 163.com.db
[root@mail named]# vim 163.com.db        #编辑数据库文件
image 
[root@mail named]# vim /etc/resolv.conf
image
[root@mail named]# service named start         #启动dns
[root@mail named]# chkconfig named on        #设置开机自动启动
三:搭建postfix:
[root@mail named]# cd /mnt/cdrom/Server/
[root@mail Server]# rpm -ivh postfix-2.3.3-2.1.el5_2.i386.rpm       #安装postfix软件包
[root@mail Server]# vim /etc/postfix/main.cf           #编辑postfix主配置文件
image
image
image
image
image
除了以上截图外,还需要添加以下内容:
broken_sasl_auth_clients = yes           #客户端需要经过身份验证
smtpd_sasl_auth_enable = yes            #客户端需要经过身份验证
smtpd_sasl_security_options = noanonymous       #不允许匿名发送
smtpd_sasl_application_name = smtpd        
smtpd_client_restrictions = permit_sasl_authenticated,reject     #发送方需要经过认证,没经过认证的拒绝
#smtpd_client_restrictions = permit_sasl_authenticated
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination     #接收本地network的,经过认证的,非认证的拒绝

image
[root@mail Server]# service saslauthd start        #启动sasl验证功能
[root@mail Server]# chkconfig saslauthd on        #设置为开机自动启动
[root@mail Server]# service postfix start
[root@mail Server]# chkconfig postfix on
四:安装squirrelmail:
[root@mail Server]# vim /etc/yum.repos.d/rhel-debuginfo.repo      #编辑yum
image
[root@mail Server]# yum install squirrelmail –y      #安装squirrel
[root@mail Server]# service httpd start       #启动httpd
[root@mail Server]# chkconfig httpd on       #设置开机自动启动
[root@mail Server]# cd /var/www/html/
[root@mail html]# ln -s /usr/share/squirrelmail/ mail      #创建符号连接mail
[root@mail html]# cd
[root@mail ~]# cd /var/www/html/mail/
[root@mail mail]# cd config/
[root@mail config]# ./conf.pl      #对web页面进行设置
image
image
image
image
image
image
image
image
image
image
[root@mail config]# vim /etc/httpd/conf/httpd.conf       #修改apache的配置文件
image
[root@mail config]# service httpd restart
五:安装dovecot:
[root@mail config]# cd /mnt/cdrom/Server/
[root@mail Server]# yum install dovecot –y       #使用yum安装dovecot
[root@mail Server]# service dovecot start
[root@mail Server]# chkconfig dovecot on
六:创建用户:
[root@mail Server]# useradd user1
[root@mail Server]# passwd user1
[root@mail Server]# useradd user2
[root@mail Server]# passwd user2
七:禁用此行:
[root@mail Server]# vim /etc/postfix/main.cf
image
[root@mail Server]# service postfix restart
八:测试:
image
image
image



----------------------------------------------------------------------------------------------
Linux下巧妙使用邮件服务器之postfix(认证+web)

一. postfix邮件服务的安装
Red Hat Enterprise Linux 提供了sendmail和postfix两种STMP邮件服务软
件,让用户可以随意地选择其中一种!不过,默认情况下安装程序已将sendmail
安装到系统中了,如果要使用postfix,就必须先将sendmail服务停止,然后再安
装postfix服务软件。具体操作步骤如下:
① 检查sendmail服务是否已启动
netstat -nutlp | grep :25
② 关闭sendmail服务及开机时自启动
/etc/rc.d/init.d/sendmail stop
chkconfig sendmail off
③ 安装postfix服务
将Red Hat Enterprise Linux 5的第3张安装盘放入光驱,加载光驱后在光盘的
Server目录下找到postfix的RPM安装包文件postfix-2.3.3-2.i386.rpm,然后使用
下面的命令安装:
rpm -ivh /mnt/Server/postfix-2.3.3-2.i386.rpm
二、postfix服务的基本配置
1.设置运行postfix服务的邮件主机的主机名、域名
myhostname = mail.gdvcp.net
mydomain = gdvcp.net
2.设置由本机寄出的邮件所使用的域名或主机名称
myorigin = $mydomain
3.设置postfix服务监听的网络接口
inet_interfaces = all
4.设置可接收邮件的主机名称或域名
mydestination = $mydomain, $myhostname
5.设置可转发(Relay)哪些网络的邮件
mynetworks = 192.168.16.0/24
6.设置可转发哪些网域的邮件
relay_domains = gdvcp.net
三、虚拟别名域的配置
① 编辑postfix主配置文件/etc/postfix/main.cf,进行如下定义:
virtual_alias_domains = dzxx.cn,example.com
virtual_alias_maps = hash:/etc/postfix/virtual
② 编辑配置文件/etc/postfix/virtual,进行如下定义:
@dzxx.cn @gdvcp.net
admin@example.com lbt
st0321@example.com st0321001,st0321002
daliu@example.com lbt,liu6812@163.com
③ 在修改配置文件main.cf和virtual后,要使更改立即生效,应分别执行/usr/sbin目录下以下的两条命令:
postmap /etc/postfix/virtual
postfix reload
此外,要使它能在单位内部网络中更好地
转发邮件,还必须进行DNS设置。
rhel5.gdvcp.net. IN A         192.168.16.177
mail.gdvcp.net. IN CNAME   rhel5.gdvcp.net.
gdvcp.net.       IN MX 10     mail.gdvcp.net.
四、用户别名的配置
① 打开postfix主配置文件/etc/postfix/main.cf,应确认文件中包含以下两条默认语句:
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
② 编辑配置文件/etc/aliases,进行如下定义:
st0322: st0322001,st0322002,st0322003,st0322004
st0323: :include: /etc/mail/st0323
jcz01: jczliuming
lm01: jczliuming,liuming86@163.com
③ 在修改配置文件main.cf和aliases后,要使更改立即生效,应分别执行/usr/sbin目录下以下的两条命令:
postalias /etc/aliases
postfix reload
五、SMTP认证的配置
Cyrus SASL是Cyrus Simple Authentication and Security Layer的简写,它最
大的功能是为应用程序提供了认证函数库。应用程序可以通过函数库所提供的功
能定义认证方式,并让SASL通过与邮件服务器主机的沟通从而提供认证的功能。
1.Cyrus-SASL认证包的安装
默认情况下,Red Hat Enterprise Linux安装程序会将Cyrus-SASL认证包,
可使用下面的命令检查系统是否已经安装了Cyrus-SASL认证包或查看已经安装
了何种版本:
rpm -qa | grep sasl
如果系统还没有安装Cyrus-SASL认证包,应将Red Hat Enterprise Linux 5第
1、2和3张安装光盘分别放入光驱,加载光驱后在光盘的Server目录下找到与
Cyrus-SASL认证包相关的RPM包文件,然后分别使用rpm -ivh命令安装。例
如,要安装第1张光盘上的cyrus-sasl-2.1.22-4.i386.rpm包文件,可使用下面的
命令:
rpm -ivh /mnt/Server/cyrus-sasl-2.1.22-4.i386.rpm
2.Cyrus-SASL V2的密码验证机制
默认情况下,Cyrus-SASL V2版使用saslauthd这个守护进程进行密码认证,而密码
认证的方法有多种,使用下面的命令可查看当前系统中的Cyrus-SASL V2所支持的密码
验证机制:
saslauthd –v
当前可使用的密码验证方法有getwent、kerberos5、pam、rimap、shadow和ldap。
为简单起见,这里准备采用shadow验证方法,也就是直接用/etc/shadow文件中的用户账
户及密码进行验证,因此,在配置文件/etc/sysconfig/saslauthd中,修改当前系统所采用
的密码验证机制为shadow,即:
MECH=shadow
image
3.测试Cyrus-SASL V2的认证功能
由于Cyrus-SASL V2版默认使用saslauthd这个守护进程进行密码认
证,因此,需要使用下面的命令来查看saslauthd进程是否已经运行:
ps aux | grep saslauthd
如果没有发现saslauthd进程,则可用下面的命令启动该进程并设置它
开机自启动:
/etc/init.d/saslauthd start
chkconfig saslauthd on
然后,可用下面的命令测试saslauthd进程的认证功能:
/usr/sbin/testsaslauthd –u lbt –p ‘123456’
image
4.设置postfix启用smtp认证
main.cf文件中有关smtp认证的设置部分
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = ''
smtpd_recipient_restrictions = permit_mynetworks,
permit_sasl_authenticated, reject_unauth_destination
broken_sasl_auth_clients=yes
smtpd_client_restrictions = permit_sasl_authenticated
smtpd_sasl_security_options = noanonymous
5.测试postfix是否启用了smtp认证
采用telnet命令连接到postfix服务器端口25来进行测试,测试过程
如下图所示。
image
启动和停止postfix服务
1.启动postfix服务
/etc/init.d/postfix start
2.停止postfix服务
/etc/init.d/postfix stop
3.重新启动postfix服务
/etc/init.d/postfix restart
4.重新载入postfix主配置文件
/etc/init.d/postfix reload
5.自动启动postfix服务
如果需要让postfix服务随系统启动而自动加载,可以执行“ntsysv”
命令启动服务配置程序,找到“postfix”服务,然后在其前面加上“*” 星
号,确定即可。
image
六、POP和IMAP邮件服务的实现
postfix服务只是一个MTA(邮件传输代理),它只提供
SMTP服务,也就是只提供邮件的转发及本地的分发功能。
要实现邮件的异地接收,还必须安装POP或IMAP服务。通
常情况下,都是将STMP服务和POP或IMAP服务安装在同一
台主机上,那么这台主机也就称为电子邮件服务器。Red
Hat Enterprise Linux 5中,有以下两个软件可以同时提供
POP和IMAP服务:dovecot和cyrus-imapd。
七、dovecot服务的实现
1.dovecot服务的安装
Red Hat Enterprise Linux安装程序默认没有安装dovecot服务,可使
用下面的命令检查系统是否已经安装了dovecot服务:
rpm -q dovecot
如果系统还没有安装dovecot服务,将Red Hat Enterprise Linux 5第2
张安装光盘放入光驱,加载光驱后在光盘的Server目录下找到dovecot服
务的RPM安装包文件dovecot-1.0-1.2.rc15.el5.i386.rpm和相关程序,然
后使用下面的命令安装dovecot服务和相关程序:
rpm -ivh /mnt/Server/perl-DBI-1.52-1.fc6.i386.rpm
rpm -ivh /mnt/Server/mysql-5.0.22-2.1.i386.rpm
rpm -ivh /mnt/Server/dovecot-1.0-1.2.rc15.el5.i386.rpm
2.dovecot服务的基本配置
dovecot服务的配置文件是/etc/dovecot.conf。要
启用最基本的dovecot服务,只需要修改该配置文件
中的以下内容:
protocols =pop3 pop3sprotocol pop3 { listen = *:10100 }
3.启动dovecot服务并设置为自启动
/etc/rc.d/init.d/dovecot start
chkconfig --level 345 dovecot on
八、cyrus-imapd服务的实现
1.cyrus-imapd服务的安装
Red Hat Enterprise Linux安装程序默认没有安装cyrus-imapd服务,可使用下
面的命令检查系统是否已经安装了cyrus-imapd服务:
rpm -qa | grep cyrus-imapd
系统当前还没有安装cyrus-imapd服务,将Red Hat Enterprise Linux 5第2、3
张安装光盘放入光驱,加载光驱后在光盘的Server目录下可找到以下与cyrus-
imapd服务相关的RPM包文件:
cyrus-imapd-perl-2.3.7-1.1.el5.i386.rpm
cyrus-imapd-utils-2.3.7-1.1.el5.i386.rpm
db4-utils-4.3.29-9.fc6.i386.rpm
lm_sensors-2.10.0-3.1.i386.rpm
cyrus-imapd-2.3.7-1.1.el5.i386.rpm
cyrus-imapd-devel-2.3.7-1.1.el5.i386.rpm
然后,可使用rpm -ivh命令分别进行安装 。
2.cyrus-imapd服务的基本配置
cyrus-imapd服务的配置文件有以下三个:
/etc/sysconfig/cyrus-imapd:是用于启动cyrus-imapd服务的配置文件;
/etc/cyrus.conf:是cyrus-imapd服务的主要配置文件,其中包含该服务中各个组件(imap、pop3、sieve和nntp等)的设置参数;
/etc/imapd.conf:是cyrus-imapd服务中的imap服务的配置文件。
为了使postfix与cyrus-imapd整合在一起,必须在postfix的
主配置文件/etc/postfix/main.cf中加入以下内容:
mailbox_transport = lmtp:unix:/var/lib/imap/socket/lmtp
3.启动cyrus-imapd服务并设置自动运行
默认情况下,利用RPM包文件安装cyrus-imapd服务后,
该服务是被停用的,所以用户必须手动启动它;如果需要开
机自动运行,也必须手动启用。使用下面的命令可启动
cyrus-imapd服务并设置其开机时自动运行:
service cyrus-imapd start
chkconfig cyrus-imapd on
4.用户邮件信箱的管理
创建邮件信箱时,为每一个邮件信箱命名的格式为:
信箱类型.名称[.文件夹名称[.文件夹名称]]…
(1)为Cyrus-IMAP管理员账户cyrus设置密码 passwd cyrus
(2) 使用cyradm管理工具为用户创建邮件信箱
/usr/bin/cyradm -u cyrus localhost
然后使用下面的命令就可为用户lbt创建一个邮件信箱:
createmailbox user.lbt
(3)在用户邮件信箱下添加其他文件夹
使用下面的命令为用户lbt在其邮件信箱下创建发件箱、垃圾箱和草稿箱
等其他文件夹:
createmailbox user.lbt.Send
createmailbox user.lbt.Trash
createmailbox user.lbt.Drafts
(4)为用户邮件信箱设置配额
为用户lbt的信箱user.lbt设置5M的配额,可使用下面的命令:
setquota user.lbt 5210
用下面的命令查看用户邮箱的使用情况:
su -l cyrus -c /usr/lib/cyrus-imapd/quota
(5)为用户邮件信箱设置权限
Cyrus-IMAP中设置用户信箱权限的6种缩写形式
image
常用的cyradm管理命令及其缩写形式
image
一.基于ip的认证
由于sendmail与postfix的服务端口相同,所以,必须暂停sendmail服务
,为了不受影响,一般建议直接卸载掉!
1.安装postfix
image
[root@mail ~]# mount /dev/cdrom  /mnt/cdrom
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@mail ~]# cd /mnt/cdrom/Server
image
[root@mail Server]# service postfix start
Starting postfix:                                          [  OK  ]
[root@mail Server]# chkconfig postfix on
[root@mail Server]# chkconfig --list |grep postfix
postfix            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@mail Server]# netstat -tupln |grep 25
tcp        0      0 127.0.0.1:25                0.0.0.0:*            LISTEN      5167/master
image
2.查看其特定参数
[root@mail postfix]# postconf –n   //可被修改的参数
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
debug_peer_level = 2
html_directory = no
inet_interfaces = localhost
mail_owner = postfix
mailq_path = /usr/bin/mailq.postfix
manpage_directory = /usr/share/man
mydestination = $myhostname, localhost.$mydomain, localhost
newaliases_path = /usr/bin/newaliases.postfix
queue_directory = /var/spool/postfix
readme_directory = /usr/share/doc/postfix-2.3.3/README_FILES
sample_directory = /usr/share/doc/postfix-2.3.3/samples
sendmail_path = /usr/sbin/sendmail.postfix
setgid_group = postdrop
unknown_local_recipient_reject_code = 550
[root@mail postfix]# postconf –d     //默认参数
[root@mail postfix]# postconf    //刚被生效的参数
[root@mail postfix]# man 5 postconf    // 可查看配置文档
[root@mail postfix]# postconf  -e  “参数=值”image
3. 配置postfix主配置文件
[root@mail postfix]# vim /etc/postfix/main.cf
image
image
image
image

image 基于ip认证
image
[root@mail postfix]# telnet 127.0.0.1 25
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 mail.163.com ESMTP Postfix
mail from:user1@163.com
250 2.1.0 Ok
rcpt to:user4@sina.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
hahaha
.
250 2.0.0 Ok: queued as 601D1BE18C
quit
221 2.0.0 Bye
Connection closed by foreign host.
接收查看:
image
imageimage
image
4.发邮件测试:
image
接收邮件:
image
二. 基于账号的认证:
1.查看是否支持sasl
[root@mail ~]# cd /usr/lib/sasl2/
[root@mail sasl2]# vim Sendmail.conf
[root@mail sasl2]# cat  Sendmail.conf
pwcheck_method:saslauthd    //支持sasl
[root@mail sasl2]# service saslauthd status
saslauthd (pid 3356 3355 3354 3353 3352) is running...
2. 修改postfix的主配置文档
[root@mail sasl2]# vim /etc/postfix/main.cf
image
255、256行:启用sasl  认证
257行:没有验证,不允许匿名账户发邮件
#smtpd_sasl_application_name = smtpd
发送服务器的名称叫什么,smtpd为master控制的一个模块!(可要可不要!)
#smtpd_client_restrictions = permit_sasl_authenticated,reject
经过认证的客户被中继,默认都是拒绝的!
接收端限制:经过mynetworks变量定义 、 sasl认证的都接收,没有认证的都拒绝!
3.测试:
C:\Users\Administrator&gt;telnet 192.168.10.99 25
220 mail.163.com ESMTP Postfix
mail from:user1@163.com
250 2.1.0 Ok
rcpt to:gjp@126.com
554 5.7.1 <gjp@126.com>: Relay access denied
auth login dXNlcjFAMTYzLmNvbQ==
334 UGFzc3dvcmQ6
MTIz
235 2.0.0 Authentication successful
mail from:user1@163.com
250 2.1.0 Ok
rcpt to:gjp@126.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
send is ok ?
.
250 2.0.0 Ok: queued as D6ED0BE189
quit
问题:任何未知名的邮件都可以向本地邮件发送信息!
发送信息:
C:\Users\Administrator&gt;telnet 192.168.10.99 25
220 mail.163.com ESMTP Postfix
mail from:haha@163.com
250 2.1.0 Ok
rcpt to:user1@163.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject hah
welcome to my ou!
.
250 2.0.0 Ok: queued as 44836BE152
quit
221 2.0.0 Bye
日志信息:
Aug  7 11:02:17 mail postfix/qmgr[3691]: 44836BE152: from=<haha@163.com>, size=345, nrcpt=1 (queue active)
Aug  7 11:02:17 mail postfix/local[4760]: 44836BE152: to=<user1@163.com>, relay=local, delay=40, delays=40/0.02/0/0.06, dsn=2.0.0, status=sent (delivered to mailbox)
[root@mail ~]# su – user1       接收!
[user1@mail ~]$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user1": 2 messages 1 new
    1 MAILER-DAEMON@mail.1  Sat Aug 04 12:53  13/544   "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA"
&gt;N  2 haha@163.com          Tue Aug  7 11:02  15/465
解决:
[root@mail ~]# vim /etc/postfix/main.cf  增加以下信息
image
[root@mail ~]# service postfix restart
Shutting down postfix:                                     [  OK  ]
Starting postfix:                                          [  OK  ]
C:\Users\Administrator&gt;telnet 192.168.10.99 25
220 mail.163.com ESMTP Postfix
mail from:me@163.com
250 2.1.0 Ok
rcpt to:user2@163.com
554 5.7.1 <unknown[192.168.10.2]>: Client host rejected: Access denied
本地都发不了,更不用说外地了!必须通过合法账号进行认证!
再次测试:(仅有合法账号验证后才能发送信件)
C:\Users\Administrator&gt;telnet 192.168.10.99 25
220 mail.163.com ESMTP Postfix
mail from:u@163.com
250 2.1.0 Ok
rcpt to:user1@163.com
554 5.7.1 <unknown[192.168.10.2]>: Client host rejected: Access denied
auth login dXNlcjFAMTYzLmNvbQ==
334 UGFzc3dvcmQ6
MTIz
235 2.0.0 Authentication successful
mail from:u@163.com    //mail from:只能输入一次!
503 5.5.1 Error: nested MAIL command
rcpt to:user1@163.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
hahah  i like there !
.
250 2.0.0 Ok: queued as F3BD6BE17B
quit
221 2.0.0 Bye
[root@mail ~]# su - user1
[user1@mail ~]$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user1": 3 messages 2 new
    1 MAILER-DAEMON@mail.1  Sat Aug 04 12:53  13/544   "DON'T"
&gt;N  2 haha@163.com          Tue Aug  7 11:02  15/465 
N  3 u@163.com             Tue Aug  7 12:08  14/448
测试:postfix邮件服务器与sendmail邮件服务器的通信状况!
sendmail 服务器基于上篇文章:
注意:如果启用sendmail比较慢,可以修改 /etc/resolv.conf  把 search一行禁掉!

[root@mail named]# hostname
mail.sina.com
[root@mail named]# su - user4
[user4@mail ~]$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user4": 2 messages 1 new 2 unread
 U  1 user1@163.COM         Tue Aug  7 17:31  27/928   "2222"
>N  2 user1@163.COM         Tue Aug  7 17:33  26/924   "de"
& r^H
Unknown command: ""
& 2
Message 2:
From user1@163.COM  Tue Aug  7 17:33:01 2012
Date: Tue, 7 Aug 2012 17:33:01 +0800 (CST)
Subject: de
From: user1@163.COM
To: user4@sina.com
User-Agent: SquirrelMail/1.4.8-5.el5_3.7
MIME-Version: 1.0
Content-Type: text/plain;charset=gb2312
Content-Transfer-Encoding: 8bit
X-Priority: 3 (Normal)
Importance: Normal
de
& r
To: user4@sina.com user1@163.COM
Subject: Re: de
hehe!
.
Cc:
&
At EOF
& quit
New mail has arrived.
Saved 1 message in mbox
Held 1 message in /var/spool/mail/user4