This repository was archived by the owner on Feb 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession.go
More file actions
886 lines (781 loc) · 22.6 KB
/
session.go
File metadata and controls
886 lines (781 loc) · 22.6 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
package gosmtp
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"log"
"net"
"strconv"
"strings"
"time"
"unicode"
)
type sessionState int
const (
sessionStateInit sessionState = iota
sessionStateGotMail
sessionStateGotRcpt
sessionStateReadyForData
sessionStateGettingData
sessionStateDataDone
sessionStateAborted
sessionStateWaitingForQuit
)
// Protocol represents the protocol used in the SMTP session
type Protocol string
const (
SMTP Protocol = "SMTP" // SMTP - plain old SMTP
ESMTP = "ESMTP" // ESMTP - Extended SMTP
)
// Peer represents the client connecting to the server
type Peer struct {
HeloName string
HeloType string
Protocol Protocol
ServerName string
Username string
Authenticated bool
Addr net.Addr
TLS *tls.ConnectionState
AdditionalField map[string]interface{}
}
// session wraps underlying SMTP connection for easier handling
type session struct {
conn net.Conn // connection
bufio *bufio.ReadWriter // buffered input/output
id string // email id
envelope *Envelope // session envelope
state sessionState // session state
badCommandsCount int // amount of bad commands
vrfyCount int // amount of vrfy commands received during current session
start time.Time // start time of the session
bodyType string
peer *Peer
// tls info
tls bool // tls enabled
tlsState tls.ConnectionState
// hello info
helloType int
helloHost string
helloSeen bool
log *log.Logger // logger
srv *Server // serve handling this request
}
// Reset resets current session, happens upon MAIL, EHLO, HELO and RSET
func (s *session) Reset() {
s.envelope.Reset()
s.state = sessionStateInit
}
// DoneAndReset resets current after receiving DATA successfully
func (s *session) ReadLine() (string, error) {
input, err := s.bufio.ReadString('\n')
if err != nil {
return "", err
}
// trim \r\n
return input[:len(input)-2], nil
}
func (s *session) Out(msgs ...string) {
// log
s.log.Printf("INFO: returning msg: '%v'", msgs)
s.conn.SetWriteDeadline(time.Now().Add(DefaultLimits.ReplyOut))
for _, msg := range msgs {
s.bufio.WriteString(msg)
s.bufio.Write([]byte("\r\n"))
}
if err := s.bufio.Flush(); err != nil {
s.log.Printf("ERROR: flush error: %s", err.Error())
s.state = sessionStateAborted
}
}
// Serve - serve given session
// scans command from input and handles given commands accordingly, any failure will result to immediate abort
// of connection
func (s *session) Serve() {
defer s.conn.Close()
// send welcome
s.handleWelcome()
// for each received command
for {
// TODO can we?
if s.badCommandsCount >= s.srv.Limits.BadCmds {
s.Out(Codes.FailMaxUnrecognizedCmd)
s.state = sessionStateAborted
break
}
line, err := s.ReadLine()
if err != nil {
s.log.Printf("ERROR: %s", err.Error())
break
}
cmd, err := parseCommand(strings.TrimRightFunc(line, unicode.IsSpace))
if err != nil {
s.log.Printf("ERROR: unrecognized command: '%s'\n", strings.TrimRightFunc(line, unicode.IsSpace))
s.Out(Codes.FailUnrecognizedCmd)
s.badCommandsCount++
continue
}
if s.state == sessionStateWaitingForQuit && cmd.commandCode != quitCmd {
s.Out(Codes.FailBadSequence)
s.badCommandsCount++
continue
}
s.log.Printf("INFO: received command: '%s'", cmd.String())
// select the right handler by commandCode
handler := handlers[cmd.commandCode]
handler(s, cmd)
if s.state == sessionStateAborted {
break
}
// TODO timeout might differ as per https://tools.ietf.org/html/rfc5321#section-4.5.3.2
s.conn.SetReadDeadline(time.Now().Add(s.srv.Limits.CmdInput))
}
}
// send Welcome upon new session creation
func (s *session) handleWelcome() {
s.Out(fmt.Sprintf("220 %s ESMTP gomstp(0.0.1) I'm mr. Meeseeks, look at me!", s.peer.ServerName))
/*
The SMTP protocol allows a server to formally reject a mail session
while still allowing the initial connection as follows: a 554
response MAY be given in the initial connection opening message
instead of the 220. A server taking this approach MUST still wait
for the client to send a QUIT (see Section 4.1.1.10) before closing
the connection and SHOULD respond to any intervening commands with
"503 bad sequence of commands". Since an attempt to make an SMTP
connection to such a system is probably in error, a server returning
a 554 response on connection opening SHOULD provide enough
information in the reply text to facilitate debugging of the sending
system.
*/
}
// handle Ehlo command
func handleEhlo(s *session, cmd *command) {
s.Reset()
s.helloSeen = true
s.helloType = cmd.commandCode
// TODO chec cmd args
s.helloHost = cmd.arguments[0]
// TODO check sending host (SPF)
if s.srv.HeloChecker != nil {
if err := s.srv.HeloChecker(s.peer, s.helloHost); err != nil {
s.Out("550 " + err.Error())
}
}
ehloResp := make([]string, 0, 10)
ehloResp = append(ehloResp, fmt.Sprintf("250-%v hello %v", "mail.example", s.conn.RemoteAddr()))
// https://tools.ietf.org/html/rfc6152
ehloResp = append(ehloResp, "250-8BITMIME")
// https://tools.ietf.org/html/rfc3030
ehloResp = append(ehloResp, "250-CHUNKING")
ehloResp = append(ehloResp, "250-BINARYMIME")
// https://tools.ietf.org/html/rfc6531
ehloResp = append(ehloResp, "250-SMTPUTF8")
// https://tools.ietf.org/html/rfc2920
ehloResp = append(ehloResp, "250-PIPELINING")
// https://tools.ietf.org/html/rfc6710
ehloResp = append(ehloResp, "250-MT-PRIORITY")
// https://tools.ietf.org/html/rfc3207
if s.srv.TLSConfig != nil { // do tls for this server
if !s.tls { // already in tls stream
ehloResp = append(ehloResp, "250-STARTTLS")
}
}
// https://tools.ietf.org/html/rfc4954
/*
RFC4954 notes: A server implementation MUST
implement a configuration in which it does NOT
permit any plaintext password mechanisms, unless
either the STARTTLS [SMTP-TLS] command has been negotiated...
*/
if len(s.srv.authMechanisms) != 0 && s.srv.TLSConfig != nil {
ehloResp = append(ehloResp, "250-AUTH "+strings.Join(s.srv.authMechanisms, " "))
}
// https://tools.ietf.org/html/rfc821
ehloResp = append(ehloResp, "250-HELP")
// https://tools.ietf.org/html/rfc1870
ehloResp = append(ehloResp, "250 SIZE 35882577") // from gmail TODO
s.Out(ehloResp...)
}
// handle Helo command
func handleHelo(s *session, cmd *command) {
s.Reset()
s.helloSeen = true
s.helloType = cmd.commandCode
// TODO check cmd args
s.helloHost = cmd.arguments[0]
// TODO check sending host (SPF)
if s.srv.HeloChecker != nil {
if err := s.srv.HeloChecker(s.peer, s.helloHost); err != nil {
s.Out("550 " + err.Error())
}
}
s.Out(fmt.Sprintf("250 %v hello %v", "mail.example", s.conn.RemoteAddr()))
}
// start TLS
func handleStartTLS(s *session, cmd *command) {
// already started TLS
if s.tls {
s.badCommandsCount++
s.Out(Codes.FailBadSequence)
return
}
if s.srv.TLSConfig == nil {
s.Out(Codes.FailCmdNotSupported)
return
}
s.Out(Codes.SuccessStartTLSCmd)
// set timeout for TLS connection negotiation
s.conn.SetDeadline(time.Now().Add(DefaultLimits.TLSSetup))
secureConn := tls.Server(s.conn, s.srv.TLSConfig)
// TLS handshake
if err := secureConn.Handshake(); err != nil {
s.log.Printf("ERROR: start tls: '%s'", err.Error())
// TODO should we abort?
s.Out(Codes.FailUndefinedSecurityStatus)
return
}
// reset session
s.Reset()
s.conn = secureConn
s.bufio = bufio.NewReadWriter(
bufio.NewReader(s.conn),
bufio.NewWriter(s.conn),
)
s.tls = true
s.tlsState = secureConn.ConnectionState()
s.peer.TLS = &s.tlsState
s.state = sessionStateInit
}
func handleMail(s *session, cmd *command) {
/*
This command tells the SMTP-receiver that a new mail transaction is
starting and to reset all its state tables and buffers, including any
recipients or mail data.
*/
s.Reset()
if !s.tls && s.srv.TLSOnly {
s.Out(Codes.FailEncryptionNeeded)
return
}
// require authentication if set in settings
if len(s.srv.authMechanisms) != 0 && !s.peer.Authenticated {
s.Out(Codes.FailAccessDenied)
return
}
// nested mail command
if s.envelope.IsSet() {
s.Out(Codes.FailNestedMailCmd)
return
}
args := cmd.arguments
if len(args) == 0 {
s.log.Print("DEBUG: Empty arguments for MAIL cmd")
s.Out(Codes.FailInvalidAddress)
return
}
// to lower and check if start with from: and is not empty
from := strings.ToLower(strings.TrimSpace(args[0]))
if from == "" || !strings.HasPrefix(from, "from:") {
s.log.Print("DEBUG: Invalid address for MAIL cmd")
s.Out(Codes.FailInvalidAddress)
return
}
fromParts := strings.Split(from, ":")
if len(fromParts) < 2 {
s.Out(Codes.FailInvalidAddress)
return
}
mailFrom, err := parseAddress(fromParts[1])
if err != nil {
s.Out(Codes.FailInvalidAddress)
return
}
if s.srv.SenderChecker != nil {
if err := s.srv.SenderChecker(s.peer, mailFrom); err != nil {
s.Out(Codes.FailAccessDenied + " " + err.Error())
return
}
}
s.envelope.MailFrom = mailFrom
args = args[1:]
// extensions size
if len(args) > 0 {
for _, ext := range args {
extValue := strings.Split(ext, "=")
if len(extValue) != 2 {
s.Out(Codes.FailInvalidAddress)
return
}
switch strings.ToUpper(extValue[0]) {
case "SIZE":
size, err := strconv.ParseInt(extValue[1], 10, 64)
if err != nil {
s.Out(Codes.FailInvalidExtension)
return
}
if int64(size) > DefaultLimits.MsgSize {
s.Out(Codes.FailTooBig)
return
}
case "BODY":
// body-value ::= "7BIT" / "8BITMIME" / "BINARYMIME"
s.bodyType = extValue[1]
case "ALT-ADDRESS":
/*
One optional parameter, ALT-ADDRESS, is added to the MAIL and
RCPT commands of SMTP. ALT-ADDRESS specifies an all-ASCII
address which can be used as a substitute for the corresponding
primary (i18mail) address when downgrading.
*/
case "AUTH":
/*
An optional parameter using the keyword "AUTH" is added to the
MAIL FROM command, and extends the maximum line length of the
MAIL FROM command by 500 characters.
*/
case "MT-PRIORITY":
/*
https://tools.ietf.org/html/rfc6710
*/
priority, err := strconv.ParseInt(extValue[1], 10, 64)
if err != nil {
s.Out(Codes.FailInvalidExtension)
return
}
if priority > 9 || priority < -9 {
s.Out(Codes.FailInvalidExtension)
return
}
s.envelope.Priority = int(priority)
default:
s.Out("555")
s.envelope.MailFrom = nil
}
}
}
// validate FQN
if err := IsFQN(s.envelope.MailFrom); err != "" {
s.Out(err)
return
}
switch s.state {
case sessionStateGotRcpt:
s.state = sessionStateReadyForData
case sessionStateInit:
s.state = sessionStateGotMail
default:
s.state = sessionStateAborted
s.Out(Codes.FailBadSequence)
return
}
s.Out(Codes.SuccessMailCmd)
}
func handleRcpt(s *session, cmd *command) {
// if auth is required
if len(s.srv.authMechanisms) != 0 && !s.peer.Authenticated {
s.Out(Codes.FailAccessDenied)
return
}
// HELO/EHLO needs to be first
if !s.helloSeen {
s.Out(Codes.FailBadSequence)
return
}
// check recipients limit
if len(s.envelope.MailTo) > DefaultLimits.MaxRcptCount {
s.Out(Codes.ErrorTooManyRecipients)
return
}
args := cmd.arguments
if args == nil {
s.Out(Codes.FailInvalidRecipient)
}
toParts := strings.Split(args[0], ":")
if len(toParts) < 2 || strings.ToUpper(strings.TrimSpace(toParts[0])) != "TO" {
s.Out(Codes.FailInvalidAddress)
return
}
/*
TODO
Servers MUST be prepared to encounter a list of source
routes in the forward-path, but they SHOULD ignore the routes or MAY
decline to support the relaying they imply.
*/
// must be implemented - RFC5321
if strings.ToLower(toParts[1]) == "<postmaster>" {
toParts[1] = "<postmaster@" + s.peer.ServerName + ">"
}
rcpt, err := parseAddress(toParts[1])
if err != nil {
s.Out(Codes.FailInvalidAddress)
return
}
// check valid recipient if this email comes from outside
err = s.srv.RecipientChecker(s.peer, rcpt)
if err != nil {
if err == ErrorRecipientNotFound {
s.Out(Codes.FailMailboxDoesntExist)
return
}
if err == ErrorRecipientsMailboxFull {
s.Out(Codes.FailMailboxFull)
return
}
s.Out(Codes.FailAccessDenied)
return
}
// extensions size
if len(args) > 0 {
for _, ext := range args {
extValue := strings.Split(ext, "=")
if len(extValue) != 2 {
s.Out(Codes.FailInvalidAddress)
return
}
switch strings.ToUpper(extValue[0]) {
case "RRVS":
// https://tools.ietf.org/html/rfc7293
since, err := time.Parse(time.RFC3339, extValue[1])
s.log.Printf("INFO: client requested Require-Recipient-Valid-Since check: %#v %#v\n", since, err)
default:
s.Out("555")
s.envelope.MailFrom = nil
}
}
}
// Add to recipients
err = s.envelope.AddRecipient(rcpt)
if err != nil {
s.Out(err.Error())
return
}
// Change state
switch s.state {
case sessionStateGotMail:
s.state = sessionStateReadyForData
case sessionStateInit:
s.state = sessionStateGotRcpt
case sessionStateGotRcpt, sessionStateReadyForData:
default:
s.state = sessionStateAborted
s.Out(Codes.FailBadSequence)
return
}
s.Out(Codes.SuccessRcptCmd)
}
func handleVrfy(s *session, _ *command) {
/*
https://tools.ietf.org/html/rfc5336
For the VRFY command, the string is a user name or a user name and
domain (see below). If a normal (i.e., 250) response is returned,
the response MAY include the full name of the user and MUST include
the mailbox of the user. It MUST be in either of the following
forms:
User Name <local-part@domain>
local-part@domain
*/
s.vrfyCount++
s.Out("252 send some mail, i'll try my best")
return
}
func handleData(s *session, cmd *command) {
if s.bodyType == "BINARYMIME" {
/*
https://tools.ietf.org/html/rfc3030
BINARYMIME cannot be used with the DATA command. If a DATA command
is issued after a MAIL command containing the body-value of
"BINARYMIME", a 503 "Bad sequence of commands" response MUST be sent.
The resulting state from this error condition is indeterminate and
the transaction MUST be reset with the RSET command.
*/
s.Out(Codes.FailBadSequence)
s.state = sessionStateAborted
return
}
// envelope is ready for data
if err := s.envelope.BeginData(); err != nil {
s.Out(err.Error())
return
}
// check if we are ready for data
if s.state == sessionStateReadyForData {
s.Out(Codes.SuccessDataCmd)
s.state = sessionStateGettingData
} else {
s.Out(Codes.FailBadSequence)
s.state = sessionStateAborted
return
}
// set data input time limit
s.conn.SetReadDeadline(time.Now().Add(DefaultLimits.MsgInput))
// TODO https://tools.ietf.org/html/rfc5321#section-4.5.3.1.6
// read data, stop on EOF or reaching maximum sizes
var size int64
for size < s.srv.Limits.MsgSize {
line, err := s.bufio.ReadString('\n')
if err != nil {
s.Out(fmt.Sprintf(Codes.FailReadErrorDataCmd, err))
s.state = sessionStateAborted
return
}
line = strings.TrimSpace(line)
if line == "." {
break
}
size += int64(len(line))
s.envelope.WriteString(line)
s.envelope.Write([]byte("\r\n"))
}
// reading ended by reaching maximum size
if size > s.srv.Limits.MsgSize {
s.Out(Codes.FailTooBig)
return
}
// add received header
/*
When forwarding a message into or out of the Internet environment, a
gateway MUST prepend a Received: line, but it MUST NOT alter in any
way a Received: line that is already in the header section.
*/
s.envelope.headers["Received"] = string(s.ReceivedHeader())
// add Message-ID, is user is aut
if s.peer.Authenticated {
s.envelope.headers["Message-ID"] = fmt.Sprintf("Message-ID: <%d.%s@%s>\r\n", time.Now().Unix(), s.id, s.peer.ServerName)
}
// data done
s.envelope.Close()
s.state = sessionStateWaitingForQuit
// add envelope to delivery system
id, err := s.srv.Handler(s.peer, s.envelope)
if err != nil {
s.Out("451 temporary queue error")
} else {
s.Out(fmt.Sprintf("%v %s", Codes.SuccessMessageQueued, id))
}
// reset session
s.Reset()
return
}
// handleRset handle reset commands, reset currents session to beginning and empties the envelope
func handleRset(s *session, _ *command) {
s.envelope.Reset()
s.state = sessionStateInit
s.Out(Codes.SuccessResetCmd)
}
func handleNoop(s *session, _ *command) {
s.Out(Codes.SuccessNoopCmd)
}
func handleQuit(s *session, _ *command) {
s.Out(Codes.SuccessQuitCmd)
s.state = sessionStateAborted
s.log.Printf("INFO: quit remote %s, server in %s", s.peer.Addr, time.Since(s.start))
}
func handleHelp(s *session, _ *command) {
/*
https://tools.ietf.org/html/rfc821
This command causes the receiver to send helpful information
to the sender of the HELP command. The command may take an
argument (e.g., any command name) and return more specific
information as a response.
*/
s.Out(Codes.SuccessHelpCmd + " CaN yOu HelP Me PLeasE!")
}
func handleBdat(s *session, cmd *command) {
args := cmd.arguments
if s.state == sessionStateDataDone {
/*
Any BDAT command sent after the BDAT LAST is illegal and
MUST be replied to with a 503 "Bad sequence of commands" reply code.
The state resulting from this error is indeterminate. A RSET command
MUST be sent to clear the transaction before continuing.
*/
s.Out("503 Bad sequence of commands")
return
}
last := false
if len(args) == 0 {
s.Out(Codes.FailUnrecognizedCmd) // TODO use the right code
return
}
chunkSize64, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
s.Out(Codes.FailUnrecognizedCmd) // TODO use the right code
s.badCommandsCount++
return
}
if (len(args) > 1 && strings.ToUpper(args[1]) == "LAST") || chunkSize64 == 0 {
last = true
}
s.log.Printf("INFO: received BDAT command, last: %t, data length: %d", last, chunkSize64)
/*
The message data is sent immediately after the trailing <CR>
<LF> of the BDAT command line. Once the receiver-SMTP receives the
specified number of octets, it will return a 250 reply code.
If a failure occurs after a BDAT command is
received, the receiver-SMTP MUST accept and discard the associated
message data before sending the appropriate 5XX or 4XX code.
*/
resp := make([]byte, chunkSize64)
if n, err := s.bufio.Read(resp); err != nil {
s.Out(fmt.Sprintf(Codes.FailReadErrorDataCmd, err))
s.state = sessionStateAborted
return
} else if int64(n) != chunkSize64 {
s.Out(fmt.Sprintf(Codes.FailReadErrorDataCmd, err))
s.state = sessionStateAborted
return
}
n, err := s.envelope.Write(resp)
if int64(n) != chunkSize64 {
s.Out(fmt.Sprintf(Codes.FailReadErrorDataCmd, err))
s.state = sessionStateAborted
return
}
if last {
// data done
s.Out(fmt.Sprintf("250 BDAT ok, BDAT finished, %d octets received", s.envelope.data.Len()))
s.envelope.Close()
s.state = sessionStateDataDone
} else {
/*
A 250 response MUST be sent to each successful BDAT data block within
a mail transaction.
*/
s.Out(fmt.Sprintf("250 BDAT ok, %d octets received", chunkSize64))
}
}
func handleExpn(s *session, _ *command) {
s.Out("252")
}
// authMechanismValid checks if selected authentication mechanism is available
func (s *session) authMechanismValid(mech string) bool {
mech = strings.ToUpper(mech)
for _, m := range s.srv.authMechanisms {
if mech == m {
return true
}
}
return false
}
func handleAuth(s *session, cmd *command) {
if !s.tls {
// Don't even allow unsecure authentication
s.Out(Codes.FailEncryptionNeeded)
return
}
// should not happen, some auth is always allowed
if len(s.srv.authMechanisms) == 0 {
s.Out(Codes.FailCmdNotSupported)
// AUTH with no AUTH enabled counts as a
// bad command. This deals with a few people
// who spam AUTH requests at non-supporting
// servers.
s.badCommandsCount++
return
}
// if authenticatedUser is already
if s.peer.Authenticated {
// RFC4954, section 4: After an AUTH
// command has been successfully
// completed, no more AUTH commands
// may be issued in the same session.
s.Out(Codes.FailBadSequence)
return
}
args := cmd.arguments
if len(args) == 0 {
s.Out(Codes.FailMissingArgument)
return
}
if !s.authMechanismValid(strings.ToUpper(args[0])) {
s.Out(Codes.ErrorCmdParamNotImplemented)
return
}
switch strings.ToUpper(args[0]) {
case "PLAIN":
s.handlePlainAuth(cmd)
case "LOGIN":
s.handleLoginAuth(cmd)
default:
s.Out(Codes.ErrorCmdParamNotImplemented)
return
}
}
func (s *session) ReceivedHeader() []byte {
/*
"Received:" header fields of messages originating from other
environments may not conform exactly to this specification. However,
the most important use of Received: lines is for debugging mail
faults, and this debugging can be severely hampered by well-meaning
gateways that try to "fix" a Received: line. As another consequence
of trace header fields arising in non-SMTP environments, receiving
systems MUST NOT reject mail based on the format of a trace header
field and SHOULD be extremely robust in the light of unexpected
information or formats in those header fields.
The gateway SHOULD indicate the environment and protocol in the "via"
clauses of Received header field(s) that it supplies.
*/
remoteIP := strings.Split(s.conn.RemoteAddr().String(), ":")[0]
remotePort := strings.Split(s.conn.RemoteAddr().String(), ":")[1]
remoteHost := "no reverse"
if remoteHosts, err := net.LookupAddr(remoteIP); err == nil {
remoteHost = remoteHosts[0]
}
localIP := strings.Split(s.conn.LocalAddr().String(), ":")[0]
localHost := "no reverse"
localHosts, err := net.LookupAddr(localIP)
if err == nil {
localHost = localHosts[0]
}
receivedHeader := bytes.NewBufferString("Received: from ")
// authenticated
auth := ""
if s.peer.Authenticated {
auth = " authenticated as " + s.peer.Username
}
// host and IP
receivedHeader.WriteString(fmt.Sprintf("%s (%s:%s %s)", remoteHost, remoteIP, remotePort, auth))
// TLS
if s.tls {
receivedHeader.WriteString(tlsInfo(&s.tlsState))
}
// local
receivedHeader.WriteString(fmt.Sprintf(" by %s (%s)", localIP, localHost))
// proto
if s.tls {
receivedHeader.WriteString(" with ESMTPS; ")
} else {
receivedHeader.WriteString(" with SMTP; ")
}
// mamail version
receivedHeader.WriteString("gomstp(0.0.1); id " + s.id)
// timestamp
receivedHeader.WriteString("; " + time.Now().Format(time.RFC1123) + "\r\n")
return wrap(receivedHeader.Bytes())
}
func (s *session) Reject() {
s.Out("421 Too busy. Try again later.")
s.state = sessionStateAborted
return
}
var handlers = []func(s *session, cmd *command){
handleHelo,
handleEhlo,
handleQuit,
handleRset,
handleNoop,
handleMail,
handleRcpt,
handleData,
handleStartTLS,
handleVrfy,
handleExpn,
handleHelp,
handleAuth,
handleBdat,
}
// http://www.rfc-base.org/txt/rfc-4408.txt
func checkHost(ip net.IPAddr, domain string, sender string) bool {
return false
}