backend.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // Process distributes an envelope to one of the backend workers
  113. func (gw *BackendGateway) Process(e *envelope.Envelope) BackendResult {
  114. if gw.State != BackendStateRunning {
  115. resp := &response.Response{
  116. EnhancedCode: response.OtherOrUndefinedProtocolStatus,
  117. BasicCode: 554,
  118. Class: response.ClassPermanentFailure,
  119. Comment: "Transaction failed - backend not running " + strconv.Itoa(gw.State),
  120. }
  121. return NewBackendResult(resp.String())
  122. }
  123. to := e.RcptTo
  124. from := e.MailFrom
  125. // place on the channel so that one of the save mail workers can pick it up
  126. // TODO: support multiple recipients
  127. savedNotify := make(chan *saveStatus)
  128. gw.saveMailChan <- &savePayload{e, from, &to[0], savedNotify}
  129. // wait for the save to complete
  130. // or timeout
  131. select {
  132. case status := <-savedNotify:
  133. if status.err != nil {
  134. resp := &response.Response{
  135. EnhancedCode: response.OtherOrUndefinedProtocolStatus,
  136. BasicCode: 554,
  137. Class: response.ClassPermanentFailure,
  138. Comment: "Error: " + status.err.Error(),
  139. }
  140. return NewBackendResult(resp.String())
  141. }
  142. resp := &response.Response{
  143. EnhancedCode: response.OtherStatus,
  144. BasicCode: 250,
  145. Class: response.ClassSuccess,
  146. Comment: fmt.Sprintf("OK : queued as %s", status.hash),
  147. }
  148. return NewBackendResult(resp.String())
  149. case <-time.After(time.Second * 30):
  150. log.Infof("Backend has timed out")
  151. resp := &response.Response{
  152. EnhancedCode: response.OtherOrUndefinedProtocolStatus,
  153. BasicCode: 554,
  154. Class: response.ClassPermanentFailure,
  155. Comment: "Error: transaction timeout",
  156. }
  157. return NewBackendResult(resp.String())
  158. }
  159. }
  160. func (gw *BackendGateway) Shutdown() error {
  161. gw.stateGuard.Lock()
  162. defer gw.stateGuard.Unlock()
  163. if gw.State != BackendStateShuttered {
  164. err := gw.b.Shutdown()
  165. if err == nil {
  166. close(gw.saveMailChan) // workers will stop
  167. gw.wg.Wait()
  168. gw.State = BackendStateShuttered
  169. }
  170. return err
  171. }
  172. return nil
  173. }
  174. // Reinitialize starts up a backend gateway that was shutdown before
  175. func (gw *BackendGateway) Reinitialize() error {
  176. if gw.State != BackendStateShuttered {
  177. return errors.New("backend must be in BackendStateshuttered state to Reinitialize")
  178. }
  179. err := gw.Initialize(gw.config)
  180. if err != nil {
  181. return fmt.Errorf("error while initializing the backend: %s", err)
  182. }
  183. gw.State = BackendStateRunning
  184. return err
  185. }
  186. func (gw *BackendGateway) Initialize(cfg BackendConfig) error {
  187. err := gw.b.Initialize(cfg)
  188. if err == nil {
  189. workersSize := gw.b.getNumberOfWorkers()
  190. if workersSize < 1 {
  191. gw.State = BackendStateError
  192. return errors.New("Must have at least 1 worker")
  193. }
  194. if err := gw.b.testSettings(); err != nil {
  195. gw.State = BackendStateError
  196. return err
  197. }
  198. gw.saveMailChan = make(chan *savePayload, workersSize)
  199. // start our savemail workers
  200. gw.wg.Add(workersSize)
  201. for i := 0; i < workersSize; i++ {
  202. go func() {
  203. gw.b.saveMailWorker(gw.saveMailChan)
  204. gw.wg.Done()
  205. }()
  206. }
  207. } else {
  208. gw.State = BackendStateError
  209. }
  210. return err
  211. }