CentOS6下的postfix、dovecot、ldap整合

来源:互联网 发布:如何查看淘宝店铺地址 编辑:程序博客网 时间:2024/05/18 01:11

准备工作

防火墙iptables

# SMTP-A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT# POP-A INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j ACCEPT# POP3S-A INPUT -m state --state NEW -m tcp -p tcp --dport 995 -j ACCEPT# IMAP-A INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT# IMAPS-A INPUT -m state --state NEW -m tcp -p tcp --dport 993 -j ACCEPT

DNS

要修改DNS,否则即使在 /etc/hosts 下加了东西,也蒙混不过,因为要的是MX类型,出错信息如下:

Host or domain name not found. Name service error for name=backup.org type=MX: Host not found, try again

vim /etc/bind/named.conf.local

 # 加上 zone "backup.org" {        type master;        file "/var/lib/bind/backup.org.hosts";        };

vim /var/lib/bind/backup.org.hosts

$ttl 38400backup.org.     IN      SOA     192.168.0.164. admin.backup.org. (                        1335510327                        10800                        3600                        604800                        38400 )backup.org.     IN      NS      192.168.0.164.backup.org.    IN      A       192.168.0.164backup.org.        IN      MX      10 192.168.0.164mail.backup.org.        IN      MX      10 192.168.0.164mail.backup.org.        IN      A       192.168.0.164

主机名

可改可不改。在用webmin的user mail功能测试时,这点可以省个事情,支持直接用用户名——当然最终我们要用LDAP的,这里作用不大,只是可以验证我们每一步的动作而已。

 vim /etc/sysconfig/network HOSTNAME=backup.org vim /etc/hosts 在127.0.0.1后面,一开始就加上 backup.org # 用hostname命令修改,则可以不用重启就生效 hostname backup.org

用系统用户做验证

参考这里 http://blog.sina.com.cn/s/blog_6f725b1a0100u997.html

注意这里用telnet验证时,请用系统用户

postfix

编辑postfix/main.cf

 sudo vim /etc/postfix/main.cf

在 main.cf 找到这些内容,并修改,改为你在 /etc/hosts中的域名

 myhostname = mail.backup.org mydomain = backup.org myorigin=$mydomain inet_interfaces = all mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain relay_domains = $mydestination mynetworks = 192.168.0.0/16, 127.0.0.0/8 home_mailbox = Maildir/ # 不显示服务器信息 smtpd_banner = $myhostname ESMTP unknown # 在main.cf最后加下面的,用SMTP认证 smtpd_sasl_auth_enable = yes # 指定SMTP认证的本地域名(主机名) smtpd_sasl_local_domain = $myhostname # 不允许匿名的方式认证 smtpd_sasl_security_options = noanonymous smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination smtpd_sasl_security_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
注:实际上这里因为没配置sasl的应用,所以这里的sasl实际测试时,是enable = no的

规定一下邮件大小

 # 规定邮件最大尺寸为150MB message_size_limit = 157286400 # 邮箱最大为300MB,当然,他需要比邮件大 mailbox_size_limit = 314572800

编辑 /etc/sasl2/smtpd.conf,原作者目的是为了使用单独的用户,但我最终要用LDAP,所以不需要理会,保留原状

pwcheck_method: saslauthdmech_list: plain login

在 /etc/skel 建立家目录模版

 mkdir /etc/skel/Maildir chmod 700 /etc/skel/Maildir

老用户似乎不会自动建立,手工建立起来吧。试试

service postfix restartservice saslauthd restartchkconfig postfix onchkconfig saslauthd on # 设置默认MTAalternatives --config mta选中postfix为默认MTA

这时候用webmin的收发是不成问题了,但仅这样还不行,还需要用telnet验证一下

这时会提示:fatal: no SASL authentication mechanisms

因此先改为

smtpd_sasl_auth_enable = no
再次telnet
telnet xx 25ehlo localhostmail from:admin@backup.orgrcpt to:admin@backup.orgdataFrom:xxoo@test.orgTo:admin@backup.orgSubject: My firsttest.quit

可以用webmin等工具看,也可以直接杀到 Maildir里看,应该在 ~/Maildir/new/ 里面。

dovecot

安装

 yum install dovecot

vim /etc/dovecot/dovecot.conf

 # 据说imap对服务器负担比较大,所以这里限定要用pop3 protocols = pop3 # 因为我禁用了ipv6,所以这里需要指定一下 listen = * # 加上trusted network,不然难以用telnet测试,会提示 # -ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections login_trusted_networks = 192.168.0.0/16, 127.0.0.0/8

用telnet验证dovecot

telnet xxoo 110user johndoepass passwordlistretr 1quit

这样可以看到上一步试验smtp时给自己的邮件,发现都没问题了,再往下走。

Virtual user by LDAP

参考 http://www.linuxmail.info/postfix-dovecot-ldap-centos-5/

