Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error handling #116

Merged
merged 6 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"time"
)

// Number of errors we'll tolerate per connection before closing. Defaults to 3.
const errThreshold = 3

type ConnectionState struct {
Hostname string
LocalAddr net.Addr
Expand All @@ -25,11 +28,14 @@ type ConnectionState struct {
}

type Conn struct {
conn net.Conn
text *textproto.Conn
server *Server
helo string
nbrErrors int
conn net.Conn
text *textproto.Conn
server *Server
helo string

// Number of errors witnessed on this connection
errCount int

session Session
locker sync.Mutex
binarymime bool
Expand Down Expand Up @@ -82,16 +88,6 @@ func (c *Conn) init() {
c.text = textproto.NewConn(rwc)
}

func (c *Conn) unrecognizedCommand(cmd string) {
c.WriteResponse(500, EnhancedCode{5, 5, 2}, fmt.Sprintf("Syntax error, %v command unrecognized", cmd))

c.nbrErrors++
if c.nbrErrors > 3 {
c.WriteResponse(500, EnhancedCode{5, 5, 2}, "Too many unrecognized commands")
c.Close()
}
}

// Commands are dispatched to the appropriate handler functions.
func (c *Conn) handle(cmd string, arg string) {
// If panic happens during command handling - send 421 response
Expand All @@ -107,7 +103,7 @@ func (c *Conn) handle(cmd string, arg string) {
}()

if cmd == "" {
c.WriteResponse(500, EnhancedCode{5, 5, 2}, "Speak up")
c.protocolError(500, EnhancedCode{5, 5, 2}, "Speak up")
return
}

Expand Down Expand Up @@ -148,14 +144,15 @@ func (c *Conn) handle(cmd string, arg string) {
c.Close()
case "AUTH":
if c.server.AuthDisabled {
c.unrecognizedCommand(cmd)
c.protocolError(500, EnhancedCode{5, 5, 2}, "Syntax error, AUTH command unrecognized")
} else {
c.handleAuth(arg)
}
case "STARTTLS":
c.handleStartTLS()
default:
c.unrecognizedCommand(cmd)
msg := fmt.Sprintf("Syntax errors, %v command unrecognized", cmd)
c.protocolError(500, EnhancedCode{5, 5, 2}, msg)
}
}

Expand Down Expand Up @@ -222,6 +219,18 @@ func (c *Conn) authAllowed() bool {
return !c.server.AuthDisabled && (isTLS || c.server.AllowInsecureAuth)
}

// protocolError writes errors responses and closes the connection once too many
// have occurred.
func (c *Conn) protocolError(code int, ec EnhancedCode, msg string) {
c.WriteResponse(code, ec, msg)

c.errCount++
if c.errCount > errThreshold {
c.WriteResponse(500, EnhancedCode{5, 5, 1}, "Too many errors. Quiting now")
c.Close()
}
}

// GREET state -> waiting for HELO
func (c *Conn) handleGreet(enhanced bool, arg string) {
if !enhanced {
Expand Down
3 changes: 1 addition & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ func (s *Server) handleConn(c *Conn) error {
if err == nil {
cmd, arg, err := parseCmd(line)
if err != nil {
c.nbrErrors++
c.WriteResponse(501, EnhancedCode{5, 5, 2}, "Bad command")
c.protocolError(501, EnhancedCode{5, 5, 2}, "Bad command")
continue
}

Expand Down