Fix Quit handling when initial HELO fails

Ensure QUIT command can be sent even if initial HELO fails. Added a check to skip retrying HELO if it already failed, allowing for proper closing of the connection. This prevents potential hangs or errors during connection termination.
This commit is contained in:
Winni Neessen 2024-10-23 21:54:06 +02:00
parent 572751ac10
commit 74fa3f6f62
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -554,9 +554,20 @@ func (c *Client) Noop() error {
// Quit sends the QUIT command and closes the connection to the server.
func (c *Client) Quit() error {
// If we did already tried to send a EHLO/HELO but it failed, we still need to be able to send
// a QUIT to close the connection.
// c.hello() will return the global helloErr of the Client, which will always be set if the HELO
// failed before. Therefore if we already sent a HELO and the error is not nil, we skip another
// EHLO/HELO try
c.mutex.RLock()
didHello := c.didHello
helloErr := c.helloError
c.mutex.RUnlock()
if !didHello || helloErr == nil {
if err := c.hello(); err != nil {
return err
}
}
_, _, err := c.cmd(221, "QUIT")
if err != nil {
return err