免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1496 | 回复: 1
打印 上一主题 下一主题

[Mail] 转两篇关于postfix 的 cyrus-SASL的 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-06-03 13:42 |只看该作者 |倒序浏览
在postfix中实现基于cyrus-sasl的SMTP认证

    摘要
    在postfix中实现基于cyrus-sasl的SMTP认证(2002-06-17 16:38:33)

By ideal

概述:

  全世界范围内的SMTP服务器与日俱增,但是这些SMTP服务器都有一个与生俱来的缺点,那就是没有任何的认证机制。因为在设计这些SMTP服务器时,全世界范围的Internet主机还不是很多,彼此间都有很高的可信任级别,所以没有全面考虑安全性问题。这种现象也可以在别的Internet 协议中看到。

  正是因为SMTP通讯过程缺乏认证机制,以及ip欺骗的容易实现,导致了以下两个方面的问题:SMTP服务器无法确认SMTP客户机的合法性; SMTP客户机也无法确认SMTP服务器的合法性。从而也就导致了用户可以不经过认证就发送邮件,用户可以冒名发送邮件,以及垃圾邮件的泛滥。

  Postfix设置了好多选项让网络管理员来限制服务器资源的使用,以及确定用户的合法性。但遗憾的是,这些认证机制都是基于ip地址或者客户 DNS信息。在客户ip地址固定的情况下,这些方法或许有一定的作用(虽然还是无法解决冒名发送邮件),但是在客户ip地址不固定的情况下,就很难通过这种机制来进行限制了。当然,现在也有一些其他的技术,比如POP-before-SMTP(在发信前收信)来解决这个问题,但是这需要运行额外的服务程序。并且用户在发行之前一定要先收信,这或多或少都会带来一些不方便。

  好在有简单认证安全层(SASL),它提供了模块化的SMTP认证扩展,因此SMTP就可以在SASL之上构建自己的SMTP认证,因为可以通过开放式的机制和协议来建立认证会话。另外,SASL在实现了对PLAIN以及CRAM-MD5加密等协议的基础上,还提供了多种认证方法,比如通过 Kerberos、用户数据库、passwd文件、PAM等。这样,SMTP程序不需要支持这些认证方法就可以实现多种认证方式。并且,SMTP在用户经过成功认证以后同样可以定义自己的访问策略来对用户访问进行控制。

  当然,首先我们必须保证该SMTP服务器能够提供对SASL的支持)

需求:

  首先要保证您的postfix版本是snapshot-20000507及其以上的版本,从而保证postfix对SASL的支持。强烈建议您升级至postfix-20010228及其以上版本。可以从www.postfix.org下载postfix的源代码包,在测试过程中我们使用的是 postfix-20010228-pl04.tar.gz。

  然后,要保证您的SASL的版本是1.5.5及其以上版本,因为已经有报告称这些版本的SASL可以和postfix工作得很好。可以从 ftp://ftp.andrew.cmu.edu/pub/cyrus-mail/下载SASL,在测试过程中我们使用的是cyrus-sasl- 1.5.24-17.rpm和cyrus-sasl-devel-1.5.24-17.rpm,即RedHat7.1自带的SASL的rpm包。

  另外说明一点,整个测试过程都是在RedHat7.1中进行的。

步骤:

1.检查系统中是否安装了cyrus-sasl-1.5.24

  执行以下命令查看系统中是否安装了cyrus-sasl:

    rpm –qa |grep cyurs

  如果您看到以下提示,则说明已经安装了cyrus-sasl-1.5.24-17和cyrus-sasl-devel-1.5.24-17:

    [root@test bye2000]# rpm -qa |grep sasl
    cyrus-sasl-devel-1.5.24-17
    cyrus-sasl-1.5.24-17

  否则,请用rpm –ivh 命令先安装所需sasl-cyrus包。

2.编译postfix

  首先,下载postfix-20010228-pl04.tar.gz,然后解开源代码包:

    [root@test /tmp]# tar xvzf postfix-20010228-pl04.tar.gz

  命令执行完后,会生成postfix-20010228-pl04目录,然后进入该目录:

    [root@test /tmp]# cd postfix-20010228-pl04

  接下来,依次执行以下命令编译postfix:

    [root@test postfix-20010228-pl04]# make tidy
    [root@test postfix-20010228-pl04]#make makefiles CCARGS='-DUSE_SASL_AUTH -I/usr/include' AUXLIBS='-L/usr/lib -lsasl'
    [root@test postfix-20010228-pl04]# make

  建立一个新用户“postfix”,该用户必须具有唯一的用户id和组id号,同时应该让该用户不能登录到系统,也即不为该用户指定可执行的登录外壳程序和可用的用户宿主目录。我们可以先用adduser postfix 添加用户再编辑/etc/passwd文件中的相关条目如下所示:

    postfix:*:12345:12345:postfix:/no/where:/no/shell

  最后执行安装脚本安装postfix

    [root@test postfix-20010228-pl04]# ./INSTALL.sh

  对于安装脚本的问题,可以统统回车确定。

