Cara Integrasi LDAP Ke Postfix dan Dovecot

Mail Server + LDAP Integration: Postfix & Dovecot

๐Ÿ“ฌ Mail Server + LDAP Integration

Postfix & Dovecot untuk Domain lksn2025.id

๐ŸŽฏ Pengertian

Postfix adalah MTA (Mail Transfer Agent) yang handle kirim-terima email via SMTP. Dovecot adalah MDA/IMAP server yang kasih akses baca email ke user. Keduanya terintegrasi ke LDAP untuk autentikasi terpusat.

๐Ÿ”„ Arsitektur Alur Kerja

Client (MUA)
    │
    ├─ SMTP (587/465) ──▶ Postfix ──▶ LDAP Auth (int-srv.lksn2025.id)
    │                                                  (cek user di ou=mail)
    │                                                        ▼
    │                                                  Maildir /var/vmail/%u/Maildir/
    │
    └─ IMAP (993) ──────▶ Dovecot ──▶ LDAP Auth
                                                │
                                                  ▼
                                              Maildir /var/vmail/%u/Maildir/

๐Ÿ“ฆ STEP 1 — Instalasi Software

# ROOT
# Update repo dulu
apt update

# Install Postfix, Dovecot (IMAP + LDAP), dan mailutils untuk testing
apt install -y postfix postfix-ldap dovecot-core dovecot-imapd dovecot-ldap mailutils

# Saat prompt Postfix muncul: pilih "Internet Site", domain = lksn2025.id

๐Ÿ‘ค STEP 2 — Buat System User & Group vmail

Semua maildir disimpan di /var/vmail/ — dikelola satu system user vmail dengan UID/GID static.

# ROOT
# Buat group vmail dengan GID 996 (sesuai virtual_gid_maps)
groupadd -g 996 vmail

# Buat user vmail dengan UID 999, GID 996, no shell, no login
useradd -u 999 -g 996 -d /var/vmail -s /usr/sbin/nologin vmail

# Buat base direktori mailbox
mkdir -p /var/vmail

# Set ownership ke vmail
chown vmail:vmail /var/vmail
chmod 700 /var/vmail

๐Ÿ” STEP 3 — SSL/TLS Certificate

๐Ÿ’ก Catatan: Pilih salah satu opsi di bawah tergantung status CA task.
# ROOT - OPSI A: Self-Signed (gunakan ini kalau belum ada CA)
# Generate self-signed cert untuk mail server
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /etc/ssl/private/mail.lksn2025.id.key \
  -out /etc/ssl/certs/mail.lksn2025.id.crt \
  -subj "/CN=mail.lksn2025.id/O=LKSN2025/C=ID"

# Set permission private key
chmod 600 /etc/ssl/private/mail.lksn2025.id.key

๐Ÿ“ฎ STEP 4 — Konfigurasi Postfix (main.cf)

# ROOT
# Edit main config Postfix
nano /etc/postfix/main.cf

Isi/tambahkan parameter berikut:

Konfigurasi
# ─── Domain ───────────────────────────────────────────────────
# Domain yang dihandle server ini
myhostname = mail.lksn2025.id
mydomain = lksn2025.id
myorigin = $mydomain
mydestination = $myhostname, localhost

# ─── Virtual Mailbox (LDAP-backed) ────────────────────────────
# Base direktori semua mailbox user virtual
virtual_mailbox_base = /var/vmail

# UID static untuk user vmail yang kelola direktori
virtual_uid_maps = static:999

# GID static untuk group vmail
virtual_gid_maps = static:996

# Mapping email -> path maildir, lookup ke file LDAP
virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cf

# Domain yang ditangani sebagai virtual mailbox
virtual_mailbox_domains = lksn2025.id

# Lempar delivery ke Dovecot via LMTP/pipe
virtual_transport = dovecot

# ─── TLS ──────────────────────────────────────────────────────
# Aktifkan TLS untuk koneksi masuk
smtpd_tls_cert_file = /etc/ssl/certs/mail.lksn2025.id.crt
smtpd_tls_key_file = /etc/ssl/private/mail.lksn2025.id.key
smtpd_tls_security_level = may
smtpd_tls_loglevel = 1

# ─── SASL Auth via Dovecot ────────────────────────────────────
# Enable autentikasi SASL
smtpd_sasl_auth_enable = yes

# Gunakan Dovecot sebagai backend SASL
smtpd_sasl_type = dovecot

# Path socket Dovecot untuk SASL
smtpd_sasl_path = private/auth

# Larang anonymous login
smtpd_sasl_security_options = noanonymous

# ─── Relay & Restrictions ─────────────────────────────────────
# Hanya relay untuk: jaringan lokal, user yg sudah auth
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

# Cek sender ke LDAP sebelum terima email — reject kalau user ga ada
smtpd_sender_restrictions = reject_unlisted_sender

