From 55a5d02fe0379879c82d48f4e35bc483355f95db Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 15 Oct 2024 19:52:31 +0200 Subject: [PATCH] Add support for configurable SMTP auth data logging Added the `logAuthData` flag to enable conditional logging of SMTP authentication data. Introduced the `SetLogAuthData` method for clients to toggle this flag. Adjusted existing logging logic to respect this new configuration. --- smtp/smtp.go | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/smtp/smtp.go b/smtp/smtp.go index 316ddb3..77b5fb0 100644 --- a/smtp/smtp.go +++ b/smtp/smtp.go @@ -54,6 +54,9 @@ type Client struct { // auth supported auth mechanisms auth []string + // authIsActive indicates that the Client is currently during SMTP authentication + authIsActive bool + // keep a reference to the connection so it can be used to create a TLS connection later conn net.Conn @@ -78,12 +81,14 @@ type Client struct { // isConnected indicates if the Client has an active connection isConnected bool + // logAuthData indicates if the Client should include SMTP authentication data in the logs + logAuthData bool + // localName is the name to use in HELO/EHLO localName string // the name to use in HELO/EHLO // logger will be used for debug logging - logger log.Logger - authIsActive bool + logger log.Logger // mutex is used to synchronize access to shared resources, ensuring that only one goroutine can access // the resource at a time. @@ -177,10 +182,13 @@ func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, s var logMsg []interface{} logMsg = args + logFmt := format if c.authIsActive { - logMsg = []interface{}{""} + logMsg = []interface{}{""} + logFmt = "%s" } - c.debugLog(log.DirClientToServer, format, logMsg...) + c.debugLog(log.DirClientToServer, logFmt, logMsg...) + id, err := c.Text.Cmd(format, args...) if err != nil { c.mutex.Unlock() @@ -190,10 +198,11 @@ func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, s code, msg, err := c.Text.ReadResponse(expectCode) logMsg = []interface{}{code, msg} - if c.authIsActive && code >= 300 { - logMsg = []interface{}{code, ""} + if c.authIsActive && code >= 300 && code <= 400 { + logMsg = []interface{}{code, ""} } c.debugLog(log.DirServerToClient, "%d %s", logMsg...) + c.Text.EndResponse(id) c.mutex.Unlock() return code, msg, err @@ -269,11 +278,15 @@ func (c *Client) Auth(a Auth) error { } c.mutex.Lock() - c.authIsActive = true + if !c.logAuthData { + c.authIsActive = true + } c.mutex.Unlock() defer func() { c.mutex.Lock() - c.authIsActive = false + if !c.logAuthData { + c.authIsActive = false + } c.mutex.Unlock() }() @@ -577,6 +590,13 @@ func (c *Client) SetLogger(l log.Logger) { c.logger = l } +// SetLogAuthData enables logging of authentication data in the Client. +func (c *Client) SetLogAuthData() { + c.mutex.Lock() + c.logAuthData = true + c.mutex.Unlock() +} + // SetDSNMailReturnOption sets the DSN mail return option for the Mail method func (c *Client) SetDSNMailReturnOption(d string) { c.dsnmrtype = d