网页浏览总次数

2010年11月20日星期六

EMAIL系统配置花絮

本文配置环境为 CentOS 5.4 for x86_64,用户帐号信息存储在OpenLDAP数据库,MTA为Postfix,MDA为Cyrus Imapd,认证通过saslauthd服务,因为内外网的关系使用dnsmasq做域名分割(domain split),为支持内网专用域解析使用了bind。

1 OpenLDAP

1.1 为安全起见会使用 TLS,但因为证书的问题经常导致认证失败,解决办法是修改 /etc/openldap/ldap.conf里的如下配置项

TLS_REQCERT allow

1.2 有些帐号希望能修改LDAP数据库,但只允许特定的ip地址来源,可在 slapd.conf做如下配置

access to dn.base="cn=Subschema" by * read

access to *
by self write
by dn.exact="uid=auth,ou=sysusers,dc=intra,dc=example,dc=com" peername.regex=127\.0\.0\.1 write
by dn.subtree="ou=sysusers,dc=intra,dc=example,dc=com" read
by anonymous auth


1.3 为提供LDAP服务的可用性,通常会配置复制(replication),以下是主端(master) slapd.conf相关配置

# For replication
index entryCSN,entryUUID eq,pres
overlay syncprov
syncprov-checkpoint 100 10
syncprov-sessionlog 100
limits dn.subtree="ou=sysusers,dc=intra,dc=example,dc=com" size=unlimited time=unlimited

从端(slave)slapd.conf的相关配置

index entryCSN,entryUUID eq,pres

syncrepl rid=101
provider=ldap://172.16.35.11:389
type=refreshAndPersist
retry="60 +"
searchbase="dc=intra,dc=example,dc=com"
filter="(objectclass=*)"
attrs="*,+"
scope=sub
schemachecking=off
updatedn="cn=Manager,dc=intra,dc=example,dc=com"
bindmethod=simple
binddn="uid=lsync,ou=sysusers,dc=intra,dc=example,dc=com"
credentials=111111

为使用replication功能需安装rpm包 openldap-servers-overlays

1.4 为调试ldap交互信息,可打开日志,按如下配置slapd.conf即可

# 为让配置生效需配置syslog
# local4.* /var/log/ldap.log
#
#loglevel Logging description
#-1 enable all debugging
#0 no debugging
#1 trace function calls
#2 debug packet handling
#4 heavy trace debugging
#8 connection management
#16 print out packets sent and received
#32 search filter processing
#64 configuration file processing
#128 access control list processing
#256 stats log connections/operations/results
#512 stats log entries sent
#1024 print communication with shell backends
#2048 print entry parsing debugging
loglevel 928

1.5 初始化ldap数据库。编辑如下文件 base.ldif

dn: dc=intra,dc=example,dc=com
dc: intra
objectClass: domain

启动ldap服务后,用ldapadd命令导入。

2 Postfix
2.1 配置从ldap数据库获取收信用户,可如下配置main.cf

local_recipient_maps = ldap:ldapintra $alias_maps
ldapintra_server_host = 172.16.35.10
ldapintra_search_base = ou=users,ou=accounts,dc=intra,dc=example,dc=com
ldapintra_bind_dn = uid=mail,ou=sysusers,dc=intra,dc=example,dc=com
ldapintra_bind_pw = 111111
ldapintra_start_tls = yes
ldapintra_version = 3
#ldapintra_scope = sub
ldapintra_query_filter = (&(mail=%s)(employeeType=staff))
ldapintra_result_attribute = uid

2.2 配置smtp认证,可如下配置main.cf
smtpd_sasl_auth_enable = yes
smtpd_sender_login_maps = pcre:/etc/postfix/sender_login
smtpd_recipient_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unauth_destination
reject_sender_login_mismatch
broken_sasl_auth_clients = yes

/usr/lib64/sasl2/smtp.conf内容如下
pwcheck_method: saslauthd
mech_list: PLAIN LOGIN

ldapdb_uri: ldap://172.16.35.10/
ldapdb_version: 3
ldapdb_start_tls: 0
ldapdb_bind_dn: cn=mail,ou=users,ou=accounts,dc=intra,dc=example,dc=com
ldapdb_bind_pw: 111111
ldapdb_search_base: ou=users,ou=accounts,dc=intra,dc=example,dc=com
ldapdb_filter: uid=%U
ldapdb_password_attr: userPassword
ldapdb_sasl: 0

