Files

92 lines
3.4 KiB
Go

package mail
import (
"fmt"
"os"
"path/filepath"
)
// Integrasi Dovecot (v0.4.3).
//
// File /etc/dovecot/hostora-users sudah lama ditulis panel, TAPI tidak
// ada kode yang menyuruh Dovecot membacanya: `doveconf -n` di VPS
// (v0.4.2) menunjukkan passdb driver=pam dan userdb driver=passwd
// (hanya user sistem), dan baris `#!include auth-passwdfile.conf.ext`
// di 10-auth.conf masih terkomentari. Akibatnya mailbox panel tidak
// bisa login IMAP sama sekali.
//
// Perbaikannya: Hostora menulis /etc/dovecot/conf.d/99-hostora.conf
// (HANYA bila belum ada — tidak pernah ditimpa, supaya penyesuaian
// operator tidak hilang) yang mengarahkan passdb dan userdb ke
// passwd-file hostora-users. Login IMAP memakai alamat email lengkap
// sebagai username (username_format=%u).
// DovecotHostoraConf adalah nama file konfigurasi Dovecot milik Hostora
// di bawah <DovecotDir>/conf.d/.
const DovecotHostoraConf = "99-hostora.conf"
// dovecotHostoraConfContent adalah isi 99-hostora.conf. Path
// passwd-file diambil dari Config (produksi:
// /etc/dovecot/hostora-users) supaya tetap benar bila lokasi file
// passwd diganti.
func (c Config) dovecotHostoraConfContent() string {
return `# Hostora — autentikasi mailbox panel (jangan edit manual).
# Login IMAP memakai alamat email lengkap sebagai username.
passdb {
driver = passwd-file
args = username_format=%u ` + c.passwdFile() + `
}
userdb {
driver = passwd-file
args = username_format=%u ` + c.passwdFile() + `
}
`
}
// dovecotConfPath mengembalikan path file konfigurasi Dovecot Hostora
// (<DovecotDir>/conf.d/99-hostora.conf).
func (c Config) dovecotConfPath() string {
return filepath.Join(c.dovecotDir(), "conf.d", DovecotHostoraConf)
}
// DovecotConfigExists melaporkan apakah file konfigurasi Dovecot
// Hostora sudah ada.
func DovecotConfigExists() bool { return DefaultConfig().DovecotConfigExists() }
// DovecotConfigExists adalah versi DovecotConfigExists dengan Config
// yang bisa diganti (untuk tes).
func (c Config) DovecotConfigExists() bool {
_, err := os.Stat(c.dovecotConfPath())
return err == nil
}
// EnsureDovecotConfig menulis /etc/dovecot/conf.d/99-hostora.conf bila
// belum ada, supaya Dovecot membaca hostora-users untuk login IMAP.
// Mengembalikan true bila file baru ditulis, false bila file sudah ada
// (dilewati, tidak ditimpa). Bila Dovecot tidak terpasang, pemanggil
// (mail.Apply / server) yang memutuskan untuk melewati dengan warning
// — fungsi ini sendiri hanya menulis file, tanpa memeriksa biner,
// supaya tetap bisa dites tanpa Dovecot terpasang.
func EnsureDovecotConfig() (bool, error) {
return DefaultConfig().EnsureDovecotConfig()
}
// EnsureDovecotConfig adalah versi EnsureDovecotConfig dengan Config
// yang bisa diganti (untuk tes).
func (c Config) EnsureDovecotConfig() (bool, error) {
// Perbaiki izin file passwd bila sudah ada (file era v0.4.2 ditulis
// 0600 root:root sehingga Dovecot tidak bisa membacanya dan login
// IMAP gagal). Best-effort: Dovecot mungkin belum terpasang.
_ = c.fixPasswdFilePerms()
path := c.dovecotConfPath()
if _, err := os.Stat(path); err == nil {
return false, nil // sudah ada — tidak ditimpa
} else if !os.IsNotExist(err) {
return false, fmt.Errorf("cek %s: %w", path, err)
}
if err := writeFileAtomic(path, []byte(c.dovecotHostoraConfContent()), 0o644); err != nil {
return false, fmt.Errorf("tulis %s: %w", path, err)
}
return true, nil
}