From 4d284312a368c7ec1e82f2fa418b33d3a7e21a3b Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 6 Jun 2020 14:46:43 -0400 Subject: [PATCH] Format the Reporting-MTA in DSN messages per the spec. --- smtp/conn.go | 12 +----------- smtp/relay.go | 6 +----- smtp/relay_test.go | 7 +------ smtp/server.go | 17 +++++++++++++++++ 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/smtp/conn.go b/smtp/conn.go index 5511799..9d807d4 100644 --- a/smtp/conn.go +++ b/smtp/conn.go @@ -510,17 +510,7 @@ func (conn *connection) handleSendAs(env *Envelope) { } func (conn *connection) getReceivedInfo(envelope Envelope) []byte { - rhost, _, err := net.SplitHostPort(conn.remoteAddr.String()) - if err != nil { - rhost = conn.remoteAddr.String() - } - - rhosts, err := net.LookupAddr(rhost) - if err == nil { - rhost = fmt.Sprintf("%s [%s]", rhosts[0], rhost) - } - - base := fmt.Sprintf("Received: from %s (%s)\r\n ", conn.ehlo, rhost) + base := fmt.Sprintf("Received: from %s (%s)\r\n ", conn.ehlo, lookupRemoteHost(conn.remoteAddr)) with := "SMTP" if conn.esmtp { diff --git a/smtp/relay.go b/smtp/relay.go index 81a91e3..ae90fe5 100644 --- a/smtp/relay.go +++ b/smtp/relay.go @@ -136,11 +136,7 @@ func deliverRelayFailure(server Server, env Envelope, log *zap.Logger, to, error fmt.Fprintf(sw, "Original-Envelope-ID: %s\n", env.ID) fmt.Fprintf(sw, "Reporting-UA: %s\n", env.EHLO) if env.RemoteAddr != nil { - rhosts, err := net.LookupAddr(env.RemoteAddr.String()) - if err == nil { - fmt.Fprintf(sw, "Reporting-MTA: %s\n", rhosts[0]) - } - fmt.Fprintf(sw, "X-Remote-Address: %s\n", env.RemoteAddr) + fmt.Fprintf(sw, "Reporting-MTA: dns; %s\n", lookupRemoteHost(env.RemoteAddr)) } fmt.Fprintf(sw, "Date: %s\n", env.Received.Format(time.RFC1123Z)) diff --git a/smtp/relay_test.go b/smtp/relay_test.go index e486e82..1934173 100644 --- a/smtp/relay_test.go +++ b/smtp/relay_test.go @@ -191,12 +191,7 @@ func TestDeliveryFailureMessage(t *testing.T) { t.Errorf("Missing %q in %q", expected, contentStr) } - expected = "Reporting-MTA: localhost\n" - if !strings.Contains(contentStr, expected) { - t.Errorf("Missing %q in %q", expected, contentStr) - } - - expected = "X-Remote-Address: 127.0.0.1\n" + expected = "Reporting-MTA: dns; localhost [127.0.0.1]\n" if !strings.Contains(contentStr, expected) { t.Errorf("Missing %q in %q", expected, contentStr) } diff --git a/smtp/server.go b/smtp/server.go index 3b0fdfa..b991a22 100644 --- a/smtp/server.go +++ b/smtp/server.go @@ -71,6 +71,23 @@ func generateEnvelopeId(prefix string, t time.Time) string { return fmt.Sprintf("%s.%d.%x", prefix, t.UnixNano(), idBytes) } +// lookupRemoteHost attempts to reverse look-up the provided IP address. On +// success, it returns the hostname and the IP as formatted for a receive +// trace. If the lookup fails, it just returns the original IP. +func lookupRemoteHost(addr net.Addr) string { + rhost, _, err := net.SplitHostPort(addr.String()) + if err != nil { + rhost = addr.String() + } + + rhosts, err := net.LookupAddr(rhost) + if err == nil { + rhost = fmt.Sprintf("%s [%s]", rhosts[0], rhost) + } + + return rhost +} + type Server interface { Name() string TLSConfig() *tls.Config -- 2.43.5