作者: 邓卫华   shamdeng(###)163.com 把(###)换为“@”
                      http://dengweihua1.blog.51cto.com
相关产品:
ChangeLog:
              2009.11.29  发布初稿
              2009.12.3  增加基于POSTFIX的邮件归档与用户收发邮件抄送部门经理
              2009.12.6  修改当某用户发送给g1(用户)和group1(组,含g1用户),g1会收到两封邮件
                              增加了用户容量配额的控制
             2010.1.4    增加:默认情况下authdaemond为了加快认证速度会对密码进行缓存,有些用户需要修改密码后立即生效.修改smtpd.conf文件
前言:
      之前我写了一个 Postfix+Dovecot+Windows Active Directory集成认证的文章,最后发现能使用的webmail确实是非常有限。最后换了好几个WEBMAIL发现还是使用extmail比其它免费的WEBMAIL无法在界面还是操作上都非常符合我们的要求。因此最后开始测试EXTMAIL,但是测试的结果非常让人沮丧,在EXTMAIL中设置好LDAP后还是无法认证成功。最后看extmail支持authlib认证,我想如果我使用authlib来试试能不能通过查询windows ad来用户名和密码使之整合。
注:在这里因为我觉得authlib是courier开发的,配合courier-imap应该是不错的选择,所以我放弃了dovecot,当然您也可以不安装courier-imap也是可以的。
一、所需软件:
1、postfix  邮件的MTA软件,负责邮件的进、出(server-server/client-server)
2、authlib  courier-imap和postfix调用authlib来查询AD进行用户密码和密码认证。
3、courier-imap :提供pop3和imap邮件功能(您也可以使用dovecot).
4、openldap的客户端及开发库文件。
5、extmail (含extmail所需要的很多perl模块)
6、apache和php
7、cyrus-sasl2 postfix通过sasl来查询authlib进行密码查询与验证。
版本:
1、openldap
openldap-2.3.43-3.el5
openldap-clients-2.3.43-3.el5
openldap-devel-2.3.43-3.el5
2、cyrus-sasl
cyrus-sasl-devel-2.1.22-5.el5
cyrus-sasl-2.1.22-5.el5
cyrus-sasl-lib-2.1.22-5.el5
cyrus-sasl-plain-2.1.22-5.el5
3、postfix
mail_version = 2.6.5
4、authlib
courier-authlib-0.62.4
6、courier-imap
courier-imap-4.6.0
6、extmail
extmail-1.1.1.tar.gz
二、Windows 2008 AD设置
1、运行dcpromo 提升2008的域控(需要DNS)
2、建立mailuser的OU,并在其下建立vmail用户密码123
p_w_picpath
三、编译程序
1、openldap
#yum install openldap.i386 openldap-clients.i386 openldap-devel.i386

2、authlib
#./configure --with-authldap –without-authmysql –without-authpgsql
#make
#make install
#make install-configure
3、postfix
$make tidy
$make makefiles CCARGS="-DUSE_SASL_AUTH -DUSE_CYRUS_SASL  -I/usr/local/include/sasl –I/usr/include" AUXLIBS="-L/usr/local/lib –lsasl2 –L/usr/lib –lldap –L/usr/lib –llber”
$make
#make install
完成后可运行如下命令检查编入的模块:cyrus
#postconf -a
cyrus
#
4、courier-imap
$./configure
$make
$make check
#make install
#make install-configure
5、apache和php可以通过yum install 来安装。
四、配置authlib查询windows ad 的LDAP认证。
# cd /usr/local/etc/authlib/
authdaemonrc的内容如下:
[root@mail authlib]# cat authdaemonrc |grep -v "^#"
authmodulelist="authldap"
authmodulelistorig="authldap"
daemons=5
authdaemonvar=/usr/local/var/spool/authdaemon
#请注意authdaemon文件的路径及权限要求postfix的运行帐户有执行的权限。
DEBUG_LOGIN=2
DEFAULTOPTIONS=""
LOGGEROPTS=""
#
配置authldaprc文件进行WINDOWS AD查询。
请注意配置文件的格式为 “变量[空格或TAB]值” 无论在行首或行未不要有任何多余的空格。
[root@mail authlib]# cat authldaprc |grep -v "^#"
LDAP_URI        ldap://10.0.3.3
LDAP_PROTOCOL_VERSION   3
LDAP_BASEDN     ou=mailuser,dc=test,dc=com
LDAP_BINDDN     vmail@test.com
LDAP_BINDPW     123
LDAP_TIMEOUT            5
LDAP_AUTHBIND           1
LDAP_MAIL               mail
LDAP_FILTER     (&(objectClass=person)(!(userAccountControl=66050)))
LDAP_DOMAIN             test.com
LDAP_GLOB_UID           postfix
LDAP_GLOB_GID           postfix
LDAP_HOMEDIR    sAMAccountName
LDAP_MAILROOT           /data/vmail
LDAP_DEREF              never
LDAP_FULLNAME   displayName
LDAP_CRYPTPW            userPassword
#2009.12.6增加(此功能需要扩展AD架构,我最近正在写扩展AD架构的一个小程序,以后将会发布)
LDAP_MAILDIRQUOTA       mailmessagestore
注意:
1、 *BASE_DN一定要指定一个明确的OU值,如果您用dc=test,dc=com 则会出现无法查询到任何帐户,这个问题害得我研究了几多天的时间.更详细的信息请见“ 关于authlib集成windows ad失败的分析并解决[草稿]
2、BIND_DN中 vmail@test.com vmail是您在Windows AD的mailuser这个OU下面建立的帐户。test.com 是您的WINDOWS AD的域名。
3、LDAP_MAIL和LDAP_FILTER是通过ldap查询时的过滤规则同mail一起使用,本文的通过authlib后就成为: (&(&(objectClass=person)(!(userAccountControl=66050)))(mail=username's email address) )
4、LDAP_GLOB_UID和GID参数的使用者帐户必须要与postfix的和extmail的一至,否则会出现chdr Maildir错误。
5、LDAP_HOMEDIR这个参数这里要注意,一般的“用户家目录”格式如下: /data/vmail/test/Maildir (您的可能会不一样),其它/data/vmail为邮件的根目录,test为用户的“家目录”,Maildir这个参数会自动在courier-imap中pop3d这个配置文件中指定MAILDIRPATH=Maildir.
6、mailMessageStore的值需要在Winows Ad中运行adsiedit.msc 找到用户并修改mailMessageStore的值,如要设置为1K则值为:1024S (我正在写一个直接在WINDOWS 上运行的用户管理程序,可以直接在程序中修改配额,不用这么麻烦)
--
下面开始配置courier-IMAP的POP3和IMAP功能(extmail只使用了pop3功能,所以可以不用配置imap)
#cd /usr/lib/courier-imap/etc
[root@mail etc]# cat pop3d |grep -v "^#"
PIDFILE=/var/run/pop3d.pid
MAXDAEMONS=40
MAXPERIP=4
POP3AUTH="LOGIN PLAIN"
POP3AUTH_ORIG="PLAIN LOGIN CRAM-MD5 CRAM-SHA1 CRAM-SHA256"
POP3AUTH_TLS=""
POP3AUTH_TLS_ORIG="LOGIN PLAIN"
POP3_PROXY=0
PORT=110
ADDRESS=0
TCPDOPTS="-nodnslookup -noidentlookup"
LOGGEROPTS="-name=pop3d"
POP3DSTART=YES
MAILDIRPATH=Maildir
[root@mail etc]#
OK,基本的POP3认证auhlib就成功了,现在我们测试一下。在开始测试test用户之前我们需要确认在WINDOWS AD中test用户的邮件地址已经写为 test@test.com 了。如下图:
p_w_picpath
#authtest –s login test “123”
Authentication succeeded.
     Authenticated: test  (uid 500, gid 501)
    Home Directory: /data/vmail/test
           Maildir: (none)
             Quota: (none)
Encrypted Password: (none)
Cleartext Password: 123
           Options: (none)
[root@mail etc]#
出现以上提示说明配置成功了,如果失败最大的可能就是你的authldaprc文件中的语法不正确或多了一个或多个空格。
本文出自:邓卫华的博客地址是:   http://dengweihua1.blog.51cto.com
四、配置postfix与sasl2。
1、增加smtpd.conf文件
[root@mail etc]# cat /usr/local/lib/sasl2/smtpd.conf
pwcheck_method:authdaemond
log_level:3
mech_list: PLAIN LOGIN
authdaemond_path:/usr/local/var/spool/authdaemon/socket
#reauth_timeout DIGEST-MD5 0 ####设置密码缓存时间为0,根据您的情况决定。
[root@mail etc]#
2、修改postfix的main.cf配置文件
[root@mail etc]# postconf –n
broken_sasl_auth_clients = yes
home_mailbox = Maildir/
mail_owner = postfix
mydestination = $myhostname,localhost.$mydomain,localhost
mydomain = test.com
myhostname = mail.test.com
mynetworks = 127.0.0.0/8
mynetworks_style = host
myorigin = $mydomain
smtp_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_authenticated_header = yes
smtpd_sasl_local_domain = test.com
smtpd_sasl_type = cyrus
unknown_local_recipient_reject_code = 550
virtual_alias_maps = ldap:/etc/postfix/ldap-group.cf
virtual_gid_maps = static:501
virtual_mailbox_base = /data/vmail
virtual_mailbox_domains = $mydomain
virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cf
virtual_uid_maps = static:500
#2009.12.3增加
always_bcc=test@test.com
sender_bcc_maps=hash:/etc/postfix/sender_bcc_maps
recipient_bcc_maps=hash:/etc/postfix/recipient_bcc
#2009.12.6增加
enable_original_recipient=no
#
#cat /etc/postfix/sender_bcc_maps //把g1用户发送的邮件抄送给部门经理或某个人。
#postmaq /etc/postfix/sender_bcc_maps //生成sender_bcc_maps文件的hash格式数据库。
#cat /etc/postfix/recipient_bcc   //把g1用户收到的邮件抄送给部门经理
#postmaq /etc/postfix/recipient_bcc
*postfix配置文件中的always_bcc 是邮件归档邮箱,把所有收发的邮件转发到test@test.com 邮件。
#cat /etc/postfix/ldap-users.cf
[root@mail etc]# cat /etc/postfix/ldap-users.cf
server_host=10.0.3.3
search_base= dc=test,dc=com
version=3
query_filter=(&(objectclass=person)(|(mail=%s)(othermailbox=%s))(!(userAccountControl=66050)))
bind=yes
bind_dn=vmail@test.com
bind_pw=123
#result_attribute=sAMAccountName
result_attribute=samaccountname
result_format = %s/Maildir/
 
[root@mail etc]# cat /etc/postfix/ldap-group.cf
server_host=10.0.3.3
search_base=dc=test,dc=com
version=3
query_filter=(&(objectClass=group)(mail=%s))
leaf_result_attribute = mail
special_result_attribute = member
bind = yes
bind_dn = vmail@test.com
bind_pw = 123
#postfix stop
#postfix start
#cat /var/log/maillog 
确认postfix启动正常无任何异常。
五、配置extmail(在之前请确认已经设置好apache及php并且把apache的user和group设置为postfix用户)
[root@mail ~]# cat /var/www/html/extmail/webmail.cf |grep -v "^#"
SYS_CONFIG = /var/www/html/extmail/  #注意你的APACHE的路径。因为是测试环境我未做太多更改
SYS_LANGDIR = /var/www/html/extmail/lang
SYS_TEMPLDIR = /var/www/html/extmail/html
SYS_HTTP_CACHE = 0
SYS_SMTP_HOST = 127.0.0.1
SYS_SMTP_PORT = 25
SYS_SMTP_TIMEOUT = 5
SYS_SPAM_REPORT_ON = 0
SYS_SPAM_REPORT_TYPE = dspam
SYS_SHOW_WARN = 0
SYS_PERMIT_NOQUOTA = 1
SYS_SESS_DIR = /tmp
SYS_UPLOAD_TMPDIR = /tmp
SYS_LOG_ON = 1
SYS_LOG_TYPE = file
SYS_LOG_FILE = /var/log/extmail.log
SYS_SESS_TIMEOUT = 0
SYS_SESS_COOKIE_ONLY = 1
SYS_USER_PSIZE = 10
SYS_USER_SCREEN = auto
SYS_USER_LANG = en_US
SYS_APP_TYPE = WebMail
SYS_USER_TEMPLATE = default
SYS_USER_CHARSET = utf-8
SYS_USER_TRYLOCAL = 1
SYS_USER_TIMEZONE = +0800
SYS_USER_CCSENT = 1
SYS_USER_SHOW_HTML = 1
SYS_USER_COMPOSE_HTML = 1
SYS_USER_CONV_LINK =1
SYS_USER_ADDR2ABOOK = 1
SYS_MESSAGE_SIZE_LIMIT = 5242880
SYS_MIN_PASS_LEN = 2
SYS_MFILTER_ON = 1
SYS_SHOW_SIGNUP = 0
SYS_DEBUG_ON = 1
SYS_AUTH_TYPE = authlib
SYS_AUTH_SCHEMA = virtual
SYS_CRYPT_TYPE = md5crypt
SYS_AUTHLIB_SOCKET = /usr/local/var/spool/authdaemon/socket
SYS_G_ABOOK_TYPE = file
SYS_G_ABOOK_LDAP_HOST = 10.0.3.3
SYS_G_ABOOK_LDAP_BASE = ou=AddressBook,dc=example.com
SYS_G_ABOOK_LDAP_ROOTDN = cn=Manager,dc=example.com
SYS_G_ABOOK_LDAP_ROOTPW = secret
SYS_G_ABOOK_LDAP_FILTER = objectClass=OfficePerson
SYS_G_ABOOK_FILE_PATH = /var/www/extsuite/extmail/globabook.cf
SYS_G_ABOOK_FILE_LOCK = 1
SYS_G_ABOOK_FILE_CONVERT = 0
SYS_G_ABOOK_FILE_CHARSET = utf-8
[root@mail ~]#
注意修改黑体字的SYS_AUTHLIB_SOCKET为您的authdaemond配置文件中的路径一至,如果出现访问拒绝则请用chmod ug+x /etc/local/var/spool/authdaemon
五、确认与测试
1、确认postfix的SMTP启用了PLAIN和LOGIN认证功能
#authdaemond start
#postfix start
#telnet 0 25
p_w_picpath
测试SMTP发信功能:
[root@mail ~]# !telnet
telnet 0 25
Trying 0.0.0.0...
Connected to 0 (0.0.0.0).
Escape character is '^]'.
220 mail.test.com ESMTP Postfix
helo mail
250 mail.test.com
mail from:<shamdeng@163.com>
250 2.1.0 Ok
rcpt to:<test@test.com>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
hello,this is a est.
.
250 2.0.0 Ok: queued as 6A68F1B8151
quit
221 2.0.0 Bye
Connection closed by foreign host.
[root@mail ~]#
命令行发送成功,在确认日志
Nov 29 15:06:26 mail postfix/smtpd[26373]: connect from localhost.localdomain[127.0.0.1]
Nov 29 15:06:29 mail postfix/smtpd[26373]: B6F6A1B8151: client=localhost.localdomain[127.0.0.1]
Nov 29 15:06:30 mail postfix/cleanup[26341]: B6F6A1B8151: message-id=<20091129070629.B6F6A1B8151@mail.test.com>
Nov 29 15:06:30 mail postfix/qmgr[24893]: B6F6A1B8151: from=<shamdeng@163.com>, size=336, nrcpt=1 (queue active)
Nov 29 15:06:30 mail postfix/virtual[26342]: B6F6A1B8151: to=<test@test.com>, relay=virtual, delay=1.5, delays=1.5/0.01/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Nov 29 15:06:30 mail postfix/qmgr[24893]: B6F6A1B8151: removed
Nov 29 15:06:30 mail postfix/smtpd[26373]: disconnect from localhost.localdomain[127.0.0.1]
[root@mail ~]#
OK,从日志上来看已经完全成功了。
2、测试POP3功能
#telnet 0 110
[root@mail ~]# telnet 0 110
Trying 0.0.0.0...
Connected to 0 (0.0.0.0).
Escape character is '^]'.
+OK Hello there.
user test
+OK Password required.
pass 123
+OK logged in.
list
+OK POP3 clients that break here, they violate STD53.
1 292
2 2119
3 421
4 654
5 996
6 443
7 428
.
quit
+OK Bye-bye.
Connection closed by foreign host.
[root@mail ~]#
3、用客户端软件对pop3和SMTP功能进行测试
未选重“我的服务器需要身份验证”功能发送。
p_w_picpath
确认无法发送成功。
p_w_picpath
4、测试EXTMAIL是否成功:
p_w_picpath \
进入后的界面
p_w_picpath
后记:
      好了,终于写完,如果您有什么问题请联系我。shamdeng ### 163.com (把###换成 @)或在我的博客留言。
      我的博客地址是:   http://dengweihua1.blog.51cto.com
我在网上看到有人把postfix和AD集成整合WINDOWS AD认证的文章在买钱(在TAOBAO网上),这样做是不是断了人家的钱路呢?呵~呵,确实现在在网上写的LINUX MAIL服务器与WINDOWS AD整合的文章非常少。
关键字: postfix ad /courier ad / windows ad / linux mail windows ad