/etc/postfix/sender_login内容如下

/^(.*)@example.com/ $1

通过sender_login配置我们可确保本域用户发给本域用户的信件是不可伪造的(我们不希望有人冒充CEO发给all邮件列表不该发的信 ^_^)。

2.3 配置备用邮件服务器。首先设置DNS服务器的MX记录,权值数高(优先级低)的为备用邮件服务器。备用邮件服务器的main.cf相关选项如下

relay_domains = example.com
transport_maps = hash:/etc/postfix/transport
relay_recipient_maps = ldap:ldapintra $alias_maps
ldapintra_server_host = 172.16.35.10
ldapintra_search_base = ou=users,ou=accounts,dc=intra,dc=example,dc=com
ldapintra_bind_dn = uid=mail,ou=sysusers,dc=intra,dc=example,dc=com
ldapintra_bind_pw = 111111
ldapintra_start_tls = yes
ldapintra_version = 3
#ldapintra_scope = sub
ldapintra_query_filter = (&(mail=%s)(employeeType=staff))
ldapintra_result_attribute = uid

/etc/postfix/transport内容如下

example.com relay:[mx1.example.com]

以上配置会把收到的邮件转发到mx1.example.com,注意relay_domains参数指定的域不能出现在mydestination配置项里。

2.4 要配置邮件域改写,比如把发给 test.com域的邮件改写成 example.com域,可如下配置main.cf。

recipient_canonical_maps = pcre:/etc/postfix/domain_rewrite

/etc/postfix/domain_rewrite内容如下

/^(.*)@test\.com/ $1@example.com

2.5 配置把收到的信发给 cyrus imapd,可如下配置main.cf

mailbox_transport = lmtp:unix:/var/lib/imap/socket/lmtp

以上unix套接口会被cyrus imapd创建。

2.6 相关工具。
postalias 修改别名文件后,运行它重建别名数据库
postmap 修改查询表后,运行它重建查询表,也可用它做查询表排错
postqueue 显示或flush邮件队列
postsuper 邮件队列管理程序
postcat 查看队列里的邮件内容

2.7 性能优化。参考 http://wiki.list.org/display/DOC/MTA+Performance+Tuning+Tips+for+Postfix 的一篇文章,相关main.cf参数为

default_destination_concurrency_limit=50 default_destination_recipient_limit=50 default_process_limit=200 smtp_mx_session_limit=100 smtpd_client_connection_count_limit=100 smtp_destination_concurrency_limit=100 maximal_backoff_time = 1000s minimal_backoff_time = 300s
3 Cyrus Imapd
3.1 /etc/imapd.conf内容如下

configdirectory: /var/lib/imap
partition-default: /var/spool/imap
admins: cyrus leo
sievedir: /var/lib/imap/sieve
sendmail: /usr/sbin/sendmail
hashimapspool: true
fulldirhash: true
sasl_pwcheck_method: saslauthd
sasl_mech_list: PLAIN LOGIN
sasl_saslauthd_path: /var/run/saslauthd/mux
tls_cert_file: /etc/pki/cyrus-imapd/cyrus-imapd.pem
tls_key_file: /etc/pki/cyrus-imapd/cyrus-imapd.pem
tls_ca_file: /etc/pki/tls/certs/ca-bundle.crt
virtualdomains: yes
defaultdomain: example.com

lmtp_downcase_rcpt: 1
munge8bit: no
allowusermoves: 1
allowplaintext: 1
allowplainwithouttls: 1
autocreatequota: 2048000
unixhierarchysep: no
quotawarn: 90
createonpost: 1
autocreateinboxfolders: Drafts|Sent|Trash
autosubscribeinboxfolders: Drafts|Sent|Trash
lmtp_over_quota_perm_failure: 1


3.2 配置saslauthd,/etc/saslauthd.conf内容如下

ldap_servers: ldap://172.16.35.10/
ldap_bind_dn: uid=mail,ou=sysusers,dc=intra,dc=example,dc=com
ldap_bind_pw: 111111
ldap_search_base: ou=users,ou=accounts,dc=intra,dc=example,dc=com
ldap_filter: uid=%u
ldap_password_attr: userPassword

/etc/sysconfig/saslauthd里配置项

MECH=ldap

3.3 要对pop/imap做分布,可用cyrus imapd murder,cyrus-imapd包已具有相关程序和文档。这个话题比较复杂,相关内容参考所附链接。
3.4 要删除邮件,比如只留7天的邮件,可用如下命令
su - cyrus -c '/usr/lib/cyrus-imapd/ipurge -f -d 7 user.myuser
参考 http://comments.gmane.org/gmane.mail.imap.cyrus/21988

