Skip to content

Commit

Permalink
Drop Server.AuthDisabled
Browse files Browse the repository at this point in the history
This is the default for servers not implementing AuthSession now.
  • Loading branch information
emersion committed Mar 28, 2024
1 parent 968926f commit 1f30586
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 2 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ func (c *Conn) handle(cmd string, arg string) {
c.writeResponse(221, EnhancedCode{2, 0, 0}, "Bye")
c.Close()
case "AUTH":
if c.server.AuthDisabled {
c.protocolError(500, EnhancedCode{5, 5, 2}, "Syntax error, AUTH command unrecognized")
} else {
c.handleAuth(arg)
}
c.handleAuth(arg)
case "STARTTLS":
c.handleStartTLS()
default:
Expand Down Expand Up @@ -207,7 +203,7 @@ func (c *Conn) Conn() net.Conn {

func (c *Conn) authAllowed() bool {
_, isTLS := c.TLSConnectionState()
return !c.server.AuthDisabled && (isTLS || c.server.AllowInsecureAuth)
return isTLS || c.server.AllowInsecureAuth
}

// protocolError writes errors responses and closes the connection once too many
Expand Down
4 changes: 0 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ type Server struct {
// Should be used only if backend supports it.
EnableDSN bool

// If set, the AUTH command will not be advertised and authentication
// attempts will be rejected. This setting overrides AllowInsecureAuth.
AuthDisabled bool

// The server backend.
Backend Backend

Expand Down
12 changes: 10 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type message struct {
}

type backend struct {
authDisabled bool

messages []*message
anonmsgs []*message

Expand Down Expand Up @@ -70,10 +72,16 @@ type session struct {
var _ smtp.AuthSession = (*session)(nil)

func (s *session) AuthMechanisms() []string {
if s.backend.authDisabled {
return nil
}
return []string{sasl.Plain}
}

func (s *session) Auth(mech string) (sasl.Server, error) {
if s.backend.authDisabled {
return nil, smtp.ErrAuthUnsupported
}
return sasl.NewPlainServer(func(identity, username, password string) error {
if identity != "" && identity != username {
return errors.New("Invalid identity")
Expand Down Expand Up @@ -217,7 +225,7 @@ type serverConfigureFunc func(*smtp.Server)

var (
authDisabled = func(s *smtp.Server) {
s.AuthDisabled = true
s.Backend.(*backend).authDisabled = true
}
)

Expand Down Expand Up @@ -698,7 +706,7 @@ func TestServer_authDisabled(t *testing.T) {

io.WriteString(c, "AUTH PLAIN\r\n")
scanner.Scan()
if scanner.Text() != "500 5.5.2 Syntax error, AUTH command unrecognized" {
if scanner.Text() != "502 5.7.0 Authentication not supported" {
t.Fatal("Invalid AUTH response with auth disabled:", scanner.Text())
}
}
Expand Down

0 comments on commit 1f30586

Please sign in to comment.