Skip to content

Commit

Permalink
server: Improved error handling (#116)
Browse files Browse the repository at this point in the history
* Factor error handling into seperate function

* Rename error handler and change status for too many errors

* Make error threshold a global const

* Switches protocolError status codes to 500 5.5.1

* Patches too many invalid commands test

Now matches the 500 code thrown by protocolError
  • Loading branch information
nsmith5 authored Sep 13, 2020
1 parent 53ebaff commit f804d2f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
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

0 comments on commit f804d2f

Please sign in to comment.