-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.go
496 lines (418 loc) · 13.3 KB
/
server.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
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
package smtpd
import (
"crypto/rand"
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/mail"
"os"
"regexp"
"strings"
"time"
)
// MessageHandler functions handle application of business logic to the inbound message
type MessageHandler func(m *Message) error
// Default values
const (
DefaultReadTimeout = time.Second * 10
DefaultWriteTimeout = time.Second * 10
DefaultMessageSizeMax = 131072
DefaultSessionCommandsMax = 100
)
// Server is an RFC2821/5321 compatible SMTP server
type Server struct {
Name string
TLSConfig *tls.Config
ServerName string
// MaxSize of incoming message objects, zero for no cap otherwise
// larger messages are thrown away
MaxSize int64
// MaxConn limits the number of concurrent connections being handled
MaxConn int
// MaxCommands is the maximum number of commands a server will accept
// from a single client before terminating the session
MaxCommands int
// RateLimiter gets called before proceeding through to message handling
// TODO: Implement
RateLimiter func(*Conn) bool
// Handler is the handoff function for messages
Handler MessageHandler
// Auth is an authentication-handling extension
Auth Extension
// Extensions is a map of server-specific extensions & overrides, by verb
Extensions map[string]Extension
// Disabled features
Disabled map[string]bool
// Server meta
listener *net.Listener
// help message to display in response to a HELP request
Help string
// Logger to print out status info
// TODO: implement better logging with configurable verbosity
Logger *log.Logger
Verbose bool
// Timeout handlers
ReadTimeout time.Duration
WriteTimeout time.Duration
// Ready is a channel that will receive a single `true` when the server has started
Ready chan bool
}
// NewServer creates a server with the default settings
func NewServer(handler func(*Message) error) *Server {
return NewServerWithLogger(handler, log.New(os.Stdout, "smtpd ", 0))
}
// NewServerWithLogger creates a server with a customer logger
func NewServerWithLogger(handler func(*Message) error, logger *log.Logger) *Server {
name, err := os.Hostname()
if err != nil {
name = "localhost"
}
return &Server{
Name: name,
ServerName: name,
MaxSize: DefaultMessageSizeMax,
MaxCommands: DefaultSessionCommandsMax,
Handler: handler,
Extensions: make(map[string]Extension),
Disabled: make(map[string]bool),
Logger: logger,
ReadTimeout: DefaultReadTimeout,
WriteTimeout: DefaultWriteTimeout,
Ready: make(chan bool, 1),
}
}
// Close the server connection
func (s *Server) Close() error {
return (*s.listener).Close()
}
// Greeting is a humanized response to EHLO to precede the list of available commands
func (s *Server) Greeting(conn *Conn) string {
return fmt.Sprintf("Welcome! [%v]", conn.LocalAddr())
}
// Extend the server to handle the supplied verb
func (s *Server) Extend(verb string, extension Extension) error {
if _, ok := s.Extensions[verb]; ok {
return fmt.Errorf("Extension for %v has already been registered", verb)
}
s.Extensions[verb] = extension
return nil
}
// Disable server capabilities
func (s *Server) Disable(verbs ...string) {
for _, verb := range verbs {
s.Disabled[strings.ToUpper(verb)] = true
}
}
// Enable server capabilities that have previously been disabled
func (s *Server) Enable(verbs ...string) {
for _, verb := range verbs {
s.Disabled[strings.ToUpper(verb)] = false
}
}
// UseTLS tries to enable TLS on the server (can also just explicitly set the TLSConfig)
func (s *Server) UseTLS(cert, key string) error {
c, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return fmt.Errorf("Could not load TLS keypair, %v", err)
}
s.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{c},
ClientAuth: tls.VerifyClientCertIfGiven,
Rand: rand.Reader,
ServerName: s.ServerName,
}
return nil
}
// UseAuth assigns the server authentication extension
func (s *Server) UseAuth(auth Extension) {
s.Auth = auth
}
// SetHelp sets a help message
func (s *Server) SetHelp(message string) error {
if len(message) > 100 || strings.TrimSpace(message) == "" {
return fmt.Errorf("Message '%v' is not a valid HELP message. Must be less than 100 characters and non-empty", message)
}
s.Help = message
return nil
}
// ListenAndServe starts listening for SMTP commands at the supplied TCP address
func (s *Server) ListenAndServe(addr string) error {
if s.listener != nil {
return ErrAlreadyRunning
}
// close the Ready channel on exit
defer func() {
close(s.Ready)
}()
// Start listening for SMTP connections
listener, err := net.Listen("tcp", addr)
if err != nil {
s.Logger.Printf("Cannot listen on %v (%v)", addr, err)
return err
}
s.Ready <- true
var clientID int64 = 1
s.listener = &listener
// @TODO maintain a fixed-size connection pool, throw immediate 554s otherwise
// see http://www.greenend.org.uk/rjk/tech/smtpreplies.html
// maybe also pass around a context? https://blog.golang.org/context
for {
conn, err := listener.Accept()
if netErr, ok := err.(*net.OpError); ok && netErr.Timeout() {
// it was a timeout
continue
} else if ok && !netErr.Temporary() {
return netErr
}
if err != nil {
log.Println("Could not handle request:", err)
continue
}
c := &Conn{
Conn: conn,
// TODO: implement ListenAndServeSSL for :465 servers
IsTLS: false,
Errors: []error{},
MaxSize: s.MaxSize,
ReadTimeout: s.ReadTimeout,
WriteTimeout: s.WriteTimeout,
}
c.SetReadDeadline(time.Now().Add(s.ReadTimeout))
c.SetWriteDeadline(time.Now().Add(s.WriteTimeout))
go s.HandleSMTP(c)
clientID++
}
}
// Address retrieves the address of the server
func (s *Server) Address() string {
if s.listener != nil {
return (*s.listener).Addr().String()
}
return ""
}
func (s *Server) handleMessage(m *Message) error {
return s.Handler(m)
}
// HandleSMTP handles a single SMTP request
func (s *Server) HandleSMTP(conn *Conn) error {
defer conn.Close()
conn.WriteSMTP(220, fmt.Sprintf("%v %v", s.Name, time.Now().Format(time.RFC1123Z)))
ReadLoop:
for i := 0; i < s.MaxCommands; i++ {
var verb, args string
var err error
if verb, args, err = conn.ReadSMTP(); err != nil {
s.Logger.Printf("Read error: %v", err)
if err == io.EOF {
// client closed the connection already
break ReadLoop
}
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
// too slow, timeout
break ReadLoop
}
return err
}
if s.Verbose {
s.Logger.Printf("%v %v", verb, args)
}
// Always check for disabled features first
if s.Disabled[verb] {
if verb == "EHLO" {
conn.WriteSMTP(550, "Not implemented")
} else {
conn.WriteSMTP(502, "Command not implemented")
}
continue
}
// Auth overrides
if s.Auth != nil && conn.User == nil {
switch verb {
case "AUTH", "EHLO", "HELO", "NOOP", "RSET", "QUIT", "STARTTLS":
// these are okay to call without authentication on an Auth-enabled server
case "*":
conn.WriteSMTP(501, "Cancelled")
continue
default:
conn.WriteSMTP(530, "Authentication required")
continue
}
}
// Handle any extensions / overrides before running default logic
if _, ok := s.Extensions[verb]; ok {
err := s.Extensions[verb].Handle(conn, args)
if err != nil {
s.Logger.Printf("Error? %v", err)
}
continue
}
switch verb {
// https://tools.ietf.org/html/rfc2821#section-4.1.1.1
case "HELO":
conn.WriteSMTP(250, fmt.Sprintf("%v Hello", s.ServerName))
case "EHLO":
// see: https://tools.ietf.org/html/rfc2821#section-4.1.4
conn.Reset()
conn.WriteEHLO(fmt.Sprintf("%v %v", s.ServerName, s.Greeting(conn)))
conn.WriteEHLO(fmt.Sprintf("SIZE %v", s.MaxSize))
if !conn.IsTLS && s.TLSConfig != nil {
conn.WriteEHLO("STARTTLS")
}
if conn.User == nil && s.Auth != nil {
conn.WriteEHLO(fmt.Sprintf("AUTH %v", s.Auth.EHLO()))
}
for verb, extension := range s.Extensions {
conn.WriteEHLO(fmt.Sprintf("%v %v", verb, extension.EHLO()))
}
conn.WriteSMTP(250, "HELP")
// The MAIL command starts off a new mail transaction
// see: https://tools.ietf.org/html/rfc2821#section-4.1.1.2
// This doesn't implement the RFC4594 addition of an AUTH param to the MAIL command
// see: http://tools.ietf.org/html/rfc4954#section-3 for details
case "MAIL":
if from, err := s.GetAddressArg("FROM", args); err == nil {
if conn.User == nil || conn.User.IsUser(from.Address) {
if err := conn.StartTX(from); err == nil {
conn.WriteSMTP(250, "Accepted")
} else {
conn.WriteSMTP(501, err.Error())
}
} else {
conn.WriteSMTP(501, fmt.Sprintf("Cannot send mail as %v", from))
}
} else {
conn.WriteSMTP(501, err.Error())
}
// https://tools.ietf.org/html/rfc2821#section-4.1.1.3
case "RCPT":
// TODO: bubble these up to the message,
if to, err := s.GetAddressArg("TO", args); err == nil {
conn.ToAddr = append(conn.ToAddr, to)
conn.WriteSMTP(250, "Accepted")
} else {
conn.WriteSMTP(501, err.Error())
}
// https://tools.ietf.org/html/rfc2821#section-4.1.1.4
case "DATA":
conn.WriteSMTP(354, "Enter message, ending with \".\" on a line by itself")
if data, err := conn.ReadData(); err == nil {
if message, err := NewMessage([]byte(data), conn.ToAddr, s.Logger); err == nil && (conn.EndTX() == nil) {
if err := s.handleMessage(message); err == nil {
conn.WriteSMTP(250, fmt.Sprintf("OK : queued as %v", message.ID()))
} else if serr, ok := err.(SMTPError); ok {
conn.WriteSMTP(serr.Code, serr.Error())
} else {
conn.WriteSMTP(554, fmt.Sprintf("Server error while processing SMTP message. %v", err))
}
} else {
conn.WriteSMTP(554, fmt.Sprintf("Error while reading SMTP message data. %v", err))
}
} else {
s.Logger.Printf("DATA read error: %v", err)
}
// Reset the connection
// see: https://tools.ietf.org/html/rfc2821#section-4.1.1.5
case "RSET":
conn.Reset()
conn.WriteOK()
// Since this is a commonly abused SPAM aid, it's better to just
// default to 252 (apparent validity / could not verify). If this is not a concern, then
// the full `params` value will be the address to verify, respond with `conn.WriteOK()`
// see: https://tools.ietf.org/html/rfc2821#section-4.1.1.6
case "VRFY":
conn.WriteSMTP(252, "But it was worth a shot, right?")
// see: https://tools.ietf.org/html/rfc2821#section-4.1.1.7
case "EXPN":
conn.WriteSMTP(252, "Maybe, maybe not")
// see: https://tools.ietf.org/html/rfc2821#section-4.1.1.8
case "HELP":
msg := fmt.Sprintf("contact the owner of %v for more information", s.ServerName)
if s.Help != "" {
msg = s.Help
}
conn.WriteSMTP(214, msg)
// NOOP doesn't do anything. Big surprise
// see: https://tools.ietf.org/html/rfc2821#section-4.1.1.9
case "NOOP":
conn.WriteOK()
// Say goodbye and close the connection
// see: https://tools.ietf.org/html/rfc2821#section-4.1.1.10
case "QUIT":
conn.WriteSMTP(221, "Bye")
break ReadLoop
// https://tools.ietf.org/html/rfc2487
case "STARTTLS":
if s.TLSConfig == nil {
conn.WriteSMTP(454, "TLS is not available on this server")
continue
}
conn.WriteSMTP(220, "Ready to start TLS")
// upgrade to TLS
tlsConn := tls.Server(conn, s.TLSConfig)
if tlsConn == nil {
s.Logger.Printf("Couldn't upgrade to TLS")
break ReadLoop
}
tlsConn.SetDeadline(time.Now().Add(s.WriteTimeout))
if err := tlsConn.Handshake(); err == nil {
conn = &Conn{
Conn: tlsConn,
IsTLS: true,
User: conn.User,
Errors: conn.Errors,
MaxSize: conn.MaxSize,
ReadTimeout: s.ReadTimeout,
WriteTimeout: s.WriteTimeout,
}
} else {
s.Logger.Printf("Could not TLS handshake:%v", err)
break ReadLoop
}
// AUTH uses the configured authentication handler to perform an SMTP-AUTH
// as defined by the ESMTP AUTH extension
// see: http://tools.ietf.org/html/rfc4954
case "AUTH":
if conn.User != nil {
conn.WriteSMTP(503, "You are already authenticated")
} else if s.Auth != nil {
if err := s.Auth.Handle(conn, args); err != nil {
if serr, ok := err.(*SMTPError); ok {
conn.WriteSMTP(serr.Code, serr.Error())
} else {
conn.WriteSMTP(500, "Authentication failed")
}
} else {
conn.WriteSMTP(235, "Authentication succeeded")
}
} else {
conn.WriteSMTP(502, "Command not implemented")
}
default:
conn.WriteSMTP(500, "Syntax error, command unrecognised")
conn.Errors = append(conn.Errors, fmt.Errorf("bad input: %v %v", verb, args))
if len(conn.Errors) > 3 {
conn.WriteSMTP(500, "Too many unrecognized commands")
break ReadLoop
}
}
}
// conn.Close() is handled in a defer
return nil
}
var pathRegex = regexp.MustCompile(`<([^@>]+@[^@>]+)>`)
// GetAddressArg extracts the address value from a supplied SMTP argument
// for handling MAIL FROM:[email protected] and RCPT TO:[email protected]
// XXX: don't like this, feels like a hack
func (s *Server) GetAddressArg(argName string, args string) (*mail.Address, error) {
argSplit := strings.SplitN(args, ":", 2)
if len(argSplit) == 2 && strings.ToUpper(argSplit[0]) == argName {
path := pathRegex.FindString(argSplit[1])
if path == "" {
return nil, fmt.Errorf("couldnt find valid FROM path in %v", argSplit[1])
}
return mail.ParseAddress(path)
}
return nil, fmt.Errorf("Bad arguments")
}