Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable LMTP over TCP #123

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions server.go
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/emersion/go-sasl"
)

var errTCPAndLMTP = errors.New("smtp: cannot start LMTP server listening on a TCP socket")
var errNoAddressSpecified = errors.New("lmtp: no port was defined and no default port exists for this protocol")
emersion marked this conversation as resolved.
Show resolved Hide resolved

// A function that creates SASL servers.
type SaslServerFactory func(conn *Conn) sasl.Server
Expand All @@ -30,9 +30,13 @@ type Server struct {
Addr string
// The server TLS configuration.
TLSConfig *tls.Config
// Enable LMTP mode, as defined in RFC 2033. LMTP mode cannot be used with a
// TCP listener.
// Enable LMTP mode, as defined in RFC 2033. While it is possible to run LMTP over TCP, it is not advisable to
// run a LMTP server on the public internet, as LMTP was only designed for local communication.
LMTP bool
// The network type this server listens on.
// It can be "tcp" for TCP/IP sockets, or "unix" for UNIX domain sockets.
// If it is blank, it defaults to "tcp" for SMTP servers, and "unix" for LMTP servers, to keep the previous behavior.
Network string

Domain string
MaxRecipients int
Expand Down Expand Up @@ -176,15 +180,20 @@ func (s *Server) handleConn(c *Conn) error {
// to handle requests on incoming connections.
//
// If s.Addr is blank and LMTP is disabled, ":smtp" is used.
// If s.Addr is blank and LMTP is enabled, an error is returned, as there is no default port for LMTP
func (s *Server) ListenAndServe() error {
network := "tcp"
if s.LMTP {
network := s.Network
if !s.LMTP && network == "" {
network = "tcp"
} else if s.LMTP && network == "" {
network = "unix"
}

addr := s.Addr
if !s.LMTP && addr == "" {
addr = ":smtp"
emersion marked this conversation as resolved.
Show resolved Hide resolved
} else if s.LMTP && addr == "" {
return errNoAddressSpecified
}

l, err := net.Listen(network, addr)
Expand All @@ -198,15 +207,14 @@ func (s *Server) ListenAndServe() error {
// ListenAndServeTLS listens on the TCP network address s.Addr and then calls
// Serve to handle requests on incoming TLS connections.
//
// If s.Addr is blank, ":smtps" is used.
// If s.Addr is blank in SMTP mode, ":smtps" is used.
// If s.Addr is blank in LMTP mode, an error is returned, as there is no default port for lmtps
func (s *Server) ListenAndServeTLS() error {
if s.LMTP {
return errTCPAndLMTP
}

addr := s.Addr
if addr == "" {
if !s.LMTP && addr == "" {
addr = ":smtps"
} else if s.LMTP && addr == "" {
return errNoAddressSpecified
}

l, err := tls.Listen("tcp", addr, s.TLSConfig)
emersion marked this conversation as resolved.
Show resolved Hide resolved
Expand Down