From 6f4e38d129ffaf469d40fc8596d3957ee94d21e9 Mon Sep 17 00:00:00 2001 From: AJ Moon Date: Mon, 31 Jul 2017 08:15:01 -0700 Subject: [GH-6366] Add functionality to disable Authentication when connecting to SMTP (#6639) * Issue #6366: Add feature to disable auth for Encrypted connections to SMTP settings. * Clean PLAIN AUTH Option * Reorder SMTP server setup and change helptext * remove unneeded setting and modify logic * text description change --- config/config.json | 3 +- model/config.go | 14 ++++ utils/mail.go | 22 +++--- .../connection_security_dropdown_setting.jsx | 19 ----- webapp/components/admin_console/email_settings.jsx | 81 ++++++++++++++-------- webapp/i18n/en.json | 8 +-- 6 files changed, 80 insertions(+), 67 deletions(-) diff --git a/config/config.json b/config/config.json index 0c7144f1a..316e9de01 100644 --- a/config/config.json +++ b/config/config.json @@ -129,6 +129,7 @@ "FeedbackName": "", "FeedbackEmail": "test@example.com", "FeedbackOrganization": "", + "EnableSMTPAuth": false, "SMTPUsername": "", "SMTPPassword": "", "SMTPServer": "dockerhost", @@ -299,4 +300,4 @@ "RunJobs": true, "RunScheduler": true } -} \ No newline at end of file +} diff --git a/model/config.go b/model/config.go index a1af59185..02af64477 100644 --- a/model/config.go +++ b/model/config.go @@ -262,6 +262,7 @@ type EmailSettings struct { FeedbackName string FeedbackEmail string FeedbackOrganization *string + EnableSMTPAuth *bool SMTPUsername string SMTPPassword string SMTPServer string @@ -785,6 +786,19 @@ func (o *Config) SetDefaults() { *o.EmailSettings.EmailBatchingInterval = EMAIL_BATCHING_INTERVAL } + if o.EmailSettings.EnableSMTPAuth == nil { + o.EmailSettings.EnableSMTPAuth = new(bool) + if o.EmailSettings.ConnectionSecurity == CONN_SECURITY_NONE { + *o.EmailSettings.EnableSMTPAuth = false + } else { + *o.EmailSettings.EnableSMTPAuth = true + } + } + + if o.EmailSettings.ConnectionSecurity == CONN_SECURITY_PLAIN { + o.EmailSettings.ConnectionSecurity = CONN_SECURITY_NONE + } + if o.EmailSettings.SkipServerCertificateVerification == nil { o.EmailSettings.SkipServerCertificateVerification = new(bool) *o.EmailSettings.SkipServerCertificateVerification = false diff --git a/utils/mail.go b/utils/mail.go index 4b3102424..41011d67e 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -6,13 +6,14 @@ package utils import ( "crypto/tls" "fmt" - l4g "github.com/alecthomas/log4go" - "github.com/mattermost/platform/model" "mime" "net" "net/mail" "net/smtp" "time" + + l4g "github.com/alecthomas/log4go" + "github.com/mattermost/platform/model" ) func encodeRFC2047Word(s string) string { @@ -59,22 +60,17 @@ func newSMTPClient(conn net.Conn, config *model.Config) (*smtp.Client, *model.Ap } } - auth := smtp.PlainAuth("", config.EmailSettings.SMTPUsername, config.EmailSettings.SMTPPassword, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort) - if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_TLS { - if err = c.Auth(auth); err != nil { - return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error()) - } - } else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_STARTTLS { + if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_STARTTLS { tlsconfig := &tls.Config{ InsecureSkipVerify: *config.EmailSettings.SkipServerCertificateVerification, ServerName: config.EmailSettings.SMTPServer, } c.StartTLS(tlsconfig) - if err = c.Auth(auth); err != nil { - return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error()) - } - } else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_PLAIN { - // note: go library only supports PLAIN auth over non-tls connections + } + + if *config.EmailSettings.EnableSMTPAuth { + auth := smtp.PlainAuth("", config.EmailSettings.SMTPUsername, config.EmailSettings.SMTPPassword, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort) + if err = c.Auth(auth); err != nil { return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error()) } diff --git a/webapp/components/admin_console/connection_security_dropdown_setting.jsx b/webapp/components/admin_console/connection_security_dropdown_setting.jsx index 3a8e5f7fe..b7b283be1 100644 --- a/webapp/components/admin_console/connection_security_dropdown_setting.jsx +++ b/webapp/components/admin_console/connection_security_dropdown_setting.jsx @@ -26,23 +26,6 @@ const SECTION_NONE = ( ); -const SECTION_PLAIN = ( - - - - - - - - -); - const SECTION_TLS = ( @@ -84,7 +67,6 @@ const CONNECTION_SECURITY_HELP_TEXT_EMAIL = ( > {SECTION_NONE} - {SECTION_PLAIN} {SECTION_TLS} {SECTION_STARTTLS} @@ -122,7 +104,6 @@ export function ConnectionSecurityDropdownSettingEmail(props) { id='connectionSecurity' values={[ {value: '', text: Utils.localizeMessage('admin.connectionSecurityNone', 'None')}, - {value: 'PLAIN', text: Utils.localizeMessage('admin.connectionSecurityPlain')}, {value: 'TLS', text: Utils.localizeMessage('admin.connectionSecurityTls', 'TLS (Recommended)')}, {value: 'STARTTLS', text: Utils.localizeMessage('admin.connectionSecurityStart')} ]} diff --git a/webapp/components/admin_console/email_settings.jsx b/webapp/components/admin_console/email_settings.jsx index ddfe3c38f..d7694ebb6 100644 --- a/webapp/components/admin_console/email_settings.jsx +++ b/webapp/components/admin_console/email_settings.jsx @@ -32,6 +32,7 @@ export default class EmailSettings extends AdminSettings { config.EmailSettings.FeedbackName = this.state.feedbackName; config.EmailSettings.FeedbackEmail = this.state.feedbackEmail; config.EmailSettings.FeedbackOrganization = this.state.feedbackOrganization; + config.EmailSettings.EnableSMTPAuth = this.state.enableSMTPAuth; config.EmailSettings.SMTPUsername = this.state.smtpUsername; config.EmailSettings.SMTPPassword = this.state.smtpPassword; config.EmailSettings.SMTPServer = this.state.smtpServer; @@ -56,6 +57,7 @@ export default class EmailSettings extends AdminSettings { feedbackName: config.EmailSettings.FeedbackName, feedbackEmail: config.EmailSettings.FeedbackEmail, feedbackOrganization: config.EmailSettings.FeedbackOrganization, + enableSMTPAuth: config.EmailSettings.EnableSMTPAuth, smtpUsername: config.EmailSettings.SMTPUsername, smtpPassword: config.EmailSettings.SMTPPassword, smtpServer: config.EmailSettings.SMTPServer, @@ -202,80 +204,99 @@ export default class EmailSettings extends AdminSettings { disabled={!this.state.sendEmailNotifications} /> } - placeholder={Utils.localizeMessage('admin.email.smtpUsernameExample', 'Ex: "admin@yourcompany.com", "AKIADTOVBGERKLCBV"')} + placeholder={Utils.localizeMessage('admin.email.smtpServerExample', 'Ex: "smtp.yourcompany.com", "email-smtp.us-east-1.amazonaws.com"')} helpText={ } - value={this.state.smtpUsername} + value={this.state.smtpServer} onChange={this.handleChange} disabled={!this.state.sendEmailNotifications} /> } - placeholder={Utils.localizeMessage('admin.email.smtpPasswordExample', 'Ex: "yourpassword", "jcuS8PuvcpGhpgHhlcpT1Mx42pnqMxQY"')} + placeholder={Utils.localizeMessage('admin.email.smtpPortExample', 'Ex: "25", "465", "587"')} helpText={ } - value={this.state.smtpPassword} + value={this.state.smtpPort} + onChange={this.handleChange} + disabled={!this.state.sendEmailNotifications} + /> + + } + helpText={[ + + ]} + value={this.state.enableSMTPAuth} onChange={this.handleChange} disabled={!this.state.sendEmailNotifications} /> } - placeholder={Utils.localizeMessage('admin.email.smtpServerExample', 'Ex: "smtp.yourcompany.com", "email-smtp.us-east-1.amazonaws.com"')} + placeholder={Utils.localizeMessage('admin.email.smtpUsernameExample', 'Ex: "admin@yourcompany.com", "AKIADTOVBGERKLCBV"')} helpText={ } - value={this.state.smtpServer} + value={this.state.smtpUsername} onChange={this.handleChange} - disabled={!this.state.sendEmailNotifications} + disabled={!this.state.sendEmailNotifications || !this.state.enableSMTPAuth} /> } - placeholder={Utils.localizeMessage('admin.email.smtpPortExample', 'Ex: "25", "465", "587"')} + placeholder={Utils.localizeMessage('admin.email.smtpPasswordExample', 'Ex: "yourpassword", "jcuS8PuvcpGhpgHhlcpT1Mx42pnqMxQY"')} helpText={ } - value={this.state.smtpPort} + value={this.state.smtpPassword} onChange={this.handleChange} - disabled={!this.state.sendEmailNotifications} + disabled={!this.state.sendEmailNotifications || !this.state.enableSMTPAuth} />