-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession.go
350 lines (303 loc) · 8.46 KB
/
session.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
package main
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/mpapi/failmail/parse"
"log"
"net/mail"
"regexp"
"strings"
)
type SessionSecurity int
const (
UNENCRYPTED SessionSecurity = iota
TLS_PRE_STARTTLS
TLS_POST_STARTTLS
SSL
)
func (s SessionSecurity) IsEncrypted() bool {
return s == TLS_POST_STARTTLS || s == SSL
}
func (s SessionSecurity) AllowStarttls() bool {
return s == TLS_PRE_STARTTLS
}
type AuthState int
const (
NOT_PERMITTED AuthState = iota
REQUIRED
AUTHENTICATED
)
var pattern = regexp.MustCompile(`\d+`)
type Response struct {
Code int
Text string
}
func (r Response) IsClose() bool {
return r.Code == 221
}
func (r Response) NeedsAuthResponse() bool {
return r.Code == 334
}
func (r Response) NeedsData() bool {
return r.Code == 354
}
func (r Response) StartsTLS() bool {
return r.Text == "Ready to switch to TLS"
}
func (r Response) WriteTo(writer stringWriter) error {
text := strings.TrimSpace(r.Text)
lines := strings.Split(text, "\r\n")
if len(lines) > 1 {
for index, line := range lines {
if index < len(lines)-1 {
if _, err := writer.WriteString(fmt.Sprintf("%d-%s\r\n", r.Code, line)); err != nil {
return err
}
} else {
if _, err := writer.WriteString(fmt.Sprintf("%d %s\r\n", r.Code, line)); err != nil {
return err
}
}
}
} else if _, err := writer.WriteString(fmt.Sprintf("%d %s\r\n", r.Code, r.Text)); err != nil {
return err
}
return writer.Flush()
}
type stringReader interface {
ReadString(delim byte) (string, error)
}
type debugReader struct {
Reader stringReader
Prefix string
}
func (r *debugReader) ReadString(delim byte) (string, error) {
result, err := r.Reader.ReadString(delim)
log.Printf("%s<<< %#v %v", r.Prefix, result, err)
return result, err
}
type stringWriter interface {
WriteString(string) (int, error)
Flush() error
}
type debugWriter struct {
Writer stringWriter
Prefix string
}
func (w *debugWriter) WriteString(str string) (int, error) {
log.Printf("%s>>> %#v", w.Prefix, str)
return w.Writer.WriteString(str)
}
func (w *debugWriter) Flush() error {
log.Printf("%s>>> (FLUSH)", w.Prefix)
return w.Writer.Flush()
}
type Auth interface {
ValidCredentials(string) (bool, error)
IsPermitted(SessionSecurity) bool
}
type SingleUserPlainAuth struct {
Username string
Password string
allowUnencryptedAuth bool
}
func (a *SingleUserPlainAuth) IsPermitted(security SessionSecurity) bool {
return security.IsEncrypted() || a.allowUnencryptedAuth
}
func (a *SingleUserPlainAuth) ValidCredentials(token string) (bool, error) {
parts := strings.Split(token, "\x00")
if len(parts) != 3 {
return false, fmt.Errorf("invalid token")
}
valid := parts[1] == a.Username && parts[2] == a.Password
return valid, nil
}
type Session struct {
Received *ReceivedMessage
hostname string
parser Parser
auth Auth
authState AuthState
security SessionSecurity
}
// Sets up a session and returns the `Response` that should be sent to a
// client immediately after it connects.
func (s *Session) Start(auth Auth, security SessionSecurity) Response {
s.initHostname()
s.parser = SMTPParser()
s.Received = &ReceivedMessage{message: &message{}}
s.auth = auth
if s.auth == nil {
s.authState = NOT_PERMITTED
} else {
s.authState = REQUIRED
}
s.security = security
return Response{220, fmt.Sprintf("%s Hi there", s.hostname)}
}
func (s *Session) initHostname() {
hostname, err := hostGetter()
if err != nil {
hostname = "localhost"
}
s.hostname = hostname
}
func (s *Session) setFrom(from string) Response {
if len(s.Received.From) > 0 || len(s.Received.To) > 0 || len(s.Received.Data) > 0 {
return Response{503, "Command out of sequence"}
}
s.Received.From = from
return Response{250, "OK"}
}
func (s *Session) addTo(to string) Response {
if len(s.Received.From) == 0 || len(s.Received.Data) > 0 {
return Response{503, "Command out of sequence"}
}
s.Received.To = append(s.Received.To, to)
return Response{250, "OK"}
}
func (s *Session) setData(data string) (Response, *ReceivedMessage) {
if len(s.Received.From) == 0 || len(s.Received.To) == 0 || len(s.Received.Data) > 0 {
return Response{503, "Command out of sequence"}, nil
}
buf := bytes.NewBufferString(data)
if msg, err := mail.ReadMessage(buf); err != nil {
return Response{451, "Failed to parse data"}, nil
} else {
received := s.Received
s.Received = &ReceivedMessage{message: &message{}}
received.Data = []byte(data)
received.Parsed = msg
return Response{250, "Got the data"}, received
}
}
// Reads and parses a single command and advances the session accordingly. In
// case of error, returns either a non-nil error (if the command couldn't be
// read from the `reader`) or a `Response` with the appropriate SMTP error code
// (for other error conditions).
func (s *Session) ReadCommand(reader stringReader) (Response, error) {
line, err := reader.ReadString('\n')
if err != nil {
return Response{500, "Parse error"}, err
}
return s.Advance(s.parser(line)), nil
}
// Reads the payload from a DATA command -- up to and including the "." on a
// newline by itself.
func (s *Session) ReadData(reader stringReader) (Response, *ReceivedMessage) {
data := new(bytes.Buffer)
for {
line, err := reader.ReadString('\n')
if err != nil {
return Response{451, "Failed to read data"}, nil
}
if line == ".\r\n" {
break
} else {
data.WriteString(line)
}
}
return s.setData(data.String())
}
func (s *Session) ReadAuthResponse(reader stringReader) Response {
line, err := reader.ReadString('\n')
if err != nil {
return Response{500, "Parse error"}
}
return s.checkCredentials(line)
}
func (s *Session) authRequired(command *parse.Node) bool {
switch strings.ToLower(command.Text) {
case "quit", "helo", "ehlo", "rset", "noop", "auth", "starttls":
return false
}
return s.authState == REQUIRED
}
func (s *Session) authenticate(method string, payload string) Response {
switch {
case method != "PLAIN":
return Response{504, "Unrecognized authentication type"}
case payload == "":
return Response{334, ""}
default:
return s.checkCredentials(payload)
}
}
func (s *Session) checkCredentials(payload string) Response {
data, err := base64.StdEncoding.DecodeString(payload)
if err != nil {
return Response{501, "Error decoding credentials"}
}
valid, err := s.auth.ValidCredentials(string(data))
if err != nil {
return Response{501, "Error validating credentials"}
}
if !valid {
return Response{535, "Authentication failed"}
} else {
s.authState = AUTHENTICATED
return Response{235, "Authentication successful"}
}
}
// Advances the state of the session according to the parsed SMTP command, and
// returns an appropriate `Response`. For example, the MAIL command modifies
// the session to store the sender's address and to expect future commands to
// specify the recipients and body of the message.
func (s *Session) Advance(node *parse.Node) Response {
if node == nil {
return Response{500, "Parse error"}
}
command, ok := node.Get("command")
if !ok {
return Response{500, "Parse error"}
}
if s.authRequired(command) {
return Response{530, "Authentication required"}
}
switch strings.ToLower(command.Text) {
case "quit":
return Response{221, fmt.Sprintf("%s See ya", s.hostname)}
case "helo":
return Response{250, "Hello"}
case "ehlo":
text := "Hello\r\nAUTH PLAIN"
if s.security.AllowStarttls() {
text += "\r\nSTARTTLS"
}
return Response{250, text}
case "noop":
return Response{250, "Noop"}
case "rcpt":
return s.addTo(node.Children["path"].Text)
case "mail":
return s.setFrom(node.Children["path"].Text)
case "vrfy":
return Response{252, "Maybe"}
case "data":
return Response{354, "Go"}
case "auth":
if s.authState == REQUIRED && !s.auth.IsPermitted(s.security) {
return Response{502, "An encrypted connection is required for authentication"}
} else if s.authState == AUTHENTICATED {
return Response{503, "Already authenticated"}
} else if s.authState == NOT_PERMITTED {
return Response{502, "Authentication is not supported"}
}
authType := node.Children["type"].Text
if payload, ok := node.Get("payload"); ok {
return s.authenticate(authType, payload.Text)
} else {
return s.authenticate(authType, "")
}
case "starttls":
if s.security == TLS_POST_STARTTLS {
return Response{500, "Already using TLS"}
} else if !s.security.AllowStarttls() {
return Response{500, "STARTTLS not supported"}
}
return Response{220, "Ready to switch to TLS"}
default:
return Response{502, "Not implemented"}
}
}