-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwrap_smtp.go
393 lines (358 loc) · 12.4 KB
/
wrap_smtp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package sparkypmtatracking
import (
"bytes"
"crypto/tls"
"encoding/base64"
"errors"
"io"
"log"
"mime"
"mime/multipart"
"net"
"net/mail"
"os"
"strings"
smtpproxy "github.com/tuck1s/go-smtpproxy"
)
//-----------------------------------------------------------------------------
// Backend handlers
// The Backend implements SMTP server methods.
type Backend struct {
outHostPort string
verbose bool
upstreamDataDebug *os.File
wrapper *Wrapper
insecureSkipVerify bool
}
// NewBackend init function
func NewBackend(outHostPort string, verbose bool, upstreamDataDebug *os.File, wrap *Wrapper, insecureSkipVerify bool) *Backend {
b := Backend{
outHostPort: outHostPort,
verbose: verbose,
upstreamDataDebug: upstreamDataDebug,
wrapper: wrap,
insecureSkipVerify: insecureSkipVerify,
}
return &b
}
// SetVerbose allows changing logging options on-the-fly
func (bkd *Backend) SetVerbose(v bool) {
bkd.verbose = v
}
// SetWrapper Verbose allows changing on-the-fly
func (bkd *Backend) SetWrapper(wrap *Wrapper) {
bkd.wrapper = wrap
}
func (bkd *Backend) logger(args ...interface{}) {
if bkd.verbose {
log.Println(args...)
}
}
func (bkd *Backend) loggerAlways(args ...interface{}) {
log.Println(args...)
}
// MakeSession returns a session for this client and backend
func MakeSession(c *smtpproxy.Client, bkd *Backend) smtpproxy.Session {
var s Session
s.bkd = bkd // just for logging
s.upstream = c // keep record of the upstream Client connection
return &s
}
// Init the backend. Here we establish the upstream connection
func (bkd *Backend) Init() (smtpproxy.Session, error) {
bkd.logger("---Connecting upstream")
c, err := smtpproxy.Dial(bkd.outHostPort)
if err != nil {
bkd.loggerAlways("< Connection error", bkd.outHostPort, err.Error())
return nil, err
}
bkd.logger("< Connection success", bkd.outHostPort)
return MakeSession(c, bkd), nil
}
//-----------------------------------------------------------------------------
// Session handlers
// A Session is returned after successful login. Here hold information that needs to persist across message phases.
type Session struct {
bkd *Backend // The backend that created this session. Allows session methods to e.g. log
upstream *smtpproxy.Client // the upstream client this backend is driving
}
// cmdTwiddle returns different flow markers depending on whether connection is secure (like Swaks does)
func cmdTwiddle(s *Session) string {
if s.upstream != nil {
if _, isTLS := s.upstream.TLSConnectionState(); isTLS {
return "~>"
}
}
return "->"
}
// respTwiddle returns different flow markers depending on whether connection is secure (like Swaks does)
func respTwiddle(s *Session) string {
if s.upstream != nil {
if _, isTLS := s.upstream.TLSConnectionState(); isTLS {
return "\t<~"
}
}
return "\t<-"
}
// Greet the upstream host and report capabilities back.
func (s *Session) Greet(helotype string) ([]string, int, string, error) {
s.bkd.logger(cmdTwiddle(s), helotype)
host, _, _ := net.SplitHostPort(s.bkd.outHostPort)
if host == "" {
host = "smtpproxy.localhost" // add dummy value in
}
code, msg, err := s.upstream.Hello(host)
if err != nil {
s.bkd.loggerAlways(respTwiddle(s), helotype, "error", err.Error())
if code == 0 {
// some errors don't show up in (code,msg) e.g. TLS cert errors, so map as a specific SMTP code/msg response
code = 599
msg = err.Error()
}
return nil, code, msg, err
}
s.bkd.logger(respTwiddle(s), helotype, "success")
caps := s.upstream.Capabilities()
s.bkd.logger("\tUpstream capabilities:", caps)
return caps, code, msg, err
}
// StartTLS command
func (s *Session) StartTLS() (int, string, error) {
host, _, _ := net.SplitHostPort(s.bkd.outHostPort)
// Try the upstream server, it will report error if unsupported
tlsconfig := &tls.Config{
InsecureSkipVerify: s.bkd.insecureSkipVerify,
ServerName: host,
}
s.bkd.logger(cmdTwiddle(s), "STARTTLS")
code, msg, err := s.upstream.StartTLS(tlsconfig)
if err != nil {
s.bkd.loggerAlways(respTwiddle(s), code, msg)
} else {
s.bkd.logger(respTwiddle(s), code, msg)
}
return code, msg, err
}
//Auth command backend handler
func (s *Session) Auth(expectcode int, cmd, arg string) (int, string, error) {
return s.Passthru(expectcode, cmd, arg)
}
//Mail command backend handler
func (s *Session) Mail(expectcode int, cmd, arg string) (int, string, error) {
return s.Passthru(expectcode, cmd, arg)
}
//Rcpt command backend handler
func (s *Session) Rcpt(expectcode int, cmd, arg string) (int, string, error) {
return s.Passthru(expectcode, cmd, arg)
}
//Reset command backend handler
func (s *Session) Reset(expectcode int, cmd, arg string) (int, string, error) {
return s.Passthru(expectcode, cmd, arg)
}
//Quit command backend handler
func (s *Session) Quit(expectcode int, cmd, arg string) (int, string, error) {
return s.Passthru(expectcode, cmd, arg)
}
//Unknown command backend handler
func (s *Session) Unknown(expectcode int, cmd, arg string) (int, string, error) {
return s.Passthru(expectcode, cmd, arg)
}
// Passthru a command to the upstream server, logging
func (s *Session) Passthru(expectcode int, cmd, arg string) (int, string, error) {
s.bkd.logger(cmdTwiddle(s), cmd, arg)
joined := cmd
if arg != "" {
joined = cmd + " " + arg
}
code, msg, err := s.upstream.MyCmd(expectcode, joined)
if err != nil {
s.bkd.loggerAlways(respTwiddle(s), cmd, code, msg, "error", err.Error())
if code == 0 {
// some errors don't show up in (code,msg) e.g. TLS cert errors, so map as a specific SMTP code/msg response
code = 599
msg = err.Error()
}
} else {
s.bkd.logger(respTwiddle(s), code, msg)
}
return code, msg, err
}
// DataCommand pass upstream, returning a place to write the data AND the usual responses
func (s *Session) DataCommand() (io.WriteCloser, int, string, error) {
s.bkd.logger(cmdTwiddle(s), "DATA")
w, code, msg, err := s.upstream.Data()
if err != nil {
s.bkd.loggerAlways(respTwiddle(s), "DATA error", err.Error())
}
return w, code, msg, err
}
// Data body (dot delimited) pass upstream, returning the usual responses
func (s *Session) Data(r io.Reader, w io.WriteCloser) (int, string, error) {
var buf bytes.Buffer
err := s.bkd.wrapper.MailCopy(&buf, r) // Pass in the engagement tracking info
if err != nil {
msg := "DATA MailCopy error"
s.bkd.loggerAlways(respTwiddle(s), msg, err.Error())
return 0, msg, err
}
// Upstream debug output - nondestructively read buf contents
if s.bkd.upstreamDataDebug != nil {
dbgWritten, err := io.Copy(s.bkd.upstreamDataDebug, bytes.NewReader(buf.Bytes()))
if err != nil {
s.bkd.loggerAlways("upstreamDataDebug error", err, ", bytes written =", dbgWritten)
return 0, "", err
}
}
// Send the data upstream
count, err := io.Copy(w, &buf)
if err != nil {
msg := "DATA io.Copy error"
s.bkd.loggerAlways(respTwiddle(s), msg, err.Error())
return 0, msg, err
}
err = w.Close() // Need to close the data phase - then we should have response from upstream
code := s.upstream.DataResponseCode
msg := s.upstream.DataResponseMsg
if err != nil {
s.bkd.loggerAlways(respTwiddle(s), "DATA Close error", err, ", bytes written =", count)
return 0, msg, err
}
if s.bkd.verbose {
s.bkd.logger(respTwiddle(s), "DATA accepted, bytes written =", count)
} else {
// Short-form logging - one line per message - used when "verbose" not set
log.Printf("Message DATA upstream,%d,%d,%s\n", count, code, msg)
}
return code, msg, err
}
//-----------------------------------------------------------------------------
// Engagement Tracking
// SparkPostMessageIDHeader is the email header name that carries the unique message ID
const SparkPostMessageIDHeader = "X-Sp-Message-Id"
const smtpCRLF = "\r\n"
// MailCopy transfers the mail body from downstream (client) to upstream (server), using the engagement wrapper
// The writer should be closed by the parent function
func (wrap *Wrapper) MailCopy(dst io.Writer, src io.Reader) error {
if !wrap.Active() {
_, err := io.Copy(dst, src) // wrapping inactive, just do a copy
return err
}
message, err := mail.ReadMessage(src)
if err != nil {
return err
}
err = wrap.ProcessMessageHeaders(message.Header)
if err != nil {
return err
}
err = writeMessageHeaders(dst, message.Header)
if err != nil {
return err
}
// Handle the message body
return wrap.HandleMessagePart(dst, message.Body, message.Header.Get("Content-Type"), message.Header.Get("Content-Transfer-Encoding"))
}
// ProcessMessageHeaders reads the message's current headers and updates/inserts any new ones required.
// The RCPT TO address is grabbed
func (wrap *Wrapper) ProcessMessageHeaders(h mail.Header) error {
rcpts, err := h.AddressList("to")
if err != nil {
return err
}
ccs, _ := h.AddressList("cc") // ignore "mail:header not in message" error return as it's expected
bccs, _ := h.AddressList("bcc")
if len(rcpts) != 1 || len(ccs) != 0 || len(bccs) != 0 {
// Multiple recipients (to, cc, bcc) would require the html to be encoded for EACH recipient and exploded into n messages, which is TODO.
return errors.New("This tracking implementation is designed for simple single-recipient messages only, sorry")
}
// See if we already have a message ID header; otherwise generate and add it
uniq := h.Get(SparkPostMessageIDHeader)
if uniq == "" {
uniq = UniqMessageID()
h[SparkPostMessageIDHeader] = []string{uniq} // Add unique value into the message headers for PowerMTA / Signals to process
}
wrap.SetMessageInfo(uniq, rcpts[0].Address)
return nil
}
// write m headers to dst. The m.Header map does not preserve order, but that should not matter.
func writeMessageHeaders(dst io.Writer, h mail.Header) error {
for hdrType, hdrList := range h {
for _, hdrVal := range hdrList {
hdrLine := hdrType + ": " + hdrVal + smtpCRLF
_, err := io.WriteString(dst, hdrLine)
if err != nil {
return err
}
}
}
// Blank line denotes end of headers
_, err := io.WriteString(dst, smtpCRLF)
return err
}
// HandleMessagePart walks the MIME structure, and may be called recursively. The incoming
// content type and cte (content transfer encoding) are passed separately
func (wrap *Wrapper) HandleMessagePart(dst io.Writer, part io.Reader, cType string, cte string) error {
// Check what MIME media type we have
mediaType, params, err := mime.ParseMediaType(cType)
if err != nil {
// if no media type, defensively handle as per plain, i.e. pass through
return handlePlainPart(dst, part)
}
if strings.HasPrefix(mediaType, "text/html") {
// Insert decoder into incoming part, and encoder into dst. Quoted-Printable is automatically handled
// by the reader, no need to handle here: https://golang.org/src/mime/multipart/multipart.go?s=825:1710#L25
if cte == "base64" {
part = base64.NewDecoder(base64.StdEncoding, part)
// pass output through base64 encoding -> line splitter
lsWriter := smtpproxy.NewLineSplitterWriter(76, []byte("\r\n"), dst)
dst = base64.NewEncoder(base64.StdEncoding, lsWriter)
} else {
if !(cte == "" || cte == "7bit" || cte == "8bit") {
log.Println("Warning: don't know how to handle Content-Type-Encoding", cte)
}
}
_, err = wrap.TrackHTML(dst, part) // Wrap the links and add tracking pixels (if active)
} else {
if strings.HasPrefix(mediaType, "multipart/") {
mr := multipart.NewReader(part, params["boundary"])
err = wrap.handleMultiPart(dst, mr, params["boundary"])
} else {
// Everything else such as text/plain, image/gif etc pass through
err = handlePlainPart(dst, part)
}
}
return err
}
// Transfer through a plain MIME part
func handlePlainPart(dst io.Writer, src io.Reader) error {
_, err := io.Copy(dst, src) // Passthrough
return err
}
// Transfer through a multipart message, handling recursively as needed.
// Based on https://golang.org/pkg/mime/multipart/#example_NewReader
func (wrap *Wrapper) handleMultiPart(dst io.Writer, mr *multipart.Reader, bound string) error {
pWrt := multipart.NewWriter(dst)
pWrt.SetBoundary(bound)
for {
p, err := mr.NextPart()
if err != nil {
if err == io.EOF {
err = nil // Usual termination
break
}
return err // Unexpected error
}
pWrt2, err := pWrt.CreatePart(p.Header)
if err != nil {
return err
}
cType := p.Header.Get("Content-Type")
cte := p.Header.Get("Content-Transfer-Encoding")
err = wrap.HandleMessagePart(pWrt2, p, cType, cte)
if err != nil {
return err
}
}
pWrt.Close() // Put the boundary in
return nil
}