3.4 性能调优。

安装cyrus-imapd后会附有调优文档参考,主要瓶颈是磁盘IO,可考虑多购买几块磁盘分布负载。

pop协议会创建锁文件,可把锁文档目录放到内存文件系统(tmpfs),一般64M的内存文件系统足够使用。

当出现DBERROR db4: Logging region out of memory错误时,应该是因为cyrus-imapd依赖的Berkeley DB默认内存参数太小。可编辑 /var/lib/imap/db/DB_CONFIG,内容如下(根据需要调整):
set_cachesize 0 2097152 1
set_lg_regionmax 1048576

停掉cyrus-imapd服务,运行如下命令重建数据库文件
# db_recover -h /var/lib/imap/db

重建后注意把/var/lib/imap/db目录下的文件所有者改成cyrus,组改成mail。启动cyrus-imapd服务,运行如下命令查看修改是否生效

# db_stat -m -h /var/lib/imap/db
# db_stat -l -h /var/lib/imap/db

细节参考 http://www.cjc.org/blog/archives/2006/08/22/cyrus-imap-log-and-cache-settings/ 。

Cyrus imapd遇到的另一个常见问题是报错Mailbox is locked by POP server,结果是邮件客户端反复要求用户输入密码。问题产生于如下方面:
同时有多个pop客户端收信,解决办法是把客户端收信方式由pop改成imap
连接非正常中断,往往发生在物理网络切换时,比如正在收信突然拔掉有线网换成无线,再次收信就会被锁,默认超时时间为10分钟(RFC规范要求)。不幸的是这种情况无法避免,可以从锁文件目录里(/var/lib/imap/proc/)找到被锁的用户,手工杀掉服务器端的pop3d进程,然后删除锁文件。另一种解决方法是在/etc/imapd.conf里把参数 poptimeout设短,但cyrus-imapd要求超时时间不能低于10分钟。如果你被这个问题困扰,可考虑把cyrus-imapd的源码找到,修改imap/pop3d.c的代码,找到登录后检查锁的位置,把之前已经登录的用户注销掉,然后重新尝试锁的检测。要生成rpm包可能还需安装 redhat-rpm-config 包
cyrus-imapd的性能遇到瓶颈,锁文件来不及释放,可参考安装包里的优化文档进行优化

4 DNS
4.1 要配置bind主从复制,且监听在定制的端口,比如153,可如下配置主服务的named.conf

acl nsservers { 172.16.35.10; 172.16.35.11; };

options {

allow-notify { nsservers; 127.0.0.1; };
allow-transfer { nsservers; 127.0.0.1; };
also-notify { 172.16.35.11 port 153; };

listen-on port 153 { any; };

};

从服务配置如下

acl nsservers { 172.16.35.10; 172.16.35.11; };

options {

allow-notify { nsservers; 127.0.0.1; };
allow-transfer { nsservers; 127.0.0.1; };

listen-on port 153 { any; };

};

view ... {

zone … {

masters port 153 { 172.16.35.10; };
...
};
...
};

每次修改zone数据后,必须增加序号,否则同步无法进行。

5 相关资料

http://dns-learning.twnic.net.tw/bind/toc.html
http://www.linuxmail.info/openldap-setup-howto/
http://tldp.org/HOWTO/Cyrus-IMAP.html
http://www.akadia.com/services/ssh_test_certificate.html
http://blogwords.neologix.net/neils/?p=140 OpenLDAP同步
http://oreilly.com/catalog/mimap/chapter/ch09.html 管理IMAP
http://www.linuxmail.info/smtp-authentication-postfix-centos-5/
http://www.postfix.org/documentation.html
http://www.yolinux.com/TUTORIALS/LinuxTutorialMailman.html
http://onlamp.com/pub/a/onlamp/2005/10/06/cyrus_imap.html?page=1
http://wpkg.org/Integrating_Mailman_with_a_Swish-e_search_engine
http://www.linuxjournal.com/article/9804 建立可扩展的邮件系统
http://www.cyrusimap.org/mediawiki/index.php/Cyrus_Murder_Design
http://download.oracle.com/docs/cd/E17076_02/html/api_reference/C/frame_main.html Berkeley DB配置参数说明

没有评论:

发表评论