搭建集成LDAP的Webmail

使用postfix+dovecot+ldap搭建roundcube webmail

LDAP

  • 安装slapd
1
2
apt install slapd
# 会配置ldap管理员密码
  • 重新配置slapd
1
2
3
4
5
6
7
8
dpkg-reconfigure slapd

# Omit no
# DNS delix.com
# OrganizationName delix
# 管理员密码
# purge yes
# remove yes
  • 生成密码

    • 在dovecot端生成
    • 生成密码adminadmin doveadm pw -s ssha -p adminadmin / slappasswd
    • 生成后可以使用scp传输到data上
  • 创建mail.ldif (随便放哪里,记得就行)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# mail组
dn: ou=mail,dc=delix,dc=com
ou: mail
objectClass: organizationalUnit

# zhangsan
dn: uid=zhangsan,ou=mail,dc=delix,dc=com
ou: mail
objectClass: account
objectClass: posixAccount
cn: zhangsan@delix.com
uid: zhangsan
uidNumber: 10001 # 每个用户不要重复
gidNumber: 8 # mail组的gid
homeDirectory: /var/mail/zhangsan
userPassword: {SSHA}E7olti3D6AXyWH0HVOce83CqH5R7Cjb9

# lisi
dn: uid=lisi,ou=mail,dc=delix,dc=com
ou: mail
objectClass: account
objectClass: posixAccount
cn: lisi@delix.com
uid: lisi
uidNumber: 10002
gidNumber: 8
homeDirectory: /var/mail/lisi
userPassword: {SSHA}E7olti3D6AXyWH0HVOce83CqH5R7Cjb9
  • 添加记录 ldapadd -x -D "cn=admin,dc=delix,dc=com" -w adminadmin -f mail.ldif

  • 删除记录 ldapdelete -x -D "cn=admin,dc=delix,dc=com" -w adminadmin "<dn>"

dovecot

dovecot建议先不要配置ssl,调通以后再上ssl

  • 安装dovecot
1
apt install dovecot-core dovecot-ldap dovecot-lmtpd dovecot-imapd
  • 配置 dovecot.conf
1
2
3
4
# 解除注释
listen = *, ::

login_trusted_networks = 用空格分开允许的ip段
  • 配置 10-master.conf
1
2
3
4
5
6
# imap端口打开(不复制配置了)
# lmtp端口打开
# Postfix smtp-auth
inet_listener {
    port = 8888
}
  • 配置 10-auth.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 这个开启ssl以后再调整为yes,没开启ssl一定要声明为no
disable_plain_text_auth yes

# 添加login
auth_mechanisms = plain login

# 解除注释
!include auth-ldap.conf.ext

# 这里改成%n是因为我们验证的用户只有用户名,没有@delix.com
auth_username_format = %n
  • auth-ldap.conf.ext文件中包含了/etc/dovecot/dovecot-ldap.conf.ext,编辑这个文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
hosts = 192.168.80.31:389
uris = ldap://192.168.80.31:389
dn = ou=mail,dc=delix,dc=com
dnpass = adminadmin
auth_bind = yes
auth_bind_userdn = uid=%u,ou=mail,dc=delix,dc=com
base = ou=mail,dc=delix,dc=com
# 解除注释
scopt = tree
# 解除注释
user_attrs = homeDirectory=home,uidNumber=uid,gidNumber=gid,cn=mail
# 解除注释
user_filter = (&(objectClass=posixAccount)(uid=%u))
# 解除注释
pass_filter = (&(objectClass=poxisAccount)(uid=%u))
  • 配置 10-mail.conf
1
2
3
4
5
namespace inbox {
    #...
    location = ~/Maildir
    #...
}

Postfix

日志需要rsyslog,安装后启用,在/var/log/mail.log

  • 安装postfix、rsyslog

  • 重新配置postfix dpkg-reconfigure postfix

  • 配置main.cf

