backend.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package backends
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "time"
  9. log "github.com/Sirupsen/logrus"
  10. "github.com/flashmob/go-guerrilla/envelope"
  11. "github.com/flashmob/go-guerrilla/response"
  12. )
  13. // Backends process received mail. Depending on the implementation, they can store mail in the database,
  14. // write to a file, check for spam, re-transmit to another server, etc.
  15. // Must return an SMTP message (i.e. "250 OK") and a boolean indicating
  16. // whether the message was processed successfully.
  17. type Backend interface {
  18. // Public methods
  19. Process(*envelope.Envelope) BackendResult
  20. Initialize(BackendConfig) error
  21. Shutdown() error
  22. // start save mail worker(s)
  23. saveMailWorker(chan *savePayload)
  24. // get the number of workers that will be stared
  25. getNumberOfWorkers() int
  26. // test database settings, permissions, correct paths, etc, before starting workers
  27. testSettings() error
  28. // parse the configuration files
  29. loadConfig(BackendConfig) error
  30. }
  31. type configLoader interface {
  32. loadConfig(backendConfig BackendConfig) (err error)
  33. }
  34. type BackendConfig map[string]interface{}
  35. var backends = map[string]Backend{}
  36. type baseConfig interface{}
  37. type saveStatus struct {
  38. err error
  39. hash string
  40. }
  41. type savePayload struct {
  42. mail *envelope.Envelope
  43. from *envelope.EmailAddress
  44. recipient *envelope.EmailAddress
  45. savedNotify chan *saveStatus
  46. }
  47. // BackendResult represents a response to an SMTP client after receiving DATA.
  48. // The String method should return an SMTP message ready to send back to the
  49. // client, for example `250 OK: Message received`.
  50. type BackendResult interface {
  51. fmt.Stringer
  52. // Code should return the SMTP code associated with this response, ie. `250`
  53. Code() int
  54. }
  55. // Internal implementation of BackendResult for use by backend implementations.
  56. type backendResult string
  57. func (br backendResult) String() string {
  58. return string(br)
  59. }
  60. // Parses the SMTP code from the first 3 characters of the SMTP message.
  61. // Returns 554 if code cannot be parsed.
  62. func (br backendResult) Code() int {
  63. trimmed := strings.TrimSpace(string(br))
  64. if len(trimmed) < 3 {
  65. return 554
  66. }
  67. code, err := strconv.Atoi(trimmed[:3])
  68. if err != nil {
  69. return 554
  70. }
  71. return code
  72. }
  73. func NewBackendResult(message string) BackendResult {
  74. return backendResult(message)
  75. }
  76. // A backend gateway is a proxy that implements the Backend interface.
  77. // It is used to start multiple goroutine workers for saving mail, and then distribute email saving to the workers
  78. // via a channel. Shutting down via Shutdown() will stop all workers.
  79. // The rest of this program always talks to the backend via this gateway.
  80. type BackendGateway struct {
  81. AbstractBackend
  82. saveMailChan chan *savePayload
  83. // waits for backend workers to start/stop
  84. wg sync.WaitGroup
  85. b Backend
  86. // controls access to state
  87. stateGuard sync.Mutex
  88. State int
  89. config BackendConfig
  90. }
  91. // possible values for state
  92. const (
  93. BackendStateRunning = iota
  94. BackendStateShuttered
  95. BackendStateError
  96. )
  97. // New retrieve a backend specified by the backendName, and initialize it using
  98. // backendConfig
  99. func New(backendName string, backendConfig BackendConfig) (Backend, error) {
  100. backend, found := backends[backendName]
  101. if !found {
  102. return nil, fmt.Errorf("backend %q not found", backendName)
  103. }
  104. gateway := &BackendGateway{b: backend, config: backendConfig}
  105. err := gateway.Initialize(backendConfig)
  106. if err != nil {
  107. return nil, fmt.Errorf("error while initializing the backend: %s", err)
  108. }
  109. gateway.State = BackendStateRunning
  110. return gateway, nil
  111. }
  112. // Distributes an envelope to one of the backend workers
  113. func (gw *BackendGateway) Process(e *envelope.Envelope) BackendResult {
  114. if gw.State != BackendStateRunning {
  115. return NewBackendResult(response.CustomString(response.OtherOrUndefinedProtocolStatus, 554, response.ClassPermanentFailure, "Transaction failed - backend not running "+strconv.Itoa(gw.State)))
  116. }
  117. to := e.RcptTo
  118. from := e.MailFrom
  119. // place on the channel so that one of the save mail workers can pick it up
  120. // TODO: support multiple recipients
  121. savedNotify := make(chan *saveStatus)
  122. gw.saveMailChan <- &savePayload{e, from, &to[0], savedNotify}
  123. // wait for the save to complete
  124. // or timeout
  125. select {
  126. case status := <-savedNotify:
  127. if status.err != nil {
  128. return NewBackendResult(response.CustomString(response.OtherOrUndefinedProtocolStatus, 554, response.ClassPermanentFailure, "Error: "+status.err.Error()))
  129. }
  130. return NewBackendResult(response.CustomString(response.OtherStatus, 250, response.ClassSuccess, fmt.Sprintf("OK : queued as %s", status.hash)))
  131. case <-time.After(time.Second * 30):
  132. log.Infof("Backend has timed out")
  133. return NewBackendResult(response.CustomString(response.OtherOrUndefinedProtocolStatus, 554, response.ClassPermanentFailure, "Error: transaction timeout"))
  134. }
  135. }
  136. func (gw *BackendGateway) Shutdown() error {
  137. gw.stateGuard.Lock()
  138. defer gw.stateGuard.Unlock()
  139. if gw.State != BackendStateShuttered {
  140. err := gw.b.Shutdown()
  141. if err == nil {
  142. close(gw.saveMailChan) // workers will stop
  143. gw.wg.Wait()
  144. gw.State = BackendStateShuttered
  145. }
  146. return err
  147. }
  148. return nil
  149. }
  150. // Reinitialize starts up a backend gateway that was shutdown before
  151. func (gw *BackendGateway) Reinitialize() error {
  152. if gw.State != BackendStateShuttered {
  153. return errors.New("backend must be in BackendStateshuttered state to Reinitialize")
  154. }
  155. err := gw.Initialize(gw.config)
  156. if err != nil {
  157. return fmt.Errorf("error while initializing the backend: %s", err)
  158. } else {
  159. gw.State = BackendStateRunning
  160. }
  161. return err
  162. }
  163. func (gw *BackendGateway) Initialize(cfg BackendConfig) error {
  164. err := gw.b.Initialize(cfg)
  165. if err == nil {
  166. workersSize := gw.b.getNumberOfWorkers()
  167. if workersSize < 1 {
  168. gw.State = BackendStateError
  169. return errors.New("Must have at least 1 worker")
  170. }
  171. if err := gw.b.testSettings(); err != nil {
  172. gw.State = BackendStateError
  173. return err
  174. }
  175. gw.saveMailChan = make(chan *savePayload, workersSize)
  176. // start our savemail workers
  177. gw.wg.Add(workersSize)
  178. for i := 0; i < workersSize; i++ {
  179. go func() {
  180. gw.b.saveMailWorker(gw.saveMailChan)
  181. gw.wg.Done()
  182. }()
  183. }
  184. } else {
  185. gw.State = BackendStateError
  186. }
  187. return err
  188. }