A relay can take minutes to answer the final dot (content filter, large message, slow link). One deadline over the whole transaction cut it after the message had been accepted, so the delivery was retried and sent again. The connection now moves its deadline as the transaction advances: setup (dial, greeting, TLS, AUTH, MAIL, RCPT, DATA) has SMTPRelay.Timeout, 60 s by default; the upload has 60 s plus the size at 32 KiB/s, at most 10 minutes; the reply to the final dot has 10 minutes (RFC 5321 4.5.3.2.6). go-smtp sets and clears its own per-command deadlines, so a wrapper clips them to the current phase. Context cancellation still closes the connection at once. A timeout in any phase is temporary; after the final dot the error says the outcome is unknown.
385 lines
12 KiB
Go
385 lines
12 KiB
Go
// Package outbound delivers messages to the SMTP relay.
|
|
package outbound
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/emersion/go-sasl"
|
|
"github.com/emersion/go-smtp"
|
|
)
|
|
|
|
// Sender delivers one message to a set of recipients.
|
|
type Sender interface {
|
|
// Send submits data with the given envelope sender to every address in to.
|
|
Send(ctx context.Context, from string, to []string, data []byte) error
|
|
}
|
|
|
|
// DefaultBatchSize is the number of recipients per SMTP transaction.
|
|
const DefaultBatchSize = 50
|
|
|
|
// TLS modes of SMTPRelay.
|
|
const (
|
|
TLSNone = "none" // plain connection (a local Postfix); the default
|
|
TLSStartTLS = "starttls" // upgrade the connection; fail if the relay does not offer it
|
|
TLSImplicit = "tls" // encrypted from the first byte (port 465)
|
|
)
|
|
|
|
// SMTPRelay sends through an SMTP relay: a local Postfix, or an external one such as SES.
|
|
type SMTPRelay struct {
|
|
Addr string // host:port
|
|
BatchSize int // recipients per transaction; DefaultBatchSize if zero
|
|
|
|
TLS string // TLSNone (default), TLSStartTLS or TLSImplicit
|
|
ServerName string // name the certificate must match; the host of Addr by default
|
|
RootCAs *x509.CertPool // trusted CAs; the system's by default
|
|
Username string // SMTP AUTH PLAIN; only over TLS or to a loopback address
|
|
Password string
|
|
Helo string // EHLO name; "localhost" by default
|
|
// Timeout bounds the setup of each transaction: connection, greeting, TLS, AUTH, MAIL FROM,
|
|
// RCPT TO and the DATA command. DefaultTimeout if zero. The upload of the message and the
|
|
// wait for its acceptance have their own budgets (see uploadBudget and finalReplyTimeout).
|
|
Timeout time.Duration
|
|
}
|
|
|
|
// DefaultTimeout is the default SMTPRelay.Timeout.
|
|
const DefaultTimeout = time.Minute
|
|
|
|
// Budgets of the later phases of a transaction. Variables so that tests can shrink them.
|
|
var (
|
|
dataFloor = time.Minute // least time to upload a message
|
|
dataRate = 32 << 10 // slowest upload we wait for, in bytes per second
|
|
dataCap = 10 * time.Minute // most time to upload a message
|
|
// finalReplyTimeout is how long the relay may take to answer the final dot: RFC 5321 4.5.3.2.6
|
|
// asks for 10 minutes, because content filters run at that point.
|
|
finalReplyTimeout = 10 * time.Minute
|
|
)
|
|
|
|
// uploadBudget is the time allowed to upload a message of size bytes: dataFloor plus the time
|
|
// it takes at dataRate, at most dataCap.
|
|
func uploadBudget(size int) time.Duration {
|
|
b := dataFloor + time.Duration(float64(size)/float64(dataRate)*float64(time.Second))
|
|
return min(b, dataCap)
|
|
}
|
|
|
|
func (r *SMTPRelay) timeout() time.Duration {
|
|
if r.Timeout > 0 {
|
|
return r.Timeout
|
|
}
|
|
return DefaultTimeout
|
|
}
|
|
|
|
// phasedConn keeps the connection within the budget of the current phase. go-smtp sets its own
|
|
// deadline around each command and clears it afterwards; here no deadline it sets, cleared or not,
|
|
// may go past the limit of the phase.
|
|
type phasedConn struct {
|
|
net.Conn
|
|
limit time.Time
|
|
}
|
|
|
|
func (p *phasedConn) SetDeadline(t time.Time) error {
|
|
if t.IsZero() || p.limit.Before(t) {
|
|
t = p.limit
|
|
}
|
|
return p.Conn.SetDeadline(t)
|
|
}
|
|
|
|
// phase gives the connection d to finish the next phase. The context's own deadline and its
|
|
// cancellation close the connection instead (see dial), so that the error is the context's.
|
|
func (p *phasedConn) phase(d time.Duration) {
|
|
p.limit = time.Now().Add(d)
|
|
p.Conn.SetDeadline(p.limit)
|
|
}
|
|
|
|
// Send splits recipients in batches and sends one transaction per batch.
|
|
func (r *SMTPRelay) Send(ctx context.Context, from string, to []string, data []byte) error {
|
|
size := r.BatchSize
|
|
if size <= 0 {
|
|
size = DefaultBatchSize
|
|
}
|
|
for start := 0; start < len(to); start += size {
|
|
end := min(start+size, len(to))
|
|
if err := r.sendBatch(ctx, from, to[start:end], data); err != nil {
|
|
if len(to) <= size {
|
|
return err // a single batch: the range adds nothing
|
|
}
|
|
return fmt.Errorf("batch %d-%d: %w", start, end, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// dial opens the connection and speaks EHLO, TLS and AUTH as configured. The connection's deadline
|
|
// is the end of the setup budget, which starts now. The returned release function must be called when the client is done: until then the connection
|
|
// is closed if ctx is cancelled, even in the middle of EHLO, STARTTLS or AUTH with a silent relay.
|
|
func (r *SMTPRelay) dial(ctx context.Context) (c *smtp.Client, conn *phasedConn, release func(), err error) {
|
|
setupEnd := time.Now().Add(r.timeout())
|
|
host, _, err := net.SplitHostPort(r.Addr)
|
|
if err != nil {
|
|
return nil, nil, nil, fmt.Errorf("relay address %q: %w", r.Addr, err)
|
|
}
|
|
mode := r.TLS
|
|
if mode == "" {
|
|
mode = TLSNone
|
|
}
|
|
if r.Username != "" && mode == TLSNone && !isLoopback(host) {
|
|
return nil, nil, nil, errors.New("refusing to send credentials in clear to a remote relay: set relay.tls to starttls or tls")
|
|
}
|
|
helo := r.Helo
|
|
if helo == "" {
|
|
helo = "localhost"
|
|
}
|
|
tlsConf := &tls.Config{ServerName: host, RootCAs: r.RootCAs, MinVersion: tls.VersionTLS12}
|
|
if r.ServerName != "" {
|
|
tlsConf.ServerName = r.ServerName
|
|
}
|
|
|
|
d := &net.Dialer{Timeout: 10 * time.Second, Deadline: setupEnd}
|
|
var raw net.Conn
|
|
if mode == TLSImplicit {
|
|
raw, err = (&tls.Dialer{NetDialer: d, Config: tlsConf}).DialContext(ctx, "tcp", r.Addr)
|
|
} else {
|
|
raw, err = d.DialContext(ctx, "tcp", r.Addr)
|
|
}
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
// A relay that stops answering must not hold us past the budget or the context: apply the
|
|
// earlier deadline to the connection, and close the connection if the context is cancelled.
|
|
conn = &phasedConn{Conn: raw}
|
|
conn.phase(time.Until(setupEnd))
|
|
stop := make(chan struct{})
|
|
go func() {
|
|
select {
|
|
case <-ctx.Done():
|
|
conn.Close()
|
|
case <-stop:
|
|
}
|
|
}()
|
|
release = func() { close(stop) }
|
|
fail := func(err error) (*smtp.Client, *phasedConn, func(), error) {
|
|
release()
|
|
conn.Close()
|
|
return nil, nil, nil, err
|
|
}
|
|
|
|
if mode == TLSStartTLS {
|
|
// go-smtp performs EHLO and STARTTLS itself and fails when STARTTLS is not offered, so a
|
|
// downgrade is impossible. Its first EHLO says "localhost"; ours follows over TLS below.
|
|
c, err = smtp.NewClientStartTLS(conn, tlsConf)
|
|
if err != nil {
|
|
return fail(err)
|
|
}
|
|
} else {
|
|
c = smtp.NewClient(conn)
|
|
}
|
|
if err := c.Hello(helo); err != nil {
|
|
c.Close()
|
|
return fail(err)
|
|
}
|
|
if r.Username != "" {
|
|
if err := c.Auth(sasl.NewPlainClient("", r.Username, r.Password)); err != nil {
|
|
c.Close()
|
|
return fail(authError(err))
|
|
}
|
|
}
|
|
return c, conn, release, nil
|
|
}
|
|
|
|
func isLoopback(host string) bool {
|
|
if host == "localhost" {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(host)
|
|
return ip != nil && ip.IsLoopback()
|
|
}
|
|
|
|
// authError turns a refusal of our credentials into a temporary error. A wrong password or a
|
|
// relay that wants authentication is a configuration problem: if it were permanent every message
|
|
// would be failed for good, instead of waiting in the queue until the admin fixes it.
|
|
func authError(err error) error {
|
|
var se *smtp.SMTPError
|
|
if errors.As(err, &se) && (se.Code == 530 || se.Code == 534 || se.Code == 535 || se.Code == 538) {
|
|
return fmt.Errorf("relay authentication failed (%d %s)", se.Code, strings.TrimSpace(se.Message))
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (r *SMTPRelay) sendBatch(ctx context.Context, from string, to []string, data []byte) (err error) {
|
|
c, conn, release, err := r.dial(ctx)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
err = ctx.Err() // report the cause, not "use of closed connection"
|
|
}
|
|
return err
|
|
}
|
|
defer release()
|
|
defer c.Close()
|
|
defer func() {
|
|
if err != nil && ctx.Err() != nil {
|
|
err = ctx.Err()
|
|
}
|
|
}()
|
|
|
|
// The commands are issued one by one, rather than with SendMail, to know which one failed.
|
|
var mailOpts *smtp.MailOptions
|
|
if !isASCII(from) || !allASCII(to) {
|
|
mailOpts = &smtp.MailOptions{UTF8: true}
|
|
}
|
|
if err := c.Mail(from, mailOpts); err != nil {
|
|
return authError(&PhaseError{Phase: "MAIL", Err: err})
|
|
}
|
|
for _, rcpt := range to {
|
|
if err := c.Rcpt(rcpt, nil); err != nil {
|
|
return authError(&PhaseError{Phase: "RCPT", Err: err})
|
|
}
|
|
}
|
|
w, err := c.Data()
|
|
if err != nil {
|
|
return authError(&PhaseError{Phase: "DATA", Err: err})
|
|
}
|
|
conn.phase(uploadBudget(len(data)))
|
|
if _, err := io.Copy(w, bytes.NewReader(data)); err != nil {
|
|
return &PhaseError{Phase: "DATA", Err: err}
|
|
}
|
|
// Close sends the rest of the message and the final dot, then waits for the verdict.
|
|
conn.phase(finalReplyTimeout)
|
|
if err := w.Close(); err != nil {
|
|
var ne net.Error
|
|
if errors.As(err, &ne) && ne.Timeout() {
|
|
// Temporary like any timeout, but the relay may have taken the message: a retry can
|
|
// deliver it twice. Say so, so the admin can tell.
|
|
err = fmt.Errorf("no reply to the end of the message, outcome unknown: %w", err)
|
|
}
|
|
return &PhaseError{Phase: "DATA", Err: err}
|
|
}
|
|
// The relay has accepted the message. If QUIT now fails, retrying would deliver it twice.
|
|
conn.phase(r.timeout())
|
|
c.Quit()
|
|
return nil
|
|
}
|
|
|
|
func isASCII(s string) bool {
|
|
for i := 0; i < len(s); i++ {
|
|
if s[i] >= 0x80 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func allASCII(ss []string) bool {
|
|
for _, s := range ss {
|
|
if !isASCII(s) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Sent is a message recorded by Fake.
|
|
type Sent struct {
|
|
From string
|
|
To []string
|
|
Data []byte
|
|
}
|
|
|
|
// Fake records messages instead of sending them. For tests.
|
|
type Fake struct {
|
|
mu sync.Mutex
|
|
Sent []Sent
|
|
}
|
|
|
|
// Messages returns a copy of what was recorded; safe while sends are running.
|
|
func (f *Fake) Messages() []Sent {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return append([]Sent(nil), f.Sent...)
|
|
}
|
|
|
|
// Send records the message.
|
|
func (f *Fake) Send(_ context.Context, from string, to []string, data []byte) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.Sent = append(f.Sent, Sent{from, append([]string(nil), to...), append([]byte(nil), data...)})
|
|
return nil
|
|
}
|
|
|
|
// PermanentError marks a failure that retrying cannot fix. SMTP 5xx replies are
|
|
// permanent without being wrapped in it; use it for other sources (and in tests).
|
|
type PermanentError struct {
|
|
Err error
|
|
BadMailbox bool // the destination mailbox is invalid
|
|
}
|
|
|
|
func (e *PermanentError) Error() string { return e.Err.Error() }
|
|
func (e *PermanentError) Unwrap() error { return e.Err }
|
|
|
|
// IsPermanent reports whether err is a permanent failure: an SMTP 5xx reply or a PermanentError.
|
|
func IsPermanent(err error) bool {
|
|
var pe *PermanentError
|
|
if errors.As(err, &pe) {
|
|
return true
|
|
}
|
|
var se *smtp.SMTPError
|
|
return errors.As(err, &se) && se.Code >= 500 && se.Code <= 599
|
|
}
|
|
|
|
// PhaseError says which SMTP command a failure answers. The same enhanced status means different
|
|
// things depending on it: 5.1.1 to RCPT is "this mailbox does not exist", but to MAIL FROM it is
|
|
// about our own sender address and says nothing about the recipient.
|
|
type PhaseError struct {
|
|
Phase string // "MAIL", "RCPT" or "DATA"
|
|
Err error
|
|
}
|
|
|
|
// Error names the command, so the admin can tell a refused recipient from a refused sender.
|
|
func (e *PhaseError) Error() string {
|
|
label := map[string]string{"MAIL": "MAIL FROM", "RCPT": "RCPT TO", "DATA": "DATA"}[e.Phase]
|
|
if label == "" {
|
|
label = e.Phase
|
|
}
|
|
return label + ": " + e.Err.Error()
|
|
}
|
|
func (e *PhaseError) Unwrap() error { return e.Err }
|
|
|
|
// AtRcpt marks err as the relay's answer to RCPT TO (SMTPRelay does this itself; it is exported
|
|
// for tests that fake a relay).
|
|
func AtRcpt(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
return &PhaseError{Phase: "RCPT", Err: err}
|
|
}
|
|
|
|
// recipientUnknown are the enhanced status codes that say the destination mailbox is invalid:
|
|
// 5.1.1 bad mailbox, 5.1.2 bad domain, 5.1.3 bad address syntax, 5.1.6 moved, 5.1.10 null MX.
|
|
var recipientUnknown = map[smtp.EnhancedCode]bool{
|
|
{5, 1, 1}: true, {5, 1, 2}: true, {5, 1, 3}: true, {5, 1, 6}: true, {5, 1, 10}: true,
|
|
}
|
|
|
|
// IsBadMailbox reports whether err is the relay saying the recipient does not exist: a 5.1.1, 5.1.2,
|
|
// 5.1.3, 5.1.6 or 5.1.10 answer to RCPT TO. Anything else, such as "relay access denied", a policy
|
|
// refusal or a rejected sender (5.1.7, 5.1.8, or any code answering MAIL FROM), says nothing about
|
|
// the recipient and must not count against a member.
|
|
func IsBadMailbox(err error) bool {
|
|
var pe *PermanentError
|
|
if errors.As(err, &pe) {
|
|
return pe.BadMailbox
|
|
}
|
|
var ph *PhaseError
|
|
if !errors.As(err, &ph) || ph.Phase != "RCPT" {
|
|
return false
|
|
}
|
|
var se *smtp.SMTPError
|
|
return errors.As(err, &se) && se.Code >= 500 && se.Code <= 599 && recipientUnknown[se.EnhancedCode]
|
|
}
|