From 71a9b231e3cf38a4fa7d7c881e7296634f6128d9 Mon Sep 17 00:00:00 2001 From: LowEel Date: Fri, 30 Jul 2021 23:20:49 +0200 Subject: [PATCH] Protected map, better check for RCPT --- backend.go | 9 ++++++++- go.mod | 5 +---- go.sum | 4 ---- handler.go | 23 +++++++++++++++++++++++ session.go | 10 +++------- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/backend.go b/backend.go index 81bb271..a04e742 100644 --- a/backend.go +++ b/backend.go @@ -5,6 +5,7 @@ import ( "log" "os" "strings" + "sync" ) // The Backend implements SMTP server methods. @@ -17,6 +18,8 @@ type Backend struct { var SmtpBackend *Backend +var mutex = &sync.Mutex{} + // Load Valid Recipients func (bkd *Backend) LoadValidRecipients() error { bkd.MaxRecipients = 0 @@ -41,9 +44,13 @@ func (bkd *Backend) LoadValidRecipients() error { } -// Checks if Mail is a valid recipient +// Checks if Mail is a valid local recipient func (bkd *Backend) CheckValidRcpt(to string) bool { + defer mutex.Unlock() + + mutex.Lock() + _, isValid := bkd.ValidRecipientsMap[to] return isValid diff --git a/go.mod b/go.mod index e169c16..9f52936 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,4 @@ module zangtumb go 1.13 -require ( - github.com/amalfra/maildir v0.0.7 - github.com/emersion/go-smtp v0.12.1 // indirect -) +require github.com/amalfra/maildir v0.0.7 diff --git a/go.sum b/go.sum index 2020e77..d61835a 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,2 @@ github.com/amalfra/maildir v0.0.7 h1:quK6vVGmjqP1zbeo9g7jtx37htGoYrKKlSpcDcplD04= github.com/amalfra/maildir v0.0.7/go.mod h1:+ynYowCpUHTWebUhg3Sb6Jp2dq+SUSWLTSLIf7Vy+ak= -github.com/emersion/go-sasl v0.0.0-20190817083125-240c8404624e h1:ba7YsgX5OV8FjGi5ZWml8Jng6oBrJAb3ahqWMJ5Ce8Q= -github.com/emersion/go-sasl v0.0.0-20190817083125-240c8404624e/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k= -github.com/emersion/go-smtp v0.12.1 h1:1R8BDqrR2HhlGwgFYcOi+BVTvK1bMjAB65QcVpJ5sNA= -github.com/emersion/go-smtp v0.12.1/go.mod h1:SD9V/xa4ndMw77lR3Mf7htkp8RBNYuPh9UeuBs9tpUQ= diff --git a/handler.go b/handler.go index 44a8835..d0ba17b 100644 --- a/handler.go +++ b/handler.go @@ -4,8 +4,14 @@ import ( "bytes" "log" "net" + "time" ) +func init() { + ZangSmtpServer.HandlerRcpt = handlerRcpt +} + + func mailHandler(origin net.Addr, from string, to []string, data []byte) { SmtpSession := new(Session) @@ -28,3 +34,20 @@ func mailHandler(origin net.Addr, from string, to []string, data []byte) { } } + + +func handlerRcpt(remoteAddr net.Addr, from string, to string) bool { + + log.Println("CHECK #2 Rcpt to:", to) + if !SmtpBackend.CheckValidRcpt(to) { + log.Printf("%s [ %s -> $s ] %s BAD!!!!", "YU NO MAIL!!! WE NU RELAY!!! ", from, to, remoteAddr.String() ) + time.Sleep(10 * time.Second) + return false + }else{ + log.Printf("%s [ %s -> $s ] %s GOOD!!!!", "WE DO!!!", from, to, remoteAddr.String() ) + } + + return true + + +} \ No newline at end of file diff --git a/session.go b/session.go index c5215c5..7087fc1 100644 --- a/session.go +++ b/session.go @@ -1,13 +1,13 @@ package main import ( - "errors" + "fmt" "io" "io/ioutil" "log" "strings" - "time" + "github.com/amalfra/maildir" ) @@ -22,11 +22,7 @@ type Session struct { } func (s *Session) Rcpt(to string) error { - log.Println("Rcpt to:", to) - if !SmtpBackend.CheckValidRcpt(to) { - time.Sleep(10 * time.Second) - return errors.New("YU NO MAIL!!! WE NU RELAY") - } + log.Println("CHECK #1 Rcpt to:", to) s.RcptTo = to if strings.Contains(to, "@") { rcptArray := strings.Split(to, "@")