CentOS6下的postfix、dovecot、ldap整合


准备工作
防火墙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 38400
backup.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.164
backup.org.        IN      MX      10 192.168.0.164
mail.backup.org.        IN      MX      10 192.168.0.164
mail.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: saslauthd
mech_list: plain login

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

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

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

service postfix restart
service saslauthd restart

chkconfig postfix on
chkconfig saslauthd on

 # 设置默认MTA
alternatives --config mta
选中postfix为默认MTA

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

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

因此先改为

smtpd_sasl_auth_enable = no
再次telnet
telnet xx 25

ehlo localhost
mail from:admin@backup.org
rcpt to:admin@backup.org
data
From:xxoo@test.org
To:admin@backup.org
Subject: My first
test
.
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 110

user johndoe
pass password
list
retr 1
quit

这样可以看到上一步试验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.1
search_base = ou=People,dc=mydomain,dc=com
version = 3
query_filter = (&(objectclass=inetOrgPerson)(initials=%s))
result_attribute = uid
result_format = %s/Maildir/
bind = yes
bind_dn = cn=Manager,dc=mydomain,dc=com
bind_pw = OK

试试这个配置行不行

postmap -q xxoo@backup.org ldap:/etc/postfix/ldap-users.cf

将输出
xxoo/Maildir/

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

virtual_mailbox_domains = $mydomain
virtual_mailbox_base = /home/vmail/
virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cf
virtual_uid_maps = static:5000
virtual_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 this
hosts = 127.0.0.1
base = ou=People,dc=mydomain,dc=com
ldap_version = 3
auth_bind = yes
dn ="cn=Manager,dc=mydomain,dc=com"
dnpass="xxoo123"
sasl_bind = no
tls = no
#auth_bind_userdn = cn=Manager,dc=mydomain,dc=com
user_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

# 只能保留一个 userdb
userdb {
  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 = yes
broken_sasl_auth_clients = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
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
message_size_limit = 157286400
mailbox_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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值