[PATCH v2] do not check for STARTTLS support when talking to a loopback address.
Export this patch
---
commands/compose/send.go | 58 +++++++++++++++++++++++++---------------
1 file changed, 37 insertions(+), 21 deletions(-)
diff --git a/commands/compose/send.go b/commands/compose/send.go
index 849182d..d6fb32c 100644
--- a/commands/compose/send.go
+++ b/commands/compose/send.go
@@ -5,6 +5,7 @@ import (
"crypto/tls"
"fmt"
"io"
+ "net"
"net/url"
"os/exec"
"strings"
@@ -360,7 +361,7 @@ func newSmtpSender(ctx sendCtx) (io.WriteCloser, error) {
func connectSmtp(starttls bool, host string) (*smtp.Client, error) {
serverName := host
if !strings.ContainsRune(host, ':') {
- host = host + ":587" // Default to submission port
+ host += ":587" // Default to submission port
} else {
serverName = host[:strings.IndexRune(host, ':')]
}
@@ -368,27 +369,42 @@ func connectSmtp(starttls bool, host string) (*smtp.Client, error) {
if err != nil {
return nil, errors.Wrap(err, "smtp.Dial")
}
- if sup, _ := conn.Extension("STARTTLS"); sup {
- if !starttls {
- err := errors.New("STARTTLS is supported by this server, " +
- "but not set in accounts.conf. " +
- "Add smtp-starttls=yes")
- conn.Close()
- return nil, err
- }
- if err = conn.StartTLS(&tls.Config{
- ServerName: serverName,
- }); err != nil {
- conn.Close()
- return nil, errors.Wrap(err, "StartTLS")
+ // no way to get the chosen ip out of the smtp.conn; repeat DNS request
+ var localhost bool
+ if ips, err := net.LookupIP(serverName); err == nil {
+ for _, ip := range ips {
+ if ip.IsLoopback() {
+ localhost = true
+ break
+ }
}
- } else {
- if starttls {
- err := errors.New("STARTTLS requested, but not supported " +
- "by this SMTP server. Is someone tampering with your " +
- "connection?")
- conn.Close()
- return nil, err
+ }
+ if !localhost {
+ if sup, _ := conn.Extension("STARTTLS"); sup {
+ if !starttls {
+ err := errors.New("STARTTLS is supported " +
+ "by this non-localhost SMTP server, " +
+ "but not requested in accounts.conf. " +
+ "Add smtp-starttls=yes")
+ conn.Close()
+ return nil, err
+ }
+ if err = conn.StartTLS(&tls.Config{
+ ServerName: serverName,
+ }); err != nil {
+ conn.Close()
+ return nil, errors.Wrap(err, "StartTLS")
+ }
+ } else {
+ if starttls {
+ err := errors.New("STARTTLS requested " +
+ "in accounts.conf, but not supported " +
+ "by this non-localhost SMTP server. " +
+ "Is someone tampering with your " +
+ "connection?")
+ conn.Close()
+ return nil, err
+ }
}
}
return conn, nil
--
2.32.0