-
Notifications
You must be signed in to change notification settings - Fork 5
/
conn.go
354 lines (314 loc) · 8.62 KB
/
conn.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
351
352
353
354
//Package smtpproxy is based heavily on https://github.com/emersion/go-smtp, with increased transparency of response codes and no sasl dependency.
package smtpproxy
import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net"
"net/textproto"
"runtime/debug"
"strings"
"sync"
"time"
)
// ConnectionState gives useful info about the incoming connection, including the TLS status
type ConnectionState struct {
Hostname string
LocalAddr net.Addr
RemoteAddr net.Addr
TLS tls.ConnectionState
}
// Conn is the incoming connection
type Conn struct {
conn net.Conn
text *textproto.Conn
server *Server
helo string
nbrErrors int
session Session
locker sync.Mutex
}
func newConn(c net.Conn, s *Server) *Conn {
sc := &Conn{
server: s,
conn: c,
}
sc.init()
return sc
}
func (c *Conn) init() {
var rwc io.ReadWriteCloser = c.conn
if c.server.Debug != nil {
rwc = struct {
io.Reader
io.Writer
io.Closer
}{
io.TeeReader(c.conn, c.server.Debug),
io.MultiWriter(c.conn, c.server.Debug),
c.conn,
}
}
c.text = textproto.NewConn(rwc)
}
// Commands are dispatched to the appropriate handler functions.
func (c *Conn) handle(cmd string, arg string) {
// If panic happens during command handling - send 421 response
// and close connection.
defer func() {
if err := recover(); err != nil {
c.WriteResponse(421, EnhancedCode{4, 0, 0}, "Internal server error")
c.Close()
stack := debug.Stack()
c.server.ErrorLog.Printf("panic serving %v: %v\n%s", c.State().RemoteAddr, err, stack)
}
}()
if cmd == "" {
c.WriteResponse(500, EnhancedCode{5, 5, 2}, "Speak up")
return
}
cmd = strings.ToUpper(cmd)
switch cmd {
case "HELO", "EHLO":
c.handleHelo(cmd, arg) // Pass in cmd as could be either
case "AUTH":
c.handleAuth(arg)
case "MAIL":
c.handleMail(arg)
case "RCPT":
c.handleRcpt(arg)
case "RSET": // Reset session
c.handleReset()
case "DATA":
c.handleData(arg)
case "STARTTLS":
c.handleStartTLS()
case "QUIT":
c.handleQuit()
c.Close()
default:
c.handleUnknown(cmd, arg) // Rather than rejecting this here, use the upstream server's responses
}
}
// Server name of this connection
func (c *Conn) Server() *Server {
return c.server
}
// Session associated with this connection
func (c *Conn) Session() Session {
c.locker.Lock()
defer c.locker.Unlock()
return c.session
}
// SetSession - setting the user resets any message being generated
func (c *Conn) SetSession(session Session) {
c.locker.Lock()
defer c.locker.Unlock()
c.session = session
}
// Close this connection
func (c *Conn) Close() error {
return c.conn.Close()
}
// TLSConnectionState returns the connection's TLS connection state.
// Zero values are returned if the connection doesn't use TLS.
func (c *Conn) TLSConnectionState() (state tls.ConnectionState, ok bool) {
tc, ok := c.conn.(*tls.Conn)
if !ok {
return
}
return tc.ConnectionState(), true
}
// State of this connection
func (c *Conn) State() ConnectionState {
state := ConnectionState{}
tlsState, ok := c.TLSConnectionState()
if ok {
state.TLS = tlsState
}
state.Hostname = c.helo
state.LocalAddr = c.conn.LocalAddr()
state.RemoteAddr = c.conn.RemoteAddr()
return state
}
func code2xxSuccess(code int) bool {
return (code >= 200) && (code <= 299)
}
func code3xxIntermediate(code int) bool {
return (code >= 300) && (code <= 399)
}
func code5xxPermFail(code int) bool {
return (code >= 500) && (code <= 559)
}
// Change the downstream (client) connection, and upstream connection (via backend) to TLS
func (c *Conn) handleStartTLS() {
if _, isTLS := c.TLSConnectionState(); isTLS {
c.WriteResponse(502, EnhancedCode{5, 5, 1}, "Already running in TLS")
return
}
// Change upstream to TLS. If this fails, don't continue with the downstream change
code, msg, err := c.Session().StartTLS()
c.WriteResponse(code, NoEnhancedCode, msg)
if err != nil {
return
}
// Change downstream to TLS
var tlsConn *tls.Conn
tlsConn = tls.Server(c.conn, c.server.TLSConfig)
if err := tlsConn.Handshake(); err != nil {
c.WriteResponse(550, EnhancedCode{5, 0, 0}, "Handshake error")
}
c.conn = tlsConn
c.init()
}
// WriteResponse back to the incoming connection.
// If you do not want an enhanced code added, pass in value NoEnhancedCode.
func (c *Conn) WriteResponse(code int, enhCode EnhancedCode, text ...string) {
// TODO: error handling
if c.server.WriteTimeout != 0 {
c.conn.SetWriteDeadline(time.Now().Add(c.server.WriteTimeout))
}
// All responses must include an enhanced code, if it is missing - use
// a generic code X.0.0.
if enhCode == EnhancedCodeNotSet {
cat := code / 100
switch cat {
case 2, 4, 5:
enhCode = EnhancedCode{cat, 0, 0}
default:
enhCode = NoEnhancedCode
}
}
for i := 0; i < len(text)-1; i++ {
c.text.PrintfLine("%v-%v", code, text[i])
}
if enhCode == NoEnhancedCode {
c.text.PrintfLine("%v %v", code, text[len(text)-1])
} else {
c.text.PrintfLine("%v %v.%v.%v %v", code, enhCode[0], enhCode[1], enhCode[2], text[len(text)-1])
}
}
// ReadLine reads a line of input from the incoming connection
func (c *Conn) ReadLine() (string, error) {
if c.server.ReadTimeout != 0 {
if err := c.conn.SetReadDeadline(time.Now().Add(c.server.ReadTimeout)); err != nil {
return "", err
}
}
return c.text.ReadLine()
}
func (c *Conn) greet() {
c.WriteResponse(220, NoEnhancedCode, fmt.Sprintf("%v ESMTP Service Ready", c.server.Domain))
}
//-----------------------------------------------------------------------------
// Transparent incoming command handlers
//-----------------------------------------------------------------------------
// handleHelo - HELO / EHLO received
func (c *Conn) handleHelo(cmd, arg string) {
domain, err := parseHelloArgument(arg)
if err != nil {
c.WriteResponse(501, EnhancedCode{5, 5, 2}, "Domain/address argument required")
return
}
c.helo = domain
// If no existing session, establish one
if c.Session() == nil {
s, err := c.server.Backend.Init()
if err != nil {
c.WriteResponse(421, EnhancedCode{4, 0, 0}, "Internal server error")
return
}
c.session = s
}
// Pass greeting to the backend, updating our server capabilities to mirror them
upstreamCaps, code, msg, err := c.Session().Greet(cmd)
if err != nil {
c.WriteResponse(code, EnhancedCode{4, 0, 0}, msg)
return
}
if len(upstreamCaps) > 0 {
c.server.caps = []string{}
for _, i := range upstreamCaps {
if i == "STARTTLS" {
// Offer STARTTLS to the downstream client, but only if our TLS is configured
// and downstream not already in TLS
if _, isTLS := c.TLSConnectionState(); c.server.TLSConfig == nil || isTLS {
continue
}
}
c.server.caps = append(c.server.caps, i)
}
}
if cmd == "HELO" {
c.WriteResponse(250, EnhancedCode{2, 0, 0}, fmt.Sprintf("Hello %s", domain))
}
args := []string{"Hello " + domain}
args = append(args, c.server.caps...)
c.WriteResponse(250, NoEnhancedCode, args...)
}
func (c *Conn) handleAuth(arg string) {
if s := c.Session(); s != nil {
c.handlePassthru("AUTH", arg, s.Auth)
}
}
func (c *Conn) handleMail(arg string) {
if s := c.Session(); s != nil {
c.handlePassthru("MAIL", arg, s.Mail)
}
}
func (c *Conn) handleRcpt(arg string) {
if s := c.Session(); s != nil {
c.handlePassthru("RCPT", arg, s.Rcpt)
}
}
func (c *Conn) handleReset() {
if s := c.Session(); s != nil {
c.handlePassthru("RSET", "", s.Reset)
}
}
func (c *Conn) handleQuit() {
if s := c.Session(); s != nil {
c.handlePassthru("QUIT", "", s.Quit)
}
}
func (c *Conn) handleUnknown(cmd, arg string) {
if s := c.Session(); s != nil {
c.handlePassthru(cmd, arg, s.Unknown)
}
}
// handlePassthru - pass the command and args through to the specified backend session function, handling responses transparently until success or permanent failure.
func (c *Conn) handlePassthru(cmd, arg string, fn SessionFunc) {
code, msg, err := fn(0, cmd, arg)
c.WriteResponse(code, NoEnhancedCode, msg)
if err != nil {
return
}
// If we have an intermediate response, need to keep going
if code3xxIntermediate(code) {
for {
encoded, err := c.ReadLine()
if err != nil {
return
}
code, msg, err := fn(0, encoded, "")
c.WriteResponse(code, NoEnhancedCode, msg)
if code2xxSuccess(code) || code5xxPermFail(code) || code == 0 {
return
}
}
}
}
// handleData
func (c *Conn) handleData(arg string) {
w, code, msg, err := c.Session().DataCommand()
// Enhanced code is at the beginning of msg, no need to add anything
c.WriteResponse(code, NoEnhancedCode, msg)
if err != nil {
return
}
r := newDataReader(c)
code, msg, err = c.Session().Data(r, w)
io.Copy(ioutil.Discard, r) // Make sure all the incoming data has been consumed
c.WriteResponse(code, NoEnhancedCode, msg)
}