3.配置postfix

  安装完postfix后,就可以编辑/etc/postfix/main.cf文件来配置postfix了,关于如何配置
postfix,可以参考本站解决方案中的相关文章来获取更多的信息。

  唯一需要补充的是,为了告诉postfix采用sasl来进行SMTP的用户认证,我们必须在/etc/postfix/main.cf文件的最后添加以下几行:

    smtpd_sasl_auth_enable = yes
    smtpd_recipient_restrictions = permit_sasl_authenticated permit_auth_destination reject
    broken_sasl_auth_clients = yes
    smtpd_sasl_security_options = noanonymous

    说明如下:

    smtpd_sasl_auth_enable:告诉SMTP客户,该SMTP服务器需要进行用户认证。

    smtpd_recipient_restrictions = permit_sasl_authenticated permit_auth_destination

    reject:仅仅中继通过SMTP认证的客户端邮件,以及接收本postfix为最后一站的邮件。

    broken_sasl_auth_clients = yes:有一些Microsoft 的SMTP客户端,比如Outlook Express 4.X采用的是非标准的SMTP认证协议,将该选项设置为yes可以解决这类不兼容问题。

    smtpd_sasl_security_options = noanonymous:控制本地SMTP认证所采用的机制,这里是允许任何非匿名认证方法。

4.配置SASL

  进入/usr/lib/sasl/目录,用touch新建这样一个文件:

    [root@test sasl]# touch smtpd.conf

  在该文件中加入以下内容:

    [root@test sasl]# more smtpd.conf
    pwcheck_method:pam

  也就是指定通过PAM来进行用户认证。

5.配置PAM

  进入/etc/pam.d/目录,用touch新建这样一个文件:

    [root@test pam.d]# touch smtp

  在该文件中加入如下内容:

    [root@test pam.d]# more smtp
    #%PAM-1.0
    auth required /lib/security/pam_stack.so service=system-auth
    account required /lib/security/pam_stack.so service=system-auth

  也即和/etc/pam.d/pop文件的内容是一致的。

6.修改/etc/shadow文件权限

  因为PAM最终要去读取/etc/shadow文件的内容来进行用户认证,但是/etc/shadow文件
权限是设置为只有root用户可以读写,而我们的postfix是运行在postfix用户权限之下,所以我们要合理的设置/etc/shadow文件权限,以便让pam可以读取其内容。

  执行以下命令,让root组也可以读取shadow:

    [root@test /etc]# chmod 640 /etc/shadow

  然后再将postfix用户加入root组,采用以下命令:

    [root@test /etc]# usermod -G root postfix

7.启动postfix

  执行

    [root@test /etc]# postfix start

  这样,一个支持SMTP认证的postfix就开始工作了。
>;

论坛徽章:
0
2 [报告]
发表于 2005-06-03 13:44 |只看该作者

转两篇关于postfix 的 cyrus-SASL的

基本思路:
courier-imap提供pop3服务,直接访问mysql
postfix的所有用户,别名和多域名支持均存储在mysql中
smtp auth使用了sasl,通过pam_mysql对mysql进行访问。

相关软件:
mysql-4.0.12.tar.gz
courier-imap-1.7.1.tar.bz2
cyrus-sasl-1.5.28.tar.gz
pam_mysql-0.5.tar.gz
postfix-2.0.6.tar.gz

安装MySQL,这个网上资料很多,如果有不清楚的,请去查文档。
#/usr/sbin/useradd mysql
#tar zvxf mysql-4.0.12.tar.gz
#cd mysql*
#./configure --prefix=/usr/local/mysql
#make
#make install
#scripts/mysql_install_db
#chown -R mysql.root /usr/local/mysql/var
#/usr/local/mysql/bin/mysqld_safe -u mysql &
#cd ..
好了,MySQL已经搞定了。

安装pam_mysql
#tar zvxf pam_mysql*gz
#cd pam_mysql*
将pam_mysql.c中第54行注释掉,/* #define DEBUG */
#make
#cp pam_mysql.so /lib/security
#cd ..

安装Cyrus-SASL
#tar zvxf cyrus-sasl*gz
#cd cyrus-sasl*
#./configure --disable-sample --disable-pwcheck --disable-cram --disable-digest --disable-krb4
--disable-gssapi --disable-anon --enable-plain --enable-login
#make
#make install
#ln -s /usr/local/lib/sasl /usr/lib/sasl
#echo /usr/local/lib >;>; /etc/ld.so.conf
#echo /usr/local/lib/sasl >;>; /etc/ld.so.conf
#echo /usr/local/mysql/lib/mysql >;>; /etc/ld.so.conf
#/sbin/ldconfig
#echo pwcheck_method: pam >; /usr/lib/sasl/smtpd.conf
#cd ..

