马哥linux高薪中级-POSTFIX邮件服务(二)

简介:

四、POSTFIX+CYRUS-SASL 结合实现用户身份认证


1、cyrus-SASL简介

Cyrus SASL介绍(翻译)

2、配置内容简介:

postfix——》/var/lib/sasl2/smtpd.conf

pwcheck_method : saslauthd

math_list :PLAIN LOGIN


3、服务脚本:saslauthd



1、启动SASL 服务

/etc/init.d/saslauthd

[root@37-test ~]# chkconfig --add saslauthd

[root@37-test ~]# chkconfig --list saslauthd

saslauthd       0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭

[root@37-test ~]# chkconfig saslauthd on

[root@37-test ~]# chkconfig --list saslauthd

saslauthd       0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

[root@37-test ~]# service saslauthd start

正在启动 saslauthd:                                       [确定]

2、SASL配置文件

/etc/sysconfig/saslauthd


# Directory in which to place saslauthd's listening socket, pid file, and so

# on.  This directory must already exist.

SOCKETDIR=/var/run/saslauthd


# Mechanism to use when checking passwords.  Run "saslauthd -v" to get a list

# of which mechanism your installation was compiled with the ablity to use.

MECH=pam

表示支持的认证方式,默认为pam,建议为shadow(linux密码)认证,其他认证包括getpwent、kerberos5、rimap、ldap。

# Options sent to the saslauthd. If the MECH is other than "pam" uncomment the next line.

# DAEMONOPTS=--user saslauth


# Additional flags to pass to saslauthd on the command line.  See saslauthd(8)

# for the list of accepted flags.

FLAGS=


3、查看postfix服务器端是否支持sasl

[root@37-test ~]# postconf -a

cyrus 就是表示支持sasl方式认证

dovecot


4、查看sasl的认证功能是否成功?

[root@37-test ~]# testsaslauthd -u gongbing -p 123123

0: OK "Success."

You have new mail in /var/spool/mail/root


5、为postfix开启基于cyrus-sasl的认证功能,为/etc/postfix/main.cf添加一下几行。(取消mynetworks的其他参数,只保留127.0.0.1/8,目的是实现sasl认证。)

############################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_sasl_path = smtpd


smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!


6、创建文件sasl配置文件/usr/lib/sasl2/smtpd.conf(此为32系统使用,我的是64位,所以用下面的配置文件),并添加一下两行内容:

[root@37-test ~]# vim /usr/lib64/sasl2/smtpd.conf

log_level = 3

pwcheck_method:saslauthd

mech_list:PLAIN LOGIN 两种认证机制


7、验证是否实现认证成功

[root@37-test ~]# telnet 10.40.0.37 25

Trying 10.40.0.37...

Connected to 10.40.0.37.

Escape character is '^]'.

220 Welcome to our 37-test.localdomain ESMTP,Warning: Version not Available!(postfix配置文件的欢迎信息)

ehlo localhost

250-37-test.localdomain

250-PIPELINING

250-SIZE 10240000

250-VRFY

250-ETRN

250-AUTH LOGIN PLAIN 此行表示认证功能开启

250-AUTH=LOGIN PLAIN

250-ENHANCEDSTATUSCODES

250-8BITMIME

250 DSN





smtp服务器的工作过程:

客户端要发送邮件先要和邮件服务器取得联系,通过helo指令来创建连接,然后通过mail from指令确定发送者信息、rcpt to 确定收件人信息、data确定邮件内容等情况。

可以通过

smtpd_helo_restrictions 参数来限定哪些用户可以发送helo指令。

smtpd_sender_restrictions 限定哪些用户可以发送mail from 指令

smtpd_recipient_restrictions 限定哪些用户可以发送rcpt to 指令,关键在于此参数。

smtpd_data_restrictions 限定哪些用户可以发送data指令

smtpd_client_restrictions 限定哪些用户可以发起服务器连接请求



访问表:postfix可以定义一个表,来定义哪些用户或者地址可以访问。

/etc/postfix/access——hash格式————/etc/postfix/access.db



postfix有多个内置的限制条件,如上面的permit_mynetworks和reject_unauth_destination,但是管理员也可以通过访问(查找)表(access map)来自定义限制条件,自定义访问表的条件通常使用check_client_access,check_helo_access,check_sender_access,check_recipient_access运行,他们后面通常跟上type:mapname格式的访问表类型和名称,其中,check_sender_access和check_recipient_access用来检查客户端提供的邮件地址,因此,其访问表中可以使用完整的邮件地址,如admin@magedu.com,也可以使用域名如,magedu.com,或者用户名的部分,如admin。

具体格式如:

smtpd_client_restrictions = check_client_access hash:/etc/postfix/access

smtpd_helo_restrictions = check_helo_access mysql:/etc/postfix/mysql_user


实验1:

禁止10.40.0.57这台主机通过工作在10.40.0.37上的postfix服务器发送邮件,访问表使用hash的格式。

a、创建/etc/postfix/access文件,并作为客户端检查的控制文件,并定义一下内容:

[root@37-test ~]# vim /etc/postfix/access

10.40.0.57 REJECT                                                                     ACCES


b、将此文件转换成hash格式

[root@37-test ~]# postmap /etc/postfix/access 

[root@37-test ~]# ll /etc/postfix/

总用量 280

-rw-r--r--  1 root root 19594 2月   5 07:14 access

-rw-r--r--  1 root root 12288 2月   5 07:15 access.db



c、配置postfix使用此文件对客户端进行检查,编辑/etc/postfix/main.cf文件。

smtpd_client_restrictions = check_client_access hash:/etc/postfix/access



d、查看测试结果

BingGongtekiMacBook-Pro:~ binggong$ telnet 10.40.0.37 25

