Files
warmbly/internal/client/smtpimap/smtp/client.go
T

568 lines
20 KiB
Go

package smtp
import (
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net"
"net/mail"
"net/smtp"
"net/textproto"
"sort"
"strings"
"time"
"github.com/warmbly/warmbly/internal/client/mailtls"
"github.com/warmbly/warmbly/internal/client/netbind"
"github.com/warmbly/warmbly/internal/errx"
"github.com/warmbly/warmbly/internal/models"
"github.com/warmbly/warmbly/internal/pkg/mailhdr"
"golang.org/x/oauth2"
)
// SendTimeout bounds one whole SMTP conversation: dial, greeting, AUTH, the
// recipients, the message and the server's verdict. It is generous because a
// large message to a slow relay is legitimate, and it exists so a peer that
// stops answering cannot hold a send goroutine forever.
const SendTimeout = 90 * time.Second
// errNoSTARTTLS distinguishes a missing upgrade from a network failure.
var errNoSTARTTLS = errors.New(errx.MailConnectionReasonNoSTARTTLS)
// tlsFailure reports a handshake that failed for a reason other than a pin.
func tlsFailure(base *errx.MailError, stage string, err error) *errx.MailError {
if mailtls.CertificateRefused(err) {
base = errx.ErrMailServerUnreachableAt(stage, errors.New(errx.MailConnectionReasonCertificate))
}
return sendFailure(base, err, stage, "mailbox")
}
// handshakeFailure marks a TLS handshake the server closed, which is how some servers shed connections.
func handshakeFailure(base *errx.MailError, stage string, err error) *errx.MailError {
failure := tlsFailure(base, stage, err)
if droppedConnection(err) && failure.Failure != nil {
failure.Failure.Cause = causeDroppedHandshake
}
return failure
}
// pinFailure reports a pinned certificate refusal as a retryable mailbox
// connection error, never as a credentials failure.
func pinFailure(perr *mailtls.PinError) *errx.MailError {
return errx.WithSendFailure(mailtls.MailError(perr, "SMTP"), errx.SendFailure{
Provider: "smtp", Protocol: "smtp", Stage: "tls", Scope: "mailbox",
ObservedAt: time.Now().UTC(), Disposition: errx.SendRetry,
})
}
// dialCause removes local and duplicated remote addresses from a dial error.
func dialCause(err error) error {
var op *net.OpError
if errors.As(err, &op) && op.Err != nil {
return op.Err
}
return err
}
// ehloName is the domain to announce in EHLO, taken from the sender's own
// address. Empty leaves net/smtp's default in place.
func ehloName(address string) string {
if at := strings.LastIndex(address, "@"); at >= 0 && at+1 < len(address) {
return address[at+1:]
}
return ""
}
type Client struct {
FirstName string
LastName string
Email string
AuthType models.AuthType
Credentials *models.Service
Oauth2 *models.Oauth2Service
// plaintext skips the STARTTLS requirement. Only the tests set it, to
// talk to an in-process server; no product path reaches it, because SMTP
// AUTH in the clear would put the mailbox password on the wire.
plaintext bool
// BindIP optionally pins outbound TCP to a specific local source address.
// When nil, WORKER_BIND_IP is consulted; when still unset, the OS default
// route is used.
BindIP *net.TCPAddr
}
// Attachment is a fully-resolved file to encode into an outbound message. Data
// is the raw bytes; MimeType drives the Content-Type.
type Attachment struct {
Filename string
MimeType string
Data []byte
}
// Send submits the message and returns the exact RFC 5322 bytes it put on the
// wire, so the caller can file the same copy in the mailbox's Sent folder.
// The bytes are returned even on failure: they are useful in a log, and a
// caller that ignores them is unaffected.
func (c *Client) Send(
ctx context.Context,
fromName string,
to, cc, bcc []string,
messageID,
subject, bodyPlain, bodyHTML,
inReplyTo string,
attachments []Attachment,
customHeaders ...map[string]string,
) ([]byte, *errx.MailError) {
// A per-send name (the mailbox as renamed in the dashboard) wins over the
// one cached at load time.
fromName = strings.TrimSpace(fromName)
if fromName == "" {
fromName = strings.TrimSpace(c.FirstName + " " + c.LastName)
}
from := mail.Address{Address: c.Email, Name: fromName}
// ----- Headers -----
headers := map[string]string{
"From": from.String(),
"To": mailhdr.AddressList(to),
// Headers are ASCII on the wire: a raw accent or emoji in the subject
// arrives as mojibake, so RFC 2047-encode it (a no-op for plain ASCII).
"Subject": mailhdr.Subject(subject),
"Date": time.Now().Format(time.RFC1123Z),
"MIME-Version": "1.0",
}
if messageID != "" {
// Write the caller's Message-ID so the id we record matches what was
// actually sent; without it the receiving server assigns its own and
// reply/bounce correlation and threading break.
headers["Message-ID"] = "<" + strings.Trim(messageID, "<>") + ">"
}
if len(cc) > 0 {
headers["Cc"] = mailhdr.AddressList(cc)
}
if inReplyTo != "" {
// The parent Message-ID may arrive already wrapped in <...>; trim before
// re-wrapping so we don't emit <<id>>, which won't match the original
// Message-ID header and breaks Gmail/Outlook threading.
mid := "<" + strings.Trim(inReplyTo, "<>") + ">"
headers["In-Reply-To"] = mid
headers["References"] = mid
}
// Add custom headers (e.g., X-Warmbly-Token for warmup)
if len(customHeaders) > 0 {
for k, v := range customHeaders[0] {
headers[k] = v
}
}
for k, v := range headers {
if err := mailhdr.CheckHeader(k, v); err != nil {
return nil, errx.MError(errx.MailErrorWarning, errx.MailErrorCodeUnsupported, k+": "+err.Error(), errx.MailErrorResolveMethodNone)
}
}
var msg bytes.Buffer
if len(attachments) > 0 {
c.writeMixedBody(&msg, headers, bodyPlain, bodyHTML, attachments)
} else {
c.writeAlternativeBody(&msg, headers, bodyPlain, bodyHTML)
}
// Envelope recipients are bare addresses: a display name belongs in the
// header, and "Ana <a@b.com>" in RCPT TO is rejected by the server.
recipients := mailhdr.BareList(to)
recipients = append(recipients, mailhdr.BareList(cc)...)
recipients = append(recipients, mailhdr.BareList(bcc)...)
raw := msg.Bytes()
return raw, c.sendRaw(ctx, from.Address, recipients, raw)
}
// writeAlternativeBody writes the message with its top-level headers: a single
// text part when there is only one body, else multipart/alternative. An
// alternative wrapper around one part is a filter signal (rspamd MIME_MA_MISSING_HTML).
func (c *Client) writeAlternativeBody(msg *bytes.Buffer, headers map[string]string, bodyPlain, bodyHTML string) {
if bodyPlain == "" || bodyHTML == "" {
contentType, body := "text/plain; charset=UTF-8", bodyPlain
if bodyPlain == "" {
contentType, body = "text/html; charset=UTF-8", bodyHTML
}
headers["Content-Type"] = contentType
headers["Content-Transfer-Encoding"] = "quoted-printable"
writeHeaders(msg, headers)
qp := quotedprintable.NewWriter(msg)
qp.Write([]byte(body))
qp.Close()
return
}
writer := multipart.NewWriter(msg)
headers["Content-Type"] = fmt.Sprintf("multipart/alternative; boundary=%s", writer.Boundary())
writeHeaders(msg, headers)
writeTextParts(writer, bodyPlain, bodyHTML)
writer.Close()
}
// headerOrder is the order top-level headers go on the wire, the one mail
// clients use. Anything not listed follows, sorted, so no two sends differ.
var headerOrder = []string{
"Date", "From", "To", "Cc", "Message-ID", "In-Reply-To", "References", "Subject",
"MIME-Version", "Content-Type", "Content-Transfer-Encoding",
}
// writeHeaders writes headers in headerOrder, then a blank line.
func writeHeaders(msg *bytes.Buffer, headers map[string]string) {
rank := make(map[string]int, len(headerOrder))
for i, k := range headerOrder {
rank[k] = i
}
keys := make([]string, 0, len(headers))
for k := range headers {
keys = append(keys, k)
}
sort.SliceStable(keys, func(i, j int) bool {
ri, iok := rank[keys[i]]
rj, jok := rank[keys[j]]
switch {
case iok && jok:
return ri < rj
case iok != jok:
return iok
}
return keys[i] < keys[j]
})
for _, k := range keys {
fmt.Fprintf(msg, "%s: %s\r\n", k, headers[k])
}
fmt.Fprint(msg, "\r\n")
}
// writeMixedBody writes a multipart/mixed message: a multipart/alternative
// sub-tree for the text bodies, then one application/* part per attachment with
// a Content-Disposition: attachment header.
func (c *Client) writeMixedBody(msg *bytes.Buffer, headers map[string]string, bodyPlain, bodyHTML string, attachments []Attachment) {
mixed := multipart.NewWriter(msg)
headers["Content-Type"] = fmt.Sprintf("multipart/mixed; boundary=%s", mixed.Boundary())
writeHeaders(msg, headers)
if bodyPlain == "" || bodyHTML == "" {
// One body is a direct child: filters read nested parts too.
writeTextParts(mixed, bodyPlain, bodyHTML)
} else {
var altBuf bytes.Buffer
alt := multipart.NewWriter(&altBuf)
writeTextParts(alt, bodyPlain, bodyHTML)
alt.Close()
altPart, _ := mixed.CreatePart(textproto.MIMEHeader{
"Content-Type": {fmt.Sprintf("multipart/alternative; boundary=%s", alt.Boundary())},
})
altPart.Write(altBuf.Bytes())
}
// One attachment part per file.
for _, a := range attachments {
mimeType := a.MimeType
if mimeType == "" {
mimeType = "application/octet-stream"
}
fn := mime.QEncoding.Encode("utf-8", a.Filename)
part, _ := mixed.CreatePart(textproto.MIMEHeader{
"Content-Type": {fmt.Sprintf("%s; name=%q", mimeType, fn)},
"Content-Transfer-Encoding": {"base64"},
"Content-Disposition": {fmt.Sprintf("attachment; filename=%q", fn)},
})
writeBase64Wrapped(part, a.Data)
}
mixed.Close()
}
// writeTextParts writes the text/plain and optional text/html quoted-printable
// parts into the given multipart writer.
func writeTextParts(writer *multipart.Writer, bodyPlain, bodyHTML string) {
if bodyPlain != "" {
part, _ := writer.CreatePart(textproto.MIMEHeader{
"Content-Type": {"text/plain; charset=UTF-8"},
"Content-Transfer-Encoding": {"quoted-printable"},
})
qp := quotedprintable.NewWriter(part)
qp.Write([]byte(bodyPlain))
qp.Close()
}
if bodyHTML != "" {
part, _ := writer.CreatePart(textproto.MIMEHeader{
"Content-Type": {"text/html; charset=UTF-8"},
"Content-Transfer-Encoding": {"quoted-printable"},
})
qp := quotedprintable.NewWriter(part)
qp.Write([]byte(bodyHTML))
qp.Close()
}
}
// writeBase64Wrapped writes data as base64, hard-wrapped at 76 columns per
// RFC 2045 so strict MTAs accept the message.
func writeBase64Wrapped(w io.Writer, data []byte) {
encoded := base64.StdEncoding.EncodeToString(data)
const lineLen = 76
for i := 0; i < len(encoded); i += lineLen {
end := i + lineLen
if end > len(encoded) {
end = len(encoded)
}
w.Write([]byte(encoded[i:end] + "\r\n"))
}
}
// ---------- Internal helpers ----------
func (c *Client) sendRaw(ctx context.Context, from string, to []string, data []byte) (result *errx.MailError) {
var host string
var port int
var security string
var policy mailtls.Policy
switch c.AuthType {
case models.AuthPlain:
host = c.Credentials.Host
port = c.Credentials.Port
security = c.Credentials.Security
policy = mailtls.PolicyFor(c.Credentials)
case models.AuthOAuth2:
host = c.Oauth2.Host
port = c.Oauth2.Port
}
// Normalized before it is used anywhere: brackets belong to the address,
// not to the host, and JoinHostPort is what puts them back for an IPv6
// literal.
host = models.NormalizeMailHost(host)
addr := models.MailDialAddress(host, port)
capture := &mailtls.Capture{}
tlsConf := mailtls.Config(host, policy, capture)
// Everything after the dial gets a deadline. net/smtp sets none of its
// own and only the connect was bounded, so a peer that stopped answering
// without closing the connection parked the send goroutine forever: the
// greeting, AUTH, each RCPT and the wait for the server's verdict at the
// end of DATA all block with nothing to fail them.
ctx, cancel := context.WithTimeout(ctx, SendTimeout)
defer cancel()
resolved := models.ResolveSMTPSecurity(security, port)
// The unencrypted mode is checked before the dial and again against the
// peer we actually got, because only the second one is a fact about this
// socket rather than about what DNS said a moment ago.
if resolved == models.MailSecurityNone && !models.CleartextMailAllowed(host) {
return errx.ErrMailInsecureRemoteHost
}
if pace := paceFor(host, c.BindIP); pace != nil {
if held := pace.wait(ctx, time.Now()); held != nil {
return held
}
defer func() { result = pace.observe(time.Now(), result) }()
}
// The socket may be 587 with STARTTLS when the mailbox's 465 never
// answered; the mode to speak is the one the dial reports.
dialed, err := DialSubmission(ctx, c.BindIP, host, port, security)
if errors.Is(err, ErrSMTPSEgressBlocked) {
return errx.ErrMailSMTPSEgressBlocked(addr)
}
if err != nil || dialed.Conn == nil {
if err == nil {
err = errors.New("dial returned no connection")
}
cause := dialCause(err)
if errors.Is(err, netbind.ErrPrivateMailHost) {
cause = errors.New(errx.MailConnectionReasonPrivateHost)
}
return sendFailure(errx.ErrMailServerUnreachableAt("dial "+addr, cause), err, "dial", "mailbox")
}
conn := dialed.Conn
defer conn.Close()
resolved = dialed.Security
if resolved == models.MailSecurityNone && !netbind.LoopbackPeer(conn) {
return errx.ErrMailInsecureRemoteHost
}
if deadline, ok := ctx.Deadline(); ok {
_ = conn.SetDeadline(deadline)
}
// Implicit TLS (SMTPS) means the server speaks TLS from the first byte,
// so a plaintext dial + STARTTLS never gets past the greeting. Its
// handshake failure is reported at the dial stage, where it always was.
implicitTLS := resolved == models.MailSecurityTLS
if implicitTLS {
tlsConn := tls.Client(conn, tlsConf)
if err := tlsConn.HandshakeContext(ctx); err != nil {
if perr := mailtls.AsPinError(err, capture); perr != nil {
return pinFailure(perr)
}
return handshakeFailure(errx.ErrMailServerUnreachableAt("dial "+addr, err), "dial", err)
}
conn = tlsConn
}
// Use the resolved host: c.Credentials is nil for OAuth2-configured clients.
client, err := smtp.NewClient(conn, host)
if err != nil {
return sendFailure(errx.ErrMailServerUnreachable, err, "greeting", "mailbox")
}
defer client.Quit()
// Announce the sender's own domain. net/smtp says "localhost" when left
// alone, which relays read as a spam signal.
if name := ehloName(c.Email); name != "" {
if err := client.Hello(name); err != nil {
return sendFailure(errx.ErrMailServerUnreachable, err, "ehlo", "mailbox")
}
}
// TLS is mandatory everywhere but the loopback mode, which was already
// proved to be talking to this machine. STARTTLS is not attempted there
// even when the relay advertises it: a self-signed certificate no client
// can verify is exactly why the mode was chosen. The MAIL_TLS_INSECURE
// dev knob additionally allows a server with no STARTTLS at all (the
// local mailpit sink); it is honoured on self-hosted and development instances.
if !implicitTLS && resolved != models.MailSecurityNone {
if ok, _ := client.Extension("STARTTLS"); ok {
if err := client.StartTLS(tlsConf); err != nil {
if perr := mailtls.AsPinError(err, capture); perr != nil {
return pinFailure(perr)
}
return handshakeFailure(errx.ErrMailServerUnreachable, "starttls", err)
}
} else if !netbind.InsecureTLS() && !c.plaintext {
return sendFailure(errx.ErrMailServerUnreachableAt("starttls", errNoSTARTTLS), errNoSTARTTLS, "starttls", "mailbox")
}
}
// --- Auth ---
switch c.AuthType {
case models.AuthPlain:
// Negotiated, not assumed: a server that advertises only LOGIN
// rejects a blind AUTH PLAIN, and that rejection reads exactly like a
// wrong password, so the mailbox was deactivated over credentials
// that were correct.
// The normalized host, not the stored one: net/smtp records the name
// it was handed in NewClient as the server name, and PlainAuth
// refuses to authenticate when its own host does not match it. A
// bracketed IPv6 literal differs from the normalized form, so the
// stored string would fail on the address it is correct about.
auth, aerr := NegotiateAuth(client, c.Credentials.Username, c.Credentials.Password, host)
if errors.Is(aerr, ErrSMTPCleartextAuth) {
return errx.ErrMailCleartextAuth
}
if aerr != nil {
return errx.ErrMailAuthUnsupported
}
if auth != nil {
if err := client.Auth(auth); err != nil {
// Our own refusal to authenticate over an unencrypted link,
// raised before anything reaches the server. Retrying cannot
// encrypt it, and calling it an outage sends the operator
// looking at a server that is answering fine.
if errors.Is(err, ErrSMTPCleartextAuth) {
return errx.ErrMailCleartextAuth
}
// A 4xx is the server saying "not now" (rate-limited AUTH, a
// backend it cannot reach); only a 5xx means the credentials
// themselves are refused.
if !permanentReply(err) {
return sendFailure(errx.ErrMailServerUnreachable, err, "auth", "mailbox")
}
return sendFailure(errx.ErrMailInvalidCredentials, err, "auth", "mailbox")
}
}
case models.AuthOAuth2:
tk, err := c.Oauth2.Token.Token()
if err != nil {
var rErr *oauth2.RetrieveError
if errors.As(err, &rErr) {
if rErr.Response.StatusCode >= 500 {
return sendFailure(errx.ErrMailServerUnreachable, err, "oauth2 token", "mailbox")
}
}
return errx.ErrMailAuthenticationFailed
}
auth := newOAuth2Auth(c.Email, tk.AccessToken)
if err := client.Auth(auth); err != nil {
if !permanentReply(err) {
return sendFailure(errx.ErrMailServerUnreachable, err, "auth", "mailbox")
}
return sendFailure(errx.ErrMailAuthenticationFailed, err, "auth", "mailbox")
}
}
if err := client.Mail(from); err != nil {
// A refused MAIL FROM is how a blocked sender, an over-quota mailbox
// and a relay-denied policy arrive. Retrying a 5xx never succeeds and
// reports an outage that is not happening.
if permanentReply(err) {
if isDomainAuthRejection(err) {
return sendFailure(errx.ErrMailDomainAuthRejected, err, "mail from", "mailbox")
}
return sendFailure(errx.ErrMailSendRejected("The SMTP server refused the sender"), err, "mail from", "mailbox")
}
return sendFailure(errx.ErrMailServerUnreachable, err, "mail from", "mailbox")
}
for _, r := range to {
if err := client.Rcpt(r); err != nil {
// A domain-authentication refusal is about OUR domain, not this
// recipient; suppressing the address would punish the wrong party.
if permanentReply(err) && isDomainAuthRejection(err) {
return sendFailure(errx.ErrMailDomainAuthRejected, err, "rcpt to", "mailbox")
}
if permanentReply(err) && senderRefusal(err) {
return sendFailure(errx.ErrMailSendRejected("The SMTP server refused the sender"), err, "rcpt to", "mailbox")
}
// A refused RCPT is a recipient problem (bad address, policy
// rejection), not a dead server; classifying it as unreachable
// hid rejections from bounce accounting. A 4xx is greylisting or
// a busy server, which is worth another attempt.
if !permanentReply(err) {
return sendFailure(errx.ErrMailServerUnreachable, err, "rcpt to", "recipient")
}
refused := sendFailure(errx.ErrMailRecipientRejected("The SMTP server refused this recipient"), err, "rcpt to", "recipient")
refused.Recipient = r
return refused
}
}
w, err := client.Data()
if err != nil {
base := errx.ErrMailServerUnreachable
if permanentReply(err) {
base = errx.ErrMailSendRejected("The SMTP server refused the message")
}
return sendFailure(base, err, "data", "mailbox")
}
if _, err := w.Write(data); err != nil {
return sendFailure(errx.ErrMailServerUnreachable, err, "message body", "mailbox")
}
if err := w.Close(); err != nil {
// The server's verdict on the whole message lands here, which is where
// Microsoft returns 5.7.515. Retrying it as an outage never succeeds.
if permanentReply(err) && isDomainAuthRejection(err) {
return sendFailure(errx.ErrMailDomainAuthRejected, err, "message accept", "mailbox")
}
if permanentReply(err) {
return sendFailure(errx.ErrMailSendRejected("The SMTP server refused the message"), err, "message accept", "mailbox")
}
return sendFailure(errx.ErrMailServerUnreachable, err, "message accept", "mailbox")
}
return nil
}