安装Postfix
如果系统上有sendmail,请先将其停止,并备份sendmail的二进制文件和配置文件。
#tar zvxf postfix*gz
#cd postfix*
#/usr/sbin/groupadd postfix
#/usr/sbin/groupadd postdrop
#/usr/sbin/useradd -g postfix -d /dev/null -s /bin/false postfix
#make -f Makefile.init makefiles 'CCARGS=-DUSE_SASL_AUTH -DHAS_MYSQL -I/usr/local/mysql/include/mysql -I/usr/local/include'
'AUXLIBS=-L/usr/local/mysql/lib/mysql -L/usr/local/lib/sasl -lmysqlclient -lsasl -lz -lm'
#make install
#cd ..

安装courier-imap
#tar jvxf courier-imap*bz2
#cd courier-imap*
#./configure --without-authpwd --without-authpam --without-authuserdb --without-authshadow
--without-cram --without-chkpw --without-ldap --without-pgsql --without-authdaemon
--without-authcustom --with-authmysql
#make
#make check
#make install
#make install-configure
#cd ..

创建数据库,表。表结构见下:
#/usr/local/mysql/bin/mysql
mysql>;create database postfix;
mysql>;exit;
#/usr/local/mysql/bin/mysql < /tmp/postfix.sql

#####cut from here#####(postfix.sql)
CREATE TABLE postfix_mailauth (
userid char(20) binary NOT NULL default '',
username char(60) NOT NULL default '',
domain char(50) NOT NULL default '',
uid smallint(5) unsigned NOT NULL default '12345',
gid smallint(5) unsigned NOT NULL default '12345',
clearpw char(20) binary NOT NULL default '',
home char(100) NOT NULL default '',
maildir char(150) NOT NULL default '',
quota char(100) NOT NULL default '',
last_access int(10) unsigned NOT NULL default '0',
status tinyint(3) unsigned NOT NULL default '1',
passwd_lastchanged int(10) unsigned NOT NULL default '0',
PRIMARY KEY (username),
KEY status (status)
) TYPE=MyISAM;

CREATE TABLE postfix_forward (
username varchar(50) NOT NULL default '',
domain varchar(40) NOT NULL default '',
forward_addr text NOT NULL,
PRIMARY KEY (username)
) TYPE=MyISAM;

CREATE TABLE postfix_transport (
domain varchar(50) NOT NULL default '',
transport varchar( NOT NULL default 'virtual:',
PRIMARY KEY (domain)
) TYPE=MyISAM;
#####cut end#####
其中的uid和gid的default值请设置为系统用户postfix的uid和gid

配置pam
在/etc/pam.d/下创建文件smtp,内容如下:
#####
auth sufficient pam_mysql.so user=your_userid passwd=your_password host=localhost db=postfix table=postfix_mailauth usercolumn=username passwdcolumn=clearpw crypt=0
account required pam_mysql.so user=your_userid passwd=your_password host=localhost db=postfix table=postfix_mailauth usercolumn=username passwdcolumn=clearpw crypt=0
auth sufficient pam_unix_auth.so
account sufficient pam_unix_acct.so
#####

编辑courier-imap配置文件/usr/lib/courier-imap/etc/authmysqlrc,设置访问mysql的一些参数,比较简单,参见注释就行。

编辑/etc/postfix/main.cf
一般的配置请参见文件内的注释,需要注意的是以下几处:
#####所有的帐号,别名均在数据库中
virtual_mailbox_domains = $mydomain, esphere.info, capitaltech.net, netruth.com, netruth.biz
transport_maps = mysql:/etc/postfix/transport.cf
virtual_mailbox_base = /
virtual_uid_maps = mysql:/etc/postfix/uid.cf
virtual_gid_maps = mysql:/etc/postfix/gid.cf
virtual_mailbox_maps = mysql:/etc/postfix/users.cf
virtual_maps = mysql:/etc/postfix/forward.cf
#####注意这里的最后的/不能丢
home_mailbox = Maildir/
#####SASL
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated permit_auth_destination reject
smtpd_sasl_security_options = noanonymous
smtpd_client_restrictions = permit_sasl_authenticated
broken_sasl_auth_clients = yes

#####uid.cf
user = your_userid
password = your_password
dbname = postfix
table = postfix_mailauth
select_field = uid
where_field = username
hosts = localhost

#####gid.cf
user = your_userid
password = your_password
dbname = postfix
table = postfix_mailauth
select_field = gid
where_field = username
hosts = localhost

#####transport.cf
user = your_userid
password = your_password
dbname = postfix
table = postfix_transport
select_field = transport
where_field = domain
hosts = localhost

#####forward.cf
user = your_userid
password = your_password
dbname = postfix
table = postfix_forward
select_field = forward_addr
where_field = username
hosts = localhost

#####users.cf
user = your_userid
password = your_password
dbname = postfix
table = postfix_mailauth
select_field = maildir
where_field = username
hosts = localhost

到此基本成功,剩下的写些php脚本,所有的事情(添加、删除、修改用户、别名、域名)都可以通过web来做。

注:上文中的your_userid和your_password是指访问mysql的用户名和密码。

#####boot pop3 service
/usr/lib/courier-imap/libexec/pop3d.rc start
#####boot postfix
/usr/sbin/postfix start

发布人:         admin         类别:         精彩技术文摘         发布日期:         2005-05-23 16:56:54         总浏览:         3
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP