Skip to content

Commit

Permalink
Merge pull request #2049 from slingamn/implicittls.1
Browse files Browse the repository at this point in the history
support implicit TLS for mail submission agents
  • Loading branch information
slingamn authored Feb 19, 2023
2 parents 3d1412a + 7c53b94 commit 3fca52b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ accounts:
# port: 25
# username: "admin"
# password: "hunter2"
# implicit-tls: false # TLS from the first byte, typically on port 465
blacklist-regexes:
# - ".*@mailinator.com"
timeout: 60s
Expand Down
16 changes: 11 additions & 5 deletions irc/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ var (
)

type MTAConfig struct {
Server string
Port int
Username string
Password string
Server string
Port int
Username string
Password string
ImplicitTLS bool `yaml:"implicit-tls"`
}

type MailtoConfig struct {
Expand Down Expand Up @@ -132,11 +133,13 @@ func SendMail(config MailtoConfig, recipient string, msg []byte) (err error) {

var addr string
var auth smtp.Auth
var implicitTLS bool
if !config.DirectSendingEnabled() {
addr = fmt.Sprintf("%s:%d", config.MTAReal.Server, config.MTAReal.Port)
if config.MTAReal.Username != "" && config.MTAReal.Password != "" {
auth = smtp.PlainAuth("", config.MTAReal.Username, config.MTAReal.Password, config.MTAReal.Server)
}
implicitTLS = config.MTAReal.ImplicitTLS
} else {
idx := strings.IndexByte(recipient, '@')
if idx == -1 {
Expand All @@ -149,5 +152,8 @@ func SendMail(config MailtoConfig, recipient string, msg []byte) (err error) {
addr = fmt.Sprintf("%s:smtp", mx)
}

return smtp.SendMail(addr, auth, config.HeloDomain, config.Sender, []string{recipient}, msg, config.RequireTLS, config.Timeout)
return smtp.SendMail(
addr, auth, config.HeloDomain, config.Sender, []string{recipient}, msg,
config.RequireTLS, implicitTLS, config.Timeout,
)
}
49 changes: 27 additions & 22 deletions irc/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ type Client struct {

// Dial returns a new Client connected to an SMTP server at addr.
// The addr must include a port, as in "mail.example.com:smtp".
func Dial(addr string, timeout time.Duration) (*Client, error) {
func Dial(addr string, timeout time.Duration, implicitTLS bool) (*Client, error) {
var conn net.Conn
var err error
dialer := net.Dialer{
Timeout: timeout,
}
start := time.Now()
if timeout == 0 {
conn, err = net.Dial("tcp", addr)
if !implicitTLS {
conn, err = dialer.Dial("tcp", addr)
} else {
conn, err = net.DialTimeout("tcp", addr, timeout)
conn, err = tls.DialWithDialer(&dialer, "tcp", addr, nil)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -338,7 +341,7 @@ var testHookStartTLS func(*tls.Config) // nil, except for tests
// functionality. Higher-level packages exist outside of the standard
// library.
// XXX: modified in Ergo to add `requireTLS`, `heloDomain`, and `timeout` arguments
func SendMail(addr string, a Auth, heloDomain string, from string, to []string, msg []byte, requireTLS bool, timeout time.Duration) error {
func SendMail(addr string, a Auth, heloDomain string, from string, to []string, msg []byte, requireTLS, implicitTLS bool, timeout time.Duration) error {
if err := validateLine(from); err != nil {
return err
}
Expand All @@ -347,31 +350,33 @@ func SendMail(addr string, a Auth, heloDomain string, from string, to []string,
return err
}
}
c, err := Dial(addr, timeout)
c, err := Dial(addr, timeout, implicitTLS)
if err != nil {
return err
}
defer c.Close()
if err = c.Hello(heloDomain); err != nil {
return err
}
if ok, _ := c.Extension("STARTTLS"); ok {
var config *tls.Config
if requireTLS {
config = &tls.Config{ServerName: c.serverName}
} else {
// if TLS isn't a hard requirement, don't verify the certificate either,
// since a MITM attacker could just remove the STARTTLS advertisement
config = &tls.Config{InsecureSkipVerify: true}
}
if testHookStartTLS != nil {
testHookStartTLS(config)
}
if err = c.StartTLS(config); err != nil {
return err
if !implicitTLS {
if ok, _ := c.Extension("STARTTLS"); ok {
var config *tls.Config
if requireTLS {
config = &tls.Config{ServerName: c.serverName}
} else {
// if TLS isn't a hard requirement, don't verify the certificate either,
// since a MITM attacker could just remove the STARTTLS advertisement
config = &tls.Config{InsecureSkipVerify: true}
}
if testHookStartTLS != nil {
testHookStartTLS(config)
}
if err = c.StartTLS(config); err != nil {
return err
}
} else if requireTLS {
return errors.New("TLS required, but not negotiated")
}
} else if requireTLS {
return errors.New("TLS required, but not negotiated")
}
if a != nil && c.ext != nil {
if _, ok := c.ext["AUTH"]; !ok {
Expand Down
1 change: 1 addition & 0 deletions traditional.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ accounts:
# port: 25
# username: "admin"
# password: "hunter2"
# implicit-tls: false # TLS from the first byte, typically on port 465
blacklist-regexes:
# - ".*@mailinator.com"
timeout: 60s
Expand Down

0 comments on commit 3fca52b

Please sign in to comment.