๐Ÿ“‚ STEP 5 — Buat File LDAP Lookup Postfix (ldap-users.cf)

# ROOT
nano /etc/postfix/ldap-users.cf
Konfigurasi
# Input dari Postfix: %u = username dari email (bagian sebelum @)
query_filter = (uid=%u)

# Kembalikan atribut 'mail' dari LDAP sebagai path maildir
result_attribute = mail

# Alamat server LDAP
server_host = ldap://int-srv.lksn2025.id

# Search di OU khusus mail — restrict sesuai soal (hanya ou=mail)
search_base = ou=mail,dc=lksn2025,dc=id

# Login ke LDAP pakai service account
bind = yes
bind_dn = cn=admin,dc=lksn2025,dc=id
bind_pw = Skills39!

# Search ke seluruh subtree (bukan cuma surface)
scope = sub

# Pakai LDAP versi 3
version = 3
⚠️ PENTING: result_attribute = mail harus berisi value berformat ani/Maildir/ (path relatif dari virtual_mailbox_base). Pastikan atribut mail di LDAP sudah diisi sesuai.

๐Ÿ“ฌ STEP 6 — Konfigurasi Dovecot Transport di Postfix (master.cf)

# ROOT
nano /etc/postfix/master.cf

Tambahkan di bagian bawah:

Konfigurasi
# Transport 'dovecot' — pipe email dari Postfix ke Dovecot-LDA
# Ini yang bikin delivery email nyampe ke Maildir user
dovecot   unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail:vmail
  argv=/usr/lib/dovecot/dovecot-lda -f ${sender} -d ${recipient}

๐Ÿ•Š️ STEP 7 — Konfigurasi Dovecot

7a. dovecot.conf — Listen di semua interface

# ROOT
nano /etc/dovecot/dovecot.conf
Konfigurasi
# Listen di semua interface (IPv4 & IPv6)
listen = *

# Enable protokol IMAP
protocols = imap

7b. conf.d/10-auth.conf — Auth via LDAP

# ROOT
nano /etc/dovecot/conf.d/10-auth.conf
Konfigurasi
# Larang plaintext auth (wajib pakai TLS)
disable_plaintext_auth = yes

# Gunakan LDAP sebagai backend auth, bukan system user
!include auth-ldap.conf.ext

# Mekanisme auth: username + password
auth_mechanisms = plain login

7c. conf.d/10-mail.conf — Lokasi Maildir

# ROOT
nano /etc/dovecot/conf.d/10-mail.conf
Konfigurasi
# Path maildir per user — %u diganti username dari LDAP
mail_location = maildir:/var/vmail/%u/Maildir

7d. conf.d/10-master.conf — Socket untuk Postfix SASL

# ROOT
nano /etc/dovecot/conf.d/10-master.conf
Konfigurasi
# Socket auth untuk Postfix SASL
service auth {
  unix_listener auth-userdb {
    mode = 0666
    user = vmail
    group = vmail
  }

  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = vmail
  }
}

# Socket stats writer
service stats {
  unix_listener stats-writer {
    mode = 0666
    user = dovecot
    group = vmail
  }
}

7e. conf.d/10-ssl.conf — TLS Config

# ROOT
nano /etc/dovecot/conf.d/10-ssl.conf
Konfigurasi
# Wajibkan TLS
ssl = required

# Path certificate
ssl_cert = 

๐Ÿ”— STEP 8 — Konfigurasi Dovecot LDAP (dovecot-ldap.conf.ext)

# ROOT
nano /etc/dovecot/dovecot-ldap.conf.ext
Konfigurasi
# Alamat server LDAP
hosts = int-srv.lksn2025.id

# Service account DN untuk bind ke LDAP
dn = cn=admin,dc=lksn2025,dc=id

# Password service account
dnpass = Skills39!

# Izinkan setiap user bind pakai password mereka sendiri
auth_bind = yes

# LDAP versi 3
ldap_version = 3

# Search hanya di OU mail (restrict sesuai soal)
base = ou=mail,dc=lksn2025,dc=id

# Search ke subtree penuh
scope = subtree

# Filter user: harus inetOrgPerson DAN uid cocok
user_filter = (&(objectClass=inetOrgPerson)(uid=%n))

# Mapping atribut LDAP ke variabel Dovecot
user_attrs = mail=user, =uid=999, =gid=996

# Filter untuk cek password (pakai uid tanpa domain)
pass_filter = (&(objectClass=inetOrgPerson)(uid=%n))

# Ambil password dari atribut userPassword di LDAP
pass_attrs = userPassword=password

7f. conf.d/auth-ldap.conf.ext — Daftarkan LDAP ke Dovecot

# ROOT
nano /etc/dovecot/conf.d/auth-ldap.conf.ext
Konfigurasi
# Definisikan passdb (cek password) dari LDAP
passdb {
  driver = ldap
  args = /etc/dovecot/dovecot-ldap.conf.ext
}