注意:这里得用LDAP上的用户,下面的配置基本是直接干掉系统用户认证的。也许有遗漏,但均未测试,不保证能用。

准备工作

创建 vmail 用户和组,这里指定gid、uid,方便不同服务器用一样的配置文件:

 groupadd --gid 5000 vmail useradd  --shell /sbin/nologin --groups vmail --gid 5000  --uid 5000 vmail

说明一下,LDAP里内部的mail用的是initials。

原因这是纯粹的内部服务器,不想放到mail域里面,这样对支持地址簿功能不够友好。

而且mail地址太多,使用时候需要折腾filter什么的,不熟悉,安全第一

postfix

创建 /etc/postfix/ldap-users.cf

server_host = 127.0.0.1search_base = ou=People,dc=mydomain,dc=comversion = 3query_filter = (&(objectclass=inetOrgPerson)(initials=%s))result_attribute = uidresult_format = %s/Maildir/bind = yesbind_dn = cn=Manager,dc=mydomain,dc=combind_pw = OK

试试这个配置行不行

postmap -q xxoo@backup.org ldap:/etc/postfix/ldap-users.cf将输出xxoo/Maildir/

现在编辑 /etc/postfix/main.cf

virtual_mailbox_domains = $mydomainvirtual_mailbox_base = /home/vmail/virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cfvirtual_uid_maps = static:5000virtual_gid_maps = static:5000# 因为设置了message_size的,所以这里注意,box还是得比message大virtual_mailbox_limit = 314572800# 直接将 mydestination 改到不支持这些域名,因为有提示:# Make sure $mydomain in mydestination has been removed,# otherwise the lookup will not work and you will# get a “User unknown in local recipient table” error.mydestination = localhost.$mydomain, localhost

因为上个步骤建立过xxoo的家目录,发现可能有干扰,所以删掉/home/xxoo。

用telnet验证一下,不错,现在邮件发送到在该在的地方了: /home/vmail/xxoo

dovecot

编辑或新建 /etc/dovecot/dovecot-ldap.conf.ext

# in your slapd.conf, should at least add this# access to attribute=userPassword#        by dn="<dovecot's dn>" read # add thishosts = 127.0.0.1base = ou=People,dc=mydomain,dc=comldap_version = 3auth_bind = yesdn ="cn=Manager,dc=mydomain,dc=com"dnpass="xxoo123"sasl_bind = notls = no#auth_bind_userdn = cn=Manager,dc=mydomain,dc=comuser_filter = (&(objectClass=posixAccount)(uid=%u))pass_filter = (&(objectclass=inetOrgPerson)(uid=%u))

编辑 /etc/dovecot/conf.d/10-auth.conf

auth_username_format = %Lu#!include auth-system.conf.ext!include auth-ldap.conf.ext

编辑 /etc/dovecot/conf.d/auth-ldap.conf.ext

# 只能保留一个 userdbuserdb {  driver = static  args = uid=5000 gid=5000 home/home/vmail/%u}

用telnet试试,应该是可以了

收尾

/etc/dovecot/dovecot.conf

删掉其中的 trust network,禁止POP情况下的明文密码

现在回过头来处理SASL的问题

/etc/postfix/main.cf 中,如果 smtpd_sasl_auth_enable 为 yes,则会引发错误:

fatal: no SASL authentication mechanisms

看起来saslauthd并不负责具体工作,yum search sasl一下,看起来是推荐cyrus-sasl,很齐全的功能。

不过我们有现成的dovecot,所以用更省事的方法:

编辑 /etc/dovecot/conf.d/10-master.conf

service auth {  # ...  # Postfix smtp-auth  unix_listener /var/spool/postfix/private/auth {    mode = 0666    user = postfix    group = postfix  }  # ...}

编辑 /etc/postfix/main.cf

 # 现在,与sasl有关的部分变成了:smtpd_sasl_auth_enable = yesbroken_sasl_auth_clients = yessmtpd_sasl_type = dovecotsmtpd_sasl_path = private/authsmtpd_sasl_local_domain = $myhostnamesmtpd_sasl_security_options = noanonymoussmtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destinationsmtpd_sasl_security_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destinationmessage_size_limit = 157286400mailbox_size_limit = 314572800

重新启动postfix和dovecot,验证应该可以了。

在网上搜索到的验证方法,不太好用,所以最终我是用foxmail来验证的。

方法是:

  1. 首先在 smtpd_sasl_auth_enable = no 的情况下建立账户
  2. 收发应该都没问题
  3. 改成 smtpd_sasl_auth_enable = no
  4. 如果不搞定上面的步骤,会发现发不出去
  5. 顺次做完,搞定

后面再考虑

实际上这个也是有不少问题的:

  1. LDAP没有用加密链接
  2. 最好用SSL/TLS代替SASL,这个可以参考 http://arch.huatai.me/?p=275

原创粉丝点击