Get receiving email a step closer with a modified go-smtp

This commit is contained in:
2018-03-07 21:58:50 +00:00
parent 697e90ab99
commit 26c702b11c
6 changed files with 77 additions and 26 deletions

View File

@@ -37,6 +37,10 @@ type Conn struct {
locker sync.Mutex
}
var (
AuthRequiredErr = fmt.Errorf("Please authenticate first.")
)
func newConn(c net.Conn, s *Server) *Conn {
sc := &Conn{
server: s,
@@ -64,6 +68,16 @@ func (c *Conn) init() {
c.text = textproto.NewConn(rwc)
}
func (c *Conn) unrecognizedCommand(cmd string) {
c.WriteResponse(500, fmt.Sprintf("Syntax error, %v command unrecognized", cmd))
c.nbrErrors++
if c.nbrErrors > 3 {
c.WriteResponse(500, "Too many unrecognized commands")
c.Close()
}
}
// Commands are dispatched to the appropriate handler functions.
func (c *Conn) handle(cmd string, arg string) {
if cmd == "" {
@@ -94,17 +108,15 @@ func (c *Conn) handle(cmd string, arg string) {
c.WriteResponse(221, "Goodnight and good luck")
c.Close()
case "AUTH":
c.handleAuth(arg)
if c.server.AuthDisabled {
c.unrecognizedCommand(cmd)
} else {
c.handleAuth(arg)
}
case "STARTTLS":
c.handleStartTLS()
default:
c.WriteResponse(500, fmt.Sprintf("Syntax error, %v command unrecognized", cmd))
c.nbrErrors++
if c.nbrErrors > 3 {
c.WriteResponse(500, "Too many unrecognized commands")
c.Close()
}
c.unrecognizedCommand(cmd)
}
}
@@ -118,10 +130,12 @@ func (c *Conn) User() User {
return c.user
}
// Setting the user resets any message beng generated
func (c *Conn) SetUser(user User) {
c.locker.Lock()
defer c.locker.Unlock()
c.user = user
c.msg = &message{}
}
func (c *Conn) Close() error {
@@ -138,6 +152,11 @@ func (c *Conn) IsTLS() bool {
return ok
}
func (c *Conn) authAllowed() bool {
return !c.server.AuthDisabled &&
(c.IsTLS() || c.server.AllowInsecureAuth)
}
// GREET state -> waiting for HELO
func (c *Conn) handleGreet(enhanced bool, arg string) {
if !enhanced {
@@ -163,7 +182,7 @@ func (c *Conn) handleGreet(enhanced bool, arg string) {
if c.server.TLSConfig != nil && !c.IsTLS() {
caps = append(caps, "STARTTLS")
}
if c.IsTLS() || c.server.AllowInsecureAuth {
if c.authAllowed() {
authCap := "AUTH"
for name, _ := range c.server.auths {
authCap += " " + name
@@ -187,9 +206,15 @@ func (c *Conn) handleMail(arg string) {
c.WriteResponse(502, "Please introduce yourself first.")
return
}
if c.msg == nil {
c.WriteResponse(502, "Please authenticate first.")
return
if c.User() == nil {
user, err := c.server.Backend.AnonymousLogin()
if err != nil {
c.WriteResponse(502, err.Error())
return
}
c.SetUser(user)
}
// Match FROM, while accepting '>' as quoted pair and in double quoted strings
@@ -318,8 +343,6 @@ func (c *Conn) handleAuth(arg string) {
if c.User() != nil {
c.WriteResponse(235, "Authentication succeeded")
c.msg = &message{}
}
}
@@ -421,12 +444,13 @@ func (c *Conn) ReadLine() (string, error) {
}
func (c *Conn) reset() {
if user := c.User(); user != nil {
user.Logout()
c.locker.Lock()
defer c.locker.Unlock()
if c.user != nil {
c.user.Logout()
}
c.locker.Lock()
c.user = nil
c.msg = nil
c.locker.Unlock()
}