2019-04-25 04:52:08 +07:00
package notification
import (
2019-12-21 01:40:01 +07:00
"crypto/tls"
2019-12-30 09:03:51 +07:00
"crypto/x509"
2019-12-21 01:40:01 +07:00
"errors"
2019-04-25 04:52:08 +07:00
"fmt"
2022-07-09 09:40:02 +07:00
"io"
2021-08-10 07:52:41 +07:00
"net"
2022-07-18 07:56:09 +07:00
"net/mail"
2019-04-25 04:52:08 +07:00
"net/smtp"
2022-07-18 07:56:09 +07:00
"os"
2019-12-21 01:40:01 +07:00
"strings"
2020-08-21 09:16:23 +07:00
"time"
2019-04-25 04:52:08 +07:00
2022-07-18 07:56:09 +07:00
"github.com/google/uuid"
2021-11-30 18:15:21 +07:00
"github.com/sirupsen/logrus"
2021-08-11 08:04:35 +07:00
"github.com/authelia/authelia/v4/internal/configuration/schema"
"github.com/authelia/authelia/v4/internal/logging"
2022-07-18 07:56:09 +07:00
"github.com/authelia/authelia/v4/internal/templates"
2021-08-11 08:04:35 +07:00
"github.com/authelia/authelia/v4/internal/utils"
2019-04-25 04:52:08 +07:00
)
2022-07-18 07:56:09 +07:00
// NewSMTPNotifier creates a SMTPNotifier using the notifier configuration.
func NewSMTPNotifier ( config * schema . SMTPNotifierConfiguration , certPool * x509 . CertPool , templateProvider * templates . Provider ) * SMTPNotifier {
notifier := & SMTPNotifier {
config : config ,
tlsConfig : utils . NewTLSConfig ( config . TLS , tls . VersionTLS12 , certPool ) ,
log : logging . Logger ( ) ,
templates : templateProvider ,
}
at := strings . LastIndex ( config . Sender . Address , "@" )
if at >= 0 {
notifier . domain = config . Sender . Address [ at : ]
}
return notifier
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// SMTPNotifier a notifier to send emails to SMTP servers.
2019-04-25 04:52:08 +07:00
type SMTPNotifier struct {
2022-07-18 07:56:09 +07:00
config * schema . SMTPNotifierConfiguration
domain string
tlsConfig * tls . Config
log * logrus . Logger
templates * templates . Provider
client * smtp . Client
2019-04-25 04:52:08 +07:00
}
2022-07-18 07:56:09 +07:00
// Send is used to email a recipient.
func ( n * SMTPNotifier ) Send ( recipient mail . Address , title , body , htmlBody string ) ( err error ) {
if err = n . dial ( ) ; err != nil {
return fmt . Errorf ( fmtSMTPDialError , err )
2019-04-25 04:52:08 +07:00
}
2020-05-06 02:35:32 +07:00
2022-07-18 07:56:09 +07:00
// Always execute QUIT at the end once we're connected.
defer n . cleanup ( )
if err = n . preamble ( recipient ) ; err != nil {
return err
}
// Compose and send the email body to the server.
if err = n . compose ( recipient , title , body , htmlBody ) ; err != nil {
return fmt . Errorf ( fmtSMTPGenericError , smtpCommandDATA , err )
}
n . log . Debug ( "Notifier SMTP client successfully sent email" )
return nil
}
// StartupCheck implements the startup check provider interface.
func ( n * SMTPNotifier ) StartupCheck ( ) ( err error ) {
if err = n . dial ( ) ; err != nil {
return fmt . Errorf ( fmtSMTPDialError , err )
}
// Always execute QUIT at the end once we're connected.
defer n . cleanup ( )
if err = n . preamble ( n . config . StartupCheckAddress ) ; err != nil {
return err
}
return n . client . Reset ( )
}
// preamble performs generic preamble requirements for sending messages via SMTP.
func ( n * SMTPNotifier ) preamble ( recipient mail . Address ) ( err error ) {
if err = n . client . Hello ( n . config . Identifier ) ; err != nil {
return fmt . Errorf ( fmtSMTPGenericError , smtpCommandHELLO , err )
}
if err = n . startTLS ( ) ; err != nil {
return fmt . Errorf ( fmtSMTPGenericError , smtpCommandSTARTTLS , err )
}
if err = n . auth ( ) ; err != nil {
return fmt . Errorf ( fmtSMTPGenericError , smtpCommandAUTH , err )
}
if err = n . client . Mail ( n . config . Sender . Address ) ; err != nil {
return fmt . Errorf ( fmtSMTPGenericError , smtpCommandMAIL , err )
}
if err = n . client . Rcpt ( recipient . Address ) ; err != nil {
return fmt . Errorf ( fmtSMTPGenericError , smtpCommandRCPT , err )
}
return nil
}
// Dial the SMTP server with the SMTPNotifier config.
func ( n * SMTPNotifier ) dial ( ) ( err error ) {
var (
client * smtp . Client
conn net . Conn
dialer = & net . Dialer { Timeout : n . config . Timeout }
)
n . log . Debugf ( "Notifier SMTP client attempting connection to %s:%d" , n . config . Host , n . config . Port )
if n . config . Port == smtpPortSUBMISSIONS {
n . log . Debugf ( "Notifier SMTP client using submissions port 465. Make sure the mail server you are connecting to is configured for submissions and not SMTPS." )
conn , err = tls . DialWithDialer ( dialer , "tcp" , fmt . Sprintf ( "%s:%d" , n . config . Host , n . config . Port ) , n . tlsConfig )
} else {
conn , err = dialer . Dial ( "tcp" , fmt . Sprintf ( "%s:%d" , n . config . Host , n . config . Port ) )
}
switch {
case err == nil :
break
case errors . Is ( err , io . EOF ) :
return fmt . Errorf ( "received %w error: this error often occurs due to network errors such as a firewall, network policies, or closed ports which may be due to smtp service not running or an incorrect port specified in configuration" , err )
default :
return err
}
if client , err = smtp . NewClient ( conn , n . config . Host ) ; err != nil {
return err
}
n . client = client
n . log . Debug ( "Notifier SMTP client connected successfully" )
return nil
2019-04-25 04:52:08 +07:00
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Do startTLS if available (some servers only provide the auth extension after, and encryption is preferred).
2020-04-23 09:01:24 +07:00
func ( n * SMTPNotifier ) startTLS ( ) error {
2022-01-31 12:25:15 +07:00
// Only start if not already encrypted.
2019-12-30 09:03:51 +07:00
if _ , ok := n . client . TLSConnectionState ( ) ; ok {
2021-11-30 18:15:21 +07:00
n . log . Debugf ( "Notifier SMTP connection is already encrypted, skipping STARTTLS" )
2020-04-23 09:01:24 +07:00
return nil
2019-12-30 09:03:51 +07:00
}
2020-05-06 07:52:06 +07:00
switch ok , _ := n . client . Extension ( "STARTTLS" ) ; ok {
case true :
2021-11-30 18:15:21 +07:00
n . log . Debugf ( "Notifier SMTP server supports STARTTLS (disableVerifyCert: %t, ServerName: %s), attempting" , n . tlsConfig . InsecureSkipVerify , n . tlsConfig . ServerName )
2019-12-30 09:03:51 +07:00
2020-04-23 09:01:24 +07:00
if err := n . client . StartTLS ( n . tlsConfig ) ; err != nil {
return err
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2021-11-30 18:15:21 +07:00
n . log . Debug ( "Notifier SMTP STARTTLS completed without error" )
2020-05-06 07:52:06 +07:00
default :
2022-07-18 07:56:09 +07:00
switch n . config . DisableRequireTLS {
2020-05-06 07:52:06 +07:00
case true :
2021-11-30 18:15:21 +07:00
n . log . Warn ( "Notifier SMTP server does not support STARTTLS and SMTP configuration is set to disable the TLS requirement (only useful for unauthenticated emails over plain text)" )
2020-05-06 07:52:06 +07:00
default :
2022-07-18 07:56:09 +07:00
return errors . New ( "server does not support TLS and it is required by default (see documentation if you want to disable this highly recommended requirement)" )
2020-05-06 07:52:06 +07:00
}
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2020-04-23 09:01:24 +07:00
return nil
2019-12-30 09:03:51 +07:00
}
2019-12-21 01:40:01 +07:00
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Attempt Authentication.
2022-07-18 07:56:09 +07:00
func ( n * SMTPNotifier ) auth ( ) ( err error ) {
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Attempt AUTH if password is specified only.
2022-07-18 07:56:09 +07:00
if n . config . Password != "" {
var (
ok bool
m string
)
if _ , ok = n . client . TLSConnectionState ( ) ; ! ok {
return errors . New ( "client does not support authentication over plain text and the connection is currently plain text" )
2019-12-28 09:49:29 +07:00
}
2019-12-21 01:40:01 +07:00
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Check the server supports AUTH, and get the mechanisms.
2022-07-18 07:56:09 +07:00
if ok , m = n . client . Extension ( smtpCommandAUTH ) ; ok {
2020-05-06 02:35:32 +07:00
var auth smtp . Auth
2021-11-30 18:15:21 +07:00
n . log . Debugf ( "Notifier SMTP server supports authentication with the following mechanisms: %s" , m )
2022-07-18 07:56:09 +07:00
2019-12-21 01:40:01 +07:00
mechanisms := strings . Split ( m , " " )
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Adaptively select the AUTH mechanism to use based on what the server advertised.
2022-07-18 07:56:09 +07:00
if utils . IsStringInSlice ( smtpAUTHMechanismPlain , mechanisms ) {
auth = smtp . PlainAuth ( "" , n . config . Username , n . config . Password , n . config . Host )
2020-05-06 02:35:32 +07:00
2021-11-30 18:15:21 +07:00
n . log . Debug ( "Notifier SMTP client attempting AUTH PLAIN with server" )
2022-07-18 07:56:09 +07:00
} else if utils . IsStringInSlice ( smtpAUTHMechanismLogin , mechanisms ) {
auth = newLoginAuth ( n . config . Username , n . config . Password , n . config . Host )
2020-05-06 02:35:32 +07:00
2021-11-30 18:15:21 +07:00
n . log . Debug ( "Notifier SMTP client attempting AUTH LOGIN with server" )
2019-12-21 01:40:01 +07:00
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Throw error since AUTH extension is not supported.
2019-12-21 01:40:01 +07:00
if auth == nil {
2022-07-18 07:56:09 +07:00
return fmt . Errorf ( "server does not advertise an AUTH mechanism that is supported (PLAIN or LOGIN are supported, but server advertised mechanisms '%s')" , m )
2019-12-21 01:40:01 +07:00
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Authenticate.
2022-07-18 07:56:09 +07:00
if err = n . client . Auth ( auth ) ; err != nil {
2020-04-23 09:01:24 +07:00
return err
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2021-11-30 18:15:21 +07:00
n . log . Debug ( "Notifier SMTP client authenticated successfully with the server" )
2020-05-06 02:35:32 +07:00
2020-04-21 12:53:47 +07:00
return nil
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2022-07-18 07:56:09 +07:00
return errors . New ( "server does not advertise the AUTH extension but config requires AUTH (password specified), either disable AUTH, or use an SMTP host that supports AUTH PLAIN or AUTH LOGIN" )
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2021-11-30 18:15:21 +07:00
n . log . Debug ( "Notifier SMTP config has no password specified so authentication is being skipped" )
2020-05-06 02:35:32 +07:00
2020-04-21 12:53:47 +07:00
return nil
2019-12-30 09:03:51 +07:00
}
2019-12-21 01:40:01 +07:00
2022-07-18 07:56:09 +07:00
func ( n * SMTPNotifier ) compose ( recipient mail . Address , title , body , htmlBody string ) ( err error ) {
n . log . Debugf ( "Notifier SMTP client attempting to send email body to %s" , recipient . String ( ) )
2020-05-06 02:35:32 +07:00
2022-07-18 07:56:09 +07:00
if ! n . config . DisableRequireTLS {
2019-12-30 09:03:51 +07:00
_ , ok := n . client . TLSConnectionState ( )
if ! ok {
2022-07-18 07:56:09 +07:00
return errors . New ( "client can't send an email over plain text connection" )
2019-12-30 09:03:51 +07:00
}
}
2020-05-06 02:35:32 +07:00
2022-07-18 07:56:09 +07:00
var (
wc io . WriteCloser
muuid uuid . UUID
)
2019-12-30 09:03:51 +07:00
2022-07-18 07:56:09 +07:00
if wc , err = n . client . Data ( ) ; err != nil {
n . log . Debugf ( "Notifier SMTP client error while obtaining WriteCloser: %v" , err )
2019-04-25 04:52:08 +07:00
return err
}
2022-07-18 07:56:09 +07:00
if muuid , err = uuid . NewRandom ( ) ; err != nil {
2019-04-25 04:52:08 +07:00
return err
}
2020-05-06 02:35:32 +07:00
2022-07-18 07:56:09 +07:00
values := templates . EmailEnvelopeValues {
ProcessID : os . Getpid ( ) ,
UUID : muuid . String ( ) ,
Host : n . config . Host ,
ServerName : n . config . TLS . ServerName ,
SenderDomain : n . domain ,
Identifier : n . config . Identifier ,
From : n . config . Sender . String ( ) ,
To : recipient . String ( ) ,
Subject : strings . ReplaceAll ( n . config . Subject , "{title}" , title ) ,
Date : time . Now ( ) ,
Boundary : utils . RandomString ( 30 , utils . AlphaNumericCharacters , true ) ,
Body : templates . EmailEnvelopeBodyValues {
PlainText : body ,
HTML : htmlBody ,
} ,
2022-07-09 09:40:02 +07:00
}
2022-07-18 07:56:09 +07:00
if err = n . templates . ExecuteEmailEnvelope ( wc , values ) ; err != nil {
n . log . Debugf ( "Notifier SMTP client error while sending email body over WriteCloser: %v" , err )
2022-07-09 09:40:02 +07:00
return err
2021-08-10 07:52:41 +07:00
}
2020-05-06 02:35:32 +07:00
2022-07-18 07:56:09 +07:00
if err = wc . Close ( ) ; err != nil {
n . log . Debugf ( "Notifier SMTP client error while closing the WriteCloser: %v" , err )
2021-08-10 07:52:41 +07:00
return err
2019-04-25 04:52:08 +07:00
}
2020-05-06 02:35:32 +07:00
2019-12-30 09:03:51 +07:00
return nil
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Closes the connection properly.
2020-01-28 11:00:43 +07:00
func ( n * SMTPNotifier ) cleanup ( ) {
2022-07-18 07:56:09 +07:00
if err := n . client . Quit ( ) ; err != nil {
n . log . Warnf ( "Notifier SMTP client encountered error during cleanup: %v" , err )
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
}
2022-07-18 07:56:09 +07:00
n . client = nil
2019-04-25 04:52:08 +07:00
}