Cấu hình người dùng ảo Postfix với OpenLDAP

Thiết lập hệ thống thư ảo sử dụng Postfix, Dovecot và OpenLDAP

Trong hướng dẫn này, chúng ta sẽ triển khai hệ thống email ảo sử dụng OpenLDAP làm kho lưu trữ người dùng. Các bước sau đây được thực hiện trên nền tảng Debian 11.

1. Cấu hình OpenLDAP cho người dùng thư

Thêm cấu trúc thư mục và tài khoản mẫu qua LDIF:

# file: mail-dich.ldif

dn: ou=EMAIL,dc=example,dc=org
objectClass: organizationalUnit
ou: EMAIL

dn: uid=nguoidung1,ou=EMAIL,dc=example,dc=org
objectClass: inetOrgPerson
uid: nguoidung1
sn: NguoiDung1
cn: NguoiDung1
mail: /var/mail/vhosts/example.org/nguoidung1/
userPassword: {SSHA}Kj7vPqRmNnZbLxY0TfGcHdIeJfKg

dn: uid=nguoidung2,ou=EMAIL,dc=example,dc=org
objectClass: inetOrgPerson
uid: nguoidung2
sn: NguoiDung2
cn: NguoiDung2
mail: /var/mail/vhosts/example.org/nguoidung2/
userPassword: {SSHA}MnBvQwSrTpYiUoPjKlMnBvQwSrTpYiU

Thực hiện import bằng lệnh:

ldapadd -x -D "cn=admin,dc=example,dc=org" -W -f mail-dich.ldif

2. Cấu hình Postfix

Tạo tài khoản hệ thống xử lý thư ảo:

useradd -r -u 150 -g vmail -s /usr/sbin/nologin -d /var/mail/vhosts vmail

Chỉnh sửa file cấu hình chính:

# /etc/postfix/main.cf

virtual_transport = lmtp:unix:private/dovecot-lmtp
virtual_gid_maps = static:150
virtual_uid_maps = static:150
virtual_mailbox_domains = example.org
virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cf
virtual_mailbox_base = /
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sender_restrictions = reject_non_fqdn_sender, permit_sasl_authenticated

File truy vấn LDAP:

# /etc/postfix/ldap-users.cf

server_host = ldap.example.org
search_base = ou=EMAIL,dc=example,dc=org
bind_dn = cn=admin,dc=example,dc=org
bind_pw = ldap_admin_password
query_filter = (&(objectClass=inetOrgPerson)(uid=%s))
result_attribute = mail

3. Thiết lập Dovecot

Cấu hình xác thực LDAP:

# /etc/dovecot/dovecot-ldap.conf.ext

hosts = ldap.example.org
dn = cn=admin,dc=example,dc=org
dnpass = ldap_admin_password
base = ou=EMAIL,dc=example,dc=org
user_attrs = mail=home
user_filter = (&(objectClass=inetOrgPerson)(uid=%u))
pass_attrs = uid=user,userPassword=password
default_pass_scheme = SSHA

Cấu hình thư mục lưu trữ:

# /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:/var/mail/vhosts/%d/%n
mail_uid = vmail
mail_gid = vmail

Thiết lập socket giao tiếp với Postfix:

# /etc/dovecot/conf.d/10-master.conf

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}

4. Kiểm tra hệ thống

Cấp quyền truy cập cho thư mục lưu trữ:

chown -R vmail:vmail /var/mail/vhosts

Khởi động lại dịch vụ:

systemctl restart postfix dovecot

Kiểm tra xác thực SMTP:

openssl s_client -connect localhost:25 -starttls smtp

Sau khi kết nối, nhập:

EHLO example.org
AUTH PLAIN AG5ndW9pZHVuZzEAcGFzc3dvcmQxMjM=
MAIL FROM: <nguoidung1@example.org>
RCPT TO: <nguoidung2@example.org>

Kiểm tra POP3:

telnet localhost 110
USER nguoidung2
PASS matkhau456
LIST

Thẻ: Postfix openldap dovecot virtual-users mail-server

Đăng vào ngày 22 tháng 6 lúc 04:45