Implement address blocking on the server.
authorRobert Sesek <rsesek@bluestatic.org>
Fri, 11 Nov 2022 19:17:55 +0000 (14:17 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Fri, 11 Nov 2022 19:17:55 +0000 (14:17 -0500)
config.go
smtp.go
smtp_test.go

index c5f24ca2a6313a8a9543d88cf936a3dc7611671e..961d2cb8f36ca36d15d32c2c8eec61428437c7f5 100644 (file)
--- 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 4173d9b81a44a31cf64cce58c2b273e5f70f5007..c22728e8f29b0de18f196b306594f4d6c0761247 100644 (file)
--- 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 ""
 }
 
index ba8c254e8cad160aeac540cb9630c6b734aa354a..b589c6979edda7e828b1d4479a1dcd0f178783da 100644 (file)
@@ -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) {