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

9
Gopkg.lock generated
View File

@@ -40,10 +40,11 @@
revision = "7e096a0a6197b89989e8cc31016daa67c8c62051" revision = "7e096a0a6197b89989e8cc31016daa67c8c62051"
[[projects]] [[projects]]
branch = "master" branch = "8-unauthed-mail"
name = "github.com/emersion/go-smtp" name = "github.com/emersion/go-smtp"
packages = ["."] packages = ["."]
revision = "a63104657743890cb7c2fd54f15a2725291f6a9f" revision = "b858ceea28e02e5fab171c5f877860ad9080f8b6"
source = "https://github.com/lupine/go-smtp"
[[projects]] [[projects]]
branch = "master" branch = "master"
@@ -52,7 +53,7 @@
"bcrypt", "bcrypt",
"blowfish" "blowfish"
] ]
revision = "91a49db82a88618983a78a06c1cbd4e00ab749ab" revision = "85f98707c97e11569271e4d9b3d397e079c4f4d0"
[[projects]] [[projects]]
branch = "master" branch = "master"
@@ -81,6 +82,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "9d6a4c3f6c1568091ba837d3207861ceae7cb7b2d4b5e094d1892ed935bd72cd" inputs-digest = "4f9c6f3cf2ad42694856bc039ddb3a7e78af676f095fc24ef633305fe499c3bf"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@@ -26,8 +26,9 @@
[[constraint]] [[constraint]]
branch = "master" branch = "8-unauthed-mail"
name = "github.com/emersion/go-smtp" name = "github.com/emersion/go-smtp"
source = "https://github.com/lupine/go-smtp"
[prune] [prune]
go-tests = true go-tests = true

View File

@@ -31,7 +31,7 @@ func NewServer(cancel context.CancelFunc, datastore store.Interface, submission
if submission { if submission {
out.handler = &Sender{} out.handler = &Sender{}
out.server.Addr = ":587" out.server.Addr = ":587"
out.allowLogin = true // Only allow login on submission ports out.submission = true // Only allow login on submission ports
} else { } else {
out.handler = &Receiver{} out.handler = &Receiver{}
out.server.Addr = ":25" out.server.Addr = ":25"
@@ -46,7 +46,7 @@ type concrete struct {
store store.Interface store store.Interface
server *smtp.Server server *smtp.Server
handler Handler handler Handler
allowLogin bool submission bool
// Session IDs // Session IDs
sid uint64 sid uint64
@@ -64,7 +64,7 @@ func (c *concrete) Run() {
// backend implementation for go-smtp // backend implementation for go-smtp
func (c *concrete) Login(user, pass string) (smtp.User, error) { func (c *concrete) Login(user, pass string) (smtp.User, error) {
if !c.allowLogin { if !c.submission {
return nil, fmt.Errorf("Login is disabled") return nil, fmt.Errorf("Login is disabled")
} }
@@ -87,6 +87,23 @@ func (c *concrete) Login(user, pass string) (smtp.User, error) {
return session, nil return session, nil
} }
func (c *concrete) AnonymousLogin() (smtp.User, error) {
if c.submission {
return nil, smtp.AuthRequiredErr
}
session := &Session{
ID: atomic.AddUint64(&c.sid, uint64(1)),
Account: nil,
Handler: c.handler,
}
log.Printf("Beginning anonymous %s session %d", c.name, session.ID)
// FIXME: TODO: Track ongoing sessions for termination or notifications
return session, nil
}
func (c *concrete) Close() error { func (c *concrete) Close() error {
c.cancel() // FIXME: this doesn't touch the server c.cancel() // FIXME: this doesn't touch the server

View File

@@ -8,6 +8,10 @@ import (
type Backend interface { type Backend interface {
// Authenticate a user. // Authenticate a user.
Login(username, password string) (User, error) Login(username, password string) (User, error)
// Called if the client attempts to send mail without logging in first.
// Respond with smtp.AuthRequiredErr if you don't want to support this.
AnonymousLogin() (User, error)
} }
// An authenticated user. // An authenticated user.

View File

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

View File

@@ -27,6 +27,10 @@ type Server struct {
AllowInsecureAuth bool AllowInsecureAuth bool
Debug io.Writer Debug io.Writer
// If set, the AUTH command will not be advertised and authentication
// attempts will be rejected. This setting overrides AllowInsecureAuth.
AuthDisabled bool
// The server backend. // The server backend.
Backend Backend Backend Backend