Change the send-as functionality to use a string in the Subject.
[mailpopbox.git] / smtp / conn.go
1 package smtp
2
3 import (
4 "bytes"
5 "crypto/rand"
6 "crypto/tls"
7 "encoding/base64"
8 "fmt"
9 "net"
10 "net/mail"
11 "net/textproto"
12 "strings"
13 "time"
14
15 "github.com/uber-go/zap"
16 )
17
18 type state int
19
20 const (
21 stateNew state = iota // Before EHLO.
22 stateInitial
23 stateMail
24 stateRecipient
25 stateData
26 )
27
28 type delivery int
29
30 func (d delivery) String() string {
31 switch d {
32 case deliverUnknown:
33 return "unknown"
34 case deliverInbound:
35 return "inbound"
36 case deliverOutbound:
37 return "outbound"
38 }
39 panic("Unknown delivery")
40 }
41
42 const (
43 deliverUnknown delivery = iota
44 deliverInbound // Mail is not from one of this server's domains.
45 deliverOutbound // Mail IS from one of this server's domains.
46 )
47
48 type connection struct {
49 server Server
50
51 tp *textproto.Conn
52
53 nc net.Conn
54 remoteAddr net.Addr
55
56 esmtp bool
57 tls *tls.ConnectionState
58
59 // The authcid from a PLAIN SASL login. Non-empty iff tls is non-nil and
60 // doAUTH() succeeded.
61 authc string
62
63 log zap.Logger
64
65 state
66 line string
67
68 delivery
69 // For deliverOutbound, replaces the From and Reply-To values.
70 sendAs *mail.Address
71
72 ehlo string
73 mailFrom *mail.Address
74 rcptTo []mail.Address
75 }
76
77 func AcceptConnection(netConn net.Conn, server Server, log zap.Logger) {
78 conn := connection{
79 server: server,
80 tp: textproto.NewConn(netConn),
81 nc: netConn,
82 remoteAddr: netConn.RemoteAddr(),
83 log: log.With(zap.Stringer("client", netConn.RemoteAddr())),
84 state: stateNew,
85 }
86
87 conn.log.Info("accepted connection")
88 conn.writeReply(220, fmt.Sprintf("%s ESMTP [%s] (mailpopbox)",
89 server.Name(), netConn.LocalAddr()))
90
91 for {
92 var err error
93 conn.line, err = conn.tp.ReadLine()
94 if err != nil {
95 conn.log.Error("ReadLine()", zap.Error(err))
96 conn.tp.Close()
97 return
98 }
99
100 lineForLog := conn.line
101 const authPlain = "AUTH PLAIN "
102 if strings.HasPrefix(conn.line, authPlain) {
103 lineForLog = authPlain + "[redacted]"
104 }
105 conn.log.Info("ReadLine()", zap.String("line", lineForLog))
106
107 var cmd string
108 if _, err = fmt.Sscanf(conn.line, "%s", &cmd); err != nil {
109 conn.reply(ReplyBadSyntax)
110 continue
111 }
112
113 switch strings.ToUpper(cmd) {
114 case "QUIT":
115 conn.writeReply(221, "Goodbye")
116 conn.tp.Close()
117 return
118 case "HELO":
119 conn.esmtp = false
120 fallthrough
121 case "EHLO":
122 conn.esmtp = true
123 conn.doEHLO()
124 case "STARTTLS":
125 conn.doSTARTTLS()
126 case "AUTH":
127 conn.doAUTH()
128 case "MAIL":
129 conn.doMAIL()
130 case "RCPT":
131 conn.doRCPT()
132 case "DATA":
133 conn.doDATA()
134 case "RSET":
135 conn.doRSET()
136 case "VRFY":
137 conn.writeReply(252, "I'll do my best")
138 case "EXPN":
139 conn.writeReply(550, "access denied")
140 case "NOOP":
141 conn.reply(ReplyOK)
142 case "HELP":
143 conn.writeReply(250, "https://tools.ietf.org/html/rfc5321")
144 default:
145 conn.writeReply(500, "unrecognized command")
146 }
147 }
148 }
149
150 func (conn *connection) reply(reply ReplyLine) error {
151 return conn.writeReply(reply.Code, reply.Message)
152 }
153
154 func (conn *connection) writeReply(code int, msg string) error {
155 conn.log.Info("writeReply", zap.Int("code", code))
156 var err error
157 if len(msg) > 0 {
158 err = conn.tp.PrintfLine("%d %s", code, msg)
159 } else {
160 err = conn.tp.PrintfLine("%d", code)
161 }
162 if err != nil {
163 conn.log.Error("writeReply",
164 zap.Int("code", code),
165 zap.Error(err))
166 }
167 return err
168 }
169
170 // parsePath parses out either a forward-, reverse-, or return-path from the
171 // current connection line. Returns a (valid-path, ReplyOK) if it was
172 // successfully parsed.
173 func (conn *connection) parsePath(command string) (string, ReplyLine) {
174 if len(conn.line) < len(command) {
175 return "", ReplyBadSyntax
176 }
177 if strings.ToUpper(command) != strings.ToUpper(conn.line[:len(command)]) {
178 return "", ReplyLine{500, "unrecognized command"}
179 }
180 params := conn.line[len(command):]
181 idx := strings.Index(params, ">")
182 if idx == -1 {
183 return "", ReplyBadSyntax
184 }
185 return strings.ToLower(params[:idx+1]), ReplyOK
186 }
187
188 func (conn *connection) doEHLO() {
189 conn.resetBuffers()
190
191 var cmd string
192 _, err := fmt.Sscanf(conn.line, "%s %s", &cmd, &conn.ehlo)
193 if err != nil {
194 conn.reply(ReplyBadSyntax)
195 return
196 }
197
198 if cmd == "HELO" {
199 conn.writeReply(250, fmt.Sprintf("Hello %s [%s]", conn.ehlo, conn.remoteAddr))
200 } else {
201 conn.tp.PrintfLine("250-Hello %s [%s]", conn.ehlo, conn.remoteAddr)
202 if conn.server.TLSConfig() != nil && conn.tls == nil {
203 conn.tp.PrintfLine("250-STARTTLS")
204 }
205 if conn.tls != nil {
206 conn.tp.PrintfLine("250-AUTH PLAIN")
207 }
208 conn.tp.PrintfLine("250 SIZE %d", 40960000)
209 }
210
211 conn.log.Info("doEHLO()", zap.String("ehlo", conn.ehlo))
212
213 conn.state = stateInitial
214 }
215
216 func (conn *connection) doSTARTTLS() {
217 if conn.state != stateInitial {
218 conn.reply(ReplyBadSequence)
219 return
220 }
221
222 tlsConfig := conn.server.TLSConfig()
223 if !conn.esmtp || tlsConfig == nil {
224 conn.writeReply(500, "unrecognized command")
225 return
226 }
227
228 conn.log.Info("doSTARTTLS()")
229 conn.writeReply(220, "initiate TLS connection")
230
231 tlsConn := tls.Server(conn.nc, tlsConfig)
232 if err := tlsConn.Handshake(); err != nil {
233 conn.log.Error("failed to do TLS handshake", zap.Error(err))
234 return
235 }
236
237 conn.nc = tlsConn
238 conn.tp = textproto.NewConn(tlsConn)
239 conn.state = stateNew
240
241 connState := tlsConn.ConnectionState()
242 conn.tls = &connState
243
244 conn.log.Info("TLS connection done", zap.String("state", conn.getTransportString()))
245 }
246
247 func (conn *connection) doAUTH() {
248 if conn.state != stateInitial || conn.tls == nil {
249 conn.reply(ReplyBadSequence)
250 return
251 }
252
253 if conn.authc != "" {
254 conn.writeReply(503, "already authenticated")
255 return
256 }
257
258 var cmd, authType, authString string
259 n, err := fmt.Sscanf(conn.line, "%s %s %s", &cmd, &authType, &authString)
260 if n < 2 {
261 conn.reply(ReplyBadSyntax)
262 return
263 }
264
265 if authType != "PLAIN" {
266 conn.writeReply(504, "unrecognized auth type")
267 return
268 }
269
270 // If only 2 tokens were scanned, then an initial response was not provided.
271 if n == 2 && conn.line[len(conn.line)-1] != ' ' {
272 conn.reply(ReplyBadSyntax)
273 return
274 }
275
276 conn.log.Info("doAUTH()")
277
278 if authString == "" {
279 conn.writeReply(334, " ")
280
281 authString, err = conn.tp.ReadLine()
282 if err != nil {
283 conn.log.Error("failed to read auth line", zap.Error(err))
284 conn.reply(ReplyBadSyntax)
285 return
286 }
287 }
288
289 authBytes, err := base64.StdEncoding.DecodeString(authString)
290 if err != nil {
291 conn.reply(ReplyBadSyntax)
292 return
293 }
294
295 authParts := strings.Split(string(authBytes), "\x00")
296 if len(authParts) != 3 {
297 conn.log.Error("bad auth line syntax")
298 conn.reply(ReplyBadSyntax)
299 return
300 }
301
302 if !conn.server.Authenticate(authParts[0], authParts[1], authParts[2]) {
303 conn.log.Error("failed to authenticate", zap.String("authc", authParts[1]))
304 conn.writeReply(535, "invalid credentials")
305 return
306 }
307
308 conn.log.Info("authenticated", zap.String("authz", authParts[0]), zap.String("authc", authParts[1]))
309 conn.authc = authParts[1]
310 conn.reply(ReplyOK)
311 }
312
313 func (conn *connection) doMAIL() {
314 if conn.state != stateInitial {
315 conn.reply(ReplyBadSequence)
316 return
317 }
318
319 mailFrom, reply := conn.parsePath("MAIL FROM:")
320 if reply != ReplyOK {
321 conn.reply(reply)
322 return
323 }
324
325 var err error
326 conn.mailFrom, err = mail.ParseAddress(mailFrom)
327 if err != nil || conn.mailFrom == nil {
328 conn.reply(ReplyBadSyntax)
329 return
330 }
331
332 if conn.server.VerifyAddress(*conn.mailFrom) == ReplyOK {
333 if DomainForAddress(*conn.mailFrom) != DomainForAddressString(conn.authc) {
334 conn.writeReply(550, "not authenticated")
335 return
336 }
337 conn.delivery = deliverOutbound
338 } else {
339 conn.delivery = deliverInbound
340 }
341
342 conn.log.Info("doMAIL()", zap.String("address", conn.mailFrom.Address))
343
344 conn.state = stateMail
345 conn.reply(ReplyOK)
346 }
347
348 func (conn *connection) doRCPT() {
349 if conn.state != stateMail && conn.state != stateRecipient {
350 conn.reply(ReplyBadSequence)
351 return
352 }
353
354 rcptTo, reply := conn.parsePath("RCPT TO:")
355 if reply != ReplyOK {
356 conn.reply(reply)
357 return
358 }
359
360 address, err := mail.ParseAddress(rcptTo)
361 if err != nil {
362 conn.reply(ReplyBadSyntax)
363 return
364 }
365
366 if reply := conn.server.VerifyAddress(*address); reply != ReplyOK && conn.delivery == deliverInbound {
367 conn.log.Warn("invalid address",
368 zap.String("address", address.Address),
369 zap.Stringer("reply", reply))
370 conn.reply(reply)
371 return
372 }
373
374 conn.log.Info("doRCPT()",
375 zap.String("address", address.Address),
376 zap.String("delivery", conn.delivery.String()))
377
378 conn.rcptTo = append(conn.rcptTo, *address)
379
380 conn.state = stateRecipient
381 conn.reply(ReplyOK)
382 }
383
384 func (conn *connection) doDATA() {
385 if conn.state != stateRecipient {
386 conn.reply(ReplyBadSequence)
387 return
388 }
389
390 conn.writeReply(354, "Start mail input; end with <CRLF>.<CRLF>")
391 conn.log.Info("doDATA()")
392
393 data, err := conn.tp.ReadDotBytes()
394 if err != nil {
395 conn.log.Error("failed to ReadDotBytes()",
396 zap.Error(err),
397 zap.String("bytes", fmt.Sprintf("%x", data)))
398 conn.writeReply(552, "transaction failed")
399 return
400 }
401
402 received := time.Now()
403 env := Envelope{
404 RemoteAddr: conn.remoteAddr,
405 EHLO: conn.ehlo,
406 MailFrom: *conn.mailFrom,
407 RcptTo: conn.rcptTo,
408 Received: received,
409 ID: conn.envelopeID(received),
410 Data: data,
411 }
412
413 conn.handleSendAs(&env)
414
415 conn.log.Info("received message",
416 zap.Int("bytes", len(data)),
417 zap.Time("date", received),
418 zap.String("id", env.ID),
419 zap.String("delivery", conn.delivery.String()))
420
421 trace := conn.getReceivedInfo(env)
422
423 env.Data = append(trace, env.Data...)
424
425 if conn.delivery == deliverInbound {
426 if reply := conn.server.OnMessageDelivered(env); reply != nil {
427 conn.log.Warn("message was rejected", zap.String("id", env.ID))
428 conn.reply(*reply)
429 return
430 }
431 } else if conn.delivery == deliverOutbound {
432 conn.server.RelayMessage(env)
433 }
434
435 conn.state = stateInitial
436 conn.resetBuffers()
437 conn.reply(ReplyOK)
438 }
439
440 func (conn *connection) handleSendAs(env *Envelope) {
441 if conn.delivery != deliverOutbound {
442 return
443 }
444
445 // Find the separator between the message header and body.
446 headerIdx := bytes.Index(env.Data, []byte("\n\n"))
447 if headerIdx == -1 {
448 conn.log.Error("send-as: could not find headers index")
449 return
450 }
451
452 var buf bytes.Buffer
453
454 headers := bytes.SplitAfter(env.Data[:headerIdx], []byte("\n"))
455
456 var fromIdx, subjectIdx int
457 for i, header := range headers {
458 if bytes.HasPrefix(header, []byte("From:")) {
459 fromIdx = i
460 continue
461 }
462 if bytes.HasPrefix(header, []byte("Subject:")) {
463 subjectIdx = i
464 continue
465 }
466 }
467
468 if subjectIdx == -1 {
469 conn.log.Error("send-as: could not find Subject header")
470 return
471 }
472 if fromIdx == -1 {
473 conn.log.Error("send-as: could not find From header")
474 return
475 }
476
477 sendAs := SendAsSubject.FindSubmatchIndex(headers[subjectIdx])
478 if sendAs == nil {
479 // No send-as modification.
480 return
481 }
482
483 // Submatch 0 is the whole sendas magic. Submatch 1 is the address prefix.
484 sendAsUser := headers[subjectIdx][sendAs[2]:sendAs[3]]
485 sendAsAddress := string(sendAsUser) + "@" + DomainForAddressString(conn.authc)
486
487 for i, header := range headers {
488 if i == subjectIdx {
489 buf.Write(header[:sendAs[0]])
490 buf.Write(header[sendAs[1]:])
491 } else if i == fromIdx {
492 addressStart := bytes.LastIndexByte(header, byte('<'))
493 buf.Write(header[:addressStart+1])
494 buf.WriteString(sendAsAddress)
495 buf.WriteString(">\n")
496 } else {
497 buf.Write(header)
498 }
499 }
500
501 buf.Write(env.Data[headerIdx:])
502
503 env.Data = buf.Bytes()
504 env.MailFrom.Address = sendAsAddress
505 }
506
507 func (conn *connection) envelopeID(t time.Time) string {
508 var idBytes [4]byte
509 rand.Read(idBytes[:])
510 return fmt.Sprintf("m.%d.%x", t.UnixNano(), idBytes)
511 }
512
513 func (conn *connection) getReceivedInfo(envelope Envelope) []byte {
514 rhost, _, err := net.SplitHostPort(conn.remoteAddr.String())
515 if err != nil {
516 rhost = conn.remoteAddr.String()
517 }
518
519 rhosts, err := net.LookupAddr(rhost)
520 if err == nil {
521 rhost = fmt.Sprintf("%s [%s]", rhosts[0], rhost)
522 }
523
524 base := fmt.Sprintf("Received: from %s (%s)\r\n ", conn.ehlo, rhost)
525
526 with := "SMTP"
527 if conn.esmtp {
528 with = "E" + with
529 }
530 if conn.tls != nil {
531 with += "S"
532 }
533 base += fmt.Sprintf("by %s (mailpopbox) with %s id %s\r\n ", conn.server.Name(), with, envelope.ID)
534
535 if len(envelope.RcptTo) > 0 {
536 base += fmt.Sprintf("for <%s>\r\n ", envelope.RcptTo[0].Address)
537 }
538
539 transport := conn.getTransportString()
540 date := envelope.Received.Format(time.RFC1123Z) // Same as RFC 5322 ยง 3.3
541 base += fmt.Sprintf("(using %s);\r\n %s\r\n", transport, date)
542
543 return []byte(base)
544 }
545
546 func (conn *connection) getTransportString() string {
547 if conn.tls == nil {
548 return "PLAINTEXT"
549 }
550
551 ciphers := map[uint16]string{
552 tls.TLS_RSA_WITH_RC4_128_SHA: "TLS_RSA_WITH_RC4_128_SHA",
553 tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA: "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
554 tls.TLS_RSA_WITH_AES_128_CBC_SHA: "TLS_RSA_WITH_AES_128_CBC_SHA",
555 tls.TLS_RSA_WITH_AES_256_CBC_SHA: "TLS_RSA_WITH_AES_256_CBC_SHA",
556 tls.TLS_RSA_WITH_AES_128_GCM_SHA256: "TLS_RSA_WITH_AES_128_GCM_SHA256",
557 tls.TLS_RSA_WITH_AES_256_GCM_SHA384: "TLS_RSA_WITH_AES_256_GCM_SHA384",
558 tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA: "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
559 tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
560 tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
561 tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA: "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
562 tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA: "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
563 tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
564 tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
565 tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
566 tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
567 tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
568 tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
569 }
570 versions := map[uint16]string{
571 tls.VersionSSL30: "SSLv3.0",
572 tls.VersionTLS10: "TLSv1.0",
573 tls.VersionTLS11: "TLSv1.1",
574 tls.VersionTLS12: "TLSv1.2",
575 }
576
577 state := conn.tls
578
579 version := versions[state.Version]
580 cipher := ciphers[state.CipherSuite]
581
582 if version == "" {
583 version = fmt.Sprintf("%x", state.Version)
584 }
585 if cipher == "" {
586 cipher = fmt.Sprintf("%x", state.CipherSuite)
587 }
588
589 name := ""
590 if state.ServerName != "" {
591 name = fmt.Sprintf(" name=%s", state.ServerName)
592 }
593
594 return fmt.Sprintf("%s cipher=%s%s", version, cipher, name)
595 }
596
597 func (conn *connection) doRSET() {
598 conn.log.Info("doRSET()")
599 conn.state = stateInitial
600 conn.resetBuffers()
601 conn.reply(ReplyOK)
602 }
603
604 func (conn *connection) resetBuffers() {
605 conn.delivery = deliverUnknown
606 conn.sendAs = nil
607 conn.mailFrom = nil
608 conn.rcptTo = make([]mail.Address, 0)
609 }