1
2
3
4
5
6
7
8
#...
smptd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smptd_sasl_mechanism_filter = plain login
smtpd_sasl_path = inet:192.168.80.33:8888 # dovecot sasl地址:端口

home_mailbox = Maildir/
mailbox_transport = lmtp:192.168.80.33:24 # dovecot lmtp

配置SSL

生成CA、证书部分省略,CN是ldap的域名就行

配置LDAP服务器

  • 编辑/etc/ldap/ldap.conf
1
TLS_CACERT  /etc/ldap/mail/ca.crt # CA证书
  • /etc/default/slapd 中确认 SLAPD_USERSLAPD_GROUP 的用户和组(运行ldap的用户和组)
  • /etc/default/slapdSLAPD_SERVICES="ldaps:/// ldapi:///"
1
SLAPD_SERVICES="ldaps:/// lapi://"
  • 修改证书和密钥的所有权为SLAPD_USERSLAPD_GROUP的值
  • 编辑add-tls.ldif
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
dn: cn=config
changetype: modify
add: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/ldap/mail/ca.crt # ca证书位置
-
add: olcTLSCertificateFile
olcTLSCertificateFile: /etc/ldap/mail/ldap.delix.com.crt # 域名证书
-
add: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/ldap/mail/ldap.delix.com.key # 域名key
  • 执行 ldapadd -Y EXTERNAL -H ldapi:/// -f add-tls.ldif
  • 检查 slapcat -b "cn=config" | grep olcTLS (有上面三条证书的结果)
  • 验证 ldapsearch -x -H ldaps://ldap.delix.com -D "cn=admin,dc=delix,dc=com" -w adminadmin -b "ou=mail,dc=delix,dc=com"

ldapsearch 可以添加-d8 参数查看详细信息

dovecot ldaps

建立在之前的配置上

  • 编辑 /etc/dovecot/dovecot-ldap.conf
1
2
3
4
5
6
hosts = 172.16.100.103:636 # data 地址和ldaps端口
uris = ldaps://ldap.delix.com/ # 有了这个就不需要tls on
#...
tls_ca_cert_file = /etc/dovecot/ca.crt # ca证书
#...
tls_require_cert = never
  • 连接测试 doveadm auth login zhangsan <密码>

dovecot imaps

  • 配置 10-master.conf 打开加密端口,打开ssl on
  • 配置 10-ssl.conf
1
2
3
4
5
ssl = yes
ssl_cert = </etc/cert/cert.crt # imap.delix.com域名证书
ssk_key = </etc/cert/cert.key # 密钥
#...
ssl_ca = </etc/cert/ca.crt

postfix smtps

  • 配置 main.cf
1
2
3
# 注释smtpd_tls_security_level部分,可以在master.cf中开启
smtpd_tls_cert_file = /root/cert/smtp.delix.com
smtpd_tls_key_file = /root/cert/ca.key
  • 配置 master.cf
1
2
3
4
submissions inet n - y - - smptd
  -o syslog_name=postfix/submissions
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes

安装webmail

  • apt install mariadb-server
  • apt install roundcube

rc

  • 设置密码

  • 设置数据库

  • dpkg-reconfigure roundcube-core

配置完成后,apache2的配置文件在/etc/apache2/conf-enable/roundcube.conf

  • 修改 /etc/apache2/sites-enabled/000-default.conf
1
DocumentRoot /var/lib/roundcube/public_html

roundcube的日志在 /var/log/roundcube

  • 设置roundcube邮件服务器,编辑/var/lib/roundcube/config/config.inc.php
1
2
3
// 这里注意最好使用单引号
$config['imap_host'] = ['ssl://imap.delix.com:993']; # 注意不要删除结尾分号
$config['smtp_host'] = 'ssl://smtp.delix.com:465'

其他文档

添加根证书信任

1
2
cp ca.crt /usr/local/share/ca-certificates
update-ca-certificates

Built with Hugo
主题 StackJimmy 设计