From ddffe0b7bfb1f124bfaf35386535194195fefa8e Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 11 Nov 2022 14:17:55 -0500 Subject: [PATCH] Implement address blocking on the server. --- config.go | 5 +++-- smtp.go | 19 ++++++++++++++++--- smtp_test.go | 6 ++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index c5f24ca..961d2cb 100644 --- a/config.go +++ b/config.go @@ -35,8 +35,9 @@ type Server struct { // Location to store the mail messages. MaildropPath string - // Blacklisted addresses that should not accept mail. - BlacklistedAddresses []string + // Addresses that should not accept mail. This should include the @domain + // component. + BlockedAddresses []string } func (c Config) GetTLSConfig() (*tls.Config, error) { diff --git a/smtp.go b/smtp.go index 4173d9b..c22728e 100644 --- a/smtp.go +++ b/smtp.go @@ -102,9 +102,15 @@ func (server *smtpServer) TLSConfig() *tls.Config { } func (server *smtpServer) VerifyAddress(addr mail.Address) smtp.ReplyLine { - if server.maildropForAddress(addr) == "" { + s := server.configForAddress(addr) + if s == nil { return smtp.ReplyBadMailbox } + for _, blocked := range s.BlockedAddresses { + if blocked == addr.Address { + return smtp.ReplyMailboxUnallowed + } + } return smtp.ReplyOK } @@ -150,14 +156,21 @@ func (server *smtpServer) DeliverMessage(en smtp.Envelope) *smtp.ReplyLine { return nil } -func (server *smtpServer) maildropForAddress(addr mail.Address) string { +func (server *smtpServer) configForAddress(addr mail.Address) *Server { domain := smtp.DomainForAddress(addr) for _, s := range server.config.Servers { if domain == s.Domain { - return s.MaildropPath + return &s } } + return nil +} +func (server *smtpServer) maildropForAddress(addr mail.Address) string { + s := server.configForAddress(addr) + if s != nil { + return s.MaildropPath + } return "" } diff --git a/smtp_test.go b/smtp_test.go index ba8c254..b589c69 100644 --- a/smtp_test.go +++ b/smtp_test.go @@ -36,6 +36,9 @@ func TestVerifyAddress(t *testing.T) { { Domain: "example.com", MaildropPath: dir, + BlockedAddresses: []string{ + "blocked@example.com", + }, }, }, }, @@ -57,6 +60,9 @@ func TestVerifyAddress(t *testing.T) { if s.VerifyAddress(mail.Address{Address: "unknown"}) == smtp.ReplyOK { t.Errorf("Invalid mailbox reports to be valid") } + if s.VerifyAddress(mail.Address{Address: "blocked@example.com"}) == smtp.ReplyOK { + t.Errorf("Blocked mailbox reports to be valid") + } } func TestMessageDelivery(t *testing.T) { -- 2.22.5