1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-23 11:49:19 +01:00
ntfy/server/smtp_server.go

332 lines
9 KiB
Go
Raw Normal View History

2021-12-27 16:39:28 +01:00
package server
import (
"bytes"
2023-02-19 15:39:04 +01:00
"encoding/base64"
2021-12-27 16:39:28 +01:00
"errors"
2022-06-01 02:38:56 +02:00
"fmt"
2021-12-27 16:39:28 +01:00
"github.com/emersion/go-smtp"
2023-04-02 19:59:26 +02:00
"github.com/microcosm-cc/bluemonday"
2021-12-27 16:39:28 +01:00
"io"
2021-12-28 01:26:20 +01:00
"mime"
"mime/multipart"
"mime/quotedprintable"
2022-06-02 05:24:44 +02:00
"net"
2022-06-01 02:38:56 +02:00
"net/http"
"net/http/httptest"
2021-12-27 16:39:28 +01:00
"net/mail"
2023-04-02 19:59:26 +02:00
"regexp"
2021-12-27 16:39:28 +01:00
"strings"
"sync"
)
2021-12-27 22:06:40 +01:00
var (
2021-12-28 01:26:20 +01:00
errInvalidDomain = errors.New("invalid domain")
errInvalidAddress = errors.New("invalid address")
errInvalidTopic = errors.New("invalid topic")
errTooManyRecipients = errors.New("too many recipients")
errMultipartNestedTooDeep = errors.New("multipart message nested too deep")
2021-12-28 01:26:20 +01:00
errUnsupportedContentType = errors.New("unsupported content type")
2021-12-27 22:06:40 +01:00
)
2023-11-16 15:49:35 +01:00
var (
onlySpacesRegex = regexp.MustCompile(`(?m)^\s+$`)
consecutiveNewLinesRegex = regexp.MustCompile(`\n{3,}`)
)
const (
maxMultipartDepth = 2
)
2021-12-27 16:39:28 +01:00
// smtpBackend implements SMTP server methods.
type smtpBackend struct {
2021-12-27 22:06:40 +01:00
config *Config
2022-06-01 02:38:56 +02:00
handler func(http.ResponseWriter, *http.Request)
2021-12-27 22:06:40 +01:00
success int64
failure int64
mu sync.Mutex
2021-12-27 16:39:28 +01:00
}
2023-01-04 15:31:32 +01:00
var _ smtp.Backend = (*smtpBackend)(nil)
var _ smtp.Session = (*smtpSession)(nil)
2022-06-01 02:38:56 +02:00
func newMailBackend(conf *Config, handler func(http.ResponseWriter, *http.Request)) *smtpBackend {
2021-12-27 16:39:28 +01:00
return &smtpBackend{
2022-06-01 02:38:56 +02:00
config: conf,
handler: handler,
2021-12-27 16:39:28 +01:00
}
}
2023-01-04 15:31:32 +01:00
func (b *smtpBackend) NewSession(conn *smtp.Conn) (smtp.Session, error) {
2023-02-15 03:22:46 +01:00
logem(conn).Debug("Incoming mail")
2023-01-04 15:31:32 +01:00
return &smtpSession{backend: b, conn: conn}, nil
2021-12-27 22:06:40 +01:00
}
2022-06-02 05:24:44 +02:00
func (b *smtpBackend) Counts() (total int64, success int64, failure int64) {
2021-12-27 22:06:40 +01:00
b.mu.Lock()
defer b.mu.Unlock()
2022-06-02 05:24:44 +02:00
return b.success + b.failure, b.success, b.failure
2021-12-27 16:39:28 +01:00
}
// smtpSession is returned after EHLO.
type smtpSession struct {
2022-06-02 05:24:44 +02:00
backend *smtpBackend
2023-01-04 15:31:32 +01:00
conn *smtp.Conn
2022-06-02 05:24:44 +02:00
topic string
token string
2022-06-02 05:24:44 +02:00
mu sync.Mutex
2021-12-27 16:39:28 +01:00
}
2023-01-04 15:31:32 +01:00
func (s *smtpSession) AuthPlain(username, _ string) error {
2023-02-15 03:22:46 +01:00
logem(s.conn).Field("smtp_username", username).Debug("AUTH PLAIN (with username %s)", username)
2021-12-27 16:39:28 +01:00
return nil
}
2023-01-04 15:31:32 +01:00
func (s *smtpSession) Mail(from string, opts *smtp.MailOptions) error {
2023-02-15 03:22:46 +01:00
logem(s.conn).Field("smtp_mail_from", from).Debug("MAIL FROM: %s", from)
2021-12-27 16:39:28 +01:00
return nil
}
func (s *smtpSession) Rcpt(to string) error {
2023-02-15 03:22:46 +01:00
logem(s.conn).Field("smtp_rcpt_to", to).Debug("RCPT TO: %s", to)
2021-12-27 22:06:40 +01:00
return s.withFailCount(func() error {
token := ""
2021-12-27 22:06:40 +01:00
conf := s.backend.config
addressList, err := mail.ParseAddressList(to)
if err != nil {
return err
} else if len(addressList) != 1 {
return errTooManyRecipients
}
to = addressList[0].Address
if !strings.HasSuffix(to, "@"+conf.SMTPServerDomain) {
return errInvalidDomain
}
2023-02-20 21:48:34 +01:00
// Remove @ntfy.sh from end of email
2021-12-27 22:06:40 +01:00
to = strings.TrimSuffix(to, "@"+conf.SMTPServerDomain)
if conf.SMTPServerAddrPrefix != "" {
if !strings.HasPrefix(to, conf.SMTPServerAddrPrefix) {
return errInvalidAddress
}
// remove ntfy- from beginning of email
2021-12-27 22:06:40 +01:00
to = strings.TrimPrefix(to, conf.SMTPServerAddrPrefix)
}
2023-02-20 21:48:34 +01:00
// If email contains token, split topic and token
if strings.Contains(to, "+") {
parts := strings.Split(to, "+")
to = parts[0]
token = parts[1]
}
2021-12-27 22:06:40 +01:00
if !topicRegex.MatchString(to) {
return errInvalidTopic
}
s.mu.Lock()
s.topic = to
s.token = token
2021-12-27 22:06:40 +01:00
s.mu.Unlock()
return nil
})
2021-12-27 16:39:28 +01:00
}
func (s *smtpSession) Data(r io.Reader) error {
2021-12-27 22:06:40 +01:00
return s.withFailCount(func() error {
2021-12-28 01:26:20 +01:00
conf := s.backend.config
2021-12-27 22:06:40 +01:00
b, err := io.ReadAll(r) // Protected by MaxMessageBytes
if err != nil {
return err
}
2023-02-15 03:22:46 +01:00
ev := logem(s.conn)
2023-02-04 04:21:50 +01:00
if ev.IsTrace() {
ev.Field("smtp_data", string(b)).Trace("DATA")
} else if ev.IsDebug() {
2023-02-15 03:22:46 +01:00
ev.Field("smtp_data_len", len(b)).Debug("DATA")
2022-06-02 05:24:44 +02:00
}
2021-12-27 22:06:40 +01:00
msg, err := mail.ReadMessage(bytes.NewReader(b))
if err != nil {
return err
}
body, err := readMailBody(msg.Body, msg.Header)
2021-12-27 22:06:40 +01:00
if err != nil {
return err
}
body = strings.TrimSpace(body)
2024-03-07 17:53:12 +01:00
if len(body) > conf.MessageSizeLimit {
body = body[:conf.MessageSizeLimit]
2021-12-28 01:26:20 +01:00
}
m := newDefaultMessage(s.topic, body)
subject := strings.TrimSpace(msg.Header.Get("Subject"))
2021-12-27 22:06:40 +01:00
if subject != "" {
2021-12-28 01:26:20 +01:00
dec := mime.WordDecoder{}
subject, err := dec.DecodeHeader(subject)
if err != nil {
return err
}
2021-12-27 22:06:40 +01:00
m.Title = subject
}
if m.Title != "" && m.Message == "" {
m.Message = m.Title // Flip them, this makes more sense
m.Title = ""
}
2022-06-01 02:38:56 +02:00
if err := s.publishMessage(m); err != nil {
2021-12-27 22:06:40 +01:00
return err
}
s.backend.mu.Lock()
s.backend.success++
s.backend.mu.Unlock()
minc(metricEmailsReceivedSuccess)
2021-12-27 22:06:40 +01:00
return nil
})
2021-12-27 16:39:28 +01:00
}
2022-06-01 02:38:56 +02:00
func (s *smtpSession) publishMessage(m *message) error {
2022-06-02 05:24:44 +02:00
// Extract remote address (for rate limiting)
2023-01-04 15:31:32 +01:00
remoteAddr, _, err := net.SplitHostPort(s.conn.Conn().RemoteAddr().String())
2022-06-02 05:24:44 +02:00
if err != nil {
2023-01-04 15:31:32 +01:00
remoteAddr = s.conn.Conn().RemoteAddr().String()
2022-06-02 05:24:44 +02:00
}
// Call HTTP handler with fake HTTP request
2022-06-01 02:38:56 +02:00
url := fmt.Sprintf("%s/%s", s.backend.config.BaseURL, m.Topic)
2022-06-02 05:24:44 +02:00
req, err := http.NewRequest("POST", url, strings.NewReader(m.Message))
req.RequestURI = "/" + m.Topic // just for the logs
req.RemoteAddr = remoteAddr // rate limiting!!
req.Header.Set("X-Forwarded-For", remoteAddr)
2022-06-01 02:38:56 +02:00
if err != nil {
return err
}
if m.Title != "" {
req.Header.Set("Title", m.Title)
}
if s.token != "" {
req.Header.Add("Authorization", "Bearer "+s.token)
}
2022-06-01 02:38:56 +02:00
rr := httptest.NewRecorder()
s.backend.handler(rr, req)
if rr.Code != http.StatusOK {
return errors.New("error: " + rr.Body.String())
}
return nil
}
2021-12-27 16:39:28 +01:00
func (s *smtpSession) Reset() {
s.mu.Lock()
2021-12-27 22:06:40 +01:00
s.topic = ""
2021-12-27 16:39:28 +01:00
s.mu.Unlock()
}
func (s *smtpSession) Logout() error {
return nil
}
2021-12-27 22:06:40 +01:00
func (s *smtpSession) withFailCount(fn func() error) error {
err := fn()
s.backend.mu.Lock()
defer s.backend.mu.Unlock()
if err != nil {
2022-06-02 05:24:44 +02:00
// Almost all of these errors are parse errors, and user input errors.
// We do not want to spam the log with WARN messages.
2023-02-15 03:22:46 +01:00
logem(s.conn).Err(err).Debug("Incoming mail error")
2021-12-27 22:06:40 +01:00
s.backend.failure++
minc(metricEmailsReceivedFailure)
2021-12-27 22:06:40 +01:00
}
return err
}
2021-12-28 01:26:20 +01:00
func readMailBody(body io.Reader, header mail.Header) (string, error) {
if header.Get("Content-Type") == "" {
return readPlainTextMailBody(body, header.Get("Content-Transfer-Encoding"))
}
contentType, params, err := mime.ParseMediaType(header.Get("Content-Type"))
2021-12-28 01:26:20 +01:00
if err != nil {
return "", err
}
2023-04-02 19:59:26 +02:00
canonicalContentType := strings.ToLower(contentType)
if canonicalContentType == "text/plain" || canonicalContentType == "text/html" {
return readTextMailBody(body, canonicalContentType, header.Get("Content-Transfer-Encoding"))
} else if strings.HasPrefix(canonicalContentType, "multipart/") {
return readMultipartMailBody(body, params)
}
return "", errUnsupportedContentType
}
2023-04-02 19:59:26 +02:00
func readMultipartMailBody(body io.Reader, params map[string]string) (string, error) {
parts := make(map[string]string)
if err := readMultipartMailBodyParts(body, params, 0, parts); err != nil && err != io.EOF {
return "", err
} else if s, ok := parts["text/plain"]; ok {
return s, nil
} else if s, ok := parts["text/html"]; ok {
return s, nil
}
return "", io.EOF
}
func readMultipartMailBodyParts(body io.Reader, params map[string]string, depth int, parts map[string]string) error {
if depth >= maxMultipartDepth {
2023-04-02 19:59:26 +02:00
return errMultipartNestedTooDeep
}
mr := multipart.NewReader(body, params["boundary"])
for {
part, err := mr.NextPart()
if err != nil { // may be io.EOF
2023-04-02 19:59:26 +02:00
return err
}
partContentType, partParams, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
2021-12-28 01:26:20 +01:00
if err != nil {
2023-04-02 19:59:26 +02:00
return err
}
2023-04-02 19:59:26 +02:00
canonicalPartContentType := strings.ToLower(partContentType)
if canonicalPartContentType == "text/plain" || canonicalPartContentType == "text/html" {
s, err := readTextMailBody(part, canonicalPartContentType, part.Header.Get("Content-Transfer-Encoding"))
if err != nil {
return err
}
parts[canonicalPartContentType] = s
} else if strings.HasPrefix(strings.ToLower(partContentType), "multipart/") {
2023-04-02 19:59:26 +02:00
if err := readMultipartMailBodyParts(part, partParams, depth+1, parts); err != nil {
return err
}
}
// Continue with next part
2023-02-19 15:39:04 +01:00
}
}
2023-04-02 19:59:26 +02:00
func readTextMailBody(reader io.Reader, contentType, transferEncoding string) (string, error) {
if contentType == "text/plain" {
return readPlainTextMailBody(reader, transferEncoding)
} else if contentType == "text/html" {
return readHTMLMailBody(reader, transferEncoding)
}
return "", fmt.Errorf("unsupported content type: %s", contentType)
}
2023-02-19 15:39:04 +01:00
func readPlainTextMailBody(reader io.Reader, transferEncoding string) (string, error) {
if strings.ToLower(transferEncoding) == "base64" {
reader = base64.NewDecoder(base64.StdEncoding, reader)
} else if strings.ToLower(transferEncoding) == "quoted-printable" {
reader = quotedprintable.NewReader(reader)
2021-12-28 01:26:20 +01:00
}
2023-02-19 15:39:04 +01:00
body, err := io.ReadAll(reader)
if err != nil {
return "", err
}
return string(body), nil
2021-12-28 01:26:20 +01:00
}
2023-04-02 19:59:26 +02:00
func readHTMLMailBody(reader io.Reader, transferEncoding string) (string, error) {
body, err := readPlainTextMailBody(reader, transferEncoding)
if err != nil {
return "", err
}
stripped := bluemonday.
StrictPolicy().
AddSpaceWhenStrippingTag(true).
Sanitize(body)
return removeExtraEmptyLines(stripped), nil
}
2023-11-16 15:49:35 +01:00
func removeExtraEmptyLines(s string) string {
s = onlySpacesRegex.ReplaceAllString(s, "")
s = consecutiveNewLinesRegex.ReplaceAllString(s, "\n\n")
return s
2023-04-02 19:59:26 +02:00
}