mirror of
https://github.com/decke/smtprelay.git
synced 2025-12-25 07:43:06 -07:00
Move SMTP auth setup to ConfigLoad()
This has several benefits: - Configuration errors are caught at startup rather than upon a connection - mailHandler() has less work to do for each connection Rather than relying on remote_user and remote_pass to control whether authentication is used, introduce an explicit "none" type for remote_auth, and make that the default. (This is effectively the same default behavior since remote_user and remote_pass default to empty.) Also, we are in a better position to more thoroughly check for configuration errors or mismatches: - If remote_auth is given, remote_user and remote_pass are required. - If remote_auth is given, remote_host is also required (because it makes no sense to say we're going to authenticate if we have no server to which to authenticate.) - If remote_user or remote_pass are given, remote_auth cannot be "none".
This commit is contained in:
17
main.go
17
main.go
@@ -4,7 +4,6 @@ import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/smtp"
|
||||
"net/textproto"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -170,20 +169,6 @@ func mailHandler(peer smtpd.Peer, env smtpd.Envelope) error {
|
||||
|
||||
logger.Info("delivering mail from peer using smarthost")
|
||||
|
||||
var auth smtp.Auth
|
||||
host, _, _ := net.SplitHostPort(*remoteHost)
|
||||
|
||||
if *remoteUser != "" && *remotePass != "" {
|
||||
switch *remoteAuth {
|
||||
case "plain":
|
||||
auth = smtp.PlainAuth("", *remoteUser, *remotePass, host)
|
||||
case "login":
|
||||
auth = LoginAuth(*remoteUser, *remotePass)
|
||||
default:
|
||||
return smtpd.Error{Code: 530, Message: "Authentication method not supported"}
|
||||
}
|
||||
}
|
||||
|
||||
env.AddReceivedLine(peer)
|
||||
|
||||
var sender string
|
||||
@@ -196,7 +181,7 @@ func mailHandler(peer smtpd.Peer, env smtpd.Envelope) error {
|
||||
|
||||
err := SendMail(
|
||||
*remoteHost,
|
||||
auth,
|
||||
remoteAuth,
|
||||
sender,
|
||||
env.Recipients,
|
||||
env.Data,
|
||||
|
||||
Reference in New Issue
Block a user