Merge pull request #95 from wneessen/94-login-auth-next-handler-should-unconditionally-succeed-if-more-is-false

Fix SMTP AUTH LOGIN method for servers with uncommon success messages
This commit is contained in:
Winni Neessen 2023-01-07 11:57:46 +01:00 committed by GitHub
commit fb1d3197c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 13 deletions

View file

@ -22,10 +22,10 @@ func TestAuth(t *testing.T) {
authTests := []authTest{ authTests := []authTest{
{ {
LoginAuth("user", "pass", "testserver"), LoginAuth("user", "pass", "testserver"),
[]string{"Username:", "Password:", "2.7.0 Authentication successful", "Invalid:"}, []string{"Username:", "Password:", "Invalid:"},
"LOGIN", "LOGIN",
[]string{"", "user", "pass", "", ""}, []string{"", "user", "pass", ""},
[]bool{false, false, false, true}, []bool{false, false, true},
}, },
} }

View file

@ -9,7 +9,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/smtp" "net/smtp"
"strings"
) )
type loginAuth struct { type loginAuth struct {
@ -23,10 +22,6 @@ const (
// ServerRespPassword represents the "Password:" response by the SMTP server // ServerRespPassword represents the "Password:" response by the SMTP server
ServerRespPassword = "Password:" ServerRespPassword = "Password:"
// ServerRespAuthSuccess represents the "Authentication successful:" response that is
// by sent by some SMTP servers
ServerRespAuthSuccess = "Authentication successful"
) )
// LoginAuth returns an Auth that implements the LOGIN authentication // LoginAuth returns an Auth that implements the LOGIN authentication
@ -70,10 +65,9 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
return []byte(a.username), nil return []byte(a.username), nil
case ServerRespPassword: case ServerRespPassword:
return []byte(a.password), nil return []byte(a.password), nil
} default:
}
if strings.HasSuffix(string(fromServer), ServerRespAuthSuccess) {
return nil, nil
}
return nil, fmt.Errorf("unexpected server response: %s", string(fromServer)) return nil, fmt.Errorf("unexpected server response: %s", string(fromServer))
} }
}
return nil, nil
}

View file

@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"net/smtp" "net/smtp"
"os" "os"
"strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -1087,10 +1088,22 @@ func getTestConnection(auth bool) (*Client, error) {
if th == "" { if th == "" {
return nil, fmt.Errorf("no TEST_HOST set") return nil, fmt.Errorf("no TEST_HOST set")
} }
c, err := NewClient(th) tp := 25
if tps := os.Getenv("TEST_PORT"); tps != "" {
tpi, err := strconv.Atoi(tps)
if err == nil {
tp = tpi
}
}
sv := false
if sve := os.Getenv("TEST_TLS_SKIP_VERIFY"); sve != "" {
sv = true
}
c, err := NewClient(th, WithPort(tp))
if err != nil { if err != nil {
return c, err return c, err
} }
c.tlsconfig.InsecureSkipVerify = sv
if auth { if auth {
st := os.Getenv("TEST_SMTPAUTH_TYPE") st := os.Getenv("TEST_SMTPAUTH_TYPE")
if st != "" { if st != "" {