Trying 10.40.0.37...

Connected to www1.dtedu.com.

Escape character is '^]'.

220 Welcome to our 37-test.localdomain ESMTP,Warning: Version not Available!

helo localhost

250 37-test.localdomain

mail from:gongbing@dtedu.com

250 2.1.0 Ok

rcpt to:gongbing

554 5.7.1 <unknown[10.40.0.57]>: Client host rejected: ACCESS(5)

data



实验2:

禁止通过本服务器向Microsoft.com域发送邮件,访问表使用hash格式

1、创建/etc/postfix/denydstdomains文件

[root@37-test ~]# vim /etc/postfix/denydstdomains


microsoft.com reject


邮箱地址的格式如下:

user@domain 用于匹配指定邮件地址

domain.com 用于匹配此域名结尾的所有邮件地址

user 用于匹配指定的主机名,用户名的邮件地址

动作的类型:

ok 接收匹配的地址

reject 拒绝匹配的地址

deffer 拒绝匹配的地址

2、转换文件格式

[root@37-test ~]# postmap /etc/postfix/denydstdomains 

[root@37-test ~]# ll /etc/postfix/denydstdomains

-rw-r--r--  1 root root    21 2月   5 07:25 denydstdomains

-rw-r--r--  1 root root 12288 2月   5 07:26 denydstdomains.db



3、配置main.cf文件对客户端的检查,注意访问表之间的先后顺序。

smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/denydstdomains 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


4、重新加载配置文件并检查

BingGongtekiMacBook-Pro:~ binggong$ telnet 10.40.0.37 25

Trying 10.40.0.37...

Connected to www1.dtedu.com.

Escape character is '^]'.

220 Welcome to our 37-test.localdomain ESMTP,Warning: Version not Available!

helo localhost

250 37-test.localdomain

mail from:aaa    

250 2.1.0 Ok

rcpt to :gongbing@microsoft.com

501 5.5.4 Syntax: RCPT TO:<address>

data

554 5.5.1 Error: no valid recipients




实现sasl认证功能,这里实验条件默认情况下是不许发送邮件到外网,通过linux自带的用户账户及密码认证就实现了认证后发送邮件的功能。

[root@37-test ~]# telnet 10.40.0.37 25

Trying 10.40.0.37...

Connected to 10.40.0.37.

Escape character is '^]'.

220 Welcome to our 37-test.localdomain ESMTP,Warning: Version not Available!

helo 10.40.0.37

250 37-test.localdomain

mail from:gongbing

250 2.1.0 Ok

rcpt to:gongbing_shenlan@163.com

501 5.5.4 Syntax: RCPT TO:<address>

quit

221 2.0.0 Bye

Connection closed by foreign host.



[root@37-test ~]# telnet 10.40.0.37 25

Trying 10.40.0.37...

Connected to 10.40.0.37.

Escape character is '^]'.

220 Welcome to our 37-test.localdomain ESMTP,Warning: Version not Available!

auth login

334 VXNlcm5hbWU6 用户名为openssl base64位编码

Z29uZ2Jpbmc=

334 UGFzc3dvcmQ6 密码为openssl base64位编码

MTIzMTIz

235 2.7.0 Authentication successful

mail from:gongbing

250 2.1.0 Ok

rcpt to:gongbing_shenlan@163.com

250 2.1.5 Ok

data

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

ni hao a 

.

250 2.0.0 Ok: queued as 368F887A004

quit

221 2.0.0 Bye

Connection closed by foreign host.


openssl base64编码方法: 

BingGongtekiMacBook-Pro:~ binggong$ echo -n "123123" |openssl base64

MTIzMTIz

BingGongtekiMacBook-Pro:~ binggong$ echo -n "gongbing" |openssl base64

Z29uZ2Jpbmc=



本文转自 blackstome 51CTO博客,原文链接:http://blog.51cto.com/137783/1916983,如需转载请自行联系原作者

相关文章
|
29天前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
运维 Linux Apache
Linux Apache服务详解——Apache虚拟目录与禁止显示目录列表实战
Linux Apache服务详解——Apache虚拟目录与禁止显示目录列表实战
21 2
|
1月前
|
域名解析 Linux Apache
Linux Apache服务详解——虚拟网站主机功能实战
Linux Apache服务详解——虚拟网站主机功能实战
31 5
|
18小时前
|
网络协议 Ubuntu Linux
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
|
18天前
|
Linux
linux中服务管理
在Linux服务管理中,从传统的System V init到Upstart,再到广泛采用的systemd,管理方式不断发展。systemd以其强大的功能和依赖解决成为现代Linux的核心。`systemctl`是管理服务的关键命令,如启动(`start`)、停止(`stop`)、重启(`restart`)服务及设置开机启动(`enable`)或取消(`disable`)。了解和服务管理,特别是systemd和`systemctl`的使用,对系统管理员至关重要。其他如xinetd用于控制网络服务,而特定环境下有OpenRC等工具。
17 2
|
24天前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】Linux 管理系统服务 ntsysv命令 使用指南
【Shell 命令集合 系统设置 】Linux 管理系统服务 ntsysv命令 使用指南
27 0
|
24天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux 管理系统服务 chkconfig命令 使用指南
【Shell 命令集合 系统设置 】Linux 管理系统服务 chkconfig命令 使用指南
27 0
|
24天前
|
Shell Linux 数据库
【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
26 1
|
1月前
|
存储 Linux
Linux Autofs自动挂载服务详解
Linux Autofs自动挂载服务详解
13 0
|
1月前
|
Web App开发 存储 Linux
Linux Apache服务详解——Apache服务访问控制
Linux Apache服务详解——Apache服务访问控制
328 7