-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_data.go
214 lines (203 loc) · 6.11 KB
/
transaction_data.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
package msmtpd
import (
"bytes"
"fmt"
"io"
"net/mail"
"net/textproto"
"time"
"go.opentelemetry.io/otel/attribute"
)
// All messages MUST have a 'Date' and 'From' header and a message may not
// contain more than one 'Date', 'From', 'Sender', 'Reply-To', 'To', 'Cc', 'Bcc',
// 'Message-Id', 'In-Reply-To', 'References' or 'Subject' header.
// (c) RFC 5322
// uniqueHeaders are headers that should not have duplicates according to RFC 5322
var uniqueHeaders = []string{
"Date",
"From",
"Sender",
"Reply-To",
"To",
"Cc",
"Bcc",
"Message-Id",
"In-Reply-To",
"References",
"Subject",
}
func (t *Transaction) handleDATA(_ command) {
var checkErr error
var deliverErr error
var createdAt time.Time
var from []*mail.Address
if t.HeloName == "" {
t.LogDebug("DATA called without HELO/EHLO!")
t.Hate(missingParameterPenalty)
t.reply(502, "Please introduce yourself first.")
return
}
if !t.Encrypted && t.server.ForceTLS {
t.LogDebug("DATA called without STARTTLS!")
t.Hate(missingParameterPenalty)
t.reply(502, "Please turn on TLS by issuing a STARTTLS command.")
return
}
if t.server.Authenticator != nil && t.Username == "" {
t.LogDebug("DATA called without authentication!")
t.Hate(missingParameterPenalty)
t.reply(530, "Authentication Required.")
return
}
if t.MailFrom.Address == "" {
t.Hate(missingParameterPenalty)
t.LogDebug("DATA called without MAIL FROM!")
t.reply(502, "It seems you haven't called MAIL FROM in order to explain who sends your message.")
return
}
if len(t.RcptTo) == 0 {
t.Hate(missingParameterPenalty)
t.LogDebug("DATA called without RCPT TO!")
t.reply(502, "It seems you haven't called RCPT TO in order to explain for whom do you want to deliver your message.")
return
}
t.LogDebug("DATA is called...")
t.reply(354, "Ok, you managed to talk me into accepting your message. Go on, end your data with <CR><LF>.<CR><LF>")
err := t.conn.SetDeadline(time.Now().Add(t.server.DataTimeout))
if err != nil {
t.LogError(err, "while setting deadline for connection")
return
}
data := bytes.NewBufferString("")
reader := textproto.NewReader(t.reader).DotReader()
_, err = io.CopyN(data, reader, int64(t.server.MaxMessageSize))
if err != nil {
if err == io.EOF {
// EOF was reached before MaxMessageSize, so we can accept and deliver message
t.Body = data.Bytes()
if !t.server.HideTransactionHeader {
t.AddHeader("MSMTPD-Transaction-Id", t.ID)
}
t.AddReceivedLine() // will be added as first one
t.LogDebug("Parsing message body with size %v...", data.Len())
t.Span.SetAttributes(attribute.Int("size", data.Len()))
t.Parsed, checkErr = mail.ReadMessage(bytes.NewReader(t.Body))
if checkErr != nil {
t.LogWarn("%s : while parsing message body", checkErr)
t.Hate(tooBigMessagePenalty)
t.error(ErrorSMTP{
Code: 521,
Message: "Stop sending me this nonsense, please!",
})
return
}
// date header is mandatory according to RFC 5322
createdAt, checkErr = t.Parsed.Header.Date()
if checkErr != nil {
t.LogWarn("%s : while parsing message date", checkErr)
t.Hate(malformedMessagePenalty)
t.error(ErrorSMTP{
Code: 521,
Message: "Stop sending me this nonsense, please!",
})
return
}
t.LogInfo("Message created on %s - %s ago",
createdAt.Format(timeFormatForHeaders),
time.Since(createdAt).String(),
)
// from header is mandatory according to RFC 5322
from, checkErr = t.Parsed.Header.AddressList("From")
if checkErr != nil {
t.LogWarn("%s : while parsing message from header %s",
checkErr, t.Parsed.Header.Get("From"),
)
t.Hate(malformedMessagePenalty)
t.error(ErrorSMTP{
Code: 521,
Message: "Stop sending me this nonsense, please!",
})
return
}
if len(from) != 1 {
t.LogWarn("From should contain 1 address")
t.Hate(malformedMessagePenalty)
t.error(ErrorSMTP{
Code: 521,
Message: "Stop sending me this nonsense, please!",
})
return
}
// check for duplicate headers
for _, header := range uniqueHeaders {
parts, found := t.Parsed.Header[header]
if found {
if len(parts) > 1 {
t.LogWarn("Duplicate header %s %v is found",
header, parts,
)
t.error(ErrorSMTP{
Code: 521,
Message: "Stop sending me this nonsense, please!",
})
}
}
}
subject := t.Parsed.Header.Get("Subject")
if subject != "" {
decoded, decodeErr := decodeBase64EncodedSubject(subject)
if decodeErr != nil {
t.LogWarn("%s : while decoding base64 encoded header", decodeErr)
} else {
subject = decoded
t.LogInfo("Subject: %s", subject)
t.Span.SetAttributes(attribute.String("subject", subject))
t.SetFact(SubjectFact, subject)
}
}
t.LogDebug("Message body of %v bytes is parsed, calling %v DataCheckers on it",
data.Len(), len(t.server.DataCheckers))
for j := range t.server.DataCheckers {
checkErr = t.server.DataCheckers[j](t)
if checkErr != nil {
t.error(checkErr)
return
}
}
t.LogInfo("Body (%v bytes) checked by %v DataCheckers successfully!",
data.Len(), len(t.server.DataCheckers))
t.Love(commandExecutedProperly)
t.LogDebug("Starting delivery by %v DataHandlers...", len(t.server.DataHandlers))
for k := range t.server.DataHandlers {
deliverErr = t.server.DataHandlers[k](t)
if deliverErr != nil {
t.error(deliverErr)
return
}
}
if len(t.server.DataHandlers) > 0 {
t.LogInfo("Message delivered by %v DataHandlers...", len(t.server.DataHandlers))
} else {
t.LogWarn("Message silently discarded - no DataHandlers set...")
}
t.reply(250, "Thank you.")
t.Love(commandExecutedProperly)
t.reset()
t.dataHandlersCalledProperly = true
return
}
t.LogError(err, "possible network error while reading message data")
}
// Discard the rest and report an error.
_, err = io.Copy(io.Discard, reader)
if err != nil {
t.LogDebug("possible network error: %s", err)
return
}
t.reply(552, fmt.Sprintf(
"Your message is too big, try to say it in less than %d bytes, please!",
t.server.MaxMessageSize,
))
t.Hate(tooBigMessagePenalty)
t.reset()
}