244 lines
8.7 KiB
Go
244 lines
8.7 KiB
Go
package mail
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Config tes: semua path di bawah direktori temp, tanpa menyentuh
|
|
// /etc/postfix, /etc/dovecot, atau /var/vmail produksi.
|
|
func testConfig(t *testing.T) Config {
|
|
t.Helper()
|
|
base := t.TempDir()
|
|
return Config{
|
|
PostfixDir: filepath.Join(base, "postfix"),
|
|
DovecotDir: filepath.Join(base, "dovecot"),
|
|
VmailDir: filepath.Join(base, "vmail"),
|
|
// Pakai uid/gid proses tes supaya chown selalu berhasil dan
|
|
// tidak mengubah user sistem (ensureVmailUser tetap best-effort).
|
|
VmailUID: os.Getuid(),
|
|
VmailGID: os.Getgid(),
|
|
}
|
|
}
|
|
|
|
// Bug 1 (v0.4.3): virtual_mailbox_domains HARUS berisi pasangan
|
|
// key+value ("domain domain"). Versi lama menulis domain telanjang,
|
|
// yang ditolak postmap ("expected format: key whitespace value")
|
|
// dan membuat Postfix menganggap domain itu remote (mail bounce).
|
|
func TestApplyDomainsFileKeyValue(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
warns, err := cfg.Apply(
|
|
[]MailboxEntry{{Address: "halo@hostora.test"}, {Address: "info@hostora.test"}},
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("Apply: %v", err)
|
|
}
|
|
raw, err := os.ReadFile(filepath.Join(cfg.PostfixDir, "virtual-domains-hostora"))
|
|
if err != nil {
|
|
t.Fatalf("baca domains file: %v", err)
|
|
}
|
|
lines := strings.Split(strings.TrimSpace(string(raw)), "\n")
|
|
if len(lines) != 2 { // header + 1 domain (kedua mailbox satu domain)
|
|
t.Fatalf("domains file = %d baris, mau 2:\n%s", len(lines), raw)
|
|
}
|
|
for _, want := range []string{"hostora.test hostora.test"} {
|
|
found := false
|
|
for _, l := range lines {
|
|
if l == want {
|
|
found = true
|
|
}
|
|
// Tidak boleh ada baris domain telanjang tanpa nilai.
|
|
if l == "hostora.test" {
|
|
t.Errorf("baris domain telanjang tanpa nilai masih ada: %q", l)
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("baris %q tidak ditemukan di:\n%s", want, raw)
|
|
}
|
|
}
|
|
// Mailboxes map harus menunjuk .../Maildir/ (bukan home langsung).
|
|
mb, err := os.ReadFile(filepath.Join(cfg.PostfixDir, "virtual-mailboxes-hostora"))
|
|
if err != nil {
|
|
t.Fatalf("baca mailboxes file: %v", err)
|
|
}
|
|
if !strings.Contains(string(mb), "halo@hostora.test hostora.test/halo/Maildir/") {
|
|
t.Errorf("mailboxes file tidak menunjuk Maildir/:\n%s", mb)
|
|
}
|
|
// Di VM build postmap/main.cf tidak ada: warnings boleh ada, tapi
|
|
// Apply tidak boleh gagal karenanya.
|
|
t.Logf("warnings (diharapkan di VM tanpa postfix): %v", warns)
|
|
}
|
|
|
|
// Bug 2 (v0.4.3): Maildir harus di home/Maildir/{cur,new,tmp}, dan
|
|
// parent /var/vmail + /var/vmail/<domain> harus 0755 agar uid virtual
|
|
// bisa traverse. Versi lama membuat cur/new/tmp langsung di home dan
|
|
// parent 0700 root.
|
|
func TestCreateMailboxStructure(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
maildir, err := cfg.CreateMailbox("hostora.test", "halo")
|
|
if err != nil {
|
|
t.Fatalf("CreateMailbox: %v", err)
|
|
}
|
|
home := filepath.Join(cfg.VmailDir, "hostora.test", "halo")
|
|
if maildir != filepath.Join(home, "Maildir") {
|
|
t.Fatalf("CreateMailbox mengembalikan %q, mau %q", maildir, filepath.Join(home, "Maildir"))
|
|
}
|
|
for _, sub := range []string{"", "cur", "new", "tmp"} {
|
|
p := filepath.Join(maildir, sub)
|
|
st, err := os.Stat(p)
|
|
if err != nil {
|
|
t.Fatalf("stat %s: %v", p, err)
|
|
}
|
|
if !st.IsDir() {
|
|
t.Fatalf("%s bukan direktori", p)
|
|
}
|
|
if st.Mode().Perm() != 0o700 {
|
|
t.Errorf("izin %s = %o, mau 0700", p, st.Mode().Perm())
|
|
}
|
|
}
|
|
// Tidak boleh ada cur/new/tmp langsung di bawah home.
|
|
for _, sub := range []string{"cur", "new", "tmp"} {
|
|
if _, err := os.Stat(filepath.Join(home, sub)); !os.IsNotExist(err) {
|
|
t.Errorf("home masih berisi %s langsung (tanpa level Maildir)", sub)
|
|
}
|
|
}
|
|
// Parent harus bisa dilalui uid virtual.
|
|
for _, p := range []string{cfg.VmailDir, filepath.Join(cfg.VmailDir, "hostora.test")} {
|
|
st, err := os.Stat(p)
|
|
if err != nil {
|
|
t.Fatalf("stat %s: %v", p, err)
|
|
}
|
|
if st.Mode().Perm() != 0o755 {
|
|
t.Errorf("izin parent %s = %o, mau 0755", p, st.Mode().Perm())
|
|
}
|
|
}
|
|
// Home 0700.
|
|
if st, err := os.Stat(home); err != nil || st.Mode().Perm() != 0o700 {
|
|
t.Errorf("izin home = %v, mau 0700", st.Mode().Perm())
|
|
}
|
|
}
|
|
|
|
// Bug 2 (lanjutan): CreateMailbox harus MEMPERBAIKI izin yang sudah
|
|
// salah (idempotent), bukan hanya saat membuat baru — skenario VPS
|
|
// v0.4.2: /var/vmail 0700 + home berisi cur/new/tmp tanpa Maildir.
|
|
func TestCreateMailboxRepairsBadPermissions(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
home := filepath.Join(cfg.VmailDir, "hostora.test", "halo")
|
|
for _, p := range []string{cfg.VmailDir, filepath.Join(cfg.VmailDir, "hostora.test"), home, filepath.Join(home, "cur")} {
|
|
if err := os.MkdirAll(p, 0o700); err != nil {
|
|
t.Fatalf("setup %s: %v", p, err)
|
|
}
|
|
if err := os.Chmod(p, 0o700); err != nil {
|
|
t.Fatalf("chmod %s: %v", p, err)
|
|
}
|
|
}
|
|
if _, err := cfg.CreateMailbox("hostora.test", "halo"); err != nil {
|
|
t.Fatalf("CreateMailbox: %v", err)
|
|
}
|
|
for _, p := range []string{cfg.VmailDir, filepath.Join(cfg.VmailDir, "hostora.test")} {
|
|
st, _ := os.Stat(p)
|
|
if st.Mode().Perm() != 0o755 {
|
|
t.Errorf("izin parent %s tidak diperbaiki: %o, mau 0755", p, st.Mode().Perm())
|
|
}
|
|
}
|
|
if _, err := os.Stat(filepath.Join(home, "Maildir", "tmp")); err != nil {
|
|
t.Errorf("Maildir/tmp tidak dibuat saat memperbaiki: %v", err)
|
|
}
|
|
}
|
|
|
|
// Bug 2 (lanjutan): userdb_mail di hostora-users harus menunjuk
|
|
// .../Maildir, bukan home itu sendiri.
|
|
func TestPasswdLineMaildir(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
line, err := cfg.passwdLine("halo@hostora.test", "$2b$12$abcdefghijklmnopqrstuv", "BLF-CRYPT")
|
|
if err != nil {
|
|
t.Fatalf("passwdLine: %v", err)
|
|
}
|
|
home := filepath.Join(cfg.VmailDir, "hostora.test", "halo")
|
|
if !strings.Contains(line, "userdb_mail=maildir:"+filepath.Join(home, "Maildir")) {
|
|
t.Errorf("userdb_mail tidak menunjuk Maildir: %q", line)
|
|
}
|
|
if strings.Contains(line, "userdb_mail=maildir:"+home+" ") || strings.HasSuffix(line, "userdb_mail=maildir:"+home) {
|
|
t.Errorf("userdb_mail masih menunjuk home tanpa /Maildir: %q", line)
|
|
}
|
|
}
|
|
|
|
// Bug 3 (v0.4.3): EnsureDovecotConfig menulis 99-hostora.conf dengan
|
|
// passdb/userdb passwd-file yang menunjuk hostora-users, dan tidak
|
|
// menimpa file yang sudah ada.
|
|
func TestEnsureDovecotConfig(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
written, err := cfg.EnsureDovecotConfig()
|
|
if err != nil {
|
|
t.Fatalf("EnsureDovecotConfig: %v", err)
|
|
}
|
|
if !written {
|
|
t.Fatalf("EnsureDovecotConfig pertama harus written=true")
|
|
}
|
|
path := filepath.Join(cfg.DovecotDir, "conf.d", "99-hostora.conf")
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("baca %s: %v", path, err)
|
|
}
|
|
content := string(raw)
|
|
for _, want := range []string{"passwd-file", "hostora-users", "username_format=%u", "passdb", "userdb"} {
|
|
if !strings.Contains(content, want) {
|
|
t.Errorf("99-hostora.conf tidak mengandung %q:\n%s", want, content)
|
|
}
|
|
}
|
|
// Pemanggilan kedua: tidak ditimpa.
|
|
if err := os.WriteFile(path, []byte("# diubah operator\n"), 0o644); err != nil {
|
|
t.Fatalf("tulis sentinel: %v", err)
|
|
}
|
|
written, err = cfg.EnsureDovecotConfig()
|
|
if err != nil {
|
|
t.Fatalf("EnsureDovecotConfig kedua: %v", err)
|
|
}
|
|
if written {
|
|
t.Errorf("EnsureDovecotConfig kedua harus written=false (file sudah ada)")
|
|
}
|
|
after, _ := os.ReadFile(path)
|
|
if string(after) != "# diubah operator\n" {
|
|
t.Errorf("file yang sudah ada ditimpa: %q", after)
|
|
}
|
|
if !cfg.DovecotConfigExists() {
|
|
t.Errorf("DovecotConfigExists harus true setelah file dibuat")
|
|
}
|
|
}
|
|
|
|
// Bug 3 (lanjutan, v0.4.3): hostora-users ditulis 0600 root:root,
|
|
// padahal proses auth Dovecot berjalan sebagai user `dovecot` —
|
|
// login IMAP gagal "Temporary authentication failure" dengan log
|
|
// "open(...) failed: Permission denied (euid=112(dovecot) ...)".
|
|
// fixPasswdFilePerms harus mengubahnya jadi root:dovecot 0640 bila
|
|
// grup dovecot ada. Di VM build grup dovecot TIDAK ada: fungsi harus
|
|
// mengembalikan error (bukan crash), dan pemanggil (upsert /
|
|
// EnsureDovecotConfig) harus tetap berhasil — perms diperbaiki lagi
|
|
// saat Dovecot terpasang.
|
|
func TestFixPasswdFilePermsNoDovecotGroup(t *testing.T) {
|
|
cfg := testConfig(t)
|
|
// doveadm tidak ada di VM build: pakai hash bcrypt panel sebagai
|
|
// fallback (HashPassword mengembalikannya apa adanya).
|
|
if _, err := cfg.SetMailboxPassword("halo@hostora.test", "rahasia123", "$2a$12$abcdefghijklmnopqrstuvwxzy0123456789ABCDEFGHIJK"); err != nil {
|
|
t.Fatalf("SetMailboxPassword: %v", err)
|
|
}
|
|
path := filepath.Join(cfg.DovecotDir, "hostora-users")
|
|
st, err := os.Stat(path)
|
|
if err != nil {
|
|
t.Fatalf("stat %s: %v", path, err)
|
|
}
|
|
if st.Mode().Perm() != 0o600 {
|
|
t.Errorf("izin hostora-users tanpa grup dovecot = %o, mau tetap 0600", st.Mode().Perm())
|
|
}
|
|
if err := cfg.fixPasswdFilePerms(); err == nil {
|
|
t.Errorf("fixPasswdFilePerms harus error bila grup dovecot tidak ada")
|
|
}
|
|
// EnsureDovecotConfig tetap berhasil walau perms tidak bisa diperbaiki.
|
|
if _, err := cfg.EnsureDovecotConfig(); err != nil {
|
|
t.Fatalf("EnsureDovecotConfig: %v", err)
|
|
}
|
|
}
|