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

fix use SMTP auth when port 25 is ban problem #16104

Closed
wants to merge 1 commit into from

Conversation

jnan88
Copy link

@jnan88 jnan88 commented Jun 8, 2021

Solve the authentication timeout problem caused by the unavailability of port 25 for SMTP authentication. When using TLS mode, use TLS connection for SMTP communication

Copy link
Member

@techknowlogick techknowlogick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what problem this PR is trying to solve. It is already possible to connect to alternative SMTP ports, also using TLS.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 13, 2021
@jnan88
Copy link
Author

jnan88 commented Jun 15, 2021

I'm not sure what problem this PR is trying to solve. It is already possible to connect to alternative SMTP ports, also using TLS.

The current code can configure TLS, but now it is based on the support of 25 to jump to use TLS. When 25 is not available, an unlinkable exception will be generated here. If TLS is confirmed in this modification, TLS will be directly used to connect, so as to avoid the abnormal unavailability when port 25 is not available

@@ -604,6 +604,9 @@ var SMTPAuths = []string{SMTPPlain, SMTPLogin}

// SMTPAuth performs an SMTP authentication.
func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
if cfg.TLS {
return SMTPAuthTLS(a,cfg)
}
c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
Copy link
Author

@jnan88 jnan88 Jun 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))

Only non TLS connection are used here

// SMTPAuthTLS SMTP authentication by TLS
func SMTPAuthTLS(a smtp.Auth, cfg *SMTPConfig) error {
addr :=fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
tlsClient, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify:cfg.SkipVerify})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tlsClient, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify:cfg.SkipVerify})
tlsClient, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify:cfg.SkipVerify})

New TLS connection

@@ -648,7 +671,11 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC

var auth smtp.Auth
if cfg.Auth == SMTPPlain {
auth = smtp.PlainAuth("", login, password, cfg.Host)
if cfg.TLS {
auth = smtp.PlainAuth("", login, password, fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auth = smtp.PlainAuth("", login, password, fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
auth = smtp.PlainAuth("", login, password, fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))

When TLS connection is used, ports need to be added at the same time

@zeripath
Copy link
Contributor

zeripath commented Aug 3, 2021

Hi @jnan88 I've finally got round to looking at this - apologies for conflicting your PR.

I now think I understand the problem.

I think we simply have to make a breaking change with the configuration of this source and make it more like the configuration for the mailer.

That is:

  1. If TLS is true or port = 465 use SMTPS
  2. If the SMTP server offers STARTTLS and we're not on TLS : use STARTTLS

To that end I'm going to propose a breaking change PR which will attempt to rationalise the configuration.

zeripath added a commit to zeripath/gitea that referenced this pull request Aug 7, 2021
…/key options

This PR has two parts:

Improvements for SMTP authentication:

* Default to use SMTPS if port is 465, and allow setting of force SMTPS.
* Always use STARTTLS if available
* Provide CRAM-MD5 mechanism
* Add options for HELO hostname disabling
* Add options for providing certificates and keys
* Handle application specific password response as a failed user login
instead of as a 500.

Close go-gitea#16104

Fix creation of new users:

* A bug was introduced when allowing users to change usernames which
prevents the creation of external users.
* The LoginSource refactor also broke this page.

Close go-gitea#16104

Signed-off-by: Andrew Thornton <art27@cantab.net>
zeripath added a commit that referenced this pull request Aug 11, 2021
* Improve SMTP authentication, Fix user creation bugs and add LDAP cert/key options

This PR has two parts:

Improvements for SMTP authentication:

* Default to use SMTPS if port is 465, and allow setting of force SMTPS.
* Always use STARTTLS if available
* Provide CRAM-MD5 mechanism
* Add options for HELO hostname disabling
* Add options for providing certificates and keys
* Handle application specific password response as a failed user login
instead of as a 500.

Close #16104

Fix creation of new users:

* A bug was introduced when allowing users to change usernames which
prevents the creation of external users.
* The LoginSource refactor also broke this page.

Close #16104

Signed-off-by: Andrew Thornton <art27@cantab.net>
@go-gitea go-gitea locked and limited conversation to collaborators Oct 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants