smtp_server.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package server
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "github.com/emersion/go-smtp"
  8. "io"
  9. "mime"
  10. "mime/multipart"
  11. "net"
  12. "net/http"
  13. "net/http/httptest"
  14. "net/mail"
  15. "strings"
  16. "sync"
  17. )
  18. var (
  19. errInvalidDomain = errors.New("invalid domain")
  20. errInvalidAddress = errors.New("invalid address")
  21. errInvalidTopic = errors.New("invalid topic")
  22. errTooManyRecipients = errors.New("too many recipients")
  23. errMultipartNestedTooDeep = errors.New("multipart message nested too deep")
  24. errUnsupportedContentType = errors.New("unsupported content type")
  25. )
  26. const (
  27. maxMultipartDepth = 2
  28. )
  29. // smtpBackend implements SMTP server methods.
  30. type smtpBackend struct {
  31. config *Config
  32. handler func(http.ResponseWriter, *http.Request)
  33. success int64
  34. failure int64
  35. mu sync.Mutex
  36. }
  37. var _ smtp.Backend = (*smtpBackend)(nil)
  38. var _ smtp.Session = (*smtpSession)(nil)
  39. func newMailBackend(conf *Config, handler func(http.ResponseWriter, *http.Request)) *smtpBackend {
  40. return &smtpBackend{
  41. config: conf,
  42. handler: handler,
  43. }
  44. }
  45. func (b *smtpBackend) NewSession(conn *smtp.Conn) (smtp.Session, error) {
  46. logem(conn).Debug("Incoming mail")
  47. return &smtpSession{backend: b, conn: conn}, nil
  48. }
  49. func (b *smtpBackend) Counts() (total int64, success int64, failure int64) {
  50. b.mu.Lock()
  51. defer b.mu.Unlock()
  52. return b.success + b.failure, b.success, b.failure
  53. }
  54. // smtpSession is returned after EHLO.
  55. type smtpSession struct {
  56. backend *smtpBackend
  57. conn *smtp.Conn
  58. topic string
  59. mu sync.Mutex
  60. }
  61. func (s *smtpSession) AuthPlain(username, _ string) error {
  62. logem(s.conn).Field("smtp_username", username).Debug("AUTH PLAIN (with username %s)", username)
  63. return nil
  64. }
  65. func (s *smtpSession) Mail(from string, opts *smtp.MailOptions) error {
  66. logem(s.conn).Field("smtp_mail_from", from).Debug("MAIL FROM: %s", from)
  67. return nil
  68. }
  69. func (s *smtpSession) Rcpt(to string) error {
  70. logem(s.conn).Field("smtp_rcpt_to", to).Debug("RCPT TO: %s", to)
  71. return s.withFailCount(func() error {
  72. conf := s.backend.config
  73. addressList, err := mail.ParseAddressList(to)
  74. if err != nil {
  75. return err
  76. } else if len(addressList) != 1 {
  77. return errTooManyRecipients
  78. }
  79. to = addressList[0].Address
  80. if !strings.HasSuffix(to, "@"+conf.SMTPServerDomain) {
  81. return errInvalidDomain
  82. }
  83. to = strings.TrimSuffix(to, "@"+conf.SMTPServerDomain)
  84. if conf.SMTPServerAddrPrefix != "" {
  85. if !strings.HasPrefix(to, conf.SMTPServerAddrPrefix) {
  86. return errInvalidAddress
  87. }
  88. to = strings.TrimPrefix(to, conf.SMTPServerAddrPrefix)
  89. }
  90. if !topicRegex.MatchString(to) {
  91. return errInvalidTopic
  92. }
  93. s.mu.Lock()
  94. s.topic = to
  95. s.mu.Unlock()
  96. return nil
  97. })
  98. }
  99. func (s *smtpSession) Data(r io.Reader) error {
  100. return s.withFailCount(func() error {
  101. conf := s.backend.config
  102. b, err := io.ReadAll(r) // Protected by MaxMessageBytes
  103. if err != nil {
  104. return err
  105. }
  106. ev := logem(s.conn)
  107. if ev.IsTrace() {
  108. ev.Field("smtp_data", string(b)).Trace("DATA")
  109. } else if ev.IsDebug() {
  110. ev.Field("smtp_data_len", len(b)).Debug("DATA")
  111. }
  112. msg, err := mail.ReadMessage(bytes.NewReader(b))
  113. if err != nil {
  114. return err
  115. }
  116. body, err := readMailBody(msg.Body, msg.Header)
  117. if err != nil {
  118. return err
  119. }
  120. body = strings.TrimSpace(body)
  121. if len(body) > conf.MessageLimit {
  122. body = body[:conf.MessageLimit]
  123. }
  124. m := newDefaultMessage(s.topic, body)
  125. subject := strings.TrimSpace(msg.Header.Get("Subject"))
  126. if subject != "" {
  127. dec := mime.WordDecoder{}
  128. subject, err := dec.DecodeHeader(subject)
  129. if err != nil {
  130. return err
  131. }
  132. m.Title = subject
  133. }
  134. if m.Title != "" && m.Message == "" {
  135. m.Message = m.Title // Flip them, this makes more sense
  136. m.Title = ""
  137. }
  138. if err := s.publishMessage(m); err != nil {
  139. return err
  140. }
  141. s.backend.mu.Lock()
  142. s.backend.success++
  143. s.backend.mu.Unlock()
  144. return nil
  145. })
  146. }
  147. func (s *smtpSession) publishMessage(m *message) error {
  148. // Extract remote address (for rate limiting)
  149. remoteAddr, _, err := net.SplitHostPort(s.conn.Conn().RemoteAddr().String())
  150. if err != nil {
  151. remoteAddr = s.conn.Conn().RemoteAddr().String()
  152. }
  153. // Call HTTP handler with fake HTTP request
  154. url := fmt.Sprintf("%s/%s", s.backend.config.BaseURL, m.Topic)
  155. req, err := http.NewRequest("POST", url, strings.NewReader(m.Message))
  156. req.RequestURI = "/" + m.Topic // just for the logs
  157. req.RemoteAddr = remoteAddr // rate limiting!!
  158. req.Header.Set("X-Forwarded-For", remoteAddr)
  159. if err != nil {
  160. return err
  161. }
  162. if m.Title != "" {
  163. req.Header.Set("Title", m.Title)
  164. }
  165. rr := httptest.NewRecorder()
  166. s.backend.handler(rr, req)
  167. if rr.Code != http.StatusOK {
  168. return errors.New("error: " + rr.Body.String())
  169. }
  170. return nil
  171. }
  172. func (s *smtpSession) Reset() {
  173. s.mu.Lock()
  174. s.topic = ""
  175. s.mu.Unlock()
  176. }
  177. func (s *smtpSession) Logout() error {
  178. return nil
  179. }
  180. func (s *smtpSession) withFailCount(fn func() error) error {
  181. err := fn()
  182. s.backend.mu.Lock()
  183. defer s.backend.mu.Unlock()
  184. if err != nil {
  185. // Almost all of these errors are parse errors, and user input errors.
  186. // We do not want to spam the log with WARN messages.
  187. logem(s.conn).Err(err).Debug("Incoming mail error")
  188. s.backend.failure++
  189. }
  190. return err
  191. }
  192. func readMailBody(body io.Reader, header mail.Header) (string, error) {
  193. if header.Get("Content-Type") == "" {
  194. return readPlainTextMailBody(body, header.Get("Content-Transfer-Encoding"))
  195. }
  196. contentType, params, err := mime.ParseMediaType(header.Get("Content-Type"))
  197. if err != nil {
  198. return "", err
  199. }
  200. if strings.ToLower(contentType) == "text/plain" {
  201. return readPlainTextMailBody(body, header.Get("Content-Transfer-Encoding"))
  202. } else if strings.HasPrefix(strings.ToLower(contentType), "multipart/") {
  203. return readMultipartMailBody(body, params, 0)
  204. }
  205. return "", errUnsupportedContentType
  206. }
  207. func readMultipartMailBody(body io.Reader, params map[string]string, depth int) (string, error) {
  208. if depth >= maxMultipartDepth {
  209. return "", errMultipartNestedTooDeep
  210. }
  211. mr := multipart.NewReader(body, params["boundary"])
  212. for {
  213. part, err := mr.NextPart()
  214. if err != nil { // may be io.EOF
  215. return "", err
  216. }
  217. partContentType, partParams, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
  218. if err != nil {
  219. return "", err
  220. }
  221. if strings.ToLower(partContentType) == "text/plain" {
  222. return readPlainTextMailBody(part, part.Header.Get("Content-Transfer-Encoding"))
  223. } else if strings.HasPrefix(strings.ToLower(partContentType), "multipart/") {
  224. return readMultipartMailBody(part, partParams, depth+1)
  225. }
  226. // Continue with next part
  227. }
  228. }
  229. func readPlainTextMailBody(reader io.Reader, transferEncoding string) (string, error) {
  230. if strings.ToLower(transferEncoding) == "base64" {
  231. reader = base64.NewDecoder(base64.StdEncoding, reader)
  232. }
  233. body, err := io.ReadAll(reader)
  234. if err != nil {
  235. return "", err
  236. }
  237. return string(body), nil
  238. }