| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"context" |
| 7 |
"crypto/ecdsa" |
| 8 |
"crypto/elliptic" |
| 9 |
"crypto/rand" |
| 10 |
"crypto/tls" |
| 11 |
"crypto/x509" |
| 12 |
"crypto/x509/pkix" |
| 13 |
"encoding/pem" |
| 14 |
"math/big" |
| 15 |
"net" |
| 16 |
"strings" |
| 17 |
"sync" |
| 18 |
"time" |
| 19 |
) |
| 20 |
|
| 21 |
// mockSMTP is a scripted SMTP listener used to exercise TLS, greetings, and DATA I/O. |
| 22 |
type mockSMTP struct { |
| 23 |
implicitTLS bool |
| 24 |
advertiseStartTLS bool |
| 25 |
acceptStartTLS bool |
| 26 |
authMechs string |
| 27 |
failGreeting bool |
| 28 |
closeAfter354 bool |
| 29 |
hangAfter string |
| 30 |
|
| 31 |
tlsConfig *tls.Config |
| 32 |
firstByte chan byte |
| 33 |
done chan struct{} |
| 34 |
closeDone sync.Once |
| 35 |
|
| 36 |
mu sync.Mutex |
| 37 |
commands []string |
| 38 |
data bytes.Buffer |
| 39 |
tlsActive bool |
| 40 |
} |
| 41 |
|
| 42 |
type firstByteConn struct { |
| 43 |
net.Conn |
| 44 |
|
| 45 |
first chan byte |
| 46 |
once sync.Once |
| 47 |
} |
| 48 |
|
| 49 |
func newMockSMTP() *mockSMTP { |
| 50 |
return &mockSMTP{ |
| 51 |
firstByte: make(chan byte, 1), |
| 52 |
done: make(chan struct{}), |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func (s *mockSMTP) commandsCopy() []string { |
| 57 |
s.mu.Lock() |
| 58 |
defer s.mu.Unlock() |
| 59 |
|
| 60 |
return append([]string(nil), s.commands...) |
| 61 |
} |
| 62 |
|
| 63 |
func (s *mockSMTP) dataString() string { |
| 64 |
s.mu.Lock() |
| 65 |
defer s.mu.Unlock() |
| 66 |
|
| 67 |
return s.data.String() |
| 68 |
} |
| 69 |
|
| 70 |
func (s *mockSMTP) hang() { |
| 71 |
select { |
| 72 |
case <-s.done: |
| 73 |
case <-time.After(5 * time.Second): |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func (s *mockSMTP) record(line string) { |
| 78 |
s.mu.Lock() |
| 79 |
defer s.mu.Unlock() |
| 80 |
|
| 81 |
s.commands = append(s.commands, line) |
| 82 |
} |
| 83 |
|
| 84 |
func mustSelfSignedCert(ip net.IP) tls.Certificate { |
| 85 |
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 86 |
if err != nil { |
| 87 |
panic(err) |
| 88 |
} |
| 89 |
|
| 90 |
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) |
| 91 |
if err != nil { |
| 92 |
panic(err) |
| 93 |
} |
| 94 |
|
| 95 |
tmpl := &x509.Certificate{ |
| 96 |
SerialNumber: serial, |
| 97 |
Subject: pkix.Name{CommonName: "mock.local"}, |
| 98 |
NotBefore: time.Now().Add(-time.Hour), |
| 99 |
NotAfter: time.Now().Add(time.Hour), |
| 100 |
KeyUsage: x509.KeyUsageDigitalSignature, |
| 101 |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 102 |
IPAddresses: []net.IP{ip}, |
| 103 |
DNSNames: []string{"localhost", "mock.local"}, |
| 104 |
} |
| 105 |
|
| 106 |
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key) |
| 107 |
if err != nil { |
| 108 |
panic(err) |
| 109 |
} |
| 110 |
|
| 111 |
keyDER, err := x509.MarshalECPrivateKey(key) |
| 112 |
if err != nil { |
| 113 |
panic(err) |
| 114 |
} |
| 115 |
|
| 116 |
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) |
| 117 |
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}) |
| 118 |
|
| 119 |
cert, err := tls.X509KeyPair(certPEM, keyPEM) |
| 120 |
if err != nil { |
| 121 |
panic(err) |
| 122 |
} |
| 123 |
|
| 124 |
return cert |
| 125 |
} |
| 126 |
|
| 127 |
func (c *firstByteConn) Read(p []byte) (int, error) { |
| 128 |
n, err := c.Conn.Read(p) |
| 129 |
if n > 0 { |
| 130 |
c.once.Do(func() { |
| 131 |
select { |
| 132 |
case c.first <- p[0]: |
| 133 |
default: |
| 134 |
} |
| 135 |
}) |
| 136 |
} |
| 137 |
|
| 138 |
return n, err |
| 139 |
} |
| 140 |
|
| 141 |
func startMockSMTP(cfg *mockSMTP) (string, func()) { |
| 142 |
var listenConfig net.ListenConfig |
| 143 |
|
| 144 |
listener, err := listenConfig.Listen(context.Background(), "tcp", "127.0.0.1:0") |
| 145 |
if err != nil { |
| 146 |
panic(err) |
| 147 |
} |
| 148 |
|
| 149 |
host, _, err := net.SplitHostPort(listener.Addr().String()) |
| 150 |
if err != nil { |
| 151 |
panic(err) |
| 152 |
} |
| 153 |
|
| 154 |
ip := net.ParseIP(host) |
| 155 |
cert := mustSelfSignedCert(ip) |
| 156 |
cfg.tlsConfig = &tls.Config{ |
| 157 |
Certificates: []tls.Certificate{cert}, |
| 158 |
MinVersion: tls.VersionTLS12, |
| 159 |
} |
| 160 |
|
| 161 |
go func() { |
| 162 |
conn, err := listener.Accept() |
| 163 |
if err != nil { |
| 164 |
return |
| 165 |
} |
| 166 |
|
| 167 |
cfg.serve(&firstByteConn{Conn: conn, first: cfg.firstByte}) |
| 168 |
}() |
| 169 |
|
| 170 |
return listener.Addr().String(), func() { |
| 171 |
cfg.closeDone.Do(func() { |
| 172 |
close(cfg.done) |
| 173 |
}) |
| 174 |
|
| 175 |
_ = listener.Close() |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func (s *mockSMTP) serve(conn net.Conn) { |
| 180 |
defer func() { _ = conn.Close() }() |
| 181 |
|
| 182 |
if s.hangAfter == "" { |
| 183 |
_ = conn.SetDeadline(time.Now().Add(10 * time.Second)) |
| 184 |
} |
| 185 |
|
| 186 |
if s.implicitTLS { |
| 187 |
tlsConn := tls.Server(conn, s.tlsConfig) |
| 188 |
if err := tlsConn.HandshakeContext(context.Background()); err != nil { |
| 189 |
return |
| 190 |
} |
| 191 |
|
| 192 |
conn = tlsConn |
| 193 |
s.tlsActive = true |
| 194 |
} |
| 195 |
|
| 196 |
reader := bufio.NewReader(conn) |
| 197 |
writer := bufio.NewWriter(conn) |
| 198 |
write := func(line string) { |
| 199 |
_, _ = writer.WriteString(line + "\r\n") |
| 200 |
_ = writer.Flush() |
| 201 |
} |
| 202 |
|
| 203 |
if s.failGreeting { |
| 204 |
write("500 not a greeting") |
| 205 |
|
| 206 |
return |
| 207 |
} |
| 208 |
|
| 209 |
write("220 mock.local ESMTP") |
| 210 |
|
| 211 |
if s.hangAfter == "220" { |
| 212 |
s.hang() |
| 213 |
|
| 214 |
return |
| 215 |
} |
| 216 |
|
| 217 |
for { |
| 218 |
line, err := reader.ReadString('\n') |
| 219 |
if err != nil { |
| 220 |
return |
| 221 |
} |
| 222 |
|
| 223 |
line = strings.TrimRight(line, "\r\n") |
| 224 |
s.record(line) |
| 225 |
|
| 226 |
cmd, _, _ := strings.Cut(line, " ") |
| 227 |
cmd = strings.ToUpper(cmd) |
| 228 |
|
| 229 |
switch cmd { |
| 230 |
case "EHLO", "HELO": |
| 231 |
write("250-mock.local") |
| 232 |
|
| 233 |
if s.advertiseStartTLS && !s.tlsActive { |
| 234 |
write("250-STARTTLS") |
| 235 |
} |
| 236 |
|
| 237 |
if s.authMechs != "" { |
| 238 |
write("250-AUTH " + s.authMechs) |
| 239 |
} |
| 240 |
|
| 241 |
write("250 8BITMIME") |
| 242 |
|
| 243 |
if s.hangAfter == "ehlo" { |
| 244 |
s.hang() |
| 245 |
|
| 246 |
return |
| 247 |
} |
| 248 |
case "STARTTLS": |
| 249 |
if !s.acceptStartTLS || s.tlsActive { |
| 250 |
write("502 STARTTLS not available") |
| 251 |
|
| 252 |
continue |
| 253 |
} |
| 254 |
|
| 255 |
write("220 Ready to start TLS") |
| 256 |
|
| 257 |
tlsConn := tls.Server(conn, s.tlsConfig) |
| 258 |
if err := tlsConn.HandshakeContext(context.Background()); err != nil { |
| 259 |
return |
| 260 |
} |
| 261 |
|
| 262 |
conn = tlsConn |
| 263 |
s.tlsActive = true |
| 264 |
reader = bufio.NewReader(conn) |
| 265 |
writer = bufio.NewWriter(conn) |
| 266 |
case "AUTH": |
| 267 |
write("235 2.7.0 Accepted") |
| 268 |
|
| 269 |
if s.hangAfter == "auth" { |
| 270 |
s.hang() |
| 271 |
|
| 272 |
return |
| 273 |
} |
| 274 |
case "MAIL": |
| 275 |
write("250 2.1.0 Sender OK") |
| 276 |
case "RCPT": |
| 277 |
write("250 2.1.5 Recipient OK") |
| 278 |
case "DATA": |
| 279 |
write("354 Go ahead") |
| 280 |
|
| 281 |
if s.closeAfter354 { |
| 282 |
return |
| 283 |
} |
| 284 |
|
| 285 |
for { |
| 286 |
dataLine, err := reader.ReadString('\n') |
| 287 |
if err != nil { |
| 288 |
return |
| 289 |
} |
| 290 |
|
| 291 |
s.mu.Lock() |
| 292 |
s.data.WriteString(dataLine) |
| 293 |
s.mu.Unlock() |
| 294 |
|
| 295 |
if strings.TrimRight(dataLine, "\r\n") == "." { |
| 296 |
break |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
write("250 2.0.0 Message accepted") |
| 301 |
case "RSET": |
| 302 |
write("250 2.0.0 Reset OK") |
| 303 |
case "QUIT": |
| 304 |
write("221 2.0.0 Bye") |
| 305 |
|
| 306 |
return |
| 307 |
default: |
| 308 |
write("502 5.5.1 Unrecognized") |
| 309 |
} |
| 310 |
} |
| 311 |
} |