# Definisikan userdb (info user/maildir) — mapping static
userdb {
  driver = static
  args = uid=vmail gid=vmail home=/var/vmail/%u
}

▶️ STEP 9 — Restart & Enable Service

# ROOT
# Restart Postfix
systemctl restart postfix
systemctl enable postfix

# Restart Dovecot
systemctl restart dovecot
systemctl enable dovecot

# Cek status keduanya
systemctl status postfix dovecot

๐Ÿงช STEP 10 — Testing dengan mailutils (Auto Maildir Creation)

✅ Perhatian: Maildir akan otomatis terbuat saat email pertama dikirim ke user. Tidak perlu buat manual!

Test kirim ke user ani:

# ROOT
# Postfix bakal otomatis auto-create /var/vmail/ani/Maildir/ 
# saat email pertama delivery

# Method 1: Pakai mailutils sendmail ke user ani
echo "Ini test email pertama ke ani — Maildir auto-created" | \
  mail -s "Test Auto Maildir Creation" ani@lksn2025.id

# Method 2: Kirim dari ani ke diri sendiri (test SMTP auth juga)
echo "Test dari ani ke ani" | \
  mail -s "Loopback Test" -r ani@lksn2025.id ani@lksn2025.id

Verifikasi Maildir sudah terbuat:

# ROOT
# Check struktur Maildir ani otomatis terbuat
ls -la /var/vmail/ani/Maildir/

# Output diharapkan:
# drwxr-xr-x cur
# drwxr-xr-x new
# drwxr-xr-x tmp
# drwxr-xr-x dovecot-uidlist
# -rw-r--r-- .maildir-uidvalidity-xxx

Lihat email yang masuk:

# ROOT
# Cek folder 'new' di Maildir ani
ls -la /var/vmail/ani/Maildir/new/

# Baca isi email
cat /var/vmail/ani/Maildir/new/* | head -30

Test ke user lain (kyw1, kyw2, dst):

# ROOT
# Kirim test ke kyw1
echo "Test auto maildir kyw1" | mail -s "Test kyw1" kyw1@lksn2025.id

# Verifikasi Maildir kyw1 auto-created
ls -la /var/vmail/kyw1/Maildir/

๐Ÿ” Troubleshooting Cepat

# ROOT
# Cek log Postfix real-time
tail -f /var/log/mail.log | grep -E "error|reject|LDAP"

# Test query LDAP manual dari server
ldapsearch -x -H ldap://int-srv.lksn2025.id \
  -D "cn=admin,dc=lksn2025,dc=id" \
  -w Skills39! \
  -b "ou=mail,dc=lksn2025,dc=id" \
  "(uid=ani)"

# Test postfix lookup langsung
postmap -q "ani@lksn2025.id" ldap:/etc/postfix/ldap-users.cf

# Cek Dovecot auth
doveadm auth test ani <password>

# Verifikasi permission socket
ls -la /var/spool/postfix/private/auth

⭐ Best Practice

  • Jangan simpan password plaintext: Chmod /etc/postfix/ldap-users.cf dan /etc/dovecot/dovecot-ldap.conf.ext ke 640, owner root:postfix / root:dovecot.
  • OU restriction adalah security layer: base = ou=mail,dc=lksn2025,dc=id memastikan user di luar OU itu nggak bisa login meskipun ada di LDAP.
  • Test dengan doveadm: Selalu test pakai doveadm auth test sebelum coba dari client — lebih cepat isolasi masalahnya di Dovecot atau LDAP.
  • Disable plaintext: disable_plaintext_auth = yes di Dovecot wajib aktif — tanpa TLS, credential bisa kesniff di jaringan.
  • Maildir auto-create: Tidak perlu buat Maildir manual — Postfix/Dovecot bakal auto-create saat delivery email pertama.

๐Ÿ“– Glosarium

Software yang handle routing & transfer email — Postfix adalah MTA.
Software yang deliver email ke mailbox user — Dovecot-LDA adalah MDA.
Protokol akses email yang sinkronisasi di server (email tetap ada di server).
Framework autentikasi yang dipakai SMTP untuk verifikasi user sebelum relay.
Format penyimpanan email per-file dalam direktori cur/new/tmp.
Virtual Mailbox
User email yang tidak punya Unix system account, dikelola Postfix secara virtual.
LMTP
Protokol delivery lokal dari Postfix ke Dovecot (varian SMTP untuk lokal).
bind_dn
Distinguished Name service account yang dipakai untuk login ke LDAP server.
ou=mail
Organizational Unit di LDAP yang berisi user-user yang boleh pakai mail server.

© 2025 LKS Network & System Engineer Guide | Postfix + Dovecot + LDAP Integration Tutorial

Comments

Popular posts from this blog

Apa itu Link Aggregation?

Apa Itu Port Security di Switch Cisco