2020-04-05 09:48:59 +02:00
|
|
|
// Copyright (c) 2020 Shivaram Lingamneni
|
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
2023-09-12 07:06:55 +02:00
|
|
|
"bufio"
|
2021-08-26 04:32:55 +02:00
|
|
|
"bytes"
|
2020-04-05 09:48:59 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-09-12 07:06:55 +02:00
|
|
|
"io"
|
2020-04-05 09:48:59 +02:00
|
|
|
"net"
|
2023-09-12 07:06:55 +02:00
|
|
|
"os"
|
2020-04-05 09:48:59 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2021-07-07 13:12:15 +02:00
|
|
|
"time"
|
2020-04-05 09:48:59 +02:00
|
|
|
|
2021-08-26 04:32:55 +02:00
|
|
|
"github.com/ergochat/ergo/irc/custime"
|
2021-05-25 06:34:38 +02:00
|
|
|
"github.com/ergochat/ergo/irc/smtp"
|
2021-08-26 04:32:55 +02:00
|
|
|
"github.com/ergochat/ergo/irc/utils"
|
2020-04-05 09:48:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrBlacklistedAddress = errors.New("Email address is blacklisted")
|
2021-07-07 12:35:30 +02:00
|
|
|
ErrInvalidAddress = errors.New("Email address is invalid")
|
2020-04-05 09:48:59 +02:00
|
|
|
ErrNoMXRecord = errors.New("Couldn't resolve MX record")
|
|
|
|
)
|
|
|
|
|
2023-09-12 07:06:55 +02:00
|
|
|
type BlacklistSyntax uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
BlacklistSyntaxGlob BlacklistSyntax = iota
|
|
|
|
BlacklistSyntaxRegexp
|
|
|
|
)
|
|
|
|
|
|
|
|
func blacklistSyntaxFromString(status string) (BlacklistSyntax, error) {
|
|
|
|
switch strings.ToLower(status) {
|
|
|
|
case "glob", "":
|
|
|
|
return BlacklistSyntaxGlob, nil
|
|
|
|
case "re", "regex", "regexp":
|
|
|
|
return BlacklistSyntaxRegexp, nil
|
|
|
|
default:
|
|
|
|
return BlacklistSyntaxRegexp, fmt.Errorf("Unknown blacklist syntax type `%s`", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BlacklistSyntax) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var orig string
|
|
|
|
var err error
|
|
|
|
if err = unmarshal(&orig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if result, err := blacklistSyntaxFromString(orig); err == nil {
|
|
|
|
*bs = result
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 09:48:59 +02:00
|
|
|
type MTAConfig struct {
|
2023-02-17 06:07:21 +01:00
|
|
|
Server string
|
|
|
|
Port int
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
ImplicitTLS bool `yaml:"implicit-tls"`
|
2020-04-05 09:48:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type MailtoConfig struct {
|
|
|
|
// legacy config format assumed the use of an MTA/smarthost,
|
|
|
|
// so server, port, etc. appear directly at top level
|
|
|
|
// XXX: see https://github.com/go-yaml/yaml/issues/63
|
2023-09-12 07:06:55 +02:00
|
|
|
MTAConfig `yaml:",inline"`
|
|
|
|
Enabled bool
|
|
|
|
Sender string
|
|
|
|
HeloDomain string `yaml:"helo-domain"`
|
|
|
|
RequireTLS bool `yaml:"require-tls"`
|
|
|
|
VerifyMessageSubject string `yaml:"verify-message-subject"`
|
|
|
|
DKIM DKIMConfig
|
|
|
|
MTAReal MTAConfig `yaml:"mta"`
|
|
|
|
AddressBlacklist []string `yaml:"address-blacklist"`
|
|
|
|
AddressBlacklistSyntax BlacklistSyntax `yaml:"address-blacklist-syntax"`
|
|
|
|
AddressBlacklistFile string `yaml:"address-blacklist-file"`
|
|
|
|
blacklistRegexes []*regexp.Regexp
|
|
|
|
Timeout time.Duration
|
|
|
|
PasswordReset struct {
|
2021-08-26 04:32:55 +02:00
|
|
|
Enabled bool
|
|
|
|
Cooldown custime.Duration
|
|
|
|
Timeout custime.Duration
|
|
|
|
} `yaml:"password-reset"`
|
2020-04-05 09:48:59 +02:00
|
|
|
}
|
|
|
|
|
2023-09-12 07:06:55 +02:00
|
|
|
func (config *MailtoConfig) compileBlacklistEntry(source string) (re *regexp.Regexp, err error) {
|
|
|
|
if config.AddressBlacklistSyntax == BlacklistSyntaxGlob {
|
|
|
|
return utils.CompileGlob(source, false)
|
|
|
|
} else {
|
|
|
|
return regexp.Compile(fmt.Sprintf("^%s$", source))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *MailtoConfig) processBlacklistFile(filename string) (result []*regexp.Regexp, err error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
reader := bufio.NewReader(f)
|
|
|
|
lineNo := 0
|
|
|
|
for {
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
lineNo++
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line != "" && line[0] != '#' {
|
|
|
|
if compiled, compileErr := config.compileBlacklistEntry(line); compileErr == nil {
|
|
|
|
result = append(result, compiled)
|
|
|
|
} else {
|
|
|
|
return result, fmt.Errorf("Failed to compile line %d of blacklist-regex-file `%s`: %w", lineNo, line, compileErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch err {
|
|
|
|
case io.EOF:
|
|
|
|
return result, nil
|
|
|
|
case nil:
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 09:48:59 +02:00
|
|
|
func (config *MailtoConfig) Postprocess(heloDomain string) (err error) {
|
|
|
|
if config.Sender == "" {
|
|
|
|
return errors.New("Invalid mailto sender address")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for MTA config fields at top level,
|
|
|
|
// copy to MTAReal if present
|
|
|
|
if config.Server != "" && config.MTAReal.Server == "" {
|
|
|
|
config.MTAReal = config.MTAConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.HeloDomain == "" {
|
|
|
|
config.HeloDomain = heloDomain
|
|
|
|
}
|
|
|
|
|
2023-09-12 07:06:55 +02:00
|
|
|
if config.AddressBlacklistFile != "" {
|
|
|
|
config.blacklistRegexes, err = config.processBlacklistFile(config.AddressBlacklistFile)
|
2020-04-05 09:48:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-12 07:06:55 +02:00
|
|
|
} else if len(config.AddressBlacklist) != 0 {
|
|
|
|
config.blacklistRegexes = make([]*regexp.Regexp, 0, len(config.AddressBlacklist))
|
|
|
|
for _, reg := range config.AddressBlacklist {
|
|
|
|
compiled, err := config.compileBlacklistEntry(reg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.blacklistRegexes = append(config.blacklistRegexes, compiled)
|
|
|
|
}
|
2020-04-05 09:48:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.MTAConfig.Server != "" {
|
|
|
|
// smarthost, nothing more to validate
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.DKIM.Postprocess()
|
|
|
|
}
|
|
|
|
|
2021-07-07 12:35:30 +02:00
|
|
|
// are we sending email directly, as opposed to deferring to an MTA?
|
|
|
|
func (config *MailtoConfig) DirectSendingEnabled() bool {
|
|
|
|
return config.MTAReal.Server == ""
|
|
|
|
}
|
|
|
|
|
2020-04-05 09:48:59 +02:00
|
|
|
// get the preferred MX record hostname, "" on error
|
|
|
|
func lookupMX(domain string) (server string) {
|
|
|
|
var minPref uint16
|
|
|
|
results, err := net.LookupMX(domain)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, result := range results {
|
|
|
|
if minPref == 0 || result.Pref < minPref {
|
|
|
|
server, minPref = result.Host, result.Pref
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-26 04:32:55 +02:00
|
|
|
func ComposeMail(config MailtoConfig, recipient, subject string) (message bytes.Buffer) {
|
|
|
|
fmt.Fprintf(&message, "From: %s\r\n", config.Sender)
|
|
|
|
fmt.Fprintf(&message, "To: %s\r\n", recipient)
|
|
|
|
dkimDomain := config.DKIM.Domain
|
|
|
|
if dkimDomain != "" {
|
|
|
|
fmt.Fprintf(&message, "Message-ID: <%s@%s>\r\n", utils.GenerateSecretKey(), dkimDomain)
|
2023-12-21 04:18:48 +01:00
|
|
|
} else {
|
|
|
|
// #2108: send Message-ID even if dkim is not enabled
|
|
|
|
fmt.Fprintf(&message, "Message-ID: <%s-%s>\r\n", utils.GenerateSecretKey(), config.Sender)
|
2021-08-26 04:32:55 +02:00
|
|
|
}
|
|
|
|
fmt.Fprintf(&message, "Date: %s\r\n", time.Now().UTC().Format(time.RFC1123Z))
|
|
|
|
fmt.Fprintf(&message, "Subject: %s\r\n", subject)
|
|
|
|
message.WriteString("\r\n") // blank line: end headers, begin message body
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
2020-04-05 09:48:59 +02:00
|
|
|
func SendMail(config MailtoConfig, recipient string, msg []byte) (err error) {
|
2023-09-12 07:06:55 +02:00
|
|
|
recipientLower := strings.ToLower(recipient)
|
2020-04-05 09:48:59 +02:00
|
|
|
for _, reg := range config.blacklistRegexes {
|
2023-09-12 07:06:55 +02:00
|
|
|
if reg.MatchString(recipientLower) {
|
2020-04-05 09:48:59 +02:00
|
|
|
return ErrBlacklistedAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.DKIM.Domain != "" {
|
|
|
|
msg, err = DKIMSign(msg, config.DKIM)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr string
|
|
|
|
var auth smtp.Auth
|
2023-02-17 06:07:21 +01:00
|
|
|
var implicitTLS bool
|
2021-07-07 12:35:30 +02:00
|
|
|
if !config.DirectSendingEnabled() {
|
2020-04-05 09:48:59 +02:00
|
|
|
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)
|
|
|
|
}
|
2023-02-17 06:07:21 +01:00
|
|
|
implicitTLS = config.MTAReal.ImplicitTLS
|
2020-04-05 09:48:59 +02:00
|
|
|
} else {
|
|
|
|
idx := strings.IndexByte(recipient, '@')
|
|
|
|
if idx == -1 {
|
|
|
|
return ErrInvalidAddress
|
|
|
|
}
|
|
|
|
mx := lookupMX(recipient[idx+1:])
|
|
|
|
if mx == "" {
|
|
|
|
return ErrNoMXRecord
|
|
|
|
}
|
|
|
|
addr = fmt.Sprintf("%s:smtp", mx)
|
|
|
|
}
|
|
|
|
|
2023-02-17 06:07:21 +01:00
|
|
|
return smtp.SendMail(
|
|
|
|
addr, auth, config.HeloDomain, config.Sender, []string{recipient}, msg,
|
|
|
|
config.RequireTLS, implicitTLS, config.Timeout,
|
|
|
|
)
|
2020-04-05 09:48:59 +02:00
|
|
|
}
|